4 * This is the HTML pseudo-parser for the Yadis library.
8 * LICENSE: See the COPYING file included in this distribution.
11 * @author JanRain, Inc. <openid@janrain.com>
12 * @copyright 2005-2008 Janrain, Inc.
13 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
17 * This class is responsible for scanning an HTML string to find META
18 * tags and their attributes. This is used by the Yadis discovery
19 * process. This class must be instantiated to be used.
23 class Auth_Yadis_ParseHTML {
28 var $_re_flags = "si";
34 "<!--.*?-->|<!\[CDATA\[.*?\]\]>|<script\b(?!:)[^>]*>.*?<\/script>";
39 var $_tag_expr = "<%s%s(?:\s.*?)?%s>";
44 var $_attr_find = '\b([-\w]+)=(".*?"|\'.*?\'|.+?)[\/\s>]';
46 function Auth_Yadis_ParseHTML()
48 $this->_attr_find = sprintf("/%s/%s",
52 $this->_removed_re = sprintf("/%s/%s",
56 $this->_entity_replacements = array(
64 sprintf("&(%s);", implode("|",
65 $this->_entity_replacements));
69 * Strip single and double quotes off of a string, if they are
73 * @param string $str The original string
74 * @return string $new_str The new string with leading and
75 * trailing quotes removed
77 function removeQuotes($str)
80 $double = '/^"(.*)"$/';
81 $single = "/^\'(.*)\'$/";
83 if (preg_match($double, $str, $matches)) {
85 } else if (preg_match($single, $str, $matches)) {
93 * Create a regular expression that will match an opening
94 * or closing tag from a set of names.
97 * @param mixed $tag_names Tag names to match
98 * @param mixed $close false/0 = no, true/1 = yes, other = maybe
99 * @param mixed $self_close false/0 = no, true/1 = yes, other = maybe
100 * @return string $regex A regular expression string to be used
101 * in, say, preg_match.
103 function tagPattern($tag_names, $close, $self_close)
105 if (is_array($tag_names)) {
106 $tag_names = '(?:'.implode('|',$tag_names).')';
109 $close = '\/' . (($close == 1)? '' : '?');
114 $self_close = '(?:\/\s*)' . (($self_close == 1)? '' : '?');
118 $expr = sprintf($this->_tag_expr, $close, $tag_names, $self_close);
120 return sprintf("/%s/%s", $expr, $this->_re_flags);
124 * Given an HTML document string, this finds all the META tags in
125 * the document, provided they are found in the
126 * <HTML><HEAD>...</HEAD> section of the document. The <HTML> tag
130 * @param string $html_string An HTMl document string
131 * @return array $tag_list Array of tags; each tag is an array of
132 * attribute -> value.
134 function getMetaTags($html_string)
136 $html_string = preg_replace($this->_removed_re,
140 $key_tags = array($this->tagPattern('html', false, false),
141 $this->tagPattern('head', false, false),
142 $this->tagPattern('head', true, false),
143 $this->tagPattern('html', true, false),
144 $this->tagPattern(array(
145 'body', 'frameset', 'frame', 'p', 'div',
146 'table','span','a'), 'maybe', 'maybe'));
147 $key_tags_pos = array();
148 foreach ($key_tags as $pat) {
150 preg_match($pat, $html_string, $matches, PREG_OFFSET_CAPTURE);
152 $key_tags_pos[] = $matches[0][1];
154 $key_tags_pos[] = null;
157 // no opening head tag
158 if (is_null($key_tags_pos[1])) {
161 // the effective </head> is the min of the following
162 if (is_null($key_tags_pos[2])) {
163 $key_tags_pos[2] = strlen($html_string);
165 foreach (array($key_tags_pos[3], $key_tags_pos[4]) as $pos) {
166 if (!is_null($pos) && $pos < $key_tags_pos[2]) {
167 $key_tags_pos[2] = $pos;
170 // closing head tag comes before opening head tag
171 if ($key_tags_pos[1] > $key_tags_pos[2]) {
174 // if there is an opening html tag, make sure the opening head tag
176 if (!is_null($key_tags_pos[0]) && $key_tags_pos[1] < $key_tags_pos[0]) {
179 $html_string = substr($html_string, $key_tags_pos[1],
180 ($key_tags_pos[2]-$key_tags_pos[1]));
182 $link_data = array();
183 $link_matches = array();
185 if (!preg_match_all($this->tagPattern('meta', false, 'maybe'),
186 $html_string, $link_matches)) {
190 foreach ($link_matches[0] as $link) {
191 $attr_matches = array();
192 preg_match_all($this->_attr_find, $link, $attr_matches);
193 $link_attrs = array();
194 foreach ($attr_matches[0] as $index => $full_match) {
195 $name = $attr_matches[1][$index];
196 $value = html_entity_decode(
197 $this->removeQuotes($attr_matches[2][$index]));
199 $link_attrs[strtolower($name)] = $value;
201 $link_data[] = $link_attrs;
208 * Looks for a META tag with an "http-equiv" attribute whose value
209 * is one of ("x-xrds-location", "x-yadis-location"), ignoring
210 * case. If such a META tag is found, its "content" attribute
213 * @param string $html_string An HTML document in string format
214 * @return mixed $content The "content" attribute value of the
215 * META tag, if found, or null if no such tag was found.
217 function getHTTPEquiv($html_string)
219 $meta_tags = $this->getMetaTags($html_string);
222 foreach ($meta_tags as $tag) {
223 if (array_key_exists('http-equiv', $tag) &&
224 (in_array(strtolower($tag['http-equiv']),
225 array('x-xrds-location', 'x-yadis-location'))) &&
226 array_key_exists('content', $tag)) {
227 return $tag['content'];