wiki.php

<?php
  error_reporting
(error_reporting() & ~E_NOTICE);
  if (
$_SERVER["PHP_SELF"] == "/view.php") {
    if (isset(
$_GET["page"]) && $_GET["page"] != "view.php") {
      
$page $_GET["page"];
    } else {
      
$page "wikihome";
    }
    
$link "http://{$_SERVER["HTTP_HOST"]}/" urlencode($page);
    
header("HTTP/1.1 301 Moved Permanently");
    
header("Location: $link");
    die(
"The URL format for Flouri has changed.  Click <a href='$link'>here</a> if you are not automatically redirected.");
  }
  if (
$_SERVER["PHP_SELF"] == "/robots.txt" ||
      
$_SERVER["PHP_SELF"] == "/favicon.ico") {
    
$filename substr($_SERVER["PHP_SELF"],1);
    if (
file_exists($filename)) {
      if (
$filename == "robots.txt")
        
header("Content-Type: text/plain");
      else
        
header("Content-Type: image/x-icon");
      
readfile($filename);
    } else {
      
header("HTTP/1.0 404 Not Found");
      echo(
"File not found.");
    }
    return;
  }
  
$link=mysql_connect();
  if (!
link)
    die(
"Could not connect to database; error: " mysql_error());
  if (!
mysql_select_db("flouridb")) {
    die(
"Could not select database; error: ".mysql_error());
  }
  require(
"inc/utils.php");
  require(
"inc/numberutils.php");
  require(
"inc/diffutils.php");
  require(
"inc/textutils.php");
  
$page substr($_SERVER["PHP_SELF"],1);
  if (!
$page) {
    
$page="wikihome";
    
$relocate=true;
  }
  if (
strpos($page,"//") !== FALSE) {
    
$page=ereg_replace("//*","/",$page);
    
$relocate=true;
  }
  if (
$page{0} == "/") {
    
$page=substr($page,1);
    
$relocate=true;
  }
  if (
$page{strlen($page)-1} == "/") {
    
$page=substr($page,0,strlen($page)-1);
    
$relocate=true;
  }
  
$test $_REQUEST["test"];
  
$resetcomments $_REQUEST["resetcomments"];
  
$setlastcomment $_REQUEST["setlastcomment"];
  
$setview $_REQUEST["setview"];
  
$setwikiname $_REQUEST["setwikiname"];
  
$setwikisig $_REQUEST["setwikisig"];
  
$setalign $_REQUEST["setalign"];
  
$setchangeorder $_REQUEST["setchangeorder"];
  
$showrev $_REQUEST["showrev"];
  if (isset(
$showrev)) {
    if (
strpos($showrev,"-") !== false) {
      
$minrev = (int)substr($showrev0strpos($showrev,"-"));
      
$maxrev = (int)substr($showrevstrpos($showrev,"-") + 1);
      if (
$minrev <= || $maxrev <= 0) {
    unset(
$minrev);
    unset(
$maxrev);
    unset(
$showrev);
      }
    } else {
      
$showrev = (int)$showrev;
      if (
$showrev <= 0)
    unset(
$showrev);
    }
  }
  
$revision = (int)$_REQUEST["revision"];
  if (
$revision <= 0)
    unset(
$revision);
  
$lastcomment $_COOKIE["lastcomment"];
  if (isset(
$lastcomment))
    
$lastcomment = (int)$lastcomment;
  
$view $_COOKIE["view"];
  
$wikiname $_COOKIE["wikiname"];
  
$wikisig $_COOKIE["wikisig"];
  
$align $_COOKIE["align"];
  
$auth $_COOKIE["auth"];
  if (
$_COOKIE["changeorder"] == "desc")
    
$changeorder "desc";
  else
    
$changeorder "asc";
  if(isset(
$setalign)) {
    
$align=$setalign;
    
setcookie("align",$align,time()+100000000,"/");
    
$relocate=true;
  }
  
$result mysql_query("select max(id) from changes");
  if (
$result === FALSE || mysql_num_rows($result) == 0)
    die(
"Database error in maxcomment");
  
$row mysql_fetch_row($result);
  
