PageRenderTime 59ms CodeModel.GetById 4ms RepoModel.GetById 0ms app.codeStats 0ms

/Auth/Yadis/ParseHTML.php

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