cache) > 0) { trigger_error('You cannot cache templates more than once per initialization', ERR_WARNING); } else { $templates = $_isso->db->query("SELECT * FROM " . $this->tablename . " WHERE " . $this->namecolumn . " IN ('" . implode("', '", $namearray) . "')" . iff($this->extrawhere, $this->extrawhere)); while ($template = $_isso->db->fetch_array($templates)) { $template = $this->_parse($template); $this->cache[ $template[ $this->namecolumn ] ] = $template[ $this->datacolumn ]; $this->usage["$name"] = 0; } } } /** * Loads a template from the cache or the _load function and * stores the parsed version of it * * @param str The name of the template * * @return str A parsed and loaded template */ function fetch($name) { global $_isso; if (isset($this->cache["$name"])) { $template = $this->cache["$name"]; } else { $this->uncached[] = $name; $_isso->debug("Manually loading template `$name`"); $template = $this->_load($name); $template = $this->_parse($template); } if (!isset($this->usage["$name"])) { $this->usage["$name"] = 0; } $this->usage["$name"]++; return $template; } /** * Output a template fully compiled to the browser * * @param str Compiled and ready template */ function flush($template) { global $_isso; ob_start(); if (empty($template)) { trigger_error('There was no output to print', ERR_FATAL); exit; } if ($_isso->debug AND isset($_GET['query'])) { if (is_array($_isso->db->history)) { echo '
';
				foreach ($_isso->db->history AS $query)
				{
					echo $query . "\n\n
\n\n"; } echo '
'; } exit; } if ($this->doneflush) { trigger_error('A template has already been sent to the output buffer', ERR_FATAL); exit; } if ($_isso->debug) { // --- START $debug = "\n"; $debug = "\n
\n" . $_isso->_message('Debug Information', $debug, 1, true); $template = str_replace('', "\n\n\n
\n$debug\n
\n\n\n", $template); } print(stripslashes($template)); } /** * Loads an additional template from the database * * @param str The name of the template * * @return str Template data from the database */ function _load($name) { global $_isso; if ($template = $_isso->db->query("SELECT * FROM " . $this->tablename . " WHERE " . $this->namecolumn . " = '$name'" . iff($this->extrawhere, $this->extrawhere))) { return $template[ $this->datacolumn ]; } else { trigger_error("The template '$name' could not be loaded", ERR_FATAL); exit; } } /** * A wrapper for all the parsing functions and compiling functins * * @param str Unparsed template data * * @return str Parsed template data */ function _parse($template) { $template = stripslashes($template); $template = str_replace('"', '\"', $template); $template = $this->_parse_phrases($template); $template = $this->_parse_conditionals($template); return $template; } /** * Prepares language and locale information inside templates * * @param str Template data to be processed * * @return str Language-ready template data */ function _parse_phrases($template) { $tag_start = 'langcall . '(\'$1\') . "', $template); while (1) { // Find the start language object tag $location_start = strpos($template, $tag_start, $location_end + 1); if ($location_start === false) { break; } // Find the end tag $location_end = strpos($template, $tag_end, $location_end + strlen($tag_end)); if ($location_end === false) { break; } // Extract the language object $phrase_bunch = substr($template, $location_start, ($location_end + strlen($tag_end)) - $location_start); // Find the close to the opening $close_of_open = strpos($phrase_bunch, $tag_start_end); if ($close_of_open === false) { break; } // Extract the opening tag so it can be parsed $init_tag = substr($phrase_bunch, 0, ($close_of_open + strlen($tag_start_end))); $init_tag = str_replace($tag_start, '', $init_tag); $init_tag = substr($init_tag, 0, strlen($init_tag) - 1); // Get the args out of the tag $args = preg_split('#([0-9].*?)=#', $init_tag); foreach ($args AS $arg) { if ($arg AND $arg != ' ') { $arg = trim($arg); $arg = substr($arg, 2); $arg = substr($arg, 0, strlen($arg) - 2); $arglist[] = $arg; } } // Just get the phrase name $phrase_name = preg_replace('#(.*?)#i', '$2', $phrase_bunch); // Wrap the parsed data into the build function $function_wrap = '" . ' . $this->langconst . '(\'' . $phrase_name . '\', "' . implode('", "', $arglist) . '") . "'; // Replace the fully-parsed string back into the template $template = substr_replace($template, $function_wrap, $location_start, $location_end + strlen($tag_end) - $location_start); unset($arglist); } return $template; } /** * Parser for in-line template conditionals * * @param str Template data awaiting processing * * @return str Parsed template data */ function _parse_conditionals($template) { global $_isso; // Tag data $tag_start = '>>>>> " . $end . " >>>>>> "; // extract the condition $condition = substr($template, $i, $end - $i); //echo "||||||" . $_isso->sanitize($condition) . "||||||"; // complete the open array_push($stack, $i); //echo "OPEN `$i`: " . print_r($stack, true); } // do we have an end? // make sure that it's the whole tag else if (substr($template, $i, strlen($tag_end)) == $tag_end) { // calculate the end of the tag $end = $i + strlen($tag_end); //echo ">>>>> $end <<<<<<"; // find the most recently opened condition $last = array_pop($stack); $depth = count($stack); //echo "CLOSE `$last`: " . print_r($stack, true); // get the full condition $fullspread = substr($template, $last, $end - $last); // remove the beginning tag $conditional = substr($fullspread, strlen($tag_start)); // find the end of the expression $temp_end = strpos($conditional, $tag_start_end); // save the expression $expression = stripslashes(substr($conditional, 0, $temp_end)); // remove the condition from the broken conditional $conditional = substr($conditional, strlen($expression) + strlen($tag_start_end)); // remove the tailing end tag $conditional = substr($conditional, 0, strlen($conditional) - strlen($tag_end)); //echo ">>>>>>> " . $_isso->sanitize($conditional) . " >>>>>>>"; // save all the data for later so we can do a quick replacement $parsed["$depth"]["$last"] = array( 'raw' => $fullspread, 'parsed' => '" . iff(' . stripslashes($expression) . ',"' . $conditional . '") . "', /*'start' => $last, 'end' => $end, 'length' => array( 'raw' => $end - $last, //strlen($fullspread), 'new' => 0 )*/ ); //$parsed["$depth"]["$last"]['length']['new'] = strlen($parsed["$depth"]["$last"]['parsed']); //$temp =& $parsed["$depth"]["$last"]; //$template = substr_replace($template, $temp['unique'], $temp['start'], $temp['end']); // replace the new unique tag into the mix //echo "<<<< " . $_isso->sanitize($parsed) . " >>>> "; // replace it back into the template //$template = substr_replace($template, $parsed, $last, $last - $end + strlen($tag_end)); //echo ">>>>>>>>>>> $template <<<<<<<<<<"; //exit; //echo "<<<<<<<<<<<<<<<<< $conditional >>>>>>>>>>>"; } } //print_r($parsed); $offset = 0; ksort($parsed); foreach ($parsed AS $depth => $conditionals) { ksort($conditionals); //print_r($conditionals); foreach ($conditionals AS $parsed) { $template = str_replace($parsed['raw'], $parsed['parsed'], $template); //print_r($parsed); //$template = substr_replace($template, $parsed['parsed'], $parsed['start'] - $offset, strlen($parsed['parsed']) + $offset); //$offset += 2 * ($parsed['length']['raw'] - $parsed['length']['new']); //$template = str_replace($conditional['unique'], $conditional['parsed'], $template); } } $template = str_replace('', '","', $template); return $template; return ''; //return $template; // ------------------------------ // ------ END //exit; while (1) { // Do we have an $tag_else $location_else = strpos($parsed, $tag_else); if ($location_else !== false) { $data = explode($tag_else, $parsed); // um... wtf? if (count($data) > 2) { trigger_error('Multiple else statements encountered while parsing conditionals in template', ERR_ALERT); } // Set the data $iftrue = $data[0]; $iffalse = $data[1]; } // Nope, reassign variables else { $iftrue = $parsed; $iffalse = null; } // Parse the iff()'d expression back into the template data $template = substr_replace($template, $parsed_expression, $location_start, $location_end + strlen($tag_end) - $location_start); } // Repeat this process until it can't find any more // expressions, then send back the parsed template data // for final output return $template; } } /*=====================================================================*\ || ################################################################### || # $HeadURL$ || # $Id$ || ################################################################### \*=====================================================================*/ ?>