HTML_Template_PHPLIB
[ class tree: HTML_Template_PHPLIB ] [ index: HTML_Template_PHPLIB ] [ all elements ]

Source for file PHPLIB.php

Documentation is available at PHPLIB.php

  1. <?php
  2. // vim: set expandtab tabstop=4 shiftwidth=4:
  3. // This code that was derived from the original PHPLIB Template class
  4. // is copyright by Kristian Koehntopp, NetUSE AG and was released
  5. // under the LGPL.
  6. //
  7. // Authors: Kristian Koehntopp <kris@koehntopp.de> (original from PHPLIB)
  8. //          Bjoern Schotte <schotte@mayflower.de> (PEARification)
  9. //          Martin Jansen <mj@php.net> (PEAR conformance)
  10. //
  11. // $Id: PHPLIB.php,v 1.4 2007/10/01 15:12:07 cweiske Exp $
  12. //
  13.  
  14. /**
  15.  * Converted PHPLIB Template class
  16.  *
  17.  * For those who want to use PHPLIB's fine template class,
  18.  * here's a PEAR conforming class with the original PHPLIB
  19.  * template code from phplib-stable CVS. Original author
  20.  * was Kristian Koehntopp <kris@koehntopp.de>
  21.  *
  22.  * @category HTML
  23.  * @package  HTML_Template_PHPLIB
  24.  * @author   Bjoern Schotte <schotte@mayflower.de>
  25.  * @author   Martin Jansen <mj@php.net> (PEAR conformance)
  26.  * @license  http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  27.  * @version  CVS: $Id: PHPLIB.php,v 1.4 2007/10/01 15:12:07 cweiske Exp $
  28.  * @link     http://pear.php.net/package/HTML_Template_PHPLIB
  29.  */
  30. {
  31.     /**
  32.      * If set, echo assignments
  33.      * @var bool 
  34.      */
  35.     var $debug     = false;
  36.  
  37.     /**
  38.      * $file[handle] = 'filename';
  39.      * @var array 
  40.      */
  41.     var $file  = array();
  42.  
  43.     /**
  44.      * fallback paths that should be defined in a child class
  45.      * @var array 
  46.      */
  47.     var $file_fallbacks = array();
  48.  
  49.     /**
  50.      * Relative filenames are relative to this pathname
  51.      * @var string 
  52.      */
  53.     var $root   = '';
  54.  
  55.     /*
  56.      * $_varKeys[key] = 'key'
  57.      * @var array
  58.      */
  59.     var $_varKeys = array();
  60.  
  61.     /**
  62.      * $_varVals[key] = 'value';
  63.      * @var array 
  64.      */
  65.     var $_varVals = array();
  66.  
  67.     /**
  68.      * 'remove'  => remove undefined variables
  69.      * 'comment' => replace undefined variables with comments
  70.      * 'keep'    => keep undefined variables
  71.      * @var string 
  72.      */
  73.     var $unknowns = 'remove';
  74.  
  75.     /**
  76.      * 'yes' => halt,
  77.      * 'report' => report error, continue,
  78.      * 'no' => ignore error quietly
  79.      * @var string 
  80.      */
  81.     var $haltOnError  = 'report';
  82.  
  83.     /**
  84.      * The last error message is retained here
  85.      * @var string 
  86.      * @see halt
  87.      */
  88.     var $_lastError     '';
  89.  
  90.  
  91.     /**
  92.      * Constructor
  93.      *
  94.      * @param string $root     Template root directory
  95.      * @param string $unknowns How to handle unknown variables
  96.      * @param array  $fallback Fallback paths
  97.      *
  98.      * @access public
  99.      */
  100.     function HTML_Template_PHPLIB($root '.'$unknowns 'remove'$fallback='')
  101.     {
  102.         $this->setRoot($root);
  103.         $this->setUnknowns($unknowns);
  104.         if (is_array($fallback)) $this->file_fallbacks = $fallback;
  105.     }
  106.  
  107.     /**
  108.      * Sets the template directory
  109.      *
  110.      * @param string $root New template directory
  111.      *
  112.      * @return bool 
  113.      * @access public
  114.      */
  115.     function setRoot($root)
  116.     {
  117.         if (!is_dir($root)) {
  118.             $this->halt('setRoot: ' $root ' is not a directory.');
  119.             return false;
  120.         }
  121.  
  122.         $this->root = $root;
  123.  
  124.         return true;
  125.     }
  126.  
  127.     /**
  128.      * What to do with unknown variables
  129.      *
  130.      * three possible values:
  131.      *
  132.      * - 'remove' will remove unknown variables
  133.      *   (don't use this if you define CSS in your page)
  134.      * - 'comment' will replace undefined variables with comments
  135.      * - 'keep' will keep undefined variables as-is
  136.      *
  137.      * @param string $unknowns Unknowns
  138.      *
  139.      * @return void 
  140.      * @access public
  141.      */
  142.     function setUnknowns($unknowns 'keep')
  143.     {
  144.         $this->unknowns = $unknowns;
  145.     }
  146.  
  147.     /**
  148.      * Set appropriate template files
  149.      *
  150.      * With this method you set the template files you want to use.
  151.      * Either you supply an associative array with key/value pairs
  152.      * where the key is the handle for the filname and the value
  153.      * is the filename itself, or you define $handle as the file name
  154.      * handle and $filename as the filename if you want to define only
  155.      * one template.
  156.      *
  157.      * @param mixed  $handle   Handle for a filename or array with
  158.      *                           handle/name value pairs
  159.      * @param string $filename Name of template file
  160.      *
  161.      * @return bool True if file could be loaded
  162.      * @access public
  163.      */
  164.     function setFile($handle$filename '')
  165.     {
  166.         if (!is_array($handle)) {
  167.  
  168.             if ($filename == ''{
  169.                 $this->halt('setFile: For handle '
  170.                             . $handle ' filename is empty.');
  171.                 return false;
  172.             }
  173.  
  174.             $this->file[$handle$this->_filename($filename);
  175.             if ($this->file[$handle=== false{
  176.                 return false;
  177.             }
  178.             return true;
  179.         else {
  180.             reset($handle);
  181.             $error = false;
  182.             while (list($h$feach($handle)) {
  183.                 $this->file[$h$this->_filename($f);
  184.                 if ($this->file[$h=== false{
  185.                     $error = true;
  186.                 }
  187.             }
  188.             return $error === false;
  189.         }
  190.     }
  191.  
  192.     /**
  193.      * Set a block in the appropriate template handle
  194.      *
  195.      * By setting a block like that:
  196.      *
  197.      * &lt;!-- BEGIN blockname --&gt;
  198.      * html code
  199.      * &lt;!-- END blockname --&gt;
  200.      *
  201.      * you can easily do repeating HTML code, i.e. output
  202.      * database data nice formatted into a HTML table where
  203.      * each DB row is placed into a HTML table row which is
  204.      * defined in this block.
  205.      * It extracts the template $handle from $parent and places
  206.      * variable {$name} instead.
  207.      *
  208.      * @param string $parent Parent handle
  209.      * @param string $handle Block name handle
  210.      * @param string $name   Variable substitution name
  211.      *
  212.      * @return void 
  213.      * @access public
  214.      */
  215.     function setBlock($parent$handle$name '')
  216.     {
  217.         if (!$this->_loadFile($parent)) {
  218.             $this->halt('setBlock: unable to load ' $parent '.');
  219.             return false;
  220.         }
  221.  
  222.         if ($name == ''{
  223.             $name $handle;
  224.         }
  225.  
  226.         $str $this->getVar($parent);
  227.         $reg = "/[ \t]*<!--\s+BEGIN $handle\s+-->\s*?\n?(\s*.*?\n?)"
  228.              . "\s*<!--\s+END $handle\s+-->\s*?\n?/sm";
  229.         preg_match_all($reg$str$m);
  230.         $str preg_replace($reg'{' $name '}'$str);
  231.  
  232.         if (isset($m[1][0])) $this->setVar($handle$m[1][0]);
  233.         $this->setVar($parent$str);
  234.     }
  235.  
  236.     /**
  237.      * Set corresponding substitutions for placeholders
  238.      *
  239.      * @param string  $varname Name of a variable that is to be defined
  240.      *                           or an array of variables with value
  241.      *                           substitution as key/value pairs
  242.      * @param string  $value   Value of that variable
  243.      * @param boolean $append  If true, the value is appended to the
  244.      *                           variable's existing value
  245.      *
  246.      * @return void 
  247.      * @access public
  248.      */
  249.     function setVar($varname$value ''$append = false)
  250.     {
  251.         if (!is_array($varname)) {
  252.  
  253.             if (!empty($varname)) {
  254.                 if ($this->debug{
  255.                     print 'scalar: set *' $varname '* to *'
  256.                          . $value '*<br>\n';
  257.                 }
  258.             }
  259.  
  260.             $this->_varKeys[$varname$this->_varname($varname);
  261.             ($append$this->_varVals[$varname.= $value
  262.                       : $this->_varVals[$varname$value;
  263.  
  264.         else {
  265.             reset($varname);
  266.  
  267.             while (list($k$veach($varname)) {
  268.                 if (!empty($k)) {
  269.                     if ($this->debug{
  270.                         print 'array: set *' $k '* to *' $v '*<br>\n';
  271.                     }
  272.                 }
  273.  
  274.                 $this->_varKeys[$k$this->_varname($k);
  275.                 ($append$this->_varVals[$k.= $v
  276.                           : $this->_varVals[$k$v;
  277.             }
  278.         }
  279.     }
  280.  
  281.     /**
  282.      * Substitute variables in handle $handle
  283.      *
  284.      * @param string $handle Name of handle
  285.      *
  286.      * @return mixed String substituted content of handle
  287.      * @access public
  288.      */
  289.     function subst($handle)
  290.     {
  291.         if (!$this->_loadFile($handle)) {
  292.             $this->halt('subst: unable to load ' $handle '.');
  293.             return false;
  294.         }
  295.  
  296.         return @str_replace($this->_varKeys,
  297.                             $this->_varVals$this->getVar($handle));
  298.     }
  299.  
  300.     /**
  301.      * Same as subst but printing the result
  302.      *
  303.      * @param string $handle Handle of template
  304.      *
  305.      * @return bool always false
  306.      * @access public
  307.      * @see subst
  308.      */
  309.     function pSubst($handle)
  310.     {
  311.         print $this->subst($handle);
  312.         return false;
  313.     }
  314.  
  315.     /**
  316.      * Parse handle into target
  317.      *
  318.      * Parses handle $handle into $target, eventually
  319.      * appending handle at $target if $append is defined
  320.      * as TRUE.
  321.      *
  322.      * @param string  $target Target handle to parse into
  323.      * @param string  $handle Which handle should be parsed
  324.      * @param boolean $append Append it to $target or not?
  325.      *
  326.      * @return string parsed handle
  327.      * @access public
  328.      */
  329.     function parse($target$handle$append = false)
  330.     {
  331.         if (!is_array($handle)) {
  332.             $str $this->subst($handle);
  333.  
  334.             ($append$this->setVar($target$this->getVar($target$str)
  335.                       : $this->setVar($target$str);
  336.         else {
  337.             reset($handle);
  338.  
  339.             while (list($heach($handle)) {
  340.                 $str $this->subst($h);
  341.                 $this->setVar($target$str);
  342.             }
  343.         }
  344.  
  345.         return $str;
  346.     }
  347.  
  348.     /**
  349.      * Same as parse, but printing it.
  350.      *
  351.      * @param string $target Target to parse into
  352.      * @param string $handle Handle which should be parsed
  353.      * @param should $append If $handle shall be appended to $target?
  354.      *
  355.      * @return bool 
  356.      * @access public
  357.      * @see parse
  358.      */
  359.     function pParse($target$handle$append = false)
  360.     {
  361.         print $this->finish($this->parse($target$handle$append));
  362.         return false;
  363.     }
  364.  
  365.     /**
  366.      * Return all defined variables and their values
  367.      *
  368.      * @return array with all defined variables and their values
  369.      * @access public
  370.      */
  371.     function getVars()
  372.     {
  373.         reset($this->_varKeys);
  374.  
  375.         while (list($keach($this->_varKeys)) {
  376.             $result[$k$this->getVar($k);
  377.         }
  378.  
  379.         return $result;
  380.     }
  381.  
  382.     /**
  383.      * Return one or more specific variable(s) with their values.
  384.      *
  385.      * @param mixed $varname Array with variable names
  386.      *                        or one variable name as a string
  387.      *
  388.      * @return mixed Array of variable names with their values
  389.      *                or value of one specific variable
  390.      * @access public
  391.      */
  392.     function getVar($varname)
  393.     {
  394.         if (!is_array($varname)) {
  395.             if (isset($this->_varVals[$varname])) {
  396.                 return $this->_varVals[$varname];
  397.             else {
  398.                 return '';
  399.             }
  400.         else {
  401.             reset($varname);
  402.  
  403.             while (list($keach($varname)) {
  404.                 $result[$k(isset($this->_varVals[$k]))
  405.                     ? $this->_varVals[$k'';
  406.             }
  407.  
  408.             return $result;
  409.         }
  410.     }
  411.  
  412.     /**
  413.      * Get undefined values of a handle
  414.      *
  415.      * @param string $handle Handle name
  416.      *
  417.      * @return mixed False if an error occured or the array of undefined values
  418.      * @access public
  419.      */
  420.     function getUndefined($handle)
  421.     {
  422.         if (!$this->_loadFile($handle)) {
  423.             $this->halt('getUndefined: unable to load ' $handle);
  424.             return false;
  425.         }
  426.  
  427.         preg_match_all("/{([^ \t\r\n}]+)}/"$this->getVar($handle)$m);
  428.         $m $m[1];
  429.         if (!is_array($m)) {
  430.             return false;
  431.         }
  432.  
  433.         reset($m);
  434.         while (list($veach($m)) {
  435.             if (!isset($this->_varKeys[$v])) {
  436.                 $result[$v$v;
  437.             }
  438.         }
  439.  
  440.         if (isset($result&& count($result)) {
  441.             return $result;
  442.         else {
  443.             return false;
  444.         }
  445.     }
  446.  
  447.     /**
  448.      * Finish string
  449.      *
  450.      * @param string $str String to finish
  451.      *
  452.      * @return finished, i.e. substituted string
  453.      * @access public
  454.      */
  455.     function finish($str)
  456.     {
  457.         switch ($this->unknowns{
  458.         case 'remove':
  459.             $str preg_replace('/{[^ \t\r\n}]+}/'''$str);
  460.             break;
  461.  
  462.         case 'comment':
  463.             $str preg_replace('/{([^ \t\r\n}]+)}/',
  464.                 '<!-- Template variable \\1 undefined -->'$str);
  465.             break;
  466.         }
  467.  
  468.         return $str;
  469.     }
  470.  
  471.     /**
  472.      * Print variable to the browser
  473.      *
  474.      * @param string $varname Name of variable to print
  475.      *
  476.      * @return void 
  477.      * @access public
  478.      */
  479.     function p($varname)
  480.     {
  481.         print $this->finish($this->getVar($varname));
  482.     }
  483.  
  484.     /**
  485.      * Get finished variable
  486.      *
  487.      * @param string $varname Name of variable to get
  488.      *
  489.      * @return string string with finished variable
  490.      * @access public public
  491.      */
  492.     function get($varname)
  493.     {
  494.         return $this->finish($this->getVar($varname));
  495.     }
  496.  
  497.     /**
  498.      * Complete filename
  499.      *
  500.      * Complete filename, i.e. testing it for slashes
  501.      *
  502.      * @param string $filename Filename to be completed
  503.      *
  504.      * @access private
  505.      * @return string completed filename
  506.      */
  507.     function _filename($filename)
  508.     {
  509.         if (!$this->_isAbsolute($filename)) {
  510.             $filename $this->root . '/' $filename;
  511.         }
  512.  
  513.         if (file_exists($filename)) return $filename;
  514.         if (is_array($this->file_fallbacks&& count($this->file_fallbacks> 0{
  515.             reset($this->file_fallbacks);
  516.             while (list(,$veach($this->file_fallbacks)) {
  517.                 if (file_exists($v.basename($filename))) {
  518.                     return $v.basename($filename);
  519.                 }
  520.             }
  521.             $this->halt(sprintf(
  522.                 'filename: file %s does not exist in the fallback paths %s.',
  523.                 $filename,
  524.                 implode(','$this->file_fallbacks)
  525.             ));
  526.             return false;
  527.         else {
  528.             $this->halt(sprintf('filename: file %s does not exist.'$filename));
  529.             return false;
  530.         }
  531.  
  532.         return $filename;
  533.     }
  534.     
  535.     
  536.     /**
  537.      * Tells you whether a filename is absolute or relative
  538.      *
  539.      * @param string $filename Filename to check
  540.      *
  541.      * @return boolean true if the filename is absolute
  542.      */
  543.     function _isAbsolute($filename)
  544.     {
  545.         if (substr($filename01== '/'{
  546.             //unix
  547.             return true;
  548.         else if (substr($filename12== ':\\'{
  549.             //windows
  550.             return true;
  551.         }
  552.         return false;
  553.     }
  554.  
  555.     /**
  556.      * Protect a replacement variable
  557.      *
  558.      * @param string $varname name of replacement variable
  559.      *
  560.      * @return string replaced variable
  561.      * @access private
  562.      */
  563.     function _varname($varname)
  564.     {
  565.         return '{' $varname '}';
  566.     }
  567.  
  568.     /**
  569.      * load file defined by handle if it is not loaded yet
  570.      *
  571.      * @param string $handle File handle
  572.      *
  573.      * @return bool False if error, true if all is ok
  574.      * @access private
  575.      */
  576.     function _loadFile($handle)
  577.     {
  578.         if (isset($this->_varKeys[$handle]and !empty($this->_varVals[$handle])) {
  579.             return true;
  580.         }
  581.  
  582.         if (!isset($this->file[$handle])) {
  583.             $this->halt('loadfile: ' $handle ' is not a valid handle.');
  584.             return false;
  585.         }
  586.  
  587.         $filename $this->file[$handle];
  588.         if (function_exists('file_get_contents')) {
  589.             $str file_get_contents($filename);
  590.         else {
  591.             if (!$fp @fopen($filename'r')) {
  592.                 $this->halt('loadfile: couldn\'t open ' $filename);
  593.                 return false;
  594.             }
  595.  
  596.             $str fread($fpfilesize($filename));
  597.             fclose($fp);
  598.         }
  599.  
  600.         if ($str == ''{
  601.             $this->halt('loadfile: While loading ' $handle ', '
  602.                 . $filename ' does not exist or is empty.');
  603.             return false;
  604.         }
  605.  
  606.         $this->setVar($handle$str);
  607.  
  608.         return true;
  609.     }
  610.  
  611.     /**
  612.      * Error function. Halt template system with message to show
  613.      *
  614.      * @param string $msg message to show
  615.      *
  616.      * @return bool 
  617.      * @access public
  618.      */
  619.     function halt($msg)
  620.     {
  621.         $this->_lastError $msg;
  622.  
  623.         if ($this->haltOnError != 'no'{
  624.             return $this->haltMsg($msg);
  625.         }
  626.  
  627.         return false;
  628.     }
  629.  
  630.     /**
  631.      * printf error message to show
  632.      *
  633.      * @param string $msg message to show
  634.      *
  635.      * @return object PEAR error object
  636.      * @access public
  637.      */
  638.     function haltMsg($msg)
  639.     {
  640.         require_once 'PEAR.php';
  641.         return PEAR::raiseError(sprintf('<b>Template Error:</b> %s<br>'
  642.              . "\n"$msg));
  643.     }
  644.  
  645.     /**
  646.      * Returns the last error message if any
  647.      *
  648.      * @return boolean|stringLast error message if any
  649.      */
  650.     function getLastError()
  651.     {
  652.         if ($this->_lastError == ''{
  653.             return false;
  654.         }
  655.         return $this->_lastError;
  656.     }
  657. }
  658.  
  659. /**
  660.  * Backwards-compatibility for HTML_Template_PHPLIB.
  661.  * Used to have this name here.
  662.  *
  663.  * @category HTML
  664.  * @package  HTML_Template_PHPLIB
  665.  * @author   Christian Weiske <cweiske@php.net>
  666.  * @license  http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  667.  * @version  CVS: $Id: PHPLIB.php,v 1.4 2007/10/01 15:12:07 cweiske Exp $
  668.  * @link     http://pear.php.net/package/HTML_Template_PHPLIB
  669.  */
  670. {
  671. }
  672.  
  673. ?>

Documentation generated on Mon, 11 Mar 2019 15:10:07 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.