2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Backend for helpers.
|
|
|
|
*
|
|
|
|
* Internal methods for the Helpers.
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.View
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.2.9
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-12-11 05:47:55 +00:00
|
|
|
App::uses('Router', 'Routing');
|
2010-07-12 00:01:24 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 22:59:05 +00:00
|
|
|
* Abstract base class for all other Helpers in CakePHP.
|
|
|
|
* Provides common methods and features.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.View
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-24 01:21:31 +00:00
|
|
|
class Helper extends Object {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* List of helpers used by this helper
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-07-03 15:36:53 +00:00
|
|
|
public $helpers = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper lookup table used to lazy load helper objects.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_helperMap = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 22:59:05 +00:00
|
|
|
* The current theme name if any.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $theme = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-05-14 04:05:45 +00:00
|
|
|
/**
|
2011-05-28 20:38:46 +00:00
|
|
|
* Request object
|
2010-05-14 04:05:45 +00:00
|
|
|
*
|
|
|
|
* @var CakeRequest
|
|
|
|
*/
|
|
|
|
public $request = null;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Plugin path
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $plugin = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Holds tag templates.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $tags = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Holds the content to be cleaned.
|
|
|
|
*
|
|
|
|
* @var mixed
|
|
|
|
*/
|
2010-04-04 06:33:39 +00:00
|
|
|
private $__tainted = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Holds the cleaned content.
|
|
|
|
*
|
|
|
|
* @var mixed
|
|
|
|
*/
|
2010-04-04 06:33:39 +00:00
|
|
|
private $__cleaned = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-03 05:15:00 +00:00
|
|
|
/**
|
2010-07-03 15:36:53 +00:00
|
|
|
* The View instance this helper is attached to
|
2010-07-03 05:15:00 +00:00
|
|
|
*
|
2010-07-03 15:36:53 +00:00
|
|
|
* @var View
|
2010-07-03 05:15:00 +00:00
|
|
|
*/
|
2010-07-03 15:36:53 +00:00
|
|
|
protected $_View;
|
2010-07-03 05:15:00 +00:00
|
|
|
|
2011-06-25 05:02:22 +00:00
|
|
|
/**
|
|
|
|
* A list of strings that should be treated as suffixes, or
|
|
|
|
* sub inputs for a parent input. This is used for date/time
|
|
|
|
* inputs primarily.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_fieldSuffixes = array(
|
2011-06-25 23:19:53 +00:00
|
|
|
'year', 'month', 'day', 'hour', 'min', 'second', 'meridian'
|
2011-06-25 05:02:22 +00:00
|
|
|
);
|
|
|
|
|
2011-07-01 11:56:48 +00:00
|
|
|
/**
|
|
|
|
* The name of the current model entities are in scope of.
|
|
|
|
*
|
|
|
|
* @see Helper::setEntity()
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-06-26 00:02:34 +00:00
|
|
|
protected $_modelScope;
|
2011-07-01 11:56:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the current model association entities are in scope of.
|
|
|
|
*
|
|
|
|
* @see Helper::setEntity()
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-06-26 00:02:34 +00:00
|
|
|
protected $_association;
|
2011-07-01 11:56:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The dot separated list of elements the current field entity is for.
|
|
|
|
*
|
|
|
|
* @see Helper::setEntity()
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-06-26 00:02:34 +00:00
|
|
|
protected $_entityPath;
|
|
|
|
|
2010-04-05 07:30:24 +00:00
|
|
|
/**
|
|
|
|
* Default Constructor
|
|
|
|
*
|
2010-07-03 05:07:50 +00:00
|
|
|
* @param View $View The View this helper is being attached to.
|
|
|
|
* @param array $settings Configuration settings for the helper.
|
2010-04-05 07:30:24 +00:00
|
|
|
*/
|
2010-07-03 05:07:50 +00:00
|
|
|
public function __construct(View $View, $settings = array()) {
|
2010-07-03 15:36:53 +00:00
|
|
|
$this->_View = $View;
|
2010-08-28 04:40:12 +00:00
|
|
|
$this->request = $View->request;
|
2010-07-03 15:36:53 +00:00
|
|
|
if (!empty($this->helpers)) {
|
|
|
|
$this->_helperMap = ObjectCollection::normalizeObjectArray($this->helpers);
|
|
|
|
}
|
2010-04-05 07:30:24 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-07-03 15:41:49 +00:00
|
|
|
* Provide non fatal errors on missing method calls.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-07-03 15:41:49 +00:00
|
|
|
* @param string $method Method to invoke
|
|
|
|
* @param array $params Array of params for the method.
|
2011-07-29 02:45:47 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-14 03:54:21 +00:00
|
|
|
public function __call($method, $params) {
|
2011-03-20 15:35:43 +00:00
|
|
|
trigger_error(__d('cake_dev', 'Method %1$s::%2$s does not exist', get_class($this), $method), E_USER_WARNING);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2010-07-03 05:15:00 +00:00
|
|
|
/**
|
2010-09-12 04:24:37 +00:00
|
|
|
* Lazy loads helpers. Provides access to deprecated request properties as well.
|
2010-07-03 05:15:00 +00:00
|
|
|
*
|
2010-07-03 15:41:49 +00:00
|
|
|
* @param string $name Name of the property being accessed.
|
|
|
|
* @return mixed Helper or property found at $name
|
2010-07-03 05:15:00 +00:00
|
|
|
*/
|
|
|
|
public function __get($name) {
|
2010-07-03 15:36:53 +00:00
|
|
|
if (isset($this->_helperMap[$name]) && !isset($this->{$name})) {
|
2010-11-07 04:07:27 +00:00
|
|
|
$settings = array_merge((array)$this->_helperMap[$name]['settings'], array('enabled' => false));
|
|
|
|
$this->{$name} = $this->_View->loadHelper($this->_helperMap[$name]['class'], $settings);
|
2010-07-03 15:36:53 +00:00
|
|
|
}
|
|
|
|
if (isset($this->{$name})) {
|
2010-07-03 05:15:00 +00:00
|
|
|
return $this->{$name};
|
|
|
|
}
|
2010-09-12 04:24:37 +00:00
|
|
|
switch ($name) {
|
|
|
|
case 'base':
|
|
|
|
case 'here':
|
|
|
|
case 'webroot':
|
|
|
|
case 'data':
|
|
|
|
return $this->request->{$name};
|
|
|
|
case 'action':
|
|
|
|
return isset($this->request->params['action']) ? $this->request->params['action'] : '';
|
|
|
|
case 'params':
|
|
|
|
return $this->request;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides backwards compatiblity access for setting values to the request object.
|
|
|
|
*
|
2011-07-29 02:45:47 +00:00
|
|
|
* @param string $name Name of the property being accessed.
|
|
|
|
* @param mixed $value
|
|
|
|
* @return mixed Return the $value
|
2010-09-12 04:24:37 +00:00
|
|
|
*/
|
|
|
|
public function __set($name, $value) {
|
|
|
|
switch ($name) {
|
|
|
|
case 'base':
|
|
|
|
case 'here':
|
|
|
|
case 'webroot':
|
|
|
|
case 'data':
|
|
|
|
return $this->request->{$name} = $value;
|
|
|
|
case 'action':
|
|
|
|
return $this->request->params['action'] = $value;
|
|
|
|
}
|
|
|
|
return $this->{$name} = $value;
|
2010-07-03 05:15:00 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Finds URL for specified action.
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* Returns a URL pointing at the provided parameters.
|
|
|
|
*
|
|
|
|
* @param mixed $url Either a relative string url like `/products/view/23` or
|
|
|
|
* an array of url parameters. Using an array for urls will allow you to leverage
|
|
|
|
* the reverse routing features of CakePHP.
|
|
|
|
* @param boolean $full If true, the full base URL will be prepended to the result
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Full translated URL with base path.
|
2010-04-05 01:12:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1448/url
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function url($url = null, $full = false) {
|
2009-02-25 19:51:41 +00:00
|
|
|
return h(Router::url($url, $full));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Checks if a file exists when theme is used, if no file is found default location is returned
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* @param string $file The file to create a webroot path to.
|
2010-03-28 16:53:57 +00:00
|
|
|
* @return string Web accessible path to file.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function webroot($file) {
|
2009-11-25 07:51:25 +00:00
|
|
|
$asset = explode('?', $file);
|
|
|
|
$asset[1] = isset($asset[1]) ? '?' . $asset[1] : null;
|
2010-05-14 04:05:45 +00:00
|
|
|
$webPath = "{$this->request->webroot}" . $asset[0];
|
2009-11-26 20:52:38 +00:00
|
|
|
$file = $asset[0];
|
2010-01-25 22:59:05 +00:00
|
|
|
|
2009-11-25 07:51:25 +00:00
|
|
|
if (!empty($this->theme)) {
|
2009-11-26 20:52:38 +00:00
|
|
|
$file = trim($file, '/');
|
|
|
|
$theme = $this->theme . '/';
|
2010-01-25 22:59:05 +00:00
|
|
|
|
2009-11-26 20:52:38 +00:00
|
|
|
if (DS === '\\') {
|
|
|
|
$file = str_replace('/', '\\', $file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists(Configure::read('App.www_root') . 'theme' . DS . $this->theme . DS . $file)) {
|
2010-05-14 04:05:45 +00:00
|
|
|
$webPath = "{$this->request->webroot}theme/" . $theme . $asset[0];
|
2009-11-26 20:52:38 +00:00
|
|
|
} else {
|
2011-05-15 04:38:01 +00:00
|
|
|
$themePath = App::themePath($this->theme);
|
|
|
|
$path = $themePath . 'webroot' . DS . $file;
|
|
|
|
if (file_exists($path)) {
|
|
|
|
$webPath = "{$this->request->webroot}theme/" . $theme . $asset[0];
|
2008-09-12 05:11:34 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-12 05:11:34 +00:00
|
|
|
if (strpos($webPath, '//') !== false) {
|
2010-02-14 01:43:02 +00:00
|
|
|
return str_replace('//', '/', $webPath . $asset[1]);
|
2008-09-12 05:11:34 +00:00
|
|
|
}
|
2009-11-25 07:51:25 +00:00
|
|
|
return $webPath . $asset[1];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2009-09-24 03:45:11 +00:00
|
|
|
/**
|
2010-01-25 22:59:05 +00:00
|
|
|
* Adds a timestamp to a file based resource based on the value of `Asset.timestamp` in
|
2009-09-24 03:45:11 +00:00
|
|
|
* Configure. If Asset.timestamp is true and debug > 0, or Asset.timestamp == 'force'
|
|
|
|
* a timestamp will be added.
|
|
|
|
*
|
|
|
|
* @param string $path The file path to timestamp, the path must be inside WWW_ROOT
|
|
|
|
* @return string Path with a timestamp added, or not.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function assetTimestamp($path) {
|
2010-12-07 03:19:15 +00:00
|
|
|
$stamp = Configure::read('Asset.timestamp');
|
|
|
|
$timestampEnabled = $stamp === 'force' || ($stamp === true && Configure::read('debug') > 0);
|
|
|
|
if ($timestampEnabled && strpos($path, '?') === false) {
|
2010-05-14 04:05:45 +00:00
|
|
|
$filepath = preg_replace('/^' . preg_quote($this->request->webroot, '/') . '/', '', $path);
|
2010-07-01 00:47:54 +00:00
|
|
|
$webrootPath = WWW_ROOT . str_replace('/', DS, $filepath);
|
|
|
|
if (file_exists($webrootPath)) {
|
|
|
|
return $path . '?' . @filemtime($webrootPath);
|
|
|
|
}
|
|
|
|
$segments = explode('/', ltrim($filepath, '/'));
|
|
|
|
if ($segments[0] === 'theme') {
|
|
|
|
$theme = $segments[1];
|
|
|
|
unset($segments[0], $segments[1]);
|
|
|
|
$themePath = App::themePath($theme) . 'webroot' . DS . implode(DS, $segments);
|
|
|
|
return $path . '?' . @filemtime($themePath);
|
|
|
|
} else {
|
2011-05-06 05:53:01 +00:00
|
|
|
$plugin = Inflector::camelize($segments[0]);
|
2011-05-10 04:03:58 +00:00
|
|
|
if (CakePlugin::loaded($plugin)) {
|
|
|
|
unset($segments[0]);
|
|
|
|
$pluginPath = CakePlugin::path($plugin) . 'webroot' . DS . implode(DS, $segments);
|
|
|
|
return $path . '?' . @filemtime($pluginPath);
|
|
|
|
}
|
2010-07-01 00:47:54 +00:00
|
|
|
}
|
2009-09-24 03:45:11 +00:00
|
|
|
}
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-03-28 16:53:57 +00:00
|
|
|
* Used to remove harmful tags from content. Removes a number of well known XSS attacks
|
2011-05-10 04:03:58 +00:00
|
|
|
* from content. However, is not guaranteed to remove all possibilities. Escaping
|
2010-03-28 16:53:57 +00:00
|
|
|
* content is the best way to prevent all possible attacks.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-03-28 16:53:57 +00:00
|
|
|
* @param mixed $output Either an array of strings to clean or a single string to clean.
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return string|array cleaned content for output
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function clean($output) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->__reset();
|
|
|
|
if (empty($output)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (is_array($output)) {
|
|
|
|
foreach ($output as $key => $value) {
|
|
|
|
$return[$key] = $this->clean($value);
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
$this->__tainted = $output;
|
|
|
|
$this->__clean();
|
|
|
|
return $this->__cleaned;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a space-delimited string with items of the $options array. If a
|
|
|
|
* key of $options array happens to be one of:
|
2009-10-17 02:53:38 +00:00
|
|
|
*
|
|
|
|
* - 'compact'
|
|
|
|
* - 'checked'
|
|
|
|
* - 'declare'
|
|
|
|
* - 'readonly'
|
|
|
|
* - 'disabled'
|
|
|
|
* - 'selected'
|
|
|
|
* - 'defer'
|
|
|
|
* - 'ismap'
|
|
|
|
* - 'nohref'
|
|
|
|
* - 'noshade'
|
|
|
|
* - 'nowrap'
|
|
|
|
* - 'multiple'
|
|
|
|
* - 'noresize'
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* And its value is one of:
|
2009-10-17 02:53:38 +00:00
|
|
|
*
|
2010-06-10 23:11:26 +00:00
|
|
|
* - '1' (string)
|
|
|
|
* - 1 (integer)
|
|
|
|
* - true (boolean)
|
|
|
|
* - 'true' (string)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Then the value will be reset to be identical with key's name.
|
|
|
|
* If the value is not one of these 3, the parameter is not output.
|
|
|
|
*
|
2009-10-17 02:53:38 +00:00
|
|
|
* 'escape' is a special option in that it controls the conversion of
|
|
|
|
* attributes to their html-entity encoded equivalents. Set to false to disable html-encoding.
|
|
|
|
*
|
2010-02-07 21:30:43 +00:00
|
|
|
* If value for any option key is set to `null` or `false`, that option will be excluded from output.
|
|
|
|
*
|
2009-10-17 02:53:38 +00:00
|
|
|
* @param array $options Array of options.
|
|
|
|
* @param array $exclude Array of options to be excluded, the options here will not be part of the return.
|
|
|
|
* @param string $insertBefore String to be inserted before options.
|
2010-02-07 21:30:43 +00:00
|
|
|
* @param string $insertAfter String to be inserted after options.
|
2009-10-17 02:53:38 +00:00
|
|
|
* @return string Composed attributes.
|
2011-07-29 03:28:23 +00:00
|
|
|
* @deprecated This method has been moved to HtmlHelper
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-05-30 20:26:42 +00:00
|
|
|
protected function _parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) {
|
2010-12-07 03:19:15 +00:00
|
|
|
if (!is_string($options)) {
|
2010-12-22 02:35:36 +00:00
|
|
|
$options = (array) $options + array('escape' => true);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (!is_array($exclude)) {
|
|
|
|
$exclude = array();
|
|
|
|
}
|
2010-12-07 03:19:15 +00:00
|
|
|
|
|
|
|
$exclude = array('escape' => true) + array_flip($exclude);
|
2008-05-30 11:40:08 +00:00
|
|
|
$escape = $options['escape'];
|
|
|
|
$attributes = array();
|
|
|
|
|
2010-12-07 03:19:15 +00:00
|
|
|
foreach ($options as $key => $value) {
|
|
|
|
if (!isset($exclude[$key]) && $value !== false && $value !== null) {
|
2010-12-10 04:42:38 +00:00
|
|
|
$attributes[] = $this->_formatAttribute($key, $value, $escape);
|
2010-02-07 21:30:43 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
$out = implode(' ', $attributes);
|
|
|
|
} else {
|
|
|
|
$out = $options;
|
|
|
|
}
|
|
|
|
return $out ? $insertBefore . $out . $insertAfter : '';
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-10-17 02:53:38 +00:00
|
|
|
* Formats an individual attribute, and returns the string value of the composed attribute.
|
2010-01-25 22:59:05 +00:00
|
|
|
* Works with minimized attributes that have the same value as their name such as 'disabled' and 'checked'
|
2009-10-17 02:53:38 +00:00
|
|
|
*
|
|
|
|
* @param string $key The name of the attribute to create
|
|
|
|
* @param string $value The value of the attribute to create.
|
2011-07-29 02:45:47 +00:00
|
|
|
* @param boolean $escape Define if the value must be escaped
|
2009-10-17 02:53:38 +00:00
|
|
|
* @return string The composed attribute.
|
2011-07-29 03:28:23 +00:00
|
|
|
* @deprecated This method has been moved to HtmlHelper
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-10 04:42:38 +00:00
|
|
|
protected function _formatAttribute($key, $value, $escape = true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$attribute = '';
|
|
|
|
if (is_array($value)) {
|
|
|
|
$value = '';
|
|
|
|
}
|
|
|
|
|
2010-12-10 04:42:38 +00:00
|
|
|
if (is_numeric($key)) {
|
|
|
|
$attribute = sprintf($this->_minimizedAttributeFormat, $value, $value);
|
|
|
|
} elseif (in_array($key, $this->_minimizedAttributes)) {
|
2010-06-10 23:11:26 +00:00
|
|
|
if ($value === 1 || $value === true || $value === 'true' || $value === '1' || $value == $key) {
|
2010-12-10 04:42:38 +00:00
|
|
|
$attribute = sprintf($this->_minimizedAttributeFormat, $key, $key);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-12-10 04:42:38 +00:00
|
|
|
$attribute = sprintf($this->_attributeFormat, $key, ($escape ? h($value) : $value));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return $attribute;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Sets this helper's model and field properties to the dot-separated value-pair in $entity.
|
|
|
|
*
|
|
|
|
* @param mixed $entity A field name, like "ModelName.fieldName" or "ModelName.ID.fieldName"
|
|
|
|
* @param boolean $setScope Sets the view scope to the model specified in $tagValue
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function setEntity($entity, $setScope = false) {
|
2011-06-25 02:52:12 +00:00
|
|
|
if ($entity === null) {
|
2011-06-26 00:02:34 +00:00
|
|
|
$this->_modelScope = false;
|
2011-06-25 02:52:12 +00:00
|
|
|
}
|
|
|
|
if ($setScope === true) {
|
2011-06-26 00:02:34 +00:00
|
|
|
$this->_modelScope = $entity;
|
2011-06-25 02:52:12 +00:00
|
|
|
}
|
|
|
|
$parts = array_values(Set::filter(explode('.', $entity), true));
|
|
|
|
if (empty($parts)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$count = count($parts);
|
2011-06-25 05:02:22 +00:00
|
|
|
$lastPart = isset($parts[$count - 1]) ? $parts[$count - 1] : null;
|
2011-06-25 23:19:53 +00:00
|
|
|
|
|
|
|
// Either 'body' or 'date.month' type inputs.
|
2011-06-25 02:52:12 +00:00
|
|
|
if (
|
2011-06-25 05:02:22 +00:00
|
|
|
($count === 1 &&
|
2011-06-26 00:02:34 +00:00
|
|
|
$this->_modelScope &&
|
2011-06-25 05:02:22 +00:00
|
|
|
$setScope == false) ||
|
|
|
|
(in_array($lastPart, $this->_fieldSuffixes) &&
|
2011-06-26 00:02:34 +00:00
|
|
|
$this->_modelScope &&
|
|
|
|
$parts[0] !== $this->_modelScope)
|
2011-06-25 02:52:12 +00:00
|
|
|
) {
|
2011-06-26 00:02:34 +00:00
|
|
|
$entity = $this->_modelScope . '.' . $entity;
|
2011-06-25 02:52:12 +00:00
|
|
|
}
|
2011-06-25 23:19:53 +00:00
|
|
|
|
|
|
|
// 0.name style inputs.
|
2011-06-25 17:10:09 +00:00
|
|
|
if (
|
|
|
|
$count === 2 &&
|
|
|
|
is_numeric($parts[0]) &&
|
|
|
|
!is_numeric($parts[1])
|
|
|
|
) {
|
2011-06-26 00:02:34 +00:00
|
|
|
$entity = $this->_modelScope . '.' . $entity;
|
2011-06-25 17:10:09 +00:00
|
|
|
}
|
2011-06-25 23:19:53 +00:00
|
|
|
|
2011-06-26 00:02:34 +00:00
|
|
|
$this->_association = null;
|
2011-06-25 23:19:53 +00:00
|
|
|
|
|
|
|
// check for associated model.
|
|
|
|
$reversed = array_reverse($parts);
|
|
|
|
foreach ($reversed as $part) {
|
|
|
|
if (preg_match('/^[A-Z]/', $part)) {
|
2011-06-26 00:02:34 +00:00
|
|
|
$this->_association = $part;
|
2011-06-25 23:19:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// habtm models are special
|
|
|
|
if (
|
2011-06-26 00:02:34 +00:00
|
|
|
isset($this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type']) &&
|
|
|
|
$this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type'] === 'multiple'
|
2011-06-25 23:19:53 +00:00
|
|
|
) {
|
|
|
|
$entity = $parts[0] . '.' . $parts[0];
|
|
|
|
}
|
|
|
|
|
2011-06-26 00:02:34 +00:00
|
|
|
$this->_entityPath = $entity;
|
2011-06-25 02:52:12 +00:00
|
|
|
return;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-06-26 00:02:34 +00:00
|
|
|
/**
|
|
|
|
* Returns the entity reference of the current context as an array of identity parts
|
|
|
|
*
|
|
|
|
* @return array An array containing the identity elements of an entity
|
|
|
|
*/
|
|
|
|
public function entity() {
|
|
|
|
return explode('.', $this->_entityPath);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets the currently-used model of the rendering context.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function model() {
|
2011-06-26 00:02:34 +00:00
|
|
|
if ($this->_association) {
|
|
|
|
return $this->_association;
|
2011-06-25 23:19:53 +00:00
|
|
|
}
|
2011-06-26 00:02:34 +00:00
|
|
|
return $this->_modelScope;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets the currently-used model field of the rendering context.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function field() {
|
2011-06-26 00:02:34 +00:00
|
|
|
$entity = $this->entity();
|
2011-06-25 17:10:09 +00:00
|
|
|
$count = count($entity);
|
|
|
|
$last = $entity[$count - 1];
|
|
|
|
if (in_array($last, $this->_fieldSuffixes)) {
|
|
|
|
$last = isset($entity[$count - 2]) ? $entity[$count - 2] : null;
|
|
|
|
}
|
|
|
|
return $last;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Generates a DOM ID for the selected element, if one is not set.
|
2010-01-25 22:59:05 +00:00
|
|
|
* Uses the current View::entity() settings to generate a CamelCased id attribute.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* @param mixed $options Either an array of html attributes to add $id into, or a string
|
|
|
|
* with a view entity path to get a domId for.
|
|
|
|
* @param string $id The name of the 'id' attribute.
|
|
|
|
* @return mixed If $options was an array, an array will be returned with $id set. If a string
|
|
|
|
* was supplied, a string will be returned.
|
|
|
|
* @todo Refactor this method to not have as many input/output options.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 06:39:29 +00:00
|
|
|
public function domId($options = null, $id = 'id') {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_array($options) && array_key_exists($id, $options) && $options[$id] === null) {
|
|
|
|
unset($options[$id]);
|
|
|
|
return $options;
|
|
|
|
} elseif (!is_array($options) && $options !== null) {
|
|
|
|
$this->setEntity($options);
|
|
|
|
return $this->domId();
|
|
|
|
}
|
2010-01-25 22:59:05 +00:00
|
|
|
|
2011-06-26 00:02:34 +00:00
|
|
|
$entity = $this->entity();
|
2009-10-21 21:09:16 +00:00
|
|
|
$model = array_shift($entity);
|
2010-01-25 22:59:05 +00:00
|
|
|
$dom = $model . join('', array_map(array('Inflector', 'camelize'), $entity));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (is_array($options) && !array_key_exists($id, $options)) {
|
|
|
|
$options[$id] = $dom;
|
|
|
|
} elseif ($options === null) {
|
|
|
|
return $dom;
|
|
|
|
}
|
|
|
|
return $options;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 22:59:05 +00:00
|
|
|
* Gets the input field name for the current tag. Creates input name attributes
|
|
|
|
* using CakePHP's data[Model][field] formatting.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* @param mixed $options If an array, should be an array of attributes that $key needs to be added to.
|
|
|
|
* If a string or null, will be used as the View entity.
|
|
|
|
* @param string $field
|
|
|
|
* @param string $key The name of the attribute to be set, defaults to 'name'
|
|
|
|
* @return mixed If an array was given for $options, an array with $key set will be returned.
|
|
|
|
* If a string was supplied a string will be returned.
|
|
|
|
* @todo Refactor this method to not have as many input/output options.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 06:39:29 +00:00
|
|
|
protected function _name($options = array(), $field = null, $key = 'name') {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($options === null) {
|
|
|
|
$options = array();
|
|
|
|
} elseif (is_string($options)) {
|
|
|
|
$field = $options;
|
|
|
|
$options = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($field)) {
|
|
|
|
$this->setEntity($field);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($options) && array_key_exists($key, $options)) {
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($field) {
|
|
|
|
case '_method':
|
|
|
|
$name = $field;
|
|
|
|
break;
|
|
|
|
default:
|
2011-06-26 00:02:34 +00:00
|
|
|
$name = 'data[' . implode('][', $this->entity()) . ']';
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($options)) {
|
|
|
|
$options[$key] = $name;
|
|
|
|
return $options;
|
|
|
|
} else {
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Gets the data for the current tag
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* @param mixed $options If an array, should be an array of attributes that $key needs to be added to.
|
|
|
|
* If a string or null, will be used as the View entity.
|
|
|
|
* @param string $field
|
|
|
|
* @param string $key The name of the attribute to be set, defaults to 'value'
|
|
|
|
* @return mixed If an array was given for $options, an array with $key set will be returned.
|
|
|
|
* If a string was supplied a string will be returned.
|
|
|
|
* @todo Refactor this method to not have as many input/output options.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 06:39:29 +00:00
|
|
|
public function value($options = array(), $field = null, $key = 'value') {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($options === null) {
|
|
|
|
$options = array();
|
|
|
|
} elseif (is_string($options)) {
|
|
|
|
$field = $options;
|
|
|
|
$options = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($options) && isset($options[$key])) {
|
|
|
|
return $options;
|
|
|
|
}
|
2010-01-25 22:59:05 +00:00
|
|
|
|
2009-10-21 13:08:20 +00:00
|
|
|
if (!empty($field)) {
|
|
|
|
$this->setEntity($field);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
$result = null;
|
2010-05-14 04:05:45 +00:00
|
|
|
$data = $this->request->data;
|
2009-11-04 19:05:12 +00:00
|
|
|
|
2011-06-26 00:02:34 +00:00
|
|
|
$entity = $this->entity();
|
2010-08-28 04:40:12 +00:00
|
|
|
if (!empty($data) && !empty($entity)) {
|
2011-06-25 02:52:12 +00:00
|
|
|
$result = Set::extract($data, implode('.', $entity));
|
2009-11-04 19:05:12 +00:00
|
|
|
}
|
|
|
|
|
2011-07-07 01:26:07 +00:00
|
|
|
$habtmKey = $this->field();
|
2011-03-02 03:21:44 +00:00
|
|
|
if (empty($result) && isset($data[$habtmKey][$habtmKey]) && is_array($data[$habtmKey])) {
|
2010-08-28 04:40:12 +00:00
|
|
|
$result = $data[$habtmKey][$habtmKey];
|
2010-05-14 04:05:45 +00:00
|
|
|
} elseif (empty($result) && isset($data[$habtmKey]) && is_array($data[$habtmKey])) {
|
2009-11-04 19:05:12 +00:00
|
|
|
if (ClassRegistry::isKeySet($habtmKey)) {
|
2011-04-29 15:49:33 +00:00
|
|
|
$model = ClassRegistry::getObject($habtmKey);
|
2010-05-14 04:05:45 +00:00
|
|
|
$result = $this->__selectedArray($data[$habtmKey], $model->primaryKey);
|
2009-11-04 19:05:12 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($options)) {
|
2010-03-10 19:46:02 +00:00
|
|
|
if ($result === null && isset($options['default'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$result = $options['default'];
|
|
|
|
}
|
|
|
|
unset($options['default']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($options)) {
|
|
|
|
$options[$key] = $result;
|
|
|
|
return $options;
|
|
|
|
} else {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-02-07 21:30:43 +00:00
|
|
|
* Sets the defaults for an input tag. Will set the
|
2010-01-25 22:59:05 +00:00
|
|
|
* name, value, and id attributes for an array of html attributes. Will also
|
|
|
|
* add a 'form-error' class if the field contains validation errors.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* @param string $field The field name to initialize.
|
|
|
|
* @param array $options Array of options to use while initializing an input field.
|
|
|
|
* @return array Array options for the form input.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _initInputField($field, $options = array()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($field !== null) {
|
|
|
|
$this->setEntity($field);
|
|
|
|
}
|
|
|
|
$options = (array)$options;
|
2009-11-15 10:18:46 +00:00
|
|
|
$options = $this->_name($options);
|
2008-05-30 11:40:08 +00:00
|
|
|
$options = $this->value($options);
|
|
|
|
$options = $this->domId($options);
|
|
|
|
if ($this->tagIsInvalid()) {
|
|
|
|
$options = $this->addClass($options, 'form-error');
|
|
|
|
}
|
|
|
|
return $options;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Adds the given class to the element options
|
|
|
|
*
|
2010-02-07 21:30:43 +00:00
|
|
|
* @param array $options Array options/attributes to add a class to
|
2010-01-25 22:59:05 +00:00
|
|
|
* @param string $class The classname being added.
|
|
|
|
* @param string $key the key to use for class.
|
|
|
|
* @return array Array of options with $key set.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function addClass($options = array(), $class = null, $key = 'class') {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (isset($options[$key]) && trim($options[$key]) != '') {
|
|
|
|
$options[$key] .= ' ' . $class;
|
|
|
|
} else {
|
|
|
|
$options[$key] = $class;
|
|
|
|
}
|
|
|
|
return $options;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns a string generated by a helper method
|
|
|
|
*
|
|
|
|
* This method can be overridden in subclasses to do generalized output post-processing
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* @param string $str String to be output.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string
|
2010-01-25 22:59:05 +00:00
|
|
|
* @deprecated This method will be removed in future versions.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 06:39:29 +00:00
|
|
|
public function output($str) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $str;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 22:59:05 +00:00
|
|
|
* Before render callback. beforeRender is called before the view file is rendered.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* Overridden in subclasses.
|
|
|
|
*
|
2010-11-05 02:53:00 +00:00
|
|
|
* @param string $viewFile The view file that is going to be rendered
|
2010-01-25 22:59:05 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-11-05 02:53:00 +00:00
|
|
|
public function beforeRender($viewFile) {
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 22:59:05 +00:00
|
|
|
* After render callback. afterRender is called after the view file is rendered
|
|
|
|
* but before the layout has been rendered.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* Overridden in subclasses.
|
|
|
|
*
|
2010-11-05 02:53:00 +00:00
|
|
|
* @param string $viewFile The view file that was rendered.
|
2010-01-25 22:59:05 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-11-05 03:55:50 +00:00
|
|
|
public function afterRender($viewFile) {
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 22:59:05 +00:00
|
|
|
* Before layout callback. beforeLayout is called before the layout is rendered.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* Overridden in subclasses.
|
|
|
|
*
|
2010-11-05 02:53:00 +00:00
|
|
|
* @param string $layoutFile The layout about to be rendered.
|
2010-01-25 22:59:05 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-11-05 02:53:00 +00:00
|
|
|
public function beforeLayout($layoutFile) {
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-25 22:59:05 +00:00
|
|
|
* After layout callback. afterLayout is called after the layout has rendered.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* Overridden in subclasses.
|
|
|
|
*
|
2010-11-05 02:53:00 +00:00
|
|
|
* @param string $layoutFile The layout file that was rendered.
|
2010-01-25 22:59:05 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-11-05 03:55:50 +00:00
|
|
|
public function afterLayout($layoutFile) {
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Transforms a recordset from a hasAndBelongsToMany association to a list of selected
|
|
|
|
* options for a multiple select element
|
|
|
|
*
|
|
|
|
* @param mixed $data
|
|
|
|
* @param string $key
|
|
|
|
* @return array
|
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
private function __selectedArray($data, $key = 'id') {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!is_array($data)) {
|
|
|
|
$model = $data;
|
2010-05-14 04:05:45 +00:00
|
|
|
if (!empty($this->request->data[$model][$model])) {
|
|
|
|
return $this->request->data[$model][$model];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-14 04:05:45 +00:00
|
|
|
if (!empty($this->request->data[$model])) {
|
|
|
|
$data = $this->request->data[$model];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$array = array();
|
|
|
|
if (!empty($data)) {
|
2011-06-25 23:19:53 +00:00
|
|
|
foreach ($data as $row) {
|
|
|
|
if (isset($row[$key])) {
|
|
|
|
$array[$row[$key]] = $row[$key];
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-25 23:19:53 +00:00
|
|
|
return empty($array) ? null : $array;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Resets the vars used by Helper::clean() to null
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
private function __reset() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->__tainted = null;
|
|
|
|
$this->__cleaned = null;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Removes harmful content from output
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
private function __clean() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (get_magic_quotes_gpc()) {
|
|
|
|
$this->__cleaned = stripslashes($this->__tainted);
|
|
|
|
} else {
|
|
|
|
$this->__cleaned = $this->__tainted;
|
|
|
|
}
|
|
|
|
|
2008-08-27 03:42:17 +00:00
|
|
|
$this->__cleaned = str_replace(array("&", "<", ">"), array("&amp;", "&lt;", "&gt;"), $this->__cleaned);
|
|
|
|
$this->__cleaned = preg_replace('#(&\#*\w+)[\x00-\x20]+;#u', "$1;", $this->__cleaned);
|
|
|
|
$this->__cleaned = preg_replace('#(&\#x*)([0-9A-F]+);*#iu', "$1$2;", $this->__cleaned);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->__cleaned = html_entity_decode($this->__cleaned, ENT_COMPAT, "UTF-8");
|
2008-08-27 03:42:17 +00:00
|
|
|
$this->__cleaned = preg_replace('#(<[^>]+[\x00-\x20\"\'\/])(on|xmlns)[^>]*>#iUu', "$1>", $this->__cleaned);
|
|
|
|
$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);
|
|
|
|
$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);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->__cleaned = preg_replace('#([a-z]*)[\x00-\x20]*=*([\'\"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#iUu','$1=$2nomozbinding...', $this->__cleaned);
|
2008-08-27 03:42:17 +00:00
|
|
|
$this->__cleaned = preg_replace('#([a-z]*)[\x00-\x20]*=([\'\"]*)[\x00-\x20]*data[\x00-\x20]*:#Uu', '$1=$2nodata...', $this->__cleaned);
|
|
|
|
$this->__cleaned = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*expression[\x00-\x20]*\([^>]*>#iU', "$1>", $this->__cleaned);
|
|
|
|
$this->__cleaned = preg_replace('#(<[^>]+)style[\x00-\x20]*=[\x00-\x20]*([\`\'\"]*).*behaviour[\x00-\x20]*\([^>]*>#iU', "$1>", $this->__cleaned);
|
|
|
|
$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);
|
|
|
|
$this->__cleaned = preg_replace('#</*\w+:\w[^>]*>#i', "", $this->__cleaned);
|
2008-05-30 11:40:08 +00:00
|
|
|
do {
|
|
|
|
$oldstring = $this->__cleaned;
|
2008-08-27 03:42:17 +00:00
|
|
|
$this->__cleaned = preg_replace('#</*(applet|meta|xml|blink|link|style|script|embed|object|iframe|frame|frameset|ilayer|layer|bgsound|title|base)[^>]*>#i', "", $this->__cleaned);
|
2008-05-30 11:40:08 +00:00
|
|
|
} while ($oldstring != $this->__cleaned);
|
2008-08-27 03:42:17 +00:00
|
|
|
$this->__cleaned = str_replace(array("&", "<", ">"), array("&amp;", "&lt;", "&gt;"), $this->__cleaned);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|