Skip to content

Commit e201b4c

Browse files
committed
Update favicon.blade.php
1 parent 3609d14 commit e201b4c

File tree

1 file changed

+148
-135
lines changed

1 file changed

+148
-135
lines changed

resources/views/components/favicon.blade.php

Lines changed: 148 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,173 +1,186 @@
11
<?php
22
use App\Models\Link;
33
4-
function getFaviconURL($url)
5-
{
6-
$ch = curl_init($url);
7-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
8-
curl_setopt($ch, CURLOPT_HEADER, true);
9-
curl_setopt($ch, CURLOPT_NOBODY, true);
10-
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');
11-
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
12-
$response = curl_exec($ch);
13-
14-
// Check if cURL request was successful
15-
if ($response === false) {
16-
return null;
17-
}
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+
}
1819
19-
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
20-
curl_close($ch);
20+
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
21+
curl_close($ch);
2122
22-
// Check if the URL is redirected
23-
if ($httpCode == 301 || $httpCode == 302) {
24-
$redirectUrl = getRedirectUrlFromHeaders($response);
25-
if ($redirectUrl) {
26-
return getFaviconURL($redirectUrl); // Recursively call getFavicon with the redirected URL
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+
}
2729
}
28-
}
2930
30-
// Try extracting favicon using DOMDocument
31-
try {
32-
$dom = new DOMDocument();
33-
$dom->strictErrorChecking = false;
34-
@$dom->loadHTMLFile($url);
35-
if ($dom) {
36-
$faviconURL = extractFaviconUrlFromDOM($dom);
37-
if ($faviconURL) {
38-
return getAbsoluteUrl($url, $faviconURL);
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+
}
3941
}
42+
} catch (Exception $e) {
43+
// Silently fail and continue to the next method
4044
}
41-
} catch (Exception $e) {
42-
// Silently fail and continue to the next method
43-
}
4445
45-
// Check directly for favicon.ico or favicon.png
46-
$parse = parse_url($url);
47-
$faviconURL = getAbsoluteUrl($url, "/favicon.ico");
48-
if (checkURLExists($faviconURL)) {
49-
return $faviconURL;
50-
}
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+
}
5152
52-
$faviconURL = getAbsoluteUrl($url, "/favicon.png");
53-
if (checkURLExists($faviconURL)) {
54-
return $faviconURL;
55-
}
53+
$faviconURL = getAbsoluteUrl($url, "/favicon.png");
54+
if (checkURLExists($faviconURL)) {
55+
return $faviconURL;
56+
}
5657
57-
// Fallback to regex extraction
58-
$faviconURL = extractFaviconUrlWithRegex($response);
59-
if ($faviconURL) {
60-
$faviconURL = getAbsoluteUrl($url, $faviconURL);
58+
// Fallback to regex extraction
59+
$faviconURL = extractFaviconUrlWithRegex($response);
60+
if ($faviconURL) {
61+
$faviconURL = getAbsoluteUrl($url, $faviconURL);
62+
}
63+
return $faviconURL;
6164
}
62-
return $faviconURL;
6365
}
6466
65-
function getRedirectUrlFromHeaders($headers)
66-
{
67-
if (preg_match('/^Location:\s+(.*)$/mi', $headers, $matches)) {
68-
return trim($matches[1]);
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;
6974
}
70-
return null;
7175
}
7276
73-
function extractFaviconUrlFromDOM($dom)
74-
{
75-
$xpath = new DOMXPath($dom);
77+
if (!function_exists('extractFaviconUrlFromDOM')) {
78+
function extractFaviconUrlFromDOM($dom)
79+
{
80+
$xpath = new DOMXPath($dom);
7681
77-
// Check for the historical rel="shortcut icon"
78-
$shortcutIcon = $xpath->query('//link[@rel="shortcut icon"]');
79-
if ($shortcutIcon->length > 0) {
80-
$path = $shortcutIcon->item(0)->getAttribute('href');
81-
return $path;
82-
}
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+
}
8388
84-
// Check for the HTML5 rel="icon"
85-
$icon = $xpath->query('//link[@rel="icon"]');
86-
if ($icon->length > 0) {
87-
$path = $icon->item(0)->getAttribute('href');
88-
return $path;
89-
}
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+
}
9095
91-
return null;
96+
return null;
97+
}
9298
}
9399
94-
function checkURLExists($url)
95-
{
96-
$headers = @get_headers($url);
97-
return ($headers && strpos($headers[0], '200') !== false);
100+
if (!function_exists('checkURLExists')) {
101+
function checkURLExists($url)
102+
{
103+
$headers = @get_headers($url);
104+
return ($headers && strpos($headers[0], '200') !== false);
105+
}
98106
}
99107
100-
function extractFaviconUrlWithRegex($html)
101-
{
102-
// Check for the historical rel="shortcut icon"
103-
if (preg_match('/<link[^>]+rel=["\']shortcut icon["\'][^>]+href=["\']([^"\']+)["\']/', $html, $matches)) {
104-
$faviconURL = $matches[1];
105-
return $faviconURL;
106-
}
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+
}
107116
108-
// Check for the HTML5 rel="icon"
109-
if (preg_match('/<link[^>]+rel=["\']icon["\'][^>]+href=["\']([^"\']+)["\']/', $html, $matches)) {
110-
$faviconURL = $matches[1];
111-
return $faviconURL;
112-
}
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+
}
113122
114-
return null;
123+
return null;
124+
}
115125
}
116126
117-
function getAbsoluteUrl($baseUrl, $relativeUrl)
118-
{
119-
$parsedUrl = parse_url($baseUrl);
120-
$scheme = isset($parsedUrl['scheme']) ? $parsedUrl['scheme'] : 'http';
121-
$host = isset($parsedUrl['host']) ? $parsedUrl['host'] : '';
122-
$path = isset($parsedUrl['path']) ? $parsedUrl['path'] : '';
123-
$basePath = "$scheme://$host$path";
124-
125-
if (strpos($relativeUrl, 'http') === 0) {
126-
return $relativeUrl; // Already an absolute URL
127-
} elseif (strpos($relativeUrl, '/') === 0) {
128-
return "$scheme://$host$relativeUrl"; // Root-relative URL
129-
} else {
130-
return "$basePath/$relativeUrl"; // Path-relative URL
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+
}
131143
}
132144
}
133145
134-
function getFavIcon($id)
135-
{
136-
try {
137-
$link = Link::find($id);
138-
$page = $link->link;
139-
140-
$url = getFaviconURL($page);
141-
142-
$fileExtension = pathinfo($url, PATHINFO_EXTENSION);
143-
$filename = $id . '.' . $fileExtension;
144-
$filepath = base_path('assets/favicon/icons') . '/' . $filename;
145-
146-
if (!file_exists($filepath)) {
147-
if (function_exists('curl_version')) {
148-
$curlHandle = curl_init($url);
149-
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
150-
curl_setopt($curlHandle, CURLOPT_TIMEOUT, 3);
151-
$faviconData = curl_exec($curlHandle);
152-
curl_close($curlHandle);
153-
154-
if ($faviconData !== false) {
155-
file_put_contents($filepath, $faviconData);
146+
if (!function_exists('getFavIcon')) {
147+
function getFavIcon($id)
148+
{
149+
try {
150+
$link = Link::find($id);
151+
$page = $link->link;
152+
153+
$url = getFaviconURL($page);
154+
155+
$fileExtension = pathinfo($url, PATHINFO_EXTENSION);
156+
$filename = $id . '.' . $fileExtension;
157+
$filepath = base_path('assets/favicon/icons') . '/' . $filename;
158+
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));
156172
}
157-
} else {
158-
file_put_contents($filepath, file_get_contents($url));
159173
}
160-
}
161174
162-
return url('assets/favicon/icons/' . $id . '.' . $fileExtension);
163-
} catch (Exception $e) {
164-
// Handle the exception by copying the default SVG favicon
165-
$defaultIcon = base_path('assets/linkstack/icons/website.svg');
166-
$filename = $id . '.svg';
167-
$filepath = base_path('assets/favicon/icons') . '/' . $filename;
168-
copy($defaultIcon, $filepath);
175+
return url('assets/favicon/icons/' . $id . '.' . $fileExtension);
176+
} catch (Exception $e) {
177+
// Handle the exception by copying the default SVG favicon
178+
$defaultIcon = base_path('assets/linkstack/icons/website.svg');
179+
$filename = $id . '.svg';
180+
$filepath = base_path('assets/favicon/icons') . '/' . $filename;
181+
copy($defaultIcon, $filepath);
169182
170-
return url('assets/favicon/icons/' . $filename);
183+
return url('assets/favicon/icons/' . $filename);
184+
}
171185
}
172186
}
173-
?>

0 commit comments

Comments
 (0)