Time | "; print "IP | " if($showip); print "Action | Page | Version |
---|---|---|---|---|
".×tamp2iso($fields[0])." | "; print "$fields[1] | " if($showip); print "$fields[2] | $fields[3] | $fields[4] |
"; $|=1; my $safedatadir=$datadir; $safedatadir =~ s/'/'"'"'/g; open(DIFF,"cd '$datadir';$diffcommand $page-$version $page|") or die "popen: $!"; while(defined($text=\n"; &printfooter("$page: $version vs. Current",0,"","",""); } sub editpage($$) { my ($page,$version)=@_; &nopermission("You are not allowed to edit a particular version of a page.") if($version); &nopermission("You cannot edit blobs") if(&blobtype($page)); my $curversion=&getpageversion($page,1); my $cgi=new CGI; my $text=$cgi->param("text"); if($text) { if($text =~ /param("curversion") or 0); &conflictingupdate($page,"$page=edit","text",$text) if($curversion ne $oldcurversion); my $newversion=&newpageversion($page,$text,1); &setpageversion($page,$newversion); } my $modtime=&modtime($page); my $action=$modtime?"Edit":"Create"; &printheader("$action $page",0,$modtime,"",""); print <<"EOD"; EOD if($modtime) { my $subpage=&blobtype($page)?"=show":""; print "\n"; } &printfooter("$action $page",0,$modtime,"",""); } sub pageversions($) { my ($page)=@_; my $subpage=&blobtype($page)?"=show":""; my $currentversion=&getpageversion($page,0); &printheader("Versions of $page",0,0,"",""); foreach my $version (sort {$b<=>$a} grep s/^$page-//s,pagelist(0,1)) { print "$version"; print " (current version)" if($version eq $currentversion); print ")) { print &htmlquote($text); } close(DIFF); print "
You have reverted to the $version version of $page.
\n"; &printfooter("Revert Page",0,0,"",""); } sub showblob($$) { my ($page,$version)=@_; my $file=$page; $file.="-$version" if($version); my $type=&blobtype($page); &nopermission("This is not a blob page") unless($type); my $modtime=&modtime($file); &nopermission("You cannot view a non-blob as an blob") unless($blobtypes{$type}); ¬found($page) unless(open(PAGE,"<$datadir/$file")); print "Content-type: $blobtypes{$type}\n"; print "Last-Modified: $modtime\n"; print "\n"; printFiles are limited to $maxpagesize bytes.
\n" if($maxpagesize); &printfooter("Upload $page",0,"","",""); } } ##### ##### Utility subroutines ##### my $mostrecentchangelogentry=""; sub addchangelogentry($$$) { my ($action,$page,$version)=@_; my $remoteip=$ENV{REMOTE_ADDR}; $remoteip="127.0.0.1" unless(defined $remoteip); my $timestamp=&secs2timestamp(time()); if($action eq "revert" and ($mostrecentchangelogentry eq "$timestamp\t$remoteip\tcreate\t$page\t$version\n" or $mostrecentchangelogentry eq "$timestamp\t$remoteip\tupdate\t$page\t$version\n")) { # We do nothing, because we just logged a create/update for this page } else { open(RECENT,">>$datadir/RecentChanges") or &nopermission("You are not allowed to modify this wiki."); print RECENT "$timestamp\t$remoteip\t$action\t$page\t$version\n"; close(RECENT); } $mostrecentchangelogentry="$timestamp\t$remoteip\t$action\t$page\t$version\n"; } sub conflictingupdate($$$$) { my ($page,$submiturl,$name,$value)=@_; &printheader("$page: Conflicting Modifications Made",0,"","",""); print <<"EOD";The page you were trying to change was modified while you were changing it. If you are sure you wish to override those changes and use the version you just submitted, you can do so by submitting this page.
\n"; &printfooter("$page: Conflicting Modifications Made",0,"","",""); exit 0; } sub errorinedit($) { my ($reason)=@_; print "Status: 406\n"; printheader("Error in Edit",0,0,"",""); print htmlquote($reason); printfooter("Error in Edit",0,0,"",""); } sub getpageversion($$) { my ($page,$allowmissingpage)=@_; my $link=readlink("$datadir/$page"); $link="$page-0" if($allowmissingpage and not $link); notfound("") unless($link); die unless($link =~ s#^$page-##s); return $link; } sub htmlquote($) { my ($str)=@_; $str.=""; # Make sure we aren't dealing with a read-only string $str =~ s/&/&/gs; $str =~ s/</gs; $str =~ s/>/>/gs; $str =~ s/"/"/gs; return $str; } sub blobtype($) { my ($str)=@_; my $type=""; $type=$1 if($str =~ /^(?:[A-Z][a-z]+){2,}\.([a-z]+)$/s); $type="jpg" if("jpeg" eq $type); $type="" unless($blobtypes{$type}); return $type; } sub iswikiword($) { my ($str)=@_; my $blobs=join("|",keys %blobtypes); return 1 if($str =~ /^(?:[A-Z][a-z]+){2,}(?:\.(?:$blobs))?$/s); return 0; } sub modtime($) { my ($page)=@_; my @stat=lstat "$datadir/$page"; return "" unless(@stat); return &prettytime($stat[9]); } sub newpageversion($$) { my ($page,$data,$istext)=@_; if($istext) { $data =~ s#\r\n?#\n#gs; $data =~ s#[ \t]+\n#\n#gs; $data =~ s#^\s+##s; $data =~ s#\s+$##s; } &nopermission("You are not allowed to post ".($istext?"pages":"files")." greater than $maxpagesize bytes.") if($maxpagesize and $maxpagesize$message
EOD exit 0; } sub notfound($) { my ($offertocreatepage)=@_; print <<"EOD"; Status: 404 File Not Found Content-type: text/html EOD print "The requested page was not found."; if($offertocreatepage) { print " Would you like to upload":"edit\">create"; print " one?"; } print "
"; exit 0; } sub pagelist($$) { my ($includespecialpages,$includeversions)=@_; my %pages=(); my $page; map {$pages{$_}=1} keys %specialpages if($includespecialpages); opendir(DATADIR,$datadir) or die "opendir: $!"; while($page=readdir(DATADIR)) { next if($page =~ /^\./s); unless($includeversions) { next if($page =~ /-/); } $pages{$page}=1; } closedir(DATADIR); return keys %pages; } sub permredirect($) { my ($url)=@_; print <<"EOD"; Status: 301 Moved Permanently Location: $url Content-type: text/htmlPlease go to $url.
EOD exit 0; } sub prettytime($) { my ($time)=@_; my @time=gmtime($time); $time[6]=(qw(Sun Mon Tue Wed Thu Fri Sat Sun))[$time[6]]; $time[4]=(qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$time[4]]; $time[5]+=1900; return sprintf "%s, %d %s %04d %02d:%02d:%02d GMT",$time[6],$time[3],$time[4],$time[5],$time[2],$time[1],$time[0]; } sub printfooter($$$$$) { my ($title,$editable,$lastmodified,$revertversion,$curversion)=@_; my $isblob=&blobtype($title); my @stuff=(); push @stuff,"Last modified: $lastmodified" if($lastmodified); if($editable) { push @stuff,"Upload":"edit\">Edit")." this page, versions of this page"; } elsif($revertversion and $diffcommand and not $isblob) { push @stuff,"Diff from this version to the current version"; } push @stuff,"Search for references to this page" if(iswikiword($title)); if($revertversion) { push @stuff,""; } if(@stuff) { print "".join("
\n",@stuff)."