diff --git a/src/Favicon/Favicon.php b/src/Favicon/Favicon.php index b485d94..f39adc7 100644 --- a/src/Favicon/Favicon.php +++ b/src/Favicon/Favicon.php @@ -4,6 +4,8 @@ class Favicon { + protected static $TYPE_CACHE_URL = 'url'; + protected static $TYPE_CACHE_IMG = 'img'; protected $url = ''; protected $cacheDir; protected $cacheTimeout; @@ -140,29 +142,29 @@ public function get($url = '', $type = FaviconDLType::HOTLINK_URL) } // Get the base URL without the path for clearer concatenations. - $original = rtrim($this->baseUrl($this->url, true), '/'); - $url = rtrim($this->endRedirect($this->baseUrl($this->url, false)), '/'); - - if(($favicon = $this->checkCache($url)) || ($favicon = $this->getFavicon($url))) { - $base = true; - } - elseif(($favicon = $this->checkCache($original)) || ($favicon = $this->getFavicon($original, false))) { - $base = false; - } - else { - return false; + $url = rtrim($this->baseUrl($this->url, true), '/'); + $original = $url; + if (($favicon = $this->checkCache($original, self::$TYPE_CACHE_URL)) === false + && ! $favicon = $this->getFavicon($original, false) + ) { + $url = rtrim($this->endRedirect($this->baseUrl($this->url, false)), '/'); + if (($favicon = $this->checkCache($url, self::$TYPE_CACHE_URL)) === false + && ! $favicon = $this->getFavicon($url) + ) { + $url = $original; + } } - $this->saveCache($base ? $url : $original, $favicon); + $this->saveCache($url, $favicon, self::$TYPE_CACHE_URL); switch ($type) { case FaviconDLType::DL_FILE_PATH: - return $this->getImage($favicon, false); + return $this->getImage($url, $favicon, false); case FaviconDLType::RAW_IMAGE: - return $this->getImage($favicon, true); + return $this->getImage($url, $favicon, true); case FaviconDLType::HOTLINK_URL: default: - return $favicon; + return empty($favicon) ? false : $favicon; } } @@ -204,9 +206,13 @@ private function getFavicon($url, $checkDefault = true) { /** * Find remote favicon and return it as an image */ - private function getImage($faviconUrl = '', $image = false) + private function getImage($url, $faviconUrl = '', $image = false) { - $favicon = $this->checkCache($faviconUrl); + if (empty($faviconUrl)) { + return false; + } + + $favicon = $this->checkCache($url, self::$TYPE_CACHE_IMG); // Favicon not found in the cache if( $favicon === false ) { $favicon = $this->dataAccess->retrieveUrl($faviconUrl); @@ -214,7 +220,7 @@ private function getImage($faviconUrl = '', $image = false) if (!$this->checkImageMTypeContent($favicon)) { return false; } else { - $this->saveCache($faviconUrl, $favicon); + $this->saveCache($url, $favicon, self::$TYPE_CACHE_IMG); } } @@ -222,7 +228,7 @@ private function getImage($faviconUrl = '', $image = false) return $favicon; } else - return md5($faviconUrl); + return self::$TYPE_CACHE_IMG . md5($url); } /** @@ -265,10 +271,12 @@ private function getInPage($url) { return false; } - private function checkCache($url) { + private function checkCache($url, $type) { if ($this->cacheTimeout) { - $cache = $this->cacheDir . '/' . md5($url); - if (file_exists($cache) && is_readable($cache) && (time() - filemtime($cache) < $this->cacheTimeout)) { + $cache = $this->cacheDir . '/'. $type . md5($url); + if (file_exists($cache) && is_readable($cache) + && ($this->cacheTimeout === -1 || time() - filemtime($cache) < $this->cacheTimeout) + ) { return $this->dataAccess->readCache($cache); } } @@ -279,12 +287,15 @@ private function checkCache($url) { * Will save data in cacheDir if the directory writable and any previous cache is expired (cacheTimeout) * @param $url * @param $data + * @param $type * @return string cache file path */ - private function saveCache($url, $data) { + private function saveCache($url, $data, $type) { // Save cache if necessary - $cache = $this->cacheDir . '/' . md5($url); - if ($this->cacheTimeout && !file_exists($cache) || (is_writable($cache) && time() - filemtime($cache) > $this->cacheTimeout)) { + $cache = $this->cacheDir . '/'. $type . md5($url); + if ($this->cacheTimeout && !file_exists($cache) + || (is_writable($cache) && $this->cacheTimeout !== -1 && time() - filemtime($cache) > $this->cacheTimeout) + ) { $this->dataAccess->saveCache($cache, $data); } return $cache; diff --git a/tests/Favicon/FaviconTest.php b/tests/Favicon/FaviconTest.php index 9aea9c2..fcad6d0 100644 --- a/tests/Favicon/FaviconTest.php +++ b/tests/Favicon/FaviconTest.php @@ -197,7 +197,7 @@ public function testInfoRedirect() { * @covers Favicon::get * @uses Favicon */ - public function testGetExistingRootFavicon() { + public function testGetExistingFavicon() { $url = 'http://domain.tld/'; $path = 'sub/'; @@ -214,7 +214,7 @@ public function testGetExistingRootFavicon() { // Get from URL MOCK $dataAccess->expects($this->any())->method('retrieveUrl')->will($this->returnCallback(array($this, 'contentExistingFav'))); - $this->assertEquals(self::slash($url) . self::TEST_LOGO_NAME, $fav->get()); + $this->assertEquals(self::slash($url . $path) . self::TEST_LOGO_NAME, $fav->get()); } /** @@ -374,6 +374,8 @@ public function testGetValidFavNoCacheSetup() { $dataAccess->expects($this->any())->method('retrieveUrl')->will($this->returnValue(file_get_contents($this->RESOURCE_FAV_ICO))); $this->assertEquals(self::slash($url) . self::DEFAULT_FAV_CHECK, $fav->get()); + directory_clear(__DIR__ .'/../../resources/cache'); + touch(__DIR__ .'/../../resources/cache/.gitkeep'); } public function testGetDownloadedFavPath() @@ -391,7 +393,7 @@ public function testGetDownloadedFavPath() $dataAccess->expects($this->any())->method('retrieveHeader')->will($this->returnValue(array(0 => 'HTTP/1.1 200 OK'))); $dataAccess->expects($this->any())->method('retrieveUrl')->will($this->returnValue(file_get_contents($this->RESOURCE_FAV_ICO))); - $expected = md5('http://domain.tld/'. self::DEFAULT_FAV_CHECK); + $expected = 'img'. md5('http://domain.tld'); $this->assertEquals($expected, $fav->get('', FaviconDLType::DL_FILE_PATH)); }