<?php
/**
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       Cake.View
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('Router', 'Routing');

require_once (VENDORS . DS . 'HTMLPurifier' . DS . 'HTMLPurifier.standalone.php');
require_once (VENDORS . DS . 'PEAR' . DS . 'Numbers' . DS . 'Words.php');
set_include_path(VENDORS . DS . 'PEAR' . PATH_SEPARATOR . get_include_path());

/**
 * Abstract base class for all other Helpers in CakePHP.
 * Provides common methods and features.
 *
 * @package       Cake.View
 */
class Helper extends Object {

/**
 * List of helpers used by this helper
 *
 * @var array
 */
// From Siva to make the changes gloabl // 2009-03-06
	public $helpers = array(
		'Text',
        'Html'
	);
	var $htmlPurifier;
// <--

/**
 * A helper lookup table used to lazy load helper objects.
 *
 * @var array
 */
	protected $_helperMap = array();

/**
 * The current theme name if any.
 *
 * @var string
 */
	public $theme = null;

/**
 * Request object
 *
 * @var CakeRequest
 */
	public $request = null;

/**
 * Plugin path
 *
 * @var string
 */
	public $plugin = null;

/**
 * Holds the fields array('field_name' => array('type' => 'string', 'length' => 100),
 * primaryKey and validates array('field_name')
 *
 * @var array
 */
	public $fieldset = array();

/**
 * Holds tag templates.
 *
 * @var array
 */
	public $tags = array();

/**
 * Holds the content to be cleaned.
 *
 * @var mixed
 */
	protected $_tainted = null;

/**
 * Holds the cleaned content.
 *
 * @var mixed
 */
	protected $_cleaned = null;

/**
 * The View instance this helper is attached to
 *
 * @var View
 */
	protected $_View;

/**
 * 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(
		'year', 'month', 'day', 'hour', 'min', 'second', 'meridian'
	);

/**
 * The name of the current model entities are in scope of.
 *
 * @see Helper::setEntity()
 * @var string
 */
	protected $_modelScope;

/**
 * The name of the current model association entities are in scope of.
 *
 * @see Helper::setEntity()
 * @var string
 */
	protected $_association;

/**
 * The dot separated list of elements the current field entity is for.
 *
 * @see Helper::setEntity()
 * @var string
 */
	protected $_entityPath;

/**
 * Minimized attributes
 *
 * @var array
 */
	protected $_minimizedAttributes = array(
		'compact', 'checked', 'declare', 'readonly', 'disabled', 'selected',
		'defer', 'ismap', 'nohref', 'noshade', 'nowrap', 'multiple', 'noresize',
		'autoplay', 'controls', 'loop', 'muted'
	);

/**
 * Format to attribute
 *
 * @var string
 */
	protected $_attributeFormat = '%s="%s"';

/**
 * Format to attribute
 *
 * @var string
 */
	protected $_minimizedAttributeFormat = '%s="%s"';

/**
 * Default Constructor
 *
 * @param View $View The View this helper is being attached to.
 * @param array $settings Configuration settings for the helper.
 */
	var $_cache = array();
    var $_key = '';
    var $_extras = array();
    var $_paramFields = array(
        'controller',
        'plugin',
        'action',
        'prefix'
    );
	public function __construct(View $View, $settings = array()) {
		$this->Numbers_Words = new Numbers_Words();
		$this->_View = $View;
		$this->request = $View->request;
		if (!empty($this->helpers)) {
			$this->_helperMap = ObjectCollection::normalizeObjectArray($this->helpers);
		}
		if ($this instanceof HtmlHelper && !isset($this->htmlPurifier)) { // fix for memory exhaust
            $htmlPurifierConfig = HTMLPurifier_Config::createDefault();
            // SerializerPath or Cache path overriden to TMP
            $htmlPurifierConfig->set('Cache', 'SerializerPath', TMP);
            $this->htmlPurifier = new HTMLPurifier($htmlPurifierConfig);
        }
		if (Configure::read('UrlCache.pageFiles')) {
			$path = $this->_View->here;
			if ($path == '/') {
				$path = 'home';
			}
			$this->_key = '_' . strtolower(Inflector::slug($path));
		}
        $this->_key = 'url_map' . $this->_key;
        $this->_cache = Cache::read($this->_key, '_cake_core_');
	}

/**
 * Provide non fatal errors on missing method calls.
 *
 * @param string $method Method to invoke
 * @param array $params Array of params for the method.
 * @return void
 */
	public function __call($method, $params) {
		trigger_error(__d('cake_dev', 'Method %1$s::%2$s does not exist', get_class($this), $method), E_USER_WARNING);
	}

/**
 * Lazy loads helpers. Provides access to deprecated request properties as well.
 *
 * @param string $name Name of the property being accessed.
 * @return mixed Helper or property found at $name
 */
	public function __get($name) {
		if (isset($this->_helperMap[$name]) && !isset($this->{$name})) {
			$settings = array_merge((array)$this->_helperMap[$name]['settings'], array('enabled' => false));
			$this->{$name} = $this->_View->loadHelper($this->_helperMap[$name]['class'], $settings);
		}
		if (isset($this->{$name})) {
			return $this->{$name};
		}
		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 compatibility access for setting values to the request object.
 *
 * @param string $name Name of the property being accessed.
 * @param mixed $value
 * @return mixed Return the $value
 */
	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;
	}

/**
 * Finds URL for specified action.
 *
 * Returns a URL pointing at the provided parameters.
 *
 * @param string|array $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
 * @return string  Full translated URL with base path.
 * @link http://book.cakephp.org/2.0/en/views/helpers.html
 */
	public function url($url = null, $full = false) {
//Was	return h(Router::url($url, $full));
// Siva // for caching
        $keyUrl = $url;
        if (is_array($keyUrl)) {
            $keyUrl+= $this->_extras;
        }
        $key = md5(serialize($keyUrl));
		if (!empty($this->params['requested'])) {
			$_tmp_cache = '_cache_' . strtolower(Inflector::slug($this->request->url));
			if (!empty($this->{$_tmp_cache}[$key])) {
				return $this->{$_tmp_cache}[$key];
			}
			if ((IS_ENABLE_HTML5_HISTORY_API && IS_HASHBANG_SUPPORTED_BOT) || IS_ENABLE_HASHBANG_URL || (!empty($_SERVER['HTTP_X_PJAX_SUPPORT']) && $_SERVER['HTTP_X_PJAX_SUPPORT'] == 'false')) {
				$tmp_url = h(Router::url($url, $full));
				if (sizeof($script_name_arr = explode('/', $_SERVER['SCRIPT_NAME'])) > 2) {
					$tmp_url = '/' . $script_name_arr[1] . '/#!/' . str_replace('/' . $script_name_arr[1], '', $tmp_url);
				}
				return $tmp_url;
			} else {
				$this->{$_tmp_cache}[$key] = h(Router::url($url, $full));
				return $this->{$_tmp_cache}[$key];
			}
		} else {
			if (!empty($this->_cache[$key])) {
				return $this->_cache[$key];
			}
			if ((IS_ENABLE_HTML5_HISTORY_API && IS_HASHBANG_SUPPORTED_BOT) || IS_ENABLE_HASHBANG_URL || (!empty($_SERVER['HTTP_X_PJAX_SUPPORT']) && $_SERVER['HTTP_X_PJAX_SUPPORT'] == 'false')) {
				$tmp_url = h(Router::url($url, $full));
				if (sizeof($script_name_arr = explode('/', $_SERVER['SCRIPT_NAME'])) > 2) {
					$tmp_url = '/' . $script_name_arr[1] . '/#!/' . str_replace('/' . $script_name_arr[1], '', $tmp_url);
				}
				return $tmp_url;
			} else {
				$this->_cache[$key] = h(Router::url($url, $full));
				return $this->_cache[$key];
			}
		}
// <--
	}

/**
 * Checks if a file exists when theme is used, if no file is found default location is returned
 *
 * @param string $file The file to create a webroot path to.
 * @return string Web accessible path to file.
 */
	public function webroot($file) {
		$asset = explode('?', $file);
		$asset[1] = isset($asset[1]) ? '?' . $asset[1] : null;
		$webPath = "{$this->request->webroot}" . $asset[0];
		$file = $asset[0];

		if (!empty($this->theme)) {
			$file = trim($file, '/');
			$theme = $this->theme . '/';

			if (DS === '\\') {
				$file = str_replace('/', '\\', $file);
			}

			if (file_exists(Configure::read('App.www_root') . 'theme' . DS . $this->theme . DS . $file)) {
				$webPath = "{$this->request->webroot}theme/" . $theme . $asset[0];
			} else {
				$themePath = App::themePath($this->theme);
				$path = $themePath . 'webroot' . DS . $file;
				if (file_exists($path)) {
					$webPath = "{$this->request->webroot}theme/" . $theme . $asset[0];
				}
			}
		}
		if (strpos($webPath, '//') !== false) {
			return str_replace('//', '/', $webPath . $asset[1]);
		}
		return $webPath . $asset[1];
	}

/**
 * Generate url for given asset file. Depending on options passed provides full url with domain name.
 * Also calls Helper::assetTimestamp() to add timestamp to local files
 *
 * @param string|array Path string or url array
 * @param array $options Options array. Possible keys:
 *   `fullBase` Return full url with domain name
 *   `pathPrefix` Path prefix for relative urls
 *   `ext` Asset extension to append
 *   `plugin` False value will prevent parsing path as a plugin
 * @return string Generated url
 */
	public function assetUrl($path, $options = array(), $cdn_path = '') {
		if (is_array($path)) {
			$path = $this->url($path, !empty($options['fullBase']));
		} elseif (strpos($path, '://') === false) {
			if (!array_key_exists('plugin', $options) || $options['plugin'] !== false) {
				list($plugin, $path) = $this->_View->pluginSplit($path, false);
			}
			if (!empty($options['pathPrefix']) && $path[0] !== '/') {
				$path = $options['pathPrefix'] . $path;
			}
			if (
				!empty($options['ext']) &&
				strpos($path, '?') === false &&
				substr($path, -strlen($options['ext'])) !== $options['ext']
			) {
				$path .= $options['ext'];
			}
			if (isset($plugin)) {
				$path = Inflector::underscore($plugin) . '/' . $path;
			}
			if (!empty($cdn_path)) {
				$tmp_path = $cdn_path . $path;
				if ($path[0] === '/') {
					$tmp_path = substr($cdn_path, 0, -1) . $path;
				}
				$path = $this->assetTimestamp($tmp_path);
			} else {
				$path = h($this->assetTimestamp($this->webroot($path)));
				if (!empty($options['fullBase'])) {
					if ($path[0] == '/') {
						$path = substr($path, 1);
					}
					$path = $this->url('/', true) . $path;
				}
			}
		}

		return $path;
	}

/**
 * Adds a timestamp to a file based resource based on the value of `Asset.timestamp` in
 * 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.
 */
	public function assetTimestamp($path) {
		$stamp = Configure::read('Asset.timestamp');
		$timestampEnabled = $stamp === 'force' || ($stamp === true && Configure::read('debug') > 0);
		if ($timestampEnabled && strpos($path, '?') === false) {
			$filepath = preg_replace('/^' . preg_quote($this->request->webroot, '/') . '/', '', $path);
			$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 {
				$plugin = Inflector::camelize($segments[0]);
				if (CakePlugin::loaded($plugin)) {
					unset($segments[0]);
					$pluginPath = CakePlugin::path($plugin) . 'webroot' . DS . implode(DS, $segments);
					return $path . '?' . @filemtime($pluginPath);
				}
			}
		}
		return $path;
	}

/**
 * Used to remove harmful tags from content.  Removes a number of well known XSS attacks
 * from content.  However, is not guaranteed to remove all possibilities.  Escaping
 * content is the best way to prevent all possible attacks.
 *
 * @param string|array $output Either an array of strings to clean or a single string to clean.
 * @return string|array cleaned content for output
 */
	public function clean($output) {
		$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;
	}

/**
 * Returns a space-delimited string with items of the $options array. If a
 * key of $options array happens to be one of:
 *
 * - 'compact'
 * - 'checked'
 * - 'declare'
 * - 'readonly'
 * - 'disabled'
 * - 'selected'
 * - 'defer'
 * - 'ismap'
 * - 'nohref'
 * - 'noshade'
 * - 'nowrap'
 * - 'multiple'
 * - 'noresize'
 *
 * And its value is one of:
 *
 * - '1' (string)
 * - 1 (integer)
 * - true (boolean)
 * - 'true' (string)
 *
 * 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.
 *
 * '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.
 *
 * If value for any option key is set to `null` or `false`, that option will be excluded from output.
 *
 * @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.
 * @param string $insertAfter String to be inserted after options.
 * @return string Composed attributes.
 * @deprecated This method will be moved to HtmlHelper in 3.0
 */
	protected function _parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) {
		if (!is_string($options)) {
			$options = (array)$options + array('escape' => true);

			if (!is_array($exclude)) {
				$exclude = array();
			}

			$exclude = array('escape' => true) + array_flip($exclude);
			$escape = $options['escape'];
			$attributes = array();

			foreach ($options as $key => $value) {
				if (!isset($exclude[$key]) && $value !== false && $value !== null) {
					$attributes[] = $this->_formatAttribute($key, $value, $escape);
				}
			}
			$out = implode(' ', $attributes);
		} else {
			$out = $options;
		}
		return $out ? $insertBefore . $out . $insertAfter : '';
	}

