<?php
//
// +----------------------------------------------------------------------+
// | PHP Version 4                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license,      |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Author: Ulf Wendel <ulf.wendel@phpdoc.de>                            |
// +----------------------------------------------------------------------+
//
// $Id: it.php,v 1.12 2004/03/02 11:17:04 roland Exp $
//
if (!defined('IS_DEDI')) die();
if (!class_exists('PEAR')) require_once $cfg_dedi['dedi_path'].'external/pear/pear.php';

define("IT_OK",                         1);
define("IT_ERROR",                     -1);
define("IT_TPL_NOT_FOUND",             -2);
define("IT_BLOCK_NOT_FOUND",           -3);
define("IT_BLOCK_DUPLICATE",           -4);

class HTML_Template_IT {

    /**
    * Contains the error objects
    * @var      array
    * @access   public
    * @see      halt(), $printError, $haltOnError
    */
    var $err = array();

    /**
    * Clear cache on get()?
    * @var      boolean
    */
    var $clearCache = false;

    /**
    * First character of a variable placeholder ( _{_VARIABLE} ).
    * @var      string
    * @access   public
    * @see      $closingDelimiter, $blocknameRegExp, $variablenameRegExp
    */
    var $openingDelimiter = "{";

    /**
    * Last character of a variable placeholder ( {VARIABLE_}_ ).
    * @var      string
    * @access   public
    * @see      $openingDelimiter, $blocknameRegExp, $variablenameRegExp
    */
    var $closingDelimiter     = "}";

    /**
    * RegExp matching a block in the template.
    * Per default "sm" is used as the regexp modifier, "i" is missing.
    * That means a case sensitive search is done.
    * @var      string
    * @access   public
    * @see      $variablenameRegExp, $openingDelimiter, $closingDelimiter
    */
    var $blocknameRegExp    = "[0-9A-Za-z_-]+";

    /**
    * RegExp matching a variable placeholder in the template.
    * Per default "sm" is used as the regexp modifier, "i" is missing.
    * That means a case sensitive search is done.
    * @var      string
    * @access   public
    * @see      $blocknameRegExp, $openingDelimiter, $closingDelimiter
    */
    var $variablenameRegExp    = "[0-9A-Za-z_-]+";

    /**
    * RegExp used to find variable placeholder, filled by the constructor.
    * @var      string    Looks somewhat like @(delimiter varname delimiter)@
    * @access   public
    * @see      IntegratedTemplate()
    */
    var $variablesRegExp = "";

    /**
    * RegExp used to strip unused variable placeholder.
    * @brother  $variablesRegExp
    */
    var $removeVariablesRegExp = "";

    /**
    * Controls the handling of unknown variables, default is remove.
    * @var      boolean
    * @access   public
    */
    var $removeUnknownVariables = true;

    /**
    * Controls the handling of empty blocks, default is remove.
    * @var      boolean
    * @access   public
    */
    var $removeEmptyBlocks = true;

    /**
    * RegExp used to find blocks an their content, filled by the constructor.
    * @var      string
    * @see      IntegratedTemplate()
    */
    var $blockRegExp = "";

    /**
    * Name of the current block.
    * @var      string
    */
    var $currentBlock = "__global__";

    /**
    * Content of the template.
    * @var      string
    */
    var $template = "";

    /**
    * Array of all blocks and their content.
    *
    * @var      array
    * @see      findBlocks()
    */
    var $blocklist = array();

    /**
    * Array with the parsed content of a block.
    *
    * @var      array
    */
    var $blockdata = array();

    /**
    * Array of variables in a block.
    * @var      array
    */
    var $blockvariables = array();

    /**
    * Array of inner blocks of a block.
    * @var      array
    */
    var $blockinner         = array();

