]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki/CGI.pm
web commit by http://adam.shand.net/
[git.ikiwiki.info.git] / IkiWiki / CGI.pm
index c785e31dec166f2d8ef18caaad14e5bdd37a0734..65a1d7fa08c00c36c08cbda7ec0e82b5f323222e 100644 (file)
@@ -20,7 +20,7 @@ sub printheader ($) { #{{{
        }
 
 } #}}}
        }
 
 } #}}}
-       
+
 sub showform ($$$$) { #{{{
        my $form=shift;
        my $buttons=shift;
 sub showform ($$$$) { #{{{
        my $form=shift;
        my $buttons=shift;
@@ -33,10 +33,9 @@ sub showform ($$$$) { #{{{
                                buttons => $buttons);
                });
        }
                                buttons => $buttons);
                });
        }
-       else {
-               printheader($session);
-               print misctemplate($form->title, $form->render(submit => $buttons));
-       }
+
+       printheader($session);
+       print misctemplate($form->title, $form->render(submit => $buttons));
 }
 
 sub redirect ($$) { #{{{
 }
 
 sub redirect ($$) { #{{{
@@ -61,25 +60,27 @@ sub check_canedit ($$$;$) { #{{{
        run_hooks(canedit => sub {
                return if defined $canedit;
                my $ret=shift->($page, $q, $session);
        run_hooks(canedit => sub {
                return if defined $canedit;
                my $ret=shift->($page, $q, $session);
-               if (defined $ret && $ret eq "") {
-                       $canedit=1;
-               }
-               elsif (defined $ret) {
-                       $canedit=0;
-                       error($ret) unless $nonfatal;
+               if (defined $ret) {
+                       if ($ret eq "") {
+                               $canedit=1;
+                       }
+                       elsif (ref $ret eq 'CODE') {
+                               $ret->() unless $nonfatal;
+                               $canedit=0;
+                       }
+                       elsif (defined $ret) {
+                               error($ret) unless $nonfatal;
+                               $canedit=0;
+                       }
                }
        });
        return $canedit;
 } #}}}
 
                }
        });
        return $canedit;
 } #}}}
 
-sub decode_form_utf8 ($) { #{{{
-       my $form = shift;
-       foreach my $f ($form->field) {
-               next if Encode::is_utf8(scalar $form->field($f));
-               $form->field(name  => $f,
-                            value => decode_utf8($form->field($f)),
-                            force => 1,
-                           );
+sub decode_cgi_utf8 ($) { #{{{
+       my $cgi = shift;
+       foreach my $f ($cgi->param) {
+               $cgi->param($f, map { decode_utf8 $_ } $cgi->param($f));
        }
 } #}}}
 
        }
 } #}}}
 
