/** * Check Remember Me * * This method checks whether RememberMe cookie exists * If it does, get the user credentials and make it global for easy use * If not, just route the login page * * @param ServerRequestInterface $req PSR-7 Request * @param ResponseInterface $res PSR-7 Response * * @return callable */ protected function checkRememberMe(Request $req, Response $res) { dd(Cookies::get('hihi')); die; $cookie = $this->app->cookies->get($this->c['myConfig']->get('auth.remember')); if ($cookie && !$this->app->auth) { $credentials = explode('___', $cookie); // If the cookie isn't valid if (empty(trim($cookie)) || count($credentials) !== 2) { return $res->withHeader('Location', $this->app->router->pathFor('login')); } $identifier = $credentials[0]; $hashLib = $this->c->get('hash'); $token = $hashLib->hash($credentials[1]); $user = User::where('remember_identifier', $identifier)->first(); if ($user) { if ($hashLib->hashCheck($token, $user->remember_token)) { // Finally, user can login $_SESSION[$this->c['myConfig']->get('auth.session')] = $user->user_id; $this->app->auth = $user; } else { $user->removeRememberCredentials(); } } // Endif user is found in DB } // Endif the cookie is there }
protected function createRequest($env) { $method = $env["REQUEST_METHOD"]; $uri = Uri::createFromEnvironment($env); $headers = Headers::createFromEnvironment($env); $cookies = Cookies::parseHeader($headers->get("Cookie", [])); $serverParams = $env->all(); $body = new Body(fopen("php://input", "r")); return new Request($method, $uri, $headers, $cookies, $serverParams, $body); }
/** * @return bool */ public function validateSession() { $session_token = $this->_cookies->get("session_token"); if (!is_null($session_token)) { $remember_token = $this->generateRememberToken($session_token); $retrieveUser = $this->_options->get("retrieveUser"); if (is_callable($retrieveUser)) { $user = $retrieveUser($remember_token); if ($user instanceof AuthUserInterface) { $this->_user = $user; $this->_authenticated = true; return true; } } else { throw new \RuntimeException("The option 'retrieveUser' must be callable."); } } return false; }
protected function createRequest() { $env = (new Environment())->mock(["PATH_INFO" => "/index/hello/", "SCRIPT_NAME" => "/index.php"]); $method = $env["REQUEST_METHOD"]; $uri = Uri::createFromEnvironment($env); $headers = Headers::createFromEnvironment($env); $cookies = Cookies::parseHeader($headers->get("Cookie", [])); $serverParams = $env->all(); $body = new Body(fopen("php://input", "r")); return new Request($method, $uri, $headers, $cookies, $serverParams, $body); }
/** * Used by slim to render out cookies. Never retrieve response cookies within the application! */ public function getResponseCookie($key) { if (!($cookie = parent::get($key))) { return null; } $value = array_key_exists('value', $cookie) ? $cookie['value'] : null; if ($value) { $value = $this->json->encode($value); if (!in_array($key, $this->unencryptedCookies)) { $value = $this->encryption->encrypt($value); } } $cookie['value'] = $value; return $cookie; }
/** * @param array $serverParams * @return ServerRequest */ public function makeServerRequest(array $serverParams) { // I have no idea why I need to type hint here, but phpstorm is complaining /** @var $serverRequest ServerRequest */ $headers = Headers::createFromArray($serverParams); $cookies = Cookies::parseHeader($headers->get('Cookie', [])); $serverRequest = new ServerRequest($serverParams, $cookies); $serverRequest = $serverRequest->withMethod($serverParams['REQUEST_METHOD']); $serverRequest = $serverRequest->withUri(); foreach ($headers->all() as $header => $value) { $serverRequest = $serverRequest->withHeader($header, $value); } $serverRequest = $serverRequest->withUploadedFiles(UploadedFile::parseUploadedFiles($_FILES)); return $serverRequest; }
/** * Make request * * @param array $globals The $_SERVER super-global * @return \Psr\Http\Message\ServerRequestInterface */ public function makeRequest(array $globals) : ServerRequestInterface { $method = $globals['REQUEST_METHOD']; $uri = $this->makeUri($globals); $headers = $this->makeHeaders($globals); $cookies = Cookies::parseHeader($headers->get('Cookie', [])); $body = $this->makeBody(); $files = []; // TODO: Create factory method for uploaded files $request = new Request($method, $uri, $headers, $cookies, $globals, $body, $files); if ($method === 'POST' && in_array($request->getMediaType(), ['application/x-www-form-urlencoded', 'multipart/form-data'])) { // parsed body must be $_POST $request = $request->withParsedBody($_POST); } return $request; }
public static function fromGlobals(array $server = null, array $query = null, array $body = null, array $cookies = null, array $files = null) { $environment = new Environment($server); $method = $environment['REQUEST_METHOD']; $uri = Uri::createFromEnvironment($environment); $headers = Headers::createFromEnvironment($environment); $cookies = Cookies::parseHeader($headers->get('Cookie', [])); $serverParams = $environment->all(); $body = new RequestBody(); $uploadedFiles = UploadedFile::createFromEnvironment($environment); $request = new ServerRequest($method, $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles); if ($method === 'POST' && in_array($request->getMediaType(), ['application/x-www-form-urlencoded', 'multipart/form-data'])) { // parsed body must be $_POST $request = $request->withParsedBody($_POST); } return $request; }
/** * Creates a new request object from the data of a reactPHP request object * * @param \React\Http\Request $request ReactPHP native request object * @param string $body Content of received call * * @return \Slim\Http\Request */ public static function createFromReactRequest(\React\Http\Request $request, $body = '') { $slimHeads = new Headers(); $cookies = []; $host = ['', 80]; foreach ($request->getHeaders() as $reactHeadKey => $reactHead) { $slimHeads->add($reactHeadKey, $reactHead); switch ($reactHeadKey) { case 'Host': $host = static::getHost($reactHead); break; case 'Cookie': $cookies = Cookies::parseHeader($reactHead); break; } } $slimUri = new Uri('http', $host[0], (int) $host[1], $request->getPath(), $request->getQuery()); $serverParams = $_SERVER; $serverParams['SERVER_PROTOCOL'] = 'HTTP/' . $request->getHttpVersion(); $slimBody = static::getBody($body); return new Request($request->getMethod(), $slimUri, $slimHeads, $cookies, $serverParams, $slimBody); }
/** * DEPRECATION WARNING! Access `cookies` property directly. * * Delete cookie * * Instead of using PHP's `setcookie()` function, Slim manually constructs the HTTP `Set-Cookie` * header on its own and delegates this responsibility to the `Slim_Http_Util` class. This * response's header is passed by reference to the utility class and is directly modified. By not * relying on PHP's native implementation, Slim allows middleware the opportunity to massage or * analyze the raw header before the response is ultimately delivered to the HTTP client. * * This method will set a cookie with the given name that has an expiration time in the past; this will * prompt the HTTP client to invalidate and remove the client-side cookie. Optionally, you may * also pass a key/value array as the second argument. If the "domain" key is present in this * array, only the Cookie with the given name AND domain will be removed. The invalidating cookie * sent with this response will adopt all properties of the second argument. * * @param string $name The name of the cookie * @param array $settings Properties for cookie including: value, expire, path, domain, secure, httponly */ public function deleteCookie($name, $settings = array()) { $this->cookies->remove($name, $settings); // Util::deleteCookieHeader($this->header, $name, $value); }
public static function createFromGlobals(array $globals) { $env = new Collection($globals); $method = $env->get('REQUEST_METHOD'); $uri = Uri::createFromGlobals($globals); $headers = Headers::createFromGlobals($globals); $cookies = Cookies::parseHeader($headers->get('Cookie', [])); $serverParams = $globals; $body = new RequestBody(); $uploadedFiles = UploadedFile::createFromGlobals($globals); $request = new static($method, $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles); if ($method === 'POST' && in_array($request->getMediaType(), ['application/x-www-form-urlencoded', 'multipart/form-data'])) { // parsed body must be $_POST $request = $request->withParsedBody($_POST); } return $request; }
$modelName = $c->get('model_name'); if ($modelName) { $modelFullName = 'App\\models\\' . $modelName; return new $modelFullName($c); } return false; }); // pagination $container['pagination'] = function ($c) { $settings = $c->get('settings')['pagination']; return new \App\lib\Pagination($settings['per_nums'], $settings['page_display_nums']); }; // cookie $container['cookie'] = function ($c) { $request = $c->get('request'); $parseCookies = \Slim\Http\Cookies::parseHeader($request->getHeader('cookie')); $cookie = new \Slim\Http\Cookies($parseCookies); return $cookie; }; // session $container['session'] = function ($c) { session_start(); return new \App\lib\Session(); }; // flash $container['flash'] = function ($c) { return new \App\lib\Flash($c['session']); }; // user $container['user'] = function ($c) { $user = ['id' => 0, 'name' => 'guest'];
/** * {@inheritdoc} * * @return $this */ public function setDefaults(array $settings) { parent::setDefaults($settings); return $this; }