    /**
    * List of blocks to preverse even if they are "empty".
    *
    * This is something special. Sometimes you have blocks that
    * should be preserved although they are empty (no placeholder replaced).
    * Think of a shopping basket. If it's empty you have to drop a message to
    * the user. If it's filled you have to show the contents of the shopping baseket.
    * Now where do you place the message that the basket is empty? It's no good
    * idea to place it in you applications as customers tend to like unecessary minor
    * text changes. Having another template file for an empty basket means that it's
    * very likely that one fine day the filled and empty basket templates have different
    * layout. I decided to introduce blocks that to not contain any placeholder but only
    * text such as the message "Your shopping basked is empty".
    *
    * Now if there is no replacement done in such a block the block will be recognized
    * as "empty" and by default ($removeEmptyBlocks = true) be stripped off. To avoid this
    * you can now call touchBlock() to avoid this.
    *
    * The array $touchedBlocks stores a list of touched block which must not be removed even
    * if they are empty.
    *
    * @var  array    $touchedBlocks
    * @see  touchBlock(), $removeEmptyBlocks
    */
    var $touchedBlocks = array();

    /**
    * Variable cache.
    *
    * Variables get cached before any replacement is done.
    * Advantage: empty blocks can be removed automatically.
    * Disadvantage: might take some more memory
    *
    * @var    array
    * @see    setVariable(), $clearCacheOnParse
    */
    var $variableCache = array();

    /**
    * Clear the variable cache on parse?
    *
    * If you're not an expert just leave the default false.
    * True reduces memory consumption somewhat if you tend to
    * add lots of values for unknown placeholder.
    *
    * @var    boolean
    */
    var $clearCacheOnParse = false;

    /**
    * Root directory for all file operations.
    * The string gets prefixed to all filenames given.
    * @var    string
    * @see    IntegratedTemplate(), setRoot()
    */
    var $fileRoot = "";

    /**
    * Internal flag indicating that a blockname was used multiple times.
    * @var    boolean
    */
    var $flagBlocktrouble = false;

    /**
    * Flag indicating that the global block was parsed.
    * @var    boolean
    */
    var $flagGlobalParsed = false;

    /**
    * EXPERIMENTAL! FIXME!
    * Flag indication that a template gets cached.
    *
    * Complex templates require some times to be preparsed
    * before the replacement can take place. Often I use
    * one template file over and over again but I don't know
    * before that I will use the same template file again.
    * Now IT could notice this and skip the preparse.
    *
    * @var    boolean
    */
    var $flagCacheTemplatefile = true;

    /**
    * EXPERIMENTAL! FIXME!
    */
    var $lastTemplatefile = "";

    /**
    * Builds some complex regular expressions and optinally sets the file root directory.
    *
    * Make sure that you call this constructor if you derive your template
    * class from this one.
    *
    * @param    string    File root directory, prefix for all filenames given to the object.
    * @see      setRoot()
    */
    function HTML_Template_IT($root = "") {

        $this->variablesRegExp = "@" . $this->openingDelimiter . "(" . $this->variablenameRegExp . ")" . $this->closingDelimiter . "@sm";
        $this->removeVariablesRegExp = "@" . $this->openingDelimiter . "\s*(" . $this->variablenameRegExp . ")\s*" . $this->closingDelimiter . "@sm";

        $this->blockRegExp = '@<!--\s+BEGIN\s+(' . $this->blocknameRegExp . ')\s+-->(.*)<!--\s+END\s+\1\s+-->@sm';

        $this->setRoot($root);
    } // end constructor


    /**
    * Return a textual error message for a IT error code
    *
    * @param integer $value error code
    *
    * @return string error message, or false if the error code was
    * not recognized
    */
    function errorMessage($value)
    {
        static $errorMessages;
        if (!isset($errorMessages)) {
            $errorMessages = array(
                IT_ERROR                    => 'unknown error',
                IT_OK                       => '',
                IT_TPL_NOT_FOUND            => 'Cannot read the template file',
                IT_BLOCK_NOT_FOUND          => 'Cannot find this block',
                IT_BLOCK_DUPLICATE          => 'The name of a block must be unique within a template. Found "$blockname" twice. Unpredictable results may appear.'
            );
        }

        if (PEAR::isError($value)) {
            $value = $value->getCode();
        }

        return isset($errorMessages[$value]) ? $errorMessages[$value] : $errorMessages[IT_ERROR];
    }


