Report abuse

<?php
/**
* Brita Behavior
*
* this is a behaviour to clean up inputted (x)html to make it validate
*
* PHP versions 4 and 5
*
* Copyright (c) 
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright      Copyright (c) 2009, Jon Bennett.
* @link            http://jben.net
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
*/

class BritaBehavior extends ModelBehavior 
{

	/**
	 * Contain settings indexed by model name.
	 *
	 * @var array
	 * @access private
	 */
	var $__settings = array();
	
	/**
	 * Textile class instance
	 *
	 * @var array
	 * @access private
	 */
	var $__texy = array();
	
	/**
	 * default 'field' settings
	 *
	 * @var array
	 * @access private
	 */
	var $__default = array(
		'TidyLevel' 	=> 'heavy',
		// 'Allowed' 		=> 'a[href|title],em,p,blockquote,h2,h3,h4,h5,h6,img,ul,li,ol,strong,br', 
		'Allowed'		=> '*[color|style|size],strong,em,p,img[src|width|height|alt|title],li,ul,ol,sup,sub',
		'Doctype'		=> 'XHTML 1.0 Transitional',
		'Encoding' 		=> 'UTF-8',
		'strip' 		=> array('span')
	);
	

	/**
	* setups the behaviour
	*
	* @param object &$model the model that the behaviour is called from 
	* @param array $settings the settings array defined in the model
	*/
	function setup(&$Model, $settings = array()) 
	{
		if (!isset($this->__settings[$Model->alias]))
		{
			if (!empty($settings))
			{
				foreach($settings as $field=>$options)
				{
					$this->__settings[$Model->alias][$field] = am($this->__default, $options);
				}			
			}
			else
			{
				// exit!
				exit;
			}
		}
	}

	/**
	* beforeSave callback
	*
	*/
	function beforeSave(&$Model) 
	{	
		foreach($this->__settings[$Model->alias] as $field => $options)
		{
			// custom code to replace short hand image tags with full paths.
			if(isset($Model->data[$Model->alias][$field]))
			{
				// Include library
				if(!App::import('Vendor','HTMLPurifier' ,array('file'=>'htmlpurifier-4.0.0'.DS.'library'.DS.'HTMLPurifier.auto.php')))
				{
					trigger_error('Could Not locate HTMLPurifier.  Please Place it in app/vendors/HTMLPurifier', E_USER_WARNING);
					return;
				}
				
				// check for Strip
				if ($options['strip'])
				{
					// Loop over and strip
					foreach($options['strip'] as $tag)
					{
						$Model->data[$Model->alias][$field] = preg_replace('/<' . $tag . '[^>]*>/i', '', $Model->data[$Model->alias][$field]);
						$Model->data[$Model->alias][$field] = preg_replace('/<\/' . $tag . '[^>]*>/i', '', $Model->data[$Model->alias][$field]);
					}
				}
				
				//the next few lines allow the config settings to be cached
		        $config = HTMLPurifier_Config::createDefault();
		        $config->set('HTML', 'DefinitionID', 'made by debugged interactive designs');
		        $config->set('HTML', 'DefinitionRev', 1);
		        //levels describe how aggressive the Tidy module should be when cleaning up html
		        //four levels: none, light, medium, heavy
		        $config->set('HTML', 'TidyLevel', 'heavy');
		        //check the top of your html file for the next two
		        $config->set('HTML', 'Doctype', $options['Doctype']);
		        $config->set('Core', 'Encoding', $options['Allowed']);

				$config->set('HTML', 'Allowed', $options['Allowed']);
				
				// Create instance of the purifier
				$cleaner = new HTMLPurifier($config);
				
				// clean data
				$Model->data[$Model->name][$field] = $clearer->purify($Model->data[$Model->name][$field]);
			}
			
		}
		
    	return parent::beforeSave($Model); 
	}
}
?>