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