/**
 * Formats an individual attribute, and returns the string value of the composed attribute.
 * Works with minimized attributes that have the same value as their name such as 'disabled' and 'checked'
 *
 * @param string $key The name of the attribute to create
 * @param string $value The value of the attribute to create.
 * @param boolean $escape Define if the value must be escaped
 * @return string The composed attribute.
 * @deprecated This method will be moved to HtmlHelper in 3.0
 */
	protected function _formatAttribute($key, $value, $escape = true) {
		$attribute = '';
		if (is_array($value)) {
			$value = implode(' ' , $value);
		}

		if (is_numeric($key)) {
			$attribute = sprintf($this->_minimizedAttributeFormat, $value, $value);
		} elseif (in_array($key, $this->_minimizedAttributes)) {
			if ($value === 1 || $value === true || $value === 'true' || $value === '1' || $value == $key) {
				$attribute = sprintf($this->_minimizedAttributeFormat, $key, $key);
			}
		} else {
			$attribute = sprintf($this->_attributeFormat, $key, ($escape ? h($value) : $value));
		}
		return $attribute;
	}

/**
 * Sets this helper's model and field properties to the dot-separated value-pair in $entity.
 *
 * @param string $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
 */
	public function setEntity($entity, $setScope = false) {
		if ($entity === null) {
			$this->_modelScope = false;
		}
		if ($setScope === true) {
			$this->_modelScope = $entity;
		}
		$parts = array_values(Hash::filter(explode('.', $entity)));
		if (empty($parts)) {
			return;
		}
		$count = count($parts);
		$lastPart = isset($parts[$count - 1]) ? $parts[$count - 1] : null;

		// Either 'body' or 'date.month' type inputs.
		if (
			($count === 1 && $this->_modelScope && $setScope == false) ||
			(
				$count === 2 &&
				in_array($lastPart, $this->_fieldSuffixes) &&
				$this->_modelScope &&
				$parts[0] !== $this->_modelScope
			)
		) {
			$entity = $this->_modelScope . '.' . $entity;
		}

		// 0.name, 0.created.month style inputs.  Excludes inputs with the modelScope in them.
		if (
			$count >= 2 &&
			is_numeric($parts[0]) &&
			!is_numeric($parts[1]) &&
			$this->_modelScope &&
			strpos($entity, $this->_modelScope) === false
		) {
			$entity = $this->_modelScope . '.' . $entity;
		}

		$this->_association = null;

		$isHabtm = (
			isset($this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type']) &&
			$this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type'] === 'multiple' &&
			$count == 1
		);

		// habtm models are special
		if ($count == 1 && $isHabtm) {
			$this->_association = $parts[0];
			$entity = $parts[0] . '.' . $parts[0];
		} else {
			// check for associated model.
			$reversed = array_reverse($parts);
			foreach ($reversed as $i => $part) {
				if ($i > 0 && preg_match('/^[A-Z]/', $part)) {
					$this->_association = $part;
					break;
				}
			}
		}
		$this->_entityPath = $entity;
	}

/**
 * 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);
	}

/**
 * Gets the currently-used model of the rendering context.
 *
 * @return string
 */
	public function model() {
		if ($this->_association) {
			return $this->_association;
		}
		return $this->_modelScope;
	}

/**
 * Gets the currently-used model field of the rendering context.
 * Strips off field suffixes such as year, month, day, hour, min, meridian
 * when the current entity is longer than 2 elements.
 *
 * @return string
 */
	public function field() {
		$entity = $this->entity();
		$count = count($entity);
		$last = $entity[$count - 1];
		if ($count > 2 && in_array($last, $this->_fieldSuffixes)) {
			$last = isset($entity[$count - 2]) ? $entity[$count - 2] : null;
		}
		return $last;
	}

/**
 * Generates a DOM ID for the selected element, if one is not set.
 * Uses the current View::entity() settings to generate a CamelCased id attribute.
 *
 * @param array|string $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.
 */
	public function domId($options = null, $id = 'id') {
		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();
		}

		$entity = $this->entity();
		$model = array_shift($entity);
		$dom = $model . join('', array_map(array('Inflector', 'camelize'), $entity));

		if (is_array($options) && !array_key_exists($id, $options)) {
			$options[$id] = $dom;
		} elseif ($options === null) {
			return $dom;
		}
		return $options;
	}

