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 ($$) { #{{{
331 my $title=basename($page);
334 my $template=HTML::Template->new(blind_cache => 1,
335 filename => "$config{templatedir}/page.tmpl");
337 if (length $config{cgiurl}) {
338 $template->param(editurl => "$config{cgiurl}?do=edit&page=$page");
340 $template->param(recentchangesurl => "$config{cgiurl}?do=recentchanges");
344 if (length $config{historyurl}) {
345 my $u=$config{historyurl};
346 $u=~s/\[\[\]\]/$pagesources{$page}/g;
347 $template->param(historyurl => $u);
352 wikiname => $config{wikiname},
353 parentlinks => [parentlinks($page)],
355 backlinks => [backlinks($page)],
356 discussionlink => htmllink($page, "Discussion", 1, 1),
359 return $template->output;
362 sub check_overwrite ($$) { #{{{
363 # Important security check. Make sure to call this before saving
364 # any files to the source directory.
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
372 ", before, so not rendering from $src");
376 sub render ($) { #{{{
379 my $type=pagetype($file);
380 my $content=readfile("$config{srcdir}/$file");
381 if ($type ne 'unknown') {
382 my $page=pagename($file);
384 $links{$page}=[findlinks($content, $page)];
386 $content=linkify($content, $page);
387 $content=htmlize($type, $content);
388 $content=finalize($content, $page);
390 check_overwrite("$config{destdir}/".htmlpage($page), $page);
391 writefile("$config{destdir}/".htmlpage($page), $content);
392 $oldpagemtime{$page}=time;
393 $renderedfiles{$page}=htmlpage($page);
397 check_overwrite("$config{destdir}/$file", $file);
398 writefile("$config{destdir}/$file", $content);
399 $oldpagemtime{$file}=time;
400 $renderedfiles{$file}=$file;
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");
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
416 return if flock(WIKILOCK, 2 | 4);
419 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
423 sub unlockwiki () { #{{{
427 sub loadindex () { #{{{
428 open (IN, "$config{srcdir}/.ikiwiki/index") || return;
430 $_=possibly_foolish_untaint($_);
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;
443 sub saveindex () { #{{{
444 if (! -d "$config{srcdir}/.ikiwiki") {
445 mkdir("$config{srcdir}/.ikiwiki");
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};
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");
464 sub rcs_commit ($) { #{{{
467 if (-d "$config{srcdir}/.svn") {
468 if (system("svn", "commit", "--quiet", "-m",
469 possibly_foolish_untaint($message),
470 $config{srcdir}) != 0) {
471 warn("svn commit failed\n");
476 sub rcs_add ($) { #{{{
479 if (-d "$config{srcdir}/.svn") {
480 my $parent=dirname($file);
481 while (! -d "$config{srcdir}/$parent/.svn") {
483 $parent=dirname($file);
486 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
487 warn("svn add failed\n");
492 sub rcs_recentchanges ($) { #{{{
496 eval q{use CGI 'escapeHTML'};
497 eval q{use Date::Parse};
498 eval q{use Time::Duration};
500 if (-d "$config{srcdir}/.svn") {
501 my $info=`LANG=C svn info $config{srcdir}`;
502 my ($svn_url)=$info=~/^URL: (.*)$/m;
504 # FIXME: currently assumes that the wiki is somewhere
505 # under trunk in svn, doesn't support other layouts.
506 my ($svn_base)=$svn_url=~m!(/trunk(?:/.*)?)$!;
508 my $div=qr/^--------------------+$/;
509 my $infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
511 my ($rev, $user, $when, @pages, @message);
512 foreach (`LANG=C svn log --limit $num -v '$svn_url'`) {
514 if ($state eq 'start' && /$div/) {
517 elsif ($state eq 'header' && /$infoline/) {
520 $when=concise(ago(time - str2time($3)));
522 elsif ($state eq 'header' && /^\s+[A-Z]\s+\Q$svn_base\E\/(.+)$/) {
523 push @pages, { link => htmllink("", pagename($1), 1) }
526 elsif ($state eq 'header' && /^$/) {
529 elsif ($state eq 'body' && /$div/) {
530 my $committype="web";
531 if (defined $message[0] &&
532 $message[0]->{line}=~/^web commit by (\w+):?(.*)/) {
534 $message[0]->{line}=$2;
540 push @ret, { rev => $rev,
541 user => htmllink("", $user, 1),
542 committype => $committype,
543 when => $when, message => [@message],
544 pages => [@pages] } if @pages;
545 return @ret if @ret >= $num;
548 $rev=$user=$when=undef;
551 elsif ($state eq 'body') {
552 push @message, {line => escapeHTML($_)},
564 my $dir=dirname($file);
565 while (rmdir($dir)) {
570 sub refresh () { #{{{
571 # find existing pages
574 eval q{use File::Find};
578 if (/$config{wiki_file_prune_regexp}/) {
580 $File::Find::prune=1;
583 elsif (! -d $_ && ! -l $_) {
584 my ($f)=/$config{wiki_file_regexp}/; # untaint
586 warn("skipping bad filename $_\n");
589 $f=~s/^\Q$config{srcdir}\E\/?//;
591 $exists{pagename($f)}=1;
599 # check for added or removed pages
601 foreach my $file (@files) {
602 my $page=pagename($file);
603 if (! $oldpagemtime{$page}) {
604 debug("new page $page");
607 $pagesources{$page}=$file;
611 foreach my $page (keys %oldpagemtime) {
612 if (! $exists{$page}) {
613 debug("removing old page $page");
614 push @del, $pagesources{$page};
615 prune($config{destdir}."/".$renderedfiles{$page});
616 delete $renderedfiles{$page};
617 $oldpagemtime{$page}=0;
618 delete $pagesources{$page};
622 # render any updated files
623 foreach my $file (@files) {
624 my $page=pagename($file);
626 if (! exists $oldpagemtime{$page} ||
627 mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
628 debug("rendering changed file $file");
634 # if any files were added or removed, check to see if each page
635 # needs an update due to linking to them
636 # TODO: inefficient; pages may get rendered above and again here;
637 # problem is the bestlink may have changed and we won't know until
640 FILE: foreach my $file (@files) {
641 my $page=pagename($file);
642 foreach my $f (@add, @del) {
644 foreach my $link (@{$links{$page}}) {
645 if (bestlink($page, $link) eq $p) {
646 debug("rendering $file, which links to $p");
656 # handle backlinks; if a page has added/removed links, update the
658 # TODO: inefficient; pages may get rendered above and again here;
659 # problem is the backlinks could be wrong in the first pass render
663 foreach my $file (keys %rendered, @del) {
664 my $page=pagename($file);
665 if (exists $links{$page}) {
666 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
668 ! exists $oldlinks{$page} ||
669 ! grep { $_ eq $link } @{$oldlinks{$page}}) {
670 $linkchanged{$link}=1;
674 if (exists $oldlinks{$page}) {
675 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
677 ! exists $links{$page} ||
678 ! grep { $_ eq $link } @{$links{$page}}) {
679 $linkchanged{$link}=1;
684 foreach my $link (keys %linkchanged) {
685 my $linkfile=$pagesources{$link};
686 if (defined $linkfile) {
687 debug("rendering $linkfile, to update its backlinks");
694 sub gen_wrapper (@) { #{{{
696 eval q{use Cwd 'abs_path'};
697 $config{srcdir}=abs_path($config{srcdir});
698 $config{destdir}=abs_path($config{destdir});
699 my $this=abs_path($0);
701 error("$this doesn't seem to be executable");
704 if ($config{setup}) {
705 error("cannot create a wrapper that uses a setup file");
708 my @params=($config{srcdir}, $config{templatedir}, $config{destdir},
709 "--wikiname=$config{wikiname}");
710 push @params, "--verbose" if $config{verbose};
711 push @params, "--rebuild" if $config{rebuild};
712 push @params, "--nosvn" if !$config{svn};
713 push @params, "--cgi" if $config{cgi};
714 push @params, "--url=$config{url}" if length $config{url};
715 push @params, "--cgiurl=$config{cgiurl}" if length $config{cgiurl};
716 push @params, "--historyurl=$config{historyurl}" if length $config{historyurl};
717 push @params, "--anonok" if $config{anonok};
718 my $params=join(" ", @params);
720 foreach my $p ($this, $this, @params) {
726 push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
727 CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
728 HTTP_COOKIE} if $config{cgi};
730 foreach my $var (@envsave) {
732 if ((s=getenv("$var")))
733 asprintf(&newenviron[i++], "%s=%s", "$var", s);
737 open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
739 /* A wrapper for ikiwiki, can be safely made suid. */
746 extern char **environ;
748 int main (int argc, char **argv) {
749 /* Sanitize environment. */
751 char *newenviron[$#envsave+3];
754 newenviron[i++]="HOME=$ENV{HOME}";
758 if (argc == 2 && strcmp(argv[1], "--params") == 0) {
759 printf("$params\\n");
764 perror("failed to run $this");
769 if (system("gcc", "ikiwiki-wrap.c", "-o", possibly_foolish_untaint($config{wrapper})) != 0) {
770 error("failed to compile ikiwiki-wrap.c");
772 unlink("ikiwiki-wrap.c");
773 if (defined $config{wrappermode} &&
774 ! chmod(oct($config{wrappermode}), possibly_foolish_untaint($config{wrapper}))) {
775 error("chmod $config{wrapper}: $!");
777 print "successfully generated $config{wrapper}\n";
780 sub misctemplate ($$) { #{{{
784 my $template=HTML::Template->new(
785 filename => "$config{templatedir}/misc.tmpl"
789 indexlink => indexlink(),
790 wikiname => $config{wikiname},
791 pagebody => $pagebody,
793 return $template->output;
796 sub cgi_recentchanges ($) { #{{{
799 my $template=HTML::Template->new(
800 filename => "$config{templatedir}/recentchanges.tmpl"
803 title => "RecentChanges",
804 indexlink => indexlink(),
805 wikiname => $config{wikiname},
806 changelog => [rcs_recentchanges(100)],
808 print $q->header, $template->output;
811 sub userinfo_get ($$) { #{{{
815 eval q{use Storable};
816 my $userdata=eval{ Storable::lock_retrieve("$config{srcdir}/.ikiwiki/userdb") };
817 if (! defined $userdata || ! ref $userdata ||
818 ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
821 return $userdata->{$user}->{$field};
824 sub userinfo_set ($$) { #{{{
828 eval q{use Storable};
829 my $userdata=eval{ Storable::lock_retrieve("$config{srcdir}/.ikiwiki/userdb") };
830 if (! defined $userdata || ! ref $userdata) {
833 $userdata->{$user}=$info;
834 my $oldmask=umask(077);
835 my $ret=Storable::lock_store($userdata, "$config{srcdir}/.ikiwiki/userdb");
840 sub cgi_signin ($$) { #{{{
844 eval q{use CGI::FormBuilder};
845 my $form = CGI::FormBuilder->new(
846 title => "$config{wikiname} signin",
847 fields => [qw(do page from name password confirm_password email)],
851 confirm_password => {
852 perl => q{eq $form->field("password")},
859 action => $q->request_uri,
861 template => (-e "$config{templatedir}/signin.tmpl" ?
862 "$config{templatedir}/signin.tmpl" : "")
865 $form->field(name => "name", required => 0);
866 $form->field(name => "do", type => "hidden");
867 $form->field(name => "page", type => "hidden");
868 $form->field(name => "from", type => "hidden");
869 $form->field(name => "password", type => "password", required => 0);
870 $form->field(name => "confirm_password", type => "password", required => 0);
871 $form->field(name => "email", required => 0);
872 if ($q->param("do") ne "signin") {
873 $form->text("You need to log in before you can edit pages.");
876 if ($form->submitted) {
877 # Set required fields based on how form was submitted.
879 "Login" => [qw(name password)],
880 "Register" => [qw(name password confirm_password email)],
881 "Mail Password" => [qw(name)],
883 foreach my $opt (@{$required{$form->submitted}}) {
884 $form->field(name => $opt, required => 1);
887 # Validate password differently depending on how
888 # form was submitted.
889 if ($form->submitted eq 'Login') {
893 length $form->field("name") &&
894 shift eq userinfo_get($form->field("name"), 'password');
897 $form->field(name => "name", validate => '/^\w+$/');
900 $form->field(name => "password", validate => 'VALUE');
902 # And make sure the entered name exists when logging
903 # in or sending email, and does not when registering.
904 if ($form->submitted eq 'Register') {
910 ! userinfo_get($name, "regdate");
920 userinfo_get($name, "regdate");
926 # First time settings.
927 $form->field(name => "name", comment => "use FirstnameLastName");
928 $form->field(name => "confirm_password", comment => "(only needed");
929 $form->field(name => "email", comment => "for registration)");
930 if ($session->param("name")) {
931 $form->field(name => "name", value => $session->param("name"));
935 if ($form->submitted && $form->validate) {
936 if ($form->submitted eq 'Login') {
937 $session->param("name", $form->field("name"));
938 if (defined $form->field("do") &&
939 $form->field("do") ne 'signin') {
941 "$config{cgiurl}?do=".$form->field("do").
942 "&page=".$form->field("page").
943 "&from=".$form->field("from"));;
946 print $q->redirect($config{url});
949 elsif ($form->submitted eq 'Register') {
950 my $user_name=$form->field('name');
951 if (userinfo_set($user_name, {
952 'email' => $form->field('email'),
953 'password' => $form->field('password'),
956 $form->field(name => "confirm_password", type => "hidden");
957 $form->field(name => "email", type => "hidden");
958 $form->text("Registration successful. Now you can Login.");
959 print $session->header();
960 print misctemplate($form->title, $form->render(submit => ["Login"]));
963 error("Error saving registration.");
966 elsif ($form->submitted eq 'Mail Password') {
967 my $user_name=$form->field("name");
968 my $template=HTML::Template->new(
969 filename => "$config{templatedir}/passwordmail.tmpl"
972 user_name => $user_name,
973 user_password => userinfo_get($user_name, "password"),
974 wikiurl => $config{url},
975 wikiname => $config{wikiname},
976 REMOTE_ADDR => $ENV{REMOTE_ADDR},
979 eval q{use Mail::Sendmail};
980 my ($fromhost) = $config{cgiurl} =~ m!/([^/]+)!;
982 To => userinfo_get($user_name, "email"),
983 From => "$config{wikiname} admin <".(getpwuid($>))[0]."@".$fromhost.">",
984 Subject => "$config{wikiname} information",
985 Message => $template->output,
986 ) or error("Failed to send mail");
988 $form->text("Your password has been emailed to you.");
989 $form->field(name => "name", required => 0);
990 print $session->header();
991 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
995 print $session->header();
996 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
1000 sub cgi_editpage ($$) { #{{{
1004 eval q{use CGI::FormBuilder};
1005 my $form = CGI::FormBuilder->new(
1006 fields => [qw(do from page content comments)],
1012 required => [qw{content}],
1015 action => $q->request_uri,
1017 template => "$config{templatedir}/editpage.tmpl"
1020 my ($page)=$form->param('page')=~/$config{wiki_file_regexp}/;
1021 if (! defined $page || ! length $page || $page ne $q->param('page') ||
1022 $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
1023 error("bad page name");
1027 $form->field(name => "do", type => 'hidden');
1028 $form->field(name => "from", type => 'hidden');
1029 $form->field(name => "page", value => "$page", force => 1);
1030 $form->field(name => "comments", type => "text", size => 80);
1031 $form->field(name => "content", type => "textarea", rows => 20,
1034 if ($form->submitted eq "Cancel") {
1035 print $q->redirect("$config{url}/".htmlpage($page));
1038 elsif ($form->submitted eq "Preview") {
1039 $form->tmpl_param("page_preview",
1040 htmlize($config{default_pageext},
1041 linkify($form->field('content'), $page)));
1044 $form->tmpl_param("page_preview", "");
1047 if (! $form->submitted || $form->submitted eq "Preview" ||
1048 ! $form->validate) {
1049 if ($form->field("do") eq "create") {
1050 if (exists $pagesources{lc($page)}) {
1051 # hmm, someone else made the page in the
1053 print $q->redirect("$config{url}/".htmlpage($page));
1059 my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/;
1060 if (! defined $from || ! length $from ||
1061 $from ne $form->param('from') ||
1062 $from=~/$config{wiki_file_prune_regexp}/ || $from=~/^\//) {
1063 @page_locs=$best_loc=$page;
1068 push @page_locs, $dir.$page;
1069 push @page_locs, "$from/$page";
1070 $best_loc="$from/$page";
1071 while (length $dir) {
1073 push @page_locs, $dir.$page;
1076 @page_locs = grep { ! exists
1077 $pagesources{lc($_)} } @page_locs;
1080 $form->tmpl_param("page_select", 1);
1081 $form->field(name => "page", type => 'select',
1082 options => \@page_locs, value => $best_loc);
1083 $form->title("creating $page");
1085 elsif ($form->field("do") eq "edit") {
1086 if (! length $form->field('content')) {
1088 if (exists $pagesources{lc($page)}) {
1089 $content=readfile("$config{srcdir}/$pagesources{lc($page)}");
1090 $content=~s/\n/\r\n/g;
1092 $form->field(name => "content", value => $content,
1095 $form->tmpl_param("page_select", 0);
1096 $form->field(name => "page", type => 'hidden');
1097 $form->title("editing $page");
1100 $form->tmpl_param("can_commit", $config{svn});
1101 $form->tmpl_param("indexlink", indexlink());
1102 print $form->render(submit => ["Save Page", "Preview", "Cancel"]);
1106 my $file=$page.$config{default_pageext};
1108 if (exists $pagesources{lc($page)}) {
1109 $file=$pagesources{lc($page)};
1113 my $content=$form->field('content');
1114 $content=~s/\r\n/\n/g;
1115 $content=~s/\r/\n/g;
1116 writefile("$config{srcdir}/$file", $content);
1118 my $message="web commit ";
1119 if ($session->param("name")) {
1120 $message.="by ".$session->param("name");
1123 $message.="from $ENV{REMOTE_ADDR}";
1125 if (defined $form->field('comments') &&
1126 length $form->field('comments')) {
1127 $message.=": ".$form->field('comments');
1134 # prevent deadlock with post-commit hook
1136 # presumably the commit will trigger an update
1138 rcs_commit($message);
1146 # The trailing question mark tries to avoid broken
1147 # caches and get the most recent version of the page.
1148 print $q->redirect("$config{url}/".htmlpage($page)."?updated");
1154 eval q{use CGI::Session};
1158 my $do=$q->param('do');
1159 if (! defined $do || ! length $do) {
1160 error("\"do\" parameter missing");
1163 # This does not need a session.
1164 if ($do eq 'recentchanges') {
1165 cgi_recentchanges($q);
1169 CGI::Session->name("ikiwiki_session");
1171 my $oldmask=umask(077);
1172 my $session = CGI::Session->new("driver:db_file", $q,
1173 { FileName => "$config{srcdir}/.ikiwiki/sessions.db" });
1176 # Everything below this point needs the user to be signed in.
1177 if ((! $config{anonok} && ! defined $session->param("name") ||
1178 ! userinfo_get($session->param("name"), "regdate")) || $do eq 'signin') {
1179 cgi_signin($q, $session);
1181 # Force session flush with safe umask.
1182 my $oldmask=umask(077);
1189 if ($do eq 'create' || $do eq 'edit') {
1190 cgi_editpage($q, $session);
1193 error("unknown do parameter");
1197 sub setup () { # {{{
1198 my $setup=possibly_foolish_untaint($config{setup});
1199 delete $config{setup};
1200 open (IN, $setup) || error("read $setup: $!\n");
1203 ($code)=$code=~/(.*)/s;
1213 setup() if $config{setup};
1214 if ($config{wrapper}) {
1215 gen_wrapper(%config);
1218 memoize('pagename');
1219 memoize('bestlink');
1220 loadindex() unless $config{rebuild};
1225 rcs_update() if $config{svn};