From c043d43d09694fd7d4af70fc73ad5a96b40c8b22 Mon Sep 17 00:00:00 2001 From: Christian Blanquera Date: Tue, 15 Jan 2019 11:44:38 +0800 Subject: [PATCH] updating syntax to 2.2 standard --- .cradle.php | 43 ++++--------------------------------------- composer.json | 2 +- src/events.php | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 40 deletions(-) create mode 100644 src/events.php diff --git a/.cradle.php b/.cradle.php index ddf9a4e..fb651d3 100644 --- a/.cradle.php +++ b/.cradle.php @@ -1,44 +1,9 @@ - -use Cradle\Http\Request; -use Cradle\Http\Response; - /** - * Loads CSRF token in stage + * This file is part of a package designed for the CradlePHP Project. * - * @param *Request $request - * @param *Response $response + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. */ -$cradle->on('csrf-load', function (Request $request, Response $response) { - //render the key - $key = md5(uniqid()); - if($request->hasSession('csrf')) { - $key = $request->getSession('csrf'); - } - - $request->setSession('csrf', $key); - $response->setResults('csrf', $key); -}); - -/** - * Validates CSRF - * - * @param *Request $request - * @param *Response $response - */ -$cradle->on('csrf-validate', function (Request $request, Response $response) { - $actual = $request->getStage('csrf'); - $expected = $request->getSession('csrf'); - - //no longer needed - $request->removeSession('csrf'); - - if($actual !== $expected) { - //prepare to error - $message = 'We prevented a potential attack on our servers coming from the request you just sent us.'; - $message = $this->package('global')->translate($message); - $response->setError(true, $message); - } - //it passed -}); +require_once __DIR__ . '/src/events.php'; diff --git a/composer.json b/composer.json index a7f0fed..09eeefc 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,6 @@ "satooshi/php-coveralls": "2.0.0" }, "require": { - "cradlephp/framework": "2.*" + "cradlephp/framework": "~2.2.0" } } diff --git a/src/events.php b/src/events.php new file mode 100644 index 0000000..07c1bb6 --- /dev/null +++ b/src/events.php @@ -0,0 +1,50 @@ + +/** + * This file is part of a package designed for the CradlePHP Project. + * + * Copyright and license information can be found at LICENSE.txt + * distributed with this package. + */ + +use Cradle\Http\Request\RequestInterface; +use Cradle\Http\Response\ResponseInterface; + +/** + * Loads CSRF token in stage + * + * @param *Request $request + * @param *Response $response + */ +$this->on('csrf-load', function (RequestInterface $request, ResponseInterface $response) { + //render the key + $key = md5(uniqid()); + if($request->hasSession('csrf')) { + $key = $request->getSession('csrf'); + } + + $request->setSession('csrf', $key); + $response->setResults('csrf', $key); +}); + +/** + * Validates CSRF + * + * @param *Request $request + * @param *Response $response + */ +$this->on('csrf-validate', function (RequestInterface $request, ResponseInterface $response) { + $actual = $request->getStage('csrf'); + $expected = $request->getSession('csrf'); + + //no longer needed + $request->removeSession('csrf'); + + if($actual !== $expected) { + //prepare to error + $message = 'We prevented a potential attack on our servers coming from the request you just sent us.'; + $message = $this->package('global')->translate($message); + $response->setError(true, $message); + } + + //it passed +});