TigerWiki PHP Cross Reference Collaborative Wikis

Source: /index.php - 276 lines - 16430 bytes - Summary - Text - Print

   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     
   7  //Fichier de configuration

   8      include ("_config.php");
   9  // Trouver la page a afficher

  10      if (! $PAGE_TITLE = stripslashes(utf8_encode($_GET["page"]))) {
  11          if ($_GET["action"] == "search")
  12              if ($_GET["query"] != "")
  13                  $PAGE_TITLE = "$SEARCH_RESULTS " . stripslashes($_GET[query]);
  14              else
  15                  $PAGE_TITLE = $LIST . " (" . count(glob("$PAGES_DIR/*.*")) . ")";

  16          elseif ($_GET["action"] == "recent")

  17              $PAGE_TITLE = "$RECENT_CHANGES";

  18          else

  19              $PAGE_TITLE = "$START_PAGE";

  20      }

  21      $action = $_GET["action"];

  22      if (isset($_GET["time"]))

  23          $gtime = $_GET["time"];

  24      $datetw = date("Y/m/d H:i", mktime(date("H") + $LOCAL_HOUR));

  25  // Arreter les acces malicieux via repertoire et accents

  26      if (preg_match("/\//", $PAGE_TITLE))

  27          $PAGE_TITLE = $START_PAGE;

  28      if (preg_match("/\//", $gtime))

  29          $gtime = '';

  30  // Ecrire les modifications, s'il y a lieu

  31         if ($_POST["content"] != "") {

  32          if ($_SERVER["REQUEST_METHOD"] == "POST") {    

  33              if ($_POST["sc"] == $PASSWORD || $_COOKIE['AutorisationTigerWiki'] == md5($PASSWORD)) {

  34                  setcookie('AutorisationTigerWiki', md5($PASSWORD), time() + 365*24*3600);

  35                  if (! $file = @fopen($PAGES_DIR . stripslashes($_POST["page"]) . ".txt", "w"))

  36                      die("Could not write page!");

  37                  if (get_magic_quotes_gpc())

  38                      fputs($file, trim(stripslashes(utf8_encode($_POST["content"]))));

  39                  else

  40                      fputs($file, trim(utf8_encode($_POST["content"])));        

  41                  fclose($file);        

  42                  if ($BACKUP_DIR <> '') {

  43                      $complete_dir_s = $BACKUP_DIR . $_POST["page"] . "/";

  44                      if (! $dir = @opendir($complete_dir_s)) {

  45                          mkdir($complete_dir_s);

  46                          chmod($complete_dir_s,0777);

  47                      }

  48                      if (! $file = @fopen($complete_dir_s . date("Ymd-Hi", mktime(date("H") + $LOCAL_HOUR)) . ".bak", "a"))

  49                          die("Could not write backup of page!");

  50                      fputs($file, "\n// " . $datetw . " / " . " " . $_SERVER['REMOTE_ADDR'] . "\n");

  51                      if (get_magic_quotes_gpc())

  52                           fputs($file, trim(stripslashes($_POST["content"])));

  53                      else

  54                           fputs($file, trim($_POST["content"]) . "\n\n");            

  55                       fclose($file);

  56                  }

  57                  header("location: ./?page=" . urlencode(stripslashes($_POST[page])));

  58              }

  59              else { header("location: ./?page=" . $_POST[page]."&action=edit&error=1"); }

  60      }

  61      }

  62      elseif (isset($_POST["content"]) && $_POST["content"] == "") {

  63          if ($_POST["sc"] == $PASSWORD || $_COOKIE['AutorisationTigerWiki'] == md5($PASSWORD)) {

  64              setcookie('AutorisationTigerWiki', md5($PASSWORD), time() + 365*24*3600);

  65              unlink($PAGES_DIR . stripslashes($_POST["page"]) . ".txt");

  66          }

  67          else

  68              header("location: ./?page=".$_POST["page"]."&action=edit&error=1");

  69      }

  70  // Lecture et analyse du modèle de page

  71      if (! $file = @fopen("template.html", "r"))

  72          die("'template.html' is missing!");

  73      $template = fread($file, filesize("template.html"));

  74      fclose($file);

  75  // Lecture du contenu et de la date de modification de la page

  76      if (($file = @fopen($PAGES_DIR . utf8_decode($PAGE_TITLE) . ".txt", "r")) || $action <> "") {

  77          if (file_exists($PAGES_DIR . utf8_decode($PAGE_TITLE) . ".txt"))

  78              $TIME = date("Y/m/d H:i", @filemtime($PAGES_DIR . utf8_decode($PAGE_TITLE) . ".txt") + $LOCAL_HOUR * 3600);

  79      $CONTENT = "\n" . @fread($file, @filesize($PAGES_DIR . utf8_decode($PAGE_TITLE) . ".txt")) . "\n";

  80          // Restaurer une page

  81          if (isset($_GET["page"]) && isset($gtime) && $_GET["restore"] == 1)

  82              if ($file = @fopen($BACKUP_DIR . $PAGE_TITLE . "/" . $gtime, "r"))

  83                  $CONTENT = "\n" . @fread($file, @filesize($BACKUP_DIR . $PAGE_TITLE . "/" . $gtime)) . "\n";

  84          @fclose($file);

  85          $CONTENT = preg_replace("/\\$/Umsi", "&#036;", $CONTENT);

  86          $CONTENT = preg_replace("/\\\/Umsi", "&#092;", $CONTENT);

  87      }

  88      else {

  89          if (!file_exists($PAGES_DIR . $PAGE_TITLE . ".txt"))

  90              $CONTENT = "\n" . $DEFAULT_CONTENT;

  91          else

  92              $action = "edit";

  93      }

  94  // Déterminer le mode d'accès

  95      if ($action == "edit" || $action == "search" || $action == "recent")

  96          $html = preg_replace('/{EDIT}/', $EDIT_BUTTON, $template);

  97      elseif (is_writable($PAGES_DIR . $PAGE_TITLE . ".txt") || !file_exists($PAGES_DIR . $PAGE_TITLE . ".txt"))

  98          $html = preg_replace('/{EDIT}/', "<a href=\"./?page=".$PAGE_TITLE."&amp;action=edit\" accesskey=\"5\" rel=\"nofollow\">$EDIT_BUTTON</a>", $template);

  99      else

 100          $html = preg_replace('/{EDIT}/', $PROTECTED_BUTTON, $template);

 101      if ($action == "recent")

 102          $html = preg_replace('/{RECENT_CHANGES}/', $RECENT_CHANGES, $html);

 103      else

 104          $html = preg_replace('/{RECENT_CHANGES}/', "<a href=\"./?action=recent\" accesskey=\"3\">$RECENT_CHANGES</a>", $html);

 105  // Remplacer les variables par des valeurs (dans le style de page)

 106      $html = preg_replace('/{PAGE_TITLE_BRUT}/', $PAGE_TITLE, $html);

 107      if ($action != "" && $action != "recent" && $action != "search")

 108         $html = preg_replace('/{PAGE_TITLE}/', "<a href=\"./?page=".$PAGE_TITLE."\">".$PAGE_TITLE."</a>", $html);

 109      else

 110          $html = preg_replace('/{PAGE_TITLE}/', $PAGE_TITLE, $html);

 111      if ($PAGE_TITLE == $START_PAGE && $action <> "search")

 112          $html = preg_replace('/{HOME}/', $HOME_BUTTON, $html);

 113      else

 114          $html = preg_replace('/{HOME}/', "<a href=\"./?page=".$START_PAGE."\" accesskey=\"1\">$HOME_BUTTON</a>", $html);

 115      $html = preg_replace('/{WIKI_TITLE}/', $WIKI_TITLE, $html);

 116      $html = preg_replace('/{LAST_CHANGE}/', $LAST_CHANGES." :", $html);

 117      if ($action != "edit")

 118          $html = preg_replace('/{HELP}/', "", $html);    

 119      else

 120          $html = preg_replace('/{HELP}/', "(<a href=\"./?page=$HELP_BUTTON\" accesskey=\"2\" rel=\"nofollow\">$HELP_BUTTON</a>)", $html);

 121      $html = preg_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);

 122      if ($action == "edit") {

 123          $html = preg_replace('/{HISTORY}/', "/ <a href=\"?page=".$PAGE_TITLE."&amp;action=history\" accesskey=\"6\" rel=\"nofollow\">".$HISTORY_BUTTON."</a><br />", $html);

 124          $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\">";

 125          if ($PASSWORD != "" && $_COOKIE['AutorisationTigerWiki'] != md5($PASSWORD))

 126              $CONTENT .= $MDP." : <input type=\"password\" name=\"sc\" />";

 127          $CONTENT .= " <input type=\"submit\" value=\"$DONE_BUTTON\" accesskey=\"s\" /></p></form>";
 128          //Retrait d'un </div> avant le </form>

 129      }

 130      elseif ($action != "history")

 131          $html = preg_replace('/{HISTORY}/', "", $html);

 132  // Liste des versions historiques d'une page

 133      if ($action == "history" && !isset($gtime)) {

 134          $html = preg_replace('/{HISTORY}/', "/ ".$HISTORY_BUTTON, $html);

 135          $complete_dir = $BACKUP_DIR . $_GET["page"] . "/";

 136          if ($opening_dir = @opendir($complete_dir)) {

 137              while (false !== ($filename = @readdir($opening_dir)))

 138                  $files[] = $filename;

 139              sort ($files);

 140                 for($cptfiles = 2; $files[$cptfiles] != ''; $cptfiles++)

 141                  $affichage = $affichage."<a href=\"?page=".$_GET["page"]."&amp;action=history&amp;time=".$files[$cptfiles]."\" rel=\"nofollow\">".$files[$cptfiles]."</a><br />";

 142              $html = preg_replace('/{CONTENT}/', $affichage, $html);

 143          }

 144          else

 145              $html = preg_replace('/{CONTENT}/', $NO_HISTORY, $html);

 146      }

 147  // Affichage d'un fichier historique

 148      if ($action == "history" && isset($gtime)) {

 149          $complete_dir = $BACKUP_DIR . $PAGE_TITLE . "/";

 150          if ($file = @fopen($BACKUP_DIR . $PAGE_TITLE . "/" . $gtime, "r")) {

 151              $html = preg_replace('/{HISTORY}/', "/ <a href=\"?page=".$PAGE_TITLE."&amp;action=history\" rel=\"nofollow\">".$HISTORY_BUTTON."</a> (<a href=\"?page=".$PAGE_TITLE."&amp;action=edit&amp;time=".$gtime."&amp;restore=1\" rel=\"nofollow\">".$RESTORE."</a>)", $html);

 152              $CONTENT = @fread($file, @filesize($complete_dir . $gtime)) . "\n";

 153            }

 154            else

 155              $html = preg_replace('/{HISTORY}/', "/ <a href=\"?page=".$PAGE_TITLE."&amp;action=history\" rel=\"nofollow\">".$HISTORY_BUTTON."</a> (-)", $html);

 156      }

 157  // Erreur du mot de passe

 158      if ($_GET['error'] == 1)

 159          $html = preg_replace('/{ERROR}/', $ERROR, $html);

 160      else

 161          $html = preg_replace('/{ERROR}/', "", $html);

 162  // Effacement du cookie

 163      if ($_GET['erasecookie'] == 1)

 164          setcookie('AutorisationTigerWiki');

 165  // Page de recherche

 166      if ($action == "search") {

 167          $dir = opendir(getcwd() . "/$PAGES_DIR");

 168          while ($file = readdir($dir)) {

 169              if (preg_match("/.txt/", $file)) {

 170                  $handle = fopen($PAGES_DIR . $file, "r");

 171                  @$content = fread($handle, filesize($PAGES_DIR . $file));

 172                  fclose($handle);

 173                  if (preg_match("/$_GET[query]/i", $content) || preg_match("/$_GET[query]/i", "$PAGES_DIR/$file")) {

 174                      $file = substr($file, 0, strlen($file) - 4);

 175                      $CONTENT .= "<a href=\"./?page=".utf8_encode($file)."\">".utf8_encode($file)."</a><br />";

 176                  }

 177              }

 178          }

 179      }

 180  // Changements récents

 181      elseif ($action == "recent") {

 182          $dir = opendir(getcwd() . "/$PAGES_DIR");

 183          while ($file = readdir($dir))

 184              if (preg_match("/.txt/", $file))

 185                  $filetime[$file] = filemtime($PAGES_DIR . $file);

 186          arsort($filetime);

 187          $filetime = array_slice($filetime, 0, 10);

 188          foreach ($filetime as $filename => $timestamp) {

 189              $filename = substr($filename, 0, strlen($filename) - 4);

 190              $CONTENT .= "<a href=\"./?page=".utf8_encode($filename)."\">".utf8_encode($filename)."</a> (" . strftime("$TIME_FORMAT", $timestamp + $LOCAL_HOUR * 3600) . ")<br />";

 191          }

 192      }

 193  // Formatage de page

 194      elseif ($action <> "edit") {

 195  

 196          if (preg_match("/%html%\s/", $CONTENT))     

 197  

 198              $CONTENT = preg_replace("/%html%\s/", "", $CONTENT);

 199          else {

 200              $CONTENT = htmlentities($CONTENT);

 201              $CONTENT = preg_replace("/&amp;#036;/Umsi", "&#036;", $CONTENT);

 202              $CONTENT = preg_replace("/&amp;#092;/Umsi", "&#092;", $CONTENT);

 203              $CONTENT = preg_replace("/\^(.)/Umsie", "'&#'.ord('\\1').';'", $CONTENT);

 204              $CONTENT = preg_replace('#\[(.+)\|([0-9a-zA-Z\.\'\s\#/~\-_%=\?\&amp;,\+]*)\]#U', '<a href="$2" class="url">$1</a>', $CONTENT);

 205              $CONTENT = preg_replace('#\[(.+)\|h(ttps?://[0-9a-zA-Z\.\#/~\-_%=\?\&amp;,\+]*)\]#U', '<a href="xx$2" class="url">$1</a>', $CONTENT);

 206              $CONTENT = preg_replace('#\[h(ttps?://[0-9a-zA-Z\.\&amp;\#\:/~\-_%=?]*\.(jpeg|jpg|gif|png))\]#i', '<img src="xx$1" />', $CONTENT);

 207              $CONTENT = preg_replace('#\[([0-9a-zA-Z\.\&amp;\#\:/~\-_%=?]*\.(jpeg|jpg|gif|png))\]#i', '<img src="$1" />', $CONTENT);

 208              $CONTENT = preg_replace('#(https?://[0-9a-zA-Z\.\&amp;\#\:/~\-_%=?]*)#i', '<a href="$0" class="url">$1</a>', $CONTENT);

 209              $CONTENT = preg_replace('#xxttp#', 'http', $CONTENT);

 210              preg_match_all("/\[([^\/]+)\]/U", $CONTENT, $matches, PREG_PATTERN_ORDER);

 211              foreach ($matches[1] as $match)

 212                  if (file_exists(html_entity_decode($PAGES_DIR."$match.txt")))

 213                      $CONTENT = str_replace("[$match]", "<a href=\"./?page=".$match."\">$match</a>", $CONTENT);

 214                  else

 215                      $CONTENT = str_replace("[$match]", "<a class=\"pending\" href=\"./?page=".$match."\">$match</a>", $CONTENT);

 216              $CONTENT = preg_replace('#(\[\?(.+)\]*)#i', '<a href="http://fr.wikipedia.org/wiki/$0" class="url">$0</a>', $CONTENT);

 217              $CONTENT = preg_replace('#([0-9a-zA-Z\./~\-_]+@[0-9a-z\./~\-_]+)#i', '<a href="mailto:$0">$0</a>', $CONTENT);

 218              $CONTENT = preg_replace('/^\*\*\*(.*)(\n)/Um', "<ul><ul><ul><li>$1</li></ul></ul></ul>$2", $CONTENT);

 219              $CONTENT = preg_replace('/^\*\*(.*)(\n)/Um', "<ul><ul><li>$1</li></ul></ul>$2", $CONTENT);

 220              $CONTENT = preg_replace('/^\*(.*)(\n)/Um', "<ul><li>$1</li></ul>$2", $CONTENT);

 221              $CONTENT = preg_replace('/^\#\#\#(.*)(\n)/Um', "<ol><ol><ol><li>$1</li></ol></ol></ol>$2", $CONTENT);

 222              $CONTENT = preg_replace('/^\#\#(.*)(\n)/Um', "<ol><ol><li>$1</li></ol></ol>$2", $CONTENT);

 223              $CONTENT = preg_replace('/^\#(.*)(\n)/Um', "<ol><li>$1</li></ol>$2", $CONTENT);

 224  

 225              $CONTENT = preg_replace('/(<\/ol>\n*<ol>|<\/ul>\n*<ul>)/', "", $CONTENT); 

 226              $CONTENT = preg_replace('/(<\/ol>\n*<ol>|<\/ul>\n*<ul>)/', "", $CONTENT); 

 227              $CONTENT = preg_replace('/(<\/ol>\n*<ol>|<\/ul>\n*<ul>)/', "", $CONTENT); 

 228  

 229  

 230              $CONTENT = preg_replace('/^!!!(.*)(\n)/Um', '<h1>$1</h1>$2', $CONTENT);

 231              $CONTENT = preg_replace('/^!!(.*)(\n)/Um', '<h2>$1</h2>$2', $CONTENT);

 232              $CONTENT = preg_replace('/^!(.*)(\n)/Um', '<h3>$1</h3>$2', $CONTENT);

 233              while (preg_match('/^  /Um', $CONTENT))

 234                $CONTENT = preg_replace('/^( +) ([^ ])/Um', '$1&nbsp;&nbsp;&nbsp;&nbsp;$2', $CONTENT);

 235                $CONTENT = preg_replace('/^ /Um', '&nbsp;&nbsp;&nbsp;&nbsp;', $CONTENT);

 236                $CONTENT = preg_replace('/----*(\r\n|\r|\n)/m', '<hr />', $CONTENT);

 237                $CONTENT = preg_replace('/\n/', '<br />', $CONTENT);

 238                $CONTENT = preg_replace('#</ul>(<br />)*#', "</ul>", $CONTENT);

 239                $CONTENT = preg_replace('#</ol>(<br />)*#', "</ol>", $CONTENT);

 240  

 241                $CONTENT = preg_replace('#</li><ul><li>*#', "<ul><li>", $CONTENT);

 242                $CONTENT = preg_replace('#</ul></ul>*#', "</ul></li></ul>", $CONTENT);

 243                $CONTENT = preg_replace('#</ul></ul>*#', "</ul></li></ul>", $CONTENT);

 244                $CONTENT = preg_replace('#</li></ul><li>*#', "</li></ul></li><li>", $CONTENT);

 245  

 246                $CONTENT = preg_replace('#</li><ol><li>*#', "<ol><li>", $CONTENT);

 247                $CONTENT = preg_replace('#</ol></ol>*#', "</ol></li></ol>", $CONTENT);

 248                $CONTENT = preg_replace('#</ol></ol>*#', "</ol></li></ol>", $CONTENT);

 249                $CONTENT = preg_replace('#</li></ol><li>*#', "</li></ol></li><li>", $CONTENT);

 250  

 251                $CONTENT = preg_replace('#(</h[123]>)<br />#', "$1", $CONTENT);

 252                $CONTENT = preg_replace("/{(.+)}/Ue", "'<pre><code>' . preg_replace('#<br />#', '', '\\1') . '</code></pre>'", $CONTENT);

 253                $CONTENT = preg_replace("/'--(.*)--'/Um", '<del>$1</del>', $CONTENT);

 254                $CONTENT = preg_replace("/'''''(.*)'''''/Um", '<strong><em>$1</em></strong>', $CONTENT);

 255                $CONTENT = preg_replace("/'''(.*)'''/Um", '<strong>$1</strong>', $CONTENT);

 256                $CONTENT = preg_replace("/''(.*)''/Um", '<em>$1</em>', $CONTENT); 

 257                $CONTENT = substr($CONTENT, 6, strlen($CONTENT) - 6);

 258                $CONTENT = html_entity_decode($CONTENT);

 259          }

 260      }

 261      if ($action != "" && $action != "edit" || (!file_exists($PAGES_DIR . $PAGE_TITLE . ".txt")))

 262          $TIME = "-";

 263      $html = preg_replace("/{CONTENT}/", $CONTENT, $html);

 264      $html = preg_replace("/{LANG}/", $LANG, $html);

 265      $html = preg_replace("/{WIKI_VERSION}/", $WIKI_VERSION, $html);

 266      $html = preg_replace("/{CHARSET}/", $CHARSET, $html);

 267      $html = preg_replace('/{TIME}/', $TIME, $html);

 268      $html = preg_replace('/{DATE}/', $datetw, $html);

 269      $html = preg_replace('/{IP}/', $_SERVER['REMOTE_ADDR'], $html);

 270      if ($_COOKIE['AutorisationTigerWiki'] != "")

 271          $html = preg_replace('/{COOKIE}/', '-- <a href="./?page='.$PAGE_TITLE.'&erasecookie=1" rel=\"nofollow\">'.$ERASE_COOKIE.'</a>', $html);

 272      else

 273          $html = preg_replace('/{COOKIE}/', '', $html);

 274  // Affichage de la page

 275      echo utf8_decode($html);

 276  ?>

title

Description

title

Description

title

Description

title

title

Body