2 /*=====================================================================*\
3 || ################################################################### ||
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # All parts of this file are ©2003-[#]year[#] Iris Studios, Inc. No # ||
7 || # part of this file may be reproduced in any way: part or whole. # ||
8 || # --------------------------------------------------------------- # ||
9 || # ©2003 - [#]year[#] Iris Studios, Inc. | http://www.iris-studios.com # ||
10 || ################################################################### ||
11 \*=====================================================================*/
13 $OBJECT = 'Database Template System';
14 $CLASS = 'DB_Template';
18 * Database-Driven Template System
20 * This framework is a backend to the database template engine and
21 * contains all the parsing algorithms.
23 * @author Iris Studios, Inc.
24 * @copyright Copyright ©2003 - [#]year[#], Iris Studios, Inc.
31 * Global environment variables
33 * This stores all of the cached and parsed template data
35 * @var tablename The name of the database table templates are in
36 * @var namecolumn The name of the database column template names are in
37 * @var datacolumn The name of the database column template data is stored in
38 * @var extrawhere Additional information for the WHERE clause in template loading
39 * @var langcall Function wrapper for phrase fetching system
40 * @var langconst Function that constructs a phrase with sprintf()
41 * @var cache Stores all fettched templates
42 * @var usage Counter for template usage
43 * @var uncached List of uncached templates
44 * @var doneflush Variable to check and see if output has been sent
46 var $templatetable = '';
50 var $langcall = 'fetch_phrase';
51 var $langconst = 'construct_phrase';
54 var $uncached = array();
55 var $doneflush = false
;
58 * Takes an array of template names, loads them, and then stores
59 * a parsed version for optimum speed.
61 * @var array List of template names to be cached
63 function cache($namearray)
67 if (sizeof($this->cache
) > 0)
69 trigger_error('You cannot cache templates more than once per initialization', ERR_WARNING
);
73 $templates = $_isso->db
->query("SELECT * FROM " . $this->tablename
. " WHERE " . $this->namecolumn
. " IN ('" . implode("', '", $namearray) . "')" . iff($this->extrawhere
, $this->extrawhere
));
74 while ($template = $_isso->db
->fetch_array($templates))
76 $template = $this->_parse($template);
77 $this->cache
[ $template[ $this->namecolumn
] ] = $template[ $this->datacolumn
];
78 $this->usage
["$name"] = 0;
84 * Loads a template from the cache or the _load function and
85 * stores the parsed version of it
87 * @param str The name of the template
89 * @return str A parsed and loaded template
95 if (isset($this->cache
["$name"]))
97 $template = $this->cache
["$name"];
101 $this->uncached
[] = $name;
102 $_isso->debug("Manually loading template `$name`");
103 $template = $this->_load($name);
104 $template = $this->_parse($template);
107 if (!isset($this->usage
["$name"]))
109 $this->usage
["$name"] = 0;
112 $this->usage
["$name"]++
;
118 * Output a template fully compiled to the browser
120 * @param str Compiled and ready template
122 function flush($template)
128 if (empty($template))
130 trigger_error('There was no output to print', ERR_FATAL
);
134 if ($_isso->debug
AND isset($_GET['query']))
136 if (is_array($_isso->db
->history
))
139 foreach ($_isso->db
->history
AS $query)
141 echo $query . "\n\n<hr />\n\n";
148 if ($this->doneflush
)
150 trigger_error('A template has already been sent to the output buffer', ERR_FATAL
);
162 foreach ($this->usage
AS $name => $count)
164 if (in_array($name, $this->uncached
))
166 $optlist[] = $name . '[' . $count . ']';
168 $usage[] = $name . " ($count)";
171 $sizeof = sizeof($this->uncached
);
174 $debug .= "\n\t<li><strong style=\"color: red\">Uncached Template(s):</strong> $sizeof ( " . implode(' ', $optlist) . " )</li>";
178 $scinfo = 'Not Under Source Control';
179 $possiblescms = array('cvs', 'svn', 'cvs_information', 'svn_information', 'scm', 'sc_information', 'scm_information');
180 foreach ($possiblescms AS $scm)
182 if (defined(strtoupper($scm)))
184 $scinfo = constant(strtoupper($scm));
188 if (strpos($scinfo, 'RCSfile:') !== false
)
192 else if (strpos($scinfo, ',v ') !== false
)
197 else if (strpos($scinfo, 'URL:') !== false
)
201 else if (strpos($scinfo, 'https://') !== false
OR strpos($scinfo, 'http://') !== false
)
205 // not found so just return it
206 // try a SVN ID tag as we can't really tell if we're using it
209 $scinfo = 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);
215 $scinfo = preg_replace('#\$' . 'RCSfile: (.+?) \$#', '\\1', $scinfo);
216 $scinfo = preg_replace('#\$' . 'Revision: (.+?) \$#', 'CVS \\1', $scinfo);
217 $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);
219 else if ($type == 'svn')
221 $scinfo = preg_replace('#\$' . '(Head)?URL: (.+?) \$#e', "end(explode('/', '\\2'))", $scinfo);
222 $scinfo = preg_replace('#\$' . '(LastModified)?Revision: (.+?) \$#', 'SVN \\2', $scinfo);
223 $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);
228 $scinfo = trim($scinfo);
229 $debug .= "\n\t<li><strong>Source Control:</strong> $scinfo</li>";
232 $debug .= "\n\t<li><strong>Total Queries:</strong> " . sizeof($_isso->db
->history
) . " (<a href=\"" . $_isso->sanitize($_SERVER['REQUEST_URI']) . iff(strpos($_SERVER['REQUEST_URI'], '?') !== false
, '&query=1', '?query=1') . "\">?<a/>)</li>";
235 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Debug Notices (" . sizeof($_isso->debuginfo
) . ")</option>";
236 foreach ((array)$_isso->debuginfo
AS $msg)
238 $debug .= "\n\t\t\t<option>--- $msg</option>";
240 $debug .= "\n\t\t</select>\n\t</li>";
243 $modules = $_isso->show_modules(true
);
244 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Loaded Modules (" . sizeof($modules) . ")</option>";
245 foreach ($modules AS $mod)
247 $debug .= "\n\t\t\t<option>--- $mod</option>";
249 $debug .= "\n\t\t</select>\n\t</li>";
252 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Template Usage (" . array_sum($this->usage
) . ")</option>";
253 foreach ($usage AS $tpl)
255 $debug .= "\n\t\t\t<option>--- $tpl</option>";
257 $debug .= "\n\t\t</select>\n\t</li>";
262 $debug = "\n<hr />\n" . $_isso->_message('Debug Information', $debug, 1, true
);
263 $template = str_replace('</body>', "\n\n<!-- dev debug -->\n<div align=\"center\">\n$debug\n</div>\n<!-- / dev debug -->\n\n</body>", $template);
266 print(stripslashes($template));
270 * Loads an additional template from the database
272 * @param str The name of the template
274 * @return str Template data from the database
276 function _load($name)
279 if ($template = $_isso->db
->query("SELECT * FROM " . $this->tablename
. " WHERE " . $this->namecolumn
. " = '$name'" . iff($this->extrawhere
, $this->extrawhere
)))
281 return $template[ $this->datacolumn
];
285 trigger_error("The template '$name' could not be loaded", ERR_FATAL
);
291 * A wrapper for all the parsing functions and compiling functins
293 * @param str Unparsed template data
295 * @return str Parsed template data
297 function _parse($template)
299 $template = stripslashes($template);
300 $template = str_replace('"', '\"', $template);
301 $template = $this->_parse_phrases($template);
302 $template = $this->_parse_conditionals($template);
307 * Prepares language and locale information inside templates
309 * @param str Template data to be processed
311 * @return str Language-ready template data
313 function _parse_phrases($template)
315 $tag_start = '<lang ';
316 $tag_start_end = '">';
317 $tag_end = '</lang>';
319 $location_start = -1;
322 // Process the empty phrase objects -- do this now so we don't have to worry about it when we're parsing later
323 $template = preg_replace('#\{lang\.(.*?)\}#i', '" . ' . $this->langcall
. '(\'$1\') . "', $template);
327 // Find the start language object tag
328 $location_start = strpos($template, $tag_start, $location_end +
1);
329 if ($location_start === false
)
335 $location_end = strpos($template, $tag_end, $location_end +
strlen($tag_end));
336 if ($location_end === false
)
341 // Extract the language object
342 $phrase_bunch = substr($template, $location_start, ($location_end +
strlen($tag_end)) - $location_start);
344 // Find the close to the opening <lang>
345 $close_of_open = strpos($phrase_bunch, $tag_start_end);
346 if ($close_of_open === false
)
351 // Extract the opening tag so it can be parsed
352 $init_tag = substr($phrase_bunch, 0, ($close_of_open +
strlen($tag_start_end)));
353 $init_tag = str_replace($tag_start, '', $init_tag);
354 $init_tag = substr($init_tag, 0, strlen($init_tag) - 1);
356 // Get the args out of the tag
357 $args = preg_split('#([0-9].*?)=#', $init_tag);
358 foreach ($args AS $arg)
360 if ($arg AND $arg != ' ')
363 $arg = substr($arg, 2);
364 $arg = substr($arg, 0, strlen($arg) - 2);
369 // Just get the phrase name
370 $phrase_name = preg_replace('#<lang(.*?)>(.*?)</lang>#i', '$2', $phrase_bunch);
372 // Wrap the parsed data into the build function
373 $function_wrap = '" . ' . $this->langconst
. '(\'' . $phrase_name . '\', "' . implode('", "', $arglist) . '") . "';
375 // Replace the fully-parsed string back into the template
376 $template = substr_replace($template, $function_wrap, $location_start, $location_end +
strlen($tag_end) - $location_start);
385 * Parser for in-line template conditionals
387 * @param str Template data awaiting processing
389 * @return str Parsed template data
391 function _parse_conditionals($template)
396 $tag_start = '<if condition=\"';
397 $tag_start_end = '\">';
398 $tag_else = '<else />';
404 for ($i = 0; $i < strlen($template); $i++
)
406 // we've found ourselves a conditional!
407 if (substr($template, $i, strlen($tag_start)) == $tag_start)
409 // where do we end the conditional
410 $end = strpos($template, $tag_start_end, $i);
411 $end +
= strlen($tag_start_end);
414 array_push($stack, $i);
416 //echo "OPEN `$i`: " . print_r($stack, true);
418 // do we have an end tag?
419 else if (substr($template, $i, strlen($tag_end)) == $tag_end)
421 // calculate the position of the end tag
422 $end = $i +
strlen($tag_end);
424 // find the most recently opened condition
425 $last = array_pop($stack);
427 // how deep are we nested?
428 $depth = count($stack);
430 //echo "CLOSE `$last`: " . print_r($stack, true);
432 // get the full conditional
433 $fullspread = substr($template, $last, $end - $last);
435 // remove the beginning tag
436 $conditional = substr($fullspread, strlen($tag_start));
438 // find the end of the expression
439 $temp_end = strpos($conditional, $tag_start_end);
441 // save the expression
442 $expression = stripslashes(substr($conditional, 0, $temp_end));
444 // remove the expression from the broken conditional
445 $conditional = substr($conditional, strlen($expression) +
strlen($tag_start_end));
447 // remove the tailing end tag
448 $conditional = substr($conditional, 0, strlen($conditional) - strlen($tag_end));
450 // save all the data for later so we can do a quick replacement
451 $parsed["$depth"]["$last"] = array(
452 'raw' => $fullspread,
453 'parsed' => '" . iff(' . stripslashes($expression) . ',"' . $conditional . '") . "',
458 // sort so the top-most ones are done first
460 foreach ($parsed AS $depth => $conditionals)
462 // sort so we do them in order
463 ksort($conditionals);
464 foreach ($conditionals AS $parsed)
466 // replace the old with the new
467 $template = str_replace($parsed['raw'], $parsed['parsed'], $template);
470 // hackish way to get end tags working
471 $template = str_replace($tag_else, '","', $template);
477 /*=====================================================================*\
478 || ###################################################################
481 || ###################################################################
482 \*=====================================================================*/