added last changed timestamp for pages
[ikiwiki.git] / ikiwiki
1 #!/usr/bin/perl -T
2 $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
3
4 use warnings;
5 use strict;
6 use Memoize;
7 use File::Spec;
8 use HTML::Template;
9 use Getopt::Long;
10
11 my (%links, %oldlinks, %oldpagemtime, %renderedfiles, %pagesources);
12
13 # Holds global config settings, also used by some modules.
14 our %config=( #{{{
15         wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$)},
16         wiki_link_regexp => qr/\[\[([^\s]+)\]\]/,
17         wiki_file_regexp => qr/(^[-A-Za-z0-9_.:\/+]+$)/,
18         verbose => 0,
19         wikiname => "wiki",
20         default_pageext => ".mdwn",
21         cgi => 0,
22         svn => 1,
23         url => '',
24         cgiurl => '',
25         historyurl => '',
26         anonok => 0,
27         rebuild => 0,
28         wrapper => undef,
29         wrappermode => undef,
30         srcdir => undef,
31         destdir => undef,
32         templatedir => undef,
33         setup => undef,
34 ); #}}}
35
36 GetOptions( #{{{
37         "setup=s" => \$config{setup},
38         "wikiname=s" => \$config{wikiname},
39         "verbose|v!" => \$config{verbose},
40         "rebuild!" => \$config{rebuild},
41         "wrapper=s" => sub { $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap" },
42         "wrappermode=i" => \$config{wrappermode},
43         "svn!" => \$config{svn},
44         "anonok!" => \$config{anonok},
45         "cgi!" => \$config{cgi},
46         "url=s" => \$config{url},
47         "cgiurl=s" => \$config{cgiurl},
48         "historyurl=s" => \$config{historyurl},
49         "exclude=s@" => sub {
50                 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
51         },
52 ) || usage();
53
54 if (! $config{setup}) {
55         usage() unless @ARGV == 3;
56         $config{srcdir} = possibly_foolish_untaint(shift);
57         $config{templatedir} = possibly_foolish_untaint(shift);
58         $config{destdir} = possibly_foolish_untaint(shift);
59         if ($config{cgi} && ! length $config{url}) {
60                 error("Must specify url to wiki with --url when using --cgi");
61         }
62 }
63 #}}}
64
65 sub usage { #{{{
66         die "usage: ikiwiki [options] source templates dest\n";
67 } #}}}
68
69 sub error { #{{{
70         if ($config{cgi}) {
71                 print "Content-type: text/html\n\n";
72                 print misctemplate("Error", "<p>Error: @_</p>");
73         }
74         die @_;
75 } #}}}
76
77 sub debug ($) { #{{{
78         return unless $config{verbose};
79         if (! $config{cgi}) {
80                 print "@_\n";
81         }
82         else {
83                 print STDERR "@_\n";
84         }
85 } #}}}
86
87 sub mtime ($) { #{{{
88         my $page=shift;
89         
90         return (stat($page))[9];
91 } #}}}
92
93 sub possibly_foolish_untaint { #{{{
94         my $tainted=shift;
95         my ($untainted)=$tainted=~/(.*)/;
96         return $untainted;
97 } #}}}
98
99 sub basename ($) { #{{{
100         my $file=shift;
101
102         $file=~s!.*/!!;
103         return $file;
104 } #}}}
105
106 sub dirname ($) { #{{{
107         my $file=shift;
108
109         $file=~s!/?[^/]+$!!;
110         return $file;
111 } #}}}
112
113 sub pagetype ($) { #{{{
114         my $page=shift;
115         
116         if ($page =~ /\.mdwn$/) {
117                 return ".mdwn";
118         }
119         else {
120                 return "unknown";
121         }
122 } #}}}
123
124 sub pagename ($) { #{{{
125         my $file=shift;
126
127         my $type=pagetype($file);
128         my $page=$file;
129         $page=~s/\Q$type\E*$// unless $type eq 'unknown';
130         return $page;
131 } #}}}
132
133 sub htmlpage ($) { #{{{
134         my $page=shift;
135
136         return $page.".html";
137 } #}}}
138
139 sub readfile ($) { #{{{
140         my $file=shift;
141
142         local $/=undef;
143         open (IN, "$file") || error("failed to read $file: $!");
144         my $ret=<IN>;
145         close IN;
146         return $ret;
147 } #}}}
148
149 sub writefile ($$) { #{{{
150         my $file=shift;
151         my $content=shift;
152
153         my $dir=dirname($file);
154         if (! -d $dir) {
155                 my $d="";
156                 foreach my $s (split(m!/+!, $dir)) {
157                         $d.="$s/";
158                         if (! -d $d) {
159                                 mkdir($d) || error("failed to create directory $d: $!");
160                         }
161                 }
162         }
163         
164         open (OUT, ">$file") || error("failed to write $file: $!");
165         print OUT $content;
166         close OUT;
167 } #}}}
168
169 sub findlinks ($$) { #{{{
170         my $content=shift;
171         my $page=shift;
172
173         my @links;
174         while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
175                 push @links, lc($1);
176         }
177         # Discussion links are a special case since they're not in the text
178         # of the page, but on its template.
179         return @links, "$page/discussion";
180 } #}}}
181
182 sub bestlink ($$) { #{{{
183         # Given a page and the text of a link on the page, determine which
184         # existing page that link best points to. Prefers pages under a
185         # subdirectory with the same name as the source page, failing that
186         # goes down the directory tree to the base looking for matching
187         # pages.
188         my $page=shift;
189         my $link=lc(shift);
190         
191         my $cwd=$page;
192         do {
193                 my $l=$cwd;
194                 $l.="/" if length $l;
195                 $l.=$link;
196
197                 if (exists $links{$l}) {
198                         #debug("for $page, \"$link\", use $l");
199                         return $l;
200                 }
201         } while $cwd=~s!/?[^/]+$!!;
202
203         #print STDERR "warning: page $page, broken link: $link\n";
204         return "";
205 } #}}}
206
207 sub isinlinableimage ($) { #{{{
208         my $file=shift;
209         
210         $file=~/\.(png|gif|jpg|jpeg)$/;
211 } #}}}
212
213 sub htmllink { #{{{
214         my $page=shift;
215         my $link=shift;
216         my $noimageinline=shift; # don't turn links into inline html images
217         my $forcesubpage=shift; # force a link to a subpage
218
219         my $bestlink;
220         if (! $forcesubpage) {
221                 $bestlink=bestlink($page, $link);
222         }
223         else {
224                 $bestlink="$page/".lc($link);
225         }
226
227         return $link if length $bestlink && $page eq $bestlink;
228         
229         # TODO BUG: %renderedfiles may not have it, if the linked to page
230         # was also added and isn't yet rendered! Note that this bug is
231         # masked by the bug mentioned below that makes all new files
232         # be rendered twice.
233         if (! grep { $_ eq $bestlink } values %renderedfiles) {
234                 $bestlink=htmlpage($bestlink);
235         }
236         if (! grep { $_ eq $bestlink } values %renderedfiles) {
237                 return "<a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$link"
238         }
239         
240         $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
241         
242         if (! $noimageinline && isinlinableimage($bestlink)) {
243                 return "<img src=\"$bestlink\">";
244         }
245         return "<a href=\"$bestlink\">$link</a>";
246 } #}}}
247
248 sub linkify ($$) { #{{{
249         my $content=shift;
250         my $page=shift;
251
252         $content =~ s{(\\?)$config{wiki_link_regexp}}{
253                 $1 ? "[[$2]]" : htmllink($page, $2)
254         }eg;
255         
256         return $content;
257 } #}}}
258
259 sub htmlize ($$) { #{{{
260         my $type=shift;
261         my $content=shift;
262         
263         if (! $INC{"/usr/bin/markdown"}) {
264                 no warnings 'once';
265                 $blosxom::version="is a proper perl module too much to ask?";
266                 use warnings 'all';
267                 do "/usr/bin/markdown";
268         }
269         
270         if ($type eq '.mdwn') {
271                 return Markdown::Markdown($content);
272         }
273         else {
274                 error("htmlization of $type not supported");
275         }
276 } #}}}
277
278 sub backlinks ($) { #{{{
279         my $page=shift;
280
281         my @links;
282         foreach my $p (keys %links) {
283                 next if bestlink($page, $p) eq $page;
284                 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
285                         my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
286                         
287                         # Trim common dir prefixes from both pages.
288                         my $p_trimmed=$p;
289                         my $page_trimmed=$page;
290                         my $dir;
291                         1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
292                                 defined $dir &&
293                                 $p_trimmed=~s/^\Q$dir\E// &&
294                                 $page_trimmed=~s/^\Q$dir\E//;
295                                        
296                         push @links, { url => $href, page => $p_trimmed };
297                 }
298         }
299
300         return sort { $a->{page} cmp $b->{page} } @links;
301 } #}}}
302         
303 sub parentlinks ($) { #{{{
304         my $page=shift;
305         
306         my @ret;
307         my $pagelink="";
308         my $path="";
309         my $skip=1;
310         foreach my $dir (reverse split("/", $page)) {
311                 if (! $skip) {
312                         $path.="../";
313                         unshift @ret, { url => "$path$dir.html", page => $dir };
314                 }
315                 else {
316                         $skip=0;
317                 }
318         }
319         unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
320         return @ret;
321 } #}}}
322
323 sub indexlink () { #{{{
324         return "<a href=\"$config{url}\">$config{wikiname}</a>";
325 } #}}}
326
327 sub finalize ($$$) { #{{{
328         my $content=shift;
329         my $page=shift;
330         my $mtime=shift;
331
332         my $title=basename($page);
333         $title=~s/_/ /g;
334         
335         my $template=HTML::Template->new(blind_cache => 1,
336                 filename => "$config{templatedir}/page.tmpl");
337         
338         if (length $config{cgiurl}) {
339                 $template->param(editurl => "$config{cgiurl}?do=edit&page=$page");
340                 if ($config{svn}) {
341                         $template->param(recentchangesurl => "$config{cgiurl}?do=recentchanges");
342                 }
343         }
344
345         if (length $config{historyurl}) {
346                 my $u=$config{historyurl};
347                 $u=~s/\[\[\]\]/$pagesources{$page}/g;
348                 $template->param(historyurl => $u);
349         }
350         
351         $template->param(
352                 title => $title,
353                 wikiname => $config{wikiname},
354                 parentlinks => [parentlinks($page)],
355                 content => $content,
356                 backlinks => [backlinks($page)],
357                 discussionlink => htmllink($page, "Discussion", 1, 1),
358                 mtime => scalar(gmtime($mtime)),
359         );
360         
361         return $template->output;
362 } #}}}
363
364 sub check_overwrite ($$) { #{{{
365         # Important security check. Make sure to call this before saving
366         # any files to the source directory.
367         my $dest=shift;
368         my $src=shift;
369         
370         if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
371                 error("$dest already exists and was rendered from ".
372                         join(" ",(grep { $renderedfiles{$_} eq $dest } keys
373                                 %renderedfiles)).
374                         ", before, so not rendering from $src");
375         }
376 } #}}}
377
378 sub render ($) { #{{{
379         my $file=shift;
380         
381         my $type=pagetype($file);
382         my $content=readfile("$config{srcdir}/$file");
383         if ($type ne 'unknown') {
384                 my $page=pagename($file);
385                 
386                 $links{$page}=[findlinks($content, $page)];
387                 
388                 $content=linkify($content, $page);
389                 $content=htmlize($type, $content);
390                 $content=finalize($content, $page,
391                         mtime("$config{srcdir}/$file"));
392                 
393                 check_overwrite("$config{destdir}/".htmlpage($page), $page);
394                 writefile("$config{destdir}/".htmlpage($page), $content);
395                 $oldpagemtime{$page}=time;
396                 $renderedfiles{$page}=htmlpage($page);
397         }
398         else {
399                 $links{$file}=[];
400                 check_overwrite("$config{destdir}/$file", $file);
401                 writefile("$config{destdir}/$file", $content);
402                 $oldpagemtime{$file}=time;
403                 $renderedfiles{$file}=$file;
404         }
405 } #}}}
406
407 sub lockwiki () { #{{{
408         # Take an exclusive lock on the wiki to prevent multiple concurrent
409         # run issues. The lock will be dropped on program exit.
410         if (! -d "$config{srcdir}/.ikiwiki") {
411                 mkdir("$config{srcdir}/.ikiwiki");
412         }
413         open(WIKILOCK, ">$config{srcdir}/.ikiwiki/lockfile") || error ("cannot write to lockfile: $!");
414         if (! flock(WIKILOCK, 2 | 4)) {
415                 debug("wiki seems to be locked, waiting for lock");
416                 my $wait=600; # arbitrary, but don't hang forever to 
417                               # prevent process pileup
418                 for (1..600) {
419                         return if flock(WIKILOCK, 2 | 4);
420                         sleep 1;
421                 }
422                 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
423         }
424 } #}}}
425
426 sub unlockwiki () { #{{{
427         close WIKILOCK;
428 } #}}}
429
430 sub loadindex () { #{{{
431         open (IN, "$config{srcdir}/.ikiwiki/index") || return;
432         while (<IN>) {
433                 $_=possibly_foolish_untaint($_);
434                 chomp;
435                 my ($mtime, $file, $rendered, @links)=split(' ', $_);
436                 my $page=pagename($file);
437                 $pagesources{$page}=$file;
438                 $oldpagemtime{$page}=$mtime;
439                 $oldlinks{$page}=[@links];
440                 $links{$page}=[@links];
441                 $renderedfiles{$page}=$rendered;
442         }
443         close IN;
444 } #}}}
445
446 sub saveindex () { #{{{
447         if (! -d "$config{srcdir}/.ikiwiki") {
448                 mkdir("$config{srcdir}/.ikiwiki");
449         }
450         open (OUT, ">$config{srcdir}/.ikiwiki/index") || error("cannot write to index: $!");
451         foreach my $page (keys %oldpagemtime) {
452                 print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
453                         join(" ", @{$links{$page}})."\n"
454                                 if $oldpagemtime{$page};
455         }
456         close OUT;
457 } #}}}
458
459 sub rcs_update () { #{{{
460         if (-d "$config{srcdir}/.svn") {
461                 if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
462                         warn("svn update failed\n");
463                 }
464         }
465 } #}}}
466
467 sub rcs_prepedit ($) { #{{{
468         # Prepares to edit a file under revision control. Returns a token
469         # that must be passed into rcs_commit when the file is ready
470         # for committing.
471         # The file is relative to the srcdir.
472         my $file=shift;
473         
474         if (-d "$config{srcdir}/.svn") {
475                 # For subversion, return the revision of the file when
476                 # editing begins.
477                 my $rev=svn_info("Revision", "$config{srcdir}/$file");
478                 return defined $rev ? $rev : "";
479         }
480 } #}}}
481
482 sub rcs_commit ($$$) { #{{{
483         # Tries to commit the page; returns undef on _success_ and
484         # a version of the page with the rcs's conflict markers on failure.
485         # The file is relative to the srcdir.
486         my $file=shift;
487         my $message=shift;
488         my $rcstoken=shift;
489
490         if (-d "$config{srcdir}/.svn") {
491                 # Check to see if the page has been changed by someone
492                 # else since rcs_prepedit was called.
493                 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
494                 my $rev=svn_info("Revision", "$config{srcdir}/$file");
495                 if (defined $rev && defined $oldrev && $rev != $oldrev) {
496                         # Merge their changes into the file that we've
497                         # changed.
498                         chdir($config{srcdir}); # svn merge wants to be here
499                         if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
500                                    "$config{srcdir}/$file") != 0) {
501                                 warn("svn merge -r$oldrev:$rev failed\n");
502                         }
503                 }
504
505                 if (system("svn", "commit", "--quiet", "-m",
506                            possibly_foolish_untaint($message),
507                            "$config{srcdir}") != 0) {
508                         my $conflict=readfile("$config{srcdir}/$file");
509                         if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
510                                 warn("svn revert failed\n");
511                         }
512                         return $conflict;
513                 }
514         }
515         return undef # success
516 } #}}}
517
518 sub rcs_add ($) { #{{{
519         # filename is relative to the root of the srcdir
520         my $file=shift;
521
522         if (-d "$config{srcdir}/.svn") {
523                 my $parent=dirname($file);
524                 while (! -d "$config{srcdir}/$parent/.svn") {
525                         $file=$parent;
526                         $parent=dirname($file);
527                 }
528                 
529                 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
530                         warn("svn add failed\n");
531                 }
532         }
533 } #}}}
534
535 sub svn_info ($$) { #{{{
536         my $field=shift;
537         my $file=shift;
538
539         my $info=`LANG=C svn info $file`;
540         my ($ret)=$info=~/^$field: (.*)$/m;
541         return $ret;
542 } #}}}
543
544 sub rcs_recentchanges ($) { #{{{
545         my $num=shift;
546         my @ret;
547         
548         eval q{use CGI 'escapeHTML'};
549         eval q{use Date::Parse};
550         eval q{use Time::Duration};
551         
552         if (-d "$config{srcdir}/.svn") {
553                 my $svn_url=svn_info("URL", $config{srcdir});
554
555                 # FIXME: currently assumes that the wiki is somewhere
556                 # under trunk in svn, doesn't support other layouts.
557                 my ($svn_base)=$svn_url=~m!(/trunk(?:/.*)?)$!;
558                 
559                 my $div=qr/^--------------------+$/;
560                 my $infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
561                 my $state='start';
562                 my ($rev, $user, $when, @pages, @message);
563                 foreach (`LANG=C svn log --limit $num -v '$svn_url'`) {
564                         chomp;
565                         if ($state eq 'start' && /$div/) {
566                                 $state='header';
567                         }
568                         elsif ($state eq 'header' && /$infoline/) {
569                                 $rev=$1;
570                                 $user=$2;
571                                 $when=concise(ago(time - str2time($3)));
572                         }
573                         elsif ($state eq 'header' && /^\s+[A-Z]\s+\Q$svn_base\E\/([^ ]+)(?:$|\s)/) {
574                                 push @pages, { link => htmllink("", pagename($1), 1) }
575                                         if length $1;
576                         }
577                         elsif ($state eq 'header' && /^$/) {
578                                 $state='body';
579                         }
580                         elsif ($state eq 'body' && /$div/) {
581                                 my $committype="web";
582                                 if (defined $message[0] &&
583                                     $message[0]->{line}=~/^web commit by (\w+):?(.*)/) {
584                                         $user="$1";
585                                         $message[0]->{line}=$2;
586                                 }
587                                 else {
588                                         $committype="svn";
589                                 }
590                                 
591                                 push @ret, { rev => $rev,
592                                         user => htmllink("", $user, 1),
593                                         committype => $committype,
594                                         when => $when, message => [@message],
595                                         pages => [@pages] } if @pages;
596                                 return @ret if @ret >= $num;
597                                 
598                                 $state='header';
599                                 $rev=$user=$when=undef;
600                                 @pages=@message=();
601                         }
602                         elsif ($state eq 'body') {
603                                 push @message, {line => escapeHTML($_)},
604                         }
605                 }
606         }
607
608         return @ret;
609 } #}}}
610
611 sub prune ($) { #{{{
612         my $file=shift;
613
614         unlink($file);
615         my $dir=dirname($file);
616         while (rmdir($dir)) {
617                 $dir=dirname($dir);
618         }
619 } #}}}
620
621 sub refresh () { #{{{
622         # find existing pages
623         my %exists;
624         my @files;
625         eval q{use File::Find};
626         find({
627                 no_chdir => 1,
628                 wanted => sub {
629                         if (/$config{wiki_file_prune_regexp}/) {
630                                 no warnings 'once';
631                                 $File::Find::prune=1;
632                                 use warnings "all";
633                         }
634                         elsif (! -d $_ && ! -l $_) {
635                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
636                                 if (! defined $f) {
637                                         warn("skipping bad filename $_\n");
638                                 }
639                                 else {
640                                         $f=~s/^\Q$config{srcdir}\E\/?//;
641                                         push @files, $f;
642                                         $exists{pagename($f)}=1;
643                                 }
644                         }
645                 },
646         }, $config{srcdir});
647
648         my %rendered;
649
650         # check for added or removed pages
651         my @add;
652         foreach my $file (@files) {
653                 my $page=pagename($file);
654                 if (! $oldpagemtime{$page}) {
655                         debug("new page $page");
656                         push @add, $file;
657                         $links{$page}=[];
658                         $pagesources{$page}=$file;
659                 }
660         }
661         my @del;
662         foreach my $page (keys %oldpagemtime) {
663                 if (! $exists{$page}) {
664                         debug("removing old page $page");
665                         push @del, $pagesources{$page};
666                         prune($config{destdir}."/".$renderedfiles{$page});
667                         delete $renderedfiles{$page};
668                         $oldpagemtime{$page}=0;
669                         delete $pagesources{$page};
670                 }
671         }
672         
673         # render any updated files
674         foreach my $file (@files) {
675                 my $page=pagename($file);
676                 
677                 if (! exists $oldpagemtime{$page} ||
678                     mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
679                         debug("rendering changed file $file");
680                         render($file);
681                         $rendered{$file}=1;
682                 }
683         }
684         
685         # if any files were added or removed, check to see if each page
686         # needs an update due to linking to them
687         # TODO: inefficient; pages may get rendered above and again here;
688         # problem is the bestlink may have changed and we won't know until
689         # now
690         if (@add || @del) {
691 FILE:           foreach my $file (@files) {
692                         my $page=pagename($file);
693                         foreach my $f (@add, @del) {
694                                 my $p=pagename($f);
695                                 foreach my $link (@{$links{$page}}) {
696                                         if (bestlink($page, $link) eq $p) {
697                                                 debug("rendering $file, which links to $p");
698                                                 render($file);
699                                                 $rendered{$file}=1;
700                                                 next FILE;
701                                         }
702                                 }
703                         }
704                 }
705         }
706
707         # handle backlinks; if a page has added/removed links, update the
708         # pages it links to
709         # TODO: inefficient; pages may get rendered above and again here;
710         # problem is the backlinks could be wrong in the first pass render
711         # above
712         if (%rendered) {
713                 my %linkchanged;
714                 foreach my $file (keys %rendered, @del) {
715                         my $page=pagename($file);
716                         if (exists $links{$page}) {
717                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
718                                         if (length $link &&
719                                             ! exists $oldlinks{$page} ||
720                                             ! grep { $_ eq $link } @{$oldlinks{$page}}) {
721                                                 $linkchanged{$link}=1;
722                                         }
723                                 }
724                         }
725                         if (exists $oldlinks{$page}) {
726                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
727                                         if (length $link &&
728                                             ! exists $links{$page} ||
729                                             ! grep { $_ eq $link } @{$links{$page}}) {
730                                                 $linkchanged{$link}=1;
731                                         }
732                                 }
733                         }
734                 }
735                 foreach my $link (keys %linkchanged) {
736                         my $linkfile=$pagesources{$link};
737                         if (defined $linkfile) {
738                                 debug("rendering $linkfile, to update its backlinks");
739                                 render($linkfile);
740                         }
741                 }
742         }
743 } #}}}
744
745 sub gen_wrapper (@) { #{{{
746         my %config=(@_);
747         eval q{use Cwd 'abs_path'};
748         $config{srcdir}=abs_path($config{srcdir});
749         $config{destdir}=abs_path($config{destdir});
750         my $this=abs_path($0);
751         if (! -x $this) {
752                 error("$this doesn't seem to be executable");
753         }
754
755         if ($config{setup}) {
756                 error("cannot create a wrapper that uses a setup file");
757         }
758         
759         my @params=($config{srcdir}, $config{templatedir}, $config{destdir},
760                 "--wikiname=$config{wikiname}");
761         push @params, "--verbose" if $config{verbose};
762         push @params, "--rebuild" if $config{rebuild};
763         push @params, "--nosvn" if !$config{svn};
764         push @params, "--cgi" if $config{cgi};
765         push @params, "--url=$config{url}" if length $config{url};
766         push @params, "--cgiurl=$config{cgiurl}" if length $config{cgiurl};
767         push @params, "--historyurl=$config{historyurl}" if length $config{historyurl};
768         push @params, "--anonok" if $config{anonok};
769         my $params=join(" ", @params);
770         my $call='';
771         foreach my $p ($this, $this, @params) {
772                 $call.=qq{"$p", };
773         }
774         $call.="NULL";
775         
776         my @envsave;
777         push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
778                        CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
779                        HTTP_COOKIE} if $config{cgi};
780         my $envsave="";
781         foreach my $var (@envsave) {
782                 $envsave.=<<"EOF"
783         if ((s=getenv("$var")))
784                 asprintf(&newenviron[i++], "%s=%s", "$var", s);
785 EOF
786         }
787         
788         open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
789         print OUT <<"EOF";
790 /* A wrapper for ikiwiki, can be safely made suid. */
791 #define _GNU_SOURCE
792 #include <stdio.h>
793 #include <unistd.h>
794 #include <stdlib.h>
795 #include <string.h>
796
797 extern char **environ;
798
799 int main (int argc, char **argv) {
800         /* Sanitize environment. */
801         char *s;
802         char *newenviron[$#envsave+3];
803         int i=0;
804 $envsave
805         newenviron[i++]="HOME=$ENV{HOME}";
806         newenviron[i]=NULL;
807         environ=newenviron;
808
809         if (argc == 2 && strcmp(argv[1], "--params") == 0) {
810                 printf("$params\\n");
811                 exit(0);
812         }
813         
814         execl($call);
815         perror("failed to run $this");
816         exit(1);
817 }
818 EOF
819         close OUT;
820         if (system("gcc", "ikiwiki-wrap.c", "-o", possibly_foolish_untaint($config{wrapper})) != 0) {
821                 error("failed to compile ikiwiki-wrap.c");
822         }
823         unlink("ikiwiki-wrap.c");
824         if (defined $config{wrappermode} &&
825             ! chmod(oct($config{wrappermode}), possibly_foolish_untaint($config{wrapper}))) {
826                 error("chmod $config{wrapper}: $!");
827         }
828         print "successfully generated $config{wrapper}\n";
829 } #}}}
830                 
831 sub misctemplate ($$) { #{{{
832         my $title=shift;
833         my $pagebody=shift;
834         
835         my $template=HTML::Template->new(
836                 filename => "$config{templatedir}/misc.tmpl"
837         );
838         $template->param(
839                 title => $title,
840                 indexlink => indexlink(),
841                 wikiname => $config{wikiname},
842                 pagebody => $pagebody,
843         );
844         return $template->output;
845 }#}}}
846
847 sub cgi_recentchanges ($) { #{{{
848         my $q=shift;
849         
850         my $template=HTML::Template->new(
851                 filename => "$config{templatedir}/recentchanges.tmpl"
852         );
853         $template->param(
854                 title => "RecentChanges",
855                 indexlink => indexlink(),
856                 wikiname => $config{wikiname},
857                 changelog => [rcs_recentchanges(100)],
858         );
859         print $q->header, $template->output;
860 } #}}}
861
862 sub userinfo_get ($$) { #{{{
863         my $user=shift;
864         my $field=shift;
865
866         eval q{use Storable};
867         my $userdata=eval{ Storable::lock_retrieve("$config{srcdir}/.ikiwiki/userdb") };
868         if (! defined $userdata || ! ref $userdata || 
869             ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
870                 return "";
871         }
872         return $userdata->{$user}->{$field};
873 } #}}}
874
875 sub userinfo_set ($$) { #{{{
876         my $user=shift;
877         my $info=shift;
878         
879         eval q{use Storable};
880         my $userdata=eval{ Storable::lock_retrieve("$config{srcdir}/.ikiwiki/userdb") };
881         if (! defined $userdata || ! ref $userdata) {
882                 $userdata={};
883         }
884         $userdata->{$user}=$info;
885         my $oldmask=umask(077);
886         my $ret=Storable::lock_store($userdata, "$config{srcdir}/.ikiwiki/userdb");
887         umask($oldmask);
888         return $ret;
889 } #}}}
890
891 sub cgi_signin ($$) { #{{{
892         my $q=shift;
893         my $session=shift;
894
895         eval q{use CGI::FormBuilder};
896         my $form = CGI::FormBuilder->new(
897                 title => "$config{wikiname} signin",
898                 fields => [qw(do page from name password confirm_password email)],
899                 header => 1,
900                 method => 'POST',
901                 validate => {
902                         confirm_password => {
903                                 perl => q{eq $form->field("password")},
904                         },
905                         email => 'EMAIL',
906                 },
907                 required => 'NONE',
908                 javascript => 0,
909                 params => $q,
910                 action => $q->request_uri,
911                 header => 0,
912                 template => (-e "$config{templatedir}/signin.tmpl" ?
913                               "$config{templatedir}/signin.tmpl" : "")
914         );
915         
916         $form->field(name => "name", required => 0);
917         $form->field(name => "do", type => "hidden");
918         $form->field(name => "page", type => "hidden");
919         $form->field(name => "from", type => "hidden");
920         $form->field(name => "password", type => "password", required => 0);
921         $form->field(name => "confirm_password", type => "password", required => 0);
922         $form->field(name => "email", required => 0);
923         if ($q->param("do") ne "signin") {
924                 $form->text("You need to log in before you can edit pages.");
925         }
926         
927         if ($form->submitted) {
928                 # Set required fields based on how form was submitted.
929                 my %required=(
930                         "Login" => [qw(name password)],
931                         "Register" => [qw(name password confirm_password email)],
932                         "Mail Password" => [qw(name)],
933                 );
934                 foreach my $opt (@{$required{$form->submitted}}) {
935                         $form->field(name => $opt, required => 1);
936                 }
937         
938                 # Validate password differently depending on how
939                 # form was submitted.
940                 if ($form->submitted eq 'Login') {
941                         $form->field(
942                                 name => "password",
943                                 validate => sub {
944                                         length $form->field("name") &&
945                                         shift eq userinfo_get($form->field("name"), 'password');
946                                 },
947                         );
948                         $form->field(name => "name", validate => '/^\w+$/');
949                 }
950                 else {
951                         $form->field(name => "password", validate => 'VALUE');
952                 }
953                 # And make sure the entered name exists when logging
954                 # in or sending email, and does not when registering.
955                 if ($form->submitted eq 'Register') {
956                         $form->field(
957                                 name => "name",
958                                 validate => sub {
959                                         my $name=shift;
960                                         length $name &&
961                                         ! userinfo_get($name, "regdate");
962                                 },
963                         );
964                 }
965                 else {
966                         $form->field(
967                                 name => "name",
968                                 validate => sub {
969                                         my $name=shift;
970                                         length $name &&
971                                         userinfo_get($name, "regdate");
972                                 },
973                         );
974                 }
975         }
976         else {
977                 # First time settings.
978                 $form->field(name => "name", comment => "use FirstnameLastName");
979                 $form->field(name => "confirm_password", comment => "(only needed");
980                 $form->field(name => "email",            comment => "for registration)");
981                 if ($session->param("name")) {
982                         $form->field(name => "name", value => $session->param("name"));
983                 }
984         }
985
986         if ($form->submitted && $form->validate) {
987                 if ($form->submitted eq 'Login') {
988                         $session->param("name", $form->field("name"));
989                         if (defined $form->field("do") && 
990                             $form->field("do") ne 'signin') {
991                                 print $q->redirect(
992                                         "$config{cgiurl}?do=".$form->field("do").
993                                         "&page=".$form->field("page").
994                                         "&from=".$form->field("from"));;
995                         }
996                         else {
997                                 print $q->redirect($config{url});
998                         }
999                 }
1000                 elsif ($form->submitted eq 'Register') {
1001                         my $user_name=$form->field('name');
1002                         if (userinfo_set($user_name, {
1003                                            'email' => $form->field('email'),
1004                                            'password' => $form->field('password'),
1005                                            'regdate' => time
1006                                          })) {
1007                                 $form->field(name => "confirm_password", type => "hidden");
1008                                 $form->field(name => "email", type => "hidden");
1009                                 $form->text("Registration successful. Now you can Login.");
1010                                 print $session->header();
1011                                 print misctemplate($form->title, $form->render(submit => ["Login"]));
1012                         }
1013                         else {
1014                                 error("Error saving registration.");
1015                         }
1016                 }
1017                 elsif ($form->submitted eq 'Mail Password') {
1018                         my $user_name=$form->field("name");
1019                         my $template=HTML::Template->new(
1020                                 filename => "$config{templatedir}/passwordmail.tmpl"
1021                         );
1022                         $template->param(
1023                                 user_name => $user_name,
1024                                 user_password => userinfo_get($user_name, "password"),
1025                                 wikiurl => $config{url},
1026                                 wikiname => $config{wikiname},
1027                                 REMOTE_ADDR => $ENV{REMOTE_ADDR},
1028                         );
1029                         
1030                         eval q{use Mail::Sendmail};
1031                         my ($fromhost) = $config{cgiurl} =~ m!/([^/]+)!;
1032                         sendmail(
1033                                 To => userinfo_get($user_name, "email"),
1034                                 From => "$config{wikiname} admin <".(getpwuid($>))[0]."@".$fromhost.">",
1035                                 Subject => "$config{wikiname} information",
1036                                 Message => $template->output,
1037                         ) or error("Failed to send mail");
1038                         
1039                         $form->text("Your password has been emailed to you.");
1040                         $form->field(name => "name", required => 0);
1041                         print $session->header();
1042                         print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
1043                 }
1044         }
1045         else {
1046                 print $session->header();
1047                 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
1048         }
1049 } #}}}
1050
1051 sub cgi_editpage ($$) { #{{{
1052         my $q=shift;
1053         my $session=shift;
1054
1055         eval q{use CGI::FormBuilder};
1056         my $form = CGI::FormBuilder->new(
1057                 fields => [qw(do rcsinfo from page content comments)],
1058                 header => 1,
1059                 method => 'POST',
1060                 validate => {
1061                         content => '/.+/',
1062                 },
1063                 required => [qw{content}],
1064                 javascript => 0,
1065                 params => $q,
1066                 action => $q->request_uri,
1067                 table => 0,
1068                 template => "$config{templatedir}/editpage.tmpl"
1069         );
1070         my @buttons=("Save Page", "Preview", "Cancel");
1071         
1072         my ($page)=$form->param('page')=~/$config{wiki_file_regexp}/;
1073         if (! defined $page || ! length $page || $page ne $q->param('page') ||
1074             $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
1075                 error("bad page name");
1076         }
1077         $page=lc($page);
1078         
1079         my $file=$page.$config{default_pageext};
1080         my $newfile=1;
1081         if (exists $pagesources{lc($page)}) {
1082                 $file=$pagesources{lc($page)};
1083                 $newfile=0;
1084         }
1085
1086         $form->field(name => "do", type => 'hidden');
1087         $form->field(name => "from", type => 'hidden');
1088         $form->field(name => "rcsinfo", type => 'hidden');
1089         $form->field(name => "page", value => "$page", force => 1);
1090         $form->field(name => "comments", type => "text", size => 80);
1091         $form->field(name => "content", type => "textarea", rows => 20,
1092                 cols => 80);
1093         $form->tmpl_param("can_commit", $config{svn});
1094         $form->tmpl_param("indexlink", indexlink());
1095         $form->tmpl_param("helponformattinglink",
1096                 htmllink("", "HelpOnFormatting", 1));
1097         if (! $form->submitted) {
1098                 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
1099                         force => 1);
1100         }
1101         
1102         if ($form->submitted eq "Cancel") {
1103                 print $q->redirect("$config{url}/".htmlpage($page));
1104                 return;
1105         }
1106         elsif ($form->submitted eq "Preview") {
1107                 $form->tmpl_param("page_preview",
1108                         htmlize($config{default_pageext},
1109                                 linkify($form->field('content'), $page)));
1110         }
1111         else {
1112                 $form->tmpl_param("page_preview", "");
1113         }
1114         $form->tmpl_param("page_conflict", "");
1115         
1116         if (! $form->submitted || $form->submitted eq "Preview" || 
1117             ! $form->validate) {
1118                 if ($form->field("do") eq "create") {
1119                         if (exists $pagesources{lc($page)}) {
1120                                 # hmm, someone else made the page in the
1121                                 # meantime?
1122                                 print $q->redirect("$config{url}/".htmlpage($page));
1123                                 return;
1124                         }
1125                         
1126                         my @page_locs;
1127                         my $best_loc;
1128                         my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/;
1129                         if (! defined $from || ! length $from ||
1130                             $from ne $form->param('from') ||
1131                             $from=~/$config{wiki_file_prune_regexp}/ || $from=~/^\//) {
1132                                 @page_locs=$best_loc=$page;
1133                         }
1134                         else {
1135                                 my $dir=$from."/";
1136                                 $dir=~s![^/]+/$!!;
1137                                 
1138                                 if ($page eq 'discussion') {
1139                                         $best_loc="$from/$page";
1140                                 }
1141                                 else {
1142                                         $best_loc=$dir.$page;
1143                                 }
1144                                 
1145                                 push @page_locs, $dir.$page;
1146                                 push @page_locs, "$from/$page";
1147                                 while (length $dir) {
1148                                         $dir=~s![^/]+/$!!;
1149                                         push @page_locs, $dir.$page;
1150                                 }
1151
1152                                 @page_locs = grep { ! exists
1153                                         $pagesources{lc($_)} } @page_locs;
1154                         }
1155
1156                         $form->tmpl_param("page_select", 1);
1157                         $form->field(name => "page", type => 'select',
1158                                 options => \@page_locs, value => $best_loc);
1159                         $form->title("creating $page");
1160                 }
1161                 elsif ($form->field("do") eq "edit") {
1162                         if (! defined $form->field('content') || 
1163                             ! length $form->field('content')) {
1164                                 my $content="";
1165                                 if (exists $pagesources{lc($page)}) {
1166                                                 $content=readfile("$config{srcdir}/$pagesources{lc($page)}");
1167                                         $content=~s/\n/\r\n/g;
1168                                 }
1169                                 $form->field(name => "content", value => $content,
1170                                         force => 1);
1171                         }
1172                         $form->tmpl_param("page_select", 0);
1173                         $form->field(name => "page", type => 'hidden');
1174                         $form->title("editing $page");
1175                 }
1176                 
1177                 print $form->render(submit => \@buttons);
1178         }
1179         else {
1180                 # save page
1181                 my $content=$form->field('content');
1182                 $content=~s/\r\n/\n/g;
1183                 $content=~s/\r/\n/g;
1184                 writefile("$config{srcdir}/$file", $content);
1185                 
1186                 my $message="web commit ";
1187                 if ($session->param("name")) {
1188                         $message.="by ".$session->param("name");
1189                 }
1190                 else {
1191                         $message.="from $ENV{REMOTE_ADDR}";
1192                 }
1193                 if (defined $form->field('comments') &&
1194                     length $form->field('comments')) {
1195                         $message.=": ".$form->field('comments');
1196                 }
1197                 
1198                 if ($config{svn}) {
1199                         if ($newfile) {
1200                                 rcs_add($file);
1201                         }
1202                         # prevent deadlock with post-commit hook
1203                         unlockwiki();
1204                         # presumably the commit will trigger an update
1205                         # of the wiki
1206                         my $conflict=rcs_commit($file, $message,
1207                                 $form->field("rcsinfo"));
1208                 
1209                         if (defined $conflict) {
1210                                 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
1211                                         force => 1);
1212                                 $form->tmpl_param("page_conflict", 1);
1213                                 $form->field("content", value => $conflict, force => 1);
1214                                 $form->field("do", "edit)");
1215                                 $form->tmpl_param("page_select", 0);
1216                                 $form->field(name => "page", type => 'hidden');
1217                                 $form->title("editing $page");
1218                                 print $form->render(submit => \@buttons);
1219                                 return;
1220                         }
1221                 }
1222                 else {
1223                         loadindex();
1224                         refresh();
1225                         saveindex();
1226                 }
1227                 
1228                 # The trailing question mark tries to avoid broken
1229                 # caches and get the most recent version of the page.
1230                 print $q->redirect("$config{url}/".htmlpage($page)."?updated");
1231         }
1232 } #}}}
1233
1234 sub cgi () { #{{{
1235         eval q{use CGI};
1236         eval q{use CGI::Session};
1237         
1238         my $q=CGI->new;
1239         
1240         my $do=$q->param('do');
1241         if (! defined $do || ! length $do) {
1242                 error("\"do\" parameter missing");
1243         }
1244         
1245         # This does not need a session.
1246         if ($do eq 'recentchanges') {
1247                 cgi_recentchanges($q);
1248                 return;
1249         }
1250         
1251         CGI::Session->name("ikiwiki_session");
1252
1253         my $oldmask=umask(077);
1254         my $session = CGI::Session->new("driver:db_file", $q,
1255                 { FileName => "$config{srcdir}/.ikiwiki/sessions.db" });
1256         umask($oldmask);
1257         
1258         # Everything below this point needs the user to be signed in.
1259         if ((! $config{anonok} && ! defined $session->param("name") ||
1260                 ! userinfo_get($session->param("name"), "regdate")) || $do eq 'signin') {
1261                 cgi_signin($q, $session);
1262         
1263                 # Force session flush with safe umask.
1264                 my $oldmask=umask(077);
1265                 $session->flush;
1266                 umask($oldmask);
1267                 
1268                 return;
1269         }
1270         
1271         if ($do eq 'create' || $do eq 'edit') {
1272                 cgi_editpage($q, $session);
1273         }
1274         else {
1275                 error("unknown do parameter");
1276         }
1277 } #}}}
1278
1279 sub setup () { # {{{
1280         my $setup=possibly_foolish_untaint($config{setup});
1281         delete $config{setup};
1282         open (IN, $setup) || error("read $setup: $!\n");
1283         local $/=undef;
1284         my $code=<IN>;
1285         ($code)=$code=~/(.*)/s;
1286         close IN;
1287
1288         eval $code;
1289         error($@) if $@;
1290         exit;
1291 } #}}}
1292
1293 # main {{{
1294 setup() if $config{setup};
1295 lockwiki();
1296 if ($config{wrapper}) {
1297         gen_wrapper(%config);
1298         exit;
1299 }
1300 memoize('pagename');
1301 memoize('bestlink');
1302 loadindex() unless $config{rebuild};
1303 if ($config{cgi}) {
1304         cgi();
1305 }
1306 else {
1307         rcs_update() if $config{svn};
1308         refresh();
1309         saveindex();
1310 }
1311 #}}}