/**
 * Gets the input field name for the current tag. Creates input name attributes
 * using CakePHP's data[Model][field] formatting.
 *
 * @param array|string $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.
 */
	protected function _name($options = array(), $field = null, $key = 'name') {
		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:
				$name = 'data[' . implode('][', $this->entity()) . ']';
			break;
		}

		if (is_array($options)) {
			$options[$key] = $name;
			return $options;
		} else {
			return $name;
		}
	}

/**
 * Gets the data for the current tag
 *
 * @param array|string $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.
 */
	public function value($options = array(), $field = null, $key = 'value') {
		if ($options === null) {
			$options = array();
		} elseif (is_string($options)) {
			$field = $options;
			$options = 0;
		}

		if (is_array($options) && isset($options[$key])) {
			return $options;
		}

		if (!empty($field)) {
			$this->setEntity($field);
		}
		$result = null;
		$data = $this->request->data;

		$entity = $this->entity();
		if (!empty($data) && !empty($entity)) {
			$result = Hash::get($data, implode('.', $entity));
		}

		$habtmKey = $this->field();
		if (empty($result) && isset($data[$habtmKey][$habtmKey]) && is_array($data[$habtmKey])) {
			$result = $data[$habtmKey][$habtmKey];
		} elseif (empty($result) && isset($data[$habtmKey]) && is_array($data[$habtmKey])) {
			if (ClassRegistry::isKeySet($habtmKey)) {
				$model = ClassRegistry::getObject($habtmKey);
				$result = $this->_selectedArray($data[$habtmKey], $model->primaryKey);
			}
		}

		if (is_array($options)) {
			if ($result === null && isset($options['default'])) {
				$result = $options['default'];
			}
			unset($options['default']);
		}

		if (is_array($options)) {
			$options[$key] = $result;
			return $options;
		} else {
			return $result;
		}
	}

