This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /* |
||
3 | * You may not change or alter any portion of this comment or credits |
||
4 | * of supporting developers from this source code or any supporting source code |
||
5 | * which is considered copyrighted (c) material of the original comment or credit authors. |
||
6 | * |
||
7 | * This program is distributed in the hope that it will be useful, |
||
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
||
10 | */ |
||
11 | |||
12 | /** |
||
13 | * @copyright {@link http://xoops.org/ XOOPS Project} |
||
14 | * @license {@link http://www.fsf.org/copyleft/gpl.html GNU public license} |
||
15 | * @package |
||
16 | * @since |
||
17 | * @author XOOPS Development Team, |
||
18 | * @author GIJ=CHECKMATE (PEAK Corp. http://www.peak.ne.jp/) |
||
19 | */ |
||
20 | |||
21 | if (!class_exists('PatTemplate')) { |
||
22 | |||
23 | /** |
||
24 | * Variable prefix |
||
25 | * @access public |
||
26 | * @const patTEMPLATE_TAG_START |
||
27 | */ |
||
28 | define('patTEMPLATE_TAG_START', '{'); |
||
29 | |||
30 | /** |
||
31 | * Variable sufix |
||
32 | * @const patTEMPLATE_TAG_END |
||
33 | * @access public |
||
34 | */ |
||
35 | define('patTEMPLATE_TAG_END', '}'); |
||
36 | |||
37 | /** |
||
38 | * Template type Standard |
||
39 | * @const patTEMPLATE_TYPE_STANDARD |
||
40 | */ |
||
41 | define('patTEMPLATE_TYPE_STANDARD', 'STANDARD'); |
||
42 | /** |
||
43 | * Template type OddEven |
||
44 | * @const patTEMPLATE_TYPE_ODDEVEN |
||
45 | */ |
||
46 | define('patTEMPLATE_TYPE_ODDEVEN', 'ODDEVEN'); |
||
47 | /** |
||
48 | * Template type Condition |
||
49 | * @const patTEMPLATE_TYPE_CONDITION |
||
50 | */ |
||
51 | define('patTEMPLATE_TYPE_CONDITION', 'CONDITION'); |
||
52 | /** |
||
53 | * Template type SimpleCondition |
||
54 | * @const patTEMPLATE_TYPE_SIMPLECONDITION |
||
55 | */ |
||
56 | define('patTEMPLATE_TYPE_SIMPLECONDITION', 'SIMPLECONDITION'); |
||
57 | |||
58 | /** |
||
59 | * Easy-to-use but powerful template engine |
||
60 | * |
||
61 | * Features include: several templates in one file, automatic repetitions, global variables, |
||
62 | * alternating lists, conditions, and much more |
||
63 | * |
||
64 | * @package PatTemplate |
||
65 | * @access public |
||
66 | * @author Stephan Schmidt <[email protected]> |
||
67 | */ |
||
68 | class PatTemplate |
||
69 | { |
||
70 | /** |
||
71 | * Constructor |
||
72 | * |
||
73 | * Create new PatTemplate object |
||
74 | * You can choose between two outputs you want tp generate: html (default) or tex (LaTex). |
||
75 | * When "tex" is used the PatTemplate markings used for variables are changed as LaTex makes use of the default PatTemplate markings. |
||
76 | * You can also change the markings later by calling setTags(); |
||
77 | * |
||
78 | * @access public |
||
79 | * @param string $type type of output you want to generate. |
||
80 | */ |
||
81 | public function __construct($type = 'html') |
||
82 | { |
||
83 | // Directory, where Templates are stored |
||
84 | $this->basedir = ''; |
||
85 | |||
86 | // counter for template iterations |
||
87 | $this->iteration = array(); |
||
88 | |||
89 | // Filenames of the templates |
||
90 | $this->filenames = array(); |
||
91 | |||
92 | // HTML/Text of unparsed templates |
||
93 | $this->plain_templates = array(); |
||
94 | |||
95 | // HTML/Text of parsed templates |
||
96 | $this->parsed_templates = array(); |
||
97 | |||
98 | // Amount and names of all templates |
||
99 | $this->cnt_templates = 0; |
||
100 | $this->templates = array(); |
||
101 | |||
102 | // These vars will be set for all added Templates |
||
103 | $this->subtemplates = array(); |
||
104 | |||
105 | $this->variables = array(); |
||
106 | $this->globals = array(); |
||
107 | |||
108 | $this->attributes = array(); |
||
109 | $this->whitespace = array(); |
||
110 | |||
111 | // Does one of the templates contain other templates |
||
112 | $this->uses_dependencies = false; |
||
113 | |||
114 | $this->dependencies = []; |
||
115 | |||
116 | // Set template tags |
||
117 | $this->setType($type); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Set template type |
||
122 | * |
||
123 | * select a predefined template type |
||
124 | * |
||
125 | * @param string $type predefined template type, like "html" or "tex" |
||
126 | * @access public |
||
127 | */ |
||
128 | public function setType($type = '') |
||
129 | { |
||
130 | switch ($type) { |
||
131 | case 'tex': |
||
132 | $this->setTags('<{', '}>'); |
||
133 | break; |
||
134 | |||
135 | case 'html': |
||
136 | default: |
||
137 | $this->setTags(patTEMPLATE_TAG_START, patTEMPLATE_TAG_END); |
||
138 | break; |
||
139 | } |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Set template tags |
||
144 | * |
||
145 | * Sets the start and end tags of template variables |
||
146 | * |
||
147 | * @param string $start start tag |
||
148 | * @param string $end end tag |
||
149 | * @access public |
||
150 | */ |
||
151 | public function setTags($start = patTEMPLATE_TAG_START, $end = patTEMPLATE_TAG_END) |
||
152 | { |
||
153 | $this->tag_start = $start; |
||
154 | $this->tag_end = $end; |
||
155 | |||
156 | $this->regex_get_all_vars = '/' . $start . '([^a-z{}]+)' . $end . '/'; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Set template directory |
||
161 | * |
||
162 | * Sets the directory where the template are stored. |
||
163 | * By default the engine looks in the directory where the original file is stored. |
||
164 | * |
||
165 | * @param string $basedir directory of the templates |
||
166 | * @access public |
||
167 | */ |
||
168 | public function setBasedir($basedir) |
||
169 | { |
||
170 | $this->basedir = $basedir; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Check if a template exists |
||
175 | * |
||
176 | * @param string $name name of the template |
||
177 | * @return bool |
||
178 | * @access public |
||
179 | */ |
||
180 | public function exists($name) |
||
181 | { |
||
182 | $name = strtoupper($name); |
||
183 | |||
184 | for ($i = 0; $i < $this->cnt_templates; ++$i) { |
||
185 | if ($this->templates[$i] == $name) { |
||
186 | return true; |
||
187 | } |
||
188 | } |
||
189 | |||
190 | return false; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Add a template |
||
195 | * |
||
196 | * Adds a plain text/html to the template engine. |
||
197 | * The file has to be in the directory that has been set using setBaseDir |
||
198 | * |
||
199 | * @param string $name name of the template |
||
200 | * @param string $filename filename of the sourcetemplate |
||
201 | * @access public |
||
202 | * @deprecated 2.4 2001/11/05 |
||
203 | * @see setBaseDir(), addTemplates() |
||
204 | */ |
||
205 | public function addTemplate($name, $filename) |
||
206 | { |
||
207 | $this->createTemplate($name, array( |
||
208 | 'type' => 'file', |
||
209 | 'filename' => $filename |
||
210 | )); |
||
211 | // Store the filename |
||
212 | $this->filenames[$name] = $filename; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Adds several templates |
||
217 | * |
||
218 | * Adds several templates to the template engine using an associative array. |
||
219 | * Names of the templates are stored in the keys, filenames are the values. |
||
220 | * The templates have to be in the directory set by setBaseDir(). |
||
221 | * |
||
222 | * @param array $templates associative Array with name/filename pairs |
||
223 | * @access public |
||
224 | * @deprecated 2.4 2001/11/05 |
||
225 | * @see setBaseDir(), addTemplate() |
||
226 | */ |
||
227 | public function addTemplates($templates) |
||
228 | { |
||
229 | // while (list($name, $file) = each($templates)) { |
||
230 | foreach ($templates as $name => $file) { |
||
231 | $this->addTemplate($name, $file); |
||
232 | } |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * creates a new template |
||
237 | * |
||
238 | * creates all needed variables |
||
239 | * |
||
240 | * @param string $name name of the template |
||
241 | * @param array $source data regarding the source of the template |
||
242 | * @access private |
||
243 | */ |
||
244 | public function createTemplate($name, $source) |
||
245 | { |
||
246 | $name = strtoupper($name); |
||
247 | |||
248 | // Store the name of the template in index table |
||
249 | $this->templates[$this->cnt_templates] = $name; |
||
250 | $this->cnt_templates++; |
||
251 | |||
252 | // Store the source |
||
253 | $this->source[$name] = $source; |
||
254 | |||
255 | // Init vars for the new Templates |
||
256 | |||
257 | // Store all attributes in Array |
||
258 | $this->attributes[$name] = array( |
||
259 | 'loop' => 1, |
||
260 | 'visibility' => 'visible', |
||
261 | 'unusedvars' => 'strip', |
||
262 | 'type' => 'STANDARD' |
||
263 | ); |
||
264 | $this->iteration[$name] = 0; |
||
265 | |||
266 | // No vars are set for this template |
||
267 | $this->variables[$name] = array(); |
||
268 | // No subtemplates have been specified |
||
269 | $this->cnt_subtemplates[$name] = 0; |
||
270 | |||
271 | $this->varsConverted[$name] = false; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Sets the type of the Template |
||
276 | * |
||
277 | * Template types can be STANDARD, CONDITION or ODDEVEN |
||
278 | * The type of the template can also be set using setAttribute() |
||
279 | * |
||
280 | * @param string $template name of the template |
||
281 | * @param string $type type of the template |
||
282 | * @access private |
||
283 | * @see setAttribute() |
||
284 | */ |
||
285 | public function setTemplateType($template, $type) |
||
286 | { |
||
287 | $template = strtoupper($template); |
||
288 | |||
289 | $this->setAttribute($template, 'type', $type); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Sets the conditionvar of a condtion Template |
||
294 | * |
||
295 | * The type of the template has to be condition |
||
296 | * |
||
297 | * @param string $template name of the template |
||
298 | * @param string $conditionvar name of the conditionvariable |
||
299 | * @access private |
||
300 | * @see setTemplateType() |
||
301 | */ |
||
302 | public function setConditionVar($template, $conditionvar) |
||
303 | { |
||
304 | $template = strtoupper($template); |
||
305 | $conditionvar = strtoupper($conditionvar); |
||
306 | |||
307 | $this->conditionvars[$template] = $conditionvar; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Sets an attribute of a template |
||
312 | * |
||
313 | * supported attributes: visibilty, loop, parse, unusedvars |
||
314 | * |
||
315 | * @param string $template name of the template |
||
316 | * @param string $attribute name of the attribute |
||
317 | * @param mixed $value value of the attribute |
||
318 | * @access public |
||
319 | * @see setAttributes(),getAttribute(), clearAttribute() |
||
320 | */ |
||
321 | View Code Duplication | public function setAttribute($template, $attribute, $value) |
|
322 | { |
||
323 | $template = strtoupper($template); |
||
324 | $attribute = strtolower($attribute); |
||
325 | |||
326 | $this->attributes[$template][$attribute] = $value; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Sets several attribute of a template |
||
331 | * |
||
332 | * $attributes has to be a assotiative arrays containing attribute/value pairs |
||
333 | * supported attributes: visibilty, loop, parse, unusedvars |
||
334 | * |
||
335 | * @param string $template name of the template |
||
336 | * @param array $attributes attribute/value pairs |
||
337 | * @access public |
||
338 | * @see setAttribute(), getAttribute(), clearAttribute() |
||
339 | * @return bool |
||
340 | */ |
||
341 | public function setAttributes($template, $attributes) |
||
342 | { |
||
343 | if (!is_array($attributes)) { |
||
344 | return false; |
||
345 | } |
||
346 | |||
347 | $template = strtoupper($template); |
||
348 | |||
349 | // while (list($attribute, $value) = each($attributes)) { |
||
350 | foreach ($attributes as $attribute => $value) { |
||
351 | $attribute = strtolower($attribute); |
||
352 | $this->attributes[$template][$attribute] = $value; |
||
353 | } |
||
354 | return true; |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * Gets an attribute of a template |
||
359 | * |
||
360 | * supported attributes: visibilty, loop, parse, unusedvars |
||
361 | * |
||
362 | * @param string $template name of the template |
||
363 | * @param string $attribute name of the attribute |
||
364 | * @return mixed value of the attribute |
||
365 | * @access public |
||
366 | * @see setAttribute(), setAttributes(), clearAttribute() |
||
367 | */ |
||
368 | |||
369 | View Code Duplication | public function getAttribute($template, $attribute) |
|
370 | { |
||
371 | $template = strtoupper($template); |
||
372 | $attribute = strtolower($attribute); |
||
373 | |||
374 | return $this->attributes[$template][$attribute]; |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * Clears an attribute of a template |
||
379 | * |
||
380 | * supported attributes: visibilty, loop, parse, unusedvars |
||
381 | * |
||
382 | * @param string $template name of the template |
||
383 | * @param string $attribute name of the attribute |
||
384 | * @access public |
||
385 | * @see setAttribute(), setAttributes(), getAttribute() |
||
386 | */ |
||
387 | View Code Duplication | public function clearAttribute($template, $attribute) |
|
388 | { |
||
389 | $template = strtoupper($template); |
||
390 | $attribute = strtolower($attribute); |
||
391 | |||
392 | unset($this->attributes[$template][$attribute]); |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Adds a subtemplate for a condition or oddeven template |
||
397 | * |
||
398 | * template type has to be condition or oddeven |
||
399 | * |
||
400 | * @param string $template name of the template |
||
401 | * @param string $condition condition for this subtemplate |
||
402 | * @access private |
||
403 | * @see setTemplateType() |
||
404 | */ |
||
405 | public function addSubTemplate($template, $condition) |
||
406 | { |
||
407 | $template = strtoupper($template); |
||
408 | |||
409 | $this->subtemplates[$template][$condition] = ''; |
||
410 | $this->subtemplate_conditions[$template][$this->cnt_subtemplates[$template]] = $condition; |
||
411 | |||
412 | $this->cnt_subtemplates[$template]++; |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * Parses several templates from one patTemplate file |
||
417 | * |
||
418 | * Templates can be seperated using Tags |
||
419 | * The file has to be located in the directory that has been set using setBaseDir. |
||
420 | * |
||
421 | * @param string $file filename |
||
422 | * @access public |
||
423 | * @see setBasedir() |
||
424 | */ |
||
425 | public function readTemplatesFromFile($file) |
||
426 | { |
||
427 | // Tag depth |
||
428 | $this->depth = -1; |
||
429 | // Names, extracted from the Tags |
||
430 | $this->template_names = array(); |
||
431 | // All HTML code, that is found between the tags |
||
432 | $this->template_data = array(); |
||
433 | // Attributes, extracted from tags |
||
434 | $this->template_types = array(); |
||
435 | |||
436 | $this->last_opened = array(); |
||
437 | $this->last_keep = array(); |
||
438 | $this->whitespace = array(); |
||
439 | |||
440 | $this->createParser($file); |
||
441 | |||
442 | $open_tag = array_pop($this->last_opened); |
||
443 | if ($open_tag !== null) { |
||
444 | die("Error in template '" . $file . "': </" . $open_tag . '> still open at end of file.'); |
||
445 | } |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * parse a template file and call the appropriate handlers |
||
450 | * |
||
451 | * @access private |
||
452 | * @param string $fname filename of the template |
||
453 | */ |
||
454 | public function createParser($fname) |
||
455 | { |
||
456 | // Store filename of the first file that has to be opened |
||
457 | // If basedir is set, prepend basedir |
||
458 | $pname = $this->basedir !== '' ? $this->basedir . '/' . $fname : $fname; |
||
459 | |||
460 | // open file for reading |
||
461 | $fp = fopen($pname, 'r'); |
||
462 | |||
463 | // couldn't open the file => exit |
||
464 | if (!$fp) { |
||
465 | die("Couldn't open file '" . $fname . "' for reading!"); |
||
466 | } |
||
467 | |||
468 | // Read line for line from the template |
||
469 | |||
470 | // current linenumber in file, used for error messages |
||
471 | $lineno = 1; |
||
472 | |||
473 | // read until end of file |
||
474 | while (!feof($fp)) { |
||
475 | // Read one line |
||
476 | $line = fgets($fp, 4096); |
||
477 | |||
478 | // check, wether leading and trailing whitepaces should be stripped |
||
479 | switch ($this->whitespace[count($this->whitespace) - 1]) { |
||
480 | case 'trim': |
||
481 | $line = trim($line); |
||
482 | break; |
||
483 | case 'ltrim': |
||
484 | $line = ltrim($line); |
||
485 | break; |
||
486 | case 'rtrim': |
||
487 | $line = rtrim($line); |
||
488 | break; |
||
489 | } |
||
490 | |||
491 | // ========= [ OPEN TAG ] ========= |
||
492 | |||
493 | // check for any <patTemplate:...> Tag by using RegExp |
||
494 | if (preg_match('/<patTemplate:([[:alnum:]]+)[[:space:]]*(.*)>/i', $line, $regs)) { |
||
495 | // Get Tag name and attributes |
||
496 | $tagname = strtolower($regs[1]); |
||
497 | $attributes = $this->parseAttributes($regs[2]); |
||
498 | |||
499 | if ($attributes['keep'] > 0) { |
||
500 | // create new attribute |
||
501 | $newkeep = $attributes['keep'] > 1 ? ' keep="' . ($attributes['keep'] - 1) . '"' : ''; |
||
502 | |||
503 | // replace old attribute with new attribute |
||
504 | $newline = str_replace(' keep="' . $attributes['keep'] . '"', $newkeep, $line); |
||
505 | |||
506 | // use this line as data |
||
507 | $this->dataHandler($fname, $newline, $lineno); |
||
508 | |||
509 | // if the tag was not empty keep the closing tag, too |
||
510 | if (substr($regs[2], -1) !== '/') { |
||
511 | $this->last_keep[] = true; |
||
512 | } |
||
513 | } else { |
||
514 | $this->last_keep[] = false; |
||
515 | |||
516 | // handle start Element |
||
517 | $this->startElementHandler($fname, $tagname, $attributes, $line, $lineno); |
||
518 | |||
519 | if (substr($regs[2], -1) === '/') { |
||
520 | $this->endElementHandler($fname, $tagname, $line, $lineno); |
||
521 | } // Store the name of the last opened tag |
||
522 | else { |
||
523 | $this->last_opened[] = $tagname; |
||
524 | } |
||
525 | } |
||
526 | } |
||
527 | |||
528 | // ========= [ CLOSING TAG ] ========= |
||
529 | |||
530 | // Check if a closing <patTemplate:...> Tag has been found |
||
531 | elseif (preg_match("/<\/patTemplate:([[:alnum:]]+)>/i", $line, $regs)) { |
||
532 | // Yes => get the tagname |
||
533 | $tagname = strtolower($regs[1]); |
||
534 | |||
535 | $keep = array_pop($this->last_keep); |
||
536 | if (!$keep) { |
||
537 | $last_opened = array_pop($this->last_opened); |
||
538 | if ($last_opened === null) { |
||
539 | die("Error in template '" . $fname . "': no opening tag found for </" . $tagname . '> in line ' . $lineno); |
||
540 | } |
||
541 | |||
542 | if ($tagname != $last_opened) { |
||
543 | die("Error in template '" . $fname . "': closing </" . $tagname . '> does not match opened <' . $last_opened . '> in line ' . $lineno); |
||
544 | } |
||
545 | |||
546 | $this->endElementHandler($fname, $tagname, $line, $lineno); |
||
547 | } else { |
||
548 | $this->dataHandler($fname, $line, $lineno); |
||
549 | } |
||
550 | } |
||
551 | |||
552 | // ========= [ CDATA SECTION ] ========= |
||
553 | |||
554 | // No tag found => store the line |
||
555 | else { |
||
556 | $this->dataHandler($fname, $line, $lineno); |
||
557 | } |
||
558 | |||
559 | // goto next line |
||
560 | ++$lineno; |
||
561 | } |
||
562 | } |
||
563 | |||
564 | /** |
||
565 | * handle a <patTemplate:...> start tag in template parser |
||
566 | * |
||
567 | * @access private |
||
568 | * @param string $fname name of the file where the tag was found (kind of parser id) |
||
569 | * @param string $tagname name of the start tag that was found |
||
570 | * @param array $attributes all attributes that were found |
||
571 | * @param string $line the complete line containing the tag |
||
572 | * @param integer $lineno lineno in the parse file (can be used for error messages |
||
573 | */ |
||
574 | public function startElementHandler($fname, $tagname, $attributes, $line, $lineno) |
||
575 | { |
||
576 | // check for whitespace attribute |
||
577 | if ($attributes['whitespace']) { |
||
578 | array_push($this->whitespace, strtolower($attributes['whitespace'])); |
||
579 | } // use whitepspace mode from last opened template |
||
580 | else { |
||
581 | array_push($this->whitespace, $this->whitespace[count($this->whitespace) - 1]); |
||
582 | } |
||
583 | |||
584 | switch ($tagname) { |
||
585 | // Beginning of a template found |
||
586 | case 'tmpl': |
||
587 | // parse all attributes from a string into an associative array |
||
588 | |||
589 | // Check for name of template, which is a necessary attribute |
||
590 | if (!$tmpl_name = strtoupper($attributes['name'])) { |
||
591 | die("Error in template '" . $fname . "': missing name for template in line " . $lineno); |
||
592 | } |
||
593 | |||
594 | unset($attributes['name']); |
||
595 | |||
596 | // Increment Tag Depth |
||
597 | $this->depth++; |
||
598 | |||
599 | // Start with a blank template |
||
600 | $this->template_data[$this->depth] = ''; |
||
601 | |||
602 | // and store the name |
||
603 | $this->template_names[$this->depth] = $tmpl_name; |
||
604 | |||
605 | // Check, if attribute "type" was found |
||
606 | if ($tmpl_type = strtoupper($attributes['type'])) { |
||
607 | $this->template_types[$this->depth] = $tmpl_type; |
||
608 | $attributes['type'] = $tmpl_type; |
||
609 | } // No type found => this is a boring standard template |
||
610 | else { |
||
611 | $attributes['type'] = 'STANDARD'; |
||
612 | $this->template_types[$this->depth] = 'STANDARD'; |
||
613 | } |
||
614 | |||
615 | // Check for src attribute => external file |
||
616 | if ($attributes['src']) { |
||
617 | // Store the filename of the external file |
||
618 | $filename = $attributes['src']; |
||
619 | |||
620 | // Has the external file to be parsed |
||
621 | if ($attributes[parse] === 'on') { |
||
622 | $this->createParser($filename); |
||
623 | } // No parsing, just take the whole content of the file |
||
624 | else { |
||
625 | // Filename including full path |
||
626 | $external = $this->basedir !== '' ? $this->basedir . '/' . $filename : $filename; |
||
627 | |||
628 | // Open the file and read all the content |
||
629 | if (!$tmp = @implode('', @file($external))) { |
||
630 | die("Couldn't open file '" . $external . "' for reading in template " . $fname . ' line ' . $lineno); |
||
631 | } |
||
632 | |||
633 | $this->template_data[$this->depth] .= $tmp; |
||
634 | } |
||
635 | |||
636 | // Delete the src attribute, it hasn't to be stored |
||
637 | unset($attributes['src']); |
||
638 | } // No external file => the template is part of teh current file |
||
639 | else { |
||
640 | $filename = '[part of ' . $fname . ']'; |
||
641 | } |
||
642 | |||
643 | // add the template |
||
644 | $this->addTemplate($this->template_names[$this->depth], $filename); |
||
645 | |||
646 | // Set all remaining attributes |
||
647 | $this->setAttributes($this->template_names[$this->depth], $attributes); |
||
648 | |||
649 | switch ($this->template_types[$this->depth]) { |
||
650 | // Template type is "ODDEVEN", it contains two alternating subtemplates |
||
651 | case 'ODDEVEN': |
||
652 | $this->setConditionVar($this->template_names[$depth], 'PAT_ROW_VAR mod 2'); |
||
653 | break; |
||
654 | |||
655 | // Template is a condition Tenplate => it needs a condition var |
||
656 | case 'CONDITION': |
||
657 | // none found => there is an error |
||
658 | if (!$conditionvar = $attributes['conditionvar']) { |
||
659 | die("Error in template '" . $fname . "': missing conditionvar for template in line " . $lineno); |
||
660 | } |
||
661 | |||
662 | // conditionvar was found => store it |
||
663 | $this->setConditionVar($this->template_names[$this->depth], $conditionvar); |
||
664 | break; |
||
665 | |||
666 | // Template is a simple condition Tenplate => it needs required vars |
||
667 | case 'SIMPLECONDITION': |
||
668 | // none found => there is an error |
||
669 | if ($requiredvars = $attributes['requiredvars']) { |
||
670 | $this->setAttribute($this->template_names[$this->depth], 'requiredvars', explode(',', $requiredvars)); |
||
671 | } else { |
||
672 | die("Error in template '" . $fname . "': missing requiredvars attribute for simple condition template in line " . $lineno); |
||
673 | } |
||
674 | |||
675 | break; |
||
676 | |||
677 | } |
||
678 | |||
679 | // if the template isn't the root( depth=0 ) template, a placeholder has |
||
680 | // to be put into the parent template |
||
681 | if ($this->depth > 0) { |
||
682 | // Is there a placeholder attribute? |
||
683 | if ($placeholder = strtoupper($attributes['placeholder'])) { |
||
684 | // placeholder="none" found => DO NOT PUT A PLACEHOLDER IN THE PARENT TEMPLATE! |
||
685 | if ($placeholder !== 'NONE') { |
||
686 | $this->template_data[$this->depth - 1] .= $this->tag_start . $placeholder . $this->tag_end; |
||
687 | } |
||
688 | } // No placeholder attribute found => standard placeholder |
||
689 | else { |
||
690 | $this->template_data[$this->depth - 1] .= $this->tag_start . 'TMPL:' . $this->template_names[$this->depth] . $this->tag_end . "\n"; |
||
691 | // Tell the parent template, that it has to parse the child template, before parsing |
||
692 | // itself |
||
693 | $this->addDependency($this->template_names[$this->depth - 1], $this->template_names[$this->depth]); |
||
694 | } |
||
695 | } |
||
696 | break; |
||
697 | |||
698 | // Found the beginning of a subtemplate |
||
699 | case 'sub': |
||
700 | // A subtemplate needs to have a "condition" attribute |
||
701 | $condition = $attributes['condition']; |
||
702 | |||
703 | // None found => error |
||
704 | if (isset($condition) == 0) { |
||
705 | die("Error in template '" . $fname . "': missing condition attribute for template in line " . $lineno); |
||
706 | } |
||
707 | |||
708 | // Everything is ok => add the subtemplate and store the condition |
||
709 | $this->addSubTemplate($this->template_names[$this->depth], $condition); |
||
710 | |||
711 | // Store the current condition |
||
712 | $this->template_condition[$this->depth] = $condition; |
||
713 | break; |
||
714 | |||
715 | // Found a link template |
||
716 | case 'link': |
||
717 | $src = strtoupper($attributes['src']); |
||
718 | |||
719 | if (!$src) { |
||
720 | die("Error in template '" . $fname . "': missing src attribute for link in line " . $lineno); |
||
721 | } |
||
722 | |||
723 | // put a placeholder into the current template |
||
724 | if ($this->depth >= 0) { |
||
725 | $this->template_data[$this->depth] .= $this->tag_start . 'TMPL:' . $src . $this->tag_end . "\n"; |
||
726 | // Tell the parent template, that it has to parse the child template, before parsing |
||
727 | // itself |
||
728 | $this->addDependency($this->template_names[$this->depth], $src); |
||
729 | } |
||
730 | break; |
||
731 | |||
732 | // No valid Tag found => |
||
733 | default: |
||
734 | die("Error in template '" . $fname . "': unkown Tag in line " . $lineno); |
||
735 | break; |
||
736 | } |
||
737 | } |
||
738 | |||
739 | /** |
||
740 | * handle a </patTemplate:...> end tag in template parser |
||
741 | * |
||
742 | * @access private |
||
743 | * @param string $fname name of the file where the tag was found (kind of parser id) |
||
744 | * @param string $tagname name of the start tag that was found |
||
745 | * @param string $line the complete line containing the tag |
||
746 | */ |
||
747 | public function endElementHandler($fname, $tagname, $line) |
||
748 | { |
||
749 | array_pop($this->whitespace); |
||
750 | |||
751 | switch ($tagname) { |
||
752 | // End of a template found |
||
753 | case 'tmpl': |
||
754 | // If the current template is a standard template, store all content |
||
755 | // found between <patTemplate> Tags |
||
756 | if ($this->template_types[$this->depth] === 'STANDARD' |
||
757 | || $this->template_types[$this->depth] === 'SIMPLECONDITION' |
||
758 | ) { |
||
759 | $this->setPlainContent($this->template_names[$this->depth], $this->template_data[$this->depth]); |
||
760 | } |
||
761 | |||
762 | // Decrease Tagdepth |
||
763 | $this->depth--; |
||
764 | |||
765 | break; |
||
766 | |||
767 | // End of a subtemplate found |
||
768 | case 'sub': |
||
769 | // Store alle content found between :sub Tags |
||
770 | $this->setPlainContent($this->template_names[$this->depth], $this->template_data[$this->depth], $this->template_condition[$this->depth]); |
||
771 | |||
772 | // clear all Data, to store the data of the next subtemplate |
||
773 | $this->template_data[$this->depth] = ''; |
||
774 | break; |
||
775 | |||
776 | // End of a link found |
||
777 | case 'link': |
||
778 | // Just ignore this tag... |
||
779 | break; |
||
780 | |||
781 | // No kown tag found |
||
782 | default: |
||
783 | die("Error in template '" . $fname . "': unkown closing tag in line " . $lineno); |
||
784 | break; |
||
785 | } |
||
786 | } |
||
787 | |||
788 | /** |
||
789 | * handle a CDATA in template parser |
||
790 | * |
||
791 | * @access private |
||
792 | * @param string $fname name of the file where the tag was found (kind of parser id) |
||
793 | * @param string $data all cdata that was found |
||
794 | */ |
||
795 | public function dataHandler($fname, $data) |
||
796 | { |
||
797 | $this->template_data[$this->depth] .= $data; |
||
798 | } |
||
799 | |||
800 | /** |
||
801 | * Adds a variable to a template |
||
802 | * |
||
803 | * Each Template can have an unlimited amount of its own variables |
||
804 | * |
||
805 | * @param string $template name of the template |
||
806 | * @param string $name name of the variables |
||
807 | * @param mixed $value value of the variable |
||
808 | * @access public |
||
809 | * @see addVars(), addRows(), addGlobalVar(), addGlobalVars() |
||
810 | */ |
||
811 | public function addVar($template, $name, $value) |
||
812 | { |
||
813 | $template = strtoupper($template); |
||
814 | $name = strtoupper($name); |
||
815 | |||
816 | if (!is_array($value)) { |
||
817 | $value = (string)$value; |
||
818 | } |
||
819 | |||
820 | // store the value and the name of the variable |
||
821 | $this->variables[$template][$name] = $value; |
||
822 | |||
823 | // if the value is an array, the template has to be repeated |
||
824 | if (is_array($value)) { |
||
825 | // Check, how often the template has to be repeated |
||
826 | if ($this->getAttribute($template, 'loop') < count($value)) { |
||
827 | $this->setAttribute($template, 'loop', count($value)); |
||
828 | } |
||
829 | } |
||
830 | } |
||
831 | |||
832 | /** |
||
833 | * Adds several variables to a template |
||
834 | * |
||
835 | * Each Template can have an unlimited amount of its own variables |
||
836 | * $variables has to be an assotiative array containing variable/value pairs |
||
837 | * |
||
838 | * @param string $template name of the template |
||
839 | * @param array $variables assotiative array of the variables |
||
840 | * @param string $prefix prefix for all variable names |
||
841 | * @access public |
||
842 | * @see addVar(), addRows(), addGlobalVar(), addGlobalVars() |
||
843 | * @return bool |
||
844 | */ |
||
845 | public function addVars($template, $variables, $prefix = '') |
||
846 | { |
||
847 | // Are there variables? |
||
848 | if (!is_array($variables)) { |
||
849 | return false; |
||
850 | } |
||
851 | |||
852 | // Add all vars |
||
853 | // while (list($name, $value) = each($variables)) { |
||
854 | foreach ($variables as $name => $value) { |
||
855 | if (!is_int($name)) { |
||
856 | $this->addVar($template, $prefix . $name, $value); |
||
857 | } |
||
858 | } |
||
859 | return true; |
||
860 | } |
||
861 | |||
862 | /** |
||
863 | * Adds several rows of variables to a template |
||
864 | * |
||
865 | * Each Template can have an unlimited amount of its own variables |
||
866 | * Can be used to add a database result as variables to a template |
||
867 | * |
||
868 | * @param string $template name of the template |
||
869 | * @param array $rows array containing assotiative arrays with variable/value pairs |
||
870 | * @param string $prefix prefix for all variable names |
||
871 | * @access public |
||
872 | * @see addVar(), addVars(), addGlobalVar(), addGlobalVars() |
||
873 | */ |
||
874 | public function addRows($template, $rows, $prefix = '') |
||
875 | { |
||
876 | // Store the vars in this array |
||
877 | $newvars = array(); |
||
878 | |||
879 | // get amount of rows |
||
880 | $cnt_rows = count($rows); |
||
881 | |||
882 | if ($cnt_rows == 1) { |
||
883 | $this->addVars($template, $rows[0], $prefix); |
||
884 | } else { |
||
885 | for ($i = 0; $i < $cnt_rows; ++$i) { |
||
886 | if (is_array($rows[$i])) { |
||
887 | // Get key and value |
||
888 | // while (list($key, $value) = each($rows[$i])) { |
||
889 | foreach ($rows[$i] as $key => $value) { |
||
890 | // check if the array key is an int value => skip it |
||
891 | if (!is_int($key)) { |
||
892 | // prepend prefix and store the value |
||
893 | $new_vars[$prefix . $key][$i] = $value; |
||
0 ignored issues
–
show
|
|||
894 | } |
||
895 | } |
||
896 | } |
||
897 | } |
||
898 | |||
899 | // add the vars to the template |
||
900 | $this->addVars($template, $new_vars); |
||
901 | } |
||
902 | } |
||
903 | |||
904 | /** |
||
905 | * Adds a global variable |
||
906 | * |
||
907 | * Global variables are valid in all templates of this object |
||
908 | * |
||
909 | * @param string $name name of the global variable |
||
910 | * @param string $value value of the variable |
||
911 | * @access public |
||
912 | * @see addGlobalVars(), addVar(), addVars(), addRows() |
||
913 | */ |
||
914 | public function addGlobalVar($name, $value) |
||
915 | { |
||
916 | $this->globals[strtoupper($name)] = (string)$value; |
||
917 | } |
||
918 | |||
919 | /** |
||
920 | * Adds several global variables |
||
921 | * |
||
922 | * Global variables are valid in all templates of this object |
||
923 | * $variables is an assotiative array, containing name/value pairs of the variables |
||
924 | * |
||
925 | * @param array $variables array containing the variables |
||
926 | * @param string $prefix prefix for variable names |
||
927 | * @access public |
||
928 | * @see addGlobalVar(), addVar(), addVars(), addRows() |
||
929 | */ |
||
930 | public function addGlobalVars($variables, $prefix = '') |
||
931 | { |
||
932 | // while (list($variable, $value) = each($variables)) { |
||
933 | foreach ($variables as $variable => $value) { |
||
934 | $this->globals[strtoupper($prefix . $variable)] = (string)$value; |
||
935 | } |
||
936 | } |
||
937 | |||
938 | /** |
||
939 | * Creates a dependeny between two templates |
||
940 | * |
||
941 | * The Dependency tells a template, which templates have to be parsed before parsing the current template, because they are its children. |
||
942 | * |
||
943 | * @param string $container the name of the template, that contains the other template |
||
944 | * @param string $child the child of the container |
||
945 | * @access private |
||
946 | */ |
||
947 | public function addDependency($container, $child) |
||
948 | { |
||
949 | $this->dependencies[strtoupper($container)][] = strtoupper($child); |
||
950 | // This template now uses dependencies |
||
951 | $this->uses_dependencies = true; |
||
952 | } |
||
953 | |||
954 | /** |
||
955 | * loads a template |
||
956 | * |
||
957 | * The template has to be defined using addTemplate() or addTemplates() |
||
958 | * |
||
959 | * @param string $name name of the template that has to be loaded |
||
960 | * @access private |
||
961 | * @deprecated 2.4 2001/11/05 |
||
962 | * @see addTemplate(), addTemplates(); |
||
963 | * @return bool |
||
964 | */ |
||
965 | public function loadTemplate($name) |
||
966 | { |
||
967 | $name = strtoupper($name); |
||
968 | |||
969 | // prepend basedirname, if it exists |
||
970 | $fname = $this->basedir !== '' ? $this->basedir . '/' . $this->source[$name][filename] : $this->source[$name][filename]; |
||
971 | |||
972 | if (stristr($fname, '[part')) { |
||
973 | return true; |
||
974 | } |
||
975 | |||
976 | if (!$this->plain_templates[$name] = @implode('', @file($fname))) { |
||
977 | die("Couldn't open template '" . $name . "' (file: '" . $fname . "') for reading."); |
||
978 | } |
||
979 | return false; |
||
980 | } |
||
981 | |||
982 | /** |
||
983 | * sets the content of a template |
||
984 | * |
||
985 | * This function should used, if a template is added using tags instead of defining it by a filename |
||
986 | * |
||
987 | * @param string $template name of the template |
||
988 | * @param string $content the content that has to be set |
||
989 | * @param string $sub condition, for the subtemplate, if any |
||
990 | * @access private |
||
991 | */ |
||
992 | public function setPlainContent($template, $content, $sub = '') |
||
993 | { |
||
994 | $template = strtoupper($template); |
||
995 | |||
996 | // The content has to be set for a subtemplate |
||
997 | if ($sub !== '') { |
||
998 | $this->plain_templates[$template][$sub] = $content; |
||
999 | } // content is meant for a template |
||
1000 | else { |
||
1001 | $this->plain_templates[$template] = $content; |
||
1002 | } |
||
1003 | } |
||
1004 | |||
1005 | /** |
||
1006 | * parses a template |
||
1007 | * |
||
1008 | * Parses a template and stores the parsed content. |
||
1009 | * mode can be "w" for write (delete already parsed content) or "a" for append (appends the |
||
1010 | * new parsed content to the already parsed content) |
||
1011 | * |
||
1012 | * |
||
1013 | * @param string $template name of the template |
||
1014 | * @param string $mode mode for the parsing |
||
1015 | * @access public |
||
1016 | * @see parseStandardTemplate(), parseIterativeTemplate() |
||
1017 | */ |
||
1018 | public function parseTemplate($template, $mode = 'w') |
||
1019 | { |
||
1020 | $template = strtoupper($template); |
||
1021 | $this->iteration[$template] = 0; |
||
1022 | |||
1023 | // The template has to be repeated |
||
1024 | if ($this->getAttribute($template, 'loop') > 1) { |
||
1025 | $this->parseIterativeTemplate($template, $mode); |
||
1026 | } // parse it once |
||
1027 | else { |
||
1028 | $this->parseStandardTemplate($template, $mode); |
||
1029 | } |
||
1030 | } |
||
1031 | |||
1032 | /** |
||
1033 | * parses a standard template |
||
1034 | * |
||
1035 | * Parses a template and stores the parsed content. |
||
1036 | * mode can be "w" for write (delete already parsed content) or "a" for append (appends the |
||
1037 | * new parsed content to the already parsed content) |
||
1038 | * |
||
1039 | * |
||
1040 | * @param string $name name of the template |
||
1041 | * @param string $mode mode for the parsing |
||
1042 | * @access private |
||
1043 | * @see parseTemplate(), parseIterativeTemplate() |
||
1044 | */ |
||
1045 | public function parseStandardTemplate($name, $mode = 'w') |
||
1046 | { |
||
1047 | $name = strtoupper($name); |
||
1048 | |||
1049 | // get a copy of the plain content |
||
1050 | |||
1051 | $temp = $this->getTemplateContent($name); |
||
1052 | |||
1053 | $vars = $this->getVars($name); |
||
1054 | $vars[$this->tag_start . 'PAT_ROW_VAR' . $this->tag_end] = 1; |
||
1055 | // while (list($tag, $value) = each($vars)) { |
||
1056 | foreach ($vars as $tag => $value) { |
||
1057 | if (is_array($value)) { |
||
1058 | $value = $value[0]; |
||
1059 | } |
||
1060 | $temp = str_replace($tag, $value, $temp); |
||
1061 | } |
||
1062 | |||
1063 | // parse all global vars |
||
1064 | $this->parseGlobals($name, $temp); |
||
1065 | |||
1066 | // parse child templates into this template |
||
1067 | $this->parseDependencies($name, $temp, $mode); |
||
1068 | |||
1069 | // Strip unsused vars |
||
1070 | $this->stripUnusedVars($name, $temp); |
||
1071 | |||
1072 | View Code Duplication | if ($mode === 'a') { |
|
1073 | $this->parsed_templates[$name] .= $temp; |
||
1074 | } elseif ($mode === 'w') { |
||
1075 | $this->parsed_templates[$name] = $temp; |
||
1076 | } |
||
1077 | } |
||
1078 | |||
1079 | /** |
||
1080 | * parses an iterative template |
||
1081 | * |
||
1082 | * Parses a template and stores the parsed content. |
||
1083 | * mode can be "w" for write (delete already parsed content) or "a" for append (appends the |
||
1084 | * new parsed content to the already parsed content) |
||
1085 | * |
||
1086 | * |
||
1087 | * @param string $name name of the template |
||
1088 | * @param string $mode mode for the parsing |
||
1089 | * @access private |
||
1090 | * @see parseTemplate(), parseStandardTemplate() |
||
1091 | */ |
||
1092 | public function parseIterativeTemplate($name, $mode) |
||
1093 | { |
||
1094 | $name = strtoupper($name); |
||
1095 | |||
1096 | $temp = ''; |
||
1097 | |||
1098 | // repeat it template_loop[$name] times |
||
1099 | for ($PAT_ROW_VAR = 0; $PAT_ROW_VAR < $this->getAttribute($name, 'loop'); ++$PAT_ROW_VAR) { |
||
1100 | // add the PAT_ROW_VAR variable to the template |
||
1101 | $this->variables[$name]['PAT_ROW_VAR'][$PAT_ROW_VAR] = $PAT_ROW_VAR + 1; |
||
1102 | $this->iteration[$name] = $PAT_ROW_VAR; |
||
1103 | |||
1104 | // get the content to be parsed (dependent on PAT_ROW_VAR or conditionvar) |
||
1105 | $current = $this->getTemplateContent($name); |
||
1106 | |||
1107 | $vars = $this->getVars($name); |
||
1108 | // while (list($tag, $value) = each($vars)) { |
||
1109 | foreach ($vars as $tag => $value) { |
||
1110 | $current = str_replace($tag, $value, $current); |
||
1111 | } |
||
1112 | |||
1113 | // and the dependent Templates |
||
1114 | $this->parseDependencies($name, $current, $mode); |
||
1115 | |||
1116 | // append this parsed to the repetition |
||
1117 | $temp .= $current; |
||
1118 | } |
||
1119 | |||
1120 | // after parsing repetitions, parse the Global Vars |
||
1121 | $this->parseGlobals($name, $temp); |
||
1122 | |||
1123 | // Strip unsused vars |
||
1124 | $this->stripUnusedVars($name, $temp); |
||
1125 | |||
1126 | View Code Duplication | if ($mode === 'a') { |
|
1127 | $this->parsed_templates[$name] .= $temp; |
||
1128 | } elseif ($mode === 'w') { |
||
1129 | $this->parsed_templates[$name] = $temp; |
||
1130 | } |
||
1131 | } |
||
1132 | |||
1133 | /** |
||
1134 | * get variables for a template |
||
1135 | * if the templates uses the attribute 'varscope' these vars will be fetched, too |
||
1136 | * |
||
1137 | * @access private |
||
1138 | * @param string $template name of the template |
||
1139 | * @return array $vars array containign vars |
||
1140 | */ |
||
1141 | public function getVars($template) |
||
1142 | { |
||
1143 | $vars = array(); |
||
1144 | // parse all vars |
||
1145 | if (is_array($this->variables[$template])) { |
||
1146 | // Pointer im Array auf 0 setzen |
||
1147 | reset($this->variables[$template]); |
||
1148 | |||
1149 | // while (list($variable, $value) = each($this->variables[$template])) { |
||
1150 | foreach ($this->variables[$template] as $variable => $value) { |
||
1151 | $tag = $this->tag_start . $variable . $this->tag_end; |
||
1152 | |||
1153 | // if the variable is an array, use the index |
||
1154 | if (is_array($value)) { |
||
1155 | $value = $value[$this->iteration[$template]]; |
||
1156 | } |
||
1157 | |||
1158 | $vars[$tag] = $value; |
||
1159 | } |
||
1160 | } |
||
1161 | |||
1162 | if ($scope = strtoupper($this->getAttribute($template, 'varscope'))) { |
||
1163 | $parentVars = $this->getVars($scope); |
||
1164 | reset($parentVars); |
||
1165 | // while (list($var, $value) = each($parentVars)) { |
||
1166 | foreach ($parentVars as $var => $value) { |
||
1167 | if (!$vars[$var]) { |
||
1168 | $vars[$var] = $value; |
||
1169 | } |
||
1170 | } |
||
1171 | } |
||
1172 | reset($vars); |
||
1173 | |||
1174 | return $vars; |
||
1175 | } |
||
1176 | |||
1177 | /** |
||
1178 | * parses the global variables in a template |
||
1179 | * |
||
1180 | * global variables are valid in all templates |
||
1181 | * |
||
1182 | * @param string $name name of the template |
||
1183 | * @param string &$temp content of the parsed Template |
||
1184 | * @access private |
||
1185 | * @see parseTemplate(), addGlobalVar(), addGlobalVars() |
||
1186 | */ |
||
1187 | public function parseGlobals($name, &$temp) |
||
1188 | { |
||
1189 | $name = strtoupper($name); |
||
1190 | |||
1191 | // check, if globals exist |
||
1192 | if (is_array($this->globals)) { |
||
1193 | reset($this->globals); |
||
1194 | |||
1195 | // while (list($variable, $value) = each($this->globals)) { |
||
1196 | foreach ($this->globals as $variable => $value) { |
||
1197 | $tag = $this->tag_start . $variable . $this->tag_end; |
||
1198 | $temp = str_replace($tag, $value, $temp); |
||
1199 | } |
||
1200 | } |
||
1201 | } |
||
1202 | |||
1203 | /** |
||
1204 | * handles unset variables |
||
1205 | * |
||
1206 | * either strips, comments, replaces or ignores them, depending on the unusedvars attribute |
||
1207 | * |
||
1208 | * @param string $name name of the template |
||
1209 | * @param string &$template content of the parsed Template |
||
1210 | * @access private |
||
1211 | * @see setAttribute() |
||
1212 | */ |
||
1213 | public function stripUnusedVars($name, &$template) |
||
1214 | { |
||
1215 | switch ($this->getAttribute($name, 'unusedvars')) { |
||
1216 | View Code Duplication | case 'comment': |
|
1217 | $template = preg_replace('/(' . $this->tag_start . '[^a-z{}]+' . $this->tag_end . ')/', "<!-- \\1 -->", $template); |
||
1218 | break; |
||
1219 | View Code Duplication | case 'strip': |
|
1220 | $template = preg_replace('/(' . $this->tag_start . '[^a-z{}]+' . $this->tag_end . ')/', '', $template); |
||
1221 | break; |
||
1222 | View Code Duplication | case 'nbsp': |
|
1223 | $template = preg_replace('/(' . $this->tag_start . '[^a-z{}]+' . $this->tag_end . ')/', ' ', $template); |
||
1224 | break; |
||
1225 | case 'ignore': |
||
1226 | break; |
||
1227 | View Code Duplication | default: |
|
1228 | $template = preg_replace('/(' . $this->tag_start . '[^a-z{}]+' . $this->tag_end . ')/', $this->getAttribute($name, 'unusedvars'), $template); |
||
1229 | break; |
||
1230 | } |
||
1231 | } |
||
1232 | |||
1233 | /** |
||
1234 | * parses dependencies of a template |
||
1235 | * |
||
1236 | * parses child templates of a template and inserts their content |
||
1237 | * |
||
1238 | * @param string $name name of the template |
||
1239 | * @param string &$temp content of the parsed Template |
||
1240 | * @param string $mode |
||
1241 | * @access private |
||
1242 | * @see addDependency() |
||
1243 | */ |
||
1244 | public function parseDependencies($name, &$temp, $mode = 'w') |
||
1245 | { |
||
1246 | $name = strtoupper($name); |
||
1247 | if (is_array($this->dependencies[$name])) { |
||
1248 | for ($i = 0, $iMax = count($this->dependencies[$name]); $i < $iMax; ++$i) { |
||
1249 | $type = $this->getAttribute(strtoupper($this->dependencies[$name][$i]), 'type'); |
||
1250 | |||
1251 | // Templates placeholders have the prefix TMPL: |
||
1252 | $tag = $this->tag_start . 'TMPL:' . $this->dependencies[$name][$i] . $this->tag_end; |
||
1253 | // Get the parsed child template and replace it |
||
1254 | $temp = str_replace($tag, $this->getParsedTemplate($this->dependencies[$name][$i]), $temp); |
||
1255 | |||
1256 | if (($type == patTEMPLATE_TYPE_CONDITION || $type == patTEMPLATE_TYPE_SIMPLECONDITION) |
||
1257 | && $mode === 'w' |
||
1258 | ) { |
||
1259 | unset($this->parsed_templates[$this->dependencies[$name][$i]]); |
||
1260 | } |
||
1261 | } |
||
1262 | } |
||
1263 | } |
||
1264 | |||
1265 | /** |
||
1266 | * returns a parsed Template |
||
1267 | * |
||
1268 | * If the template already has been parsed, it just returns the parsed template. |
||
1269 | * If the template has not been loaded, it will be loaded. |
||
1270 | * |
||
1271 | * @param string $name name of the template |
||
1272 | * @return string $content Content of the parsed template |
||
1273 | * @access public |
||
1274 | * @see displayParsedTemplate() |
||
1275 | */ |
||
1276 | public function getParsedTemplate($name = '') |
||
1277 | { |
||
1278 | $name = strtoupper($name); |
||
1279 | |||
1280 | // if a name was given, parse only this template |
||
1281 | if ($name !== '') { |
||
1282 | // check, wther template was disabled |
||
1283 | if ($this->getAttribute($name, 'visibility') === 'hidden') { |
||
1284 | return false; |
||
1285 | } |
||
1286 | |||
1287 | // check, if the template has already been parsed => just return it |
||
1288 | if (!empty($this->parsed_templates[$name])) { |
||
1289 | return $this->parsed_templates[$name]; |
||
1290 | } |
||
1291 | |||
1292 | // Check, if the template has been loaded, if not, load it |
||
1293 | if (empty($this->plain_templates[$name])) { |
||
1294 | $this->loadTemplate($name); |
||
1295 | } |
||
1296 | |||
1297 | // Template is loaded, but not parsed then parse it! |
||
1298 | $this->parseTemplate($name); |
||
1299 | |||
1300 | // And return the parsed template |
||
1301 | return $this->parsed_templates[$name]; |
||
1302 | } // No name given |
||
1303 | else { |
||
1304 | // The template uses dependencies, then start with the root template |
||
1305 | if ($this->uses_dependencies) { |
||
1306 | return $this->getParsedTemplate($this->templates[0]); |
||
1307 | } // Only one template => parse and return it |
||
1308 | elseif ($this->cnt_templates == 1) { |
||
1309 | return $this->getParsedTemplate($this->templates[0]); |
||
1310 | } // No dependencies, but more than one => return all parsed templates in an array |
||
1311 | View Code Duplication | else { |
|
1312 | for ($i = 0; $i < $this->cnt_templates; ++$i) { |
||
1313 | $arr[$this->templates[$i]] = $this->getParsedTemplate($this->templates[$i]); |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$arr was never initialized. Although not strictly required by PHP, it is generally a good practice to add $arr = array(); before regardless.
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code. Let’s take a look at an example: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop. ![]() |
|||
1314 | } |
||
1315 | |||
1316 | return $arr; |
||
1317 | } |
||
1318 | } |
||
1319 | } |
||
1320 | |||
1321 | /** |
||
1322 | * displays a parsed Template |
||
1323 | * |
||
1324 | * If the template has not been loaded, it will be loaded. |
||
1325 | * |
||
1326 | * @param string $name name of the template |
||
1327 | * @access public |
||
1328 | * @see getParsedTemplate() |
||
1329 | */ |
||
1330 | public function displayParsedTemplate($name = '') |
||
1331 | { |
||
1332 | $name = strtoupper($name); |
||
1333 | |||
1334 | // if a name was given, parse and display it |
||
1335 | if ($name) { |
||
1336 | echo $this->getParsedTemplate($name); |
||
1337 | } // No name was given, display them all! |
||
1338 | else { |
||
1339 | // if the template uses dependencies, start with the root template |
||
1340 | if ($this->uses_dependencies) { |
||
1341 | echo $this->getParsedTemplate($this->templates[0]); |
||
1342 | } // Only one template => parse and return it |
||
1343 | elseif ($this->cnt_templates == 1) { |
||
1344 | echo $this->getParsedTemplate($this->templates[0]); |
||
1345 | } // parse and display them all |
||
1346 | View Code Duplication | else { |
|
1347 | $templates = $this->getParsedTemplate(); |
||
1348 | for ($i = 0; $i < $this->cnt_templates; ++$i) { |
||
1349 | echo $templates[$this->templates[$i]]; |
||
1350 | } |
||
1351 | } |
||
1352 | } |
||
1353 | } |
||
1354 | |||
1355 | /** |
||
1356 | * returns an unparsed Template |
||
1357 | * |
||
1358 | * If the template has not been loaded, it will be loaded. |
||
1359 | * |
||
1360 | * @param string $name name of the template |
||
1361 | * @access private |
||
1362 | * @deprecated 2.4 2001/11/05 |
||
1363 | * @return string $content Unparsed content of the template |
||
1364 | * @see getPlainSubTemplate(), displayPlainTemplate() |
||
1365 | */ |
||
1366 | public function getPlainTemplate($name) |
||
1367 | { |
||
1368 | $name = strtoupper($name); |
||
1369 | |||
1370 | // check, wether the template is already loaded |
||
1371 | if (empty($this->plain_templates[$name])) { |
||
1372 | $this->loadTemplate($name); |
||
1373 | } |
||
1374 | |||
1375 | // return it |
||
1376 | return $this->plain_templates[$name]; |
||
1377 | } |
||
1378 | |||
1379 | /** |
||
1380 | * returns an unparsed Subtemplate |
||
1381 | * |
||
1382 | * The template of the template has to be set |
||
1383 | * |
||
1384 | * @param string $name name of the template |
||
1385 | * @param string $sub condition for the subtemplate |
||
1386 | * @access private |
||
1387 | * @deprecated 2.4 2001/11/05 |
||
1388 | * @return string $content Unparsed content of the template |
||
1389 | * @see getPlainTemplate(), displayPlainTemplate() |
||
1390 | */ |
||
1391 | public function getPlainSubTemplate($name, $sub) |
||
1392 | { |
||
1393 | $name = strtoupper($name); |
||
1394 | |||
1395 | return $this->plain_templates[$name][$sub]; |
||
1396 | } |
||
1397 | |||
1398 | /** |
||
1399 | * displays an unparsed Template |
||
1400 | * |
||
1401 | * If the template has not been loaded, it will be loaded. |
||
1402 | * |
||
1403 | * @param string $name name of the template |
||
1404 | * @access private |
||
1405 | * @deprecated 2.4 2001/11/05 |
||
1406 | * @see getPlainTemplate(), getPlainSubTemplate() |
||
1407 | */ |
||
1408 | public function displayPlainTemplate($name) |
||
1409 | { |
||
1410 | $name = strtoupper($name); |
||
1411 | |||
1412 | echo $this->getPlainTemplate($name); |
||
1413 | } |
||
1414 | |||
1415 | /** |
||
1416 | * clears a parsed Template |
||
1417 | * |
||
1418 | * parsed Content, variables and the loop attribute are cleared |
||
1419 | * |
||
1420 | * @param string $name name of the template |
||
1421 | * @access public |
||
1422 | */ |
||
1423 | public function clearTemplate($name) |
||
1424 | { |
||
1425 | $name = strtoupper($name); |
||
1426 | |||
1427 | unset($this->parsed_templates[$name], $this->variables[$name]); |
||
1428 | $this->clearAttribute($name, 'loop'); |
||
1429 | } |
||
1430 | |||
1431 | /** |
||
1432 | * clears all templates |
||
1433 | * |
||
1434 | * @access public |
||
1435 | */ |
||
1436 | public function clearAllTemplates() |
||
1437 | { |
||
1438 | for ($i = 0, $iMax = count($this->templates); $i < $iMax; ++$i) { |
||
1439 | $this->clearTemplate($this->templates[$i]); |
||
1440 | } |
||
1441 | } |
||
1442 | |||
1443 | /** |
||
1444 | * parsed attributes from a string |
||
1445 | * |
||
1446 | * used for parsing <patTemplate> Tags |
||
1447 | * |
||
1448 | * @param string $string string containing the attributes |
||
1449 | * @return array $array assotiative array, containing all attributes |
||
1450 | * @access private |
||
1451 | */ |
||
1452 | public function parseAttributes($string) |
||
1453 | { |
||
1454 | // Check for trailing slash, if tag was an empty XML Tag |
||
1455 | if (substr($string, -1) === '/') { |
||
1456 | $string = substr($string, 0, strlen($string) - 1); |
||
1457 | } |
||
1458 | |||
1459 | $pairs = explode(' ', $string); |
||
1460 | for ($i = 0, $iMax = count($pairs); $i < $iMax; ++$i) { |
||
1461 | $pair = explode('=', trim(str_replace('"', '', $pairs[$i]))); |
||
1462 | |||
1463 | if (count($pair) == 1) { |
||
1464 | $pair[1] = 'yes'; |
||
1465 | } |
||
1466 | |||
1467 | $attributes[strtolower($pair[0])] = $pair[1]; |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$attributes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attributes = array(); before regardless.
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code. Let’s take a look at an example: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop. ![]() |
|||
1468 | } |
||
1469 | |||
1470 | return $attributes; |
||
1471 | } |
||
1472 | |||
1473 | /** |
||
1474 | * returns the plain content of a template |
||
1475 | * |
||
1476 | * return value depends on iteration value |
||
1477 | * |
||
1478 | * @param string $name name of the template |
||
1479 | * @return string $content plain content of the template |
||
1480 | * @internal param int $index iteration number |
||
1481 | * @access private |
||
1482 | */ |
||
1483 | public function getTemplateContent($name) |
||
1484 | { |
||
1485 | $name = strtoupper($name); |
||
1486 | $index = $this->iteration[$name]; |
||
1487 | |||
1488 | // Is it a standard, oddeven or condition template |
||
1489 | switch ($this->getAttribute($name, 'type')) { |
||
1490 | case patTEMPLATE_TYPE_ODDEVEN: |
||
1491 | $sub = ($index + 1) % 2 == 0 ? 'even' : 'odd'; |
||
1492 | |||
1493 | return $this->plain_templates[$name][$sub]; |
||
1494 | break; |
||
1495 | case patTEMPLATE_TYPE_CONDITION: |
||
1496 | $conditionval = $this->getVar($name, $this->conditionvars[$name]); |
||
1497 | |||
1498 | // check, if conditionvalue is empty |
||
1499 | if (!isset($conditionval) || (is_string($conditionval) && $conditionval === '') |
||
1500 | || $conditionval === false |
||
1501 | ) { |
||
1502 | $conditionval = 'empty'; |
||
1503 | } |
||
1504 | |||
1505 | // check if condition was specified, otherwise use default |
||
1506 | $condition_found = false; |
||
1507 | for ($i = 0; $i < $this->cnt_subtemplates[$name]; ++$i) { |
||
1508 | if ($this->subtemplate_conditions[$name][$i] == $conditionval) { |
||
1509 | $condition_found = true; |
||
1510 | break; |
||
1511 | } |
||
1512 | } |
||
1513 | if (!$condition_found) { |
||
1514 | $conditionval = 'default'; |
||
1515 | } |
||
1516 | |||
1517 | return $this->plain_templates[$name][$conditionval]; |
||
1518 | break; |
||
1519 | |||
1520 | case patTEMPLATE_TYPE_SIMPLECONDITION: |
||
1521 | // get required vars |
||
1522 | $requiredVars = $this->getAttribute($name, 'requiredvars'); |
||
1523 | |||
1524 | // check, if all are set |
||
1525 | for ($i = 0, $iMax = count($requiredVars); $i < $iMax; ++$i) { |
||
1526 | if (!$this->getVar($name, $requiredVars[$i])) { |
||
1527 | return ''; |
||
1528 | } |
||
1529 | } |
||
1530 | |||
1531 | return $this->plain_templates[$name]; |
||
1532 | break; |
||
1533 | |||
1534 | default: |
||
1535 | return $this->plain_templates[$name]; |
||
1536 | break; |
||
1537 | } |
||
1538 | } |
||
1539 | |||
1540 | /** |
||
1541 | * get the value of a variable |
||
1542 | * |
||
1543 | * @param string $template name of the template |
||
1544 | * @param string $var name of the variable |
||
1545 | * @return mixed $value value of the variable / false if it doesn't exist |
||
1546 | * @internal param int $index no of repetition |
||
1547 | */ |
||
1548 | public function getVar($template, $var) |
||
1549 | { |
||
1550 | // should the var from a different template be used |
||
1551 | if (stristr($var, '.')) { |
||
1552 | list($template, $var) = explode('.', $var); |
||
1553 | } |
||
1554 | |||
1555 | $var = strtoupper($var); |
||
1556 | $index = $this->iteration[$template]; |
||
1557 | if ($scope = $this->getAttribute($template, 'varscope')) { |
||
1558 | $val = $this->getVar(strtoupper($scope), $var); |
||
1559 | } else { |
||
1560 | $val = $this->variables[$template][$var]; |
||
1561 | } |
||
1562 | |||
1563 | // check, if global var should be used |
||
1564 | if (!$val && $this->getAttribute($template, 'useglobals') === 'yes') { |
||
1565 | $val = $this->globals[$var]; |
||
1566 | } |
||
1567 | |||
1568 | if (is_array($val)) { |
||
1569 | $val = $val[$index]; |
||
1570 | } |
||
1571 | |||
1572 | return $val; |
||
1573 | } |
||
1574 | |||
1575 | /** |
||
1576 | * displays useful information about all templates |
||
1577 | * |
||
1578 | * returns content, variables, attributes and unused variables |
||
1579 | * |
||
1580 | * @access public |
||
1581 | */ |
||
1582 | public function dump() |
||
1583 | { |
||
1584 | echo "<style type=\"text/css\">\n"; |
||
1585 | echo ".text {font-family: Verdana, Arial, sans-serif; font-size: 10px; color: #000000}\n"; |
||
1586 | echo ".mid {font-family: Verdana, Arial, sans-serif; font-size: 12px; color: #000000}\n"; |
||
1587 | echo ".head {font-family: Verdana, Arial, sans-serif; font-size: 16px; font-weight: bold; color: #000000}\n"; |
||
1588 | echo "</style>\n"; |
||
1589 | |||
1590 | echo "<table border=\"1\" cellpadding=\"2\" cellspacing=\"1\" bgcolor=\"White\" >\n"; |
||
1591 | |||
1592 | for ($i = 0; $i < $this->cnt_templates; ++$i) { |
||
1593 | $name = $this->templates[$i]; |
||
1594 | |||
1595 | // Template name |
||
1596 | echo " <tr bgcolor=\"#EEEEEE\">\n"; |
||
1597 | echo " <td class=\"head\" valign=\"top\">Template</td>\n"; |
||
1598 | echo ' <td class="head" valign="top">' . $name . "</td>\n"; |
||
1599 | echo " </tr>\n"; |
||
1600 | |||
1601 | $fname = $this->basedir !== '' ? $this->basedir . '/' . $this->filenames[$name] : $this->filenames[$name]; |
||
1602 | |||
1603 | // Template file |
||
1604 | echo " <tr>\n"; |
||
1605 | echo " <td class=\"mid\" valign=\"top\"><b>Filename</b></td>\n"; |
||
1606 | echo ' <td class="text" valign="top">' . $fname . "</td>\n"; |
||
1607 | echo " </tr>\n"; |
||
1608 | |||
1609 | // Template Attributes |
||
1610 | |||
1611 | echo " <tr>\n"; |
||
1612 | echo " <td class=\"mid\" valign=\"top\"><b>Attributes</b></td>\n"; |
||
1613 | echo " <td class=\"text\" valign=\"top\">\n"; |
||
1614 | |||
1615 | echo " <table border=\"0\" cellpadding=\"0\" cellspacing=\"1\">\n"; |
||
1616 | |||
1617 | // Display all Attributes in table |
||
1618 | // while (list($key, $value) = each($this->attributes[$name])) { |
||
1619 | foreach ($this->attributes[$name] as $key => $value) { |
||
1620 | echo " <tr>\n"; |
||
1621 | echo ' <td class="text"><b>' . $key . "</b></td>\n"; |
||
1622 | echo " <td class=\"text\"> : </td>\n"; |
||
1623 | echo ' <td class="text">' . $value . "</td>\n"; |
||
1624 | echo " </tr>\n"; |
||
1625 | } |
||
1626 | echo " </table>\n"; |
||
1627 | |||
1628 | echo " </td>\n"; |
||
1629 | echo " </tr>\n"; |
||
1630 | |||
1631 | echo " <tr>\n"; |
||
1632 | echo " <td class=\"text\" valign=\"top\" colspan=\"2\"> </td>\n"; |
||
1633 | echo " </tr>\n"; |
||
1634 | |||
1635 | if ($this->cnt_subtemplates[$name] > 0) { |
||
1636 | // display template Data |
||
1637 | echo " <tr>\n"; |
||
1638 | echo " <td class=\"mid\" valign=\"top\"><b>Template Data:</b></td>\n"; |
||
1639 | echo ' <td class="text" valign="top">Amount of subtemplates: ' . $this->cnt_subtemplates[$name] . "</td>\n"; |
||
1640 | echo " </tr>\n"; |
||
1641 | |||
1642 | // Display all Subtemplates |
||
1643 | for ($j = 0; $j < $this->cnt_subtemplates[$name]; ++$j) { |
||
1644 | $condition = $this->subtemplate_conditions[$name][$j]; |
||
1645 | echo " <tr bgcolor=\"#DDDDDD\">\n"; |
||
1646 | echo " <td class=\"text\"><b>Condition</b></td>\n"; |
||
1647 | echo ' <td class="text">' . $condition . "</td>\n"; |
||
1648 | echo " </tr>\n"; |
||
1649 | |||
1650 | echo " <tr>\n"; |
||
1651 | echo " <td class=\"text\" valign=\"top\"><b>Template Data</b></td>\n"; |
||
1652 | echo ' <td class="text" valign="top"><pre>' . htmlspecialchars($this->getPlainSubTemplate($name, $condition)) . "</pre></td>\n"; |
||
1653 | echo " </tr>\n"; |
||
1654 | |||
1655 | unset($matches); |
||
1656 | // Check for unset variables |
||
1657 | preg_match_all($this->regex_get_all_vars, $this->getPlainSubTemplate($name, $condition), $matches); |
||
1658 | |||
1659 | // Empty the array that stores the unused Vars |
||
1660 | unset($unused); |
||
1661 | |||
1662 | View Code Duplication | if (is_array($matches[0]) && count($matches[0]) > 0) { |
|
1663 | // Check, wether variable is unused |
||
1664 | for ($k = 0, $kMax = count($matches[0]); $k <= $kMax; ++$k) { |
||
1665 | if ($matches[1][$k] !== '' && !isset($this->variables[$name][$matches[1][$k]]) |
||
1666 | && !isset($this->globals[$matches[1][$k]]) |
||
1667 | ) { |
||
1668 | $unused[] = $matches[0][$k]; |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$unused was never initialized. Although not strictly required by PHP, it is generally a good practice to add $unused = array(); before regardless.
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code. Let’s take a look at an example: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop. ![]() |
|||
1669 | } |
||
1670 | } |
||
1671 | } |
||
1672 | |||
1673 | View Code Duplication | if (is_array($unused) && count($unused) > 0) { |
|
1674 | echo " <tr>\n"; |
||
1675 | echo " <td class=\"mid\" valign=\"top\"><b>Unused variables</b></td>\n"; |
||
1676 | echo " <td class=\"text\" valign=\"top\">\n"; |
||
1677 | |||
1678 | echo " <table border=\"0\" cellpadding=\"0\" cellspacing=\"1\">\n"; |
||
1679 | |||
1680 | // Display all Variables in table |
||
1681 | for ($k = 0, $kMax = count($unused); $k <= $kMax; ++$k) { |
||
1682 | { |
||
1683 | echo " <tr>\n"; |
||
1684 | echo ' <td class="text">' . $unused[$k] . "</td>\n"; |
||
1685 | echo " </tr>\n"; |
||
1686 | } |
||
1687 | } |
||
1688 | echo " </table>\n"; |
||
1689 | |||
1690 | echo " </td>\n"; |
||
1691 | echo " </tr>\n"; |
||
1692 | } |
||
1693 | |||
1694 | echo " <tr>\n"; |
||
1695 | echo " <td class=\"text\" colspan=\"2\"> </td>\n"; |
||
1696 | echo " </tr>\n"; |
||
1697 | } |
||
1698 | } else { |
||
1699 | // display template Data |
||
1700 | echo " <tr>\n"; |
||
1701 | echo " <td class=\"mid\" valign=\"top\"><b>Template Data:</b></td>\n"; |
||
1702 | echo " <td class=\"text\" valign=\"top\">\n"; |
||
1703 | echo ' <pre>' . htmlspecialchars($this->getPlainTemplate($name)) . "</pre>\n"; |
||
1704 | echo " </td>\n"; |
||
1705 | echo " </tr>\n"; |
||
1706 | |||
1707 | unset($matches); |
||
1708 | // Check for unset variables |
||
1709 | preg_match_all($this->regex_get_all_vars, $this->getPlainTemplate($name), $matches); |
||
1710 | |||
1711 | // Empty the array that stores the unused Vars |
||
1712 | unset($unused); |
||
1713 | |||
1714 | View Code Duplication | if (is_array($matches[0]) && count($matches[0]) > 0) { |
|
1715 | // Check, wether variable is unused |
||
1716 | for ($k = 0, $kMax = count($matches[0]); $k < $kMax; ++$k) { |
||
1717 | if ($matches[1][$k] !== '' && !isset($this->variables[$name][$matches[1][$k]]) |
||
1718 | && !isset($this->globals[$matches[1][$k]]) |
||
1719 | ) { |
||
1720 | $unused[] = $matches[0][$k]; |
||
1721 | } |
||
1722 | } |
||
1723 | } |
||
1724 | |||
1725 | View Code Duplication | if (is_array($unused) && count($unused) > 0) { |
|
1726 | echo " <tr>\n"; |
||
1727 | echo " <td class=\"mid\" valign=\"top\"><b>Unused variables</b></td>\n"; |
||
1728 | echo " <td class=\"text\" valign=\"top\">\n"; |
||
1729 | |||
1730 | echo " <table border=\"0\" cellpadding=\"0\" cellspacing=\"1\">\n"; |
||
1731 | |||
1732 | // Display all Variables in table |
||
1733 | for ($k = 0, $kMax = count($unused); $k <= $kMax; ++$k) { |
||
1734 | { |
||
1735 | echo " <tr>\n"; |
||
1736 | echo ' <td class="text">' . $unused[$k] . "</td>\n"; |
||
1737 | echo " </tr>\n"; |
||
1738 | } |
||
1739 | } |
||
1740 | echo " </table>\n"; |
||
1741 | |||
1742 | echo " </td>\n"; |
||
1743 | echo " </tr>\n"; |
||
1744 | } |
||
1745 | } |
||
1746 | |||
1747 | // Display Variables |
||
1748 | |||
1749 | if (is_array($this->variables[$name]) && count($this->variables[$name]) > 0) { |
||
1750 | reset($this->variables[$name]); |
||
1751 | |||
1752 | echo " <tr>\n"; |
||
1753 | echo " <td class=\"mid\" valign=\"top\"><b>Variables</b></td>\n"; |
||
1754 | echo " <td class=\"text\" valign=\"top\">\n"; |
||
1755 | |||
1756 | echo " <table border=\"0\" cellpadding=\"0\" cellspacing=\"1\">\n"; |
||
1757 | |||
1758 | // Display all Variables in table |
||
1759 | // while (list($key, $value) = each($this->variables[$name])) { |
||
1760 | foreach ($this->variables[$name] as $key => $value) { |
||
1761 | if (is_array($value)) { |
||
1762 | $value = implode(', ', $value); |
||
1763 | } |
||
1764 | |||
1765 | echo " <tr>\n"; |
||
1766 | echo ' <td class="text"><b>' . $this->tag_start . $key . $this->tag_end . "</b></td>\n"; |
||
1767 | echo " <td class=\"text\"> => </td>\n"; |
||
1768 | echo ' <td class="text">' . $value . "</td>\n"; |
||
1769 | echo " </tr>\n"; |
||
1770 | } |
||
1771 | echo " </table>\n"; |
||
1772 | |||
1773 | echo " </td>\n"; |
||
1774 | echo " </tr>\n"; |
||
1775 | } |
||
1776 | |||
1777 | echo " <tr>\n"; |
||
1778 | echo " <td class=\"text\" valign=\"top\" colspan=\"2\"> </td>\n"; |
||
1779 | echo " </tr>\n"; |
||
1780 | } |
||
1781 | echo '</table>'; |
||
1782 | } |
||
1783 | } |
||
1784 | } |
||
1785 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.