Favicon.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 = $this->getInPage($url);
  161. }
  162. // Make sure the favicon is an absolute URL.
  163. if( $favicon && filter_var($favicon, FILTER_VALIDATE_URL) === false ) {
  164. $favicon = $url . '/' . $favicon;
  165. }
  166. // Sometimes people lie, so check the status.
  167. // And sometimes, it's not even an image. Sneaky bastards!
  168. // If cacheDir isn't writable, that's not our problem
  169. if ($favicon && is_writable($this->cacheDir) && extension_loaded('fileinfo') && !$this->checkImageMType($favicon)) {
  170. $favicon = false;
  171. }
  172. return $favicon;
  173. }
  174. /**
  175. * Find remote favicon and return it as an image
  176. */
  177. private function getImage($url, $faviconUrl = '', $image = false)
  178. {
  179. if (empty($faviconUrl)) {
  180. return false;
  181. }
  182. $favicon = $this->checkCache($url, self::$TYPE_CACHE_IMG);
  183. // Favicon not found in the cache
  184. if( $favicon === false ) {
  185. $favicon = $this->dataAccess->retrieveUrl($faviconUrl);
  186. // Definitely not found
  187. if (!$this->checkImageMTypeContent($favicon)) {
  188. return false;
  189. } else {
  190. $this->saveCache($url, $favicon, self::$TYPE_CACHE_IMG);
  191. }
  192. }
  193. if( $image ) {
  194. return $favicon;
  195. }
  196. else
  197. return self::$TYPE_CACHE_IMG . md5($url);
  198. }
  199. /**
  200. * Display data as a PNG Favicon, then exit
  201. * @param $data
  202. */
  203. private function displayFavicon($data) {
  204. header('Content-Type: image/png');
  205. header('Cache-Control: private, max-age=10800, pre-check=10800');
  206. header('Pragma: private');
  207. header('Expires: ' . date(DATE_RFC822,strtotime('7 day')));
  208. echo $data;
  209. exit;
  210. }
  211. private function getInPage($url) {
  212. $html = $this->dataAccess->retrieveUrl("{$url}/");
  213. preg_match('!<head.*?>.*</head>!ims', $html, $match);
  214. if(empty($match) || count($match) == 0) {
  215. return false;
  216. }
  217. $head = $match[0];
  218. $dom = new \DOMDocument();
  219. // Use error suppression, because the HTML might be too malformed.
  220. if (@$dom->loadHTML($head)) {
  221. $links = $dom->getElementsByTagName('link');
  222. foreach ($links as $link) {
  223. if ($link->hasAttribute('rel') && strtolower($link->getAttribute('rel')) == 'shortcut icon') {
  224. return $link->getAttribute('href');
  225. } elseif ($link->hasAttribute('rel') && strtolower($link->getAttribute('rel')) == 'icon') {
  226. return $link->getAttribute('href');
  227. } elseif ($link->hasAttribute('href') && strpos($link->getAttribute('href'), 'favicon') !== FALSE) {
  228. return $link->getAttribute('href');
  229. }
  230. }
  231. }
  232. return false;
  233. }
  234. private function checkCache($url, $type) {
  235. if ($this->cacheTimeout) {
  236. $cache = $this->cacheDir . '/'. $type . md5($url);
  237. if (file_exists($cache) && is_readable($cache)
  238. && ($this->cacheTimeout === -1 || time() - filemtime($cache) < $this->cacheTimeout)
  239. ) {
  240. return $this->dataAccess->readCache($cache);
  241. }
  242. }
  243. return false;
  244. }
  245. /**
  246. * Will save data in cacheDir if the directory writable and any previous cache is expired (cacheTimeout)
  247. * @param $url
  248. * @param $data
  249. * @param $type
  250. * @return string cache file path
  251. */
  252. private function saveCache($url, $data, $type) {
  253. // Save cache if necessary
  254. $cache = $this->cacheDir . '/'. $type . md5($url);
  255. if ($this->cacheTimeout && !file_exists($cache)
  256. || (is_writable($cache) && $this->cacheTimeout !== -1 && time() - filemtime($cache) > $this->cacheTimeout)
  257. ) {
  258. $this->dataAccess->saveCache($cache, $data);
  259. }
  260. return $cache;
  261. }
  262. private function checkImageMType($url) {
  263. $fileContent = $this->dataAccess->retrieveUrl($url);
  264. return $this->checkImageMTypeContent($fileContent);
  265. }
  266. private function checkImageMTypeContent($content) {
  267. if(empty($content)) return false;
  268. $isImage = true;
  269. try {
  270. $fInfo = finfo_open(FILEINFO_MIME_TYPE);
  271. $isImage = strpos(finfo_buffer($fInfo, $content), 'image') !== false;
  272. finfo_close($fInfo);
  273. } catch (Exception $e) {
  274. }
  275. return $isImage;
  276. }
  277. /**
  278. * @return mixed
  279. */
  280. public function getCacheDir()
  281. {
  282. return $this->cacheDir;
  283. }
  284. /**
  285. * @param mixed $cacheDir
  286. */
  287. public function setCacheDir($cacheDir)
  288. {
  289. $this->cacheDir = $cacheDir;
  290. }
  291. /**
  292. * @return mixed
  293. */
  294. public function getCacheTimeout()
  295. {
  296. return $this->cacheTimeout;
  297. }
  298. /**
  299. * @param mixed $cacheTimeout
  300. */
  301. public function setCacheTimeout($cacheTimeout)
  302. {
  303. $this->cacheTimeout = $cacheTimeout;
  304. }
  305. /**
  306. * @return string
  307. */
  308. public function getUrl()
  309. {
  310. return $this->url;
  311. }
  312. /**
  313. * @param string $url
  314. */
  315. public function setUrl($url)
  316. {
  317. $this->url = $url;
  318. }
  319. /**
  320. * @param DataAccess|\PHPUnit_Framework_MockObject_MockObject $dataAccess
  321. */
  322. public function setDataAccess($dataAccess)
  323. {
  324. $this->dataAccess = $dataAccess;
  325. }
  326. }