More relaxed check (only first X characters)
[core.git] / inc / classes / main / template / class_BaseTemplateEngine.php
1 <?php
2 /**
3  * A generic template engine
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class BaseTemplateEngine extends BaseFrameworkSystem {
25         /**
26          * The local path name where all templates and sub folders for special
27          * templates are stored. We will internally determine the language plus
28          * "html" for web templates or "emails" for email templates
29          */
30         private $templateBasePath = '';
31
32         /**
33          * Template type
34          */
35         private $templateType = 'html';
36
37         /**
38          * The extension for web and email templates (not compiled templates)
39          */
40         private $templateExtension = '.tpl';
41
42         /**
43          * The extension for code templates (not compiled templates)
44          */
45         private $codeExtension = '.ctp';
46
47         /**
48          * Path relative to $templateBasePath and language code for compiled code-templates
49          */
50         private $compileOutputPath = 'templates/_compiled/';
51
52         /**
53          * The path name for all templates
54          */
55         private $genericBasePath = 'templates/';
56
57         /**
58          * The raw (maybe uncompiled) template
59          */
60         private $rawTemplateData = '';
61
62         /**
63          * Template data with compiled-in variables
64          */
65         private $compiledData = '';
66
67         /**
68          * The last loaded template's FQFN for debugging the engine
69          */
70         private $lastTemplate = '';
71
72         /**
73          * The variable stack for the templates
74          */
75         private $varStack = array();
76
77         /**
78          * Loaded templates for recursive protection and detection
79          */
80         private $loadedTemplates = array();
81
82         /**
83          * Compiled templates for recursive protection and detection
84          */
85         private $compiledTemplates = array();
86
87         /**
88          * Loaded raw template data
89          */
90         private $loadedRawData = null;
91
92         /**
93          * Raw templates which are linked in code templates
94          */
95         private $rawTemplates = null;
96
97         /**
98          * A regular expression for variable=value pairs
99          */
100         private $regExpVarValue = '/([\w_]+)(="([^"]*)"|=([\w_]+))?/';
101
102         /**
103          * A regular expression for filtering out code tags
104          *
105          * E.g.: {?template:variable=value;var2=value2;[...]?}
106          */
107         private $regExpCodeTags = '/\{\?([a-z_]+)(:("[^"]+"|[^?}]+)+)?\?\}/';
108
109         /**
110          * Loaded helpers
111          */
112         private $helpers = array();
113
114         /**
115          * Current variable group
116          */
117         private $currGroup = 'general';
118
119         /**
120          * All template groups except "general"
121          */
122         private $varGroups = array();
123
124         /**
125          * Code begin
126          */
127         private $codeBegin = '<?php';
128
129         /**
130          * Code end
131          */
132         private $codeEnd = '?>';
133
134         /**
135          * Language support is enabled by default
136          */
137         private $languageSupport = true;
138
139         /**
140          * XML compacting is disabled by default
141          */
142         private $xmlCompacting = false;
143
144         // Exception codes for the template engine
145         const EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED   = 0x110;
146         const EXCEPTION_TEMPLATE_CONTAINS_INVALID_VAR = 0x111;
147         const EXCEPTION_INVALID_VIEW_HELPER           = 0x112;
148         const EXCEPTION_VARIABLE_IS_MISSING           = 0x113;
149
150         /**
151          * Protected constructor
152          *
153          * @param       $className      Name of the class
154          * @return      void
155          */
156         protected function __construct ($className) {
157                 // Call parent constructor
158                 parent::__construct($className);
159         }
160
161         /**
162          * Search for a variable in the stack
163          *
164          * @param       $var    The variable we are looking for
165          * @param       $stack  Optional variable stack to look in
166          * @return      $index  FALSE means not found, >=0 means found on a specific index
167          */
168         private function getVariableIndex ($var, $stack = null) {
169                 // First everything is not found
170                 $found = false;
171
172                 // If the stack is null, use the current group
173                 if (is_null($stack)) $stack = $this->currGroup;
174
175                 // Is the group there?
176                 if ($this->isVarStackSet($stack)) {
177                         // Now search for it
178                         foreach ($this->getVarStack($stack) as $index => $currEntry) {
179                                 //* DEBUG: */ echo __METHOD__.":currGroup={$stack},idx={$index},currEntry={$currEntry['name']},var={$var}<br />\n";
180                                 // Is the entry found?
181                                 if ($currEntry['name'] == $var) {
182                                         // Found!
183                                         //* DEBUG: */ echo __METHOD__.":FOUND!<br />\n";
184                                         $found = $index;
185                                         break;
186                                 } // END - if
187                         } // END - foreach
188                 } // END - if
189
190                 // Return the current position
191                 return $found;
192         }
193
194         /**
195          * Checks wether the given variable stack is set
196          *
197          * @param       $stack  Variable stack to check
198          * @return      $isSet  Wether the given variable stack is set
199          */
200         protected final function isVarStackSet ($stack) {
201                 // Check it
202                 $isSet = isset($this->varStack[$stack]);
203
204                 // Return result
205                 return $isSet;
206         }
207
208         /**
209          * Getter for given variable stack
210          *
211          * @param       $stack          Variable stack to check
212          * @return      $varStack       Found variable stack
213          */
214         public final function getVarStack ($stack) {
215                 return $this->varStack[$stack];
216         }
217
218         /**
219          * Setter for given variable stack
220          *
221          * @param       $stack          Variable stack to check
222          * @param       $varStack       Variable stack to check
223          * @return      void
224          */
225         protected final function setVarStack ($stack, array $varStack) {
226                 $this->varStack[$stack]  = $varStack;
227         }
228
229         /**
230          * Return a content of a variable or null if not found
231          *
232          * @param       $var            The variable we are looking for
233          * @param       $stack          Optional variable stack to look in
234          * @return      $content        Content of the variable or null if not found
235          */
236         protected function readVariable ($var, $stack = null) {
237                 // First everything is not found
238                 $content = null;
239
240                 // If the stack is null, use the current group
241                 if (is_null($stack)) $stack = $this->currGroup;
242
243                 // Get variable index
244                 $found = $this->getVariableIndex($var, $stack);
245
246                 // Is the variable found?
247                 if ($found !== false) {
248                         // Read it
249                         $content = $this->getVariableValue($stack, $found);
250                 } // END - if
251
252                 // Return the current position
253                 //* DEBUG: */ echo __METHOD__.": group=".$stack.",var=".$var.", content[".gettype($content)."]=".$content."<br />\n";
254                 return $content;
255         }
256
257         /**
258          * Add a variable to the stack
259          *
260          * @param       $var    The variable we are looking for
261          * @param       $value  The value we want to store in the variable
262          * @return      void
263          */
264         private function addVariable ($var, $value) {
265                 // Set general variable group
266                 $this->setVariableGroup('general');
267
268                 // Add it to the stack
269                 $this->addGroupVariable($var, $value);
270         }
271
272         /**
273          * Returns all variables of current group or empty array
274          *
275          * @return      $result         Wether array of found variables or empty array
276          */
277         private function readCurrentGroup () {
278                 // Default is not found
279                 $result = array();
280
281                 // Is the group there?
282                 if ($this->isVarStackSet($this->currGroup)) {
283                         // Then use it
284                         $result = $this->getVarStack($this->currGroup);
285                 } // END - if
286
287                 // Return result
288                 return $result;
289         }
290
291         /**
292          * Settter for variable group
293          *
294          * @param       $groupName      Name of variable group
295          * @param       $add            Wether add this group
296          * @return      void
297          */
298         public function setVariableGroup ($groupName, $add = true) {
299                 // Set group name
300                 //* DEBIG: */ echo __METHOD__.": currGroup=".$groupName."<br />\n";
301                 $this->currGroup = $groupName;
302
303                 // Skip group 'general'
304                 if (($groupName != 'general') && ($add === true)) {
305                         $this->varGroups[$groupName] = 'OK';
306                 } // END - if
307         }
308
309
310         /**
311          * Adds a variable to current group
312          *
313          * @param       $var    Variable to set
314          * @param       $value  Value to store in variable
315          * @return      void
316          */
317         public function addGroupVariable ($var, $value) {
318                 //* DEBUG: */ echo __METHOD__.": group=".$this->currGroup.", var=".$var.", value=".$value."<br />\n";
319
320                 // Get current variables in group
321                 $currVars = $this->readCurrentGroup();
322
323                 // Append our variable
324                 $currVars[] = $this->generateVariableArray($var, $value);
325
326                 // Add it to the stack
327                 $this->setVarStack($this->currGroup, $currVars);
328         }
329
330         /**
331          * Getter for variable value, throws a NoVariableException if the variable is not found
332          *
333          * @param       $varGroup       Variable group to use
334          * @param       $index          Index in variable array
335          * @return      $value          Value to set
336          */
337         private function getVariableValue ($varGroup, $index) {
338                 // Return it
339                 return $this->varStack[$varGroup][$index]['value'];
340         }
341
342         /**
343          * Modify an entry on the stack
344          *
345          * @param       $var    The variable we are looking for
346          * @param       $value  The value we want to store in the variable
347          * @return      void
348          * @throws      NoVariableException     If the given variable is not found
349          */
350         private function modifyVariable ($var, $value) {
351                 // Get index for variable
352                 $index = $this->getVariableIndex($var);
353
354                 // Is the variable set?
355                 if ($index === false) {
356                         // Unset variables cannot be modified
357                         throw new NoVariableException(array($this, $var, $value), self::EXCEPTION_VARIABLE_IS_MISSING);
358                 } // END - if
359
360                 // Then modify it
361                 $this->setVariableValue($this->currGroup, $index, $value);
362         }
363
364         /**
365          * Sets a variable value for given variable group and index
366          *
367          * @param       $varGroup       Variable group to use
368          * @param       $index          Index in variable array
369          * @param       $value          Value to set
370          * @return      void
371          */
372         private function setVariableValue ($varGroup, $index, $value) {
373                 $this->varStack[$varGroup][$index]['value'] = $value;
374         }
375
376         /**
377          * Sets a variable within given group. This method does detect if the
378          * variable is already set. If so, the variable got modified, otherwise
379          * added.
380          *
381          * @param       $varGroup       Variable group to use
382          * @param       $var            Variable to set
383          * @param       $value          Value to set
384          * @return      void
385          */
386         protected function setVariable ($varGroup, $var, $value) {
387                 // Get index for variable
388                 $index = $this->getVariableIndex($var);
389
390                 // Is the variable set?
391                 if ($index === false) {
392                         // Not found, add it
393                         $this->varStack[$varGroup][] = $this->generateVariableArray($var, $value);
394                 } else {
395                         // Then modify it
396                         $this->setVariableValue($this->currGroup, $index, $value);
397                 }
398         }
399
400         /**
401          * "Generates" (better returns) an array for all variables for given
402          * variable/value pay.
403          *
404          * @param       $var            Variable to set
405          * @param       $value          Value to set
406          * @return      $varData        Variable data array
407          */
408         private function generateVariableArray ($var, $value) {
409                 // Generate the temporary array
410                 $varData = array(
411                         'name'  => $var,
412                         'value' => $value
413                 );
414
415                 // And return it
416                 return $varData;
417         }
418
419         /**
420          * Setter for template type. Only 'html', 'emails' and 'compiled' should
421          * be sent here
422          *
423          * @param       $templateType   The current template's type
424          * @return      void
425          */
426         protected final function setTemplateType ($templateType) {
427                 $this->templateType = (string) $templateType;
428         }
429
430         /**
431          * Setter for the last loaded template's FQFN
432          *
433          * @param       $template       The last loaded template
434          * @return      void
435          */
436         private final function setLastTemplate ($template) {
437                 $this->lastTemplate = (string) $template;
438         }
439
440         /**
441          * Getter for the last loaded template's FQFN
442          *
443          * @return      $template       The last loaded template
444          */
445         private final function getLastTemplate () {
446                 return $this->lastTemplate;
447         }
448
449         /**
450          * Setter for base path
451          *
452          * @param               $templateBasePath               The relative base path for all templates
453          * @return      void
454          */
455         public final function setTemplateBasePath ($templateBasePath) {
456                 // And set it
457                 $this->templateBasePath = (string) $templateBasePath;
458         }
459
460         /**
461          * Getter for base path
462          *
463          * @return      $templateBasePath               The relative base path for all templates
464          */
465         public final function getTemplateBasePath () {
466                 // And set it
467                 return $this->templateBasePath;
468         }
469
470         /**
471          * Getter for generic base path
472          *
473          * @return      $templateBasePath               The relative base path for all templates
474          */
475         public final function getGenericBasePath () {
476                 // And set it
477                 return $this->genericBasePath;
478         }
479
480         /**
481          * Setter for template extension
482          *
483          * @param               $templateExtension      The file extension for all uncompiled
484          *                                                      templates
485          * @return      void
486          */
487         public final function setRawTemplateExtension ($templateExtension) {
488                 // And set it
489                 $this->templateExtension = (string) $templateExtension;
490         }
491
492         /**
493          * Setter for code template extension
494          *
495          * @param               $codeExtension          The file extension for all uncompiled
496          *                                                      templates
497          * @return      void
498          */
499         public final function setCodeTemplateExtension ($codeExtension) {
500                 // And set it
501                 $this->codeExtension = (string) $codeExtension;
502         }
503
504         /**
505          * Getter for template extension
506          *
507          * @return      $templateExtension      The file extension for all uncompiled
508          *                                                      templates
509          */
510         public final function getRawTemplateExtension () {
511                 // And set it
512                 return $this->templateExtension;
513         }
514
515         /**
516          * Getter for code-template extension
517          *
518          * @return      $codeExtension          The file extension for all code-
519          *                                                      templates
520          */
521         public final function getCodeTemplateExtension () {
522                 // And set it
523                 return $this->codeExtension;
524         }
525
526         /**
527          * Setter for path of compiled templates
528          *
529          * @param       $compileOutputPath      The local base path for all compiled
530          *                                                              templates
531          * @return      void
532          */
533         public final function setCompileOutputPath ($compileOutputPath) {
534                 // And set it
535                 $this->compileOutputPath = (string) $compileOutputPath;
536         }
537
538         /**
539          * Getter for template type
540          *
541          * @return      $templateType   The current template's type
542          */
543         public final function getTemplateType () {
544                 return $this->templateType;
545         }
546
547         /**
548          * Assign (add) a given variable with a value
549          *
550          * @param       $var    The variable we are looking for
551          * @param       $value  The value we want to store in the variable
552          * @return      void
553          * @throws      EmptyVariableException  If the variable name is left empty
554          */
555         public final function assignVariable ($var, $value) {
556                 // Trim spaces of variable name
557                 $var = trim($var);
558
559                 // Empty variable found?
560                 if (empty($var)) {
561                         // Throw an exception
562                         throw new EmptyVariableException(array($this, 'var'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
563                 } // END - if
564
565                 // First search for the variable if it was already added
566                 $index = $this->getVariableIndex($var);
567
568                 // Was it found?
569                 if ($index === false) {
570                         // Add it to the stack
571                         //* DEBUG: */ echo "ADD: ".$var."<br />\n";
572                         $this->addVariable($var, $value);
573                 } elseif (!empty($value)) {
574                         // Modify the stack entry
575                         //* DEBUG: */ echo "MOD: ".$var."<br />\n";
576                         $this->modifyVariable($var, $value);
577                 }
578         }
579
580         /**
581          * Removes a given variable
582          *
583          * @param       $var    The variable we are looking for
584          * @return      void
585          */
586         public final function removeVariable ($var) {
587                 // First search for the variable if it was already added
588                 $index = $this->getVariableIndex($var);
589
590                 // Was it found?
591                 if ($index !== false) {
592                         // Remove this variable
593                         $this->unsetVariableStackOffset($index);
594                 } // END - if
595         }
596
597         /**
598          * Unsets the given offset in the variable stack
599          *
600          * @param       $index  Index to unset
601          * @return      void
602          */
603         protected final function unsetVariableStackOffset ($index) {
604                 // Is the entry there?
605                 if (!isset($this->varStack[$this->currGroup][$index])) {
606                         // Abort here, we need fixing!
607                         $this->debugInstance();
608                 } // END - if
609
610                 // Remove it
611                 unset($this->varStack[$this->currGroup][$index]);
612         }
613
614         /**
615          * Private setter for raw template data
616          *
617          * @param       $rawTemplateData        The raw data from the template
618          * @return      void
619          */
620         protected final function setRawTemplateData ($rawTemplateData) {
621                 // And store it in this class
622                 //* DEBUG: */ echo __METHOD__.':'.$this->getUniqueId().': '.strlen($rawTemplateData).' Bytes set.<br />\n';
623                 //* DEBUG: */ echo $this->currGroup.' variables: '.count($this->getVarStack($this->currGroup)).', groups='.count($this->varStack).'<br />\n';
624                 $this->rawTemplateData = (string) $rawTemplateData;
625         }
626
627         /**
628          * Getter for raw template data
629          *
630          * @return      $rawTemplateData        The raw data from the template
631          */
632         public final function getRawTemplateData () {
633                 //* DEBUG: */ echo __METHOD__.':'.$this->getUniqueId().': '.strlen($this->rawTemplateData).' Bytes read.<br />\n';
634                 return $this->rawTemplateData;
635         }
636
637         /**
638          * Private setter for compiled templates
639          *
640          * @return      void
641          */
642         private final function setCompiledData ($compiledData) {
643                 // And store it in this class
644                 //* DEBUG: */ echo __METHOD__.':'.$this->getUniqueId().': '.strlen($compiledData).' Bytes set.<br />\n';
645                 $this->compiledData = (string) $compiledData;
646         }
647
648         /**
649          * Getter for compiled templates
650          *
651          * @return      $compiledData   Compiled template data
652          */
653         public final function getCompiledData () {
654                 //* DEBUG: */ echo __METHOD__.':'.$this->getUniqueId().': '.strlen($this->compiledData).' Bytes read.<br />\n';
655                 return $this->compiledData;
656         }
657
658         /**
659          * Private loader for all template types
660          *
661          * @param       $template       The template we shall load
662          * @param       $extOther       An other extension to use
663          * @return      void
664          * @throws      FileIoException If the template was not found
665          */
666         protected function loadTemplate ($template, $extOther = '') {
667                 // Get extension for the template if empty
668                 if (empty($extOther)) {
669                         // None provided, so get the raw one
670                         $ext = $this->getRawTemplateExtension();
671                 } else {
672                         // Then use it!
673                         $ext = (string) $extOther;
674                 }
675
676                 // Is language support enabled?
677                 if ($this->isLanguageSupportEnabled()) {
678                         // Construct the FQFN for the template by honoring the current language
679                         $fqfn = sprintf("%s%s%s%s/%s/%s%s",
680                                 $this->getConfigInstance()->getConfigEntry('base_path'),
681                                 $this->getTemplateBasePath(),
682                                 $this->getGenericBasePath(),
683                                 $this->getLanguageInstance()->getLanguageCode(),
684                                 $this->getTemplateType(),
685                                 (string) $template,
686                                 $ext
687                         );
688                 } else {
689                         // Construct the FQFN for the template without language
690                         $fqfn = sprintf("%s%s%s%s/%s%s",
691                                 $this->getConfigInstance()->getConfigEntry('base_path'),
692                                 $this->getTemplateBasePath(),
693                                 $this->getGenericBasePath(),
694                                 $this->getTemplateType(),
695                                 (string) $template,
696                                 $ext
697                         );
698                 }
699
700                 // First try this
701                 try {
702                         // Load the raw template data
703                         $this->loadRawTemplateData($fqfn);
704                 } catch (FileIoException $e) {
705                         // If we shall load a code-template we need to switch the file extension
706                         if (($this->getTemplateType() != $this->getConfigInstance()->getConfigEntry('web_template_type')) && (empty($extOther))) {
707                                 // Switch over to the code-template extension and try it again
708                                 $ext = $this->getCodeTemplateExtension();
709
710                                 // Try it again...
711                                 $this->loadTemplate($template, $ext);
712                         } else {
713                                 // Throw it again
714                                 throw new FileIoException($fqfn, FrameworkFileInputPointer::EXCEPTION_FILE_NOT_FOUND);
715                         }
716                 }
717
718         }
719
720         /**
721          * A private loader for raw template names
722          *
723          * @param       $fqfn   The full-qualified file name for a template
724          * @return      void
725          */
726         private function loadRawTemplateData ($fqfn) {
727                 // Get a input/output instance from the middleware
728                 $ioInstance = $this->getFileIoInstance();
729
730                 // Some debug code to look on the file which is being loaded
731                 //* DEBUG: */ echo __METHOD__.": FQFN=".$fqfn."<br />\n";
732
733                 // Load the raw template
734                 $rawTemplateData = $ioInstance->loadFileContents($fqfn);
735
736                 // Store the template's contents into this class
737                 $this->setRawTemplateData($rawTemplateData);
738
739                 // Remember the template's FQFN
740                 $this->setLastTemplate($fqfn);
741         }
742
743         /**
744          * Try to assign an extracted template variable as a "content" or 'config'
745          * variable.
746          *
747          * @param       $varName        The variable's name (shall be content orconfig) by
748          *                                              default
749          * @param       $var            The variable we want to assign
750          */
751         private function assignTemplateVariable ($varName, $var) {
752                 // Is it not a config variable?
753                 if ($varName != 'config') {
754                         // Regular template variables
755                         $this->assignVariable($var, '');
756                 } else {
757                         // Configuration variables
758                         $this->assignConfigVariable($var);
759                 }
760         }
761
762         /**
763          * Extract variables from a given raw data stream
764          *
765          * @param       $rawData        The raw template data we shall analyze
766          * @return      void
767          */
768         private function extractVariablesFromRawData ($rawData) {
769                 // Cast to string
770                 $rawData = (string) $rawData;
771
772                 // Search for variables
773                 @preg_match_all('/\$(\w+)(\[(\w+)\])?/', $rawData, $variableMatches);
774
775                 // Did we find some variables?
776                 if ((is_array($variableMatches)) && (count($variableMatches) == 4) && (count($variableMatches[0]) > 0)) {
777                         // Initialize all missing variables
778                         foreach ($variableMatches[3] as $key => $var) {
779                                 // Variable name
780                                 $varName = $variableMatches[1][$key];
781
782                                 // Workarround: Do not assign empty variables
783                                 if (!empty($var)) {
784                                         // Try to assign it, empty strings are being ignored
785                                         $this->assignTemplateVariable($varName, $var);
786                                 } // END - if
787                         } // END - foreach
788                 } // END - if
789         }
790
791         /**
792          * Main analysis of the loaded template
793          *
794          * @param       $templateMatches        Found template place-holders, see below
795          * @return      void
796          *
797          *---------------------------------
798          * Structure of $templateMatches:
799          *---------------------------------
800          * [0] => Array - An array with all full matches
801          * [1] => Array - An array with left part (before the ':') of a match
802          * [2] => Array - An array with right part of a match including ':'
803          * [3] => Array - An array with right part of a match excluding ':'
804          */
805         private function analyzeTemplate (array $templateMatches) {
806                 // Backup raw template data
807                 $backup = $this->getRawTemplateData();
808
809                 // Initialize some arrays
810                 if (is_null($this->loadedRawData)) { $this->loadedRawData = array(); $this->rawTemplates = array(); }
811
812                 // Load all requested templates
813                 foreach ($templateMatches[1] as $template) {
814
815                         // Load and compile only templates which we have not yet loaded
816                         // RECURSIVE PROTECTION! BE CAREFUL HERE!
817                         if ((!isset($this->loadedRawData[$template])) && (!in_array($template, $this->loadedTemplates))) {
818
819                                 // Template not found, but maybe variable assigned?
820                                 //* DEBUG: */ echo __METHOD__.":template={$template}<br />\n";
821                                 if ($this->getVariableIndex($template) !== false) {
822                                         // Use that content here
823                                         $this->loadedRawData[$template] = $this->readVariable($template);
824
825                                         // Recursive protection:
826                                         $this->loadedTemplates[] = $template;
827                                 } elseif ($this->getVariableIndex($template, 'config')) {
828                                         // Use that content here
829                                         $this->loadedRawData[$template] = $this->readVariable($template, 'config');
830
831                                         // Recursive protection:
832                                         $this->loadedTemplates[] = $template;
833                                 } else {
834                                         // Then try to search for code-templates
835                                         try {
836                                                 // Load the code template and remember it's contents
837                                                 $this->loadCodeTemplate($template);
838                                                 $this->loadedRawData[$template] = $this->getRawTemplateData();
839
840                                                 // Remember this template for recursion detection
841                                                 // RECURSIVE PROTECTION!
842                                                 $this->loadedTemplates[] = $template;
843                                         } catch (FileIoException $e) {
844                                                 // Even this is not done... :/
845                                                 $this->rawTemplates[] = $template;
846                                         }
847                                 }
848                         } // END - if
849                 } // END - foreach
850
851                 // Restore the raw template data
852                 $this->setRawTemplateData($backup);
853         }
854
855         /**
856          * Compile a given raw template code and remember it for later usage
857          *
858          * @param       $code           The raw template code
859          * @param       $template       The template's name
860          * @return      void
861          */
862         private function compileCode ($code, $template) {
863                 // Is this template already compiled?
864                 if (in_array($template, $this->compiledTemplates)) {
865                         // Abort here...
866                         return;
867                 } // END - if
868
869                 // Remember this template being compiled
870                 $this->compiledTemplates[] = $template;
871
872                 // Compile the loaded code in five steps:
873                 //
874                 // 1. Backup current template data
875                 $backup = $this->getRawTemplateData();
876
877                 // 2. Set the current template's raw data as the new content
878                 $this->setRawTemplateData($code);
879
880                 // 3. Compile the template data
881                 $this->compileTemplate();
882
883                 // 4. Remember it's contents
884                 $this->loadedRawData[$template] = $this->getRawTemplateData();
885
886                 // 5. Restore the previous raw content from backup variable
887                 $this->setRawTemplateData($backup);
888         }
889
890         /**
891          * Insert all given and loaded templates by running through all loaded
892          * codes and searching for their place-holder in the main template
893          *
894          * @param       $templateMatches        See method analyzeTemplate()
895          * @return      void
896          */
897         private function insertAllTemplates (array $templateMatches) {
898                 // Run through all loaded codes
899                 foreach ($this->loadedRawData as $template => $code) {
900
901                         // Search for the template
902                         $foundIndex = array_search($template, $templateMatches[1]);
903
904                         // Lookup the matching template replacement
905                         if (($foundIndex !== false) && (isset($templateMatches[0][$foundIndex]))) {
906
907                                 // Get the current raw template
908                                 $rawData = $this->getRawTemplateData();
909
910                                 // Replace the space holder with the template code
911                                 $rawData = str_replace($templateMatches[0][$foundIndex], $code, $rawData);
912
913                                 // Set the new raw data
914                                 $this->setRawTemplateData($rawData);
915                         } // END - if
916                 } // END - foreach
917         }
918
919         /**
920          * Load all extra raw templates
921          *
922          * @return      void
923          */
924         private function loadExtraRawTemplates () {
925                 // Are there some raw templates we need to load?
926                 if (count($this->rawTemplates) > 0) {
927                         // Try to load all raw templates
928                         foreach ($this->rawTemplates as $key => $template) {
929                                 try {
930                                         // Load the template
931                                         $this->loadWebTemplate($template);
932
933                                         // Remember it's contents
934                                         $this->rawTemplates[$template] = $this->getRawTemplateData();
935
936                                         // Remove it from the loader list
937                                         unset($this->rawTemplates[$key]);
938
939                                         // Remember this template for recursion detection
940                                         // RECURSIVE PROTECTION!
941                                         $this->loadedTemplates[] = $template;
942                                 } catch (FileIoException $e) {
943                                         // This template was never found. We silently ignore it
944                                         unset($this->rawTemplates[$key]);
945                                 }
946                         } // END - foreach
947                 } // END - if
948         }
949
950         /**
951          * Assign all found template variables
952          *
953          * @param       $varMatches             An array full of variable/value pairs.
954          * @return      void
955          * @todo        Unfinished work or don't die here.
956          */
957         private function assignAllVariables (array $varMatches) {
958                 // Search for all variables
959                 foreach ($varMatches[1] as $key => $var) {
960
961                         // Detect leading equals
962                         if (substr($varMatches[2][$key], 0, 1) == '=') {
963                                 // Remove and cast it
964                                 $varMatches[2][$key] = (string) substr($varMatches[2][$key], 1);
965                         } // END - if
966
967                         // Do we have some quotes left and right side? Then it is free text
968                         if ((substr($varMatches[2][$key], 0, 1) == "\"") && (substr($varMatches[2][$key], -1, 1) == "\"")) {
969                                 // Free string detected! Which we can assign directly
970                                 $this->assignVariable($var, $varMatches[3][$key]);
971                         } elseif (!empty($varMatches[2][$key])) {
972                                 // @TODO Non-string found so we need some deeper analysis...
973                                 ApplicationEntryPoint::app_die('Deeper analysis not yet implemented!');
974                         }
975
976                 } // for ($varMatches ...
977         }
978
979         /**
980          * Compiles all loaded raw templates
981          *
982          * @param       $templateMatches        See method analyzeTemplate() for details
983          * @return      void
984          */
985         private function compileRawTemplateData (array $templateMatches) {
986                 // Are some code-templates found which we need to compile?
987                 if (count($this->loadedRawData) > 0) {
988
989                         // Then compile all!
990                         foreach ($this->loadedRawData as $template => $code) {
991
992                                 // Is this template already compiled?
993                                 if (in_array($template, $this->compiledTemplates)) {
994                                         // Then skip it
995                                         continue;
996                                 }
997
998                                 // Search for the template
999                                 $foundIndex = array_search($template, $templateMatches[1]);
1000
1001                                 // Lookup the matching variable data
1002                                 if (($foundIndex !== false) && (isset($templateMatches[3][$foundIndex]))) {
1003
1004                                         // Split it up with another reg. exp. into variable=value pairs
1005                                         preg_match_all($this->regExpVarValue, $templateMatches[3][$foundIndex], $varMatches);
1006
1007                                         // Assign all variables
1008                                         $this->assignAllVariables($varMatches);
1009
1010                                 } // END - if (isset($templateMatches ...
1011
1012                                 // Compile the loaded template
1013                                 $this->compileCode($code, $template);
1014
1015                         } // END - foreach ($this->loadedRawData ...
1016
1017                         // Insert all templates
1018                         $this->insertAllTemplates($templateMatches);
1019
1020                 } // END - if (count($this->loadedRawData) ...
1021         }
1022
1023         /**
1024          * Inserts all raw templates into their respective variables
1025          *
1026          * @return      void
1027          */
1028         private function insertRawTemplates () {
1029                 // Load all templates
1030                 foreach ($this->rawTemplates as $template => $content) {
1031                         // Set the template as a variable with the content
1032                         $this->assignVariable($template, $content);
1033                 }
1034         }
1035
1036         /**
1037          * Finalizes the compilation of all template variables
1038          *
1039          * @return      void
1040          */
1041         private function finalizeVariableCompilation () {
1042                 // Get the content
1043                 $content = $this->getRawTemplateData();
1044                 //* DEBUG: */ echo __METHOD__.': content before='.strlen($content).' ('.md5($content).')<br />\n';
1045
1046                 // Do we have the stack?
1047                 if (!$this->isVarStackSet('general')) {
1048                         // Abort here silently
1049                         // @TODO This silent abort should be logged, maybe.
1050                         return;
1051                 } // END - if
1052
1053                 // Walk through all variables
1054                 foreach ($this->getVarStack('general') as $currEntry) {
1055                         //* DEBUG: */ echo __METHOD__.': name='.$currEntry['name'].', value=<pre>'.htmlentities($currEntry['value']).'</pre>\n';
1056                         // Replace all [$var] or {?$var?} with the content
1057                         // @TODO Old behaviour, will become obsolete!
1058                         $content = str_replace('$content[' . $currEntry['name'] . ']', $currEntry['value'], $content);
1059
1060                         // @TODO Yet another old way
1061                         $content = str_replace('[' . $currEntry['name'] . ']', $currEntry['value'], $content);
1062
1063                         // The new behaviour
1064                         $content = str_replace('{?' . $currEntry['name'] . '?}', $currEntry['value'], $content);
1065                 } // END - for
1066
1067                 //* DEBUG: */ echo __METHOD__.': content after='.strlen($content).' ('.md5($content).')<br />\n';
1068
1069                 // Set the content back
1070                 $this->setRawTemplateData($content);
1071         }
1072
1073         /**
1074          * Load a specified web template into the engine
1075          *
1076          * @param       $template       The web template we shall load which is located in
1077          *                                              'html' by default
1078          * @return      void
1079          */
1080         public function loadWebTemplate ($template) {
1081                 // Set template type
1082                 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('web_template_type'));
1083
1084                 // Load the special template
1085                 $this->loadTemplate($template);
1086         }
1087
1088         /**
1089          * Assign a given congfiguration variable with a value
1090          *
1091          * @param       $var    The configuration variable we want to assign
1092          * @return      void
1093          */
1094         public function assignConfigVariable ($var) {
1095                 // Sweet and simple...
1096                 //* DEBUG: */ echo __METHOD__.':var={$var}<br />\n';
1097                 $this->setVariable('config', $var, $this->getConfigInstance()->getConfigEntry($var));
1098         }
1099
1100         /**
1101          * Load a specified code template into the engine
1102          *
1103          * @param       $template       The code template we shall load which is
1104          *                                              located in 'code' by default
1105          * @return      void
1106          */
1107         public function loadCodeTemplate ($template) {
1108                 // Set template type
1109                 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('code_template_type'));
1110
1111                 // Load the special template
1112                 $this->loadTemplate($template);
1113         }
1114
1115         /**
1116          * Compiles configuration place-holders in all variables. This 'walks'
1117          * through the variable stack 'general'. It interprets all values from that
1118          * variables as configuration entries after compiling them.
1119          *
1120          * @return      void
1121          */
1122         public final function compileConfigInVariables () {
1123                 // Do we have the stack?
1124                 if (!$this->isVarStackSet('general')) {
1125                         // Abort here silently
1126                         // @TODO This silent abort should be logged, maybe.
1127                         return;
1128                 } // END - if
1129
1130                 // Iterate through all general variables
1131                 foreach ($this->getVarStack('general') as $index=>$currVariable) {
1132                         // Compile the value
1133                         $value = $this->compileRawCode($this->readVariable($currVariable['name']), true);
1134
1135                         // Remove it from stack
1136                         $this->removeVariable($index, 'general');
1137
1138                         // Re-assign the variable
1139                         $this->assignConfigVariable($value);
1140                 } // END - foreach
1141         }
1142
1143         /**
1144          * Compile all variables by inserting their respective values
1145          *
1146          * @return      void
1147          * @todo        Make this code some nicer...
1148          */
1149         public final function compileVariables () {
1150                 // Initialize the $content array
1151                 $validVar = $this->getConfigInstance()->getConfigEntry('tpl_valid_var');
1152                 $dummy = array();
1153
1154                 // Iterate through all general variables
1155                 foreach ($this->getVarStack('general') as $currVariable) {
1156                         // Transfer it's name/value combination to the $content array
1157                         //* DEBUG: */ echo $currVariable['name'].'=<pre>'.htmlentities($currVariable['value']).'</pre>\n';
1158                         $dummy[$currVariable['name']] = $currVariable['value'];
1159                 }// END - if
1160
1161                 // Set the new variable (don't remove the second dollar!)
1162                 $$validVar = $dummy;
1163
1164                 // Prepare all configuration variables
1165                 $config = null;
1166                 if ($this->isVarStackSet('config')) {
1167                         $config = $this->getVarStack('config');
1168                 } // END - if
1169
1170                 // Remove some variables
1171                 unset($index);
1172                 unset($currVariable);
1173
1174                 // Run the compilation three times to get content from helper classes in
1175                 $cnt = 0;
1176                 while ($cnt < 3) {
1177                         // Finalize the compilation of template variables
1178                         $this->finalizeVariableCompilation();
1179
1180                         // Prepare the eval() command for comiling the template
1181                         $eval = sprintf("\$result = \"%s\";",
1182                                 addslashes($this->getRawTemplateData())
1183                         );
1184
1185                         // This loop does remove the backslashes (\) in PHP parameters
1186                         while (strpos($eval, $this->codeBegin) !== false) {
1187                                 // Get left part before "<?"
1188                                 $evalLeft = substr($eval, 0, strpos($eval, $this->codeBegin));
1189
1190                                 // Get all from right of "<?"
1191                                 $evalRight = substr($eval, (strpos($eval, $this->codeBegin) + 5));
1192
1193                                 // Cut middle part out and remove escapes
1194                                 $evalMiddle = trim(substr($evalRight, 0, strpos($evalRight, $this->codeEnd)));
1195                                 $evalMiddle = stripslashes($evalMiddle);
1196
1197                                 // Remove the middle part from right one
1198                                 $evalRight = substr($evalRight, (strpos($evalRight, $this->codeEnd) + 2));
1199
1200                                 // And put all together
1201                                 $eval = sprintf("%s<%%php %s %%>%s", $evalLeft, $evalMiddle, $evalRight);
1202                         } // END - while
1203
1204                         // Prepare PHP code for eval() command
1205                         $eval = str_replace(
1206                                 "<%php", "\";",
1207                                 str_replace(
1208                                         "%>",
1209                                         "\n\$result .= \"",
1210                                         $eval
1211                                 )
1212                         );
1213
1214                         // Run the constructed command. This will "compile" all variables in
1215                         eval($eval);
1216
1217                         // Goes something wrong?
1218                         if ((!isset($result)) || (empty($result))) {
1219                                 // Output eval command
1220                                 $this->debugOutput(sprintf("Failed eval() code: <pre>%s</pre>", $this->markupCode($eval, true)), true);
1221
1222                                 // Output backtrace here
1223                                 $this->debugBackTrace();
1224                         } // END - if
1225
1226                         // Set raw template data
1227                         $this->setRawTemplateData($result);
1228                         $cnt++;
1229                 } // END - while
1230
1231                 // Final variable assignment
1232                 $this->finalizeVariableCompilation();
1233
1234                 // Set the new content
1235                 $this->setCompiledData($this->getRawTemplateData());
1236         }
1237
1238         /**
1239          * Compile all required templates into the current loaded one
1240          *
1241          * @return      void
1242          * @throws      UnexpectedTemplateTypeException If the template type is
1243          *                                                                                      not "code"
1244          * @throws      InvalidArrayCountException              If an unexpected array
1245          *                                                                                      count has been found
1246          */
1247         public function compileTemplate () {
1248                 // Get code type to make things shorter
1249                 $codeType = $this->getConfigInstance()->getConfigEntry('code_template_type');
1250
1251                 // We will only work with template type "code" from configuration
1252                 if (substr($this->getTemplateType(), 0, strlen($codeType)) != $codeType) {
1253                         // Abort here
1254                         throw new UnexpectedTemplateTypeException(array($this, $this->getTemplateType(), $this->getConfigInstance()->getConfigEntry('code_template_type')), self::EXCEPTION_TEMPLATE_TYPE_IS_UNEXPECTED);
1255                 } // END - if
1256
1257                 // Get the raw data.
1258                 $rawData = $this->getRawTemplateData();
1259
1260                 // Remove double spaces and trim leading/trailing spaces
1261                 $rawData = trim(str_replace('  ', ' ', $rawData));
1262
1263                 // Search for raw variables
1264                 $this->extractVariablesFromRawData($rawData);
1265
1266                 // Search for code-tags which are {? ?}
1267                 preg_match_all($this->regExpCodeTags, $rawData, $templateMatches);
1268
1269                 // Analyze the matches array
1270                 if ((is_array($templateMatches)) && (count($templateMatches) == 4) && (count($templateMatches[0]) > 0)) {
1271                         // Entries are found:
1272                         //
1273                         // The main analysis
1274                         $this->analyzeTemplate($templateMatches);
1275
1276                         // Compile raw template data
1277                         $this->compileRawTemplateData($templateMatches);
1278
1279                         // Are there some raw templates left for loading?
1280                         $this->loadExtraRawTemplates();
1281
1282                         // Are some raw templates found and loaded?
1283                         if (count($this->rawTemplates) > 0) {
1284
1285                                 // Insert all raw templates
1286                                 $this->insertRawTemplates();
1287
1288                                 // Remove the raw template content as well
1289                                 $this->setRawTemplateData('');
1290
1291                         } // END - if
1292
1293                 } // END - if($templateMatches ...
1294         }
1295
1296         /**
1297          * Loads a given view helper (by name)
1298          *
1299          * @param       $helperName             The helper's name
1300          * @return      void
1301          */
1302         protected function loadViewHelper ($helperName) {
1303                 // Make first character upper case, rest low
1304                 $helperName = $this->convertToClassName($helperName);
1305
1306                 // Is this view helper loaded?
1307                 if (!isset($this->helpers[$helperName])) {
1308                         // Create a class name
1309                         $className = "{$helperName}ViewHelper";
1310
1311                         // Generate new instance
1312                         $this->helpers[$helperName] = ObjectFactory::createObjectByName($className);
1313                 } // END - if
1314
1315                 // Return the requested instance
1316                 return $this->helpers[$helperName];
1317         }
1318
1319         /**
1320          * Assigns the last loaded raw template content with a given variable
1321          *
1322          * @param       $templateName   Name of the template we want to assign
1323          * @param       $variableName   Name of the variable we want to assign
1324          * @return      void
1325          */
1326         public function assignTemplateWithVariable ($templateName, $variableName) {
1327                 // Get the content from last loaded raw template
1328                 $content = $this->getRawTemplateData();
1329
1330                 // Assign the variable
1331                 $this->assignVariable($variableName, $content);
1332
1333                 // Purge raw content
1334                 $this->setRawTemplateData('');
1335         }
1336
1337         /**
1338          * Transfers the content of this template engine to a given response instance
1339          *
1340          * @param       $responseInstance       An instance of a response class
1341          * @return      void
1342          */
1343         public function transferToResponse (Responseable $responseInstance) {
1344                 // Get the content and set it in response class
1345                 $responseInstance->writeToBody($this->getCompiledData());
1346         }
1347
1348         /**
1349          * Assigns all the application data with template variables
1350          *
1351          * @param       $appInstance    A manageable application instance
1352          * @return      void
1353          */
1354         public function assignApplicationData (ManageableApplication $appInstance) {
1355                 // Get long name and assign it
1356                 $this->assignVariable('app_full_name' , $appInstance->getAppName());
1357
1358                 // Get short name and assign it
1359                 $this->assignVariable('app_short_name', $appInstance->getAppShortName());
1360
1361                 // Get version number and assign it
1362                 $this->assignVariable('app_version'   , $appInstance->getAppVersion());
1363
1364                 // Assign extra application-depending data
1365                 $appInstance->assignExtraTemplateData($this);
1366         }
1367
1368         /**
1369          * "Compiles" a variable by replacing {?var?} with it's content
1370          *
1371          * @param       $rawCode                        Raw code to compile
1372          * @param       $setMatchAsCode         Sets $match if readVariable() returns empty result
1373          * @return      $rawCode        Compile code with inserted variable value
1374          */
1375         public function compileRawCode ($rawCode, $setMatchAsCode=false) {
1376                 // Find the variables
1377                 //* DEBUG: */ echo __METHOD__.":rawCode=<pre>".htmlentities($rawCode)."</pre>\n";
1378                 preg_match_all($this->regExpVarValue, $rawCode, $varMatches);
1379
1380                 // Compile all variables
1381                 //* DEBUG: */ echo __METHOD__.":<pre>".print_r($varMatches, true)."</pre>\n";
1382                 foreach ($varMatches[0] as $match) {
1383                         // Add variable tags around it
1384                         $varCode = '{?' . $match . '?}';
1385
1386                         // Is the variable found in code? (safes some calls)
1387                         if (strpos($rawCode, $varCode) !== false) {
1388                                 // Replace the variable with it's value, if found
1389                                 $value = $this->readVariable($match);
1390                                 //* DEBUG: */ echo __METHOD__.": match=".$match.",value[".gettype($value)."]=".$value."<br />\n";
1391                                 if (($setMatchAsCode === true) && (is_null($value))) {
1392                                         // Insert match
1393                                         $rawCode = str_replace($varCode, $match, $rawCode);
1394                                 } else {
1395                                         // Insert value
1396                                         $rawCode = str_replace($varCode, $value, $rawCode);
1397                                 }
1398                         } // END - if
1399                 } // END - foreach
1400
1401                 // Return the compiled data
1402                 //* DEBUG: */ echo __METHOD__.":rawCode=<pre>".htmlentities($rawCode)."</pre>\n";
1403                 return $rawCode;
1404         }
1405
1406         /**
1407          * Getter for variable group array
1408          *
1409          * @return      $vargroups      All variable groups
1410          */
1411         public final function getVariableGroups () {
1412                 return $this->varGroups;
1413         }
1414
1415         /**
1416          * Renames a variable in code and in stack
1417          *
1418          * @param       $oldName        Old name of variable
1419          * @param       $newName        New name of variable
1420          * @return      void
1421          */
1422         public function renameVariable ($oldName, $newName) {
1423                 //* DEBUG: */ echo __METHOD__.": oldName={$oldName}, newName={$newName}<br />\n";
1424                 // Get raw template code
1425                 $rawData = $this->getRawTemplateData();
1426
1427                 // Replace it
1428                 $rawData = str_replace($oldName, $newName, $rawData);
1429
1430                 // Set the code back
1431                 $this->setRawTemplateData($rawData);
1432         }
1433
1434         /**
1435          * Renders the given XML content
1436          *
1437          * @param       $content        Valid XML content or if not set the current loaded raw content
1438          * @return      void
1439          * @throws      XmlParserException      If an XML error was found
1440          */
1441         public function renderXmlContent ($content = null) {
1442                 // Is the content set?
1443                 if (is_null($content)) {
1444                         // Get current content
1445                         $content = $this->getRawTemplateData();
1446                 } // END - if
1447
1448                 // Get a XmlParser instance
1449                 $parserInstance = ObjectFactory::createObjectByConfiguredName('xml_parser_class', array($this));
1450
1451                 // Check if we have XML compacting enabled
1452                 if ($this->isXmlCompactingEnabled()) {
1453                         // Yes, so get a decorator class for transparent compacting
1454                         $parserInstance = ObjectFactory::createObjectByConfiguredName('deco_compacting_xml_parser_class', array($parserInstance));
1455                 } // END - if
1456
1457                 // Parse the XML document
1458                 $parserInstance->parseXmlContent($content);
1459         }
1460
1461         /**
1462          * Enables or disables language support
1463          *
1464          * @param       $languageSupport        New language support setting
1465          * @return      void
1466          */
1467         public final function enableLanguageSupport ($languageSupport = true) {
1468                 $this->languageSupport = (bool) $languageSupport;
1469         }
1470
1471         /**
1472          * Checks wether language support is enabled
1473          *
1474          * @return      $languageSupport        Wether language support is enabled or disabled
1475          */
1476         public final function isLanguageSupportEnabled () {
1477                 return $this->languageSupport;
1478         }
1479
1480         /**
1481          * Enables or disables XML compacting
1482          *
1483          * @param       $xmlCompacting  New XML compacting setting
1484          * @return      void
1485          */
1486         public final function enableXmlCompacting ($xmlCompacting = true) {
1487                 $this->xmlCompacting = (bool) $xmlCompacting;
1488         }
1489
1490         /**
1491          * Checks wether XML compacting is enabled
1492          *
1493          * @return      $xmlCompacting  Wether XML compacting is enabled or disabled
1494          */
1495         public final function isXmlCompactingEnabled () {
1496                 return $this->xmlCompacting;
1497         }
1498
1499         /**
1500          * Removes all commentd, tabs and new-line characters to compact the content
1501          *
1502          * @param       $uncompactedContent             The uncompacted content
1503          * @return      $compactedContent               The compacted content
1504          */
1505         public function compactContent ($uncompactedContent) {
1506                 // First, remove all tab/new-line/revert characters
1507                 $compactedContent = str_replace("\t", '', str_replace("\n", '', str_replace("\r", '', $uncompactedContent)));
1508
1509                 // Then regex all comments like <!-- //--> away
1510                 preg_match_all('/<!--[\w\W]*?(\/\/){0,1}-->/', $compactedContent, $matches);
1511
1512                 // Do we have entries?
1513                 if (isset($matches[0][0])) {
1514                         // Remove all
1515                         foreach ($matches[0] as $match) {
1516                                 // Remove the match
1517                                 $compactedContent = str_replace($match, '', $compactedContent);
1518                         } // END - foreach
1519                 } // END - if
1520
1521                 // Return compacted content
1522                 return $compactedContent;
1523         }
1524 }
1525
1526 // [EOF]
1527 ?>