Falko Timme
2013-10-18 2d2fd172e1548dd24e1719accd0b856cff6a31a0
interface/lib/classes/tpl.inc.php
@@ -22,12 +22,12 @@
//
// $Id: class.tpl.inc.php,v 1.1 2003/07/08 12:31:10 platinum Exp $
//** check to avoid multiple including of class
//** check and avoid multiple loading of class
if (!defined('vlibTemplateClassLoaded')) {
    define('vlibTemplateClassLoaded', 1);
    global $conf;
      include_once ($conf['classpath'].'/tpl_error.inc.php');
      include_once ($conf['classpath'].'/tpl_ini.inc.php');
      include_once (ISPC_CLASS_PATH.'/tpl_error.inc.php');
      include_once (ISPC_CLASS_PATH.'/tpl_ini.inc.php');
    class tpl{
    
@@ -148,19 +148,18 @@
          
      
        /**
         * FUNCTION: newTemplate
         *
         * Usually called by the class constructor.
         * Stores the filename in $this->_tmplfilename.
         * Raises an error if the template file is not found.
         *
         * @param string $tmplfile full path to template file
         * @return boolean true
         * @access public
         */
        public function newTemplate($tmplfile)
        {
            if (!$tfile = $this->_fileSearch($tmplfile)) vlibTemplateError::raiseError('VT_ERROR_NOFILE',KILL,$tmplfile);
            if (!$tfile = $this->_fileSearch($tmplfile)){
                vlibTemplateError::raiseError('VT_ERROR_NOFILE', KILL, $tmplfile);
            }
            //* make sure that any parsing vars are cleared for the new template
            $this->_tmplfile = null;
@@ -172,19 +171,17 @@
            $this->_totalparsetime = null;
            //* reset debug module
            if ($this->_debug) $this->_debugReset();
            if ($this->_debug){
                $this->_debugReset();
            }
            $this->_tmplfilename = $tfile;
            return true;
        }
        /**
         * FUNCTION: setVar
         *
         * Sets variables to be used by the template
         * If $k is an array, then it will treat it as an associative array
         * using the keys as variable names and the values as variable values.
         *
         * @param mixed $k key to define variable name
         * @param mixed $v variable to assign to $k
         * @return boolean true/false
@@ -195,12 +192,12 @@
            if (is_array($k)) {
                foreach($k as $key => $value){
                    $key = ($this->OPTIONS['CASELESS']) ? strtolower(trim($key)) : trim($key);
                    if (preg_match('/^[A-Za-z_]+[A-Za-z0-9_]*$/', $key) && $value !== null ) {
                    if (preg_match('/^[A-Za-z]+[A-Za-z0-9_]*$/', $key) && $value !== null ) {
                        $this->_vars[$key] = $value;
                    }
                }
            } else {
                if (preg_match('/^[A-Za-z_]+[A-Za-z0-9_]*$/', $k) && $v !== null) {
                if (preg_match('/^[A-Za-z]+[A-Za-z0-9_]*$/', $k) && $v !== null) {
                    if ($this->OPTIONS['CASELESS']) $k = strtolower($k);
                    $this->_vars[trim($k)] = $v;
                } else {
@@ -211,12 +208,9 @@
        }
        
        /**
         * FUNCTION: setInclude
         *
         * Sets dynamic includes to be used by the template
         * If $k is an array, then it will treat it as an associative array
         * using the keys as variable names and the values as variable values.
         *
         * @param mixed $k key to define variable name
         * @param mixed $v variable to assign to $k
         * @return boolean true/false
@@ -235,11 +229,8 @@
      }
        /**
         * FUNCTION: unsetVar
         *
         * Unsets a variable which has already been set
         * Parse in all vars wanted for deletion in seperate parametres
         *
         * @param string var name to remove use: vlibTemplate::unsetVar(var[, var..])
         * @return boolean true/false returns true unless called with 0 params
         * @access public
@@ -252,46 +243,35 @@
            for ($i = 0; $i < $num_args; $i++) {
                $var = func_get_arg($i);
                if ($this->OPTIONS['CASELESS']) $var = strtolower($var);
                if (!preg_match('/^[A-Za-z_]+[A-Za-z0-9_]*$/', $var)) continue;
                if (!preg_match('/^[A-Za-z]+[A-Za-z0-9_]*$/', $var)) continue;
                unset($this->_vars[$var]);
            }
            return true;
        }
        /**
         * FUNCTION: getVars
         *
         * Gets all vars currently set in global namespace.
         *
         * @return array
         * @access public
         */
        public function getVars()
        {
            if (empty($this->_vars)) return false;
            return $this->_vars;
            return empty($this->_vars) ? false : $this->_vars;
        }
        /**
         * FUNCTION: getVar
         *
         * Gets a single var from the global namespace
         *
         * @return var
         * @access public
         */
        public function getVar($var)
        {
            if ($this->OPTIONS['CASELESS']) $var = strtolower($var);
            if (empty($var) || !isset($this->_vars[$var])) return false;
            return $this->_vars[$var];
            return (empty($var) || !isset($this->_vars[$var])) ? false : $this->_vars[$var];
        }
        /**
         * FUNCTION: setContextVars
         *
         * sets the GLOBAL_CONTEXT_VARS
         *
         * @return true
         * @access public
         */
@@ -312,10 +292,7 @@
        }
        /**
         * FUNCTION: setLoop
         *
         * Builds the loop construct for use with <TMPL_LOOP>.
         *
         * @param string $k string to define loop name
         * @param array $v array to assign to $k
         * @return boolean true/false
@@ -323,22 +300,22 @@
         */
        public function setLoop($k, $v)
        {
            if (is_array($v) && preg_match('/^[A-Za-z_]+[A-Za-z0-9_]*$/', $k)) {
            if (is_array($v) && preg_match('/^[A-Za-z]+[A-Za-z0-9_]*$/', $k)) {
                $k = ($this->OPTIONS['CASELESS']) ? strtolower(trim($k)) : trim($k);
                $this->_arrvars[$k] = array();
                if ($this->OPTIONS['SET_LOOP_VAR'] && !empty($v)) $this->setvar($k, 1);
                if (($this->_arrvars[$k] = $this->_arrayBuild($v)) == false) {
                    vlibTemplateError::raiseError('VT_WARNING_INVALID_ARR',WARNING,$k);
                    vlibTemplateError::raiseError('VT_WARNING_INVALID_ARR', WARNING, $k);
                } else {
                    $this->vars['_'.$k.'_num'] = count($v);
                }
            }
            return true;
        }
        /**
         * FUNCTION: setDbLoop [** EXPERIMENTAL **]
         *
         * [** EXPERIMENTAL **]
         * Function to create a loop from a Db result resource link.
         *
         * @param string $loopname to commit loop. If not set, will use last loopname set using newLoop()
         * @param string $result link to a Db result resource
         * @param string $db_type, type of db that the result resource belongs to.
@@ -347,18 +324,20 @@
         */
        public function setDbLoop($loopname, $result, $db_type = 'MYSQL')
        {
            $db_type = strtoupper($db_type);
            /*
         $db_type = strtoupper($db_type);
            if (!in_array($db_type, $this->allowed_loop_dbs)) {
                vlibTemplateError::raiseError('VT_WARNING_INVALID_LOOP_DB',WARNING, $db_type);
                vlibTemplateError::raiseError('VT_WARNING_INVALID_LOOP_DB', WARNING, $db_type);
                return false;
            }
            $loop_arr = array();
            // TODO: Are all these necessary as were onyl using mysql and possible postgres ? - pedro
            switch ($db_type) {
                case 'MYSQL':
                    if (get_resource_type($result) != 'mysql result') {
                        vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE',WARNING, $db_type);
                        vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE', WARNING, $db_type);
                        return false;
                    }
                    while($r = mysql_fetch_assoc($result)) {
@@ -368,7 +347,7 @@
                case 'POSTGRESQL':
                    if (get_resource_type($result) != 'pgsql result') {
                        vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE',WARNING, $db_type);
                        vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE', WARNING, $db_type);
                        return false;
                    }
@@ -381,7 +360,7 @@
                case 'INFORMIX':
                    if (!$result) {
                        vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE',WARNING, $db_type);
                        vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE', WARNING, $db_type);
                        return false;
                    }
                    while($r = ifx_fetch_row($result, 'NEXT')) {
@@ -391,7 +370,7 @@
                case 'INTERBASE':
                    if (get_resource_type($result) != 'interbase result') {
                        vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE',WARNING, $db_type);
                        vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE', WARNING, $db_type);
                        return false;
                    }
                    while($r = ibase_fetch_row($result)) {
@@ -401,7 +380,7 @@
                case 'INGRES':
                    if (!$result) {
                        vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE',WARNING, $db_type);
                        vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE', WARNING, $db_type);
                        return false;
                    }
                    while($r = ingres_fetch_array(INGRES_ASSOC, $result)) {
@@ -411,7 +390,7 @@
                case 'MSSQL':
                    if (get_resource_type($result) != 'mssql result') {
                        vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE',WARNING, $db_type);
                        vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE', WARNING, $db_type);
                        return false;
                    }
                    while($r = mssql_fetch_array($result)) {
@@ -421,7 +400,7 @@
                case 'MSQL':
                    if (get_resource_type($result) != 'msql result') {
                        vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE',WARNING, $db_type);
                        vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE', WARNING, $db_type);
                        return false;
                    }
                    while($r = msql_fetch_array($result, MSQL_ASSOC)) {
@@ -431,7 +410,7 @@
                case 'OCI8':
                    if (get_resource_type($result) != 'oci8 statement') {
                        vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE',WARNING, $db_type);
                        vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE', WARNING, $db_type);
                        return false;
                    }
                    while(OCIFetchInto($result, &$r, OCI_ASSOC+OCI_RETURN_LOBS)) {
@@ -441,7 +420,7 @@
                case 'ORACLE':
                    if (get_resource_type($result) != 'oracle Cursor') {
                        vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE',WARNING, $db_type);
                        vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE', WARNING, $db_type);
                        return false;
                    }
                    while(ora_fetch_into($result, &$r, ORA_FETCHINTO_ASSOC)) {
@@ -451,7 +430,7 @@
                case 'OVRIMOS':
                    if (!$result) {
                        vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE',WARNING, $db_type);
                        vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE', WARNING, $db_type);
                        return false;
                    }
                    while(ovrimos_fetch_into($result, &$r, 'NEXT')) {
@@ -461,7 +440,7 @@
                case 'SYBASE':
                    if (get_resource_type($result) != 'sybase-db result') {
                        vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE',WARNING, $db_type);
                        vlibTemplateError::raiseError('VT_WARNING_INVALID_RESOURCE', WARNING, $db_type);
                        return false;
                    }
@@ -472,13 +451,11 @@
            }
            $this->setLoop($loopname, $loop_arr);
            return true;
         */
        }
        /**
         * FUNCTION: newLoop
         *
         * Sets the name for the curent loop in the 3 step loop process.
         *
         * @param string $name string to define loop name
         * @return boolean true/false
         * @access public
@@ -495,10 +472,7 @@
        }
        /**
         * FUNCTION: addRow
         *
         * Adds a row to the current loop in the 3 step loop process.
         *
         * @param array $row loop row to add to current loop
         * @param string $loopname loop to which you want to add row, if not set will use last loop set using newLoop().
         * @return boolean true/false
@@ -509,7 +483,7 @@
            if (!$loopname) $loopname = $this->_currloopname[(count($this->_currloopname)-1)];
            if (!isset($this->_currloop[$loopname]) || empty($this->_currloopname)) {
                vlibTemplateError::raiseError('VT_WARNING_LOOP_NOT_SET',WARNING);
                vlibTemplateError::raiseError('VT_WARNING_LOOP_NOT_SET', WARNING);
                return false;
            }
            if (is_array($row)) {
@@ -521,11 +495,8 @@
        }
        /**
         * FUNCTION: addLoop
         *
         * Completes the 3 step loop process. This assigns the rows and resets
         * the variables used.
         *
         * @param string $loopname to commit loop. If not set, will use last loopname set using newLoop()
         * @return boolean true/false
         * @access public
@@ -554,11 +525,8 @@
        }
        /**
         * FUNCTION: unsetLoop
         *
         * Unsets a loop which has already been set.
         * Can only unset top level loops.
         *
         * @param string loop to remove use: vlibTemplate::unsetLoop(loop[, loop..])
         * @return boolean true/false returns true unless called with 0 params
         * @access public
@@ -578,11 +546,8 @@
        }
        /**
         * FUNCTION: reset
         *
         * Resets the vlibTemplate object. After using vlibTemplate::reset() you must
         * use vlibTemplate::newTemplate(tmpl) to reuse, not passing in the options array.
         *
         * @return boolean true
         * @access public
         */
@@ -604,10 +569,7 @@
        }
        /**
         * FUNCTION: clearVars
         *
         * Unsets all variables in the template
         *
         * @return boolean true
         * @access public
         */
@@ -618,10 +580,7 @@
        }
        /**
         * FUNCTION: clearLoops
         *
         * Unsets all loops in the template
         *
         * @return boolean true
         * @access public
         */
