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