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
29 $OBJECT = 'Database Template System';
30 $CLASS = 'DB_Template';
34 * Database-Driven Template System
36 * This framework is a backend to the database template engine and
37 * contains all the parsing algorithms.
39 * @author Iris Studios, Inc.
40 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
48 * Framework registry object
54 * Name of the database table templates are in
57 var $templatetable = '';
60 * Name of the table column template names are in
66 * Name of the table column templates are in
72 * Additional WHERE clauses for the template fetch SQL
78 * The name of the function phrases are fetched with
81 var $langcall = '$GLOBALS[\'_isso\']->lang->string';
84 * The name of the function phrases are sprintf() parsed with
87 var $langconst = 'sprintf';
90 * Array of pre-compiled templates that are stored to decrease server load
96 * A list of the number of times each template has been used
102 * A list of templates that weren't cached, but are still used
105 var $uncached = array();
108 * Whether or not the page has been flush()'d already
111 var $doneflush = false;
116 function DB_Template($registry)
118 $this->registry
= $registry;
122 * Takes an array of template names, loads them, and then stores
123 * a parsed version for optimum speed.
125 * @param array List of template names to be cached
127 function cache($namearray)
131 if (sizeof($this->cache
) > 0)
133 trigger_error('You cannot cache templates more than once per initialization', ERR_WARNING
);
137 $templates = $_isso->db
->query("SELECT * FROM " . $this->tablename
. " WHERE " . $this->namecolumn
. " IN ('" . implode("', '", $namearray) . "')" . (($this->extrawhere
) ? $this->extrawhere
: ''));
138 while ($template = $_isso->db
->fetch_array($templates))
140 $template = $this->_parse($template);
141 $this->cache
[ $template[ $this->namecolumn
] ] = $template[ $this->datacolumn
];
142 $this->usage
["$name"] = 0;
148 * Loads a template from the cache or the _load function and
149 * stores the parsed version of it
151 * @param string The name of the template
153 * @return string A parsed and loaded template
155 function fetch($name)
159 if (isset($this->cache["$name"]))
161 $template = $this->cache
["$name"];
165 $this->uncached[] = $name;
166 $_isso->debug("Manually loading template `
$name`
");
167 $template = $this->_load($name);
168 $template = $this->_parse($template);
171 if (!isset($this->usage["$name"]))
173 $this->usage
["$name"] = 0;
176 $this->usage["$name"]++
;
182 * Output a template fully compiled to the browser
184 * @param string Compiled and ready template
186 function flush($template)
192 if (empty($template))
194 trigger_error('There was no output to print', E_USER_ERROR
);
198 if ($_isso->debug
AND isset($_GET['query']))
200 if (is_array($_isso->db
->history
))
203 foreach ($_isso->db
->history
AS $query)
205 echo $query . "\n\n<hr />\n\n";
212 if ($this->doneflush
)
214 trigger_error('A template has already been sent to the output buffer', E_USER_ERROR
);
226 foreach ($this->usage
AS $name => $count)
228 if (in_array($name, $this->uncached
))
230 $optlist[] = $name . '[' . $count . ']';
232 $usage[] = $name . " ($count)";
235 $sizeof = sizeof($this->uncached
);
238 $debug .= "\n\t<li><strong style=\"color: red\">Uncached Template(s):</strong> $sizeof ( " . implode(' ', $optlist) . " )</li>";
242 $scinfo = 'Not Under Source Control';
243 $possiblescms = array('cvs', 'svn', 'cvs_information', 'svn_information', 'scm', 'sc_information', 'scm_information');
244 foreach ($possiblescms AS $scm)
246 if (defined(strtoupper($scm)))
248 $scinfo = constant(strtoupper($scm));
252 if (strpos($scinfo, 'RCSfile:') !== false)
256 else if (strpos($scinfo, ',v ') !== false)
261 else if (strpos($scinfo, 'URL:') !== false)
265 else if (strpos($scinfo, 'https://') !== false OR strpos($scinfo, 'http://') !== false)
269 // not found so just return it
270 // try a SVN ID tag as we can't really tell if we're using it
273 $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);
274 if ($test == '$' . 'Id
: $')
276 $scinfo = 'Not Under Source Control';
287 $scinfo = preg_replace('#\$' . 'RCSfile: (.+?) \$#', '\\1', $scinfo);
288 $scinfo = preg_replace('#\$' . 'Revision: (.+?) \$#', 'CVS \\1', $scinfo);
289 $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);
291 else if ($type == 'svn')
293 $scinfo = preg_replace('#\$' . '(Head)?URL: (.+?) \$#e', "end(explode('/', '\\2'))", $scinfo);
294 $scinfo = preg_replace('#\$' . '(LastModified)?Revision: (.+?) \$#', 'SVN \\2', $scinfo);
295 $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);
299 $scinfo = 'Not Under Source Control';
304 $scinfo = trim($scinfo);
305 $debug .= "\n\t<li><strong>Source Control:</strong> $scinfo</li>";
308 if (isset($_isso->db
) AND is_object($_isso->db
))
310 $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>";
313 // total execution time
314 if (defined('ISSO_MT_START'))
316 $_isso->load('functions');
317 $debug .= "\n\t<li><strong>Total Execution Time:</strong> " . round($_isso->funct
->fetch_microtime_diff(ISSO_MT_START
), 10) . "</li>";
321 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Debug Notices (" . sizeof($_isso->debuginfo
) . ")</option>";
322 foreach ((array)$_isso->debuginfo
AS $msg)
324 $debug .= "\n\t\t\t<option>--- $msg</option>";
326 $debug .= "\n\t\t</select>\n\t</li>";
329 $modules = $_isso->show_modules(true);
330 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Loaded Modules (" . sizeof($modules) . ")</option>";
331 foreach ($modules AS $mod)
333 $debug .= "\n\t\t\t<option>--- $mod</option>";
335 $debug .= "\n\t\t</select>\n\t</li>";
338 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Template Usage (" . array_sum($this->usage
) . ")</option>";
339 foreach ($usage AS $tpl)
341 $debug .= "\n\t\t\t<option>--- $tpl</option>";
343 $debug .= "\n\t\t</select>\n\t</li>";
348 $debug = "\n<hr />\n" . $_isso->_message('Debug Information', $debug, 1, true);
349 $template = str_replace('</body>', "\n\n<!-- dev debug -->\n<div align=\"center\">\n$debug\n</div>\n<!-- / dev debug -->\n\n</body>", $template);
352 print(stripslashes($template));
356 * Loads an additional template from the database
358 * @param string The name of the template
360 * @return string Template data from the database
362 function _load($name)
365 if ($template = $_isso->db
->query("SELECT * FROM " . $this->tablename
. " WHERE " . $this->namecolumn
. " = '$name'" . (($this->extrawhere) ? $this->extrawhere : '')))
367 return $template[ $this->datacolumn ];
371 trigger_error("The template
'$name' could not be loaded", E_USER_ERROR);
377 * A wrapper for all the parsing functions and compiling functins
379 * @param string Unparsed template data
381 * @return string Parsed template data
383 function _parse($template)
385 $template = stripslashes($template);
386 $template = str_replace('"', '\"', $template);
387 $template = $this->_parse_phrases($template);
388 $template = $this->_parse_conditionals($template);
393 * Prepares language and locale information inside templates
395 * @param string Template data to be processed
397 * @return string Language-ready template data
399 function _parse_phrases($template)
401 $tag_start = '<lang ';
402 $tag_start_end = '\">';
403 $tag_end = '</lang>';
405 $location_start = -1;
408 // Process the empty phrase objects -- do this now so we don't have to worry about it when we're parsing later
409 $template = preg_replace('#\{@\\\"(.*?)\\\"\}#i', '" . ' . $this->langcall . '(\'$1\') . "', $template);
413 // Find the start language object tag
414 $location_start = strpos($template, $tag_start, $location_end + 1);
415 if ($location_start === false)
421 $location_end = strpos($template, $tag_end, $location_end + strlen($tag_end));
422 if ($location_end === false)
427 // Extract the language object
428 $phrase_bunch = substr($template, $location_start, ($location_end + strlen($tag_end)) - $location_start);
430 // Find the close to the opening <lang>
431 $close_of_open = strpos($phrase_bunch, $tag_start_end);
432 if ($close_of_open === false)
437 // Extract the opening tag so it can be parsed
438 $init_tag = substr($phrase_bunch, 0, ($close_of_open + strlen($tag_start_end)));
439 $init_tag = str_replace($tag_start, '', $init_tag);
440 $init_tag = substr($init_tag, 0, strlen($init_tag) - 1);
442 // Get the args out of the tag
443 $args = preg_split('#([0-9].*?)=#', $init_tag);
444 foreach ($args AS $arg)
446 if ($arg AND $arg != ' ')
449 $arg = substr($arg, 2);
450 $arg = substr($arg, 0, strlen($arg) - 2);
455 // Just get the phrase name
456 $phrase_name = preg_replace('#<lang(.*?)>(.*?)</lang>#i', '$2', $phrase_bunch);
458 // Wrap the parsed data into the build function
459 $function_wrap = '" . ' . $this->langconst . '("' . $phrase_name . '", "' . implode('", "', $arglist) . '") . "';
461 // Replace the fully-parsed string back into the template
462 $template = substr_replace($template, $function_wrap, $location_start, $location_end + strlen($tag_end) - $location_start);
471 * Parser for in-line template conditionals
473 * @param string Template data awaiting processing
475 * @return string Parsed template data
477 function _parse_conditionals($template)
482 $tag_start = '<if condition=\"';
483 $tag_start_end = '\">';
484 $tag_else = '<else />';
490 // the information about the current active tag
499 if (strpos($template, $tag_start) === false)
504 for ($i = $offset; $i < strlen($template); $i++)
506 // we've found ourselves a conditional!
507 if (substr($template, $i, strlen($tag_start)) == $tag_start)
509 // push the position into the tag stack
512 array_push($stack, $i);
516 $tag_full['posi'] = $i;
520 else if (substr($template, $i, strlen($tag_else)) == $tag_else)
522 if (count($stack) == 0 AND !isset($tag_full['else']))
524 $tag_full['else'] = $i;
527 // do we have an end tag?
528 else if (substr($template, $i, strlen($tag_end)) == $tag_end)
530 if (count($stack) != 0)
536 // calculate the position of the end tag
537 $tag_full['posf'] = $i + strlen($tag_end) - 1;
539 // extract the entire conditional from the template
540 $fullspread = substr($template, $tag_full['posi'], $tag_full['posf'] - $tag_full['posi'] + 1);
542 // remove the beginning tag
543 $conditional = substr($fullspread, strlen($tag_start));
545 // find the end of the expression
546 $temp_end = strpos($conditional, $tag_start_end);
548 // save the expression
549 $parsed[0] = stripslashes(substr($conditional, 0, $temp_end));
551 // remove the expression from the conditional
552 $conditional = substr($conditional, strlen($parsed[0]) + strlen($tag_start_end));
554 // remove the tailing end tag
555 $conditional = substr($conditional, 0, strlen($conditional) - strlen($tag_end));
558 if (isset($tag_full['else']))
560 // now relative to the start of the <if>
561 $relpos = $tag_full['else'] - $tag_full['posi'];
563 // calculate the length of the expression and opening tag
564 $length = strlen($parsed[0]) + strlen($tag_start) + strlen($tag_start_end);
566 // relative to the start of iftrue
567 $elsepos = $relpos - $length;
569 $parsed[1] = substr($conditional, 0, $elsepos);
570 $parsed[2] = substr($conditional, $elsepos + strlen($tag_else));
575 $parsed[1] = $conditional;
580 // final parsed output
581 $parsed = '" . ((' . stripslashes($parsed[0]) . ') ? "' . $parsed[1] . '" : "' . $parsed[2] . '") . "';
583 // replace the conditional
584 $template = str_replace($fullspread, $parsed, $template);
587 $offset = $tag_full['posi'] + strlen($tag_start) + strlen($tag_start_end);
591 unset($fullspread, $conditional, $temp_end, $relpos, $length, $elsepos);
602 * Debugging function used to print characters
603 * in a string that are around a certain position.
605 * @param string The haystack string
606 * @param integer Position to print around
608 function print_around($str, $pos)
610 echo '>>> PA >>>>>>>>[';
611 echo htmlspecialchars($str[ $pos - 5 ]);
612 echo htmlspecialchars($str[ $pos - 4 ]);
613 echo htmlspecialchars($str[ $pos - 3 ]);
614 echo htmlspecialchars($str[ $pos - 2 ]);
615 echo htmlspecialchars($str[ $pos - 1 ]);
617 echo htmlspecialchars($str[ $pos + 0 ]);
618 echo htmlspecialchars($str[ $pos + 1 ]);
619 echo htmlspecialchars($str[ $pos + 2 ]);
620 echo htmlspecialchars($str[ $pos + 3 ]);
621 echo htmlspecialchars($str[ $pos + 4 ]);
622 echo htmlspecialchars($str[ $pos + 5 ]);
623 echo ']<<<<<<<< PA <<<';
626 /*=====================================================================*\
627 || ###################################################################
630 || ###################################################################
631 \*=====================================================================*/