Index: branches/5.0.x/core/kernel/nparser/nparser.php =================================================================== diff -u -r12299 -r12323 --- branches/5.0.x/core/kernel/nparser/nparser.php (.../nparser.php) (revision 12299) +++ branches/5.0.x/core/kernel/nparser/nparser.php (.../nparser.php) (revision 12323) @@ -1,6 +1,6 @@ _compressOutput = $this->Application->ConfigValue('UseTemplateCompression'); - } - function Compile($pre_parsed, $template_name = 'unknown') { $data = file_get_contents($pre_parsed['tname']); @@ -76,23 +62,8 @@ } // saving compiled version (only when compilation was successful) - if (defined('SAFE_MODE') && SAFE_MODE) { // store cache files in database since can't save on filesystem - if (!isset($conn)) $conn =& $this->Application->GetADODBConnection(); - $conn->Query('REPLACE INTO '.TABLE_PREFIX.'Cache (VarName, Data, Cached) VALUES ('.$conn->qstr($pre_parsed['fname']).','.$conn->qstr($this->Buffers[0]).','.adodb_mktime().')'); - } - else { - $compiled = fopen($pre_parsed['fname'], 'w'); + $this->Application->TemplatesCache->saveTemplate($pre_parsed['fname'], $this->Buffers[0]); - if ($this->_compressOutput) { - $this->Buffers[0] = $this->_compress($this->Buffers[0]); - } - - if (!fwrite($compiled, $this->Buffers[0])) { - trigger_error('Saving compiled template failed', E_USER_ERROR); - } - fclose($compiled); - } - return true; } @@ -329,14 +300,18 @@ return $code; } - function CheckTemplate($t, $silent=null) + function CheckTemplate($t, $silent = null) { $pre_parsed = $this->Application->TemplatesCache->GetPreParsed($t); if (!$pre_parsed) { if (!$silent) { - if ($this->Application->isDebugMode()) $this->Application->Debugger->appendTrace(); + if ($this->Application->isDebugMode()) { + $this->Application->Debugger->appendTrace(); + } + trigger_error('Cannot include "' . $t . '" - file does not exist', E_USER_ERROR); } + return false; } @@ -349,44 +324,32 @@ $t = preg_replace('/^theme:.*?\//', '', $t); } - if (!$inc_parser->Compile($pre_parsed, $t)) return false; + if (!$inc_parser->Compile($pre_parsed, $t)) { + return false; + } } + return $pre_parsed; } - function Run($t, $silent=null) + function Run($t, $silent = null) { if ((strpos($t, '../') !== false) || (trim($t) !== $t)) { // when relative paths or special chars are found template names from url, then it's hacking attempt return false; } $pre_parsed = $this->CheckTemplate($t, $silent); - if (!$pre_parsed) return false; + if (!$pre_parsed) { + return false; + } + $backup_template = $this->TemplateName; $backup_fullpath = $this->TempalteFullPath; $this->TemplateName = $t; $this->TempalteFullPath = $pre_parsed['tname']; - ob_start(); - $_parser =& $this; - if (defined('SAFE_MODE') && SAFE_MODE) { // read cache files from database since can't save on filesystem - $conn =& $this->Application->GetADODBConnection(); - $cached = $conn->GetRow('SELECT * FROM '.TABLE_PREFIX.'Cache WHERE VarName = "'.$pre_parsed['fname'].'"'); - if ($cached !== false && $cached['Cached'] > filemtime($pre_parsed['tname'])) { - eval('?'.'>'.$cached['Data']); - } - } - else { - if ($pre_parsed['mode'] == 'file') { - include($pre_parsed['fname']); - } - else { - eval('?'.'>'.$pre_parsed['content']); - } - } - $output = ob_get_contents(); - ob_end_clean(); + $output =& $this->Application->TemplatesCache->runTemplate($this, $pre_parsed); $this->TemplateName = $backup_template; $this->TempalteFullPath = $backup_fullpath; @@ -739,140 +702,4 @@ $this->CacheSet($this->PopPointer(), $ret); // . ($this->CurrentKeyPart ? ':'.$this->CurrentKeyPart : '') echo $ret; } - - function _compress($src) { - // Whitespaces left and right from this signs can be ignored - static $IW = array( - T_CONCAT_EQUAL, // .= - T_DOUBLE_ARROW, // => - T_BOOLEAN_AND, // && - T_BOOLEAN_OR, // || - T_IS_EQUAL, // == - T_IS_NOT_EQUAL, // != or <> - T_IS_SMALLER_OR_EQUAL, // <= - T_IS_GREATER_OR_EQUAL, // >= - T_INC, // ++ - T_DEC, // -- - T_PLUS_EQUAL, // += - T_MINUS_EQUAL, // -= - T_MUL_EQUAL, // *= - T_DIV_EQUAL, // /= - T_IS_IDENTICAL, // === - T_IS_NOT_IDENTICAL, // !== - T_DOUBLE_COLON, // :: - T_PAAMAYIM_NEKUDOTAYIM, // :: - T_OBJECT_OPERATOR, // -> - T_DOLLAR_OPEN_CURLY_BRACES, // ${ - T_AND_EQUAL, // &= - T_MOD_EQUAL, // %= - T_XOR_EQUAL, // ^= - T_OR_EQUAL, // |= - T_SL, // << - T_SR, // >> - T_SL_EQUAL, // <<= - T_SR_EQUAL, // >>= - ); - - $tokens = token_get_all($src); - - $new = ""; - $c = sizeof($tokens); - $iw = false; // ignore whitespace - $ih = false; // in HEREDOC - $ls = ""; // last sign - $ot = null; // open tag - - for ($i = 0; $i < $c; $i++) { - $token = $tokens[$i]; - - if (is_array($token)) { - list ($tn, $ts) = $token; // tokens: number, string, line - $tname = token_name($tn); - - if ($tn == T_INLINE_HTML) { - $new .= $ts; - $iw = false; - } else { - if ($tn == T_OPEN_TAG) { - if (strpos($ts, " ") || strpos($ts, "\n") || strpos($ts, "\t") || strpos($ts, "\r")) { - $ts = rtrim($ts); - } - - $ts .= " "; - $new .= $ts; - $ot = T_OPEN_TAG; - $iw = true; - } elseif ($tn == T_OPEN_TAG_WITH_ECHO) { - $new .= $ts; - $ot = T_OPEN_TAG_WITH_ECHO; - $iw = true; - } elseif ($tn == T_CLOSE_TAG) { - if ($ot == T_OPEN_TAG_WITH_ECHO) { - $new = rtrim($new, "; "); - } else { - $ts = " ".$ts; - } - - $new .= $ts; - $ot = null; - $iw = false; - } elseif (in_array($tn, $IW)) { - $new .= $ts; - $iw = true; - } elseif ($tn == T_CONSTANT_ENCAPSED_STRING || $tn == T_ENCAPSED_AND_WHITESPACE) { - if ($ts[0] == '"') { - $ts = addcslashes($ts, "\n\t\r"); - } - - $new .= $ts; - $iw = true; - } elseif ($tn == T_WHITESPACE) { - $nt = @$tokens[$i+1]; - if (!$iw && (!is_string($nt) || $nt == '$') && !in_array($nt[0], $IW)) { - $new .= " "; - } - - $iw = false; - } elseif ($tn == T_START_HEREDOC) { - $new .= "<<