"Fossies" - the Fresh Open Source Software Archive

Member "icingaweb2-2.11.4/library/vendor/Zend/View/Abstract.php" (26 Jan 2023, 32881 Bytes) of package /linux/www/icingaweb2-2.11.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 /**
    3  * Zend Framework
    4  *
    5  * LICENSE
    6  *
    7  * This source file is subject to the new BSD license that is bundled
    8  * with this package in the file LICENSE.txt.
    9  * It is also available through the world-wide-web at this URL:
   10  * http://framework.zend.com/license/new-bsd
   11  * If you did not receive a copy of the license and are unable to
   12  * obtain it through the world-wide-web, please send an email
   13  * to license@zend.com so we can send you a copy immediately.
   14  *
   15  * @category   Zend
   16  * @package    Zend_View
   17  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
   18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
   19  * @version    $Id$
   20  */
   21 
   22 /** @see Zend_Loader */
   23 
   24 /** @see Zend_Loader_PluginLoader */
   25 
   26 /** @see Zend_View_Interface */
   27 
   28 /**
   29  * Abstract class for Zend_View to help enforce private constructs.
   30  *
   31  * @category   Zend
   32  * @package    Zend_View
   33  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
   34  * @license    http://framework.zend.com/license/new-bsd     New BSD License
   35  */
   36 abstract class Zend_View_Abstract implements Zend_View_Interface
   37 {
   38     /**
   39      * Path stack for script, helper, and filter directories.
   40      *
   41      * @var array
   42      */
   43     private $_path = array(
   44         'script' => array(),
   45         'helper' => array(),
   46         'filter' => array(),
   47     );
   48 
   49     /**
   50      * Script file name to execute
   51      *
   52      * @var string
   53      */
   54     private $_file = null;
   55 
   56     /**
   57      * Instances of helper objects.
   58      *
   59      * @var array
   60      */
   61     private $_helper = array();
   62 
   63     /**
   64      * Map of helper => class pairs to help in determining helper class from
   65      * name
   66      * @var array
   67      */
   68     private $_helperLoaded = array();
   69 
   70     /**
   71      * Map of helper => classfile pairs to aid in determining helper classfile
   72      * @var array
   73      */
   74     private $_helperLoadedDir = array();
   75 
   76     /**
   77      * Stack of Zend_View_Filter names to apply as filters.
   78      * @var array
   79      */
   80     private $_filter = array();
   81 
   82     /**
   83      * Stack of Zend_View_Filter objects that have been loaded
   84      * @var array
   85      */
   86     private $_filterClass = array();
   87 
   88     /**
   89      * Map of filter => class pairs to help in determining filter class from
   90      * name
   91      * @var array
   92      */
   93     private $_filterLoaded = array();
   94 
   95     /**
   96      * Map of filter => classfile pairs to aid in determining filter classfile
   97      * @var array
   98      */
   99     private $_filterLoadedDir = array();
  100 
  101     /**
  102      * Callback for escaping.
  103      *
  104      * @var string
  105      */
  106     private $_escape = 'htmlspecialchars';
  107 
  108     /**
  109      * Encoding to use in escaping mechanisms; defaults to utf-8
  110      * @var string
  111      */
  112     private $_encoding = 'UTF-8';
  113 
  114     /**
  115      * Flag indicating whether or not LFI protection for rendering view scripts is enabled
  116      * @var bool
  117      */
  118     private $_lfiProtectionOn = true;
  119 
  120     /**
  121      * Plugin loaders
  122      * @var array
  123      */
  124     private $_loaders = array();
  125 
  126     /**
  127      * Plugin types
  128      * @var array
  129      */
  130     private $_loaderTypes = array('filter', 'helper');
  131 
  132     /**
  133      * Strict variables flag; when on, undefined variables accessed in the view
  134      * scripts will trigger notices
  135      * @var boolean
  136      */
  137     private $_strictVars = false;
  138 
  139     /**
  140      * Constructor.
  141      *
  142      * @param array $config Configuration key-value pairs.
  143      */
  144     public function __construct($config = array())
  145     {
  146         // set inital paths and properties
  147         $this->setScriptPath(null);
  148 
  149         // $this->setHelperPath(null);
  150         $this->setFilterPath(null);
  151 
  152         // user-defined escaping callback
  153         if (array_key_exists('escape', $config)) {
  154             $this->setEscape($config['escape']);
  155         }
  156 
  157         // encoding
  158         if (array_key_exists('encoding', $config)) {
  159             $this->setEncoding($config['encoding']);
  160         }
  161 
  162         // base path
  163         if (array_key_exists('basePath', $config)) {
  164             $prefix = 'Zend_View';
  165             if (array_key_exists('basePathPrefix', $config)) {
  166                 $prefix = $config['basePathPrefix'];
  167             }
  168             $this->setBasePath($config['basePath'], $prefix);
  169         }
  170 
  171         // user-defined view script path
  172         if (array_key_exists('scriptPath', $config)) {
  173             $this->addScriptPath($config['scriptPath']);
  174         }
  175 
  176         // user-defined helper path
  177         if (array_key_exists('helperPath', $config)) {
  178             if (is_array($config['helperPath'])) {
  179                 foreach ($config['helperPath'] as $prefix => $path) {
  180                     $this->addHelperPath($path, $prefix);
  181                 }
  182             } else {
  183                 $prefix = 'Zend_View_Helper';
  184                 if (array_key_exists('helperPathPrefix', $config)) {
  185                     $prefix = $config['helperPathPrefix'];
  186                 }
  187                 $this->addHelperPath($config['helperPath'], $prefix);
  188             }
  189         }
  190 
  191         // user-defined filter path
  192         if (array_key_exists('filterPath', $config)) {
  193             if (is_array($config['filterPath'])) {
  194                 foreach ($config['filterPath'] as $prefix => $path) {
  195                     $this->addFilterPath($path, $prefix);
  196                 }
  197             } else {
  198                 $prefix = 'Zend_View_Filter';
  199                 if (array_key_exists('filterPathPrefix', $config)) {
  200                     $prefix = $config['filterPathPrefix'];
  201                 }
  202                 $this->addFilterPath($config['filterPath'], $prefix);
  203             }
  204         }
  205 
  206         // user-defined filters
  207         if (array_key_exists('filter', $config)) {
  208             $this->addFilter($config['filter']);
  209         }
  210 
  211         // strict vars
  212         if (array_key_exists('strictVars', $config)) {
  213             $this->strictVars($config['strictVars']);
  214         }
  215 
  216         // LFI protection flag
  217         if (array_key_exists('lfiProtectionOn', $config)) {
  218             $this->setLfiProtection($config['lfiProtectionOn']);
  219         }
  220 
  221         if (array_key_exists('assign', $config)
  222             && is_array($config['assign'])
  223         ) {
  224             foreach ($config['assign'] as $key => $value) {
  225                 $this->assign($key, $value);
  226             }
  227         }
  228 
  229         $this->init();
  230     }
  231 
  232     /**
  233      * Return the template engine object
  234      *
  235      * Returns the object instance, as it is its own template engine
  236      *
  237      * @return Zend_View_Abstract
  238      */
  239     public function getEngine()
  240     {
  241         return $this;
  242     }
  243 
  244     /**
  245      * Allow custom object initialization when extending Zend_View_Abstract or
  246      * Zend_View
  247      *
  248      * Triggered by {@link __construct() the constructor} as its final action.
  249      *
  250      * @return void
  251      */
  252     public function init()
  253     {
  254     }
  255 
  256     /**
  257      * Prevent E_NOTICE for nonexistent values
  258      *
  259      * If {@link strictVars()} is on, raises a notice.
  260      *
  261      * @param  string $key
  262      * @return null
  263      */
  264     public function __get($key)
  265     {
  266         if ($this->_strictVars) {
  267             trigger_error('Key "' . $key . '" does not exist', E_USER_NOTICE);
  268         }
  269 
  270         return null;
  271     }
  272 
  273     /**
  274      * Allows testing with empty() and isset() to work inside
  275      * templates.
  276      *
  277      * @param  string $key
  278      * @return boolean
  279      */
  280     public function __isset($key)
  281     {
  282         if ('_' != substr($key, 0, 1)) {
  283             return isset($this->$key);
  284         }
  285 
  286         return false;
  287     }
  288 
  289     /**
  290      * Directly assigns a variable to the view script.
  291      *
  292      * Checks first to ensure that the caller is not attempting to set a
  293      * protected or private member (by checking for a prefixed underscore); if
  294      * not, the public member is set; otherwise, an exception is raised.
  295      *
  296      * @param string $key The variable name.
  297      * @param mixed $val The variable value.
  298      * @return void
  299      * @throws Zend_View_Exception if an attempt to set a private or protected
  300      * member is detected
  301      */
  302     public function __set($key, $val)
  303     {
  304         if ('_' != substr($key, 0, 1)) {
  305             $this->$key = $val;
  306             return;
  307         }
  308 
  309         $e = new Zend_View_Exception('Setting private or protected class members is not allowed');
  310         $e->setView($this);
  311         throw $e;
  312     }
  313 
  314     /**
  315      * Allows unset() on object properties to work
  316      *
  317      * @param string $key
  318      * @return void
  319      */
  320     public function __unset($key)
  321     {
  322         if ('_' != substr($key, 0, 1) && isset($this->$key)) {
  323             unset($this->$key);
  324         }
  325     }
  326 
  327     /**
  328      * Accesses a helper object from within a script.
  329      *
  330      * If the helper class has a 'view' property, sets it with the current view
  331      * object.
  332      *
  333      * @param string $name The helper name.
  334      * @param array $args The parameters for the helper.
  335      * @return string The result of the helper output.
  336      */
  337     public function __call($name, $args)
  338     {
  339         // is the helper already loaded?
  340         $helper = $this->getHelper($name);
  341 
  342         // call the helper method
  343         return call_user_func_array(
  344             array($helper, $name),
  345             $args
  346         );
  347     }
  348 
  349     /**
  350      * Given a base path, sets the script, helper, and filter paths relative to it
  351      *
  352      * Assumes a directory structure of:
  353      * <code>
  354      * basePath/
  355      *     scripts/
  356      *     helpers/
  357      *     filters/
  358      * </code>
  359      *
  360      * @param  string $path
  361      * @param  string $prefix Prefix to use for helper and filter paths
  362      * @return Zend_View_Abstract
  363      */
  364     public function setBasePath($path, $classPrefix = 'Zend_View')
  365     {
  366         $path        = rtrim($path, '/');
  367         $path        = rtrim($path, '\\');
  368         $path       .= DIRECTORY_SEPARATOR;
  369         $classPrefix = rtrim($classPrefix, '_') . '_';
  370         $this->setScriptPath($path . 'scripts');
  371         $this->setHelperPath($path . 'helpers', $classPrefix . 'Helper');
  372         $this->setFilterPath($path . 'filters', $classPrefix . 'Filter');
  373         return $this;
  374     }
  375 
  376     /**
  377      * Given a base path, add script, helper, and filter paths relative to it
  378      *
  379      * Assumes a directory structure of:
  380      * <code>
  381      * basePath/
  382      *     scripts/
  383      *     helpers/
  384      *     filters/
  385      * </code>
  386      *
  387      * @param  string $path
  388      * @param  string $prefix Prefix to use for helper and filter paths
  389      * @return Zend_View_Abstract
  390      */
  391     public function addBasePath($path, $classPrefix = 'Zend_View')
  392     {
  393         $path        = rtrim($path, '/');
  394         $path        = rtrim($path, '\\');
  395         $path       .= DIRECTORY_SEPARATOR;
  396         $classPrefix = rtrim($classPrefix, '_') . '_';
  397         $this->addScriptPath($path . 'scripts');
  398         $this->addHelperPath($path . 'helpers', $classPrefix . 'Helper');
  399         $this->addFilterPath($path . 'filters', $classPrefix . 'Filter');
  400         return $this;
  401     }
  402 
  403     /**
  404      * Adds to the stack of view script paths in LIFO order.
  405      *
  406      * @param string|array The directory (-ies) to add.
  407      * @return Zend_View_Abstract
  408      */
  409     public function addScriptPath($path)
  410     {
  411         $this->_addPath('script', $path);
  412         return $this;
  413     }
  414 
  415     /**
  416      * Resets the stack of view script paths.
  417      *
  418      * To clear all paths, use Zend_View::setScriptPath(null).
  419      *
  420      * @param string|array The directory (-ies) to set as the path.
  421      * @return Zend_View_Abstract
  422      */
  423     public function setScriptPath($path)
  424     {
  425         $this->_path['script'] = array();
  426         $this->_addPath('script', $path);
  427         return $this;
  428     }
  429 
  430     /**
  431      * Return full path to a view script specified by $name
  432      *
  433      * @param  string $name
  434      * @return false|string False if script not found
  435      * @throws Zend_View_Exception if no script directory set
  436      */
  437     public function getScriptPath($name)
  438     {
  439         try {
  440             $path = $this->_script($name);
  441             return $path;
  442         } catch (Zend_View_Exception $e) {
  443             if (strstr($e->getMessage(), 'no view script directory set')) {
  444                 throw $e;
  445             }
  446 
  447             return false;
  448         }
  449     }
  450 
  451     /**
  452      * Returns an array of all currently set script paths
  453      *
  454      * @return array
  455      */
  456     public function getScriptPaths()
  457     {
  458         return $this->_getPaths('script');
  459     }
  460 
  461     /**
  462      * Set plugin loader for a particular plugin type
  463      *
  464      * @param  Zend_Loader_PluginLoader $loader
  465      * @param  string $type
  466      * @return Zend_View_Abstract
  467      */
  468     public function setPluginLoader(Zend_Loader_PluginLoader $loader, $type)
  469     {
  470         $type = strtolower($type);
  471         if (!in_array($type, $this->_loaderTypes)) {
  472             $e = new Zend_View_Exception(sprintf('Invalid plugin loader type "%s"', $type));
  473             $e->setView($this);
  474             throw $e;
  475         }
  476 
  477         $this->_loaders[$type] = $loader;
  478         return $this;
  479     }
  480 
  481     /**
  482      * Retrieve plugin loader for a specific plugin type
  483      *
  484      * @param  string $type
  485      * @return Zend_Loader_PluginLoader
  486      */
  487     public function getPluginLoader($type)
  488     {
  489         $type = strtolower($type);
  490         if (!in_array($type, $this->_loaderTypes)) {
  491             $e = new Zend_View_Exception(sprintf('Invalid plugin loader type "%s"; cannot retrieve', $type));
  492             $e->setView($this);
  493             throw $e;
  494         }
  495 
  496         if (!array_key_exists($type, $this->_loaders)) {
  497             $prefix     = 'Zend_View_';
  498             $pathPrefix = 'Zend/View/';
  499 
  500             $pType = ucfirst($type);
  501             switch ($type) {
  502                 case 'filter':
  503                 case 'helper':
  504                 default:
  505                     $prefix     .= $pType;
  506                     $pathPrefix .= $pType;
  507                     $loader = new Zend_Loader_PluginLoader(array(
  508                         $prefix => $pathPrefix
  509                     ));
  510                     $this->_loaders[$type] = $loader;
  511                     break;
  512             }
  513         }
  514         return $this->_loaders[$type];
  515     }
  516 
  517     /**
  518      * Adds to the stack of helper paths in LIFO order.
  519      *
  520      * @param string|array The directory (-ies) to add.
  521      * @param string $classPrefix Class prefix to use with classes in this
  522      * directory; defaults to Zend_View_Helper
  523      * @return Zend_View_Abstract
  524      */
  525     public function addHelperPath($path, $classPrefix = 'Zend_View_Helper_')
  526     {
  527         return $this->_addPluginPath('helper', $classPrefix, (array) $path);
  528     }
  529 
  530     /**
  531      * Resets the stack of helper paths.
  532      *
  533      * To clear all paths, use Zend_View::setHelperPath(null).
  534      *
  535      * @param string|array $path The directory (-ies) to set as the path.
  536      * @param string $classPrefix The class prefix to apply to all elements in
  537      * $path; defaults to Zend_View_Helper
  538      * @return Zend_View_Abstract
  539      */
  540     public function setHelperPath($path, $classPrefix = 'Zend_View_Helper_')
  541     {
  542         unset($this->_loaders['helper']);
  543         return $this->addHelperPath($path, $classPrefix);
  544     }
  545 
  546     /**
  547      * Get full path to a helper class file specified by $name
  548      *
  549      * @param  string $name
  550      * @return string|false False on failure, path on success
  551      */
  552     public function getHelperPath($name)
  553     {
  554         return $this->_getPluginPath('helper', $name);
  555     }
  556 
  557     /**
  558      * Returns an array of all currently set helper paths
  559      *
  560      * @return array
  561      */
  562     public function getHelperPaths()
  563     {
  564         return $this->getPluginLoader('helper')->getPaths();
  565     }
  566 
  567     /**
  568      * Registers a helper object, bypassing plugin loader
  569      *
  570      * @param  Zend_View_Helper_Abstract|object $helper
  571      * @param  string $name
  572      * @return Zend_View_Abstract
  573      * @throws Zend_View_Exception
  574      */
  575     public function registerHelper($helper, $name)
  576     {
  577         if (!is_object($helper)) {
  578             $e = new Zend_View_Exception('View helper must be an object');
  579             $e->setView($this);
  580             throw $e;
  581         }
  582 
  583         if (!$helper instanceof Zend_View_Interface) {
  584             if (!method_exists($helper, $name)) {
  585                 $e =  new Zend_View_Exception(
  586                     'View helper must implement Zend_View_Interface or have a method matching the name provided'
  587                 );
  588                 $e->setView($this);
  589                 throw $e;
  590             }
  591         }
  592 
  593         if (method_exists($helper, 'setView')) {
  594             $helper->setView($this);
  595         }
  596 
  597         $name = ucfirst($name);
  598         $this->_helper[$name] = $helper;
  599         return $this;
  600     }
  601 
  602     /**
  603      * Get a helper by name
  604      *
  605      * @param  string $name
  606      * @return object
  607      */
  608     public function getHelper($name)
  609     {
  610         return $this->_getPlugin('helper', $name);
  611     }
  612 
  613     /**
  614      * Get array of all active helpers
  615      *
  616      * Only returns those that have already been instantiated.
  617      *
  618      * @return array
  619      */
  620     public function getHelpers()
  621     {
  622         return $this->_helper;
  623     }
  624 
  625     /**
  626      * Adds to the stack of filter paths in LIFO order.
  627      *
  628      * @param string|array The directory (-ies) to add.
  629      * @param string $classPrefix Class prefix to use with classes in this
  630      * directory; defaults to Zend_View_Filter
  631      * @return Zend_View_Abstract
  632      */
  633     public function addFilterPath($path, $classPrefix = 'Zend_View_Filter_')
  634     {
  635         return $this->_addPluginPath('filter', $classPrefix, (array) $path);
  636     }
  637 
  638     /**
  639      * Resets the stack of filter paths.
  640      *
  641      * To clear all paths, use Zend_View::setFilterPath(null).
  642      *
  643      * @param string|array The directory (-ies) to set as the path.
  644      * @param string $classPrefix The class prefix to apply to all elements in
  645      * $path; defaults to Zend_View_Filter
  646      * @return Zend_View_Abstract
  647      */
  648     public function setFilterPath($path, $classPrefix = 'Zend_View_Filter_')
  649     {
  650         unset($this->_loaders['filter']);
  651         return $this->addFilterPath($path, $classPrefix);
  652     }
  653 
  654     /**
  655      * Get full path to a filter class file specified by $name
  656      *
  657      * @param  string $name
  658      * @return string|false False on failure, path on success
  659      */
  660     public function getFilterPath($name)
  661     {
  662         return $this->_getPluginPath('filter', $name);
  663     }
  664 
  665     /**
  666      * Get a filter object by name
  667      *
  668      * @param  string $name
  669      * @return object
  670      */
  671     public function getFilter($name)
  672     {
  673         return $this->_getPlugin('filter', $name);
  674     }
  675 
  676     /**
  677      * Return array of all currently active filters
  678      *
  679      * Only returns those that have already been instantiated.
  680      *
  681      * @return array
  682      */
  683     public function getFilters()
  684     {
  685         return $this->_filter;
  686     }
  687 
  688     /**
  689      * Returns an array of all currently set filter paths
  690      *
  691      * @return array
  692      */
  693     public function getFilterPaths()
  694     {
  695         return $this->getPluginLoader('filter')->getPaths();
  696     }
  697 
  698     /**
  699      * Return associative array of path types => paths
  700      *
  701      * @return array
  702      */
  703     public function getAllPaths()
  704     {
  705         $paths = $this->_path;
  706         $paths['helper'] = $this->getHelperPaths();
  707         $paths['filter'] = $this->getFilterPaths();
  708         return $paths;
  709     }
  710 
  711     /**
  712      * Add one or more filters to the stack in FIFO order.
  713      *
  714      * @param string|array One or more filters to add.
  715      * @return Zend_View_Abstract
  716      */
  717     public function addFilter($name)
  718     {
  719         foreach ((array) $name as $val) {
  720             $this->_filter[] = $val;
  721         }
  722         return $this;
  723     }
  724 
  725     /**
  726      * Resets the filter stack.
  727      *
  728      * To clear all filters, use Zend_View::setFilter(null).
  729      *
  730      * @param string|array One or more filters to set.
  731      * @return Zend_View_Abstract
  732      */
  733     public function setFilter($name)
  734     {
  735         $this->_filter = array();
  736         $this->addFilter($name);
  737         return $this;
  738     }
  739 
  740     /**
  741      * Sets the _escape() callback.
  742      *
  743      * @param mixed $spec The callback for _escape() to use.
  744      * @return Zend_View_Abstract
  745      */
  746     public function setEscape($spec)
  747     {
  748         $this->_escape = $spec;
  749         return $this;
  750     }
  751 
  752     /**
  753      * Set LFI protection flag
  754      *
  755      * @param  bool $flag
  756      * @return Zend_View_Abstract
  757      */
  758     public function setLfiProtection($flag)
  759     {
  760         $this->_lfiProtectionOn = (bool) $flag;
  761         return $this;
  762     }
  763 
  764     /**
  765      * Return status of LFI protection flag
  766      *
  767      * @return bool
  768      */
  769     public function isLfiProtectionOn()
  770     {
  771         return $this->_lfiProtectionOn;
  772     }
  773 
  774     /**
  775      * Assigns variables to the view script via differing strategies.
  776      *
  777      * Zend_View::assign('name', $value) assigns a variable called 'name'
  778      * with the corresponding $value.
  779      *
  780      * Zend_View::assign($array) assigns the array keys as variable
  781      * names (with the corresponding array values).
  782      *
  783      * @see    __set()
  784      * @param  string|array The assignment strategy to use.
  785      * @param  mixed (Optional) If assigning a named variable, use this
  786      * as the value.
  787      * @return Zend_View_Abstract Fluent interface
  788      * @throws Zend_View_Exception if $spec is neither a string nor an array,
  789      * or if an attempt to set a private or protected member is detected
  790      */
  791     public function assign($spec, $value = null)
  792     {
  793         // which strategy to use?
  794         if (is_string($spec)) {
  795             // assign by name and value
  796             if ('_' == substr($spec, 0, 1)) {
  797                 $e = new Zend_View_Exception('Setting private or protected class members is not allowed');
  798                 $e->setView($this);
  799                 throw $e;
  800             }
  801             $this->$spec = $value;
  802         } elseif (is_array($spec)) {
  803             // assign from associative array
  804             $error = false;
  805             foreach ($spec as $key => $val) {
  806                 if ('_' == substr($key, 0, 1)) {
  807                     $error = true;
  808                     break;
  809                 }
  810                 $this->$key = $val;
  811             }
  812             if ($error) {
  813                 $e = new Zend_View_Exception('Setting private or protected class members is not allowed');
  814                 $e->setView($this);
  815                 throw $e;
  816             }
  817         } else {
  818             $e = new Zend_View_Exception('assign() expects a string or array, received ' . gettype($spec));
  819             $e->setView($this);
  820             throw $e;
  821         }
  822 
  823         return $this;
  824     }
  825 
  826     /**
  827      * Return list of all assigned variables
  828      *
  829      * Returns all public properties of the object. Reflection is not used
  830      * here as testing reflection properties for visibility is buggy.
  831      *
  832      * @return array
  833      */
  834     public function getVars()
  835     {
  836         $vars   = get_object_vars($this);
  837         foreach ($vars as $key => $value) {
  838             if ('_' == substr($key, 0, 1)) {
  839                 unset($vars[$key]);
  840             }
  841         }
  842 
  843         return $vars;
  844     }
  845 
  846     /**
  847      * Clear all assigned variables
  848      *
  849      * Clears all variables assigned to Zend_View either via {@link assign()} or
  850      * property overloading ({@link __set()}).
  851      *
  852      * @return void
  853      */
  854     public function clearVars()
  855     {
  856         $vars   = get_object_vars($this);
  857         foreach ($vars as $key => $value) {
  858             if ('_' != substr($key, 0, 1)) {
  859                 unset($this->$key);
  860             }
  861         }
  862     }
  863 
  864     /**
  865      * Processes a view script and returns the output.
  866      *
  867      * @param string $name The script name to process.
  868      * @return string The script output.
  869      */
  870     public function render($name)
  871     {
  872         // find the script file name using the parent private method
  873         $this->_file = $this->_script($name);
  874         unset($name); // remove $name from local scope
  875 
  876         ob_start();
  877         $this->_run($this->_file);
  878 
  879         return $this->_filter(ob_get_clean()); // filter output
  880     }
  881 
  882     /**
  883      * Escapes a value for output in a view script.
  884      *
  885      * If escaping mechanism is one of htmlspecialchars or htmlentities, uses
  886      * {@link $_encoding} setting.
  887      *
  888      * @param mixed $var The output to escape.
  889      * @return mixed The escaped value.
  890      */
  891     public function escape($var)
  892     {
  893         if (in_array($this->_escape, array('htmlspecialchars', 'htmlentities'))) {
  894             return call_user_func($this->_escape, $var, ENT_COMPAT, $this->_encoding);
  895         }
  896 
  897         if (1 == func_num_args()) {
  898             return call_user_func($this->_escape, $var);
  899         }
  900         $args = func_get_args();
  901         return call_user_func_array($this->_escape, $args);
  902     }
  903 
  904     /**
  905      * Set encoding to use with htmlentities() and htmlspecialchars()
  906      *
  907      * @param string $encoding
  908      * @return Zend_View_Abstract
  909      */
  910     public function setEncoding($encoding)
  911     {
  912         $this->_encoding = $encoding;
  913         return $this;
  914     }
  915 
  916     /**
  917      * Return current escape encoding
  918      *
  919      * @return string
  920      */
  921     public function getEncoding()
  922     {
  923         return $this->_encoding;
  924     }
  925 
  926     /**
  927      * Enable or disable strict vars
  928      *
  929      * If strict variables are enabled, {@link __get()} will raise a notice
  930      * when a variable is not defined.
  931      *
  932      * Use in conjunction with {@link Zend_View_Helper_DeclareVars the declareVars() helper}
  933      * to enforce strict variable handling in your view scripts.
  934      *
  935      * @param  boolean $flag
  936      * @return Zend_View_Abstract
  937      */
  938     public function strictVars($flag = true)
  939     {
  940         $this->_strictVars = ($flag) ? true : false;
  941 
  942         return $this;
  943     }
  944 
  945     /**
  946      * Finds a view script from the available directories.
  947      *
  948      * @param string $name The base name of the script.
  949      * @return void
  950      */
  951     protected function _script($name)
  952     {
  953         if ($this->isLfiProtectionOn() && preg_match('#\.\.[\\\/]#', $name)) {
  954             $e = new Zend_View_Exception('Requested scripts may not include parent directory traversal ("../", "..\\" notation)');
  955             $e->setView($this);
  956             throw $e;
  957         }
  958 
  959         if (0 == count($this->_path['script'])) {
  960             $e = new Zend_View_Exception('no view script directory set; unable to determine location for view script');
  961             $e->setView($this);
  962             throw $e;
  963         }
  964 
  965         foreach ($this->_path['script'] as $dir) {
  966             if (is_readable($dir . $name)) {
  967                 return $dir . $name;
  968             }
  969         }
  970 
  971         $message = "script '$name' not found in path ("
  972                  . implode(PATH_SEPARATOR, $this->_path['script'])
  973                  . ")";
  974         $e = new Zend_View_Exception($message);
  975         $e->setView($this);
  976         throw $e;
  977     }
  978 
  979     /**
  980      * Applies the filter callback to a buffer.
  981      *
  982      * @param string $buffer The buffer contents.
  983      * @return string The filtered buffer.
  984      */
  985     private function _filter($buffer)
  986     {
  987         // loop through each filter class
  988         foreach ($this->_filter as $name) {
  989             // load and apply the filter class
  990             $filter = $this->getFilter($name);
  991             $buffer = call_user_func(array($filter, 'filter'), $buffer);
  992         }
  993 
  994         // done!
  995         return $buffer;
  996     }
  997 
  998     /**
  999      * Adds paths to the path stack in LIFO order.
 1000      *
 1001      * Zend_View::_addPath($type, 'dirname') adds one directory
 1002      * to the path stack.
 1003      *
 1004      * Zend_View::_addPath($type, $array) adds one directory for
 1005      * each array element value.
 1006      *
 1007      * In the case of filter and helper paths, $prefix should be used to
 1008      * specify what class prefix to use with the given path.
 1009      *
 1010      * @param string $type The path type ('script', 'helper', or 'filter').
 1011      * @param string|array $path The path specification.
 1012      * @param string $prefix Class prefix to use with path (helpers and filters
 1013      * only)
 1014      * @return void
 1015      */
 1016     private function _addPath($type, $path, $prefix = null)
 1017     {
 1018         foreach ((array) $path as $dir) {
 1019             // attempt to strip any possible separator and
 1020             // append the system directory separator
 1021             $dir  = rtrim($dir, '/');
 1022             $dir  = rtrim($dir, '\\');
 1023             $dir .= '/';
 1024 
 1025             switch ($type) {
 1026                 case 'script':
 1027                     // add to the top of the stack.
 1028                     array_unshift($this->_path[$type], $dir);
 1029                     break;
 1030                 case 'filter':
 1031                 case 'helper':
 1032                 default:
 1033                     // add as array with prefix and dir keys
 1034                     array_unshift($this->_path[$type], array('prefix' => $prefix, 'dir' => $dir));
 1035                     break;
 1036             }
 1037         }
 1038     }
 1039 
 1040     /**
 1041      * Resets the path stack for helpers and filters.
 1042      *
 1043      * @param string $type The path type ('helper' or 'filter').
 1044      * @param string|array $path The directory (-ies) to set as the path.
 1045      * @param string $classPrefix Class prefix to apply to elements of $path
 1046      */
 1047     private function _setPath($type, $path, $classPrefix = null)
 1048     {
 1049         $dir = DIRECTORY_SEPARATOR . ucfirst($type) . DIRECTORY_SEPARATOR;
 1050 
 1051         switch ($type) {
 1052             case 'script':
 1053                 $this->_path[$type] = array(dirname(__FILE__) . $dir);
 1054                 $this->_addPath($type, $path);
 1055                 break;
 1056             case 'filter':
 1057             case 'helper':
 1058             default:
 1059                 $this->_path[$type] = array(array(
 1060                     'prefix' => 'Zend_View_' . ucfirst($type) . '_',
 1061                     'dir'    => dirname(__FILE__) . $dir
 1062                 ));
 1063                 $this->_addPath($type, $path, $classPrefix);
 1064                 break;
 1065         }
 1066     }
 1067 
 1068     /**
 1069      * Return all paths for a given path type
 1070      *
 1071      * @param string $type The path type  ('helper', 'filter', 'script')
 1072      * @return array
 1073      */
 1074     private function _getPaths($type)
 1075     {
 1076         return $this->_path[$type];
 1077     }
 1078 
 1079     /**
 1080      * Register helper class as loaded
 1081      *
 1082      * @param  string $name
 1083      * @param  string $class
 1084      * @param  string $file path to class file
 1085      * @return void
 1086      */
 1087     private function _setHelperClass($name, $class, $file)
 1088     {
 1089         $this->_helperLoadedDir[$name] = $file;
 1090         $this->_helperLoaded[$name]    = $class;
 1091     }
 1092 
 1093     /**
 1094      * Register filter class as loaded
 1095      *
 1096      * @param  string $name
 1097      * @param  string $class
 1098      * @param  string $file path to class file
 1099      * @return void
 1100      */
 1101     private function _setFilterClass($name, $class, $file)
 1102     {
 1103         $this->_filterLoadedDir[$name] = $file;
 1104         $this->_filterLoaded[$name]    = $class;
 1105     }
 1106 
 1107     /**
 1108      * Add a prefixPath for a plugin type
 1109      *
 1110      * @param  string $type
 1111      * @param  string $classPrefix
 1112      * @param  array $paths
 1113      * @return Zend_View_Abstract
 1114      */
 1115     private function _addPluginPath($type, $classPrefix, array $paths)
 1116     {
 1117         $loader = $this->getPluginLoader($type);
 1118         foreach ($paths as $path) {
 1119             $loader->addPrefixPath($classPrefix, $path);
 1120         }
 1121         return $this;
 1122     }
 1123 
 1124     /**
 1125      * Get a path to a given plugin class of a given type
 1126      *
 1127      * @param  string $type
 1128      * @param  string $name
 1129      * @return string|false
 1130      */
 1131     private function _getPluginPath($type, $name)
 1132     {
 1133         $loader = $this->getPluginLoader($type);
 1134         if ($loader->isLoaded($name)) {
 1135             return $loader->getClassPath($name);
 1136         }
 1137 
 1138         try {
 1139             $loader->load($name);
 1140             return $loader->getClassPath($name);
 1141         } catch (Zend_Loader_Exception $e) {
 1142             return false;
 1143         }
 1144     }
 1145 
 1146     /**
 1147      * Retrieve a plugin object
 1148      *
 1149      * @param  string $type
 1150      * @param  string $name
 1151      * @return object
 1152      */
 1153     private function _getPlugin($type, $name)
 1154     {
 1155         $name = ucfirst($name);
 1156         switch ($type) {
 1157             case 'filter':
 1158                 $storeVar = '_filterClass';
 1159                 $store    = $this->_filterClass;
 1160                 break;
 1161             case 'helper':
 1162                 $storeVar = '_helper';
 1163                 $store    = $this->_helper;
 1164                 break;
 1165         }
 1166 
 1167         if (!isset($store[$name])) {
 1168             $class = $this->getPluginLoader($type)->load($name);
 1169             $store[$name] = new $class();
 1170             if (method_exists($store[$name], 'setView')) {
 1171                 $store[$name]->setView($this);
 1172             }
 1173         }
 1174 
 1175         $this->$storeVar = $store;
 1176         return $store[$name];
 1177     }
 1178 
 1179     /**
 1180      * Use to include the view script in a scope that only allows public
 1181      * members.
 1182      *
 1183      * @return mixed
 1184      */
 1185     abstract protected function _run();
 1186 }