indvdum@0: template = &$template; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Load template source from file indvdum@0: * @access private indvdum@0: */ indvdum@0: function _tpl_load_file($handle, $store_in_db = false) indvdum@0: { indvdum@0: // Try and open template for read indvdum@0: if (!file_exists($this->template->files[$handle])) indvdum@0: { indvdum@0: trigger_error("template->_tpl_load_file(): File {$this->template->files[$handle]} does not exist or is empty", E_USER_ERROR); indvdum@0: } indvdum@0: indvdum@0: $this->template->compiled_code[$handle] = $this->compile(trim(@file_get_contents($this->template->files[$handle]))); indvdum@0: indvdum@0: // Actually compile the code now. indvdum@0: $this->compile_write($handle, $this->template->compiled_code[$handle]); indvdum@0: indvdum@0: // Store in database if required... indvdum@0: if ($store_in_db) indvdum@0: { indvdum@0: global $db, $user; indvdum@0: indvdum@0: $sql_ary = array( indvdum@0: 'template_id' => $this->template->files_template[$handle], indvdum@0: 'template_filename' => $this->template->filename[$handle], indvdum@0: 'template_included' => '', indvdum@0: 'template_mtime' => time(), indvdum@0: 'template_data' => trim(@file_get_contents($this->template->files[$handle])), indvdum@0: ); indvdum@0: indvdum@0: $sql = 'INSERT INTO ' . STYLES_TEMPLATE_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary); indvdum@0: $db->sql_query($sql); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Remove any PHP tags that do not belong, these regular expressions are derived from indvdum@0: * the ones that exist in zend_language_scanner.l indvdum@0: * @access private indvdum@0: */ indvdum@0: function remove_php_tags(&$code) indvdum@0: { indvdum@0: // This matches the information gathered from the internal PHP lexer indvdum@0: $match = array( indvdum@0: '#<([\?%])=?.*?\1>#s', indvdum@0: '#.*?#s', indvdum@0: '#<\?php(?:\r\n?|[ \n\t]).*?\?>#s' indvdum@0: ); indvdum@0: indvdum@0: $code = preg_replace($match, '', $code); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * The all seeing all doing compile method. Parts are inspired by or directly from Smarty indvdum@0: * @access private indvdum@0: */ indvdum@0: function compile($code, $no_echo = false, $echo_var = '') indvdum@0: { indvdum@0: global $config; indvdum@0: indvdum@0: if ($echo_var) indvdum@0: { indvdum@0: global $$echo_var; indvdum@0: } indvdum@0: indvdum@0: // Remove any "loose" php ... we want to give admins the ability indvdum@0: // to switch on/off PHP for a given template. Allowing unchecked indvdum@0: // php is a no-no. There is a potential issue here in that non-php indvdum@0: // content may be removed ... however designers should use entities indvdum@0: // if they wish to display < and > indvdum@0: $this->remove_php_tags($code); indvdum@0: indvdum@0: // Pull out all block/statement level elements and separate plain text indvdum@0: preg_match_all('#(.*?)#s', $code, $matches); indvdum@0: $php_blocks = $matches[1]; indvdum@0: $code = preg_replace('#.*?#s', '', $code); indvdum@0: indvdum@0: preg_match_all('##', $code, $matches); indvdum@0: $include_blocks = $matches[1]; indvdum@0: $code = preg_replace('##', '', $code); indvdum@0: indvdum@0: preg_match_all('##', $code, $matches); indvdum@0: $includephp_blocks = $matches[1]; indvdum@0: $code = preg_replace('##', '', $code); indvdum@0: indvdum@0: preg_match_all('##', $code, $blocks, PREG_SET_ORDER); indvdum@0: indvdum@0: $text_blocks = preg_split('##', $code); indvdum@0: indvdum@0: for ($i = 0, $j = sizeof($text_blocks); $i < $j; $i++) indvdum@0: { indvdum@0: $this->compile_var_tags($text_blocks[$i]); indvdum@0: } indvdum@0: $compile_blocks = array(); indvdum@0: indvdum@0: for ($curr_tb = 0, $tb_size = sizeof($blocks); $curr_tb < $tb_size; $curr_tb++) indvdum@0: { indvdum@0: $block_val = &$blocks[$curr_tb]; indvdum@0: indvdum@0: switch ($block_val[1]) indvdum@0: { indvdum@0: case 'BEGIN': indvdum@0: $this->block_else_level[] = false; indvdum@0: $compile_blocks[] = 'compile_tag_block($block_val[2]) . ' ?>'; indvdum@0: break; indvdum@0: indvdum@0: case 'BEGINELSE': indvdum@0: $this->block_else_level[sizeof($this->block_else_level) - 1] = true; indvdum@0: $compile_blocks[] = ''; indvdum@0: break; indvdum@0: indvdum@0: case 'END': indvdum@0: array_pop($this->block_names); indvdum@0: $compile_blocks[] = 'block_else_level)) ? '}' : '}}') . ' ?>'; indvdum@0: break; indvdum@0: indvdum@0: case 'IF': indvdum@0: $compile_blocks[] = 'compile_tag_if($block_val[2], false) . ' ?>'; indvdum@0: break; indvdum@0: indvdum@0: case 'ELSE': indvdum@0: $compile_blocks[] = ''; indvdum@0: break; indvdum@0: indvdum@0: case 'ELSEIF': indvdum@0: $compile_blocks[] = 'compile_tag_if($block_val[2], true) . ' ?>'; indvdum@0: break; indvdum@0: indvdum@0: case 'ENDIF': indvdum@0: $compile_blocks[] = ''; indvdum@0: break; indvdum@0: indvdum@0: case 'DEFINE': indvdum@0: $compile_blocks[] = 'compile_tag_define($block_val[2], true) . ' ?>'; indvdum@0: break; indvdum@0: indvdum@0: case 'UNDEFINE': indvdum@0: $compile_blocks[] = 'compile_tag_define($block_val[2], false) . ' ?>'; indvdum@0: break; indvdum@0: indvdum@0: case 'INCLUDE': indvdum@0: $temp = array_shift($include_blocks); indvdum@0: indvdum@0: // Dynamic includes indvdum@0: // Cheap match rather than a full blown regexp, we already know indvdum@0: // the format of the input so just use string manipulation. indvdum@0: if ($temp[0] == '{') indvdum@0: { indvdum@0: $file = false; indvdum@0: indvdum@0: if ($temp[1] == '$') indvdum@0: { indvdum@0: $var = substr($temp, 2, -1); indvdum@0: //$file = $this->template->_tpldata['DEFINE']['.'][$var]; indvdum@0: $temp = "\$this->_tpldata['DEFINE']['.']['$var']"; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $var = substr($temp, 1, -1); indvdum@0: //$file = $this->template->_rootref[$var]; indvdum@0: $temp = "\$this->_rootref['$var']"; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $file = $temp; indvdum@0: } indvdum@0: indvdum@0: $compile_blocks[] = 'compile_tag_include($temp) . ' ?>'; indvdum@0: indvdum@0: // No point in checking variable includes indvdum@0: if ($file) indvdum@0: { indvdum@0: $this->template->_tpl_include($file, false); indvdum@0: } indvdum@0: break; indvdum@0: indvdum@0: case 'INCLUDEPHP': indvdum@0: $compile_blocks[] = ($config['tpl_allow_php']) ? 'compile_tag_include_php(array_shift($includephp_blocks)) . ' ?>' : ''; indvdum@0: break; indvdum@0: indvdum@0: case 'PHP': indvdum@0: $compile_blocks[] = ($config['tpl_allow_php']) ? '' : ''; indvdum@0: break; indvdum@0: indvdum@0: default: indvdum@0: $this->compile_var_tags($block_val[0]); indvdum@0: $trim_check = trim($block_val[0]); indvdum@0: $compile_blocks[] = (!$no_echo) ? ((!empty($trim_check)) ? $block_val[0] : '') : ((!empty($trim_check)) ? $block_val[0] : ''); indvdum@0: break; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $template_php = ''; indvdum@0: for ($i = 0, $size = sizeof($text_blocks); $i < $size; $i++) indvdum@0: { indvdum@0: $trim_check_text = trim($text_blocks[$i]); indvdum@0: $template_php .= (!$no_echo) ? (($trim_check_text != '') ? $text_blocks[$i] : '') . ((isset($compile_blocks[$i])) ? $compile_blocks[$i] : '') : (($trim_check_text != '') ? $text_blocks[$i] : '') . ((isset($compile_blocks[$i])) ? $compile_blocks[$i] : ''); indvdum@0: } indvdum@0: indvdum@0: // Remove unused opening/closing tags indvdum@0: $template_php = str_replace(' ?>([\r\n])#', '?>\1\1', $template_php); indvdum@0: indvdum@0: // There will be a number of occasions where we switch into and out of indvdum@0: // PHP mode instantaneously. Rather than "burden" the parser with this indvdum@0: // we'll strip out such occurences, minimising such switching indvdum@0: if ($no_echo) indvdum@0: { indvdum@0: return "\$$echo_var .= '" . $template_php . "'"; indvdum@0: } indvdum@0: indvdum@0: return $template_php; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Compile variables indvdum@0: * @access private indvdum@0: */ indvdum@0: function compile_var_tags(&$text_blocks) indvdum@0: { indvdum@0: // change template varrefs into PHP varrefs indvdum@0: $varrefs = array(); indvdum@0: indvdum@0: // This one will handle varrefs WITH namespaces indvdum@0: preg_match_all('#\{((?:[a-z0-9\-_]+\.)+)(\$)?([A-Z0-9\-_]+)\}#', $text_blocks, $varrefs, PREG_SET_ORDER); indvdum@0: indvdum@0: foreach ($varrefs as $var_val) indvdum@0: { indvdum@0: $namespace = $var_val[1]; indvdum@0: $varname = $var_val[3]; indvdum@0: $new = $this->generate_block_varref($namespace, $varname, true, $var_val[2]); indvdum@0: indvdum@0: $text_blocks = str_replace($var_val[0], $new, $text_blocks); indvdum@0: } indvdum@0: indvdum@0: // This will handle the remaining root-level varrefs indvdum@0: // transform vars prefixed by L_ into their language variable pendant if nothing is set within the tpldata array indvdum@0: if (strpos($text_blocks, '{L_') !== false) indvdum@0: { indvdum@0: $text_blocks = preg_replace('#\{L_([A-Z0-9\-_]+)\}#', "_rootref['L_\\1'])) ? \$this->_rootref['L_\\1'] : ((isset(\$user->lang['\\1'])) ? \$user->lang['\\1'] : '{ \\1 }')); ?>", $text_blocks); indvdum@0: } indvdum@0: indvdum@0: // Handle addslashed language variables prefixed with LA_ indvdum@0: // If a template variable already exist, it will be used in favor of it... indvdum@0: if (strpos($text_blocks, '{LA_') !== false) indvdum@0: { indvdum@0: $text_blocks = preg_replace('#\{LA_([A-Z0-9\-_]+)\}#', "_rootref['LA_\\1'])) ? \$this->_rootref['LA_\\1'] : ((isset(\$this->_rootref['L_\\1'])) ? addslashes(\$this->_rootref['L_\\1']) : ((isset(\$user->lang['\\1'])) ? addslashes(\$user->lang['\\1']) : '{ \\1 }'))); ?>", $text_blocks); indvdum@0: } indvdum@0: indvdum@0: // Handle remaining varrefs indvdum@0: $text_blocks = preg_replace('#\{([A-Z0-9\-_]+)\}#', "_rootref['\\1'])) ? \$this->_rootref['\\1'] : ''; ?>", $text_blocks); indvdum@0: $text_blocks = preg_replace('#\{\$([A-Z0-9\-_]+)\}#', "_tpldata['DEFINE']['.']['\\1'])) ? \$this->_tpldata['DEFINE']['.']['\\1'] : ''; ?>", $text_blocks); indvdum@0: indvdum@0: return; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Compile blocks indvdum@0: * @access private indvdum@0: */ indvdum@0: function compile_tag_block($tag_args) indvdum@0: { indvdum@0: $no_nesting = false; indvdum@0: indvdum@0: // Is the designer wanting to call another loop in a loop? indvdum@0: if (strpos($tag_args, '!') === 0) indvdum@0: { indvdum@0: // Count the number of ! occurrences (not allowed in vars) indvdum@0: $no_nesting = substr_count($tag_args, '!'); indvdum@0: $tag_args = substr($tag_args, $no_nesting); indvdum@0: } indvdum@0: indvdum@0: // Allow for control of looping (indexes start from zero): indvdum@0: // foo(2) : Will start the loop on the 3rd entry indvdum@0: // foo(-2) : Will start the loop two entries from the end indvdum@0: // foo(3,4) : Will start the loop on the fourth entry and end it on the fifth indvdum@0: // foo(3,-4) : Will start the loop on the fourth entry and end it four from last indvdum@0: if (preg_match('#^([^()]*)\(([\-\d]+)(?:,([\-\d]+))?\)$#', $tag_args, $match)) indvdum@0: { indvdum@0: $tag_args = $match[1]; indvdum@0: indvdum@0: if ($match[2] < 0) indvdum@0: { indvdum@0: $loop_start = '($_' . $tag_args . '_count ' . $match[2] . ' < 0 ? 0 : $_' . $tag_args . '_count ' . $match[2] . ')'; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $loop_start = '($_' . $tag_args . '_count < ' . $match[2] . ' ? $_' . $tag_args . '_count : ' . $match[2] . ')'; indvdum@0: } indvdum@0: indvdum@0: if (strlen($match[3]) < 1 || $match[3] == -1) indvdum@0: { indvdum@0: $loop_end = '$_' . $tag_args . '_count'; indvdum@0: } indvdum@0: else if ($match[3] >= 0) indvdum@0: { indvdum@0: $loop_end = '(' . ($match[3] + 1) . ' > $_' . $tag_args . '_count ? $_' . $tag_args . '_count : ' . ($match[3] + 1) . ')'; indvdum@0: } indvdum@0: else //if ($match[3] < -1) indvdum@0: { indvdum@0: $loop_end = '$_' . $tag_args . '_count' . ($match[3] + 1); indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $loop_start = 0; indvdum@0: $loop_end = '$_' . $tag_args . '_count'; indvdum@0: } indvdum@0: indvdum@0: $tag_template_php = ''; indvdum@0: array_push($this->block_names, $tag_args); indvdum@0: indvdum@0: if ($no_nesting !== false) indvdum@0: { indvdum@0: // We need to implode $no_nesting times from the end... indvdum@0: $block = array_slice($this->block_names, -$no_nesting); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $block = $this->block_names; indvdum@0: } indvdum@0: indvdum@0: if (sizeof($block) < 2) indvdum@0: { indvdum@0: // Block is not nested. indvdum@0: $tag_template_php = '$_' . $tag_args . "_count = (isset(\$this->_tpldata['$tag_args'])) ? sizeof(\$this->_tpldata['$tag_args']) : 0;"; indvdum@0: $varref = "\$this->_tpldata['$tag_args']"; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // This block is nested. indvdum@0: // Generate a namespace string for this block. indvdum@0: $namespace = implode('.', $block); indvdum@0: indvdum@0: // Get a reference to the data array for this block that depends on the indvdum@0: // current indices of all parent blocks. indvdum@0: $varref = $this->generate_block_data_ref($namespace, false); indvdum@0: indvdum@0: // Create the for loop code to iterate over this block. indvdum@0: $tag_template_php = '$_' . $tag_args . '_count = (isset(' . $varref . ')) ? sizeof(' . $varref . ') : 0;'; indvdum@0: } indvdum@0: indvdum@0: $tag_template_php .= 'if ($_' . $tag_args . '_count) {'; indvdum@0: indvdum@0: /** indvdum@0: * The following uses foreach for iteration instead of a for loop, foreach is faster but requires PHP to make a copy of the contents of the array which uses more memory indvdum@0: * indvdum@0: * if (!$offset) indvdum@0: * { indvdum@0: * $tag_template_php .= 'foreach (' . $varref . ' as $_' . $tag_args . '_i => $_' . $tag_args . '_val){'; indvdum@0: * } indvdum@0: * indvdum@0: */ indvdum@0: indvdum@0: $tag_template_php .= 'for ($_' . $tag_args . '_i = ' . $loop_start . '; $_' . $tag_args . '_i < ' . $loop_end . '; ++$_' . $tag_args . '_i){'; indvdum@0: $tag_template_php .= '$_'. $tag_args . '_val = &' . $varref . '[$_'. $tag_args. '_i];'; indvdum@0: indvdum@0: return $tag_template_php; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Compile IF tags - much of this is from Smarty with indvdum@0: * some adaptions for our block level methods indvdum@0: * @access private indvdum@0: */ indvdum@0: function compile_tag_if($tag_args, $elseif) indvdum@0: { indvdum@0: // Tokenize args for 'if' tag. indvdum@0: preg_match_all('/(?: indvdum@0: "[^"\\\\]*(?:\\\\.[^"\\\\]*)*" | indvdum@0: \'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\' | indvdum@0: [(),] | indvdum@0: [^\s(),]+)/x', $tag_args, $match); indvdum@0: indvdum@0: $tokens = $match[0]; indvdum@0: $is_arg_stack = array(); indvdum@0: indvdum@0: for ($i = 0, $size = sizeof($tokens); $i < $size; $i++) indvdum@0: { indvdum@0: $token = &$tokens[$i]; indvdum@0: indvdum@0: switch ($token) indvdum@0: { indvdum@0: case '!==': indvdum@0: case '===': indvdum@0: case '<<': indvdum@0: case '>>': indvdum@0: case '|': indvdum@0: case '^': indvdum@0: case '&': indvdum@0: case '~': indvdum@0: case ')': indvdum@0: case ',': indvdum@0: case '+': indvdum@0: case '-': indvdum@0: case '*': indvdum@0: case '/': indvdum@0: case '@': indvdum@0: break; indvdum@0: indvdum@0: case '==': indvdum@0: case 'eq': indvdum@0: $token = '=='; indvdum@0: break; indvdum@0: indvdum@0: case '!=': indvdum@0: case '<>': indvdum@0: case 'ne': indvdum@0: case 'neq': indvdum@0: $token = '!='; indvdum@0: break; indvdum@0: indvdum@0: case '<': indvdum@0: case 'lt': indvdum@0: $token = '<'; indvdum@0: break; indvdum@0: indvdum@0: case '<=': indvdum@0: case 'le': indvdum@0: case 'lte': indvdum@0: $token = '<='; indvdum@0: break; indvdum@0: indvdum@0: case '>': indvdum@0: case 'gt': indvdum@0: $token = '>'; indvdum@0: break; indvdum@0: indvdum@0: case '>=': indvdum@0: case 'ge': indvdum@0: case 'gte': indvdum@0: $token = '>='; indvdum@0: break; indvdum@0: indvdum@0: case '&&': indvdum@0: case 'and': indvdum@0: $token = '&&'; indvdum@0: break; indvdum@0: indvdum@0: case '||': indvdum@0: case 'or': indvdum@0: $token = '||'; indvdum@0: break; indvdum@0: indvdum@0: case '!': indvdum@0: case 'not': indvdum@0: $token = '!'; indvdum@0: break; indvdum@0: indvdum@0: case '%': indvdum@0: case 'mod': indvdum@0: $token = '%'; indvdum@0: break; indvdum@0: indvdum@0: case '(': indvdum@0: array_push($is_arg_stack, $i); indvdum@0: break; indvdum@0: indvdum@0: case 'is': indvdum@0: $is_arg_start = ($tokens[$i-1] == ')') ? array_pop($is_arg_stack) : $i-1; indvdum@0: $is_arg = implode(' ', array_slice($tokens, $is_arg_start, $i - $is_arg_start)); indvdum@0: indvdum@0: $new_tokens = $this->_parse_is_expr($is_arg, array_slice($tokens, $i+1)); indvdum@0: indvdum@0: array_splice($tokens, $is_arg_start, sizeof($tokens), $new_tokens); indvdum@0: indvdum@0: $i = $is_arg_start; indvdum@0: indvdum@0: // no break indvdum@0: indvdum@0: default: indvdum@0: if (preg_match('#^((?:[a-z0-9\-_]+\.)+)?(\$)?(?=[A-Z])([A-Z0-9\-_]+)#s', $token, $varrefs)) indvdum@0: { indvdum@0: $token = (!empty($varrefs[1])) ? $this->generate_block_data_ref(substr($varrefs[1], 0, -1), true, $varrefs[2]) . '[\'' . $varrefs[3] . '\']' : (($varrefs[2]) ? '$this->_tpldata[\'DEFINE\'][\'.\'][\'' . $varrefs[3] . '\']' : '$this->_rootref[\'' . $varrefs[3] . '\']'); indvdum@0: } indvdum@0: else if (preg_match('#^\.((?:[a-z0-9\-_]+\.?)+)$#s', $token, $varrefs)) indvdum@0: { indvdum@0: // Allow checking if loops are set with .loopname indvdum@0: // It is also possible to check the loop count by doing for example indvdum@0: $blocks = explode('.', $varrefs[1]); indvdum@0: indvdum@0: // If the block is nested, we have a reference that we can grab. indvdum@0: // If the block is not nested, we just go and grab the block from _tpldata indvdum@0: if (sizeof($blocks) > 1) indvdum@0: { indvdum@0: $block = array_pop($blocks); indvdum@0: $namespace = implode('.', $blocks); indvdum@0: $varref = $this->generate_block_data_ref($namespace, true); indvdum@0: indvdum@0: // Add the block reference for the last child. indvdum@0: $varref .= "['" . $block . "']"; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $varref = '$this->_tpldata'; indvdum@0: indvdum@0: // Add the block reference for the last child. indvdum@0: $varref .= "['" . $blocks[0] . "']"; indvdum@0: } indvdum@0: $token = "sizeof($varref)"; indvdum@0: } indvdum@0: else if (!empty($token)) indvdum@0: { indvdum@0: $token = '(' . $token . ')'; indvdum@0: } indvdum@0: indvdum@0: break; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // If there are no valid tokens left or only control/compare characters left, we do skip this statement indvdum@0: if (!sizeof($tokens) || str_replace(array(' ', '=', '!', '<', '>', '&', '|', '%', '(', ')'), '', implode('', $tokens)) == '') indvdum@0: { indvdum@0: $tokens = array('false'); indvdum@0: } indvdum@0: return (($elseif) ? '} else if (' : 'if (') . (implode(' ', $tokens) . ') { '); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Compile DEFINE tags indvdum@0: * @access private indvdum@0: */ indvdum@0: function compile_tag_define($tag_args, $op) indvdum@0: { indvdum@0: preg_match('#^((?:[a-z0-9\-_]+\.)+)?\$(?=[A-Z])([A-Z0-9_\-]*)(?: = (\'?)([^\']*)(\'?))?$#', $tag_args, $match); indvdum@0: indvdum@0: if (empty($match[2]) || (!isset($match[4]) && $op)) indvdum@0: { indvdum@0: return ''; indvdum@0: } indvdum@0: indvdum@0: if (!$op) indvdum@0: { indvdum@0: return 'unset(' . (($match[1]) ? $this->generate_block_data_ref(substr($match[1], 0, -1), true, true) . '[\'' . $match[2] . '\']' : '$this->_tpldata[\'DEFINE\'][\'.\'][\'' . $match[2] . '\']') . ');'; indvdum@0: } indvdum@0: indvdum@0: // Are we a string? indvdum@0: if ($match[3] && $match[5]) indvdum@0: { indvdum@0: $match[4] = str_replace(array('\\\'', '\\\\', '\''), array('\'', '\\', '\\\''), $match[4]); indvdum@0: indvdum@0: // Compile reference, we allow template variables in defines... indvdum@0: $match[4] = $this->compile($match[4]); indvdum@0: indvdum@0: // Now replace the php code indvdum@0: $match[4] = "'" . str_replace(array(''), array("' . ", " . '"), $match[4]) . "'"; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: preg_match('#true|false|\.#i', $match[4], $type); indvdum@0: indvdum@0: switch (strtolower($type[0])) indvdum@0: { indvdum@0: case 'true': indvdum@0: case 'false': indvdum@0: $match[4] = strtoupper($match[4]); indvdum@0: break; indvdum@0: indvdum@0: case '.': indvdum@0: $match[4] = doubleval($match[4]); indvdum@0: break; indvdum@0: indvdum@0: default: indvdum@0: $match[4] = intval($match[4]); indvdum@0: break; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: return (($match[1]) ? $this->generate_block_data_ref(substr($match[1], 0, -1), true, true) . '[\'' . $match[2] . '\']' : '$this->_tpldata[\'DEFINE\'][\'.\'][\'' . $match[2] . '\']') . ' = ' . $match[4] . ';'; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Compile INCLUDE tag indvdum@0: * @access private indvdum@0: */ indvdum@0: function compile_tag_include($tag_args) indvdum@0: { indvdum@0: // Process dynamic includes indvdum@0: if ($tag_args[0] == '$') indvdum@0: { indvdum@0: return "if (isset($tag_args)) { \$this->_tpl_include($tag_args); }"; indvdum@0: } indvdum@0: indvdum@0: return "\$this->_tpl_include('$tag_args');"; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Compile INCLUDE_PHP tag indvdum@0: * @access private indvdum@0: */ indvdum@0: function compile_tag_include_php($tag_args) indvdum@0: { indvdum@0: return "\$this->_php_include('$tag_args');"; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * parse expression indvdum@0: * This is from Smarty indvdum@0: * @access private indvdum@0: */ indvdum@0: function _parse_is_expr($is_arg, $tokens) indvdum@0: { indvdum@0: $expr_end = 0; indvdum@0: $negate_expr = false; indvdum@0: indvdum@0: if (($first_token = array_shift($tokens)) == 'not') indvdum@0: { indvdum@0: $negate_expr = true; indvdum@0: $expr_type = array_shift($tokens); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $expr_type = $first_token; indvdum@0: } indvdum@0: indvdum@0: switch ($expr_type) indvdum@0: { indvdum@0: case 'even': indvdum@0: if (@$tokens[$expr_end] == 'by') indvdum@0: { indvdum@0: $expr_end++; indvdum@0: $expr_arg = $tokens[$expr_end++]; indvdum@0: $expr = "!(($is_arg / $expr_arg) % $expr_arg)"; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $expr = "!($is_arg & 1)"; indvdum@0: } indvdum@0: break; indvdum@0: indvdum@0: case 'odd': indvdum@0: if (@$tokens[$expr_end] == 'by') indvdum@0: { indvdum@0: $expr_end++; indvdum@0: $expr_arg = $tokens[$expr_end++]; indvdum@0: $expr = "(($is_arg / $expr_arg) % $expr_arg)"; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $expr = "($is_arg & 1)"; indvdum@0: } indvdum@0: break; indvdum@0: indvdum@0: case 'div': indvdum@0: if (@$tokens[$expr_end] == 'by') indvdum@0: { indvdum@0: $expr_end++; indvdum@0: $expr_arg = $tokens[$expr_end++]; indvdum@0: $expr = "!($is_arg % $expr_arg)"; indvdum@0: } indvdum@0: break; indvdum@0: } indvdum@0: indvdum@0: if ($negate_expr) indvdum@0: { indvdum@0: $expr = "!($expr)"; indvdum@0: } indvdum@0: indvdum@0: array_splice($tokens, 0, $expr_end, $expr); indvdum@0: indvdum@0: return $tokens; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Generates a reference to the given variable inside the given (possibly nested) indvdum@0: * block namespace. This is a string of the form: indvdum@0: * ' . $this->_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['varname'] . ' indvdum@0: * It's ready to be inserted into an "echo" line in one of the templates. indvdum@0: * NOTE: expects a trailing "." on the namespace. indvdum@0: * @access private indvdum@0: */ indvdum@0: function generate_block_varref($namespace, $varname, $echo = true, $defop = false) indvdum@0: { indvdum@0: // Strip the trailing period. indvdum@0: $namespace = substr($namespace, 0, -1); indvdum@0: indvdum@0: // Get a reference to the data block for this namespace. indvdum@0: $varref = $this->generate_block_data_ref($namespace, true, $defop); indvdum@0: // Prepend the necessary code to stick this in an echo line. indvdum@0: indvdum@0: // Append the variable reference. indvdum@0: $varref .= "['$varname']"; indvdum@0: $varref = ($echo) ? "" : ((isset($varref)) ? $varref : ''); indvdum@0: indvdum@0: return $varref; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Generates a reference to the array of data values for the given indvdum@0: * (possibly nested) block namespace. This is a string of the form: indvdum@0: * $this->_tpldata['parent'][$_parent_i]['$child1'][$_child1_i]['$child2'][$_child2_i]...['$childN'] indvdum@0: * indvdum@0: * If $include_last_iterator is true, then [$_childN_i] will be appended to the form shown above. indvdum@0: * NOTE: does not expect a trailing "." on the blockname. indvdum@0: * @access private indvdum@0: */ indvdum@0: function generate_block_data_ref($blockname, $include_last_iterator, $defop = false) indvdum@0: { indvdum@0: // Get an array of the blocks involved. indvdum@0: $blocks = explode('.', $blockname); indvdum@0: $blockcount = sizeof($blocks) - 1; indvdum@0: indvdum@0: // DEFINE is not an element of any referenced variable, we must use _tpldata to access it indvdum@0: if ($defop) indvdum@0: { indvdum@0: $varref = '$this->_tpldata[\'DEFINE\']'; indvdum@0: // Build up the string with everything but the last child. indvdum@0: for ($i = 0; $i < $blockcount; $i++) indvdum@0: { indvdum@0: $varref .= "['" . $blocks[$i] . "'][\$_" . $blocks[$i] . '_i]'; indvdum@0: } indvdum@0: // Add the block reference for the last child. indvdum@0: $varref .= "['" . $blocks[$blockcount] . "']"; indvdum@0: // Add the iterator for the last child if requried. indvdum@0: if ($include_last_iterator) indvdum@0: { indvdum@0: $varref .= '[$_' . $blocks[$blockcount] . '_i]'; indvdum@0: } indvdum@0: return $varref; indvdum@0: } indvdum@0: else if ($include_last_iterator) indvdum@0: { indvdum@0: return '$_'. $blocks[$blockcount] . '_val'; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: return '$_'. $blocks[$blockcount - 1] . '_val[\''. $blocks[$blockcount]. '\']'; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Write compiled file to cache directory indvdum@0: * @access private indvdum@0: */ indvdum@0: function compile_write($handle, $data) indvdum@0: { indvdum@0: global $phpEx; indvdum@0: indvdum@0: $filename = $this->template->cachepath . str_replace('/', '.', $this->template->filename[$handle]) . '.' . $phpEx; indvdum@0: indvdum@0: $data = "' . $data); indvdum@0: indvdum@0: if ($fp = @fopen($filename, 'wb')) indvdum@0: { indvdum@0: @flock($fp, LOCK_EX); indvdum@0: @fwrite ($fp, $data); indvdum@0: @flock($fp, LOCK_UN); indvdum@0: @fclose($fp); indvdum@0: indvdum@0: phpbb_chmod($filename, CHMOD_READ | CHMOD_WRITE); indvdum@0: } indvdum@0: indvdum@0: return; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: ?>