PageRenderTime 50ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/code/ryzom/tools/server/www/webtt/cake/libs/view/helper.php

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