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