@@ -634,10 +593,7 @@
        }
        /**
         * FUNCTION: clearAll
         *
         * Unsets all variables and loops set using setVar/Loop()
         *
         * @return boolean true
         * @access public
         */
@@ -649,11 +605,8 @@
        }
        /**
         * FUNCTION: unknownsExist
         *
         * Returns true if unknowns were found after parsing.
         * Function MUST be called AFTER one of the parsing functions to have any relevance.
         *
         * @return boolean true/false
         * @access public
         */
@@ -663,10 +616,7 @@
        }
        /**
         * FUNCTION: unknowns
         *
         * Alias for unknownsExist.
         *
         * @access public
         */
        public function unknowns()
@@ -675,11 +625,8 @@
        }
        /**
         * FUNCTION: getUnknowns
         *
         * Returns an array of all unknown vars found when parsing.
         * This function is only relevant after parsing a document.
         *
         * @return array
         * @access public
         */
@@ -689,11 +636,8 @@
        }
        /**
         * FUNCTION: setUnknowns
         *
         * Sets how you want to handle variables that were found in the
          * Sets how you want to handle variables that were found in the
         * template but not set in vlibTemplate using vlibTemplate::setVar().
         *
         * @param  string $arg ignore, remove, print, leave or comment
         * @return boolean
         * @access public
@@ -709,8 +653,6 @@
        }
        /**
         * FUNCTION: setPath
         *
         * function sets the paths to use when including files.
         * Use of this function: vlibTemplate::setPath(string path [, string path, ..]);
         * i.e. if $tmpl is your template object do: $tmpl->setPath('/web/htdocs/templates','/web/htdocs/www');
@@ -736,8 +678,6 @@
        }
        /**
         * FUNCTION: getParseTime
         *
         * After using one of the parse functions, this will allow you
         * access the time taken to parse the template.
         * see OPTION 'TIME_PARSE'.
@@ -755,8 +695,6 @@
        /**
         * FUNCTION: fastPrint
         *
         * Identical to pparse() except that it uses output buffering w/ gz compression thus
         * printing the output directly and compressed if poss.
         * Will possibly if parsing a huge template.
@@ -773,10 +711,7 @@
        /**
         * FUNCTION: pparse
         *
         * Calls parse, and then prints out $this->_tmploutput
         *
         * @access public
         * @return boolean true/false
         */
