public function resolve(Request $request, Response $response) { // Check whitelisted IP if (in_array($request->client('address'), $this->whitelist())) { return; } $request->setUri($this->template); }
public function resolve(Request $request, Response $response) { // Stop processing when previous resolvers has done something and given a response status code. if ($response->status()) { return; } $path = $request->uri('path'); // note; decode escaped URI characters into escaped shell path $path = preg_replace_callback('/%([\\dA-F]{2,2})/i', function ($matches) { return '\\' . chr(hexdec($matches[1])); }, $path); // Store original request if (empty($request->__directoryIndex)) { $request->__uri = $request->uri(); } if (stripos($path, $this->pathPrefix) === 0) { $path = substr($path, strlen($this->pathPrefix)); } if (strpos($path, '?') !== false) { $path = strstr($path, '?', true); } $path = urldecode($path); if (!$path) { $path = './'; } //------------------------------ // Emulate DirectoryIndex //------------------------------ if (is_dir($path)) { if (!is_file($path) && !isset($request->__directoryIndex)) { // Prevent redirection loop $request->__directoryIndex = true; foreach ($this->directoryIndex() as $file) { $request->setUri(preg_replace('/^\\.\\//', '', $path) . $file); // Exit whenever an index is handled successfully, this will exit. if ($this->resolve($request, $response)) { return; } } unset($request->__directoryIndex); // Nothing works, going down. if (isset($request->__uri)) { $request->setUri($request->__uri); } } } else { if (empty($request->__directoryIndex)) { $dirname = dirname($path); if ($dirname == '.') { $dirname = '/'; } if (in_array(pathinfo($path, PATHINFO_FILENAME), $this->directoryIndex())) { // extension-less if (!pathinfo($path, PATHINFO_EXTENSION) || is_file($path)) { $response->redirect($dirname); return true; } } unset($dirname); } } //------------------------------ // Virtual file handling //------------------------------ $this->createVirtualFile($path); if (is_file($path)) { try { $this->handle($path, $request, $response); } catch (ResolverException $e) { $response->status($e->statusCode()); } if (!$response->status()) { $response->status(200); } return true; } }