1 : <?php
2 :
3 : /**
4 : * Abstract class for a set of proprietary modules that clean up (tidy)
5 : * poorly written HTML.
6 : * @todo Figure out how to protect some of these methods/properties
7 : */
8 1 : class HTMLPurifier_HTMLModule_Tidy extends HTMLPurifier_HTMLModule
9 : {
10 :
11 : /**
12 : * List of supported levels. Index zero is a special case "no fixes"
13 : * level.
14 : */
15 : public $levels = array(0 => 'none', 'light', 'medium', 'heavy');
16 :
17 : /**
18 : * Default level to place all fixes in. Disabled by default
19 : */
20 : public $defaultLevel = null;
21 :
22 : /**
23 : * Lists of fixes used by getFixesForLevel(). Format is:
24 : * HTMLModule_Tidy->fixesForLevel[$level] = array('fix-1', 'fix-2');
25 : */
26 : public $fixesForLevel = array(
27 : 'light' => array(),
28 : 'medium' => array(),
29 : 'heavy' => array()
30 : );
31 :
32 : /**
33 : * Lazy load constructs the module by determining the necessary
34 : * fixes to create and then delegating to the populate() function.
35 : * @todo Wildcard matching and error reporting when an added or
36 : * subtracted fix has no effect.
37 : */
38 : public function construct($config) {
39 :
40 : // create fixes, initialize fixesForLevel
41 1 : $fixes = $this->makeFixes();
42 1 : $this->makeFixesForLevel($fixes);
43 :
44 : // figure out which fixes to use
45 1 : $level = $config->get('HTML', 'TidyLevel');
46 1 : $fixes_lookup = $this->getFixesForLevel($level);
47 :
48 : // get custom fix declarations: these need namespace processing
49 1 : $add_fixes = $config->get('HTML', 'TidyAdd');
50 1 : $remove_fixes = $config->get('HTML', 'TidyRemove');
51 :
52 1 : foreach ($fixes as $name => $fix) {
53 : // needs to be refactored a little to implement globbing
54 : if (
55 1 : isset($remove_fixes[$name]) ||
56 1 : (!isset($add_fixes[$name]) && !isset($fixes_lookup[$name]))
57 1 : ) {
58 1 : unset($fixes[$name]);
59 1 : }
60 1 : }
61 :
62 : // populate this module with necessary fixes
63 1 : $this->populate($fixes);
64 :
65 1 : }
66 :
67 : /**
68 : * Retrieves all fixes per a level, returning fixes for that specific
69 : * level as well as all levels below it.
70 : * @param $level String level identifier, see $levels for valid values
71 : * @return Lookup up table of fixes
72 : */
73 : public function getFixesForLevel($level) {
74 1 : if ($level == $this->levels[0]) {
75 0 : return array();
76 : }
77 1 : $activated_levels = array();
78 1 : for ($i = 1, $c = count($this->levels); $i < $c; $i++) {
79 1 : $activated_levels[] = $this->levels[$i];
80 1 : if ($this->levels[$i] == $level) break;
81 1 : }
82 1 : if ($i == $c) {
83 0 : trigger_error(
84 0 : 'Tidy level ' . htmlspecialchars($level) . ' not recognized',
85 : E_USER_WARNING
86 0 : );
87 0 : return array();
88 : }
89 1 : $ret = array();
90 1 : foreach ($activated_levels as $level) {
91 1 : foreach ($this->fixesForLevel[$level] as $fix) {
92 1 : $ret[$fix] = true;
93 1 : }
94 1 : }
95 1 : return $ret;
96 : }
97 :
98 : /**
99 : * Dynamically populates the $fixesForLevel member variable using
100 : * the fixes array. It may be custom overloaded, used in conjunction
101 : * with $defaultLevel, or not used at all.
102 : */
103 : public function makeFixesForLevel($fixes) {
104 1 : if (!isset($this->defaultLevel)) return;
105 1 : if (!isset($this->fixesForLevel[$this->defaultLevel])) {
106 0 : trigger_error(
107 0 : 'Default level ' . $this->defaultLevel . ' does not exist',
108 : E_USER_ERROR
109 0 : );
110 0 : return;
111 : }
112 1 : $this->fixesForLevel[$this->defaultLevel] = array_keys($fixes);
113 1 : }
114 :
115 : /**
116 : * Populates the module with transforms and other special-case code
117 : * based on a list of fixes passed to it
118 : * @param $lookup Lookup table of fixes to activate
119 : */
120 : public function populate($fixes) {
121 1 : foreach ($fixes as $name => $fix) {
122 : // determine what the fix is for
123 1 : list($type, $params) = $this->getFixType($name);
124 : switch ($type) {
125 1 : case 'attr_transform_pre':
126 1 : case 'attr_transform_post':
127 1 : $attr = $params['attr'];
128 1 : if (isset($params['element'])) {
129 0 : $element = $params['element'];
130 0 : if (empty($this->info[$element])) {
131 0 : $e = $this->addBlankElement($element);
132 0 : } else {
133 0 : $e = $this->info[$element];
134 : }
135 0 : } else {
136 1 : $type = "info_$type";
137 1 : $e = $this;
138 : }
139 : // PHP does some weird parsing when I do
140 : // $e->$type[$attr], so I have to assign a ref.
141 1 : $f =& $e->$type;
142 1 : $f[$attr] = $fix;
143 1 : break;
144 0 : case 'tag_transform':
145 0 : $this->info_tag_transform[$params['element']] = $fix;
146 0 : break;
147 0 : case 'child':
148 0 : case 'content_model_type':
149 0 : $element = $params['element'];
150 0 : if (empty($this->info[$element])) {
151 0 : $e = $this->addBlankElement($element);
152 0 : } else {
153 0 : $e = $this->info[$element];
154 : }
155 0 : $e->$type = $fix;
156 0 : break;
157 0 : default:
158 0 : trigger_error("Fix type $type not supported", E_USER_ERROR);
159 0 : break;
160 0 : }
161 1 : }
162 1 : }
163 :
164 : /**
165 : * Parses a fix name and determines what kind of fix it is, as well
166 : * as other information defined by the fix
167 : * @param $name String name of fix
168 : * @return array(string $fix_type, array $fix_parameters)
169 : * @note $fix_parameters is type dependant, see populate() for usage
170 : * of these parameters
171 : */
172 : public function getFixType($name) {
173 : // parse it
174 1 : $property = $attr = null;
175 1 : if (strpos($name, '#') !== false) list($name, $property) = explode('#', $name);
176 1 : if (strpos($name, '@') !== false) list($name, $attr) = explode('@', $name);
177 :
178 : // figure out the parameters
179 1 : $params = array();
180 1 : if ($name !== '') $params['element'] = $name;
181 1 : if (!is_null($attr)) $params['attr'] = $attr;
182 :
183 : // special case: attribute transform
184 1 : if (!is_null($attr)) {
185 1 : if (is_null($property)) $property = 'pre';
186 1 : $type = 'attr_transform_' . $property;
187 1 : return array($type, $params);
188 : }
189 :
190 : // special case: tag transform
191 0 : if (is_null($property)) {
192 0 : return array('tag_transform', $params);
193 : }
194 :
195 0 : return array($property, $params);
196 :
197 : }
198 :
199 : /**
200 : * Defines all fixes the module will perform in a compact
201 : * associative array of fix name to fix implementation.
202 : */
203 0 : public function makeFixes() {}
204 :
205 : }
206 :
207 :
|