@@ -138,9 +139,7 @@ sub needsignin ($$) { #{{{
 
        if (! defined $session->param("name") ||
            ! userinfo_get($session->param("name"), "regdate")) {
 
        if (! defined $session->param("name") ||
            ! userinfo_get($session->param("name"), "regdate")) {
-               if (! defined $session->param("postsignin")) {
-                       $session->param(postsignin => $ENV{QUERY_STRING});
-               }
+               $session->param(postsignin => $ENV{QUERY_STRING});
                cgi_signin($q, $session);
                cgi_savesession($session);
                exit;
                cgi_signin($q, $session);
                cgi_savesession($session);
                exit;
@@ -151,6 +150,7 @@ sub cgi_signin ($$) { #{{{
        my $q=shift;
        my $session=shift;
 
        my $q=shift;
        my $session=shift;
 
+       decode_cgi_utf8($q);
        eval q{use CGI::FormBuilder};
        error($@) if $@;
        my $form = CGI::FormBuilder->new(
        eval q{use CGI::FormBuilder};
        error($@) if $@;
        my $form = CGI::FormBuilder->new(
@@ -174,8 +174,6 @@ sub cgi_signin ($$) { #{{{
        $form->field(name => "do", type => "hidden", value => "signin",
                force => 1);
        
        $form->field(name => "do", type => "hidden", value => "signin",
                force => 1);
        
-       decode_form_utf8($form);
-       
        run_hooks(formbuilder_setup => sub {
                shift->(form => $form, cgi => $q, session => $session,
                        buttons => $buttons);
        run_hooks(formbuilder_setup => sub {
                shift->(form => $form, cgi => $q, session => $session,
                        buttons => $buttons);
@@ -201,9 +199,7 @@ sub cgi_postsignin ($$) { #{{{
                exit;
        }
        else {
                exit;
        }
        else {
-               # This can occur, for example, if a user went to the signin
-               # url via a bookmark.
-               redirect($q, $config{url});
+               error(gettext("login failed, perhaps you need to turn on cookies?"));
        }
 } #}}}
 
        }
 } #}}}
 
@@ -213,6 +209,7 @@ sub cgi_prefs ($$) { #{{{
 
        needsignin($q, $session);
 
 
        needsignin($q, $session);
 
+       decode_cgi_utf8($q);
        eval q{use CGI::FormBuilder};
        error($@) if $@;
        my $form = CGI::FormBuilder->new(
        eval q{use CGI::FormBuilder};
        error($@) if $@;
        my $form = CGI::FormBuilder->new(
@@ -238,8 +235,6 @@ sub cgi_prefs ($$) { #{{{
        );
        my $buttons=["Save Preferences", "Logout", "Cancel"];
 
        );
        my $buttons=["Save Preferences", "Logout", "Cancel"];
 
-       decode_form_utf8($form);
-
        run_hooks(formbuilder_setup => sub {
                shift->(form => $form, cgi => $q, session => $session,
                        buttons => $buttons);
        run_hooks(formbuilder_setup => sub {
                shift->(form => $form, cgi => $q, session => $session,
                        buttons => $buttons);
@@ -249,7 +244,7 @@ sub cgi_prefs ($$) { #{{{
        $form->field(name => "email", size => 50, fieldset => "preferences");
        $form->field(name => "subscriptions", size => 50,
                fieldset => "preferences",
        $form->field(name => "email", size => 50, fieldset => "preferences");
        $form->field(name => "subscriptions", size => 50,
                fieldset => "preferences",
-               comment => "(".htmllink("", "", "PageSpec", noimageinline => 1).")");
+               comment => "(".htmllink("", "", "ikiwiki/PageSpec", noimageinline => 1).")");
        $form->field(name => "banned_users", size => 50,
                fieldset => "admin");
        
        $form->field(name => "banned_users", size => 50,
                fieldset => "admin");
        
@@ -280,7 +275,7 @@ sub cgi_prefs ($$) { #{{{
        }
        elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
                foreach my $field (qw(email subscriptions)) {
        }
        elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
                foreach my $field (qw(email subscriptions)) {
-                       if (defined $form->field($field) && length $form->field($field)) {
+                       if (defined $form->field($field)) {
                                userinfo_set($user_name, $field, $form->field($field)) ||
                                        error("failed to set $field");
                        }
                                userinfo_set($user_name, $field, $form->field($field)) ||
                                        error("failed to set $field");
                        }
@@ -304,6 +299,7 @@ sub cgi_editpage ($$) { #{{{
        my @fields=qw(do rcsinfo subpage from page type editcontent comments);
        my @buttons=("Save Page", "Preview", "Cancel");
        
        my @fields=qw(do rcsinfo subpage from page type editcontent comments);
        my @buttons=("Save Page", "Preview", "Cancel");
        
+       decode_cgi_utf8($q);
        eval q{use CGI::FormBuilder};
        error($@) if $@;
        my $form = CGI::FormBuilder->new(
        eval q{use CGI::FormBuilder};
        error($@) if $@;
        my $form = CGI::FormBuilder->new(
@@ -311,9 +307,6 @@ sub cgi_editpage ($$) { #{{{
                fields => \@fields,
                charset => "utf-8",
                method => 'POST',
                fields => \@fields,
                charset => "utf-8",
                method => 'POST',
-               validate => {
-                       editcontent => '/.+/',
-               },
                required => [qw{editcontent}],
                javascript => 0,
                params => $q,
                required => [qw{editcontent}],
                javascript => 0,
                params => $q,
@@ -324,8 +317,6 @@ sub cgi_editpage ($$) { #{{{
                wikiname => $config{wikiname},
        );
        
                wikiname => $config{wikiname},
        );
        
-       decode_form_utf8($form);
-       
        run_hooks(formbuilder_setup => sub {
                shift->(form => $form, cgi => $q, session => $session,
                        buttons => \@buttons);
        run_hooks(formbuilder_setup => sub {
                shift->(form => $form, cgi => $q, session => $session,
                        buttons => \@buttons);
@@ -357,13 +348,14 @@ sub cgi_editpage ($$) { #{{{
                        $form->field(name => "rcsinfo",
                                value => rcs_prepedit($file), force => 1);
                }
                        $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);
                }
        }
        else {
                $type=$form->param('type');
                if (defined $type && length $type && $hooks{htmlize}{$type}) {
                        $type=possibly_foolish_untaint($type);
                }
-               elsif (defined $from) {
+               elsif (defined $from && exists $pagesources{$from}) {
                        # favor the type of linking page
                        $type=pagetype($pagesources{$from});
                }
                        # favor the type of linking page
                        $type=pagetype($pagesources{$from});
                }
@@ -372,6 +364,7 @@ sub cgi_editpage ($$) { #{{{
                if (! $form->submitted) {
                        $form->field(name => "rcsinfo", value => "", force => 1);
                }
                if (! $form->submitted) {
                        $form->field(name => "rcsinfo", value => "", force => 1);
                }
+               $form->field(name => "editcontent", validate => '/.+/');
        }
 
        $form->field(name => "do", type => 'hidden');
        }
 
        $form->field(name => "do", type => 'hidden');
@@ -386,7 +379,9 @@ sub cgi_editpage ($$) { #{{{
        $form->tmpl_param("can_commit", $config{rcs});
        $form->tmpl_param("indexlink", indexlink());
        $form->tmpl_param("helponformattinglink",
        $form->tmpl_param("can_commit", $config{rcs});
        $form->tmpl_param("indexlink", indexlink());
        $form->tmpl_param("helponformattinglink",
-               htmllink("", "", "HelpOnFormatting", noimageinline => 1));
+               htmllink("", "", "ikiwiki/formatting",
+                       noimageinline => 1,
+                       linktext => "FormattingHelp"));
        $form->tmpl_param("baseurl", baseurl());
        
        if ($form->submitted eq "Cancel") {
        $form->tmpl_param("baseurl", baseurl());
        
        if ($form->submitted eq "Cancel") {
@@ -506,6 +501,7 @@ sub cgi_editpage ($$) { #{{{
                }
                
                showform($form, \@buttons, $session, $q);
                }
                
                showform($form, \@buttons, $session, $q);
+               saveindex();
        }
        else {
                # save page
        }
        else {
                # save page
@@ -550,6 +546,7 @@ sub cgi_editpage ($$) { #{{{
                });
                $content=~s/\r\n/\n/g;
                $content=~s/\r/\n/g;
                });
                $content=~s/\r\n/\n/g;
                $content=~s/\r/\n/g;
+               $content.="\n" if $content !~ /\n$/;
 
                $config{cgi}=0; # avoid cgi error message
                eval { writefile($file, $config{srcdir}, $content) };
 
                $config{cgi}=0; # avoid cgi error message
                eval { writefile($file, $config{srcdir}, $content) };
@@ -595,6 +592,10 @@ sub cgi_editpage ($$) { #{{{
                # may have been committed while the post-commit hook was
                # disabled.
                require IkiWiki::Render;
                # may have been committed while the post-commit hook was
                # disabled.
                require IkiWiki::Render;
+               # Reload index, since the first time it's loaded is before
+               # the wiki is locked, and things may have changed in the
+               # meantime.
+               loadindex();
                refresh();
                saveindex();
 
                refresh();
                saveindex();
 
@@ -711,17 +712,14 @@ sub cgi (;$$) { #{{{
                cgi_signin($q, $session);
                cgi_savesession($session);
        }
                cgi_signin($q, $session);
                cgi_savesession($session);
        }
-       elsif (defined $session->param("postsignin")) {
-               cgi_postsignin($q, $session);
-       }
        elsif ($do eq 'prefs') {
                cgi_prefs($q, $session);
        }
        elsif ($do eq 'create' || $do eq 'edit') {
                cgi_editpage($q, $session);
        }
        elsif ($do eq 'prefs') {
                cgi_prefs($q, $session);
        }
        elsif ($do eq 'create' || $do eq 'edit') {
                cgi_editpage($q, $session);
        }
-       elsif ($do eq 'postsignin') {
-               error(gettext("login failed, perhaps you need to turn on cookies?"));
+       elsif (defined $session->param("postsignin") || $do eq 'postsignin') {
+               cgi_postsignin($q, $session);
        }
        else {
                error("unknown do parameter");
        }
        else {
                error("unknown do parameter");