These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 2017 Simple Machines and individual contributors |
||
11 | * @license http://www.simplemachines.org/about/smf/license.php BSD |
||
12 | * |
||
13 | * @version 2.1 Beta 3 |
||
14 | */ |
||
15 | |||
16 | define('SMF', 'proxy'); |
||
17 | |||
18 | /** |
||
19 | * Class ProxyServer |
||
20 | */ |
||
21 | class ProxyServer |
||
22 | { |
||
23 | /** |
||
24 | * @var bool $enabled Whether or not this is enabled |
||
25 | */ |
||
26 | protected $enabled; |
||
27 | |||
28 | /** |
||
29 | * @var int $maxSize The maximum size for files to cache |
||
30 | */ |
||
31 | protected $maxSize; |
||
32 | |||
33 | /** |
||
34 | * @var string $secret A secret code used for hashing |
||
35 | */ |
||
36 | protected $secret; |
||
37 | |||
38 | /** |
||
39 | * @var string The cache directory |
||
40 | */ |
||
41 | protected $cache; |
||
42 | |||
43 | /** |
||
44 | * Constructor, loads up the Settings for the proxy |
||
45 | * |
||
46 | * @access public |
||
47 | */ |
||
48 | public function __construct() |
||
49 | { |
||
50 | global $image_proxy_enabled, $image_proxy_maxsize, $image_proxy_secret, $cachedir, $sourcedir; |
||
51 | |||
52 | require_once(dirname(__FILE__) . '/Settings.php'); |
||
53 | require_once($sourcedir . '/Class-CurlFetchWeb.php'); |
||
54 | |||
55 | $this->enabled = (bool) $image_proxy_enabled; |
||
56 | $this->maxSize = (int) $image_proxy_maxsize; |
||
57 | $this->secret = (string) $image_proxy_secret; |
||
58 | $this->cache = $cachedir . '/images'; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Checks whether the request is valid or not |
||
63 | * |
||
64 | * @access public |
||
65 | * @return bool Whether the request is valid |
||
66 | */ |
||
67 | public function checkRequest() |
||
68 | { |
||
69 | if (!$this->enabled) |
||
70 | return false; |
||
71 | |||
72 | // Try to create the image cache directory if it doesn't exist |
||
73 | if (!file_exists($this->cache)) |
||
74 | if (!mkdir($this->cache) || !copy(dirname($this->cache) . '/index.php', $this->cache . '/index.php')) |
||
75 | return false; |
||
76 | |||
77 | if (empty($_GET['hash']) || empty($_GET['request'])) |
||
78 | return false; |
||
79 | |||
80 | $hash = $_GET['hash']; |
||
81 | $request = $_GET['request']; |
||
82 | |||
83 | if (md5($request . $this->secret) != $hash) |
||
84 | return false; |
||
85 | |||
86 | // Attempt to cache the request if it doesn't exist |
||
87 | if (!$this->isCached($request)) |
||
88 | return $this->cacheImage($request); |
||
89 | |||
90 | return true; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Serves the request |
||
95 | * |
||
96 | * @access public |
||
97 | * @return void |
||
98 | */ |
||
99 | public function serve() |
||
100 | { |
||
101 | $request = $_GET['request']; |
||
102 | $cached_file = $this->getCachedPath($request); |
||
103 | $cached = json_decode(file_get_contents($cached_file), true); |
||
104 | |||
105 | // Is the cache expired? |
||
106 | if (!$cached || time() - $cached['time'] > (5 * 86400)) |
||
107 | { |
||
108 | @unlink($cached_file); |
||
0 ignored issues
–
show
|
|||
109 | if ($this->checkRequest()) |
||
110 | $this->serve(); |
||
111 | exit; |
||
112 | } |
||
113 | |||
114 | // Make sure we're serving an image |
||
115 | $contentParts = explode('/', !empty($cached['content_type']) ? $cached['content_type'] : ''); |
||
116 | if ($contentParts[0] != 'image') |
||
117 | exit; |
||
118 | |||
119 | header('Content-type: ' . $cached['content_type']); |
||
120 | header('Content-length: ' . $cached['size']); |
||
121 | echo base64_decode($cached['body']); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Returns the request's hashed filepath |
||
126 | * |
||
127 | * @access public |
||
128 | * @param string $request The request to get the path for |
||
129 | * @return string The hashed filepath for the specified request |
||
130 | */ |
||
131 | protected function getCachedPath($request) |
||
132 | { |
||
133 | return $this->cache . '/' . sha1($request . $this->secret); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Check whether the image exists in local cache or not |
||
138 | * |
||
139 | * @access protected |
||
140 | * @param string $request The image to check for in the cache |
||
141 | * @return bool Whether or not the requested image is cached |
||
142 | */ |
||
143 | protected function isCached($request) |
||
144 | { |
||
145 | return file_exists($this->getCachedPath($request)); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Attempts to cache the image while validating it |
||
150 | * |
||
151 | * @access protected |
||
152 | * @param string $request The image to cache/validate |
||
153 | * @return bool Whether the specified image was cached |
||
154 | */ |
||
155 | protected function cacheImage($request) |
||
156 | { |
||
157 | $dest = $this->getCachedPath($request); |
||
158 | |||
159 | $curl = new curl_fetch_web_data(array(CURLOPT_BINARYTRANSFER => 1)); |
||
160 | $request = $curl->get_url_data($request); |
||
161 | $response = $request->result(); |
||
162 | |||
163 | if (empty($response)) |
||
164 | return false; |
||
165 | |||
166 | $headers = $response['headers']; |
||
167 | |||
168 | // Make sure the url is returning an image |
||
169 | $contentParts = explode('/', !empty($headers['content-type']) ? $headers['content-type'] : ''); |
||
170 | if ($contentParts[0] != 'image') |
||
171 | return false; |
||
172 | |||
173 | // Validate the filesize |
||
174 | if ($response['size'] > ($this->maxSize * 1024)) |
||
175 | return false; |
||
176 | |||
177 | return file_put_contents($dest, json_encode(array( |
||
178 | 'content_type' => $headers['content-type'], |
||
179 | 'size' => $response['size'], |
||
180 | 'time' => time(), |
||
181 | 'body' => base64_encode($response['body']), |
||
182 | ))); |
||
183 | } |
||
184 | } |
||
185 | |||
186 | $proxy = new ProxyServer(); |
||
187 | if ($proxy->checkRequest()) |
||
188 | $proxy->serve(); |
||
189 | |||
190 | exit; |
||
191 |
If you suppress an error, we recommend checking for the error condition explicitly: