mirror of
https://github.com/LinkStackOrg/LinkStack.git
synced 2025-02-01 11:56:44 +01:00
Removed requirement for "simplexml" extension
Replaced the usage of simplexml_import_dom() with an alternative method to extract favicon URLs from the HTML https://github.com/LinkStackOrg/linkstack-docker/pull/76 https://github.com/LinkStackOrg/linkstack-docker/issues/75
This commit is contained in:
parent
b99a975de1
commit
faeb78fd7b
@ -33,18 +33,9 @@ function getFaviconURL($url)
|
|||||||
$dom->strictErrorChecking = false;
|
$dom->strictErrorChecking = false;
|
||||||
@$dom->loadHTMLFile($url);
|
@$dom->loadHTMLFile($url);
|
||||||
if ($dom) {
|
if ($dom) {
|
||||||
$domxml = simplexml_import_dom($dom);
|
$faviconURL = extractFaviconUrlFromDOM($dom);
|
||||||
// Check for the historical rel="shortcut icon"
|
if ($faviconURL) {
|
||||||
if ($domxml->xpath('//link[@rel="shortcut icon"]')) {
|
return getAbsoluteUrl($url, $faviconURL);
|
||||||
$path = $domxml->xpath('//link[@rel="shortcut icon"]');
|
|
||||||
$faviconURL = getAbsoluteUrl($url, $path[0]['href']);
|
|
||||||
return $faviconURL;
|
|
||||||
}
|
|
||||||
// Check for the HTML5 rel="icon"
|
|
||||||
elseif ($domxml->xpath('//link[@rel="icon"]')) {
|
|
||||||
$path = $domxml->xpath('//link[@rel="icon"]');
|
|
||||||
$faviconURL = getAbsoluteUrl($url, $path[0]['href']);
|
|
||||||
return $faviconURL;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
@ -53,15 +44,13 @@ function getFaviconURL($url)
|
|||||||
|
|
||||||
// Check directly for favicon.ico or favicon.png
|
// Check directly for favicon.ico or favicon.png
|
||||||
$parse = parse_url($url);
|
$parse = parse_url($url);
|
||||||
$favicon_headers = @get_headers("http://" . $parse['host'] . "/favicon.ico");
|
$faviconURL = getAbsoluteUrl($url, "/favicon.ico");
|
||||||
if ($favicon_headers && $favicon_headers[0] != 'HTTP/1.1 404 Not Found') {
|
if (checkURLExists($faviconURL)) {
|
||||||
$faviconURL = "http://" . $parse['host'] . "/favicon.ico";
|
|
||||||
return $faviconURL;
|
return $faviconURL;
|
||||||
}
|
}
|
||||||
|
|
||||||
$favicon_headers = @get_headers("http://" . $parse['host'] . "/favicon.png");
|
$faviconURL = getAbsoluteUrl($url, "/favicon.png");
|
||||||
if ($favicon_headers && $favicon_headers[0] != 'HTTP/1.1 404 Not Found') {
|
if (checkURLExists($faviconURL)) {
|
||||||
$faviconURL = "http://" . $parse['host'] . "/favicon.png";
|
|
||||||
return $faviconURL;
|
return $faviconURL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,6 +70,33 @@ function getRedirectUrlFromHeaders($headers)
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function extractFaviconUrlFromDOM($dom)
|
||||||
|
{
|
||||||
|
$xpath = new DOMXPath($dom);
|
||||||
|
|
||||||
|
// Check for the historical rel="shortcut icon"
|
||||||
|
$shortcutIcon = $xpath->query('//link[@rel="shortcut icon"]');
|
||||||
|
if ($shortcutIcon->length > 0) {
|
||||||
|
$path = $shortcutIcon->item(0)->getAttribute('href');
|
||||||
|
return $path;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for the HTML5 rel="icon"
|
||||||
|
$icon = $xpath->query('//link[@rel="icon"]');
|
||||||
|
if ($icon->length > 0) {
|
||||||
|
$path = $icon->item(0)->getAttribute('href');
|
||||||
|
return $path;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkURLExists($url)
|
||||||
|
{
|
||||||
|
$headers = @get_headers($url);
|
||||||
|
return ($headers && strpos($headers[0], '200') !== false);
|
||||||
|
}
|
||||||
|
|
||||||
function extractFaviconUrlWithRegex($html)
|
function extractFaviconUrlWithRegex($html)
|
||||||
{
|
{
|
||||||
// Check for the historical rel="shortcut icon"
|
// Check for the historical rel="shortcut icon"
|
||||||
@ -117,36 +133,34 @@ function getAbsoluteUrl($baseUrl, $relativeUrl)
|
|||||||
|
|
||||||
function getFavIcon($id)
|
function getFavIcon($id)
|
||||||
{
|
{
|
||||||
try{
|
try {
|
||||||
|
$link = Link::find($id);
|
||||||
|
$page = $link->link;
|
||||||
|
|
||||||
$link = Link::find($id);
|
$url = getFaviconURL($page);
|
||||||
$page = $link->link;
|
|
||||||
|
|
||||||
$url = getFaviconURL($page);
|
$fileExtension = pathinfo($url, PATHINFO_EXTENSION);
|
||||||
|
$filename = $id . '.' . $fileExtension;
|
||||||
|
$filepath = base_path('assets/favicon/icons') . '/' . $filename;
|
||||||
|
|
||||||
$fileExtension = pathinfo($url, PATHINFO_EXTENSION);
|
if (!file_exists($filepath)) {
|
||||||
$filename = $id . '.' . $fileExtension;
|
if (function_exists('curl_version')) {
|
||||||
$filepath = base_path('assets/favicon/icons') . '/' . $filename;
|
$curlHandle = curl_init($url);
|
||||||
|
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
curl_setopt($curlHandle, CURLOPT_TIMEOUT, 3);
|
||||||
|
$faviconData = curl_exec($curlHandle);
|
||||||
|
curl_close($curlHandle);
|
||||||
|
|
||||||
if (!file_exists($filepath)) {
|
if ($faviconData !== false) {
|
||||||
if (function_exists('curl_version')) {
|
file_put_contents($filepath, $faviconData);
|
||||||
$curlHandle = curl_init($url);
|
}
|
||||||
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
|
} else {
|
||||||
curl_setopt($curlHandle, CURLOPT_TIMEOUT, 3);
|
file_put_contents($filepath, file_get_contents($url));
|
||||||
$faviconData = curl_exec($curlHandle);
|
|
||||||
curl_close($curlHandle);
|
|
||||||
|
|
||||||
if ($faviconData !== false) {
|
|
||||||
file_put_contents($filepath, $faviconData);
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
file_put_contents($filepath, file_get_contents($url));
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return url('assets/favicon/icons/' . $id . '.' . $fileExtension);
|
return url('assets/favicon/icons/' . $id . '.' . $fileExtension);
|
||||||
|
} catch (Exception $e) {
|
||||||
}catch(Exception $e){
|
|
||||||
// Handle the exception by copying the default SVG favicon
|
// Handle the exception by copying the default SVG favicon
|
||||||
$defaultIcon = base_path('assets/linkstack/icons/website.svg');
|
$defaultIcon = base_path('assets/linkstack/icons/website.svg');
|
||||||
$filename = $id . '.svg';
|
$filename = $id . '.svg';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user