2 /*=====================================================================*\
3 || ################################################################### ||
4 || # Renapsus [#]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 \*=====================================================================*/
17 var $uncached = array();
19 var $doneflush = false;
21 // ###################### Start Template::cache ######################
22 function cache($namearray)
24 if (sizeof($this->cache
) > 0)
26 user_error('You cannot Template::cache() more than once per initialization', E_USER_WARNING
);
30 foreach ($namearray AS $name)
32 $template = $this->_load($name);
33 $template = $this->_parse($template);
34 $this->cache
["$name"] = $template;
39 // ###################### Start Template::fetch ######################
42 if (isset($this->cache["$name"]))
44 $template = $this->cache
["$name"];
48 $this->uncached[] = $name;
49 $template = $this->_load($name);
50 $template = $this->_parse($template);
53 $this->usage["$name"]++
;
58 // ###################### Start Template::flush ######################
59 function flush($template)
67 user_error('Template::flush() produced no output', E_USER_WARNING
);
71 if (DEVDEBUG
AND $_GET['query'])
73 if (is_array($DB_sql->query_history
))
76 foreach ($DB_sql->query_history
AS $query)
78 echo $query . "\n\n<hr />\n\n";
87 user_error('You cannot Template::flush() more than once per initialization', E_USER_WARNING
);
93 foreach ($this->usage
AS $name => $count)
95 $optlist[] = '<option>' . iff($this->uncached
["$name"], ' -- [UNCACHED] ') . "$name ($count)</option
>";
98 $debug .= "\r
<hr
/><br
/>\r<table cellpadding
=\"4\" cellspacing
=\"1\" border
=\"0\" align
=\"center\" width
=\"99%\"
class=\"tborder\"
>\r<tr
>";
99 $debug .= "\r\t
<td width
=\"33%\" align
=\"center\"
class=\"panelback\" style
=\"text
-align
:center\"
>" . 'construct_debug_info_list()' . "</td
>\r\t<td width
=\"33%\" align
=\"center\"
class=\"panelback\" style
=\"text
-align
:center\"
>$revision</td
>";
100 $debug .= "\r\t
<td width
=\"33%\" align
=\"center\"
class=\"panelback\" style
=\"text
-align
:center\"
><a href
=\"" . SCRIPT_URI . iff(strpos(SCRIPT_URI, '?') !== false, '&query=1', '?query=1') . "\"
>Total queries used
: " . sizeof($DB_sql->query_history) . "</a
>";
101 $debug .= "</td
>\r</tr
>\r<tr
>\r\t<td align
=\"center\"
class=\"panelback\" style
=\"text
-align
:center\" colspan
=\"3\">";
102 $debug .= "\r
" . iff(is_array($this->uncached), sizeof($uncached) . " Uncached
Template(s
)\r<br
/>\r") . "<select
>\r\t<option
>TEMPLATE USAGE
</option
>\r\t" . implode("\r\t
", $optlist) . "\r
</select
>\r</td
></tr
></table
><br
/>";
104 $template = str_replace('</body>', "\r\r
<!-- dev debug
-->$debug\r<!-- / dev debug
-->\r\r</body
>", $template);
107 print(stripslashes($template));
110 // ###################### Start Template::_load ######################
111 function _load($name)
115 if (($template = @file_get_contents("{$bugsys
->options
['ts_includepath']}$name.tpl
")) !== false)
121 echo "could not load template file {$bugsys
->options
['ts_includepath']}$name.tpl
";
126 // ###################### Start Template::_parse #####################
127 function _parse($template)
129 $template = stripslashes($template);
130 $template = str_replace('"', '\"
', $template);
131 $template = $this->_parse_phrases($template);
132 $template = $this->_parse_conditionals($template);
136 // ################## Start Template::_parse_phrases #################
137 function _parse_phrases($template)
139 $tag_start = '<lang
';
140 $tag_start_end = '">';
141 $tag_end = '</lang>';
143 $location_start = -1;
146 // Process the empty phrase objects -- do this now so we don't have to worry about it when we're parsing later
147 $template = preg_replace('#\{lang\.(.*?)\}#i', '" . fetch_phrase(\'$1\') . "', $template);
151 // Find the start language object tag
152 $location_start = strpos($template, $tag_start, $location_end + 1);
153 if ($location_start === false)
159 $location_end = strpos($template, $tag_end, $location_end + strlen($tag_end));
160 if ($location_end === false)
165 // Extract the language object
166 $phrase_bunch = substr($template, $location_start, ($location_end + strlen($tag_end)) - $location_start);
168 // Find the close to the opening <lang>
169 $close_of_open = strpos($phrase_bunch, $tag_start_end);
170 if ($close_of_open === false)
175 // Extract the opening tag so it can be parsed
176 $init_tag = substr($phrase_bunch, 0, ($close_of_open + strlen($tag_start_end)));
177 $init_tag = str_replace($tag_start, '', $init_tag);
178 $init_tag = substr($init_tag, 0, strlen($init_tag) - 1);
180 // Get the args out of the tag
181 $args = preg_split('#([0-9].*?)=#', $init_tag);
182 foreach ($args AS $arg)
184 if ($arg AND $arg != ' ')
187 $arg = substr($arg, 2);
188 $arg = substr($arg, 0, strlen($arg) - 2);
193 // Just get the phrase name
194 $phrase_name = preg_replace('#<lang(.*?)>(.*?)</lang>#i', '$2', $phrase_bunch);
196 // Wrap the parsed data into the build function
197 $function_wrap = '" . construct_phrase(\'' . $phrase_name . '\'
, "' . implode('", "', $arglist) . '") . "';
199 // Replace the fully-parsed string back into the template
200 $template = substr_replace($template, $function_wrap, $location_start, $location_end + strlen($tag_end) - $location_start);
208 // ############### Start Template::_parse_conditionals ###############
209 function _parse_conditionals($template)
212 $location_start = -1;
216 $tag_start = '<if condition=\\"';
217 $tag_start_end = '\\
">';
218 $tag_else = '<else />';
223 // Finds the start tag: starts looking through the haystack one position
224 // after the old startpoint
225 $location_start = strpos($template, $tag_start, $location_start + 1);
226 if ($location_start === false)
231 // Finds the end tag: starts looking through the haystack after the old end
233 $location_end = strpos($template, $tag_end, $location_end + strlen($tag_end));
234 if ($location_end === false)
239 // Split the template expression out of the template data
240 $expression = substr($template, $location_start, ($location_end + strlen($tag_end)) - $location_start);
242 // If we have a nested expression, work it out by setting
243 // the search counters to be one and two after the current
245 if (substr_count($expression, $tag_end) > 1)
247 $location_start = $location_start + 1;
248 $location_end = $location_start + 2;
251 // Strip out the opening '<if expression="' part of the tag
252 $shell_removed = substr($expression, strlen($tag_start));
254 // Get the position of the end of the '<if epxression
="' tag
255 $shell_end = strpos($shell_removed, $tag_start_end);
257 // Take the condition out of the mix
258 $expression_condition = stripslashes(substr($shell_removed, 0, $shell_end));
260 // Now that we've safely stored the condition, parse out the rest of the data
261 $shell_removed = substr($shell_removed, $shell_end + strlen($tag_start_end));
263 // Find the end tag and then take out the iftrue data
264 $parsed = str_replace($tag_end, '', $shell_removed);
266 // Do we have an $tag_else
267 $location_else = strpos($parsed, $tag_else);
268 if ($location_else !== false)
270 $data = explode($tag_else, $parsed);
273 if (count($data) > 2)
275 user_error('Multiple else statements encountered while parsing conditionals in template', E_USER_WARNING);
282 // Nope, reassign variables
289 // Put the condition and iftrue in the iff() function
290 $parsed_expression = '" . iff(' . stripslashes($expression_condition) . ',"' . $iftrue . '","' . $iffalse . '") . "';
292 // Parse the iff()'d expression back into the template data
293 $template = substr_replace($template, $parsed_expression, $location_start, $location_end + strlen($tag_end) - $location_start);
296 // Repeat this process until it can't find any more
297 // expressions, then send back the parsed template data
303 /*=====================================================================*\
304 || ###################################################################
307 || ###################################################################
308 \*=====================================================================*/