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