#!/usr/bin/perl

use CGI qw/:standard/;

# WikiPl version 0.2
# Copyright(c) 2002 Alexandre Janon <alex14fr@yahoo.fr>
# This is a small wiki written in Perl
# Released under GPL <http://www.gnu.org/licenses/gpl.html>
# To install this wiki:
#    1: create a directory for the wiki
#    2: configure the variables below
#    3: make the script executable 
#        >  chmod 755 wiki.pl
#    4: create a first page from your web browser
#        http://www.mysite.org/cgi-bin/wiki.pl?edit_page=HomePage
# general information about wikis can be found in many places on the
# web, for example http://c2.com/cgi/wiki


########################################################
#         start of configuration settings                #
########################################################

$programname = $0;
$programname =~ s"\/.*\/"";
# the above 2 lines should detect the name of thism file. 
# If not (a possible cause of 'file not found' errors), uncomment 
# the line below (i.e. remove the initial '#') and replace wiki.pl
# with the correct filename. 
#$programname = "wiki.pl";

$dbfile = "/tmp/WikiDB";
$cachedir="/tmp/wikiCache/";
#replace /tmp/ with a directory of your choice in both these lines
#you should ensure that the directories exist.

$title = "Wiki"; #change this to anything you want to call your wiki 
@allowedhtml = ("b", "i", "s", "u", "br", "h1", "h2", "h3", "h4", "center", "div", "em", "tt", "blockquote");
$style = "body { font-family: Verdana; }
          a:link { color: blue; text-decoration: none; }
          a:hover { background-color: #c6c6c6; text-decoration: underline; }
          a:visited { color: blue; text-decoration: none; }
          a:visited:hover { background-color: #c6c6c6; text-decoration: underline; }
          .page { background-color: #efefef }";

$defaultpage="HomePage";


########################################################
#         end of configuration settings                #
########################################################

if(!dbmopen(%db, $dbfile, 0666)) {
	print header(-server => 'WikiPl', -status => '666 Server Error');
	print "<h1>Error</h1>Can't open wiki db $dbfile";
	exit;
}

print header(-server => 'WikiPl');
print "<style type=text/css>$style</style>";
sub printbar {
	print "<form action=? method=get>Goto <input type=text name=page> Search <input type=text name=search><input type=submit value=Ok></form><p><small><i>Powered by <a href=http://alex14.home.dyndns.org/wikipl.html>WikiPl</a></i></small>";
}

if(param('page') ne "") {
	$pgname=param('page');
	if(open(CACHED, "<".$cachedir.$pgname."_cache.html")) {
		while(<CACHED>) {
			$txt=$_;
			print;
		}
	} else {
		open(CACHED, ">".$cachedir.$pgname."_cache.html");
		gencache($pgname, CACHED);
		close CACHED;
		open(CACHED, "<".$cachedir.$pgname."_cache.html");
		while(<CACHED>) {
			$txt=$_;
			print;
		}
	}

	unless($pgname=~/^\$/ or $txt eq "") {
		print "<p><a href=?edit_page=$pgname>Edit this page</a> | This page has been visited ".$db{"\$".$pgname."_visits"}." times | Revision: ".$db{"\$".$pgname."_currev"}." | <a href=?page_hist=$pgname>Page history</a>";
		$db{"\$".$pgname."_visits"}++;
	}
	printbar;
	dbmclose %db;
	exit;
}

sub gencache {
	my $pgname = shift;
	my $FD = shift;
	print "<!-- Wasn't in cache ! -->";
	print $FD "<h1><a href=?>$title</a>: $pgname</h1><p>";
	$txt=$db{$pgname};
	if($txt eq "") {
		print $FD "This page doesn't exist. You will probably <a href=?edit_page=$pgname>edit it</a> or <a href=?search=$pgname>search for similar ones</a>.";
	}
	$txt =~ s/\n/<br>/g;


	for $a(@allowedhtml) {
		$txt =~ s/<$a ([a-zA-Z0-9\=\"\' ]{0,})>/\$$a $1\$/g;
		$txt =~ s/<$a>/\$$a\$/g;
		$txt =~ s/<\/${a}>/\$\/$a\$/g;
	}
	$txt =~ s/</<!-- HTML REMOVED: /g;
	$txt =~ s/>/ -->/g;
	for $a(@allowedhtml) {
		$txt =~ s/\$$a ([a-zA-Z0-9\=\"\' ]{0,})\$/<$a $1>/g;
		$txt =~ s/\$$a\$/<$a>/g;
		$txt =~ s/\$\/$a\$/<\/$a>/g;
	}

	$txt =~ s/\[img: (http|ftp|https)([a-zA-Z0-9\:\/\\\?\.\_\+\-\=\&]+)\]/<img src=$1$2>/g;
         #image links img: http://somewhere.com/image.jpg
	$txt =~ s/\[([A-Za-z0-9]+)\]/<a href=?page=$1>$1<\/a>/g;
         #wikilinks written [likethis]

        $txt=~ s/([A-Z][a-z]*[A-Z][a-z]*)\b/<a href=?page=$1>$1<\/a>/g;
         #wikilinks written LikeThis

        $txt=~s/\*(.*?)\*/<b>$1<\/b>/g;
        $txt=~s/\_(.*?)\_/<i>$1<\/i>/g;
         # *bold text* and _italics_ (bold text in asterisks)


        $txt =~ s/\[url: (http|ftp|https)([a-zA-Z0-9\:\/\\\?\.\_\+\-\=\&]+)\]/<a href=$1$2>$1$2<\/a>/g;
         #URLs url: http://www.someewhere.com


	print $FD "<center><table><tr><td width=\"1324\" class=page>$txt</td></tr></table></center>";

}

if(param('edit_page') ne ""){
	$pgname=param('edit_page');
	print "<h1><a href=?>$title</a>: Edit <a href=?page=$pgname>$pgname</a></h1>";
	print "<form method=post action=".$programname."><input type=hidden name=save_page value=".$pgname."><textarea name=text rows=20 cols=60>".$db{$pgname}."</textarea><p><input type=submit value=Submit></form>";

	dbmclose %db;
	exit;
}

if(param('save_page')) {
	if(param('save_page')=~/^\$/) {
		print "error: You can't edit system pages like this one !";
		dbmclose %db;
		exit;
	}
	if($db{"\$".param('save_page')."_currev"}) {
		$db{"\$".param('save_page')."_rev".$db{"\$".param('save_page')."_currev"}}=$db{param('save_page')};
	}
	$db{param('save_page')}=param('text');
	$db{"\$".param('save_page')."_currev"}++;
	$db{"\$".param('save_page')."_rev".$db{"\$".param('save_page')."_currev"}."_date"}=localtime;
	$db{"\$".param('save_page')."_rev".$db{"\$".param('save_page')."_currev"}."_ip"}=$ENV{'REMOTE_ADDR'};
	print "Changes saved. <a href=?page=".param('save_page').">Back</a>";
	unlink($cachedir.param('save_page')."_cache.html");
	dbmclose %db;
	exit;
}

if(param('list_pages')) {
	while(($name,$content)=each %db) {
		print "<a href=?page=".$name.">".$name."</a><br>";
	}
	dbmclose %db;
	exit;
}

if(param('page_hist')) {
	$pgname = param('page_hist');
	print "<h1><a href=?>$title</a>: History of <a href=?page=$pgname>$pgname</a></h1>";
	$rev=$db{"\$".$pgname."_currev"}-1;
	print "<table border=1><tr><td>Revision #</td><td>Date</td><td>Author IP</td></tr>";
	for (1..$rev) {
		print "<tr><td><a href=?page=\$".$pgname."_rev".$_.">".$_."</td><td>".$db{"\$".$pgname."_rev".$_."_date"}."</td><td>".$db{"\$".$pgname."_rev".$_."_ip"}."</td></tr>";
	}

	print "<tr><td><a href=?page=$pgname>Current</a></td><td>".$db{"\$".$pgname."_rev".$db{"\$".$pgname."_currev"}."_date"}."</td><td>".$db{"\$".$pgname."_rev".$db{"\$".$pgname."_currev"}."_ip"}."</td></tr>";

	print "</table>";
	printbar;
	dbmclose %db;
	exit;
}

if(param('search')) {
	$search = param('search');
	print "<h1><a href=?>$title</a>: Search results for '$search'</h1>";
	$cnt=0;
	while(($name,$val)=each %db) {
		if( ($val=~/$search/i or $name=~/$search/i) and (!($name=~/^\$/))) {
			$cnt++;
			$nam=$name;
			print "<a href=?page=$name>$name</a><p>";
		}
	}

	if($cnt eq 0) {
		print "No result found... <a href=http://www.google.com/search?q=$search>Google is your friend :)</a>";
	}
	if($cnt eq 1) {
		print "<p><b>Only one result found. Redirecting you to the result page...<meta http-equiv=Refresh content='1;url=?page=$nam'></b>";
	}

	printbar;
	dbmclose %db;
	exit;
}


dbmclose %db;
print "<html><head><meta http-equiv=\"Refresh\" content=\"0;url=?page=$defaultpage\"></head></html>";
