@@ -33,18 +33,9 @@ function getFaviconURL($url)
33
33
$dom -> strictErrorChecking = false ;
34
34
@ $dom -> loadHTMLFile ($url );
35
35
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 );
48
39
}
49
40
}
50
41
} catch (Exception $e ) {
@@ -53,15 +44,13 @@ function getFaviconURL($url)
53
44
54
45
// Check directly for favicon.ico or favicon.png
55
46
$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 )) {
59
49
return $faviconURL ;
60
50
}
61
51
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 )) {
65
54
return $faviconURL ;
66
55
}
67
56
@@ -81,6 +70,33 @@ function getRedirectUrlFromHeaders($headers)
81
70
return null ;
82
71
}
83
72
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
+
84
100
function extractFaviconUrlWithRegex ($html )
85
101
{
86
102
// Check for the historical rel="shortcut icon"
@@ -117,36 +133,34 @@ function getAbsoluteUrl($baseUrl, $relativeUrl)
117
133
118
134
function getFavIcon ($id )
119
135
{
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 ;
126
139
127
- $fileExtension = pathinfo ($url , PATHINFO_EXTENSION );
128
- $filename = $id . ' .' . $fileExtension ;
129
- $filepath = base_path (' assets/favicon/icons' ) . ' /' . $filename ;
140
+ $url = getFaviconURL ($page );
130
141
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 ;
138
145
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 ));
141
159
}
142
- } else {
143
- file_put_contents ($filepath , file_get_contents ($url ));
144
160
}
145
- }
146
161
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 ) {
150
164
// Handle the exception by copying the default SVG favicon
151
165
$defaultIcon = base_path (' assets/linkstack/icons/website.svg' );
152
166
$filename = $id . ' .svg' ;
0 commit comments