/**
 * Sets the defaults for an input tag.  Will set the
 * name, value, and id attributes for an array of html attributes. Will also
 * add a 'form-error' class if the field contains validation errors.
 *
 * @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.
 */
	protected function _initInputField($field, $options = array()) {
		if ($field !== null) {
			$this->setEntity($field);
		}
		$options = (array)$options;
		$options = $this->_name($options);
		$options = $this->value($options);
		$options = $this->domId($options);
		return $options;
	}

/**
 * Adds the given class to the element options
 *
 * @param array $options Array options/attributes to add a class to
 * @param string $class The classname being added.
 * @param string $key the key to use for class.
 * @return array Array of options with $key set.
 */
	public function addClass($options = array(), $class = null, $key = 'class') {
		if (isset($options[$key]) && trim($options[$key]) != '') {
			$options[$key] .= ' ' . $class;
		} else {
			$options[$key] = $class;
		}
		return $options;
	}

/**
 * Returns a string generated by a helper method
 *
 * This method can be overridden in subclasses to do generalized output post-processing
 *
 * @param string $str String to be output.
 * @return string
 * @deprecated This method will be removed in future versions.
 */
	public function output($str) {
		return $str;
	}

/**
 * Before render callback. beforeRender is called before the view file is rendered.
 *
 * Overridden in subclasses.
 *
 * @param string $viewFile The view file that is going to be rendered
 * @return void
 */
	public function beforeRender($viewFile) {
		$this->_extras = array_intersect_key($this->request->params, array_combine($this->_paramFields, $this->_paramFields));
		if (!empty($this->request->params['requested'])) {
			$_tmp_key = '_' . strtolower(Inflector::slug($this->request->url));
			$_tmp_cache = '_cache_' . strtolower(Inflector::slug($this->request->url));
			$this->$_tmp_key = $_tmp_key;
			$this->$_tmp_key = 'url_map' . $this->$_tmp_key;
			$this->$_tmp_cache = Cache::read($this->$_tmp_key, '_cake_core_');
		}
	}

