Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?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); 
	}
}
?>