From 331b832fc0f48015ee49743352f654fcceea781b Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Wed, 8 Oct 2014 15:01:39 +0200 Subject: cleanup favicon fetcher --- utility/faviconfetcher.php | 34 +++++++++++++++++++++++++++++++--- utility/novalidurlexception.php | 19 +++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 utility/novalidurlexception.php diff --git a/utility/faviconfetcher.php b/utility/faviconfetcher.php index cf7eed32e..8a285a47b 100644 --- a/utility/faviconfetcher.php +++ b/utility/faviconfetcher.php @@ -48,10 +48,26 @@ class FaviconFetcher { // check the url for a valid image if($faviconUrl && $this->isImage($faviconUrl)) { return $faviconUrl; - } elseif($url) { + } elseif ($url) { // try /favicon.ico as fallback $parts = parse_url($url); - $faviconUrl = $parts['scheme'] . "://" . $parts['host'] . (array_key_exists("port", $parts) ? $parts['port'] : '') . "/favicon.ico"; + + // malformed url + if ($parts === false || !array_key_exists('host', $parts)) { + return null; + } + + $port = ''; + if (array_key_exists("port", $parts)) { + $port = $parts['port']; + } + + $scheme = 'http'; + if (array_key_exists("scheme", $parts)) { + $scheme = $parts['scheme']; + } + + $faviconUrl = $scheme . "://" . $parts['host'] . $port . "/favicon.ico"; if($this->isImage($faviconUrl)) { return $faviconUrl; @@ -61,6 +77,18 @@ class FaviconFetcher { return null; } + /** + * Get the attribute if its a DOMElement, otherwise return null + */ + private function getAttribute($item, $name) { + // used to fix scrutinizer errors + if ($item instanceof \DOMElement) { + return $item->getAttribute($name); + } else { + return null; + } + } + /** * Tries to get a favicon from a page @@ -88,7 +116,7 @@ class FaviconFetcher { if ($elements->length > 0) { /** @noinspection PhpUndefinedMethodInspection */ - $iconPath = $elements->item(0)->getAttribute('href'); + $iconPath = $this->getAttribute($elements->item(0), 'href'); $absPath = \SimplePie_Misc::absolutize_url($iconPath, $url); return $absPath; } diff --git a/utility/novalidurlexception.php b/utility/novalidurlexception.php new file mode 100644 index 000000000..04d6f2bfc --- /dev/null +++ b/utility/novalidurlexception.php @@ -0,0 +1,19 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Utility; + +/** + * Thrown when no valid url was found by faviconfetcher + */ +class NoValidUrlException extends \Exception { } -- cgit v1.2.3