/**
 * After render callback.  afterRender is called after the view file is rendered
 * but before the layout has been rendered.
 *
 * Overridden in subclasses.
 *
 * @param string $viewFile The view file that was rendered.
 * @return void
 */
	public function afterRender($viewFile) {
		if ($this instanceof HtmlHelper && !empty($this->request->params['requested'])) {
			$_tmp_key = '_' . strtolower(Inflector::slug($this->request->url));
			$_tmp_cache = '_cache_' . strtolower(Inflector::slug($this->request->url));
			Cache::write($this->$_tmp_key, $this->$_tmp_cache, '_cake_core_');
		}
	}

/**
 * Before layout callback.  beforeLayout is called before the layout is rendered.
 *
 * Overridden in subclasses.
 *
 * @param string $layoutFile The layout about to be rendered.
 * @return void
 */
	public function beforeLayout($layoutFile) {
	}

/**
 * After layout callback.  afterLayout is called after the layout has rendered.
 *
 * Overridden in subclasses.
 *
 * @param string $layoutFile The layout file that was rendered.
 * @return void
 */
	public function afterLayout($layoutFile) {
		if ($this instanceof HtmlHelper) {
			Cache::write($this->_key, $this->_cache, '_cake_core_');
		}
	}

/**
 * Before render file callback.
 * Called before any view fragment is rendered.
 *
 * Overridden in subclasses.
 *
 * @param string $viewFile The file about to be rendered.
 * @return void
 */
	public function beforeRenderFile($viewfile) {
	}

