Abstract.php 33 KB

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