]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki/CGI.pm
No longer using ikiwiki
[git.ikiwiki.info.git] / IkiWiki / CGI.pm
index 82cad40c80ad92961fb88e1717f9d0f06049af9e..1db96f9f284ac24ee395224484517a6d264270ad 100644 (file)
@@ -12,16 +12,17 @@ use Encode;
 sub printheader ($) {
        my $session=shift;
        
-       if ($config{sslcookie}) {
+       if (($ENV{HTTPS} && lc $ENV{HTTPS} ne "off") || $config{sslcookie}) {
                print $session->header(-charset => 'utf-8',
                        -cookie => $session->cookie(-httponly => 1, -secure => 1));
-       } else {
+       }
+       else {
                print $session->header(-charset => 'utf-8',
                        -cookie => $session->cookie(-httponly => 1));
        }
 }
 
-sub showform ($$$$;@) {
+sub prepform {
        my $form=shift;
        my $buttons=shift;
        my $session=shift;
@@ -34,13 +35,68 @@ sub showform ($$$$;@) {
                });
        }
 
+       return $form;
+}
+
+sub showform ($$$$;@) {
+       my $form=prepform(@_);
+       shift;
+       my $buttons=shift;
+       my $session=shift;
+       my $cgi=shift;
+
        printheader($session);
-       print misctemplate($form->title, $form->render(submit => $buttons), @_);
+       print cgitemplate($cgi, $form->title,
+               $form->render(submit => $buttons), @_);
+}
+
+sub cgitemplate ($$$;@) {
+       my $cgi=shift;
+       my $title=shift;
+       my $content=shift;
+       my %params=@_;
+       
+       my $template=template("page.tmpl");
+
+       my $page="";
+       if (exists $params{page}) {
+               $page=delete $params{page};
+               $params{forcebaseurl}=urlto($page);
+       }
+       run_hooks(pagetemplate => sub {
+               shift->(
+                       page => $page,
+                       destpage => $page,
+                       template => $template,
+               );
+       });
+       templateactions($template, "");
+
+       my $baseurl = baseurl();
+
+       $template->param(
+               dynamic => 1,
+               title => $title,
+               wikiname => $config{wikiname},
+               content => $content,
+               baseurl => $baseurl,
+               html5 => $config{html5},
+               %params,
+       );
+       
+       return $template->output;
 }
 
 sub redirect ($$) {
        my $q=shift;
-       my $url=shift;
+       eval q{use URI};
+
+       my $topurl;
+       if (defined $q && ! $config{w3mmode} && ! $config{reverse_proxy}) {
+               $topurl = $q->url;
+       }
+
+       my $url=URI->new(urlabs(shift, $topurl));
        if (! $config{w3mmode}) {
                print $q->redirect($url);
        }
@@ -51,21 +107,33 @@ sub redirect ($$) {
 }
 
 sub decode_cgi_utf8 ($) {
-       # decode_form_utf8 method is needed for 5.10
+       # decode_form_utf8 method is needed for 5.01
        if ($] < 5.01) {
                my $cgi = shift;
                foreach my $f ($cgi->param) {
-                       $cgi->param($f, map { decode_utf8 $_ } $cgi->param($f));
+                       $cgi->param($f, map { decode_utf8 $_ }
+                               @{$cgi->param_fetch($f)});
                }
        }
 }
 
