Tidy.php 7.0 KB

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