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