diff --git a/classes/template/TemplateHandler.class.php b/classes/template/TemplateHandler.class.php index 59e6fb1ff..3f234f88c 100644 --- a/classes/template/TemplateHandler.class.php +++ b/classes/template/TemplateHandler.class.php @@ -1,561 +1,562 @@ xe_path = rtrim(preg_replace('/([^\.^\/]+)\.php$/i','',$_SERVER['SCRIPT_NAME']),'/'); + } + + /** + * @brief returns TemplateHandler's singleton object + * @return TemplateHandler instance + **/ + function &getInstance() + { + static $oTemplate = null; + + if(__DEBUG__==3 ) { + if(!isset($GLOBALS['__TemplateHandlerCalled__'])) $GLOBALS['__TemplateHandlerCalled__']=1; + else $GLOBALS['__TemplateHandlerCalled__']++; + } + + if(!$oTemplate) $oTemplate = new TemplateHandler(); + + return $oTemplate; + } + + /** + * @brief set variables for template compile + **/ + function init($tpl_path, $tpl_filename, $tpl_file='') + { + // verify arguments + if(substr($tpl_path,-1)!='/') $tpl_path .= '/'; + if(!file_exists($tpl_path.$tpl_filename)&&file_exists($tpl_path.$tpl_filename.'.html')) $tpl_filename .= '.html'; + + // create tpl_file variable + if(!$tpl_file) $tpl_file = $tpl_path.$tpl_filename; + + // set template file infos. + $this->path = $tpl_path; + $this->filename = $tpl_filename; + $this->file = $tpl_file; + + $this->web_path = $this->xe_path.'/'.ltrim(preg_replace('@^'.preg_quote(_XE_PATH_,'@').'|\./@','',$this->path),'/'); + + // get compiled file name + $hash = md5($this->file . __ZBXE_VERSION__); + $this->compiled_file = "{$this->compiled_path}{$hash}.compiled.php"; + + // compare various file's modified time for check changed + $this->handler_mtime = filemtime(__FILE__); + + $skip = array(''); + } + + /** + * @brief compiles specified tpl file and execution result in Context into resultant content + * @param[in] $tpl_path path of the directory containing target template file + * @param[in] $tpl_filename target template file's name + * @param[in] $tpl_file if specified use it as template file's full path + * @return Returns compiled result in case of success, NULL otherwise + */ + function compile($tpl_path, $tpl_filename, $tpl_file='') { + global $__templatehandler_root_tpl; + + $buff = ''; + + // store the starting time for debug information + if(__DEBUG__==3 ) $start = getMicroTime(); + + // initiation + $this->init($tpl_path, $tpl_filename, $tpl_file); + + // if target file does not exist exit + if(!$this->file || !file_exists($this->file)) return "Err : '{$this->file}' template file does not exists."; + + // for backward compatibility + if(is_null($__templatehandler_root_tpl)) { + $__templatehandler_root_tpl = $this->file; + } + + $source_template_mtime = filemtime($this->file); + $latest_mtime = $source_template_mtime>$this->handler_mtime?$source_template_mtime:$this->handler_mtime; + + // cache control + $oCacheHandler = &CacheHandler::getInstance('template'); + + // get cached buff + if($oCacheHandler->isSupport()){ + $cache_key = 'template:'.$this->file; + $buff = $oCacheHandler->get($cache_key, $latest_mtime); + } else { + if(is_readable($this->compiled_file) && filemtime($this->compiled_file)>$latest_mtime && filesize($this->compiled_file)) { + $buff = 'file://'.$this->compiled_file; + } + } + + if(!$buff) { + $buff = $this->parse(); + if($oCacheHandler->isSupport()) $oCacheHandler->put($cache_key, $buff); + else FileHandler::writeFile($this->compiled_file, $buff); + } + + $output = $this->_fetch($buff); + + if($__templatehandler_root_tpl == $this->file) { $__templatehandler_root_tpl = null; - - $this->xe_path = rtrim(preg_replace('/([^\.^\/]+)\.php$/i','',$_SERVER['SCRIPT_NAME']),'/'); } - /** - * @brief returns TemplateHandler's singleton object - * @return TemplateHandler instance - **/ - function &getInstance() - { - static $oTemplate = null; + // store the ending time for debug information + if(__DEBUG__==3 ) $GLOBALS['__template_elapsed__'] += getMicroTime() - $start; - if(__DEBUG__==3 ) { - if(!isset($GLOBALS['__TemplateHandlerCalled__'])) $GLOBALS['__TemplateHandlerCalled__']=1; - else $GLOBALS['__TemplateHandlerCalled__']++; - } + return $output; + } - if(!$oTemplate) $oTemplate = new TemplateHandler(); + /** + * @brief compile specified file and immediately return + * @param[in] $tpl_path path of the directory containing target template file + * @param[in] $tpl_filename target template file's name + * @return Returns compiled content in case of success or NULL in case of failure + **/ + function compileDirect($tpl_path, $tpl_filename) { + $this->init($tpl_path, $tpl_filename, null); - return $oTemplate; - } - - /** - * @brief set variables for template compile - **/ - function init($tpl_path, $tpl_filename, $tpl_file='') - { - // verify arguments - if(substr($tpl_path,-1)!='/') $tpl_path .= '/'; - if(!file_exists($tpl_path.$tpl_filename)&&file_exists($tpl_path.$tpl_filename.'.html')) $tpl_filename .= '.html'; - - // create tpl_file variable - if(!$tpl_file) $tpl_file = $tpl_path.$tpl_filename; - - // set template file infos. - $this->path = $tpl_path; - $this->filename = $tpl_filename; - $this->file = $tpl_file; - - $this->web_path = $this->xe_path.'/'.ltrim(preg_replace('@^'.preg_quote(_XE_PATH_,'@').'|\./@','',$this->path),'/'); - - // get compiled file name - $hash = md5($this->file . __ZBXE_VERSION__); - $this->compiled_file = "{$this->compiled_path}{$hash}.compiled.php"; - - // compare various file's modified time for check changed - $this->handler_mtime = filemtime(__FILE__); - - $skip = array(''); + // if target file does not exist exit + if(!$this->file || !file_exists($this->file)) { + Context::close(); + exit("Cannot find the template file: '{$this->file}'"); } - /** - * @brief compiles specified tpl file and execution result in Context into resultant content - * @param[in] $tpl_path path of the directory containing target template file - * @param[in] $tpl_filename target template file's name - * @param[in] $tpl_file if specified use it as template file's full path - * @return Returns compiled result in case of success, NULL otherwise - */ - function compile($tpl_path, $tpl_filename, $tpl_file='') { - global $__templatehandler_root_tpl; + return $this->parse(); + } - $buff = ''; + /** + * @brief compile a template file specified in $tpl_file and + * @pre files specified by $tpl_file exists. + * @param[in] $tpl_file path of tpl file + * @param[in] $compiled_tpl_file if specified, write compiled result into the file + * @return compiled result in case of success or NULL in case of error + **/ + function parse($buff=null) { + if(is_null($buff)) { + if(!is_readable($this->file)) return; - // store the starting time for debug information - if(__DEBUG__==3 ) $start = getMicroTime(); + // read tpl file + $buff = FileHandler::readFile($this->file); + } - // initiation - $this->init($tpl_path, $tpl_filename, $tpl_file); + // HTML tags to skip + if(is_null($this->skipTags)) { + $this->skipTags = array('marquee'); + } - // if target file does not exist exit - if(!$this->file || !file_exists($this->file)) return "Err : '{$this->file}' template file does not exists."; + // replace comments + $buff = preg_replace('@@s', '', $buff); - // for backward compatibility - if(is_null($__templatehandler_root_tpl)) { - $__templatehandler_root_tpl = $this->file; - } + // replace value of src in img/input/script tag + $buff = preg_replace_callback('/<(?:img|input|script)(?:(?!["\'\/]\s*>).)* src="(?!https?:\/\/|[\/\{])([^"]+)"/is', array($this, '_replacePath'), $buff); - $source_template_mtime = filemtime($this->file); - $latest_mtime = $source_template_mtime>$this->handler_mtime?$source_template_mtime:$this->handler_mtime; + // replace loop and cond template syntax + $buff = $this->_parseInline($buff); - // cache control - $oCacheHandler = &CacheHandler::getInstance('template'); + // include, unload/load, import + $buff = preg_replace_callback('/{(@[\s\S]+?|(?=\$\w+|_{1,2}[A-Z]+|[!\(+-]|\w+(?:\(|::)|\d+|[\'"].*?[\'"]).+?)}|<(!--[#%])?(include|import|(un)?load(?(4)|(?:_js_plugin)?))(?(2)\("([^"]+)")(.*?)(?(2)\)--|\/)>|/', array($this, '_parseResource'), $buff); - // get cached buff - if($oCacheHandler->isSupport()){ - $cache_key = 'template:'.$this->file; - $buff = $oCacheHandler->get($cache_key, $latest_mtime); - } else { - if(is_readable($this->compiled_file) && filemtime($this->compiled_file)>$latest_mtime && filesize($this->compiled_file)) { - $buff = 'file://'.$this->compiled_file; - } - } + // remove block which is a virtual tag and remove comments + $buff = preg_replace('@?block\s*>|\s?@is','',$buff); - if(!$buff) { - $buff = $this->parse(); - if($oCacheHandler->isSupport()) $oCacheHandler->put($cache_key, $buff); - else FileHandler::writeFile($this->compiled_file, $buff); - } + // form auto generation + $buff = preg_replace_callback('/(