summaryrefslogtreecommitdiff
path: root/IkiWiki/CGI.pm
blob: 3e4cbe4cf4a97598b42bf03fad16ff54b2269423 (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 page_locked ($$;$) { #{{{
  10. my $page=shift;
  11. my $session=shift;
  12. my $nonfatal=shift;
  13. my $user=$session->param("name");
  14. return if defined $user && is_admin($user);
  15. foreach my $admin (@{$config{adminuser}}) {
  16. my $locked_pages=userinfo_get($admin, "locked_pages");
  17. if (globlist_match($page, userinfo_get($admin, "locked_pages"))) {
  18. return 1 if $nonfatal;
  19. error(htmllink("", "", $page, 1)." is locked by ".
  20. htmllink("", "", $admin, 1)." and cannot be edited.");
  21. }
  22. }
  23. return 0;
  24. } #}}}
  25. sub cgi_recentchanges ($) { #{{{
  26. my $q=shift;
  27. unlockwiki();
  28. my $template=template("recentchanges.tmpl");
  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. print $q->header(-charset=>'utf-8'), $template->output;
  38. } #}}}
  39. sub cgi_signin ($$) { #{{{
  40. my $q=shift;
  41. my $session=shift;
  42. eval q{use CGI::FormBuilder};
  43. my $form = CGI::FormBuilder->new(
  44. title => "signin",
  45. fields => [qw(do title page subpage from name password confirm_password email)],
  46. header => 1,
  47. charset => "utf-8",
  48. method => 'POST',
  49. validate => {
  50. confirm_password => {
  51. perl => q{eq $form->field("password")},
  52. },
  53. email => 'EMAIL',
  54. },
  55. required => 'NONE',
  56. javascript => 0,
  57. params => $q,
  58. action => $config{cgiurl},
  59. header => 0,
  60. template => (-e "$config{templatedir}/signin.tmpl" ?
  61. {template_params("signin.tmpl")} : ""),
  62. stylesheet => styleurl(),
  63. );
  64. $form->field(name => "name", required => 0);
  65. $form->field(name => "do", type => "hidden");
  66. $form->field(name => "page", type => "hidden");
  67. $form->field(name => "title", type => "hidden");
  68. $form->field(name => "from", type => "hidden");
  69. $form->field(name => "subpage", type => "hidden");
  70. $form->field(name => "password", type => "password", required => 0);
  71. $form->field(name => "confirm_password", type => "password", required => 0);
  72. $form->field(name => "email", required => 0);
  73. if ($q->param("do") ne "signin") {
  74. $form->text("You need to log in first.");
  75. }
  76. if ($form->submitted) {
  77. # Set required fields based on how form was submitted.
  78. my %required=(
  79. "Login" => [qw(name password)],
  80. "Register" => [qw(name password confirm_password email)],
  81. "Mail Password" => [qw(name)],
  82. );
  83. foreach my $opt (@{$required{$form->submitted}}) {
  84. $form->field(name => $opt, required => 1);
  85. }
  86. # Validate password differently depending on how
  87. # form was submitted.
  88. if ($form->submitted eq 'Login') {
  89. $form->field(
  90. name => "password",
  91. validate => sub {
  92. length $form->field("name") &&
  93. shift eq userinfo_get($form->field("name"), 'password');
  94. },
  95. );
  96. $form->field(name => "name", validate => '/^\w+$/');
  97. }
  98. else {
  99. $form->field(name => "password", validate => 'VALUE');
  100. }
  101. # And make sure the entered name exists when logging
  102. # in or sending email, and does not when registering.
  103. if ($form->submitted eq 'Register') {
  104. $form->field(
  105. name => "name",
  106. validate => sub {
  107. my $name=shift;
  108. length $name &&
  109. $name=~/$config{wiki_file_regexp}/ &&
  110. ! userinfo_get($name, "regdate");
  111. },
  112. );
  113. }
  114. else {
  115. $form->field(
  116. name => "name",
  117. validate => sub {
  118. my $name=shift;
  119. length $name &&
  120. userinfo_get($name, "regdate");
  121. },
  122. );
  123. }
  124. }
  125. else {
  126. # First time settings.
  127. $form->field(name => "name", comment => "use FirstnameLastName");
  128. $form->field(name => "confirm_password", comment => "(only needed");
  129. $form->field(name => "email", comment => "for registration)");
  130. if ($session->param("name")) {
  131. $form->field(name => "name", value => $session->param("name"));
  132. }
  133. }
  134. if ($form->submitted && $form->validate) {
  135. if ($form->submitted eq 'Login') {
  136. $session->param("name", $form->field("name"));
  137. if (defined $form->field("do") &&
  138. $form->field("do") ne 'signin') {
  139. print $q->redirect(cgiurl(
  140. do => $form->field("do"),
  141. page => $form->field("page"),
  142. title => $form->field("title"),
  143. subpage => $form->field("subpage"),
  144. from => $form->field("from"),
  145. ));
  146. }
  147. else {
  148. print $q->redirect($config{url});
  149. }
  150. }
  151. elsif ($form->submitted eq 'Register') {
  152. my $user_name=$form->field('name');
  153. if (userinfo_setall($user_name, {
  154. 'email' => $form->field('email'),
  155. 'password' => $form->field('password'),
  156. 'regdate' => time
  157. })) {
  158. $form->field(name => "confirm_password", type => "hidden");
  159. $form->field(name => "email", type => "hidden");
  160. $form->text("Registration successful. Now you can Login.");
  161. print $session->header(-charset=>'utf-8');
  162. print misctemplate($form->title, $form->render(submit => ["Login"]));
  163. }
  164. else {
  165. error("Error saving registration.");
  166. }
  167. }
  168. elsif ($form->submitted eq 'Mail Password') {
  169. my $user_name=$form->field("name");
  170. my $template=template("passwordmail.tmpl");
  171. $template->param(
  172. user_name => $user_name,
  173. user_password => userinfo_get($user_name, "password"),
  174. wikiurl => $config{url},
  175. wikiname => $config{wikiname},
  176. REMOTE_ADDR => $ENV{REMOTE_ADDR},
  177. );
  178. eval q{use Mail::Sendmail};
  179. sendmail(
  180. To => userinfo_get($user_name, "email"),
  181. From => "$config{wikiname} admin <$config{adminemail}>",
  182. Subject => "$config{wikiname} information",
  183. Message => $template->output,
  184. ) or error("Failed to send mail");
  185. $form->text("Your password has been emailed to you.");
  186. $form->field(name => "name", required => 0);
  187. print $session->header(-charset=>'utf-8');
  188. print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
  189. }
  190. }
  191. else {
  192. print $session->header(-charset=>'utf-8');
  193. print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
  194. }
  195. } #}}}
  196. sub cgi_prefs ($$) { #{{{
  197. my $q=shift;
  198. my $session=shift;
  199. eval q{use CGI::FormBuilder};
  200. my $form = CGI::FormBuilder->new(
  201. title => "preferences",
  202. fields => [qw(do name password confirm_password email
  203. subscriptions locked_pages)],
  204. header => 0,
  205. charset => "utf-8",
  206. method => 'POST',
  207. validate => {
  208. confirm_password => {
  209. perl => q{eq $form->field("password")},
  210. },
  211. email => 'EMAIL',
  212. },
  213. required => 'NONE',
  214. javascript => 0,
  215. params => $q,
  216. action => $config{cgiurl},
  217. template => (-e "$config{templatedir}/prefs.tmpl" ?
  218. {template_params("prefs.tmpl")} : ""),
  219. stylesheet => styleurl(),
  220. );
  221. my @buttons=("Save Preferences", "Logout", "Cancel");
  222. my $user_name=$session->param("name");
  223. $form->field(name => "do", type => "hidden");
  224. $form->field(name => "name", disabled => 1,
  225. value => $user_name, force => 1);
  226. $form->field(name => "password", type => "password");
  227. $form->field(name => "confirm_password", type => "password");
  228. $form->field(name => "subscriptions", size => 50,
  229. comment => "(".htmllink("", "", "GlobList", 1).")");
  230. $form->field(name => "locked_pages", size => 50,
  231. comment => "(".htmllink("", "", "GlobList", 1).")");
  232. if (! is_admin($user_name)) {
  233. $form->field(name => "locked_pages", type => "hidden");
  234. }
  235. if (! $form->submitted) {
  236. $form->field(name => "email", force => 1,
  237. value => userinfo_get($user_name, "email"));
  238. $form->field(name => "subscriptions", force => 1,
  239. value => userinfo_get($user_name, "subscriptions"));
  240. $form->field(name => "locked_pages", force => 1,
  241. value => userinfo_get($user_name, "locked_pages"));
  242. }
  243. if ($form->submitted eq 'Logout') {
  244. $session->delete();
  245. print $q->redirect($config{url});
  246. return;
  247. }
  248. elsif ($form->submitted eq 'Cancel') {
  249. print $q->redirect($config{url});
  250. return;
  251. }
  252. elsif ($form->submitted eq "Save Preferences" && $form->validate) {
  253. foreach my $field (qw(password email subscriptions locked_pages)) {
  254. if (length $form->field($field)) {
  255. userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
  256. }
  257. }
  258. $form->text("Preferences saved.");
  259. }
  260. print $session->header(-charset=>'utf-8');
  261. print misctemplate($form->title, $form->render(submit => \@buttons));
  262. } #}}}
  263. sub cgi_editpage ($$) { #{{{
  264. my $q=shift;
  265. my $session=shift;
  266. eval q{use CGI::FormBuilder};
  267. my $form = CGI::FormBuilder->new(
  268. fields => [qw(do rcsinfo subpage from page editcontent comments)],
  269. header => 1,
  270. charset => "utf-8",
  271. method => 'POST',
  272. validate => {
  273. editcontent => '/.+/',
  274. },
  275. required => [qw{editcontent}],
  276. javascript => 0,
  277. params => $q,
  278. action => $config{cgiurl},
  279. table => 0,
  280. template => {template_params("editpage.tmpl")},
  281. );
  282. my @buttons=("Save Page", "Preview", "Cancel");
  283. # This untaint is safe because titlepage removes any problimatic
  284. # characters.
  285. my ($page)=Encode::decode_utf8($form->param('page'));
  286. $page=titlepage(possibly_foolish_untaint(lc($page)));
  287. if (! defined $page || ! length $page ||
  288. $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
  289. error("bad page name");
  290. }
  291. $page=lc($page);
  292. my $file;
  293. if (exists $pagesources{lc($page)}) {
  294. $file=$pagesources{lc($page)};
  295. }
  296. else {
  297. $file=$page.".".$config{default_pageext};
  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. my $content = Encode::decode_utf8($form->field('editcontent'));
  330. my $comments = Encode::decode_utf8($form->field('comments'));
  331. $form->field(name => "editcontent",
  332. value => $content, force => 1);
  333. $form->field(name => "comments",
  334. value => $comments, force => 1);
  335. $form->tmpl_param("page_preview",
  336. htmlize(pagetype($file),
  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. my $content=Encode::decode_utf8($form->field('editcontent'));
  411. $content=~s/\r\n/\n/g;
  412. $content=~s/\r/\n/g;
  413. writefile($file, $config{srcdir}, $content);
  414. my $message="web commit ";
  415. if (defined $session->param("name") &&
  416. length $session->param("name")) {
  417. $message.="by ".$session->param("name");
  418. }
  419. else {
  420. $message.="from $ENV{REMOTE_ADDR}";
  421. }
  422. if (defined $form->field('comments') &&
  423. length $form->field('comments')) {
  424. $message.=": ".Encode::decode_utf8($form->field('comments'));
  425. }
  426. if ($config{rcs}) {
  427. if ($newfile) {
  428. rcs_add($file);
  429. }
  430. # prevent deadlock with post-commit hook
  431. unlockwiki();
  432. # presumably the commit will trigger an update
  433. # of the wiki
  434. my $conflict=rcs_commit($file, $message,
  435. $form->field("rcsinfo"));
  436. if (defined $conflict) {
  437. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  438. force => 1);
  439. $form->tmpl_param("page_conflict", 1);
  440. $form->field("editcontent", value => $conflict, force => 1);
  441. $form->field(name => "comments", value => Encode::decode_utf8($form->field('comments')), 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