00001 <?php
00002
00023 class Auth_Yadis_ParseHTML {
00024
00028 var $_re_flags = "si";
00029
00033 var $_removed_re =
00034 "<!--.*?-->|<!\[CDATA\[.*?\]\]>|<script\b(?!:)[^>]*>.*?<\/script>";
00035
00039 var $_tag_expr = "<%s%s(?:\s.*?)?%s>";
00040
00044 var $_attr_find = '\b([-\w]+)=(".*?"|\'.*?\'|.+?)[\/\s>]';
00045
00046 function Auth_Yadis_ParseHTML()
00047 {
00048 $this->_attr_find = sprintf("/%s/%s",
00049 $this->_attr_find,
00050 $this->_re_flags);
00051
00052 $this->_removed_re = sprintf("/%s/%s",
00053 $this->_removed_re,
00054 $this->_re_flags);
00055
00056 $this->_entity_replacements = array(
00057 'amp' => '&',
00058 'lt' => '<',
00059 'gt' => '>',
00060 'quot' => '"'
00061 );
00062
00063 $this->_ent_replace =
00064 sprintf("&(%s);", implode("|",
00065 $this->_entity_replacements));
00066 }
00067
00077 function replaceEntities($str)
00078 {
00079 foreach ($this->_entity_replacements as $old => $new) {
00080 $str = preg_replace(sprintf("/&%s;/", $old), $new, $str);
00081 }
00082
00083
00084
00085 $str = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $str);
00086 $str = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $str);
00087
00088 return $str;
00089 }
00090
00100 function removeQuotes($str)
00101 {
00102 $matches = array();
00103 $double = '/^"(.*)"$/';
00104 $single = "/^\'(.*)\'$/";
00105
00106 if (preg_match($double, $str, $matches)) {
00107 return $matches[1];
00108 } else if (preg_match($single, $str, $matches)) {
00109 return $matches[1];
00110 } else {
00111 return $str;
00112 }
00113 }
00114
00126 function tagPattern($tag_names, $close, $self_close)
00127 {
00128 if (is_array($tag_names)) {
00129 $tag_names = '(?:'.implode('|',$tag_names).')';
00130 }
00131 if ($close) {
00132 $close = '\/' . (($close == 1)? '' : '?');
00133 } else {
00134 $close = '';
00135 }
00136 if ($self_close) {
00137 $self_close = '(?:\/\s*)' . (($self_close == 1)? '' : '?');
00138 } else {
00139 $self_close = '';
00140 }
00141 $expr = sprintf($this->_tag_expr, $close, $tag_names, $self_close);
00142
00143 return sprintf("/%s/%s", $expr, $this->_re_flags);
00144 }
00145
00157 function getMetaTags($html_string)
00158 {
00159 $html_string = preg_replace($this->_removed_re,
00160 "",
00161 $html_string);
00162
00163 $key_tags = array($this->tagPattern('html', false, false),
00164 $this->tagPattern('head', false, false),
00165 $this->tagPattern('head', true, false),
00166 $this->tagPattern('html', true, false),
00167 $this->tagPattern(array(
00168 'body', 'frameset', 'frame', 'p', 'div',
00169 'table','span','a'), 'maybe', 'maybe'));
00170 $key_tags_pos = array();
00171 foreach ($key_tags as $pat) {
00172 $matches = array();
00173 preg_match($pat, $html_string, $matches, PREG_OFFSET_CAPTURE);
00174 if($matches) {
00175 $key_tags_pos[] = $matches[0][1];
00176 } else {
00177 $key_tags_pos[] = null;
00178 }
00179 }
00180
00181 if (is_null($key_tags_pos[1])) {
00182 return array();
00183 }
00184
00185 if (is_null($key_tags_pos[2])) {
00186 $key_tags_pos[2] = strlen($html_string);
00187 }
00188 foreach (array($key_tags_pos[3], $key_tags_pos[4]) as $pos) {
00189 if (!is_null($pos) && $pos < $key_tags_pos[2]) {
00190 $key_tags_pos[2] = $pos;
00191 }
00192 }
00193
00194 if ($key_tags_pos[1] > $key_tags_pos[2]) {
00195 return array();
00196 }
00197
00198
00199 if (!is_null($key_tags_pos[0]) && $key_tags_pos[1] < $key_tags_pos[0]) {
00200 return array();
00201 }
00202 $html_string = substr($html_string, $key_tags_pos[1],
00203 ($key_tags_pos[2]-$key_tags_pos[1]));
00204
00205 $link_data = array();
00206 $link_matches = array();
00207
00208 if (!preg_match_all($this->tagPattern('meta', false, 'maybe'),
00209 $html_string, $link_matches)) {
00210 return array();
00211 }
00212
00213 foreach ($link_matches[0] as $link) {
00214 $attr_matches = array();
00215 preg_match_all($this->_attr_find, $link, $attr_matches);
00216 $link_attrs = array();
00217 foreach ($attr_matches[0] as $index => $full_match) {
00218 $name = $attr_matches[1][$index];
00219 $value = $this->replaceEntities(
00220 $this->removeQuotes($attr_matches[2][$index]));
00221
00222 $link_attrs[strtolower($name)] = $value;
00223 }
00224 $link_data[] = $link_attrs;
00225 }
00226
00227 return $link_data;
00228 }
00229
00240 function getHTTPEquiv($html_string)
00241 {
00242 $meta_tags = $this->getMetaTags($html_string);
00243
00244 if ($meta_tags) {
00245 foreach ($meta_tags as $tag) {
00246 if (array_key_exists('http-equiv', $tag) &&
00247 (in_array(strtolower($tag['http-equiv']),
00248 array('x-xrds-location', 'x-yadis-location'))) &&
00249 array_key_exists('content', $tag)) {
00250 return $tag['content'];
00251 }
00252 }
00253 }
00254
00255 return null;
00256 }
00257 }
00258
00259 ?>