$maxc $row[0];
  if (
$lastcomment $maxc)
    unset(
$lastcomment);
  if(isset(
$resetcomments)) {
    
$lastcomment=$maxc;
    
setcookie("lastcomment",$maxc,time()+100000000,"/");
    
$relocate=true;
  } elseif(isset(
$setlastcomment)) {
    
$lastcomment=$setlastcomment;
    
setcookie("lastcomment",$lastcomment,time()+100000000,"/");
    
$relocate=true;
  }
  if(isset(
$setview)) {
    
$view=$setview;
    
setcookie("view",$view,time()+100000000,"/");
    
$relocate=true;
  }
  if(isset(
$setwikiname)) {
    
$wikiname=ereg_replace("[^A-Za-z0-9 ]","",$setwikiname);
    
setcookie("wikiname",$wikiname,time()+100000000,"/");
    
$relocate=true;
  }
  if(isset(
$setwikisig)) {
    
$wikisig=ereg_replace("[^A-Za-z0-9 ]","",$setwikisig);
    
setcookie("wikisig",$wikisig,time()+100000000,"/");
    
$relocate=true;
  }
  if(isset(
$setchangeorder)) {
    
$changeorder = ($setchangeorder=="desc" "desc" "asc");
    
setcookie("changeorder",$changeorder,time()+100000000,"/");
    
$relocate=true;
  }
  
$npage=normalize($page);
  if (
$relocate) {
    
header("Location: http://{$_SERVER['HTTP_HOST']}/$npage");
    return;
  }
  
$special $_SERVER["QUERY_STRING"];
  if (
$special != "edit" &&
      
$special != "commit" &&
      
$special != "authenticate" &&
      
$special != "preview")
    unset(
$special);
  if(
strpos($npage,"/") !== FALSE) {
    
$user=substr($npage,0,strpos($npage,"/"));
    
$result mysql_query("select password from users where name='$user'");
    if (
$result === FALSE)
      die(
"Database error in getuser");
    if(
mysql_num_rows($result)==0)
      unset(
$user);
    else if (
$special != "authenticate") {
      
$row mysql_fetch_row($result);
      if (
$row[0] !== NULL && (!isset($auth[$user]) ||
           
$auth[$user] != $row[0])) {
        
header("Location: http://".$_SERVER['HTTP_HOST'].
               
"/".urlencode($page)."?authenticate");
        return;
      }
    }
  }
  
header("Content-type: text/html; charset=utf-8");
  if (isset(
$special)) {
    include(
"inc/$special.php");
    return;
  }
  if (!
check_page($npage) && !check_files($npage))
    
header("HTTP/1.0 404 Not Found");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title><?php
  
