summaryrefslogtreecommitdiff
path: root/ikiwiki
blob: 44e2197e67bee1889f81ad69463945f3847823ea (plain)
  1. #!/usr/bin/perl -T
  2. use warnings;
  3. use strict;
  4. use File::Find;
  5. use Memoize;
  6. use File::Spec;
  7. use HTML::Template;
  8. BEGIN {
  9. $blosxom::version="is a proper perl module too much to ask?";
  10. do "/usr/bin/markdown";
  11. }
  12. $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
  13. my ($srcdir, $templatedir, $destdir, %links, %oldlinks, %oldpagemtime,
  14. %renderedfiles, %pagesources);
  15. my $wiki_link_regexp=qr/\[\[([^\s]+)\]\]/;
  16. my $wiki_file_regexp=qr/(^[-A-Za-z0-9_.:\/+]+$)/;
  17. my $wiki_file_prune_regexp=qr!((^|/).svn/|\.\.|^\.|\/\.|\.html?$)!;
  18. my $verbose=0;
  19. my $wikiname="wiki";
  20. my $default_pagetype=".mdwn";
  21. my $cgi=0;
  22. my $url="";
  23. my $cgiurl="";
  24. my $historyurl="";
  25. my $svn=1;
  26. my $anonok=0;
  27. sub usage { #{{{
  28. die "usage: ikiwiki [options] source templates dest\n";
  29. } #}}}
  30. sub error ($) { #{{{
  31. if ($cgi) {
  32. print "Content-type: text/html\n\n";
  33. print misctemplate("Error", "<p>Error: @_</p>");
  34. exit 1;
  35. }
  36. else {
  37. die @_;
  38. }
  39. } #}}}
  40. sub debug ($) { #{{{
  41. if (! $cgi) {
  42. print "@_\n" if $verbose;
  43. }
  44. else {
  45. print STDERR "@_\n" if $verbose;
  46. }
  47. } #}}}
  48. sub mtime ($) { #{{{
  49. my $page=shift;
  50. return (stat($page))[9];
  51. } #}}}
  52. sub possibly_foolish_untaint ($) { #{{{
  53. my $tainted=shift;
  54. my ($untainted)=$tainted=~/(.*)/;
  55. return $untainted;
  56. } #}}}
  57. sub basename ($) { #{{{
  58. my $file=shift;
  59. $file=~s!.*/!!;
  60. return $file;
  61. } #}}}
  62. sub dirname ($) { #{{{
  63. my $file=shift;
  64. $file=~s!/?[^/]+$!!;
  65. return $file;
  66. } #}}}
  67. sub pagetype ($) { #{{{
  68. my $page=shift;
  69. if ($page =~ /\.mdwn$/) {
  70. return ".mdwn";
  71. }
  72. else {
  73. return "unknown";
  74. }
  75. } #}}}
  76. sub pagename ($) { #{{{
  77. my $file=shift;
  78. my $type=pagetype($file);
  79. my $page=$file;
  80. $page=~s/\Q$type\E*$// unless $type eq 'unknown';
  81. return $page;
  82. } #}}}
  83. sub htmlpage ($) { #{{{
  84. my $page=shift;
  85. return $page.".html";
  86. } #}}}
  87. sub readfile ($) { #{{{
  88. my $file=shift;
  89. local $/=undef;
  90. open (IN, "$file") || error("failed to read $file: $!");
  91. my $ret=<IN>;
  92. close IN;
  93. return $ret;
  94. } #}}}
  95. sub writefile ($$) { #{{{
  96. my $file=shift;
  97. my $content=shift;
  98. my $dir=dirname($file);
  99. if (! -d $dir) {
  100. my $d="";
  101. foreach my $s (split(m!/+!, $dir)) {
  102. $d.="$s/";
  103. if (! -d $d) {
  104. mkdir($d) || error("failed to create directory $d: $!");
  105. }
  106. }
  107. }
  108. open (OUT, ">$file") || error("failed to write $file: $!");
  109. print OUT $content;
  110. close OUT;
  111. } #}}}
  112. sub findlinks ($) { #{{{
  113. my $content=shift;
  114. my @links;
  115. while ($content =~ /$wiki_link_regexp/g) {
  116. push @links, lc($1);
  117. }
  118. return @links;
  119. } #}}}
  120. # Given a page and the text of a link on the page, determine which existing
  121. # page that link best points to. Prefers pages under a subdirectory with
  122. # the same name as the source page, failing that goes down the directory tree
  123. # to the base looking for matching pages.
  124. sub bestlink ($$) { #{{{
  125. my $page=shift;
  126. my $link=lc(shift);
  127. my $cwd=$page;
  128. do {
  129. my $l=$cwd;
  130. $l.="/" if length $l;
  131. $l.=$link;
  132. if (exists $links{$l}) {
  133. #debug("for $page, \"$link\", use $l");
  134. return $l;
  135. }
  136. } while $cwd=~s!/?[^/]+$!!;
  137. #print STDERR "warning: page $page, broken link: $link\n";
  138. return "";
  139. } #}}}
  140. sub isinlinableimage ($) { #{{{
  141. my $file=shift;
  142. $file=~/\.(png|gif|jpg|jpeg)$/;
  143. } #}}}
  144. sub htmllink { #{{{
  145. my $page=shift;
  146. my $link=shift;
  147. my $noimagelink=shift;
  148. my $bestlink=bestlink($page, $link);
  149. return $link if $page eq $bestlink;
  150. # TODO BUG: %renderedfiles may not have it, if the linked to page
  151. # was also added and isn't yet rendered! Note that this bug is
  152. # masked by the bug mentioned below that makes all new files
  153. # be rendered twice.
  154. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  155. $bestlink=htmlpage($bestlink);
  156. }
  157. if (! grep { $_ eq $bestlink } values %renderedfiles) {
  158. return "<a href=\"$cgiurl?do=create&page=$link&from=$page\">?</a>$link"
  159. }
  160. $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
  161. if (! $noimagelink && isinlinableimage($bestlink)) {
  162. return "<img src=\"$bestlink\">";
  163. }
  164. return "<a href=\"$bestlink\">$link</a>";
  165. } #}}}
  166. sub linkify ($$) { #{{{
  167. my $content=shift;
  168. my $file=shift;
  169. $content =~ s/$wiki_link_regexp/htmllink(pagename($file), $1)/eg;
  170. return $content;
  171. } #}}}
  172. sub htmlize ($$) { #{{{
  173. my $type=shift;
  174. my $content=shift;
  175. if ($type eq '.mdwn') {
  176. return Markdown::Markdown($content);
  177. }
  178. else {
  179. error("htmlization of $type not supported");
  180. }
  181. } #}}}
  182. sub backlinks ($) { #{{{
  183. my $page=shift;
  184. my @links;
  185. foreach my $p (keys %links) {
  186. next if bestlink($page, $p) eq $page;
  187. if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
  188. my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
  189. # Trim common dir prefixes from both pages.
  190. my $p_trimmed=$p;
  191. my $page_trimmed=$page;
  192. my $dir;
  193. 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
  194. defined $dir &&
  195. $p_trimmed=~s/^\Q$dir\E// &&
  196. $page_trimmed=~s/^\Q$dir\E//;
  197. push @links, { url => $href, page => $p_trimmed };
  198. }
  199. }
  200. return sort { $a->{page} cmp $b->{page} } @links;
  201. } #}}}
  202. sub parentlinks ($) { #{{{
  203. my $page=shift;
  204. my @ret;
  205. my $pagelink="";
  206. my $path="";
  207. my $skip=1;
  208. foreach my $dir (reverse split("/", $page)) {
  209. if (! $skip) {
  210. unshift @ret, { url => "$path$dir.html", page => $dir };
  211. }
  212. else {
  213. $skip=0;
  214. }
  215. $path.="../";
  216. }
  217. unshift @ret, { url => $path , page => $wikiname };
  218. return @ret;
  219. } #}}}
  220. sub indexlink () { #{{{
  221. return "<a href=\"$url\">$wikiname</a>";
  222. } #}}}
  223. sub finalize ($$) { #{{{
  224. my $content=shift;
  225. my $page=shift;
  226. my $title=basename($page);
  227. $title=~s/_/ /g;
  228. my $template=HTML::Template->new(blind_cache => 1,
  229. filename => "$templatedir/page.tmpl");
  230. if (length $cgiurl) {
  231. $template->param(editurl => "$cgiurl?do=edit&page=$page");
  232. if ($svn) {
  233. $template->param(recentchangesurl => "$cgiurl?do=recentchanges");
  234. }
  235. }
  236. if (length $historyurl) {
  237. my $u=$historyurl;
  238. $u=~s/\[\[\]\]/$pagesources{$page}/g;
  239. $template->param(historyurl => $u);
  240. }
  241. $template->param(
  242. title => $title,
  243. wikiname => $wikiname,
  244. parentlinks => [parentlinks($page)],
  245. content => $content,
  246. backlinks => [backlinks($page)],
  247. );
  248. return $template->output;
  249. } #}}}
  250. # Important security check. Make sure to call this before saving any files
  251. # to the source directory.
  252. sub check_overwrite ($$) { #{{{
  253. my $dest=shift;
  254. my $src=shift;
  255. if (! exists $renderedfiles{$src} && -e $dest) {
  256. error("$dest exists and was not rendered from $src before, not overwriting");
  257. }
  258. } #}}}
  259. sub render ($) { #{{{
  260. my $file=shift;
  261. my $type=pagetype($file);
  262. my $content=readfile("$srcdir/$file");
  263. if ($type ne 'unknown') {
  264. my $page=pagename($file);
  265. $links{$page}=[findlinks($content)];
  266. $content=linkify($content, $file);
  267. $content=htmlize($type, $content);
  268. $content=finalize($content, $page);
  269. check_overwrite("$destdir/".htmlpage($page), $page);
  270. writefile("$destdir/".htmlpage($page), $content);
  271. $oldpagemtime{$page}=time;
  272. $renderedfiles{$page}=htmlpage($page);
  273. }
  274. else {
  275. $links{$file}=[];
  276. check_overwrite("$destdir/$file", $file);
  277. writefile("$destdir/$file", $content);
  278. $oldpagemtime{$file}=time;
  279. $renderedfiles{$file}=$file;
  280. }
  281. } #}}}
  282. sub loadindex () { #{{{
  283. open (IN, "$srcdir/.ikiwiki/index") || return;
  284. while (<IN>) {
  285. $_=possibly_foolish_untaint($_);
  286. chomp;
  287. my ($mtime, $file, $rendered, @links)=split(' ', $_);
  288. my $page=pagename($file);
  289. $pagesources{$page}=$file;
  290. $oldpagemtime{$page}=$mtime;
  291. $oldlinks{$page}=[@links];
  292. $links{$page}=[@links];
  293. $renderedfiles{$page}=$rendered;
  294. }
  295. close IN;
  296. } #}}}
  297. sub saveindex () { #{{{
  298. if (! -d "$srcdir/.ikiwiki") {
  299. mkdir("$srcdir/.ikiwiki");
  300. }
  301. open (OUT, ">$srcdir/.ikiwiki/index") || error("cannot write to index: $!");
  302. foreach my $page (keys %oldpagemtime) {
  303. print OUT "$oldpagemtime{$page} $pagesources{$page} $renderedfiles{$page} ".
  304. join(" ", @{$links{$page}})."\n"
  305. if $oldpagemtime{$page};
  306. }
  307. close OUT;
  308. } #}}}
  309. sub rcs_update () { #{{{
  310. if (-d "$srcdir/.svn") {
  311. if (system("svn", "update", "--quiet", $srcdir) != 0) {
  312. warn("svn update failed\n");
  313. }
  314. }
  315. } #}}}
  316. sub rcs_commit ($) { #{{{
  317. my $message=shift;
  318. if (-d "$srcdir/.svn") {
  319. if (system("svn", "commit", "--quiet", "-m",
  320. possibly_foolish_untaint($message), $srcdir) != 0) {
  321. warn("svn commit failed\n");
  322. }
  323. }
  324. } #}}}
  325. sub rcs_add ($) { #{{{
  326. my $file=shift;
  327. if (-d "$srcdir/.svn") {
  328. my $parent=dirname($file);
  329. while (! -d "$srcdir/$parent/.svn") {
  330. $file=$parent;
  331. $parent=dirname($file);
  332. }
  333. if (system("svn", "add", "--quiet", "$srcdir/$file") != 0) {
  334. warn("svn add failed\n");
  335. }
  336. }
  337. } #}}}
  338. sub rcs_recentchanges ($) { #{{{
  339. my $num=shift;
  340. my @ret;
  341. eval q{use Date::Parse};
  342. eval q{use Time::Duration};
  343. if (-d "$srcdir/.svn") {
  344. my $info=`LANG=C svn info $srcdir`;
  345. my ($svn_url)=$info=~/^URL: (.*)$/m;
  346. # FIXME: currently assumes that the wiki is somewhere
  347. # under trunk in svn, doesn't support other layouts.
  348. my ($svn_base)=$svn_url=~m!(/trunk(?:/.*)?)$!;
  349. my $div=qr/^--------------------+$/;
  350. my $infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
  351. my $state='start';
  352. my ($rev, $user, $when, @pages, @message);
  353. foreach (`LANG=C svn log -v '$svn_url'`) {
  354. chomp;
  355. if ($state eq 'start' && /$div/) {
  356. $state='header';
  357. }
  358. elsif ($state eq 'header' && /$infoline/) {
  359. $rev=$1;
  360. $user=$2;
  361. $when=concise(ago(time - str2time($3)));
  362. }
  363. elsif ($state eq 'header' && /^\s+[A-Z]\s+\Q$svn_base\E\/(.+)$/) {
  364. push @pages, { link => htmllink("", pagename($1), 1) }
  365. if length $1;
  366. }
  367. elsif ($state eq 'header' && /^$/) {
  368. $state='body';
  369. }
  370. elsif ($state eq 'body' && /$div/) {
  371. push @ret, { rev => $rev, user => $user,
  372. when => $when, message => [@message],
  373. pages => [@pages] } if @pages;
  374. return @ret if @ret >= $num;
  375. $state='header';
  376. $rev=$user=$when=undef;
  377. @pages=@message=();
  378. }
  379. elsif ($state eq 'body') {
  380. push @message, {line => $_},
  381. }
  382. }
  383. }
  384. return @ret;
  385. } #}}}
  386. sub prune ($) { #{{{
  387. my $file=shift;
  388. unlink($file);
  389. my $dir=dirname($file);
  390. while (rmdir($dir)) {
  391. $dir=dirname($dir);
  392. }
  393. } #}}}
  394. sub refresh () { #{{{
  395. # Find existing pages.
  396. my %exists;
  397. my @files;
  398. find({
  399. no_chdir => 1,
  400. wanted => sub {
  401. if (/$wiki_file_prune_regexp/) {
  402. $File::Find::prune=1;
  403. }
  404. elsif (! -d $_) {
  405. my ($f)=/$wiki_file_regexp/; # untaint
  406. if (! defined $f) {
  407. warn("skipping bad filename $_\n");
  408. }
  409. else {
  410. $f=~s/^\Q$srcdir\E\/?//;
  411. push @files, $f;
  412. $exists{pagename($f)}=1;
  413. }
  414. }
  415. },
  416. }, $srcdir);
  417. my %rendered;
  418. # check for added or removed pages
  419. my @add;
  420. foreach my $file (@files) {
  421. my $page=pagename($file);
  422. if (! $oldpagemtime{$page}) {
  423. debug("new page $page");
  424. push @add, $file;
  425. $links{$page}=[];
  426. $pagesources{$page}=$file;
  427. }
  428. }
  429. my @del;
  430. foreach my $page (keys %oldpagemtime) {
  431. if (! $exists{$page}) {
  432. debug("removing old page $page");
  433. push @del, $renderedfiles{$page};
  434. prune($destdir."/".$renderedfiles{$page});
  435. delete $renderedfiles{$page};
  436. $oldpagemtime{$page}=0;
  437. delete $pagesources{$page};
  438. }
  439. }
  440. # render any updated files
  441. foreach my $file (@files) {
  442. my $page=pagename($file);
  443. if (! exists $oldpagemtime{$page} ||
  444. mtime("$srcdir/$file") > $oldpagemtime{$page}) {
  445. debug("rendering changed file $file");
  446. render($file);
  447. $rendered{$file}=1;
  448. }
  449. }
  450. # if any files were added or removed, check to see if each page
  451. # needs an update due to linking to them
  452. # TODO: inefficient; pages may get rendered above and again here;
  453. # problem is the bestlink may have changed and we won't know until
  454. # now
  455. if (@add || @del) {
  456. FILE: foreach my $file (@files) {
  457. my $page=pagename($file);
  458. foreach my $f (@add, @del) {
  459. my $p=pagename($f);
  460. foreach my $link (@{$links{$page}}) {
  461. if (bestlink($page, $link) eq $p) {
  462. debug("rendering $file, which links to $p");
  463. render($file);
  464. $rendered{$file}=1;
  465. next FILE;
  466. }
  467. }
  468. }
  469. }
  470. }
  471. # handle backlinks; if a page has added/removed links, update the
  472. # pages it links to
  473. # TODO: inefficient; pages may get rendered above and again here;
  474. # problem is the backlinks could be wrong in the first pass render
  475. # above
  476. if (%rendered) {
  477. my %linkchanged;
  478. foreach my $file (keys %rendered, @del) {
  479. my $page=pagename($file);
  480. if (exists $links{$page}) {
  481. foreach my $link (@{$links{$page}}) {
  482. $link=bestlink($page, $link);
  483. if (length $link &&
  484. ! exists $oldlinks{$page} ||
  485. ! grep { $_ eq $link } @{$oldlinks{$page}}) {
  486. $linkchanged{$link}=1;
  487. }
  488. }
  489. }
  490. if (exists $oldlinks{$page}) {
  491. foreach my $link (@{$oldlinks{$page}}) {
  492. $link=bestlink($page, $link);
  493. if (length $link &&
  494. ! exists $links{$page} ||
  495. ! grep { $_ eq $link } @{$links{$page}}) {
  496. $linkchanged{$link}=1;
  497. }
  498. }
  499. }
  500. }
  501. foreach my $link (keys %linkchanged) {
  502. my $linkfile=$pagesources{$link};
  503. if (defined $linkfile) {
  504. debug("rendering $linkfile, to update its backlinks");
  505. render($linkfile);
  506. }
  507. }
  508. }
  509. } #}}}
  510. # Generates a C wrapper program for running ikiwiki in a specific way.
  511. # The wrapper may be safely made suid.
  512. sub gen_wrapper ($$) { #{{{
  513. my ($svn, $rebuild)=@_;
  514. eval q{use Cwd 'abs_path'};
  515. $srcdir=abs_path($srcdir);
  516. $destdir=abs_path($destdir);
  517. my $this=abs_path($0);
  518. if (! -x $this) {
  519. error("$this doesn't seem to be executable");
  520. }
  521. my @params=($srcdir, $templatedir, $destdir, "--wikiname=$wikiname");
  522. push @params, "--verbose" if $verbose;
  523. push @params, "--rebuild" if $rebuild;
  524. push @params, "--nosvn" if !$svn;
  525. push @params, "--cgi" if $cgi;
  526. push @params, "--url=$url" if $url;
  527. push @params, "--cgiurl=$cgiurl" if $cgiurl;
  528. push @params, "--historyurl=$historyurl" if $historyurl;
  529. push @params, "--anonok" if $anonok;
  530. my $params=join(" ", @params);
  531. my $call='';
  532. foreach my $p ($this, $this, @params) {
  533. $call.=qq{"$p", };
  534. }
  535. $call.="NULL";
  536. my @envsave;
  537. push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
  538. CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
  539. HTTP_COOKIE} if $cgi;
  540. my $envsave="";
  541. foreach my $var (@envsave) {
  542. $envsave.=<<"EOF"
  543. if ((s=getenv("$var")))
  544. asprintf(&newenviron[i++], "%s=%s", "$var", s);
  545. EOF
  546. }
  547. open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
  548. print OUT <<"EOF";
  549. /* A wrapper for ikiwiki, can be safely made suid. */
  550. #define _GNU_SOURCE
  551. #include <stdio.h>
  552. #include <unistd.h>
  553. #include <stdlib.h>
  554. #include <string.h>
  555. extern char **environ;
  556. int main (int argc, char **argv) {
  557. /* Sanitize environment. */
  558. char *s;
  559. char *newenviron[$#envsave+3];
  560. int i=0;
  561. $envsave
  562. newenviron[i++]="HOME=$ENV{HOME}";
  563. newenviron[i]=NULL;
  564. environ=newenviron;
  565. if (argc == 2 && strcmp(argv[1], "--params") == 0) {
  566. printf("$params\\n");
  567. exit(0);
  568. }
  569. execl($call);
  570. perror("failed to run $this");
  571. exit(1);
  572. }
  573. EOF
  574. close OUT;
  575. if (system("gcc", "ikiwiki-wrap.c", "-o", "ikiwiki-wrap") != 0) {
  576. error("failed to compile ikiwiki-wrap.c");
  577. }
  578. unlink("ikiwiki-wrap.c");
  579. print "successfully generated ikiwiki-wrap\n";
  580. exit 0;
  581. } #}}}
  582. sub misctemplate ($$) { #{{{
  583. my $title=shift;
  584. my $pagebody=shift;
  585. my $template=HTML::Template->new(
  586. filename => "$templatedir/misc.tmpl"
  587. );
  588. $template->param(
  589. title => $title,
  590. indexlink => indexlink(),
  591. wikiname => $wikiname,
  592. pagebody => $pagebody,
  593. );
  594. return $template->output;
  595. }#}}}
  596. sub cgi_recentchanges ($) { #{{{
  597. my $q=shift;
  598. my $template=HTML::Template->new(
  599. filename => "$templatedir/recentchanges.tmpl"
  600. );
  601. $template->param(
  602. title => "RecentChanges",
  603. indexlink => indexlink(),
  604. wikiname => $wikiname,
  605. changelog => [rcs_recentchanges(100)],
  606. );
  607. print $q->header, $template->output;
  608. } #}}}
  609. sub cgi_signin ($$) { #{{{
  610. my $q=shift;
  611. my $session=shift;
  612. eval q{use CGI::FormBuilder};
  613. my $form = CGI::FormBuilder->new(
  614. title => "$wikiname signin",
  615. fields => [qw(do page name password confirm_password email)],
  616. header => 1,
  617. method => 'POST',
  618. validate => {
  619. name => '/^\w+$/',
  620. confirm_password => {
  621. perl => q{eq $form->field("password")},
  622. },
  623. email => 'EMAIL',
  624. },
  625. required => 'NONE',
  626. javascript => 0,
  627. params => $q,
  628. action => $q->request_uri,
  629. header => 0,
  630. template => (-e "$templatedir/signin.tmpl" ? "$templatedir/signin.tmpl" : "")
  631. );
  632. $form->field(name => "name", required => 0);
  633. $form->field(name => "do", type => "hidden");
  634. $form->field(name => "page", type => "hidden");
  635. $form->field(name => "password", type => "password", required => 0);
  636. $form->field(name => "confirm_password", type => "password", required => 0);
  637. $form->field(name => "email", required => 0);
  638. if ($session->param("name")) {
  639. $form->field(name => "name", value => $session->param("name"));
  640. }
  641. if ($q->param("do") ne "signin") {
  642. $form->text("You need to log in before you can edit pages.");
  643. }
  644. if ($form->submitted) {
  645. # Set required fields based on how form was submitted.
  646. my %required=(
  647. "Login" => [qw(name password)],
  648. "Register" => [qw(name password confirm_password email)],
  649. "Mail Password" => [qw(name)],
  650. );
  651. foreach my $opt (@{$required{$form->submitted}}) {
  652. $form->field(name => $opt, required => 1);
  653. }
  654. # Validate password differently depending on how form was
  655. # submitted.
  656. if ($form->submitted eq 'Login') {
  657. $form->field(
  658. name => "password",
  659. validate => sub {
  660. # TODO get real user password
  661. shift eq "foo";
  662. },
  663. );
  664. }
  665. else {
  666. $form->field(name => "password", validate => 'VALUE');
  667. }
  668. }
  669. else {
  670. # Comments only shown first time.
  671. $form->field(name => "name", comment => "use FirstnameLastName");
  672. $form->field(name => "confirm_password", comment => "(only needed");
  673. $form->field(name => "email", comment => "for registration)");
  674. }
  675. if ($form->submitted && $form->validate) {
  676. if ($form->submitted eq 'Login') {
  677. $session->param("name", $form->field("name"));
  678. if (defined $form->field("do") &&
  679. $form->field("do") ne 'signin') {
  680. print $q->redirect(
  681. "$cgiurl?do=".$form->field("do").
  682. "&page=".$form->field("page"));
  683. }
  684. else {
  685. print $q->redirect($url);
  686. }
  687. }
  688. elsif ($form->submitted eq 'Register') {
  689. # TODO: save registration info
  690. $form->field(name => "confirm_password", type => "hidden");
  691. $form->field(name => "email", type => "hidden");
  692. $form->text("Registration successful. Now you can Login.");
  693. print $session->header();
  694. print misctemplate($form->title, $form->render(submit => ["Login"]));
  695. }
  696. elsif ($form->submitted eq 'Mail Password') {
  697. # TODO mail password
  698. $form->text("Your password has been emailed to you.");
  699. print $session->header();
  700. print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
  701. }
  702. }
  703. else {
  704. print $session->header();
  705. print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
  706. }
  707. } #}}}
  708. sub cgi_editpage ($$) { #{{{
  709. my $q=shift;
  710. my $session=shift;
  711. eval q{use CGI::FormBuilder};
  712. my $form = CGI::FormBuilder->new(
  713. fields => [qw(do from page content comments)],
  714. header => 1,
  715. method => 'POST',
  716. validate => {},
  717. required => [qw{}],
  718. javascript => 0,
  719. params => $q,
  720. action => $q->request_uri,
  721. table => 0,
  722. template => "$templatedir/editpage.tmpl"
  723. );
  724. my ($page)=$form->param('page')=~/$wiki_file_regexp/;
  725. if (! defined $page || ! length $page || $page ne $q->param('page') ||
  726. $page=~/$wiki_file_prune_regexp/ || $page=~/^\//) {
  727. error("bad page name");
  728. }
  729. $page=lc($page);
  730. $form->field(name => "do", type => 'hidden');
  731. $form->field(name => "from", type => 'hidden');
  732. $form->field(name => "page", value => "$page", force => 1);
  733. $form->field(name => "comments", type => "text", size => 80);
  734. $form->field(name => "content", type => "textarea", rows => 20,
  735. cols => 80);
  736. if (! $form->submitted || ! $form->validate) {
  737. if ($form->field("do") eq "create") {
  738. if (exists $pagesources{lc($page)}) {
  739. # hmm, someone else made the page in the
  740. # meantime?
  741. print $q->redirect("$url/".htmlpage($page));
  742. return;
  743. }
  744. my @page_locs;
  745. my ($from)=$form->param('from')=~/$wiki_file_regexp/;
  746. if (! defined $from || ! length $from ||
  747. $from ne $form->param('from') ||
  748. $from=~/$wiki_file_prune_regexp/ || $from=~/^\//) {
  749. @page_locs=$page;
  750. }
  751. else {
  752. my $dir=$from."/";
  753. $dir=~s![^/]+/$!!;
  754. push @page_locs, $dir.$page;
  755. push @page_locs, "$from/$page";
  756. while (length $dir) {
  757. $dir=~s![^/]+/$!!;
  758. push @page_locs, $dir.$page;
  759. }
  760. }
  761. $form->tmpl_param("page_select", 1);
  762. $form->field(name => "page", type => 'select',
  763. options => \@page_locs);
  764. $form->title("creating $page");
  765. }
  766. elsif ($form->field("do") eq "edit") {
  767. my $content="";
  768. if (exists $pagesources{lc($page)}) {
  769. $content=readfile("$srcdir/$pagesources{lc($page)}");
  770. $content=~s/\n/\r\n/g;
  771. }
  772. $form->tmpl_param("page_select", 0);
  773. $form->field(name => "content", value => $content,
  774. force => 1);
  775. $form->field(name => "page", type => 'hidden');
  776. $form->title("editing $page");
  777. }
  778. $form->tmpl_param("can_commit", $svn);
  779. $form->tmpl_param("indexlink", indexlink());
  780. print $form->render(submit => ["Save Page"]);
  781. }
  782. else {
  783. # save page
  784. my $file=$page.$default_pagetype;
  785. my $newfile=1;
  786. if (exists $pagesources{lc($page)}) {
  787. $file=$pagesources{lc($page)};
  788. $newfile=0;
  789. }
  790. my $content=$form->field('content');
  791. $content=~s/\r\n/\n/g;
  792. $content=~s/\r/\n/g;
  793. writefile("$srcdir/$file", $content);
  794. my $message="web commit ";
  795. if ($session->param("name")) {
  796. $message.="by ".$session->param("name");
  797. }
  798. else {
  799. $message.="from $ENV{REMOTE_ADDR}";
  800. }
  801. if (length $form->field('comments')) {
  802. $message.=": ".$form->field('comments');
  803. }
  804. if ($svn) {
  805. if ($newfile) {
  806. rcs_add($file);
  807. }
  808. # presumably the commit will trigger an update
  809. # of the wiki
  810. rcs_commit($message);
  811. }
  812. else {
  813. refresh();
  814. }
  815. print $q->redirect("$url/".htmlpage($page));
  816. }
  817. } #}}}
  818. sub cgi () { #{{{
  819. eval q{use CGI};
  820. eval q{use CGI::Session};
  821. my $q=CGI->new;
  822. my $do=$q->param('do');
  823. if (! defined $do || ! length $do) {
  824. error("\"do\" parameter missing");
  825. }
  826. # This does not need a session.
  827. if ($do eq 'recentchanges') {
  828. cgi_recentchanges($q);
  829. return;
  830. }
  831. CGI::Session->name("ikiwiki_session");
  832. my $oldmask=umask(077);
  833. my $session = CGI::Session->new("driver:db_file", $q,
  834. { FileName => "$srcdir/.ikiwiki/sessions.db" });
  835. umask($oldmask);
  836. # Everything below this point needs the user to be signed in.
  837. if ((! $anonok && ! defined $session->param("name")) || $do eq 'signin') {
  838. cgi_signin($q, $session);
  839. # Force session flush with safe umask.
  840. my $oldmask=umask(077);
  841. $session->flush;
  842. umask($oldmask);
  843. return;
  844. }
  845. if ($do eq 'create' || $do eq 'edit') {
  846. cgi_editpage($q, $session);
  847. }
  848. else {
  849. error("unknown do parameter");
  850. }
  851. } #}}}
  852. # main {{{
  853. my $rebuild=0;
  854. my $wrapper=0;
  855. if (grep /^-/, @ARGV) {
  856. eval {use Getopt::Long};
  857. GetOptions(
  858. "wikiname=s" => \$wikiname,
  859. "verbose|v" => \$verbose,
  860. "rebuild" => \$rebuild,
  861. "wrapper" => \$wrapper,
  862. "svn!" => \$svn,
  863. "anonok!" => \$anonok,
  864. "cgi" => \$cgi,
  865. "url=s" => \$url,
  866. "cgiurl=s" => \$cgiurl,
  867. "historyurl=s" => \$historyurl,
  868. ) || usage();
  869. }
  870. usage() unless @ARGV == 3;
  871. ($srcdir) = possibly_foolish_untaint(shift);
  872. ($templatedir) = possibly_foolish_untaint(shift);
  873. ($destdir) = possibly_foolish_untaint(shift);
  874. if ($cgi && ! length $url) {
  875. error("Must specify url to wiki with --url when using --cgi");
  876. }
  877. gen_wrapper($svn, $rebuild) if $wrapper;
  878. memoize('pagename');
  879. memoize('bestlink');
  880. loadindex() unless $rebuild;
  881. if ($cgi) {
  882. cgi();
  883. }
  884. else {
  885. rcs_update() if $svn;
  886. refresh();
  887. saveindex();
  888. }
  889. #}}}