]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki.pm
(no commit message)
[git.ikiwiki.info.git] / IkiWiki.pm
index d5d11ee857c02b1520fc6a971032cc592df78d23..fa71f479107a2388fde2fe00a67bfa2daa4fb3a9 100644 (file)
@@ -5,6 +5,7 @@ package IkiWiki;
 use warnings;
 use strict;
 use Encode;
 use warnings;
 use strict;
 use Encode;
+use Fcntl q{:flock};
 use URI::Escape q{uri_escape_utf8};
 use POSIX ();
 use Storable;
 use URI::Escape q{uri_escape_utf8};
 use POSIX ();
 use Storable;
@@ -108,6 +109,14 @@ sub getsetup () {
                safe => 1,
                rebuild => 1,
        },
                safe => 1,
                rebuild => 1,
        },
+       reverse_proxy => {
+               type => "boolean",
+               default => 0,
+               description => "do not adjust cgiurl if CGI is accessed via different URL",
+               advanced => 0,
+               safe => 1,
+               rebuild => 0, # only affects CGI requests
+       },
        cgi_wrapper => {
                type => "string",
                default => '',
        cgi_wrapper => {
                type => "string",
                default => '',
@@ -266,7 +275,7 @@ sub getsetup () {
        html5 => {
                type => "boolean",
                default => 0,
        html5 => {
                type => "boolean",
                default => 0,
-               description => "generate HTML5?",
+               description => "use elements new in HTML5 like <section>?",
                advanced => 0,
                safe => 1,
                rebuild => 1,
                advanced => 0,
                safe => 1,
                rebuild => 1,
@@ -349,11 +358,20 @@ sub getsetup () {
                safe => 0, # paranoia
                rebuild => 0,
        },
                safe => 0, # paranoia
                rebuild => 0,
        },
+       libdirs => {
+               type => "string",
+               default => [],
+               example => ["$ENV{HOME}/.local/share/ikiwiki"],
+               description => "extra library and plugin directories",
+               advanced => 1,
+               safe => 0, # directory
+               rebuild => 0,
+       },
        libdir => {
                type => "string",
                default => "",
                example => "$ENV{HOME}/.ikiwiki/",
        libdir => {
                type => "string",
                default => "",
                example => "$ENV{HOME}/.ikiwiki/",
-               description => "extra library and plugin directory",
+               description => "extra library and plugin directory (searched after libdirs)",
                advanced => 1,
                safe => 0, # directory
                rebuild => 0,
                advanced => 1,
                safe => 0, # directory
                rebuild => 0,
@@ -535,12 +553,38 @@ sub getsetup () {
        },
        useragent => {
                type => "string",
        },
        useragent => {
                type => "string",
-               default => undef,
+               default => "ikiwiki/$version",
                example => "Wget/1.13.4 (linux-gnu)",
                description => "set custom user agent string for outbound HTTP requests e.g. when fetching aggregated RSS feeds",
                safe => 0,
                rebuild => 0,
        },
                example => "Wget/1.13.4 (linux-gnu)",
                description => "set custom user agent string for outbound HTTP requests e.g. when fetching aggregated RSS feeds",
                safe => 0,
                rebuild => 0,
        },
+       responsive_layout => {
+               type => "boolean",
+               default => 1,
+               description => "theme has a responsive layout? (mobile-optimized)",
+               safe => 1,
+               rebuild => 1,
+       },
+       deterministic => {
+               type => "boolean",
+               default => 0,
+               description => "try harder to produce deterministic output",
+               safe => 1,
+               rebuild => 1,
+               advanced => 1,
+       },
+}
+
+sub getlibdirs () {
+       my @libdirs;
+       if ($config{libdirs}) {
+               @libdirs = @{$config{libdirs}};
+       }
+       if (length $config{libdir}) {
+               push @libdirs, $config{libdir};
+       }
+       return @libdirs;
 }
 
 sub defaultconfig () {
 }
 
 sub defaultconfig () {
@@ -583,9 +627,20 @@ sub checkconfig () {
        if (defined $config{timezone} && length $config{timezone}) {
                $ENV{TZ}=$config{timezone};
        }
        if (defined $config{timezone} && length $config{timezone}) {
                $ENV{TZ}=$config{timezone};
        }
-       else {
+       elsif (defined $ENV{TZ} && length $ENV{TZ}) {
                $config{timezone}=$ENV{TZ};
        }
                $config{timezone}=$ENV{TZ};
        }
+       else {
+               eval q{use Config qw()};
+               error($@) if $@;
+
+               if ($Config::Config{d_gnulibc} && -e '/etc/localtime') {
+                       $config{timezone}=$ENV{TZ}=':/etc/localtime';
+               }
+               else {
+                       $config{timezone}=$ENV{TZ}='GMT';
+               }
+       }
 
        if ($config{w3mmode}) {
                eval q{use Cwd q{abs_path}};
 
        if ($config{w3mmode}) {
                eval q{use Cwd q{abs_path}};
@@ -613,12 +668,39 @@ sub checkconfig () {
 
                        $local_cgiurl = $cgiurl->path;
 
 
                        $local_cgiurl = $cgiurl->path;
 
-                       if ($cgiurl->scheme ne $baseurl->scheme or
-                               $cgiurl->authority ne $baseurl->authority) {
+                       if ($cgiurl->scheme eq 'https' &&
+                               $baseurl->scheme eq 'http') {
+                               # We assume that the same content is available
+                               # over both http and https, because if it
+                               # wasn't, accessing the static content
+                               # from the CGI would be mixed-content,
+                               # which would be a security flaw.
+
+                               if ($cgiurl->authority ne $baseurl->authority) {
+                                       # use protocol-relative URL for
+                                       # static content
+                                       $local_url = "$config{url}/";
+                                       $local_url =~ s{^http://}{//};
+                               }
+                               # else use host-relative URL for static content
+
+                               # either way, CGI needs to be absolute
+                               $local_cgiurl = $config{cgiurl};
+                       }
+                       elsif ($cgiurl->scheme ne $baseurl->scheme) {
                                # too far apart, fall back to absolute URLs
                                $local_url = "$config{url}/";
                                $local_cgiurl = $config{cgiurl};
                        }
                                # too far apart, fall back to absolute URLs
                                $local_url = "$config{url}/";
                                $local_cgiurl = $config{cgiurl};
                        }
+                       elsif ($cgiurl->authority ne $baseurl->authority) {
+                               # slightly too far apart, fall back to
+                               # protocol-relative URLs
+                               $local_url = "$config{url}/";
+                               $local_url =~ s{^https?://}{//};
+                               $local_cgiurl = $config{cgiurl};
+                               $local_cgiurl =~ s{^https?://}{//};
+                       }
+                       # else keep host-relative URLs
                }
 
                $local_url =~ s{//$}{/};
                }
 
                $local_url =~ s{//$}{/};
@@ -658,14 +740,14 @@ sub checkconfig () {
 sub listplugins () {
        my %ret;
 
 sub listplugins () {
        my %ret;
 
-       foreach my $dir (@INC, $config{libdir}) {
+       foreach my $dir (@INC, getlibdirs()) {
                next unless defined $dir && length $dir;
                foreach my $file (glob("$dir/IkiWiki/Plugin/*.pm")) {
                        my ($plugin)=$file=~/.*\/(.*)\.pm$/;
                        $ret{$plugin}=1;
                }
        }
                next unless defined $dir && length $dir;
                foreach my $file (glob("$dir/IkiWiki/Plugin/*.pm")) {
                        my ($plugin)=$file=~/.*\/(.*)\.pm$/;
                        $ret{$plugin}=1;
                }
        }
-       foreach my $dir ($config{libdir}, "$installdir/lib/ikiwiki") {
+       foreach my $dir (getlibdirs(), "$installdir/lib/ikiwiki") {
                next unless defined $dir && length $dir;
                foreach my $file (glob("$dir/plugins/*")) {
                        $ret{basename($file)}=1 if -x $file;
                next unless defined $dir && length $dir;
                foreach my $file (glob("$dir/plugins/*")) {
                        $ret{basename($file)}=1 if -x $file;
@@ -676,8 +758,8 @@ sub listplugins () {
 }
 
 sub loadplugins () {
 }
 
 sub loadplugins () {
-       if (defined $config{libdir} && length $config{libdir}) {
-               unshift @INC, possibly_foolish_untaint($config{libdir});
+       foreach my $dir (getlibdirs()) {
+               unshift @INC, possibly_foolish_untaint($dir);
        }
 
        foreach my $plugin (@{$config{default_plugins}}, @{$config{add_plugins}}) {
        }
 
        foreach my $plugin (@{$config{default_plugins}}, @{$config{add_plugins}}) {
@@ -710,8 +792,8 @@ sub loadplugin ($;$) {
 
        return if ! $force && grep { $_ eq $plugin} @{$config{disable_plugins}};
 
 
        return if ! $force && grep { $_ eq $plugin} @{$config{disable_plugins}};
 
-       foreach my $dir (defined $config{libdir} ? possibly_foolish_untaint($config{libdir}) : undef,
-                        "$installdir/lib/ikiwiki") {
+       foreach my $possiblytainteddir (getlibdirs(), "$installdir/lib/ikiwiki") {
+               my $dir = possibly_foolish_untaint($possiblytainteddir);
                if (defined $dir && -x "$dir/plugins/$plugin") {
                        eval { require IkiWiki::Plugin::external };
                        if ($@) {
                if (defined $dir && -x "$dir/plugins/$plugin") {
                        eval { require IkiWiki::Plugin::external };
                        if ($@) {
@@ -761,10 +843,9 @@ sub log_message ($$) {
                        $log_open=1;
                }
                eval {
                        $log_open=1;
                }
                eval {
-                       # keep a copy to avoid editing the original config repeatedly
-                       my $wikiname = $config{wikiname};
-                       utf8::encode($wikiname);
-                       Sys::Syslog::syslog($type, "[$wikiname] %s", join(" ", @_));
+                       my $message = "[$config{wikiname}] ".join(" ", @_);
+                       utf8::encode($message);
+                       Sys::Syslog::syslog($type, "%s", $message);
                };
                 if ($@) {
                     print STDERR "failed to syslog: $@" unless $log_failed;
                };
                 if ($@) {
                     print STDERR "failed to syslog: $@" unless $log_failed;
@@ -1143,7 +1224,7 @@ sub cgiurl (@) {
        }
 
        return $cgiurl."?".
        }
 
        return $cgiurl."?".
-               join("&amp;", map $_."=".uri_escape_utf8($params{$_}), keys %params);
+               join("&amp;", map $_."=".uri_escape_utf8($params{$_}), sort(keys %params));
 }
 
 sub cgiurl_abs (@) {
 }
 
 sub cgiurl_abs (@) {
@@ -1356,6 +1437,7 @@ sub userpage ($) {
        return length $config{userdir} ? "$config{userdir}/$user" : $user;
 }
 
        return length $config{userdir} ? "$config{userdir}/$user" : $user;
 }
 
+# Username to display for openid accounts.
 sub openiduser ($) {
        my $user=shift;
 
 sub openiduser ($) {
        my $user=shift;
 
@@ -1390,6 +1472,36 @@ sub openiduser ($) {
        return;
 }
 
        return;
 }
 
+# Username to display for emailauth accounts. 
+sub emailuser ($) {
+       my $user=shift;
+       if (defined $user && $user =~ m/(.+)@/) {
+               my $nick=$1;
+               # remove any characters from not allowed in wiki files
+               # support use w/o %config set
+               my $chars = defined $config{wiki_file_chars} ? $config{wiki_file_chars} : "-[:alnum:]+/.:_";
+               $nick=~s/[^$chars]/_/g;
+               return $nick;
+       }
+       return;
+}
+
+# Some user information should not be exposed in commit metadata, etc.
+# This generates a cloaked form of such information.
+sub cloak ($) {
+       my $user=shift;
+       # cloak email address using http://xmlns.com/foaf/spec/#term_mbox_sha1sum
+       if ($user=~m/(.+)@/) {
+               my $nick=$1;
+               eval q{use Digest::SHA};
+               return $user if $@;
+               return $nick.'@'.Digest::SHA::sha1_hex("mailto:$user");
+       }
+       else {
+               return $user;
+       }
+}
+
 sub htmlize ($$$$) {
        my $page=shift;
        my $destpage=shift;
 sub htmlize ($$$$) {
        my $page=shift;
        my $destpage=shift;
@@ -1535,6 +1647,8 @@ sub preprocess ($$$;$$) {
                                if ($@) {
                                        my $error=$@;
                                        chomp $error;
                                if ($@) {
                                        my $error=$@;
                                        chomp $error;
+                                       eval q{use HTML::Entities};
+                                       $error = encode_entities($error);
                                        $ret="[[!$command <span class=\"error\">".
                                                gettext("Error").": $error"."</span>]]";
                                }
                                        $ret="[[!$command <span class=\"error\">".
                                                gettext("Error").": $error"."</span>]]";
                                }
@@ -1771,8 +1885,11 @@ sub lockwiki () {
        }
        open($wikilock, '>', "$config{wikistatedir}/lockfile") ||
                error ("cannot write to $config{wikistatedir}/lockfile: $!");
        }
        open($wikilock, '>', "$config{wikistatedir}/lockfile") ||
                error ("cannot write to $config{wikistatedir}/lockfile: $!");
-       if (! flock($wikilock, 2)) { # LOCK_EX
-               error("failed to get lock");
+       if (! flock($wikilock, LOCK_EX | LOCK_NB)) {
+               debug("failed to get lock; waiting...");
+               if (! flock($wikilock, LOCK_EX)) {
+                       error("failed to get lock");
+               }
        }
        return 1;
 }
        }
        return 1;
 }