summaryrefslogtreecommitdiff
path: root/IkiWiki/CGI.pm
blob: 0d010b1150d3e5c5ee94028821c05d0558ee5f73 (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. use CGI;
  9. $CGI::DISABLE_UPLOADS=1;
  10. package IkiWiki;
  11. sub printheader ($) { #{{{
  12. my $session=shift;
  13. if ($config{sslcookie}) {
  14. print $session->header(-charset => 'utf-8',
  15. -cookie => $session->cookie(-secure => 1));
  16. } else {
  17. print $session->header(-charset => 'utf-8');
  18. }
  19. } #}}}
  20. sub showform ($$$$;@) { #{{{
  21. my $form=shift;
  22. my $buttons=shift;
  23. my $session=shift;
  24. my $cgi=shift;
  25. if (exists $hooks{formbuilder}) {
  26. run_hooks(formbuilder => sub {
  27. shift->(form => $form, cgi => $cgi, session => $session,
  28. buttons => $buttons);
  29. });
  30. }
  31. printheader($session);
  32. print misctemplate($form->title, $form->render(submit => $buttons), @_);
  33. }
  34. sub redirect ($$) { #{{{
  35. my $q=shift;
  36. my $url=shift;
  37. if (! $config{w3mmode}) {
  38. print $q->redirect($url);
  39. }
  40. else {
  41. print "Content-type: text/plain\n";
  42. print "W3m-control: GOTO $url\n\n";
  43. }
  44. } #}}}
  45. sub check_canedit ($$$;$) { #{{{
  46. my $page=shift;
  47. my $q=shift;
  48. my $session=shift;
  49. my $nonfatal=shift;
  50. my $canedit;
  51. run_hooks(canedit => sub {
  52. return if defined $canedit;
  53. my $ret=shift->($page, $q, $session);
  54. if (defined $ret) {
  55. if ($ret eq "") {
  56. $canedit=1;
  57. }
  58. elsif (ref $ret eq 'CODE') {
  59. $ret->() unless $nonfatal;
  60. $canedit=0;
  61. }
  62. elsif (defined $ret) {
  63. error($ret) unless $nonfatal;
  64. $canedit=0;
  65. }
  66. }
  67. });
  68. return $canedit;
  69. } #}}}
  70. sub decode_cgi_utf8 ($) { #{{{
  71. # decode_form_utf8 method is needed for 5.10
  72. if ($] < 5.01) {
  73. my $cgi = shift;
  74. foreach my $f ($cgi->param) {
  75. $cgi->param($f, map { decode_utf8 $_ } $cgi->param($f));
  76. }
  77. }
  78. } #}}}
  79. sub decode_form_utf8 ($) { #{{{
  80. if ($] >= 5.01) {
  81. my $form = shift;
  82. foreach my $f ($form->field) {
  83. $form->field(name => $f,
  84. value => decode_utf8($form->field($f)),
  85. force => 1,
  86. );
  87. }
  88. }
  89. } #}}}
  90. # Check if the user is signed in. If not, redirect to the signin form and
  91. # save their place to return to later.
  92. sub needsignin ($$) { #{{{
  93. my $q=shift;
  94. my $session=shift;
  95. if (! defined $session->param("name") ||
  96. ! userinfo_get($session->param("name"), "regdate")) {
  97. $session->param(postsignin => $ENV{QUERY_STRING});
  98. cgi_signin($q, $session);
  99. cgi_savesession($session);
  100. exit;
  101. }
  102. } #}}}
  103. sub cgi_signin ($$) { #{{{
  104. my $q=shift;
  105. my $session=shift;
  106. decode_cgi_utf8($q);
  107. eval q{use CGI::FormBuilder};
  108. error($@) if $@;
  109. my $form = CGI::FormBuilder->new(
  110. title => "signin",
  111. name => "signin",
  112. charset => "utf-8",
  113. method => 'POST',
  114. required => 'NONE',
  115. javascript => 0,
  116. params => $q,
  117. action => $config{cgiurl},
  118. header => 0,
  119. template => {type => 'div'},
  120. stylesheet => baseurl()."style.css",
  121. );
  122. my $buttons=["Login"];
  123. if ($q->param("do") ne "signin" && !$form->submitted) {
  124. $form->text(gettext("You need to log in first."));
  125. }
  126. $form->field(name => "do", type => "hidden", value => "signin",
  127. force => 1);
  128. decode_form_utf8($form);
  129. run_hooks(formbuilder_setup => sub {
  130. shift->(form => $form, cgi => $q, session => $session,
  131. buttons => $buttons);
  132. });
  133. decode_form_utf8($form);
  134. if ($form->submitted) {
  135. $form->validate;
  136. }
  137. showform($form, $buttons, $session, $q);
  138. } #}}}
  139. sub cgi_postsignin ($$) { #{{{
  140. my $q=shift;
  141. my $session=shift;
  142. # Continue with whatever was being done before the signin process.
  143. if (defined $session->param("postsignin")) {
  144. my $postsignin=CGI->new($session->param("postsignin"));
  145. $session->clear("postsignin");
  146. cgi($postsignin, $session);
  147. cgi_savesession($session);
  148. exit;
  149. }
  150. else {
  151. error(gettext("login failed, perhaps you need to turn on cookies?"));
  152. }
  153. } #}}}
  154. sub cgi_prefs ($$) { #{{{
  155. my $q=shift;
  156. my $session=shift;
  157. needsignin($q, $session);
  158. decode_cgi_utf8($q);
  159. # The session id is stored on the form and checked to
  160. # guard against CSRF.
  161. my $sid=$q->param('sid');
  162. if (! defined $sid) {
  163. $q->delete_all;
  164. }
  165. elsif ($sid ne $session->id) {
  166. error(gettext("Your login session has expired."));
  167. }
  168. eval q{use CGI::FormBuilder};
  169. error($@) if $@;
  170. my $form = CGI::FormBuilder->new(
  171. title => "preferences",
  172. name => "preferences",
  173. header => 0,
  174. charset => "utf-8",
  175. method => 'POST',
  176. validate => {
  177. email => 'EMAIL',
  178. },
  179. required => 'NONE',
  180. javascript => 0,
  181. params => $q,
  182. action => $config{cgiurl},
  183. template => {type => 'div'},
  184. stylesheet => baseurl()."style.css",
  185. fieldsets => [
  186. [login => gettext("Login")],
  187. [preferences => gettext("Preferences")],
  188. [admin => gettext("Admin")]
  189. ],
  190. );
  191. my $buttons=["Save Preferences", "Logout", "Cancel"];
  192. decode_form_utf8($form);
  193. run_hooks(formbuilder_setup => sub {
  194. shift->(form => $form, cgi => $q, session => $session,
  195. buttons => $buttons);
  196. });
  197. decode_form_utf8($form);
  198. $form->field(name => "do", type => "hidden", value => "prefs",
  199. force => 1);
  200. $form->field(name => "sid", type => "hidden", value => $session->id,
  201. force => 1);
  202. $form->field(name => "email", size => 50, fieldset => "preferences");
  203. $form->field(name => "banned_users", size => 50,
  204. fieldset => "admin");
  205. my $user_name=$session->param("name");
  206. if (! is_admin($user_name)) {
  207. $form->field(name => "banned_users", type => "hidden");
  208. }
  209. if (! $form->submitted) {
  210. $form->field(name => "email", force => 1,
  211. value => userinfo_get($user_name, "email"));
  212. if (is_admin($user_name)) {
  213. $form->field(name => "banned_users", force => 1,
  214. value => join(" ", get_banned_users()));
  215. }
  216. }
  217. if ($form->submitted eq 'Logout') {
  218. $session->delete();
  219. redirect($q, $config{url});
  220. return;
  221. }
  222. elsif ($form->submitted eq 'Cancel') {
  223. redirect($q, $config{url});
  224. return;
  225. }
  226. elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
  227. if (defined $form->field('email')) {
  228. userinfo_set($user_name, 'email', $form->field('email')) ||
  229. error("failed to set email");
  230. }
  231. if (is_admin($user_name)) {
  232. set_banned_users(grep { ! is_admin($_) }
  233. split(' ',
  234. $form->field("banned_users"))) ||
  235. error("failed saving changes");
  236. }
  237. $form->text(gettext("Preferences saved."));
  238. }
  239. showform($form, $buttons, $session, $q);
  240. } #}}}
  241. sub cgi_editpage ($$) { #{{{
  242. my $q=shift;
  243. my $session=shift;
  244. decode_cgi_utf8($q);
  245. my @fields=qw(do rcsinfo subpage from page type editcontent comments);
  246. my @buttons=("Save Page", "Preview", "Cancel");
  247. eval q{use CGI::FormBuilder};
  248. error($@) if $@;
  249. my $form = CGI::FormBuilder->new(
  250. fields => \@fields,
  251. charset => "utf-8",
  252. method => 'POST',
  253. required => [qw{editcontent}],
  254. javascript => 0,
  255. params => $q,
  256. action => $config{cgiurl},
  257. header => 0,
  258. table => 0,
  259. template => scalar template_params("editpage.tmpl"),
  260. wikiname => $config{wikiname},
  261. );
  262. decode_form_utf8($form);
  263. run_hooks(formbuilder_setup => sub {
  264. shift->(form => $form, cgi => $q, session => $session,
  265. buttons => \@buttons);
  266. });
  267. decode_form_utf8($form);
  268. # This untaint is safe because titlepage removes any problematic
  269. # characters.
  270. my ($page)=$form->field('page');
  271. $page=titlepage(possibly_foolish_untaint($page));
  272. if (! defined $page || ! length $page ||
  273. file_pruned($page, $config{srcdir}) || $page=~/^\//) {
  274. error("bad page name");
  275. }
  276. my $baseurl=$config{url}."/".htmlpage($page);
  277. my $from;
  278. if (defined $form->field('from')) {
  279. ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
  280. }
  281. my $file;
  282. my $type;
  283. if (exists $pagesources{$page} && $form->field("do") ne "create") {
  284. $file=$pagesources{$page};
  285. $type=pagetype($file);
  286. if (! defined $type || $type=~/^_/) {
  287. error(sprintf(gettext("%s is not an editable page"), $page));
  288. }
  289. if (! $form->submitted) {
  290. $form->field(name => "rcsinfo",
  291. value => rcs_prepedit($file), force => 1);
  292. }
  293. $form->field(name => "editcontent", validate => '/.*/');
  294. }
  295. else {
  296. $type=$form->param('type');
  297. if (defined $type && length $type && $hooks{htmlize}{$type}) {
  298. $type=possibly_foolish_untaint($type);
  299. }
  300. elsif (defined $from && exists $pagesources{$from}) {
  301. # favor the type of linking page
  302. $type=pagetype($pagesources{$from});
  303. }
  304. $type=$config{default_pageext} unless defined $type;
  305. $file=$page.".".$type;
  306. if (! $form->submitted) {
  307. $form->field(name => "rcsinfo", value => "", force => 1);
  308. }
  309. $form->field(name => "editcontent", validate => '/.+/');
  310. }
  311. $form->field(name => "do", type => 'hidden');
  312. $form->field(name => "sid", type => "hidden", value => $session->id,
  313. force => 1);
  314. $form->field(name => "from", type => 'hidden');
  315. $form->field(name => "rcsinfo", type => 'hidden');
  316. $form->field(name => "subpage", type => 'hidden');
  317. $form->field(name => "page", value => pagetitle($page, 1), force => 1);
  318. $form->field(name => "type", value => $type, force => 1);
  319. $form->field(name => "comments", type => "text", size => 80);
  320. $form->field(name => "editcontent", type => "textarea", rows => 20,
  321. cols => 80);
  322. $form->tmpl_param("can_commit", $config{rcs});
  323. $form->tmpl_param("indexlink", indexlink());
  324. $form->tmpl_param("helponformattinglink",
  325. htmllink($page, $page, "ikiwiki/formatting",
  326. noimageinline => 1,
  327. linktext => "FormattingHelp"));
  328. if ($form->submitted eq "Cancel") {
  329. if ($form->field("do") eq "create" && defined $from) {
  330. redirect($q, "$config{url}/".htmlpage($from));
  331. }
  332. elsif ($form->field("do") eq "create") {
  333. redirect($q, $config{url});
  334. }
  335. else {
  336. redirect($q, "$config{url}/".htmlpage($page));
  337. }
  338. return;
  339. }
  340. elsif ($form->submitted eq "Preview") {
  341. my $new=not exists $pagesources{$page};
  342. if ($new) {
  343. # temporarily record its type
  344. $pagesources{$page}=$page.".".$type;
  345. }
  346. my $content=$form->field('editcontent');
  347. run_hooks(editcontent => sub {
  348. $content=shift->(
  349. content => $content,
  350. page => $page,
  351. cgi => $q,
  352. session => $session,
  353. );
  354. });
  355. my $preview=htmlize($page, $page, $type,
  356. linkify($page, $page,
  357. preprocess($page, $page,
  358. filter($page, $page, $content), 0, 1)));
  359. run_hooks(format => sub {
  360. $preview=shift->(
  361. page => $page,
  362. content => $preview,
  363. );
  364. });
  365. $form->tmpl_param("page_preview", $preview);
  366. if ($new) {
  367. delete $pagesources{$page};
  368. }
  369. # previewing may have created files on disk
  370. saveindex();
  371. }
  372. elsif ($form->submitted eq "Save Page") {
  373. $form->tmpl_param("page_preview", "");
  374. }
  375. $form->tmpl_param("page_conflict", "");
  376. if ($form->submitted ne "Save Page" || ! $form->validate) {
  377. if ($form->field("do") eq "create") {
  378. my @page_locs;
  379. my $best_loc;
  380. if (! defined $from || ! length $from ||
  381. $from ne $form->field('from') ||
  382. file_pruned($from, $config{srcdir}) ||
  383. $from=~/^\// ||
  384. $form->submitted eq "Preview") {
  385. @page_locs=$best_loc=$page;
  386. }
  387. else {
  388. my $dir=$from."/";
  389. $dir=~s![^/]+/+$!!;
  390. if ((defined $form->field('subpage') && length $form->field('subpage')) ||
  391. $page eq gettext('discussion')) {
  392. $best_loc="$from/$page";
  393. }
  394. else {
  395. $best_loc=$dir.$page;
  396. }
  397. push @page_locs, $dir.$page;
  398. push @page_locs, "$from/$page";
  399. while (length $dir) {
  400. $dir=~s![^/]+/+$!!;
  401. push @page_locs, $dir.$page;
  402. }
  403. push @page_locs, "$config{userdir}/$page"
  404. if length $config{userdir};
  405. }
  406. @page_locs = grep {
  407. ! exists $pagecase{lc $_}
  408. } @page_locs;
  409. if (! @page_locs) {
  410. # hmm, someone else made the page in the
  411. # meantime?
  412. if ($form->submitted eq "Preview") {
  413. # let them go ahead with the edit
  414. # and resolve the conflict at save
  415. # time
  416. @page_locs=$page;
  417. }
  418. else {
  419. redirect($q, "$config{url}/".htmlpage($page));
  420. return;
  421. }
  422. }
  423. my @editable_locs = grep {
  424. check_canedit($_, $q, $session, 1)
  425. } @page_locs;
  426. if (! @editable_locs) {
  427. # let it throw an error this time
  428. map { check_canedit($_, $q, $session) } @page_locs;
  429. }
  430. my @page_types;
  431. if (exists $hooks{htmlize}) {
  432. @page_types=grep { !/^_/ }
  433. keys %{$hooks{htmlize}};
  434. }
  435. $form->tmpl_param("page_select", 1);
  436. $form->field(name => "page", type => 'select',
  437. options => [ map { pagetitle($_, 1) } @editable_locs ],
  438. value => pagetitle($best_loc, 1));
  439. $form->field(name => "type", type => 'select',
  440. options => \@page_types);
  441. $form->title(sprintf(gettext("creating %s"), pagetitle($page)));
  442. }
  443. elsif ($form->field("do") eq "edit") {
  444. check_canedit($page, $q, $session);
  445. if (! defined $form->field('editcontent') ||
  446. ! length $form->field('editcontent')) {
  447. my $content="";
  448. if (exists $pagesources{$page}) {
  449. $content=readfile(srcfile($pagesources{$page}));
  450. $content=~s/\n/\r\n/g;
  451. }
  452. $form->field(name => "editcontent", value => $content,
  453. force => 1);
  454. }
  455. $form->tmpl_param("page_select", 0);
  456. $form->field(name => "page", type => 'hidden');
  457. $form->field(name => "type", type => 'hidden');
  458. $form->title(sprintf(gettext("editing %s"), pagetitle($page)));
  459. }
  460. showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
  461. }
  462. else {
  463. # save page
  464. check_canedit($page, $q, $session);
  465. # The session id is stored on the form and checked to
  466. # guard against CSRF. But only if the user is logged in,
  467. # as anonok can allow anonymous edits.
  468. if (defined $session->param("name")) {
  469. my $sid=$q->param('sid');
  470. if (! defined $sid || $sid ne $session->id) {
  471. error(gettext("Your login session has expired."));
  472. }
  473. }
  474. my $exists=-e "$config{srcdir}/$file";
  475. if ($form->field("do") ne "create" && ! $exists &&
  476. ! defined srcfile($file, 1)) {
  477. $form->tmpl_param("page_gone", 1);
  478. $form->field(name => "do", value => "create", force => 1);
  479. $form->tmpl_param("page_select", 0);
  480. $form->field(name => "page", type => 'hidden');
  481. $form->field(name => "type", type => 'hidden');
  482. $form->title(sprintf(gettext("editing %s"), $page));
  483. showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
  484. return;
  485. }
  486. elsif ($form->field("do") eq "create" && $exists) {
  487. $form->tmpl_param("creation_conflict", 1);
  488. $form->field(name => "do", value => "edit", force => 1);
  489. $form->tmpl_param("page_select", 0);
  490. $form->field(name => "page", type => 'hidden');
  491. $form->field(name => "type", type => 'hidden');
  492. $form->title(sprintf(gettext("editing %s"), $page));
  493. $form->field("editcontent",
  494. value => readfile("$config{srcdir}/$file").
  495. "\n\n\n".$form->field("editcontent"),
  496. force => 1);
  497. showform($form, \@buttons, $session, $q, forcebaseurl => $baseurl);
  498. return;
  499. }
  500. my $content=$form->field('editcontent');
  501. run_hooks(editcontent => sub {
  502. $content=shift->(
  503. content => $content,
  504. page => $page,
  505. cgi => $q,
  506. session => $session,
  507. );
  508. });
  509. $content=~s/\r\n/\n/g;
  510. $content=~s/\r/\n/g;
  511. $content.="\n" if $content !~ /\n$/;
  512. $config{cgi}=0; # avoid cgi error message
  513. eval { writefile($file, $config{srcdir}, $content) };
  514. $config{cgi}=1;
  515. if ($@) {
  516. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  517. force => 1);
  518. $form->tmpl_param("failed_save", 1);
  519. $form->tmpl_param("error_message", $@);
  520. $form->field("editcontent", value => $content, force => 1);
  521. $form->tmpl_param("page_select", 0);
  522. $form->field(name => "page", type => 'hidden');
  523. $form->field(name => "type", type => 'hidden');
  524. $form->title(sprintf(gettext("editing %s"), $page));
  525. showform($form, \@buttons, $session, $q,
  526. forcebaseurl => $baseurl);
  527. return;
  528. }
  529. my $conflict;
  530. if ($config{rcs}) {
  531. my $message="";
  532. if (defined $form->field('comments') &&
  533. length $form->field('comments')) {
  534. $message=$form->field('comments');
  535. }
  536. if (! $exists) {
  537. rcs_add($file);
  538. }
  539. # Prevent deadlock with post-commit hook by
  540. # signaling to it that it should not try to
  541. # do anything.
  542. disable_commit_hook();
  543. $conflict=rcs_commit($file, $message,
  544. $form->field("rcsinfo"),
  545. $session->param("name"), $ENV{REMOTE_ADDR});
  546. enable_commit_hook();
  547. rcs_update();
  548. }
  549. # Refresh even if there was a conflict, since other changes
  550. # may have been committed while the post-commit hook was
  551. # disabled.
  552. require IkiWiki::Render;
  553. refresh();
  554. saveindex();
  555. if (defined $conflict) {
  556. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  557. force => 1);
  558. $form->tmpl_param("page_conflict", 1);
  559. $form->field("editcontent", value => $conflict, force => 1);
  560. $form->field("do", "edit", force => 1);
  561. $form->tmpl_param("page_select", 0);
  562. $form->field(name => "page", type => 'hidden');
  563. $form->field(name => "type", type => 'hidden');
  564. $form->title(sprintf(gettext("editing %s"), $page));
  565. showform($form, \@buttons, $session, $q,
  566. forcebaseurl => $baseurl);
  567. return;
  568. }
  569. else {
  570. # The trailing question mark tries to avoid broken
  571. # caches and get the most recent version of the page.
  572. redirect($q, "$config{url}/".htmlpage($page)."?updated");
  573. }
  574. }
  575. } #}}}
  576. sub cgi_getsession ($) { #{{{
  577. my $q=shift;
  578. eval q{use CGI::Session};
  579. error($@) if $@;
  580. CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
  581. my $oldmask=umask(077);
  582. my $session = CGI::Session->new("driver:DB_File", $q,
  583. { FileName => "$config{wikistatedir}/sessions.db" });
  584. umask($oldmask);
  585. return $session;
  586. } #}}}
  587. sub cgi_savesession ($) { #{{{
  588. my $session=shift;
  589. # Force session flush with safe umask.
  590. my $oldmask=umask(077);
  591. $session->flush;
  592. umask($oldmask);
  593. } #}}}
  594. sub cgi (;$$) { #{{{
  595. my $q=shift;
  596. my $session=shift;
  597. if (! $q) {
  598. binmode(STDIN);
  599. $q=CGI->new;
  600. binmode(STDIN, ":utf8");
  601. run_hooks(cgi => sub { shift->($q) });
  602. }
  603. my $do=$q->param('do');
  604. if (! defined $do || ! length $do) {
  605. my $error = $q->cgi_error;
  606. if ($error) {
  607. error("Request not processed: $error");
  608. }
  609. else {
  610. error("\"do\" parameter missing");
  611. }
  612. }
  613. # Need to lock the wiki before getting a session.
  614. lockwiki();
  615. loadindex();
  616. if (! $session) {
  617. $session=cgi_getsession($q);
  618. }
  619. # Auth hooks can sign a user in.
  620. if ($do ne 'signin' && ! defined $session->param("name")) {
  621. run_hooks(auth => sub {
  622. shift->($q, $session)
  623. });
  624. if (defined $session->param("name")) {
  625. # Make sure whatever user was authed is in the
  626. # userinfo db.
  627. if (! userinfo_get($session->param("name"), "regdate")) {
  628. userinfo_setall($session->param("name"), {
  629. email => "",
  630. password => "",
  631. regdate => time,
  632. }) || error("failed adding user");
  633. }
  634. }
  635. }
  636. if (defined $session->param("name") &&
  637. userinfo_get($session->param("name"), "banned")) {
  638. print $q->header(-status => "403 Forbidden");
  639. $session->delete();
  640. print gettext("You are banned.");
  641. cgi_savesession($session);
  642. }
  643. run_hooks(sessioncgi => sub { shift->($q, $session) });
  644. if ($do eq 'signin') {
  645. cgi_signin($q, $session);
  646. cgi_savesession($session);
  647. }
  648. elsif ($do eq 'prefs') {
  649. cgi_prefs($q, $session);
  650. }
  651. elsif ($do eq 'create' || $do eq 'edit') {
  652. cgi_editpage($q, $session);
  653. }
  654. elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
  655. cgi_postsignin($q, $session);
  656. }
  657. else {
  658. error("unknown do parameter");
  659. }
  660. } #}}}
  661. 1