X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=C4%2FTemplates.pm;h=dd54a6e3f4128e2dfce610c8ee1ddba876325683;hb=ad83d2e07d3c2c28e962b1a2e8d04b6f25a4a868;hp=47bda045b25556f7cffe3d60e9f8f6e88e6145fe;hpb=fbc309bdde3138fb218f40597d2a98e8773b2226;p=srvgit diff --git a/C4/Templates.pm b/C4/Templates.pm index 47bda045b2..dd54a6e3f4 100644 --- a/C4/Templates.pm +++ b/C4/Templates.pm @@ -2,41 +2,43 @@ package C4::Templates; use strict; use warnings; -use Carp; -use CGI; -use List::MoreUtils qw/any/; +use Carp qw( carp ); +use CGI qw ( -utf8 ); +use List::MoreUtils qw( uniq ); # Copyright 2009 Chris Cormack and The Koha Dev Team # # 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 . =head1 NAME - Koha::Templates - Object for manipulating templates for use with Koha +C4::Templates - Object for manipulating templates for use with Koha =cut use base qw(Class::Accessor); use Template; -use Template::Constants qw( :debug ); -use C4::Languages qw(getTranslatedLanguages get_bidi regex_lang_subtags language_get_description accept_language ); +use C4::Languages qw( get_bidi getTranslatedLanguages regex_lang_subtags ); use C4::Context; -__PACKAGE__->mk_accessors(qw( theme lang filename htdocs interface vars)); +use Koha::Cache::Memory::Lite; +use Koha::Exceptions; + +__PACKAGE__->mk_accessors(qw( theme activethemes preferredtheme lang filename htdocs interface vars)); @@ -53,18 +55,23 @@ sub new { else { $htdocs = C4::Context->config('intrahtdocs'); } - my ($theme, $lang)= themelanguage( $htdocs, $tmplbase, $interface, $query); + my ($theme, $lang, $activethemes)= themelanguage( $htdocs, $tmplbase, $interface, $query); + my @includes; + foreach (@$activethemes) { + push @includes, "$htdocs/$_/$lang/includes"; + push @includes, "$htdocs/$_/en/includes" unless $lang eq 'en'; + } + # Do not use template cache if script is called from commandline + my $use_template_cache = C4::Context->config('template_cache_dir') && defined $ENV{GATEWAY_INTERFACE}; my $template = Template->new( { EVAL_PERL => 1, ABSOLUTE => 1, PLUGIN_BASE => 'Koha::Template::Plugin', - COMPILE_EXT => C4::Context->config('template_cache_dir')?'.ttc':'', - COMPILE_DIR => C4::Context->config('template_cache_dir')?C4::Context->config('template_cache_dir'):'',, - INCLUDE_PATH => [ - "$htdocs/$theme/$lang/includes", - "$htdocs/$theme/en/includes" - ], + COMPILE_EXT => $use_template_cache ? '.ttc' : '', + COMPILE_DIR => $use_template_cache ? C4::Context->config('template_cache_dir') : '', + INCLUDE_PATH => \@includes, FILTERS => {}, + ENCODING => 'UTF-8', } ) or die Template->error(); my $self = { @@ -74,6 +81,8 @@ sub new { bless $self, $class; $self->theme($theme); $self->lang($lang); + $self->activethemes($activethemes); + $self->preferredtheme($activethemes->[0]); $self->filename($filename); $self->htdocs($htdocs); $self->interface($interface); @@ -95,84 +104,31 @@ sub output { $vars->{themelang} = '/opac-tmpl'; } $vars->{lang} = $self->lang; - $vars->{themelang} .= '/' . $self->theme . '/' . $self->lang; - $vars->{yuipath} = - ( C4::Context->preference("yuipath") eq "local" - ? $vars->{themelang} . "/lib/yui" - : C4::Context->preference("yuipath") ); + $vars->{themelang} .= '/' . $self->preferredtheme . '/' . $self->lang; $vars->{interface} = ( $self->{interface} ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' ); $vars->{theme} = $self->theme; - $vars->{opaccolorstylesheet} = - C4::Context->preference('opaccolorstylesheet'); - $vars->{opacsmallimage} = C4::Context->preference('opacsmallimage'); - $vars->{opacstylesheet} = C4::Context->preference('opacstylesheet'); + $vars->{OpacAdditionalStylesheet} = + C4::Context->preference('OpacAdditionalStylesheet'); + $vars->{opaclayoutstylesheet} = + C4::Context->preference('opaclayoutstylesheet'); + + if(exists $self->{VARS}{lang}) { + warn "Preventing \$template->lang='" . ($self->{vars}{lang}//'-undef-') + . "' to be overwritten by template->{VARS}{lang}='" . ($self->{VARS}{lang}//'-undef-') . "'"; + delete $self->{VARS}{lang}; + } # add variables set via param to $vars for processing - # and clean any utf8 mess - for my $k ( keys %{ $self->{VARS} } ) { - $vars->{$k} = $self->{VARS}->{$k}; - if (ref($vars->{$k}) eq 'ARRAY'){ - utf8_arrayref($vars->{$k}); - } - elsif (ref($vars->{$k}) eq 'HASH'){ - utf8_hashref($vars->{$k}); - } - else { - utf8::encode($vars->{$k}) if utf8::is_utf8($vars->{$k}); - } - } + $vars = { %$vars, %{ $self->{VARS} } }; + my $data; -# binmode( STDOUT, ":utf8" ); + binmode( STDOUT, ":encoding(UTF-8)" ); $template->process( $self->filename, $vars, \$data ) || die "Template process failed: ", $template->error(); return $data; } -sub utf8_arrayref { - my $arrayref = shift; - foreach my $element (@$arrayref){ - if (ref($element) eq 'ARRAY'){ - utf8_arrayref($element); - next; - } - if (ref($element) eq 'HASH'){ - utf8_hashref($element); - next; - } - utf8::encode($element) if utf8::is_utf8($element); - } -} - -sub utf8_hashref { - my $hashref = shift; - for my $key (keys %{$hashref}){ - if (ref($hashref->{$key}) eq 'ARRAY'){ - utf8_arrayref($hashref->{$key}); - next; - } - if (ref($hashref->{$key}) eq 'HASH'){ - utf8_hashref($hashref->{$key}); - next; - } - utf8::encode($hashref->{$key}) if utf8::is_utf8($hashref->{$key}); - } -} - - -# FIXME - this is a horrible hack to cache -# the current known-good language, temporarily -# put in place to resolve bug 4403. It is -# used only by C4::XSLT::XSLTParse4Display; -# the language is set via the usual call -# to themelanguage. -my $_current_language = 'en'; - -sub _current_language { - return $_current_language; -} - - # wrapper method to allow easier transition from HTML template pro to Template Toolkit sub param { my $self = shift; @@ -210,46 +166,78 @@ sub _get_template_file { my $is_intranet = $interface eq 'intranet'; my $htdocs = C4::Context->config($is_intranet ? 'intrahtdocs' : 'opachtdocs'); - my ($theme, $lang) = themelanguage($htdocs, $tmplbase, $interface, $query); - my $opacstylesheet = C4::Context->preference('opacstylesheet'); - - # if the template doesn't exist, load the English one as a last resort - my $filename = "$htdocs/$theme/$lang/modules/$tmplbase"; - unless (-f $filename) { - $lang = 'en'; - $filename = "$htdocs/$theme/$lang/modules/$tmplbase"; - } - return ($htdocs, $theme, $lang, $filename); + my ($theme, $lang, $availablethemes) = themelanguage($htdocs, $tmplbase, $interface, $query); + $lang //= 'en'; + $theme //= ''; + $tmplbase = "$htdocs/$theme/$lang/modules/$tmplbase" if $tmplbase !~ /^\//; + # do not prefix an absolute path + + 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 - make sure we operate on a copy of the config + my $dirs = C4::Context->config("pluginsdir"); + if ( !ref($dirs) ) { + $dirs = [ $dirs ]; + } + else { + $dirs = [ @$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 ) = @_; - ($query) or warn "no query in gettemplate"; - my $path = C4::Context->preference('intranet_includes') || 'includes'; - my $opacstylesheet = C4::Context->preference('opacstylesheet'); - $tmplbase =~ s/\.tmpl$/.tt/; my ($htdocs, $theme, $lang, $filename) = _get_template_file($tmplbase, $interface, $query); + badtemplatecheck( $filename ); # single trip for bad templates my $template = C4::Templates->new($interface, $filename, $tmplbase, $query); - my $is_intranet = $interface eq 'intranet'; - my $themelang = - ($is_intranet ? '/intranet-tmpl' : '/opac-tmpl') . - "/$theme/$lang"; - $template->param( - themelang => $themelang, - yuipath => C4::Context->preference("yuipath") eq "local" - ? "$themelang/lib/yui" - : C4::Context->preference("yuipath"), - interface => $is_intranet ? '/intranet-tmpl' : '/opac-tmpl', - theme => $theme, - lang => $lang - ); - # Bidirectionality +# NOTE: Commenting these out rather than deleting them so that those who need +# to know how we previously shimmed these directories will be able to understand. +# my $is_intranet = $interface eq 'intranet'; +# my $themelang = +# ($is_intranet ? '/intranet-tmpl' : '/opac-tmpl') . +# "/$theme/$lang"; +# $template->param( +# themelang => $themelang, +# interface => $is_intranet ? '/intranet-tmpl' : '/opac-tmpl', +# theme => $theme, +# lang => $lang +# ); + + # Bidirectionality, must be sent even if is the only language my $current_lang = regex_lang_subtags($lang); my $bidi; $bidi = get_bidi($current_lang->{script}) if $current_lang->{script}; + $template->param( + bidi => $bidi, + ); # Languages my $languages_loop = getTranslatedLanguages($interface,$theme,$lang); my $num_languages_enabled = 0; @@ -258,86 +246,112 @@ sub gettemplate { $num_languages_enabled++ if $sublang->{enabled}; } } + my $one_language_enabled = ($num_languages_enabled <= 1) ? 1 : 0; # deal with zero enabled langs as well $template->param( languages_loop => $languages_loop, - bidi => $bidi, - one_language_enabled => ($num_languages_enabled <= 1) ? 1 : 0, # deal with zero enabled langs as well - ) unless @$languages_loop<2; + one_language_enabled => $one_language_enabled, + ) unless $one_language_enabled; return $template; } -#--------------------------------------------------------------------------------------------------------- -# FIXME - POD +=head2 themelanguage + + my ($theme,$lang,\@themes) = themelanguage($htdocs,$tmpl,$interface,query); + +This function returns the theme and language to be used for rendering the UI. +It also returns the list of themes that should be applied as a fallback. This is +used for the theme overlay feature (i.e. if a file doesn't exist on the requested +theme, fallback to the configured fallback). + +Important: this function is used on the webinstaller too, so always consider +the use case where the DB is not populated already when rewriting/fixing. + +=cut + sub themelanguage { my ($htdocs, $tmpl, $interface, $query) = @_; - ($query) or warn "no query in themelanguage"; # Select a language based on cookie, syspref available languages & browser - my $lang = getlanguage($query, $interface); + my $lang = C4::Languages::getlanguage($query); - # Select theme - my $is_intranet = $interface eq 'intranet'; - my @themes = split(" ", C4::Context->preference( - $is_intranet ? "template" : "opacthemes" )); - push @themes, 'prog'; + return availablethemes($htdocs, $tmpl, $interface, $lang); +} - # Try to find first theme for the selected language +sub availablethemes { + my ($htdocs, $tmpl, $interface, $lang) = @_; + + # Get theme + my @themes; + my $theme_syspref = ($interface eq 'intranet') ? 'template' : 'opacthemes'; + my $fallback_syspref = ($interface eq 'intranet') ? 'template' : 'OPACFallback'; + # Yeah, hardcoded, last resort if the DB is not populated + my $hardcoded_theme = ($interface eq 'intranet') ? 'prog' : 'bootstrap'; + + # Configured theme is the first one + push @themes, C4::Context->preference( $theme_syspref ) + if C4::Context->preference( $theme_syspref ); + # Configured fallback next + push @themes, C4::Context->preference( $fallback_syspref ) + if C4::Context->preference( $fallback_syspref ); + # The hardcoded fallback theme is the last one + push @themes, $hardcoded_theme; + + # Try to find first theme for the selected theme/lang, then for fallback/lang + my $where = $tmpl =~ /xsl$/ ? 'xslt' : 'modules'; for my $theme (@themes) { - if ( -e "$htdocs/$theme/$lang/modules/$tmpl" ) { - $_current_language = $lang; - return ($theme, $lang) + if ( -e "$htdocs/$theme/$lang/$where/$tmpl" ) { + return ( $theme, $lang, [ uniq(@themes) ] ); } } - # Otherwise, return prog theme in English 'en' - return ('prog', 'en'); + # Otherwise return theme/'en', last resort fallback/'en' + for my $theme (@themes) { + if ( -e "$htdocs/$theme/en/$where/$tmpl" ) { + 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) ] ); + } } - sub setlanguagecookie { my ( $query, $language, $uri ) = @_; - my $cookie = $query->cookie( - -name => 'KohaOpacLanguage', - -value => $language, - -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 ); } +=head2 getlanguagecookie -sub getlanguage { - my ($query, $interface) = @_; - - # Select a language based on cookie, syspref available languages & browser - my $preference_to_check = - $interface eq 'intranet' ? 'language' : 'opaclanguages'; - my @languages = split /,/, C4::Context->preference($preference_to_check); - - my $lang; + my $cookie = getlanguagecookie($query,$language); - # cookie - if ( $query->cookie('KohaOpacLanguage') ) { - $lang = $query->cookie('KohaOpacLanguage'); - $lang =~ s/[^a-zA-Z_-]*//; # sanitize cookie - } +Returns a cookie object containing the calculated language to be used. - # HTTP_ACCEPT_LANGUAGE - if ( !$lang && $ENV{HTTP_ACCEPT_LANGUAGE} ) { - $lang = accept_language( $ENV{HTTP_ACCEPT_LANGUAGE}, - getTranslatedLanguages( $interface, 'prog' ) ); - } +=cut - # Ignore a lang not selected in sysprefs - if ( $lang && any { $_ eq $lang } @languages ) { - return $lang; - } +sub getlanguagecookie { + my ( $query, $language ) = @_; + my $cookie = $query->cookie( + -name => 'KohaOpacLanguage', + -value => $language, + -HttpOnly => 1, + -expires => '+3y', + -sameSite => 'Lax', + -secure => ( C4::Context->https_enabled() ? 1 : 0 ), + ); - # Fall back to English if necessary - return 'en'; + return $cookie; } 1;