ParseHTML.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * This is the HTML pseudo-parser for the Yadis library.
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: See the COPYING file included in this distribution.
  8. *
  9. * @package OpenID
  10. * @author JanRain, Inc. <openid@janrain.com>
  11. * @copyright 2005-2008 Janrain, Inc.
  12. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
  13. */
  14. /**
  15. * This class is responsible for scanning an HTML string to find META
  16. * tags and their attributes. This is used by the Yadis discovery
  17. * process. This class must be instantiated to be used.
  18. *
  19. * @package OpenID
  20. */
  21. class Auth_Yadis_ParseHTML {
  22. /**
  23. * @access private
  24. */
  25. var $_re_flags = "si";
  26. /**
  27. * @access private
  28. */
  29. var $_removed_re =
  30. "<!--.*?-->|<!\[CDATA\[.*?\]\]>|<script\b(?!:)[^>]*>.*?<\/script>";
  31. /**
  32. * @access private
  33. */
  34. var $_tag_expr = "<%s%s(?:\s.*?)?%s>";
  35. /**
  36. * @access private
  37. */
  38. var $_attr_find = '\b([-\w]+)=(".*?"|\'.*?\'|.+?)[\/\s>]';
  39. function Auth_Yadis_ParseHTML()
  40. {
  41. $this->_attr_find = sprintf("/%s/%s",
  42. $this->_attr_find,
  43. $this->_re_flags);
  44. $this->_removed_re = sprintf("/%s/%s",
  45. $this->_removed_re,
  46. $this->_re_flags);
  47. $this->_entity_replacements = array(
  48. 'amp' => '&',
  49. 'lt' => '<',
  50. 'gt' => '>',
  51. 'quot' => '"'
  52. );
  53. $this->_ent_replace =
  54. sprintf("&(%s);", implode("|",
  55. $this->_entity_replacements));
  56. }
  57. /**
  58. * Replace HTML entities (amp, lt, gt, and quot) as well as
  59. * numeric entities (e.g. #x9f;) with their actual values and
  60. * return the new string.
  61. *
  62. * @access private
  63. * @param string $str The string in which to look for entities
  64. * @return string $new_str The new string entities decoded
  65. */
  66. function replaceEntities($str)
  67. {
  68. foreach ($this->_entity_replacements as $old => $new) {
  69. $str = preg_replace(sprintf("/&%s;/", $old), $new, $str);
  70. }
  71. // Replace numeric entities because html_entity_decode doesn't
  72. // do it for us.
  73. $str = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $str);
  74. $str = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $str);
  75. return $str;
  76. }
  77. /**
  78. * Strip single and double quotes off of a string, if they are
  79. * present.
  80. *
  81. * @access private
  82. * @param string $str The original string
  83. * @return string $new_str The new string with leading and
  84. * trailing quotes removed
  85. */
  86. function removeQuotes($str)
  87. {
  88. $matches = array();
  89. $double = '/^"(.*)"$/';
  90. $single = "/^\'(.*)\'$/";
  91. if (preg_match($double, $str, $matches)) {
  92. return $matches[1];
  93. } else if (preg_match($single, $str, $matches)) {
  94. return $matches[1];
  95. } else {
  96. return $str;
  97. }
  98. }
  99. /**
  100. * Create a regular expression that will match an opening
  101. * or closing tag from a set of names.
  102. *
  103. * @access private
  104. * @param mixed $tag_names Tag names to match
  105. * @param mixed $close false/0 = no, true/1 = yes, other = maybe
  106. * @param mixed $self_close false/0 = no, true/1 = yes, other = maybe
  107. * @return string $regex A regular expression string to be used
  108. * in, say, preg_match.
  109. */
  110. function tagPattern($tag_names, $close, $self_close)
  111. {
  112. if (is_array($tag_names)) {
  113. $tag_names = '(?:'.implode('|',$tag_names).')';
  114. }
  115. if ($close) {
  116. $close = '\/' . (($close == 1)? '' : '?');
  117. } else {
  118. $close = '';
  119. }
  120. if ($self_close) {
  121. $self_close = '(?:\/\s*)' . (($self_close == 1)? '' : '?');
  122. } else {
  123. $self_close = '';
  124. }
  125. $expr = sprintf($this->_tag_expr, $close, $tag_names, $self_close);
  126. return sprintf("/%s/%s", $expr, $this->_re_flags);
  127. }
  128. /**
  129. * Given an HTML document string, this finds all the META tags in
  130. * the document, provided they are found in the
  131. * <HTML><HEAD>...</HEAD> section of the document. The <HTML> tag
  132. * may be missing.
  133. *
  134. * @access private
  135. * @param string $html_string An HTMl document string
  136. * @return array $tag_list Array of tags; each tag is an array of
  137. * attribute -> value.
  138. */
  139. function getMetaTags($html_string)
  140. {
  141. $html_string = preg_replace($this->_removed_re,
  142. "",
  143. $html_string);
  144. $key_tags = array($this->tagPattern('html', false, false),
  145. $this->tagPattern('head', false, false),
  146. $this->tagPattern('head', true, false),
  147. $this->tagPattern('html', true, false),
  148. $this->tagPattern(array(
  149. 'body', 'frameset', 'frame', 'p', 'div',
  150. 'table','span','a'), 'maybe', 'maybe'));
  151. $key_tags_pos = array();
  152. foreach ($key_tags as $pat) {
  153. $matches = array();
  154. preg_match($pat, $html_string, $matches, PREG_OFFSET_CAPTURE);
  155. if($matches) {
  156. $key_tags_pos[] = $matches[0][1];
  157. } else {
  158. $key_tags_pos[] = null;
  159. }
  160. }
  161. // no opening head tag
  162. if (is_null($key_tags_pos[1])) {
  163. return array();
  164. }
  165. // the effective </head> is the min of the following
  166. if (is_null($key_tags_pos[2])) {
  167. $key_tags_pos[2] = strlen($html_string);
  168. }
  169. foreach (array($key_tags_pos[3], $key_tags_pos[4]) as $pos) {
  170. if (!is_null($pos) && $pos < $key_tags_pos[2]) {
  171. $key_tags_pos[2] = $pos;
  172. }
  173. }
  174. // closing head tag comes before opening head tag
  175. if ($key_tags_pos[1] > $key_tags_pos[2]) {
  176. return array();
  177. }
  178. // if there is an opening html tag, make sure the opening head tag
  179. // comes after it
  180. if (!is_null($key_tags_pos[0]) && $key_tags_pos[1] < $key_tags_pos[0]) {
  181. return array();
  182. }
  183. $html_string = substr($html_string, $key_tags_pos[1],
  184. ($key_tags_pos[2]-$key_tags_pos[1]));
  185. $link_data = array();
  186. $link_matches = array();
  187. if (!preg_match_all($this->tagPattern('meta', false, 'maybe'),
  188. $html_string, $link_matches)) {
  189. return array();
  190. }
  191. foreach ($link_matches[0] as $link) {
  192. $attr_matches = array();
  193. preg_match_all($this->_attr_find, $link, $attr_matches);
  194. $link_attrs = array();
  195. foreach ($attr_matches[0] as $index => $full_match) {
  196. $name = $attr_matches[1][$index];
  197. $value = $this->replaceEntities(
  198. $this->removeQuotes($attr_matches[2][$index]));
  199. $link_attrs[strtolower($name)] = $value;
  200. }
  201. $link_data[] = $link_attrs;
  202. }
  203. return $link_data;
  204. }
  205. /**
  206. * Looks for a META tag with an "http-equiv" attribute whose value
  207. * is one of ("x-xrds-location", "x-yadis-location"), ignoring
  208. * case. If such a META tag is found, its "content" attribute
  209. * value is returned.
  210. *
  211. * @param string $html_string An HTML document in string format
  212. * @return mixed $content The "content" attribute value of the
  213. * META tag, if found, or null if no such tag was found.
  214. */
  215. function getHTTPEquiv($html_string)
  216. {
  217. $meta_tags = $this->getMetaTags($html_string);
  218. if ($meta_tags) {
  219. foreach ($meta_tags as $tag) {
  220. if (array_key_exists('http-equiv', $tag) &&
  221. (in_array(strtolower($tag['http-equiv']),
  222. array('x-xrds-location', 'x-yadis-location'))) &&
  223. array_key_exists('content', $tag)) {
  224. return $tag['content'];
  225. }
  226. }
  227. }
  228. return null;
  229. }
  230. }