db = $db; } // ################################################################### /** * Sets the table name and column information * * @param string The database table name * @param string Name of the column that stores the name of the template * @param string Name of the column that stores template data */ public function setDatabaseTable($tableName, $nameColumn, $dataColumn) { $this->tableName = $tableName; $this->nameColumn = $nameColumn; $this->dataColumn = $dataColumn; } // ################################################################### /** * Sets the pre-parse hook method which is called before any other * processing is done on the template. * * @param string Method name */ public function setPreParseHook($hook) { $this->preParseHook = $hook; } // ################################################################### /** * 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 */ public function cache($namearray) { if (sizeof($this->cache) > 0) { trigger_error('You cannot cache templates more than once per initialization'); } else { $templates = $this->db->query("SELECT * FROM " . $this->tableName . " WHERE " . $this->nameColumn . " IN ('" . implode("', '", $namearray) . "')" . ($this->extrawhere ? ' ' . $this->extrawhere : '')); while ($template = $this->db->fetch_array($templates)) { $this->cache[ $template[ $this->nameColumn ] ] = $this->_parse($template[ $this->dataColumn ]); $this->usage[ $template[ $this->nameColumn ] ] = 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 */ public function fetch($name) { if (isset($this->cache["$name"])) { $template = $this->cache["$name"]; } else { $this->uncached[] = $name; BSRegister::Debug("Manually loading template `$name`"); $template = $this->_loadTemplate($name); $template = $this->_parseTemplate($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 */ public function flush($template) { ob_start(); if (empty($template)) { trigger_error('There was no output to print'); exit; } if (BSRegister::GetDebug() AND isset($_GET['query'])) { if (is_array($this->registry->modules[ISSO_DB_LAYER]->history)) { foreach ($this->registry->modules[ISSO_DB_LAYER]->history AS $query) { echo $this->registry->modules[ISSO_DB_LAYER]->construct_query_debug($query); } } exit; } if ($this->doneflush) { trigger_error('A template has already been sent to the output buffer'); exit; } $template = str_replace('', $this->registry->construct_debug_block(true) . '', $template); print($template); } // ################################################################### /** * Loads an additional template from the database * * @param string The name of the template * * @return string Template data from the database */ private function _loadTemplate($name) { if ($template = $this->db->query_first("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"); exit; } } // ################################################################### /** * A wrapper for all the parsing functions and compiling functins * * @param string Unparsed template data * * @return string Parsed template data */ protected function _parseTemplate($template) { $template = str_replace('"', '\"', $template); if (function_exists($this->preParseHook)) { $template = call_user_func($this->preParseHook, $template); } $template = $this->_parsePhrases($template); $template = $this->_parseConditionals($template); return $template; } // ################################################################### /** * Prepares language and locale information inside templates * * @param string Template data to be processed * * @return string Language-ready template data */ private function _parsePhrases($template) { $tag_start = ' $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); } // Process the empty phrase objects -- do this now so we don't have to worry about it when we're parsing later $template = preg_replace('#\{@\\\"(.*?)\\\"\}#ise', '$this->_templateString(\'$1\')', $template); return $template; } // ################################################################### /** * Turns a localized phrase tag into a function call * * @param string Phrase text * * @return string Function call for phrase text */ private function _templateString($text) { return '" . ' . $this->langcall . '(\'' . str_replace(array('\\\"', "'"), array('"', "\'"), $text) . '\') . "'; } // ################################################################### /** * Parser for in-line template conditionals * * @param string Template data awaiting processing * * @return string Parsed template data */ private function _parseConditionals($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] = ''; } // 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. * * @access private * * @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$ || ################################################################### \*=====================================================================*/ ?>