| Total Complexity | 42 | 
| Total Lines | 276 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like ProxyServer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProxyServer, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 43 | class ProxyServer  | 
            ||
| 44 | { | 
            ||
| 45 | /** @var bool $enabled Whether or not this is enabled */  | 
            ||
| 46 | protected $enabled;  | 
            ||
| 47 | |||
| 48 | /** @var int $maxSize The maximum size for files to cache */  | 
            ||
| 49 | protected $maxSize;  | 
            ||
| 50 | |||
| 51 | /** @var string $secret A secret code used for hashing */  | 
            ||
| 52 | protected $secret;  | 
            ||
| 53 | |||
| 54 | /** @var string The cache directory */  | 
            ||
| 55 | protected $cache;  | 
            ||
| 56 | |||
| 57 | /** @var int $maxDays until entries get deleted */  | 
            ||
| 58 | protected $maxDays;  | 
            ||
| 59 | |||
| 60 | /** @var int $cachedtime time object cached */  | 
            ||
| 61 | protected $cachedtime;  | 
            ||
| 62 | |||
| 63 | /** @var string $cachedtype type of object cached */  | 
            ||
| 64 | protected $cachedtype;  | 
            ||
| 65 | |||
| 66 | /** @var int $cachedsize size of object cached */  | 
            ||
| 67 | protected $cachedsize;  | 
            ||
| 68 | |||
| 69 | /** @var string $cachedbody body of object cached */  | 
            ||
| 70 | protected $cachedbody;  | 
            ||
| 71 | |||
| 72 | /**  | 
            ||
| 73 | * Constructor, loads up the Settings for the proxy  | 
            ||
| 74 | *  | 
            ||
| 75 | * @access public  | 
            ||
| 76 | */  | 
            ||
| 77 | public function __construct()  | 
            ||
| 78 | 	{ | 
            ||
| 79 | global $image_proxy_enabled, $image_proxy_maxsize, $image_proxy_secret, $cachedir, $sourcedir;  | 
            ||
| 80 | |||
| 81 | require_once(dirname(__FILE__) . '/Settings.php');  | 
            ||
| 82 | require_once($sourcedir . '/Subs.php');  | 
            ||
| 83 | |||
| 84 | // Ensure we don't trip over disabled internal functions  | 
            ||
| 85 | if (version_compare(PHP_VERSION, '8.0.0', '>='))  | 
            ||
| 86 | require_once($sourcedir . '/Subs-Compat.php');  | 
            ||
| 87 | |||
| 88 | // Make absolutely sure the cache directory is defined and writable.  | 
            ||
| 89 | if (empty($cachedir) || !is_dir($cachedir) || !is_writable($cachedir))  | 
            ||
| 90 | 		{ | 
            ||
| 91 | if (is_dir($boarddir . '/cache') && is_writable($boarddir . '/cache'))  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 92 | $cachedir = $boarddir . '/cache';  | 
            ||
| 93 | else  | 
            ||
| 94 | 			{ | 
            ||
| 95 | $cachedir = sys_get_temp_dir() . '/smf_cache_' . md5($boarddir);  | 
            ||
| 96 | @mkdir($cachedir, 0750);  | 
            ||
| 97 | }  | 
            ||
| 98 | }  | 
            ||
| 99 | |||
| 100 | // Turn off all error reporting; any extra junk makes for an invalid image.  | 
            ||
| 101 | error_reporting(0);  | 
            ||
| 102 | |||
| 103 | $this->enabled = (bool) $image_proxy_enabled;  | 
            ||
| 104 | $this->maxSize = (int) $image_proxy_maxsize;  | 
            ||
| 105 | $this->secret = (string) $image_proxy_secret;  | 
            ||
| 106 | $this->cache = $cachedir . '/images';  | 
            ||
| 107 | $this->maxDays = 5;  | 
            ||
| 108 | }  | 
            ||
| 109 | |||
| 110 | /**  | 
            ||
| 111 | * Checks whether the request is valid or not  | 
            ||
| 112 | *  | 
            ||
| 113 | * @access public  | 
            ||
| 114 | * @return bool Whether the request is valid  | 
            ||
| 115 | */  | 
            ||
| 116 | public function checkRequest()  | 
            ||
| 117 | 	{ | 
            ||
| 118 | if (!$this->enabled)  | 
            ||
| 119 | return false;  | 
            ||
| 120 | |||
| 121 | // Try to create the image cache directory if it doesn't exist  | 
            ||
| 122 | if (!file_exists($this->cache))  | 
            ||
| 123 | if (!mkdir($this->cache) || !copy(dirname($this->cache) . '/index.php', $this->cache . '/index.php'))  | 
            ||
| 124 | return false;  | 
            ||
| 125 | |||
| 126 | // Basic sanity check  | 
            ||
| 127 | $_GET['request'] = validate_iri($_GET['request']);  | 
            ||
| 128 | |||
| 129 | // We aren't going anywhere without these  | 
            ||
| 130 | if (empty($_GET['hash']) || empty($_GET['request']))  | 
            ||
| 131 | return false;  | 
            ||
| 132 | |||
| 133 | $hash = $_GET['hash'];  | 
            ||
| 134 | $request = $_GET['request'];  | 
            ||
| 135 | |||
| 136 | 		if (hash_hmac('sha1', $request, $this->secret) != $hash) | 
            ||
| 137 | return false;  | 
            ||
| 138 | |||
| 139 | // Ensure any non-ASCII characters in the URL are encoded correctly  | 
            ||
| 140 | $request = iri_to_url($request);  | 
            ||
| 141 | |||
| 142 | // Attempt to cache the request if it doesn't exist  | 
            ||
| 143 | if (!$this->isCached($request))  | 
            ||
| 144 | return $this->cacheImage($request);  | 
            ||
| 145 | |||
| 146 | return true;  | 
            ||
| 147 | }  | 
            ||
| 148 | |||
| 149 | /**  | 
            ||
| 150 | * Serves the request  | 
            ||
| 151 | *  | 
            ||
| 152 | * @access public  | 
            ||
| 153 | */  | 
            ||
| 154 | public function serve()  | 
            ||
| 155 | 	{ | 
            ||
| 156 | $request = $_GET['request'];  | 
            ||
| 157 | // Did we get an error when trying to fetch the image  | 
            ||
| 158 | $response = $this->checkRequest();  | 
            ||
| 159 | if (!$response)  | 
            ||
| 160 | 		{ | 
            ||
| 161 | // Throw a 404  | 
            ||
| 162 | send_http_status(404);  | 
            ||
| 163 | exit;  | 
            ||
| 164 | }  | 
            ||
| 165 | |||
| 166 | // We should have a cached image at this point  | 
            ||
| 167 | $cached_file = $this->getCachedPath($request);  | 
            ||
| 168 | |||
| 169 | // Read from cache if you need to...  | 
            ||
| 170 | if ($this->cachedbody === null)  | 
            ||
| 171 | 		{ | 
            ||
| 172 | $cached = json_decode(file_get_contents($cached_file), true);  | 
            ||
| 173 | $this->cachedtime = $cached['time'];  | 
            ||
| 174 | $this->cachedtype = $cached['content_type'];  | 
            ||
| 175 | $this->cachedsize = $cached['size'];  | 
            ||
| 176 | $this->cachedbody = $cached['body'];  | 
            ||
| 177 | }  | 
            ||
| 178 | |||
| 179 | $time = time();  | 
            ||
| 180 | |||
| 181 | // Is the cache expired? Delete and reload.  | 
            ||
| 182 | if ($time - $this->cachedtime > ($this->maxDays * 86400))  | 
            ||
| 183 | 		{ | 
            ||
| 184 | @unlink($cached_file);  | 
            ||
| 185 | if ($this->checkRequest())  | 
            ||
| 186 | $this->serve();  | 
            ||
| 187 | $this->redirectexit($request);  | 
            ||
| 188 | }  | 
            ||
| 189 | |||
| 190 | $eTag = '"' . substr(sha1($request) . $this->cachedtime, 0, 64) . '"';  | 
            ||
| 191 | if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) && strpos($_SERVER['HTTP_IF_NONE_MATCH'], $eTag) !== false)  | 
            ||
