Helper.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. <?php
  2. /**
  3. * Backend for helpers.
  4. *
  5. * Internal methods for the Helpers.
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package Cake.View
  18. * @since CakePHP(tm) v 0.2.9
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('Router', 'Routing');
  22. /**
  23. * Abstract base class for all other Helpers in CakePHP.
  24. * Provides common methods and features.
  25. *
  26. * @package Cake.View
  27. */
  28. class Helper extends Object {
  29. /**
  30. * List of helpers used by this helper
  31. *
  32. * @var array
  33. */
  34. public $helpers = array();
  35. /**
  36. * A helper lookup table used to lazy load helper objects.
  37. *
  38. * @var array
  39. */
  40. protected $_helperMap = array();
  41. /**
  42. * The current theme name if any.
  43. *
  44. * @var string
  45. */
  46. public $theme = null;
  47. /**
  48. * Request object
  49. *
  50. * @var CakeRequest
  51. */
  52. public $request = null;
  53. /**
  54. * Plugin path
  55. *
  56. * @var string
  57. */
  58. public $plugin = null;
  59. /**
  60. * Holds the fields array('field_name' => array('type'=> 'string', 'length'=> 100),
  61. * primaryKey and validates array('field_name')
  62. *
  63. * @var array
  64. */
  65. public $fieldset = array();
  66. /**
  67. * Holds tag templates.
  68. *
  69. * @var array
  70. */
  71. public $tags = array();
  72. /**
  73. * Holds the content to be cleaned.
  74. *
  75. * @var mixed
  76. */
  77. protected $_tainted = null;
  78. /**
  79. * Holds the cleaned content.
  80. *
  81. * @var mixed
  82. */
  83. protected $_cleaned = null;
  84. /**
  85. * The View instance this helper is attached to
  86. *
  87. * @var View
  88. */
  89. protected $_View;
  90. /**
  91. * A list of strings that should be treated as suffixes, or
  92. * sub inputs for a parent input. This is used for date/time
  93. * inputs primarily.
  94. *
  95. * @var array
  96. */
  97. protected $_fieldSuffixes = array(
  98. 'year', 'month', 'day', 'hour', 'min', 'second', 'meridian'
  99. );
  100. /**
  101. * The name of the current model entities are in scope of.
  102. *
  103. * @see Helper::setEntity()
  104. * @var string
  105. */
  106. protected $_modelScope;
  107. /**
  108. * The name of the current model association entities are in scope of.
  109. *
  110. * @see Helper::setEntity()
  111. * @var string
  112. */
  113. protected $_association;
  114. /**
  115. * The dot separated list of elements the current field entity is for.
  116. *
  117. * @see Helper::setEntity()
  118. * @var string
  119. */
  120. protected $_entityPath;
  121. /**
  122. * Default Constructor
  123. *
  124. * @param View $View The View this helper is being attached to.
  125. * @param array $settings Configuration settings for the helper.
  126. */
  127. public function __construct(View $View, $settings = array()) {
  128. $this->_View = $View;
  129. $this->request = $View->request;
  130. if (!empty($this->helpers)) {
  131. $this->_helperMap = ObjectCollection::normalizeObjectArray($this->helpers);
  132. }
  133. }
  134. /**
  135. * Provide non fatal errors on missing method calls.
  136. *
  137. * @param string $method Method to invoke
  138. * @param array $params Array of params for the method.
  139. * @return void
  140. */
  141. public function __call($method, $params) {
  142. trigger_error(__d('cake_dev', 'Method %1$s::%2$s does not exist', get_class($this), $method), E_USER_WARNING);
  143. }
  144. /**
  145. * Lazy loads helpers. Provides access to deprecated request properties as well.
  146. *
  147. * @param string $name Name of the property being accessed.
  148. * @return mixed Helper or property found at $name
  149. */
  150. public function __get($name) {
  151. if (isset($this->_helperMap[$name]) && !isset($this->{$name})) {
  152. $settings = array_merge((array)$this->_helperMap[$name]['settings'], array('enabled' => false));
  153. $this->{$name} = $this->_View->loadHelper($this->_helperMap[$name]['class'], $settings);
  154. }
  155. if (isset($this->{$name})) {
  156. return $this->{$name};
  157. }
  158. switch ($name) {
  159. case 'base':
  160. case 'here':
  161. case 'webroot':
  162. case 'data':
  163. return $this->request->{$name};
  164. case 'action':
  165. return isset($this->request->params['action']) ? $this->request->params['action'] : '';
  166. case 'params':
  167. return $this->request;
  168. }
  169. }
  170. /**
  171. * Provides backwards compatiblity access for setting values to the request object.
  172. *
  173. * @param string $name Name of the property being accessed.
  174. * @param mixed $value
  175. * @return mixed Return the $value
  176. */
  177. public function __set($name, $value) {
  178. switch ($name) {
  179. case 'base':
  180. case 'here':
  181. case 'webroot':
  182. case 'data':
  183. return $this->request->{$name} = $value;
  184. case 'action':
  185. return $this->request->params['action'] = $value;
  186. }
  187. return $this->{$name} = $value;
  188. }
  189. /**
  190. * Finds URL for specified action.
  191. *
  192. * Returns a URL pointing at the provided parameters.
  193. *
  194. * @param mixed $url Either a relative string url like `/products/view/23` or
  195. * an array of url parameters. Using an array for urls will allow you to leverage
  196. * the reverse routing features of CakePHP.
  197. * @param boolean $full If true, the full base URL will be prepended to the result
  198. * @return string Full translated URL with base path.
  199. * @link http://book.cakephp.org/view/1448/url
  200. */
  201. public function url($url = null, $full = false) {
  202. return h(Router::url($url, $full));
  203. }
  204. /**
  205. * Checks if a file exists when theme is used, if no file is found default location is returned
  206. *
  207. * @param string $file The file to create a webroot path to.
  208. * @return string Web accessible path to file.
  209. */
  210. public function webroot($file) {
  211. $asset = explode('?', $file);
  212. $asset[1] = isset($asset[1]) ? '?' . $asset[1] : null;
  213. $webPath = "{$this->request->webroot}" . $asset[0];
  214. $file = $asset[0];
  215. if (!empty($this->theme)) {
  216. $file = trim($file, '/');
  217. $theme = $this->theme . '/';
  218. if (DS === '\\') {
  219. $file = str_replace('/', '\\', $file);
  220. }
  221. if (file_exists(Configure::read('App.www_root') . 'theme' . DS . $this->theme . DS . $file)) {
  222. $webPath = "{$this->request->webroot}theme/" . $theme . $asset[0];
  223. } else {
  224. $themePath = App::themePath($this->theme);
  225. $path = $themePath . 'webroot' . DS . $file;
  226. if (file_exists($path)) {
  227. $webPath = "{$this->request->webroot}theme/" . $theme . $asset[0];
  228. }
  229. }
  230. }
  231. if (strpos($webPath, '//') !== false) {
  232. return str_replace('//', '/', $webPath . $asset[1]);
  233. }
  234. return $webPath . $asset[1];
  235. }
  236. /**
  237. * Adds a timestamp to a file based resource based on the value of `Asset.timestamp` in
  238. * Configure. If Asset.timestamp is true and debug > 0, or Asset.timestamp == 'force'
  239. * a timestamp will be added.
  240. *
  241. * @param string $path The file path to timestamp, the path must be inside WWW_ROOT
  242. * @return string Path with a timestamp added, or not.
  243. */
  244. public function assetTimestamp($path) {
  245. $stamp = Configure::read('Asset.timestamp');
  246. $timestampEnabled = $stamp === 'force' || ($stamp === true && Configure::read('debug') > 0);
  247. if ($timestampEnabled && strpos($path, '?') === false) {
  248. $filepath = preg_replace('/^' . preg_quote($this->request->webroot, '/') . '/', '', $path);
  249. $webrootPath = WWW_ROOT . str_replace('/', DS, $filepath);
  250. if (file_exists($webrootPath)) {
  251. return $path . '?' . @filemtime($webrootPath);
  252. }
  253. $segments = explode('/', ltrim($filepath, '/'));
  254. if ($segments[0] === 'theme') {
  255. $theme = $segments[1];
  256. unset($segments[0], $segments[1]);
  257. $themePath = App::themePath($theme) . 'webroot' . DS . implode(DS, $segments);
  258. return $path . '?' . @filemtime($themePath);
  259. } else {
  260. $plugin = Inflector::camelize($segments[0]);
  261. if (CakePlugin::loaded($plugin)) {
  262. unset($segments[0]);
  263. $pluginPath = CakePlugin::path($plugin) . 'webroot' . DS . implode(DS, $segments);
  264. return $path . '?' . @filemtime($pluginPath);
  265. }
  266. }
  267. }
  268. return $path;
  269. }
  270. /**
  271. * Used to remove harmful tags from content. Removes a number of well known XSS attacks
  272. * from content. However, is not guaranteed to remove all possibilities. Escaping
  273. * content is the best way to prevent all possible attacks.
  274. *
  275. * @param mixed $output Either an array of strings to clean or a single string to clean.
  276. * @return string|array cleaned content for output
  277. */
  278. public function clean($output) {
  279. $this->_reset();
  280. if (empty($output)) {
  281. return null;
  282. }
  283. if (is_array($output)) {
  284. foreach ($output as $key => $value) {
  285. $return[$key] = $this->clean($value);
  286. }
  287. return $return;
  288. }
  289. $this->_tainted = $output;
  290. $this->_clean();
  291. return $this->_cleaned;
  292. }
  293. /**
  294. * Returns a space-delimited string with items of the $options array. If a
  295. * key of $options array happens to be one of:
  296. *
  297. * - 'compact'
  298. * - 'checked'
  299. * - 'declare'
  300. * - 'readonly'
  301. * - 'disabled'
  302. * - 'selected'
  303. * - 'defer'
  304. * - 'ismap'
  305. * - 'nohref'
  306. * - 'noshade'
  307. * - 'nowrap'
  308. * - 'multiple'
  309. * - 'noresize'
  310. *
  311. * And its value is one of:
  312. *
  313. * - '1' (string)
  314. * - 1 (integer)
  315. * - true (boolean)
  316. * - 'true' (string)
  317. *
  318. * Then the value will be reset to be identical with key's name.
  319. * If the value is not one of these 3, the parameter is not output.
  320. *
  321. * 'escape' is a special option in that it controls the conversion of
  322. * attributes to their html-entity encoded equivalents. Set to false to disable html-encoding.
  323. *
  324. * If value for any option key is set to `null` or `false`, that option will be excluded from output.
  325. *
  326. * @param array $options Array of options.
  327. * @param array $exclude Array of options to be excluded, the options here will not be part of the return.
  328. * @param string $insertBefore String to be inserted before options.
  329. * @param string $insertAfter String to be inserted after options.
  330. * @return string Composed attributes.
  331. * @deprecated This method has been moved to HtmlHelper
  332. */
  333. protected function _parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) {
  334. if (!is_string($options)) {
  335. $options = (array) $options + array('escape' => true);
  336. if (!is_array($exclude)) {
  337. $exclude = array();
  338. }
  339. $exclude = array('escape' => true) + array_flip($exclude);
  340. $escape = $options['escape'];
  341. $attributes = array();
  342. foreach ($options as $key => $value) {
  343. if (!isset($exclude[$key]) && $value !== false && $value !== null) {
  344. $attributes[] = $this->_formatAttribute($key, $value, $escape);
  345. }
  346. }
  347. $out = implode(' ', $attributes);
  348. } else {
  349. $out = $options;
  350. }
  351. return $out ? $insertBefore . $out . $insertAfter : '';
  352. }
  353. /**
  354. * Formats an individual attribute, and returns the string value of the composed attribute.
  355. * Works with minimized attributes that have the same value as their name such as 'disabled' and 'checked'
  356. *
  357. * @param string $key The name of the attribute to create
  358. * @param string $value The value of the attribute to create.
  359. * @param boolean $escape Define if the value must be escaped
  360. * @return string The composed attribute.
  361. * @deprecated This method has been moved to HtmlHelper
  362. */
  363. protected function _formatAttribute($key, $value, $escape = true) {
  364. $attribute = '';
  365. if (is_array($value)) {
  366. $value = '';
  367. }
  368. if (is_numeric($key)) {
  369. $attribute = sprintf($this->_minimizedAttributeFormat, $value, $value);
  370. } elseif (in_array($key, $this->_minimizedAttributes)) {
  371. if ($value === 1 || $value === true || $value === 'true' || $value === '1' || $value == $key) {
  372. $attribute = sprintf($this->_minimizedAttributeFormat, $key, $key);
  373. }
  374. } else {
  375. $attribute = sprintf($this->_attributeFormat, $key, ($escape ? h($value) : $value));
  376. }
  377. return $attribute;
  378. }
  379. /**
  380. * Sets this helper's model and field properties to the dot-separated value-pair in $entity.
  381. *
  382. * @param mixed $entity A field name, like "ModelName.fieldName" or "ModelName.ID.fieldName"
  383. * @param boolean $setScope Sets the view scope to the model specified in $tagValue
  384. * @return void
  385. */
  386. public function setEntity($entity, $setScope = false) {
  387. if ($entity === null) {
  388. $this->_modelScope = false;
  389. }
  390. if ($setScope === true) {
  391. $this->_modelScope = $entity;
  392. }
  393. $parts = array_values(Set::filter(explode('.', $entity), true));
  394. if (empty($parts)) {
  395. return;
  396. }
  397. $count = count($parts);
  398. $lastPart = isset($parts[$count - 1]) ? $parts[$count - 1] : null;
  399. // Either 'body' or 'date.month' type inputs.
  400. if (
  401. ($count === 1 && $this->_modelScope && $setScope == false) ||
  402. (
  403. $count === 2 &&
  404. in_array($lastPart, $this->_fieldSuffixes) &&
  405. $this->_modelScope &&
  406. $parts[0] !== $this->_modelScope
  407. )
  408. ) {
  409. $entity = $this->_modelScope . '.' . $entity;
  410. }
  411. // 0.name, 0.created.month style inputs.
  412. if (
  413. $count >= 2 && is_numeric($parts[0]) && !is_numeric($parts[1]) && $this->_modelScope
  414. ) {
  415. $entity = $this->_modelScope . '.' . $entity;
  416. }
  417. $this->_association = null;
  418. // habtm models are special
  419. if (
  420. isset($this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type']) &&
  421. $this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type'] === 'multiple'
  422. ) {
  423. $this->_association = $parts[0];
  424. $entity = $parts[0] . '.' . $parts[0];
  425. } else {
  426. // check for associated model.
  427. $reversed = array_reverse($parts);
  428. foreach ($reversed as $part) {
  429. if (
  430. !isset($this->fieldset[$this->_modelScope]['fields'][$part]) &&
  431. preg_match('/^[A-Z]/', $part)
  432. ) {
  433. $this->_association = $part;
  434. break;
  435. }
  436. }
  437. }
  438. $this->_entityPath = $entity;
  439. return;
  440. }
  441. /**
  442. * Returns the entity reference of the current context as an array of identity parts
  443. *
  444. * @return array An array containing the identity elements of an entity
  445. */
  446. public function entity() {
  447. return explode('.', $this->_entityPath);
  448. }
  449. /**
  450. * Gets the currently-used model of the rendering context.
  451. *
  452. * @return string
  453. */
  454. public function model() {
  455. if ($this->_association) {
  456. return $this->_association;
  457. }
  458. return $this->_modelScope;
  459. }
  460. /**
  461. * Gets the currently-used model field of the rendering context.
  462. *
  463. * @return string
  464. */
  465. public function field() {
  466. $entity = $this->entity();
  467. $count = count($entity);
  468. $last = $entity[$count - 1];
  469. if (in_array($last, $this->_fieldSuffixes)) {
  470. $last = isset($entity[$count - 2]) ? $entity[$count - 2] : null;
  471. }
  472. return $last;
  473. }
  474. /**
  475. * Generates a DOM ID for the selected element, if one is not set.
  476. * Uses the current View::entity() settings to generate a CamelCased id attribute.
  477. *
  478. * @param mixed $options Either an array of html attributes to add $id into, or a string
  479. * with a view entity path to get a domId for.
  480. * @param string $id The name of the 'id' attribute.
  481. * @return mixed If $options was an array, an array will be returned with $id set. If a string
  482. * was supplied, a string will be returned.
  483. * @todo Refactor this method to not have as many input/output options.
  484. */
  485. public function domId($options = null, $id = 'id') {
  486. if (is_array($options) && array_key_exists($id, $options) && $options[$id] === null) {
  487. unset($options[$id]);
  488. return $options;
  489. } elseif (!is_array($options) && $options !== null) {
  490. $this->setEntity($options);
  491. return $this->domId();
  492. }
  493. $entity = $this->entity();
  494. $model = array_shift($entity);
  495. $dom = $model . join('', array_map(array('Inflector', 'camelize'), $entity));
  496. if (is_array($options) && !array_key_exists($id, $options)) {
  497. $options[$id] = $dom;
  498. } elseif ($options === null) {
  499. return $dom;
  500. }
  501. return $options;
  502. }
  503. /**
  504. * Gets the input field name for the current tag. Creates input name attributes
  505. * using CakePHP's data[Model][field] formatting.
  506. *
  507. * @param mixed $options If an array, should be an array of attributes that $key needs to be added to.
  508. * If a string or null, will be used as the View entity.
  509. * @param string $field
  510. * @param string $key The name of the attribute to be set, defaults to 'name'
  511. * @return mixed If an array was given for $options, an array with $key set will be returned.
  512. * If a string was supplied a string will be returned.
  513. * @todo Refactor this method to not have as many input/output options.
  514. */
  515. protected function _name($options = array(), $field = null, $key = 'name') {
  516. if ($options === null) {
  517. $options = array();
  518. } elseif (is_string($options)) {
  519. $field = $options;
  520. $options = 0;
  521. }
  522. if (!empty($field)) {
  523. $this->setEntity($field);
  524. }
  525. if (is_array($options) && array_key_exists($key, $options)) {
  526. return $options;
  527. }
  528. switch ($field) {
  529. case '_method':
  530. $name = $field;
  531. break;
  532. default:
  533. $name = 'data[' . implode('][', $this->entity()) . ']';
  534. break;
  535. }
  536. if (is_array($options)) {
  537. $options[$key] = $name;
  538. return $options;
  539. } else {
  540. return $name;
  541. }
  542. }
  543. /**
  544. * Gets the data for the current tag
  545. *
  546. * @param mixed $options If an array, should be an array of attributes that $key needs to be added to.
  547. * If a string or null, will be used as the View entity.
  548. * @param string $field
  549. * @param string $key The name of the attribute to be set, defaults to 'value'
  550. * @return mixed If an array was given for $options, an array with $key set will be returned.
  551. * If a string was supplied a string will be returned.
  552. * @todo Refactor this method to not have as many input/output options.
  553. */
  554. public function value($options = array(), $field = null, $key = 'value') {
  555. if ($options === null) {
  556. $options = array();
  557. } elseif (is_string($options)) {
  558. $field = $options;
  559. $options = 0;
  560. }
  561. if (is_array($options) && isset($options[$key])) {
  562. return $options;
  563. }
  564. if (!empty($field)) {
  565. $this->setEntity($field);
  566. }
  567. $result = null;
  568. $data = $this->request->data;
  569. $entity = $this->entity();
  570. if (!empty($data) && !empty($entity)) {
  571. $result = Set::extract(implode('.', $entity), $data);
  572. }
  573. $habtmKey = $this->field();
  574. if (empty($result) && isset($data[$habtmKey][$habtmKey]) && is_array($data[$habtmKey])) {
  575. $result = $data[$habtmKey][$habtmKey];
  576. } elseif (empty($result) && isset($data[$habtmKey]) && is_array($data[$habtmKey])) {
  577. if (ClassRegistry::isKeySet($habtmKey)) {
  578. $model = ClassRegistry::getObject($habtmKey);
  579. $result = $this->_selectedArray($data[$habtmKey], $model->primaryKey);
  580. }
  581. }
  582. if (is_array($options)) {
  583. if ($result === null && isset($options['default'])) {
  584. $result = $options['default'];
  585. }
  586. unset($options['default']);
  587. }
  588. if (is_array($options)) {
  589. $options[$key] = $result;
  590. return $options;
  591. } else {
  592. return $result;
  593. }
  594. }
  595. /**
  596. * Sets the defaults for an input tag. Will set the
  597. * name, value, and id attributes for an array of html attributes. Will also
  598. * add a 'form-error' class if the field contains validation errors.
  599. *
  600. * @param string $field The field name to initialize.
  601. * @param array $options Array of options to use while initializing an input field.
  602. * @return array Array options for the form input.
  603. */
  604. protected function _initInputField($field, $options = array()) {
  605. if ($field !== null) {
  606. $this->setEntity($field);
  607. }
  608. $options = (array)$options;
  609. $options = $this->_name($options);
  610. $options = $this->value($options);
  611. $options = $this->domId($options);
  612. if ($this->tagIsInvalid() !== false) {
  613. $options = $this->addClass($options, 'form-error');
  614. }
  615. return $options;
  616. }
  617. /**
  618. * Adds the given class to the element options
  619. *
  620. * @param array $options Array options/attributes to add a class to
  621. * @param string $class The classname being added.
  622. * @param string $key the key to use for class.
  623. * @return array Array of options with $key set.
  624. */
  625. public function addClass($options = array(), $class = null, $key = 'class') {
  626. if (isset($options[$key]) && trim($options[$key]) != '') {
  627. $options[$key] .= ' ' . $class;
  628. } else {
  629. $options[$key] = $class;
  630. }
  631. return $options;
  632. }
  633. /**
  634. * Returns a string generated by a helper method
  635. *
  636. * This method can be overridden in subclasses to do generalized output post-processing
  637. *
  638. * @param string $str String to be output.
  639. * @return string
  640. * @deprecated This method will be removed in future versions.
  641. */
  642. public function output($str) {
  643. return $str;
  644. }
  645. /**
  646. * Before render callback. beforeRender is called before the view file is rendered.
  647. *
  648. * Overridden in subclasses.
  649. *
  650. * @param string $viewFile The view file that is going to be rendered
  651. * @return void
  652. */
  653. public function beforeRender($viewFile) {
  654. }
  655. /**
  656. * After render callback. afterRender is called after the view file is rendered
  657. * but before the layout has been rendered.
  658. *
  659. * Overridden in subclasses.
  660. *
  661. * @param string $viewFile The view file that was rendered.
  662. * @return void
  663. */
  664. public function afterRender($viewFile) {
  665. }
  666. /**
  667. * Before layout callback. beforeLayout is called before the layout is rendered.
  668. *
  669. * Overridden in subclasses.
  670. *
  671. * @param string $layoutFile The layout about to be rendered.
  672. * @return void
  673. */
  674. public function beforeLayout($layoutFile) {
  675. }
  676. /**
  677. * After layout callback. afterLayout is called after the layout has rendered.
  678. *
  679. * Overridden in subclasses.
  680. *
  681. * @param string $layoutFile The layout file that was rendered.
  682. * @return void
  683. */
  684. public function afterLayout($layoutFile) {
  685. }
  686. /**
  687. * Transforms a recordset from a hasAndBelongsToMany association to a list of selected
  688. * options for a multiple select element
  689. *
  690. * @param mixed $data
  691. * @param string $key
  692. * @return array
  693. */
  694. protected function _selectedArray($data, $key = 'id') {
  695. if (!is_array($data)) {
  696. $model = $data;
  697. if (!empty($this->request->data[$model][$model])) {
  698. return $this->request->data[$model][$model];
  699. }
  700. if (!empty($this->request->data[$model])) {
  701. $data = $this->request->data[$model];
  702. }
  703. }
  704. $array = array();
  705. if (!empty($data)) {
  706. foreach ($data as $row) {
  707. if (isset($row[$key])) {
  708. $array[$row[$key]] = $row[$key];
  709. }
  710. }
  711. }
  712. return empty($array) ? null : $array;
  713. }
  714. /**
  715. * Resets the vars used by Helper::clean() to null
  716. *
  717. * @return void
  718. */
  719. protected function _reset() {
  720. $this->_tainted = null;
  721. $this->_cleaned = null;
  722. }
  723. /**
  724. * Removes harmful content from output
  725. *
  726. * @return void
  727. */
  728. protected function _clean() {
  729. if (get_magic_quotes_gpc()) {
  730. $this->_cleaned = stripslashes($this->_tainted);
  731. } else {
  732. $this->_cleaned = $this->_tainted;
  733. }
  734. $this->_cleaned = str_replace(array("&amp;", "&lt;", "&gt;"), array("&amp;amp;", "&amp;lt;", "&amp;gt;"), $this->_cleaned);
  735. $this->_cleaned = preg_replace('#(&\#*\w+)[\x00-\x20]+;#u', "$1;", $this->_cleaned);
  736. $this->_cleaned = preg_replace('#(&\#x*)([0-9A-F]+);*#iu', "$1$2;", $this->_cleaned);
  737. $this->_cleaned = html_entity_decode($this->_cleaned, ENT_COMPAT, "UTF-8");
  738. $this->_cleaned = preg_replace('#(<[^>]+[\x00-\x20\"\'\/])(on|xmlns)[^>]*>#iUu', "$1>", $this->_cleaned);
  739. $this->_cleaned = preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*)[\\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iUu', '$1=$2nojavascript...', $this->_cleaned);
  740. $this->_cleaned = preg_replace('#([a-z]*)[\x00-\x20]*=([\'\"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iUu', '$1=$2novbscript...', $this->_cleaned);
  741. $this->_cleaned = preg_replace('#([a-z]*)[\x00-\x20]*=*([\'\"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#iUu','$1=$2nomozbinding...', $this->_cleaned);
  742. $this->_cleaned = preg_replace('#([a-z]*)[\x00-\x20]*=([\'\"]*)[\x00-\x20]*data[\x00-\x20]*:#Uu', '$1=$2nodata...', $this->_cleaned);
  743. $this->_cleaned = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*expression[\x00-\x20]*\([^>]*>#iU', "$1>", $this->_cleaned);
  744. $this->_cleaned = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*behaviour[\x00-\x20]*\([^>]*>#iU', "$1>", $this->_cleaned);
  745. $this->_cleaned = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*>#iUu', "$1>", $this->_cleaned);
  746. $this->_cleaned = preg_replace('#</*\w+:\w[^>]*>#i', "", $this->_cleaned);
  747. do {
  748. $oldstring = $this->_cleaned;
  749. $this->_cleaned = preg_replace('#</*(applet|meta|xml|blink|link|style|script|embed|object|iframe|frame|frameset|ilayer|layer|bgsound|title|base)[^>]*>#i', "", $this->_cleaned);
  750. } while ($oldstring != $this->_cleaned);
  751. $this->_cleaned = str_replace(array("&amp;", "&lt;", "&gt;"), array("&amp;amp;", "&amp;lt;", "&amp;gt;"), $this->_cleaned);
  752. }
  753. }