if (isset($showrev))
    echo(
"Changes to ");
  echo(
strip_control(get_title($page)));
  if (isset(
$minrev) && isset($maxrev))
    echo(
" in revisions $minrev-$maxrev");
  elseif (isset(
$showrev))
    echo(
" in revision $showrev");
  elseif (isset(
$revision))
    echo(
", revision $revision");
?></title>
<?php if (isset($revision)) { ?>
<meta name="ROBOTS" content="NOINDEX,NOFOLLOW">
<?php } elseif ($npage=="recentchanges") { ?>
<meta name="ROBOTS" content="NOFOLLOW">
<?php ?>
<style type="text/css"><!--
a.ext{text-decoration:underline}
a.ext:hover{text-decoration:none}
a.int{text-decoration:none}
a.int:hover{text-decoration:underline}
a.broken{text-decoration:line-through}
a.broken:hover{text-decoration:underline}
img{border-width:2px;border-style:none;border-style:hidden}
img:hover{border-style:solid}
p{margin-bottom:6pt;margin-top:6pt}
p.text{text-indent:4em}
ol{margin:0}
ul{margin:0}
li.blank{list-style-type:none}
hr{margin-top:12pt;margin-bottom:0;text-align:center}
hr.wide{width:100%}
hr.medium{width:67%}
hr.narrow{width:33%}
div.indent{margin-left:4em}
div.center{text-align:center}
.oldrev{background-color:pink;text-decoration:line-through}
.newrev{background-color:lightgreen;text-decoration:underline}
.error{font-color:red}
.success{font-color:red}
.metatext{font-size:smaller}
--></style>
<link rel="SHORTCUT ICON" href="/favicon.ico" />
<META name="verify-v1" content="LZ8PGmM4ENnhsKCR1NZXAXfK7mYeugBI73BXUMIKWWE=" />
</head>
<body>
<?php
  
if ($test) {
    
$result mysql_query("select id,page,revision,changetype,content,oldtitle from changes order by id desc");
    while((
$row mysql_fetch_row($result)) !== false) {
      
$revid $row[0];
      
$revpage $row[1];
      
$revnum $row[2];
      
$revtype $row[3];
      
$revcontent $row[4];
      
$revoldtitle $row[5];
      if (
$revtype != 'e' && $revtype != 'm')
    continue;
      if (
$revoldtitle === null)
    continue;
      
$revtext get_revision_text($revpage$revnum);
      
$diffarray texttodiff($revcontent$revtext);
      if (
$revid == 33681) {
    echo(
"<!-- diffarray:\n");
    
var_dump($diffarray);
    echo(
"\n-->\n");
      }
      
$fail false;
      for (
$idx 0$idx count($diffarray); $idx++) {
    if (
$diffarray[$idx][1] !== null) {
      
$fail true;
      break;
    }
      }
      if (
$fail)
    continue;
      echo(
"<!-- Title change: id $revid, page $revpage, revision $revnum, content $revcontent-->\n");
    }
  }

  if (isset(
$minrev) && isset($maxrev)) {
    
$title get_revision_title($page$maxrev);
    
$oldtitle get_revision_title($page$minrev-1);
  } elseif (isset(
$showrev)) {
    
$title get_revision_title($page$showrev);
    
$oldtitle get_revision_title($page$showrev-1);
  } elseif (isset(
$revision)) {
    
$title get_revision_title($page$revision);
    
$oldtitle get_revision_title($page$revision-1);
  } else
    
$title get_title($page);
  echo(
"<h1>" format_title($title));
  if (isset(
$oldtitle) && $title != $oldtitle)
    echo(
"<br />\n<span style=\"font-size:smaller\"><i>(was: </i>" format_title($oldtitletrue) . "<i>)</i></span>");
  echo(
"</h1>\n");

  if(!(
$wikiname && $wikisig) && $_SERVER["HTTP_REFERER"] == "http://flouri.shyou.org/$page?edit") {
    echo(
'<p><span class="error">Your changes were not saved.  You must first set your Wiki Name and a signature before you can edit pages on the Wiki.  Press the Back button on your browser and follow the link from the Edit page.</font></p>');
  }
  if(
$npage=="wikiname" || $npage=="recentchanges")
    include(
"inc/$npage.php");
  if(
check_files($page)) {
    if(
get_image($page)) {
      
printf("<div class=\"metatext\">(<a href=\"?setview=%s\" class=int>%s Images</a>)</div>\n",
          (
$view=="full"?"quick":"full"), ($view=="full"?"Small":"Full-size"));
      
printf("<a href=\"%s\"><img src=\"%s\" %s hspace=20></A>\n",
          
get_image($page),($view=="full"?get_image($page):get_small($page)), 
          (
$align=="vert"?"":"align=left"));
    }
    if(
get_text($page)) {
      
$utext=file_get_contents(get_text($page));
      if (
$utext !== false) {
        echo(
"<p>\n".encode_text(strtr($utext,array("\r\n"=>"\n",
                                                    
"\r"=>"\n")),$page));
        if(
get_image($page))
          echo(
"<br />\n<span class=metatext>(<a href=\"?setalign=".
             (
$align=="vert"?"horiz":"vert")."\" class=int>Display text ".
             (
$align=="vert"?"beside":"below")." image</A>)</span>\n");
      }
      unset(
$utext);
    }
    if(
get_php_source($page))
      
highlight_file(get_php_source($page));
    echo 
"<br clear=left>\n";
  }
  if (isset(
$showrev)) {
    if (isset(
$minrev) && isset($maxrev))
      
$result mysql_query("select changetype,content from changes where page='$npage' and revision <= $maxrev and revision >= $minrev order by revision desc");
    else
      
$result mysql_query("select changetype,content from changes where page='$npage' and revision = $showrev");
    if (!
mysql_num_rows($result))
      echo(
"Revision not found.");
    else {
      
$row mysql_fetch_row($result);
      
$type $row[0];
      
$content $row[1];
      if (
$type == 'c')
    echo(
"<div class='newrev'>" encode_text($content$page) . "</div>");
      elseif (
$type == 'd')
    echo(
"<div class='oldrev'>" encode_text($content$page) . "</div>");
      elseif (
$type == 'm' || $type == 'e') {
    
$text get_revision_text($page,$showrev);
    
$diffarray texttodiff($content$text);
        echo(
encode_diff($diffarray$page));
      }
    }
  } else {
    if (isset(
$revision))
      
$text get_revision_text($page,$revision);
    else
      
$text get_page_text($page);
    if (
$text !== FALSE) {
      echo(
"<div id='pagecontent'>\n");
      echo(
encode_text($text,$page));
      echo(
"</div>\n");
    }
  }
  echo 
"<div><hr />\n";
  if(isset(
$lastcomment) && $lastcomment<$maxc) {
    
$result mysql_query("select count(*) from changes as t1 left join changes as t2 on t1.id = t2.id-1 where t1.id > $lastcomment and not (t1.page <=> t2.page and t1.wikiname <=> t2.wikiname and not (left(t2.content,1) <=> 'd'))");
    if (
$result === false || mysql_num_rows($result) == 0)
      
$numcomments $maxc $lastcomment;
    else {
      
$row mysql_fetch_row($result);
      
$numcomments $row[0];
    }
    echo 
"There ha".(($numcomments==1)?"s":"ve")." been <a href=\"/recentchanges\" class=int>".number_name($numcomments)." revision".(($numcomments==1)?"":"s").
          
"</a> to the Wiki since you last edited it.  <span class=metatext>(<a href=\"?resetcomments=yes\" class=int>Reset this counter</A>)</span><br />";
  }
  echo 
"<a href=\"?edit\" class=int>Edit this page</a>!";
  if (!isset(
$showrev) || isset($minrev))
    
$result mysql_query("select max(revision) from changes where page='$npage'");
  else
    
$result mysql_query("select max(revision) from changes where page='$npage' and revision < $showrev");
  
$row mysql_fetch_row($result);
  if (
$row[0]) {
    if (!isset(
$showrev) || isset($minrev))
      echo(
"<br />View the <a href=\"?showrev={$row[0]}\" class=int>most recent revision</a> to this page.\n");
    else
      echo(
"<br />View the <a href=\"?showrev={$row[0]}\" class=int>previous revision</a> to this page.\n");
  }
  if(!isset(
$lastcomment)) 
    echo(
"<br />".format_text("If you haven't yet, read the {Wiki Introduction}."));
  
$levels 0;
  while (
strrpos($npage,"/") !== FALSE) {
    
$npage substr($npage,0,strrpos($npage,"/"));
    
$levels++;
    if (
$npage == $user)
      echo(
"<br />".format_text("Return to user's home page: {{$user}}"));
    elseif (
check_page($npage))
      echo(
"<br />".format_text("Up ".number_name($levels)." level".($levels==1?"":"s")." to {{$npage}}."));
  }
  if(
$page!="wikihome")
    echo(
"<br />".format_text("Return to the {Wiki Home}."));
  else {
?>
<br /><pre class="metatext">
 ___,        
(     /)                   /)
  |- // ___    __  .  _   |/   _  ____
 /) _\_(_)(_(_/ (_/_./_)_// )_/(_((_)(_(_.org
                               (/
</pre>
<?php
  
}
?>
</div>
<div class="metatext" style="font-style:italic">Hosted by <a href="http://cheri.shyou.org/">Shyou.org Webservices</a>.</div>
</body>
</html>


Notes:

This file is a bit bulky. Find some way to redistribute its workload.


Edit this page!
View the most recent revision to this page.
If you haven't yet, read the Wiki Introduction.
Return to user's home page: Flouri
Return to the Wiki Home.
Hosted by Shyou.org Webservices.