summaryrefslogtreecommitdiff
path: root/ikiwiki
blob: fa214971bb96bc11c8684844f319b31ec8830e26 (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. $rev=$user=$when=undef;
  359. @pages=@message=();
  360. }
  361. elsif ($state eq 'body') {
  362. push @message, {line => $_},
  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. print $q->header, $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. template => (-e "$templatedir/signin.tmpl" ? "$templatedir/signin.tmpl" : "")
  597. );
  598. $form->sessionid($session->id);
  599. $form->field(name => "name", required => 0);
  600. $form->field(name => "do", type => "hidden");
  601. $form->field(name => "page", type => "hidden");
  602. $form->field(name => "password", type => "password", required => 0);
  603. $form->field(name => "confirm_password", type => "password", required => 0);
  604. $form->field(name => "email", required => 0);
  605. if ($session->param("name")) {
  606. $form->field(name => "name", value => $session->param("name"));
  607. }
  608. if ($q->param("do") ne "signin") {
  609. $form->text("You need to log in before you can edit pages.");
  610. }
  611. if ($form->submitted) {
  612. # Set required fields based on how form was submitted.
  613. my %required=(
  614. "Login" => [qw(name password)],
  615. "Register" => [qw(name password confirm_password email)],
  616. "Mail Password" => [qw(name)],
  617. );
  618. foreach my $opt (@{$required{$form->submitted}}) {
  619. $form->field(name => $opt, required => 1);
  620. }
  621. # Validate password differently depending on how form was
  622. # submitted.
  623. if ($form->submitted eq 'Login') {
  624. $form->field(
  625. name => "password",
  626. validate => sub {
  627. # TODO get real user password
  628. shift eq "foo";
  629. },
  630. );
  631. }
  632. else {
  633. $form->field(name => "password", validate => 'VALUE');
  634. }
  635. }
  636. else {
  637. # Comments only shown first time.
  638. $form->field(name => "name", comment => "use FirstnameLastName");
  639. $form->field(name => "confirm_password", comment => "(only needed");
  640. $form->field(name => "email", comment => "for registration)");
  641. }
  642. if ($form->submitted && $form->validate) {
  643. if ($form->submitted eq 'Login') {
  644. $session->param("name", $form->field("name"));
  645. if (defined $form->field("do")) {
  646. $q->redirect(
  647. "$cgiurl?do=".$form->field("do").
  648. "&page=".$form->field("page"));
  649. }
  650. else {
  651. $q->redirect($url);
  652. }
  653. }
  654. elsif ($form->submitted eq 'Register') {
  655. # TODO: save registration info
  656. $form->field(name => "confirm_password", type => "hidden");
  657. $form->field(name => "email", type => "hidden");
  658. $form->text("Registration successful. Now you can Login.");
  659. print $form->render(submit => ["Login"]);;
  660. }
  661. elsif ($form->submitted eq 'Mail Password') {
  662. # TODO mail password
  663. $form->text("Your password has been emailed to you.");
  664. print $form->render(submit => ["Login", "Register", "Mail Password"]);;
  665. }
  666. }
  667. else {
  668. print $form->render(submit => ["Login", "Register", "Mail Password"]);;
  669. }
  670. } #}}}
  671. sub cgi () { #{{{
  672. eval q{use CGI};
  673. eval q{use CGI::Session};
  674. my $q=CGI->new;
  675. my $do=$q->param('do');
  676. if (! defined $do || ! length $do) {
  677. error("\"do\" parameter missing");
  678. }
  679. # This does not need a session.
  680. if ($do eq 'recentchanges') {
  681. cgi_recentchanges($q);
  682. return;
  683. }
  684. # session id has to be _sessionid for CGI::FormBuilder to work.
  685. # TODO: stop having the formbuilder emit cookies and change session
  686. # id to something else.
  687. CGI::Session->name("_sessionid");
  688. my $session = CGI::Session->new(undef, $q,
  689. { Directory=> "$srcdir/.ikiwiki/sessions" });
  690. # Everything below this point needs the user to be signed in.
  691. if ((! $anonok && ! defined $session->param("name")) || $do eq 'signin') {
  692. cgi_signin($q, $session);
  693. return;
  694. }
  695. my ($page)=$q->param('page')=~/$wiki_file_regexp/;
  696. if (! defined $page || ! length $page || $page ne $q->param('page') ||
  697. $page=~/$wiki_file_prune_regexp/ || $page=~/^\//) {
  698. error("bad page name");
  699. }
  700. $page=lc($page);
  701. my $action=$q->request_uri;
  702. $action=~s/\?.*//;
  703. if ($do eq 'create') {
  704. if (exists $pagesources{lc($page)}) {
  705. # hmm, someone else made the page in the meantime?
  706. print $q->redirect("$url/".htmlpage($page));
  707. }
  708. my @page_locs;
  709. my ($from)=$q->param('from')=~/$wiki_file_regexp/;
  710. if (! defined $from || ! length $from ||
  711. $from ne $q->param('from') ||
  712. $from=~/$wiki_file_prune_regexp/ || $from=~/^\//) {
  713. @page_locs=$page;
  714. }
  715. else {
  716. my $dir=$from."/";
  717. $dir=~s![^/]+/$!!;
  718. push @page_locs, $dir.$page;
  719. push @page_locs, "$from/$page";
  720. while (length $dir) {
  721. $dir=~s![^/]+/$!!;
  722. push @page_locs, $dir.$page;
  723. }
  724. }
  725. $q->param("do", "save");
  726. print $q->header,
  727. $q->start_html("Creating $page"),
  728. $q->h1(indexlink()." Creating $page"),
  729. $q->start_form(-action => $action),
  730. $q->hidden('do'),
  731. "Select page location:",
  732. $q->popup_menu('page', \@page_locs),
  733. $q->textarea(-name => 'content',
  734. -default => "",
  735. -rows => 20,
  736. -columns => 80),
  737. $q->br,
  738. "Optional comment about this change:",
  739. $q->br,
  740. $q->textfield(-name => "comments", -size => 80),
  741. $q->br,
  742. $q->submit("Save Page"),
  743. $q->end_form,
  744. $q->end_html;
  745. }
  746. elsif ($do eq 'edit') {
  747. my $content="";
  748. if (exists $pagesources{lc($page)}) {
  749. $content=readfile("$srcdir/$pagesources{lc($page)}");
  750. $content=~s/\n/\r\n/g;
  751. }
  752. $q->param("do", "save");
  753. print $q->header,
  754. $q->start_html("Editing $page"),
  755. $q->h1(indexlink()." Editing $page"),
  756. $q->start_form(-action => $action),
  757. $q->hidden('do'),
  758. $q->hidden('page'),
  759. $q->textarea(-name => 'content',
  760. -default => $content,
  761. -rows => 20,
  762. -columns => 80),
  763. $q->br,
  764. "Optional comment about this change:",
  765. $q->br,
  766. $q->textfield(-name => "comments", -size => 80),
  767. $q->br,
  768. $q->submit("Save Page"),
  769. $q->end_form,
  770. $q->end_html;
  771. }
  772. elsif ($do eq 'save') {
  773. my $file=$page.$default_pagetype;
  774. my $newfile=1;
  775. if (exists $pagesources{lc($page)}) {
  776. $file=$pagesources{lc($page)};
  777. $newfile=0;
  778. }
  779. my $content=$q->param('content');
  780. $content=~s/\r\n/\n/g;
  781. $content=~s/\r/\n/g;
  782. writefile("$srcdir/$file", $content);
  783. my $message="web commit from $ENV{REMOTE_ADDR}";
  784. if (defined $q->param('comments')) {
  785. $message.=": ".$q->param('comments');
  786. }
  787. if ($svn) {
  788. if ($newfile) {
  789. rcs_add($file);
  790. }
  791. # presumably the commit will trigger an update
  792. # of the wiki
  793. rcs_commit($message);
  794. }
  795. else {
  796. refresh();
  797. }
  798. print $q->redirect("$url/".htmlpage($page));
  799. }
  800. else {
  801. error("unknown do parameter");
  802. }
  803. } #}}}
  804. # main {{{
  805. my $rebuild=0;
  806. my $wrapper=0;
  807. if (grep /^-/, @ARGV) {
  808. eval {use Getopt::Long};
  809. GetOptions(
  810. "wikiname=s" => \$wikiname,
  811. "verbose|v" => \$verbose,
  812. "rebuild" => \$rebuild,
  813. "wrapper" => \$wrapper,
  814. "svn!" => \$svn,
  815. "anonok!" => \$anonok,
  816. "cgi" => \$cgi,
  817. "url=s" => \$url,
  818. "cgiurl=s" => \$cgiurl,
  819. "historyurl=s" => \$historyurl,
  820. ) || usage();
  821. }
  822. usage() unless @ARGV == 3;
  823. ($srcdir) = possibly_foolish_untaint(shift);
  824. ($templatedir) = possibly_foolish_untaint(shift);
  825. ($destdir) = possibly_foolish_untaint(shift);
  826. if ($cgi && ! length $url) {
  827. error("Must specify url to wiki with --url when using --cgi");
  828. }
  829. gen_wrapper($svn, $rebuild) if $wrapper;
  830. memoize('pagename');
  831. memoize('bestlink');
  832. loadindex() unless $rebuild;
  833. if ($cgi) {
  834. cgi();
  835. }
  836. else {
  837. rcs_update() if $svn;
  838. refresh();
  839. saveindex();
  840. }
  841. #}}}