    /**
    * Print a certain block with all replacements done.
    * @brother get()
    */
    function show($block = "__global__") {
        echo $this->get($block);
    } // end func show

    /**
    * Returns a block with all replacements done.
    *
    * @param    string     name of the block
    * @return   string
    * @throws   PEAR_Error
    * @access   public
    * @see      show()
    */
    function get($block = "__global__") {

        if ("__global__" == $block && !$this->flagGlobalParsed)
            $this->parse("__global__");

        if (!isset($this->blocklist[$block])) {
            $this->err[] = PEAR::raiseError( $this->errorMessage( IT_BLOCK_NOT_FOUND ) . '"' . $block . "'", IT_BLOCK_NOT_FOUND  );
            return "";
        }

        if ($this->clearCache) {

            $data = (isset($this->blockdata[$block])) ? $this->blockdata[$block] : "";
            unset($this->blockdata[$block]);
            return $data;

        } else {

            return (isset($this->blockdata[$block])) ? $this->blockdata[$block] : "";

        }

    } // end func get()

    /**
    * Parses the given block.
    *
    * @param    string    name of the block to be parsed
    * @access   public
    * @see      parseCurrentBlock()
    * @throws   PEAR_Error
    */
    function parse($block = "__global__", $flag_recursion = false) {

        if (!isset($this->blocklist[$block])) {
            return PEAR::raiseError( $this->errorMessage( IT_BLOCK_NOT_FOUND ) . '"' . $block . "'", IT_BLOCK_NOT_FOUND  );
        }

        if ("__global__" == $block)
            $this->flagGlobalParsed = true;

        $regs = array();
        $values = array();

        if ($this->clearCacheOnParse) {

            foreach ($this->variableCache as $name => $value) {
                $regs[] = "@" . $this->openingDelimiter . $name . $this->closingDelimiter . "@";
                $values[] = $value;
            }
            $this->variableCache = array();

        } else {

            foreach ($this->blockvariables[$block] as $allowedvar => $v) {

                if (isset($this->variableCache[$allowedvar])) {
                   $regs[]   = "@".$this->openingDelimiter . $allowedvar . $this->closingDelimiter . "@";
                   $values[] = $this->variableCache[$allowedvar];
                    unset($this->variableCache[$allowedvar]);
                }

            }

        }

        $outer = (0 == count($regs)) ? $this->blocklist[$block] : preg_replace($regs, $values, $this->blocklist[$block]);
        $empty = (0 == count($values)) ? true : false;

        if (isset($this->blockinner[$block])) {

          foreach ($this->blockinner[$block] as $k => $innerblock) {

            $this->parse($innerblock, true);
                if ("" != $this->blockdata[$innerblock])
                    $empty = false;

                $placeholder = $this->openingDelimiter . "__" . $innerblock . "__" . $this->closingDelimiter;
                $outer = str_replace($placeholder, $this->blockdata[$innerblock], $outer);
                $this->blockdata[$innerblock] = "";
          }

        }

        if ($this->removeUnknownVariables)
            $outer = preg_replace($this->removeVariablesRegExp, "", $outer);

        if ($empty) {

            if (!$this->removeEmptyBlocks) {

                $this->blockdata[$block ].= $outer;

            } else {

                if (isset($this->touchedBlocks[$block])) {
                    $this->blockdata[$block] .= $outer;
                    unset($this->touchedBlocks[$block]);
                }

            }

        } else {

            $this->blockdata[$block] .= $outer;

        }

        return $empty;
    } // end func parse

    /**
    * Parses the current block
    * @see      parse(), setCurrentBlock(), $currentBlock
    * @access   public
    */
    function parseCurrentBlock() {
        return $this->parse($this->currentBlock);
    } // end func parseCurrentBlock

