Helper.php 24 KB

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