EntityParser.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. // if want to implement error collecting here, we'll need to use some sort
  3. // of global data (probably trigger_error) because it's impossible to pass
  4. // $config or $context to the callback functions.
  5. /**
  6. * Handles referencing and derefencing character entities
  7. */
  8. class HTMLPurifier_EntityParser
  9. {
  10. /**
  11. * Reference to entity lookup table.
  12. * @type HTMLPurifier_EntityLookup
  13. */
  14. protected $_entity_lookup;
  15. /**
  16. * Callback regex string for entities in text.
  17. * @type string
  18. */
  19. protected $_textEntitiesRegex;
  20. /**
  21. * Callback regex string for entities in attributes.
  22. * @type string
  23. */
  24. protected $_attrEntitiesRegex;
  25. /**
  26. * Tests if the beginning of a string is a semi-optional regex
  27. */
  28. protected $_semiOptionalPrefixRegex;
  29. public function __construct() {
  30. // From
  31. // http://stackoverflow.com/questions/15532252/why-is-reg-being-rendered-as-without-the-bounding-semicolon
  32. $semi_optional = "quot|QUOT|lt|LT|gt|GT|amp|AMP|AElig|Aacute|Acirc|Agrave|Aring|Atilde|Auml|COPY|Ccedil|ETH|Eacute|Ecirc|Egrave|Euml|Iacute|Icirc|Igrave|Iuml|Ntilde|Oacute|Ocirc|Ograve|Oslash|Otilde|Ouml|REG|THORN|Uacute|Ucirc|Ugrave|Uuml|Yacute|aacute|acirc|acute|aelig|agrave|aring|atilde|auml|brvbar|ccedil|cedil|cent|copy|curren|deg|divide|eacute|ecirc|egrave|eth|euml|frac12|frac14|frac34|iacute|icirc|iexcl|igrave|iquest|iuml|laquo|macr|micro|middot|nbsp|not|ntilde|oacute|ocirc|ograve|ordf|ordm|oslash|otilde|ouml|para|plusmn|pound|raquo|reg|sect|shy|sup1|sup2|sup3|szlig|thorn|times|uacute|ucirc|ugrave|uml|uuml|yacute|yen|yuml";
  33. // NB: three empty captures to put the fourth match in the right
  34. // place
  35. $this->_semiOptionalPrefixRegex = "/&()()()($semi_optional)/";
  36. $this->_textEntitiesRegex =
  37. '/&(?:'.
  38. // hex
  39. '[#]x([a-fA-F0-9]+);?|'.
  40. // dec
  41. '[#]0*(\d+);?|'.
  42. // string (mandatory semicolon)
  43. // NB: order matters: match semicolon preferentially
  44. '([A-Za-z_:][A-Za-z0-9.\-_:]*);|'.
  45. // string (optional semicolon)
  46. "($semi_optional)".
  47. ')/';
  48. $this->_attrEntitiesRegex =
  49. '/&(?:'.
  50. // hex
  51. '[#]x([a-fA-F0-9]+);?|'.
  52. // dec
  53. '[#]0*(\d+);?|'.
  54. // string (mandatory semicolon)
  55. // NB: order matters: match semicolon preferentially
  56. '([A-Za-z_:][A-Za-z0-9.\-_:]*);|'.
  57. // string (optional semicolon)
  58. // don't match if trailing is equals or alphanumeric (URL
  59. // like)
  60. "($semi_optional)(?![=;A-Za-z0-9])".
  61. ')/';
  62. }
  63. /**
  64. * Substitute entities with the parsed equivalents. Use this on
  65. * textual data in an HTML document (as opposed to attributes.)
  66. *
  67. * @param string $string String to have entities parsed.
  68. * @return string Parsed string.
  69. */
  70. public function substituteTextEntities($string)
  71. {
  72. return preg_replace_callback(
  73. $this->_textEntitiesRegex,
  74. array($this, 'entityCallback'),
  75. $string
  76. );
  77. }
  78. /**
  79. * Substitute entities with the parsed equivalents. Use this on
  80. * attribute contents in documents.
  81. *
  82. * @param string $string String to have entities parsed.
  83. * @return string Parsed string.
  84. */
  85. public function substituteAttrEntities($string)
  86. {
  87. return preg_replace_callback(
  88. $this->_attrEntitiesRegex,
  89. array($this, 'entityCallback'),
  90. $string
  91. );
  92. }
  93. /**
  94. * Callback function for substituteNonSpecialEntities() that does the work.
  95. *
  96. * @param array $matches PCRE matches array, with 0 the entire match, and
  97. * either index 1, 2 or 3 set with a hex value, dec value,
  98. * or string (respectively).
  99. * @return string Replacement string.
  100. */
  101. protected function entityCallback($matches)
  102. {
  103. $entity = $matches[0];
  104. $hex_part = @$matches[1];
  105. $dec_part = @$matches[2];
  106. $named_part = empty($matches[3]) ? (empty($matches[4]) ? "" : $matches[4]) : $matches[3];
  107. if ($hex_part !== NULL && $hex_part !== "") {
  108. return HTMLPurifier_Encoder::unichr(hexdec($hex_part));
  109. } elseif ($dec_part !== NULL && $dec_part !== "") {
  110. return HTMLPurifier_Encoder::unichr((int) $dec_part);
  111. } else {
  112. if (!$this->_entity_lookup) {
  113. $this->_entity_lookup = HTMLPurifier_EntityLookup::instance();
  114. }
  115. if (isset($this->_entity_lookup->table[$named_part])) {
  116. return $this->_entity_lookup->table[$named_part];
  117. } else {
  118. // exact match didn't match anything, so test if
  119. // any of the semicolon optional match the prefix.
  120. // Test that this is an EXACT match is important to
  121. // prevent infinite loop
  122. if (!empty($matches[3])) {
  123. return preg_replace_callback(
  124. $this->_semiOptionalPrefixRegex,
  125. array($this, 'entityCallback'),
  126. $entity
  127. );
  128. }
  129. return $entity;
  130. }
  131. }
  132. }
  133. // LEGACY CODE BELOW
  134. /**
  135. * Callback regex string for parsing entities.
  136. * @type string
  137. */
  138. protected $_substituteEntitiesRegex =
  139. '/&(?:[#]x([a-fA-F0-9]+)|[#]0*(\d+)|([A-Za-z_:][A-Za-z0-9.\-_:]*));?/';
  140. // 1. hex 2. dec 3. string (XML style)
  141. /**
  142. * Decimal to parsed string conversion table for special entities.
  143. * @type array
  144. */
  145. protected $_special_dec2str =
  146. array(
  147. 34 => '"',
  148. 38 => '&',
  149. 39 => "'",
  150. 60 => '<',
  151. 62 => '>'
  152. );
  153. /**
  154. * Stripped entity names to decimal conversion table for special entities.
  155. * @type array
  156. */
  157. protected $_special_ent2dec =
  158. array(
  159. 'quot' => 34,
  160. 'amp' => 38,
  161. 'lt' => 60,
  162. 'gt' => 62
  163. );
  164. /**
  165. * Substitutes non-special entities with their parsed equivalents. Since
  166. * running this whenever you have parsed character is t3h 5uck, we run
  167. * it before everything else.
  168. *
  169. * @param string $string String to have non-special entities parsed.
  170. * @return string Parsed string.
  171. */
  172. public function substituteNonSpecialEntities($string)
  173. {
  174. // it will try to detect missing semicolons, but don't rely on it
  175. return preg_replace_callback(
  176. $this->_substituteEntitiesRegex,
  177. array($this, 'nonSpecialEntityCallback'),
  178. $string
  179. );
  180. }
  181. /**
  182. * Callback function for substituteNonSpecialEntities() that does the work.
  183. *
  184. * @param array $matches PCRE matches array, with 0 the entire match, and
  185. * either index 1, 2 or 3 set with a hex value, dec value,
  186. * or string (respectively).
  187. * @return string Replacement string.
  188. */
  189. protected function nonSpecialEntityCallback($matches)
  190. {
  191. // replaces all but big five
  192. $entity = $matches[0];
  193. $is_num = (@$matches[0][1] === '#');
  194. if ($is_num) {
  195. $is_hex = (@$entity[2] === 'x');
  196. $code = $is_hex ? hexdec($matches[1]) : (int) $matches[2];
  197. // abort for special characters
  198. if (isset($this->_special_dec2str[$code])) {
  199. return $entity;
  200. }
  201. return HTMLPurifier_Encoder::unichr($code);
  202. } else {
  203. if (isset($this->_special_ent2dec[$matches[3]])) {
  204. return $entity;
  205. }
  206. if (!$this->_entity_lookup) {
  207. $this->_entity_lookup = HTMLPurifier_EntityLookup::instance();
  208. }
  209. if (isset($this->_entity_lookup->table[$matches[3]])) {
  210. return $this->_entity_lookup->table[$matches[3]];
  211. } else {
  212. return $entity;
  213. }
  214. }
  215. }
  216. /**
  217. * Substitutes only special entities with their parsed equivalents.
  218. *
  219. * @notice We try to avoid calling this function because otherwise, it
  220. * would have to be called a lot (for every parsed section).
  221. *
  222. * @param string $string String to have non-special entities parsed.
  223. * @return string Parsed string.
  224. */
  225. public function substituteSpecialEntities($string)
  226. {
  227. return preg_replace_callback(
  228. $this->_substituteEntitiesRegex,
  229. array($this, 'specialEntityCallback'),
  230. $string
  231. );
  232. }
  233. /**
  234. * Callback function for substituteSpecialEntities() that does the work.
  235. *
  236. * This callback has same syntax as nonSpecialEntityCallback().
  237. *
  238. * @param array $matches PCRE-style matches array, with 0 the entire match, and
  239. * either index 1, 2 or 3 set with a hex value, dec value,
  240. * or string (respectively).
  241. * @return string Replacement string.
  242. */
  243. protected function specialEntityCallback($matches)
  244. {
  245. $entity = $matches[0];
  246. $is_num = (@$matches[0][1] === '#');
  247. if ($is_num) {
  248. $is_hex = (@$entity[2] === 'x');
  249. $int = $is_hex ? hexdec($matches[1]) : (int) $matches[2];
  250. return isset($this->_special_dec2str[$int]) ?
  251. $this->_special_dec2str[$int] :
  252. $entity;
  253. } else {
  254. return isset($this->_special_ent2dec[$matches[3]]) ?
  255. $this->_special_dec2str[$this->_special_ent2dec[$matches[3]]] :
  256. $entity;
  257. }
  258. }
  259. }
  260. // vim: et sw=4 sts=4