    /**
    * Sets a variable value.
    *
    * The function can be used eighter like setVariable( "varname", "value")
    * or with one array $variables["varname"] = "value" given setVariable($variables)
    * quite like phplib templates set_var().
    *
    * @param    mixed     string with the variable name or an array %variables["varname"] = "value"
    * @param    string    value of the variable or empty if $variable is an array.
    * @param    string    prefix for variable names
    * @access   public
    */
    function setVariable($variable, $value = "") {

        if (is_array($variable)) {

            $this->variableCache = array_merge($this->variableCache, $variable);

        } else {

            $this->variableCache[$variable] = $value;

        }

    } // end func setVariable

    /**
    * Sets the name of the current block that is the block where variables are added.
    *
    * @param    string      name of the block
    * @return   boolean     false on failure, otherwise true
    * @throws   PEAR_Error
    * @access   public
    */
    function setCurrentBlock($block = "__global__") {

        if (!isset($this->blocklist[$block]))
            return PEAR::raiseError( $this->errorMessage( IT_BLOCK_NOT_FOUND ) . '"' . $block . "'", IT_BLOCK_NOT_FOUND  );

        $this->currentBlock = $block;

        return true;
    } // end func setCurrentBlock

    /**
    * Preserves an empty block even if removeEmptyBlocks is true.
    *
    * @param    string      name of the block
    * @return   boolean     false on false, otherwise true
    * @throws   PEAR_Error
    * @access   public
    * @see      $removeEmptyBlocks
    */
    function touchBlock($block) {

        if (!isset($this->blocklist[$block]))
            return PEAR::raiseError( $this->errorMessage( IT_BLOCK_NOT_FOUND ) . '"' . $block . "'", IT_BLOCK_NOT_FOUND  );

        $this->touchedBlocks[$block] = true;

        return true;
    } // end func touchBlock

    /**
    * Clears all datafields of the object and rebuild the internal blocklist
    *
    * LoadTemplatefile() and setTemplate() automatically call this function
    * when a new template is given. Don't use this function
    * unless you know what you're doing.
    *
    * @access   public
    * @see      free()
    */
    function init() {

        $this->free();
        $this->findBlocks($this->template);
        // we don't need it any more
        $this->template = '';
        $this->buildBlockvariablelist();

    } // end func init

    /**
    * Clears all datafields of the object.
    *
    * Don't use this function unless you know what you're doing.
    *
    * @access   public
    * @see      init()
    */
    function free() {

        $this->err = array();

        $this->currentBlock = "__global__";

        $this->variableCache    = array();
        $this->blocklookup      = array();
        $this->touchedBlocks    = array();

        $this->flagBlocktrouble = false;
        $this->flagGlobalParsed = false;

    } // end func free

    /**
    * Sets the template.
    *
    * You can eighter load a template file from disk with LoadTemplatefile() or set the
    * template manually using this function.
    *
    * @param        string      template content
    * @param        boolean     remove unknown/unused variables?
    * @param        boolean     remove empty blocks?
    * @see          LoadTemplatefile(), $template
    * @access       public
    */
    function setTemplate($template, $removeUnknownVariables = true, $removeEmptyBlocks = true) {

        $this->removeUnknownVariables = $removeUnknownVariables;
        $this->removeEmptyBlocks = $removeEmptyBlocks;

        if ("" == $template && $this->flagCacheTemplatefile) {

            $this->variableCache = array();
            $this->blockdata = array();
            $this->touchedBlocks = array();
            $this->currentBlock = "__global__";

        } else {

            $this->template = '<!-- BEGIN __global__ -->' . $template . '<!-- END __global__ -->';
            $this->init();

        }

        if ($this->flagBlocktrouble)
            return false;

        return true;
    } // end func setTemplate

    /**
    * Reads a template file from the disk.
    *
    * @param    string      name of the template file
    * @param    bool        how to handle unknown variables.
    * @param    bool        how to handle empty blocks.
    * @access   public
    * @return   boolean    false on failure, otherwise true
    * @see      $template, setTemplate(), $removeUnknownVariables, $removeEmptyBlocks
    */
    function loadTemplatefile($filename, $removeUnknownVariables = true, $removeEmptyBlocks = true) {

        $template = "";
        if (!$this->flagCacheTemplatefile || $this->lastTemplatefile != $filename)
            $template = $this->getfile($filename);

        $this->lastTemplatefile = $filename;

        return $template!=""?$this->setTemplate($template, $removeUnknownVariables, $removeEmptyBlocks):false;
    } // end func LoadTemplatefile

