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