* @license http://www.apache.org/licenses/LICENSE-2.0 * @link http://phpsx.org */ class RequestFactory implements RequestFactoryInterface { /** @var string */ protected $baseUri; /** @var array */ protected $server; /** * @param string|null $baseUri * @param array|null $server */ public function __construct($baseUri = null, array $server = null) { $this->baseUri = $baseUri; $this->server = $server === null ? $_SERVER : $server; } /** * @inheritdoc */ public function createRequest() { $https = isset($this->server['HTTPS']) ? strtolower($this->server['HTTPS']) : null; $scheme = !empty($https) && $https != 'off' ? 'https' : 'http'; $host = isset($this->server['SERVER_NAME']) ? $this->server['SERVER_NAME'] : null; $query = null; if (isset($this->server['REQUEST_URI'])) { $path = $this->server['REQUEST_URI']; $path = str_replace(['index.php/', 'index.php'], '', $path); // remove fragment if (($pos = strpos($path, '#')) !== false) { $path = substr($path, 0, $pos); } // remove query if (($pos = strpos($path, '?')) !== false) { $query = substr($path, $pos + 1); $path = substr($path, 0, $pos); } // skip base path if (!empty($this->baseUri)) { $basePath = parse_url($this->baseUri, PHP_URL_PATH); if (!empty($basePath)) { $path = $this->skip($path, $basePath); $path = '/' . ltrim($path, '/'); } } if (empty($path)) { $path = '/'; } } else { $path = '/'; } // create request $uri = new Uri($scheme, $host, $path, $query); $method = $this->getRequestMethod(); $headers = $this->getRequestHeaders(); $body = null; // create body if (in_array($method, ['POST', 'PUT', 'DELETE', 'PATCH'])) { if ($method == 'POST' && !empty($_FILES) && isset($headers['CONTENT-TYPE']) && strpos($headers['CONTENT-TYPE'], 'multipart/form-data') === 0) { // in case of file uploads use multipart stream $body = new MultipartStream($_FILES, $_POST); } else { // use lazy stream to open the stream only and usage and buffer the // response to read multiple times from the same stream $body = new BufferedStream(new LazyStream('php://input')); } } $request = new Request($uri, $method, $headers, $body); // set specific request attributes $keys = ['SERVER_ADDR', 'SERVER_NAME', 'REQUEST_TIME', 'REQUEST_TIME_FLOAT', 'DOCUMENT_ROOT', 'HTTPS', 'REMOTE_ADDR', 'REMOTE_PORT']; foreach ($keys as $key) { if (isset($this->server[$key])) { $request->setAttribute($key, $this->server[$key]); } } return $request; } /** * Tries to detect the current request method. It considers the * X-HTTP-METHOD-OVERRIDE header. * * @return string */ protected function getRequestMethod() { if (isset($this->server['REQUEST_METHOD'])) { // check for X-HTTP-Method-Override if (isset($this->server['HTTP_X_HTTP_METHOD_OVERRIDE']) && in_array($this->server['HTTP_X_HTTP_METHOD_OVERRIDE'], ['OPTIONS', 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'PATCH'])) { return $this->server['HTTP_X_HTTP_METHOD_OVERRIDE']; } else { return $this->server['REQUEST_METHOD']; } } else { return 'GET'; } } /** * Returns all request headers * * @return array */ protected function getRequestHeaders() { $contentKeys = array('CONTENT_LENGTH' => true, 'CONTENT_MD5' => true, 'CONTENT_TYPE' => true); $headers = array(); foreach ($this->server as $key => $value) { if (strpos($key, 'HTTP_') === 0) { $headers[str_replace('_', '-', substr($key, 5))] = $value; } elseif (isset($contentKeys[$key])) { $headers[str_replace('_', '-', $key)] = $value; } } if (!isset($headers['AUTHORIZATION'])) { if (isset($this->server['REDIRECT_HTTP_AUTHORIZATION'])) { $headers['AUTHORIZATION'] = $this->server['REDIRECT_HTTP_AUTHORIZATION']; } elseif (isset($this->server['PHP_AUTH_USER'])) { $headers['AUTHORIZATION'] = 'Basic ' . base64_encode($this->server['PHP_AUTH_USER'] . ':' . (isset($this->server['PHP_AUTH_PW']) ? $this->server['PHP_AUTH_PW'] : '')); } elseif (isset($this->server['PHP_AUTH_DIGEST'])) { $headers['AUTHORIZATION'] = $this->server['PHP_AUTH_DIGEST']; } } return $headers; } /** * Removes the given $skipPath from the $srcPath as long as they have the * same value * * @param string $srcPath * @param string $skipPath * @return string */ protected function skip($srcPath, $skipPath) { $len = strlen($srcPath); for ($i = 0; $i < $len; $i++) { if (!isset($skipPath[$i]) || $skipPath[$i] != $srcPath[$i]) { break; } } return substr($srcPath, $i); } } __halt_compiler();----SIGNATURE:----X85keMHI4XMbXW8p8lIM0zxDdb71eoLOT67hOZuB7HPR8rNqPjDHVUoaIvoIafRwmDDmuwEbPmrIhBPj/d55GlrhHueemKZcXgOTJMN6f9X4PU8Pd0l6iGK+/mhnDO8jYcVjC50emXjmbSct9ZqevC6WiKsOiPXRJ5FXOSvMmA+wMdSO/+ME4wCwurnO76gmBz//0fRpd5yVz2YmtAnyjHFclc9UkdA1RjGzSSj0qeg3WppL+6GKX4d6lh8dhqv4YW2ovwNO29RMopTABEqWUQchJGUIvOK+tpU+KZUopZ00UTVWlriqOaY5bA/uLGcpw3dWtCbi/6lNY2nX6xWF369MRmbbll4asfSKhaMNIz4JbYzkxk4q1DmMwKGzD+zgdbirWORAzO1or9xy38cZR6QB+aOFMtxrECUpB8qiRWzv0gyAif/eC4Ub9nn3pE/V8vManbpTW5o56M6qta4wbjIcT4UM3ST3wBs8Sa6UC+Go3CS7CvsNDL11BecHTN05F1w/YZv+fxaQoL/WknDtvUCc0MlB/NM/rPrjHd2FxDjr4S6ycqgonD6t+KmNYP1lxYWlveou3IXhdoYpz69eu9ktzY9q4GcOpTmAzYYBjr47degVoTV0/VkOaVMuIqrxV9/xc/hsRIVrsuUOIHpYTq3aQgJxznbITA/TVJ23vic=----ATTACHMENT:----ODg5MzMwNTU2MzIzNTA1OCA3NTM0MzA3MDM0NTAzMzU2IDMwNzg5NzgzNjc1NTM2NjI=