/** * @param string $secret * @param string $verifyToken * @param ServerRequestInterface|null $request */ public function __construct($secret, $verifyToken, ServerRequestInterface $request = null, EventDispatcherInterface $dispatcher = null) { $this->secret = $secret; $this->verifyToken = $verifyToken; $this->request = $request ?: ServerRequest::fromGlobals(); $this->dispatcher = $dispatcher ?: new EventDispatcher(); }
public function testFromGlobals() { $_SERVER = ['PHP_SELF' => '/blog/article.php', 'GATEWAY_INTERFACE' => 'CGI/1.1', 'SERVER_ADDR' => 'Server IP: 217.112.82.20', 'SERVER_NAME' => 'www.blakesimpson.co.uk', 'SERVER_SOFTWARE' => 'Apache/2.2.15 (Win32) JRun/4.0 PHP/5.2.13', 'SERVER_PROTOCOL' => 'HTTP/1.0', 'REQUEST_METHOD' => 'POST', 'REQUEST_TIME' => 'Request start time: 1280149029', 'QUERY_STRING' => 'id=10&user=foo', 'DOCUMENT_ROOT' => '/path/to/your/server/root/', 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', 'HTTP_ACCEPT_ENCODING' => 'gzip,deflate', 'HTTP_ACCEPT_LANGUAGE' => 'en-gb,en;q=0.5', 'HTTP_CONNECTION' => 'keep-alive', 'HTTP_HOST' => 'www.blakesimpson.co.uk', 'HTTP_REFERER' => 'http://previous.url.com', 'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 ( .NET CLR 3.5.30729)', 'HTTPS' => '1', 'REMOTE_ADDR' => '193.60.168.69', 'REMOTE_HOST' => 'Client server\'s host name', 'REMOTE_PORT' => '5390', 'SCRIPT_FILENAME' => '/path/to/this/script.php', 'SERVER_ADMIN' => '*****@*****.**', 'SERVER_PORT' => '80', 'SERVER_SIGNATURE' => 'Version signature: 5.123', 'SCRIPT_NAME' => '/blog/article.php', 'REQUEST_URI' => '/blog/article.php?id=10&user=foo']; $_COOKIE = ['logged-in' => 'yes!']; $_POST = ['name' => 'Pesho', 'email' => '*****@*****.**']; $_GET = ['id' => 10, 'user' => 'foo']; $_FILES = ['file' => ['name' => 'MyFile.txt', 'type' => 'text/plain', 'tmp_name' => '/tmp/php/php1h4j1o', 'error' => UPLOAD_ERR_OK, 'size' => 123]]; $server = ServerRequest::fromGlobals(); $this->assertEquals('POST', $server->getMethod()); $this->assertEquals(['Host' => ['www.blakesimpson.co.uk']], $server->getHeaders()); $this->assertEquals('', (string) $server->getBody()); $this->assertEquals('1.0', $server->getProtocolVersion()); $this->assertEquals($_COOKIE, $server->getCookieParams()); $this->assertEquals($_POST, $server->getParsedBody()); $this->assertEquals($_GET, $server->getQueryParams()); $this->assertEquals(new Uri('http://www.blakesimpson.co.uk/blog/article.php?id=10&user=foo'), $server->getUri()); $expectedFiles = ['file' => new UploadedFile('/tmp/php/php1h4j1o', 123, UPLOAD_ERR_OK, 'MyFile.txt', 'text/plain')]; $this->assertEquals($expectedFiles, $server->getUploadedFiles()); }
/** * Get DatatableRequest populated from superglobal arrays * @return DatatableRequestInterface */ public static function fromGlobals() { return new static(ServerRequest::fromGlobals()); }
/** * request * * @return ServerRequest */ public function request() { return ServerRequest::fromGlobals(); }
<?php use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\ServerRequest; use HansOtt\PSR7Cookies\SetCookie; use HansOtt\PSR7Cookies\Signer\Key; use HansOtt\PSR7Cookies\RequestCookies; use HansOtt\PSR7Cookies\Signer\Hmac\Sha256; require_once __DIR__ . '/../vendor/autoload.php'; date_default_timezone_set('UTC'); $serverRequest = ServerRequest::fromGlobals(); $requestCookies = RequestCookies::createFromRequest($serverRequest); $signer = new Sha256(); $key = new Key('LAp27106kAgG14u74t5kb^AYrW4^5ih$'); $counter = 0; if ($requestCookies->has('counter')) { $counterCookie = $requestCookies->get('counter'); try { $counterCookie = $signer->verify($counterCookie, $key); $counter = (int) $counterCookie->getValue(); } catch (\HansOtt\PSR7Cookies\Signer\Mismatch $e) { } $counter++; } $setCounterCookie = SetCookie::thatStaysForever('counter', $counter); $setCounterCookie = $signer->sign($setCounterCookie, $key); $response = new Response(); $body = \GuzzleHttp\Psr7\stream_for(sprintf('Counter: %d', $counter)); $response = $response->withBody($body); $response = $setCounterCookie->addToResponse($response); header(sprintf('HTTP/%s %s %s', $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase()));