summaryrefslogtreecommitdiff
path: root/IkiWiki/CGI.pm
blob: d29151ace59356bf2f548592f08f2d46da3ca83a (plain)
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use IkiWiki;
  5. use IkiWiki::UserInfo;
  6. use open qw{:utf8 :std};
  7. use Encode;
  8. package IkiWiki;
  9. sub printheader ($) { #{{{
  10. my $session=shift;
  11. if ($config{sslcookie}) {
  12. print $session->header(-charset => 'utf-8',
  13. -cookie => $session->cookie(-secure => 1));
  14. } else {
  15. print $session->header(-charset => 'utf-8');
  16. }
  17. } #}}}
  18. sub redirect ($$) { #{{{
  19. my $q=shift;
  20. my $url=shift;
  21. if (! $config{w3mmode}) {
  22. print $q->redirect($url);
  23. }
  24. else {
  25. print "Content-type: text/plain\n";
  26. print "W3m-control: GOTO $url\n\n";
  27. }
  28. } #}}}
  29. sub page_locked ($$;$) { #{{{
  30. my $page=shift;
  31. my $session=shift;
  32. my $nonfatal=shift;
  33. my $user=$session->param("name");
  34. return if defined $user && is_admin($user);
  35. foreach my $admin (@{$config{adminuser}}) {
  36. my $locked_pages=userinfo_get($admin, "locked_pages");
  37. if (pagespec_match($page, userinfo_get($admin, "locked_pages"))) {
  38. return 1 if $nonfatal;
  39. error(htmllink("", "", $page, 1)." is locked by ".
  40. userlink($admin)." and cannot be edited.");
  41. }
  42. }
  43. return 0;
  44. } #}}}
  45. sub decode_form_utf8 ($) { #{{{
  46. my $form = shift;
  47. foreach my $f ($form->field) {
  48. next if Encode::is_utf8(scalar $form->field($f));
  49. $form->field(name => $f,
  50. value => decode_utf8($form->field($f)),
  51. force => 1,
  52. );
  53. }
  54. } #}}}
  55. sub cgi_recentchanges ($) { #{{{
  56. my $q=shift;
  57. # Optimisation: building recentchanges means calculating lots of
  58. # links. Memoizing htmllink speeds it up a lot (can't be memoized
  59. # during page builds as the return values may change, but they
  60. # won't here.)
  61. eval q{use Memoize};
  62. error($@) if $@;
  63. memoize("htmllink");
  64. eval q{use Time::Duration};
  65. error($@) if $@;
  66. my $changelog=[rcs_recentchanges(100)];
  67. foreach my $change (@$changelog) {
  68. $change->{when} = concise(ago($change->{when}));
  69. $change->{user} = userlink($change->{user});
  70. my $is_excess = exists $change->{pages}[10]; # limit pages to first 10
  71. delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
  72. $change->{pages} = [
  73. map {
  74. $_->{link} = htmllink("", "", $_->{page}, 1);
  75. $_;
  76. } @{$change->{pages}}
  77. ];
  78. push @{$change->{pages}}, { link => '...' } if $is_excess;
  79. }
  80. my $template=template("recentchanges.tmpl");
  81. $template->param(
  82. title => "RecentChanges",
  83. indexlink => indexlink(),
  84. wikiname => $config{wikiname},
  85. changelog => $changelog,
  86. baseurl => baseurl(),
  87. );
  88. run_hooks(pagetemplate => sub {
  89. shift->(page => "", destpage => "", template => $template);
  90. });
  91. print $q->header(-charset => 'utf-8'), $template->output;
  92. } #}}}
  93. sub cgi_signin ($$) { #{{{
  94. my $q=shift;
  95. my $session=shift;
  96. eval q{use CGI::FormBuilder};
  97. error($@) if $@;
  98. my $form = CGI::FormBuilder->new(
  99. title => "signin",
  100. header => 1,
  101. charset => "utf-8",
  102. method => 'POST',
  103. required => 'NONE',
  104. javascript => 0,
  105. params => $q,
  106. action => $config{cgiurl},
  107. header => 0,
  108. template => (-e "$config{templatedir}/signin.tmpl" ?
  109. {template_params("signin.tmpl")} : ""),
  110. stylesheet => baseurl()."style.css",
  111. );
  112. my $buttons=["Login"];
  113. $form->field(name => "do", type => "hidden");
  114. if ($q->param("do") ne "signin" && !$form->submitted) {
  115. $form->text("You need to log in first.");
  116. }
  117. run_hooks(formbuilder_setup => sub {
  118. shift->(form => $form, cgi => $q, session => $session);
  119. });
  120. decode_form_utf8($form);
  121. if (exists $hooks{formbuilder}) {
  122. run_hooks(formbuilder => sub {
  123. shift->(form => $form, cgi => $q, session => $session,
  124. buttons => $buttons);
  125. });
  126. }
  127. else {
  128. if ($form->submitted) {
  129. $form->validate;
  130. }
  131. printheader($session);
  132. print misctemplate($form->title, $form->render(submit => $buttons));
  133. }
  134. } #}}}
  135. sub cgi_postsignin ($$) { #{{{
  136. my $q=shift;
  137. my $session=shift;
  138. # Continue with whatever was being done before the signin process.
  139. if (defined $q->param("do") && $q->param("do") ne "signin" &&
  140. defined $session->param("postsignin")) {
  141. my $postsignin=CGI->new($session->param("postsignin"));
  142. $session->clear("postsignin");
  143. cgi($postsignin, $session);
  144. exit;
  145. }
  146. else {
  147. redirect($q, $config{url});
  148. }
  149. } #}}}
  150. sub cgi_prefs ($$) { #{{{
  151. my $q=shift;
  152. my $session=shift;
  153. eval q{use CGI::FormBuilder};
  154. error($@) if $@;
  155. my $form = CGI::FormBuilder->new(
  156. title => "preferences",
  157. header => 0,
  158. charset => "utf-8",
  159. method => 'POST',
  160. validate => {
  161. email => 'EMAIL',
  162. },
  163. required => 'NONE',
  164. javascript => 0,
  165. params => $q,
  166. action => $config{cgiurl},
  167. template => (-e "$config{templatedir}/prefs.tmpl" ?
  168. {template_params("prefs.tmpl")} : ""),
  169. stylesheet => baseurl()."style.css",
  170. );
  171. my $buttons=["Save Preferences", "Logout", "Cancel"];
  172. run_hooks(formbuilder_setup => sub {
  173. shift->(form => $form, cgi => $q, session => $session);
  174. });
  175. $form->field(name => "do", type => "hidden");
  176. $form->field(name => "email", size => 50);
  177. $form->field(name => "subscriptions", size => 50,
  178. comment => "(".htmllink("", "", "PageSpec", 1).")");
  179. $form->field(name => "locked_pages", size => 50,
  180. comment => "(".htmllink("", "", "PageSpec", 1).")");
  181. $form->field(name => "banned_users", size => 50);
  182. my $user_name=$session->param("name");
  183. if (! is_admin($user_name)) {
  184. $form->field(name => "locked_pages", type => "hidden");
  185. $form->field(name => "banned_users", type => "hidden");
  186. }
  187. if (! $form->submitted) {
  188. $form->field(name => "email", force => 1,
  189. value => userinfo_get($user_name, "email"));
  190. $form->field(name => "subscriptions", force => 1,
  191. value => userinfo_get($user_name, "subscriptions"));
  192. $form->field(name => "locked_pages", force => 1,
  193. value => userinfo_get($user_name, "locked_pages"));
  194. if (is_admin($user_name)) {
  195. $form->field(name => "banned_users", force => 1,
  196. value => join(" ", get_banned_users()));
  197. }
  198. }
  199. decode_form_utf8($form);
  200. if ($form->submitted eq 'Logout') {
  201. $session->delete();
  202. redirect($q, $config{url});
  203. return;
  204. }
  205. elsif ($form->submitted eq 'Cancel') {
  206. redirect($q, $config{url});
  207. return;
  208. }
  209. elsif ($form->submitted eq "Save Preferences" && $form->validate) {
  210. foreach my $field (qw(email subscriptions locked_pages)) {
  211. if (defined $form->field($field) && length $form->field($field)) {
  212. userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
  213. }
  214. }
  215. if (is_admin($user_name)) {
  216. set_banned_users(grep { ! is_admin($_) }
  217. split(' ', $form->field("banned_users")));
  218. }
  219. $form->text("Preferences saved.");
  220. }
  221. if (exists $hooks{formbuilder}) {
  222. run_hooks(formbuilder => sub {
  223. shift->(form => $form, cgi => $q, session => $session,
  224. buttons => $buttons);
  225. });
  226. }
  227. else {
  228. printheader($session);
  229. print misctemplate($form->title, $form->render(submit => $buttons));
  230. }
  231. } #}}}
  232. sub cgi_editpage ($$) { #{{{
  233. my $q=shift;
  234. my $session=shift;
  235. my @fields=qw(do rcsinfo subpage from page type editcontent comments);
  236. my @buttons=("Save Page", "Preview", "Cancel");
  237. eval q{use CGI::FormBuilder; use CGI::FormBuilder::Template::HTML};
  238. error($@) if $@;
  239. my $renderer=CGI::FormBuilder::Template::HTML->new(
  240. fields => \@fields,
  241. template_params("editpage.tmpl"),
  242. );
  243. run_hooks(pagetemplate => sub {
  244. shift->(page => "", destpage => "", template => $renderer->engine);
  245. });
  246. my $form = CGI::FormBuilder->new(
  247. fields => \@fields,
  248. header => 1,
  249. charset => "utf-8",
  250. method => 'POST',
  251. validate => {
  252. editcontent => '/.+/',
  253. },
  254. required => [qw{editcontent}],
  255. javascript => 0,
  256. params => $q,
  257. action => $config{cgiurl},
  258. table => 0,
  259. template => $renderer,
  260. );
  261. run_hooks(formbuilder_setup => sub {
  262. shift->(form => $form, cgi => $q, session => $session);
  263. });
  264. decode_form_utf8($form);
  265. # This untaint is safe because titlepage removes any problematic
  266. # characters.
  267. my ($page)=$form->field('page');
  268. $page=titlepage(possibly_foolish_untaint($page));
  269. if (! defined $page || ! length $page ||
  270. $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
  271. error("bad page name");
  272. }
  273. my $from;
  274. if (defined $form->field('from')) {
  275. ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
  276. }
  277. my $file;
  278. my $type;
  279. if (exists $pagesources{$page}) {
  280. $file=$pagesources{$page};
  281. $type=pagetype($file);
  282. }
  283. else {
  284. $type=$form->param('type');
  285. if (defined $type && length $type && $hooks{htmlize}{$type}) {
  286. $type=possibly_foolish_untaint($type);
  287. }
  288. elsif (defined $from) {
  289. # favor the type of linking page
  290. $type=pagetype($pagesources{$from});
  291. }
  292. $type=$config{default_pageext} unless defined $type;
  293. $file=$page.".".$type;
  294. }
  295. my $newfile=0;
  296. if (! -e "$config{srcdir}/$file") {
  297. $newfile=1;
  298. }
  299. $form->field(name => "do", type => 'hidden');
  300. $form->field(name => "from", type => 'hidden');
  301. $form->field(name => "rcsinfo", type => 'hidden');
  302. $form->field(name => "subpage", type => 'hidden');
  303. $form->field(name => "page", value => $page, force => 1);
  304. $form->field(name => "type", value => $type, force => 1);
  305. $form->field(name => "comments", type => "text", size => 80);
  306. $form->field(name => "editcontent", type => "textarea", rows => 20,
  307. cols => 80);
  308. $form->tmpl_param("can_commit", $config{rcs});
  309. $form->tmpl_param("indexlink", indexlink());
  310. $form->tmpl_param("helponformattinglink",
  311. htmllink("", "", "HelpOnFormatting", 1));
  312. $form->tmpl_param("baseurl", baseurl());
  313. if (! $form->submitted) {
  314. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  315. force => 1);
  316. }
  317. if ($form->submitted eq "Cancel") {
  318. if ($newfile && defined $from) {
  319. redirect($q, "$config{url}/".htmlpage($from));
  320. }
  321. elsif ($newfile) {
  322. redirect($q, $config{url});
  323. }
  324. else {
  325. redirect($q, "$config{url}/".htmlpage($page));
  326. }
  327. return;
  328. }
  329. elsif ($form->submitted eq "Preview") {
  330. my $content=$form->field('editcontent');
  331. my $comments=$form->field('comments');
  332. $form->field(name => "editcontent",
  333. value => $content, force => 1);
  334. $form->field(name => "comments",
  335. value => $comments, force => 1);
  336. $config{rss}=$config{atom}=0; # avoid preview writing a feed!
  337. $form->tmpl_param("page_preview",
  338. htmlize($page, $type,
  339. linkify($page, "",
  340. preprocess($page, $page,
  341. filter($page, $content)))));
  342. }
  343. else {
  344. $form->tmpl_param("page_preview", "");
  345. }
  346. $form->tmpl_param("page_conflict", "");
  347. if (! $form->submitted || $form->submitted eq "Preview" ||
  348. ! $form->validate) {
  349. if ($form->field("do") eq "create") {
  350. my @page_locs;
  351. my $best_loc;
  352. if (! defined $from || ! length $from ||
  353. $from ne $form->field('from') ||
  354. $from=~/$config{wiki_file_prune_regexp}/ ||
  355. $from=~/^\// ||
  356. $form->submitted eq "Preview") {
  357. @page_locs=$best_loc=$page;
  358. }
  359. else {
  360. my $dir=$from."/";
  361. $dir=~s![^/]+/+$!!;
  362. if ((defined $form->field('subpage') && length $form->field('subpage')) ||
  363. $page eq 'discussion') {
  364. $best_loc="$from/$page";
  365. }
  366. else {
  367. $best_loc=$dir.$page;
  368. }
  369. push @page_locs, $dir.$page;
  370. push @page_locs, "$from/$page";
  371. while (length $dir) {
  372. $dir=~s![^/]+/+$!!;
  373. push @page_locs, $dir.$page;
  374. }
  375. }
  376. @page_locs = grep {
  377. ! exists $pagecase{lc $_} &&
  378. ! page_locked($_, $session, 1)
  379. } @page_locs;
  380. if (! @page_locs) {
  381. # hmm, someone else made the page in the
  382. # meantime?
  383. redirect($q, "$config{url}/".htmlpage($page));
  384. return;
  385. }
  386. my @page_types;
  387. if (exists $hooks{htmlize}) {
  388. @page_types=keys %{$hooks{htmlize}};
  389. }
  390. $form->tmpl_param("page_select", 1);
  391. $form->field(name => "page", type => 'select',
  392. options => \@page_locs, value => $best_loc);
  393. $form->field(name => "type", type => 'select',
  394. options => \@page_types);
  395. $form->title("creating ".pagetitle($page));
  396. }
  397. elsif ($form->field("do") eq "edit") {
  398. page_locked($page, $session);
  399. if (! defined $form->field('editcontent') ||
  400. ! length $form->field('editcontent')) {
  401. my $content="";
  402. if (exists $pagesources{$page}) {
  403. $content=readfile(srcfile($pagesources{$page}));
  404. $content=~s/\n/\r\n/g;
  405. }
  406. $form->field(name => "editcontent", value => $content,
  407. force => 1);
  408. }
  409. $form->tmpl_param("page_select", 0);
  410. $form->field(name => "page", type => 'hidden');
  411. $form->field(name => "type", type => 'hidden');
  412. $form->title("editing ".pagetitle($page));
  413. }
  414. print $form->render(submit => \@buttons);
  415. }
  416. else {
  417. # save page
  418. page_locked($page, $session);
  419. my $content=$form->field('editcontent');
  420. $content=~s/\r\n/\n/g;
  421. $content=~s/\r/\n/g;
  422. writefile($file, $config{srcdir}, $content);
  423. my $message="web commit ";
  424. if (defined $session->param("name") &&
  425. length $session->param("name")) {
  426. $message.="by ".$session->param("name");
  427. }
  428. else {
  429. $message.="from $ENV{REMOTE_ADDR}";
  430. }
  431. if (defined $form->field('comments') &&
  432. length $form->field('comments')) {
  433. $message.=": ".$form->field('comments');
  434. }
  435. if ($config{rcs}) {
  436. if ($newfile) {
  437. rcs_add($file);
  438. }
  439. # prevent deadlock with post-commit hook
  440. unlockwiki();
  441. # presumably the commit will trigger an update
  442. # of the wiki
  443. my $conflict=rcs_commit($file, $message,
  444. $form->field("rcsinfo"));
  445. if (defined $conflict) {
  446. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  447. force => 1);
  448. $form->tmpl_param("page_conflict", 1);
  449. $form->field("editcontent", value => $conflict, force => 1);
  450. $form->field(name => "comments", value => $form->field('comments'), force => 1);
  451. $form->field("do", "edit)");
  452. $form->tmpl_param("page_select", 0);
  453. $form->field(name => "page", type => 'hidden');
  454. $form->field(name => "type", type => 'hidden');
  455. $form->title("editing $page");
  456. print $form->render(submit => \@buttons);
  457. return;
  458. }
  459. }
  460. else {
  461. require IkiWiki::Render;
  462. refresh();
  463. saveindex();
  464. }
  465. # The trailing question mark tries to avoid broken
  466. # caches and get the most recent version of the page.
  467. redirect($q, "$config{url}/".htmlpage($page)."?updated");
  468. }
  469. } #}}}
  470. sub cgi (;$$) { #{{{
  471. my $q=shift;
  472. my $session=shift;
  473. if (! $q) {
  474. eval q{use CGI; use CGI::Session};
  475. error($@) if $@;
  476. $q=CGI->new;
  477. run_hooks(cgi => sub { shift->($q) });
  478. }
  479. my $do=$q->param('do');
  480. if (! defined $do || ! length $do) {
  481. my $error = $q->cgi_error;
  482. if ($error) {
  483. error("Request not processed: $error");
  484. }
  485. else {
  486. error("\"do\" parameter missing");
  487. }
  488. }
  489. # Things that do not need a session.
  490. if ($do eq 'recentchanges') {
  491. cgi_recentchanges($q);
  492. return;
  493. }
  494. elsif ($do eq 'hyperestraier') {
  495. cgi_hyperestraier();
  496. }
  497. # Need to lock the wiki before getting a session.
  498. lockwiki();
  499. if (! $session) {
  500. CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
  501. my $oldmask=umask(077);
  502. $session = CGI::Session->new("driver:DB_File", $q,
  503. { FileName => "$config{wikistatedir}/sessions.db" });
  504. umask($oldmask);
  505. }
  506. # Auth hooks can sign a user in.
  507. if ($do ne 'signin' && ! defined $session->param("name")) {
  508. run_hooks(auth => sub {
  509. shift->($q, $session)
  510. });
  511. if (defined $session->param("name")) {
  512. # Make sure whatever user was authed is in the
  513. # userinfo db.
  514. if (! userinfo_get($session->param("name"), "regdate")) {
  515. userinfo_setall($session->param("name"), {
  516. email => "",
  517. password => "",
  518. regdate => time,
  519. });
  520. }
  521. }
  522. }
  523. # Everything below this point needs the user to be signed in.
  524. if (((! $config{anonok} || $do eq 'prefs') &&
  525. (! defined $session->param("name") ||
  526. ! userinfo_get($session->param("name"), "regdate")))
  527. || $do eq 'signin') {
  528. if ($do ne 'signin' && ! defined $session->param("postsignin")) {
  529. $session->param(postsignin => $ENV{QUERY_STRING});
  530. }
  531. cgi_signin($q, $session);
  532. # Force session flush with safe umask.
  533. my $oldmask=umask(077);
  534. $session->flush;
  535. umask($oldmask);
  536. return;
  537. }
  538. elsif (defined $session->param("postsignin")) {
  539. cgi_postsignin($q, $session);
  540. }
  541. if (defined $session->param("name") && userinfo_get($session->param("name"), "banned")) {
  542. print $q->header(-status => "403 Forbidden");
  543. $session->delete();
  544. print "You are banned.";
  545. exit;
  546. }
  547. if ($do eq 'create' || $do eq 'edit') {
  548. cgi_editpage($q, $session);
  549. }
  550. elsif ($do eq 'prefs') {
  551. cgi_prefs($q, $session);
  552. }
  553. elsif ($do eq 'blog') {
  554. my $page=titlepage(decode_utf8($q->param('title')));
  555. # if the page already exists, munge it to be unique
  556. my $from=$q->param('from');
  557. my $add="";
  558. while (exists $pagecase{lc "$from/$page$add"}) {
  559. $add=1 unless length $add;
  560. $add++;
  561. }
  562. $q->param('page', $page.$add);
  563. # now run same as create
  564. $q->param('do', 'create');
  565. cgi_editpage($q, $session);
  566. }
  567. else {
  568. error("unknown do parameter");
  569. }
  570. } #}}}
  571. sub userlink ($) { #{{{
  572. my $user=shift;
  573. eval q{use CGI 'escapeHTML'};
  574. error($@) if $@;
  575. if ($user =~ m!^https?://! &&
  576. eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
  577. # Munge user-urls, as used by eg, OpenID.
  578. my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
  579. my $display=$oid->display;
  580. # Convert "user.somehost.com" to "user [somehost.com]".
  581. if ($display !~ /\[/) {
  582. $display=~s/^(.*?)\.([^.]+\.[a-z]+)$/$1 [$2]/;
  583. }
  584. return "<a href=\"$user\">".escapeHTML($display)."</a>";
  585. }
  586. else {
  587. return htmllink("", "", escapeHTML($user), 1);
  588. }
  589. } #}}}
  590. 1