summaryrefslogtreecommitdiff
path: root/IkiWiki/CGI.pm
blob: 8b01ca3f89ab004a81d04d4066d081c3d3baea65 (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. htmllink("", "", $admin, 1)." 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. unlockwiki();
  58. # Optimisation: building recentchanges means calculating lots of
  59. # links. Memoizing htmllink speeds it up a lot (can't be memoized
  60. # during page builds as the return values may change, but they
  61. # won't here.)
  62. eval q{use Memoize};
  63. memoize("htmllink");
  64. eval q{use Time::Duration};
  65. eval q{use CGI 'escapeHTML'};
  66. my $changelog=[rcs_recentchanges(100)];
  67. foreach my $change (@$changelog) {
  68. $change->{when} = concise(ago($change->{when}));
  69. $change->{user} = htmllink("", "", escapeHTML($change->{user}), 1);
  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. my $form = CGI::FormBuilder->new(
  98. title => "signin",
  99. fields => [qw(do title page subpage from name password confirm_password email)],
  100. header => 1,
  101. charset => "utf-8",
  102. method => 'POST',
  103. validate => {
  104. confirm_password => {
  105. perl => q{eq $form->field("password")},
  106. },
  107. email => 'EMAIL',
  108. },
  109. required => 'NONE',
  110. javascript => 0,
  111. params => $q,
  112. action => $config{cgiurl},
  113. header => 0,
  114. template => (-e "$config{templatedir}/signin.tmpl" ?
  115. {template_params("signin.tmpl")} : ""),
  116. stylesheet => baseurl()."style.css",
  117. );
  118. decode_form_utf8($form);
  119. $form->field(name => "name", required => 0);
  120. $form->field(name => "do", type => "hidden");
  121. $form->field(name => "page", type => "hidden");
  122. $form->field(name => "title", type => "hidden");
  123. $form->field(name => "from", type => "hidden");
  124. $form->field(name => "subpage", type => "hidden");
  125. $form->field(name => "password", type => "password", required => 0);
  126. $form->field(name => "confirm_password", type => "password", required => 0);
  127. $form->field(name => "email", required => 0);
  128. if ($q->param("do") ne "signin" && !$form->submitted) {
  129. $form->text("You need to log in first.");
  130. }
  131. if ($form->submitted) {
  132. # Set required fields based on how form was submitted.
  133. my %required=(
  134. "Login" => [qw(name password)],
  135. "Register" => [qw(name password confirm_password email)],
  136. "Mail Password" => [qw(name)],
  137. );
  138. foreach my $opt (@{$required{$form->submitted}}) {
  139. $form->field(name => $opt, required => 1);
  140. }
  141. # Validate password differently depending on how
  142. # form was submitted.
  143. if ($form->submitted eq 'Login') {
  144. $form->field(
  145. name => "password",
  146. validate => sub {
  147. length $form->field("name") &&
  148. shift eq userinfo_get($form->field("name"), 'password');
  149. },
  150. );
  151. $form->field(name => "name", validate => '/^\w+$/');
  152. }
  153. else {
  154. $form->field(name => "password", validate => 'VALUE');
  155. }
  156. # And make sure the entered name exists when logging
  157. # in or sending email, and does not when registering.
  158. if ($form->submitted eq 'Register') {
  159. $form->field(
  160. name => "name",
  161. validate => sub {
  162. my $name=shift;
  163. length $name &&
  164. $name=~/$config{wiki_file_regexp}/ &&
  165. ! userinfo_get($name, "regdate");
  166. },
  167. );
  168. }
  169. else {
  170. $form->field(
  171. name => "name",
  172. validate => sub {
  173. my $name=shift;
  174. length $name &&
  175. userinfo_get($name, "regdate");
  176. },
  177. );
  178. }
  179. }
  180. else {
  181. # First time settings.
  182. $form->field(name => "name", comment => "use FirstnameLastName");
  183. $form->field(name => "confirm_password", comment => "(only needed");
  184. $form->field(name => "email", comment => "for registration)");
  185. if ($session->param("name")) {
  186. $form->field(name => "name", value => $session->param("name"));
  187. }
  188. }
  189. if ($form->submitted && $form->validate) {
  190. if ($form->submitted eq 'Login') {
  191. $session->param("name", $form->field("name"));
  192. if (defined $form->field("do") &&
  193. $form->field("do") ne 'signin') {
  194. redirect($q, cgiurl(
  195. do => $form->field("do"),
  196. page => $form->field("page"),
  197. title => $form->field("title"),
  198. subpage => $form->field("subpage"),
  199. from => $form->field("from"),
  200. ));
  201. }
  202. else {
  203. redirect($q, $config{url});
  204. }
  205. }
  206. elsif ($form->submitted eq 'Register') {
  207. my $user_name=$form->field('name');
  208. if (userinfo_setall($user_name, {
  209. 'email' => $form->field('email'),
  210. 'password' => $form->field('password'),
  211. 'regdate' => time
  212. })) {
  213. $form->field(name => "confirm_password", type => "hidden");
  214. $form->field(name => "email", type => "hidden");
  215. $form->text("Registration successful. Now you can Login.");
  216. printheader($session);
  217. print misctemplate($form->title, $form->render(submit => ["Login"]));
  218. }
  219. else {
  220. error("Error saving registration.");
  221. }
  222. }
  223. elsif ($form->submitted eq 'Mail Password') {
  224. my $user_name=$form->field("name");
  225. my $template=template("passwordmail.tmpl");
  226. $template->param(
  227. user_name => $user_name,
  228. user_password => userinfo_get($user_name, "password"),
  229. wikiurl => $config{url},
  230. wikiname => $config{wikiname},
  231. REMOTE_ADDR => $ENV{REMOTE_ADDR},
  232. );
  233. eval q{use Mail::Sendmail};
  234. sendmail(
  235. To => userinfo_get($user_name, "email"),
  236. From => "$config{wikiname} admin <$config{adminemail}>",
  237. Subject => "$config{wikiname} information",
  238. Message => $template->output,
  239. ) or error("Failed to send mail");
  240. $form->text("Your password has been emailed to you.");
  241. $form->field(name => "name", required => 0);
  242. printheader($session);
  243. print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
  244. }
  245. }
  246. else {
  247. printheader($session);
  248. print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
  249. }
  250. } #}}}
  251. sub cgi_prefs ($$) { #{{{
  252. my $q=shift;
  253. my $session=shift;
  254. eval q{use CGI::FormBuilder};
  255. my $form = CGI::FormBuilder->new(
  256. title => "preferences",
  257. fields => [qw(do name password confirm_password email
  258. subscriptions locked_pages)],
  259. header => 0,
  260. charset => "utf-8",
  261. method => 'POST',
  262. validate => {
  263. confirm_password => {
  264. perl => q{eq $form->field("password")},
  265. },
  266. email => 'EMAIL',
  267. },
  268. required => 'NONE',
  269. javascript => 0,
  270. params => $q,
  271. action => $config{cgiurl},
  272. template => (-e "$config{templatedir}/prefs.tmpl" ?
  273. {template_params("prefs.tmpl")} : ""),
  274. stylesheet => baseurl()."style.css",
  275. );
  276. my @buttons=("Save Preferences", "Logout", "Cancel");
  277. my $user_name=$session->param("name");
  278. $form->field(name => "do", type => "hidden");
  279. $form->field(name => "name", disabled => 1,
  280. value => $user_name, force => 1);
  281. $form->field(name => "password", type => "password");
  282. $form->field(name => "confirm_password", type => "password");
  283. $form->field(name => "subscriptions", size => 50,
  284. comment => "(".htmllink("", "", "PageSpec", 1).")");
  285. $form->field(name => "locked_pages", size => 50,
  286. comment => "(".htmllink("", "", "PageSpec", 1).")");
  287. $form->field(name => "banned_users", size => 50);
  288. if (! is_admin($user_name)) {
  289. $form->field(name => "locked_pages", type => "hidden");
  290. $form->field(name => "banned_users", type => "hidden");
  291. }
  292. if ($config{httpauth}) {
  293. $form->field(name => "password", type => "hidden");
  294. $form->field(name => "confirm_password", type => "hidden");
  295. }
  296. if (! $form->submitted) {
  297. $form->field(name => "email", force => 1,
  298. value => userinfo_get($user_name, "email"));
  299. $form->field(name => "subscriptions", force => 1,
  300. value => userinfo_get($user_name, "subscriptions"));
  301. $form->field(name => "locked_pages", force => 1,
  302. value => userinfo_get($user_name, "locked_pages"));
  303. if (is_admin($user_name)) {
  304. $form->field(name => "banned_users", force => 1,
  305. value => join(" ", get_banned_users()));
  306. }
  307. }
  308. decode_form_utf8($form);
  309. if ($form->submitted eq 'Logout') {
  310. $session->delete();
  311. redirect($q, $config{url});
  312. return;
  313. }
  314. elsif ($form->submitted eq 'Cancel') {
  315. redirect($q, $config{url});
  316. return;
  317. }
  318. elsif ($form->submitted eq "Save Preferences" && $form->validate) {
  319. foreach my $field (qw(password email subscriptions locked_pages)) {
  320. if (length $form->field($field)) {
  321. userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
  322. }
  323. }
  324. if (is_admin($user_name)) {
  325. set_banned_users(grep { ! is_admin($_) }
  326. split(' ', $form->field("banned_users")));
  327. }
  328. $form->text("Preferences saved.");
  329. }
  330. printheader($session);
  331. print misctemplate($form->title, $form->render(submit => \@buttons));
  332. } #}}}
  333. sub cgi_editpage ($$) { #{{{
  334. my $q=shift;
  335. my $session=shift;
  336. my @fields=qw(do rcsinfo subpage from page type editcontent comments);
  337. my @buttons=("Save Page", "Preview", "Cancel");
  338. eval q{use CGI::FormBuilder; use CGI::FormBuilder::Template::HTML};
  339. my $renderer=CGI::FormBuilder::Template::HTML->new(
  340. fields => \@fields,
  341. template_params("editpage.tmpl"),
  342. );
  343. run_hooks(pagetemplate => sub {
  344. shift->(page => "", destpage => "", template => $renderer->engine);
  345. });
  346. my $form = CGI::FormBuilder->new(
  347. fields => \@fields,
  348. header => 1,
  349. charset => "utf-8",
  350. method => 'POST',
  351. validate => {
  352. editcontent => '/.+/',
  353. },
  354. required => [qw{editcontent}],
  355. javascript => 0,
  356. params => $q,
  357. action => $config{cgiurl},
  358. table => 0,
  359. template => $renderer,
  360. );
  361. decode_form_utf8($form);
  362. # This untaint is safe because titlepage removes any problematic
  363. # characters.
  364. my ($page)=$form->field('page');
  365. $page=titlepage(possibly_foolish_untaint($page));
  366. if (! defined $page || ! length $page ||
  367. $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
  368. error("bad page name");
  369. }
  370. my $from;
  371. if (defined $form->field('from')) {
  372. ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
  373. }
  374. my $file;
  375. my $type;
  376. if (exists $pagesources{$page}) {
  377. $file=$pagesources{$page};
  378. $type=pagetype($file);
  379. }
  380. else {
  381. $type=$form->param('type');
  382. if (defined $type && length $type && $hooks{htmlize}{$type}) {
  383. $type=possibly_foolish_untaint($type);
  384. }
  385. elsif (defined $from) {
  386. # favor the type of linking page
  387. $type=pagetype($pagesources{$from});
  388. }
  389. $type=$config{default_pageext} unless defined $type;
  390. $file=$page.".".$type;
  391. }
  392. my $newfile=0;
  393. if (! -e "$config{srcdir}/$file") {
  394. $newfile=1;
  395. }
  396. $form->field(name => "do", type => 'hidden');
  397. $form->field(name => "from", type => 'hidden');
  398. $form->field(name => "rcsinfo", type => 'hidden');
  399. $form->field(name => "subpage", type => 'hidden');
  400. $form->field(name => "page", value => $page, force => 1);
  401. $form->field(name => "type", value => $type, force => 1);
  402. $form->field(name => "comments", type => "text", size => 80);
  403. $form->field(name => "editcontent", type => "textarea", rows => 20,
  404. cols => 80);
  405. $form->tmpl_param("can_commit", $config{rcs});
  406. $form->tmpl_param("indexlink", indexlink());
  407. $form->tmpl_param("helponformattinglink",
  408. htmllink("", "", "HelpOnFormatting", 1));
  409. $form->tmpl_param("baseurl", baseurl());
  410. if (! $form->submitted) {
  411. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  412. force => 1);
  413. }
  414. if ($form->submitted eq "Cancel") {
  415. if ($newfile && defined $from) {
  416. redirect($q, "$config{url}/".htmlpage($from));
  417. }
  418. elsif ($newfile) {
  419. redirect($q, $config{url});
  420. }
  421. else {
  422. redirect($q, "$config{url}/".htmlpage($page));
  423. }
  424. return;
  425. }
  426. elsif ($form->submitted eq "Preview") {
  427. my $content=$form->field('editcontent');
  428. my $comments=$form->field('comments');
  429. $form->field(name => "editcontent",
  430. value => $content, force => 1);
  431. $form->field(name => "comments",
  432. value => $comments, force => 1);
  433. $config{rss}=$config{atom}=0; # avoid preview writing a feed!
  434. $form->tmpl_param("page_preview",
  435. htmlize($page, $type,
  436. linkify($page, "",
  437. preprocess($page, $page,
  438. filter($page, $content)))));
  439. }
  440. else {
  441. $form->tmpl_param("page_preview", "");
  442. }
  443. $form->tmpl_param("page_conflict", "");
  444. if (! $form->submitted || $form->submitted eq "Preview" ||
  445. ! $form->validate) {
  446. if ($form->field("do") eq "create") {
  447. my @page_locs;
  448. my $best_loc;
  449. if (! defined $from || ! length $from ||
  450. $from ne $form->field('from') ||
  451. $from=~/$config{wiki_file_prune_regexp}/ ||
  452. $from=~/^\// ||
  453. $form->submitted eq "Preview") {
  454. @page_locs=$best_loc=$page;
  455. }
  456. else {
  457. my $dir=$from."/";
  458. $dir=~s![^/]+/+$!!;
  459. if ((defined $form->field('subpage') && length $form->field('subpage')) ||
  460. $page eq 'discussion') {
  461. $best_loc="$from/$page";
  462. }
  463. else {
  464. $best_loc=$dir.$page;
  465. }
  466. push @page_locs, $dir.$page;
  467. push @page_locs, "$from/$page";
  468. while (length $dir) {
  469. $dir=~s![^/]+/+$!!;
  470. push @page_locs, $dir.$page;
  471. }
  472. }
  473. @page_locs = grep {
  474. ! exists $pagecase{lc $_} &&
  475. ! page_locked($_, $session, 1)
  476. } @page_locs;
  477. if (! @page_locs) {
  478. # hmm, someone else made the page in the
  479. # meantime?
  480. redirect($q, "$config{url}/".htmlpage($page));
  481. return;
  482. }
  483. my @page_types;
  484. if (exists $hooks{htmlize}) {
  485. @page_types=keys %{$hooks{htmlize}};
  486. }
  487. $form->tmpl_param("page_select", 1);
  488. $form->field(name => "page", type => 'select',
  489. options => \@page_locs, value => $best_loc);
  490. $form->field(name => "type", type => 'select',
  491. options => \@page_types);
  492. $form->title("creating ".pagetitle($page));
  493. }
  494. elsif ($form->field("do") eq "edit") {
  495. page_locked($page, $session);
  496. if (! defined $form->field('editcontent') ||
  497. ! length $form->field('editcontent')) {
  498. my $content="";
  499. if (exists $pagesources{$page}) {
  500. $content=readfile(srcfile($pagesources{$page}));
  501. $content=~s/\n/\r\n/g;
  502. }
  503. $form->field(name => "editcontent", value => $content,
  504. force => 1);
  505. }
  506. $form->tmpl_param("page_select", 0);
  507. $form->field(name => "page", type => 'hidden');
  508. $form->field(name => "type", type => 'hidden');
  509. $form->title("editing ".pagetitle($page));
  510. }
  511. print $form->render(submit => \@buttons);
  512. }
  513. else {
  514. # save page
  515. page_locked($page, $session);
  516. my $content=$form->field('editcontent');
  517. $content=~s/\r\n/\n/g;
  518. $content=~s/\r/\n/g;
  519. writefile($file, $config{srcdir}, $content);
  520. my $message="web commit ";
  521. if (defined $session->param("name") &&
  522. length $session->param("name")) {
  523. $message.="by ".$session->param("name");
  524. }
  525. else {
  526. $message.="from $ENV{REMOTE_ADDR}";
  527. }
  528. if (defined $form->field('comments') &&
  529. length $form->field('comments')) {
  530. $message.=": ".$form->field('comments');
  531. }
  532. if ($config{rcs}) {
  533. if ($newfile) {
  534. rcs_add($file);
  535. }
  536. # prevent deadlock with post-commit hook
  537. unlockwiki();
  538. # presumably the commit will trigger an update
  539. # of the wiki
  540. my $conflict=rcs_commit($file, $message,
  541. $form->field("rcsinfo"));
  542. if (defined $conflict) {
  543. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  544. force => 1);
  545. $form->tmpl_param("page_conflict", 1);
  546. $form->field("editcontent", value => $conflict, force => 1);
  547. $form->field(name => "comments", value => $form->field('comments'), force => 1);
  548. $form->field("do", "edit)");
  549. $form->tmpl_param("page_select", 0);
  550. $form->field(name => "page", type => 'hidden');
  551. $form->field(name => "type", type => 'hidden');
  552. $form->title("editing $page");
  553. print $form->render(submit => \@buttons);
  554. return;
  555. }
  556. }
  557. else {
  558. require IkiWiki::Render;
  559. refresh();
  560. saveindex();
  561. }
  562. # The trailing question mark tries to avoid broken
  563. # caches and get the most recent version of the page.
  564. redirect($q, "$config{url}/".htmlpage($page)."?updated");
  565. }
  566. } #}}}
  567. sub cgi () { #{{{
  568. eval q{use CGI};
  569. eval q{use CGI::Session};
  570. my $q=CGI->new;
  571. run_hooks(cgi => sub { shift->($q) });
  572. my $do=$q->param('do');
  573. if (! defined $do || ! length $do) {
  574. my $error = $q->cgi_error;
  575. if ($error) {
  576. error("Request not processed: $error");
  577. }
  578. else {
  579. error("\"do\" parameter missing");
  580. }
  581. }
  582. # Things that do not need a session.
  583. if ($do eq 'recentchanges') {
  584. cgi_recentchanges($q);
  585. return;
  586. }
  587. elsif ($do eq 'hyperestraier') {
  588. cgi_hyperestraier();
  589. }
  590. CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
  591. my $oldmask=umask(077);
  592. my $session = CGI::Session->new("driver:DB_File", $q,
  593. { FileName => "$config{wikistatedir}/sessions.db" });
  594. umask($oldmask);
  595. # Everything below this point needs the user to be signed in.
  596. if (((! $config{anonok} || $do eq 'prefs') &&
  597. (! $config{httpauth}) &&
  598. (! defined $session->param("name") ||
  599. ! userinfo_get($session->param("name"), "regdate"))) || $do eq 'signin') {
  600. cgi_signin($q, $session);
  601. # Force session flush with safe umask.
  602. my $oldmask=umask(077);
  603. $session->flush;
  604. umask($oldmask);
  605. return;
  606. }
  607. if ($config{httpauth} && (! defined $session->param("name"))) {
  608. if (! defined $q->remote_user()) {
  609. error("Could not determine authenticated username.");
  610. }
  611. else {
  612. $session->param("name", $q->remote_user());
  613. if (! userinfo_get($session->param("name"), "regdate")) {
  614. userinfo_setall($session->param("name"), {
  615. email => "",
  616. password => "",
  617. regdate=>time,
  618. });
  619. }
  620. }
  621. }
  622. if (userinfo_get($session->param("name"), "banned")) {
  623. print $q->header(-status => "403 Forbidden");
  624. $session->delete();
  625. print "You are banned.";
  626. exit;
  627. }
  628. if ($do eq 'create' || $do eq 'edit') {
  629. cgi_editpage($q, $session);
  630. }
  631. elsif ($do eq 'prefs') {
  632. cgi_prefs($q, $session);
  633. }
  634. elsif ($do eq 'blog') {
  635. my $page=titlepage(decode_utf8($q->param('title')));
  636. # if the page already exists, munge it to be unique
  637. my $from=$q->param('from');
  638. my $add="";
  639. while (exists $pagecase{lc "$from/$page$add"}) {
  640. $add=1 unless length $add;
  641. $add++;
  642. }
  643. $q->param('page', $page.$add);
  644. # now run same as create
  645. $q->param('do', 'create');
  646. cgi_editpage($q, $session);
  647. }
  648. else {
  649. error("unknown do parameter");
  650. }
  651. } #}}}
  652. 1