2 $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
11 my (%links, %oldlinks, %oldpagemtime, %renderedfiles, %pagesources);
13 # Holds global config settings, also used by some modules.
15 wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$)},
16 wiki_link_regexp => qr/\[\[([^\s]+)\]\]/,
17 wiki_file_regexp => qr/(^[-A-Za-z0-9_.:\/+]+$)/,
20 default_pageext => ".mdwn",
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},
50 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
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");
66 die "usage: ikiwiki [options] source templates dest\n";
71 print "Content-type: text/html\n\n";
72 print misctemplate("Error", "<p>Error: @_</p>");
78 return unless $config{verbose};
90 return (stat($page))[9];
93 sub possibly_foolish_untaint { #{{{
95 my ($untainted)=$tainted=~/(.*)/;
99 sub basename ($) { #{{{
106 sub dirname ($) { #{{{
113 sub pagetype ($) { #{{{
116 if ($page =~ /\.mdwn$/) {
124 sub pagename ($) { #{{{
127 my $type=pagetype($file);
129 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
133 sub htmlpage ($) { #{{{
136 return $page.".html";
139 sub readfile ($) { #{{{
143 open (IN, "$file") || error("failed to read $file: $!");
149 sub writefile ($$) { #{{{
153 my $dir=dirname($file);
156 foreach my $s (split(m!/+!, $dir)) {
159 mkdir($d) || error("failed to create directory $d: $!");
164 open (OUT, ">$file") || error("failed to write $file: $!");
169 sub findlinks ($$) { #{{{
174 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
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";
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
194 $l.="/" if length $l;
197 if (exists $links{$l}) {
198 #debug("for $page, \"$link\", use $l");
201 } while $cwd=~s!/?[^/]+$!!;
203 #print STDERR "warning: page $page, broken link: $link\n";
207 sub isinlinableimage ($) { #{{{
210 $file=~/\.(png|gif|jpg|jpeg)$/;
216 my $noimageinline=shift; # don't turn links into inline html images
217 my $forcesubpage=shift; # force a link to a subpage
220 if (! $forcesubpage) {
221 $bestlink=bestlink($page, $link);
224 $bestlink="$page/".lc($link);
227 return $link if length $bestlink && $page eq $bestlink;
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
233 if (! grep { $_ eq $bestlink } values %renderedfiles) {
234 $bestlink=htmlpage($bestlink);
236 if (! grep { $_ eq $bestlink } values %renderedfiles) {
237 return "<a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$link"
240 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
242 if (! $noimageinline && isinlinableimage($bestlink)) {
243 return "<img src=\"$bestlink\">";
245 return "<a href=\"$bestlink\">$link</a>";
248 sub linkify ($$) { #{{{
252 $content =~ s{(\\?)$config{wiki_link_regexp}}{
253 $1 ? "[[$2]]" : htmllink($page, $2)
259 sub htmlize ($$) { #{{{
263 if (! $INC{"/usr/bin/markdown"}) {
265 $blosxom::version="is a proper perl module too much to ask?";
267 do "/usr/bin/markdown";
270 if ($type eq '.mdwn') {
271 return Markdown::Markdown($content);
274 error("htmlization of $type not supported");
278 sub backlinks ($) { #{{{
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));
287 # Trim common dir prefixes from both pages.
289 my $page_trimmed=$page;
291 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
293 $p_trimmed=~s/^\Q$dir\E// &&
294 $page_trimmed=~s/^\Q$dir\E//;
296 push @links, { url => $href, page => $p_trimmed };
300 return sort { $a->{page} cmp $b->{page} } @links;
303 sub parentlinks ($) { #{{{
310 foreach my $dir (reverse split("/", $page)) {
313 unshift @ret, { url => "$path$dir.html", page => $dir };
319 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
323 sub indexlink () { #{{{
324 return "<a href=\"$config{url}\">$config{wikiname}</a>";
327 sub finalize ($$$) { #{{{
332 my $title=basename($page);
335 my $template=HTML::Template->new(blind_cache => 1,
336 filename => "$config{templatedir}/page.tmpl");
338 if (length $config{cgiurl}) {
339 $template->param(editurl => "$config{cgiurl}?do=edit&page=$page");
341 $template->param(recentchangesurl => "$config{cgiurl}?do=recentchanges");
345 if (length $config{historyurl}) {
346 my $u=$config{historyurl};
347 $u=~s/\[\[\]\]/$pagesources{$page}/g;
348 $template->param(historyurl => $u);
353 wikiname => $config{wikiname},
354 parentlinks => [parentlinks($page)],
356 backlinks => [backlinks($page)],
357 discussionlink => htmllink($page, "Discussion", 1, 1),
358 mtime => scalar(gmtime($mtime)),
361 return $template->output;
364 sub check_overwrite ($$) { #{{{
365 # Important security check. Make sure to call this before saving
366 # any files to the source directory.
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
374 ", before, so not rendering from $src");
378 sub render ($) { #{{{
381 my $type=pagetype($file);
382 my $content=readfile("$config{srcdir}/$file");
383 if ($type ne 'unknown') {
384 my $page=pagename($file);
386 $links{$page}=[findlinks($content, $page)];
388 $content=linkify($content, $page);
389 $content=htmlize($type, $content);
390 $content=finalize($content, $page,
391 mtime("$config{srcdir}/$file"));
393 check_overwrite("$config{destdir}/".htmlpage($page), $page);
394 writefile("$config{destdir}/".htmlpage($page), $content);
395 $oldpagemtime{$page}=time;
396 $renderedfiles{$page}=htmlpage($page);
400 check_overwrite("$config{destdir}/$file", $file);
401 writefile("$config{destdir}/$file", $content);
402 $oldpagemtime{$file}=time;
403 $renderedfiles{$file}=$file;
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");
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
419 return if flock(WIKILOCK, 2 | 4);
422 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
426 sub unlockwiki () { #{{{
430 sub loadindex () { #{{{
431 open (IN, "$config{srcdir}/.ikiwiki/index") || return;
433 $_=possibly_foolish_untaint($_);
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;
446 sub saveindex () { #{{{
447 if (! -d "$config{srcdir}/.ikiwiki") {
448 mkdir("$config{srcdir}/.ikiwiki");
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};
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");
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
471 # The file is relative to the srcdir.
474 if (-d "$config{srcdir}/.svn") {
475 # For subversion, return the revision of the file when
477 my $rev=svn_info("Revision", "$config{srcdir}/$file");
478 return defined $rev ? $rev : "";
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.
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
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");
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");
515 return undef # success
518 sub rcs_add ($) { #{{{
519 # filename is relative to the root of the srcdir
522 if (-d "$config{srcdir}/.svn") {
523 my $parent=dirname($file);
524 while (! -d "$config{srcdir}/$parent/.svn") {
526 $parent=dirname($file);
529 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
530 warn("svn add failed\n");
535 sub svn_info ($$) { #{{{
539 my $info=`LANG=C svn info $file`;
540 my ($ret)=$info=~/^$field: (.*)$/m;
544 sub rcs_recentchanges ($) { #{{{
548 eval q{use CGI 'escapeHTML'};
549 eval q{use Date::Parse};
550 eval q{use Time::Duration};
552 if (-d "$config{srcdir}/.svn") {
553 my $svn_url=svn_info("URL", $config{srcdir});
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(?:/.*)?)$!;
559 my $div=qr/^--------------------+$/;
560 my $infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
562 my ($rev, $user, $when, @pages, @message);
563 foreach (`LANG=C svn log --limit $num -v '$svn_url'`) {
565 if ($state eq 'start' && /$div/) {
568 elsif ($state eq 'header' && /$infoline/) {
571 $when=concise(ago(time - str2time($3)));
573 elsif ($state eq 'header' && /^\s+[A-Z]\s+\Q$svn_base\E\/([^ ]+)(?:$|\s)/) {
574 push @pages, { link => htmllink("", pagename($1), 1) }
577 elsif ($state eq 'header' && /^$/) {
580 elsif ($state eq 'body' && /$div/) {
581 my $committype="web";
582 if (defined $message[0] &&
583 $message[0]->{line}=~/^web commit by (\w+):?(.*)/) {
585 $message[0]->{line}=$2;
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;
599 $rev=$user=$when=undef;
602 elsif ($state eq 'body') {
603 push @message, {line => escapeHTML($_)},
615 my $dir=dirname($file);
616 while (rmdir($dir)) {
621 sub refresh () { #{{{
622 # find existing pages
625 eval q{use File::Find};
629 if (/$config{wiki_file_prune_regexp}/) {
631 $File::Find::prune=1;
634 elsif (! -d $_ && ! -l $_) {
635 my ($f)=/$config{wiki_file_regexp}/; # untaint
637 warn("skipping bad filename $_\n");
640 $f=~s/^\Q$config{srcdir}\E\/?//;
642 $exists{pagename($f)}=1;
650 # check for added or removed pages
652 foreach my $file (@files) {
653 my $page=pagename($file);
654 if (! $oldpagemtime{$page}) {
655 debug("new page $page");
658 $pagesources{$page}=$file;
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};
673 # render any updated files
674 foreach my $file (@files) {
675 my $page=pagename($file);
677 if (! exists $oldpagemtime{$page} ||
678 mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
679 debug("rendering changed file $file");
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
691 FILE: foreach my $file (@files) {
692 my $page=pagename($file);
693 foreach my $f (@add, @del) {
695 foreach my $link (@{$links{$page}}) {
696 if (bestlink($page, $link) eq $p) {
697 debug("rendering $file, which links to $p");
707 # handle backlinks; if a page has added/removed links, update the
709 # TODO: inefficient; pages may get rendered above and again here;
710 # problem is the backlinks could be wrong in the first pass render
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}}) {
719 ! exists $oldlinks{$page} ||
720 ! grep { $_ eq $link } @{$oldlinks{$page}}) {
721 $linkchanged{$link}=1;
725 if (exists $oldlinks{$page}) {
726 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
728 ! exists $links{$page} ||
729 ! grep { $_ eq $link } @{$links{$page}}) {
730 $linkchanged{$link}=1;
735 foreach my $link (keys %linkchanged) {
736 my $linkfile=$pagesources{$link};
737 if (defined $linkfile) {
738 debug("rendering $linkfile, to update its backlinks");
745 sub gen_wrapper (@) { #{{{
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);
752 error("$this doesn't seem to be executable");
755 if ($config{setup}) {
756 error("cannot create a wrapper that uses a setup file");
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);
771 foreach my $p ($this, $this, @params) {
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};
781 foreach my $var (@envsave) {
783 if ((s=getenv("$var")))
784 asprintf(&newenviron[i++], "%s=%s", "$var", s);
788 open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
790 /* A wrapper for ikiwiki, can be safely made suid. */
797 extern char **environ;
799 int main (int argc, char **argv) {
800 /* Sanitize environment. */
802 char *newenviron[$#envsave+3];
805 newenviron[i++]="HOME=$ENV{HOME}";
809 if (argc == 2 && strcmp(argv[1], "--params") == 0) {
810 printf("$params\\n");
815 perror("failed to run $this");
820 if (system("gcc", "ikiwiki-wrap.c", "-o", possibly_foolish_untaint($config{wrapper})) != 0) {
821 error("failed to compile ikiwiki-wrap.c");
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}: $!");
828 print "successfully generated $config{wrapper}\n";
831 sub misctemplate ($$) { #{{{
835 my $template=HTML::Template->new(
836 filename => "$config{templatedir}/misc.tmpl"
840 indexlink => indexlink(),
841 wikiname => $config{wikiname},
842 pagebody => $pagebody,
844 return $template->output;
847 sub cgi_recentchanges ($) { #{{{
850 my $template=HTML::Template->new(
851 filename => "$config{templatedir}/recentchanges.tmpl"
854 title => "RecentChanges",
855 indexlink => indexlink(),
856 wikiname => $config{wikiname},
857 changelog => [rcs_recentchanges(100)],
859 print $q->header, $template->output;
862 sub userinfo_get ($$) { #{{{
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}) {
872 return $userdata->{$user}->{$field};
875 sub userinfo_set ($$) { #{{{
879 eval q{use Storable};
880 my $userdata=eval{ Storable::lock_retrieve("$config{srcdir}/.ikiwiki/userdb") };
881 if (! defined $userdata || ! ref $userdata) {
884 $userdata->{$user}=$info;
885 my $oldmask=umask(077);
886 my $ret=Storable::lock_store($userdata, "$config{srcdir}/.ikiwiki/userdb");
891 sub cgi_signin ($$) { #{{{
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)],
902 confirm_password => {
903 perl => q{eq $form->field("password")},
910 action => $q->request_uri,
912 template => (-e "$config{templatedir}/signin.tmpl" ?
913 "$config{templatedir}/signin.tmpl" : "")
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.");
927 if ($form->submitted) {
928 # Set required fields based on how form was submitted.
930 "Login" => [qw(name password)],
931 "Register" => [qw(name password confirm_password email)],
932 "Mail Password" => [qw(name)],
934 foreach my $opt (@{$required{$form->submitted}}) {
935 $form->field(name => $opt, required => 1);
938 # Validate password differently depending on how
939 # form was submitted.
940 if ($form->submitted eq 'Login') {
944 length $form->field("name") &&
945 shift eq userinfo_get($form->field("name"), 'password');
948 $form->field(name => "name", validate => '/^\w+$/');
951 $form->field(name => "password", validate => 'VALUE');
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') {
961 ! userinfo_get($name, "regdate");
971 userinfo_get($name, "regdate");
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"));
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') {
992 "$config{cgiurl}?do=".$form->field("do").
993 "&page=".$form->field("page").
994 "&from=".$form->field("from"));;
997 print $q->redirect($config{url});
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'),
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"]));
1014 error("Error saving registration.");
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"
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},
1030 eval q{use Mail::Sendmail};
1031 my ($fromhost) = $config{cgiurl} =~ m!/([^/]+)!;
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");
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"]));
1046 print $session->header();
1047 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
1051 sub cgi_editpage ($$) { #{{{
1055 eval q{use CGI::FormBuilder};
1056 my $form = CGI::FormBuilder->new(
1057 fields => [qw(do rcsinfo from page content comments)],
1063 required => [qw{content}],
1066 action => $q->request_uri,
1068 template => "$config{templatedir}/editpage.tmpl"
1070 my @buttons=("Save Page", "Preview", "Cancel");
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");
1079 my $file=$page.$config{default_pageext};
1081 if (exists $pagesources{lc($page)}) {
1082 $file=$pagesources{lc($page)};
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,
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),
1102 if ($form->submitted eq "Cancel") {
1103 print $q->redirect("$config{url}/".htmlpage($page));
1106 elsif ($form->submitted eq "Preview") {
1107 $form->tmpl_param("page_preview",
1108 htmlize($config{default_pageext},
1109 linkify($form->field('content'), $page)));
1112 $form->tmpl_param("page_preview", "");
1114 $form->tmpl_param("page_conflict", "");
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
1122 print $q->redirect("$config{url}/".htmlpage($page));
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;
1138 if ($page eq 'discussion') {
1139 $best_loc="$from/$page";
1142 $best_loc=$dir.$page;
1145 push @page_locs, $dir.$page;
1146 push @page_locs, "$from/$page";
1147 while (length $dir) {
1149 push @page_locs, $dir.$page;
1152 @page_locs = grep { ! exists
1153 $pagesources{lc($_)} } @page_locs;
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");
1161 elsif ($form->field("do") eq "edit") {
1162 if (! defined $form->field('content') ||
1163 ! length $form->field('content')) {
1165 if (exists $pagesources{lc($page)}) {
1166 $content=readfile("$config{srcdir}/$pagesources{lc($page)}");
1167 $content=~s/\n/\r\n/g;
1169 $form->field(name => "content", value => $content,
1172 $form->tmpl_param("page_select", 0);
1173 $form->field(name => "page", type => 'hidden');
1174 $form->title("editing $page");
1177 print $form->render(submit => \@buttons);
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);
1186 my $message="web commit ";
1187 if ($session->param("name")) {
1188 $message.="by ".$session->param("name");
1191 $message.="from $ENV{REMOTE_ADDR}";
1193 if (defined $form->field('comments') &&
1194 length $form->field('comments')) {
1195 $message.=": ".$form->field('comments');
1202 # prevent deadlock with post-commit hook
1204 # presumably the commit will trigger an update
1206 my $conflict=rcs_commit($file, $message,
1207 $form->field("rcsinfo"));
1209 if (defined $conflict) {
1210 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
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);
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");
1236 eval q{use CGI::Session};
1240 my $do=$q->param('do');
1241 if (! defined $do || ! length $do) {
1242 error("\"do\" parameter missing");
1245 # This does not need a session.
1246 if ($do eq 'recentchanges') {
1247 cgi_recentchanges($q);
1251 CGI::Session->name("ikiwiki_session");
1253 my $oldmask=umask(077);
1254 my $session = CGI::Session->new("driver:db_file", $q,
1255 { FileName => "$config{srcdir}/.ikiwiki/sessions.db" });
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);
1263 # Force session flush with safe umask.
1264 my $oldmask=umask(077);
1271 if ($do eq 'create' || $do eq 'edit') {
1272 cgi_editpage($q, $session);
1275 error("unknown do parameter");
1279 sub setup () { # {{{
1280 my $setup=possibly_foolish_untaint($config{setup});
1281 delete $config{setup};
1282 open (IN, $setup) || error("read $setup: $!\n");
1285 ($code)=$code=~/(.*)/s;
1294 setup() if $config{setup};
1296 if ($config{wrapper}) {
1297 gen_wrapper(%config);
1300 memoize('pagename');
1301 memoize('bestlink');
1302 loadindex() unless $config{rebuild};
1307 rcs_update() if $config{svn};