summaryrefslogtreecommitdiff
path: root/ikiwiki
blob: 6b8a5153512b3a2e83bc4f4194154ce7a4f7a56e (plain)
  1. #!/usr/bin/perl -T
  2. eval 'exec /usr/bin/perl -T -S $0 ${1+"$@"}'
  3. if 0; # not running under some shell
  4. $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
  5. use warnings;
  6. use strict;
  7. use Memoize;
  8. use File::Spec;
  9. use HTML::Template;
  10. use Getopt::Long;
  11. my (%links, %oldlinks, %oldpagemtime, %renderedfiles, %pagesources);
  12. # Holds global config settings, also used by some modules.
  13. our %config=( #{{{
  14. wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$)},
  15. wiki_link_regexp => qr/\[\[([^\s\]]+)\]\]/,
  16. wiki_file_regexp => qr/(^[-A-Za-z0-9_.:\/+]+$)/,
  17. verbose => 0,
  18. wikiname => "wiki",
  19. default_pageext => ".mdwn",
  20. cgi => 0,
  21. svn => 1,
  22. url => '',
  23. cgiurl => '',
  24. historyurl => '',
  25. diffurl => '',
  26. anonok => 0,
  27. rebuild => 0,
  28. wrapper => undef,
  29. wrappermode => undef,
  30. srcdir => undef,
  31. destdir => undef,
  32. templatedir => "/usr/share/ikiwiki/templates",
  33. setup => undef,
  34. adminuser => undef,
  35. ); #}}}
  36. GetOptions( #{{{
  37. "setup|s=s" => \$config{setup},
  38. "wikiname=s" => \$config{wikiname},
  39. "verbose|v!" => \$config{verbose},
  40. "rebuild!" => \$config{rebuild},
  41. "wrapper=s" => sub { $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap" },
  42. "wrappermode=i" => \$config{wrappermode},
  43. "svn!" => \$config{svn},
  44. "anonok!" => \$config{anonok},
  45. "cgi!" => \$config{cgi},
  46. "url=s" => \$config{url},
  47. "cgiurl=s" => \$config{cgiurl},
  48. "historyurl=s" => \$config{historyurl},
  49. "diffurl=s" => \$config{diffurl},
  50. "exclude=s@" => sub {
  51. $config{wiki_file_prune_regexp}=qr/$config{wiki_file_prune_regexp}|$_[1]/;
  52. },
  53. "adminuser=s@" => sub { push @{$config{adminuser}}, $_[1] },
  54. "templatedir=s" => sub { $config{templatedir}=possibly_foolish_untaint($_[1]) },
  55. ) || usage();
  56. if (! $config{setup}) {
  57. usage() unless @ARGV == 2;
  58. $config{srcdir} = possibly_foolish_untaint(shift);
  59. $config{destdir} = possibly_foolish_untaint(shift);
  60. checkoptions();
  61. }
  62. #}}}
  63. sub checkoptions { #{{{
  64. if ($config{cgi} && ! length $config{url}) {
  65. error("Must specify url to wiki with --url when using --cgi");
  66. }
  67. $config{wikistatedir}="$config{srcdir}/.ikiwiki"
  68. unless exists $config{wikistatedir};
  69. } #}}}
  70. sub usage { #{{{
  71. die "usage: ikiwiki [options] source dest\n";
  72. } #}}}
  73. sub error { #{{{
  74. if ($config{cgi}) {
  75. print "Content-type: text/html\n\n";
  76. print misctemplate("Error", "<p>Error: @_</p>");
  77. }
  78. die @_;
  79. } #}}}
  80. sub debug ($) { #{{{
  81. return unless $config{verbose};
  82. if (! $config{cgi}) {
  83. print "@_\n";
  84. }
  85. else {
  86. print STDERR "@_\n";
  87. }
  88. } #}}}
  89. sub mtime ($) { #{{{
  90. my $page=shift;
  91. return (stat($page))[9];
  92. } #}}}
  93. sub possibly_foolish_untaint { #{{{
  94. my $tainted=shift;
  95. my ($untainted)=$tainted=~/(.*)/;
  96. return $untainted;
  97. } #}}}
  98. sub basename ($) { #{{{
  99. my $file=shift;
  100. $file=~s!.*/!!;
  101. return $file;
  102. } #}}}
  103. sub dirname ($) { #{{{
  104. my $file=shift;
  105. $file=~s!/?[^/]+$!!;
  106. return $file;
  107. } #}}}
  108. sub pagetype ($) { #{{{
  109. my $page=shift;
  110. if ($page =~ /\.mdwn$/) {
  111. return ".mdwn";
  112. }
  113. else {
  114. return "unknown";
  115. }
  116. } #}}}
  117. sub pagename ($) { #{{{
  118. my $file=shift;
  119. my $type=pagetype($file);
  120. my $page=$file;
  121. $page=~s/\Q$type\E*$// unless $type eq 'unknown';
  122. return $page;
  123. } #}}}
  124. sub htmlpage ($) { #{{{
  125. my $page=shift;
  126. return $page.".html";
  127. } #}}}
  128. sub readfile ($) { #{{{
  129. my $file=shift;
  130. if (-l $file) {
  131. error("cannot read a symlink ($file)");
  132. }
  133. local $/=undef;
  134. open (IN, "$file") || error("failed to read $file: $!");
  135. my $ret=<IN>;
  136. close IN;
  137. return $ret;
  138. } #}}}
  139. sub writefile ($$) { #{{{
  140. my $file=shift;
  141. my $content=shift;
  142. if (-l $file) {
  143. error("cannot write to a symlink ($file)");
  144. }
  145. my $dir=dirname($file);
  146. if (! -d $dir) {
  147. my $d="";
  148. foreach my $s (split(m!/+!, $dir)) {
  149. $d.="$s/";
  150. if (! -d $d) {
  151. mkdir($d) || error("failed to create directory $d: $!");
  152. }
  153. }
  154. }
  155. open (OUT, ">$file") || error("failed to write $file: $!");
  156. print OUT $content;
  157. close OUT;
  158. } #}}}
  159. sub findlinks ($$) { #{{{
  160. my $content=shift;
  161. my $page=shift;
  162. my @links;
  163. while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
  164. push @links, lc($1);
  165. }
  166. # Discussion links are a special case since they're not in the text
  167. # of the page, but on its template.
  168. return @links, "$page/discussion";
  169. } #}}}
  170. sub bestlink ($$) { #{{{
  171. # Given a page and the text of a link on the page, determine which
  172. # existing page that link best points to. Prefers pages under a
  173. # subdirectory with the same name as the source page, failing that
  174. # goes down the directory tree to the base looking for matching
  175. # pages.
  176. my $page=shift;
  177. my $link=lc(shift);
  178. my $cwd=$page;
  179. do {
  180. my $l=$cwd;
  181. $l.="/" if length $l;
  182. $l.=$link;
  183. if (exists $links{$l}) {
  184. #debug("for $page, \"$link\", use $l");
  185. return $l;
  186. }
  187. } while $cwd=~s!/?[^/]+$!!;
  188. #print STDERR "warning: page $page, broken link: $link\n";
  189. return "";
  190. } #}}}
  191. sub isinlinableimage ($) { #{{{
  192. my $file=shift;
  193. $file=~/\.(png|gif|jpg|jpeg)$/;
  194. } #}}}
  195. sub htmllink { #{{{
  196. my $page=shift;
  197. my $link=shift;
  198. my $noimageinline=shift; # don't turn links into inline html images
  199. my $forcesubpage=shift; # force a link to a subpage
  200. my $bestlink;
  201. if (! $forcesubpage) {
  202. $bestlink=bestlink($page, $link);
  203. }
  204. else {
  205. $bestlink="$page/".lc($link);
  206. }
  207. return $link if length $bestlink && $page eq $bestlink;
  208. # TODO BUG: %renderedfiles may not have it, if the linked to page
  209. # was also added and isn't yet rendered! Note that this bug is
  210. # masked by the bug mentioned below that makes all new files
  211. # be rendered twice.
  212. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  213. $bestlink=htmlpage($bestlink);
  214. }
  215. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  216. return "<a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$link"
  217. }
  218. $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
  219. if (! $noimageinline && isinlinableimage($bestlink)) {
  220. return "<img src=\"$bestlink\">";
  221. }
  222. return "<a href=\"$bestlink\">$link</a>";
  223. } #}}}
  224. sub linkify ($$) { #{{{
  225. my $content=shift;
  226. my $page=shift;
  227. $content =~ s{(\\?)$config{wiki_link_regexp}}{
  228. $1 ? "[[$2]]" : htmllink($page, $2)
  229. }eg;
  230. return $content;
  231. } #}}}
  232. sub htmlize ($$) { #{{{
  233. my $type=shift;
  234. my $content=shift;
  235. if (! $INC{"/usr/bin/markdown"}) {
  236. no warnings 'once';
  237. $blosxom::version="is a proper perl module too much to ask?";
  238. use warnings 'all';
  239. do "/usr/bin/markdown";
  240. }
  241. if ($type eq '.mdwn') {
  242. return Markdown::Markdown($content);
  243. }
  244. else {
  245. error("htmlization of $type not supported");
  246. }
  247. } #}}}
  248. sub backlinks ($) { #{{{
  249. my $page=shift;
  250. my @links;
  251. foreach my $p (keys %links) {
  252. next if bestlink($page, $p) eq $page;
  253. if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
  254. my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
  255. # Trim common dir prefixes from both pages.
  256. my $p_trimmed=$p;
  257. my $page_trimmed=$page;
  258. my $dir;
  259. 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
  260. defined $dir &&
  261. $p_trimmed=~s/^\Q$dir\E// &&
  262. $page_trimmed=~s/^\Q$dir\E//;
  263. push @links, { url => $href, page => $p_trimmed };
  264. }
  265. }
  266. return sort { $a->{page} cmp $b->{page} } @links;
  267. } #}}}
  268. sub parentlinks ($) { #{{{
  269. my $page=shift;
  270. my @ret;
  271. my $pagelink="";
  272. my $path="";
  273. my $skip=1;
  274. foreach my $dir (reverse split("/", $page)) {
  275. if (! $skip) {
  276. $path.="../";
  277. unshift @ret, { url => "$path$dir.html", page => $dir };
  278. }
  279. else {
  280. $skip=0;
  281. }
  282. }
  283. unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
  284. return @ret;
  285. } #}}}
  286. sub indexlink () { #{{{
  287. return "<a href=\"$config{url}\">$config{wikiname}</a>";
  288. } #}}}
  289. sub finalize ($$$) { #{{{
  290. my $content=shift;
  291. my $page=shift;
  292. my $mtime=shift;
  293. my $title=basename($page);
  294. $title=~s/_/ /g;
  295. my $template=HTML::Template->new(blind_cache => 1,
  296. filename => "$config{templatedir}/page.tmpl");
  297. if (length $config{cgiurl}) {
  298. $template->param(editurl => "$config{cgiurl}?do=edit&page=$page");
  299. $template->param(prefsurl => "$config{cgiurl}?do=prefs");
  300. if ($config{svn}) {
  301. $template->param(recentchangesurl => "$config{cgiurl}?do=recentchanges");
  302. }
  303. }
  304. if (length $config{historyurl}) {
  305. my $u=$config{historyurl};
  306. $u=~s/\[\[file\]\]/$pagesources{$page}/g;
  307. $template->param(historyurl => $u);
  308. }
  309. $template->param(
  310. title => $title,
  311. wikiname => $config{wikiname},
  312. parentlinks => [parentlinks($page)],
  313. content => $content,
  314. backlinks => [backlinks($page)],
  315. discussionlink => htmllink($page, "Discussion", 1, 1),
  316. mtime => scalar(gmtime($mtime)),
  317. );
  318. return $template->output;
  319. } #}}}
  320. sub check_overwrite ($$) { #{{{
  321. # Important security check. Make sure to call this before saving
  322. # any files to the source directory.
  323. my $dest=shift;
  324. my $src=shift;
  325. if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
  326. error("$dest already exists and was rendered from ".
  327. join(" ",(grep { $renderedfiles{$_} eq $dest } keys
  328. %renderedfiles)).
  329. ", before, so not rendering from $src");
  330. }
  331. } #}}}
  332. sub render ($) { #{{{
  333. my $file=shift;
  334. my $type=pagetype($file);
  335. my $content=readfile("$config{srcdir}/$file");
  336. if ($type ne 'unknown') {
  337. my $page=pagename($file);
  338. $links{$page}=[findlinks($content, $page)];
  339. $content=linkify($content, $page);
  340. $content=htmlize($type, $content);
  341. $content=finalize($content, $page,
  342. mtime("$config{srcdir}/$file"));
  343. check_overwrite("$config{destdir}/".htmlpage($page), $page);
  344. writefile("$config{destdir}/".htmlpage($page), $content);
  345. $oldpagemtime{$page}=time;
  346. $renderedfiles{$page}=htmlpage($page);
  347. }
  348. else {
  349. $links{$file}=[];
  350. check_overwrite("$config{destdir}/$file", $file);
  351. writefile("$config{destdir}/$file", $content);
  352. $oldpagemtime{$file}=time;
  353. $renderedfiles{$file}=$file;
  354. }
  355. } #}}}
  356. sub lockwiki () { #{{{
  357. # Take an exclusive lock on the wiki to prevent multiple concurrent
  358. # run issues. The lock will be dropped on program exit.
  359. if (! -d $config{wikistatedir}) {
  360. mkdir($config{wikistatedir});
  361. }
  362. open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
  363. error ("cannot write to $config{wikistatedir}/lockfile: $!");
  364. if (! flock(WIKILOCK, 2 | 4)) {
  365. debug("wiki seems to be locked, waiting for lock");
  366. my $wait=600; # arbitrary, but don't hang forever to
  367. # prevent process pileup
  368. for (1..600) {
  369. return if flock(WIKILOCK, 2 | 4);
  370. sleep 1;
  371. }
  372. error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
  373. }
  374. } #}}}
  375. sub unlockwiki () { #{{{
  376. close WIKILOCK;
  377. } #}}}
  378. sub loadindex () { #{{{
  379. open (IN, "$config{wikistatedir}/index") || return;
  380. while (<IN>) {
  381. $_=possibly_foolish_untaint($_);
  382. chomp;
  383. my ($mtime, $file, $rendered, @links)=split(' ', $_);
  384. my $page=pagename($file);
  385. $pagesources{$page}=$file;
  386. $oldpagemtime{$page}=$mtime;
  387. $oldlinks{$page}=[@links];
  388. $links{$page}=[@links];
  389. $renderedfiles{$page}=$rendered;
  390. }
  391. close IN;
  392. } #}}}
  393. sub saveindex () { #{{{
  394. if (! -d $config{wikistatedir}) {
  395. mkdir($config{wikistatedir});
  396. }
  397. open (OUT, ">$config{wikistatedir}/index") ||
  398. error("cannot write to $config{wikistatedir}/index: $!");
  399. foreach my $page (keys %oldpagemtime) {
  400. print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
  401. join(" ", @{$links{$page}})."\n"
  402. if $oldpagemtime{$page};
  403. }
  404. close OUT;
  405. } #}}}
  406. sub rcs_update () { #{{{
  407. if (-d "$config{srcdir}/.svn") {
  408. if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
  409. warn("svn update failed\n");
  410. }
  411. }
  412. } #}}}
  413. sub rcs_prepedit ($) { #{{{
  414. # Prepares to edit a file under revision control. Returns a token
  415. # that must be passed into rcs_commit when the file is ready
  416. # for committing.
  417. # The file is relative to the srcdir.
  418. my $file=shift;
  419. if (-d "$config{srcdir}/.svn") {
  420. # For subversion, return the revision of the file when
  421. # editing begins.
  422. my $rev=svn_info("Revision", "$config{srcdir}/$file");
  423. return defined $rev ? $rev : "";
  424. }
  425. } #}}}
  426. sub rcs_commit ($$$) { #{{{
  427. # Tries to commit the page; returns undef on _success_ and
  428. # a version of the page with the rcs's conflict markers on failure.
  429. # The file is relative to the srcdir.
  430. my $file=shift;
  431. my $message=shift;
  432. my $rcstoken=shift;
  433. if (-d "$config{srcdir}/.svn") {
  434. # Check to see if the page has been changed by someone
  435. # else since rcs_prepedit was called.
  436. my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
  437. my $rev=svn_info("Revision", "$config{srcdir}/$file");
  438. if (defined $rev && defined $oldrev && $rev != $oldrev) {
  439. # Merge their changes into the file that we've
  440. # changed.
  441. chdir($config{srcdir}); # svn merge wants to be here
  442. if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
  443. "$config{srcdir}/$file") != 0) {
  444. warn("svn merge -r$oldrev:$rev failed\n");
  445. }
  446. }
  447. if (system("svn", "commit", "--quiet", "-m",
  448. possibly_foolish_untaint($message),
  449. "$config{srcdir}") != 0) {
  450. my $conflict=readfile("$config{srcdir}/$file");
  451. if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
  452. warn("svn revert failed\n");
  453. }
  454. return $conflict;
  455. }
  456. }
  457. return undef # success
  458. } #}}}
  459. sub rcs_add ($) { #{{{
  460. # filename is relative to the root of the srcdir
  461. my $file=shift;
  462. if (-d "$config{srcdir}/.svn") {
  463. my $parent=dirname($file);
  464. while (! -d "$config{srcdir}/$parent/.svn") {
  465. $file=$parent;
  466. $parent=dirname($file);
  467. }
  468. if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
  469. warn("svn add failed\n");
  470. }
  471. }
  472. } #}}}
  473. sub svn_info ($$) { #{{{
  474. my $field=shift;
  475. my $file=shift;
  476. my $info=`LANG=C svn info $file`;
  477. my ($ret)=$info=~/^$field: (.*)$/m;
  478. return $ret;
  479. } #}}}
  480. sub rcs_recentchanges ($) { #{{{
  481. my $num=shift;
  482. my @ret;
  483. eval q{use CGI 'escapeHTML'};
  484. eval q{use Date::Parse};
  485. eval q{use Time::Duration};
  486. if (-d "$config{srcdir}/.svn") {
  487. my $svn_url=svn_info("URL", $config{srcdir});
  488. # FIXME: currently assumes that the wiki is somewhere
  489. # under trunk in svn, doesn't support other layouts.
  490. my ($svn_base)=$svn_url=~m!(/trunk(?:/.*)?)$!;
  491. my $div=qr/^--------------------+$/;
  492. my $infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
  493. my $state='start';
  494. my ($rev, $user, $when, @pages, @message);
  495. foreach (`LANG=C svn log --limit $num -v '$svn_url'`) {
  496. chomp;
  497. if ($state eq 'start' && /$div/) {
  498. $state='header';
  499. }
  500. elsif ($state eq 'header' && /$infoline/) {
  501. $rev=$1;
  502. $user=$2;
  503. $when=concise(ago(time - str2time($3)));
  504. }
  505. elsif ($state eq 'header' && /^\s+[A-Z]\s+\Q$svn_base\E\/([^ ]+)(?:$|\s)/) {
  506. my $file=$1;
  507. my $diffurl=$config{diffurl};
  508. $diffurl=~s/\[\[file\]\]/$file/g;
  509. $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
  510. $diffurl=~s/\[\[r2\]\]/$rev/g;
  511. push @pages, {
  512. link => htmllink("", pagename($file), 1),
  513. diffurl => $diffurl,
  514. } if length $file;
  515. }
  516. elsif ($state eq 'header' && /^$/) {
  517. $state='body';
  518. }
  519. elsif ($state eq 'body' && /$div/) {
  520. my $committype="web";
  521. if (defined $message[0] &&
  522. $message[0]->{line}=~/^web commit by (\w+):?(.*)/) {
  523. $user="$1";
  524. $message[0]->{line}=$2;
  525. }
  526. else {
  527. $committype="svn";
  528. }
  529. push @ret, { rev => $rev,
  530. user => htmllink("", $user, 1),
  531. committype => $committype,
  532. when => $when, message => [@message],
  533. pages => [@pages],
  534. } if @pages;
  535. return @ret if @ret >= $num;
  536. $state='header';
  537. $rev=$user=$when=undef;
  538. @pages=@message=();
  539. }
  540. elsif ($state eq 'body') {
  541. push @message, {line => escapeHTML($_)},
  542. }
  543. }
  544. }
  545. return @ret;
  546. } #}}}
  547. sub prune ($) { #{{{
  548. my $file=shift;
  549. unlink($file);
  550. my $dir=dirname($file);
  551. while (rmdir($dir)) {
  552. $dir=dirname($dir);
  553. }
  554. } #}}}
  555. sub refresh () { #{{{
  556. # find existing pages
  557. my %exists;
  558. my @files;
  559. eval q{use File::Find};
  560. find({
  561. no_chdir => 1,
  562. wanted => sub {
  563. if (/$config{wiki_file_prune_regexp}/) {
  564. no warnings 'once';
  565. $File::Find::prune=1;
  566. use warnings "all";
  567. }
  568. elsif (! -d $_ && ! -l $_) {
  569. my ($f)=/$config{wiki_file_regexp}/; # untaint
  570. if (! defined $f) {
  571. warn("skipping bad filename $_\n");
  572. }
  573. else {
  574. $f=~s/^\Q$config{srcdir}\E\/?//;
  575. push @files, $f;
  576. $exists{pagename($f)}=1;
  577. }
  578. }
  579. },
  580. }, $config{srcdir});
  581. my %rendered;
  582. # check for added or removed pages
  583. my @add;
  584. foreach my $file (@files) {
  585. my $page=pagename($file);
  586. if (! $oldpagemtime{$page}) {
  587. debug("new page $page");
  588. push @add, $file;
  589. $links{$page}=[];
  590. $pagesources{$page}=$file;
  591. }
  592. }
  593. my @del;
  594. foreach my $page (keys %oldpagemtime) {
  595. if (! $exists{$page}) {
  596. debug("removing old page $page");
  597. push @del, $pagesources{$page};
  598. prune($config{destdir}."/".$renderedfiles{$page});
  599. delete $renderedfiles{$page};
  600. $oldpagemtime{$page}=0;
  601. delete $pagesources{$page};
  602. }
  603. }
  604. # render any updated files
  605. foreach my $file (@files) {
  606. my $page=pagename($file);
  607. if (! exists $oldpagemtime{$page} ||
  608. mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
  609. debug("rendering changed file $file");
  610. render($file);
  611. $rendered{$file}=1;
  612. }
  613. }
  614. # if any files were added or removed, check to see if each page
  615. # needs an update due to linking to them
  616. # TODO: inefficient; pages may get rendered above and again here;
  617. # problem is the bestlink may have changed and we won't know until
  618. # now
  619. if (@add || @del) {
  620. FILE: foreach my $file (@files) {
  621. my $page=pagename($file);
  622. foreach my $f (@add, @del) {
  623. my $p=pagename($f);
  624. foreach my $link (@{$links{$page}}) {
  625. if (bestlink($page, $link) eq $p) {
  626. debug("rendering $file, which links to $p");
  627. render($file);
  628. $rendered{$file}=1;
  629. next FILE;
  630. }
  631. }
  632. }
  633. }
  634. }
  635. # handle backlinks; if a page has added/removed links, update the
  636. # pages it links to
  637. # TODO: inefficient; pages may get rendered above and again here;
  638. # problem is the backlinks could be wrong in the first pass render
  639. # above
  640. if (%rendered) {
  641. my %linkchanged;
  642. foreach my $file (keys %rendered, @del) {
  643. my $page=pagename($file);
  644. if (exists $links{$page}) {
  645. foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
  646. if (length $link &&
  647. ! exists $oldlinks{$page} ||
  648. ! grep { $_ eq $link } @{$oldlinks{$page}}) {
  649. $linkchanged{$link}=1;
  650. }
  651. }
  652. }
  653. if (exists $oldlinks{$page}) {
  654. foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
  655. if (length $link &&
  656. ! exists $links{$page} ||
  657. ! grep { $_ eq $link } @{$links{$page}}) {
  658. $linkchanged{$link}=1;
  659. }
  660. }
  661. }
  662. }
  663. foreach my $link (keys %linkchanged) {
  664. my $linkfile=$pagesources{$link};
  665. if (defined $linkfile) {
  666. debug("rendering $linkfile, to update its backlinks");
  667. render($linkfile);
  668. }
  669. }
  670. }
  671. } #}}}
  672. sub gen_wrapper () { #{{{
  673. eval q{use Cwd 'abs_path'};
  674. $config{srcdir}=abs_path($config{srcdir});
  675. $config{destdir}=abs_path($config{destdir});
  676. my $this=abs_path($0);
  677. if (! -x $this) {
  678. error("$this doesn't seem to be executable");
  679. }
  680. if ($config{setup}) {
  681. error("cannot create a wrapper that uses a setup file");
  682. }
  683. my @params=($config{srcdir}, $config{destdir},
  684. "--wikiname=$config{wikiname}",
  685. "--templatedir=$config{templatedir}");
  686. push @params, "--verbose" if $config{verbose};
  687. push @params, "--rebuild" if $config{rebuild};
  688. push @params, "--nosvn" if !$config{svn};
  689. push @params, "--cgi" if $config{cgi};
  690. push @params, "--url=$config{url}" if length $config{url};
  691. push @params, "--cgiurl=$config{cgiurl}" if length $config{cgiurl};
  692. push @params, "--historyurl=$config{historyurl}" if length $config{historyurl};
  693. push @params, "--diffurl=$config{diffurl}" if length $config{diffurl};
  694. push @params, "--anonok" if $config{anonok};
  695. push @params, "--adminuser=$_" foreach @{$config{adminuser}};
  696. my $params=join(" ", @params);
  697. my $call='';
  698. foreach my $p ($this, $this, @params) {
  699. $call.=qq{"$p", };
  700. }
  701. $call.="NULL";
  702. my @envsave;
  703. push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
  704. CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
  705. HTTP_COOKIE} if $config{cgi};
  706. my $envsave="";
  707. foreach my $var (@envsave) {
  708. $envsave.=<<"EOF"
  709. if ((s=getenv("$var")))
  710. asprintf(&newenviron[i++], "%s=%s", "$var", s);
  711. EOF
  712. }
  713. open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
  714. print OUT <<"EOF";
  715. /* A wrapper for ikiwiki, can be safely made suid. */
  716. #define _GNU_SOURCE
  717. #include <stdio.h>
  718. #include <unistd.h>
  719. #include <stdlib.h>
  720. #include <string.h>
  721. extern char **environ;
  722. int main (int argc, char **argv) {
  723. /* Sanitize environment. */
  724. char *s;
  725. char *newenviron[$#envsave+3];
  726. int i=0;
  727. $envsave
  728. newenviron[i++]="HOME=$ENV{HOME}";
  729. newenviron[i]=NULL;
  730. environ=newenviron;
  731. if (argc == 2 && strcmp(argv[1], "--params") == 0) {
  732. printf("$params\\n");
  733. exit(0);
  734. }
  735. execl($call);
  736. perror("failed to run $this");
  737. exit(1);
  738. }
  739. EOF
  740. close OUT;
  741. if (system("gcc", "ikiwiki-wrap.c", "-o", possibly_foolish_untaint($config{wrapper})) != 0) {
  742. error("failed to compile ikiwiki-wrap.c");
  743. }
  744. unlink("ikiwiki-wrap.c");
  745. if (defined $config{wrappermode} &&
  746. ! chmod(oct($config{wrappermode}), possibly_foolish_untaint($config{wrapper}))) {
  747. error("chmod $config{wrapper}: $!");
  748. }
  749. print "successfully generated $config{wrapper}\n";
  750. } #}}}
  751. sub misctemplate ($$) { #{{{
  752. my $title=shift;
  753. my $pagebody=shift;
  754. my $template=HTML::Template->new(
  755. filename => "$config{templatedir}/misc.tmpl"
  756. );
  757. $template->param(
  758. title => $title,
  759. indexlink => indexlink(),
  760. wikiname => $config{wikiname},
  761. pagebody => $pagebody,
  762. );
  763. return $template->output;
  764. }#}}}
  765. sub cgi_recentchanges ($) { #{{{
  766. my $q=shift;
  767. my $template=HTML::Template->new(
  768. filename => "$config{templatedir}/recentchanges.tmpl"
  769. );
  770. $template->param(
  771. title => "RecentChanges",
  772. indexlink => indexlink(),
  773. wikiname => $config{wikiname},
  774. changelog => [rcs_recentchanges(100)],
  775. );
  776. print $q->header, $template->output;
  777. } #}}}
  778. sub userinfo_get ($$) { #{{{
  779. my $user=shift;
  780. my $field=shift;
  781. eval q{use Storable};
  782. my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  783. if (! defined $userdata || ! ref $userdata ||
  784. ! exists $userdata->{$user} || ! ref $userdata->{$user} ||
  785. ! exists $userdata->{$user}->{$field}) {
  786. return "";
  787. }
  788. return $userdata->{$user}->{$field};
  789. } #}}}
  790. sub userinfo_set ($$$) { #{{{
  791. my $user=shift;
  792. my $field=shift;
  793. my $value=shift;
  794. eval q{use Storable};
  795. my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  796. if (! defined $userdata || ! ref $userdata ||
  797. ! exists $userdata->{$user} || ! ref $userdata->{$user}) {
  798. return "";
  799. }
  800. $userdata->{$user}->{$field}=$value;
  801. my $oldmask=umask(077);
  802. my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
  803. umask($oldmask);
  804. return $ret;
  805. } #}}}
  806. sub userinfo_setall ($$) { #{{{
  807. my $user=shift;
  808. my $info=shift;
  809. eval q{use Storable};
  810. my $userdata=eval{ Storable::lock_retrieve("$config{wikistatedir}/userdb") };
  811. if (! defined $userdata || ! ref $userdata) {
  812. $userdata={};
  813. }
  814. $userdata->{$user}=$info;
  815. my $oldmask=umask(077);
  816. my $ret=Storable::lock_store($userdata, "$config{wikistatedir}/userdb");
  817. umask($oldmask);
  818. return $ret;
  819. } #}}}
  820. sub cgi_signin ($$) { #{{{
  821. my $q=shift;
  822. my $session=shift;
  823. eval q{use CGI::FormBuilder};
  824. my $form = CGI::FormBuilder->new(
  825. title => "signin",
  826. fields => [qw(do page from name password confirm_password email)],
  827. header => 1,
  828. method => 'POST',
  829. validate => {
  830. confirm_password => {
  831. perl => q{eq $form->field("password")},
  832. },
  833. email => 'EMAIL',
  834. },
  835. required => 'NONE',
  836. javascript => 0,
  837. params => $q,
  838. action => $q->request_uri,
  839. header => 0,
  840. template => (-e "$config{templatedir}/signin.tmpl" ?
  841. "$config{templatedir}/signin.tmpl" : "")
  842. );
  843. $form->field(name => "name", required => 0);
  844. $form->field(name => "do", type => "hidden");
  845. $form->field(name => "page", type => "hidden");
  846. $form->field(name => "from", type => "hidden");
  847. $form->field(name => "password", type => "password", required => 0);
  848. $form->field(name => "confirm_password", type => "password", required => 0);
  849. $form->field(name => "email", required => 0);
  850. if ($q->param("do") ne "signin") {
  851. $form->text("You need to log in first.");
  852. }
  853. if ($form->submitted) {
  854. # Set required fields based on how form was submitted.
  855. my %required=(
  856. "Login" => [qw(name password)],
  857. "Register" => [qw(name password confirm_password email)],
  858. "Mail Password" => [qw(name)],
  859. );
  860. foreach my $opt (@{$required{$form->submitted}}) {
  861. $form->field(name => $opt, required => 1);
  862. }
  863. # Validate password differently depending on how
  864. # form was submitted.
  865. if ($form->submitted eq 'Login') {
  866. $form->field(
  867. name => "password",
  868. validate => sub {
  869. length $form->field("name") &&
  870. shift eq userinfo_get($form->field("name"), 'password');
  871. },
  872. );
  873. $form->field(name => "name", validate => '/^\w+$/');
  874. }
  875. else {
  876. $form->field(name => "password", validate => 'VALUE');
  877. }
  878. # And make sure the entered name exists when logging
  879. # in or sending email, and does not when registering.
  880. if ($form->submitted eq 'Register') {
  881. $form->field(
  882. name => "name",
  883. validate => sub {
  884. my $name=shift;
  885. length $name &&
  886. ! userinfo_get($name, "regdate");
  887. },
  888. );
  889. }
  890. else {
  891. $form->field(
  892. name => "name",
  893. validate => sub {
  894. my $name=shift;
  895. length $name &&
  896. userinfo_get($name, "regdate");
  897. },
  898. );
  899. }
  900. }
  901. else {
  902. # First time settings.
  903. $form->field(name => "name", comment => "use FirstnameLastName");
  904. $form->field(name => "confirm_password", comment => "(only needed");
  905. $form->field(name => "email", comment => "for registration)");
  906. if ($session->param("name")) {
  907. $form->field(name => "name", value => $session->param("name"));
  908. }
  909. }
  910. if ($form->submitted && $form->validate) {
  911. if ($form->submitted eq 'Login') {
  912. $session->param("name", $form->field("name"));
  913. if (defined $form->field("do") &&
  914. $form->field("do") ne 'signin') {
  915. print $q->redirect(
  916. "$config{cgiurl}?do=".$form->field("do").
  917. "&page=".$form->field("page").
  918. "&from=".$form->field("from"));;
  919. }
  920. else {
  921. print $q->redirect($config{url});
  922. }
  923. }
  924. elsif ($form->submitted eq 'Register') {
  925. my $user_name=$form->field('name');
  926. if (userinfo_setall($user_name, {
  927. 'email' => $form->field('email'),
  928. 'password' => $form->field('password'),
  929. 'regdate' => time
  930. })) {
  931. $form->field(name => "confirm_password", type => "hidden");
  932. $form->field(name => "email", type => "hidden");
  933. $form->text("Registration successful. Now you can Login.");
  934. print $session->header();
  935. print misctemplate($form->title, $form->render(submit => ["Login"]));
  936. }
  937. else {
  938. error("Error saving registration.");
  939. }
  940. }
  941. elsif ($form->submitted eq 'Mail Password') {
  942. my $user_name=$form->field("name");
  943. my $template=HTML::Template->new(
  944. filename => "$config{templatedir}/passwordmail.tmpl"
  945. );
  946. $template->param(
  947. user_name => $user_name,
  948. user_password => userinfo_get($user_name, "password"),
  949. wikiurl => $config{url},
  950. wikiname => $config{wikiname},
  951. REMOTE_ADDR => $ENV{REMOTE_ADDR},
  952. );
  953. eval q{use Mail::Sendmail};
  954. my ($fromhost) = $config{cgiurl} =~ m!/([^/]+)!;
  955. sendmail(
  956. To => userinfo_get($user_name, "email"),
  957. From => "$config{wikiname} admin <".(getpwuid($>))[0]."@".$fromhost.">",
  958. Subject => "$config{wikiname} information",
  959. Message => $template->output,
  960. ) or error("Failed to send mail");
  961. $form->text("Your password has been emailed to you.");
  962. $form->field(name => "name", required => 0);
  963. print $session->header();
  964. print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
  965. }
  966. }
  967. else {
  968. print $session->header();
  969. print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
  970. }
  971. } #}}}
  972. sub is_admin ($) { #{{{
  973. my $user_name=shift;
  974. return grep { $_ eq $user_name } @{$config{adminuser}};
  975. } #}}}
  976. sub glob_match ($$) { #{{{
  977. my $page=shift;
  978. my $glob=shift;
  979. # turn glob into safe regexp
  980. $glob=quotemeta($glob);
  981. $glob=~s/\\\*/.*/g;
  982. $glob=~s/\\\?/./g;
  983. $glob=~s!\\/!/!g;
  984. $page=~/^$glob$/i;
  985. } #}}}
  986. sub globlist_match ($$) { #{{{
  987. my $page=shift;
  988. my @globlist=split(" ", shift);
  989. # check any negated globs first
  990. foreach my $glob (@globlist) {
  991. return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
  992. }
  993. foreach my $glob (@globlist) {
  994. return 1 if glob_match($page, $glob);
  995. }
  996. return 0;
  997. } #}}}
  998. sub page_locked ($$;$) { #{{{
  999. my $page=shift;
  1000. my $session=shift;
  1001. my $nonfatal=shift;
  1002. my $user=$session->param("name");
  1003. return if length $user && is_admin($user);
  1004. foreach my $admin (@{$config{adminuser}}) {
  1005. my $locked_pages=userinfo_get($admin, "locked_pages");
  1006. if (globlist_match($page, userinfo_get($admin, "locked_pages"))) {
  1007. return 1 if $nonfatal;
  1008. error(htmllink("", $page, 1)." is locked by ".
  1009. htmllink("", $admin, 1)." and cannot be edited.");
  1010. }
  1011. }
  1012. return 0;
  1013. } #}}}
  1014. sub cgi_prefs ($$) { #{{{
  1015. my $q=shift;
  1016. my $session=shift;
  1017. eval q{use CGI::FormBuilder};
  1018. my $form = CGI::FormBuilder->new(
  1019. title => "preferences",
  1020. fields => [qw(do name password confirm_password email locked_pages)],
  1021. header => 0,
  1022. method => 'POST',
  1023. validate => {
  1024. confirm_password => {
  1025. perl => q{eq $form->field("password")},
  1026. },
  1027. email => 'EMAIL',
  1028. },
  1029. required => 'NONE',
  1030. javascript => 0,
  1031. params => $q,
  1032. action => $q->request_uri,
  1033. template => (-e "$config{templatedir}/prefs.tmpl" ?
  1034. "$config{templatedir}/prefs.tmpl" : "")
  1035. );
  1036. my @buttons=("Save Preferences", "Logout", "Cancel");
  1037. my $user_name=$session->param("name");
  1038. $form->field(name => "do", type => "hidden");
  1039. $form->field(name => "name", disabled => 1,
  1040. value => $user_name, force => 1);
  1041. $form->field(name => "password", type => "password");
  1042. $form->field(name => "confirm_password", type => "password");
  1043. $form->field(name => "locked_pages", size => 50,
  1044. comment => "(".htmllink("", "GlobList", 1).")");
  1045. if (! is_admin($user_name)) {
  1046. $form->field(name => "locked_pages", type => "hidden");
  1047. }
  1048. if (! $form->submitted) {
  1049. $form->field(name => "email", force => 1,
  1050. value => userinfo_get($user_name, "email"));
  1051. $form->field(name => "locked_pages", force => 1,
  1052. value => userinfo_get($user_name, "locked_pages"));
  1053. }
  1054. if ($form->submitted eq 'Logout') {
  1055. $session->delete();
  1056. print $q->redirect($config{url});
  1057. return;
  1058. }
  1059. elsif ($form->submitted eq 'Cancel') {
  1060. print $q->redirect($config{url});
  1061. return;
  1062. }
  1063. elsif ($form->submitted eq "Save Preferences" && $form->validate) {
  1064. foreach my $field (qw(password email locked_pages)) {
  1065. if (length $form->field($field)) {
  1066. userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
  1067. }
  1068. }
  1069. $form->text("Preferences saved.");
  1070. }
  1071. print $session->header();
  1072. print misctemplate($form->title, $form->render(submit => \@buttons));
  1073. } #}}}
  1074. sub cgi_editpage ($$) { #{{{
  1075. my $q=shift;
  1076. my $session=shift;
  1077. eval q{use CGI::FormBuilder};
  1078. my $form = CGI::FormBuilder->new(
  1079. fields => [qw(do rcsinfo from page content comments)],
  1080. header => 1,
  1081. method => 'POST',
  1082. validate => {
  1083. content => '/.+/',
  1084. },
  1085. required => [qw{content}],
  1086. javascript => 0,
  1087. params => $q,
  1088. action => $q->request_uri,
  1089. table => 0,
  1090. template => "$config{templatedir}/editpage.tmpl"
  1091. );
  1092. my @buttons=("Save Page", "Preview", "Cancel");
  1093. my ($page)=$form->param('page')=~/$config{wiki_file_regexp}/;
  1094. if (! defined $page || ! length $page || $page ne $q->param('page') ||
  1095. $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
  1096. error("bad page name");
  1097. }
  1098. $page=lc($page);
  1099. my $file=$page.$config{default_pageext};
  1100. my $newfile=1;
  1101. if (exists $pagesources{lc($page)}) {
  1102. $file=$pagesources{lc($page)};
  1103. $newfile=0;
  1104. }
  1105. $form->field(name => "do", type => 'hidden');
  1106. $form->field(name => "from", type => 'hidden');
  1107. $form->field(name => "rcsinfo", type => 'hidden');
  1108. $form->field(name => "page", value => "$page", force => 1);
  1109. $form->field(name => "comments", type => "text", size => 80);
  1110. $form->field(name => "content", type => "textarea", rows => 20,
  1111. cols => 80);
  1112. $form->tmpl_param("can_commit", $config{svn});
  1113. $form->tmpl_param("indexlink", indexlink());
  1114. $form->tmpl_param("helponformattinglink",
  1115. htmllink("", "HelpOnFormatting", 1));
  1116. if (! $form->submitted) {
  1117. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  1118. force => 1);
  1119. }
  1120. if ($form->submitted eq "Cancel") {
  1121. print $q->redirect("$config{url}/".htmlpage($page));
  1122. return;
  1123. }
  1124. elsif ($form->submitted eq "Preview") {
  1125. $form->tmpl_param("page_preview",
  1126. htmlize($config{default_pageext},
  1127. linkify($form->field('content'), $page)));
  1128. }
  1129. else {
  1130. $form->tmpl_param("page_preview", "");
  1131. }
  1132. $form->tmpl_param("page_conflict", "");
  1133. if (! $form->submitted || $form->submitted eq "Preview" ||
  1134. ! $form->validate) {
  1135. if ($form->field("do") eq "create") {
  1136. if (exists $pagesources{lc($page)}) {
  1137. # hmm, someone else made the page in the
  1138. # meantime?
  1139. print $q->redirect("$config{url}/".htmlpage($page));
  1140. return;
  1141. }
  1142. my @page_locs;
  1143. my $best_loc;
  1144. my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/;
  1145. if (! defined $from || ! length $from ||
  1146. $from ne $form->param('from') ||
  1147. $from=~/$config{wiki_file_prune_regexp}/ || $from=~/^\//) {
  1148. @page_locs=$best_loc=$page;
  1149. }
  1150. else {
  1151. my $dir=$from."/";
  1152. $dir=~s![^/]+/$!!;
  1153. if ($page eq 'discussion') {
  1154. $best_loc="$from/$page";
  1155. }
  1156. else {
  1157. $best_loc=$dir.$page;
  1158. }
  1159. push @page_locs, $dir.$page;
  1160. push @page_locs, "$from/$page";
  1161. while (length $dir) {
  1162. $dir=~s![^/]+/$!!;
  1163. push @page_locs, $dir.$page;
  1164. }
  1165. @page_locs = grep {
  1166. ! exists $pagesources{lc($_)} &&
  1167. ! page_locked($_, $session, 1)
  1168. } @page_locs;
  1169. }
  1170. $form->tmpl_param("page_select", 1);
  1171. $form->field(name => "page", type => 'select',
  1172. options => \@page_locs, value => $best_loc);
  1173. $form->title("creating $page");
  1174. }
  1175. elsif ($form->field("do") eq "edit") {
  1176. page_locked($page, $session);
  1177. if (! defined $form->field('content') ||
  1178. ! length $form->field('content')) {
  1179. my $content="";
  1180. if (exists $pagesources{lc($page)}) {
  1181. $content=readfile("$config{srcdir}/$pagesources{lc($page)}");
  1182. $content=~s/\n/\r\n/g;
  1183. }
  1184. $form->field(name => "content", value => $content,
  1185. force => 1);
  1186. }
  1187. $form->tmpl_param("page_select", 0);
  1188. $form->field(name => "page", type => 'hidden');
  1189. $form->title("editing $page");
  1190. }
  1191. print $form->render(submit => \@buttons);
  1192. }
  1193. else {
  1194. # save page
  1195. page_locked($page, $session);
  1196. my $content=$form->field('content');
  1197. $content=~s/\r\n/\n/g;
  1198. $content=~s/\r/\n/g;
  1199. writefile("$config{srcdir}/$file", $content);
  1200. my $message="web commit ";
  1201. if (length $session->param("name")) {
  1202. $message.="by ".$session->param("name");
  1203. }
  1204. else {
  1205. $message.="from $ENV{REMOTE_ADDR}";
  1206. }
  1207. if (defined $form->field('comments') &&
  1208. length $form->field('comments')) {
  1209. $message.=": ".$form->field('comments');
  1210. }
  1211. if ($config{svn}) {
  1212. if ($newfile) {
  1213. rcs_add($file);
  1214. }
  1215. # prevent deadlock with post-commit hook
  1216. unlockwiki();
  1217. # presumably the commit will trigger an update
  1218. # of the wiki
  1219. my $conflict=rcs_commit($file, $message,
  1220. $form->field("rcsinfo"));
  1221. if (defined $conflict) {
  1222. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  1223. force => 1);
  1224. $form->tmpl_param("page_conflict", 1);
  1225. $form->field("content", value => $conflict, force => 1);
  1226. $form->field("do", "edit)");
  1227. $form->tmpl_param("page_select", 0);
  1228. $form->field(name => "page", type => 'hidden');
  1229. $form->title("editing $page");
  1230. print $form->render(submit => \@buttons);
  1231. return;
  1232. }
  1233. }
  1234. else {
  1235. loadindex();
  1236. refresh();
  1237. saveindex();
  1238. }
  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");
  1242. }
  1243. } #}}}
  1244. sub cgi () { #{{{
  1245. eval q{use CGI};
  1246. eval q{use CGI::Session};
  1247. my $q=CGI->new;
  1248. my $do=$q->param('do');
  1249. if (! defined $do || ! length $do) {
  1250. error("\"do\" parameter missing");
  1251. }
  1252. # This does not need a session.
  1253. if ($do eq 'recentchanges') {
  1254. cgi_recentchanges($q);
  1255. return;
  1256. }
  1257. CGI::Session->name("ikiwiki_session");
  1258. my $oldmask=umask(077);
  1259. my $session = CGI::Session->new("driver:db_file", $q,
  1260. { FileName => "$config{wikistatedir}/sessions.db" });
  1261. umask($oldmask);
  1262. # Everything below this point needs the user to be signed in.
  1263. if ((! $config{anonok} && ! defined $session->param("name") ||
  1264. ! defined $session->param("name") ||
  1265. ! userinfo_get($session->param("name"), "regdate")) || $do eq 'signin') {
  1266. cgi_signin($q, $session);
  1267. # Force session flush with safe umask.
  1268. my $oldmask=umask(077);
  1269. $session->flush;
  1270. umask($oldmask);
  1271. return;
  1272. }
  1273. if ($do eq 'create' || $do eq 'edit') {
  1274. cgi_editpage($q, $session);
  1275. }
  1276. elsif ($do eq 'prefs') {
  1277. cgi_prefs($q, $session);
  1278. }
  1279. else {
  1280. error("unknown do parameter");
  1281. }
  1282. } #}}}
  1283. sub setup () { # {{{
  1284. my $setup=possibly_foolish_untaint($config{setup});
  1285. delete $config{setup};
  1286. open (IN, $setup) || error("read $setup: $!\n");
  1287. local $/=undef;
  1288. my $code=<IN>;
  1289. ($code)=$code=~/(.*)/s;
  1290. close IN;
  1291. eval $code;
  1292. error($@) if $@;
  1293. exit;
  1294. } #}}}
  1295. # main {{{
  1296. setup() if $config{setup};
  1297. lockwiki();
  1298. if ($config{wrapper}) {
  1299. gen_wrapper();
  1300. exit;
  1301. }
  1302. memoize('pagename');
  1303. memoize('bestlink');
  1304. loadindex() unless $config{rebuild};
  1305. if ($config{cgi}) {
  1306. cgi();
  1307. }
  1308. else {
  1309. rcs_update() if $config{svn};
  1310. refresh();
  1311. saveindex();
  1312. }
  1313. #}}}