@@ -33,18 +33,9 @@ function getFaviconURL($url)
3333 $dom -> strictErrorChecking = false ;
3434 @ $dom -> loadHTMLFile ($url );
3535 if ($dom ) {
36- $domxml = simplexml_import_dom ($dom );
37- // Check for the historical rel="shortcut icon"
38- if ($domxml -> xpath (' //link[@rel="shortcut icon"]' )) {
39- $path = $domxml -> xpath (' //link[@rel="shortcut icon"]' );
40- $faviconURL = getAbsoluteUrl ($url , $path [0 ][' href' ]);
41- return $faviconURL ;
42- }
43- // Check for the HTML5 rel="icon"
44- elseif ($domxml -> xpath (' //link[@rel="icon"]' )) {
45- $path = $domxml -> xpath (' //link[@rel="icon"]' );
46- $faviconURL = getAbsoluteUrl ($url , $path [0 ][' href' ]);
47- return $faviconURL ;
36+ $faviconURL = extractFaviconUrlFromDOM ($dom );
37+ if ($faviconURL ) {
38+ return getAbsoluteUrl ($url , $faviconURL );
4839 }
4940 }
5041 } catch (Exception $e ) {
@@ -53,15 +44,13 @@ function getFaviconURL($url)
5344
5445 // Check directly for favicon.ico or favicon.png
5546 $parse = parse_url ($url );
56- $favicon_headers = @ get_headers (" http://" . $parse [' host' ] . " /favicon.ico" );
57- if ($favicon_headers && $favicon_headers [0 ] != ' HTTP/1.1 404 Not Found' ) {
58- $faviconURL = " http://" . $parse [' host' ] . " /favicon.ico" ;
47+ $faviconURL = getAbsoluteUrl ($url , " /favicon.ico" );
48+ if (checkURLExists ($faviconURL )) {
5949 return $faviconURL ;
6050 }
6151
62- $favicon_headers = @ get_headers (" http://" . $parse [' host' ] . " /favicon.png" );
63- if ($favicon_headers && $favicon_headers [0 ] != ' HTTP/1.1 404 Not Found' ) {
64- $faviconURL = " http://" . $parse [' host' ] . " /favicon.png" ;
52+ $faviconURL = getAbsoluteUrl ($url , " /favicon.png" );
53+ if (checkURLExists ($faviconURL )) {
6554 return $faviconURL ;
6655 }
6756
@@ -81,6 +70,33 @@ function getRedirectUrlFromHeaders($headers)
8170 return null ;
8271}
8372
73+ function extractFaviconUrlFromDOM ($dom )
74+ {
75+ $xpath = new DOMXPath ($dom );
76+
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+ }
83+
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+ }
90+
91+ return null ;
92+ }
93+
94+ function checkURLExists ($url )
95+ {
96+ $headers = @ get_headers ($url );
97+ return ($headers && strpos ($headers [0 ], ' 200' ) !== false );
98+ }
99+
84100function extractFaviconUrlWithRegex ($html )
85101{
86102 // Check for the historical rel="shortcut icon"
@@ -117,36 +133,34 @@ function getAbsoluteUrl($baseUrl, $relativeUrl)
117133
118134function getFavIcon ($id )
119135{
120- try {
121-
122- $link = Link:: find ($id );
123- $page = $link -> link ;
124-
125- $url = getFaviconURL ($page );
136+ try {
137+ $link = Link:: find ($id );
138+ $page = $link -> link ;
126139
127- $fileExtension = pathinfo ($url , PATHINFO_EXTENSION );
128- $filename = $id . ' .' . $fileExtension ;
129- $filepath = base_path (' assets/favicon/icons' ) . ' /' . $filename ;
140+ $url = getFaviconURL ($page );
130141
131- if (! file_exists ($filepath )) {
132- if (function_exists (' curl_version' )) {
133- $curlHandle = curl_init ($url );
134- curl_setopt ($curlHandle , CURLOPT_RETURNTRANSFER , true );
135- curl_setopt ($curlHandle , CURLOPT_TIMEOUT , 3 );
136- $faviconData = curl_exec ($curlHandle );
137- curl_close ($curlHandle );
142+ $fileExtension = pathinfo ($url , PATHINFO_EXTENSION );
143+ $filename = $id . ' .' . $fileExtension ;
144+ $filepath = base_path (' assets/favicon/icons' ) . ' /' . $filename ;
138145
139- if ($faviconData !== false ) {
140- file_put_contents ($filepath , $faviconData );
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 );
156+ }
157+ } else {
158+ file_put_contents ($filepath , file_get_contents ($url ));
141159 }
142- } else {
143- file_put_contents ($filepath , file_get_contents ($url ));
144160 }
145- }
146161
147- return url (' assets/favicon/icons/' . $id . ' .' . $fileExtension );
148-
149- }catch (Exception $e ){
162+ return url (' assets/favicon/icons/' . $id . ' .' . $fileExtension );
163+ } catch (Exception $e ) {
150164 // Handle the exception by copying the default SVG favicon
151165 $defaultIcon = base_path (' assets/linkstack/icons/website.svg' );
152166 $filename = $id . ' .svg' ;
0 commit comments