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