| 192 | 		{ | 
            ||
| 193 | send_http_status(304);  | 
            ||
| 194 | exit;  | 
            ||
| 195 | }  | 
            ||
| 196 | |||
| 197 | // Make sure we're serving an image  | 
            ||
| 198 | 		$contentParts = explode('/', !empty($this->cachedtype) ? $this->cachedtype : ''); | 
            ||
| 199 | if ($contentParts[0] != 'image')  | 
            ||
| 200 | exit;  | 
            ||
| 201 | |||
| 202 | $max_age = $time - $this->cachedtime + (5 * 86400);  | 
            ||
| 203 | 		header('content-type: ' . $this->cachedtype); | 
            ||
| 204 | 		header('content-length: ' . $this->cachedsize); | 
            ||
| 205 | 		header('cache-control: public, max-age=' . $max_age); | 
            ||
| 206 | 		header('last-modified: ' . gmdate('D, d M Y H:i:s', $this->cachedtime) . ' GMT'); | 
            ||
| 207 | 		header('etag: ' . $eTag); | 
            ||
| 208 | echo base64_decode($this->cachedbody);  | 
            ||
| 209 | }  | 
            ||
| 210 | |||
| 211 | /**  | 
            ||
| 212 | * Returns the request's hashed filepath  | 
            ||
| 213 | *  | 
            ||
| 214 | * @access public  | 
            ||
| 215 | * @param string $request The request to get the path for  | 
            ||
| 216 | * @return string The hashed filepath for the specified request  | 
            ||
| 217 | */  | 
            ||
