LinkStack/resources/views/components/favicon.blade.php

84 lines
2.7 KiB
PHP
Raw Normal View History

2022-11-28 19:44:02 +01:00
<?php use App\Models\Link; ?>
2022-11-24 17:23:49 +01:00
<?php
2022-11-28 19:44:02 +01:00
function getFavIcon($id) {
2023-06-20 13:05:25 +02:00
$link = Link::find($id);
$url = $link->link;
$html = false;
$context = stream_context_create();
// Set timeout to 3 seconds
stream_context_set_option($context, 'http', 'timeout', 3);
// Set custom User-Agent header
$userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36';
stream_context_set_option($context, 'http', 'header', "User-Agent: $userAgent\r\n");
// Attempt to fetch HTML content with timeout
if (function_exists('curl_version')) {
$curlHandle = curl_init($url);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandle, CURLOPT_TIMEOUT, 3);
curl_setopt($curlHandle, CURLOPT_USERAGENT, $userAgent);
$html = curl_exec($curlHandle);
curl_close($curlHandle);
} else {
$html = @file_get_contents($url, false, $context);
}
2022-11-24 20:10:39 +01:00
2023-06-20 13:05:25 +02:00
$dom = new DOMDocument();
if ($html !== false) {
@$dom->loadHTML($html);
}
2022-11-24 17:23:49 +01:00
2023-06-20 13:05:25 +02:00
$xpath = new DOMXPath($dom);
2022-11-24 17:23:49 +01:00
2023-06-20 13:05:25 +02:00
$faviconUrl = '';
2022-11-24 17:23:49 +01:00
2023-06-20 13:05:25 +02:00
// Search for <link> tags with rel="icon" or rel="shortcut icon"
$linkTags = $xpath->query("//link[contains(@rel, 'icon') or contains(@rel, 'shortcut icon')]");
foreach ($linkTags as $tag) {
$faviconUrl = $tag->getAttribute('href');
if (strpos($faviconUrl, 'http') !== 0) {
$faviconUrl = $url . '/' . ltrim($faviconUrl, '/');
2022-11-24 17:23:49 +01:00
}
2023-06-20 13:05:25 +02:00
break; // Stop after the first matching <link> tag
2022-11-24 17:23:49 +01:00
}
2023-06-20 13:05:25 +02:00
$fallbackFavicon = 'assets/linkstack/icons/website.svg';
2022-12-09 18:06:53 +01:00
2023-06-20 13:05:25 +02:00
if (empty($faviconUrl)) {
$faviconUrl = $fallbackFavicon;
2022-12-09 18:06:53 +01:00
}
2022-11-24 17:23:49 +01:00
2023-06-20 13:05:25 +02:00
$extension = pathinfo($faviconUrl, PATHINFO_EXTENSION);
$filename = $id . "." . $extension;
$filepath = base_path("assets/favicon/icons") . "/" . $filename;
if (!file_exists($filepath)) {
if ($faviconUrl !== $fallbackFavicon) {
if (function_exists('curl_version')) {
$curlHandle = curl_init($faviconUrl);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandle, CURLOPT_TIMEOUT, 3);
curl_setopt($curlHandle, CURLOPT_USERAGENT, $userAgent);
$faviconData = curl_exec($curlHandle);
curl_close($curlHandle);
if ($faviconData !== false) {
file_put_contents($filepath, $faviconData);
}
} else {
file_put_contents($filepath, file_get_contents($faviconUrl, false, $context));
}
} else {
copy($fallbackFavicon, $filepath);
}
}
2022-12-09 18:06:53 +01:00
2023-06-20 13:05:25 +02:00
return $filename;
2022-11-28 19:44:02 +01:00
}
2023-06-20 13:05:25 +02:00
?>
2022-11-28 19:44:02 +01:00