summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBernhard Posselt <dev@bernhard-posselt.com>2014-10-08 15:01:39 +0200
committerBernhard Posselt <dev@bernhard-posselt.com>2014-10-08 15:01:39 +0200
commit331b832fc0f48015ee49743352f654fcceea781b (patch)
treeee5ed1dc67a5ec974f581fda7d9cb7c6ee7d928c
parentc96445eadb1dbf93b21c7894332c4b4b98cab7e5 (diff)
cleanup favicon fetcher
-rw-r--r--utility/faviconfetcher.php34
-rw-r--r--utility/novalidurlexception.php19
2 files changed, 50 insertions, 3 deletions
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 @@
+<?php
+/**
+ * ownCloud - News
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later. See the COPYING file.
+ *
+ * @author Alessandro Cosentino <cosenal@gmail.com>
+ * @author Bernhard Posselt <dev@bernhard-posselt.com>
+ * @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 { }