diff options
author | John Kristensen <john@jerrykan.com> | 2013-04-05 00:24:52 +1100 |
---|---|---|
committer | John Kristensen <john@jerrykan.com> | 2013-04-05 00:24:52 +1100 |
commit | 42685490dfd7b9deaf54ed337aec92f251ef5adc (patch) | |
tree | 7a3a0bace6ea7140592694546bbfedd2b37a4dc2 | |
parent | 312796b1ca251567159368dddcaed1a3f0ddafc0 (diff) |
Improve feedfetcher discoverFavicon()
Using Xpath is more reliable than a regular expression for retrieving
the correct link href and we need to ensure that local references are
dealt with correctly.
-rw-r--r-- | utility/feedfetcher.php | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/utility/feedfetcher.php b/utility/feedfetcher.php index 284c40ba7..5d600c5c6 100644 --- a/utility/feedfetcher.php +++ b/utility/feedfetcher.php @@ -159,16 +159,23 @@ class FeedFetcher implements IFeedFetcher { $page = $this->api->getUrlContent($absoluteUrl); if ( FALSE !== $page ) { - // FIXME: dont use regex to get xml, use xpath! - preg_match ( '/<[^>]*link[^>]*(rel=["\']icon["\']|rel=["\']shortcut icon["\']) .*href=["\']([^>]*)["\'].*>/iU', $page, $match ); - if (1<sizeof($match)) { - // the specified uri might be an url, an absolute or a relative path - // we have to turn it into an url to be able to display it out of context - $favicon = htmlspecialchars_decode ( $match[2] ); - // test for an url - if (parse_url($favicon,PHP_URL_SCHEME)) { - if($this->checkFavicon($favicon)) - return $favicon; + $doc = @\DOMDocument::loadHTML($page); + + if ( $doc !== FALSE ) { + $xpath = new \DOMXpath($doc); + $elements = $xpath->query("//link[contains(@rel, 'icon')]"); + + if ( $elements->length > 0 ) { + if ( $favicon = $elements->item(0)->getAttribute('href') ) { + // the specified uri might be an url, an absolute or a relative path + // we have to turn it into a url to be able to display it out of context + if ( !parse_url($favicon, PHP_URL_SCHEME) ) { + $favicon = \SimplePie_Misc::absolutize_url($favicon, $absoluteUrl); + } + + if($this->checkFavicon($favicon)) + return $favicon; + } } } } |