@@ -788,10 +723,7 @@
        }
        /**
         * FUNCTION: pprint
         *
         * Alias for pparse()
         *
         * @access public
         */
        public function pprint()
@@ -801,8 +733,6 @@
        /**
         * FUNCTION: grab
         *
         * Returns the parsed output, ready for printing, passing to mail() ...etc.
         * Invokes $this->_parse() if template has not yet been parsed.
         *
@@ -820,11 +750,8 @@
        \-----------------------------------------------------------------------------*/
        /**
         * FUNCTION: vlibTemplate
         *
         * vlibTemplate constructor.
         * if $tmplfile has been passed to it, it will send to $this->newTemplate()
         *
         * @param string $tmplfile full path to template file
         * @param array $options see above
         * @return boolean true/false
@@ -860,8 +787,7 @@
            return true;
        }
        /** FUNCTION: _getData
         *
        /**
         * function returns the text from the file, or if we're using cache, the text
         * from the cache file. MUST RETURN DATA.
         * @param string tmplfile contains path to template file
@@ -875,7 +801,9 @@
            if ($this->_includedepth > $this->OPTIONS['MAX_INCLUDES'] || $tmplfile == false) {
                return;
            } else {
                if ($this->_debug) array_push ($this->_debugIncludedfiles, $tmplfile);
                if ($this->_debug){
                    array_push ($this->_debugIncludedfiles, $tmplfile);
                }
                if ($do_eval) {
                    array_push($this->_currentincludedir, dirname($tmplfile));
                    $this->_includedepth++;
@@ -923,8 +851,8 @@
                $regex.=    '[\"\']?';
                $regex.= ')?\s*';
                $regex.= '(?:>|\/>|}|-->){1}';
                $regex.= '([\r\n|\n|\r])?/ie';
                $data = preg_replace($regex,"\$this->_parseTag(array('\\0','\\1','\\2','\\3','\\4','\\5','\\6','\\7','\\8','\\9'));",$data);
                $regex.= '([\r\n|\n|\r])?/i';
                $data = preg_replace_callback($regex, array($this, _parseTag), $data);
                if ($this->_cache) { // add cache if need be
                    $this->_createCache($data);
@@ -945,18 +873,22 @@
        }
        /**
         * FUNCTION: _fileSearch
         *
         * Searches for all possible instances of file { $file }
         *
         * @param string $file path of file we're looking for
         * @access private
         * @return mixed fullpath to file or boolean false
         */
        private function _fileSearch($file) 
        {
            $filename = basename($file);
         $filename = basename($file);
            $filepath = dirname($file);
         if(isset($_SESSION['s']['module']['name']) && isset($_SESSION['s']['theme'])) {
            if(is_file(ISPC_THEMES_PATH.'/'.$_SESSION['s']['theme'].'/templates/'.$_SESSION['s']['module']['name'].'/'.$filename)) {
               return ISPC_THEMES_PATH.'/'.$_SESSION['s']['theme'].'/templates/'.$_SESSION['s']['module']['name'].'/'.$filename;
            }
         }
            //* check fullpath first..
            $fullpath = $filepath.'/'.$filename;
@@ -1002,12 +934,9 @@
        }
        /**
         * FUNCTION: _arrayBuild
         *
         * Modifies the array $arr to add Template variables, __FIRST__, __LAST__ ..etc
         * if $this->OPTIONS['LOOP_CONTEXT_VARS'] is true.
         * Used by $this->setloop().
         *
         * @param array $arr
         * @return array new look array
         * @access private
@@ -1045,9 +974,7 @@
        }
        /**
         * FUNCTION: _parseIf
         * returns a string used for parsing in tmpl_if statements.
         *
         * @param string $varname
         * @param string $value
         * @param string $op
@@ -1102,9 +1029,7 @@
        /**
         * FUNCTION: _parseLoop
         * returns a string used for parsing in tmpl_loop statements.
         *
         * @param string $varname
         * @access private
         * @return string used for eval'ing
@@ -1122,10 +1047,7 @@
        }
        /**
         * FUNCTION: _parseVar
         *
         * returns a string used for parsing in tmpl_var statements.
         *
         * @param string $wholetag
         * @param string $tag
         * @param string $varname
@@ -1184,7 +1106,7 @@
            switch (strtolower($this->OPTIONS['UNKNOWNS'])) {
                case 'comment':
                    $comment = addcslashes('<!-- unknown variable '.ereg_replace('<!--|-->', '', $wholetag).'//-->', '"');
                    $comment = addcslashes('<!-- unknown variable '.preg_replace('/<!--|-->/', '', $wholetag).'//-->', '"');
                    $retstr .= ' else { print("'.$comment.'"); $this->_setUnknown("'.$varname.'"); }';
                    return $retstr;
@@ -1208,7 +1130,6 @@
        }
        /**
         * FUNCTION: _parseTag
         * takes values from preg_replace in $this->_intparse() and determines
         * the replace string.
         *
@@ -1304,8 +1225,6 @@
        }
        /**
         * FUNCTION: _intParse
         *
         * Parses $this->_tmplfile into correct format for eval() to work
         * Called by $this->_parse(), or $this->fastPrint, this replaces all <tmpl_*> references
         * with their correct php representation, i.e. <tmpl_var title> becomes $this->vars['title']
@@ -1316,16 +1235,14 @@
         */
        private function _intParse ()
        {
            $mqrt = get_magic_quotes_runtime();
            set_magic_quotes_runtime(0);
            //$mqrt = get_magic_quotes_runtime();
            //set_magic_quotes_runtime(0);
            $this->_tmplfilep = '?>'.$this->_getData($this->_tmplfilename).'<?php return true;';
            set_magic_quotes_runtime($mqrt);
            //set_magic_quotes_runtime($mqrt);
            return true;
        }
        /**
         * FUNCTION: _parse
         *
         * Calls _intParse, and eval()s $this->tmplfilep
         * and outputs the results to $this->tmploutput
         *
@@ -1362,8 +1279,6 @@
        }
        /**
         * FUNCTION: _setOption
         *
         * Sets one or more of the boolean options 1/0, that control certain actions in the template.
         * Use of this function:
         * either: vlibTemplate::_setOptions(string option_name, bool option_val [, string option_name, bool option_val ..]);
@@ -1413,11 +1328,8 @@
        }
        /**
         * FUNCTION: _setUnknown
         *
         * Used during parsing, this function sets an unknown var checking to see if it
         * has been previously set.
         *
         * @param string var
         * @access private
         */
@@ -1427,9 +1339,7 @@
        }
        /**
         * FUNCTION: _getMicrotime
         * Returns microtime as a float number
         *
         * @return float microtime
         * @access private
         */
@@ -1440,9 +1350,7 @@
        }
        /**
         * FUNCTION: _escape_hex
         * Returns str encoded to hex code.
         *
         * @param string str to be encoded
         * @param bool true/false specify whether to use hex_entity
         * @return string encoded in hex
@@ -1468,8 +1376,8 @@
        function setCacheExtension() {vlibTemplateError::raiseError('VT_WARNING_NOT_CACHE_OBJ', WARNING, 'setCacheExtension()');}
    } // << end class Def
    //include_once ($conf['classpath'].'/vlibTemplate/debug.php');
    include_once ($conf['classpath'].'/tpl_cache.inc.php');
    //include_once (ISPC_CLASS_PATH.'/vlibTemplate/debug.php');
    include_once (ISPC_CLASS_PATH.'/tpl_cache.inc.php');
} // << end if(!defined())..
?>