url = $args['url']; } $this->cacheDir = __DIR__ . '/../../resources/cache'; $this->cacheTimeout = 604800; $this->dataAccess = new DataAccess(); } /** * Set cache settings: * - dir: cache directory * - timeout: in seconds * * @param array $args */ public function cache($args = []) { if (isset($args['dir'])) { $this->cacheDir = $args['dir']; } if (!empty($args['timeout'])) { $this->cacheTimeout = $args['timeout']; } } public static function baseUrl($url, $path = false) { $return = ''; if (!$url = parse_url($url)) { return false; } // Scheme $scheme = isset($url['scheme']) ? strtolower($url['scheme']) : null; if ($scheme != 'http' && $scheme != 'https') { return false; } $return .= "{$scheme}://"; // Username and password if (isset($url['user'])) { $return .= $url['user']; if (isset($url['pass'])) { $return .= ":{$url['pass']}"; } $return .= '@'; } // Hostname if (!isset($url['host'])) { return false; } $return .= $url['host']; // Port if (isset($url['port'])) { $return .= ":{$url['port']}"; } // Path if ($path && isset($url['path'])) { $return .= $url['path']; } $return .= '/'; return $return; } public function info($url) { if (empty($url) || $url === false) { return false; } $max_loop = 5; // Discover real status by following redirects. $loop = true; while ($loop && $max_loop-- > 0) { $headers = $this->dataAccess->retrieveHeader($url); if (empty($headers) || !is_array($headers) || !array_key_exists(0, $headers)) { return false; } $exploded = explode(' ', $headers[0]); if (!isset($exploded[1])) { return false; } list(,$status) = $exploded; switch ($status) { case '301': case '302': if (isset($headers['location'])) { $url = $headers['location']; if (is_array($url)) { $url = end($url); } } else { return false; } break; default: $loop = false; break; } } return ['status' => $status, 'url' => $url]; } public function endRedirect($url) { $out = $this->info($url); return !empty($out['url']) ? $out['url'] : false; } /** * Find remote (or cached) favicon * * @param string $url to look for a favicon * @param int $type type of retrieval (FaviconDLType): * - HOTLINK_URL: returns remote URL * - DL_FILE_PATH: returns file path of the favicon downloaded locally * - RAW_IMAGE: returns the favicon image binary string * * @return string|bool favicon URL, false if nothing was found */ public function get($url = '', $type = FaviconDLType::HOTLINK_URL) { // URLs passed to this method take precedence. if (!empty($url)) { $this->url = $url; } $url = rtrim(self::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(self::baseUrl($this->url, false)), '/'); if ( ($favicon = $this->checkCache($url, self::$TYPE_CACHE_URL)) === false && ! $favicon = $this->getFavicon($url) ) { $url = $original; } } $this->saveCache($url, $favicon, self::$TYPE_CACHE_URL); switch ($type) { case FaviconDLType::DL_FILE_PATH: return $this->getImage($url, $favicon, false); case FaviconDLType::RAW_IMAGE: return $this->getImage($url, $favicon, true); case FaviconDLType::HOTLINK_URL: default: return empty($favicon) ? false : $favicon; } } private function getFavicon($url, $checkDefault = true) { $favicon = false; if (empty($url)) { return false; } // Try /favicon.ico first. if ($checkDefault) { $info = $this->info("{$url}/favicon.ico"); if ($info !== false && $info['status'] == '200') { $favicon = $info['url']; } } // See if it's specified in a link tag in domain url. if (!$favicon) { $favicon = trim($this->getInPage($url)); } // Case of protocol-relative URLs if (substr($favicon, 0, 2) === '//') { if (preg_match('%^(https?:)//%i', $url, $matches)) { $favicon = $matches[1] . $favicon; } else { $favicon = 'https:' . $favicon; } } // Make sure the favicon is an absolute URL. if ($favicon && filter_var($favicon, FILTER_VALIDATE_URL) === false) { // Make sure that favicons starting with "/" get concatenated with host instead of full URL if ($favicon[0] === '/') { $favicon = self::baseUrl($url) . ltrim($favicon, '/'); } else { $favicon = rtrim($url, '/') . '/' . ltrim($favicon, '/'); } } // Sometimes people lie, so check the status. // And sometimes, it's not even an image. Sneaky bastards! // If cacheDir isn't writable, that's not our problem if ( $favicon && is_writable($this->cacheDir) && extension_loaded('fileinfo') && !$this->checkImageMType($favicon) ) { $favicon = false; } return $favicon; } /** * Find remote favicon and return it as an image */ private function getImage($url, $faviconUrl = '', $image = false) { if (empty($faviconUrl)) { return false; } $favicon = $this->checkCache($url, self::$TYPE_CACHE_IMG); // OCA\Mail\Vendor\Favicon not found in the cache if ($favicon === false) { $favicon = $this->dataAccess->retrieveUrl($faviconUrl); // Definitely not found if (!$this->checkImageMTypeContent($favicon)) { return false; } else { $this->saveCache($url, $favicon, self::$TYPE_CACHE_IMG); } } if ($image) { return $favicon; } else { return self::$TYPE_CACHE_IMG . md5($url); } } private function getInPage($url) { $html = $this->dataAccess->retrieveUrl("{$url}/"); preg_match('!