ParseHTML.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. * Strip single and double quotes off of a string, if they are
  59. * present.
  60. *
  61. * @access private
  62. * @param string $str The original string
  63. * @return string $new_str The new string with leading and
  64. * trailing quotes removed
  65. */
  66. function removeQuotes($str)
  67. {
  68. $matches = array();
  69. $double = '/^"(.*)"$/';
  70. $single = "/^\'(.*)\'$/";
  71. if (preg_match($double, $str, $matches)) {
  72. return $matches[1];
  73. } else if (preg_match($single, $str, $matches)) {
  74. return $matches[1];
  75. } else {
  76. return $str;
  77. }
  78. }
  79. /**
  80. * Create a regular expression that will match an opening
  81. * or closing tag from a set of names.
  82. *
  83. * @access private
  84. * @param mixed $tag_names Tag names to match
  85. * @param mixed $close false/0 = no, true/1 = yes, other = maybe
  86. * @param mixed $self_close false/0 = no, true/1 = yes, other = maybe
  87. * @return string $regex A regular expression string to be used
  88. * in, say, preg_match.
  89. */
  90. function tagPattern($tag_names, $close, $self_close)
  91. {
  92. if (is_array($tag_names)) {
  93. $tag_names = '(?:'.implode('|',$tag_names).')';
  94. }
  95. if ($close) {
  96. $close = '\/' . (($close == 1)? '' : '?');
  97. } else {
  98. $close = '';
  99. }
  100. if ($self_close) {
  101. $self_close = '(?:\/\s*)' . (($self_close == 1)? '' : '?');
  102. } else {
  103. $self_close = '';
  104. }
  105. $expr = sprintf($this->_tag_expr, $close, $tag_names, $self_close);
  106. return sprintf("/%s/%s", $expr, $this->_re_flags);
  107. }
  108. /**
  109. * Given an HTML document string, this finds all the META tags in
  110. * the document, provided they are found in the
  111. * <HTML><HEAD>...</HEAD> section of the document. The <HTML> tag
  112. * may be missing.
  113. *
  114. * @access private
  115. * @param string $html_string An HTMl document string
  116. * @return array $tag_list Array of tags; each tag is an array of
  117. * attribute -> value.
  118. */
  119. function getMetaTags($html_string)
  120. {
  121. $html_string = preg_replace($this->_removed_re,
  122. "",
  123. $html_string);
  124. $key_tags = array($this->tagPattern('html', false, false),
  125. $this->tagPattern('head', false, false),
  126. $this->tagPattern('head', true, false),
  127. $this->tagPattern('html', true, false),
  128. $this->tagPattern(array(
  129. 'body', 'frameset', 'frame', 'p', 'div',
  130. 'table','span','a'), 'maybe', 'maybe'));
  131. $key_tags_pos = array();
  132. foreach ($key_tags as $pat) {
  133. $matches = array();
  134. preg_match($pat, $html_string, $matches, PREG_OFFSET_CAPTURE);
  135. if($matches) {
  136. $key_tags_pos[] = $matches[0][1];
  137. } else {
  138. $key_tags_pos[] = null;
  139. }
  140. }
  141. // no opening head tag
  142. if (is_null($key_tags_pos[1])) {
  143. return array();
  144. }
  145. // the effective </head> is the min of the following
  146. if (is_null($key_tags_pos[2])) {
  147. $key_tags_pos[2] = strlen($html_string);
  148. }
  149. foreach (array($key_tags_pos[3], $key_tags_pos[4]) as $pos) {
  150. if (!is_null($pos) && $pos < $key_tags_pos[2]) {
  151. $key_tags_pos[2] = $pos;
  152. }
  153. }
  154. // closing head tag comes before opening head tag
  155. if ($key_tags_pos[1] > $key_tags_pos[2]) {
  156. return array();
  157. }
  158. // if there is an opening html tag, make sure the opening head tag
  159. // comes after it
  160. if (!is_null($key_tags_pos[0]) && $key_tags_pos[1] < $key_tags_pos[0]) {
  161. return array();
  162. }
  163. $html_string = substr($html_string, $key_tags_pos[1],
  164. ($key_tags_pos[2]-$key_tags_pos[1]));
  165. $link_data = array();
  166. $link_matches = array();
  167. if (!preg_match_all($this->tagPattern('meta', false, 'maybe'),
  168. $html_string, $link_matches)) {
  169. return array();
  170. }
  171. foreach ($link_matches[0] as $link) {
  172. $attr_matches = array();
  173. preg_match_all($this->_attr_find, $link, $attr_matches);
  174. $link_attrs = array();
  175. foreach ($attr_matches[0] as $index => $full_match) {
  176. $name = $attr_matches[1][$index];
  177. $value = html_entity_decode(
  178. $this->removeQuotes($attr_matches[2][$index]));
  179. $link_attrs[strtolower($name)] = $value;
  180. }
  181. $link_data[] = $link_attrs;
  182. }
  183. return $link_data;
  184. }
  185. /**
  186. * Looks for a META tag with an "http-equiv" attribute whose value
  187. * is one of ("x-xrds-location", "x-yadis-location"), ignoring
  188. * case. If such a META tag is found, its "content" attribute
  189. * value is returned.
  190. *
  191. * @param string $html_string An HTML document in string format
  192. * @return mixed $content The "content" attribute value of the
  193. * META tag, if found, or null if no such tag was found.
  194. */
  195. function getHTTPEquiv($html_string)
  196. {
  197. $meta_tags = $this->getMetaTags($html_string);
  198. if ($meta_tags) {
  199. foreach ($meta_tags as $tag) {
  200. if (array_key_exists('http-equiv', $tag) &&
  201. (in_array(strtolower($tag['http-equiv']),
  202. array('x-xrds-location', 'x-yadis-location'))) &&
  203. array_key_exists('content', $tag)) {
  204. return $tag['content'];
  205. }
  206. }
  207. }
  208. return null;
  209. }
  210. }