]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/CGI.pm
59e9ce161b022cff7be26350e251b16d7f5a5da6
[git.ikiwiki.info.git] / IkiWiki / CGI.pm
1 #!/usr/bin/perl
3 use warnings;
4 use strict;
5 use IkiWiki;
6 use IkiWiki::UserInfo;
7 use open qw{:utf8 :std};
8 use Encode;
10 package IkiWiki;
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         }
22 } #}}}
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 } #}}}
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);
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         }
53         return 0;
54 } #}}}
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 } #}}}
67 sub cgi_recentchanges ($) { #{{{
68         my $q=shift;
69         
70         unlockwiki();
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");
80         eval q{use Time::Duration};
81         error($@) if $@;
82         eval q{use CGI 'escapeHTML'};
83         error($@) if $@;
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);
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         }
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 } #}}}
115 sub cgi_signin ($$) { #{{{
116         my $q=shift;
117         my $session=shift;
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         }
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 } #}}}
297 sub cgi_prefs ($$) { #{{{
298         my $q=shift;
299         my $session=shift;
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         }
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 } #}}}
389 sub cgi_editpage ($$) { #{{{
390         my $q=shift;
391         my $session=shift;
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         }
456         my $newfile=0;
457         if (! -e "$config{srcdir}/$file") {
458                 $newfile=1;
459         }
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                         }
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 "Content-Type: text/html\n\n";
586                 print $form->render(submit => \@buttons);
587         }
588         else {
589                 # save page
590                 page_locked($page, $session);
591                 
592                 my $content=$form->field('editcontent');
594                 $content=~s/\r\n/\n/g;
595                 $content=~s/\r/\n/g;
596                 writefile($file, $config{srcdir}, $content);
597                 
598                 my $message="web commit ";
599                 if (defined $session->param("name") && 
600                     length $session->param("name")) {
601                         $message.="by ".$session->param("name");
602                 }
603                 else {
604                         $message.="from $ENV{REMOTE_ADDR}";
605                 }
606                 if (defined $form->field('comments') &&
607                     length $form->field('comments')) {
608                         $message.=": ".$form->field('comments');
609                 }
610                 
611                 if ($config{rcs}) {
612                         if ($newfile) {
613                                 rcs_add($file);
614                         }
615                         # prevent deadlock with post-commit hook
616                         unlockwiki();
617                         # presumably the commit will trigger an update
618                         # of the wiki
619                         my $conflict=rcs_commit($file, $message,
620                                 $form->field("rcsinfo"));
621                 
622                         if (defined $conflict) {
623                                 $form->field(name => "rcsinfo", value => rcs_prepedit($file),
624                                         force => 1);
625                                 $form->tmpl_param("page_conflict", 1);
626                                 $form->field("editcontent", value => $conflict, force => 1);
627                                 $form->field(name => "comments", value => $form->field('comments'), force => 1);
628                                 $form->field("do", "edit)");
629                                 $form->tmpl_param("page_select", 0);
630                                 $form->field(name => "page", type => 'hidden');
631                                 $form->field(name => "type", type => 'hidden');
632                                 $form->title("editing $page");
633                                 print $form->render(submit => \@buttons);
634                                 return;
635                         }
636                 }
637                 else {
638                         require IkiWiki::Render;
639                         refresh();
640                         saveindex();
641                 }
642                 
643                 # The trailing question mark tries to avoid broken
644                 # caches and get the most recent version of the page.
645                 redirect($q, "$config{url}/".htmlpage($page)."?updated");
646         }
647 } #}}}
649 sub cgi () { #{{{
650         eval q{use CGI; use CGI::Session};
651         error($@) if $@;
652         
653         my $q=CGI->new;
654         
655         run_hooks(cgi => sub { shift->($q) });
656         
657         my $do=$q->param('do');
658         if (! defined $do || ! length $do) {
659                 my $error = $q->cgi_error;
660                 if ($error) {
661                         error("Request not processed: $error");
662                 }
663                 else {
664                         error("\"do\" parameter missing");
665                 }
666         }
667         
668         # Things that do not need a session.
669         if ($do eq 'recentchanges') {
670                 cgi_recentchanges($q);
671                 return;
672         }
673         elsif ($do eq 'hyperestraier') {
674                 cgi_hyperestraier();
675         }
676         
677         CGI::Session->name("ikiwiki_session_".encode_utf8($config{wikiname}));
678         
679         my $oldmask=umask(077);
680         my $session = CGI::Session->new("driver:DB_File", $q,
681                 { FileName => "$config{wikistatedir}/sessions.db" });
682         umask($oldmask);
683         
684         # Everything below this point needs the user to be signed in.
685         if (((! $config{anonok} || $do eq 'prefs') &&
686              (! $config{httpauth}) &&
687              (! defined $session->param("name") ||
688              ! userinfo_get($session->param("name"), "regdate"))) || $do eq 'signin') {
689                 cgi_signin($q, $session);
690         
691                 # Force session flush with safe umask.
692                 my $oldmask=umask(077);
693                 $session->flush;
694                 umask($oldmask);
695                 
696                 return;
697         }
699         if ($config{httpauth} && (! defined $session->param("name"))) {
700                 if (! defined $q->remote_user()) {
701                         error("Could not determine authenticated username.");
702                 }
703                 else {
704                         $session->param("name", $q->remote_user());
705                         if (! userinfo_get($session->param("name"), "regdate")) {
706                                 userinfo_setall($session->param("name"), {
707                                         email => "",
708                                         password => "",
709                                         regdate=>time,
710                                 });
711                         }
712                 }
713         }
715         if (userinfo_get($session->param("name"), "banned")) {
716                 print $q->header(-status => "403 Forbidden");
717                 $session->delete();
718                 print "You are banned.";
719                 exit;
720         }
721         
722         if ($do eq 'create' || $do eq 'edit') {
723                 cgi_editpage($q, $session);
724         }
725         elsif ($do eq 'prefs') {
726                 cgi_prefs($q, $session);
727         }
728         elsif ($do eq 'blog') {
729                 my $page=titlepage(decode_utf8($q->param('title')));
730                 # if the page already exists, munge it to be unique
731                 my $from=$q->param('from');
732                 my $add="";
733                 while (exists $pagecase{lc "$from/$page$add"}) {
734                         $add=1 unless length $add;
735                         $add++;
736                 }
737                 $q->param('page', $page.$add);
738                 # now run same as create
739                 $q->param('do', 'create');
740                 cgi_editpage($q, $session);
741         }
742         else {
743                 error("unknown do parameter");
744         }
745 } #}}}