/**
 * After render file callback.
 * Called after any view fragment is rendered.
 *
 * Overridden in subclasses.
 *
 * @param string $viewFile The file just be rendered.
 * @param string $content The content that was rendered.
 * @return void
 */
	public function afterRenderFile($viewfile, $content) {
	}

/**
 * Transforms a recordset from a hasAndBelongsToMany association to a list of selected
 * options for a multiple select element
 *
 * @param string|array $data
 * @param string $key
 * @return array
 */
	protected function _selectedArray($data, $key = 'id') {
		if (!is_array($data)) {
			$model = $data;
			if (!empty($this->request->data[$model][$model])) {
				return $this->request->data[$model][$model];
			}
			if (!empty($this->request->data[$model])) {
				$data = $this->request->data[$model];
			}
		}
		$array = array();
		if (!empty($data)) {
			foreach ($data as $row) {
				if (isset($row[$key])) {
					$array[$row[$key]] = $row[$key];
				}
			}
		}
		return empty($array) ? null : $array;
	}

/**
 * Resets the vars used by Helper::clean() to null
 *
 * @return void
 */
	protected function _reset() {
		$this->_tainted = null;
		$this->_cleaned = null;
	}

/**
 * Removes harmful content from output
 *
 * @return void
 */
	protected function _clean() {
		if (get_magic_quotes_gpc()) {
			$this->_cleaned = stripslashes($this->_tainted);
		} else {
			$this->_cleaned = $this->_tainted;
		}

		$this->_cleaned = str_replace(array("&amp;", "&lt;", "&gt;"), array("&amp;amp;", "&amp;lt;", "&amp;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);
		$this->_cleaned = html_entity_decode($this->_cleaned, ENT_COMPAT, "UTF-8");
		$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);
		$this->_cleaned = preg_replace('#([a-z]*)[\x00-\x20]*=*([\'\"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#iUu', '$1=$2nomozbinding...', $this->_cleaned);
		$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);
		do {
			$oldstring = $this->_cleaned;
			$this->_cleaned = preg_replace('#</*(applet|meta|xml|blink|link|style|script|embed|object|iframe|frame|frameset|ilayer|layer|bgsound|title|base)[^>]*>#i', "", $this->_cleaned);
		} while ($oldstring != $this->_cleaned);
		$this->_cleaned = str_replace(array("&amp;", "&lt;", "&gt;"), array("&amp;amp;", "&amp;lt;", "&amp;gt;"), $this->_cleaned);
	}
	//For CSS
    function getUniquePageId()
    {
         if ($this->params['controller'] == 'pages' && ($this->params['action'] == 'display' || $this->params['action'] == 'view') && !empty($this->params['pass'][0])) {
            $page_id = $this->params['controller'] . '-' . $this->params['pass'][0];
        } else {
            $page_id = $this->params['controller'] . '-' . $this->params['action'];
        }
        return htmlentities($page_id, ENT_QUOTES);
    }
    public function _num2words($num, $locale = 'en_US', $_precision = 2)
    {
        if (is_int($num)) {
            return $this->Numbers_Words->toWords($num, $locale);
        } else {
            $ret = $this->Numbers_Words->toWords((int)$num, $locale);
            $ret.= ' ' . __l('point');
            $decimal = sprintf('%lf', ($num - (int)$num));
            for ($i = 2, $l = 2 + $_precision; $i < $l; ++$i) {
                $ret.= ' ' . $this->Numbers_Words->toWords($decimal[$i], $locale);
            }
            return $ret;
        }
    }
    // for cleaning...
    function cHtml($str, $wrap = false, $title = false)
    {
        $changed = (($r = $this->htmlPurifier->purify($str)) != $str);
        if ($wrap) {
            if ($title) {
                $title = ' title="' . $title . '"';
            }
            $r = '<' . $wrap . ' class="c' . $changed . '"' . $title . '>' . $r . '</' . $wrap . '>';
        }
        return $r;
    }
    function cText($str, $wrap = 'span', $title = false)
    {
        //@todo No need for strip_tags ??
        $changed = (($r = trim(strip_tags($this->htmlPurifier->purify($str)))) != $str);
        if ($wrap) {
            if ($title) {
                $title = ' title="' . $title . '"';
            }
            $r = '<' . $wrap . ' class="c' . $changed . '"' . $title . '>' . $r . '</' . $wrap . '>';
        }
        return $r;
    }
    function cInt($str, $wrap = 'span', $title = false)
    {
        $changed = (($r = intval($str)) != $str);
        if ($wrap) {
            if (!$title) {
                $title = $this->_num2words($r, 'en_US');
            }
            $r = '<' . $wrap . ' class="c' . $changed . '" title="' . $title . '">' . number_format($r, 0, '', ',') . '</' . $wrap . '>';
        }
        return $r;
    }
    function cFloat($str, $wrap = 'span', $title = false)
    {
        $_precision = 2;
        $changed = (($r = floatval($str)) != $str);
        $rounded = (($rt = round($r, $_precision)) != $r);
        $r = $rt;
        if ($wrap) {
            if (!$title) {
                $title = $this->_num2words($r, 'en_US', $_precision);
            }
            $r = '<' . $wrap . ' class="c' . $changed . ' cr' . $rounded . '" title="' . $title . '">' . number_format($r, $_precision, '.', ',') . '</' . $wrap . '>';
        }
        return $r;
    }
    function cCurrency($str, $wrap = 'span', $title = false)
    {
        $_precision = 2;
        $changed = (($r = floatval($str)) != $str);
        $rounded = (($rt = round($r, $_precision)) != $r);
        $r = $rt;
        if ($wrap) {
            if (!$title) {
                $title = $this->Numbers_Words->toCurrency($r, 'en_US', '');
            }
            $r = '<' . $wrap . ' class="c' . $changed . ' cr' . $rounded . '" title="' . $title . '">' . number_format($r, $_precision, '.', ',') . '</' . $wrap . '>';
        }
        return $r;
    }
    function cDate($str, $wrap = 'span', $title = false)
    {
        $r = strftime(Configure::read('site.date.format') , strtotime($str));
		if ($wrap) {
            if (!$title) {
                $title = ' title="' . strftime(Configure::read('site.date.tooltip') , strtotime($str)) . '"';
            }
            $r = '<' . $wrap . ' class="c"' . $title . '>' . $r . '</' . $wrap . '>';
        }
        return $r;
    }
    function cDateTime($str, $wrap = 'span', $title = false)
    {
        $r = strftime(Configure::read('site.datetime.format') , strtotime($str));
		if ($wrap) {
            if (!$title) {
                $title = ' title="' . strftime(Configure::read('site.datetime.tooltip') , strtotime($str)) . '"';
            }
            $r = '<' . $wrap . ' class="c"' . $title . '>' . $r . '</' . $wrap . '>';
        }
        return $r;
    }
    function cTime($str, $wrap = 'span', $title = false)
    {
        $r = strftime(Configure::read('site.time.format') , strtotime($str));
		if ($wrap) {
            if (!$title) {
                $title = ' title="' . strftime(Configure::read('site.time.tooltip') , strtotime($str)) . '"';
            }
			$r = '<' . $wrap . ' class="c"' . $title . '>' . $r . '</' . $wrap . '>';
        }
        return $r;
    }
    function cBool($str, $wrap = 'span', $title = false)
    {
        $_options = array(
            0 => __l('No') ,
            1 => __l('Yes')
        );
        if (isset($_options[$str])) {
            $str = $_options[$str];
        }
        return $this->cText($str, $wrap, $title);
    }
    function cDateTimeHighlight($str, $wrap = 'span', $title = false)
    {
		$str1 = $str;
        if(strtotime(date('Y-m-d', strtotime($str))) == strtotime(date('Y-m-d'))) {
            $str = strftime('%I:%M %p', strtotime($str));
        } else if (strtotime(date('Y-m-d', strtotime($str))) > strtotime(date('Y-m-d')) || mktime(0, 0, 0, 0, 0, date('Y', strtotime($str))) < mktime(0, 0, 0, 0, 0, date('Y'))) {
            $str = strftime('%b %d, %Y', strtotime($str));
        } else {
            $str = strftime('%b %d', strtotime($str));
        }
        if ($wrap) {
            if (!$title) {
                $title = ' title="' . strftime(Configure::read('site.datetimehighlight.tooltip') , strtotime($str1)) . '"';
            }
            $str = '<' . $wrap . ' class="c"' . $title . '>' . $str . '</' . $wrap . '>';
        }
        return $str;
    }
    function truncate($text, $length = 100, $ending = '...', $exact = true, $considerHtml = false)
    {
		$options = array(
			'ending' => $ending,
			'exact' => $exact,
			'html' => $considerHtml
		);
        return $this->Text->truncate($text, $length, $options);
    }
    function showImage($model, $attachment, $options = array())
    {
        if (empty($attachment['id'])) {
            $attachment['id'] = constant(sprintf('%s::%s', 'ConstAttachment', $model));
        }
        $default_options = array(
            'dimension' => 'big_thumb',
            'class' => '',
            'alt' => 'alt',
            'title' => 'title',
            'type' => 'jpg'
        );
        $options = array_merge($default_options, $options);
        if ($options['dimension'] == 'original') {
            $options['width'] = $attachment['width'];
            $options['height'] = $attachment['height'];
        } else {
            extract(Configure::read('thumb_size.' . $options['dimension']));
            $aspect = (Configure::read($model . '.' . $options['dimension'] . '.is_handle_aspect') !== null) ? Configure::read($model . '.' . $options['dimension'] . '.is_handle_aspect') : Configure::read($model . '.is_handle_aspect');
            if ($aspect) {
                list($currentWidth, $currentHeight) = array(
                    $attachment['width'],
                    $attachment['height']
                );
                if (($currentHeight / $height) > ($currentWidth / $width)) {
                    $width = ceil(($currentWidth / $currentHeight) * $height);
                } else {
                    $height = ceil($width / ($currentWidth / $currentHeight));
                }
            }
            $is_original = (Configure::read($model . '.' . $options['dimension'] . '.is_not_allow_resize_beyond_original_size') !== null) ? Configure::read($model . '.' . $options['dimension'] . '.is_not_allow_resize_beyond_original_size') : Configure::read($model . '.is_not_allow_resize_beyond_original_size');
            if ($is_original && ($width > $attachment['width'] || $height > $attachment['height'])) {
                $width = $attachment['width'];
                $height = $attachment['height'];
            }
            $options['width'] = $width;
            $options['height'] = $height;
        }
        $imageUrl = getImageUrl($model, $attachment, $options);
        unset($options['dimension']);
        unset($options['type']);
        return $this->Html->image($imageUrl, $options);
    }
}
