Abstract.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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-2009 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. /** Zend_Loader */
  22. require_once 'Zend/Loader.php';
  23. /** Zend_Loader_PluginLoader */
  24. require_once 'Zend/Loader/PluginLoader.php';
  25. /** 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-2009 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 latin1 (ISO-8859-1)
  99. * @var string
  100. */
  101. private $_encoding = 'ISO-8859-1';
  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 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. return call_user_func($this->_escape, $var);
  816. }
  817. /**
  818. * Set encoding to use with htmlentities() and htmlspecialchars()
  819. *
  820. * @param string $encoding
  821. * @return Zend_View_Abstract
  822. */
  823. public function setEncoding($encoding)
  824. {
  825. $this->_encoding = $encoding;
  826. return $this;
  827. }
  828. /**
  829. * Return current escape encoding
  830. *
  831. * @return string
  832. */
  833. public function getEncoding()
  834. {
  835. return $this->_encoding;
  836. }
  837. /**
  838. * Enable or disable strict vars
  839. *
  840. * If strict variables are enabled, {@link __get()} will raise a notice
  841. * when a variable is not defined.
  842. *
  843. * Use in conjunction with {@link Zend_View_Helper_DeclareVars the declareVars() helper}
  844. * to enforce strict variable handling in your view scripts.
  845. *
  846. * @param boolean $flag
  847. * @return Zend_View_Abstract
  848. */
  849. public function strictVars($flag = true)
  850. {
  851. $this->_strictVars = ($flag) ? true : false;
  852. return $this;
  853. }
  854. /**
  855. * Finds a view script from the available directories.
  856. *
  857. * @param $name string The base name of the script.
  858. * @return void
  859. */
  860. protected function _script($name)
  861. {
  862. if ($this->isLfiProtectionOn() && preg_match('#\.\.[\\\/]#', $name)) {
  863. require_once 'Zend/View/Exception.php';
  864. $e = new Zend_View_Exception('Requested scripts may not include parent directory traversal ("../", "..\\" notation)');
  865. $e->setView($this);
  866. throw $e;
  867. }
  868. if (0 == count($this->_path['script'])) {
  869. require_once 'Zend/View/Exception.php';
  870. $e = new Zend_View_Exception('no view script directory set; unable to determine location for view script');
  871. $e->setView($this);
  872. throw $e;
  873. }
  874. foreach ($this->_path['script'] as $dir) {
  875. if (is_readable($dir . $name)) {
  876. return $dir . $name;
  877. }
  878. }
  879. require_once 'Zend/View/Exception.php';
  880. $message = "script '$name' not found in path ("
  881. . implode(PATH_SEPARATOR, $this->_path['script'])
  882. . ")";
  883. $e = new Zend_View_Exception($message);
  884. $e->setView($this);
  885. throw $e;
  886. }
  887. /**
  888. * Applies the filter callback to a buffer.
  889. *
  890. * @param string $buffer The buffer contents.
  891. * @return string The filtered buffer.
  892. */
  893. private function _filter($buffer)
  894. {
  895. // loop through each filter class
  896. foreach ($this->_filter as $name) {
  897. // load and apply the filter class
  898. $filter = $this->getFilter($name);
  899. $buffer = call_user_func(array($filter, 'filter'), $buffer);
  900. }
  901. // done!
  902. return $buffer;
  903. }
  904. /**
  905. * Adds paths to the path stack in LIFO order.
  906. *
  907. * Zend_View::_addPath($type, 'dirname') adds one directory
  908. * to the path stack.
  909. *
  910. * Zend_View::_addPath($type, $array) adds one directory for
  911. * each array element value.
  912. *
  913. * In the case of filter and helper paths, $prefix should be used to
  914. * specify what class prefix to use with the given path.
  915. *
  916. * @param string $type The path type ('script', 'helper', or 'filter').
  917. * @param string|array $path The path specification.
  918. * @param string $prefix Class prefix to use with path (helpers and filters
  919. * only)
  920. * @return void
  921. */
  922. private function _addPath($type, $path, $prefix = null)
  923. {
  924. foreach ((array) $path as $dir) {
  925. // attempt to strip any possible separator and
  926. // append the system directory separator
  927. $dir = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $dir);
  928. $dir = rtrim($dir, DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR)
  929. . DIRECTORY_SEPARATOR;
  930. switch ($type) {
  931. case 'script':
  932. // add to the top of the stack.
  933. array_unshift($this->_path[$type], $dir);
  934. break;
  935. case 'filter':
  936. case 'helper':
  937. default:
  938. // add as array with prefix and dir keys
  939. array_unshift($this->_path[$type], array('prefix' => $prefix, 'dir' => $dir));
  940. break;
  941. }
  942. }
  943. }
  944. /**
  945. * Resets the path stack for helpers and filters.
  946. *
  947. * @param string $type The path type ('helper' or 'filter').
  948. * @param string|array $path The directory (-ies) to set as the path.
  949. * @param string $classPrefix Class prefix to apply to elements of $path
  950. */
  951. private function _setPath($type, $path, $classPrefix = null)
  952. {
  953. $dir = DIRECTORY_SEPARATOR . ucfirst($type) . DIRECTORY_SEPARATOR;
  954. switch ($type) {
  955. case 'script':
  956. $this->_path[$type] = array(dirname(__FILE__) . $dir);
  957. $this->_addPath($type, $path);
  958. break;
  959. case 'filter':
  960. case 'helper':
  961. default:
  962. $this->_path[$type] = array(array(
  963. 'prefix' => 'Zend_View_' . ucfirst($type) . '_',
  964. 'dir' => dirname(__FILE__) . $dir
  965. ));
  966. $this->_addPath($type, $path, $classPrefix);
  967. break;
  968. }
  969. }
  970. /**
  971. * Return all paths for a given path type
  972. *
  973. * @param string $type The path type ('helper', 'filter', 'script')
  974. * @return array
  975. */
  976. private function _getPaths($type)
  977. {
  978. return $this->_path[$type];
  979. }
  980. /**
  981. * Register helper class as loaded
  982. *
  983. * @param string $name
  984. * @param string $class
  985. * @param string $file path to class file
  986. * @return void
  987. */
  988. private function _setHelperClass($name, $class, $file)
  989. {
  990. $this->_helperLoadedDir[$name] = $file;
  991. $this->_helperLoaded[$name] = $class;
  992. }
  993. /**
  994. * Register filter class as loaded
  995. *
  996. * @param string $name
  997. * @param string $class
  998. * @param string $file path to class file
  999. * @return void
  1000. */
  1001. private function _setFilterClass($name, $class, $file)
  1002. {
  1003. $this->_filterLoadedDir[$name] = $file;
  1004. $this->_filterLoaded[$name] = $class;
  1005. }
  1006. /**
  1007. * Add a prefixPath for a plugin type
  1008. *
  1009. * @param string $type
  1010. * @param string $classPrefix
  1011. * @param array $paths
  1012. * @return Zend_View_Abstract
  1013. */
  1014. private function _addPluginPath($type, $classPrefix, array $paths)
  1015. {
  1016. $loader = $this->getPluginLoader($type);
  1017. foreach ($paths as $path) {
  1018. $loader->addPrefixPath($classPrefix, $path);
  1019. }
  1020. return $this;
  1021. }
  1022. /**
  1023. * Get a path to a given plugin class of a given type
  1024. *
  1025. * @param string $type
  1026. * @param string $name
  1027. * @return string|false
  1028. */
  1029. private function _getPluginPath($type, $name)
  1030. {
  1031. $loader = $this->getPluginLoader($type);
  1032. if ($loader->isLoaded($name)) {
  1033. return $loader->getClassPath($name);
  1034. }
  1035. try {
  1036. $loader->load($name);
  1037. return $loader->getClassPath($name);
  1038. } catch (Zend_Loader_Exception $e) {
  1039. return false;
  1040. }
  1041. }
  1042. /**
  1043. * Retrieve a plugin object
  1044. *
  1045. * @param string $type
  1046. * @param string $name
  1047. * @return object
  1048. */
  1049. private function _getPlugin($type, $name)
  1050. {
  1051. $name = ucfirst($name);
  1052. switch ($type) {
  1053. case 'filter':
  1054. $storeVar = '_filterClass';
  1055. $store = $this->_filterClass;
  1056. break;
  1057. case 'helper':
  1058. $storeVar = '_helper';
  1059. $store = $this->_helper;
  1060. break;
  1061. }
  1062. if (!isset($store[$name])) {
  1063. $class = $this->getPluginLoader($type)->load($name);
  1064. $store[$name] = new $class();
  1065. if (method_exists($store[$name], 'setView')) {
  1066. $store[$name]->setView($this);
  1067. }
  1068. }
  1069. $this->$storeVar = $store;
  1070. return $store[$name];
  1071. }
  1072. /**
  1073. * Use to include the view script in a scope that only allows public
  1074. * members.
  1075. *
  1076. * @return mixed
  1077. */
  1078. abstract protected function _run();
  1079. }