Favicon.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. namespace Favicon;
  3. class Favicon
  4. {
  5. protected static $TYPE_CACHE_URL = 'url';
  6. protected static $TYPE_CACHE_IMG = 'img';
  7. protected $url = '';
  8. protected $cacheDir;
  9. protected $cacheTimeout;
  10. protected $dataAccess;
  11. public function __construct($args = array())
  12. {
  13. if (isset($args['url'])) {
  14. $this->url = $args['url'];
  15. }
  16. $this->cacheDir = __DIR__ . '/../../resources/cache';
  17. $this->cacheTimeout = 604800;
  18. $this->dataAccess = new DataAccess();
  19. }
  20. /**
  21. * Set cache settings:
  22. * - dir: cache directory
  23. * - timeout: in seconds
  24. *
  25. * @param array $args
  26. */
  27. public function cache($args = array()) {
  28. if (isset($args['dir'])) {
  29. $this->cacheDir = $args['dir'];
  30. }
  31. if (!empty($args['timeout'])) {
  32. $this->cacheTimeout = $args['timeout'];
  33. }
  34. }
  35. public static function baseUrl($url, $path = false)
  36. {
  37. $return = '';
  38. if (!$url = parse_url($url)) {
  39. return FALSE;
  40. }
  41. // Scheme
  42. $scheme = isset($url['scheme']) ? strtolower($url['scheme']) : null;
  43. if ($scheme != 'http' && $scheme != 'https') {
  44. return FALSE;
  45. }
  46. $return .= "{$scheme}://";
  47. // Username and password
  48. if (isset($url['user'])) {
  49. $return .= $url['user'];
  50. if (isset($url['pass'])) {
  51. $return .= ":{$url['pass']}";
  52. }
  53. $return .= '@';
  54. }
  55. // Hostname
  56. if( !isset($url['host']) ) {
  57. return FALSE;
  58. }
  59. $return .= $url['host'];
  60. // Port
  61. if (isset($url['port'])) {
  62. $return .= ":{$url['port']}";
  63. }
  64. // Path
  65. if( $path && isset($url['path']) ) {
  66. $return .= $url['path'];
  67. }
  68. $return .= '/';
  69. return $return;
  70. }
  71. public function info($url)
  72. {
  73. if(empty($url) || $url === false) {
  74. return false;
  75. }
  76. $max_loop = 5;
  77. // Discover real status by following redirects.
  78. $loop = TRUE;
  79. while ($loop && $max_loop-- > 0) {
  80. $headers = $this->dataAccess->retrieveHeader($url);
  81. if (empty($headers)) {
  82. return false;
  83. }
  84. $exploded = explode(' ', $headers[0]);
  85. if( !isset($exploded[1]) ) {
  86. return false;
  87. }
  88. list(,$status) = $exploded;
  89. switch ($status) {
  90. case '301':
  91. case '302':
  92. $url = isset($headers['location']) ? $headers['location'] : '';
  93. break;
  94. default:
  95. $loop = FALSE;
  96. break;
  97. }
  98. }
  99. return array('status' => $status, 'url' => $url);
  100. }
  101. public function endRedirect($url) {
  102. $out = $this->info($url);
  103. return !empty($out['url']) ? $out['url'] : false;
  104. }
  105. /**
  106. * Find remote (or cached) favicon
  107. *
  108. * @param string $url to look for a favicon
  109. * @param int $type type of retrieval (FaviconDLType):
  110. * - HOTLINK_URL: returns remote URL
  111. * - DL_FILE_PATH: returns file path of the favicon downloaded locally
  112. * - RAW_IMAGE: returns the favicon image binary string
  113. *
  114. * @return string|bool favicon URL, false if nothing was found
  115. */
  116. public function get($url = '', $type = FaviconDLType::HOTLINK_URL)
  117. {
  118. // URLs passed to this method take precedence.
  119. if (!empty($url)) {
  120. $this->url = $url;
  121. }
  122. // Get the base URL without the path for clearer concatenations.
  123. $url = rtrim($this->baseUrl($this->url, true), '/');
  124. $original = $url;
  125. if (($favicon = $this->checkCache($original, self::$TYPE_CACHE_URL)) === false
  126. && ! $favicon = $this->getFavicon($original, false)
  127. ) {
  128. $url = rtrim($this->endRedirect($this->baseUrl($this->url, false)), '/');
  129. if (($favicon = $this->checkCache($url, self::$TYPE_CACHE_URL)) === false
  130. && ! $favicon = $this->getFavicon($url)
  131. ) {
  132. $url = $original;
  133. }
  134. }
  135. $this->saveCache($url, $favicon, self::$TYPE_CACHE_URL);
  136. switch ($type) {
  137. case FaviconDLType::DL_FILE_PATH:
  138. return $this->getImage($url, $favicon, false);
  139. case FaviconDLType::RAW_IMAGE:
  140. return $this->getImage($url, $favicon, true);
  141. case FaviconDLType::HOTLINK_URL:
  142. default:
  143. return empty($favicon) ? false : $favicon;
  144. }
  145. }
  146. private function getFavicon($url, $checkDefault = true) {
  147. $favicon = false;
  148. if(empty($url)) {
  149. return false;
  150. }
  151. // Try /favicon.ico first.
  152. if( $checkDefault ) {
  153. $info = $this->info("{$url}/favicon.ico");
  154. if ($info['status'] == '200') {
  155. $favicon = $info['url'];
  156. }
  157. }
  158. // See if it's specified in a link tag in domain url.
  159. if (!$favicon) {
  160. $favicon = trim($this->getInPage($url));
  161. }
  162. if (substr($favicon, 0, 2) === '//') {
  163. $favicon = 'https:' . $favicon;
  164. }
  165. // Make sure the favicon is an absolute URL.
  166. if( $favicon && filter_var($favicon, FILTER_VALIDATE_URL) === false ) {
  167. $favicon = $url . '/' . $favicon;
  168. }
  169. // Sometimes people lie, so check the status.
  170. // And sometimes, it's not even an image. Sneaky bastards!
  171. // If cacheDir isn't writable, that's not our problem
  172. if ($favicon && is_writable($this->cacheDir) && extension_loaded('fileinfo') && !$this->checkImageMType($favicon)) {
  173. $favicon = false;
  174. }
  175. return $favicon;
  176. }
  177. /**
  178. * Find remote favicon and return it as an image
  179. */
  180. private function getImage($url, $faviconUrl = '', $image = false)
  181. {
  182. if (empty($faviconUrl)) {
  183. return false;
  184. }
  185. $favicon = $this->checkCache($url, self::$TYPE_CACHE_IMG);
  186. // Favicon not found in the cache
  187. if( $favicon === false ) {
  188. $favicon = $this->dataAccess->retrieveUrl($faviconUrl);
  189. // Definitely not found
  190. if (!$this->checkImageMTypeContent($favicon)) {
  191. return false;
  192. } else {
  193. $this->saveCache($url, $favicon, self::$TYPE_CACHE_IMG);
  194. }
  195. }
  196. if( $image ) {
  197. return $favicon;
  198. }
  199. else
  200. return self::$TYPE_CACHE_IMG . md5($url);
  201. }
  202. /**
  203. * Display data as a PNG Favicon, then exit
  204. * @param $data
  205. */
  206. private function displayFavicon($data) {
  207. header('Content-Type: image/png');
  208. header('Cache-Control: private, max-age=10800, pre-check=10800');
  209. header('Pragma: private');
  210. header('Expires: ' . date(DATE_RFC822,strtotime('7 day')));
  211. echo $data;
  212. exit;
  213. }
  214. private function getInPage($url) {
  215. $html = $this->dataAccess->retrieveUrl("{$url}/");
  216. preg_match('!<head.*?>.*</head>!ims', $html, $match);
  217. if(empty($match) || count($match) == 0) {
  218. return false;
  219. }
  220. $head = $match[0];
  221. $dom = new \DOMDocument();
  222. // Use error suppression, because the HTML might be too malformed.
  223. if (@$dom->loadHTML($head)) {
  224. $links = $dom->getElementsByTagName('link');
  225. foreach ($links as $link) {
  226. if ($link->hasAttribute('rel') && strtolower($link->getAttribute('rel')) == 'shortcut icon') {
  227. return $link->getAttribute('href');
  228. }
  229. }
  230. foreach ($links as $link) {
  231. if ($link->hasAttribute('rel') && strtolower($link->getAttribute('rel')) == 'icon') {
  232. return $link->getAttribute('href');
  233. }
  234. }
  235. foreach ($links as $link) {
  236. if ($link->hasAttribute('href') && strpos($link->getAttribute('href'), 'favicon') !== FALSE) {
  237. return $link->getAttribute('href');
  238. }
  239. }
  240. }
  241. return false;
  242. }
  243. private function checkCache($url, $type) {
  244. if ($this->cacheTimeout) {
  245. $cache = $this->cacheDir . '/'. $type . md5($url);
  246. if (file_exists($cache) && is_readable($cache)
  247. && ($this->cacheTimeout === -1 || time() - filemtime($cache) < $this->cacheTimeout)
  248. ) {
  249. return $this->dataAccess->readCache($cache);
  250. }
  251. }
  252. return false;
  253. }
  254. /**
  255. * Will save data in cacheDir if the directory writable and any previous cache is expired (cacheTimeout)
  256. * @param $url
  257. * @param $data
  258. * @param $type
  259. * @return string cache file path
  260. */
  261. private function saveCache($url, $data, $type) {
  262. // Save cache if necessary
  263. $cache = $this->cacheDir . '/'. $type . md5($url);
  264. if ($this->cacheTimeout && !file_exists($cache)
  265. || (is_writable($cache) && $this->cacheTimeout !== -1 && time() - filemtime($cache) > $this->cacheTimeout)
  266. ) {
  267. $this->dataAccess->saveCache($cache, $data);
  268. }
  269. return $cache;
  270. }
  271. private function checkImageMType($url) {
  272. $fileContent = $this->dataAccess->retrieveUrl($url);
  273. return $this->checkImageMTypeContent($fileContent);
  274. }
  275. private function checkImageMTypeContent($content) {
  276. if(empty($content)) return false;
  277. $isImage = true;
  278. try {
  279. $fInfo = finfo_open(FILEINFO_MIME_TYPE);
  280. $isImage = strpos(finfo_buffer($fInfo, $content), 'image') !== false;
  281. finfo_close($fInfo);
  282. } catch (\Exception $e) {
  283. error_log('Favicon checkImageMTypeContent error: ' . $e->getMessage());
  284. }
  285. return $isImage;
  286. }
  287. /**
  288. * @return mixed
  289. */
  290. public function getCacheDir()
  291. {
  292. return $this->cacheDir;
  293. }
  294. /**
  295. * @param mixed $cacheDir
  296. */
  297. public function setCacheDir($cacheDir)
  298. {
  299. $this->cacheDir = $cacheDir;
  300. }
  301. /**
  302. * @return mixed
  303. */
  304. public function getCacheTimeout()
  305. {
  306. return $this->cacheTimeout;
  307. }
  308. /**
  309. * @param mixed $cacheTimeout
  310. */
  311. public function setCacheTimeout($cacheTimeout)
  312. {
  313. $this->cacheTimeout = $cacheTimeout;
  314. }
  315. /**
  316. * @return string
  317. */
  318. public function getUrl()
  319. {
  320. return $this->url;
  321. }
  322. /**
  323. * @param string $url
  324. */
  325. public function setUrl($url)
  326. {
  327. $this->url = $url;
  328. }
  329. /**
  330. * @param DataAccess|\PHPUnit_Framework_MockObject_MockObject $dataAccess
  331. */
  332. public function setDataAccess($dataAccess)
  333. {
  334. $this->dataAccess = $dataAccess;
  335. }
  336. }