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 * Name of the database table templates are in
52 var $templatetable = '';
55 * Name of the table column template names are in
57 * @see $templatetable, _load()
62 * Name of the table column templates are in
64 * @see templatetable, _load()
69 * Additional WHERE clauses for the template fetch SQL
71 * @see _load(), cache()
76 * The name of the function phrases are fetched with
78 * @see _parse_phrases()
80 var $langcall = '$GLOBALS[\'_isso\']->lang->string';
83 * The name of the function phrases are sprintf() parsed with
85 * @see _parse_phrases()
87 var $langconst = 'sprintf';
90 * Array of pre-compiled templates that are stored to decrease server load
97 * A list of the number of times each template has been used
101 var $usage = array();
104 * A list of templates that weren't cached, but are still used
108 var $uncached = array();
111 * Whether or not the page has been flush()'d already
115 var $doneflush = false
;
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)
127 if (sizeof($this->cache
) > 0)
129 trigger_error('You cannot cache templates more than once per initialization', ERR_WARNING
);
133 $templates = $_isso->db
->query("SELECT * FROM " . $this->tablename
. " WHERE " . $this->namecolumn
. " IN ('" . implode("', '", $namearray) . "')" . (($this->extrawhere
) ?
$this->extrawhere
: ''));
134 while ($template = $_isso->db
->fetch_array($templates))
136 $template = $this->_parse($template);
137 $this->cache
[ $template[ $this->namecolumn
] ] = $template[ $this->datacolumn
];
138 $this->usage
["$name"] = 0;
144 * Loads a template from the cache or the _load function and
145 * stores the parsed version of it
147 * @param str The name of the template
149 * @return str A parsed and loaded template
151 function fetch($name)
155 if (isset($this->cache
["$name"]))
157 $template = $this->cache
["$name"];
161 $this->uncached
[] = $name;
162 $_isso->debug("Manually loading template `$name`");
163 $template = $this->_load($name);
164 $template = $this->_parse($template);
167 if (!isset($this->usage
["$name"]))
169 $this->usage
["$name"] = 0;
172 $this->usage
["$name"]++
;
178 * Output a template fully compiled to the browser
180 * @param str Compiled and ready template
182 function flush($template)
188 if (empty($template))
190 trigger_error('There was no output to print', E_USER_ERROR
);
194 if ($_isso->debug
AND isset($_GET['query']))
196 if (is_array($_isso->db
->history
))
199 foreach ($_isso->db
->history
AS $query)
201 echo $query . "\n\n<hr />\n\n";
208 if ($this->doneflush
)
210 trigger_error('A template has already been sent to the output buffer', E_USER_ERROR
);
222 foreach ($this->usage
AS $name => $count)
224 if (in_array($name, $this->uncached
))
226 $optlist[] = $name . '[' . $count . ']';
228 $usage[] = $name . " ($count)";
231 $sizeof = sizeof($this->uncached
);
234 $debug .= "\n\t<li><strong style=\"color: red\">Uncached Template(s):</strong> $sizeof ( " . implode(' ', $optlist) . " )</li>";
238 $scinfo = 'Not Under Source Control';
239 $possiblescms = array('cvs', 'svn', 'cvs_information', 'svn_information', 'scm', 'sc_information', 'scm_information');
240 foreach ($possiblescms AS $scm)
242 if (defined(strtoupper($scm)))
244 $scinfo = constant(strtoupper($scm));
248 if (strpos($scinfo, 'RCSfile:') !== false
)
252 else if (strpos($scinfo, ',v ') !== false
)
257 else if (strpos($scinfo, 'URL:') !== false
)
261 else if (strpos($scinfo, 'https://') !== false
OR strpos($scinfo, 'http://') !== false
)
265 // not found so just return it
266 // try a SVN ID tag as we can't really tell if we're using it
269 $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);
270 if ($test == '$' . 'Id: $')
272 $scinfo = 'Not Under Source Control';
283 $scinfo = preg_replace('#\$' . 'RCSfile: (.+?) \$#', '\\1', $scinfo);
284 $scinfo = preg_replace('#\$' . 'Revision: (.+?) \$#', 'CVS \\1', $scinfo);
285 $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);
287 else if ($type == 'svn')
289 $scinfo = preg_replace('#\$' . '(Head)?URL: (.+?) \$#e', "end(explode('/', '\\2'))", $scinfo);
290 $scinfo = preg_replace('#\$' . '(LastModified)?Revision: (.+?) \$#', 'SVN \\2', $scinfo);
291 $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);
295 $scinfo = 'Not Under Source Control';
300 $scinfo = trim($scinfo);
301 $debug .= "\n\t<li><strong>Source Control:</strong> $scinfo</li>";
304 if (isset($_isso->db
) AND is_object($_isso->db
))
306 $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>";
309 // total execution time
310 if (defined('ISSO_MT_START'))
312 $_isso->load('functions');
313 $debug .= "\n\t<li><strong>Total Execution Time:</strong> " . round($_isso->funct
->fetch_microtime_diff(ISSO_MT_START
), 10) . "</li>";
317 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Debug Notices (" . sizeof($_isso->debuginfo
) . ")</option>";
318 foreach ((array)$_isso->debuginfo
AS $msg)
320 $debug .= "\n\t\t\t<option>--- $msg</option>";
322 $debug .= "\n\t\t</select>\n\t</li>";
325 $modules = $_isso->show_modules(true
);
326 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Loaded Modules (" . sizeof($modules) . ")</option>";
327 foreach ($modules AS $mod)
329 $debug .= "\n\t\t\t<option>--- $mod</option>";
331 $debug .= "\n\t\t</select>\n\t</li>";
334 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Template Usage (" . array_sum($this->usage
) . ")</option>";
335 foreach ($usage AS $tpl)
337 $debug .= "\n\t\t\t<option>--- $tpl</option>";
339 $debug .= "\n\t\t</select>\n\t</li>";
344 $debug = "\n<hr />\n" . $_isso->_message('Debug Information', $debug, 1, true
);
345 $template = str_replace('</body>', "\n\n<!-- dev debug -->\n<div align=\"center\">\n$debug\n</div>\n<!-- / dev debug -->\n\n</body>", $template);
348 print(stripslashes($template));
352 * Loads an additional template from the database
354 * @param str The name of the template
356 * @return str Template data from the database
358 function _load($name)
361 if ($template = $_isso->db
->query("SELECT * FROM " . $this->tablename
. " WHERE " . $this->namecolumn
. " = '$name'" . (($this->extrawhere
) ?
$this->extrawhere
: '')))
363 return $template[ $this->datacolumn
];
367 trigger_error("The template '$name' could not be loaded", E_USER_ERROR
);
373 * A wrapper for all the parsing functions and compiling functins
375 * @param str Unparsed template data
377 * @return str Parsed template data
379 function _parse($template)
381 $template = stripslashes($template);
382 $template = str_replace('"', '\"', $template);
383 $template = $this->_parse_phrases($template);
384 $template = $this->_parse_conditionals($template);
389 * Prepares language and locale information inside templates
391 * @param str Template data to be processed
393 * @return str Language-ready template data
395 function _parse_phrases($template)
397 $tag_start = '<lang ';
398 $tag_start_end = '\">';
399 $tag_end = '</lang>';
401 $location_start = -1;
404 // Process the empty phrase objects -- do this now so we don't have to worry about it when we're parsing later
405 $template = preg_replace('#\{@\\\"(.*?)\\\"\}#i', '" . ' . $this->langcall
. '(\'$1\') . "', $template);
409 // Find the start language object tag
410 $location_start = strpos($template, $tag_start, $location_end +
1);
411 if ($location_start === false
)
417 $location_end = strpos($template, $tag_end, $location_end +
strlen($tag_end));
418 if ($location_end === false
)
423 // Extract the language object
424 $phrase_bunch = substr($template, $location_start, ($location_end +
strlen($tag_end)) - $location_start);
426 // Find the close to the opening <lang>
427 $close_of_open = strpos($phrase_bunch, $tag_start_end);
428 if ($close_of_open === false
)
433 // Extract the opening tag so it can be parsed
434 $init_tag = substr($phrase_bunch, 0, ($close_of_open +
strlen($tag_start_end)));
435 $init_tag = str_replace($tag_start, '', $init_tag);
436 $init_tag = substr($init_tag, 0, strlen($init_tag) - 1);
438 // Get the args out of the tag
439 $args = preg_split('#([0-9].*?)=#', $init_tag);
440 foreach ($args AS $arg)
442 if ($arg AND $arg != ' ')
445 $arg = substr($arg, 2);
446 $arg = substr($arg, 0, strlen($arg) - 2);
451 // Just get the phrase name
452 $phrase_name = preg_replace('#<lang(.*?)>(.*?)</lang>#i', '$2', $phrase_bunch);
454 // Wrap the parsed data into the build function
455 $function_wrap = '" . ' . $this->langconst
. '("' . $phrase_name . '", "' . implode('", "', $arglist) . '") . "';
457 // Replace the fully-parsed string back into the template
458 $template = substr_replace($template, $function_wrap, $location_start, $location_end +
strlen($tag_end) - $location_start);
467 * Parser for in-line template conditionals
469 * @param str Template data awaiting processing
471 * @return str Parsed template data
473 function _parse_conditionals($template)
478 $tag_start = '<if condition=\"';
479 $tag_start_end = '\">';
480 $tag_else = '<else />';
486 // the information about the current active tag
495 if (strpos($template, $tag_start) === false
)
500 for ($i = $offset; $i < strlen($template); $i++
)
502 // we've found ourselves a conditional!
503 if (substr($template, $i, strlen($tag_start)) == $tag_start)
505 // push the position into the tag stack
508 array_push($stack, $i);
512 $tag_full['posi'] = $i;
516 else if (substr($template, $i, strlen($tag_else)) == $tag_else)
518 if (count($stack) == 0 AND !isset($tag_full['else']))
520 $tag_full['else'] = $i;
523 // do we have an end tag?
524 else if (substr($template, $i, strlen($tag_end)) == $tag_end)
526 if (count($stack) != 0)
532 // calculate the position of the end tag
533 $tag_full['posf'] = $i +
strlen($tag_end) - 1;
535 // extract the entire conditional from the template
536 $fullspread = substr($template, $tag_full['posi'], $tag_full['posf'] - $tag_full['posi'] +
1);
538 // remove the beginning tag
539 $conditional = substr($fullspread, strlen($tag_start));
541 // find the end of the expression
542 $temp_end = strpos($conditional, $tag_start_end);
544 // save the expression
545 $parsed[0] = stripslashes(substr($conditional, 0, $temp_end));
547 // remove the expression from the conditional
548 $conditional = substr($conditional, strlen($parsed[0]) +
strlen($tag_start_end));
550 // remove the tailing end tag
551 $conditional = substr($conditional, 0, strlen($conditional) - strlen($tag_end));
554 if (isset($tag_full['else']))
556 // now relative to the start of the <if>
557 $relpos = $tag_full['else'] - $tag_full['posi'];
559 // calculate the length of the expression and opening tag
560 $length = strlen($parsed[0]) +
strlen($tag_start) +
strlen($tag_start_end);
562 // relative to the start of iftrue
563 $elsepos = $relpos - $length;
565 $parsed[1] = substr($conditional, 0, $elsepos);
566 $parsed[2] = substr($conditional, $elsepos +
strlen($tag_else));
571 $parsed[1] = $conditional;
576 // final parsed output
577 $parsed = '" . ((' . stripslashes($parsed[0]) . ') ? "' . $parsed[1] . '" : "' . $parsed[2] . '") . "';
579 // replace the conditional
580 $template = str_replace($fullspread, $parsed, $template);
583 $offset = $tag_full['posi'] +
strlen($tag_start) +
strlen($tag_start_end);
587 unset($fullspread, $conditional, $temp_end, $relpos, $length, $elsepos);
598 * Debugging function used to print characters
599 * in a string that are around a certain position.
601 * @param string The haystack string
602 * @param integer Position to print around
604 function print_around($str, $pos)
606 echo '>>> PA >>>>>>>>[';
607 echo htmlspecialchars($str[ $pos - 5 ]);
608 echo htmlspecialchars($str[ $pos - 4 ]);
609 echo htmlspecialchars($str[ $pos - 3 ]);
610 echo htmlspecialchars($str[ $pos - 2 ]);
611 echo htmlspecialchars($str[ $pos - 1 ]);
613 echo htmlspecialchars($str[ $pos +
0 ]);
614 echo htmlspecialchars($str[ $pos +
1 ]);
615 echo htmlspecialchars($str[ $pos +
2 ]);
616 echo htmlspecialchars($str[ $pos +
3 ]);
617 echo htmlspecialchars($str[ $pos +
4 ]);
618 echo htmlspecialchars($str[ $pos +
5 ]);
619 echo ']<<<<<<<< PA <<<';
622 /*=====================================================================*\
623 || ###################################################################
626 || ###################################################################
627 \*=====================================================================*/