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