2 /*=====================================================================*\
3 || ################################################################### ||
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || # --------------------------------------------------------------- # ||
9 || # User License Agreement at http://www.iris-studios.com/license/ # ||
10 || ################################################################### ||
11 \*=====================================================================*/
14 * Database-Driven Template System
20 $OBJECT = 'Database Template System';
21 $CLASS = 'DB_Template';
25 * Database-Driven Template System
27 * This framework is a backend to the database template engine and
28 * contains all the parsing algorithms.
30 * @author Iris Studios, Inc.
31 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
39 * Name of the database table templates are in
43 var $templatetable = '';
46 * Name of the table column template names are in
48 * @see $templatetable, _load()
53 * Name of the table column templates are in
55 * @see templatetable, _load()
60 * Additional WHERE clauses for the template fetch SQL
62 * @see _load(), cache()
67 * The name of the function phrases are fetched with
69 * @see _parse_phrases()
71 var $langcall = 'fetch_phrase';
74 * The name of the function phrases are sprintf() parsed with
76 * @see _parse_phrases()
78 var $langconst = 'construct_phrase';
81 * Array of pre-compiled templates that are stored to decrease server load
88 * A list of the number of times each template has been used
95 * A list of templates that weren't cached, but are still used
99 var $uncached = array();
102 * Whether or not the page has been flush()'d already
106 var $doneflush = false;
109 * Takes an array of template names, loads them, and then stores
110 * a parsed version for optimum speed.
112 * @param array List of template names to be cached
114 function cache($namearray)
118 if (sizeof($this->cache
) > 0)
120 trigger_error('You cannot cache templates more than once per initialization', ERR_WARNING
);
124 $templates = $_isso->db
->query("SELECT * FROM " . $this->tablename
. " WHERE " . $this->namecolumn
. " IN ('" . implode("', '", $namearray) . "')" . (($this->extrawhere
) ? $this->extrawhere
: ''));
125 while ($template = $_isso->db
->fetch_array($templates))
127 $template = $this->_parse($template);
128 $this->cache
[ $template[ $this->namecolumn
] ] = $template[ $this->datacolumn
];
129 $this->usage
["$name"] = 0;
135 * Loads a template from the cache or the _load function and
136 * stores the parsed version of it
138 * @param str The name of the template
140 * @return str A parsed and loaded template
142 function fetch($name)
146 if (isset($this->cache["$name"]))
148 $template = $this->cache
["$name"];
152 $this->uncached[] = $name;
153 $_isso->debug("Manually loading template `
$name`
");
154 $template = $this->_load($name);
155 $template = $this->_parse($template);
158 if (!isset($this->usage["$name"]))
160 $this->usage
["$name"] = 0;
163 $this->usage["$name"]++
;
169 * Output a template fully compiled to the browser
171 * @param str Compiled and ready template
173 function flush($template)
179 if (empty($template))
181 trigger_error('There was no output to print', E_USER_ERROR
);
185 if ($_isso->debug
AND isset($_GET['query']))
187 if (is_array($_isso->db
->history
))
190 foreach ($_isso->db
->history
AS $query)
192 echo $query . "\n\n<hr />\n\n";
199 if ($this->doneflush
)
201 trigger_error('A template has already been sent to the output buffer', E_USER_ERROR
);
213 foreach ($this->usage
AS $name => $count)
215 if (in_array($name, $this->uncached
))
217 $optlist[] = $name . '[' . $count . ']';
219 $usage[] = $name . " ($count)";
222 $sizeof = sizeof($this->uncached
);
225 $debug .= "\n\t<li><strong style=\"color: red\">Uncached Template(s):</strong> $sizeof ( " . implode(' ', $optlist) . " )</li>";
229 $scinfo = 'Not Under Source Control';
230 $possiblescms = array('cvs', 'svn', 'cvs_information', 'svn_information', 'scm', 'sc_information', 'scm_information');
231 foreach ($possiblescms AS $scm)
233 if (defined(strtoupper($scm)))
235 $scinfo = constant(strtoupper($scm));
239 if (strpos($scinfo, 'RCSfile:') !== false)
243 else if (strpos($scinfo, ',v ') !== false)
248 else if (strpos($scinfo, 'URL:') !== false)
252 else if (strpos($scinfo, 'https://') !== false OR strpos($scinfo, 'http://') !== false)
256 // not found so just return it
257 // try a SVN ID tag as we can't really tell if we're using it
260 $test = preg_replace('#\$' . 'Id: (.+?) (.+?) [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}(.+?) (.+?) \$#', '\\1 - SVN \\2', $scinfo);
261 if ($test == '$' . 'Id
: $')
263 $scinfo = 'Not Under Source Control';
274 $scinfo = preg_replace('#\$' . 'RCSfile: (.+?) \$#', '\\1', $scinfo);
275 $scinfo = preg_replace('#\$' . 'Revision: (.+?) \$#', 'CVS \\1', $scinfo);
276 $scinfo = preg_replace('#\$' . 'Id: (.+?) (.+?) [0-9]{4}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} (.+?) (.+?) \$#', '\\1 - CVS \\2', $scinfo);
278 else if ($type == 'svn')
280 $scinfo = preg_replace('#\$' . '(Head)?URL: (.+?) \$#e', "end(explode('/', '\\2'))", $scinfo);
281 $scinfo = preg_replace('#\$' . '(LastModified)?Revision: (.+?) \$#', 'SVN \\2', $scinfo);
282 $scinfo = preg_replace('#\$' . 'Id: (.+?) ([0-9].+?) [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}(.+?) (.+?) \$#', '\\1 - SVN \\2', $scinfo);
286 $scinfo = 'Not Under Source Control';
291 $scinfo = trim($scinfo);
292 $debug .= "\n\t<li><strong>Source Control:</strong> $scinfo</li>";
295 $debug .= "\n\t<li><strong>Total Queries:</strong> " . sizeof($_isso->db
->history
) . " (<a href=\"" . $_isso->sanitize($_SERVER['REQUEST_URI']) . ((strpos($_SERVER['REQUEST_URI'], '?') !== false) ? '&query=1' : '?query=1') . "\">?<a/>)</li>";
298 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Debug Notices (" . sizeof($_isso->debuginfo
) . ")</option>";
299 foreach ((array)$_isso->debuginfo
AS $msg)
301 $debug .= "\n\t\t\t<option>--- $msg</option>";
303 $debug .= "\n\t\t</select>\n\t</li>";
306 $modules = $_isso->show_modules(true);
307 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Loaded Modules (" . sizeof($modules) . ")</option>";
308 foreach ($modules AS $mod)
310 $debug .= "\n\t\t\t<option>--- $mod</option>";
312 $debug .= "\n\t\t</select>\n\t</li>";
315 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Template Usage (" . array_sum($this->usage
) . ")</option>";
316 foreach ($usage AS $tpl)
318 $debug .= "\n\t\t\t<option>--- $tpl</option>";
320 $debug .= "\n\t\t</select>\n\t</li>";
325 $debug = "\n<hr />\n" . $_isso->_message('Debug Information', $debug, 1, true);
326 $template = str_replace('</body>', "\n\n<!-- dev debug -->\n<div align=\"center\">\n$debug\n</div>\n<!-- / dev debug -->\n\n</body>", $template);
329 print(stripslashes($template));
333 * Loads an additional template from the database
335 * @param str The name of the template
337 * @return str Template data from the database
339 function _load($name)
342 if ($template = $_isso->db
->query("SELECT * FROM " . $this->tablename
. " WHERE " . $this->namecolumn
. " = '$name'" . (($this->extrawhere) ? $this->extrawhere : '')))
344 return $template[ $this->datacolumn ];
348 trigger_error("The template
'$name' could not be loaded", E_USER_ERROR);
354 * A wrapper for all the parsing functions and compiling functins
356 * @param str Unparsed template data
358 * @return str Parsed template data
360 function _parse($template)
362 $template = stripslashes($template);
363 $template = str_replace('"', '\"', $template);
364 $template = $this->_parse_phrases($template);
365 $template = $this->_parse_conditionals($template);
370 * Prepares language and locale information inside templates
372 * @param str Template data to be processed
374 * @return str Language-ready template data
376 function _parse_phrases($template)
378 $tag_start = '<lang ';
379 $tag_start_end = '">';
380 $tag_end = '</lang
>';
382 $location_start = -1;
385 // Process the empty phrase objects -- do this now so we don't have to worry about it when we
're parsing later
386 $template = preg_replace('#\{lang\.(.*?)\}#i', '" . ' . $this->langcall . '(\'$1\') . "', $template);
390 // Find the start language object tag
391 $location_start = strpos($template, $tag_start, $location_end +
1);
392 if ($location_start === false)
398 $location_end = strpos($template, $tag_end, $location_end +
strlen($tag_end));
399 if ($location_end === false)
404 // Extract the language object
405 $phrase_bunch = substr($template, $location_start, ($location_end +
strlen($tag_end)) - $location_start);
407 // Find the close to the opening <lang>
408 $close_of_open = strpos($phrase_bunch, $tag_start_end);
409 if ($close_of_open === false)
414 // Extract the opening tag so it can be parsed
415 $init_tag = substr($phrase_bunch, 0, ($close_of_open +
strlen($tag_start_end)));
416 $init_tag = str_replace($tag_start, '', $init_tag);
417 $init_tag = substr($init_tag, 0, strlen($init_tag) - 1);
419 // Get the args out of the tag
420 $args = preg_split('#([0-9].*?)=#', $init_tag);
421 foreach ($args AS $arg)
423 if ($arg AND $arg != ' ')
426 $arg = substr($arg, 2);
427 $arg = substr($arg, 0, strlen($arg) - 2);
432 // Just get the phrase name
433 $phrase_name = preg_replace('#<lang(.*?)>(.*?)</lang>#i', '$2', $phrase_bunch);
435 // Wrap the parsed data into the build function
436 $function_wrap = '" . ' . $this->langconst . '(\'' . $phrase_name . '\', "' . implode('", "', $arglist) . '") . "';
438 // Replace the fully-parsed string back into the template
439 $template = substr_replace($template, $function_wrap, $location_start, $location_end + strlen($tag_end) - $location_start);
448 * Parser for in-line template conditionals
450 * @param str Template data awaiting processing
452 * @return str Parsed template data
454 function _parse_conditionals($template)
459 $tag_start = '<if condition
=\"';
460 $tag_start_end = '\"
>';
461 $tag_else = '<else />';
468 for ($i = 0; $i < strlen($template); $i++)
470 // we've found ourselves a conditional
!
471 if (substr($template, $i, strlen($tag_start)) == $tag_start)
473 // push the position into the tag stack
474 array_push($stack, $i);
476 //echo "OPEN `$i`: " . print_r($stack, true);
478 // do we have an end tag?
479 else if (substr($template, $i, strlen($tag_end)) == $tag_end)
481 // calculate the position of the end tag
482 $end = $i +
strlen($tag_end);
484 // find the most recently opened condition
485 $last = array_pop($stack);
487 // how deep are we nested?
488 $depth = count($stack);
490 //echo "CLOSE `$last`: " . print_r($stack, true);
492 // get the full conditional
493 $fullspread = substr($template, $last, $end - $last);
495 // remove the beginning tag
496 $conditional = substr($fullspread, strlen($tag_start));
498 // find the end of the expression
499 $temp_end = strpos($conditional, $tag_start_end);
501 // save the expression
502 $expression = stripslashes(substr($conditional, 0, $temp_end));
504 // remove the expression from the broken conditional
505 $conditional = substr($conditional, strlen($expression) +
strlen($tag_start_end));
507 // remove the tailing end tag
508 $conditional = substr($conditional, 0, strlen($conditional) - strlen($tag_end));
510 // save all the data for later so we can do a quick replacement
511 $parsed["$depth"]["$last"] = array(
512 'raw' => $fullspread,
516 if (substr_count($conditional, $tag_else) == 0)
518 $parsed["$depth"]["$last"]['parsed'] = '" . ((' . stripslashes($expression) . ') ? "' . $conditional . '" : "") . "';
522 $parsed["$depth"]["$last"]['parsed'] = '" . ((' . stripslashes($expression) . ') ? "' . $conditional . '") . "';
527 // sort so the top-most ones are done first
529 foreach ($parsed AS $depth => $conditionals)
531 // sort so we do them in order
532 ksort($conditionals);
533 foreach ($conditionals AS $parsed)
535 // replace the old with the new
536 $template = str_replace($parsed['raw'], $parsed['parsed'], $template);
539 // hackish way to get end tags working
540 $template = str_replace($tag_else, '" : "', $template);
546 /*=====================================================================*\
547 || ###################################################################
550 || ###################################################################
551 \*=====================================================================*/