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