| 218 | protected function getCachedPath($request)  | 
            ||
| 221 | }  | 
            ||
| 222 | |||
| 223 | /**  | 
            ||
| 224 | * Check whether the image exists in local cache or not  | 
            ||
| 225 | *  | 
            ||
| 226 | * @access protected  | 
            ||
| 227 | * @param string $request The image to check for in the cache  | 
            ||
| 228 | * @return bool Whether or not the requested image is cached  | 
            ||
| 229 | */  | 
            ||
| 230 | protected function isCached($request)  | 
            ||
| 231 | 	{ | 
            ||
| 232 | return file_exists($this->getCachedPath($request));  | 
            ||
| 233 | }  | 
            ||
| 234 | |||
| 235 | /**  | 
            ||
| 236 | * Attempts to cache the image while validating it  | 
            ||
| 237 | *  | 
            ||
| 238 | * Redirects to the origin if  | 
            ||
| 239 | * - the image couldn't be fetched  | 
            ||
| 240 | * - the MIME type doesn't indicate an image  | 
            ||
| 241 | * - the image is too large  | 
            ||
| 242 | *  | 
            ||
| 243 | * @access protected  | 
            ||
| 244 | * @param string $request The image to cache/validate  | 
            ||
| 245 | * @return bool Whether the specified image was cached  | 
            ||
| 246 | */  | 
            ||
| 247 | protected function cacheImage($request)  | 
            ||
| 288 | }  | 
            ||
| 289 | |||
| 290 | /**  | 
            ||
| 291 | * A helper function to redirect a request  | 
            ||
| 292 | *  | 
            ||
| 293 | * @access private  | 
            ||
| 294 | * @param string $request  | 
            ||
| 295 | */  | 
            ||
| 296 | private function redirectexit($request)  | 
            ||
| 300 | }  | 
            ||
| 301 | |||
| 302 | /**  | 
            ||
| 303 | * Delete all old entries  | 
            ||
| 304 | *  | 
            ||
| 305 | * @access public  | 
            ||
| 306 | */  | 
            ||
| 307 | public function housekeeping()  | 
            ||
| 329 | ?>  |