Passed
Pull Request — release-2.1 (#5778)
by Fran
03:34
created

proxy.php (4 issues)

1
<?php
2
3
/**
4
 * This is a lightweight proxy for serving images, generally meant to be used alongside SSL
5
 *
6
 * Simple Machines Forum (SMF)
7
 *
8
 * @package SMF
9
 * @author Simple Machines http://www.simplemachines.org
10
 * @copyright 2019 Simple Machines and individual contributors
11
 * @license http://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1 RC2
14
 */
15
16
if (!defined('SMF'))
17
	define('SMF', 'PROXY');
18
19
if (!defined('SMF_VERSION'))
20
	define('SMF_VERSION', '2.1 RC2');
21
22
if (!defined('SMF_FULL_VERSION'))
23
	define('SMF_FULL_VERSION', 'SMF ' . SMF_VERSION);
24
25
if (!defined('SMF_SOFTWARE_YEAR'))
26
	define('SMF_SOFTWARE_YEAR', '2019');
27
28
if (!defined('JQUERY_VERSION'))
29
	define('JQUERY_VERSION', '3.4.1');
30
	
31
/**
32
 * Class ProxyServer
33
 */
34
class ProxyServer
35
{
36
	/** @var bool $enabled Whether or not this is enabled */
37
	protected $enabled;
38
39
	/** @var int $maxSize The maximum size for files to cache */
40
	protected $maxSize;
41
42
	/** @var string $secret A secret code used for hashing */
43
	protected $secret;
44
45
	/** @var string The cache directory */
46
	protected $cache;
47
48
	/** @var int $maxDays until entries get deleted */
49
	protected $maxDays;
50
51
	/** @var int $cachedtime time object cached */
52
	protected $cachedtime;
53
54
	/** @var string $cachedtype type of object cached */
55
	protected $cachedtype;
56
57
	/** @var int $cachedsize size of object cached */
58
	protected $cachedsize;
59
60
	/** @var string $cachedbody body of object cached */
61
	protected $cachedbody;
62
63
	/**
64
	 * Constructor, loads up the Settings for the proxy
65
	 *
66
	 * @access public
67
	 */
68
	public function __construct()
69
	{
70
		global $image_proxy_enabled, $image_proxy_maxsize, $image_proxy_secret, $cachedir, $sourcedir;
71
72
		require_once(dirname(__FILE__) . '/Settings.php');
73
		require_once($sourcedir . '/Subs.php');
74
75
		// Turn off all error reporting; any extra junk makes for an invalid image.
76
		error_reporting(0);
77
78
		$this->enabled = (bool) $image_proxy_enabled;
79
		$this->maxSize = (int) $image_proxy_maxsize;
80
		$this->secret = (string) $image_proxy_secret;
81
		$this->cache = $cachedir . '/images';
82
		$this->maxDays = 5;
83
	}
84
85
	/**
86
	 * Checks whether the request is valid or not
87
	 *
88
	 * @access public
89
	 * @return bool Whether the request is valid
90
	 */
91
	public function checkRequest()
92
	{
93
		if (!$this->enabled)
94
			return false;
95
96
		// Try to create the image cache directory if it doesn't exist
97
		if (!file_exists($this->cache))
98
			if (!mkdir($this->cache) || !copy(dirname($this->cache) . '/index.php', $this->cache . '/index.php'))
99
				return false;
100
101
		// Basic sanity check
102
		$_GET['request'] = validate_iri($_GET['request']);
103
104
		// We aren't going anywhere without these
105
		if (empty($_GET['hash']) || empty($_GET['request']))
106
			return false;
107
108
		$hash = $_GET['hash'];
109
		$request = $_GET['request'];
110
111
		if (hash_hmac('sha1', $request, $this->secret) != $hash)
112
			return false;
113
114
		// Ensure any non-ASCII characters in the URL are encoded correctly
115
		$request = iri_to_url($request);
116
117
		// Attempt to cache the request if it doesn't exist
118
		if (!$this->isCached($request))
119
			return $this->cacheImage($request);
120
121
		return false;
122
	}
123
124
	/**
125
	 * Serves the request
126
	 *
127
	 * @access public
128
	 */
129
	public function serve()
130
	{
131
		$request = $_GET['request'];
132
		// Did we get an error when trying to fetch the image
133
		$response = $this->checkRequest();
134
		if (!$response)
135
		{
136
			// Throw a 404
137
			send_http_status(404);
138
			exit;
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
139
		}
140
141
		// We should have a cached image at this point
142
		$cached_file = $this->getCachedPath($request);
143
144
		// Read from cache if you need to...
145
		if ($this->cachedbody === null)
146
		{
147
			$cached = json_decode(file_get_contents($cached_file), true);
148
			$this->cachedtime = $cached['time'];
149
			$this->cachedtype = $cached['content_type'];
150
			$this->cachedsize = $cached['size'];
151
			$this->cachedbody = $cached['body'];
152
		}
153
154
		$time = time();
155
156
		// Is the cache expired? Delete and reload.
157
		if ($time - $this->cachedtime > ($this->maxDays * 86400))
158
		{
159
			@unlink($cached_file);
160
			if ($this->checkRequest())
161
				$this->serve();
162
			$this->redirectexit($request);
163
		}
164
165
		$eTag = '"' . substr(sha1($request) . $this->cachedtime, 0, 64) . '"';
166
		if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) && strpos($_SERVER['HTTP_IF_NONE_MATCH'], $eTag) !== false)
167
		{
168
			send_http_status(304);
169
			exit;
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
170
		}
171
172
		// Make sure we're serving an image
173
		$contentParts = explode('/', !empty($this->cachedtype) ? $this->cachedtype : '');
174
		if ($contentParts[0] != 'image')
175
			exit;
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
176
177
		$max_age = $time - $this->cachedtime + (5 * 86400);
178
		header('content-type: ' . $this->cachedtype);
179
		header('content-length: ' . $this->cachedsize);
180
		header('cache-control: public, max-age=' . $max_age);
181
		header('last-modified: ' . gmdate('D, d M Y H:i:s', $this->cachedtime) . ' GMT');
182
		header('etag: ' . $eTag);
183
		echo base64_decode($this->cachedbody);
184
	}
