summaryrefslogtreecommitdiff
path: root/IkiWiki/CGI.pm
blob: 3e324ae1cbdcc834e467b131114006d17a07ed44 (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. cgi_savesession($session);
  145. exit;
  146. }
  147. else {
  148. redirect($q, $config{url});
  149. }
  150. } #}}}
  151. sub cgi_prefs ($$) { #{{{
  152. my $q=shift;
  153. my $session=shift;
  154. eval q{use CGI::FormBuilder};
  155. error($@) if $@;
  156. my $form = CGI::FormBuilder->new(
  157. title => "preferences",
  158. header => 0,
  159. charset => "utf-8",
  160. method => 'POST',
  161. validate => {
  162. email => 'EMAIL',
  163. },
  164. required => 'NONE',
  165. javascript => 0,
  166. params => $q,
  167. action => $config{cgiurl},
  168. template => (-e "$config{templatedir}/prefs.tmpl" ?
  169. {template_params("prefs.tmpl")} : ""),
  170. stylesheet => baseurl()."style.css",
  171. );
  172. my $buttons=["Save Preferences", "Logout", "Cancel"];
  173. run_hooks(formbuilder_setup => sub {
  174. shift->(form => $form, cgi => $q, session => $session);
  175. });
  176. $form->field(name => "do", type => "hidden");
  177. $form->field(name => "email", size => 50);
  178. $form->field(name => "subscriptions", size => 50,
  179. comment => "(".htmllink("", "", "PageSpec", 1).")");
  180. $form->field(name => "locked_pages", size => 50,
  181. comment => "(".htmllink("", "", "PageSpec", 1).")");
  182. $form->field(name => "banned_users", size => 50);
  183. my $user_name=$session->param("name");
  184. if (! is_admin($user_name)) {
  185. $form->field(name => "locked_pages", type => "hidden");
  186. $form->field(name => "banned_users", type => "hidden");
  187. }
  188. if (! $form->submitted) {
  189. $form->field(name => "email", force => 1,
  190. value => userinfo_get($user_name, "email"));
  191. $form->field(name => "subscriptions", force => 1,
  192. value => userinfo_get($user_name, "subscriptions"));
  193. $form->field(name => "locked_pages", force => 1,
  194. value => userinfo_get($user_name, "locked_pages"));
  195. if (is_admin($user_name)) {
  196. $form->field(name => "banned_users", force => 1,
  197. value => join(" ", get_banned_users()));
  198. }
  199. }
  200. decode_form_utf8($form);
  201. if ($form->submitted eq 'Logout') {
  202. $session->delete();
  203. redirect($q, $config{url});
  204. return;
  205. }
  206. elsif ($form->submitted eq 'Cancel') {
  207. redirect($q, $config{url});
  208. return;
  209. }
  210. elsif ($form->submitted eq "Save Preferences" && $form->validate) {
  211. foreach my $field (qw(email subscriptions locked_pages)) {
  212. if (defined $form->field($field) && length $form->field($field)) {
  213. userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
  214. }
  215. }
  216. if (is_admin($user_name)) {
  217. set_banned_users(grep { ! is_admin($_) }
  218. split(' ', $form->field("banned_users")));
  219. }
  220. $form->text("Preferences saved.");
  221. }
  222. if (exists $hooks{formbuilder}) {
  223. run_hooks(formbuilder => sub {
  224. shift->(form => $form, cgi => $q, session => $session,
  225. buttons => $buttons);
  226. });
  227. }
  228. else {
  229. printheader($session);
  230. print misctemplate($form->title, $form->render(submit => $buttons));
  231. }
  232. } #}}}
  233. sub cgi_editpage ($$) { #{{{
  234. my $q=shift;
  235. my $session=shift;
  236. my @fields=qw(do rcsinfo subpage from page type editcontent comments);
  237. my @buttons=("Save Page", "Preview", "Cancel");
  238. eval q{use CGI::FormBuilder; use CGI::FormBuilder::Template::HTML};
  239. error($@) if $@;
  240. my $renderer=CGI::FormBuilder::Template::HTML->new(
  241. fields => \@fields,
  242. template_params("editpage.tmpl"),
  243. );
  244. run_hooks(pagetemplate => sub {
  245. shift->(page => "", destpage => "", template => $renderer->engine);
  246. });
  247. my $form = CGI::FormBuilder->new(
  248. fields => \@fields,
  249. header => 1,
  250. charset => "utf-8",
  251. method => 'POST',
  252. validate => {
  253. editcontent => '/.+/',
  254. },
  255. required => [qw{editcontent}],
  256. javascript => 0,
  257. params => $q,
  258. action => $config{cgiurl},
  259. table => 0,
  260. template => $renderer,
  261. );
  262. run_hooks(formbuilder_setup => sub {
  263. shift->(form => $form, cgi => $q, session => $session);
  264. });
  265. decode_form_utf8($form);
  266. # This untaint is safe because titlepage removes any problematic
  267. # characters.
  268. my ($page)=$form->field('page');
  269. $page=titlepage(possibly_foolish_untaint($page));
  270. if (! defined $page || ! length $page ||
  271. $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
  272. error("bad page name");
  273. }
  274. my $from;
  275. if (defined $form->field('from')) {
  276. ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
  277. }
  278. my $file;
  279. my $type;
  280. if (exists $pagesources{$page}) {
  281. $file=$pagesources{$page};
  282. $type=pagetype($file);
  283. }
  284. else {
  285. $type=$form->param('type');
  286. if (defined $type && length $type && $hooks{htmlize}{$type}) {
  287. $type=possibly_foolish_untaint($type);
  288. }
  289. elsif (defined $from) {
  290. # favor the type of linking page
  291. $type=pagetype($pagesources{$from});
  292. }
  293. $type=$config{default_pageext} unless defined $type;
  294. $file=$page.".".$type;
  295. }
  296. my $newfile=0;
  297. if (! -e "$config{srcdir}/$file") {
  298. $newfile=1;
  299. }
  300. $form->field(name => "do", type => 'hidden');
  301. $form->field(name => "from", type => 'hidden');
  302. $form->field(name => "rcsinfo", type => 'hidden');
  303. $form->field(name => "subpage", type => 'hidden');
  304. $form->field(name => "page", value => $page, force => 1);
  305. $form->field(name => "type", value => $type, force => 1);
  306. $form->field(name => "comments", type => "text", size => 80);
  307. $form->field(name => "editcontent", type => "textarea", rows => 20,
  308. cols => 80);
  309. $form->tmpl_param("can_commit", $config{rcs});
  310. $form->tmpl_param("indexlink", indexlink());
  311. $form->tmpl_param("helponformattinglink",
  312. htmllink("", "", "HelpOnFormatting", 1));
  313. $form->tmpl_param("baseurl", baseurl());
  314. if (! $form->submitted) {
  315. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  316. force => 1);
  317. }
  318. if ($form->submitted eq "Cancel") {
  319. if ($newfile && defined $from) {
  320. redirect($q, "$config{url}/".htmlpage($from));
  321. }
  322. elsif ($newfile) {
  323. redirect($q, $config{url});
  324. }
  325. else {
  326. redirect($q, "$config{url}/".htmlpage($page));
  327. }
  328. return;
  329. }
  330. elsif ($form->submitted eq "Preview") {
  331. my $content=$form->field('editcontent');
  332. my $comments=$form->field('comments');
  333. $form->field(name => "editcontent",
  334. value => $content, force => 1);
  335. $form->field(name => "comments",
  336. value => $comments, force => 1);
  337. $config{rss}=$config{atom}=0; # avoid preview writing a feed!
  338. $form->tmpl_param("page_preview",
  339. htmlize($page, $type,
  340. linkify($page, "",
  341. preprocess($page, $page,
  342. filter($page, $content)))));
  343. }
  344. else {
  345. $form->tmpl_param("page_preview", "");
  346. }
  347. $form->tmpl_param("page_conflict", "");
  348. if (! $form->submitted || $form->submitted eq "Preview" ||
  349. ! $form->validate) {
  350. if ($form->field("do") eq "create") {
  351. my @page_locs;
  352. my $best_loc;
  353. if (! defined $from || ! length $from ||
  354. $from ne $form->field('from') ||
  355. $from=~/$config{wiki_file_prune_regexp}/ ||
  356. $from=~/^\// ||
  357. $form->submitted eq "Preview") {
  358. @page_locs=$best_loc=$page;
  359. }
  360. else {
  361. my $dir=$from."/";
  362. $dir=~s![^/]+/+$!!;
  363. if ((defined $form->field('subpage') && length $form->field('subpage')) ||
  364. $page eq 'discussion') {
  365. $best_loc="$from/$page";
  366. }
  367. else {
  368. $best_loc=$dir.$page;
  369. }
  370. push @page_locs, $dir.$page;
  371. push @page_locs, "$from/$page";
  372. while (length $dir) {
  373. $dir=~s![^/]+/+$!!;
  374. push @page_locs, $dir.$page;
  375. }
  376. }
  377. @page_locs = grep {
  378. ! exists $pagecase{lc $_} &&
  379. ! page_locked($_, $session, 1)
  380. } @page_locs;
  381. if (! @page_locs) {
  382. # hmm, someone else made the page in the
  383. # meantime?
  384. redirect($q, "$config{url}/".htmlpage($page));
  385. return;
  386. }
  387. my @page_types;
  388. if (exists $hooks{htmlize}) {
  389. @page_types=keys %{$hooks{htmlize}};
  390. }
  391. $form->tmpl_param("page_select", 1);
  392. $form->field(name => "page", type => 'select',
  393. options => \@page_locs, value => $best_loc);
  394. $form->field(name => "type", type => 'select',
  395. options => \@page_types);
  396. $form->title("creating ".pagetitle($page));
  397. }
  398. elsif ($form->field("do") eq "edit") {
  399. page_locked($page, $session);
  400. if (! defined $form->field('editcontent') ||
  401. ! length $form->field('editcontent')) {
  402. my $content="";
  403. if (exists $pagesources{$page}) {
  404. $content=readfile(srcfile($pagesources{$page}));
  405. $content=~s/\n/\r\n/g;
  406. }
  407. $form->field(name => "editcontent", value => $content,
  408. force => 1);
  409. }
  410. $form->tmpl_param("page_select", 0);
  411. $form->field(name => "page", type => 'hidden');
  412. $form->field(name => "type", type => 'hidden');
  413. $form->title("editing ".pagetitle($page));
  414. }
  415. print $form->render(submit => \@buttons);
  416. }
  417. else {
  418. # save page
  419. page_locked($page, $session);
  420. my $content=$form->field('editcontent');
  421. $content=~s/\r\n/\n/g;
  422. $content=~s/\r/\n/g;
  423. writefile($file, $config{srcdir}, $content);
  424. if ($config{rcs}) {
  425. my $message="";
  426. if (defined $form->field('comments') &&
  427. length $form->field('comments')) {
  428. $message=$form->field('comments');
  429. }
  430. if ($newfile) {
  431. rcs_add($file);
  432. }
  433. # prevent deadlock with post-commit hook
  434. unlockwiki();
  435. # presumably the commit will trigger an update
  436. # of the wiki
  437. my $conflict=rcs_commit($file, $message,
  438. $form->field("rcsinfo"),
  439. $session->param("name"), $ENV{REMOTE_ADDR});
  440. if (defined $conflict) {
  441. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  442. force => 1);
  443. $form->tmpl_param("page_conflict", 1);
  444. $form->field("editcontent", value => $conflict, force => 1);
  445. $form->field(name => "comments", value => $form->field('comments'), force => 1);
  446. $form->field("do", "edit)");
  447. $form->tmpl_param("page_select", 0);
  448. $form->field(name => "page", type => 'hidden');
  449. $form->field(name => "type", type => 'hidden');
  450. $form->title("editing $page");
  451. print $form->render(submit => \@buttons);
  452. return;
  453. }
  454. }
  455. else {
  456. require IkiWiki::Render;
  457. refresh();
  458. saveindex();
  459. }
  460. # The trailing question mark tries to avoid broken
  461. # caches and get the most recent version of the page.
  462. redirect($q, "$config{url}/".htmlpage($page)."?updated");
  463. }
  464. } #}}}
  465. sub cgi_getsession ($) { #{{{
  466. my $q=shift;
  467. eval q{use CGI::Session};
  468. CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
  469. my $oldmask=umask(077);
  470. my $session = CGI::Session->new("driver:DB_File", $q,
  471. { FileName => "$config{wikistatedir}/sessions.db" });
  472. umask($oldmask);
  473. return $session;
  474. } #}}}
  475. sub cgi_savesession ($) { #{{{
  476. my $session=shift;
  477. # Force session flush with safe umask.
  478. my $oldmask=umask(077);
  479. $session->flush;
  480. umask($oldmask);
  481. }
  482. sub cgi (;$$) { #{{{
  483. my $q=shift;
  484. my $session=shift;
  485. if (! $q) {
  486. eval q{use CGI};
  487. error($@) if $@;
  488. $q=CGI->new;
  489. run_hooks(cgi => sub { shift->($q) });
  490. }
  491. my $do=$q->param('do');
  492. if (! defined $do || ! length $do) {
  493. my $error = $q->cgi_error;
  494. if ($error) {
  495. error("Request not processed: $error");
  496. }
  497. else {
  498. error("\"do\" parameter missing");
  499. }
  500. }
  501. # Things that do not need a session.
  502. if ($do eq 'recentchanges') {
  503. cgi_recentchanges($q);
  504. return;
  505. }
  506. elsif ($do eq 'hyperestraier') {
  507. cgi_hyperestraier();
  508. }
  509. # Need to lock the wiki before getting a session.
  510. lockwiki();
  511. if (! $session) {
  512. $session=cgi_getsession($q);
  513. }
  514. # Auth hooks can sign a user in.
  515. if ($do ne 'signin' && ! defined $session->param("name")) {
  516. run_hooks(auth => sub {
  517. shift->($q, $session)
  518. });
  519. if (defined $session->param("name")) {
  520. # Make sure whatever user was authed is in the
  521. # userinfo db.
  522. if (! userinfo_get($session->param("name"), "regdate")) {
  523. userinfo_setall($session->param("name"), {
  524. email => "",
  525. password => "",
  526. regdate => time,
  527. });
  528. }
  529. }
  530. }
  531. # Everything below this point needs the user to be signed in.
  532. if (((! $config{anonok} || $do eq 'prefs') &&
  533. (! defined $session->param("name") ||
  534. ! userinfo_get($session->param("name"), "regdate")))
  535. || $do eq 'signin') {
  536. if ($do ne 'signin' && ! defined $session->param("postsignin")) {
  537. $session->param(postsignin => $ENV{QUERY_STRING});
  538. }
  539. cgi_signin($q, $session);
  540. cgi_savesession($session);
  541. return;
  542. }
  543. elsif (defined $session->param("postsignin")) {
  544. cgi_postsignin($q, $session);
  545. }
  546. if (defined $session->param("name") && userinfo_get($session->param("name"), "banned")) {
  547. print $q->header(-status => "403 Forbidden");
  548. $session->delete();
  549. print "You are banned.";
  550. cgi_savesession($session);
  551. exit;
  552. }
  553. if ($do eq 'create' || $do eq 'edit') {
  554. cgi_editpage($q, $session);
  555. }
  556. elsif ($do eq 'prefs') {
  557. cgi_prefs($q, $session);
  558. }
  559. elsif ($do eq 'blog') {
  560. my $page=titlepage(decode_utf8($q->param('title')));
  561. # if the page already exists, munge it to be unique
  562. my $from=$q->param('from');
  563. my $add="";
  564. while (exists $pagecase{lc "$from/$page$add"}) {
  565. $add=1 unless length $add;
  566. $add++;
  567. }
  568. $q->param('page', $page.$add);
  569. # now run same as create
  570. $q->param('do', 'create');
  571. cgi_editpage($q, $session);
  572. }
  573. else {
  574. error("unknown do parameter");
  575. }
  576. } #}}}
  577. sub userlink ($) { #{{{
  578. my $user=shift;
  579. eval q{use CGI 'escapeHTML'};
  580. error($@) if $@;
  581. if ($user =~ m!^https?://! &&
  582. eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
  583. # Munge user-urls, as used by eg, OpenID.
  584. my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
  585. my $display=$oid->display;
  586. # Convert "user.somehost.com" to "user [somehost.com]".
  587. if ($display !~ /\[/) {
  588. $display=~s/^(.*?)\.([^.]+\.[a-z]+)$/$1 [$2]/;
  589. }
  590. $display=~s!^https?://!!; # make sure this is removed
  591. return "<a href=\"$user\">".escapeHTML($display)."</a>";
  592. }
  593. else {
  594. return htmllink("", "", escapeHTML($user), 1);
  595. }
  596. } #}}}
  597. 1