    /**
    * Sets the file root. The file root gets prefixed to all filenames passed to the object.
    *
    * Make sure that you override this function when using the class
    * on windows.
    *
    * @param    string
    * @see      IntegratedTemplate()
    * @access   public
    */
    function setRoot($root) {

        if ("" != $root && "/" != substr($root, -1))
            $root .= "/";

        $this->fileRoot = $root;

    } // end func setRoot

    /**
    * Build a list of all variables within of a block
    */
    function buildBlockvariablelist() {

        foreach ($this->blocklist as $name => $content) {
            preg_match_all( $this->variablesRegExp, $content, $regs );

            if (0 != count($regs[1])) {

                foreach ($regs[1] as $k => $var)
                    $this->blockvariables[$name][$var] = true;

            } else {

                $this->blockvariables[$name] = array();

            }

        }

    } // end func buildBlockvariablelist

    /**
    * Returns a list of all
    */
    function getGlobalvariables() {

        $regs   = array();
        $values = array();

        foreach ($this->blockvariables["__global__"] as $allowedvar => $v) {

            if (isset($this->variableCache[$allowedvar])) {
                $regs[]   = "@" . $this->openingDelimiter . $allowedvar . $this->closingDelimiter."@";
                $values[] = $this->variableCache[$allowedvar];
                unset($this->variableCache[$allowedvar]);
            }

        }

        return array($regs, $values);
    } // end func getGlobalvariables

    /**
    * Recusively builds a list of all blocks within the template.
    *
    * @param    string    string that gets scanned
    * @see      $blocklist
    */
    function findBlocks($string) {

        $blocklist = array();

        if (preg_match_all($this->blockRegExp, $string, $regs, PREG_SET_ORDER)) {

            foreach ($regs as $k => $match) {

                $blockname         = $match[1];
                $blockcontent = $match[2];

                if (isset($this->blocklist[$blockname])) {
                    $this->err[] = PEAR::raiseError( $this->errorMessage( IT_BLOCK_DUPLICATE ) . '"' . $blockname . "'", IT_BLOCK_DUPLICATE  );
                    $this->flagBlocktrouble = true;
                }

                $this->blocklist[$blockname] = $blockcontent;
                $this->blockdata[$blockname] = "";

                $blocklist[] = $blockname;

                $inner = $this->findBlocks($blockcontent);
                foreach ($inner as $k => $name) {

                    $pattern = sprintf('@<!--\s+BEGIN\s+%s\s+-->(.*)<!--\s+END\s+%s\s+-->@sm',
                                                    $name,
                                                    $name
                                                );

                    $this->blocklist[$blockname] = preg_replace(    $pattern,
                                                                    $this->openingDelimiter . "__" . $name . "__" . $this->closingDelimiter,
                                                                    $this->blocklist[$blockname]
                                                               );
                    $this->blockinner[$blockname][] = $name;
                    $this->blockparents[$name] = $blockname;

                }

            }

        }

        return $blocklist;
    } // end func findBlocks

    /**
    * Reads a file from disk and returns its content.
    * @param    string    Filename
    * @return   string    Filecontent
    */
    function getFile($filename) {

        if ("/" == $filename{0} && "/" == substr($this->fileRoot, -1))
            $filename = substr($filename, 1);

        $filename = $this->fileRoot . $filename;

        if (!($fh = @fopen($filename, "r"))) {
            $this->err[] = PEAR::raiseError($this->errorMessage(IT_TPL_NOT_FOUND) . ': "' .$filename .'"',IT_TPL_NOT_FOUND);
            return "";
        }

        $content = fread($fh, filesize($filename));
        fclose($fh);

        return preg_replace("#<!-- INCLUDE (.*) -->#ime", "\$this->getFile('\\1')", $content);
    } // end func getFile

} // end class IntegratedTemplate
?>