summaryrefslogtreecommitdiff
path: root/IkiWiki/CGI.pm
blob: 53237bcdb571c646b5d19d9403d1056cc58dfa8b (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 printheader ($) { #{{{
  10. my $session=shift;
  11. if ($config{sslcookie}) {
  12. print $session->header(-charset => 'utf-8',
  13. -cookie => $session->cookie(-secure => 1));
  14. } else {
  15. print $session->header(-charset => 'utf-8');
  16. }
  17. } #}}}
  18. sub redirect ($$) { #{{{
  19. my $q=shift;
  20. my $url=shift;
  21. if (! $config{w3mmode}) {
  22. print $q->redirect($url);
  23. }
  24. else {
  25. print "Content-type: text/plain\n";
  26. print "W3m-control: GOTO $url\n\n";
  27. }
  28. } #}}}
  29. sub page_locked ($$;$) { #{{{
  30. my $page=shift;
  31. my $session=shift;
  32. my $nonfatal=shift;
  33. my $user=$session->param("name");
  34. return if defined $user && is_admin($user);
  35. foreach my $admin (@{$config{adminuser}}) {
  36. my $locked_pages=userinfo_get($admin, "locked_pages");
  37. if (pagespec_match($page, userinfo_get($admin, "locked_pages"))) {
  38. return 1 if $nonfatal;
  39. error(htmllink("", "", $page, 1)." is locked by ".
  40. htmllink("", "", $admin, 1)." and cannot be edited.");
  41. }
  42. }
  43. return 0;
  44. } #}}}
  45. sub decode_form_utf8 ($) { #{{{
  46. my $form = shift;
  47. foreach my $f ($form->field) {
  48. next if Encode::is_utf8(scalar $form->field($f));
  49. $form->field(name => $f,
  50. value => decode_utf8($form->field($f)),
  51. force => 1,
  52. );
  53. }
  54. } #}}}
  55. sub cgi_recentchanges ($) { #{{{
  56. my $q=shift;
  57. unlockwiki();
  58. # Optimisation: building recentchanges means calculating lots of
  59. # links. Memoizing htmllink speeds it up a lot (can't be memoized
  60. # during page builds as the return values may change, but they
  61. # won't here.)
  62. eval q{use Memoize};
  63. error($@) if $@;
  64. memoize("htmllink");
  65. eval q{use Time::Duration};
  66. error($@) if $@;
  67. eval q{use CGI 'escapeHTML'};
  68. error($@) if $@;
  69. my $changelog=[rcs_recentchanges(100)];
  70. foreach my $change (@$changelog) {
  71. $change->{when} = concise(ago($change->{when}));
  72. if ($change->{user} =~ m!^https?://! &&
  73. eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
  74. # Munge user-urls, as used by eg, OpenID.
  75. my $oid=Net::OpenID::VerifiedIdentity->new(identity => $change->{user});
  76. $change->{user} = "<a href=\"".$change->{user}."\">".escapeHTML($oid->display)."</a>";
  77. }
  78. else {
  79. $change->{user} = htmllink("", "", escapeHTML($change->{user}), 1);
  80. }
  81. my $is_excess = exists $change->{pages}[10]; # limit pages to first 10
  82. delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
  83. $change->{pages} = [
  84. map {
  85. $_->{link} = htmllink("", "", $_->{page}, 1);
  86. $_;
  87. } @{$change->{pages}}
  88. ];
  89. push @{$change->{pages}}, { link => '...' } if $is_excess;
  90. }
  91. my $template=template("recentchanges.tmpl");
  92. $template->param(
  93. title => "RecentChanges",
  94. indexlink => indexlink(),
  95. wikiname => $config{wikiname},
  96. changelog => $changelog,
  97. baseurl => baseurl(),
  98. );
  99. run_hooks(pagetemplate => sub {
  100. shift->(page => "", destpage => "", template => $template);
  101. });
  102. print $q->header(-charset => 'utf-8'), $template->output;
  103. } #}}}
  104. sub cgi_signin ($$) { #{{{
  105. my $q=shift;
  106. my $session=shift;
  107. eval q{use CGI::FormBuilder};
  108. error($@) if $@;
  109. my $form = CGI::FormBuilder->new(
  110. title => "signin",
  111. fields => [qw(do name password openid_url)],
  112. header => 1,
  113. charset => "utf-8",
  114. method => 'POST',
  115. validate => {
  116. confirm_password => {
  117. perl => q{eq $form->field("password")},
  118. },
  119. email => 'EMAIL',
  120. },
  121. required => 'NONE',
  122. javascript => 0,
  123. params => $q,
  124. action => $config{cgiurl},
  125. header => 0,
  126. template => (-e "$config{templatedir}/signin.tmpl" ?
  127. {template_params("signin.tmpl")} : ""),
  128. stylesheet => baseurl()."style.css",
  129. );
  130. decode_form_utf8($form);
  131. $form->field(name => "name", required => 0, size => 30);
  132. $form->field(name => "do", type => "hidden");
  133. $form->field(name => "password", type => "password", required => 0);
  134. if ($config{openid}) {
  135. $form->field(name => "openid_url", label => "OpenID", size => 30,
  136. comment => '('.
  137. htmllink("", "", "OpenID", 1, 0, "What's this?")
  138. .($config{openidsignup} ? " | <a href=\"$config{openidsignup}\">Get an OpenID</a>" : "")
  139. .')');
  140. }
  141. else {
  142. $form->field(name => "openid_url", type => "hidden");
  143. }
  144. if ($form->submitted eq "Register" || $form->submitted eq "Create Account") {
  145. $form->title("register");
  146. $form->text("");
  147. $form->fields(qw(do name password confirm_password email));
  148. $form->field(name => "confirm_password", type => "password");
  149. $form->field(name => "email", type => "text");
  150. $form->field(name => "openid_url", type => "hidden");
  151. }
  152. if ($q->param("do") ne "signin" && !$form->submitted) {
  153. $form->text("You need to log in first.");
  154. }
  155. if ($form->submitted) {
  156. my $submittype=$form->submitted;
  157. # OpenID login uses the Login button, but validates
  158. # differently.
  159. if ($submittype eq "Login" && $config{openid} &&
  160. length $form->field("openid_url")) {
  161. $submittype="OpenID";
  162. $form->field(
  163. name => "openid_url",
  164. validate => sub {
  165. # FIXME: ugh
  166. IkiWiki::Plugin::openid::validate($q, $session, shift, $form);
  167. },
  168. );
  169. }
  170. # Set required fields based on how form was submitted.
  171. my %required=(
  172. "Login" => [qw(name password)],
  173. "Register" => [],
  174. "Create Account" => [qw(name password confirm_password email)],
  175. "Mail Password" => [qw(name)],
  176. "OpenID" => [qw(openid_url)],
  177. );
  178. foreach my $opt (@{$required{$submittype}}) {
  179. $form->field(name => $opt, required => 1);
  180. }
  181. # Validate password differently depending on how
  182. # form was submitted.
  183. if ($submittype eq 'Login') {
  184. $form->field(
  185. name => "password",
  186. validate => sub {
  187. length $form->field("name") &&
  188. shift eq userinfo_get($form->field("name"), 'password');
  189. },
  190. );
  191. $form->field(name => "name", validate => '/^\w+$/');
  192. }
  193. elsif ($submittype ne 'OpenID') {
  194. $form->field(name => "password", validate => 'VALUE');
  195. }
  196. # And make sure the entered name exists when logging
  197. # in or sending email, and does not when registering.
  198. if ($submittype eq 'Create Account' ||
  199. $submittype eq 'Register') {
  200. $form->field(
  201. name => "name",
  202. validate => sub {
  203. my $name=shift;
  204. length $name &&
  205. $name=~/$config{wiki_file_regexp}/ &&
  206. ! userinfo_get($name, "regdate");
  207. },
  208. );
  209. }
  210. elsif ($submittype ne 'OpenID') {
  211. $form->field(
  212. name => "name",
  213. validate => sub {
  214. my $name=shift;
  215. length $name &&
  216. userinfo_get($name, "regdate");
  217. },
  218. );
  219. }
  220. }
  221. else {
  222. # First time settings.
  223. $form->field(name => "name", comment => "use FirstnameLastName");
  224. if ($session->param("name")) {
  225. $form->field(name => "name", value => $session->param("name"));
  226. }
  227. }
  228. if ($form->submitted && $form->validate) {
  229. if ($form->submitted eq 'Login') {
  230. $session->param("name", $form->field("name"));
  231. cgi_postsignin($q, $session);
  232. }
  233. elsif ($form->submitted eq 'Create Account') {
  234. my $user_name=$form->field('name');
  235. if (userinfo_setall($user_name, {
  236. 'email' => $form->field('email'),
  237. 'password' => $form->field('password'),
  238. 'regdate' => time
  239. })) {
  240. $form->field(name => "confirm_password", type => "hidden");
  241. $form->field(name => "email", type => "hidden");
  242. $form->text("Account creation successful. Now you can Login.");
  243. printheader($session);
  244. print misctemplate($form->title, $form->render(submit => ["Login"]));
  245. }
  246. else {
  247. error("Error creating account.");
  248. }
  249. }
  250. elsif ($form->submitted eq 'Mail Password') {
  251. my $user_name=$form->field("name");
  252. my $template=template("passwordmail.tmpl");
  253. $template->param(
  254. user_name => $user_name,
  255. user_password => userinfo_get($user_name, "password"),
  256. wikiurl => $config{url},
  257. wikiname => $config{wikiname},
  258. REMOTE_ADDR => $ENV{REMOTE_ADDR},
  259. );
  260. eval q{use Mail::Sendmail};
  261. error($@) if $@;
  262. sendmail(
  263. To => userinfo_get($user_name, "email"),
  264. From => "$config{wikiname} admin <$config{adminemail}>",
  265. Subject => "$config{wikiname} information",
  266. Message => $template->output,
  267. ) or error("Failed to send mail");
  268. $form->text("Your password has been emailed to you.");
  269. $form->field(name => "name", required => 0);
  270. printheader($session);
  271. print misctemplate($form->title, $form->render(submit => ["Login", "Mail Password"]));
  272. }
  273. elsif ($form->submitted eq "Register") {
  274. printheader($session);
  275. print misctemplate($form->title, $form->render(submit => ["Create Account"]));
  276. }
  277. }
  278. elsif ($form->submitted eq "Create Account") {
  279. printheader($session);
  280. print misctemplate($form->title, $form->render(submit => ["Create Account"]));
  281. }
  282. else {
  283. printheader($session);
  284. print misctemplate($form->title, $form->render(submit => ["Login", "Register", "Mail Password"]));
  285. }
  286. } #}}}
  287. sub cgi_postsignin ($$) { #{{{
  288. my $q=shift;
  289. my $session=shift;
  290. # Continue with whatever was being done before the signin process.
  291. if (defined $q->param("do") && $q->param("do") ne "signin" &&
  292. defined $session->param("postsignin")) {
  293. my $postsignin=CGI->new($session->param("postsignin"));
  294. $session->clear("postsignin");
  295. cgi($postsignin, $session);
  296. exit;
  297. }
  298. else {
  299. redirect($q, $config{url});
  300. }
  301. } #}}}
  302. sub cgi_prefs ($$) { #{{{
  303. my $q=shift;
  304. my $session=shift;
  305. eval q{use CGI::FormBuilder};
  306. error($@) if $@;
  307. my $form = CGI::FormBuilder->new(
  308. title => "preferences",
  309. fields => [qw(do name password confirm_password email
  310. subscriptions locked_pages)],
  311. header => 0,
  312. charset => "utf-8",
  313. method => 'POST',
  314. validate => {
  315. confirm_password => {
  316. perl => q{eq $form->field("password")},
  317. },
  318. email => 'EMAIL',
  319. },
  320. required => 'NONE',
  321. javascript => 0,
  322. params => $q,
  323. action => $config{cgiurl},
  324. template => (-e "$config{templatedir}/prefs.tmpl" ?
  325. {template_params("prefs.tmpl")} : ""),
  326. stylesheet => baseurl()."style.css",
  327. );
  328. my @buttons=("Save Preferences", "Logout", "Cancel");
  329. my $user_name=$session->param("name");
  330. $form->field(name => "do", type => "hidden");
  331. $form->field(name => "name", disabled => 1,
  332. value => $user_name, force => 1, size => 30);
  333. $form->field(name => "password", type => "password");
  334. $form->field(name => "confirm_password", type => "password");
  335. $form->field(name => "subscriptions", size => 50,
  336. comment => "(".htmllink("", "", "PageSpec", 1).")");
  337. $form->field(name => "locked_pages", size => 50,
  338. comment => "(".htmllink("", "", "PageSpec", 1).")");
  339. $form->field(name => "banned_users", size => 50);
  340. if (! is_admin($user_name)) {
  341. $form->field(name => "locked_pages", type => "hidden");
  342. $form->field(name => "banned_users", type => "hidden");
  343. }
  344. if ($config{httpauth}) {
  345. $form->field(name => "password", type => "hidden");
  346. $form->field(name => "confirm_password", type => "hidden");
  347. }
  348. if (! $form->submitted) {
  349. $form->field(name => "email", force => 1,
  350. value => userinfo_get($user_name, "email"));
  351. $form->field(name => "subscriptions", force => 1,
  352. value => userinfo_get($user_name, "subscriptions"));
  353. $form->field(name => "locked_pages", force => 1,
  354. value => userinfo_get($user_name, "locked_pages"));
  355. if (is_admin($user_name)) {
  356. $form->field(name => "banned_users", force => 1,
  357. value => join(" ", get_banned_users()));
  358. }
  359. }
  360. decode_form_utf8($form);
  361. if ($form->submitted eq 'Logout') {
  362. $session->delete();
  363. redirect($q, $config{url});
  364. return;
  365. }
  366. elsif ($form->submitted eq 'Cancel') {
  367. redirect($q, $config{url});
  368. return;
  369. }
  370. elsif ($form->submitted eq "Save Preferences" && $form->validate) {
  371. foreach my $field (qw(password email subscriptions locked_pages)) {
  372. if (length $form->field($field)) {
  373. userinfo_set($user_name, $field, $form->field($field)) || error("failed to set $field");
  374. }
  375. }
  376. if (is_admin($user_name)) {
  377. set_banned_users(grep { ! is_admin($_) }
  378. split(' ', $form->field("banned_users")));
  379. }
  380. $form->text("Preferences saved.");
  381. }
  382. printheader($session);
  383. print misctemplate($form->title, $form->render(submit => \@buttons));
  384. } #}}}
  385. sub cgi_editpage ($$) { #{{{
  386. my $q=shift;
  387. my $session=shift;
  388. my @fields=qw(do rcsinfo subpage from page type editcontent comments);
  389. my @buttons=("Save Page", "Preview", "Cancel");
  390. eval q{use CGI::FormBuilder; use CGI::FormBuilder::Template::HTML};
  391. error($@) if $@;
  392. my $renderer=CGI::FormBuilder::Template::HTML->new(
  393. fields => \@fields,
  394. template_params("editpage.tmpl"),
  395. );
  396. run_hooks(pagetemplate => sub {
  397. shift->(page => "", destpage => "", template => $renderer->engine);
  398. });
  399. my $form = CGI::FormBuilder->new(
  400. fields => \@fields,
  401. header => 1,
  402. charset => "utf-8",
  403. method => 'POST',
  404. validate => {
  405. editcontent => '/.+/',
  406. },
  407. required => [qw{editcontent}],
  408. javascript => 0,
  409. params => $q,
  410. action => $config{cgiurl},
  411. table => 0,
  412. template => $renderer,
  413. );
  414. decode_form_utf8($form);
  415. # This untaint is safe because titlepage removes any problematic
  416. # characters.
  417. my ($page)=$form->field('page');
  418. $page=titlepage(possibly_foolish_untaint($page));
  419. if (! defined $page || ! length $page ||
  420. $page=~/$config{wiki_file_prune_regexp}/ || $page=~/^\//) {
  421. error("bad page name");
  422. }
  423. my $from;
  424. if (defined $form->field('from')) {
  425. ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
  426. }
  427. my $file;
  428. my $type;
  429. if (exists $pagesources{$page}) {
  430. $file=$pagesources{$page};
  431. $type=pagetype($file);
  432. }
  433. else {
  434. $type=$form->param('type');
  435. if (defined $type && length $type && $hooks{htmlize}{$type}) {
  436. $type=possibly_foolish_untaint($type);
  437. }
  438. elsif (defined $from) {
  439. # favor the type of linking page
  440. $type=pagetype($pagesources{$from});
  441. }
  442. $type=$config{default_pageext} unless defined $type;
  443. $file=$page.".".$type;
  444. }
  445. my $newfile=0;
  446. if (! -e "$config{srcdir}/$file") {
  447. $newfile=1;
  448. }
  449. $form->field(name => "do", type => 'hidden');
  450. $form->field(name => "from", type => 'hidden');
  451. $form->field(name => "rcsinfo", type => 'hidden');
  452. $form->field(name => "subpage", type => 'hidden');
  453. $form->field(name => "page", value => $page, force => 1);
  454. $form->field(name => "type", value => $type, force => 1);
  455. $form->field(name => "comments", type => "text", size => 80);
  456. $form->field(name => "editcontent", type => "textarea", rows => 20,
  457. cols => 80);
  458. $form->tmpl_param("can_commit", $config{rcs});
  459. $form->tmpl_param("indexlink", indexlink());
  460. $form->tmpl_param("helponformattinglink",
  461. htmllink("", "", "HelpOnFormatting", 1));
  462. $form->tmpl_param("baseurl", baseurl());
  463. if (! $form->submitted) {
  464. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  465. force => 1);
  466. }
  467. if ($form->submitted eq "Cancel") {
  468. if ($newfile && defined $from) {
  469. redirect($q, "$config{url}/".htmlpage($from));
  470. }
  471. elsif ($newfile) {
  472. redirect($q, $config{url});
  473. }
  474. else {
  475. redirect($q, "$config{url}/".htmlpage($page));
  476. }
  477. return;
  478. }
  479. elsif ($form->submitted eq "Preview") {
  480. my $content=$form->field('editcontent');
  481. my $comments=$form->field('comments');
  482. $form->field(name => "editcontent",
  483. value => $content, force => 1);
  484. $form->field(name => "comments",
  485. value => $comments, force => 1);
  486. $config{rss}=$config{atom}=0; # avoid preview writing a feed!
  487. $form->tmpl_param("page_preview",
  488. htmlize($page, $type,
  489. linkify($page, "",
  490. preprocess($page, $page,
  491. filter($page, $content)))));
  492. }
  493. else {
  494. $form->tmpl_param("page_preview", "");
  495. }
  496. $form->tmpl_param("page_conflict", "");
  497. if (! $form->submitted || $form->submitted eq "Preview" ||
  498. ! $form->validate) {
  499. if ($form->field("do") eq "create") {
  500. my @page_locs;
  501. my $best_loc;
  502. if (! defined $from || ! length $from ||
  503. $from ne $form->field('from') ||
  504. $from=~/$config{wiki_file_prune_regexp}/ ||
  505. $from=~/^\// ||
  506. $form->submitted eq "Preview") {
  507. @page_locs=$best_loc=$page;
  508. }
  509. else {
  510. my $dir=$from."/";
  511. $dir=~s![^/]+/+$!!;
  512. if ((defined $form->field('subpage') && length $form->field('subpage')) ||
  513. $page eq 'discussion') {
  514. $best_loc="$from/$page";
  515. }
  516. else {
  517. $best_loc=$dir.$page;
  518. }
  519. push @page_locs, $dir.$page;
  520. push @page_locs, "$from/$page";
  521. while (length $dir) {
  522. $dir=~s![^/]+/+$!!;
  523. push @page_locs, $dir.$page;
  524. }
  525. }
  526. @page_locs = grep {
  527. ! exists $pagecase{lc $_} &&
  528. ! page_locked($_, $session, 1)
  529. } @page_locs;
  530. if (! @page_locs) {
  531. # hmm, someone else made the page in the
  532. # meantime?
  533. redirect($q, "$config{url}/".htmlpage($page));
  534. return;
  535. }
  536. my @page_types;
  537. if (exists $hooks{htmlize}) {
  538. @page_types=keys %{$hooks{htmlize}};
  539. }
  540. $form->tmpl_param("page_select", 1);
  541. $form->field(name => "page", type => 'select',
  542. options => \@page_locs, value => $best_loc);
  543. $form->field(name => "type", type => 'select',
  544. options => \@page_types);
  545. $form->title("creating ".pagetitle($page));
  546. }
  547. elsif ($form->field("do") eq "edit") {
  548. page_locked($page, $session);
  549. if (! defined $form->field('editcontent') ||
  550. ! length $form->field('editcontent')) {
  551. my $content="";
  552. if (exists $pagesources{$page}) {
  553. $content=readfile(srcfile($pagesources{$page}));
  554. $content=~s/\n/\r\n/g;
  555. }
  556. $form->field(name => "editcontent", value => $content,
  557. force => 1);
  558. }
  559. $form->tmpl_param("page_select", 0);
  560. $form->field(name => "page", type => 'hidden');
  561. $form->field(name => "type", type => 'hidden');
  562. $form->title("editing ".pagetitle($page));
  563. }
  564. print $form->render(submit => \@buttons);
  565. }
  566. else {
  567. # save page
  568. page_locked($page, $session);
  569. my $content=$form->field('editcontent');
  570. $content=~s/\r\n/\n/g;
  571. $content=~s/\r/\n/g;
  572. writefile($file, $config{srcdir}, $content);
  573. my $message="web commit ";
  574. if (defined $session->param("name") &&
  575. length $session->param("name")) {
  576. $message.="by ".$session->param("name");
  577. }
  578. else {
  579. $message.="from $ENV{REMOTE_ADDR}";
  580. }
  581. if (defined $form->field('comments') &&
  582. length $form->field('comments')) {
  583. $message.=": ".$form->field('comments');
  584. }
  585. if ($config{rcs}) {
  586. if ($newfile) {
  587. rcs_add($file);
  588. }
  589. # prevent deadlock with post-commit hook
  590. unlockwiki();
  591. # presumably the commit will trigger an update
  592. # of the wiki
  593. my $conflict=rcs_commit($file, $message,
  594. $form->field("rcsinfo"));
  595. if (defined $conflict) {
  596. $form->field(name => "rcsinfo", value => rcs_prepedit($file),
  597. force => 1);
  598. $form->tmpl_param("page_conflict", 1);
  599. $form->field("editcontent", value => $conflict, force => 1);
  600. $form->field(name => "comments", value => $form->field('comments'), force => 1);
  601. $form->field("do", "edit)");
  602. $form->tmpl_param("page_select", 0);
  603. $form->field(name => "page", type => 'hidden');
  604. $form->field(name => "type", type => 'hidden');
  605. $form->title("editing $page");
  606. print $form->render(submit => \@buttons);
  607. return;
  608. }
  609. }
  610. else {
  611. require IkiWiki::Render;
  612. refresh();
  613. saveindex();
  614. }
  615. # The trailing question mark tries to avoid broken
  616. # caches and get the most recent version of the page.
  617. redirect($q, "$config{url}/".htmlpage($page)."?updated");
  618. }
  619. } #}}}
  620. sub cgi (;$$) { #{{{
  621. my $q=shift;
  622. my $session=shift;
  623. if (! $q) {
  624. eval q{use CGI; use CGI::Session};
  625. error($@) if $@;
  626. $q=CGI->new;
  627. run_hooks(cgi => sub { shift->($q) });
  628. }
  629. my $do=$q->param('do');
  630. if (! defined $do || ! length $do) {
  631. my $error = $q->cgi_error;
  632. if ($error) {
  633. error("Request not processed: $error");
  634. }
  635. else {
  636. error("\"do\" parameter missing");
  637. }
  638. }
  639. # Things that do not need a session.
  640. if ($do eq 'recentchanges') {
  641. cgi_recentchanges($q);
  642. return;
  643. }
  644. elsif ($do eq 'hyperestraier') {
  645. cgi_hyperestraier();
  646. }
  647. if (! $session) {
  648. CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
  649. my $oldmask=umask(077);
  650. $session = CGI::Session->new("driver:DB_File", $q,
  651. { FileName => "$config{wikistatedir}/sessions.db" });
  652. umask($oldmask);
  653. }
  654. # Auth hooks can sign a user in.
  655. if ($do ne 'signin' && ! defined $session->param("name")) {
  656. run_hooks(auth => sub {
  657. shift->($q, $session)
  658. });
  659. if (defined $session->param("name")) {
  660. # Make sure whatever user was authed is in the
  661. # userinfo db.
  662. if (! userinfo_get($session->param("name"), "regdate")) {
  663. userinfo_setall($session->param("name"), {
  664. email => "",
  665. password => "",
  666. regdate => time,
  667. });
  668. }
  669. }
  670. }
  671. # Everything below this point needs the user to be signed in.
  672. if (((! $config{anonok} || $do eq 'prefs') &&
  673. (! defined $session->param("name") ||
  674. ! userinfo_get($session->param("name"), "regdate")))
  675. || $do eq 'signin') {
  676. if ($do ne 'signin' && ! defined $session->param("postsignin")) {
  677. $session->param(postsignin => $ENV{QUERY_STRING});
  678. }
  679. cgi_signin($q, $session);
  680. # Force session flush with safe umask.
  681. my $oldmask=umask(077);
  682. $session->flush;
  683. umask($oldmask);
  684. return;
  685. }
  686. elsif (defined $session->param("postsignin")) {
  687. cgi_postsignin($q, $session);
  688. }
  689. if (defined $session->param("name") && userinfo_get($session->param("name"), "banned")) {
  690. print $q->header(-status => "403 Forbidden");
  691. $session->delete();
  692. print "You are banned.";
  693. exit;
  694. }
  695. if ($do eq 'create' || $do eq 'edit') {
  696. cgi_editpage($q, $session);
  697. }
  698. elsif ($do eq 'prefs') {
  699. cgi_prefs($q, $session);
  700. }
  701. elsif ($do eq 'blog') {
  702. my $page=titlepage(decode_utf8($q->param('title')));
  703. # if the page already exists, munge it to be unique
  704. my $from=$q->param('from');
  705. my $add="";
  706. while (exists $pagecase{lc "$from/$page$add"}) {
  707. $add=1 unless length $add;
  708. $add++;
  709. }
  710. $q->param('page', $page.$add);
  711. # now run same as create
  712. $q->param('do', 'create');
  713. cgi_editpage($q, $session);
  714. }
  715. else {
  716. error("unknown do parameter");
  717. }
  718. } #}}}
  719. 1