summaryrefslogtreecommitdiff
path: root/IkiWiki/CGI.pm
blob: f07a4e5a2c7941561d3d1416b13de3cf47d30feb (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. redirect($q, "$config{url}/".htmlpage($page));
  399. return;
  400. }
  401. elsif ($form->submitted eq "Preview") {
  402. my $content=$form->field('editcontent');
  403. my $comments=$form->field('comments');
  404. $form->field(name => "editcontent",
  405. value => $content, force => 1);
  406. $form->field(name => "comments",
  407. value => $comments, force => 1);
  408. $config{rss}=0; # avoid preview writing an rss feed!
  409. $form->tmpl_param("page_preview",
  410. htmlize($page, $type,
  411. linkify($page, "",
  412. preprocess($page, $page,
  413. filter($page, $content)))));
  414. }
  415. else {
  416. $form->tmpl_param("page_preview", "");
  417. }
  418. $form->tmpl_param("page_conflict", "");
  419. if (! $form->submitted || $form->submitted eq "Preview" ||
  420. ! $form->validate) {
  421. if ($form->field("do") eq "create") {
  422. my @page_locs;
  423. my $best_loc;
  424. if (! defined $from || ! length $from ||
  425. $from ne $form->field('from') ||
  426. $from=~/$config{wiki_file_prune_regexp}/ ||
  427. $from=~/^\// ||
  428. $form->submitted eq "Preview") {
  429. @page_locs=$best_loc=$page;
  430. }
  431. else {
  432. my $dir=$from."/";
  433. $dir=~s![^/]+/+$!!;
  434. if ((defined $form->field('subpage') && length $form->field('subpage')) ||
  435. $page eq 'discussion') {
  436. $best_loc="$from/$page";
  437. }
  438. else {
  439. $best_loc=$dir.$page;
  440. }
  441. push @page_locs, $dir.$page;
  442. push @page_locs, "$from/$page";
  443. while (length $dir) {
  444. $dir=~s![^/]+/+$!!;
  445. push @page_locs, $dir.$page;
  446. }
  447. }
  448. @page_locs = grep {
  449. ! exists $pagecase{lc $_} &&
  450. ! page_locked($_, $session, 1)
  451. } @page_locs;
  452. if (! @page_locs) {
  453. # hmm, someone else made the page in the
  454. # meantime?
  455. redirect($q, "$config{url}/".htmlpage($page));
  456. return;
  457. }
  458. my @page_types;
  459. if (exists $hooks{htmlize}) {
  460. @page_types=keys %{$hooks{htmlize}};
  461. }
  462. $form->tmpl_param("page_select", 1);
  463. $form->field(name => "page", type => 'select',
  464. options => \@page_locs, value => $best_loc);
  465. $form->field(name => "type", type => 'select',
  466. options => \@page_types);
  467. $form->title("creating ".pagetitle($page));
  468. }
  469. elsif ($form->field("do") eq "edit") {
  470. page_locked($page, $session);
  471. if (! defined $form->field('editcontent') ||
  472. ! length $form->field('editcontent')) {
  473. my $content="";
  474. if (exists $pagesources{$page}) {
  475. $content=readfile(srcfile($pagesources{$page}));
  476. $content=~s/\n/\r\n/g;
  477. }
  478. $form->field(name => "editcontent", value => $content,
  479. force => 1);
  480. }
  481. $form->tmpl_param("page_select", 0);
  482. $form->field(name => "page", type => 'hidden');
  483. $form->field(name => "type", type => 'hidden');
  484. $form->title("editing ".pagetitle($page));
  485. }
  486. print $form->render(submit => \@buttons);
  487. }
  488. else {
  489. # save page
  490. page_locked($page, $session);
  491. my $content=$form->field('editcontent');
  492. $content=~s/\r\n/\n/g;
  493. $content=~s/\r/\n/g;
  494. writefile($file, $config{srcdir}, $content);
  495. my $message="web commit ";
  496. if (defined $session->param("name") &&
  497. length $session->param("name")) {
  498. $message.="by ".$session->param("name");
  499. }
  500. else {
  501. $message.="from $ENV{REMOTE_ADDR}";
  502. }
  503. if (defined $form->field('comments') &&
  504. length $form->field('comments')) {
  505. $message.=": ".$form->field('comments');
  506. }
  507. if ($config{rcs}) {
  508. if ($newfile) {
  509. rcs_add($file);
  510. }
  511. # prevent deadlock with post-commit hook
  512. unlockwiki();
  513. # presumably the commit will trigger an update
  514. # of the wiki
  515. my $conflict=rcs_commit($file, $message,
  516. $form->field("rcsinfo"));
  517. if (defined $conflict) {
  518. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  519. force => 1);
  520. $form->tmpl_param("page_conflict", 1);
  521. $form->field("editcontent", value => $conflict, force => 1);
  522. $form->field(name => "comments", value => $form->field('comments'), force => 1);
  523. $form->field("do", "edit)");
  524. $form->tmpl_param("page_select", 0);
  525. $form->field(name => "page", type => 'hidden');
  526. $form->field(name => "type", type => 'hidden');
  527. $form->title("editing $page");
  528. print $form->render(submit => \@buttons);
  529. return;
  530. }
  531. }
  532. else {
  533. require IkiWiki::Render;
  534. refresh();
  535. saveindex();
  536. }
  537. # The trailing question mark tries to avoid broken
  538. # caches and get the most recent version of the page.
  539. redirect($q, "$config{url}/".htmlpage($page)."?updated");
  540. }
  541. } #}}}
  542. sub cgi () { #{{{
  543. eval q{use CGI};
  544. eval q{use CGI::Session};
  545. my $q=CGI->new;
  546. run_hooks(cgi => sub { shift->($q) });
  547. my $do=$q->param('do');
  548. if (! defined $do || ! length $do) {
  549. my $error = $q->cgi_error;
  550. if ($error) {
  551. error("Request not processed: $error");
  552. }
  553. else {
  554. error("\"do\" parameter missing");
  555. }
  556. }
  557. # Things that do not need a session.
  558. if ($do eq 'recentchanges') {
  559. cgi_recentchanges($q);
  560. return;
  561. }
  562. elsif ($do eq 'hyperestraier') {
  563. cgi_hyperestraier();
  564. }
  565. CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
  566. my $oldmask=umask(077);
  567. my $session = CGI::Session->new("driver:DB_File", $q,
  568. { FileName => "$config{wikistatedir}/sessions.db" });
  569. umask($oldmask);
  570. # Everything below this point needs the user to be signed in.
  571. if (((! $config{anonok} || $do eq 'prefs') &&
  572. (! defined $session->param("name") ||
  573. ! userinfo_get($session->param("name"), "regdate"))) || $do eq 'signin') {
  574. cgi_signin($q, $session);
  575. # Force session flush with safe umask.
  576. my $oldmask=umask(077);
  577. $session->flush;
  578. umask($oldmask);
  579. return;
  580. }
  581. if ($do eq 'create' || $do eq 'edit') {
  582. cgi_editpage($q, $session);
  583. }
  584. elsif ($do eq 'prefs') {
  585. cgi_prefs($q, $session);
  586. }
  587. elsif ($do eq 'blog') {
  588. my $page=titlepage(decode_utf8($q->param('title')));
  589. # if the page already exists, munge it to be unique
  590. my $from=$q->param('from');
  591. my $add="";
  592. while (exists $pagecase{lc "$from/$page$add"}) {
  593. $add=1 unless length $add;
  594. $add++;
  595. }
  596. $q->param('page', $page.$add);
  597. # now run same as create
  598. $q->param('do', 'create');
  599. cgi_editpage($q, $session);
  600. }
  601. else {
  602. error("unknown do parameter");
  603. }
  604. } #}}}
  605. 1