|
1
|
|
|
<?php |
|
2
|
|
|
////////////////////////////////////////////////////////////// |
|
3
|
|
|
// phpThumb() by James Heinrich <[email protected]> // |
|
4
|
|
|
// available at http://phpthumb.sourceforge.net // |
|
5
|
|
|
// and/or https://github.com/JamesHeinrich/phpThumb // |
|
6
|
|
|
////////////////////////////////////////////////////////////// |
|
7
|
|
|
/// // |
|
8
|
|
|
// phpthumb.functions.php - general support functions // |
|
9
|
|
|
// /// |
|
10
|
|
|
////////////////////////////////////////////////////////////// |
|
11
|
|
|
|
|
12
|
|
|
class phpthumb_functions { |
|
13
|
|
|
|
|
14
|
|
|
public static function user_function_exists($functionname) { |
|
15
|
|
|
if (function_exists('get_defined_functions')) { |
|
16
|
|
|
static $get_defined_functions = array(); |
|
17
|
|
|
if (empty($get_defined_functions)) { |
|
18
|
|
|
$get_defined_functions = get_defined_functions(); |
|
19
|
|
|
} |
|
20
|
|
|
return in_array(strtolower($functionname), $get_defined_functions['user']); |
|
21
|
|
|
} |
|
22
|
|
|
return function_exists($functionname); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
public static function builtin_function_exists($functionname) { |
|
27
|
|
|
if (function_exists('get_defined_functions')) { |
|
28
|
|
|
static $get_defined_functions = array(); |
|
29
|
|
|
if (empty($get_defined_functions)) { |
|
30
|
|
|
$get_defined_functions = get_defined_functions(); |
|
31
|
|
|
} |
|
32
|
|
|
return in_array(strtolower($functionname), $get_defined_functions['internal']); |
|
33
|
|
|
} |
|
34
|
|
|
return function_exists($functionname); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
public static function version_compare_replacement_sub($version1, $version2, $operator='') { |
|
39
|
|
|
// If you specify the third optional operator argument, you can test for a particular relationship. |
|
40
|
|
|
// The possible operators are: <, lt, <=, le, >, gt, >=, ge, ==, =, eq, !=, <>, ne respectively. |
|
41
|
|
|
// Using this argument, the function will return 1 if the relationship is the one specified by the operator, 0 otherwise. |
|
42
|
|
|
|
|
43
|
|
|
// If a part contains special version strings these are handled in the following order: |
|
44
|
|
|
// (any string not found in this list) < (dev) < (alpha = a) < (beta = b) < (RC = rc) < (#) < (pl = p) |
|
45
|
|
|
static $versiontype_lookup = array(); |
|
46
|
|
|
if (empty($versiontype_lookup)) { |
|
47
|
|
|
$versiontype_lookup['dev'] = 10001; |
|
48
|
|
|
$versiontype_lookup['a'] = 10002; |
|
49
|
|
|
$versiontype_lookup['alpha'] = 10002; |
|
50
|
|
|
$versiontype_lookup['b'] = 10003; |
|
51
|
|
|
$versiontype_lookup['beta'] = 10003; |
|
52
|
|
|
$versiontype_lookup['RC'] = 10004; |
|
53
|
|
|
$versiontype_lookup['rc'] = 10004; |
|
54
|
|
|
$versiontype_lookup['#'] = 10005; |
|
55
|
|
|
$versiontype_lookup['pl'] = 10006; |
|
56
|
|
|
$versiontype_lookup['p'] = 10006; |
|
57
|
|
|
} |
|
58
|
|
|
$version1 = (isset($versiontype_lookup[$version1]) ? $versiontype_lookup[$version1] : $version1); |
|
59
|
|
|
$version2 = (isset($versiontype_lookup[$version2]) ? $versiontype_lookup[$version2] : $version2); |
|
60
|
|
|
|
|
61
|
|
|
switch ($operator) { |
|
62
|
|
|
case '<': |
|
63
|
|
|
case 'lt': |
|
64
|
|
|
return (int) ($version1 < $version2); |
|
65
|
|
|
break; |
|
|
|
|
|
|
66
|
|
|
case '<=': |
|
67
|
|
|
case 'le': |
|
68
|
|
|
return (int) ($version1 <= $version2); |
|
69
|
|
|
break; |
|
70
|
|
|
case '>': |
|
71
|
|
|
case 'gt': |
|
72
|
|
|
return (int) ($version1 > $version2); |
|
73
|
|
|
break; |
|
74
|
|
|
case '>=': |
|
75
|
|
|
case 'ge': |
|
76
|
|
|
return (int) ($version1 >= $version2); |
|
77
|
|
|
break; |
|
78
|
|
|
case '==': |
|
79
|
|
|
case '=': |
|
80
|
|
|
case 'eq': |
|
81
|
|
|
return (int) ($version1 == $version2); |
|
82
|
|
|
break; |
|
83
|
|
|
case '!=': |
|
84
|
|
|
case '<>': |
|
85
|
|
|
case 'ne': |
|
86
|
|
|
return (int) ($version1 != $version2); |
|
87
|
|
|
break; |
|
88
|
|
|
} |
|
89
|
|
|
if ($version1 == $version2) { |
|
90
|
|
|
return 0; |
|
91
|
|
|
} elseif ($version1 < $version2) { |
|
92
|
|
|
return -1; |
|
93
|
|
|
} |
|
94
|
|
|
return 1; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
public static function version_compare_replacement($version1, $version2, $operator='') { |
|
99
|
|
|
if (function_exists('version_compare')) { |
|
100
|
|
|
// built into PHP v4.1.0+ |
|
101
|
|
|
return version_compare($version1, $version2, $operator); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// The function first replaces _, - and + with a dot . in the version strings |
|
105
|
|
|
$version1 = strtr($version1, '_-+', '...'); |
|
106
|
|
|
$version2 = strtr($version2, '_-+', '...'); |
|
107
|
|
|
|
|
108
|
|
|
// and also inserts dots . before and after any non number so that for example '4.3.2RC1' becomes '4.3.2.RC.1'. |
|
109
|
|
|
// Then it splits the results like if you were using explode('.',$ver). Then it compares the parts starting from left to right. |
|
110
|
|
|
$version1 = preg_replace('#([\d]+)([A-Z]+)([\d]+)#i', '$1.$2.$3', $version1); |
|
111
|
|
|
$version2 = preg_replace('#([\d]+)([A-Z]+)([\d]+)#i', '$1.$2.$3', $version2); |
|
112
|
|
|
|
|
113
|
|
|
$parts1 = explode('.', $version1); |
|
114
|
|
|
$parts2 = explode('.', $version1); |
|
115
|
|
|
$parts_count = max(count($parts1), count($parts2)); |
|
116
|
|
|
for ($i = 0; $i < $parts_count; $i++) { |
|
117
|
|
|
$comparison = self::version_compare_replacement_sub($version1, $version2, $operator); |
|
118
|
|
|
if ($comparison != 0) { |
|
119
|
|
|
return $comparison; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
return 0; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public static function escapeshellarg_replacement($arg) { |
|
126
|
|
|
if (function_exists('escapeshellarg') && !self::FunctionIsDisabled('escapeshellarg')) { |
|
127
|
|
|
return escapeshellarg($arg); |
|
128
|
|
|
} |
|
129
|
|
|
return '\''.str_replace('\'', '\\\'', $arg).'\''; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public static function phpinfo_array() { |
|
133
|
|
|
static $phpinfo_array = array(); |
|
134
|
|
|
if (empty($phpinfo_array)) { |
|
135
|
|
|
ob_start(); |
|
136
|
|
|
phpinfo(); |
|
137
|
|
|
$phpinfo = ob_get_contents(); |
|
138
|
|
|
ob_end_clean(); |
|
139
|
|
|
$phpinfo_array = explode("\n", $phpinfo); |
|
140
|
|
|
} |
|
141
|
|
|
return $phpinfo_array; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
public static function exif_info() { |
|
146
|
|
|
static $exif_info = array(); |
|
147
|
|
|
if (empty($exif_info)) { |
|
148
|
|
|
// based on code by johnschaefer at gmx dot de |
|
149
|
|
|
// from PHP help on gd_info() |
|
150
|
|
|
$exif_info = array( |
|
151
|
|
|
'EXIF Support' => '', |
|
152
|
|
|
'EXIF Version' => '', |
|
153
|
|
|
'Supported EXIF Version' => '', |
|
154
|
|
|
'Supported filetypes' => '' |
|
155
|
|
|
); |
|
156
|
|
|
$phpinfo_array = self::phpinfo_array(); |
|
157
|
|
|
foreach ($phpinfo_array as $line) { |
|
158
|
|
|
$line = trim(strip_tags($line)); |
|
159
|
|
|
foreach ($exif_info as $key => $value) { |
|
160
|
|
|
if (strpos($line, $key) === 0) { |
|
161
|
|
|
$newvalue = trim(str_replace($key, '', $line)); |
|
162
|
|
|
$exif_info[$key] = $newvalue; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
return $exif_info; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
public static function ImageTypeToMIMEtype($imagetype) { |
|
172
|
|
|
if (function_exists('image_type_to_mime_type') && ($imagetype >= 1) && ($imagetype <= 18)) { |
|
173
|
|
|
// PHP v4.3.0+ |
|
174
|
|
|
return image_type_to_mime_type($imagetype); |
|
175
|
|
|
} |
|
176
|
|
|
static $image_type_to_mime_type = array( |
|
177
|
|
|
1 => 'image/gif', // IMAGETYPE_GIF |
|
178
|
|
|
2 => 'image/jpeg', // IMAGETYPE_JPEG |
|
179
|
|
|
3 => 'image/png', // IMAGETYPE_PNG |
|
180
|
|
|
4 => 'application/x-shockwave-flash', // IMAGETYPE_SWF |
|
181
|
|
|
5 => 'image/psd', // IMAGETYPE_PSD |
|
182
|
|
|
6 => 'image/bmp', // IMAGETYPE_BMP |
|
183
|
|
|
7 => 'image/tiff', // IMAGETYPE_TIFF_II (intel byte order) |
|
184
|
|
|
8 => 'image/tiff', // IMAGETYPE_TIFF_MM (motorola byte order) |
|
185
|
|
|
9 => 'application/octet-stream', // IMAGETYPE_JPC |
|
186
|
|
|
10 => 'image/jp2', // IMAGETYPE_JP2 |
|
187
|
|
|
11 => 'application/octet-stream', // IMAGETYPE_JPX |
|
188
|
|
|
12 => 'application/octet-stream', // IMAGETYPE_JB2 |
|
189
|
|
|
13 => 'application/x-shockwave-flash', // IMAGETYPE_SWC |
|
190
|
|
|
14 => 'image/iff', // IMAGETYPE_IFF |
|
191
|
|
|
15 => 'image/vnd.wap.wbmp', // IMAGETYPE_WBMP |
|
192
|
|
|
16 => 'image/xbm', // IMAGETYPE_XBM |
|
193
|
|
|
17 => 'image/x-icon', // IMAGETYPE_ICO |
|
194
|
|
|
18 => 'image/webp', // IMAGETYPE_WEBP |
|
195
|
|
|
|
|
196
|
|
|
'gif' => 'image/gif', // IMAGETYPE_GIF |
|
197
|
|
|
'jpg' => 'image/jpeg', // IMAGETYPE_JPEG |
|
198
|
|
|
'jpeg' => 'image/jpeg', // IMAGETYPE_JPEG |
|
199
|
|
|
'png' => 'image/png', // IMAGETYPE_PNG |
|
200
|
|
|
'bmp' => 'image/bmp', // IMAGETYPE_BMP |
|
201
|
|
|
'ico' => 'image/x-icon', // IMAGETYPE_ICO |
|
202
|
|
|
'webp' => 'image/webp', // IMAGETYPE_WEBP |
|
203
|
|
|
); |
|
204
|
|
|
|
|
205
|
|
|
return (isset($image_type_to_mime_type[$imagetype]) ? $image_type_to_mime_type[$imagetype] : false); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
public static function TranslateWHbyAngle($width, $height, $angle) { |
|
210
|
|
|
if (($angle % 180) == 0) { |
|
211
|
|
|
return array($width, $height); |
|
212
|
|
|
} |
|
213
|
|
|
$newwidth = (abs(sin(deg2rad($angle))) * $height) + (abs(cos(deg2rad($angle))) * $width); |
|
214
|
|
|
$newheight = (abs(sin(deg2rad($angle))) * $width) + (abs(cos(deg2rad($angle))) * $height); |
|
215
|
|
|
return array($newwidth, $newheight); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
public static function HexCharDisplay($string) { |
|
219
|
|
|
$len = strlen($string); |
|
220
|
|
|
$output = ''; |
|
221
|
|
|
for ($i = 0; $i < $len; $i++) { |
|
222
|
|
|
$output .= ' 0x'.str_pad(dechex(ord($string[$i])), 2, '0', STR_PAD_LEFT); |
|
223
|
|
|
} |
|
224
|
|
|
return $output; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
|
|
228
|
|
|
public static function IsHexColor($HexColorString) { |
|
229
|
|
|
return preg_match('#^[0-9A-F]{6}$#i', $HexColorString); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
|
|
233
|
|
|
public static function ImageColorAllocateAlphaSafe(&$gdimg_hexcolorallocate, $R, $G, $B, $alpha=false) { |
|
234
|
|
|
if (self::version_compare_replacement(PHP_VERSION, '4.3.2', '>=') && ($alpha !== false)) { |
|
235
|
|
|
return imagecolorallocatealpha($gdimg_hexcolorallocate, $R, $G, $B, (int) $alpha); |
|
236
|
|
|
} else { |
|
237
|
|
|
return imagecolorallocate($gdimg_hexcolorallocate, $R, $G, $B); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
public static function ImageHexColorAllocate(&$gdimg_hexcolorallocate, $HexColorString, $dieOnInvalid=false, $alpha=false) { |
|
242
|
|
|
if (!is_resource($gdimg_hexcolorallocate)) { |
|
243
|
|
|
die('$gdimg_hexcolorallocate is not a GD resource in ImageHexColorAllocate()'); |
|
|
|
|
|
|
244
|
|
|
} |
|
245
|
|
|
if (self::IsHexColor($HexColorString)) { |
|
246
|
|
|
$R = hexdec(substr($HexColorString, 0, 2)); |
|
247
|
|
|
$G = hexdec(substr($HexColorString, 2, 2)); |
|
248
|
|
|
$B = hexdec(substr($HexColorString, 4, 2)); |
|
249
|
|
|
return self::ImageColorAllocateAlphaSafe($gdimg_hexcolorallocate, $R, $G, $B, $alpha); |
|
250
|
|
|
} |
|
251
|
|
|
if ($dieOnInvalid) { |
|
252
|
|
|
die('Invalid hex color string: "'.$HexColorString.'"'); |
|
|
|
|
|
|
253
|
|
|
} |
|
254
|
|
|
return imagecolorallocate($gdimg_hexcolorallocate, 0x00, 0x00, 0x00); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
|
|
258
|
|
|
public static function HexColorXOR($hexcolor) { |
|
259
|
|
|
return strtoupper(str_pad(dechex(~hexdec($hexcolor) & 0xFFFFFF), 6, '0', STR_PAD_LEFT)); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
|
|
263
|
|
|
public static function GetPixelColor(&$img, $x, $y) { |
|
264
|
|
|
if (!is_resource($img)) { |
|
265
|
|
|
return false; |
|
266
|
|
|
} |
|
267
|
|
|
return @imagecolorsforindex($img, @imagecolorat($img, $x, $y)); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
|
|
271
|
|
|
public static function PixelColorDifferencePercent($currentPixel, $targetPixel) { |
|
272
|
|
|
$diff = 0; |
|
273
|
|
|
foreach ($targetPixel as $channel => $currentvalue) { |
|
274
|
|
|
$diff = max($diff, (max($currentPixel[$channel], $targetPixel[$channel]) - min($currentPixel[$channel], $targetPixel[$channel])) / 255); |
|
275
|
|
|
} |
|
276
|
|
|
return $diff * 100; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
public static function GrayscaleValue($r, $g, $b) { |
|
280
|
|
|
return round(($r * 0.30) + ($g * 0.59) + ($b * 0.11)); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
|
|
284
|
|
|
public static function GrayscalePixel($OriginalPixel) { |
|
285
|
|
|
$gray = self::GrayscaleValue($OriginalPixel[ 'red'], $OriginalPixel[ 'green'], $OriginalPixel[ 'blue']); |
|
286
|
|
|
return array('red'=>$gray, 'green'=>$gray, 'blue'=>$gray); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
|
|
290
|
|
|
public static function GrayscalePixelRGB($rgb) { |
|
291
|
|
|
$r = ($rgb >> 16) & 0xFF; |
|
292
|
|
|
$g = ($rgb >> 8) & 0xFF; |
|
293
|
|
|
$b = $rgb & 0xFF; |
|
294
|
|
|
return ($r * 0.299) + ($g * 0.587) + ($b * 0.114); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
|
|
298
|
|
|
public static function ScaleToFitInBox($width, $height, $maxwidth=null, $maxheight=null, $allow_enlarge=true, $allow_reduce=true) { |
|
299
|
|
|
$maxwidth = (null === $maxwidth ? $width : $maxwidth); |
|
300
|
|
|
$maxheight = (null === $maxheight ? $height : $maxheight); |
|
301
|
|
|
$scale_x = 1; |
|
302
|
|
|
$scale_y = 1; |
|
303
|
|
|
if (($width > $maxwidth) || ($width < $maxwidth)) { |
|
304
|
|
|
$scale_x = ($maxwidth / $width); |
|
305
|
|
|
} |
|
306
|
|
|
if (($height > $maxheight) || ($height < $maxheight)) { |
|
307
|
|
|
$scale_y = ($maxheight / $height); |
|
308
|
|
|
} |
|
309
|
|
|
$scale = min($scale_x, $scale_y); |
|
310
|
|
|
if (!$allow_enlarge) { |
|
311
|
|
|
$scale = min($scale, 1); |
|
312
|
|
|
} |
|
313
|
|
|
if (!$allow_reduce) { |
|
314
|
|
|
$scale = max($scale, 1); |
|
315
|
|
|
} |
|
316
|
|
|
return $scale; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
public static function ImageCopyResampleBicubic($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { |
|
320
|
|
|
// ron at korving dot demon dot nl |
|
321
|
|
|
// http://www.php.net/imagecopyresampled |
|
322
|
|
|
|
|
323
|
|
|
$scaleX = ($src_w - 1) / $dst_w; |
|
324
|
|
|
$scaleY = ($src_h - 1) / $dst_h; |
|
325
|
|
|
|
|
326
|
|
|
$scaleX2 = $scaleX / 2.0; |
|
327
|
|
|
$scaleY2 = $scaleY / 2.0; |
|
328
|
|
|
|
|
329
|
|
|
$isTrueColor = imageistruecolor($src_img); |
|
330
|
|
|
|
|
331
|
|
|
for ($y = $src_y; $y < $src_y + $dst_h; $y++) { |
|
332
|
|
|
$sY = $y * $scaleY; |
|
333
|
|
|
$siY = (int) $sY; |
|
334
|
|
|
$siY2 = (int) $sY + $scaleY2; |
|
335
|
|
|
|
|
336
|
|
|
for ($x = $src_x; $x < $src_x + $dst_w; $x++) { |
|
337
|
|
|
$sX = $x * $scaleX; |
|
338
|
|
|
$siX = (int) $sX; |
|
339
|
|
|
$siX2 = (int) $sX + $scaleX2; |
|
340
|
|
|
|
|
341
|
|
|
if ($isTrueColor) { |
|
342
|
|
|
|
|
343
|
|
|
$c1 = imagecolorat($src_img, $siX, $siY2); |
|
344
|
|
|
$c2 = imagecolorat($src_img, $siX, $siY); |
|
345
|
|
|
$c3 = imagecolorat($src_img, $siX2, $siY2); |
|
346
|
|
|
$c4 = imagecolorat($src_img, $siX2, $siY); |
|
347
|
|
|
|
|
348
|
|
|
$r = (( $c1 + $c2 + $c3 + $c4 ) >> 2) & 0xFF0000; |
|
349
|
|
|
$g = ((($c1 & 0x00FF00) + ($c2 & 0x00FF00) + ($c3 & 0x00FF00) + ($c4 & 0x00FF00)) >> 2) & 0x00FF00; |
|
350
|
|
|
$b = ((($c1 & 0x0000FF) + ($c2 & 0x0000FF) + ($c3 & 0x0000FF) + ($c4 & 0x0000FF)) >> 2); |
|
351
|
|
|
|
|
352
|
|
|
} else { |
|
353
|
|
|
|
|
354
|
|
|
$c1 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX, $siY2)); |
|
355
|
|
|
$c2 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX, $siY)); |
|
356
|
|
|
$c3 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX2, $siY2)); |
|
357
|
|
|
$c4 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX2, $siY)); |
|
358
|
|
|
|
|
359
|
|
|
$r = ($c1['red'] + $c2['red'] + $c3['red'] + $c4['red'] ) << 14; |
|
360
|
|
|
$g = ($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) << 6; |
|
361
|
|
|
$b = ($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue'] ) >> 2; |
|
362
|
|
|
|
|
363
|
|
|
} |
|
364
|
|
|
imagesetpixel($dst_img, $dst_x + $x - $src_x, $dst_y + $y - $src_y, $r+$g+$b); |
|
365
|
|
|
} |
|
366
|
|
|
} |
|
367
|
|
|
return true; |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
|
|
371
|
|
|
public static function ImageCreateFunction($x_size, $y_size) { |
|
372
|
|
|
$ImageCreateFunction = 'imagecreate'; |
|
373
|
|
|
if (self::gd_version() >= 2.0) { |
|
374
|
|
|
$ImageCreateFunction = 'imagecreatetruecolor'; |
|
375
|
|
|
} |
|
376
|
|
|
if (!function_exists($ImageCreateFunction)) { |
|
377
|
|
|
return phpthumb::ErrorImage($ImageCreateFunction.'() does not exist - no GD support?'); |
|
|
|
|
|
|
378
|
|
|
} |
|
379
|
|
|
if (($x_size <= 0) || ($y_size <= 0)) { |
|
380
|
|
|
return phpthumb::ErrorImage('Invalid image dimensions: '.$ImageCreateFunction.'('.$x_size.', '.$y_size.')'); |
|
381
|
|
|
} |
|
382
|
|
|
return $ImageCreateFunction(round($x_size), round($y_size)); |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
|
|
386
|
|
|
public static function ImageCopyRespectAlpha(&$dst_im, &$src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $opacity_pct=100) { |
|
387
|
|
|
$opacipct = $opacity_pct / 100; |
|
388
|
|
|
for ($x = $src_x; $x < $src_w; $x++) { |
|
389
|
|
|
for ($y = $src_y; $y < $src_h; $y++) { |
|
390
|
|
|
$RealPixel = self::GetPixelColor($dst_im, $dst_x + $x, $dst_y + $y); |
|
391
|
|
|
$OverlayPixel = self::GetPixelColor($src_im, $x, $y); |
|
392
|
|
|
$alphapct = $OverlayPixel['alpha'] / 127; |
|
393
|
|
|
$overlaypct = (1 - $alphapct) * $opacipct; |
|
394
|
|
|
|
|
395
|
|
|
$newcolor = self::ImageColorAllocateAlphaSafe( |
|
396
|
|
|
$dst_im, |
|
397
|
|
|
$RealPixel['alpha'] == 127 ? $OverlayPixel['red'] : ($OverlayPixel['alpha'] == 127 ? $RealPixel['red'] : (round($RealPixel['red'] * (1 - $overlaypct)) + ($OverlayPixel['red'] * $overlaypct))), |
|
398
|
|
|
$RealPixel['alpha'] == 127 ? $OverlayPixel['green'] : ($OverlayPixel['alpha'] == 127 ? $RealPixel['green'] : (round($RealPixel['green'] * (1 - $overlaypct)) + ($OverlayPixel['green'] * $overlaypct))), |
|
399
|
|
|
$RealPixel['alpha'] == 127 ? $OverlayPixel['blue'] : ($OverlayPixel['alpha'] == 127 ? $RealPixel['blue'] : (round($RealPixel['blue'] * (1 - $overlaypct)) + ($OverlayPixel['blue'] * $overlaypct))), |
|
400
|
|
|
// 0); |
|
401
|
|
|
min([$RealPixel['alpha'], floor($OverlayPixel['alpha'] * $opacipct)]) |
|
402
|
|
|
); |
|
403
|
|
|
|
|
404
|
|
|
imagesetpixel($dst_im, $dst_x + $x, $dst_y + $y, $newcolor); |
|
405
|
|
|
} |
|
406
|
|
|
} |
|
407
|
|
|
return true; |
|
408
|
|
|
} |
|
409
|
|
|
|
|
410
|
|
|
|
|
411
|
|
|
public static function ProportionalResize($old_width, $old_height, $new_width=false, $new_height=false) { |
|
412
|
|
|
$old_aspect_ratio = $old_width / $old_height; |
|
413
|
|
|
if (($new_width === false) && ($new_height === false)) { |
|
414
|
|
|
return false; |
|
415
|
|
|
} elseif ($new_width === false) { |
|
416
|
|
|
$new_width = $new_height * $old_aspect_ratio; |
|
417
|
|
|
} elseif ($new_height === false) { |
|
418
|
|
|
$new_height = $new_width / $old_aspect_ratio; |
|
419
|
|
|
} |
|
420
|
|
|
$new_aspect_ratio = $new_width / $new_height; |
|
421
|
|
|
if ($new_aspect_ratio == $old_aspect_ratio) { |
|
422
|
|
|
// great, done |
|
423
|
|
|
} elseif ($new_aspect_ratio < $old_aspect_ratio) { |
|
424
|
|
|
// limited by width |
|
425
|
|
|
$new_height = $new_width / $old_aspect_ratio; |
|
426
|
|
|
} elseif ($new_aspect_ratio > $old_aspect_ratio) { |
|
427
|
|
|
// limited by height |
|
428
|
|
|
$new_width = $new_height * $old_aspect_ratio; |
|
429
|
|
|
} |
|
430
|
|
|
return array( |
|
431
|
|
|
(int) round($new_width), |
|
432
|
|
|
(int) round($new_height) |
|
433
|
|
|
); |
|
434
|
|
|
} |
|
435
|
|
|
|
|
436
|
|
|
|
|
437
|
|
|
public static function FunctionIsDisabled($function) { |
|
438
|
|
|
static $DisabledFunctions = null; |
|
439
|
|
|
if (null === $DisabledFunctions) { |
|
440
|
|
|
$disable_functions_local = explode(',', strtolower(@ini_get('disable_functions'))); |
|
441
|
|
|
$disable_functions_global = explode(',', strtolower(@get_cfg_var('disable_functions'))); |
|
442
|
|
|
foreach ($disable_functions_local as $key => $value) { |
|
443
|
|
|
$DisabledFunctions[trim($value)] = 'local'; |
|
444
|
|
|
} |
|
445
|
|
|
foreach ($disable_functions_global as $key => $value) { |
|
446
|
|
|
$DisabledFunctions[trim($value)] = 'global'; |
|
447
|
|
|
} |
|
448
|
|
|
if (@ini_get('safe_mode')) { |
|
449
|
|
|
$DisabledFunctions['shell_exec'] = 'local'; |
|
450
|
|
|
$DisabledFunctions['set_time_limit'] = 'local'; |
|
451
|
|
|
} |
|
452
|
|
|
} |
|
453
|
|
|
return isset($DisabledFunctions[strtolower($function)]); |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
|
|
457
|
|
|
public static function SafeExec($command) { |
|
458
|
|
|
static $AllowedExecFunctions = array(); |
|
459
|
|
|
if (empty($AllowedExecFunctions)) { |
|
460
|
|
|
$AllowedExecFunctions = array('shell_exec'=>true, 'passthru'=>true, 'system'=>true, 'exec'=>true); |
|
461
|
|
|
foreach ($AllowedExecFunctions as $key => $value) { |
|
462
|
|
|
$AllowedExecFunctions[$key] = !self::FunctionIsDisabled($key); |
|
463
|
|
|
} |
|
464
|
|
|
} |
|
465
|
|
|
$command .= ' 2>&1'; // force redirect stderr to stdout |
|
466
|
|
|
foreach ($AllowedExecFunctions as $execfunction => $is_allowed) { |
|
467
|
|
|
if (!$is_allowed) { |
|
468
|
|
|
continue; |
|
469
|
|
|
} |
|
470
|
|
|
$returnvalue = false; |
|
471
|
|
|
switch ($execfunction) { |
|
472
|
|
|
case 'passthru': |
|
473
|
|
|
case 'system': |
|
474
|
|
|
ob_start(); |
|
475
|
|
|
$execfunction($command); |
|
476
|
|
|
$returnvalue = ob_get_contents(); |
|
477
|
|
|
ob_end_clean(); |
|
478
|
|
|
break; |
|
479
|
|
|
|
|
480
|
|
|
case 'exec': |
|
481
|
|
|
$output = array(); |
|
482
|
|
|
$lastline = $execfunction($command, $output); |
|
|
|
|
|
|
483
|
|
|
$returnvalue = implode("\n", $output); |
|
484
|
|
|
break; |
|
485
|
|
|
|
|
486
|
|
|
case 'shell_exec': |
|
487
|
|
|
ob_start(); |
|
488
|
|
|
$returnvalue = $execfunction($command); |
|
489
|
|
|
ob_end_clean(); |
|
490
|
|
|
break; |
|
491
|
|
|
} |
|
492
|
|
|
return $returnvalue; |
|
493
|
|
|
} |
|
494
|
|
|
return false; |
|
495
|
|
|
} |
|
496
|
|
|
|
|
497
|
|
|
|
|
498
|
|
|
public static function ApacheLookupURIarray($filename) { |
|
499
|
|
|
// apache_lookup_uri() only works when PHP is installed as an Apache module. |
|
500
|
|
|
if (PHP_SAPI == 'apache') { |
|
501
|
|
|
//$property_exists_exists = function_exists('property_exists'); |
|
502
|
|
|
$keys = array('status', 'the_request', 'status_line', 'method', 'content_type', 'handler', 'uri', 'filename', 'path_info', 'args', 'boundary', 'no_cache', 'no_local_copy', 'allowed', 'send_bodyct', 'bytes_sent', 'byterange', 'clength', 'unparsed_uri', 'mtime', 'request_time'); |
|
503
|
|
|
if ($apacheLookupURIobject = @apache_lookup_uri($filename)) { |
|
504
|
|
|
$apacheLookupURIarray = array(); |
|
505
|
|
|
foreach ($keys as $key) { |
|
506
|
|
|
$apacheLookupURIarray[$key] = @$apacheLookupURIobject->$key; |
|
507
|
|
|
} |
|
508
|
|
|
return $apacheLookupURIarray; |
|
509
|
|
|
} |
|
510
|
|
|
} |
|
511
|
|
|
return false; |
|
512
|
|
|
} |
|
513
|
|
|
|
|
514
|
|
|
|
|
515
|
|
|
public static function gd_is_bundled() { |
|
516
|
|
|
static $isbundled = null; |
|
517
|
|
|
if (null === $isbundled) { |
|
518
|
|
|
$gd_info = gd_info(); |
|
519
|
|
|
$isbundled = (strpos($gd_info['GD Version'], 'bundled') !== false); |
|
520
|
|
|
} |
|
521
|
|
|
return $isbundled; |
|
522
|
|
|
} |
|
523
|
|
|
|
|
524
|
|
|
|
|
525
|
|
|
public static function gd_version($fullstring=false) { |
|
526
|
|
|
static $cache_gd_version = array(); |
|
527
|
|
|
if (empty($cache_gd_version)) { |
|
528
|
|
|
$gd_info = gd_info(); |
|
529
|
|
|
if (preg_match('#bundled \((.+)\)$#i', $gd_info['GD Version'], $matches)) { |
|
530
|
|
|
$cache_gd_version[1] = $gd_info['GD Version']; // e.g. "bundled (2.0.15 compatible)" |
|
531
|
|
|
$cache_gd_version[0] = (float) $matches[1]; // e.g. "2.0" (not "bundled (2.0.15 compatible)") |
|
532
|
|
|
} else { |
|
533
|
|
|
$cache_gd_version[1] = $gd_info['GD Version']; // e.g. "1.6.2 or higher" |
|
534
|
|
|
$cache_gd_version[0] = (float) substr($gd_info['GD Version'], 0, 3); // e.g. "1.6" (not "1.6.2 or higher") |
|
535
|
|
|
} |
|
536
|
|
|
} |
|
537
|
|
|
return $cache_gd_version[ (int) $fullstring ]; |
|
538
|
|
|
} |
|
539
|
|
|
|
|
540
|
|
|
|
|
541
|
|
|
public static function filesize_remote($remotefile, $timeout=10) { |
|
542
|
|
|
$size = false; |
|
543
|
|
|
$parsed_url = self::ParseURLbetter($remotefile); |
|
544
|
|
|
if ($fp = @fsockopen($parsed_url['host'], $parsed_url['port'], $errno, $errstr, $timeout)) { |
|
545
|
|
|
fwrite($fp, 'HEAD '.$parsed_url['path'].$parsed_url['query'].' HTTP/1.0'."\r\n".'Host: '.$parsed_url['host']."\r\n\r\n"); |
|
546
|
|
|
if (self::version_compare_replacement(PHP_VERSION, '4.3.0', '>=')) { |
|
547
|
|
|
stream_set_timeout($fp, $timeout); |
|
548
|
|
|
} |
|
549
|
|
|
while (!feof($fp)) { |
|
550
|
|
|
$headerline = fgets($fp, 4096); |
|
551
|
|
|
if (preg_match('#^Content-Length: (.*)#i', $headerline, $matches)) { |
|
552
|
|
|
$size = (int) $matches[ 1]; |
|
553
|
|
|
break; |
|
554
|
|
|
} |
|
555
|
|
|
} |
|
556
|
|
|
fclose ($fp); |
|
557
|
|
|
} |
|
558
|
|
|
return $size; |
|
559
|
|
|
} |
|
560
|
|
|
|
|
561
|
|
|
|
|
562
|
|
|
public static function filedate_remote($remotefile, $timeout=10) { |
|
563
|
|
|
$date = false; |
|
564
|
|
|
$parsed_url = self::ParseURLbetter($remotefile); |
|
565
|
|
|
if ($fp = @fsockopen($parsed_url['host'], $parsed_url['port'], $errno, $errstr, $timeout)) { |
|
566
|
|
|
fwrite($fp, 'HEAD '.$parsed_url['path'].$parsed_url['query'].' HTTP/1.0'."\r\n".'Host: '.$parsed_url['host']."\r\n\r\n"); |
|
567
|
|
|
if (self::version_compare_replacement(PHP_VERSION, '4.3.0', '>=')) { |
|
568
|
|
|
stream_set_timeout($fp, $timeout); |
|
569
|
|
|
} |
|
570
|
|
|
while (!feof($fp)) { |
|
571
|
|
|
$headerline = fgets($fp, 4096); |
|
572
|
|
|
if (preg_match('#^Last-Modified: (.*)#i', $headerline, $matches)) { |
|
573
|
|
|
$date = strtotime($matches[1]) - date('Z'); |
|
574
|
|
|
break; |
|
575
|
|
|
} |
|
576
|
|
|
} |
|
577
|
|
|
fclose ($fp); |
|
578
|
|
|
} |
|
579
|
|
|
return $date; |
|
580
|
|
|
} |
|
581
|
|
|
|
|
582
|
|
|
|
|
583
|
|
|
public static function md5_file_safe($filename) { |
|
584
|
|
|
// md5_file() doesn't exist in PHP < 4.2.0 |
|
585
|
|
|
if (function_exists('md5_file')) { |
|
586
|
|
|
return md5_file($filename); |
|
587
|
|
|
} |
|
588
|
|
|
if ($fp = @fopen($filename, 'rb')) { |
|
589
|
|
|
$rawData = ''; |
|
590
|
|
|
do { |
|
591
|
|
|
$buffer = fread($fp, 8192); |
|
592
|
|
|
$rawData .= $buffer; |
|
593
|
|
|
} while (strlen($buffer) > 0); |
|
594
|
|
|
fclose($fp); |
|
595
|
|
|
return md5($rawData); |
|
596
|
|
|
} |
|
597
|
|
|
return false; |
|
598
|
|
|
} |
|
599
|
|
|
|
|
600
|
|
|
|
|
601
|
|
|
public static function nonempty_min() { |
|
602
|
|
|
$arg_list = func_get_args(); |
|
603
|
|
|
$acceptable = array(); |
|
604
|
|
|
foreach ($arg_list as $arg) { |
|
605
|
|
|
if ($arg) { |
|
606
|
|
|
$acceptable[] = $arg; |
|
607
|
|
|
} |
|
608
|
|
|
} |
|
609
|
|
|
return min($acceptable); |
|
610
|
|
|
} |
|
611
|
|
|
|
|
612
|
|
|
|
|
613
|
|
|
public static function LittleEndian2String($number, $minbytes=1) { |
|
614
|
|
|
$intstring = ''; |
|
615
|
|
|
while ($number > 0) { |
|
616
|
|
|
$intstring .= chr($number & 255); |
|
617
|
|
|
$number >>= 8; |
|
618
|
|
|
} |
|
619
|
|
|
return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT); |
|
620
|
|
|
} |
|
621
|
|
|
|
|
622
|
|
|
public static function OneOfThese() { |
|
623
|
|
|
// return the first useful (non-empty/non-zero/non-false) value from those passed |
|
624
|
|
|
$arg_list = func_get_args(); |
|
625
|
|
|
foreach ($arg_list as $key => $value) { |
|
626
|
|
|
if ($value) { |
|
627
|
|
|
return $value; |
|
628
|
|
|
} |
|
629
|
|
|
} |
|
630
|
|
|
return false; |
|
631
|
|
|
} |
|
632
|
|
|
|
|
633
|
|
|
public static function CaseInsensitiveInArray($needle, $haystack) { |
|
634
|
|
|
$needle = strtolower($needle); |
|
635
|
|
|
foreach ($haystack as $key => $value) { |
|
636
|
|
|
if (is_array($value)) { |
|
637
|
|
|
// skip? |
|
638
|
|
|
} elseif ($needle == strtolower($value)) { |
|
639
|
|
|
return true; |
|
640
|
|
|
} |
|
641
|
|
|
} |
|
642
|
|
|
return false; |
|
643
|
|
|
} |
|
644
|
|
|
|
|
645
|
|
|
public static function URLreadFsock($host, $file, &$errstr, $successonly=true, $port=-1, $timeout=10) { |
|
646
|
|
|
if (!function_exists('fsockopen') || self::FunctionIsDisabled('fsockopen')) { |
|
647
|
|
|
$errstr = 'URLreadFsock says: function fsockopen() unavailable'; |
|
648
|
|
|
return false; |
|
649
|
|
|
} |
|
650
|
|
|
$port = (int) ($port ? $port : -1); // passing anything as the $port parameter (even empty values like null, false, 0, "") will override the default -1. fsockopen uses -1 as the default port value. |
|
651
|
|
|
//if ($fp = @fsockopen($host, $port, $errno, $errstr, $timeout)) { |
|
652
|
|
|
if ($fp = @fsockopen((($port == 443) ? 'ssl://' : '').$host, $port, $errno, $errstr, $timeout)) { // https://github.com/JamesHeinrich/phpThumb/issues/39 |
|
653
|
|
|
$out = 'GET '.$file.' HTTP/1.0'."\r\n"; |
|
654
|
|
|
$out .= 'Host: '.$host."\r\n"; |
|
655
|
|
|
$out .= 'Connection: Close'."\r\n\r\n"; |
|
656
|
|
|
fwrite($fp, $out); |
|
657
|
|
|
|
|
658
|
|
|
$isHeader = true; |
|
659
|
|
|
$data_header = ''; |
|
660
|
|
|
$data_body = ''; |
|
661
|
|
|
$header_newlocation = ''; |
|
662
|
|
|
while (!feof($fp)) { |
|
663
|
|
|
$line = fgets($fp, 1024); |
|
664
|
|
|
if ($isHeader) { |
|
665
|
|
|
$data_header .= $line; |
|
666
|
|
|
} else { |
|
667
|
|
|
$data_body .= $line; |
|
668
|
|
|
} |
|
669
|
|
|
if (preg_match('#^HTTP/[\\.\d]+ ([\d]+)\s*(.+)?$#i', rtrim($line), $matches)) { |
|
670
|
|
|
list( , $errno, $errstr) = $matches; |
|
671
|
|
|
$errno = (int) $errno; |
|
672
|
|
|
} elseif (preg_match('#^Location: (.*)$#i', rtrim($line), $matches)) { |
|
673
|
|
|
$header_newlocation = $matches[1]; |
|
674
|
|
|
} |
|
675
|
|
|
if ($isHeader && ($line == "\r\n")) { |
|
676
|
|
|
$isHeader = false; |
|
677
|
|
|
if ($successonly) { |
|
678
|
|
|
switch ($errno) { |
|
679
|
|
|
case 200: |
|
680
|
|
|
// great, continue |
|
681
|
|
|
break; |
|
682
|
|
|
|
|
683
|
|
|
default: |
|
684
|
|
|
$errstr = $errno.' '.$errstr.($header_newlocation ? '; Location: '.$header_newlocation : ''); |
|
685
|
|
|
fclose($fp); |
|
686
|
|
|
return false; |
|
687
|
|
|
break; |
|
|
|
|
|
|
688
|
|
|
} |
|
689
|
|
|
} |
|
690
|
|
|
} |
|
691
|
|
|
} |
|
692
|
|
|
fclose($fp); |
|
693
|
|
|
return $data_body; |
|
694
|
|
|
} |
|
695
|
|
|
return null; |
|
696
|
|
|
} |
|
697
|
|
|
|
|
698
|
|
|
public static function CleanUpURLencoding($url, $queryseperator='&') { |
|
699
|
|
|
if (!0 === stripos($url, "http") ) { |
|
700
|
|
|
return $url; |
|
701
|
|
|
} |
|
702
|
|
|
$parsed_url = self::ParseURLbetter($url); |
|
703
|
|
|
$pathelements = explode('/', $parsed_url['path']); |
|
704
|
|
|
$CleanPathElements = array(); |
|
705
|
|
|
$TranslationMatrix = array(' '=>'%20'); |
|
706
|
|
|
foreach ($pathelements as $key => $pathelement) { |
|
707
|
|
|
$CleanPathElements[] = strtr($pathelement, $TranslationMatrix); |
|
708
|
|
|
} |
|
709
|
|
|
foreach ($CleanPathElements as $key => $value) { |
|
710
|
|
|
if ($value === '') { |
|
711
|
|
|
unset($CleanPathElements[$key]); |
|
712
|
|
|
} |
|
713
|
|
|
} |
|
714
|
|
|
|
|
715
|
|
|
$queries = explode($queryseperator, $parsed_url['query']); |
|
716
|
|
|
$CleanQueries = array(); |
|
717
|
|
|
foreach ($queries as $key => $query) { |
|
718
|
|
|
@list($param, $value) = explode('=', $query); |
|
719
|
|
|
$CleanQueries[] = strtr($param, $TranslationMatrix).($value ? '='.strtr($value, $TranslationMatrix) : ''); |
|
720
|
|
|
} |
|
721
|
|
|
foreach ($CleanQueries as $key => $value) { |
|
722
|
|
|
if ($value === '') { |
|
723
|
|
|
unset($CleanQueries[$key]); |
|
724
|
|
|
} |
|
725
|
|
|
} |
|
726
|
|
|
|
|
727
|
|
|
$cleaned_url = $parsed_url['scheme'].'://'; |
|
728
|
|
|
$cleaned_url .= ($parsed_url['user'] ? $parsed_url['user'].($parsed_url['pass'] ? ':'.$parsed_url['pass'] : '').'@' : ''); |
|
729
|
|
|
$cleaned_url .= $parsed_url['host']; |
|
730
|
|
|
$cleaned_url .= (($parsed_url['port'] && ($parsed_url['port'] != self::URLschemeDefaultPort($parsed_url['scheme']))) ? ':'.$parsed_url['port'] : ''); |
|
731
|
|
|
$cleaned_url .= '/'.implode('/', $CleanPathElements); |
|
732
|
|
|
$cleaned_url .= (!empty($CleanQueries) ? '?'.implode($queryseperator, $CleanQueries) : ''); |
|
733
|
|
|
return $cleaned_url; |
|
734
|
|
|
} |
|
735
|
|
|
|
|
736
|
|
|
public static function URLschemeDefaultPort($scheme) { |
|
737
|
|
|
static $schemePort = array( |
|
738
|
|
|
'ftp' => 21, |
|
739
|
|
|
'http' => 80, |
|
740
|
|
|
'https' => 443, |
|
741
|
|
|
); |
|
742
|
|
|
return (isset($schemePort[strtolower($scheme)]) ? $schemePort[strtolower($scheme)] : null); |
|
743
|
|
|
} |
|
744
|
|
|
|
|
745
|
|
|
public static function ParseURLbetter($url) { |
|
746
|
|
|
$parsedURL = @parse_url($url); |
|
747
|
|
|
foreach (array('scheme', 'host', 'port', 'user', 'pass', 'path', 'query', 'fragment') as $key) { // ensure all possible array keys are always returned |
|
748
|
|
|
if (!array_key_exists($key, $parsedURL)) { |
|
749
|
|
|
$parsedURL[$key] = null; |
|
750
|
|
|
} |
|
751
|
|
|
} |
|
752
|
|
|
$parsedURL['port'] = ($parsedURL['port'] ? $parsedURL['port'] : self::URLschemeDefaultPort($parsedURL['scheme'])); |
|
753
|
|
|
return $parsedURL; |
|
754
|
|
|
} |
|
755
|
|
|
|
|
756
|
|
|
public static function SafeURLread($url, &$error, $timeout=10, $followredirects=true) { |
|
757
|
|
|
$error = ''; |
|
758
|
|
|
$errstr = ''; |
|
759
|
|
|
$rawData = ''; |
|
760
|
|
|
|
|
761
|
|
|
$parsed_url = self::ParseURLbetter($url); |
|
762
|
|
|
$alreadyLookedAtURLs[trim($url)] = true; |
|
|
|
|
|
|
763
|
|
|
|
|
764
|
|
|
while (true) { |
|
765
|
|
|
$tryagain = false; |
|
766
|
|
|
$rawData = self::URLreadFsock($parsed_url['host'], $parsed_url['path'].'?'.$parsed_url['query'], $errstr, true, $parsed_url['port'], $timeout); |
|
767
|
|
|
if ($followredirects && preg_match('#302 [a-z ]+; Location\\: (http.*)#i', $errstr, $matches)) { |
|
768
|
|
|
$matches[1] = trim(@$matches[1]); |
|
769
|
|
|
if (!@$alreadyLookedAtURLs[$matches[1]]) { |
|
770
|
|
|
// loop through and examine new URL |
|
771
|
|
|
$error .= 'URL "'.$url.'" redirected to "'.$matches[1].'"'; |
|
772
|
|
|
|
|
773
|
|
|
$tryagain = true; |
|
774
|
|
|
$alreadyLookedAtURLs[$matches[1]] = true; |
|
775
|
|
|
$parsed_url = self::ParseURLbetter($matches[ 1]); |
|
776
|
|
|
} |
|
777
|
|
|
} |
|
778
|
|
|
if (!$tryagain) { |
|
779
|
|
|
break; |
|
780
|
|
|
} |
|
781
|
|
|
} |
|
782
|
|
|
|
|
783
|
|
|
if ($rawData === false) { |
|
784
|
|
|
$error .= 'Error opening "'.$url.'":'."\n\n".$errstr; |
|
785
|
|
|
return false; |
|
786
|
|
|
} elseif ($rawData === null) { |
|
|
|
|
|
|
787
|
|
|
// fall through |
|
788
|
|
|
$error .= 'Error opening "'.$url.'":'."\n\n".$errstr; |
|
789
|
|
|
} else { |
|
790
|
|
|
return $rawData; |
|
791
|
|
|
} |
|
792
|
|
|
|
|
793
|
|
|
if (function_exists('curl_version') && !self::FunctionIsDisabled('curl_exec')) { |
|
794
|
|
|
$ch = curl_init(); |
|
795
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
796
|
|
|
curl_setopt($ch, CURLOPT_HEADER, false); |
|
797
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
798
|
|
|
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); |
|
799
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); |
|
800
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); |
|
801
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, (bool) $followredirects); |
|
802
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); |
|
803
|
|
|
$rawData = curl_exec($ch); |
|
804
|
|
|
curl_close($ch); |
|
805
|
|
|
if (strlen($rawData) > 0) { |
|
806
|
|
|
$error .= 'CURL succeeded ('.strlen($rawData).' bytes); '; |
|
807
|
|
|
return $rawData; |
|
808
|
|
|
} |
|
809
|
|
|
$error .= 'CURL available but returned no data; '; |
|
810
|
|
|
} else { |
|
811
|
|
|
$error .= 'CURL unavailable; '; |
|
812
|
|
|
} |
|
813
|
|
|
|
|
814
|
|
|
$BrokenURLfopenPHPversions = array('4.4.2'); |
|
815
|
|
|
if (in_array(PHP_VERSION, $BrokenURLfopenPHPversions)) { |
|
816
|
|
|
$error .= 'fopen(URL) broken in PHP v'. PHP_VERSION .'; '; |
|
817
|
|
|
} elseif (@ini_get('allow_url_fopen')) { |
|
818
|
|
|
$rawData = ''; |
|
819
|
|
|
$error_fopen = ''; |
|
820
|
|
|
ob_start(); |
|
821
|
|
|
if ($fp = fopen($url, 'rb')) { |
|
822
|
|
|
do { |
|
823
|
|
|
$buffer = fread($fp, 8192); |
|
824
|
|
|
$rawData .= $buffer; |
|
825
|
|
|
} while (strlen($buffer) > 0); |
|
826
|
|
|
fclose($fp); |
|
827
|
|
|
} else { |
|
828
|
|
|
$error_fopen .= trim(strip_tags(ob_get_contents())); |
|
829
|
|
|
} |
|
830
|
|
|
ob_end_clean(); |
|
831
|
|
|
$error .= $error_fopen; |
|
832
|
|
|
if (!$error_fopen) { |
|
833
|
|
|
$error .= '; "allow_url_fopen" succeeded ('.strlen($rawData).' bytes); '; |
|
834
|
|
|
return $rawData; |
|
835
|
|
|
} |
|
836
|
|
|
$error .= '; "allow_url_fopen" enabled but returned no data ('.$error_fopen.'); '; |
|
837
|
|
|
} else { |
|
838
|
|
|
$error .= '"allow_url_fopen" disabled; '; |
|
839
|
|
|
} |
|
840
|
|
|
|
|
841
|
|
|
return false; |
|
842
|
|
|
} |
|
843
|
|
|
|
|
844
|
|
|
public static function EnsureDirectoryExists($dirname, $mask = 0755) { |
|
845
|
|
|
$directory_elements = explode(DIRECTORY_SEPARATOR, $dirname); |
|
846
|
|
|
$startoffset = (!$directory_elements[0] ? 2 : 1); // unix with leading "/" then start with 2nd element; Windows with leading "c:\" then start with 1st element |
|
847
|
|
|
$open_basedirs = preg_split('#[;:]#', ini_get('open_basedir')); |
|
848
|
|
|
foreach ($open_basedirs as $key => $open_basedir) { |
|
849
|
|
|
if (preg_match('#^'.preg_quote($open_basedir).'#', $dirname) && (strlen($dirname) > strlen($open_basedir))) { |
|
850
|
|
|
$startoffset = substr_count($open_basedir, DIRECTORY_SEPARATOR) + 1; |
|
851
|
|
|
break; |
|
852
|
|
|
} |
|
853
|
|
|
} |
|
854
|
|
|
$i = $startoffset; |
|
|
|
|
|
|
855
|
|
|
$endoffset = count($directory_elements); |
|
856
|
|
|
for ($i = $startoffset; $i <= $endoffset; $i++) { |
|
857
|
|
|
$test_directory = implode(DIRECTORY_SEPARATOR, array_slice($directory_elements, 0, $i)); |
|
858
|
|
|
if (!$test_directory) { |
|
859
|
|
|
continue; |
|
860
|
|
|
} |
|
861
|
|
|
if (!@is_dir($test_directory)) { |
|
862
|
|
|
if (@file_exists($test_directory)) { |
|
863
|
|
|
// directory name already exists as a file |
|
864
|
|
|
return false; |
|
865
|
|
|
} |
|
866
|
|
|
@mkdir($test_directory, $mask); |
|
|
|
|
|
|
867
|
|
|
@chmod($test_directory, $mask); |
|
|
|
|
|
|
868
|
|
|
if (!@is_dir($test_directory) || !@is_writable($test_directory)) { |
|
869
|
|
|
return false; |
|
870
|
|
|
} |
|
871
|
|
|
} |
|
872
|
|
|
} |
|
873
|
|
|
return true; |
|
874
|
|
|
} |
|
875
|
|
|
|
|
876
|
|
|
|
|
877
|
|
|
public static function GetAllFilesInSubfolders($dirname) { |
|
878
|
|
|
$AllFiles = array(); |
|
879
|
|
|
$dirname = rtrim(realpath($dirname), '/\\'); |
|
880
|
|
|
if ($dirhandle = @opendir($dirname)) { |
|
881
|
|
|
while (($file = readdir($dirhandle)) !== false) { |
|
882
|
|
|
$fullfilename = $dirname.DIRECTORY_SEPARATOR.$file; |
|
883
|
|
|
if (is_file($fullfilename)) { |
|
884
|
|
|
$AllFiles[] = $fullfilename; |
|
885
|
|
|
} elseif (is_dir($fullfilename)) { |
|
886
|
|
|
switch ($file) { |
|
887
|
|
|
case '.': |
|
888
|
|
|
case '..': |
|
889
|
|
|
break; |
|
890
|
|
|
|
|
891
|
|
|
default: |
|
892
|
|
|
$AllFiles[] = $fullfilename; |
|
893
|
|
|
$subfiles = self::GetAllFilesInSubfolders($fullfilename); |
|
894
|
|
|
foreach ($subfiles as $filename) { |
|
895
|
|
|
$AllFiles[] = $filename; |
|
896
|
|
|
} |
|
897
|
|
|
break; |
|
898
|
|
|
} |
|
899
|
|
|
} else { |
|
900
|
|
|
// ignore? |
|
901
|
|
|
} |
|
902
|
|
|
} |
|
903
|
|
|
closedir($dirhandle); |
|
904
|
|
|
} |
|
905
|
|
|
sort($AllFiles); |
|
906
|
|
|
return array_unique($AllFiles); |
|
907
|
|
|
} |
|
908
|
|
|
|
|
909
|
|
|
|
|
910
|
|
|
public static function SanitizeFilename($filename) { |
|
911
|
|
|
$filename = preg_replace('/[^'.preg_quote(' !#$%^()+,-.;<>=@[]_{}').'a-zA-Z0-9]/', '_', $filename); |
|
912
|
|
|
if (self::version_compare_replacement(PHP_VERSION, '4.1.0', '>=')) { |
|
913
|
|
|
$filename = trim($filename, '.'); |
|
914
|
|
|
} |
|
915
|
|
|
return $filename; |
|
916
|
|
|
} |
|
917
|
|
|
|
|
918
|
|
|
public static function PasswordStrength($password) { |
|
919
|
|
|
$strength = 0; |
|
920
|
|
|
$strength += strlen(preg_replace('#[^a-z]#', '', $password)) * 0.5; // lowercase characters are weak |
|
921
|
|
|
$strength += strlen(preg_replace('#[^A-Z]#', '', $password)) * 0.8; // uppercase characters are somewhat better |
|
922
|
|
|
$strength += strlen(preg_replace('#[^0-9]#', '', $password)) * 1.0; // numbers are somewhat better |
|
923
|
|
|
$strength += strlen(preg_replace('#[a-zA-Z0-9]#', '', $password)) * 2.0; // other non-alphanumeric characters are best |
|
924
|
|
|
return $strength; |
|
925
|
|
|
} |
|
926
|
|
|
|
|
927
|
|
|
} |
|
928
|
|
|
|
|
929
|
|
|
|
|
930
|
|
|
////////////// END: class phpthumb_functions ////////////// |
|
931
|
|
|
|
|
932
|
|
|
|
|
933
|
|
|
if (!function_exists('gd_info')) { |
|
934
|
|
|
// built into PHP v4.3.0+ (with bundled GD2 library) |
|
935
|
|
|
function gd_info() { |
|
936
|
|
|
static $gd_info = array(); |
|
937
|
|
|
if (empty($gd_info)) { |
|
938
|
|
|
// based on code by johnschaefer at gmx dot de |
|
939
|
|
|
// from PHP help on gd_info() |
|
940
|
|
|
$gd_info = array( |
|
941
|
|
|
'GD Version' => '', |
|
942
|
|
|
'FreeType Support' => false, |
|
943
|
|
|
'FreeType Linkage' => '', |
|
944
|
|
|
'T1Lib Support' => false, |
|
945
|
|
|
'GIF Read Support' => false, |
|
946
|
|
|
'GIF Create Support' => false, |
|
947
|
|
|
'JPG Support' => false, |
|
948
|
|
|
'PNG Support' => false, |
|
949
|
|
|
'WBMP Support' => false, |
|
950
|
|
|
'XBM Support' => false |
|
951
|
|
|
); |
|
952
|
|
|
$phpinfo_array = phpthumb_functions::phpinfo_array(); |
|
953
|
|
|
foreach ($phpinfo_array as $line) { |
|
954
|
|
|
$line = trim(strip_tags($line)); |
|
955
|
|
|
foreach ($gd_info as $key => $value) { |
|
956
|
|
|
//if (strpos($line, $key) !== false) { |
|
957
|
|
|
if (strpos($line, $key) === 0) { |
|
958
|
|
|
$newvalue = trim(str_replace($key, '', $line)); |
|
959
|
|
|
$gd_info[$key] = $newvalue; |
|
960
|
|
|
} |
|
961
|
|
|
} |
|
962
|
|
|
} |
|
963
|
|
|
if (empty($gd_info['GD Version'])) { |
|
964
|
|
|
// probable cause: "phpinfo() disabled for security reasons" |
|
965
|
|
|
if (function_exists('imagetypes')) { |
|
966
|
|
|
$imagetypes = imagetypes(); |
|
967
|
|
|
if ($imagetypes & IMG_PNG) { |
|
968
|
|
|
$gd_info['PNG Support'] = true; |
|
969
|
|
|
} |
|
970
|
|
|
if ($imagetypes & IMG_GIF) { |
|
971
|
|
|
$gd_info['GIF Create Support'] = true; |
|
972
|
|
|
} |
|
973
|
|
|
if ($imagetypes & IMG_JPG) { |
|
974
|
|
|
$gd_info['JPG Support'] = true; |
|
975
|
|
|
} |
|
976
|
|
|
if ($imagetypes & IMG_WBMP) { |
|
977
|
|
|
$gd_info['WBMP Support'] = true; |
|
978
|
|
|
} |
|
979
|
|
|
} |
|
980
|
|
|
// to determine capability of GIF creation, try to use imagecreatefromgif on a 1px GIF |
|
981
|
|
|
if (function_exists('imagecreatefromgif')) { |
|
982
|
|
|
if ($tempfilename = phpthumb::phpThumb_tempnam()) { |
|
|
|
|
|
|
983
|
|
|
if ($fp_tempfile = @fopen($tempfilename, 'wb')) { |
|
984
|
|
|
fwrite($fp_tempfile, base64_decode('R0lGODlhAQABAIAAAH//AP///ywAAAAAAQABAAACAUQAOw==')); // very simple 1px GIF file base64-encoded as string |
|
985
|
|
|
fclose($fp_tempfile); |
|
986
|
|
|
@chmod($tempfilename, $this->getParameter('config_file_create_mask')); |
|
|
|
|
|
|
987
|
|
|
|
|
988
|
|
|
// if we can convert the GIF file to a GD image then GIF create support must be enabled, otherwise it's not |
|
989
|
|
|
$gd_info['GIF Read Support'] = (bool) @imagecreatefromgif($tempfilename); |
|
990
|
|
|
} |
|
991
|
|
|
unlink($tempfilename); |
|
992
|
|
|
} |
|
993
|
|
|
} |
|
994
|
|
|
if (function_exists('imagecreatetruecolor') && @imagecreatetruecolor(1, 1)) { |
|
995
|
|
|
$gd_info['GD Version'] = '2.0.1 or higher (assumed)'; |
|
996
|
|
|
} elseif (function_exists('imagecreate') && @imagecreate(1, 1)) { |
|
997
|
|
|
$gd_info['GD Version'] = '1.6.0 or higher (assumed)'; |
|
998
|
|
|
} |
|
999
|
|
|
} |
|
1000
|
|
|
} |
|
1001
|
|
|
return $gd_info; |
|
1002
|
|
|
} |
|
1003
|
|
|
} |
|
1004
|
|
|
|
|
1005
|
|
|
|
|
1006
|
|
|
if (!function_exists('is_executable')) { |
|
1007
|
|
|
// in PHP v3+, but v5.0+ for Windows |
|
1008
|
|
|
function is_executable($filename) { |
|
1009
|
|
|
// poor substitute, but better than nothing |
|
1010
|
|
|
return file_exists($filename); |
|
1011
|
|
|
} |
|
1012
|
|
|
} |
|
1013
|
|
|
|
|
1014
|
|
|
|
|
1015
|
|
|
if (!function_exists('preg_quote')) { |
|
1016
|
|
|
// included in PHP v3.0.9+, but may be unavailable if not compiled in |
|
1017
|
|
|
function preg_quote($string, $delimiter='\\') { |
|
1018
|
|
|
static $preg_quote_array = array(); |
|
1019
|
|
|
if (empty($preg_quote_array)) { |
|
1020
|
|
|
$escapeables = '.\\+*?[^]$(){}=!<>|:'; |
|
1021
|
|
|
for ($i = 0, $iMax = strlen($escapeables); $i < $iMax; $i++) { |
|
1022
|
|
|
$strtr_preg_quote[$escapeables[$i]] = $delimiter.$escapeables[$i]; |
|
1023
|
|
|
} |
|
1024
|
|
|
} |
|
1025
|
|
|
return strtr($string, $strtr_preg_quote); |
|
|
|
|
|
|
1026
|
|
|
} |
|
1027
|
|
|
} |
|
1028
|
|
|
|
|
1029
|
|
|
if (!function_exists('file_get_contents')) { |
|
1030
|
|
|
// included in PHP v4.3.0+ |
|
1031
|
|
|
function file_get_contents($filename) { |
|
1032
|
|
|
if (preg_match('#^(f|ht)tp\://#i', $filename)) { |
|
1033
|
|
|
return SafeURLread($filename, $error); |
|
|
|
|
|
|
1034
|
|
|
} |
|
1035
|
|
|
if ($fp = @fopen($filename, 'rb')) { |
|
1036
|
|
|
$rawData = ''; |
|
1037
|
|
|
do { |
|
1038
|
|
|
$buffer = fread($fp, 8192); |
|
1039
|
|
|
$rawData .= $buffer; |
|
1040
|
|
|
} while (strlen($buffer) > 0); |
|
1041
|
|
|
fclose($fp); |
|
1042
|
|
|
return $rawData; |
|
1043
|
|
|
} |
|
1044
|
|
|
return false; |
|
1045
|
|
|
} |
|
1046
|
|
|
} |
|
1047
|
|
|
|
|
1048
|
|
|
|
|
1049
|
|
|
if (!function_exists('file_put_contents')) { |
|
1050
|
|
|
// included in PHP v5.0.0+ |
|
1051
|
|
|
function file_put_contents($filename, $filedata) { |
|
1052
|
|
|
if ($fp = @fopen($filename, 'wb')) { |
|
1053
|
|
|
fwrite($fp, $filedata); |
|
1054
|
|
|
fclose($fp); |
|
1055
|
|
|
return true; |
|
1056
|
|
|
} |
|
1057
|
|
|
return false; |
|
1058
|
|
|
} |
|
1059
|
|
|
} |
|
1060
|
|
|
|
|
1061
|
|
|
if (!function_exists('imagealphablending')) { |
|
1062
|
|
|
// built-in function requires PHP v4.0.6+ *and* GD v2.0.1+ |
|
1063
|
|
|
function imagealphablending(&$img, $blendmode=true) { |
|
|
|
|
|
|
1064
|
|
|
// do nothing, this function is declared here just to |
|
1065
|
|
|
// prevent runtime errors if GD2 is not available |
|
1066
|
|
|
return true; |
|
1067
|
|
|
} |
|
1068
|
|
|
} |
|
1069
|
|
|
|
|
1070
|
|
|
if (!function_exists('imagesavealpha')) { |
|
1071
|
|
|
// built-in function requires PHP v4.3.2+ *and* GD v2.0.1+ |
|
1072
|
|
|
function imagesavealpha(&$img, $blendmode=true) { |
|
|
|
|
|
|
1073
|
|
|
// do nothing, this function is declared here just to |
|
1074
|
|
|
// prevent runtime errors if GD2 is not available |
|
1075
|
|
|
return true; |
|
1076
|
|
|
} |
|
1077
|
|
|
} |
|
1078
|
|
|
|
The
breakstatement is not necessary if it is preceded for example by areturnstatement:If you would like to keep this construct to be consistent with other
casestatements, you can safely mark this issue as a false-positive.