Retour [./2.21_str_code/index_str_code.php]
show/hide
0
1 <?php
2 // TigerWiki 2 (Version 2.21 - 2007-07-27)
3 // Copyleft (C) Chabel.org 2005-2007, licence GNU/GPL (disponible sur www.gnu.org)
4 // http://chabel.org/
5 $WIKI_VERSION = "TigerWiki 2.21+";
6 // BugLinks
7 // profile
8 // str_replace {VARIABLES}
9
10
11 function profile ($txt,$dump=FALSE)
12 {
13 static $times;
14 if ($dump)
15 {
16 if (!file_exists("perfs.log"))
17 {
18 $f = fopen("perfs.log","w");
19 for ($i=1;$i<sizeof($times);$i++)
20 fwrite($f,sprintf("%s:",$times[$i][0])); // header
21 fwrite($f,"\n");
22 }
23 else
24 $f = fopen("perfs.log","a");
25 for ($i=1;$i<sizeof($times);$i++)
26 fwrite($f,sprintf("%f:",$times[$i][1]-$times[$i-1][1])); // valeurs
27 fwrite($f,"\n");
28 fclose($f);
29 }
30 else
31 {
32 list($usec, $sec) = explode(" ", microtime());
33 $times[] = array($txt,((float)$usec + (float)$sec));
34 }
35 }
36 profile("Debut");
37
38 //Fichier de configuration
39 include("_config.php");
40 // Trouver la page a afficher
41 if (! $PAGE_TITLE = stripslashes(utf8_encode($_GET["page"])))
42 {
43 if ($_GET["action"] == "search")
44 if ($_GET["query"] != "")
45 $PAGE_TITLE = "$SEARCH_RESULTS " . stripslashes($_GET[query]);
46 else
47 $PAGE_TITLE = $LIST . " (" . count(glob("$PAGES_DIR/*.*")) . ")";
48 elseif ($_GET["action"] == "recent")
49 $PAGE_TITLE = "$RECENT_CHANGES";
50 else
51 $PAGE_TITLE = "$START_PAGE";
52 }
53 $action = $_GET["action"];
54 if (isset($_GET["time"]))
55 $gtime = $_GET["time"];
56 $datetw = date("Y/m/d H:i", mktime(date("H") + $LOCAL_HOUR));
57 // Arreter les acces malicieux via repertoire et accents
58 if (preg_match("/\//", $PAGE_TITLE))
59 $PAGE_TITLE = $START_PAGE;
60 if (preg_match("/\//", $gtime))
61 $gtime = '';
62 // Ecrire les modifications, s'il y a lieu
63 if ($_POST["content"] != "")
64 {
65 if ($_SERVER["REQUEST_METHOD"] == "POST")
66 {
67 if ($_POST["sc"] == $PASSWORD || $_COOKIE['AutorisationTigerWiki'] == md5($PASSWORD))
68 {
69 setcookie('AutorisationTigerWiki', md5($PASSWORD), time() + 365*24*3600);
70 if (! $file = @fopen($PAGES_DIR . stripslashes($_POST["page"]) . ".txt", "w"))
71 die("Could not write page!");
72 if (get_magic_quotes_gpc())
73 fputs($file, trim(stripslashes(utf8_encode($_POST["content"]))));
74 else
75 fputs($file, trim(utf8_encode($_POST["content"])));
76 fclose($file);
77 if ($BACKUP_DIR <> '')
78 {
79 $complete_dir_s = $BACKUP_DIR . $_POST["page"] . "/";
80 if (! $dir = @opendir($complete_dir_s))
81 {
82 mkdir($complete_dir_s);
83 chmod($complete_dir_s,0777);
84 }
85 if (! $file = @fopen($complete_dir_s . date("Ymd-Hi", mktime(date("H") + $LOCAL_HOUR)) . ".bak", "a"))
86 die("Could not write backup of page!");
87 fputs($file, "\n// " . $datetw . " / " . " " . $_SERVER['REMOTE_ADDR'] . "\n");
88 if (get_magic_quotes_gpc())
89 fputs($file, trim(stripslashes($_POST["content"])));
90 else
91 fputs($file, trim($_POST["content"]) . "\n\n");
92 fclose($file);
93 }
94 header("location: ./?page=" . urlencode(stripslashes($_POST[page])));
95 }
96 else
97 {
98 header("location: ./?page=" . $_POST[page]."&action=edit&error=1");
99 }
100 }
101 }
102 elseif (isset($_POST["content"]) && $_POST["content"] == "")
103 {
104 if ($_POST["sc"] == $PASSWORD || $_COOKIE['AutorisationTigerWiki'] == md5($PASSWORD))
105 {
106 setcookie('AutorisationTigerWiki', md5($PASSWORD), time() + 365*24*3600);
107 unlink($PAGES_DIR . stripslashes($_POST["page"]) . ".txt");
108 }
109 else
110 header("location: ./?page=".$_POST["page"]."&action=edit&error=1");
111 }
112 profile("Edit");
113 // Lecture et analyse du modèle de page
114 if (! $file = @fopen("template.html", "r"))
115 die("'template.html' is missing!");
116 $template = fread($file, filesize("template.html"));
117 fclose($file);
118 // Lecture du contenu et de la date de modification de la page
119 if (($file = @fopen($PAGES_DIR . utf8_decode($PAGE_TITLE) . ".txt", "r")) || $action <> "")
120 {
121 if (file_exists($PAGES_DIR . utf8_decode($PAGE_TITLE) . ".txt"))
122 $TIME = date("Y/m/d H:i", @filemtime($PAGES_DIR . utf8_decode($PAGE_TITLE) . ".txt") + $LOCAL_HOUR * 3600);
123 $CONTENT = "\n" . @fread($file, @filesize($PAGES_DIR . utf8_decode($PAGE_TITLE) . ".txt")) . "\n";
124 // Restaurer une page
125 if (isset($_GET["page"]) && isset($gtime) && $_GET["restore"] == 1)
126 if ($file = @fopen($BACKUP_DIR . $PAGE_TITLE . "/" . $gtime, "r"))
127 $CONTENT = "\n" . @fread($file, @filesize($BACKUP_DIR . $PAGE_TITLE . "/" . $gtime)) . "\n";
128 @fclose($file);
129 $CONTENT = preg_replace("/\\$/Umsi", "$", $CONTENT);
130 $CONTENT = preg_replace("/\\\/Umsi", "\", $CONTENT);
131 }
132 else
133 {
134 if (!file_exists($PAGES_DIR . $PAGE_TITLE . ".txt"))
135 $CONTENT = "\n" . $DEFAULT_CONTENT;
136 else
137 $action = "edit";
138 }
139 // Déterminer le mode d'accès
140 if ($action == "edit" || $action == "search" || $action == "recent")
141 $html = str_replace('{EDIT}', $EDIT_BUTTON, $template);
142 elseif (is_writable($PAGES_DIR . $PAGE_TITLE . ".txt") || !file_exists($PAGES_DIR . $PAGE_TITLE . ".txt"))
143 $html = str_replace('{EDIT}', "<a href=\"./?page=".$PAGE_TITLE."&action=edit\" accesskey=\"5\" rel=\"nofollow\">$EDIT_BUTTON</a>", $template);
144 else
145 $html = str_replace('{EDIT}', $PROTECTED_BUTTON, $template);
146 if ($action == "recent")
147 $html = str_replace('{RECENT_CHANGES}', $RECENT_CHANGES, $html);
148 else
149 $html = str_replace('{RECENT_CHANGES}', "<a href=\"./?action=recent\" accesskey=\"3\">$RECENT_CHANGES</a>", $html);
150 // Remplacer les variables par des valeurs (dans le style de page)
151 $html = str_replace('{PAGE_TITLE_BRUT}', $PAGE_TITLE, $html);
152 if ($action != "" && $action != "recent" && $action != "search")
153 $html = str_replace('{PAGE_TITLE}', "<a href=\"./?page=".$PAGE_TITLE."\">".$PAGE_TITLE."</a>", $html);
154 else
155 $html = str_replace('{PAGE_TITLE}', $PAGE_TITLE, $html);
156 if ($PAGE_TITLE == $START_PAGE && $action <> "search")
157 $html = str_replace('{HOME}', $HOME_BUTTON, $html);
158 else
159 $html = str_replace('{HOME}', "<a href=\"./?page=".$START_PAGE."\" accesskey=\"1\">$HOME_BUTTON</a>", $html);
160 $html = str_replace('{WIKI_TITLE}', $WIKI_TITLE, $html);
161 $html = str_replace('{LAST_CHANGE}', $LAST_CHANGES." :", $html);
162 if ($action != "edit")
163 $html = str_replace('{HELP}', "", $html);
164 else
165 $html = str_replace('{HELP}', "(<a href=\"./?page=$HELP_BUTTON\" accesskey=\"2\" rel=\"nofollow\">$HELP_BUTTON</a>)", $html);
166 $html = str_replace('{SEARCH}', "<form method=\"get\" action=\"./?page=".$_GET[page]."\"><div><input type=\"hidden\" name=\"action\" value=\"search\" /><input type=\"text\" name=\"query\" value=\"$_GET[query]\" tabindex=1 /> <input type=\"submit\" value=\"$SEARCH_BUTTON\" accesskey=\"q\" /></div></form>", $html);
167 if ($action == "edit")
168 {
169 $html = str_replace('{HISTORY}', "/ <a href=\"?page=".$PAGE_TITLE."&action=history\" accesskey=\"6\" rel=\"nofollow\">".$HISTORY_BUTTON."</a><br />", $html);
170 $CONTENT = "<form method=\"post\" action=\"./\"><textarea name=\"content\" cols=\"83\" rows=\"30\" style=\"width: 100%;\">$CONTENT</textarea><input type=\"hidden\" name=\"page\" value=\"".$PAGE_TITLE."\" /><br /><p align=\"right\">";
171 if ($PASSWORD != "" && $_COOKIE['AutorisationTigerWiki'] != md5($PASSWORD))
172 $CONTENT .= $MDP." : <input type=\"password\" name=\"sc\" />";
173 $CONTENT .= " <input type=\"submit\" value=\"$DONE_BUTTON\" accesskey=\"s\" /></p></form>";
174 //Retrait d'un </div> avant le </form>
175 }
176 elseif ($action != "history")
177 $html = str_replace('{HISTORY}', "", $html);
178 // Liste des versions historiques d'une page
179 if ($action == "history" && !isset($gtime))
180 {
181 $html = str_replace('{HISTORY}', "/ ".$HISTORY_BUTTON, $html);
182 $complete_dir = $BACKUP_DIR . $_GET["page"] . "/";
183 if ($opening_dir = @opendir($complete_dir))
184 {
185 while (false !== ($filename = @readdir($opening_dir)))
186 $files[] = $filename;
187 sort ($files);
188 for($cptfiles = 2; $files[$cptfiles] != ''; $cptfiles++)
189 $affichage = $affichage."<a href=\"?page=".$_GET["page"]."&action=history&time=".$files[$cptfiles]."\" rel=\"nofollow\">".$files[$cptfiles]."</a><br />";
190 $html = str_replace('{CONTENT}', $affichage, $html);
191 }
192 else
193 $html = str_replace('{CONTENT}', $NO_HISTORY, $html);
194 }
195 // Affichage d'un fichier historique
196 if ($action == "history" && isset($gtime))
197 {
198 $complete_dir = $BACKUP_DIR . $PAGE_TITLE . "/";
199 if ($file = @fopen($BACKUP_DIR . $PAGE_TITLE . "/" . $gtime, "r"))
200 {
201 $html = str_replace('{HISTORY}', "/ <a href=\"?page=".$PAGE_TITLE."&action=history\" rel=\"nofollow\">".$HISTORY_BUTTON."</a> (<a href=\"?page=".$PAGE_TITLE."&action=edit&time=".$gtime."&restore=1\" rel=\"nofollow\">".$RESTORE."</a>)", $html);
202 $CONTENT = @fread($file, @filesize($complete_dir . $gtime)) . "\n";
203 }
204 else
205 $html = str_replace('{HISTORY}', "/ <a href=\"?page=".$PAGE_TITLE."&action=history\" rel=\"nofollow\">".$HISTORY_BUTTON."</a> (-)", $html);
206 }
207 // Erreur du mot de passe
208 if ($_GET['error'] == 1)
209 $html = str_replace('{ERROR}', $ERROR, $html);
210 else
211 $html = str_replace('{ERROR}', "", $html);
212 // Effacement du cookie
213 if ($_GET['erasecookie'] == 1)
214 setcookie('AutorisationTigerWiki');
215 // Page de recherche
216 if ($action == "search")
217 {
218 $dir = opendir(getcwd() . "/$PAGES_DIR");
219 while ($file = readdir($dir))
220 {
221 if (preg_match("/.txt/", $file))
222 {
223 $handle = fopen($PAGES_DIR . $file, "r");
224 @$content = fread($handle, filesize($PAGES_DIR . $file));
225 fclose($handle);
226 if (preg_match("/$_GET[query]/i", $content) || preg_match("/$_GET[query]/i", "$PAGES_DIR/$file"))
227 {
228 $file = substr($file, 0, strlen($file) - 4);
229 $CONTENT .= "<a href=\"./?page=".utf8_encode($file)."\">".utf8_encode($file)."</a><br />";
230 }
231 }
232 }
233 }
234 // Changements récents
235 elseif ($action == "recent")
236 {
237 $dir = opendir(getcwd() . "/$PAGES_DIR");
238 while ($file = readdir($dir))
239 if (preg_match("/.txt/", $file))
240 $filetime[$file] = filemtime($PAGES_DIR . $file);
241 arsort($filetime);
242 $filetime = array_slice($filetime, 0, 10);
243 foreach ($filetime as $filename => $timestamp)
244 {
245 $filename = substr($filename, 0, strlen($filename) - 4);
246 $CONTENT .= "<a href=\"./?page=".utf8_encode($filename)."\">".utf8_encode($filename)."</a> (" . strftime("$TIME_FORMAT", $timestamp + $LOCAL_HOUR * 3600) . ")<br />";
247 }
248 }
249 // Formatage de page
250 elseif ($action <> "edit")
251 {
252 profile("Traitements");
253 if (preg_match("/%html%\s/", $CONTENT))
254
255 $CONTENT = preg_replace("/%html%\s/", "", $CONTENT);
256 else
257 {
258 $CONTENT = htmlentities($CONTENT);
259 $CONTENT = str_replace("&#036;", "$", $CONTENT);
260 $CONTENT = str_replace("&#092;", "\", $CONTENT);
261 // JJL test code {} DBG
262 $nbcode = preg_match_all("/{{(.+)}}/Ums",$CONTENT,$matches_code);
263 $CONTENT = preg_replace("/{{(.+)}}/Ums","<code><pre>{{CODE}}</pre></code>",$CONTENT);
264
265 $CONTENT = preg_replace("/\^(.)/Umsie", "'&#'.ord('\\1').';'", $CONTENT);
266 $CONTENT = preg_replace('#\[([^\]]+)\|([0-9a-zA-Z\.\'\s\#/~\-_%=\?\&,\+]*)\]#U', '<a href="$2" class="url">$1</a>', $CONTENT);
267 $CONTENT = preg_replace('#\[([^\]]+)\|h(ttps?://[0-9a-zA-Z\.\#/~\-_%=\?\&,\+]*)\]#U', '<a href="xx$2" class="url">$1</a>', $CONTENT);
268 $CONTENT = preg_replace('#\[h(ttps?://[0-9a-zA-Z\.\&\#\:/~\-_%=?]*\.(jpeg|jpg|gif|png))\]#i', '<img src="xx$1" />', $CONTENT);
269 $CONTENT = preg_replace('#\[([0-9a-zA-Z\.\&\#\:/~\-_%=?]*\.(jpeg|jpg|gif|png))\]#i', '<img src="$1" />', $CONTENT);
270 $CONTENT = preg_replace('#(https?://[0-9a-zA-Z\.\&\#\:/~\-_%=?]*)#i', '<a href="$0" class="url">$1</a>', $CONTENT);
271 $CONTENT = preg_replace('#xxttp#', 'http', $CONTENT);
272 preg_match_all("/\[([^\/]+)\]/U", $CONTENT, $matches, PREG_PATTERN_ORDER);
273 foreach ($matches[1] as $match)
274 if (file_exists(html_entity_decode($PAGES_DIR."$match.txt")))
275 $CONTENT = str_replace("[$match]", "<a href=\"./?page=".$match."\">$match</a>", $CONTENT);
276 else
277 $CONTENT = str_replace("[$match]", "<a class=\"pending\" href=\"./?page=".$match."\">$match</a>", $CONTENT);
278 $CONTENT = preg_replace('#(\[\?(.+)\]*)#i', '<a href="http://fr.wikipedia.org/wiki/$0" class="url">$0</a>', $CONTENT);
279 $CONTENT = preg_replace('#([0-9a-zA-Z\./~\-_]+@[0-9a-z\./~\-_]+)#i', '<a href="mailto:$0">$0</a>', $CONTENT);
280 $CONTENT = preg_replace('/^\*\*\*(.*)(\n)/Um', "<ul><ul><ul><li>$1</li></ul></ul></ul>$2", $CONTENT);
281 $CONTENT = preg_replace('/^\*\*(.*)(\n)/Um', "<ul><ul><li>$1</li></ul></ul>$2", $CONTENT);
282 $CONTENT = preg_replace('/^\*(.*)(\n)/Um', "<ul><li>$1</li></ul>$2", $CONTENT);
283 $CONTENT = preg_replace('/^\#\#\#(.*)(\n)/Um', "<ol><ol><ol><li>$1</li></ol></ol></ol>$2", $CONTENT);
284 $CONTENT = preg_replace('/^\#\#(.*)(\n)/Um', "<ol><ol><li>$1</li></ol></ol>$2", $CONTENT);
285 $CONTENT = preg_replace('/^\#(.*)(\n)/Um', "<ol><li>$1</li></ol>$2", $CONTENT);
286
287 $CONTENT = preg_replace('/(<\/ol>\n*<ol>|<\/ul>\n*<ul>)/', "", $CONTENT);
288 $CONTENT = preg_replace('/(<\/ol>\n*<ol>|<\/ul>\n*<ul>)/', "", $CONTENT);
289 $CONTENT = preg_replace('/(<\/ol>\n*<ol>|<\/ul>\n*<ul>)/', "", $CONTENT);
290
291
292 $CONTENT = preg_replace('/^!!!(.*)(\n)/Um', '<h1>$1</h1>$2', $CONTENT);
293 $CONTENT = preg_replace('/^!!(.*)(\n)/Um', '<h2>$1</h2>$2', $CONTENT);
294 $CONTENT = preg_replace('/^!(.*)(\n)/Um', '<h3>$1</h3>$2', $CONTENT);
295 while (preg_match('/^ /Um', $CONTENT))
296 $CONTENT = preg_replace('/^( +) ([^ ])/Um', '$1 $2', $CONTENT);
297 $CONTENT = preg_replace('/^ /Um', ' ', $CONTENT);
298 $CONTENT = preg_replace('/----*(\r\n|\r|\n)/m', '<hr />', $CONTENT);
299 $CONTENT = preg_replace('/\n/', '<br />', $CONTENT);
300 $CONTENT = preg_replace('#</ul>(<br />)*#', "</ul>", $CONTENT);
301 $CONTENT = preg_replace('#</ol>(<br />)*#', "</ol>", $CONTENT);
302
303 $CONTENT = preg_replace('#</li><ul><li>*#', "<ul><li>", $CONTENT);
304 $CONTENT = preg_replace('#</ul></ul>*#', "</ul></li></ul>", $CONTENT);
305 $CONTENT = preg_replace('#</ul></ul>*#', "</ul></li></ul>", $CONTENT);
306 $CONTENT = preg_replace('#</li></ul><li>*#', "</li></ul></li><li>", $CONTENT);
307
308 $CONTENT = preg_replace('#</li><ol><li>*#', "<ol><li>", $CONTENT);
309 $CONTENT = preg_replace('#</ol></ol>*#', "</ol></li></ol>", $CONTENT);
310 $CONTENT = preg_replace('#</ol></ol>*#', "</ol></li></ol>", $CONTENT);
311 $CONTENT = preg_replace('#</li></ol><li>*#', "</li></ol></li><li>", $CONTENT);
312
313 $CONTENT = preg_replace('#(</h[123]>)<br />#', "$1", $CONTENT);
314 // $CONTENT = preg_replace("/{(.+)}/Ue", "'<pre><code>' . preg_replace('#<br />#', '', '\\1') . '</code></pre>'", $CONTENT); // JJL {{code}}
315 $CONTENT = preg_replace("/'--(.*)--'/Um", '<del>$1</del>', $CONTENT);
316 $CONTENT = preg_replace("/'''''(.*)'''''/Um", '<strong><em>$1</em></strong>', $CONTENT);
317 $CONTENT = preg_replace("/'''(.*)'''/Um", '<strong>$1</strong>', $CONTENT);
318 $CONTENT = preg_replace("/''(.*)''/Um", '<em>$1</em>', $CONTENT);
319 $CONTENT = substr($CONTENT, 6, strlen($CONTENT) - 6);
320 $CONTENT = html_entity_decode($CONTENT);
321 // JJL {{code}}
322 function matchcode($m) {global $matches_code;static $idxcode=0; return $matches_code[1][$idxcode++];}
323 if ($nbcode > 0)
324 $CONTENT = preg_replace_callback(array_fill(0,$nbcode,"/{{CODE}}/"),"matchcode",$CONTENT);
325 }
326 }
327 if ($action != "" && $action != "edit" || (!file_exists($PAGES_DIR . $PAGE_TITLE . ".txt")))
328 $TIME = "-";
329 $html = str_replace("{CONTENT}", $CONTENT, $html);
330 $html = str_replace("{LANG}", $LANG, $html);
331 $html = str_replace("{WIKI_VERSION}", $WIKI_VERSION, $html);
332 $html = str_replace("{CHARSET}", $CHARSET, $html);
333 $html = str_replace('{TIME}', $TIME, $html);
334 $html = str_replace('{DATE}', $datetw, $html);
335 $html = str_replace('{IP}', $_SERVER['REMOTE_ADDR'], $html);
336 if ($_COOKIE['AutorisationTigerWiki'] != "")
337 $html = str_replace('{COOKIE}', '-- <a href="./?page='.$PAGE_TITLE.'&erasecookie=1" rel=\"nofollow\">'.$ERASE_COOKIE.'</a>', $html);
338 else
339 $html = str_replace('{COOKIE}', '', $html);
340 // Affichage de la page
341 echo utf8_decode($html);
342 profile("Formatage");
343 profile("",TRUE);
344 ?>
345
346
347