diff options
-rw-r--r-- | Auth/OpenID/Parse.php | 61 | ||||
-rw-r--r-- | Tests/Auth/OpenID/Parse.php | 5 |
2 files changed, 57 insertions, 9 deletions
diff --git a/Auth/OpenID/Parse.php b/Auth/OpenID/Parse.php index 7d48515..d689f9e 100644 --- a/Auth/OpenID/Parse.php +++ b/Auth/OpenID/Parse.php @@ -105,6 +105,9 @@ class Auth_OpenID_Parse { var $_attr_find = '\b(\w+)=("[^"]*"|\'[^\']*\'|[^\'"\s\/<>]+)'; + var $_open_tag_expr = "<%s\b"; + var $_close_tag_expr = "<((\/%s\b)|(%s[^>\/]*\/))>"; + function Auth_OpenID_Parse() { $this->_link_find = sprintf("/<link\b(?!:)([^>]*)(?!<)>/%s", @@ -136,6 +139,8 @@ class Auth_OpenID_Parse { */ function tagMatcher($tag_name, $close_tags = null) { + $expr = $this->_tag_expr; + if ($close_tags) { $options = implode("|", array_merge(array($tag_name), $close_tags)); $closer = sprintf("(?:%s)", $options); @@ -143,18 +148,49 @@ class Auth_OpenID_Parse { $closer = $tag_name; } - $expr = sprintf($this->_tag_expr, $tag_name, $closer); + $expr = sprintf($expr, $tag_name, $closer); return sprintf("/%s/%s", $expr, $this->_re_flags); } - function htmlFind() + function openTag($tag_name) { - return $this->tagMatcher('html'); + $expr = sprintf($this->_open_tag_expr, $tag_name); + return sprintf("/%s/%s", $expr, $this->_re_flags); + } + + function closeTag($tag_name) + { + $expr = sprintf($this->_close_tag_expr, $tag_name, $tag_name); + return sprintf("/%s/%s", $expr, $this->_re_flags); + } + + function htmlBegin($s) + { + $matches = array(); + $result = preg_match($this->openTag('html'), $s, + $matches, PREG_OFFSET_CAPTURE); + if ($result === false || !$matches) { + return false; + } + // Return the offset of the first match. + return $matches[0][1]; + } + + function htmlEnd($s) + { + $matches = array(); + $result = preg_match($this->closeTag('html'), $s, + $matches, PREG_OFFSET_CAPTURE); + if ($result === false || !$matches) { + return false; + } + // Return the offset of the first match. + return $matches[count($matches) - 1][1]; } function headFind() { - return $this->tagMatcher('head', array('body')); + return $this->tagMatcher('head', array('body', 'html')); } function replaceEntities($str) @@ -194,17 +230,24 @@ class Auth_OpenID_Parse { "", $html); - // Try to find the <HTML> tag. - $html_re = $this->htmlFind(); - $html_matches = array(); - if (!preg_match($html_re, $stripped, $html_matches)) { + $html_begin = $this->htmlBegin($stripped); + $html_end = $this->htmlEnd($stripped); + + if ($html_begin === false) { return array(); } + if ($html_end === false) { + $html_end = strlen($stripped); + } + + $stripped = substr($stripped, $html_begin, + $html_end - $html_begin); + // Try to find the <HEAD> tag. $head_re = $this->headFind(); $head_matches = array(); - if (!preg_match($head_re, $html_matches[0], $head_matches)) { + if (!preg_match($head_re, $stripped, $head_matches)) { return array(); } diff --git a/Tests/Auth/OpenID/Parse.php b/Tests/Auth/OpenID/Parse.php index 1748651..30c3c28 100644 --- a/Tests/Auth/OpenID/Parse.php +++ b/Tests/Auth/OpenID/Parse.php @@ -45,6 +45,11 @@ class Tests_Auth_OpenID_Link extends PHPUnit_TestCase { continue; } + if (count($parsed) <= $i) { + $i++; + continue; + } + $act_link = $parsed[$i]; $increment = true; |