summaryrefslogtreecommitdiff
path: root/ikiwiki
blob: 2815a8e1dd666cb7685c6b7da8382464c3f41aac (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, pagename($1) if length $1;
  346. }
  347. elsif ($state eq 'header' && /^$/) {
  348. $state='body';
  349. }
  350. elsif ($state eq 'body' && /$div/) {
  351. push @ret, { rev => $rev, user => $user,
  352. when => $when, message => $message,
  353. pages => [@pages] } if @pages;
  354. return @ret if @ret >= $num;
  355. $state='header';
  356. $message=$rev=$user=$when=undef;
  357. @pages=();
  358. }
  359. elsif ($state eq 'body') {
  360. $message.="$_<br>\n";
  361. }
  362. }
  363. }
  364. return @ret;
  365. } #}}}
  366. sub prune ($) { #{{{
  367. my $file=shift;
  368. unlink($file);
  369. my $dir=dirname($file);
  370. while (rmdir($dir)) {
  371. $dir=dirname($dir);
  372. }
  373. } #}}}
  374. sub refresh () { #{{{
  375. # Find existing pages.
  376. my %exists;
  377. my @files;
  378. find({
  379. no_chdir => 1,
  380. wanted => sub {
  381. if (/$wiki_file_prune_regexp/) {
  382. $File::Find::prune=1;
  383. }
  384. elsif (! -d $_) {
  385. my ($f)=/$wiki_file_regexp/; # untaint
  386. if (! defined $f) {
  387. warn("skipping bad filename $_\n");
  388. }
  389. else {
  390. $f=~s/^\Q$srcdir\E\/?//;
  391. push @files, $f;
  392. $exists{pagename($f)}=1;
  393. }
  394. }
  395. },
  396. }, $srcdir);
  397. my %rendered;
  398. # check for added or removed pages
  399. my @add;
  400. foreach my $file (@files) {
  401. my $page=pagename($file);
  402. if (! $oldpagemtime{$page}) {
  403. debug("new page $page");
  404. push @add, $file;
  405. $links{$page}=[];
  406. $pagesources{$page}=$file;
  407. }
  408. }
  409. my @del;
  410. foreach my $page (keys %oldpagemtime) {
  411. if (! $exists{$page}) {
  412. debug("removing old page $page");
  413. push @del, $renderedfiles{$page};
  414. prune($destdir."/".$renderedfiles{$page});
  415. delete $renderedfiles{$page};
  416. $oldpagemtime{$page}=0;
  417. delete $pagesources{$page};
  418. }
  419. }
  420. # render any updated files
  421. foreach my $file (@files) {
  422. my $page=pagename($file);
  423. if (! exists $oldpagemtime{$page} ||
  424. mtime("$srcdir/$file") > $oldpagemtime{$page}) {
  425. debug("rendering changed file $file");
  426. render($file);
  427. $rendered{$file}=1;
  428. }
  429. }
  430. # if any files were added or removed, check to see if each page
  431. # needs an update due to linking to them
  432. # TODO: inefficient; pages may get rendered above and again here;
  433. # problem is the bestlink may have changed and we won't know until
  434. # now
  435. if (@add || @del) {
  436. FILE: foreach my $file (@files) {
  437. my $page=pagename($file);
  438. foreach my $f (@add, @del) {
  439. my $p=pagename($f);
  440. foreach my $link (@{$links{$page}}) {
  441. if (bestlink($page, $link) eq $p) {
  442. debug("rendering $file, which links to $p");
  443. render($file);
  444. $rendered{$file}=1;
  445. next FILE;
  446. }
  447. }
  448. }
  449. }
  450. }
  451. # handle backlinks; if a page has added/removed links, update the
  452. # pages it links to
  453. # TODO: inefficient; pages may get rendered above and again here;
  454. # problem is the backlinks could be wrong in the first pass render
  455. # above
  456. if (%rendered) {
  457. my %linkchanged;
  458. foreach my $file (keys %rendered, @del) {
  459. my $page=pagename($file);
  460. if (exists $links{$page}) {
  461. foreach my $link (@{$links{$page}}) {
  462. $link=bestlink($page, $link);
  463. if (length $link &&
  464. ! exists $oldlinks{$page} ||
  465. ! grep { $_ eq $link } @{$oldlinks{$page}}) {
  466. $linkchanged{$link}=1;
  467. }
  468. }
  469. }
  470. if (exists $oldlinks{$page}) {
  471. foreach my $link (@{$oldlinks{$page}}) {
  472. $link=bestlink($page, $link);
  473. if (length $link &&
  474. ! exists $links{$page} ||
  475. ! grep { $_ eq $link } @{$links{$page}}) {
  476. $linkchanged{$link}=1;
  477. }
  478. }
  479. }
  480. }
  481. foreach my $link (keys %linkchanged) {
  482. my $linkfile=$pagesources{$link};
  483. if (defined $linkfile) {
  484. debug("rendering $linkfile, to update its backlinks");
  485. render($linkfile);
  486. }
  487. }
  488. }
  489. } #}}}
  490. # Generates a C wrapper program for running ikiwiki in a specific way.
  491. # The wrapper may be safely made suid.
  492. sub gen_wrapper ($$) { #{{{
  493. my ($svn, $rebuild)=@_;
  494. eval q{use Cwd 'abs_path'};
  495. $srcdir=abs_path($srcdir);
  496. $destdir=abs_path($destdir);
  497. my $this=abs_path($0);
  498. if (! -x $this) {
  499. error("$this doesn't seem to be executable");
  500. }
  501. my @params=($srcdir, $destdir, "--wikiname=$wikiname");
  502. push @params, "--verbose" if $verbose;
  503. push @params, "--rebuild" if $rebuild;
  504. push @params, "--nosvn" if !$svn;
  505. push @params, "--cgi" if $cgi;
  506. push @params, "--url=$url" if $url;
  507. push @params, "--cgiurl=$cgiurl" if $cgiurl;
  508. push @params, "--historyurl=$historyurl" if $historyurl;
  509. my $params=join(" ", @params);
  510. my $call='';
  511. foreach my $p ($this, $this, @params) {
  512. $call.=qq{"$p", };
  513. }
  514. $call.="NULL";
  515. my @envsave;
  516. push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
  517. CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
  518. HTTP_COOKIE} if $cgi;
  519. my $envsave="";
  520. foreach my $var (@envsave) {
  521. $envsave.=<<"EOF"
  522. if ((s=getenv("$var")))
  523. asprintf(&newenviron[i++], "%s=%s", "$var", s);
  524. EOF
  525. }
  526. open(OUT, ">ikiwiki-wrap.c") || error("failed to write ikiwiki-wrap.c: $!");;
  527. print OUT <<"EOF";
  528. /* A wrapper for ikiwiki, can be safely made suid. */
  529. #define _GNU_SOURCE
  530. #include <stdio.h>
  531. #include <unistd.h>
  532. #include <stdlib.h>
  533. #include <string.h>
  534. extern char **environ;
  535. int main (int argc, char **argv) {
  536. /* Sanitize environment. */
  537. char *s;
  538. char *newenviron[$#envsave+3];
  539. int i=0;
  540. $envsave
  541. newenviron[i++]="HOME=$ENV{HOME}";
  542. newenviron[i]=NULL;
  543. environ=newenviron;
  544. if (argc == 2 && strcmp(argv[1], "--params") == 0) {
  545. printf("$params\\n");
  546. exit(0);
  547. }
  548. execl($call);
  549. perror("failed to run $this");
  550. exit(1);
  551. }
  552. EOF
  553. close OUT;
  554. if (system("gcc", "ikiwiki-wrap.c", "-o", "ikiwiki-wrap") != 0) {
  555. error("failed to compile ikiwiki-wrap.c");
  556. }
  557. unlink("ikiwiki-wrap.c");
  558. print "successfully generated ikiwiki-wrap\n";
  559. exit 0;
  560. } #}}}
  561. sub cgi_recentchanges ($) { #{{{
  562. my $q=shift;
  563. my $list="<ul>\n";
  564. foreach my $change (rcs_recentchanges(100)) {
  565. $list.="<li>";
  566. $list.=join(", ", map { htmllink("", $_, 1) } @{$change->{pages}});
  567. $list.="<br>\n";
  568. $list.="changed ".$change->{when}." by ".
  569. htmllink("", $change->{user}, 1).
  570. ": <i>".$change->{message}."</i>\n";
  571. $list.="</li>\n";
  572. }
  573. $list.="</ul>\n";
  574. print $q->header,
  575. $q->start_html("RecentChanges"),
  576. $q->h1(indexlink()." RecentChanges"),
  577. $list,
  578. $q->end_form,
  579. $q->end_html;
  580. } #}}}
  581. sub cgi_signin ($$) { #{{{
  582. my $q=shift;
  583. my $session=shift;
  584. eval q{use CGI::FormBuilder};
  585. my $form = CGI::FormBuilder->new(
  586. title => "$wikiname signin",
  587. fields => [qw(do page name password confirm_password email)],
  588. header => 1,
  589. method => 'POST',
  590. validate => {
  591. name => '/^\w+$/',
  592. confirm_password => {
  593. perl => q{eq $form->field("password")},
  594. },
  595. email => 'EMAIL',
  596. },
  597. required => 'NONE',
  598. javascript => 0,
  599. params => $q,
  600. action => $q->request_uri,
  601. );
  602. $form->sessionid($session->id);
  603. $form->field(name => "name", required => 0);
  604. $form->field(name => "do", type => "hidden");
  605. $form->field(name => "page", type => "hidden");
  606. $form->field(name => "password", type => "password", required => 0);
  607. $form->field(name => "confirm_password", type => "password", required => 0);
  608. $form->field(name => "email", required => 0);
  609. if ($session->param("name")) {
  610. $form->field(name => "name", value => $session->param("name"));
  611. }
  612. if ($q->param("do") ne "signin") {
  613. $form->text("You need to log in before you can edit pages.");
  614. }
  615. if ($form->submitted) {
  616. # Set required fields based on how form was submitted.
  617. my %required=(
  618. "Login" => [qw(name password)],
  619. "Register" => [qw(name password confirm_password email)],
  620. "Mail Password" => [qw(name)],
  621. );
  622. foreach my $opt (@{$required{$form->submitted}}) {
  623. $form->field(name => $opt, required => 1);
  624. }
  625. # Validate password differently depending on how form was
  626. # submitted.
  627. if ($form->submitted eq 'Login') {
  628. $form->field(
  629. name => "password",
  630. validate => sub {
  631. # TODO get real user password
  632. shift eq "foo";
  633. },
  634. );
  635. }
  636. else {
  637. $form->field(name => "password", validate => 'VALUE');
  638. }
  639. }
  640. else {
  641. # Comments only shown first time.
  642. $form->field(name => "name", comment => "use FirstnameLastName");
  643. $form->field(name => "confirm_password", comment => "(only needed");
  644. $form->field(name => "email", comment => "for registration)");
  645. }
  646. if ($form->submitted && $form->validate) {
  647. if ($form->submitted eq 'Login') {
  648. $session->param("name", $form->field("name"));
  649. if (defined $form->field("do")) {
  650. $q->redirect(
  651. "$cgiurl?do=".$form->field("do").
  652. "&page=".$form->field("page"));
  653. }
  654. else {
  655. $q->redirect($url);
  656. }
  657. }
  658. elsif ($form->submitted eq 'Register') {
  659. # TODO: save registration info
  660. $form->field(name => "confirm_password", type => "hidden");
  661. $form->field(name => "email", type => "hidden");
  662. $form->text("Registration successful. Now you can Login.");
  663. print $form->render(submit => ["Login"]);;
  664. }
  665. elsif ($form->submitted eq 'Mail Password') {
  666. # TODO mail password
  667. $form->text("Your password has been emailed to you.");
  668. print $form->render(submit => ["Login", "Register", "Mail Password"]);;
  669. }
  670. }
  671. else {
  672. print $form->render(submit => ["Login", "Register", "Mail Password"]);;
  673. }
  674. } #}}}
  675. sub cgi () { #{{{
  676. eval q{use CGI};
  677. eval q{use CGI::Session};
  678. my $q=CGI->new;
  679. # session id has to be _sessionid for CGI::FormBuilder to work.
  680. # TODO: stop having the formbuilder emit cookies and change session
  681. # id to something else.
  682. CGI::Session->name("_sessionid");
  683. my $session = CGI::Session->new(undef, $q,
  684. { Directory=> "$srcdir/.ikiwiki/sessions" });
  685. my $do=$q->param('do');
  686. if (! defined $do || ! length $do) {
  687. error("\"do\" parameter missing");
  688. }
  689. if ($do eq 'recentchanges') {
  690. cgi_recentchanges($q);
  691. return;
  692. }
  693. if (! defined $session->param("name") || $do eq 'signin') {
  694. cgi_signin($q, $session);
  695. return;
  696. }
  697. my ($page)=$q->param('page')=~/$wiki_file_regexp/;
  698. if (! defined $page || ! length $page || $page ne $q->param('page') ||
  699. $page=~/$wiki_file_prune_regexp/ || $page=~/^\//) {
  700. error("bad page name");
  701. }
  702. $page=lc($page);
  703. my $action=$q->request_uri;
  704. $action=~s/\?.*//;
  705. if ($do eq 'create') {
  706. if (exists $pagesources{lc($page)}) {
  707. # hmm, someone else made the page in the meantime?
  708. print $q->redirect("$url/".htmlpage($page));
  709. }
  710. my @page_locs;
  711. my ($from)=$q->param('from')=~/$wiki_file_regexp/;
  712. if (! defined $from || ! length $from ||
  713. $from ne $q->param('from') ||
  714. $from=~/$wiki_file_prune_regexp/ || $from=~/^\//) {
  715. @page_locs=$page;
  716. }
  717. else {
  718. my $dir=$from."/";
  719. $dir=~s![^/]+/$!!;
  720. push @page_locs, $dir.$page;
  721. push @page_locs, "$from/$page";
  722. while (length $dir) {
  723. $dir=~s![^/]+/$!!;
  724. push @page_locs, $dir.$page;
  725. }
  726. }
  727. $q->param("do", "save");
  728. print $q->header,
  729. $q->start_html("Creating $page"),
  730. $q->h1(indexlink()." Creating $page"),
  731. $q->start_form(-action => $action),
  732. $q->hidden('do'),
  733. "Select page location:",
  734. $q->popup_menu('page', \@page_locs),
  735. $q->textarea(-name => 'content',
  736. -default => "",
  737. -rows => 20,
  738. -columns => 80),
  739. $q->br,
  740. "Optional comment about this change:",
  741. $q->br,
  742. $q->textfield(-name => "comments", -size => 80),
  743. $q->br,
  744. $q->submit("Save Page"),
  745. $q->end_form,
  746. $q->end_html;
  747. }
  748. elsif ($do eq 'edit') {
  749. my $content="";
  750. if (exists $pagesources{lc($page)}) {
  751. $content=readfile("$srcdir/$pagesources{lc($page)}");
  752. $content=~s/\n/\r\n/g;
  753. }
  754. $q->param("do", "save");
  755. print $q->header,
  756. $q->start_html("Editing $page"),
  757. $q->h1(indexlink()." Editing $page"),
  758. $q->start_form(-action => $action),
  759. $q->hidden('do'),
  760. $q->hidden('page'),
  761. $q->textarea(-name => 'content',
  762. -default => $content,
  763. -rows => 20,
  764. -columns => 80),
  765. $q->br,
  766. "Optional comment about this change:",
  767. $q->br,
  768. $q->textfield(-name => "comments", -size => 80),
  769. $q->br,
  770. $q->submit("Save Page"),
  771. $q->end_form,
  772. $q->end_html;
  773. }
  774. elsif ($do eq 'save') {
  775. my $file=$page.$default_pagetype;
  776. my $newfile=1;
  777. if (exists $pagesources{lc($page)}) {
  778. $file=$pagesources{lc($page)};
  779. $newfile=0;
  780. }
  781. my $content=$q->param('content');
  782. $content=~s/\r\n/\n/g;
  783. $content=~s/\r/\n/g;
  784. writefile("$srcdir/$file", $content);
  785. my $message="web commit from $ENV{REMOTE_ADDR}";
  786. if (defined $q->param('comments')) {
  787. $message.=": ".$q->param('comments');
  788. }
  789. if ($svn) {
  790. if ($newfile) {
  791. rcs_add($file);
  792. }
  793. # presumably the commit will trigger an update
  794. # of the wiki
  795. rcs_commit($message);
  796. }
  797. else {
  798. refresh();
  799. }
  800. print $q->redirect("$url/".htmlpage($page));
  801. }
  802. else {
  803. error("unknown do parameter");
  804. }
  805. } #}}}
  806. # main {{{
  807. my $rebuild=0;
  808. my $wrapper=0;
  809. if (grep /^-/, @ARGV) {
  810. eval {use Getopt::Long};
  811. GetOptions(
  812. "wikiname=s" => \$wikiname,
  813. "verbose|v" => \$verbose,
  814. "rebuild" => \$rebuild,
  815. "wrapper" => \$wrapper,
  816. "svn!" => \$svn,
  817. "cgi" => \$cgi,
  818. "url=s" => \$url,
  819. "cgiurl=s" => \$cgiurl,
  820. "historyurl=s" => \$historyurl,
  821. ) || usage();
  822. }
  823. usage() unless @ARGV == 3;
  824. ($srcdir) = possibly_foolish_untaint(shift);
  825. ($templatedir) = possibly_foolish_untaint(shift);
  826. ($destdir) = possibly_foolish_untaint(shift);
  827. if ($cgi && ! length $url) {
  828. error("Must specify url to wiki with --url when using --cgi");
  829. }
  830. gen_wrapper($svn, $rebuild) if $wrapper;
  831. memoize('pagename');
  832. memoize('bestlink');
  833. loadindex() unless $rebuild;
  834. if ($cgi) {
  835. cgi();
  836. }
  837. else {
  838. rcs_update() if $svn;
  839. refresh();
  840. saveindex();
  841. }
  842. #}}}