Bug 18925: Move maxissueqty and maxonsiteissueqty to circulation_rules
[koha.git] / C4 / Templates.pm
index 52a7229..343a374 100644 (file)
@@ -10,22 +10,22 @@ use List::MoreUtils qw/ any uniq /;
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along with
-# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 =head1 NAME 
 
-    Koha::Templates - Object for manipulating templates for use with Koha
+C4::Templates - Object for manipulating templates for use with Koha
 
 =cut
 
@@ -36,6 +36,9 @@ use C4::Languages qw(getTranslatedLanguages get_bidi regex_lang_subtags language
 
 use C4::Context;
 
+use Koha::Cache::Memory::Lite;
+use Koha::Exceptions;
+
 __PACKAGE__->mk_accessors(qw( theme activethemes preferredtheme lang filename htdocs interface vars));
 
 
@@ -106,15 +109,13 @@ sub output {
     $vars->{interface} =
       ( $self->{interface} ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' );
     $vars->{theme} = $self->theme;
-    $vars->{opaccolorstylesheet} =
-        C4::Context->preference('opaccolorstylesheet');
+    $vars->{OpacAdditionalStylesheet} =
+        C4::Context->preference('OpacAdditionalStylesheet');
     $vars->{opaclayoutstylesheet} =
         C4::Context->preference('opaclayoutstylesheet');
 
     # add variables set via param to $vars for processing
-    for my $k ( keys %{ $self->{VARS} } ) {
-        $vars->{$k} = $self->{VARS}->{$k};
-    }
+    $vars = { %$vars, %{ $self->{VARS} } };
 
     my $data;
     binmode( STDOUT, ":utf8" );
@@ -161,19 +162,51 @@ sub _get_template_file {
     my $is_intranet = $interface eq 'intranet';
     my $htdocs = C4::Context->config($is_intranet ? 'intrahtdocs' : 'opachtdocs');
     my ($theme, $lang, $availablethemes) = themelanguage($htdocs, $tmplbase, $interface, $query);
-    my $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
+    $lang //= 'en';
+    $theme //= '';
+    $tmplbase = "$htdocs/$theme/$lang/modules/$tmplbase" if $tmplbase !~ /^\//;
+        # do not prefix an absolute path
 
-    return ($htdocs, $theme, $lang, $filename);
+    return ( $htdocs, $theme, $lang, $tmplbase );
 }
 
+=head2 badtemplatecheck
+
+    badtemplatecheck( $template_path );
+
+    The sub will throw an exception if the template path is not allowed.
+
+    Note: At this moment the sub is actually a helper routine for
+    sub gettemplate.
+
+=cut
+
+sub badtemplatecheck {
+    my ( $template ) = @_;
+    if( !$template || $template !~ m/^[a-zA-Z0-9_\-\/]+\.(tt|pref)$/ ) {
+        # This also includes two dots
+        Koha::Exceptions::NoPermission->throw( 'bad template path' );
+    } else {
+        # Check allowed dirs
+        my $dirs = C4::Context->config("pluginsdir");
+        $dirs = [ $dirs ] if !ref($dirs);
+        unshift @$dirs, C4::Context->config('opachtdocs'), C4::Context->config('intrahtdocs');
+        my $found = 0;
+        foreach my $dir ( @$dirs ) {
+            $dir .= '/' if $dir !~ m/\/$/;
+            $found++ if $template =~ m/^$dir/;
+            last if $found;
+        }
+        Koha::Exceptions::NoPermission->throw( 'bad template path' ) if !$found;
+    }
+}
 
 sub gettemplate {
-    my ( $tmplbase, $interface, $query, $is_plugin ) = @_;
+    my ( $tmplbase, $interface, $query ) = @_;
     ($query) or warn "no query in gettemplate";
-    my $path = C4::Context->preference('intranet_includes') || 'includes';
     my ($htdocs, $theme, $lang, $filename)
        =  _get_template_file($tmplbase, $interface, $query);
-    $filename = $tmplbase if ( $is_plugin );
+    badtemplatecheck( $filename ); # single trip for bad templates
     my $template = C4::Templates->new($interface, $filename, $tmplbase, $query);
 
 # NOTE: Commenting these out rather than deleting them so that those who need
@@ -255,18 +288,18 @@ sub themelanguage {
     my $where = $tmpl =~ /xsl$/ ? 'xslt' : 'modules';
     for my $theme (@themes) {
         if ( -e "$htdocs/$theme/$lang/$where/$tmpl" ) {
-            return ( $theme, $lang, uniq( \@themes ) );
+            return ( $theme, $lang, [ uniq(@themes) ] );
         }
     }
     # Otherwise return theme/'en', last resort fallback/'en'
     for my $theme (@themes) {
         if ( -e "$htdocs/$theme/en/$where/$tmpl" ) {
-            return ( $theme, 'en', uniq( \@themes ) );
+            return ( $theme, 'en', [ uniq(@themes) ] );
         }
     }
     # tmpl is a full path, so this is a template for a plugin
     if ( $tmpl =~ /^\// && -e $tmpl ) {
-        return ( $themes[0], $lang, uniq( \@themes ) );
+        return ( $themes[0], $lang, [ uniq(@themes) ] );
     }
 }
 
@@ -274,12 +307,12 @@ sub themelanguage {
 sub setlanguagecookie {
     my ( $query, $language, $uri ) = @_;
 
-    my $cookie = $query->cookie(
-        -name    => 'KohaOpacLanguage',
-        -value   => $language,
-        -HttpOnly => 1,
-        -expires => '+3y'
-    );
+    my $cookie = getlanguagecookie( $query, $language );
+
+    # We do not want to set getlanguage in cache, some additional checks are
+    # done in C4::Languages::getlanguage
+    Koha::Cache::Memory::Lite->get_instance()->clear_from_cache( 'getlanguage' );
+
     print $query->redirect(
         -uri    => $uri,
         -cookie => $cookie
@@ -331,7 +364,7 @@ sub GetColumnDefs {
     # Build columns.def path
     my $path = "$htdocs/$theme/$lang/$columns_file";
     my $fh;
-    if ( ! open ( $fh, q{<}, $path ) )  {
+    if ( ! open ( $fh, q{<:encoding(utf-8)}, $path ) )  {
         carp "Error opening $path. Check your templates.";
         return;
     }