|
@@ -5,184 +5,13 @@
|
5
|
5
|
* Thumbnail aide, mostly
|
6
|
6
|
*/
|
7
|
7
|
class ImageTools {
|
8
|
|
- /**
|
9
|
|
- * Store processed links to avoid repetition
|
10
|
|
- * @var array 'URL' => 'Parsed URL'
|
11
|
|
- */
|
12
|
|
- private static $Storage = [];
|
13
|
|
-
|
14
|
|
- /**
|
15
|
|
- * We use true as an extra property to make the domain an array key
|
16
|
|
- * @var array $Hosts Array of image hosts
|
17
|
|
- */
|
18
|
|
- private static $Hosts = array(
|
19
|
|
-// 'imgur.com' => true,
|
20
|
|
- 'whatimg.com' => true
|
21
|
|
- );
|
22
|
|
-
|
23
|
|
- /**
|
24
|
|
- * Blacklisted sites
|
25
|
|
- * @var array $Blacklist Array of blacklisted hosts
|
26
|
|
- */
|
27
|
|
- private static $Blacklist = array(
|
28
|
|
- 'tinypic.com'
|
29
|
|
- );
|
30
|
|
-
|
31
|
|
- /**
|
32
|
|
- * Array of image hosts that provide thumbnailing
|
33
|
|
- * @var array $Thumbs
|
34
|
|
- */
|
35
|
|
- private static $Thumbs = array(
|
36
|
|
-// 'i.imgur.com' => true,
|
37
|
|
- 'whatimg.com' => true
|
38
|
|
- );
|
39
|
|
-
|
40
|
|
- /**
|
41
|
|
- * Array of extensions
|
42
|
|
- * @var array $Extensions
|
43
|
|
- */
|
44
|
|
- private static $Extensions = array(
|
45
|
|
- 'jpg' => true,
|
46
|
|
- 'jpeg' => true,
|
47
|
|
- 'png' => true,
|
48
|
|
- 'gif' => true,
|
49
|
|
- );
|
50
|
|
-
|
51
|
|
- /**
|
52
|
|
- * Array of user IDs whose avatars have been checked for size
|
53
|
|
- * @var array $CheckedAvatars
|
54
|
|
- */
|
55
|
|
- private static $CheckedAvatars = [];
|
56
|
|
- private static $CheckedAvatars2 = [];
|
57
|
|
-
|
58
|
|
- /**
|
59
|
|
- * Array of user IDs whose donor icons have been checked for size
|
60
|
|
- * @var array $CheckedDonorIcons
|
61
|
|
- */
|
62
|
|
- private static $CheckedDonorIcons = [];
|
63
|
|
-
|
64
|
|
- /**
|
65
|
|
- * Checks from our list of valid hosts
|
66
|
|
- * @param string $Host Domain/host to check
|
67
|
|
- * @return boolean
|
68
|
|
- */
|
69
|
|
- public static function valid_host($Host) {
|
70
|
|
- return !empty(self::$Hosts[$Host]) && self::$Hosts[$Host] === true;
|
71
|
|
- }
|
72
|
|
-
|
73
|
|
- /**
|
74
|
|
- * Checks if a link's host is (not) good, otherwise displays an error.
|
75
|
|
- * @param string $Url Link to an image
|
76
|
|
- * @return boolean
|
77
|
|
- */
|
78
|
|
- public static function blacklisted($Url, $ShowError = true) {
|
79
|
|
- foreach (self::$Blacklist as &$Value) {
|
80
|
|
- $Blacklisted = stripos($Url, $Value);
|
81
|
|
- if ($Blacklisted !== false) {
|
82
|
|
- $ParsedUrl = parse_url($Url);
|
83
|
|
- if ($ShowError) {
|
84
|
|
- error($ParsedUrl['host'] . ' is not an allowed image host. Please use a different host.');
|
85
|
|
- }
|
86
|
|
- return true;
|
87
|
|
- }
|
88
|
|
- }
|
89
|
|
- return false;
|
90
|
|
- }
|
91
|
|
-
|
92
|
|
- /**
|
93
|
|
- * Checks to see if a link has a thumbnail
|
94
|
|
- * @param string $Url Link to an image
|
95
|
|
- * @return string|false Matched host or false
|
96
|
|
- */
|
97
|
|
- private static function thumbnailable($Url) {
|
98
|
|
- $ParsedUrl = parse_url($Url);
|
99
|
|
- return !empty(self::$Thumbs[$ParsedUrl['host']]);
|
100
|
|
- }
|
101
|
|
-
|
102
|
|
- /**
|
103
|
|
- * Checks an extension
|
104
|
|
- * @param string $Ext Extension to check
|
105
|
|
- * @return boolean
|
106
|
|
- */
|
107
|
|
- private static function valid_extension($Ext) {
|
108
|
|
-// return @self::$Extensions[$Ext] === true;
|
109
|
|
- return !empty(self::$Extensions[$Ext]) && (self::$Extensions[$Ext] === true);
|
110
|
|
- }
|
111
|
|
-
|
112
|
|
- /**
|
113
|
|
- * Stores a link with a (thumbnail) link
|
114
|
|
- * @param type $Link
|
115
|
|
- * @param type $Processed
|
116
|
|
- */
|
117
|
|
- private static function store($Link, $Processed) {
|
118
|
|
- self::$Storage[$Link] = $Processed;
|
119
|
|
- }
|
120
|
|
-
|
121
|
|
- /**
|
122
|
|
- * Retrieves an entry from our storage
|
123
|
|
- * @param type $Link
|
124
|
|
- * @return boolean|string Returns false if no match
|
125
|
|
- */
|
126
|
|
- private static function get_stored($Link) {
|
127
|
|
- if (isset(self::$Storage[$Link])) {
|
128
|
|
- return self::$Storage[$Link];
|
129
|
|
- }
|
130
|
|
- return false;
|
131
|
|
- }
|
132
|
|
-
|
133
|
|
- /**
|
134
|
|
- * Checks if URL points to a whatimg thumbnail.
|
135
|
|
- */
|
136
|
|
- private static function has_whatimg_thumb($Url) {
|
137
|
|
- return (strpos($Url, '_thumb') !== false);
|
138
|
|
- }
|
139
|
|
-
|
140
|
|
- /**
|
141
|
|
- * Cleans up imgur URL if it already has a modifier attached to the end of it.
|
142
|
|
- */
|
143
|
|
- private static function clean_imgur_url($Url) {
|
144
|
|
- $Extension = pathinfo($Url, PATHINFO_EXTENSION);
|
145
|
|
- $Full = preg_replace('/\.[^.]*$/', '', $Url);
|
146
|
|
- $Base = substr($Full, 0, strrpos($Full, '/'));
|
147
|
|
- $Path = substr($Full, strrpos($Full, '/') + 1);
|
148
|
|
- if (strlen($Path) == 6) {
|
149
|
|
- $Last = $Path[strlen($Path) - 1];
|
150
|
|
- if ($Last == 'm' || $Last == 'l' || $Last == 's' || $Last == 'h' || $Last == 'b') {
|
151
|
|
- $Path = substr($Path, 0, -1);
|
152
|
|
- }
|
153
|
|
- }
|
154
|
|
- return "$Base/$Path.$Extension";
|
155
|
|
- }
|
156
|
|
-
|
157
|
|
- /**
|
158
|
|
- * Replaces the extension.
|
159
|
|
- */
|
160
|
|
- private static function replace_extension($String, $Extension) {
|
161
|
|
- return preg_replace('/\.[^.]*$/', $Extension, $String);
|
162
|
|
- }
|
163
|
8
|
|
164
|
9
|
/**
|
165
|
10
|
* Create image proxy URL
|
166
|
11
|
* @param string $Url image URL
|
167
|
|
- * @param bool/string $CheckSize - accepts one of false, "avatar", "avatar2", or "donoricon"
|
168
|
|
- * @param bool/string/number $UserID - user ID for avatars and donor icons
|
169
|
12
|
* @return image proxy URL
|
170
|
13
|
*/
|
171
|
|
- public static function proxy_url($Url, $CheckSize, $UserID, &$ExtraInfo) {
|
172
|
|
- if ($UserID) {
|
173
|
|
- $ExtraInfo = "&userid=$UserID";
|
174
|
|
- if ($CheckSize === 'avatar' && !isset(self::$CheckedAvatars[$UserID])) {
|
175
|
|
- $ExtraInfo .= "&type=$CheckSize";
|
176
|
|
- self::$CheckedAvatars[$UserID] = true;
|
177
|
|
- } elseif ($CheckSize === 'avatar2' && !isset(self::$CheckedAvatars2[$UserID])) {
|
178
|
|
- $ExtraInfo .= "&type=$CheckSize";
|
179
|
|
- self::$CheckedAvatars2[$UserID] = true;
|
180
|
|
- } elseif ($CheckSize === 'donoricon' && !isset(self::$CheckedDonorIcons[$UserID])) {
|
181
|
|
- $ExtraInfo .= "&type=$CheckSize";
|
182
|
|
- self::$CheckedDonorIcons[$UserID] = true;
|
183
|
|
- }
|
184
|
|
- }
|
185
|
|
-
|
|
14
|
+ public static function proxy_url($Url) {
|
186
|
15
|
if (preg_match('/^https:\/\/('.SITE_DOMAIN.'|'.IMAGE_DOMAIN.')\//', $Url) || $Url[0]=='/') {
|
187
|
16
|
if (strpos($Url, '?') === false) $Url .= '?';
|
188
|
17
|
return $Url;
|
|
@@ -195,56 +24,28 @@ class ImageTools {
|
195
|
24
|
* Determine the image URL. This takes care of the image proxy and thumbnailing.
|
196
|
25
|
* @param string $Url
|
197
|
26
|
* @param bool $Thumb
|
198
|
|
- * @param bool/string $CheckSize - accepts one of false, "avatar", "avatar2", or "donoricon"
|
199
|
|
- * @param bool/string/number $UserID - user ID for avatars and donor icons
|
200
|
27
|
* @return string
|
201
|
28
|
*/
|
202
|
|
- public static function process($Url, $Thumb = false, $CheckSize = false, $UserID = false) {
|
203
|
|
- if (empty($Url)) {
|
204
|
|
- return '';
|
205
|
|
- }
|
206
|
|
-
|
207
|
|
- if ($Found = self::get_stored($Url . ($Thumb ? '_thumb' : ''))) {
|
208
|
|
- return $Found;
|
209
|
|
- }
|
210
|
|
-
|
211
|
|
- $ProcessedUrl = $Url;
|
212
|
|
- if ($Thumb) {
|
213
|
|
- $Extension = pathinfo($Url, PATHINFO_EXTENSION);
|
214
|
|
- if (self::thumbnailable($Url) && self::valid_extension($Extension)) {
|
215
|
|
- if (strpos($Url, 'whatimg') !== false && !self::has_whatimg_thumb($Url)) {
|
216
|
|
- $ProcessedUrl = self::replace_extension($Url, '_thumb.' . $Extension);
|
217
|
|
- } elseif (strpos($Url, 'imgur') !== false) {
|
218
|
|
- $ProcessedUrl = self::replace_extension(self::clean_imgur_url($Url), 'm.' . $Extension);
|
219
|
|
- }
|
220
|
|
- }
|
221
|
|
- }
|
222
|
|
-
|
223
|
|
- $ExtraInfo = '';
|
224
|
|
- if (check_perms('site_proxy_images')) {
|
225
|
|
- $ProcessedUrl = self::proxy_url($ProcessedUrl, $CheckSize, $UserID, $ExtraInfo);
|
226
|
|
- }
|
227
|
|
- self::store($Url . ($Thumb ? '_thumb' : ''), $ProcessedUrl);
|
228
|
|
- return $ProcessedUrl . $ExtraInfo;
|
|
29
|
+ public static function process($Url = '', $Thumb = false) {
|
|
30
|
+ // TODO: Thumbnailing
|
|
31
|
+ return $Url ? self::proxy_url($Url) : '';
|
229
|
32
|
}
|
230
|
33
|
|
231
|
34
|
/**
|
232
|
|
- * Cover art thumbnail in browse, on artist pages etc.
|
233
|
|
- * @global array $CategoryIcons
|
234
|
|
- * @param string $Url
|
235
|
|
- * @param int $CategoryID
|
|
35
|
+ * Checks if a link's host is (not) good, otherwise displays an error.
|
|
36
|
+ * @param string $Url Link to an image
|
|
37
|
+ * @return boolean
|
236
|
38
|
*/
|
237
|
|
- public static function cover_thumb($Url, $CategoryID) {
|
238
|
|
- global $CategoryIcons;
|
239
|
|
- if ($Url) {
|
240
|
|
- $Src = self::process($Url, true);
|
241
|
|
- $Lightbox = self::process($Url);
|
242
|
|
- } else {
|
243
|
|
- $Src = STATIC_SERVER . 'common/noartwork/' . $CategoryIcons[$CategoryID - 1];
|
244
|
|
- $Lightbox = $Src;
|
|
39
|
+ public static function blacklisted($Url, $ShowError = true) {
|
|
40
|
+ $Blacklist = ['tinypic.com'];
|
|
41
|
+ foreach ($Blacklist as $Value) {
|
|
42
|
+ if (stripos($Url, $Value) !== false) {
|
|
43
|
+ if ($ShowError) {
|
|
44
|
+ error($Value . ' is not an allowed image host. Please use a different host.');
|
|
45
|
+ }
|
|
46
|
+ return true;
|
|
47
|
+ }
|
245
|
48
|
}
|
246
|
|
-?>
|
247
|
|
- <img src="<?=$Src?>" width="90" height="90" alt="Cover" class="lightbox-init" lightbox-img="<?=$Lightbox?>" />
|
248
|
|
-<?
|
|
49
|
+ return false;
|
249
|
50
|
}
|
250
|
51
|
}
|