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 = '$this->registry->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
;
112 function Template(&$registry)
114 $this->registry
=& $registry;
118 * Takes an array of template names, loads them, and then stores
119 * a parsed version for optimum speed.
121 * @param array List of template names to be cached
123 function cache($namearray)
125 if (sizeof($this->cache
) > 0)
127 trigger_error('You cannot cache templates more than once per initialization', E_USER_WARNING
);
131 $templates = $this->registry
->modules
['db_mysql']->query("SELECT * FROM " . $this->tablename
. " WHERE " . $this->namecolumn
. " IN ('" . implode("', '", $namearray) . "')" . ($this->extrawhere ?
$this->extrawhere
: ''));
132 while ($template = $this->registry
->modules
['db_mysql']->fetch_array($templates))
134 $this->cache
[ $template[ $this->namecolumn
] ] = $this->_parse($template[ $this->datacolumn
]);
135 $this->usage
["$name"] = 0;
141 * Loads a template from the cache or the _load function and
142 * stores the parsed version of it
144 * @param string The name of the template
146 * @return string A parsed and loaded template
148 function fetch($name)
150 if (isset($this->cache
["$name"]))
152 $template = $this->cache
["$name"];
156 $this->uncached
[] = $name;
157 $this->registry
->debug("Manually loading template `$name`");
158 $template = $this->_load($name);
159 $template = $this->_parse($template);
162 if (!isset($this->usage
["$name"]))
164 $this->usage
["$name"] = 0;
167 $this->usage
["$name"]++
;
173 * Output a template fully compiled to the browser
175 * @param string Compiled and ready template
177 function flush($template)
181 if (empty($template))
183 trigger_error('There was no output to print', E_USER_ERROR
);
187 if ($this->registry
->debug
AND isset($_GET['query']))
189 if (is_array($this->registry
->modules
['db_mysql']->history
))
192 foreach ($this->registry
->modules
['db_mysql']->history
AS $query)
194 echo $query . "\n\n<hr />\n\n";
201 if ($this->doneflush
)
203 trigger_error('A template has already been sent to the output buffer', E_USER_ERROR
);
207 if ($this->registry
->debug
)
215 foreach ($this->usage
AS $name => $count)
217 if (in_array($name, $this->uncached
))
219 $optlist[] = $name . '[' . $count . ']';
221 $usage[] = $name . " ($count)";
224 $sizeof = sizeof($this->uncached
);
227 $debug .= "\n\t<li><strong style=\"color: red\">Uncached Template(s):</strong> $sizeof ( " . implode(' ', $optlist) . " )</li>";
231 $scinfo = 'Not Under Source Control';
232 $possiblescms = array('cvs', 'svn', 'cvs_information', 'svn_information', 'scm', 'sc_information', 'scm_information');
233 foreach ($possiblescms AS $scm)
235 if (defined(strtoupper($scm)))
237 $scinfo = constant(strtoupper($scm));
241 if (strpos($scinfo, 'RCSfile:') !== false
)
245 else if (strpos($scinfo, ',v ') !== false
)
250 else if (strpos($scinfo, 'URL:') !== false
)
254 else if (strpos($scinfo, 'https://') !== false
OR strpos($scinfo, 'http://') !== false
)
258 // not found so just return it
259 // try a SVN ID tag as we can't really tell if we're using it
262 $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);
263 if ($test == '$' . 'Id: $')
265 $scinfo = 'Not Under Source Control';
276 $scinfo = preg_replace('#\$' . 'RCSfile: (.+?) \$#', '\\1', $scinfo);
277 $scinfo = preg_replace('#\$' . 'Revision: (.+?) \$#', 'CVS \\1', $scinfo);
278 $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);
280 else if ($type == 'svn')
282 $scinfo = preg_replace('#\$' . '(Head)?URL: (.+?) \$#e', "end(explode('/', '\\2'))", $scinfo);
283 $scinfo = preg_replace('#\$' . '(LastModified)?Revision: (.+?) \$#', 'SVN \\2', $scinfo);
284 $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);
288 $scinfo = 'Not Under Source Control';
293 $scinfo = trim($scinfo);
294 $debug .= "\n\t<li><strong>Source Control:</strong> $scinfo</li>";
297 if (is_object($this->registry
->modules
['db_mysql']))
299 $debug .= "\n\t<li><strong>Total Queries:</strong> " . sizeof($this->registry
->modules
['db_mysql']->history
) . " (<a href=\"" . $this->registry
->sanitize($_SERVER['REQUEST_URI']) . ((strpos($_SERVER['REQUEST_URI'], '?') !== false
) ?
'&query=1' : '?query=1') . "\">?</a>)</li>";
302 // total execution time
303 if (defined('ISSO_MT_START'))
305 $this->registry
->load('functions', 'functions');
306 $debug .= "\n\t<li><strong>Total Execution Time:</strong> " . round($this->registry
->modules
['functions']->fetch_microtime_diff(ISSO_MT_START
), 10) . "</li>";
310 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Debug Notices (" . sizeof($this->registry
->debuginfo
) . ")</option>";
311 foreach ((array)$this->registry
->debuginfo
AS $msg)
313 $debug .= "\n\t\t\t<option>--- $msg</option>";
315 $debug .= "\n\t\t</select>\n\t</li>";
318 $modules = $this->registry
->show_modules(true
);
319 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Loaded Modules (" . sizeof($modules) . ")</option>";
320 foreach ($modules AS $mod)
322 $debug .= "\n\t\t\t<option>--- $mod</option>";
324 $debug .= "\n\t\t</select>\n\t</li>";
327 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Template Usage (" . array_sum($this->usage
) . ")</option>";
328 foreach ($usage AS $tpl)
330 $debug .= "\n\t\t\t<option>--- $tpl</option>";
332 $debug .= "\n\t\t</select>\n\t</li>";
337 $debug = "\n<hr />\n" . $this->registry
->_message('Debug Information', $debug, 1, true
);
338 $template = str_replace('</body>', "\n\n<!-- dev debug -->\n<div align=\"center\">\n$debug\n</div>\n<!-- / dev debug -->\n\n</body>", $template);
341 print(stripslashes($template));
345 * Loads an additional template from the database
347 * @param string The name of the template
349 * @return string Template data from the database
351 function _load($name)
353 if ($template = $this->registry
->modules
['db_mysql']->query("SELECT * FROM " . $this->tablename
. " WHERE " . $this->namecolumn
. " = '$name'" . ($this->extrawhere ?
$this->extrawhere
: '')))
355 return $template[ $this->datacolumn
];
359 trigger_error("The template '$name' could not be loaded", E_USER_ERROR
);
365 * A wrapper for all the parsing functions and compiling functins
367 * @param string Unparsed template data
369 * @return string Parsed template data
371 function _parse($template)
373 $template = stripslashes($template);
374 $template = str_replace('"', '\"', $template);
375 $template = $this->_parse_phrases($template);
376 $template = $this->_parse_conditionals($template);
381 * Prepares language and locale information inside templates
383 * @param string Template data to be processed
385 * @return string Language-ready template data
387 function _parse_phrases($template)
389 $tag_start = '<lang ';
390 $tag_start_end = '\">';
391 $tag_end = '</lang>';
393 $location_start = -1;
396 // Process the empty phrase objects -- do this now so we don't have to worry about it when we're parsing later
397 $template = preg_replace('#\{@\\\"(.*?)\\\"\}#i', '" . ' . $this->langcall
. '(\'$1\') . "', $template);
401 // Find the start language object tag
402 $location_start = strpos($template, $tag_start, $location_end +
1);
403 if ($location_start === false
)
409 $location_end = strpos($template, $tag_end, $location_end +
strlen($tag_end));
410 if ($location_end === false
)
415 // Extract the language object
416 $phrase_bunch = substr($template, $location_start, ($location_end +
strlen($tag_end)) - $location_start);
418 // Find the close to the opening <lang>
419 $close_of_open = strpos($phrase_bunch, $tag_start_end);
420 if ($close_of_open === false
)
425 // Extract the opening tag so it can be parsed
426 $init_tag = substr($phrase_bunch, 0, ($close_of_open +
strlen($tag_start_end)));
427 $init_tag = str_replace($tag_start, '', $init_tag);
428 $init_tag = substr($init_tag, 0, strlen($init_tag) - 1);
430 // Get the args out of the tag
431 $args = preg_split('#([0-9].*?)=#', $init_tag);
432 foreach ($args AS $arg)
434 if ($arg AND $arg != ' ')
437 $arg = substr($arg, 2);
438 $arg = substr($arg, 0, strlen($arg) - 2);
443 // Just get the phrase name
444 $phrase_name = preg_replace('#<lang(.*?)>(.*?)</lang>#i', '$2', $phrase_bunch);
446 // Wrap the parsed data into the build function
447 $function_wrap = '" . ' . $this->langconst
. '("' . $phrase_name . '", "' . implode('", "', $arglist) . '") . "';
449 // Replace the fully-parsed string back into the template
450 $template = substr_replace($template, $function_wrap, $location_start, $location_end +
strlen($tag_end) - $location_start);
459 * Parser for in-line template conditionals
461 * @param string Template data awaiting processing
463 * @return string Parsed template data
465 function _parse_conditionals($template)
468 $tag_start = '<if condition=\"';
469 $tag_start_end = '\">';
470 $tag_else = '<else />';
476 // the information about the current active tag
485 if (strpos($template, $tag_start) === false
)
490 for ($i = $offset; $i < strlen($template); $i++
)
492 // we've found ourselves a conditional!
493 if (substr($template, $i, strlen($tag_start)) == $tag_start)
495 // push the position into the tag stack
498 array_push($stack, $i);
502 $tag_full['posi'] = $i;
506 else if (substr($template, $i, strlen($tag_else)) == $tag_else)
508 if (count($stack) == 0 AND !isset($tag_full['else']))
510 $tag_full['else'] = $i;
513 // do we have an end tag?
514 else if (substr($template, $i, strlen($tag_end)) == $tag_end)
516 if (count($stack) != 0)
522 // calculate the position of the end tag
523 $tag_full['posf'] = $i +
strlen($tag_end) - 1;
525 // extract the entire conditional from the template
526 $fullspread = substr($template, $tag_full['posi'], $tag_full['posf'] - $tag_full['posi'] +
1);
528 // remove the beginning tag
529 $conditional = substr($fullspread, strlen($tag_start));
531 // find the end of the expression
532 $temp_end = strpos($conditional, $tag_start_end);
534 // save the expression
535 $parsed[0] = stripslashes(substr($conditional, 0, $temp_end));
537 // remove the expression from the conditional
538 $conditional = substr($conditional, strlen($parsed[0]) +
strlen($tag_start_end));
540 // remove the tailing end tag
541 $conditional = substr($conditional, 0, strlen($conditional) - strlen($tag_end));
544 if (isset($tag_full['else']))
546 // now relative to the start of the <if>
547 $relpos = $tag_full['else'] - $tag_full['posi'];
549 // calculate the length of the expression and opening tag
550 $length = strlen($parsed[0]) +
strlen($tag_start) +
strlen($tag_start_end);
552 // relative to the start of iftrue
553 $elsepos = $relpos - $length;
555 $parsed[1] = substr($conditional, 0, $elsepos);
556 $parsed[2] = substr($conditional, $elsepos +
strlen($tag_else));
561 $parsed[1] = $conditional;
566 // final parsed output
567 $parsed = '" . ((' . stripslashes($parsed[0]) . ') ? "' . $parsed[1] . '" : "' . $parsed[2] . '") . "';
569 // replace the conditional
570 $template = str_replace($fullspread, $parsed, $template);
573 $offset = $tag_full['posi'] +
strlen($tag_start) +
strlen($tag_start_end);
577 unset($fullspread, $conditional, $temp_end, $relpos, $length, $elsepos);
588 * Debugging function used to print characters
589 * in a string that are around a certain position.
591 * @param string The haystack string
592 * @param integer Position to print around
594 function print_around($str, $pos)
596 echo '>>> PA >>>>>>>>[';
597 echo htmlspecialchars($str[ $pos - 5 ]);
598 echo htmlspecialchars($str[ $pos - 4 ]);
599 echo htmlspecialchars($str[ $pos - 3 ]);
600 echo htmlspecialchars($str[ $pos - 2 ]);
601 echo htmlspecialchars($str[ $pos - 1 ]);
603 echo htmlspecialchars($str[ $pos +
0 ]);
604 echo htmlspecialchars($str[ $pos +
1 ]);
605 echo htmlspecialchars($str[ $pos +
2 ]);
606 echo htmlspecialchars($str[ $pos +
3 ]);
607 echo htmlspecialchars($str[ $pos +
4 ]);
608 echo htmlspecialchars($str[ $pos +
5 ]);
609 echo ']<<<<<<<< PA <<<';
612 /*=====================================================================*\
613 || ###################################################################
616 || ###################################################################
617 \*=====================================================================*/