don't lock before setup
[ikiwiki] / 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
331         my $title=basename($page);
332         $title=~s/_/ /g;
333         
334         my $template=HTML::Template->new(blind_cache => 1,
335                 filename => "$config{templatedir}/page.tmpl");
336         
337         if (length $config{cgiurl}) {
338                 $template->param(editurl => "$config{cgiurl}?do=edit&page=$page");
339                 if ($config{svn}) {
340                         $template->param(recentchangesurl => "$config{cgiurl}?do=recentchanges");
341                 }
342         }
343
344         if (length $config{historyurl}) {
345                 my $u=$config{historyurl};
346                 $u=~s/\[\[\]\]/$pagesources{$page}/g;
347                 $template->param(historyurl => $u);
348         }
349         
350         $template->param(
351                 title => $title,
352                 wikiname => $config{wikiname},
353                 parentlinks => [parentlinks($page)],
354                 content => $content,
355                 backlinks => [backlinks($page)],
356                 discussionlink => htmllink($page, "Discussion", 1, 1),
357         );
358         
359         return $template->output;
360 } #}}}
361
362 sub check_overwrite ($$) { #{{{
363         # Important security check. Make sure to call this before saving
364         # any files to the source directory.
365         my $dest=shift;
366         my $src=shift;
367         
368         if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
369                 error("$dest already exists and was rendered from ".
370                         join(" ",(grep { $renderedfiles{$_} eq $dest } keys
371                                 %renderedfiles)).
372                         ", before, so not rendering from $src");
373         }
374 } #}}}
375
376 sub render ($) { #{{{
377         my $file=shift;
378         
379         my $type=pagetype($file);
380         my $content=readfile("$config{srcdir}/$file");
381         if ($type ne 'unknown') {
382                 my $page=pagename($file);
383                 
384                 $links{$page}=[findlinks($content, $page)];
385                 
386                 $content=linkify($content, $page);
387                 $content=htmlize($type, $content);
388                 $content=finalize($content, $page);
389                 
390                 check_overwrite("$config{destdir}/".htmlpage($page), $page);
391                 writefile("$config{destdir}/".htmlpage($page), $content);
392                 $oldpagemtime{$page}=time;
393                 $renderedfiles{$page}=htmlpage($page);
394         }
395         else {
396                 $links{$file}=[];
397                 check_overwrite("$config{destdir}/$file", $file);
398                 writefile("$config{destdir}/$file", $content);
399                 $oldpagemtime{$file}=time;
400                 $renderedfiles{$file}=$file;
401         }
402 } #}}}
403
404 sub lockwiki () { #{{{
405         # Take an exclusive lock on the wiki to prevent multiple concurrent
406         # run issues. The lock will be dropped on program exit.
407         if (! -d "$config{srcdir}/.ikiwiki") {
408                 mkdir("$config{srcdir}/.ikiwiki");
409         }
410         open(WIKILOCK, ">$config{srcdir}/.ikiwiki/lockfile") || error ("cannot write to lockfile: $!");
411         if (! flock(WIKILOCK, 2 | 4)) {
412                 debug("wiki seems to be locked, waiting for lock");
413                 my $wait=600; # arbitrary, but don't hang forever to 
414                               # prevent process pileup
415                 for (1..600) {
416                         return if flock(WIKILOCK, 2 | 4);
417                         sleep 1;
418                 }
419                 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
420         }
421 } #}}}
422
423 sub unlockwiki () { #{{{
424         close WIKILOCK;
425 } #}}}
426
427 sub loadindex () { #{{{
428         open (IN, "$config{srcdir}/.ikiwiki/index") || return;
429         while (<IN>) {
430                 $_=possibly_foolish_untaint($_);
431                 chomp;
432                 my ($mtime, $file, $rendered, @links)=split(' ', $_);
433                 my $page=pagename($file);
434                 $pagesources{$page}=$file;
435                 $oldpagemtime{$page}=$mtime;
436                 $oldlinks{$page}=[@links];
437                 $links{$page}=[@links];
438                 $renderedfiles{$page}=$rendered;
439         }
440         close IN;
441 } #}}}
442
443 sub saveindex () { #{{{
444         if (! -d "$config{srcdir}/.ikiwiki") {
445                 mkdir("$config{srcdir}/.ikiwiki");
446         }
447         open (OUT, ">$config{srcdir}/.ikiwiki/index") || error("cannot write to index: $!");
448         foreach my $page (keys %oldpagemtime) {
449                 print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
450                         join(" ", @{$links{$page}})."\n"
451                                 if $oldpagemtime{$page};
452         }
453         close OUT;
454 } #}}}
455
456 sub rcs_update () { #{{{
457         if (-d "$config{srcdir}/.svn") {
458                 if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
459                         warn("svn update failed\n");
460                 }
461         }
462 } #}}}
463
464 sub rcs_prepedit ($) { #{{{
465         # Prepares to edit a file under revision control. Returns a token
466         # that must be passed into rcs_commit when the file is ready
467         # for committing.
468         # The file is relative to the srcdir.
469         my $file=shift;
470         
471         if (-d "$config{srcdir}/.svn") {
472                 # For subversion, return the revision of the file when
473                 # editing begins.
474                 my $rev=svn_info("Revision", "$config{srcdir}/$file");
475                 return defined $rev ? $rev : "";
476         }
477 } #}}}
478
479 sub rcs_commit ($$$) { #{{{
480         # Tries to commit the page; returns undef on _success_ and
481         # a version of the page with the rcs's conflict markers on failure.
482         # The file is relative to the srcdir.
483         my $file=shift;
484         my $message=shift;
485         my $rcstoken=shift;
486
487         if (-d "$config{srcdir}/.svn") {
488                 # Check to see if the page has been changed by someone
489                 # else since rcs_prepedit was called.
490                 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
491                 my $rev=svn_info("Revision", "$config{srcdir}/$file");
492                 if ($rev != $oldrev) {
493                         # Merge their changes into the file that we've
494                         # changed.
495                         chdir($config{srcdir}); # svn merge wants to be here
496                         if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
497                                    "$config{srcdir}/$file") != 0) {
498                                 warn("svn merge -r$oldrev:$rev failed\n");
499                         }
500                 }
501
502                 if (system("svn", "commit", "--quiet", "-m",
503                            possibly_foolish_untaint($message),
504                            "$config{srcdir}/$file") != 0) {
505                         my $conflict=readfile("$config{srcdir}/$file");
506                         if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
507                                 warn("svn revert failed\n");
508                         }
509                         return $conflict;
510                 }
511         }
512         return undef # success
513 } #}}}
514
515 sub rcs_add ($) { #{{{
516         # filename is relative to the root of the srcdir
517         my $file=shift;
518
519         if (-d "$config{srcdir}/.svn") {
520                 my $parent=dirname($file);
521                 while (! -d "$config{srcdir}/$parent/.svn") {
522                         $file=$parent;
523                         $parent=dirname($file);
524                 }
525                 
526                 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
527                         warn("svn add failed\n");
528                 }
529         }
530 } #}}}
531
532 sub svn_info ($$) { #{{{
533         my $field=shift;
534         my $file=shift;
535
536         my $info=`LANG=C svn info $file`;
537         my ($ret)=$info=~/^$field: (.*)$/m;
538         return $ret;
539 } #}}}
540
541 sub rcs_recentchanges ($) { #{{{
542         my $num=shift;
543         my @ret;
544         
545         eval q{use CGI 'escapeHTML'};
546         eval q{use Date::Parse};
547         eval q{use Time::Duration};
548         
549         if (-d "$config{srcdir}/.svn") {
550                 my $svn_url=svn_info("URL", $config{srcdir});
551
552                 # FIXME: currently assumes that the wiki is somewhere
553                 # under trunk in svn, doesn't support other layouts.
554                 my ($svn_base)=$svn_url=~m!(/trunk(?:/.*)?)$!;
555                 
556                 my $div=qr/^--------------------+$/;
557                 my $infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
558                 my $state='start';
559                 my ($rev, $user, $when, @pages, @message);
560                 foreach (`LANG=C svn log --limit $num -v '$svn_url'`) {
561                         chomp;
562                         if ($state eq 'start' && /$div/) {
563                                 $state='header';
564                         }
565                         elsif ($state eq 'header' && /$infoline/) {
566                                 $rev=$1;
567                                 $user=$2;
568                                 $when=concise(ago(time - str2time($3)));
569                         }
570                         elsif ($state eq 'header' && /^\s+[A-Z]\s+\Q$svn_base\E\/(.+)$/) {
571                                 push @pages, { link => htmllink("", pagename($1), 1) }
572                                         if length $1;
573                         }
574                         elsif ($state eq 'header' && /^$/) {
575                                 $state='body';
576                         }
577                         elsif ($state eq 'body' && /$div/) {
578                                 my $committype="web";
579                                 if (defined $message[0] &&
580                                     $message[0]->{line}=~/^web commit by (\w+):?(.*)/) {
581                                         $user="$1";
582                                         $message[0]->{line}=$2;
583                                 }
584                                 else {
585                                         $committype="svn";
586                                 }
587                                 
588                                 push @ret, { rev => $rev,
589                                         user => htmllink("", $user, 1),
590                                         committype => $committype,
591                                         when => $when, message => [@message],
592                                         pages => [@pages] } if @pages;
593                                 return @ret if @ret >= $num;
594                                 
595                                 $state='header';
596                                 $rev=$user=$when=undef;
597                                 @pages=@message=();
598                         }
599                         elsif ($state eq 'body') {
600                                 push @message, {line => escapeHTML($_)},
601                         }
602                 }
603         }
604
605         return @ret;
606 } #}}}
607
608 sub prune ($) { #{{{
609         my $file=shift;
610
611         unlink($file);
612         my $dir=dirname($file);
613         while (rmdir($dir)) {
614                 $dir=dirname($dir);
615         }
616 } #}}}
617
618 sub refresh () { #{{{
619         # find existing pages
620         my %exists;
621         my @files;
622         eval q{use File::Find};
623         find({
624                 no_chdir => 1,
625                 wanted => sub {
626                         if (/$config{wiki_file_prune_regexp}/) {
627                                 no warnings 'once';
628                                 $File::Find::prune=1;
629                                 use warnings "all";
630                         }
631                         elsif (! -d $_ && ! -l $_) {
632                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
633                                 if (! defined $f) {
634                                         warn("skipping bad filename $_\n");
635                                 }
636                                 else {
637                                         $f=~s/^\Q$config{srcdir}\E\/?//;
638                                         push @files, $f;
639                                         $exists{pagename($f)}=1;
640                                 }
641                         }
642                 },
643         }, $config{srcdir});
644
645         my %rendered;
646
647         # check for added or removed pages
648         my @add;
649         foreach my $file (@files) {
650                 my $page=pagename($file);
651                 if (! $oldpagemtime{$page}) {
652                         debug("new page $page");
653                         push @add, $file;
654                         $links{$page}=[];
655                         $pagesources{$page}=$file;
656                 }
657         }
658         my @del;
659         foreach my $page (keys %oldpagemtime) {
660                 if (! $exists{$page}) {
661                         debug("removing old page $page");
662                         push @del, $pagesources{$page};
663                         prune($config{destdir}."/".$renderedfiles{$page});
664                         delete $renderedfiles{$page};
665                         $oldpagemtime{$page}=0;
666                         delete $pagesources{$page};
667                 }
668         }
669         
670         # render any updated files
671         foreach my $file (@files) {
672                 my $page=pagename($file);
673                 
674                 if (! exists $oldpagemtime{$page} ||
675                     mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
676                         debug("rendering changed file $file");
677                         render($file);
678                         $rendered{$file}=1;
679                 }
680         }
681         
682         # if any files were added or removed, check to see if each page
683         # needs an update due to linking to them
684         # TODO: inefficient; pages may get rendered above and again here;
685         # problem is the bestlink may have changed and we won't know until
686         # now
687         if (@add || @del) {
688 FILE:           foreach my $file (@files) {
689                         my $page=pagename($file);
690                         foreach my $f (@add, @del) {
691                                 my $p=pagename($f);
692                                 foreach my $link (@{$links{$page}}) {
693                                         if (bestlink($page, $link) eq $p) {
694                                                 debug("rendering $file, which links to $p");
695                                                 render($file);
696                                                 $rendered{$file}=1;
697                                                 next FILE;
698                                         }
699                                 }
700                         }
701                 }
702         }
703
704         # handle backlinks; if a page has added/removed links, update the
705         # pages it links to
706         # TODO: inefficient; pages may get rendered above and again here;
707         # problem is the backlinks could be wrong in the first pass render
708         # above
709         if (%rendered) {
710                 my %linkchanged;
711                 foreach my $file (keys %rendered, @del) {
712                         my $page=pagename($file);
713                         if (exists $links{$page}) {
714                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
715                                         if (length $link &&
716                                             ! exists $oldlinks{$page} ||
717                                             ! grep { $_ eq $link } @{$oldlinks{$page}}) {
718                                                 $linkchanged{$link}=1;
719                                         }
720                                 }
721                         }
722                         if (exists $oldlinks{$page}) {
723                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
724                                         if (length $link &&
725                                             ! exists $links{$page} ||
726                                             ! grep { $_ eq $link } @{$links{$page}}) {
727                                                 $linkchanged{$link}=1;
728                                         }
729                                 }
730                         }
731                 }
732                 foreach my $link (keys %linkchanged) {
733                         my $linkfile=$pagesources{$link};
734                         if (defined $linkfile) {
735                                 debug("rendering $linkfile, to update its backlinks");
736                                 render($linkfile);
737                         }
738                 }
739         }
740 } #}}}
741
742 sub gen_wrapper (@) { #{{{
743         my %config=(@_);
744         eval q{use Cwd 'abs_path'};
745         $config{srcdir}=abs_path($config{srcdir});
746         $config{destdir}=abs_path($config{destdir});
747         my $this=abs_path($0);
748         if (! -x $this) {
749                 error("$this doesn't seem to be executable");
750         }
751
752         if ($config{setup}) {
753                 error("cannot create a wrapper that uses a setup file");
754         }
755         
756         my @params=($config{srcdir}, $config{templatedir}, $config{destdir},
757                 "--wikiname=$config{wikiname}");
758         push @params, "--verbose" if $config{verbose};
759         push @params, "--rebuild" if $config{rebuild};
760         push @params, "--nosvn" if !$config{svn};
761         push @params, "--cgi" if $config{cgi};
762         push @params, "--url=$config{url}" if length $config{url};
763         push @params, "--cgiurl=$config{cgiurl}" if length $config{cgiurl};
764         push @params, "--historyurl=$config{historyurl}" if length $config{historyurl};
765         push @params, "--anonok" if $config{anonok};
766         my $params=join(" ", @params);
767         my $call='';
768         foreach my $p ($this, $this, @params) {
769                 $call.=qq{"$p", };
770         }
771         $call.="NULL";
772         
773         my @envsave;
774         push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
775                        CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
776                        HTTP_COOKIE} if $config{cgi};
777         my $envsave="";
778         foreach my $var (@envsave) {
779                 $envsave.=<<"EOF"
780         if ((s=getenv("$var")))
781                 asprintf(&newenviron[i++], "%s=%s", "$var", s);
782 EOF
783         }
784         
785         open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
786         print OUT <<"EOF";
787 /* A wrapper for ikiwiki, can be safely made suid. */
788 #define _GNU_SOURCE
789 #include <stdio.h>
790 #include <unistd.h>
791 #include <stdlib.h>
792 #include <string.h>
793
794 extern char **environ;
795
796 int main (int argc, char **argv) {
797         /* Sanitize environment. */
798         char *s;
799         char *newenviron[$#envsave+3];
800         int i=0;
801 $envsave
802         newenviron[i++]="HOME=$ENV{HOME}";
803         newenviron[i]=NULL;
804         environ=newenviron;
805
806         if (argc == 2 && strcmp(argv[1], "--params") == 0) {
807                 printf("$params\\n");
808                 exit(0);
809         }
810         
811         execl($call);
812         perror("failed to run $this");
813         exit(1);
814 }
815 EOF
816         close OUT;
817         if (system("gcc", "ikiwiki-wrap.c", "-o", possibly_foolish_untaint($config{wrapper})) != 0) {
818                 error("failed to compile ikiwiki-wrap.c");
819         }
820         unlink("ikiwiki-wrap.c");
821         if (defined $config{wrappermode} &&
822             ! chmod(oct($config{wrappermode}), possibly_foolish_untaint($config{wrapper}))) {
823                 error("chmod $config{wrapper}: $!");
824         }
825         print "successfully generated $config{wrapper}\n";
826 } #}}}
827                 
828 sub misctemplate ($$) { #{{{
829         my $title=shift;
830         my $pagebody=shift;
831         
832         my $template=HTML::Template->new(
833                 filename => "$config{templatedir}/misc.tmpl"
834         );
835         $template->param(
836                 title => $title,
837                 indexlink => indexlink(),
838                 wikiname => $config{wikiname},
839                 pagebody => $pagebody,
840         );
841         return $template->output;
842 }#}}}
843
844 sub cgi_recentchanges ($) { #{{{
845         my $q=shift;
846         
847         my $template=HTML::Template->new(
848                 filename => "$config{templatedir}/recentchanges.tmpl"
849         );
850         $template->param(
851                 title => "RecentChanges",
852                 indexlink => indexlink(),
853                 wikiname => $config{wikiname},
854                 changelog => [rcs_recentchanges(100)],
855         );
856         print $q->header, $template->output;
857 } #}}}
858
859 sub userinfo_get ($$) { #{{{
860         my $user=shift;
861         my $field=shift;
862
863         eval q{use Storable};
864         my $userdata=eval{ Storable::lock_retrieve("$config{srcdir}/.ikiwiki/userdb") };
865         if (! defined $userdata || ! ref $userdata || 
866             ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
867                 return "";
868         }
869         return $userdata->{$user}->{$field};
870 } #}}}
871
872 sub userinfo_set ($$) { #{{{
873         my $user=shift;
874         my $info=shift;
875         
876         eval q{use Storable};
877         my $userdata=eval{ Storable::lock_retrieve("$config{srcdir}/.ikiwiki/userdb") };
878         if (! defined $userdata || ! ref $userdata) {
879                 $userdata={};
880         }
881         $userdata->{$user}=$info;
882         my $oldmask=umask(077);
883         my $ret=Storable::lock_store($userdata, "$config{srcdir}/.ikiwiki/userdb");
884         umask($oldmask);
885         return $ret;
886 } #}}}
887
888 sub cgi_signin ($$) { #{{{
889         my $q=shift;
890         my $session=shift;
891
892         eval q{use CGI::FormBuilder};
893         my $form = CGI::FormBuilder->new(
894                 title => "$config{wikiname} signin",
895                 fields => [qw(do page from name password confirm_password email)],
896                 header => 1,
897                 method => 'POST',
898                 validate => {
899                         confirm_password => {
900                                 perl => q{eq $form->field("password")},
901                         },
902                         email => 'EMAIL',
903                 },
904                 required => 'NONE',
905                 javascript => 0,
906                 params => $q,
907                 action => $q->request_uri,
908                 header => 0,
909                 template => (-e "$config{templatedir}/signin.tmpl" ?
910                               "$config{templatedir}/signin.tmpl" : "")
911         );
912         
913         $form->field(name => "name", required => 0);
914         $form->field(name => "do", type => "hidden");
915         $form->field(name => "page", type => "hidden");
916         $form->field(name => "from", type => "hidden");
917         $form->field(name => "password", type => "password", required => 0);
918         $form->field(name => "confirm_password", type => "password", required => 0);
919         $form->field(name => "email", required => 0);
920         if ($q->param("do") ne "signin") {
921                 $form->text("You need to log in before you can edit pages.");
922         }
923         
924         if ($form->submitted) {
925                 # Set required fields based on how form was submitted.
926                 my %required=(
927                         "Login" => [qw(name password)],
928                         "Register" => [qw(name password confirm_password email)],
929                         "Mail Password" => [qw(name)],
930                 );
931                 foreach my $opt (@{$required{$form->submitted}}) {
932                         $form->field(name => $opt, required => 1);
933                 }
934         
935                 # Validate password differently depending on how
936                 # form was submitted.
937                 if ($form->submitted eq 'Login') {
938                         $form->field(
939                                 name => "password",
940                                 validate => sub {
941                                         length $form->field("name") &&
942                                         shift eq userinfo_get($form->field("name"), 'password');
943                                 },
944                         );
945                         $form->field(name => "name", validate => '/^\w+$/');
946                 }
947                 else {
948                         $form->field(name => "password", validate => 'VALUE');
949                 }
950                 # And make sure the entered name exists when logging
951                 # in or sending email, and does not when registering.
952                 if ($form->submitted eq 'Register') {
953                         $form->field(
954                                 name => "name",
955                                 validate => sub {
956                                         my $name=shift;
957                                         length $name &&
958                                         ! userinfo_get($name, "regdate");
959                                 },
960                         );
961                 }
962                 else {
963                         $form->field(
964                                 name => "name",
965                                 validate => sub {
966                                         my $name=shift;
967                                         length $name &&
968                                         userinfo_get($name, "regdate");
969                                 },
970                         );
971                 }
972         }
973         else {
974                 # First time settings.
975                 $form->field(name => "name", comment => "use FirstnameLastName");
976                 $form->field(name => "confirm_password", comment => "(only needed");
977                 $form->field(name => "email",            comment => "for registration)");
978                 if ($session->param("name")) {
979                         $form->field(name => "name", value => $session->param("name"));
980                 }
981         }
982
983         if ($form->submitted && $form->validate) {
984                 if ($form->submitted eq 'Login') {
985                         $session->param("name", $form->field("name"));
986                         if (defined $form->field("do") && 
987                             $form->field("do") ne 'signin') {
988                                 print $q->redirect(
989                                         "$config{cgiurl}?do=".$form->field("do").
990                                         "&page=".$form->field("page").
991                                         "&from=".$form->field("from"));;
992                         }
993                         else {
994                                 print $q->redirect($config{url});
995                         }
996                 }
997                 elsif ($form->submitted eq 'Register') {
998                         my $user_name=$form->field('name');
999                         if (userinfo_set($user_name, {
1000                                            'email' => $form->field('email'),
1001                                            'password' => $form->field('password'),
1002                                            'regdate' => time
1003                                          })) {
1004                                 $form->field(name => "confirm_password", type => "hidden");
1005                                 $form->field(name => "email", type => "hidden");
1006                                 $form->text("Registration successful. Now you can Login.");
1007                                 print $session->header();
1008                                 print misctemplate($form->title, $form->render(submit => ["Login"]));
1009                         }
1010                         else {
1011                                 error("Error saving registration.");
1012                         }
1013                 }
1014                 elsif ($form->submitted eq 'Mail Password') {
1015                         my $user_name=$form->field("name");
1016                         my $template=HTML::Template->new(
1017                                 filename => "$config{templatedir}/passwordmail.tmpl"
1018                         );
1019                         $template->param(
1020                                 user_name => $user_name,
1021                                 user_password => userinfo_get($user_name, "password"),
1022                                 wikiurl => $config{url},
1023                                 wikiname => $config{wikiname},
1024                                 REMOTE_ADDR => $ENV{REMOTE_ADDR},
1025                         );
1026                         
1027                         eval q{use Mail::Sendmail};
1028                         my ($fromhost) = $config{cgiurl} =~ m!/([^/]+)!;
1029                         sendmail(
1030                                 To => userinfo_get($user_name, "email"),
1031                                 From => "$config{wikiname} admin <".(getpwuid($>))[0]."@".$fromhost.">",
1032                                 Subject => "$config{wikiname} information",
1033                                 Message => $template->output,
1034                         ) or error("Failed to send mail");
1035                         
1036                         $form->text("Your password has been emailed to you.");
1037                         $form->field(name => "name", required => 0);
1038                         print $session->header();
1039                         print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
1040                 }
1041         }
1042         else {
1043                 print $session->header();
1044                 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
1045         }
1046 } #}}}
1047
1048 sub cgi_editpage ($$) { #{{{
1049         my $q=shift;
1050         my $session=shift;
1051
1052         eval q{use CGI::FormBuilder};
1053         my $form = CGI::FormBuilder->new(
1054                 fields => [qw(do rcsinfo from page content comments)],
1055                 header => 1,
1056                 method => 'POST',
1057                 validate => {
1058                         content => '/.+/',
1059                 },
1060                 required => [qw{content}],
1061                 javascript => 0,
1062                 params => $q,
1063                 action => $q->request_uri,
1064                 table => 0,
1065                 template => "$config{templatedir}/editpage.tmpl"
1066         );
1067         my @buttons=("Save Page", "Preview", "Cancel");
1068         
1069         my ($page)=$form->param('page')=~/$config{wiki_file_regexp}/;
1070         if (! defined $page || ! length $page || $page ne $q->param('page') ||
1071             $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
1072                 error("bad page name");
1073         }
1074         $page=lc($page);
1075         
1076         my $file=$page.$config{default_pageext};
1077         my $newfile=1;
1078         if (exists $pagesources{lc($page)}) {
1079                 $file=$pagesources{lc($page)};
1080                 $newfile=0;
1081         }
1082
1083         $form->field(name => "do", type => 'hidden');
1084         $form->field(name => "from", type => 'hidden');
1085         $form->field(name => "rcsinfo", type => 'hidden');
1086         $form->field(name => "page", value => "$page", force => 1);
1087         $form->field(name => "comments", type => "text", size => 80);
1088         $form->field(name => "content", type => "textarea", rows => 20,
1089                 cols => 80);
1090         $form->tmpl_param("can_commit", $config{svn});
1091         $form->tmpl_param("indexlink", indexlink());
1092         $form->tmpl_param("helponformattinglink",
1093                 htmllink("", "HelpOnFormatting", 1));
1094         if (! $form->submitted) {
1095                 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
1096                         force => 1);
1097         }
1098         
1099         if ($form->submitted eq "Cancel") {
1100                 print $q->redirect("$config{url}/".htmlpage($page));
1101                 return;
1102         }
1103         elsif ($form->submitted eq "Preview") {
1104                 $form->tmpl_param("page_preview",
1105                         htmlize($config{default_pageext},
1106                                 linkify($form->field('content'), $page)));
1107         }
1108         else {
1109                 $form->tmpl_param("page_preview", "");
1110         }
1111         $form->tmpl_param("page_conflict", "");
1112         
1113         if (! $form->submitted || $form->submitted eq "Preview" || 
1114             ! $form->validate) {
1115                 if ($form->field("do") eq "create") {
1116                         if (exists $pagesources{lc($page)}) {
1117                                 # hmm, someone else made the page in the
1118                                 # meantime?
1119                                 print $q->redirect("$config{url}/".htmlpage($page));
1120                                 return;
1121                         }
1122                         
1123                         my @page_locs;
1124                         my $best_loc;
1125                         my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/;
1126                         if (! defined $from || ! length $from ||
1127                             $from ne $form->param('from') ||
1128                             $from=~/$config{wiki_file_prune_regexp}/ || $from=~/^\//) {
1129                                 @page_locs=$best_loc=$page;
1130                         }
1131                         else {
1132                                 my $dir=$from."/";
1133                                 $dir=~s![^/]+/$!!;
1134                                 push @page_locs, $dir.$page;
1135                                 push @page_locs, "$from/$page";
1136                                 $best_loc="$from/$page";
1137                                 while (length $dir) {
1138                                         $dir=~s![^/]+/$!!;
1139                                         push @page_locs, $dir.$page;
1140                                 }
1141
1142                                 @page_locs = grep { ! exists
1143                                         $pagesources{lc($_)} } @page_locs;
1144                         }
1145
1146                         $form->tmpl_param("page_select", 1);
1147                         $form->field(name => "page", type => 'select',
1148                                 options => \@page_locs, value => $best_loc);
1149                         $form->title("creating $page");
1150                 }
1151                 elsif ($form->field("do") eq "edit") {
1152                         if (! defined $form->field('content') || 
1153                             ! length $form->field('content')) {
1154                                 my $content="";
1155                                 if (exists $pagesources{lc($page)}) {
1156                                                 $content=readfile("$config{srcdir}/$pagesources{lc($page)}");
1157                                         $content=~s/\n/\r\n/g;
1158                                 }
1159                                 $form->field(name => "content", value => $content,
1160                                         force => 1);
1161                         }
1162                         $form->tmpl_param("page_select", 0);
1163                         $form->field(name => "page", type => 'hidden');
1164                         $form->title("editing $page");
1165                 }
1166                 
1167                 print $form->render(submit => \@buttons);
1168         }
1169         else {
1170                 # save page
1171                 my $content=$form->field('content');
1172                 $content=~s/\r\n/\n/g;
1173                 $content=~s/\r/\n/g;
1174                 writefile("$config{srcdir}/$file", $content);
1175                 
1176                 my $message="web commit ";
1177                 if ($session->param("name")) {
1178                         $message.="by ".$session->param("name");
1179                 }
1180                 else {
1181                         $message.="from $ENV{REMOTE_ADDR}";
1182                 }
1183                 if (defined $form->field('comments') &&
1184                     length $form->field('comments')) {
1185                         $message.=": ".$form->field('comments');
1186                 }
1187                 
1188                 if ($config{svn}) {
1189                         if ($newfile) {
1190                                 rcs_add($file);
1191                         }
1192                         # prevent deadlock with post-commit hook
1193                         unlockwiki();
1194                         # presumably the commit will trigger an update
1195                         # of the wiki
1196                         my $conflict=rcs_commit($file, $message,
1197                                 $form->field("rcsinfo"));
1198                 
1199                         if (defined $conflict) {
1200                                 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
1201                                         force => 1);
1202                                 $form->tmpl_param("page_conflict", 1);
1203                                 $form->field("content", value => $conflict, force => 1);
1204                                 $form->field("do", "edit)");
1205                                 $form->tmpl_param("page_select", 0);
1206                                 $form->field(name => "page", type => 'hidden');
1207                                 $form->title("editing $page");
1208                                 print $form->render(submit => \@buttons);
1209                                 return;
1210                         }
1211                 }
1212                 else {
1213                         loadindex();
1214                         refresh();
1215                         saveindex();
1216                 }
1217                 
1218                 # The trailing question mark tries to avoid broken
1219                 # caches and get the most recent version of the page.
1220                 print $q->redirect("$config{url}/".htmlpage($page)."?updated");
1221         }
1222 } #}}}
1223
1224 sub cgi () { #{{{
1225         eval q{use CGI};
1226         eval q{use CGI::Session};
1227         
1228         my $q=CGI->new;
1229         
1230         my $do=$q->param('do');
1231         if (! defined $do || ! length $do) {
1232                 error("\"do\" parameter missing");
1233         }
1234         
1235         # This does not need a session.
1236         if ($do eq 'recentchanges') {
1237                 cgi_recentchanges($q);
1238                 return;
1239         }
1240         
1241         CGI::Session->name("ikiwiki_session");
1242
1243         my $oldmask=umask(077);
1244         my $session = CGI::Session->new("driver:db_file", $q,
1245                 { FileName => "$config{srcdir}/.ikiwiki/sessions.db" });
1246         umask($oldmask);
1247         
1248         # Everything below this point needs the user to be signed in.
1249         if ((! $config{anonok} && ! defined $session->param("name") ||
1250                 ! userinfo_get($session->param("name"), "regdate")) || $do eq 'signin') {
1251                 cgi_signin($q, $session);
1252         
1253                 # Force session flush with safe umask.
1254                 my $oldmask=umask(077);
1255                 $session->flush;
1256                 umask($oldmask);
1257                 
1258                 return;
1259         }
1260         
1261         if ($do eq 'create' || $do eq 'edit') {
1262                 cgi_editpage($q, $session);
1263         }
1264         else {
1265                 error("unknown do parameter");
1266         }
1267 } #}}}
1268
1269 sub setup () { # {{{
1270         my $setup=possibly_foolish_untaint($config{setup});
1271         delete $config{setup};
1272         open (IN, $setup) || error("read $setup: $!\n");
1273         local $/=undef;
1274         my $code=<IN>;
1275         ($code)=$code=~/(.*)/s;
1276         close IN;
1277
1278         eval $code;
1279         error($@) if $@;
1280         exit;
1281 } #}}}
1282
1283 # main {{{
1284 setup() if $config{setup};
1285 lockwiki();
1286 if ($config{wrapper}) {
1287         gen_wrapper(%config);
1288         exit;
1289 }
1290 memoize('pagename');
1291 memoize('bestlink');
1292 loadindex() unless $config{rebuild};
1293 if ($config{cgi}) {
1294         cgi();
1295 }
1296 else {
1297         rcs_update() if $config{svn};
1298         refresh();
1299         saveindex();
1300 }
1301 #}}}