forked from SimpleMachines/SMF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.php
172 lines (144 loc) · 3.9 KB
/
proxy.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
<?php
/**
* This is a lightweight proxy for serving images, generally meant to be used alongside SSL
*
* Simple Machines Forum (SMF)
*
* @package SMF
* @author Simple Machines http://www.simplemachines.org
* @copyright 2014 Simple Machines and individual contributors
* @license http://www.simplemachines.org/about/smf/license.php BSD
*
* @version 2.1 Beta 1
*/
define('SMF', 'proxy');
class ProxyServer
{
protected $enabled;
protected $maxSize;
protected $secret;
protected $cache;
/**
* Constructor, loads up the Settings for the proxy
*
* @access public
*/
public function __construct()
{
global $image_proxy_enabled, $image_proxy_maxsize, $image_proxy_secret, $cachedir, $sourcedir;
require_once(dirname(__FILE__) . '/Settings.php');
require_once($sourcedir . '/Class-CurlFetchWeb.php');
$this->enabled = (bool) $image_proxy_enabled;
$this->maxSize = (int) $image_proxy_maxsize;
$this->secret = (string) $image_proxy_secret;
$this->cache = $cachedir . '/images';
}
/**
* Checks whether the request is valid or not
*
* @access public
* @return bool
*/
public function checkRequest()
{
if (!$this->enabled)
return false;
// Try to create the image cache directory if it doesn't exist
if (!file_exists($this->cache))
if (!mkdir($this->cache) || !copy(dirname($this->cache) . '/index.php', $this->cache . '/index.php'))
return false;
if (empty($_GET['hash']) || empty($_GET['request']))
return false;
$hash = $_GET['hash'];
$request = $_GET['request'];
if (md5($request . $this->secret) != $hash)
return false;
// Attempt to cache the request if it doesn't exist
if (!$this->isCached($request))
return $this->cacheImage($request);
return true;
}
/**
* Serves the request
*
* @access public
* @return void
*/
public function serve()
{
$request = $_GET['request'];
$cached_file = $this->getCachedPath($request);
$cached = json_decode(file_get_contents($cached_file), true);
// Is the cache expired?
if (!$cached || time() - $cached['time'] > (5 * 86400))
{
@unlink($cached_file);
if ($this->checkRequest())
$this->serve();
exit;
}
// Make sure we're serving an image
$contentParts = explode('/', !empty($cached['content-type']) ? $cached['content-type'] : '');
if ($contentParts[0] != 'image')
exit;
header('Content-type: ' . $cached['content_type']);
header('Content-length: ' . $cached['size']);
echo base64_decode($cached['body']);
}
/**
* Returns the request's hashed filepath
*
* @access public
* @param string $request
* @return string
*/
protected function getCachedPath($request)
{
return $this->cache . '/' . sha1($request . $this->secret);
}
/**
* Check whether the image exists in local cache or not
*
* @access protected
* @param string $request
* @return bool
*/
protected function isCached($request)
{
return file_exists($this->getCachedPath($request));
}
/**
* Attempts to cache the image while validating it
*
* @access protected
* @param string
* @return bool
*/
protected function cacheImage($request)
{
$dest = $this->getCachedPath($request);
$curl = new curl_fetch_web_data(array(CURLOPT_BINARYTRANSFER => 1));
$request = $curl->get_url_data($request);
$response = $request->result();
if (empty($response))
return false;
$headers = $response['headers'];
// Make sure the url is returning an image
$contentParts = explode('/', !empty($headers['content-type']) ? $headers['content-type'] : '');
if ($contentParts[0] != 'image')
return false;
// Validate the filesize
if ($response['size'] > ($this->maxSize * 1024))
return false;
return file_put_contents($dest, json_encode(array(
'content_type' => $headers['content-type'],
'size' => $response['size'],
'time' => time(),
'body' => base64_encode($response['body']),
)));
}
}
$proxy = new ProxyServer();
if ($proxy->checkRequest())
$proxy->serve();
exit;