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_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
468 # The file is relative to the srcdir.
471 if (-d "$config{srcdir}/.svn") {
472 # For subversion, return the revision of the file when
474 my $rev=svn_info("Revision", "$config{srcdir}/$file");
475 return defined $rev ? $rev : "";
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.
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
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");
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");
512 return undef # success
515 sub rcs_add ($) { #{{{
516 # filename is relative to the root of the srcdir
519 if (-d "$config{srcdir}/.svn") {
520 my $parent=dirname($file);
521 while (! -d "$config{srcdir}/$parent/.svn") {
523 $parent=dirname($file);
526 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
527 warn("svn add failed\n");
532 sub svn_info ($$) { #{{{
536 my $info=`LANG=C svn info $file`;
537 my ($ret)=$info=~/^$field: (.*)$/m;
541 sub rcs_recentchanges ($) { #{{{
545 eval q{use CGI 'escapeHTML'};
546 eval q{use Date::Parse};
547 eval q{use Time::Duration};
549 if (-d "$config{srcdir}/.svn") {
550 my $svn_url=svn_info("URL", $config{srcdir});
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(?:/.*)?)$!;
556 my $div=qr/^--------------------+$/;
557 my $infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
559 my ($rev, $user, $when, @pages, @message);
560 foreach (`LANG=C svn log --limit $num -v '$svn_url'`) {
562 if ($state eq 'start' && /$div/) {
565 elsif ($state eq 'header' && /$infoline/) {
568 $when=concise(ago(time - str2time($3)));
570 elsif ($state eq 'header' && /^\s+[A-Z]\s+\Q$svn_base\E\/(.+)$/) {
571 push @pages, { link => htmllink("", pagename($1), 1) }
574 elsif ($state eq 'header' && /^$/) {
577 elsif ($state eq 'body' && /$div/) {
578 my $committype="web";
579 if (defined $message[0] &&
580 $message[0]->{line}=~/^web commit by (\w+):?(.*)/) {
582 $message[0]->{line}=$2;
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;
596 $rev=$user=$when=undef;
599 elsif ($state eq 'body') {
600 push @message, {line => escapeHTML($_)},
612 my $dir=dirname($file);
613 while (rmdir($dir)) {
618 sub refresh () { #{{{
619 # find existing pages
622 eval q{use File::Find};
626 if (/$config{wiki_file_prune_regexp}/) {
628 $File::Find::prune=1;
631 elsif (! -d $_ && ! -l $_) {
632 my ($f)=/$config{wiki_file_regexp}/; # untaint
634 warn("skipping bad filename $_\n");
637 $f=~s/^\Q$config{srcdir}\E\/?//;
639 $exists{pagename($f)}=1;
647 # check for added or removed pages
649 foreach my $file (@files) {
650 my $page=pagename($file);
651 if (! $oldpagemtime{$page}) {
652 debug("new page $page");
655 $pagesources{$page}=$file;
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};
670 # render any updated files
671 foreach my $file (@files) {
672 my $page=pagename($file);
674 if (! exists $oldpagemtime{$page} ||
675 mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
676 debug("rendering changed file $file");
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
688 FILE: foreach my $file (@files) {
689 my $page=pagename($file);
690 foreach my $f (@add, @del) {
692 foreach my $link (@{$links{$page}}) {
693 if (bestlink($page, $link) eq $p) {
694 debug("rendering $file, which links to $p");
704 # handle backlinks; if a page has added/removed links, update the
706 # TODO: inefficient; pages may get rendered above and again here;
707 # problem is the backlinks could be wrong in the first pass render
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}}) {
716 ! exists $oldlinks{$page} ||
717 ! grep { $_ eq $link } @{$oldlinks{$page}}) {
718 $linkchanged{$link}=1;
722 if (exists $oldlinks{$page}) {
723 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
725 ! exists $links{$page} ||
726 ! grep { $_ eq $link } @{$links{$page}}) {
727 $linkchanged{$link}=1;
732 foreach my $link (keys %linkchanged) {
733 my $linkfile=$pagesources{$link};
734 if (defined $linkfile) {
735 debug("rendering $linkfile, to update its backlinks");
742 sub gen_wrapper (@) { #{{{
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);
749 error("$this doesn't seem to be executable");
752 if ($config{setup}) {
753 error("cannot create a wrapper that uses a setup file");
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);
768 foreach my $p ($this, $this, @params) {
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};
778 foreach my $var (@envsave) {
780 if ((s=getenv("$var")))
781 asprintf(&newenviron[i++], "%s=%s", "$var", s);
785 open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
787 /* A wrapper for ikiwiki, can be safely made suid. */
794 extern char **environ;
796 int main (int argc, char **argv) {
797 /* Sanitize environment. */
799 char *newenviron[$#envsave+3];
802 newenviron[i++]="HOME=$ENV{HOME}";
806 if (argc == 2 && strcmp(argv[1], "--params") == 0) {
807 printf("$params\\n");
812 perror("failed to run $this");
817 if (system("gcc", "ikiwiki-wrap.c", "-o", possibly_foolish_untaint($config{wrapper})) != 0) {
818 error("failed to compile ikiwiki-wrap.c");
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}: $!");
825 print "successfully generated $config{wrapper}\n";
828 sub misctemplate ($$) { #{{{
832 my $template=HTML::Template->new(
833 filename => "$config{templatedir}/misc.tmpl"
837 indexlink => indexlink(),
838 wikiname => $config{wikiname},
839 pagebody => $pagebody,
841 return $template->output;
844 sub cgi_recentchanges ($) { #{{{
847 my $template=HTML::Template->new(
848 filename => "$config{templatedir}/recentchanges.tmpl"
851 title => "RecentChanges",
852 indexlink => indexlink(),
853 wikiname => $config{wikiname},
854 changelog => [rcs_recentchanges(100)],
856 print $q->header, $template->output;
859 sub userinfo_get ($$) { #{{{
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}) {
869 return $userdata->{$user}->{$field};
872 sub userinfo_set ($$) { #{{{
876 eval q{use Storable};
877 my $userdata=eval{ Storable::lock_retrieve("$config{srcdir}/.ikiwiki/userdb") };
878 if (! defined $userdata || ! ref $userdata) {
881 $userdata->{$user}=$info;
882 my $oldmask=umask(077);
883 my $ret=Storable::lock_store($userdata, "$config{srcdir}/.ikiwiki/userdb");
888 sub cgi_signin ($$) { #{{{
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)],
899 confirm_password => {
900 perl => q{eq $form->field("password")},
907 action => $q->request_uri,
909 template => (-e "$config{templatedir}/signin.tmpl" ?
910 "$config{templatedir}/signin.tmpl" : "")
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.");
924 if ($form->submitted) {
925 # Set required fields based on how form was submitted.
927 "Login" => [qw(name password)],
928 "Register" => [qw(name password confirm_password email)],
929 "Mail Password" => [qw(name)],
931 foreach my $opt (@{$required{$form->submitted}}) {
932 $form->field(name => $opt, required => 1);
935 # Validate password differently depending on how
936 # form was submitted.
937 if ($form->submitted eq 'Login') {
941 length $form->field("name") &&
942 shift eq userinfo_get($form->field("name"), 'password');
945 $form->field(name => "name", validate => '/^\w+$/');
948 $form->field(name => "password", validate => 'VALUE');
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') {
958 ! userinfo_get($name, "regdate");
968 userinfo_get($name, "regdate");
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"));
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') {
989 "$config{cgiurl}?do=".$form->field("do").
990 "&page=".$form->field("page").
991 "&from=".$form->field("from"));;
994 print $q->redirect($config{url});
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'),
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"]));
1011 error("Error saving registration.");
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"
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},
1027 eval q{use Mail::Sendmail};
1028 my ($fromhost) = $config{cgiurl} =~ m!/([^/]+)!;
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");
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"]));
1043 print $session->header();
1044 print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
1048 sub cgi_editpage ($$) { #{{{
1052 eval q{use CGI::FormBuilder};
1053 my $form = CGI::FormBuilder->new(
1054 fields => [qw(do rcsinfo from page content comments)],
1060 required => [qw{content}],
1063 action => $q->request_uri,
1065 template => "$config{templatedir}/editpage.tmpl"
1067 my @buttons=("Save Page", "Preview", "Cancel");
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");
1076 my $file=$page.$config{default_pageext};
1078 if (exists $pagesources{lc($page)}) {
1079 $file=$pagesources{lc($page)};
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,
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),
1099 if ($form->submitted eq "Cancel") {
1100 print $q->redirect("$config{url}/".htmlpage($page));
1103 elsif ($form->submitted eq "Preview") {
1104 $form->tmpl_param("page_preview",
1105 htmlize($config{default_pageext},
1106 linkify($form->field('content'), $page)));
1109 $form->tmpl_param("page_preview", "");
1111 $form->tmpl_param("page_conflict", "");
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
1119 print $q->redirect("$config{url}/".htmlpage($page));
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;
1134 push @page_locs, $dir.$page;
1135 push @page_locs, "$from/$page";
1136 $best_loc="$from/$page";
1137 while (length $dir) {
1139 push @page_locs, $dir.$page;
1142 @page_locs = grep { ! exists
1143 $pagesources{lc($_)} } @page_locs;
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");
1151 elsif ($form->field("do") eq "edit") {
1152 if (! defined $form->field('content') ||
1153 ! length $form->field('content')) {
1155 if (exists $pagesources{lc($page)}) {
1156 $content=readfile("$config{srcdir}/$pagesources{lc($page)}");
1157 $content=~s/\n/\r\n/g;
1159 $form->field(name => "content", value => $content,
1162 $form->tmpl_param("page_select", 0);
1163 $form->field(name => "page", type => 'hidden');
1164 $form->title("editing $page");
1167 print $form->render(submit => \@buttons);
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);
1176 my $message="web commit ";
1177 if ($session->param("name")) {
1178 $message.="by ".$session->param("name");
1181 $message.="from $ENV{REMOTE_ADDR}";
1183 if (defined $form->field('comments') &&
1184 length $form->field('comments')) {
1185 $message.=": ".$form->field('comments');
1192 # prevent deadlock with post-commit hook
1194 # presumably the commit will trigger an update
1196 my $conflict=rcs_commit($file, $message,
1197 $form->field("rcsinfo"));
1199 if (defined $conflict) {
1200 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
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);
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");
1226 eval q{use CGI::Session};
1230 my $do=$q->param('do');
1231 if (! defined $do || ! length $do) {
1232 error("\"do\" parameter missing");
1235 # This does not need a session.
1236 if ($do eq 'recentchanges') {
1237 cgi_recentchanges($q);
1241 CGI::Session->name("ikiwiki_session");
1243 my $oldmask=umask(077);
1244 my $session = CGI::Session->new("driver:db_file", $q,
1245 { FileName => "$config{srcdir}/.ikiwiki/sessions.db" });
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);
1253 # Force session flush with safe umask.
1254 my $oldmask=umask(077);
1261 if ($do eq 'create' || $do eq 'edit') {
1262 cgi_editpage($q, $session);
1265 error("unknown do parameter");
1269 sub setup () { # {{{
1270 my $setup=possibly_foolish_untaint($config{setup});
1271 delete $config{setup};
1272 open (IN, $setup) || error("read $setup: $!\n");
1275 ($code)=$code=~/(.*)/s;
1285 setup() if $config{setup};
1286 if ($config{wrapper}) {
1287 gen_wrapper(%config);
1290 memoize('pagename');
1291 memoize('bestlink');
1292 loadindex() unless $config{rebuild};
1297 rcs_update() if $config{svn};