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",
38 "setup=s" => \$config{setup},
39 "wikiname=s" => \$config{wikiname},
40 "verbose|v!" => \$config{verbose},
41 "rebuild!" => \$config{rebuild},
42 "wrapper=s" => sub { $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap" },
43 "wrappermode=i" => \$config{wrappermode},
44 "svn!" => \$config{svn},
45 "anonok!" => \$config{anonok},
46 "cgi!" => \$config{cgi},
47 "url=s" => \$config{url},
48 "cgiurl=s" => \$config{cgiurl},
49 "historyurl=s" => \$config{historyurl},
50 "diffurl=s" => \$config{diffurl},
52 $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
56 if (! $config{setup}) {
57 usage() unless @ARGV == 3;
58 $config{srcdir} = possibly_foolish_untaint(shift);
59 $config{templatedir} = possibly_foolish_untaint(shift);
60 $config{destdir} = possibly_foolish_untaint(shift);
61 if ($config{cgi} && ! length $config{url}) {
62 error("Must specify url to wiki with --url when using --cgi");
68 die "usage: ikiwiki [options] source templates dest\n";
73 print "Content-type: text/html\n\n";
74 print misctemplate("Error", "<p>Error: @_</p>");
80 return unless $config{verbose};
92 return (stat($page))[9];
95 sub possibly_foolish_untaint { #{{{
97 my ($untainted)=$tainted=~/(.*)/;
101 sub basename ($) { #{{{
108 sub dirname ($) { #{{{
115 sub pagetype ($) { #{{{
118 if ($page =~ /\.mdwn$/) {
126 sub pagename ($) { #{{{
129 my $type=pagetype($file);
131 $page=~s/\Q$type\E*$// unless $type eq 'unknown';
135 sub htmlpage ($) { #{{{
138 return $page.".html";
141 sub readfile ($) { #{{{
145 open (IN, "$file") || error("failed to read $file: $!");
151 sub writefile ($$) { #{{{
155 my $dir=dirname($file);
158 foreach my $s (split(m!/+!, $dir)) {
161 mkdir($d) || error("failed to create directory $d: $!");
166 open (OUT, ">$file") || error("failed to write $file: $!");
171 sub findlinks ($$) { #{{{
176 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
179 # Discussion links are a special case since they're not in the text
180 # of the page, but on its template.
181 return @links, "$page/discussion";
184 sub bestlink ($$) { #{{{
185 # Given a page and the text of a link on the page, determine which
186 # existing page that link best points to. Prefers pages under a
187 # subdirectory with the same name as the source page, failing that
188 # goes down the directory tree to the base looking for matching
196 $l.="/" if length $l;
199 if (exists $links{$l}) {
200 #debug("for $page, \"$link\", use $l");
203 } while $cwd=~s!/?[^/]+$!!;
205 #print STDERR "warning: page $page, broken link: $link\n";
209 sub isinlinableimage ($) { #{{{
212 $file=~/\.(png|gif|jpg|jpeg)$/;
218 my $noimageinline=shift; # don't turn links into inline html images
219 my $forcesubpage=shift; # force a link to a subpage
222 if (! $forcesubpage) {
223 $bestlink=bestlink($page, $link);
226 $bestlink="$page/".lc($link);
229 return $link if length $bestlink && $page eq $bestlink;
231 # TODO BUG: %renderedfiles may not have it, if the linked to page
232 # was also added and isn't yet rendered! Note that this bug is
233 # masked by the bug mentioned below that makes all new files
235 if (! grep { $_ eq $bestlink } values %renderedfiles) {
236 $bestlink=htmlpage($bestlink);
238 if (! grep { $_ eq $bestlink } values %renderedfiles) {
239 return "<a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$link"
242 $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
244 if (! $noimageinline && isinlinableimage($bestlink)) {
245 return "<img src=\"$bestlink\">";
247 return "<a href=\"$bestlink\">$link</a>";
250 sub linkify ($$) { #{{{
254 $content =~ s{(\\?)$config{wiki_link_regexp}}{
255 $1 ? "[[$2]]" : htmllink($page, $2)
261 sub htmlize ($$) { #{{{
265 if (! $INC{"/usr/bin/markdown"}) {
267 $blosxom::version="is a proper perl module too much to ask?";
269 do "/usr/bin/markdown";
272 if ($type eq '.mdwn') {
273 return Markdown::Markdown($content);
276 error("htmlization of $type not supported");
280 sub backlinks ($) { #{{{
284 foreach my $p (keys %links) {
285 next if bestlink($page, $p) eq $page;
286 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
287 my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
289 # Trim common dir prefixes from both pages.
291 my $page_trimmed=$page;
293 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
295 $p_trimmed=~s/^\Q$dir\E// &&
296 $page_trimmed=~s/^\Q$dir\E//;
298 push @links, { url => $href, page => $p_trimmed };
302 return sort { $a->{page} cmp $b->{page} } @links;
305 sub parentlinks ($) { #{{{
312 foreach my $dir (reverse split("/", $page)) {
315 unshift @ret, { url => "$path$dir.html", page => $dir };
321 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
325 sub indexlink () { #{{{
326 return "<a href=\"$config{url}\">$config{wikiname}</a>";
329 sub finalize ($$$) { #{{{
334 my $title=basename($page);
337 my $template=HTML::Template->new(blind_cache => 1,
338 filename => "$config{templatedir}/page.tmpl");
340 if (length $config{cgiurl}) {
341 $template->param(editurl => "$config{cgiurl}?do=edit&page=$page");
343 $template->param(recentchangesurl => "$config{cgiurl}?do=recentchanges");
347 if (length $config{historyurl}) {
348 my $u=$config{historyurl};
349 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
350 $template->param(historyurl => $u);
355 wikiname => $config{wikiname},
356 parentlinks => [parentlinks($page)],
358 backlinks => [backlinks($page)],
359 discussionlink => htmllink($page, "Discussion", 1, 1),
360 mtime => scalar(gmtime($mtime)),
363 return $template->output;
366 sub check_overwrite ($$) { #{{{
367 # Important security check. Make sure to call this before saving
368 # any files to the source directory.
372 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
373 error("$dest already exists and was rendered from ".
374 join(" ",(grep { $renderedfiles{$_} eq $dest } keys
376 ", before, so not rendering from $src");
380 sub render ($) { #{{{
383 my $type=pagetype($file);
384 my $content=readfile("$config{srcdir}/$file");
385 if ($type ne 'unknown') {
386 my $page=pagename($file);
388 $links{$page}=[findlinks($content, $page)];
390 $content=linkify($content, $page);
391 $content=htmlize($type, $content);
392 $content=finalize($content, $page,
393 mtime("$config{srcdir}/$file"));
395 check_overwrite("$config{destdir}/".htmlpage($page), $page);
396 writefile("$config{destdir}/".htmlpage($page), $content);
397 $oldpagemtime{$page}=time;
398 $renderedfiles{$page}=htmlpage($page);
402 check_overwrite("$config{destdir}/$file", $file);
403 writefile("$config{destdir}/$file", $content);
404 $oldpagemtime{$file}=time;
405 $renderedfiles{$file}=$file;
409 sub lockwiki () { #{{{
410 # Take an exclusive lock on the wiki to prevent multiple concurrent
411 # run issues. The lock will be dropped on program exit.
412 if (! -d "$config{srcdir}/.ikiwiki") {
413 mkdir("$config{srcdir}/.ikiwiki");
415 open(WIKILOCK, ">$config{srcdir}/.ikiwiki/lockfile") || error ("cannot write to lockfile: $!");
416 if (! flock(WIKILOCK, 2 | 4)) {
417 debug("wiki seems to be locked, waiting for lock");
418 my $wait=600; # arbitrary, but don't hang forever to
419 # prevent process pileup
421 return if flock(WIKILOCK, 2 | 4);
424 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
428 sub unlockwiki () { #{{{
432 sub loadindex () { #{{{
433 open (IN, "$config{srcdir}/.ikiwiki/index") || return;
435 $_=possibly_foolish_untaint($_);
437 my ($mtime, $file, $rendered, @links)=split(' ', $_);
438 my $page=pagename($file);
439 $pagesources{$page}=$file;
440 $oldpagemtime{$page}=$mtime;
441 $oldlinks{$page}=[@links];
442 $links{$page}=[@links];
443 $renderedfiles{$page}=$rendered;
448 sub saveindex () { #{{{
449 if (! -d "$config{srcdir}/.ikiwiki") {
450 mkdir("$config{srcdir}/.ikiwiki");
452 open (OUT, ">$config{srcdir}/.ikiwiki/index") || error("cannot write to index: $!");
453 foreach my $page (keys %oldpagemtime) {
454 print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
455 join(" ", @{$links{$page}})."\n"
456 if $oldpagemtime{$page};
461 sub rcs_update () { #{{{
462 if (-d "$config{srcdir}/.svn") {
463 if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
464 warn("svn update failed\n");
469 sub rcs_prepedit ($) { #{{{
470 # Prepares to edit a file under revision control. Returns a token
471 # that must be passed into rcs_commit when the file is ready
473 # The file is relative to the srcdir.
476 if (-d "$config{srcdir}/.svn") {
477 # For subversion, return the revision of the file when
479 my $rev=svn_info("Revision", "$config{srcdir}/$file");
480 return defined $rev ? $rev : "";
484 sub rcs_commit ($$$) { #{{{
485 # Tries to commit the page; returns undef on _success_ and
486 # a version of the page with the rcs's conflict markers on failure.
487 # The file is relative to the srcdir.
492 if (-d "$config{srcdir}/.svn") {
493 # Check to see if the page has been changed by someone
494 # else since rcs_prepedit was called.
495 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
496 my $rev=svn_info("Revision", "$config{srcdir}/$file");
497 if (defined $rev && defined $oldrev && $rev != $oldrev) {
498 # Merge their changes into the file that we've
500 chdir($config{srcdir}); # svn merge wants to be here
501 if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
502 "$config{srcdir}/$file") != 0) {
503 warn("svn merge -r$oldrev:$rev failed\n");
507 if (system("svn", "commit", "--quiet", "-m",
508 possibly_foolish_untaint($message),
509 "$config{srcdir}") != 0) {
510 my $conflict=readfile("$config{srcdir}/$file");
511 if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
512 warn("svn revert failed\n");
517 return undef # success
520 sub rcs_add ($) { #{{{
521 # filename is relative to the root of the srcdir
524 if (-d "$config{srcdir}/.svn") {
525 my $parent=dirname($file);
526 while (! -d "$config{srcdir}/$parent/.svn") {
528 $parent=dirname($file);
531 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
532 warn("svn add failed\n");
537 sub svn_info ($$) { #{{{
541 my $info=`LANG=C svn info $file`;
542 my ($ret)=$info=~/^$field: (.*)$/m;
546 sub rcs_recentchanges ($) { #{{{
550 eval q{use CGI 'escapeHTML'};
551 eval q{use Date::Parse};
552 eval q{use Time::Duration};
554 if (-d "$config{srcdir}/.svn") {
555 my $svn_url=svn_info("URL", $config{srcdir});
557 # FIXME: currently assumes that the wiki is somewhere
558 # under trunk in svn, doesn't support other layouts.
559 my ($svn_base)=$svn_url=~m!(/trunk(?:/.*)?)$!;
561 my $div=qr/^--------------------+$/;
562 my $infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
564 my ($rev, $user, $when, @pages, @message);
565 foreach (`LANG=C svn log --limit $num -v '$svn_url'`) {
567 if ($state eq 'start' && /$div/) {
570 elsif ($state eq 'header' && /$infoline/) {
573 $when=concise(ago(time - str2time($3)));
575 elsif ($state eq 'header' && /^\s+[A-Z]\s+\Q$svn_base\E\/([^ ]+)(?:$|\s)/) {
577 my $diffurl=$config{diffurl};
578 $diffurl=~s/\[\[file\]\]/$file/g;
579 $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
580 $diffurl=~s/\[\[r2\]\]/$rev/g;
582 link => htmllink("", pagename($file), 1),
586 elsif ($state eq 'header' && /^$/) {
589 elsif ($state eq 'body' && /$div/) {
590 my $committype="web";
591 if (defined $message[0] &&
592 $message[0]->{line}=~/^web commit by (\w+):?(.*)/) {
594 $message[0]->{line}=$2;
600 push @ret, { rev => $rev,
601 user => htmllink("", $user, 1),
602 committype => $committype,
603 when => $when, message => [@message],
606 return @ret if @ret >= $num;
609 $rev=$user=$when=undef;
612 elsif ($state eq 'body') {
613 push @message, {line => escapeHTML($_)},
625 my $dir=dirname($file);
626 while (rmdir($dir)) {
631 sub refresh () { #{{{
632 # find existing pages
635 eval q{use File::Find};
639 if (/$config{wiki_file_prune_regexp}/) {
641 $File::Find::prune=1;
644 elsif (! -d $_ && ! -l $_) {
645 my ($f)=/$config{wiki_file_regexp}/; # untaint
647 warn("skipping bad filename $_\n");
650 $f=~s/^\Q$config{srcdir}\E\/?//;
652 $exists{pagename($f)}=1;
660 # check for added or removed pages
662 foreach my $file (@files) {
663 my $page=pagename($file);
664 if (! $oldpagemtime{$page}) {
665 debug("new page $page");
668 $pagesources{$page}=$file;
672 foreach my $page (keys %oldpagemtime) {
673 if (! $exists{$page}) {
674 debug("removing old page $page");
675 push @del, $pagesources{$page};
676 prune($config{destdir}."/".$renderedfiles{$page});
677 delete $renderedfiles{$page};
678 $oldpagemtime{$page}=0;
679 delete $pagesources{$page};
683 # render any updated files
684 foreach my $file (@files) {
685 my $page=pagename($file);
687 if (! exists $oldpagemtime{$page} ||
688 mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
689 debug("rendering changed file $file");
695 # if any files were added or removed, check to see if each page
696 # needs an update due to linking to them
697 # TODO: inefficient; pages may get rendered above and again here;
698 # problem is the bestlink may have changed and we won't know until
701 FILE: foreach my $file (@files) {
702 my $page=pagename($file);
703 foreach my $f (@add, @del) {
705 foreach my $link (@{$links{$page}}) {
706 if (bestlink($page, $link) eq $p) {
707 debug("rendering $file, which links to $p");
717 # handle backlinks; if a page has added/removed links, update the
719 # TODO: inefficient; pages may get rendered above and again here;
720 # problem is the backlinks could be wrong in the first pass render
724 foreach my $file (keys %rendered, @del) {
725 my $page=pagename($file);
726 if (exists $links{$page}) {
727 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
729 ! exists $oldlinks{$page} ||
730 ! grep { $_ eq $link } @{$oldlinks{$page}}) {
731 $linkchanged{$link}=1;
735 if (exists $oldlinks{$page}) {
736 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
738 ! exists $links{$page} ||
739 ! grep { $_ eq $link } @{$links{$page}}) {
740 $linkchanged{$link}=1;
745 foreach my $link (keys %linkchanged) {
746 my $linkfile=$pagesources{$link};
747 if (defined $linkfile) {
748 debug("rendering $linkfile, to update its backlinks");
755 sub gen_wrapper (@) { #{{{
757 eval q{use Cwd 'abs_path'};
758 $config{srcdir}=abs_path($config{srcdir});
759 $config{destdir}=abs_path($config{destdir});
760 my $this=abs_path($0);
762 error("$this doesn't seem to be executable");
765 if ($config{setup}) {
766 error("cannot create a wrapper that uses a setup file");
769 my @params=($config{srcdir}, $config{templatedir}, $config{destdir},
770 "--wikiname=$config{wikiname}");
771 push @params, "--verbose" if $config{verbose};
772 push @params, "--rebuild" if $config{rebuild};
773 push @params, "--nosvn" if !$config{svn};
774 push @params, "--cgi" if $config{cgi};
775 push @params, "--url=$config{url}" if length $config{url};
776 push @params, "--cgiurl=$config{cgiurl}" if length $config{cgiurl};
777 push @params, "--historyurl=$config{historyurl}" if length $config{historyurl};
778 push @params, "--diffurl=$config{diffurl}" if length $config{diffurl};
779 push @params, "--anonok" if $config{anonok};
780 my $params=join(" ", @params);
782 foreach my $p ($this, $this, @params) {
788 push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
789 CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
790 HTTP_COOKIE} if $config{cgi};
792 foreach my $var (@envsave) {
794 if ((s=getenv("$var")))
795 asprintf(&newenviron[i++], "%s=%s", "$var", s);
799 open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
801 /* A wrapper for ikiwiki, can be safely made suid. */
808 extern char **environ;
810 int main (int argc, char **argv) {
811 /* Sanitize environment. */
813 char *newenviron[$#envsave+3];
816 newenviron[i++]="HOME=$ENV{HOME}";
820 if (argc == 2 && strcmp(argv[1], "--params") == 0) {
821 printf("$params\\n");
826 perror("failed to run $this");
831 if (system("gcc", "ikiwiki-wrap.c", "-o", possibly_foolish_untaint($config{wrapper})) != 0) {
832 error("failed to compile ikiwiki-wrap.c");
834 unlink("ikiwiki-wrap.c");
835 if (defined $config{wrappermode} &&
836 ! chmod(oct($config{wrappermode}), possibly_foolish_untaint($config{wrapper}))) {
837 error("chmod $config{wrapper}: $!");
839 print "successfully generated $config{wrapper}\n";
842 sub misctemplate ($$) { #{{{
846 my $template=HTML::Template->new(
847 filename => "$config{templatedir}/misc.tmpl"
851 indexlink => indexlink(),
852 wikiname => $config{wikiname},
853 pagebody => $pagebody,
855 return $template->output;
858 sub cgi_recentchanges ($) { #{{{
861 my $template=HTML::Template->new(
862 filename => "$config{templatedir}/recentchanges.tmpl"
865 title => "RecentChanges",
866 indexlink => indexlink(),
867 wikiname => $config{wikiname},
868 changelog => [rcs_recentchanges(100)],
870 print $q->header, $template->output;
873 sub userinfo_get ($$) { #{{{
877 eval q{use Storable};
878 my $userdata=eval{ Storable::lock_retrieve("$config{srcdir}/.ikiwiki/userdb") };
879 if (! defined $userdata || ! ref $userdata ||
880 ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
883 return $userdata->{$user}->{$field};
886 sub userinfo_set ($$) { #{{{
890 eval q{use Storable};
891 my $userdata=eval{ Storable::lock_retrieve("$config{srcdir}/.ikiwiki/userdb") };
892 if (! defined $userdata || ! ref $userdata) {
895 $userdata->{$user}=$info;
896 my $oldmask=umask(077);
897 my $ret=Storable::lock_store($userdata, "$config{srcdir}/.ikiwiki/userdb");
902 sub cgi_signin ($$) { #{{{
906 eval q{use CGI::FormBuilder};
907 my $form = CGI::FormBuilder->new(
908 title => "$config{wikiname} signin",
909 fields => [qw(do page from name password confirm_password email)],
913 confirm_password => {
914 perl => q{eq $form->field("password")},
921 action => $q->request_uri,
923 template => (-e "$config{templatedir}/signin.tmpl" ?
924 "$config{templatedir}/signin.tmpl" : "")
927 $form->field(name => "name", required => 0);
928 $form->field(name => "do", type => "hidden");
929 $form->field(name => "page", type => "hidden");
930 $form->field(name => "from", type => "hidden");
931 $form->field(name => "password", type => "password", required => 0);
932 $form->field(name => "confirm_password", type => "password", required => 0);
933 $form->field(name => "email", required => 0);
934 if ($q->param("do") ne "signin") {
935 $form->text("You need to log in before you can edit pages.");
938 if ($form->submitted) {
939 # Set required fields based on how form was submitted.
941 "Login" => [qw(name password)],
942 "Register" => [qw(name password confirm_password email)],
943 "Mail Password" => [qw(name)],
945 foreach my $opt (@{$required{$form->submitted}}) {
946 $form->field(name => $opt, required => 1);
949 # Validate password differently depending on how
950 # form was submitted.
951 if ($form->submitted eq 'Login') {
955 length $form->field("name") &&
956 shift eq userinfo_get($form->field("name"), 'password');
959 $form->field(name => "name", validate => '/^\w+$/');
962 $form->field(name => "password", validate => 'VALUE');
964 # And make sure the entered name exists when logging
965 # in or sending email, and does not when registering.
966 if ($form->submitted eq 'Register') {
972 ! userinfo_get($name, "regdate");
982 userinfo_get($name, "regdate");
988 # First time settings.
989 $form->field(name => "name", comment => "use FirstnameLastName");
990 $form->field(name => "confirm_password", comment => "(only needed");
991 $form->field(name => "email", comment => "for registration)");
992 if ($session->param("name")) {
993 $form->field(name => "name", value => $session->param("name"));
997 if ($form->submitted && $form->validate) {
998 if ($form->submitted eq 'Login') {
999 $session->param("name", $form->field("name"));
1000 if (defined $form->field("do") &&
1001 $form->field("do") ne 'signin') {
1003 "$config{cgiurl}?do=".$form->field("do").
1004 "&page=".$form->field("page").
1005 "&from=".$form->field("from"));;
1008 print $q->redirect($config{url});
1011 elsif ($form->submitted eq 'Register') {
1012 my $user_name=$form->field('name');
1013 if (userinfo_set($user_name, {
1014 'email' => $form->field('email'),
1015 'password' => $form->field('password'),
1018 $form->field(name => "confirm_password", type => "hidden");
1019 $form->field(name => "email", type => "hidden");
1020 $form->text("Registration successful. Now you can Login.");
1021 print $session->header();
1022 print misctemplate($form->title, $form->render(submit => ["Login"]));
1025 error("Error saving registration.");
1028 elsif ($form->submitted eq 'Mail Password') {
1029 my $user_name=$form->field("name");
1030 my $template=HTML::Template->new(
1031 filename => "$config{templatedir}/passwordmail.tmpl"
1034 user_name => $user_name,
1035 user_password => userinfo_get($user_name, "password"),
1036 wikiurl => $config{url},
1037 wikiname => $config{wikiname},
1038 REMOTE_ADDR => $ENV{REMOTE_ADDR},
1041 eval q{use Mail::Sendmail};
1042 my ($fromhost) = $config{cgiurl} =~ m!/([^/]+)!;
1044 To => userinfo_get($user_name, "email"),
1045 From => "$config{wikiname} admin <".(getpwuid($>))[0]."@".$fromhost.">",
1046 Subject => "$config{wikiname} information",
1047 Message => $template->output,
1048 ) or error("Failed to send mail");
1050 $form->text("Your password has been emailed to you.");
1051 $form->field(name => "name", required => 0);
1052 print $session->header();
1053 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
1057 print $session->header();
1058 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
1062 sub cgi_editpage ($$) { #{{{
1066 eval q{use CGI::FormBuilder};
1067 my $form = CGI::FormBuilder->new(
1068 fields => [qw(do rcsinfo from page content comments)],
1074 required => [qw{content}],
1077 action => $q->request_uri,
1079 template => "$config{templatedir}/editpage.tmpl"
1081 my @buttons=("Save Page", "Preview", "Cancel");
1083 my ($page)=$form->param('page')=~/$config{wiki_file_regexp}/;
1084 if (! defined $page || ! length $page || $page ne $q->param('page') ||
1085 $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
1086 error("bad page name");
1090 my $file=$page.$config{default_pageext};
1092 if (exists $pagesources{lc($page)}) {
1093 $file=$pagesources{lc($page)};
1097 $form->field(name => "do", type => 'hidden');
1098 $form->field(name => "from", type => 'hidden');
1099 $form->field(name => "rcsinfo", type => 'hidden');
1100 $form->field(name => "page", value => "$page", force => 1);
1101 $form->field(name => "comments", type => "text", size => 80);
1102 $form->field(name => "content", type => "textarea", rows => 20,
1104 $form->tmpl_param("can_commit", $config{svn});
1105 $form->tmpl_param("indexlink", indexlink());
1106 $form->tmpl_param("helponformattinglink",
1107 htmllink("", "HelpOnFormatting", 1));
1108 if (! $form->submitted) {
1109 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
1113 if ($form->submitted eq "Cancel") {
1114 print $q->redirect("$config{url}/".htmlpage($page));
1117 elsif ($form->submitted eq "Preview") {
1118 $form->tmpl_param("page_preview",
1119 htmlize($config{default_pageext},
1120 linkify($form->field('content'), $page)));
1123 $form->tmpl_param("page_preview", "");
1125 $form->tmpl_param("page_conflict", "");
1127 if (! $form->submitted || $form->submitted eq "Preview" ||
1128 ! $form->validate) {
1129 if ($form->field("do") eq "create") {
1130 if (exists $pagesources{lc($page)}) {
1131 # hmm, someone else made the page in the
1133 print $q->redirect("$config{url}/".htmlpage($page));
1139 my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/;
1140 if (! defined $from || ! length $from ||
1141 $from ne $form->param('from') ||
1142 $from=~/$config{wiki_file_prune_regexp}/ || $from=~/^\//) {
1143 @page_locs=$best_loc=$page;
1149 if ($page eq 'discussion') {
1150 $best_loc="$from/$page";
1153 $best_loc=$dir.$page;
1156 push @page_locs, $dir.$page;
1157 push @page_locs, "$from/$page";
1158 while (length $dir) {
1160 push @page_locs, $dir.$page;
1163 @page_locs = grep { ! exists
1164 $pagesources{lc($_)} } @page_locs;
1167 $form->tmpl_param("page_select", 1);
1168 $form->field(name => "page", type => 'select',
1169 options => \@page_locs, value => $best_loc);
1170 $form->title("creating $page");
1172 elsif ($form->field("do") eq "edit") {
1173 if (! defined $form->field('content') ||
1174 ! length $form->field('content')) {
1176 if (exists $pagesources{lc($page)}) {
1177 $content=readfile("$config{srcdir}/$pagesources{lc($page)}");
1178 $content=~s/\n/\r\n/g;
1180 $form->field(name => "content", value => $content,
1183 $form->tmpl_param("page_select", 0);
1184 $form->field(name => "page", type => 'hidden');
1185 $form->title("editing $page");
1188 print $form->render(submit => \@buttons);
1192 my $content=$form->field('content');
1193 $content=~s/\r\n/\n/g;
1194 $content=~s/\r/\n/g;
1195 writefile("$config{srcdir}/$file", $content);
1197 my $message="web commit ";
1198 if ($session->param("name")) {
1199 $message.="by ".$session->param("name");
1202 $message.="from $ENV{REMOTE_ADDR}";
1204 if (defined $form->field('comments') &&
1205 length $form->field('comments')) {
1206 $message.=": ".$form->field('comments');
1213 # prevent deadlock with post-commit hook
1215 # presumably the commit will trigger an update
1217 my $conflict=rcs_commit($file, $message,
1218 $form->field("rcsinfo"));
1220 if (defined $conflict) {
1221 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
1223 $form->tmpl_param("page_conflict", 1);
1224 $form->field("content", value => $conflict, force => 1);
1225 $form->field("do", "edit)");
1226 $form->tmpl_param("page_select", 0);
1227 $form->field(name => "page", type => 'hidden');
1228 $form->title("editing $page");
1229 print $form->render(submit => \@buttons);
1239 # The trailing question mark tries to avoid broken
1240 # caches and get the most recent version of the page.
1241 print $q->redirect("$config{url}/".htmlpage($page)."?updated");
1247 eval q{use CGI::Session};
1251 my $do=$q->param('do');
1252 if (! defined $do || ! length $do) {
1253 error("\"do\" parameter missing");
1256 # This does not need a session.
1257 if ($do eq 'recentchanges') {
1258 cgi_recentchanges($q);
1262 CGI::Session->name("ikiwiki_session");
1264 my $oldmask=umask(077);
1265 my $session = CGI::Session->new("driver:db_file", $q,
1266 { FileName => "$config{srcdir}/.ikiwiki/sessions.db" });
1269 # Everything below this point needs the user to be signed in.
1270 if ((! $config{anonok} && ! defined $session->param("name") ||
1271 ! userinfo_get($session->param("name"), "regdate")) || $do eq 'signin') {
1272 cgi_signin($q, $session);
1274 # Force session flush with safe umask.
1275 my $oldmask=umask(077);
1282 if ($do eq 'create' || $do eq 'edit') {
1283 cgi_editpage($q, $session);
1286 error("unknown do parameter");
1290 sub setup () { # {{{
1291 my $setup=possibly_foolish_untaint($config{setup});
1292 delete $config{setup};
1293 open (IN, $setup) || error("read $setup: $!\n");
1296 ($code)=$code=~/(.*)/s;
1305 setup() if $config{setup};
1307 if ($config{wrapper}) {
1308 gen_wrapper(%config);
1311 memoize('pagename');
1312 memoize('bestlink');
1313 loadindex() unless $config{rebuild};
1318 rcs_update() if $config{svn};