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