-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfavicon.php
182 lines (154 loc) · 5.1 KB
/
favicon.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
class Favicon
{
protected $url = '';
protected $default = '';
protected $cacheDir;
protected $cacheTimeout;
public function __construct($args = array())
{
if (isset($args['url'])) {
$this->url = $args['url'];
}
if (isset($args['default'])) {
$this->default = $args['default'];
}
}
public function cache($args) {
if (isset($args['dir'])) {
$this->cacheDir = $args['dir'];
} else {
$this->cacheDir = '/tmp';
}
if (isset($args['timeout'])) {
if ($args['timeout']) {
$this->cacheTimeout = $args['timeout'];
} else {
$this->cacheTimeout = 0;
}
}
}
public static function baseUrl($url)
{
$return = '';
if (!$url = parse_url($url)) {
return FALSE;
}
// Scheme
$scheme = strtolower($url['scheme']);
if ($scheme != 'http' && $scheme != 'https') {
return FALSE;
}
$return .= "{$scheme}://";
// Username and password
if (isset($url['user'])) {
$return .= $url['user'];
if (isset($url['pass'])) {
$return .= ":{$url['pass']}";
}
$return .= '@';
}
// Hostname
$return .= $url['host'];
// Port
if (isset($url['port'])) {
$return .= ":{$url['port']}";
}
// Path
$return .= '/';
return $return;
}
public static function info($url)
{
// Discover real status by following redirects.
$loop = TRUE;
while ($loop) {
$headers = get_headers($url, TRUE);
list(,$status) = explode(' ', $headers[0]);
switch ($status) {
case '301':
case '302':
$url = $headers['Location'];
break;
default:
$loop = FALSE;
break;
}
}
return array('status' => $status, 'url' => $url);
}
public function get($url = '')
{
// URLs passed to this method take precedence.
if (!empty($url)) {
$this->url = $url;
}
// Get the base URL without the path for clearer concatenations.
$url = rtrim($this->baseUrl($this->url), '/');
$found = FALSE;
// Check the cache first.
if ($this->cacheTimeout) {
$cache = $this->cacheDir . '/' . md5($url);
if (file_exists($cache) && is_readable($cache) && (time() - filemtime($cache) < $this->cacheTimeout)) {
$favicon = file_get_contents($cache);
$found = TRUE;
}
} else {
$cache = FALSE;
}
if (!$found) {
// Try /favicon.ico first.
$info = $this->info("{$url}/favicon.ico");
if ($info['status'] == '200') {
$favicon = $info['url'];
$found = TRUE;
}
}
// See if it's specified in a link tag.
if (!$found) {
$html = file_get_contents("{$url}/");
preg_match('!<head.*?>.*</head>!ims', $html, $match);
$head = $match[0];
$dom = new DOMDocument();
// Use error supression, because the HTML might be too malformed.
if (@$dom->loadHTML($head)) {
$links = $dom->getElementsByTagName('link');
// TODO: Improve this to adhere to a determined precedence.
foreach ($links as $link) {
if ($link->hasAttribute('rel') && strtolower($link->getAttribute('rel')) == 'shortcut icon') {
$favicon = $link->getAttribute('href');
$found = TRUE;
} elseif ($link->hasAttribute('rel') && strtolower($link->getAttribute('rel')) == 'icon') {
$favicon = $link->getAttribute('href');
$found = TRUE;
} elseif ($link->hasAttribute('href') && strpos($link->getAttribute('href'), 'favicon') !== FALSE) {
$favicon = $link->getAttribute('href');
$found = TRUE;
}
}
}
}
// Make sure the favicon is an absolute URL.
$parsed = parse_url($favicon);
if (!isset($parsed['scheme'])) {
$favicon = $url . $parsed['path'];
}
// Sometimes people lie, so check the status.
$info = $this->info($favicon);
if ($info['status'] != '200') {
$found = FALSE;
}
if ($found) {
// Check to see if result should be cached.
if ($cache) {
if (!file_exists($cache) || (is_writable($cache) && time() - filemtime($cache) > $this->cacheTimeout)) {
file_put_contents($cache, $favicon);
}
}
return $favicon;
} else {
return $this->default;
}
}
}
?>