-
Notifications
You must be signed in to change notification settings - Fork 1
/
imthumb-source-http.class.php
149 lines (124 loc) · 4.35 KB
/
imthumb-source-http.class.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
<?php
/**
* Remote image loader
*
* @package ImThumb
* @author Sam Pospischil <[email protected]>
* @since 2014-04-03
*/
if (!class_exists('ImThumbSource_Local')) {
require_once(IMTHUMB_BASE . '/imthumb-source-local.class.php');
}
class ImThumbSource_HTTP extends ImThumbSource_Local
{
// for size checking remote assets as they stream in
private static $curlDataWritten;
private static $curlFH;
private static $requestor;
public function readMetadata($src, ImThumb $requestor)
{
$localCacheFile = $this->cacheRemoteResource($src, $requestor);
if (!$localCacheFile) {
return new ImThumbMeta(); // could not be read, return invalid metadata
}
// :IMPORTANT: we update the path in the requestor so that it reads against the local file from here forwards
$requestor->forceSrc($localCacheFile);
return parent::readMetadata($localCacheFile, $requestor);
}
public function readResource(ImThumbMeta $meta, ImThumb $requestor)
{
if (!$meta->valid) {
return null;
}
$target = new Imagick();
$target->readImage($meta->src);
return $target;
}
//--------------------------------------------------------------------------
public function cacheRemoteResource($src, ImThumb $requestor)
{
if (!$requestor->cache) {
throw new ImThumbException('Cannot serve remote images without caching enabled', ImThumb::ERR_CACHE);
}
$localCacheFile = $requestor->cache->getDirectory() . '/' . urlencode($src);
if (!file_exists($localCacheFile)) {
// :TODO: set a timeout on local caches and reload periodically
self::$requestor = $requestor;
if (!($localCacheFile = $this->readUrl($src, $localCacheFile, $requestor))) {
return null;
}
}
return $localCacheFile;
}
protected function readUrl($url, $destFile, ImThumb $requestor)
{
$url = preg_replace('/ /', '%20', $url);
// prefer cURL, or hope that stream wrappers are enabled
if (function_exists('curl_init')) {
$this->readUrl_Curl($url, $destFile, $requestor);
} else {
$this->readUrl_StreamWrapper($url, $destFile);
}
return $destFile;
}
// mostly taken from TimThumb
private function readUrl_Curl($url, $destFile, ImThumb $requestor)
{
self::$curlFH = @fopen($destFile, 'w');
if (!self::$curlFH) {
throw new ImThumbException('Could not open cache file for remote image', ImThumb::ERR_CACHE);
}
self::$curlDataWritten = 0;
$curl = curl_init($url);
curl_setopt ($curl, CURLOPT_TIMEOUT, $requestor->param('externalRequestTimeout'));
curl_setopt ($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 Safari/534.30");
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt ($curl, CURLOPT_HEADER, 0);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($curl, CURLOPT_WRITEFUNCTION, 'ImThumbSource_HTTP::curlWrite');
@curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, true);
@curl_setopt ($curl, CURLOPT_MAXREDIRS, 10);
$curlResult = curl_exec($curl);
$httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
fclose(self::$curlFH);
curl_close($curl);
if ($httpStatus == 404) {
throw new ImThumbNotFoundException('Could not locate remote image', ImThumb::ERR_SRC_IMAGE);
}
if ($httpStatus == 302) {
throw new ImThumbNotFoundException("External Image is Redirecting. Try alternate image url", ImThumb::ERR_SRC_IMAGE);
}
if (!$curlResult) {
throw new ImThumbException("Internal cURL error fetching remote file", ImThumb::ERR_SRC_IMAGE);
}
}
// mostly taken from TimThumb
private function readUrl_StreamWrapper($url, $destFile)
{
$img = file_get_contents($url);
if ($img === false) { // some fun oldPHP for determining a 404 error in the stream wrapper
$err = error_get_last();
if (is_array($err) && $err['message']) {
$err = $err['message'];
}
if (preg_match('/404/', $err)) {
throw new ImThumbNotFoundException('Could not locate remote image', ImThumb::ERR_SRC_IMAGE);
}
return false;
}
if (!@file_put_contents($destFile, $img)) {
throw new ImThumbException('Could not open cache file for remote image', ImThumb::ERR_CACHE);
}
}
// taken straight from TimThumb, allows streaming of remote data
public static function curlWrite($h, $d)
{
fwrite(self::$curlFH, $d);
self::$curlDataWritten += strlen($d);
if (self::$curlDataWritten > self::$requestor->param('maxSize')) {
return 0;
} else {
return strlen($d);
}
}
}