From 048139074df674d0ac82da12357d3934af3abc2e Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Thu, 27 Nov 2014 14:19:00 +0100 Subject: Add functions to modify cookies to response class Currently there is no AppFramework way to modify cookies, which makes it unusable for quite some use-cases or results in untestable code. This PR adds some basic functionalities to add and invalidate cookies. Usage: ```php $response = new TemplateResponse(...); $response->addCookie('foo', 'bar'); $response->invalidateCookie('foo'); $response->addCookie('bar', 'foo', new \DateTime('2015-01-01 00:00')); ``` Existing cookies can be accessed with the AppFramework using `$this->request->getCookie($name)`. --- lib/private/appframework/app.php | 10 +++++++++- lib/private/appframework/http/dispatcher.php | 8 +++++--- 2 files changed, 14 insertions(+), 4 deletions(-) (limited to 'lib/private') diff --git a/lib/private/appframework/app.php b/lib/private/appframework/app.php index baf52d02054..074b6cc3fd2 100644 --- a/lib/private/appframework/app.php +++ b/lib/private/appframework/app.php @@ -53,7 +53,7 @@ class App { // initialize the dispatcher and run all the middleware before the controller $dispatcher = $container['Dispatcher']; - list($httpHeaders, $responseHeaders, $output) = + list($httpHeaders, $responseHeaders, $responseCookies, $output) = $dispatcher->dispatch($controller, $methodName); if(!is_null($httpHeaders)) { @@ -64,6 +64,14 @@ class App { header($name . ': ' . $value); } + foreach($responseCookies as $name => $value) { + $expireDate = null; + if($value['expireDate'] instanceof \DateTime) { + $expireDate = $value['expireDate']->getTimestamp(); + } + setcookie($name, $value['value'], $expireDate, \OC::$WEBROOT, null, \OC::$server->getConfig()->getSystemValue('forcessl', false), true); + } + if(!is_null($output)) { header('Content-Length: ' . strlen($output)); print($output); diff --git a/lib/private/appframework/http/dispatcher.php b/lib/private/appframework/http/dispatcher.php index 29a661d5743..24540ef3c94 100644 --- a/lib/private/appframework/http/dispatcher.php +++ b/lib/private/appframework/http/dispatcher.php @@ -48,7 +48,7 @@ class Dispatcher { * @param Http $protocol the http protocol with contains all status headers * @param MiddlewareDispatcher $middlewareDispatcher the dispatcher which * runs the middleware - * @param ControllerMethodReflector the reflector that is used to inject + * @param ControllerMethodReflector $reflector the reflector that is used to inject * the arguments for the controller * @param IRequest $request the incoming request */ @@ -71,6 +71,7 @@ class Dispatcher { * @return array $array[0] contains a string with the http main header, * $array[1] contains headers in the form: $key => value, $array[2] contains * the response output + * @throws \Exception */ public function dispatch(Controller $controller, $methodName) { $out = array(null, array(), null); @@ -102,13 +103,14 @@ class Dispatcher { // get the output which should be printed and run the after output // middleware to modify the response $output = $response->render(); - $out[2] = $this->middlewareDispatcher->beforeOutput( + $out[3] = $this->middlewareDispatcher->beforeOutput( $controller, $methodName, $output); // depending on the cache object the headers need to be changed $out[0] = $this->protocol->getStatusHeader($response->getStatus(), $response->getLastModified(), $response->getETag()); - $out[1] = $response->getHeaders(); + $out[1] = array_merge($response->getHeaders()); + $out[2] = $response->getCookies(); return $out; } -- cgit v1.2.3 From d197f434757c9c21d813584122329b774487f15e Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Thu, 27 Nov 2014 14:36:11 +0100 Subject: Use server container --- lib/private/appframework/app.php | 2 +- lib/private/server.php | 9 +++++++++ lib/public/iservercontainer.php | 7 +++++++ 3 files changed, 17 insertions(+), 1 deletion(-) (limited to 'lib/private') diff --git a/lib/private/appframework/app.php b/lib/private/appframework/app.php index 074b6cc3fd2..f56ba4af870 100644 --- a/lib/private/appframework/app.php +++ b/lib/private/appframework/app.php @@ -69,7 +69,7 @@ class App { if($value['expireDate'] instanceof \DateTime) { $expireDate = $value['expireDate']->getTimestamp(); } - setcookie($name, $value['value'], $expireDate, \OC::$WEBROOT, null, \OC::$server->getConfig()->getSystemValue('forcessl', false), true); + setcookie($name, $value['value'], $expireDate, $container->getServer()->getWebRoot(), null, $container->getServer()->getConfig()->getSystemValue('forcessl', false), true); } if(!is_null($output)) { diff --git a/lib/private/server.php b/lib/private/server.php index c413ee8bf6d..e28e8362796 100644 --- a/lib/private/server.php +++ b/lib/private/server.php @@ -631,4 +631,13 @@ class Server extends SimpleContainer implements IServerContainer { function getAppManager() { return $this->query('AppManager'); } + + /** + * Get the webroot + * + * @return string + */ + function getWebRoot() { + return \OC::$WEBROOT; + } } diff --git a/lib/public/iservercontainer.php b/lib/public/iservercontainer.php index b734d1b4161..301f47c68fa 100644 --- a/lib/public/iservercontainer.php +++ b/lib/public/iservercontainer.php @@ -298,4 +298,11 @@ interface IServerContainer { * @return \OCP\App\IAppManager */ function getAppManager(); + + /** + * Get the webroot + * + * @return string + */ + function getWebRoot(); } -- cgit v1.2.3 From fef32e63ddd706a13c48111dd5b0792703becc78 Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Thu, 27 Nov 2014 14:38:38 +0100 Subject: Remove redundant code --- lib/private/server.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/private') diff --git a/lib/private/server.php b/lib/private/server.php index e28e8362796..328a2e05cae 100644 --- a/lib/private/server.php +++ b/lib/private/server.php @@ -33,15 +33,15 @@ class Server extends SimpleContainer implements IServerContainer { $this->registerService('ContactsManager', function ($c) { return new ContactsManager(); }); - $this->registerService('Request', function ($c) { + $this->registerService('Request', function (Server $c) { if (isset($c['urlParams'])) { $urlParams = $c['urlParams']; } else { $urlParams = array(); } - if (\OC::$server->getSession()->exists('requesttoken')) { - $requestToken = \OC::$server->getSession()->get('requesttoken'); + if ($c->getSession()->exists('requesttoken')) { + $requestToken = $c->getSession()->get('requesttoken'); } else { $requestToken = false; } -- cgit v1.2.3 From e35feadac2ed68f0aad911713cb3d5f8725707e6 Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Thu, 27 Nov 2014 14:50:14 +0100 Subject: Pass \OC::$WEBROOT to the ctr --- lib/base.php | 2 +- lib/private/server.php | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) (limited to 'lib/private') diff --git a/lib/base.php b/lib/base.php index 5c33be351a4..cd5d8feb1f6 100644 --- a/lib/base.php +++ b/lib/base.php @@ -466,7 +466,7 @@ class OC { } // setup the basic server - self::$server = new \OC\Server(); + self::$server = new \OC\Server(\OC::$WEBROOT); \OC::$server->getEventLogger()->log('autoloader', 'Autoloader', $loaderStart, $loaderEnd); \OC::$server->getEventLogger()->start('boot', 'Initialize'); diff --git a/lib/private/server.php b/lib/private/server.php index 328a2e05cae..59ca2a244d6 100644 --- a/lib/private/server.php +++ b/lib/private/server.php @@ -29,7 +29,15 @@ use OC\Tagging\TagMapper; * TODO: hookup all manager classes */ class Server extends SimpleContainer implements IServerContainer { - function __construct() { + /** @var string */ + private $webRoot; + + /** + * @param string $webRoot + */ + function __construct($webRoot) { + $this->webRoot = $webRoot; + $this->registerService('ContactsManager', function ($c) { return new ContactsManager(); }); @@ -233,8 +241,7 @@ class Server extends SimpleContainer implements IServerContainer { return new NullQueryLogger(); } }); - $this->registerService('TempManager', function ($c) { - /** @var Server $c */ + $this->registerService('TempManager', function (Server $c) { return new TempManager(get_temp_dir(), $c->getLogger()); }); $this->registerService('AppManager', function(Server $c) { @@ -638,6 +645,6 @@ class Server extends SimpleContainer implements IServerContainer { * @return string */ function getWebRoot() { - return \OC::$WEBROOT; + return $this->webRoot; } } -- cgit v1.2.3