433 |
ddelon |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
|
|
|
4 |
/*
|
|
|
5 |
Yes, most of the formatting used in this file is HORRIBLY BAD STYLE. However,
|
|
|
6 |
most of the action happens outside of this file, and I really wanted the code
|
|
|
7 |
to look as small as what it does. Basically. Oh, I just suck. :)
|
|
|
8 |
*/
|
|
|
9 |
|
|
|
10 |
// do not change this line, you fool. In fact, don't change anything! Ever!
|
|
|
11 |
define("WAKKA_VERSION", "0.1.1");
|
|
|
12 |
define("WIKINI_VERSION", "0.3");
|
|
|
13 |
/* Mise en commentaire pour transformation en bibliothèque
|
|
|
14 |
// start the compute time
|
|
|
15 |
list($g_usec, $g_sec) = explode(" ",microtime());
|
|
|
16 |
define ("t_start", (float)$g_usec + (float)$g_sec);
|
|
|
17 |
$t_SQL=0;
|
|
|
18 |
*/
|
|
|
19 |
class Wiki
|
|
|
20 |
{
|
|
|
21 |
var $dblink;
|
|
|
22 |
var $page;
|
|
|
23 |
var $tag;
|
|
|
24 |
var $parameter = array();
|
|
|
25 |
var $queryLog = array();
|
|
|
26 |
var $interWiki = array();
|
|
|
27 |
var $VERSION;
|
|
|
28 |
|
|
|
29 |
// constructor
|
|
|
30 |
function Wiki($config)
|
|
|
31 |
{
|
|
|
32 |
$this->config = $config;
|
|
|
33 |
/* might be a good idea to change this to mysql_connect */
|
|
|
34 |
$this->dblink = mysql_pconnect (
|
|
|
35 |
$this->config["mysql_host"],
|
|
|
36 |
$this->config["mysql_user"],
|
|
|
37 |
$this->config["mysql_password"]);
|
|
|
38 |
mysql_select_db($this->config["mysql_database"], $this->dblink);
|
|
|
39 |
$this->VERSION = WAKKA_VERSION;
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
// DATABASE
|
|
|
43 |
function Query($query)
|
|
|
44 |
{
|
|
|
45 |
if($this->GetConfigValue("debug")) $start = $this->GetMicroTime();
|
|
|
46 |
if (!$result = mysql_query($query, $this->dblink))
|
|
|
47 |
{
|
|
|
48 |
ob_end_clean();
|
|
|
49 |
die("Query failed: ".$query." (".mysql_error().")");
|
|
|
50 |
}
|
|
|
51 |
if($this->GetConfigValue("debug"))
|
|
|
52 |
{
|
|
|
53 |
$time = $this->GetMicroTime() - $start;
|
|
|
54 |
$this->queryLog[] = array(
|
|
|
55 |
"query" => $query,
|
|
|
56 |
"time" => $time);
|
|
|
57 |
}
|
|
|
58 |
return $result;
|
|
|
59 |
}
|
|
|
60 |
function LoadSingle($query) { if ($data = $this->LoadAll($query)) return $data[0]; }
|
|
|
61 |
function LoadAll($query)
|
|
|
62 |
{
|
|
|
63 |
$data=array();
|
|
|
64 |
if ($r = $this->Query($query))
|
|
|
65 |
{
|
|
|
66 |
while ($row = mysql_fetch_assoc($r)) $data[] = $row;
|
|
|
67 |
mysql_free_result($r);
|
|
|
68 |
}
|
|
|
69 |
return $data;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
// MISC
|
|
|
74 |
function GetMicroTime() { list($usec, $sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec); }
|
|
|
75 |
function IncludeBuffered($filename, $notfoundText = "", $vars = "", $path = "")
|
|
|
76 |
{
|
|
|
77 |
if ($path) $dirs = explode(":", $path);
|
|
|
78 |
else $dirs = array("");
|
|
|
79 |
|
|
|
80 |
foreach($dirs as $dir)
|
|
|
81 |
{
|
|
|
82 |
if ($dir) $dir .= "/";
|
|
|
83 |
$fullfilename = $dir.$filename;
|
|
|
84 |
if (file_exists($fullfilename))
|
|
|
85 |
{
|
|
|
86 |
if (is_array($vars)) extract($vars);
|
|
|
87 |
|
|
|
88 |
ob_start();
|
|
|
89 |
include($fullfilename);
|
|
|
90 |
$output = ob_get_contents();
|
|
|
91 |
ob_end_clean();
|
|
|
92 |
return $output;
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
if ($notfoundText) return $notfoundText;
|
|
|
96 |
else return false;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
// VARIABLES
|
|
|
100 |
function GetPageTag() { return $this->tag; }
|
|
|
101 |
function GetPageTime() { return $this->page["time"]; }
|
|
|
102 |
function GetMethod() { return $this->method; }
|
|
|
103 |
function GetConfigValue($name) { return $this->config[$name]; }
|
|
|
104 |
function GetWakkaName() { return $this->GetConfigValue("wakka_name"); }
|
|
|
105 |
function GetWakkaVersion() { return $this->VERSION; }
|
|
|
106 |
function GetWikiNiVersion() { $ver=$this->GetWakkaVersion().'.'.WIKINI_VERSION; return $ver; }
|
|
|
107 |
// PAGES
|
|
|
108 |
function LoadPage($tag, $time = "", $cache = 1) {
|
|
|
109 |
// retrieve from cache
|
|
|
110 |
if (!$time && $cache && ($cachedPage = $this->GetCachedPage($tag))) { $page = $cachedPage;}
|
|
|
111 |
// load page
|
|
|
112 |
if (!isset($page)) $page = $this->LoadSingle("select * from ".$this->config["table_prefix"]."pages where tag = '".mysql_escape_string($tag)."' ".($time ? "and time = '".mysql_escape_string($time)."'" : "and latest = 'Y'")." limit 1");
|
|
|
113 |
// cache result
|
|
|
114 |
if (!$time) $this->CachePage($page);
|
|
|
115 |
return $page;
|
|
|
116 |
}
|
|
|
117 |
function GetCachedPage($tag) {return (isset($this->pageCache[$tag]) ? $this->pageCache[$tag] : ''); }
|
|
|
118 |
function CachePage($page) { $this->pageCache[$page["tag"]] = $page; }
|
|
|
119 |
function SetPage($page) { $this->page = $page; if ($this->page["tag"]) $this->tag = $this->page["tag"]; }
|
|
|
120 |
function LoadPageById($id) { return $this->LoadSingle("select * from ".$this->config["table_prefix"]."pages where id = '".mysql_escape_string($id)."' limit 1"); }
|
|
|
121 |
function LoadRevisions($page) { return $this->LoadAll("select * from ".$this->config["table_prefix"]."pages where tag = '".mysql_escape_string($page)."' order by time desc"); }
|
|
|
122 |
function LoadPagesLinkingTo($tag) { return $this->LoadAll("select from_tag as tag from ".$this->config["table_prefix"]."links where to_tag = '".mysql_escape_string($tag)."' order by tag"); }
|
|
|
123 |
function LoadRecentlyChanged($limit=50)
|
|
|
124 |
{
|
|
|
125 |
$limit= (int) $limit;
|
|
|
126 |
if ($pages = $this->LoadAll("select tag, time, user, owner from ".$this->config["table_prefix"]."pages where latest = 'Y' and comment_on = '' order by time desc limit $limit"))
|
|
|
127 |
{
|
|
|
128 |
foreach ($pages as $page)
|
|
|
129 |
{
|
|
|
130 |
$this->CachePage($page);
|
|
|
131 |
}
|
|
|
132 |
return $pages;
|
|
|
133 |
}
|
|
|
134 |
}
|
|
|
135 |
function LoadAllPages() { return $this->LoadAll("select * from ".$this->config["table_prefix"]."pages where latest = 'Y' order by tag"); }
|
|
|
136 |
function FullTextSearch($phrase) { return $this->LoadAll("select * from ".$this->config["table_prefix"]."pages where latest = 'Y' and match(tag, body) against('".mysql_escape_string($phrase)."')"); }
|
|
|
137 |
function LoadWantedPages() { return $this->LoadAll("select distinct ".$this->config["table_prefix"]."links.to_tag as tag,count(".$this->config["table_prefix"]."links.from_tag) as count from ".$this->config["table_prefix"]."links left join ".$this->config["table_prefix"]."pages on ".$this->config["table_prefix"]."links.to_tag = ".$this->config["table_prefix"]."pages.tag where ".$this->config["table_prefix"]."pages.tag is NULL group by tag order by count desc"); }
|
|
|
138 |
function LoadOrphanedPages() { return $this->LoadAll("select distinct tag from ".$this->config["table_prefix"]."pages left join ".$this->config["table_prefix"]."links on ".$this->config["table_prefix"]."pages.tag = ".$this->config["table_prefix"]."links.to_tag where ".$this->config["table_prefix"]."links.to_tag is NULL and ".$this->config["table_prefix"]."pages.comment_on = '' order by tag"); }
|
|
|
139 |
function IsOrphanedPage($tag) { return $this->LoadAll("select distinct tag from ".$this->config["table_prefix"]."pages left join ".$this->config["table_prefix"]."links on ".$this->config["table_prefix"]."pages.tag = ".$this->config["table_prefix"]."links.to_tag where ".$this->config["table_prefix"]."links.to_tag is NULL and ".$this->config["table_prefix"]."pages.comment_on ='' and tag='".mysql_escape_string($tag)."'"); }
|
|
|
140 |
function DeleteOrphanedPage($tag)
|
|
|
141 |
{
|
|
|
142 |
$this->Query("delete from ".$this->config["table_prefix"]."pages where tag='".mysql_escape_string($tag)."' ");
|
|
|
143 |
$this->Query("delete from ".$this->config["table_prefix"]."links where from_tag='".mysql_escape_string($tag)."' ");
|
|
|
144 |
$this->Query("delete from ".$this->config["table_prefix"]."acls where page_tag='".mysql_escape_string($tag)."' ");
|
|
|
145 |
$this->Query("delete from ".$this->config["table_prefix"]."referrers where page_tag='".mysql_escape_string($tag)."' ");
|
|
|
146 |
}
|
|
|
147 |
function SavePage($tag, $body, $comment_on = "")
|
|
|
148 |
{
|
|
|
149 |
// get current user
|
|
|
150 |
$user = $this->GetUserName();
|
|
|
151 |
|
|
|
152 |
//die($tag);
|
|
|
153 |
|
|
|
154 |
// TODO: check write privilege
|
|
|
155 |
if ($this->HasAccess("write", $tag))
|
|
|
156 |
{
|
|
|
157 |
// is page new?
|
|
|
158 |
if (!$oldPage = $this->LoadPage($tag))
|
|
|
159 |
{
|
|
|
160 |
// create default write acl. store empty write ACL for comments.
|
|
|
161 |
$this->SaveAcl($tag, "write", ($comment_on ? "" : $this->GetConfigValue("default_write_acl")));
|
|
|
162 |
|
|
|
163 |
// create default read acl
|
|
|
164 |
$this->SaveAcl($tag, "read", $this->GetConfigValue("default_read_acl"));
|
|
|
165 |
|
|
|
166 |
// create default comment acl.
|
|
|
167 |
$this->SaveAcl($tag, "comment", $this->GetConfigValue("default_comment_acl"));
|
|
|
168 |
|
|
|
169 |
// current user is owner; if user is logged in! otherwise, no owner.
|
|
|
170 |
if ($this->GetUser()) $owner = $user;
|
|
|
171 |
}
|
|
|
172 |
else
|
|
|
173 |
{
|
|
|
174 |
// aha! page isn't new. keep owner!
|
|
|
175 |
$owner = $oldPage["owner"];
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
|
|
|
179 |
// set all other revisions to old
|
|
|
180 |
$this->Query("update ".$this->config["table_prefix"]."pages set latest = 'N' where tag = '".mysql_Escape_string($tag)."'");
|
|
|
181 |
|
|
|
182 |
// add new revision
|
|
|
183 |
$this->Query("insert into ".$this->config["table_prefix"]."pages set ".
|
|
|
184 |
"tag = '".mysql_escape_string($tag)."', ".
|
|
|
185 |
($comment_on ? "comment_on = '".mysql_escape_string($comment_on)."', " : "").
|
|
|
186 |
"time = now(), ".
|
|
|
187 |
"owner = '".mysql_escape_string($owner)."', ".
|
|
|
188 |
"user = '".mysql_escape_string($user)."', ".
|
|
|
189 |
"latest = 'Y', ".
|
|
|
190 |
"body = '".mysql_escape_string(trim($body))."'");
|
|
|
191 |
}
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
// COOKIES
|
|
|
195 |
function SetSessionCookie($name, $value) { SetCookie($name, $value, 0, "/"); $_COOKIE[$name] = $value; }
|
|
|
196 |
function SetPersistentCookie($name, $value, $remember = 0) { SetCookie($name, $value, time() + ($remember ? 90*24*60*60 : 60 * 60), "/"); $_COOKIE[$name] = $value; }
|
|
|
197 |
function DeleteCookie($name) { SetCookie($name, "", 1, "/"); $_COOKIE[$name] = ""; }
|
|
|
198 |
function GetCookie($name) { return $_COOKIE[$name]; }
|
|
|
199 |
|
|
|
200 |
// HTTP/REQUEST/LINK RELATED
|
|
|
201 |
function SetMessage($message) { $_SESSION["message"] = $message; }
|
|
|
202 |
function GetMessage() { $message = $_SESSION["message"]; $_SESSION["message"] = ""; return $message; }
|
|
|
203 |
function Redirect($url) { header("Location: $url"); exit; }
|
|
|
204 |
// returns just PageName[/method].
|
|
|
205 |
function MiniHref($method = "", $tag = "") { if (!$tag = trim($tag)) $tag = $this->tag; return $tag.($method ? "/".$method : ""); }
|
|
|
206 |
// returns the full url to a page/method.
|
|
|
207 |
function Href($method = "", $tag = "", $params = "")
|
|
|
208 |
{
|
|
|
209 |
$href = $this->config["base_url"].$this->MiniHref($method, $tag);
|
|
|
210 |
if ($params)
|
|
|
211 |
{
|
|
|
212 |
$href .= ($this->config["rewrite_mode"] ? "?" : "&").$params;
|
|
|
213 |
}
|
|
|
214 |
return $href;
|
|
|
215 |
}
|
|
|
216 |
function Link($tag, $method = "", $text = "", $track = 1) {
|
|
|
217 |
$tag=htmlspecialchars($tag);//avoid xss
|
|
|
218 |
$text=htmlspecialchars($text);//paranoiac again
|
|
|
219 |
if (!$text) $text = $tag;
|
|
|
220 |
|
|
|
221 |
// is this an interwiki link?
|
|
|
222 |
if (preg_match("/^([A-Z][A-Z,a-z]+)[:]([A-Z,a-z,0-9]*)$/s", $tag, $matches))
|
|
|
223 |
{
|
|
|
224 |
$tag = $this->GetInterWikiUrl($matches[1], $matches[2]);
|
|
|
225 |
return "<a href=\"$tag\">$text (interwiki)</a>";
|
|
|
226 |
}
|
|
|
227 |
// is this a full link? ie, does it contain alpha-numeric characters?
|
|
|
228 |
else if (preg_match("/[^[:alnum:]]/", $tag))
|
|
|
229 |
{
|
|
|
230 |
// check for email addresses
|
|
|
231 |
if (preg_match("/^.+\@.+$/", $tag))
|
|
|
232 |
{
|
|
|
233 |
$tag = "mailto:".$tag;
|
|
|
234 |
}
|
|
|
235 |
// check for protocol-less URLs
|
|
|
236 |
else if (!preg_match("/:\/\//", $tag))
|
|
|
237 |
{
|
|
|
238 |
$tag = "http://".$tag; //Very important for xss (avoid javascript:() hacking)
|
|
|
239 |
}
|
|
|
240 |
// is this an inline image (text!=tag and url ends png,gif,jpeg)
|
|
|
241 |
if($text!=$tag and preg_match("/.(gif|jpeg|png|jpg)$/i",$tag)){
|
|
|
242 |
return "<img src=\"$tag\" alt=\"$text\" \\>";
|
|
|
243 |
}else{
|
|
|
244 |
return "<a href=\"$tag\">$text</a>";
|
|
|
245 |
}
|
|
|
246 |
}
|
|
|
247 |
else
|
|
|
248 |
{
|
|
|
249 |
// it's a Wiki link!
|
|
|
250 |
if ($_SESSION["linktracking"] && $track) $this->TrackLinkTo($tag);
|
|
|
251 |
return ($this->LoadPage($tag) ? "<a href=\"".$this->href($method, $tag)."\">".$text."</a>" : "<span class=\"missingpage\">".$text."</span><a href=\"".$this->href("edit", $tag)."\">?</a>");
|
|
|
252 |
}
|
|
|
253 |
}
|
|
|
254 |
function ComposeLinkToPage($tag, $method = "", $text = "", $track = 1)
|
|
|
255 |
{
|
|
|
256 |
if (!$text) $text = $tag;
|
|
|
257 |
$text = htmlentities($text);
|
|
|
258 |
if ($_SESSION["linktracking"] && $track)
|
|
|
259 |
$this->TrackLinkTo($tag);
|
|
|
260 |
return '<a href="'.$this->href($method, $tag).'">'.$text.'</a>';
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
// function PregPageLink($matches) { return $this->Link($matches[1]); }
|
|
|
264 |
|
|
|
265 |
|
|
|
266 |
function IsWikiName($text) { return preg_match("/^[A-Z][a-z]+[A-Z,0-9][A-Z,a-z,0-9]*$/", $text); }
|
|
|
267 |
function TrackLinkTo($tag) { $_SESSION["linktable"][] = $tag; }
|
|
|
268 |
function GetLinkTable() { return $_SESSION["linktable"]; }
|
|
|
269 |
function ClearLinkTable() { $_SESSION["linktable"] = array(); }
|
|
|
270 |
function StartLinkTracking() { $_SESSION["linktracking"] = 1; }
|
|
|
271 |
function StopLinkTracking() { $_SESSION["linktracking"] = 0; }
|
|
|
272 |
function WriteLinkTable()
|
|
|
273 |
{
|
|
|
274 |
// delete old link table
|
|
|
275 |
$this->Query("delete from ".$this->config["table_prefix"]."links where from_tag = '".mysql_escape_string($this->GetPageTag())."'");
|
|
|
276 |
if ($linktable = $this->GetLinkTable())
|
|
|
277 |
{
|
|
|
278 |
$from_tag = mysql_escape_string($this->GetPageTag());
|
|
|
279 |
foreach ($linktable as $to_tag)
|
|
|
280 |
{
|
|
|
281 |
$lower_to_tag = strtolower($to_tag);
|
|
|
282 |
if (!$written[$lower_to_tag])
|
|
|
283 |
{
|
|
|
284 |
$this->Query("insert into ".$this->config["table_prefix"]."links set from_tag = '".$from_tag."', to_tag = '".mysql_escape_string($to_tag)."'");
|
|
|
285 |
$written[$lower_to_tag] = 1;
|
|
|
286 |
}
|
|
|
287 |
}
|
|
|
288 |
}
|
|
|
289 |
}
|
|
|
290 |
function Header() { return $this->Action($this->GetConfigValue("header_action"), 1); }
|
|
|
291 |
function Footer() { return $this->Action($this->GetConfigValue("footer_action"), 1); }
|
|
|
292 |
|
|
|
293 |
// FORMS
|
|
|
294 |
function FormOpen($method = "", $tag = "", $formMethod = "post")
|
|
|
295 |
{
|
|
|
296 |
$result = "<form action=\"".$this->href($method, $tag)."\" method=\"".$formMethod."\">\n";
|
|
|
297 |
if (!$this->config["rewrite_mode"]) $result .= "<input type=\"hidden\" name=\"wiki\" value=\"".$this->MiniHref($method, $tag)."\" />\n";
|
|
|
298 |
return $result;
|
|
|
299 |
}
|
|
|
300 |
function FormClose()
|
|
|
301 |
{
|
|
|
302 |
return "</form>\n";
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
// INTERWIKI STUFF
|
|
|
306 |
function ReadInterWikiConfig()
|
|
|
307 |
{
|
|
|
308 |
if ($lines = file("php/lib/wikini/interwiki.conf"))
|
|
|
309 |
{
|
|
|
310 |
foreach ($lines as $line)
|
|
|
311 |
{
|
|
|
312 |
if ($line = trim($line))
|
|
|
313 |
{
|
|
|
314 |
list($wikiName, $wikiUrl) = explode(" ", trim($line));
|
|
|
315 |
$this->AddInterWiki($wikiName, $wikiUrl);
|
|
|
316 |
}
|
|
|
317 |
}
|
|
|
318 |
}
|
|
|
319 |
}
|
|
|
320 |
function AddInterWiki($name, $url)
|
|
|
321 |
{
|
|
|
322 |
$this->interWiki[$name] = $url;
|
|
|
323 |
}
|
|
|
324 |
function GetInterWikiUrl($name, $tag)
|
|
|
325 |
{
|
|
|
326 |
if (isset($this->interWiki[$name]))
|
|
|
327 |
{
|
|
|
328 |
return $this->interWiki[$name].$tag;
|
|
|
329 |
}else{
|
|
|
330 |
return 'http://'.$tag; //avoid xss by putting http:// in front of JavaScript:()
|
|
|
331 |
}
|
|
|
332 |
}
|
|
|
333 |
|
|
|
334 |
// REFERRERS
|
|
|
335 |
function LogReferrer($tag = "", $referrer = "")
|
|
|
336 |
{
|
|
|
337 |
// fill values
|
|
|
338 |
if (!$tag = trim($tag)) $tag = $this->GetPageTag();
|
|
|
339 |
if (!$referrer = trim($referrer) AND isset($_SERVER["HTTP_REFERER"])) $referrer = $_SERVER["HTTP_REFERER"];
|
|
|
340 |
|
|
|
341 |
// check if it's coming from another site
|
|
|
342 |
if ($referrer && !preg_match("/^".preg_quote($this->GetConfigValue("base_url"), "/")."/", $referrer))
|
|
|
343 |
{
|
|
|
344 |
$this->Query("insert into ".$this->config["table_prefix"]."referrers set ".
|
|
|
345 |
"page_tag = '".mysql_escape_string($tag)."', ".
|
|
|
346 |
"referrer = '".mysql_escape_string($referrer)."', ".
|
|
|
347 |
"time = now()");
|
|
|
348 |
}
|
|
|
349 |
}
|
|
|
350 |
function LoadReferrers($tag = "")
|
|
|
351 |
{
|
|
|
352 |
return $this->LoadAll("select referrer, count(referrer) as num from ".$this->config["table_prefix"]."referrers ".($tag = trim($tag) ? "where page_tag = '".mysql_escape_string($tag)."'" : "")." group by referrer order by num desc");
|
|
|
353 |
}
|
|
|
354 |
|
|
|
355 |
// PLUGINS
|
|
|
356 |
function Action($action, $forceLinkTracking = 0)
|
|
|
357 |
{
|
|
|
358 |
$action = trim($action); $vars=array();
|
|
|
359 |
// stupid attributes check
|
|
|
360 |
if ((stristr($action, "=\"")) || (stristr($action, "/")))
|
|
|
361 |
{
|
|
|
362 |
// extract $action and $vars_temp ("raw" attributes)
|
|
|
363 |
preg_match("/^([A-Za-z0-9]*)\/?(.*)$/", $action, $matches);
|
|
|
364 |
list(, $action, $vars_temp) = $matches;
|
|
|
365 |
// match all attributes (key and value)
|
|
|
366 |
$this->parameter[$vars_temp]=$vars_temp;
|
|
|
367 |
preg_match_all("/([A-Za-z0-9]*)=\"(.*)\"/U", $vars_temp, $matches);
|
|
|
368 |
|
|
|
369 |
// prepare an array for extract() to work with (in $this->IncludeBuffered())
|
|
|
370 |
if (is_array($matches))
|
|
|
371 |
{
|
|
|
372 |
for ($a = 0; $a < count($matches); $a++)
|
|
|
373 |
{
|
|
|
374 |
$vars[$matches[1][$a]] = $matches[2][$a];
|
|
|
375 |
$this->parameter[$matches[1][$a]]=$matches[2][$a];
|
|
|
376 |
}
|
|
|
377 |
}
|
|
|
378 |
}
|
|
|
379 |
if (!$forceLinkTracking) $this->StopLinkTracking();
|
|
|
380 |
$result = $this->IncludeBufferedim(strtolower($action).".php", "<i>Action inconnue \"$action\"</i>", $vars, $this->config["action_path"]);
|
|
|
381 |
$this->StartLinkTracking();
|
|
|
382 |
if (isset($parameter)) unset($this->parameter[$parameter]);
|
|
|
383 |
unset($this->parameter);
|
|
|
384 |
return $result;
|
|
|
385 |
}
|
|
|
386 |
function Method($method)
|
|
|
387 |
{
|
|
|
388 |
if (!$handler = $this->page["handler"]) $handler = "page";
|
|
|
389 |
$methodLocation = $handler."/".$method.".php";
|
|
|
390 |
return $this->IncludeBuffered($methodLocation, "<i>Méthode inconue \"$methodLocation\"</i>", "", $this->config["handler_path"]);
|
|
|
391 |
}
|
|
|
392 |
function Format($text, $formatter = "wakka")
|
|
|
393 |
{
|
|
|
394 |
//Modification : inclusion d'un chemin d'accès provenant du fichier de config.
|
|
|
395 |
return $this->IncludeBuffered($formatter.".php", "<i>Impossible de trouver le formateur \"$formatter\"</i>", compact("text"), $this->config["formatters_path"] );
|
|
|
396 |
}
|
|
|
397 |
|
|
|
398 |
// USERS
|
|
|
399 |
function LoadUser($name, $password = 0) { return $this->LoadSingle("select * from ".$this->config["table_prefix"]."users where name = '".mysql_escape_string($name)."' ".($password === 0 ? "" : "and password = '".mysql_escape_string($password)."'")." limit 1"); }
|
|
|
400 |
function LoadUsers() { return $this->LoadAll("select * from ".$this->config["table_prefix"]."users order by name"); }
|
|
|
401 |
function GetUserName() { if ($user = $this->GetUser()) $name = $user["name"]; else if (!$name = gethostbyaddr($_SERVER["REMOTE_ADDR"])) $name = $_SERVER["REMOTE_ADDR"]; return $name; }
|
|
|
402 |
function UserName() { /* deprecated! */ return $this->GetUserName(); }
|
|
|
403 |
function GetUser() { return (isset($_SESSION["user"]) ? $_SESSION["user"] : '');}
|
|
|
404 |
function SetUser($user, $remember=0) { $_SESSION["user"] = $user; $this->SetPersistentCookie("name", $user["name"], $remember); $this->SetPersistentCookie("password", $user["password"], $remember); $this->SetPersistentCookie("remember", $remember, $remember); }
|
|
|
405 |
function LogoutUser() { $_SESSION["user"] = ""; $this->DeleteCookie("name"); $this->DeleteCookie("password"); }
|
|
|
406 |
function UserWantsComments() { if (!$user = $this->GetUser()) return false; return ($user["show_comments"] == "Y"); }
|
|
|
407 |
function GetParameter($parameter) { return (isset($this->parameter[$parameter]) ? $this->parameter[$parameter] :''); }
|
|
|
408 |
|
|
|
409 |
|
|
|
410 |
// COMMENTS
|
|
|
411 |
function LoadComments($tag) { return $this->LoadAll("select * from ".$this->config["table_prefix"]."pages where comment_on = '".mysql_escape_string($tag)."' and latest = 'Y' order by time"); }
|
|
|
412 |
function LoadRecentComments() { return $this->LoadAll("select * from ".$this->config["table_prefix"]."pages where comment_on != '' and latest = 'Y' order by time desc"); }
|
|
|
413 |
function LoadRecentlyCommented($limit = 50)
|
|
|
414 |
{
|
|
|
415 |
// NOTE: this is really stupid. Maybe my SQL-Fu is too weak, but apparently there is no easier way to simply select
|
|
|
416 |
// all comment pages sorted by their first revision's (!) time. ugh!
|
|
|
417 |
|
|
|
418 |
// load ids of the first revisions of latest comments. err, huh?
|
|
|
419 |
if ($ids = $this->LoadAll("select min(id) as id from ".$this->config["table_prefix"]."pages where comment_on != '' group by tag order by id desc"))
|
|
|
420 |
{
|
|
|
421 |
// load complete comments
|
|
|
422 |
foreach ($ids as $id)
|
|
|
423 |
{
|
|
|
424 |
$comment = $this->LoadSingle("select * from ".$this->config["table_prefix"]."pages where id = '".$id["id"]."' limit 1");
|
|
|
425 |
if (!$comments[$comment["comment_on"]] && $num < $limit)
|
|
|
426 |
{
|
|
|
427 |
$comments[$comment["comment_on"]] = $comment;
|
|
|
428 |
$num++;
|
|
|
429 |
}
|
|
|
430 |
}
|
|
|
431 |
|
|
|
432 |
// now load pages
|
|
|
433 |
if ($comments)
|
|
|
434 |
{
|
|
|
435 |
// now using these ids, load the actual pages
|
|
|
436 |
foreach ($comments as $comment)
|
|
|
437 |
{
|
|
|
438 |
$page = $this->LoadPage($comment["comment_on"]);
|
|
|
439 |
$page["comment_user"] = $comment["user"];
|
|
|
440 |
$page["comment_time"] = $comment["time"];
|
|
|
441 |
$page["comment_tag"] = $comment["tag"];
|
|
|
442 |
$pages[] = $page;
|
|
|
443 |
}
|
|
|
444 |
}
|
|
|
445 |
}
|
|
|
446 |
// load tags of pages
|
|
|
447 |
//return $this->LoadAll("select comment_on as tag, max(time) as time, tag as comment_tag, user from ".$this->config["table_prefix"]."pages where comment_on != '' group by comment_on order by time desc");
|
|
|
448 |
return $pages;
|
|
|
449 |
}
|
|
|
450 |
|
|
|
451 |
// ACCESS CONTROL
|
|
|
452 |
// returns true if logged in user is owner of current page, or page specified in $tag
|
|
|
453 |
function UserIsOwner($tag = "")
|
|
|
454 |
{
|
|
|
455 |
// check if user is logged in
|
|
|
456 |
if (!$this->GetUser()) return false;
|
|
|
457 |
|
|
|
458 |
// set default tag
|
|
|
459 |
if (!$tag = trim($tag)) $tag = $this->GetPageTag();
|
|
|
460 |
|
|
|
461 |
// check if user is owner
|
|
|
462 |
if ($this->GetPageOwner($tag) == $this->GetUserName()) return true;
|
|
|
463 |
}
|
|
|
464 |
function GetPageOwner($tag = "", $time = "") { if (!$tag = trim($tag)) $tag = $this->GetPageTag(); if ($page = $this->LoadPage($tag, $time)) return $page["owner"]; }
|
|
|
465 |
function SetPageOwner($tag, $user)
|
|
|
466 |
{
|
|
|
467 |
// check if user exists
|
|
|
468 |
if (!$this->LoadUser($user)) return;
|
|
|
469 |
|
|
|
470 |
// updated latest revision with new owner
|
|
|
471 |
$this->Query("update ".$this->config["table_prefix"]."pages set owner = '".mysql_escape_string($user)."' where tag = '".mysql_escape_string($tag)."' and latest = 'Y' limit 1");
|
|
|
472 |
}
|
|
|
473 |
function LoadAcl($tag, $privilege, $useDefaults = 1)
|
|
|
474 |
{
|
|
|
475 |
if ((!$acl = $this->LoadSingle("select * from ".$this->config["table_prefix"]."acls where page_tag = '".mysql_escape_string($tag)."' and privilege = '".mysql_escape_string($privilege)."' limit 1")) && $useDefaults)
|
|
|
476 |
{
|
|
|
477 |
$acl = array("page_tag" => $tag, "privilege" => $privilege, "list" => $this->GetConfigValue("default_".$privilege."_acl"));
|
|
|
478 |
}
|
|
|
479 |
return $acl;
|
|
|
480 |
}
|
|
|
481 |
function SaveAcl($tag, $privilege, $list) {
|
|
|
482 |
if ($this->LoadAcl($tag, $privilege, 0)) $this->Query("update ".$this->config["table_prefix"]."acls set list = '".mysql_escape_string(trim(str_replace("\r", "", $list)))."' where page_tag = '".mysql_escape_string($tag)."' and privilege = '".mysql_escape_string($privilege)."' limit 1");
|
|
|
483 |
else $this->Query("insert into ".$this->config["table_prefix"]."acls set list = '".mysql_escape_string(trim(str_replace("\r", "", $list)))."', page_tag = '".mysql_escape_string($tag)."', privilege = '".mysql_escape_string($privilege)."'");
|
|
|
484 |
}
|
|
|
485 |
// returns true if $user (defaults to current user) has access to $privilege on $page_tag (defaults to current page)
|
|
|
486 |
function HasAccess($privilege, $tag = "", $user = "")
|
|
|
487 |
{
|
|
|
488 |
// set defaults
|
|
|
489 |
if (!$tag = trim($tag)) $tag = $this->GetPageTag();
|
|
|
490 |
if (!$user = $this->GetUserName());
|
|
|
491 |
|
|
|
492 |
// load acl
|
|
|
493 |
$acl = $this->LoadAcl($tag, $privilege);
|
|
|
494 |
|
|
|
495 |
// if current user is owner, return true. owner can do anything!
|
|
|
496 |
if ($this->UserIsOwner($tag)) return true;
|
|
|
497 |
|
|
|
498 |
// fine fine... now go through acl
|
|
|
499 |
foreach (explode("\n", $acl["list"]) as $line)
|
|
|
500 |
{
|
|
|
501 |
$line = trim($line);
|
|
|
502 |
|
|
|
503 |
// check for inversion character "!"
|
|
|
504 |
if (preg_match("/^[!](.*)$/", $line, $matches))
|
|
|
505 |
{
|
|
|
506 |
$negate = 1;
|
|
|
507 |
$line = $matches[1];
|
|
|
508 |
}
|
|
|
509 |
else
|
|
|
510 |
{
|
|
|
511 |
$negate = 0;
|
|
|
512 |
}
|
|
|
513 |
|
|
|
514 |
// if there's still anything left... lines with just a "!" don't count!
|
|
|
515 |
if ($line)
|
|
|
516 |
{
|
|
|
517 |
switch ($line[0])
|
|
|
518 |
{
|
|
|
519 |
// comments
|
|
|
520 |
case "#":
|
|
|
521 |
break;
|
|
|
522 |
// everyone
|
|
|
523 |
case "*":
|
|
|
524 |
return !$negate;
|
|
|
525 |
// aha! a user entry.
|
|
|
526 |
case "+":
|
|
|
527 |
if (!$this->LoadUser($user))
|
|
|
528 |
{
|
|
|
529 |
return $negate;
|
|
|
530 |
}
|
|
|
531 |
else
|
|
|
532 |
{
|
|
|
533 |
return !$negate;
|
|
|
534 |
}
|
|
|
535 |
default:
|
|
|
536 |
if ($line == $user)
|
|
|
537 |
{
|
|
|
538 |
return !$negate;
|
|
|
539 |
}
|
|
|
540 |
}
|
|
|
541 |
}
|
|
|
542 |
}
|
|
|
543 |
|
|
|
544 |
// tough luck.
|
|
|
545 |
return false;
|
|
|
546 |
}
|
|
|
547 |
|
|
|
548 |
// MAINTENANCE
|
|
|
549 |
function Maintenance()
|
|
|
550 |
{
|
|
|
551 |
// purge referrers
|
|
|
552 |
if ($days = $this->GetConfigValue("referrers_purge_time")) {
|
|
|
553 |
$this->Query("delete from ".$this->config["table_prefix"]."referrers where time < date_sub(now(), interval '".mysql_escape_string($days)."' day)");
|
|
|
554 |
}
|
|
|
555 |
|
|
|
556 |
// purge old page revisions
|
|
|
557 |
if ($days = $this->GetConfigValue("pages_purge_time")) {
|
|
|
558 |
$this->Query("delete from ".$this->config["table_prefix"]."pages where time < date_sub(now(), interval '".mysql_escape_string($days)."' day) and latest = 'N'");
|
|
|
559 |
}
|
|
|
560 |
}
|
|
|
561 |
|
|
|
562 |
// THE BIG EVIL NASTY ONE!
|
|
|
563 |
function Run($tag, $method = "")
|
|
|
564 |
{
|
|
|
565 |
if(!($this->GetMicroTime()%3)) $this->Maintenance();
|
|
|
566 |
|
|
|
567 |
$this->ReadInterWikiConfig();
|
|
|
568 |
|
|
|
569 |
// do our stuff!
|
|
|
570 |
if (!$this->method = trim($method)) $this->method = "show";
|
|
|
571 |
if (!$this->tag = trim($tag)) $this->Redirect($this->href("", $this->config["root_page"]));
|
|
|
572 |
if ((!$this->GetUser() && isset($_COOKIE["name"])) && ($user = $this->LoadUser($_COOKIE["name"], $_COOKIE["password"]))) $this->SetUser($user, $_COOKIE["remember"]);
|
|
|
573 |
$this->SetPage($this->LoadPage($tag, (isset($_REQUEST["time"]) ? $_REQUEST["time"] :'')));
|
|
|
574 |
$this->LogReferrer();
|
|
|
575 |
switch ($this->method)
|
|
|
576 |
{
|
|
|
577 |
case "xml":
|
|
|
578 |
header("Content-type: text/xml");
|
|
|
579 |
case "raw":
|
|
|
580 |
//ATTENTION: remplacement print par return et retourne seulement le contenu de la page wikini
|
|
|
581 |
return ( $this->Method( $this->method ) );
|
|
|
582 |
break;
|
|
|
583 |
default:
|
|
|
584 |
//ATTENTION: remplacement print par return et retourne seulement le contenu de la page wikini
|
|
|
585 |
return ( $this->Method( $this->method ) );
|
|
|
586 |
}
|
|
|
587 |
}
|
|
|
588 |
}//Fin de la classe Wiki
|
|
|
589 |
|
|
|
590 |
//ATTENTION : Suppression du reste du fichier car géré par les applications de Génésia.
|
|
|
591 |
?>
|