Skip to content

Commit 79dfe59

Browse files
committed
Use Google API to fetch favicons
Now supporting captchas
1 parent 5fcd0f6 commit 79dfe59

File tree

1 file changed

+9
-164
lines changed

1 file changed

+9
-164
lines changed
Lines changed: 9 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -1,178 +1,23 @@
11
<?php
22
use App\Models\Link;
33
4-
if (!function_exists('getFaviconURL')) {
5-
function getFaviconURL($url)
6-
{
7-
$ch = curl_init($url);
8-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
9-
curl_setopt($ch, CURLOPT_HEADER, true);
10-
curl_setopt($ch, CURLOPT_NOBODY, true);
11-
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36');
12-
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
13-
$response = curl_exec($ch);
14-
15-
// Check if cURL request was successful
16-
if ($response === false) {
17-
return null;
18-
}
19-
20-
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
21-
curl_close($ch);
22-
23-
// Check if the URL is redirected
24-
if ($httpCode == 301 || $httpCode == 302) {
25-
$redirectUrl = getRedirectUrlFromHeaders($response);
26-
if ($redirectUrl) {
27-
return getFaviconURL($redirectUrl); // Recursively call getFavicon with the redirected URL
28-
}
29-
}
30-
31-
// Try extracting favicon using DOMDocument
32-
try {
33-
$dom = new DOMDocument();
34-
$dom->strictErrorChecking = false;
35-
@$dom->loadHTMLFile($url);
36-
if ($dom) {
37-
$faviconURL = extractFaviconUrlFromDOM($dom);
38-
if ($faviconURL) {
39-
return getAbsoluteUrl($url, $faviconURL);
40-
}
41-
}
42-
} catch (Exception $e) {
43-
// Silently fail and continue to the next method
44-
}
45-
46-
// Check directly for favicon.ico or favicon.png
47-
$parse = parse_url($url);
48-
$faviconURL = getAbsoluteUrl($url, "/favicon.ico");
49-
if (checkURLExists($faviconURL)) {
50-
return $faviconURL;
51-
}
52-
53-
$faviconURL = getAbsoluteUrl($url, "/favicon.png");
54-
if (checkURLExists($faviconURL)) {
55-
return $faviconURL;
56-
}
57-
58-
// Fallback to regex extraction
59-
$faviconURL = extractFaviconUrlWithRegex($response);
60-
if ($faviconURL) {
61-
$faviconURL = getAbsoluteUrl($url, $faviconURL);
62-
}
63-
return $faviconURL;
64-
}
65-
}
66-
67-
if (!function_exists('getRedirectUrlFromHeaders')) {
68-
function getRedirectUrlFromHeaders($headers)
69-
{
70-
if (preg_match('/^Location:\s+(.*)$/mi', $headers, $matches)) {
71-
return trim($matches[1]);
72-
}
73-
return null;
74-
}
75-
}
76-
77-
if (!function_exists('extractFaviconUrlFromDOM')) {
78-
function extractFaviconUrlFromDOM($dom)
79-
{
80-
$xpath = new DOMXPath($dom);
81-
82-
// Check for the historical rel="shortcut icon"
83-
$shortcutIcon = $xpath->query('//link[@rel="shortcut icon"]');
84-
if ($shortcutIcon->length > 0) {
85-
$path = $shortcutIcon->item(0)->getAttribute('href');
86-
return $path;
87-
}
88-
89-
// Check for the HTML5 rel="icon"
90-
$icon = $xpath->query('//link[@rel="icon"]');
91-
if ($icon->length > 0) {
92-
$path = $icon->item(0)->getAttribute('href');
93-
return $path;
94-
}
95-
96-
return null;
97-
}
98-
}
99-
100-
if (!function_exists('checkURLExists')) {
101-
function checkURLExists($url)
102-
{
103-
$headers = @get_headers($url);
104-
return ($headers && strpos($headers[0], '200') !== false);
105-
}
106-
}
107-
108-
if (!function_exists('extractFaviconUrlWithRegex')) {
109-
function extractFaviconUrlWithRegex($html)
110-
{
111-
// Check for the historical rel="shortcut icon"
112-
if (preg_match('/<link[^>]+rel=["\']shortcut icon["\'][^>]+href=["\']([^"\']+)["\']/', $html, $matches)) {
113-
$faviconURL = $matches[1];
114-
return $faviconURL;
115-
}
116-
117-
// Check for the HTML5 rel="icon"
118-
if (preg_match('/<link[^>]+rel=["\']icon["\'][^>]+href=["\']([^"\']+)["\']/', $html, $matches)) {
119-
$faviconURL = $matches[1];
120-
return $faviconURL;
121-
}
122-
123-
return null;
124-
}
125-
}
126-
127-
if (!function_exists('getAbsoluteUrl')) {
128-
function getAbsoluteUrl($baseUrl, $relativeUrl)
129-
{
130-
$parsedUrl = parse_url($baseUrl);
131-
$scheme = isset($parsedUrl['scheme']) ? $parsedUrl['scheme'] : 'http';
132-
$host = isset($parsedUrl['host']) ? $parsedUrl['host'] : '';
133-
$path = isset($parsedUrl['path']) ? $parsedUrl['path'] : '';
134-
$basePath = "$scheme://$host$path";
135-
136-
if (strpos($relativeUrl, 'http') === 0) {
137-
return $relativeUrl; // Already an absolute URL
138-
} elseif (strpos($relativeUrl, '/') === 0) {
139-
return "$scheme://$host$relativeUrl"; // Root-relative URL
140-
} else {
141-
return "$basePath/$relativeUrl"; // Path-relative URL
142-
}
143-
}
144-
}
145-
1464
if (!function_exists('getFavIcon')) {
1475
function getFavIcon($id)
1486
{
1497
try {
1508
$link = Link::find($id);
151-
$page = $link->link;
9+
$url = $link->link;
15210
153-
$url = getFaviconURL($page);
11+
// Use Google's Favicon API
12+
$faviconUrl = 'http://www.google.com/s2/favicons?sz=256&domain=' . $url;
15413
155-
$fileExtension = pathinfo($url, PATHINFO_EXTENSION);
156-
$filename = $id . '.' . $fileExtension;
14+
// Get the favicon and save it to the desired location
15+
$favicon = file_get_contents($faviconUrl);
16+
$filename = $id . '.png';
15717
$filepath = base_path('assets/favicon/icons') . '/' . $filename;
18+
file_put_contents($filepath, $favicon);
15819
159-
if (!file_exists($filepath)) {
160-
if (function_exists('curl_version')) {
161-
$curlHandle = curl_init($url);
162-
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
163-
curl_setopt($curlHandle, CURLOPT_TIMEOUT, 3);
164-
$faviconData = curl_exec($curlHandle);
165-
curl_close($curlHandle);
166-
167-
if ($faviconData !== false) {
168-
file_put_contents($filepath, $faviconData);
169-
}
170-
} else {
171-
file_put_contents($filepath, file_get_contents($url));
172-
}
173-
}
174-
175-
return url('assets/favicon/icons/' . $id . '.' . $fileExtension);
20+
return url('assets/favicon/icons/' . $filename);
17621
} catch (Exception $e) {
17722
// Handle the exception by copying the default SVG favicon
17823
$defaultIcon = base_path('assets/linkstack/icons/website.svg');
@@ -183,4 +28,4 @@ function getFavIcon($id)
18328
return url('assets/favicon/icons/' . $filename);
18429
}
18530
}
186-
}
31+
}

0 commit comments

Comments
 (0)