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