-
- decode_form_utf8($form);
- run_hooks(formbuilder_setup => sub {
- shift->(form => $form, cgi => $q, session => $session,
- buttons => \@buttons);
- });
- decode_form_utf8($form);
-
- # This untaint is safe because we check file_pruned.
- my $page=$form->field('page');
- $page=possibly_foolish_untaint($page);
- my $absolute=($page =~ s#^/+##);
- if (! defined $page || ! length $page ||
- file_pruned($page, $config{srcdir})) {
- error("bad page name");
- }
-
- my $baseurl=$config{url}."/".htmlpage($page);
-
- my $from;
- if (defined $form->field('from')) {
- ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
- }
-
- my $file;
- my $type;
- if (exists $pagesources{$page} && $form->field("do") ne "create") {
- $file=$pagesources{$page};
- $type=pagetype($file);
- if (! defined $type || $type=~/^_/) {
- error(sprintf(gettext("%s is not an editable page"), $page));
- }
- if (! $form->submitted) {
- $form->field(name => "rcsinfo",
- value => rcs_prepedit($file), force => 1);
- }
- $form->field(name => "editcontent", validate => '/.*/');
- }
- else {
- $type=$form->param('type');
- if (defined $type && length $type && $hooks{htmlize}{$type}) {
- $type=possibly_foolish_untaint($type);
- }
- elsif (defined $from && exists $pagesources{$from}) {
- # favor the type of linking page
- $type=pagetype($pagesources{$from});
- }
- $type=$config{default_pageext} unless defined $type;
- $file=$page.".".$type;
- if (! $form->submitted) {
- $form->field(name => "rcsinfo", value => "", force => 1);
- }
- $form->field(name => "editcontent", validate => '/.+/');
- }
-
- $form->field(name => "do", type => 'hidden');
- $form->field(name => "sid", type => "hidden", value => $session->id,
- force => 1);
- $form->field(name => "from", type => 'hidden');
- $form->field(name => "rcsinfo", type => 'hidden');
- $form->field(name => "subpage", type => 'hidden');
- $form->field(name => "page", value => $page, force => 1);
- $form->field(name => "type", value => $type, force => 1);
- $form->field(name => "comments", type => "text", size => 80);
- $form->field(name => "editcontent", type => "textarea", rows => 20,
- cols => 80);
- $form->tmpl_param("can_commit", $config{rcs});
- $form->tmpl_param("indexlink", indexlink());
- $form->tmpl_param("helponformattinglink",
- htmllink($page, $page, "ikiwiki/formatting",
- noimageinline => 1,
- linktext => "FormattingHelp"));
-
- if ($form->submitted eq "Cancel") {
- if ($form->field("do") eq "create" && defined $from) {
- redirect($q, "$config{url}/".htmlpage($from));
- }
- elsif ($form->field("do") eq "create") {
- redirect($q, $config{url});
- }
- else {
- redirect($q, "$config{url}/".htmlpage($page));
- }
- return;
- }
- elsif ($form->submitted eq "Preview") {
- my $new=not exists $pagesources{$page};
- if ($new) {
- # temporarily record its type
- $pagesources{$page}=$page.".".$type;
- }
-
- my $content=$form->field('editcontent');
-
- run_hooks(editcontent => sub {
- $content=shift->(
- content => $content,
- page => $page,
- cgi => $q,
- session => $session,
- );
- });
- my $preview=htmlize($page, $page, $type,
- linkify($page, $page,
- preprocess($page, $page,
- filter($page, $page, $content), 0, 1)));
- run_hooks(format => sub {
- $preview=shift->(
- page => $page,
- content => $preview,
- );
- });
- $form->tmpl_param("page_preview", $preview);
-
- if ($new) {
- delete $pagesources{$page};
- }
- # previewing may have created files on disk
- saveindex();
- }
- elsif ($form->submitted eq "Save Page") {
- $form->tmpl_param("page_preview", "");
- }
-
- if ($form->submitted ne "Save Page" || ! $form->validate) {
- if ($form->field("do") eq "create") {
- my @page_locs;
- my $best_loc;
- if (! defined $from || ! length $from ||
- $from ne $form->field('from') ||
- file_pruned($from, $config{srcdir}) ||
- $from=~/^\// ||
- $absolute ||
- $form->submitted eq "Preview") {
- @page_locs=$best_loc=$page;
- }
- else {
- my $dir=$from."/";
- $dir=~s![^/]+/+$!!;
-
- if ((defined $form->field('subpage') && length $form->field('subpage')) ||
- $page eq gettext('discussion')) {
- $best_loc="$from/$page";
- }
- else {
- $best_loc=$dir.$page;
- }
-
- push @page_locs, $dir.$page;
- push @page_locs, "$from/$page";
- while (length $dir) {
- $dir=~s![^/]+/+$!!;
- push @page_locs, $dir.$page;
- }
-
- push @page_locs, "$config{userdir}/$page"
- if length $config{userdir};
- }