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