PageRenderTime 55ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/View/Abstract.php

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