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 if ($this->registry
->debug
)
234 foreach ($this->usage
AS $name => $count)
236 if (in_array($name, $this->uncached
))
238 $optlist[] = $name . '[' . $count . ']';
240 $usage[] = $name . " ($count)";
243 $sizeof = sizeof($this->uncached
);
246 $debug .= "\n\t<li><strong style=\"color: red\">Uncached Template(s):</strong> $sizeof ( " . implode(' ', $optlist) . " )</li>";
250 $scinfo = 'Not Under Source Control';
251 $possiblescms = array('cvs', 'svn', 'cvs_information', 'svn_information', 'scm', 'sc_information', 'scm_information');
252 foreach ($possiblescms AS $scm)
254 if (defined(strtoupper($scm)))
256 $scinfo = constant(strtoupper($scm));
260 if (strpos($scinfo, 'RCSfile:') !== false)
264 else if (strpos($scinfo, ',v ') !== false)
269 else if (strpos($scinfo, 'URL:') !== false)
273 else if (strpos($scinfo, 'https://') !== false OR strpos($scinfo, 'http://') !== false)
277 // not found so just return it
278 // try a SVN ID tag as we can't really tell if we're using it
281 $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);
282 if ($test == '$' . 'Id
: $')
284 $scinfo = 'Not Under Source Control';
295 $scinfo = preg_replace('#\$' . 'RCSfile: (.+?) \$#', '\\1', $scinfo);
296 $scinfo = preg_replace('#\$' . 'Revision: (.+?) \$#', 'CVS \\1', $scinfo);
297 $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);
299 else if ($type == 'svn')
301 $scinfo = preg_replace('#\$' . '(Head)?URL: (.+?) \$#e', "end(explode('/', '\\2'))", $scinfo);
302 $scinfo = preg_replace('#\$' . '(LastModified)?Revision: (.+?) \$#', 'SVN \\2', $scinfo);
303 $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);
307 $scinfo = 'Not Under Source Control';
312 $scinfo = trim($scinfo);
313 $debug .= "\n\t<li><strong>Source Control:</strong> $scinfo</li>";
316 if (is_object($this->registry
->modules
['db_mysql']))
318 $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>";
321 // total execution time
322 if (defined('ISSO_MT_START'))
324 $this->registry
->load('functions', 'functions');
325 $debug .= "\n\t<li><strong>Total Execution Time:</strong> " . round($this->registry
->modules
['functions']->fetch_microtime_diff(ISSO_MT_START
), 10) . "</li>";
329 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Debug Notices (" . sizeof($this->registry
->debuginfo
) . ")</option>";
330 foreach ((array)$this->registry
->debuginfo
AS $msg)
332 $debug .= "\n\t\t\t<option>--- $msg</option>";
334 $debug .= "\n\t\t</select>\n\t</li>";
337 $modules = $this->registry
->show_modules(true);
338 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Loaded Modules (" . sizeof($modules) . ")</option>";
339 foreach ($modules AS $mod)
341 $debug .= "\n\t\t\t<option>--- $mod</option>";
343 $debug .= "\n\t\t</select>\n\t</li>";
346 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Template Usage (" . array_sum($this->usage
) . ")</option>";
347 foreach ($usage AS $tpl)
349 $debug .= "\n\t\t\t<option>--- $tpl</option>";
351 $debug .= "\n\t\t</select>\n\t</li>";
356 $debug = "\n<hr />\n" . $this->registry
->message('Debug Information', $debug, 1, true, false);
357 $template = str_replace('</body>', "\n\n<!-- dev debug -->\n<div align=\"center\">\n$debug\n</div>\n<!-- / dev debug -->\n\n</body>", $template);
363 // ###################################################################
365 * Loads an additional template from the database
369 * @param string The name of the template
371 * @return string Template data from the database
373 function _load($name)
375 if ($template = $this->registry
->modules
['db_mysql']->query("SELECT * FROM " . $this->tablename
. " WHERE " . $this->namecolumn
. " = '$name'" . ($this->extrawhere ? $this->extrawhere : '')))
377 return $template[ $this->datacolumn ];
381 trigger_error("The template
'$name' could not be loaded", E_USER_ERROR);
386 // ###################################################################
388 * A wrapper for all the parsing functions and compiling functins
392 * @param string Unparsed template data
394 * @return string Parsed template data
396 function _parse($template)
398 $template = str_replace('"', '\"', $template);
399 $template = $this->_parse_phrases($template);
400 $template = $this->_parse_conditionals($template);
404 // ###################################################################
406 * Prepares language and locale information inside templates
410 * @param string Template data to be processed
412 * @return string Language-ready template data
414 function _parse_phrases($template)
416 $tag_start = '<lang ';
417 $tag_start_end = '\">';
418 $tag_end = '</lang>';
420 $location_start = -1;
423 // Process the empty phrase objects -- do this now so we don't have to worry about it when we're parsing later
424 $template = preg_replace('#\{@\\\"(.*?)\\\"\}#ie', '$this->_phrase_string(\'$1\')', $template);
428 // Find the start language object tag
429 $location_start = strpos($template, $tag_start, $location_end + 1);
430 if ($location_start === false)
436 $location_end = strpos($template, $tag_end, $location_end + strlen($tag_end));
437 if ($location_end === false)
442 // Extract the language object
443 $phrase_bunch = substr($template, $location_start, ($location_end + strlen($tag_end)) - $location_start);
445 // Find the close to the opening <lang>
446 $close_of_open = strpos($phrase_bunch, $tag_start_end);
447 if ($close_of_open === false)
452 // Extract the opening tag so it can be parsed
453 $init_tag = substr($phrase_bunch, 0, ($close_of_open + strlen($tag_start_end)));
454 $init_tag = str_replace($tag_start, '', $init_tag);
455 $init_tag = substr($init_tag, 0, strlen($init_tag) - 1);
457 // Get the args out of the tag
458 $args = preg_split('#([0-9].*?)=#', $init_tag);
459 foreach ($args AS $arg)
461 if ($arg AND $arg != ' ')
464 $arg = substr($arg, 2);
465 $arg = substr($arg, 0, strlen($arg) - 2);
470 // Just get the phrase name
471 $phrase_name = preg_replace('#<lang(.*?)>(.*?)</lang>#i', '$2', $phrase_bunch);
473 // Wrap the parsed data into the build function
474 $function_wrap = '" . ' . $this->langconst . '("' . /*str_replace(array('\"', "'"), array('"', "\'
"),*/ $phrase_name/*)*/ . '", "' . implode('", "', $arglist) . '") . "';
476 // Replace the fully-parsed string back into the template
477 $template = substr_replace($template, $function_wrap, $location_start, $location_end + strlen($tag_end) - $location_start);
485 // ###################################################################
487 * Turns a localized phrase tag into a function call
491 * @param string Phrase text
493 * @return string Function call for phrase text
495 function _phrase_string($text)
497 return '" . ' . $this->langcall . '(\'' . str_replace(array('\\\"
', "'"), array('"', "\'"), $text) . '\'
) . "';
500 // ###################################################################
502 * Parser for in-line template conditionals
506 * @param string Template data awaiting processing
508 * @return string Parsed template data
510 function _parse_conditionals($template)
513 $tag_start = '<if condition=\"';
514 $tag_start_end = '\">';
515 $tag_else = '<else />';
521 // the information about the current active tag
530 if (strpos($template, $tag_start) === false)
535 for ($i = $offset; $i < strlen($template); $i++)
537 // we've found ourselves a conditional!
538 if (substr($template, $i, strlen($tag_start)) == $tag_start)
540 // push the position into the tag stack
543 array_push($stack, $i);
547 $tag_full['posi'] = $i;
551 else if (substr($template, $i, strlen($tag_else)) == $tag_else)
553 if (count($stack) == 0 AND !isset($tag_full['else']))
555 $tag_full['else'] = $i;
558 // do we have an end tag?
559 else if (substr($template, $i, strlen($tag_end)) == $tag_end)
561 if (count($stack) != 0)
567 // calculate the position of the end tag
568 $tag_full['posf'] = $i + strlen($tag_end) - 1;
570 // extract the entire conditional from the template
571 $fullspread = substr($template, $tag_full['posi'], $tag_full['posf'] - $tag_full['posi'] + 1);
573 // remove the beginning tag
574 $conditional = substr($fullspread, strlen($tag_start));
576 // find the end of the expression
577 $temp_end = strpos($conditional, $tag_start_end);
579 // save the expression
580 $parsed[0] = stripslashes(substr($conditional, 0, $temp_end));
582 // remove the expression from the conditional
583 $conditional = substr($conditional, strlen($parsed[0]) + strlen($tag_start_end));
585 // remove the tailing end tag
586 $conditional = substr($conditional, 0, strlen($conditional) - strlen($tag_end));
589 if (isset($tag_full['else']))
591 // now relative to the start of the <if>
592 $relpos = $tag_full['else'] - $tag_full['posi'];
594 // calculate the length of the expression and opening tag
595 $length = strlen($parsed[0]) + strlen($tag_start) + strlen($tag_start_end);
597 // relative to the start of iftrue
598 $elsepos = $relpos - $length;
600 $parsed[1] = substr($conditional, 0, $elsepos);
601 $parsed[2] = substr($conditional, $elsepos + strlen($tag_else));
606 $parsed[1] = $conditional;
611 // final parsed output
612 $parsed = '" . ((' . stripslashes($parsed[0]) . ') ? "' . $parsed[1] . '" : "' . $parsed[2] . '") . "';
614 // replace the conditional
615 $template = str_replace($fullspread, $parsed, $template);
618 $offset = $tag_full['posi'] + strlen($tag_start) + strlen($tag_start_end);
622 unset($fullspread, $conditional, $temp_end, $relpos, $length, $elsepos);
632 // ###################################################################
634 * Debugging function used to print characters in a string that are
635 * around a certain position.
639 * @param string The haystack string
640 * @param integer Position to print around
642 function print_around($str, $pos)
644 echo '>>> PA >>>>>>>>[';
645 echo htmlspecialchars($str[ $pos - 5 ]);
646 echo htmlspecialchars($str[ $pos - 4 ]);
647 echo htmlspecialchars($str[ $pos - 3 ]);
648 echo htmlspecialchars($str[ $pos - 2 ]);
649 echo htmlspecialchars($str[ $pos - 1 ]);
651 echo htmlspecialchars($str[ $pos + 0 ]);
652 echo htmlspecialchars($str[ $pos + 1 ]);
653 echo htmlspecialchars($str[ $pos + 2 ]);
654 echo htmlspecialchars($str[ $pos + 3 ]);
655 echo htmlspecialchars($str[ $pos + 4 ]);
656 echo htmlspecialchars($str[ $pos + 5 ]);
657 echo ']<<<<<<<< PA <<<';
660 /*=====================================================================*\
661 || ###################################################################
664 || ###################################################################
665 \*=====================================================================*/