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