summaryrefslogtreecommitdiff
path: root/IkiWiki/CGI.pm
blob: fe89e2758b924cdf8063eb1d03ea3f082be360dc (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. if (! is_admin($user_name)) {
  288. $form->field(name => "locked_pages", type => "hidden");
  289. }
  290. if ($config{httpauth}) {
  291. $form->field(name => "password", type => "hidden");
  292. $form->field(name => "confirm_password", type => "hidden");
  293. }
  294. if (! $form->submitted) {
  295. $form->field(name => "email", force => 1,
  296. value => userinfo_get($user_name, "email"));
  297. $form->field(name => "subscriptions", force => 1,
  298. value => userinfo_get($user_name, "subscriptions"));
  299. $form->field(name => "locked_pages", force => 1,
  300. value => userinfo_get($user_name, "locked_pages"));
  301. }
  302. decode_form_utf8($form);
  303. if ($form->submitted eq 'Logout') {
  304. $session->delete();
  305. redirect($q, $config{url});
  306. return;
  307. }
  308. elsif ($form->submitted eq 'Cancel') {
  309. redirect($q, $config{url});
  310. return;
  311. }
  312. elsif ($form->submitted eq "Save Preferences" && $form->validate) {
  313. foreach my $field (qw(password email subscriptions locked_pages)) {
  314. if (length $form->field($field)) {
  315. userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
  316. }
  317. }
  318. $form->text("Preferences saved.");
  319. }
  320. printheader($session);
  321. print misctemplate($form->title, $form->render(submit => \@buttons));
  322. } #}}}
  323. sub cgi_editpage ($$) { #{{{
  324. my $q=shift;
  325. my $session=shift;
  326. my @fields=qw(do rcsinfo subpage from page type editcontent comments);
  327. my @buttons=("Save Page", "Preview", "Cancel");
  328. eval q{use CGI::FormBuilder; use CGI::FormBuilder::Template::HTML};
  329. my $renderer=CGI::FormBuilder::Template::HTML->new(
  330. fields => \@fields,
  331. template_params("editpage.tmpl"),
  332. );
  333. run_hooks(pagetemplate => sub {
  334. shift->(page => "", destpage => "", template => $renderer->engine);
  335. });
  336. my $form = CGI::FormBuilder->new(
  337. fields => \@fields,
  338. header => 1,
  339. charset => "utf-8",
  340. method => 'POST',
  341. validate => {
  342. editcontent => '/.+/',
  343. },
  344. required => [qw{editcontent}],
  345. javascript => 0,
  346. params => $q,
  347. action => $config{cgiurl},
  348. table => 0,
  349. template => $renderer,
  350. );
  351. decode_form_utf8($form);
  352. # This untaint is safe because titlepage removes any problematic
  353. # characters.
  354. my ($page)=$form->field('page');
  355. $page=titlepage(possibly_foolish_untaint($page));
  356. if (! defined $page || ! length $page ||
  357. $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
  358. error("bad page name");
  359. }
  360. my $from;
  361. if (defined $form->field('from')) {
  362. ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
  363. }
  364. my $file;
  365. my $type;
  366. if (exists $pagesources{$page}) {
  367. $file=$pagesources{$page};
  368. $type=pagetype($file);
  369. }
  370. else {
  371. $type=$form->param('type');
  372. if (defined $type && length $type && $hooks{htmlize}{$type}) {
  373. $type=possibly_foolish_untaint($type);
  374. }
  375. elsif (defined $from) {
  376. # favor the type of linking page
  377. $type=pagetype($pagesources{$from});
  378. }
  379. $type=$config{default_pageext} unless defined $type;
  380. $file=$page.".".$type;
  381. }
  382. my $newfile=0;
  383. if (! -e "$config{srcdir}/$file") {
  384. $newfile=1;
  385. }
  386. $form->field(name => "do", type => 'hidden');
  387. $form->field(name => "from", type => 'hidden');
  388. $form->field(name => "rcsinfo", type => 'hidden');
  389. $form->field(name => "subpage", type => 'hidden');
  390. $form->field(name => "page", value => $page, force => 1);
  391. $form->field(name => "type", value => $type, force => 1);
  392. $form->field(name => "comments", type => "text", size => 80);
  393. $form->field(name => "editcontent", type => "textarea", rows => 20,
  394. cols => 80);
  395. $form->tmpl_param("can_commit", $config{rcs});
  396. $form->tmpl_param("indexlink", indexlink());
  397. $form->tmpl_param("helponformattinglink",
  398. htmllink("", "", "HelpOnFormatting", 1));
  399. $form->tmpl_param("baseurl", baseurl());
  400. if (! $form->submitted) {
  401. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  402. force => 1);
  403. }
  404. if ($form->submitted eq "Cancel") {
  405. if ($newfile && defined $from) {
  406. redirect($q, "$config{url}/".htmlpage($from));
  407. }
  408. elsif ($newfile) {
  409. redirect($q, $config{url});
  410. }
  411. else {
  412. redirect($q, "$config{url}/".htmlpage($page));
  413. }
  414. return;
  415. }
  416. elsif ($form->submitted eq "Preview") {
  417. my $content=$form->field('editcontent');
  418. my $comments=$form->field('comments');
  419. $form->field(name => "editcontent",
  420. value => $content, force => 1);
  421. $form->field(name => "comments",
  422. value => $comments, force => 1);
  423. $config{rss}=$config{atom}=0; # avoid preview writing a feed!
  424. $form->tmpl_param("page_preview",
  425. htmlize($page, $type,
  426. linkify($page, "",
  427. preprocess($page, $page,
  428. filter($page, $content)))));
  429. }
  430. else {
  431. $form->tmpl_param("page_preview", "");
  432. }
  433. $form->tmpl_param("page_conflict", "");
  434. if (! $form->submitted || $form->submitted eq "Preview" ||
  435. ! $form->validate) {
  436. if ($form->field("do") eq "create") {
  437. my @page_locs;
  438. my $best_loc;
  439. if (! defined $from || ! length $from ||
  440. $from ne $form->field('from') ||
  441. $from=~/$config{wiki_file_prune_regexp}/ ||
  442. $from=~/^\// ||
  443. $form->submitted eq "Preview") {
  444. @page_locs=$best_loc=$page;
  445. }
  446. else {
  447. my $dir=$from."/";
  448. $dir=~s![^/]+/+$!!;
  449. if ((defined $form->field('subpage') && length $form->field('subpage')) ||
  450. $page eq 'discussion') {
  451. $best_loc="$from/$page";
  452. }
  453. else {
  454. $best_loc=$dir.$page;
  455. }
  456. push @page_locs, $dir.$page;
  457. push @page_locs, "$from/$page";
  458. while (length $dir) {
  459. $dir=~s![^/]+/+$!!;
  460. push @page_locs, $dir.$page;
  461. }
  462. }
  463. @page_locs = grep {
  464. ! exists $pagecase{lc $_} &&
  465. ! page_locked($_, $session, 1)
  466. } @page_locs;
  467. if (! @page_locs) {
  468. # hmm, someone else made the page in the
  469. # meantime?
  470. redirect($q, "$config{url}/".htmlpage($page));
  471. return;
  472. }
  473. my @page_types;
  474. if (exists $hooks{htmlize}) {
  475. @page_types=keys %{$hooks{htmlize}};
  476. }
  477. $form->tmpl_param("page_select", 1);
  478. $form->field(name => "page", type => 'select',
  479. options => \@page_locs, value => $best_loc);
  480. $form->field(name => "type", type => 'select',
  481. options => \@page_types);
  482. $form->title("creating ".pagetitle($page));
  483. }
  484. elsif ($form->field("do") eq "edit") {
  485. page_locked($page, $session);
  486. if (! defined $form->field('editcontent') ||
  487. ! length $form->field('editcontent')) {
  488. my $content="";
  489. if (exists $pagesources{$page}) {
  490. $content=readfile(srcfile($pagesources{$page}));
  491. $content=~s/\n/\r\n/g;
  492. }
  493. $form->field(name => "editcontent", value => $content,
  494. force => 1);
  495. }
  496. $form->tmpl_param("page_select", 0);
  497. $form->field(name => "page", type => 'hidden');
  498. $form->field(name => "type", type => 'hidden');
  499. $form->title("editing ".pagetitle($page));
  500. }
  501. print $form->render(submit => \@buttons);
  502. }
  503. else {
  504. # save page
  505. page_locked($page, $session);
  506. my $content=$form->field('editcontent');
  507. $content=~s/\r\n/\n/g;
  508. $content=~s/\r/\n/g;
  509. writefile($file, $config{srcdir}, $content);
  510. my $message="web commit ";
  511. if (defined $session->param("name") &&
  512. length $session->param("name")) {
  513. $message.="by ".$session->param("name");
  514. }
  515. else {
  516. $message.="from $ENV{REMOTE_ADDR}";
  517. }
  518. if (defined $form->field('comments') &&
  519. length $form->field('comments')) {
  520. $message.=": ".$form->field('comments');
  521. }
  522. if ($config{rcs}) {
  523. if ($newfile) {
  524. rcs_add($file);
  525. }
  526. # prevent deadlock with post-commit hook
  527. unlockwiki();
  528. # presumably the commit will trigger an update
  529. # of the wiki
  530. my $conflict=rcs_commit($file, $message,
  531. $form->field("rcsinfo"));
  532. if (defined $conflict) {
  533. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  534. force => 1);
  535. $form->tmpl_param("page_conflict", 1);
  536. $form->field("editcontent", value => $conflict, force => 1);
  537. $form->field(name => "comments", value => $form->field('comments'), force => 1);
  538. $form->field("do", "edit)");
  539. $form->tmpl_param("page_select", 0);
  540. $form->field(name => "page", type => 'hidden');
  541. $form->field(name => "type", type => 'hidden');
  542. $form->title("editing $page");
  543. print $form->render(submit => \@buttons);
  544. return;
  545. }
  546. }
  547. else {
  548. require IkiWiki::Render;
  549. refresh();
  550. saveindex();
  551. }
  552. # The trailing question mark tries to avoid broken
  553. # caches and get the most recent version of the page.
  554. redirect($q, "$config{url}/".htmlpage($page)."?updated");
  555. }
  556. } #}}}
  557. sub cgi () { #{{{
  558. eval q{use CGI};
  559. eval q{use CGI::Session};
  560. my $q=CGI->new;
  561. run_hooks(cgi => sub { shift->($q) });
  562. my $do=$q->param('do');
  563. if (! defined $do || ! length $do) {
  564. my $error = $q->cgi_error;
  565. if ($error) {
  566. error("Request not processed: $error");
  567. }
  568. else {
  569. error("\"do\" parameter missing");
  570. }
  571. }
  572. # Things that do not need a session.
  573. if ($do eq 'recentchanges') {
  574. cgi_recentchanges($q);
  575. return;
  576. }
  577. elsif ($do eq 'hyperestraier') {
  578. cgi_hyperestraier();
  579. }
  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. # Everything below this point needs the user to be signed in.
  586. if (((! $config{anonok} || $do eq 'prefs') &&
  587. (! $config{httpauth}) &&
  588. (! defined $session->param("name") ||
  589. ! userinfo_get($session->param("name"), "regdate"))) || $do eq 'signin') {
  590. cgi_signin($q, $session);
  591. # Force session flush with safe umask.
  592. my $oldmask=umask(077);
  593. $session->flush;
  594. umask($oldmask);
  595. return;
  596. }
  597. if ($config{httpauth} && (! defined $session->param("name"))) {
  598. if (! defined $q->remote_user()) {
  599. error("Could not determine authenticated username.");
  600. }
  601. else {
  602. $session->param("name", $q->remote_user());
  603. if (!userinfo_get($session->param("name"),"regdate")) {
  604. userinfo_setall($session->param("name"), {
  605. email => "",
  606. password => "",
  607. regdate=>time,
  608. });
  609. }
  610. }
  611. }
  612. if ($do eq 'create' || $do eq 'edit') {
  613. cgi_editpage($q, $session);
  614. }
  615. elsif ($do eq 'prefs') {
  616. cgi_prefs($q, $session);
  617. }
  618. elsif ($do eq 'blog') {
  619. my $page=titlepage(decode_utf8($q->param('title')));
  620. # if the page already exists, munge it to be unique
  621. my $from=$q->param('from');
  622. my $add="";
  623. while (exists $pagecase{lc "$from/$page$add"}) {
  624. $add=1 unless length $add;
  625. $add++;
  626. }
  627. $q->param('page', $page.$add);
  628. # now run same as create
  629. $q->param('do', 'create');
  630. cgi_editpage($q, $session);
  631. }
  632. else {
  633. error("unknown do parameter");
  634. }
  635. } #}}}
  636. 1