21 require_once
'PEAR.php';
24 define(
'IT_ERROR', -1);
25 define(
'IT_TPL_NOT_FOUND', -2);
26 define(
'IT_BLOCK_NOT_FOUND', -3);
27 define(
'IT_BLOCK_DUPLICATE', -4);
28 define(
'IT_UNKNOWN_OPTION', -6);
355 'preserve_data' =>
false,
375 $this->variablesRegExp =
'@' . $this->openingDelimiter .
376 '(' . $this->variablenameRegExp .
')' .
377 $this->closingDelimiter .
'@sm';
378 $this->removeVariablesRegExp =
'@' . $this->openingDelimiter .
379 "\s*(" . $this->variablenameRegExp .
380 ")\s*" . $this->closingDelimiter .
'@sm';
382 $this->blockRegExp =
'@<!--\s+BEGIN\s+(' . $this->blocknameRegExp .
383 ')\s+-->(.*)<!--\s+END\s+\1\s+-->@sm';
399 if (array_key_exists($option, $this->_options)) {
400 $this->_options[$option] = $value;
425 foreach (
$options as $option => $value) {
426 $error = $this->
setOption($option, $value);
440 function show($block =
'__global__')
442 print $this->
get($block);
454 function get($block =
'__global__')
456 if ($block ==
'__global__' && !$this->flagGlobalParsed) {
457 $this->
parse(
'__global__');
460 if (!isset($this->blocklist[$block])) {
469 if (isset($this->blockdata[$block])) {
470 $ret = $this->blockdata[$block];
471 if ($this->clearCache) {
472 unset($this->blockdata[$block]);
474 if ($this->_options[
'preserve_data']) {
476 $this->openingDelimiter .
477 '%preserved%' . $this->closingDelimiter,
478 $this->openingDelimiter,
496 function parse($block =
'__global__', $flag_recursion =
false)
498 static $regs, $values;
500 if (!isset($this->blocklist[$block])) {
507 if ($block ==
'__global__') {
508 $this->flagGlobalParsed =
true;
511 if (!$flag_recursion) {
515 $outer = $this->blocklist[$block];
518 if ($this->clearCacheOnParse) {
519 foreach ($this->variableCache as $name => $value) {
520 $regs[] = $this->openingDelimiter .
525 $this->variableCache = array();
527 foreach ($this->blockvariables[$block] as $allowedvar => $v) {
529 if (isset($this->variableCache[$allowedvar])) {
530 $regs[] = $this->openingDelimiter .
532 $values[] = $this->variableCache[$allowedvar];
533 unset($this->variableCache[$allowedvar]);
539 if (isset($this->blockinner[$block])) {
540 foreach ($this->blockinner[$block] as $k => $innerblock) {
542 $this->
parse($innerblock,
true);
543 if ($this->blockdata[$innerblock] !=
'') {
547 $placeholder = $this->openingDelimiter .
"__" .
549 $outer = str_replace(
551 $this->blockdata[$innerblock], $outer
553 $this->blockdata[$innerblock] =
"";
558 if (!$flag_recursion && 0 != count($values)) {
559 if ($this->_options[
'use_preg']) {
560 $regs = array_map(array(
561 &$this,
'_addPregDelimiters'),
564 $funcReplace =
'preg_replace';
566 $funcReplace =
'str_replace';
569 if ($this->_options[
'preserve_data']) {
571 array(&$this,
'_preserveOpeningDelimiter'), $values
575 $outer = $funcReplace($regs, $values, $outer);
577 if ($this->removeUnknownVariables) {
578 $outer = preg_replace($this->removeVariablesRegExp,
"", $outer);
583 if (!$this->removeEmptyBlocks) {
584 $this->blockdata[$block ].= $outer;
586 if (isset($this->touchedBlocks[$block])) {
587 $this->blockdata[$block] .= $outer;
588 unset($this->touchedBlocks[$block]);
592 if (empty($this->blockdata[$block])) {
593 $this->blockdata[$block] = $outer;
595 $this->blockdata[$block] .= $outer;
609 return $this->
parse($this->currentBlock);
628 if (is_array($variable)) {
629 $this->variableCache = array_merge(
630 $this->variableCache, $variable
633 $this->variableCache[$variable] = $value;
649 if (!isset($this->blocklist[$block])) {
656 $this->currentBlock = $block;
672 if (!isset($this->blocklist[$block])) {
678 $this->touchedBlocks[$block] =
true;
698 $this->
template =
'';
712 $this->err = array();
714 $this->currentBlock =
'__global__';
716 $this->variableCache = array();
717 $this->blocklist = array();
718 $this->touchedBlocks = array();
720 $this->flagBlocktrouble =
false;
721 $this->flagGlobalParsed =
false;
743 if (
$template ==
'' && $this->flagCacheTemplatefile) {
744 $this->variableCache = array();
745 $this->blockdata = array();
746 $this->touchedBlocks = array();
747 $this->currentBlock =
'__global__';
749 $this->
template =
'<!-- BEGIN __global__ -->' .
$template .
750 '<!-- END __global__ -->';
754 if ($this->flagBlocktrouble) {
777 if (!$this->flagCacheTemplatefile ||
803 if ($root !=
'' && substr($root, -1) !=
'/') {
807 $this->fileRoot = $root;
815 foreach ($this->blocklist as $name => $content) {
816 preg_match_all($this->variablesRegExp, $content, $regs);
818 if (count($regs[1]) != 0) {
819 foreach ($regs[1] as $k => $var) {
820 $this->blockvariables[$name][$var] =
true;
823 $this->blockvariables[$name] = array();
836 foreach ($this->blockvariables[
'__global__'] as $allowedvar => $v) {
837 if (isset($this->variableCache[$allowedvar])) {
838 $regs[] =
'@' . $this->openingDelimiter .
839 $allowedvar . $this->closingDelimiter .
'@';
840 $values[] = $this->variableCache[$allowedvar];
841 unset($this->variableCache[$allowedvar]);
845 return array($regs, $values);
858 if (preg_match_all($this->blockRegExp, $string, $regs, PREG_SET_ORDER)) {
859 foreach ($regs as $k => $match) {
860 $blockname = $match[1];
861 $blockcontent = $match[2];
863 if (isset($this->blocklist[$blockname])) {
869 $this->flagBlocktrouble =
true;
872 $this->blocklist[$blockname] = $blockcontent;
873 $this->blockdata[$blockname] =
"";
878 foreach ($inner as $k => $name) {
880 '@<!--\s+BEGIN\s+%s\s+-->(.*)<!--\s+END\s+%s\s+-->@sm',
885 $this->blocklist[$blockname] = preg_replace(
887 $this->openingDelimiter .
888 '__' . $name .
'__' .
889 $this->closingDelimiter,
890 $this->blocklist[$blockname]
892 $this->blockinner[$blockname][] = $name;
893 $this->blockparents[$name] = $blockname;
908 if (
$filename{0} ==
'/' && substr($this->fileRoot, -1) ==
'/') {
929 $content = fread($fh, $fsize);
933 "#<!-- INCLUDE (.*) -->#ime",
"\$this->getFile('\\1')", $content
946 return '@' . $str .
'@';
957 return (
false === strpos($str, $this->openingDelimiter))?
960 $this->openingDelimiter,
961 $this->openingDelimiter .
962 '%preserved%' . $this->closingDelimiter,
977 static $errorMessages;
978 if (!isset($errorMessages)) {
979 $errorMessages = array(
985 ' uniquewithin a template.'.
986 ' Found "' . $blockname .
'" twice.'.
987 'Unpredictable results '.
994 $value = $value->getCode();
997 return isset($errorMessages[$value]) ?
998 $errorMessages[$value] : $errorMessages[
IT_ERROR];