+sub safe_decode_utf8 ($) {
+    my $octets = shift;
+    if (!Encode::is_utf8($octets)) {
+        return decode_utf8($octets);
+    }
+    else {
+        return $octets;
+    }
+}
+
 sub decode_form_utf8 ($) {
        if ($] >= 5.01) {
                my $form = shift;
                foreach my $f ($form->field) {
+                       my @value=map { safe_decode_utf8($_) } $form->field($f);
                        $form->field(name  => $f,
-                                    value => decode_utf8($form->field($f)),
+                                    value => \@value,
                                     force => 1,
                        );
                }
@@ -80,16 +148,17 @@ sub needsignin ($$) {
 
        if (! defined $session->param("name") ||
            ! userinfo_get($session->param("name"), "regdate")) {
-               $session->param(postsignin => $ENV{QUERY_STRING});
+               $session->param(postsignin => $q->query_string);
                cgi_signin($q, $session);
                cgi_savesession($session);
                exit;
        }
 }
 
-sub cgi_signin ($$) {
+sub cgi_signin ($$;$) {
        my $q=shift;
        my $session=shift;
+       my $returnhtml=shift;
 
        decode_cgi_utf8($q);
        eval q{use CGI::FormBuilder};
@@ -102,16 +171,13 @@ sub cgi_signin ($$) {
                required => 'NONE',
                javascript => 0,
                params => $q,
-               action => $config{cgiurl},
+               action => cgiurl(),
                header => 0,
                template => {type => 'div'},
-               stylesheet => baseurl()."style.css",
+               stylesheet => 1,
        );
        my $buttons=["Login"];
        
-       if ($q->param("do") ne "signin" && !$form->submitted) {
-               $form->text(gettext("You need to log in first."));
-       }
        $form->field(name => "do", type => "hidden", value => "signin",
                force => 1);
        
@@ -126,6 +192,11 @@ sub cgi_signin ($$) {
                $form->validate;
        }
 
+       if ($returnhtml) {
+               $form=prepform($form, $buttons, $session, $q);
+               return $form->render(submit => $buttons);
+       }
+
        showform($form, $buttons, $session, $q);
 }
 
@@ -142,7 +213,12 @@ sub cgi_postsignin ($$) {
                exit;
        }
        else {
-               error(gettext("login failed, perhaps you need to turn on cookies?"));
+               if ($config{sslcookie} && ! $q->https()) {
+                       error(gettext("probable misconfiguration: sslcookie is set, but you are attempting to login via http, not https"));
+               }
+               else {
+                       error(gettext("login failed, perhaps you need to turn on cookies?"));
+               }
        }
 }
 
@@ -177,9 +253,9 @@ sub cgi_prefs ($$) {
                required => 'NONE',
                javascript => 0,
                params => $q,
-               action => $config{cgiurl},
+               action => cgiurl(),
                template => {type => 'div'},
-               stylesheet => baseurl()."style.css",
+               stylesheet => 1,
                fieldsets => [
                        [login => gettext("Login")],
                        [preferences => gettext("Preferences")],
@@ -210,39 +286,77 @@ sub cgi_prefs ($$) {
        
        if ($form->submitted eq 'Logout') {
                $session->delete();
-               redirect($q, $config{url});
+               redirect($q, baseurl(undef));
                return;
        }
        elsif ($form->submitted eq 'Cancel') {
-               redirect($q, $config{url});
+               redirect($q, baseurl(undef));
                return;
        }
        elsif ($form->submitted eq 'Save Preferences' && $form->validate) {
-               if (defined $form->field('email')) {
-                       userinfo_set($user_name, 'email', $form->field('email')) ||
+               my $email = $form->field('email');
+               if (defined $email) {
+                       userinfo_set($user_name, 'email', $email) ||
                                error("failed to set email");
                }
 
                $form->text(gettext("Preferences saved."));
        }
        
-       showform($form, $buttons, $session, $q);
+       showform($form, $buttons, $session, $q,
+               prefsurl => "", # avoid showing the preferences link
+       );
+}
+
+sub cgi_custom_failure ($$$) {
+       my $q=shift;
+       my $httpstatus=shift;
+       my $message=shift;
+
+       print $q->header(
+               -status => $httpstatus,
+               -charset => 'utf-8',
+       );
+       print $message;
+
+       # Internet Explod^Hrer won't show custom 404 responses
+       # unless they're >= 512 bytes
+       print ' ' x 512;
+
+       exit;
 }
 
 sub check_banned ($$) {
        my $q=shift;
        my $session=shift;
 
+       my $banned=0;
        my $name=$session->param("name");
-       if (defined $name) {
-               if (grep { $name eq $_ } @{$config{banned_users}}) {
-                       print $q->header(-status => "403 Forbidden");
-                       $session->delete();
-                       print gettext("You are banned.");
-                       cgi_savesession($session);
-                       exit;
+       my $cloak=cloak($name) if defined $name;
+       if (defined $name && 
+           grep { $name eq $_ || $cloak eq $_ } @{$config{banned_users}}) {
+               $banned=1;
+       }
+
+       foreach my $b (@{$config{banned_users}}) {
+               if (pagespec_match("", $b,
+                       ip => $session->remote_addr(),
+                       name => defined $name ? $name : "")
+                  || pagespec_match("", $b,
+                       ip => cloak($session->remote_addr()),
+                       name => defined $cloak ? $cloak : "")) {
+                       $banned=1;
+                       last;
                }
        }
+
+       if ($banned) {
+               $session->delete();
+               cgi_savesession($session);
+               cgi_custom_failure(
+                       $q, "403 Forbidden",
+                       gettext("You are banned."));
+       }
 }
 
 sub cgi_getsession ($) {
@@ -258,7 +372,8 @@ sub cgi_getsession ($) {
                        { FileName => "$config{wikistatedir}/sessions.db" })
        };
        if (! $session || $@) {
-               error($@." ".CGI::Session->errstr());
+               my $error = $@;
+               error($error." ".CGI::Session->errstr());
        }
        
        umask($oldmask);
@@ -291,94 +406,15 @@ sub cgi_savesession ($) {
        umask($oldmask);
 }
 
-# cgi_goto(CGI, [page])
-# Redirect to a specified page, or display "not found". If not specified,
-# the page param from the CGI object is used.
-sub cgi_goto ($;$) {
-       my $q = shift;
-       my $page = shift;
-
-       if (!defined $page) {
-               $page = decode_utf8($q->param("page"));
-
-               if (!defined $page) {
-                       error("missing page parameter");
-               }
-       }
-
-       loadindex();
-
-       # If the page is internal (like a comment), see if it has a
-       # permalink. Comments do.
-       if (isinternal($page) &&
-           defined $pagestate{$page}{meta}{permalink}) {
-               redirect($q, $pagestate{$page}{meta}{permalink});
-       }
-
-       my $link = bestlink("", $page);
-
-       if (! length $link) {
-               print $q->header(-status => "404 Not Found");
-               print misctemplate(gettext("missing page"),
-                       "<p>".
-                       sprintf(gettext("The page %s does not exist."),
-                               htmllink("", "", $page)).
-                       "</p>");
-       }
-       else {
-               redirect($q, urlto($link, undef, 1));
-       }
-
-       exit;
-}
-
-sub cgi_page_from_404 ($$$) {
-       my $path = shift;
-       my $baseurl = shift;
-       my $usedirs = shift;
-
-       # fail if missing from environment or whatever
-       return undef unless defined $path;
-       return undef unless defined $baseurl;
-
-       # with usedirs on, path is like /~fred/foo/bar/ or /~fred/foo/bar or
-       #    /~fred/foo/bar/index.html
-       # with usedirs off, path is like /~fred/foo/bar.html
-       # baseurl is like 'http://people.example.com/~fred'
-
-       # convert baseurl to ~fred
-       unless ($baseurl =~ s{^https?://[^/]+/?}{}) {
-               return undef;
-       }
-
-       # convert path to /~fred/foo/bar
-       if ($usedirs) {
-               $path =~ s/\/*(?:index\.$config{htmlext})?$//;
-       }
-       else {
-               $path =~ s/\.$config{htmlext}$//;
-       }
-
-       # remove /~fred/
-       unless ($path =~ s{^/*\Q$baseurl\E/*}{}) {
-               return undef;
-       }
-
-       # special case for the index
-       unless ($path) {
-               return 'index';
-       }
-
-       return $path;
-}
-
 sub cgi (;$$) {
        my $q=shift;
        my $session=shift;
 
        eval q{use CGI};
        error($@) if $@;
+       no warnings "once";
        $CGI::DISABLE_UPLOADS=$config{cgi_disable_uploads};
+       use warnings;
 
        if (! $q) {
                binmode(STDIN);
@@ -399,13 +435,6 @@ sub cgi (;$$) {
                }
        }
 
-       # goto is the preferred name for this; recentchanges_link and
-       # commenter are for compatibility with any saved URLs
-       if ($do eq 'goto' || $do eq 'recentchanges_link' ||
-           $do eq 'commenter') {
-               cgi_goto($q);
-       }
-
        # Need to lock the wiki before getting a session.
        lockwiki();
        loadindex();
@@ -424,7 +453,7 @@ sub cgi (;$$) {
                        # userinfo db.
                        if (! userinfo_get($session->param("name"), "regdate")) {
                                userinfo_setall($session->param("name"), {
-                                       email => "",
+                                       email => defined $session->param("email") ? $session->param("email") : "",
                                        password => "",
                                        regdate => time,
                                }) || error("failed adding user");
@@ -455,8 +484,11 @@ sub cgi (;$$) {
 sub cgierror ($) {
        my $message=shift;
 
+       eval q{use HTML::Entities};
+       $message = encode_entities($message);
+
        print "Content-type: text/html\n\n";
-       print misctemplate(gettext("Error"),
+       print cgitemplate(undef, gettext("Error"),
                "<p class=\"error\">".gettext("Error").": $message</p>");
        die $@;
 }