registry->modules[\'localize\']->string'; /** * The name of the function phrases are sprintf() parsed with * @var string */ var $langconst = 'sprintf'; /** * Array of pre-compiled templates that are stored to decrease server load * @var array */ var $cache = array(); /** * A list of the number of times each template has been used * @var array */ var $usage = array(); /** * A list of templates that weren't cached, but are still used * @var array */ var $uncached = array(); /** * Whether or not the page has been flush()'d already * @var bool */ var $doneflush = false; /** * Constructor */ function Template(&$registry) { $this->registry =& $registry; } /** * Takes an array of template names, loads them, and then stores * a parsed version for optimum speed. * * @param array List of template names to be cached */ function cache($namearray) { if (sizeof($this->cache) > 0) { trigger_error('You cannot cache templates more than once per initialization', E_USER_WARNING); } else { $templates = $this->registry->modules['db_mysql']->query("SELECT * FROM " . $this->tablename . " WHERE " . $this->namecolumn . " IN ('" . implode("', '", $namearray) . "')" . ($this->extrawhere ? $this->extrawhere : '')); while ($template = $this->registry->modules['db_mysql']->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 string The name of the template * * @return string A parsed and loaded template */ function fetch($name) { if (isset($this->cache["$name"])) { $template = $this->cache["$name"]; } else { $this->uncached[] = $name; $this->registry->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 string Compiled and ready template */ function flush($template) { ob_start(); if (empty($template)) { trigger_error('There was no output to print', E_USER_ERROR); exit; } if ($this->registry->debug AND isset($_GET['query'])) { if (is_array($this->registry->modules['db_mysql']->history)) { echo '
';
				foreach ($this->registry->modules['db_mysql']->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', E_USER_ERROR); exit; } if ($this->registry->debug) { // --- START $debug = "\n"; $debug = "\n
\n" . $this->registry->_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 string The name of the template * * @return string Template data from the database */ function _load($name) { if ($template = $this->registry->modules['db_mysql']->query("SELECT * FROM " . $this->tablename . " WHERE " . $this->namecolumn . " = '$name'" . ($this->extrawhere ? $this->extrawhere : ''))) { return $template[ $this->datacolumn ]; } else { trigger_error("The template '$name' could not be loaded", E_USER_ERROR); exit; } } /** * A wrapper for all the parsing functions and compiling functins * * @param string Unparsed template data * * @return string 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 string Template data to be processed * * @return string 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 string Template data awaiting processing * * @return string Parsed template data */ function _parse_conditionals($template) { // tag data $tag_start = ' $relpos = $tag_full['else'] - $tag_full['posi']; // calculate the length of the expression and opening tag $length = strlen($parsed[0]) + strlen($tag_start) + strlen($tag_start_end); // relative to the start of iftrue $elsepos = $relpos - $length; $parsed[1] = substr($conditional, 0, $elsepos); $parsed[2] = substr($conditional, $elsepos + strlen($tag_else)); } // no else to handle else { $parsed[1] = $conditional; $parsed[2] = ''; } #var_dump($parsed); // final parsed output $parsed = '" . ((' . stripslashes($parsed[0]) . ') ? "' . $parsed[1] . '" : "' . $parsed[2] . '") . "'; // replace the conditional $template = str_replace($fullspread, $parsed, $template); // reset the parser $offset = $tag_full['posi'] + strlen($tag_start) + strlen($tag_start_end); $tag_full = array(); $stack = array(); $parsed = array(); unset($fullspread, $conditional, $temp_end, $relpos, $length, $elsepos); break; } } } return $template; } } /** * Debugging function used to print characters * in a string that are around a certain position. * * @param string The haystack string * @param integer Position to print around */ function print_around($str, $pos) { echo '>>> PA >>>>>>>>['; echo htmlspecialchars($str[ $pos - 5 ]); echo htmlspecialchars($str[ $pos - 4 ]); echo htmlspecialchars($str[ $pos - 3 ]); echo htmlspecialchars($str[ $pos - 2 ]); echo htmlspecialchars($str[ $pos - 1 ]); echo '©'; echo htmlspecialchars($str[ $pos + 0 ]); echo htmlspecialchars($str[ $pos + 1 ]); echo htmlspecialchars($str[ $pos + 2 ]); echo htmlspecialchars($str[ $pos + 3 ]); echo htmlspecialchars($str[ $pos + 4 ]); echo htmlspecialchars($str[ $pos + 5 ]); echo ']<<<<<<<< PA <<<'; } /*=====================================================================*\ || ################################################################### || # $HeadURL$ || # $Id$ || ################################################################### \*=====================================================================*/ ?>