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