PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Cake/View/Helper.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 823 lines | 405 code | 68 blank | 350 comment | 103 complexity | 97d9718a81975cae0897b5185a105ca4 MD5 | raw file
  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 compatibility 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/2.0/en/views/helpers.html
  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. Excludes inputs with the modelScope in them.
  412. if (
  413. $count >= 2 &&
  414. is_numeric($parts[0]) &&
  415. !is_numeric($parts[1]) &&
  416. $this->_modelScope &&
  417. strpos($entity, $this->_modelScope) === false
  418. ) {
  419. $entity = $this->_modelScope . '.' . $entity;
  420. }
  421. $this->_association = null;
  422. $isHabtm = (
  423. isset($this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type']) &&
  424. $this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type'] === 'multiple' &&
  425. $count == 1
  426. );
  427. // habtm models are special
  428. if ($count == 1 && $isHabtm) {
  429. $this->_association = $parts[0];
  430. $entity = $parts[0] . '.' . $parts[0];
  431. } else {
  432. // check for associated model.
  433. $reversed = array_reverse($parts);
  434. foreach ($reversed as $i => $part) {
  435. if ($i > 0 && preg_match('/^[A-Z]/', $part)) {
  436. $this->_association = $part;
  437. break;
  438. }
  439. }
  440. }
  441. $this->_entityPath = $entity;
  442. return;
  443. }
  444. /**
  445. * Returns the entity reference of the current context as an array of identity parts
  446. *
  447. * @return array An array containing the identity elements of an entity
  448. */
  449. public function entity() {
  450. return explode('.', $this->_entityPath);
  451. }
  452. /**
  453. * Gets the currently-used model of the rendering context.
  454. *
  455. * @return string
  456. */
  457. public function model() {
  458. if ($this->_association) {
  459. return $this->_association;
  460. }
  461. return $this->_modelScope;
  462. }
  463. /**
  464. * Gets the currently-used model field of the rendering context.
  465. * Strips off field suffixes such as year, month, day, hour, min, meridian
  466. * when the current entity is longer than 2 elements.
  467. *
  468. * @return string
  469. */
  470. public function field() {
  471. $entity = $this->entity();
  472. $count = count($entity);
  473. $last = $entity[$count - 1];
  474. if ($count > 2 && in_array($last, $this->_fieldSuffixes)) {
  475. $last = isset($entity[$count - 2]) ? $entity[$count - 2] : null;
  476. }
  477. return $last;
  478. }
  479. /**
  480. * Generates a DOM ID for the selected element, if one is not set.
  481. * Uses the current View::entity() settings to generate a CamelCased id attribute.
  482. *
  483. * @param mixed $options Either an array of html attributes to add $id into, or a string
  484. * with a view entity path to get a domId for.
  485. * @param string $id The name of the 'id' attribute.
  486. * @return mixed If $options was an array, an array will be returned with $id set. If a string
  487. * was supplied, a string will be returned.
  488. * @todo Refactor this method to not have as many input/output options.
  489. */
  490. public function domId($options = null, $id = 'id') {
  491. if (is_array($options) && array_key_exists($id, $options) && $options[$id] === null) {
  492. unset($options[$id]);
  493. return $options;
  494. } elseif (!is_array($options) && $options !== null) {
  495. $this->setEntity($options);
  496. return $this->domId();
  497. }
  498. $entity = $this->entity();
  499. $model = array_shift($entity);
  500. $dom = $model . join('', array_map(array('Inflector', 'camelize'), $entity));
  501. if (is_array($options) && !array_key_exists($id, $options)) {
  502. $options[$id] = $dom;
  503. } elseif ($options === null) {
  504. return $dom;
  505. }
  506. return $options;
  507. }
  508. /**
  509. * Gets the input field name for the current tag. Creates input name attributes
  510. * using CakePHP's data[Model][field] formatting.
  511. *
  512. * @param mixed $options If an array, should be an array of attributes that $key needs to be added to.
  513. * If a string or null, will be used as the View entity.
  514. * @param string $field
  515. * @param string $key The name of the attribute to be set, defaults to 'name'
  516. * @return mixed If an array was given for $options, an array with $key set will be returned.
  517. * If a string was supplied a string will be returned.
  518. * @todo Refactor this method to not have as many input/output options.
  519. */
  520. protected function _name($options = array(), $field = null, $key = 'name') {
  521. if ($options === null) {
  522. $options = array();
  523. } elseif (is_string($options)) {
  524. $field = $options;
  525. $options = 0;
  526. }
  527. if (!empty($field)) {
  528. $this->setEntity($field);
  529. }
  530. if (is_array($options) && array_key_exists($key, $options)) {
  531. return $options;
  532. }
  533. switch ($field) {
  534. case '_method':
  535. $name = $field;
  536. break;
  537. default:
  538. $name = 'data[' . implode('][', $this->entity()) . ']';
  539. break;
  540. }
  541. if (is_array($options)) {
  542. $options[$key] = $name;
  543. return $options;
  544. } else {
  545. return $name;
  546. }
  547. }
  548. /**
  549. * Gets the data for the current tag
  550. *
  551. * @param mixed $options If an array, should be an array of attributes that $key needs to be added to.
  552. * If a string or null, will be used as the View entity.
  553. * @param string $field
  554. * @param string $key The name of the attribute to be set, defaults to 'value'
  555. * @return mixed If an array was given for $options, an array with $key set will be returned.
  556. * If a string was supplied a string will be returned.
  557. * @todo Refactor this method to not have as many input/output options.
  558. */
  559. public function value($options = array(), $field = null, $key = 'value') {
  560. if ($options === null) {
  561. $options = array();
  562. } elseif (is_string($options)) {
  563. $field = $options;
  564. $options = 0;
  565. }
  566. if (is_array($options) && isset($options[$key])) {
  567. return $options;
  568. }
  569. if (!empty($field)) {
  570. $this->setEntity($field);
  571. }
  572. $result = null;
  573. $data = $this->request->data;
  574. $entity = $this->entity();
  575. if (!empty($data) && !empty($entity)) {
  576. $result = Set::extract(implode('.', $entity), $data);
  577. }
  578. $habtmKey = $this->field();
  579. if (empty($result) && isset($data[$habtmKey][$habtmKey]) && is_array($data[$habtmKey])) {
  580. $result = $data[$habtmKey][$habtmKey];
  581. } elseif (empty($result) && isset($data[$habtmKey]) && is_array($data[$habtmKey])) {
  582. if (ClassRegistry::isKeySet($habtmKey)) {
  583. $model = ClassRegistry::getObject($habtmKey);
  584. $result = $this->_selectedArray($data[$habtmKey], $model->primaryKey);
  585. }
  586. }
  587. if (is_array($options)) {
  588. if ($result === null && isset($options['default'])) {
  589. $result = $options['default'];
  590. }
  591. unset($options['default']);
  592. }
  593. if (is_array($options)) {
  594. $options[$key] = $result;
  595. return $options;
  596. } else {
  597. return $result;
  598. }
  599. }
  600. /**
  601. * Sets the defaults for an input tag. Will set the
  602. * name, value, and id attributes for an array of html attributes. Will also
  603. * add a 'form-error' class if the field contains validation errors.
  604. *
  605. * @param string $field The field name to initialize.
  606. * @param array $options Array of options to use while initializing an input field.
  607. * @return array Array options for the form input.
  608. */
  609. protected function _initInputField($field, $options = array()) {
  610. if ($field !== null) {
  611. $this->setEntity($field);
  612. }
  613. $options = (array)$options;
  614. $options = $this->_name($options);
  615. $options = $this->value($options);
  616. $options = $this->domId($options);
  617. return $options;
  618. }
  619. /**
  620. * Adds the given class to the element options
  621. *
  622. * @param array $options Array options/attributes to add a class to
  623. * @param string $class The classname being added.
  624. * @param string $key the key to use for class.
  625. * @return array Array of options with $key set.
  626. */
  627. public function addClass($options = array(), $class = null, $key = 'class') {
  628. if (isset($options[$key]) && trim($options[$key]) != '') {
  629. $options[$key] .= ' ' . $class;
  630. } else {
  631. $options[$key] = $class;
  632. }
  633. return $options;
  634. }
  635. /**
  636. * Returns a string generated by a helper method
  637. *
  638. * This method can be overridden in subclasses to do generalized output post-processing
  639. *
  640. * @param string $str String to be output.
  641. * @return string
  642. * @deprecated This method will be removed in future versions.
  643. */
  644. public function output($str) {
  645. return $str;
  646. }
  647. /**
  648. * Before render callback. beforeRender is called before the view file is rendered.
  649. *
  650. * Overridden in subclasses.
  651. *
  652. * @param string $viewFile The view file that is going to be rendered
  653. * @return void
  654. */
  655. public function beforeRender($viewFile) {
  656. }
  657. /**
  658. * After render callback. afterRender is called after the view file is rendered
  659. * but before the layout has been rendered.
  660. *
  661. * Overridden in subclasses.
  662. *
  663. * @param string $viewFile The view file that was rendered.
  664. * @return void
  665. */
  666. public function afterRender($viewFile) {
  667. }
  668. /**
  669. * Before layout callback. beforeLayout is called before the layout is rendered.
  670. *
  671. * Overridden in subclasses.
  672. *
  673. * @param string $layoutFile The layout about to be rendered.
  674. * @return void
  675. */
  676. public function beforeLayout($layoutFile) {
  677. }
  678. /**
  679. * After layout callback. afterLayout is called after the layout has rendered.
  680. *
  681. * Overridden in subclasses.
  682. *
  683. * @param string $layoutFile The layout file that was rendered.
  684. * @return void
  685. */
  686. public function afterLayout($layoutFile) {
  687. }
  688. /**
  689. * Transforms a recordset from a hasAndBelongsToMany association to a list of selected
  690. * options for a multiple select element
  691. *
  692. * @param mixed $data
  693. * @param string $key
  694. * @return array
  695. */
  696. protected function _selectedArray($data, $key = 'id') {
  697. if (!is_array($data)) {
  698. $model = $data;
  699. if (!empty($this->request->data[$model][$model])) {
  700. return $this->request->data[$model][$model];
  701. }
  702. if (!empty($this->request->data[$model])) {
  703. $data = $this->request->data[$model];
  704. }
  705. }
  706. $array = array();
  707. if (!empty($data)) {
  708. foreach ($data as $row) {
  709. if (isset($row[$key])) {
  710. $array[$row[$key]] = $row[$key];
  711. }
  712. }
  713. }
  714. return empty($array) ? null : $array;
  715. }
  716. /**
  717. * Resets the vars used by Helper::clean() to null
  718. *
  719. * @return void
  720. */
  721. protected function _reset() {
  722. $this->_tainted = null;
  723. $this->_cleaned = null;
  724. }
  725. /**
  726. * Removes harmful content from output
  727. *
  728. * @return void
  729. */
  730. protected function _clean() {
  731. if (get_magic_quotes_gpc()) {
  732. $this->_cleaned = stripslashes($this->_tainted);
  733. } else {
  734. $this->_cleaned = $this->_tainted;
  735. }
  736. $this->_cleaned = str_replace(array("&amp;", "&lt;", "&gt;"), array("&amp;amp;", "&amp;lt;", "&amp;gt;"), $this->_cleaned);
  737. $this->_cleaned = preg_replace('#(&\#*\w+)[\x00-\x20]+;#u', "$1;", $this->_cleaned);
  738. $this->_cleaned = preg_replace('#(&\#x*)([0-9A-F]+);*#iu', "$1$2;", $this->_cleaned);
  739. $this->_cleaned = html_entity_decode($this->_cleaned, ENT_COMPAT, "UTF-8");
  740. $this->_cleaned = preg_replace('#(<[^>]+[\x00-\x20\"\'\/])(on|xmlns)[^>]*>#iUu', "$1>", $this->_cleaned);
  741. $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);
  742. $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);
  743. $this->_cleaned = preg_replace('#([a-z]*)[\x00-\x20]*=*([\'\"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#iUu', '$1=$2nomozbinding...', $this->_cleaned);
  744. $this->_cleaned = preg_replace('#([a-z]*)[\x00-\x20]*=([\'\"]*)[\x00-\x20]*data[\x00-\x20]*:#Uu', '$1=$2nodata...', $this->_cleaned);
  745. $this->_cleaned = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*expression[\x00-\x20]*\([^>]*>#iU', "$1>", $this->_cleaned);
  746. $this->_cleaned = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*behaviour[\x00-\x20]*\([^>]*>#iU', "$1>", $this->_cleaned);
  747. $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);
  748. $this->_cleaned = preg_replace('#</*\w+:\w[^>]*>#i', "", $this->_cleaned);
  749. do {
  750. $oldstring = $this->_cleaned;
  751. $this->_cleaned = preg_replace('#</*(applet|meta|xml|blink|link|style|script|embed|object|iframe|frame|frameset|ilayer|layer|bgsound|title|base)[^>]*>#i', "", $this->_cleaned);
  752. } while ($oldstring != $this->_cleaned);
  753. $this->_cleaned = str_replace(array("&amp;", "&lt;", "&gt;"), array("&amp;amp;", "&amp;lt;", "&amp;gt;"), $this->_cleaned);
  754. }
  755. }