2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
23 * Database-Driven Template System
30 * Database-Driven Template System
32 * This framework is a backend to the database template engine and
33 * contains all the parsing algorithms.
35 * @author Iris Studios, Inc.
36 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
44 * Framework registry object
50 * Name of the database table templates are in
56 * Name of the table column template names are in
62 * Name of the table column templates are in
68 * Additional WHERE clauses for the template fetch SQL
74 * The name of the function phrases are fetched with
77 var $langcall = '$GLOBALS[\'isso:null-framework\']->modules[\'localize\']->string';
80 * The name of the function phrases are sprintf() parsed with
83 var $langconst = 'sprintf';
86 * Array of pre-compiled templates that are stored to decrease server load
92 * A list of the number of times each template has been used
98 * A list of templates that weren't cached, but are still used
101 var $uncached = array();
104 * Whether or not the page has been flush()'d already
107 var $doneflush = false
;
109 // ###################################################################
113 function __construct(&$registry)
115 $this->registry
=& $registry;
118 // ###################################################################
120 * (PHP 4) Constructor
122 function Template(&$registry)
124 $this->__construct($registry);
127 // ###################################################################
129 * Takes an array of template names, loads them, and then stores a
130 * parsed version for optimum speed.
134 * @param array List of template names to be cached
136 function cache($namearray)
138 if (sizeof($this->cache
) > 0)
140 trigger_error('You cannot cache templates more than once per initialization', E_USER_WARNING
);
144 $templates = $this->registry
->modules
['db_mysql']->query("SELECT * FROM " . $this->tablename
. " WHERE " . $this->namecolumn
. " IN ('" . implode("', '", $namearray) . "')" . ($this->extrawhere ?
$this->extrawhere
: ''));
145 while ($template = $this->registry
->modules
['db_mysql']->fetch_array($templates))
147 $this->cache
[ $template[ $this->namecolumn
] ] = $this->_parse($template[ $this->datacolumn
]);
148 $this->usage
["$name"] = 0;
153 // ###################################################################
155 * Loads a template from the cache or the _load function and stores the
156 * parsed version of it
160 * @param string The name of the template
162 * @return string A parsed and loaded template
164 function fetch($name)
166 if (isset($this->cache
["$name"]))
168 $template = $this->cache
["$name"];
172 $this->uncached
[] = $name;
173 $this->registry
->debug("Manually loading template `$name`");
174 $template = $this->_load($name);
175 $template = $this->_parse($template);
178 if (!isset($this->usage
["$name"]))
180 $this->usage
["$name"] = 0;
183 $this->usage
["$name"]++
;
188 // ###################################################################
190 * Output a template fully compiled to the browser
194 * @param string Compiled and ready template
196 function flush($template)
200 if (empty($template))
202 trigger_error('There was no output to print', E_USER_ERROR
);
206 if ($this->registry
->debug
AND isset($_GET['query']))
208 if (is_array($this->registry
->modules
['db_mysql']->history
))
211 foreach ($this->registry
->modules
['db_mysql']->history
AS $query)
213 echo $query . "\n\n<hr />\n\n";
220 if ($this->doneflush
)
222 trigger_error('A template has already been sent to the output buffer', E_USER_ERROR
);
226 $template = str_replace('</body>', $this->registry
->construct_debug_block(true
) . '</body>', $template);
231 // ###################################################################
233 * Loads an additional template from the database
237 * @param string The name of the template
239 * @return string Template data from the database
241 function _load($name)
243 if ($template = $this->registry
->modules
['db_mysql']->query("SELECT * FROM " . $this->tablename
. " WHERE " . $this->namecolumn
. " = '$name'" . ($this->extrawhere ?
$this->extrawhere
: '')))
245 return $template[ $this->datacolumn
];
249 trigger_error("The template '$name' could not be loaded", E_USER_ERROR
);
254 // ###################################################################
256 * A wrapper for all the parsing functions and compiling functins
260 * @param string Unparsed template data
262 * @return string Parsed template data
264 function _parse($template)
266 $template = str_replace('"', '\"', $template);
267 $template = $this->_parse_phrases($template);
268 $template = $this->_parse_conditionals($template);
272 // ###################################################################
274 * Prepares language and locale information inside templates
278 * @param string Template data to be processed
280 * @return string Language-ready template data
282 function _parse_phrases($template)
284 $tag_start = '<lang ';
285 $tag_start_end = '\">';
286 $tag_end = '</lang>';
288 $location_start = -1;
291 // Process the empty phrase objects -- do this now so we don't have to worry about it when we're parsing later
292 $template = preg_replace('#\{@\\\"(.*?)\\\"\}#ie', '$this->_phrase_string(\'$1\')', $template);
296 // Find the start language object tag
297 $location_start = strpos($template, $tag_start, $location_end +
1);
298 if ($location_start === false
)
304 $location_end = strpos($template, $tag_end, $location_end +
strlen($tag_end));
305 if ($location_end === false
)
310 // Extract the language object
311 $phrase_bunch = substr($template, $location_start, ($location_end +
strlen($tag_end)) - $location_start);
313 // Find the close to the opening <lang>
314 $close_of_open = strpos($phrase_bunch, $tag_start_end);
315 if ($close_of_open === false
)
320 // Extract the opening tag so it can be parsed
321 $init_tag = substr($phrase_bunch, 0, ($close_of_open +
strlen($tag_start_end)));
322 $init_tag = str_replace($tag_start, '', $init_tag);
323 $init_tag = substr($init_tag, 0, strlen($init_tag) - 1);
325 // Get the args out of the tag
326 $args = preg_split('#([0-9].*?)=#', $init_tag);
327 foreach ($args AS $arg)
329 if ($arg AND $arg != ' ')
332 $arg = substr($arg, 2);
333 $arg = substr($arg, 0, strlen($arg) - 2);
338 // Just get the phrase name
339 $phrase_name = preg_replace('#<lang(.*?)>(.*?)</lang>#i', '$2', $phrase_bunch);
341 // Wrap the parsed data into the build function
342 $function_wrap = '" . ' . $this->langconst
. '("' . /*str_replace(array('\"', "'"), array('"', "\'"),*/ $phrase_name/*)*/ . '", "' . implode('", "', $arglist) . '") . "';
344 // Replace the fully-parsed string back into the template
345 $template = substr_replace($template, $function_wrap, $location_start, $location_end +
strlen($tag_end) - $location_start);
353 // ###################################################################
355 * Turns a localized phrase tag into a function call
359 * @param string Phrase text
361 * @return string Function call for phrase text
363 function _phrase_string($text)
365 return '" . ' . $this->langcall
. '(\'' . str_replace(array('\\\"', "'"), array('"', "\'"), $text) . '\') . "';
368 // ###################################################################
370 * Parser for in-line template conditionals
374 * @param string Template data awaiting processing
376 * @return string Parsed template data
378 function _parse_conditionals($template)
381 $tag_start = '<if condition=\"';
382 $tag_start_end = '\">';
383 $tag_else = '<else />';
389 // the information about the current active tag
398 if (strpos($template, $tag_start) === false
)
403 for ($i = $offset; $i < strlen($template); $i++
)
405 // we've found ourselves a conditional!
406 if (substr($template, $i, strlen($tag_start)) == $tag_start)
408 // push the position into the tag stack
411 array_push($stack, $i);
415 $tag_full['posi'] = $i;
419 else if (substr($template, $i, strlen($tag_else)) == $tag_else)
421 if (count($stack) == 0 AND !isset($tag_full['else']))
423 $tag_full['else'] = $i;
426 // do we have an end tag?
427 else if (substr($template, $i, strlen($tag_end)) == $tag_end)
429 if (count($stack) != 0)
435 // calculate the position of the end tag
436 $tag_full['posf'] = $i +
strlen($tag_end) - 1;
438 // extract the entire conditional from the template
439 $fullspread = substr($template, $tag_full['posi'], $tag_full['posf'] - $tag_full['posi'] +
1);
441 // remove the beginning tag
442 $conditional = substr($fullspread, strlen($tag_start));
444 // find the end of the expression
445 $temp_end = strpos($conditional, $tag_start_end);
447 // save the expression
448 $parsed[0] = stripslashes(substr($conditional, 0, $temp_end));
450 // remove the expression from the conditional
451 $conditional = substr($conditional, strlen($parsed[0]) +
strlen($tag_start_end));
453 // remove the tailing end tag
454 $conditional = substr($conditional, 0, strlen($conditional) - strlen($tag_end));
457 if (isset($tag_full['else']))
459 // now relative to the start of the <if>
460 $relpos = $tag_full['else'] - $tag_full['posi'];
462 // calculate the length of the expression and opening tag
463 $length = strlen($parsed[0]) +
strlen($tag_start) +
strlen($tag_start_end);
465 // relative to the start of iftrue
466 $elsepos = $relpos - $length;
468 $parsed[1] = substr($conditional, 0, $elsepos);
469 $parsed[2] = substr($conditional, $elsepos +
strlen($tag_else));
474 $parsed[1] = $conditional;
479 // final parsed output
480 $parsed = '" . ((' . stripslashes($parsed[0]) . ') ? "' . $parsed[1] . '" : "' . $parsed[2] . '") . "';
482 // replace the conditional
483 $template = str_replace($fullspread, $parsed, $template);
486 $offset = $tag_full['posi'] +
strlen($tag_start) +
strlen($tag_start_end);
490 unset($fullspread, $conditional, $temp_end, $relpos, $length, $elsepos);
500 // ###################################################################
502 * Debugging function used to print characters in a string that are
503 * around a certain position.
507 * @param string The haystack string
508 * @param integer Position to print around
510 function print_around($str, $pos)
512 echo '>>> PA >>>>>>>>[';
513 echo htmlspecialchars($str[ $pos - 5 ]);
514 echo htmlspecialchars($str[ $pos - 4 ]);
515 echo htmlspecialchars($str[ $pos - 3 ]);
516 echo htmlspecialchars($str[ $pos - 2 ]);
517 echo htmlspecialchars($str[ $pos - 1 ]);
519 echo htmlspecialchars($str[ $pos +
0 ]);
520 echo htmlspecialchars($str[ $pos +
1 ]);
521 echo htmlspecialchars($str[ $pos +
2 ]);
522 echo htmlspecialchars($str[ $pos +
3 ]);
523 echo htmlspecialchars($str[ $pos +
4 ]);
524 echo htmlspecialchars($str[ $pos +
5 ]);
525 echo ']<<<<<<<< PA <<<';
528 /*=====================================================================*\
529 || ###################################################################
532 || ###################################################################
533 \*=====================================================================*/