185
186
	/**
187
	 * Returns the request's hashed filepath
188
	 *
189
	 * @access public
190
	 * @param string $request The request to get the path for
191
	 * @return string The hashed filepath for the specified request
192
	 */
193
	protected function getCachedPath($request)
194
	{
195
		return $this->cache . '/' . sha1($request . $this->secret);
196
	}
197
198
	/**
199
	 * Check whether the image exists in local cache or not
200
	 *
201
	 * @access protected
202
	 * @param string $request The image to check for in the cache
203
	 * @return bool Whether or not the requested image is cached
204
	 */
205
	protected function isCached($request)
206
	{
207
		return file_exists($this->getCachedPath($request));
208
	}
209
210
	/**
211
	 * Attempts to cache the image while validating it
212
	 *
213
	 * Redirects to the origin if
214
	 *    - the image couldn't be fetched
215
	 *    - the MIME type doesn't indicate an image
216
	 *    - the image is too large
217
	 *
218
	 * @access protected
219
	 * @param string $request The image to cache/validate
220
	 * @return bool Whether the specified image was cached
221
	 */
222
	protected function cacheImage($request)
223
	{
224
		$dest = $this->getCachedPath($request);
225
		$ext = strtolower(pathinfo(parse_url($request, PHP_URL_PATH), PATHINFO_EXTENSION));
226
227
		$image = fetch_web_data($request);
228
229
		// Looks like nobody was home
230
		if (empty($image))
231
			$this->redirectexit($request);
232
233
		// What kind of file did they give us?
234
		$finfo = finfo_open(FILEINFO_MIME_TYPE);
235
		$mime_type = finfo_buffer($finfo, $image);
236
237
		// SVG needs a little extra care
238
		if ($ext == 'svg' && $mime_type == 'text/plain')
239
			$mime_type = 'image/svg+xml';
240
241
		// Make sure the url is returning an image
242
		if (strpos($mime_type, 'image/') !== 0)
243
			$this->redirectexit($request);
244
245
		// Validate the filesize
246
		$size = strlen($image);
247
		if ($size > ($this->maxSize * 1024))
248
			$this->redirectexit($request);
249
250
		// Populate object for current serve execution (so you don't have to read it again...)
251
		$this->cachedtime = time();
252
		$this->cachedtype = $mime_type;
253
		$this->cachedsize = $size;
254
		$this->cachedbody = base64_encode($image);
255
256
		// Cache it for later
257
		return file_put_contents($dest, json_encode(array(
258
			'content_type' => $this->cachedtype,
259
			'size' => $this->cachedsize,
260
			'time' => $this->cachedtime,
261
			'body' => $this->cachedbody,
262
		))) !== false;
263
	}
264
265
	/**
266
	 * A helper function to redirect a request
267
	 *
268
	 * @access private
269
	 * @param string $request
270
	 */
271
	private function redirectexit($request)
272
	{
273
		header('Location: ' . un_htmlspecialchars($request), false, 301);
274
		exit;
275
	}
276
277
	/**
278
	 * Delete all old entries
279
	 *
280
	 * @access public
281
	 */
282
	public function housekeeping()
283
	{
284
		$path = $this->cache . '/';
285
		if ($handle = opendir($path))
286
		{
287
			while (false !== ($file = readdir($handle)))
288
			{
289
				$filelastmodified = filemtime($path . $file);
290
291
				if ((time() - $filelastmodified) > ($this->maxDays * 86400))
292
				{
293
					unlink($path . $file);
294
				}
295
			}
296
297
			closedir($handle);
298
		}
299
	}
300
}
301
302
if (SMF == 'PROXY')
0 ignored issues
show
The condition SMF == 'PROXY' is always false.
Loading history...
303
{
304
	$proxy = new ProxyServer();
305
	$proxy->serve();
306
}
307
308
?>