var $html_head_elements = array(); private $html_style = ''; const COMBINED_SCRIPTS_TAG = ''; var $scriptLoader; const COMBINED_CSS_TAG = ''; var $css_by_priority = array(); var $picture_buttons = array(); var $index_buttons = array(); function Template($root = ".", $theme= "", $path = "template") { global $conf, $lang_info; SmartyException::$escape = false; $this->scriptLoader = new ScriptLoader; $this->smarty = new SmartyBC; $this->smarty->debugging = $conf['debug_template']; if (!$this->smarty->debugging) $this->smarty->error_reporting = error_reporting() & ~E_NOTICE; $this->smarty->compile_check = $conf['template_compile_check']; $this->smarty->force_compile = $conf['template_force_compile']; if (!isset($conf['data_dir_checked'])) { $dir = PHPWG_ROOT_PATH.$conf['data_location']; mkgetdir($dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR); if (!is_writable($dir)) { load_language('admin.lang'); fatal_error( sprintf( l10n('Give write access (chmod 777) to "%s" directory at the root of your Piwigo installation'), $conf['data_location'] ), l10n('an error happened'), false // show trace ); } if (function_exists('pwg_query')) { conf_update_param('data_dir_checked', 1); } } $compile_dir = PHPWG_ROOT_PATH.$conf['data_location'].'templates_c'; mkgetdir( $compile_dir ); $this->smarty->setCompileDir($compile_dir); $this->smarty->assign( 'pwg', new PwgTemplateAdapter() ); $this->smarty->registerPlugin('modifiercompiler', 'translate', array('Template', 'modcompiler_translate') ); $this->smarty->registerPlugin('modifiercompiler', 'translate_dec', array('Template', 'modcompiler_translate_dec') ); $this->smarty->registerPlugin('modifier', 'explode', array('Template', 'mod_explode') ); $this->smarty->registerPlugin('modifier', 'get_extent', array($this, 'get_extent') ); $this->smarty->registerPlugin('block', 'html_head', array($this, 'block_html_head') ); $this->smarty->registerPlugin('block', 'html_style', array($this, 'block_html_style') ); $this->smarty->registerPlugin('function', 'combine_script', array($this, 'func_combine_script') ); $this->smarty->registerPlugin('function', 'get_combined_scripts', array($this, 'func_get_combined_scripts') ); $this->smarty->registerPlugin('function', 'combine_css', array($this, 'func_combine_css') ); $this->smarty->registerPlugin('function', 'define_derivative', array($this, 'func_define_derivative') ); $this->smarty->registerPlugin('compiler', 'get_combined_css', array($this, 'func_get_combined_css') ); $this->smarty->registerPlugin('block', 'footer_script', array($this, 'block_footer_script') ); $this->smarty->registerFilter('pre', array('Template', 'prefilter_white_space') ); if ( $conf['compiled_template_cache_language'] ) { $this->smarty->registerFilter('post', array('Template', 'postfilter_language') ); } $this->smarty->setTemplateDir(array()); if ( !empty($theme) ) { $this->set_theme($root, $theme, $path); if (!defined('IN_ADMIN')) { $this->set_prefilter( 'header', array('Template', 'prefilter_local_css') ); } } else $this->set_template_dir($root); $this->smarty->assign('lang_info', $lang_info); if (!defined('IN_ADMIN') and isset($conf['extents_for_templates'])) { $tpl_extents = unserialize($conf['extents_for_templates']); $this->set_extents($tpl_extents, './template-extension/', true, $theme); } } /** * Load theme's parameters. */ function set_theme($root, $theme, $path, $load_css=true, $load_local_head=true) { $this->set_template_dir($root.'/'.$theme.'/'.$path); $themeconf = $this->load_themeconf($root.'/'.$theme); if (isset($themeconf['parent']) and $themeconf['parent'] != $theme) { $this->set_theme( $root, $themeconf['parent'], $path, isset($themeconf['load_parent_css']) ? $themeconf['load_parent_css'] : $load_css, isset($themeconf['load_parent_local_head']) ? $themeconf['load_parent_local_head'] : $load_local_head ); } $tpl_var = array( 'id' => $theme, 'load_css' => $load_css, ); if (!empty($themeconf['local_head']) and $load_local_head) { $tpl_var['local_head'] = realpath($root.'/'.$theme.'/'.$themeconf['local_head'] ); } $themeconf['id'] = $theme; $this->smarty->append('themes', $tpl_var); $this->smarty->append('themeconf', $themeconf, true); } /** * Add template directory for this Template object. * Set compile id if not exists. */ function set_template_dir($dir) { $this->smarty->addTemplateDir($dir); if (!isset($this->smarty->compile_id)) { $compile_id = "1"; $compile_id .= ($real_dir = realpath($dir))===false ? $dir : $real_dir; $this->smarty->compile_id = base_convert(crc32($compile_id), 10, 36 ); } } /** * Gets the template root directory for this Template object. */ function get_template_dir() { return $this->smarty->getTemplateDir(); } /** * Deletes all compiled templates. */ function delete_compiled_templates() { $save_compile_id = $this->smarty->compile_id; $this->smarty->compile_id = null; $this->smarty->clearCompiledTemplate(); $this->smarty->compile_id = $save_compile_id; file_put_contents($this->smarty->getCompileDir().'/index.htm', 'Not allowed!'); } function get_themeconf($val) { $tc = $this->smarty->getTemplateVars('themeconf'); return isset($tc[$val]) ? $tc[$val] : ''; } /** * Sets the template filename for handle. */ function set_filename($handle, $filename) { return $this->set_filenames( array($handle=>$filename) ); } /** * Sets the template filenames for handles. $filename_array should be a * hash of handle => filename pairs. */ function set_filenames($filename_array) { if (!is_array($filename_array)) { return false; } reset($filename_array); while(list($handle, $filename) = each($filename_array)) { if (is_null($filename)) { unset($this->files[$handle]); } else { $this->files[$handle] = $this->get_extent($filename, $handle); } } return true; } /** * Sets template extention filename for handles. */ function set_extent($filename, $param, $dir='', $overwrite=true, $theme='N/A') { return $this->set_extents(array($filename => $param), $dir, $overwrite); } /** * Sets template extentions filenames for handles. * $filename_array should be an hash of filename => array( handle, param) or filename => handle */ function set_extents($filename_array, $dir='', $overwrite=true, $theme='N/A') { if (!is_array($filename_array)) { return false; } foreach ($filename_array as $filename => $value) { if (is_array($value)) { $handle = $value[0]; $param = $value[1]; $thm = $value[2]; } elseif (is_string($value)) { $handle = $value; $param = 'N/A'; $thm = 'N/A'; } else { return false; } if ((stripos(implode('',array_keys($_GET)), '/'.$param) !== false or $param == 'N/A') and ($thm == $theme or $thm == 'N/A') and (!isset($this->extents[$handle]) or $overwrite) and file_exists($dir . $filename)) { $this->extents[$handle] = realpath($dir . $filename); } } return true; } /** return template extension if exists */ function get_extent($filename='', $handle='') { if (isset($this->extents[$handle])) { $filename = $this->extents[$handle]; } return $filename; } /** see smarty assign http://www.smarty.net/manual/en/api.assign.php */ function assign($tpl_var, $value = null) { $this->smarty->assign( $tpl_var, $value ); } /** * Inserts the uncompiled code for $handle as the value of $varname in the * root-level. This can be used to effectively include a template in the * middle of another template. * This is equivalent to assign($varname, $this->parse($handle, true)) */ function assign_var_from_handle($varname, $handle) { $this->assign($varname, $this->parse($handle, true)); return true; } /** see smarty append http://www.smarty.net/manual/en/api.append.php */ function append($tpl_var, $value=null, $merge=false) { $this->smarty->append( $tpl_var, $value, $merge ); } /** * Root-level variable concatenation. Appends a string to an existing * variable assignment with the same name. */ function concat($tpl_var, $value) { $this->assign($tpl_var, $this->smarty->getTemplateVars($tpl_var) . $value); } /** see smarty append http://www.smarty.net/manual/en/api.clear_assign.php */ function clear_assign($tpl_var) { $this->smarty->clearAssign( $tpl_var ); } /** see smarty get_template_vars http://www.smarty.net/manual/en/api.get_template_vars.php */ function get_template_vars($name=null) { return $this->smarty->getTemplateVars( $name ); } /** * Load the file for the handle, eventually compile the file and run the compiled * code. This will add the output to the results or return the result if $return * is true. */ function parse($handle, $return=false) { if ( !isset($this->files[$handle]) ) { fatal_error("Template->parse(): Couldn't load template file for handle $handle"); } $this->smarty->assign( 'ROOT_URL', get_root_url() ); $save_compile_id = $this->smarty->compile_id; $this->load_external_filters($handle); global $conf, $lang_info; if ( $conf['compiled_template_cache_language'] and isset($lang_info['code']) ) { $this->smarty->compile_id .= '_'.$lang_info['code']; } $v = $this->smarty->fetch($this->files[$handle]); $this->smarty->compile_id = $save_compile_id; $this->unload_external_filters($handle); if ($return) { return $v; } $this->output .= $v; } /** * Load the file for the handle, eventually compile the file and run the compiled * code. This will print out the results of executing the template. */ function pparse($handle) { $this->parse($handle, false); $this->flush(); } function flush() { if (!$this->scriptLoader->did_head()) { $pos = strpos( $this->output, self::COMBINED_SCRIPTS_TAG ); if ($pos !== false) { $scripts = $this->scriptLoader->get_head_scripts(); $content = array(); foreach ($scripts as $script) { $content[]= ''; } $this->output = substr_replace( $this->output, implode( "\n", $content ), $pos, strlen(self::COMBINED_SCRIPTS_TAG) ); } //else maybe error or warning ? } if(!empty($this->css_by_priority)) { ksort($this->css_by_priority); global $conf; $css = array(); if ($conf['template_combine_files']) { $combiner = new FileCombiner('css'); foreach ($this->css_by_priority as $files) { foreach ($files as $file_ver) $combiner->add( $file_ver[0], $file_ver[1] ); } if ( $combiner->combine( $out_file, $out_version) ) $css[] = array($out_file, $out_version); } else { foreach ($this->css_by_priority as $files) $css = array_merge($css, $files); } $content = array(); foreach( $css as $file_ver ) { $href = embellish_url(get_root_url().$file_ver[0]); if ($file_ver[1] !== false) $href .= '?v' . ($file_ver[1] ? $file_ver[1] : PHPWG_VERSION); // trigger the event for eventual use of a cdn $href = trigger_event('combined_css', $href, $file_ver[0], $file_ver[1]); $content[] = ''; } $this->output = str_replace(self::COMBINED_CSS_TAG, implode( "\n", $content ), $this->output ); $this->css_by_priority = array(); } if ( count($this->html_head_elements) || strlen($this->html_style) ) { $search = "\n"; $pos = strpos( $this->output, $search ); if ($pos !== false) { $rep = "\n".implode( "\n", $this->html_head_elements ); if (strlen($this->html_style)) { $rep.=''; } $this->output = substr_replace( $this->output, $rep, $pos, 0 ); } //else maybe error or warning ? $this->html_head_elements = array(); $this->html_style = ''; } echo $this->output; $this->output=''; } /** flushes the output */ function p() { $this->flush(); if ($this->smarty->debugging) { global $t2; $this->smarty->assign( array( 'AAAA_DEBUG_TOTAL_TIME__' => get_elapsed_time($t2, get_moment()) ) ); Smarty_Internal_Debug::display_debug($this->smarty); } } private static function get_php_str_val($str) { if (is_string($str) && strlen($str)>1) { if ( ($str[0]=='\'' && $str[strlen($str)-1]=='\'') || ($str[0]=='"' && $str[strlen($str)-1]=='"')) { eval('$tmp='.$str.';'); return $tmp; } } return null; } /** * translate variable modifier - translates a text to the currently loaded language */ static function modcompiler_translate($params) { global $conf, $lang; switch (count($params)) { case 1: if ($conf['compiled_template_cache_language'] && ($key=self::get_php_str_val($params[0])) !== null && isset($lang[$key]) ) { return var_export($lang[$key], true); } return 'l10n('.$params[0].')'; default: if ($conf['compiled_template_cache_language']) { $ret = 'sprintf('; $ret .= self::modcompiler_translate( array($params[0]) ); $ret .= ','. implode(',', array_slice($params, 1)); $ret .= ')'; return $ret; } return 'l10n('.$params[0].','.implode(',', array_slice($params, 1)).')'; } } static function modcompiler_translate_dec($params) { global $conf, $lang, $lang_info; if ($conf['compiled_template_cache_language']) { $ret = 'sprintf('; if ($lang_info['zero_plural']) { $ret .= '($tmp=('.$params[0].'))>1||$tmp==0'; } else { $ret .= '($tmp=('.$params[0].'))>1'; } $ret .= '?'; $ret .= self::modcompiler_translate( array($params[2]) ); $ret .= ':'; $ret .= self::modcompiler_translate( array($params[1]) ); $ret .= ',$tmp'; $ret .= ')'; return $ret; } return 'l10n_dec('.$params[1].','.$params[2].','.$params[0].')'; } /** * explode variable modifier - similar to php explode * 'Yes;No'|@explode:';' -> array('Yes', 'No') */ static function mod_explode($text, $delimiter=',') { return explode($delimiter, $text); } /** * This smarty "html_head" block allows to add content just before * element in the output after the head has been parsed. This is * handy in order to respect strict standards when