Abstract.php 32 KB

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