diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2013-10-16 21:15:25 +0200 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2013-10-16 21:15:25 +0200 |
commit | 221a650815800e47d6b2c9d7dfe0e9434ebc9edf (patch) | |
tree | 1da8fba37b07bbfd600f98039335a612c351972e /lib | |
parent | 852a50aa8968560738f61e32d8b1cb7f5cd329c1 (diff) | |
parent | 2fe9294c5d65e7e2264a8ddf10985bdd00c83a5a (diff) | |
download | nextcloud-server-221a650815800e47d6b2c9d7dfe0e9434ebc9edf.tar.gz nextcloud-server-221a650815800e47d6b2c9d7dfe0e9434ebc9edf.zip |
Merge branch 'master' into activities-api
Conflicts:
lib/private/server.php
Diffstat (limited to 'lib')
18 files changed, 139 insertions, 313 deletions
diff --git a/lib/base.php b/lib/base.php index 9068fe76981..7bebb5f93a0 100644 --- a/lib/base.php +++ b/lib/base.php @@ -69,8 +69,9 @@ class OC { * check if owncloud runs in cli mode */ public static $CLI = false; - /* - * OC router + + /** + * @var OC_Router */ protected static $router = null; @@ -343,6 +344,9 @@ class OC { return OC_Config::getValue('session_lifetime', 60 * 60 * 24); } + /** + * @return OC_Router + */ public static function getRouter() { if (!isset(OC::$router)) { OC::$router = new OC_Router(); diff --git a/lib/private/appframework/app.php b/lib/private/appframework/app.php index 7ff55bb809d..6d3effbf1fa 100644 --- a/lib/private/appframework/app.php +++ b/lib/private/appframework/app.php @@ -42,12 +42,9 @@ class App { * @param string $controllerName the name of the controller under which it is * stored in the DI container * @param string $methodName the method that you want to call - * @param array $urlParams an array with variables extracted from the routes * @param DIContainer $container an instance of a pimple container. */ - public static function main($controllerName, $methodName, array $urlParams, - IAppContainer $container) { - $container['urlParams'] = $urlParams; + public static function main($controllerName, $methodName, IAppContainer $container) { $controller = $container[$controllerName]; // initialize the dispatcher and run all the middleware before the controller diff --git a/lib/private/appframework/core/api.php b/lib/private/appframework/core/api.php index 39522ee3dd5..e7269373bb0 100644 --- a/lib/private/appframework/core/api.php +++ b/lib/private/appframework/core/api.php @@ -100,89 +100,6 @@ class API implements IApi{ /** - * Returns the translation object - * @return \OC_L10N the translation object - */ - public function getTrans(){ - # TODO: use public api - return \OC_L10N::get($this->appName); - } - - - /** - * Returns the URL for a route - * @param string $routeName the name of the route - * @param array $arguments an array with arguments which will be filled into the url - * @return string the url - */ - public function linkToRoute($routeName, $arguments=array()){ - return \OCP\Util::linkToRoute($routeName, $arguments); - } - - - /** - * Returns an URL for an image or file - * @param string $file the name of the file - * @param string $appName the name of the app, defaults to the current one - */ - public function linkTo($file, $appName=null){ - if($appName === null){ - $appName = $this->appName; - } - return \OCP\Util::linkTo($appName, $file); - } - - - /** - * Returns the link to an image, like link to but only with prepending img/ - * @param string $file the name of the file - * @param string $appName the name of the app, defaults to the current one - */ - public function imagePath($file, $appName=null){ - if($appName === null){ - $appName = $this->appName; - } - return \OCP\Util::imagePath($appName, $file); - } - - - /** - * Makes an URL absolute - * @param string $url the url - * @return string the absolute url - */ - public function getAbsoluteURL($url){ - # TODO: use public api - return \OC_Helper::makeURLAbsolute($url); - } - - - /** - * links to a file - * @param string $file the name of the file - * @param string $appName the name of the app, defaults to the current one - * @deprecated replaced with linkToRoute() - * @return string the url - */ - public function linkToAbsolute($file, $appName=null){ - if($appName === null){ - $appName = $this->appName; - } - return \OCP\Util::linkToAbsolute($appName, $file); - } - - - /** - * Checks if the CSRF check was correct - * @return bool true if CSRF check passed - */ - public function passesCSRFCheck(){ - # TODO: use public api - return \OC_Util::isCallRegistered(); - } - - - /** * Checks if an app is enabled * @param string $appName the name of an app * @return bool true if app is enabled @@ -193,44 +110,6 @@ class API implements IApi{ /** - * Writes a function into the error log - * @param string $msg the error message to be logged - * @param int $level the error level - */ - public function log($msg, $level=null){ - switch($level){ - case 'debug': - $level = \OCP\Util::DEBUG; - break; - case 'info': - $level = \OCP\Util::INFO; - break; - case 'warn': - $level = \OCP\Util::WARN; - break; - case 'fatal': - $level = \OCP\Util::FATAL; - break; - default: - $level = \OCP\Util::ERROR; - break; - } - \OCP\Util::writeLog($this->appName, $msg, $level); - } - - - /** - * turns an owncloud path into a path on the filesystem - * @param string path the path to the file on the oc filesystem - * @return string the filepath in the filesystem - */ - public function getLocalFilePath($path){ - # TODO: use public api - return \OC_Filesystem::getLocalFile($path); - } - - - /** * used to return and open a new eventsource * @return \OC_EventSource a new open EventSource class */ @@ -275,15 +154,6 @@ class API implements IApi{ } } - /** - * Gets the content of an URL by using CURL or a fallback if it is not - * installed - * @param string $url the url that should be fetched - * @return string the content of the webpage - */ - public function getUrlContent($url) { - return \OC_Util::getUrlContent($url); - } /** * Register a backgroundjob task @@ -295,25 +165,6 @@ class API implements IApi{ \OCP\Backgroundjob::addRegularTask($className, $methodName); } - /** - * Returns a template - * @param string $templateName the name of the template - * @param string $renderAs how it should be rendered - * @param string $appName the name of the app - * @return \OCP\Template a new template - */ - public function getTemplate($templateName, $renderAs='user', $appName=null){ - if($appName === null){ - $appName = $this->appName; - } - - if($renderAs === 'blank'){ - return new \OCP\Template($appName, $templateName); - } else { - return new \OCP\Template($appName, $templateName, $renderAs); - } - } - /** * Tells ownCloud to include a template in the admin overview @@ -330,19 +181,4 @@ class API implements IApi{ } - /** - * get the filesystem info - * - * @param string $path - * @return array with the following keys: - * - size - * - mtime - * - mimetype - * - encrypted - * - versioned - */ - public function getFileInfo($path) { - return \OC\Files\Filesystem::getFileInfo($path); - } - } diff --git a/lib/private/appframework/dependencyinjection/dicontainer.php b/lib/private/appframework/dependencyinjection/dicontainer.php index e62b72fd973..81910df6990 100644 --- a/lib/private/appframework/dependencyinjection/dicontainer.php +++ b/lib/private/appframework/dependencyinjection/dicontainer.php @@ -35,6 +35,7 @@ use OC\AppFramework\Utility\TimeFactory; use OCP\AppFramework\IApi; use OCP\AppFramework\IAppContainer; use OCP\AppFramework\IMiddleWare; +use OCP\AppFramework\Middleware; use OCP\IServerContainer; @@ -49,9 +50,10 @@ class DIContainer extends SimpleContainer implements IAppContainer{ * Put your class dependencies in here * @param string $appName the name of the app */ - public function __construct($appName){ + public function __construct($appName, $urlParams = array()){ $this['AppName'] = $appName; + $this['urlParams'] = $urlParams; $this->registerParameter('ServerContainer', \OC::$server); @@ -66,6 +68,7 @@ class DIContainer extends SimpleContainer implements IAppContainer{ /** @var $c SimpleContainer */ /** @var $server IServerContainer */ $server = $c->query('ServerContainer'); + $server->registerParameter('urlParams', $c['urlParams']); return $server->getRequest(); }); @@ -86,7 +89,7 @@ class DIContainer extends SimpleContainer implements IAppContainer{ * Middleware */ $this['SecurityMiddleware'] = $this->share(function($c){ - return new SecurityMiddleware($c['API'], $c['Request']); + return new SecurityMiddleware($this, $c['Request']); }); $middleWares = $this->middleWares; @@ -130,10 +133,10 @@ class DIContainer extends SimpleContainer implements IAppContainer{ } /** - * @param IMiddleWare $middleWare + * @param Middleware $middleWare * @return boolean */ - function registerMiddleWare(IMiddleWare $middleWare) { + function registerMiddleWare(Middleware $middleWare) { array_push($this->middleWares, $middleWare); } @@ -144,4 +147,49 @@ class DIContainer extends SimpleContainer implements IAppContainer{ function getAppName() { return $this->query('AppName'); } + + /** + * @return boolean + */ + function isLoggedIn() { + return \OC_User::isLoggedIn(); + } + + /** + * @return boolean + */ + function isAdminUser() { + $uid = $this->getUserId(); + return \OC_User::isAdminUser($uid); + } + + private function getUserId() { + return \OC::$session->get('user_id'); + } + + /** + * @param $message + * @param $level + * @return mixed + */ + function log($message, $level) { + switch($level){ + case 'debug': + $level = \OCP\Util::DEBUG; + break; + case 'info': + $level = \OCP\Util::INFO; + break; + case 'warn': + $level = \OCP\Util::WARN; + break; + case 'fatal': + $level = \OCP\Util::FATAL; + break; + default: + $level = \OCP\Util::ERROR; + break; + } + \OCP\Util::writeLog($this->getAppName(), $message, $level); + } } diff --git a/lib/private/appframework/http/dispatcher.php b/lib/private/appframework/http/dispatcher.php index ea57a6860cc..51283fd64e7 100644 --- a/lib/private/appframework/http/dispatcher.php +++ b/lib/private/appframework/http/dispatcher.php @@ -24,8 +24,8 @@ namespace OC\AppFramework\Http; -use \OC\AppFramework\Controller\Controller; use \OC\AppFramework\Middleware\MiddlewareDispatcher; +use OCP\AppFramework\Controller; /** diff --git a/lib/private/appframework/middleware/middlewaredispatcher.php b/lib/private/appframework/middleware/middlewaredispatcher.php index c2377b8844b..681140c2242 100644 --- a/lib/private/appframework/middleware/middlewaredispatcher.php +++ b/lib/private/appframework/middleware/middlewaredispatcher.php @@ -24,7 +24,7 @@ namespace OC\AppFramework\Middleware; -use OC\AppFramework\Controller\Controller; +use OCP\AppFramework\Controller; use OCP\AppFramework\Http\Response; use OCP\AppFramework\MiddleWare; diff --git a/lib/private/appframework/middleware/security/securitymiddleware.php b/lib/private/appframework/middleware/security/securitymiddleware.php index d6daf737bb4..f103a40ee7f 100644 --- a/lib/private/appframework/middleware/security/securitymiddleware.php +++ b/lib/private/appframework/middleware/security/securitymiddleware.php @@ -24,15 +24,14 @@ namespace OC\AppFramework\Middleware\Security; -use OC\AppFramework\Controller\Controller; use OC\AppFramework\Http\Http; -use OC\AppFramework\Http\Request; use OC\AppFramework\Http\RedirectResponse; use OC\AppFramework\Utility\MethodAnnotationReader; -use OC\AppFramework\Core\API; use OCP\AppFramework\Middleware; use OCP\AppFramework\Http\Response; use OCP\AppFramework\Http\JSONResponse; +use OCP\AppFramework\IAppContainer; +use OCP\IRequest; /** @@ -43,18 +42,22 @@ use OCP\AppFramework\Http\JSONResponse; */ class SecurityMiddleware extends Middleware { - private $api; + /** + * @var \OCP\AppFramework\IAppContainer + */ + private $app; /** - * @var \OC\AppFramework\Http\Request + * @var \OCP\IRequest */ private $request; /** - * @param API $api an instance of the api + * @param IAppContainer $app + * @param IRequest $request */ - public function __construct(API $api, Request $request){ - $this->api = $api; + public function __construct(IAppContainer $app, IRequest $request){ + $this->app = $app; $this->request = $request; } @@ -74,24 +77,24 @@ class SecurityMiddleware extends Middleware { // this will set the current navigation entry of the app, use this only // for normal HTML requests and not for AJAX requests - $this->api->activateNavigationEntry(); + $this->app->getServer()->getNavigationManager()->setActiveEntry($this->app->getAppName()); // security checks $isPublicPage = $annotationReader->hasAnnotation('PublicPage'); if(!$isPublicPage) { - if(!$this->api->isLoggedIn()) { + if(!$this->app->isLoggedIn()) { throw new SecurityException('Current user is not logged in', Http::STATUS_UNAUTHORIZED); } if(!$annotationReader->hasAnnotation('NoAdminRequired')) { - if(!$this->api->isAdminUser($this->api->getUserId())) { + if(!$this->app->isAdminUser()) { throw new SecurityException('Logged in user must be an admin', Http::STATUS_FORBIDDEN); } } } if(!$annotationReader->hasAnnotation('NoCSRFRequired')) { - if(!$this->api->passesCSRFCheck()) { + if(!$this->request->passesCSRFCheck()) { throw new SecurityException('CSRF check failed', Http::STATUS_PRECONDITION_FAILED); } } @@ -118,12 +121,13 @@ class SecurityMiddleware extends Middleware { array('message' => $exception->getMessage()), $exception->getCode() ); - $this->api->log($exception->getMessage(), 'debug'); + $this->app->log($exception->getMessage(), 'debug'); } else { - $url = $this->api->linkToAbsolute('index.php', ''); // TODO: replace with link to route + // TODO: replace with link to route + $url = $this->app->getServer()->getURLGenerator()->getAbsoluteURL('index.php'); $response = new RedirectResponse($url); - $this->api->log($exception->getMessage(), 'debug'); + $this->app->log($exception->getMessage(), 'debug'); } return $response; diff --git a/lib/apphelper.php b/lib/private/apphelper.php index bd02f3aabfa..bd02f3aabfa 100644 --- a/lib/apphelper.php +++ b/lib/private/apphelper.php diff --git a/lib/private/helper.php b/lib/private/helper.php index 1236e748256..e9b129db0ca 100644 --- a/lib/private/helper.php +++ b/lib/private/helper.php @@ -81,7 +81,7 @@ class OC_Helper { * Returns a absolute url to the given app and file. */ public static function makeURLAbsolute($url) { - return OC::$server->getURLGenerator()->makeURLAbsolute($url); + return OC::$server->getURLGenerator()->getAbsoluteURL($url); } /** diff --git a/lib/private/l10n/factory.php b/lib/private/l10n/factory.php index ba168872acd..8c65f368171 100644 --- a/lib/private/l10n/factory.php +++ b/lib/private/l10n/factory.php @@ -22,7 +22,7 @@ class Factory { * get an L10N instance * @param $app string * @param $lang string|null - * @return OC_L10N + * @return \OC_L10N */ public function get($app) { if (!isset($this->instances[$app])) { diff --git a/lib/private/server.php b/lib/private/server.php index 7f86919f845..d450907534b 100644 --- a/lib/private/server.php +++ b/lib/private/server.php @@ -34,7 +34,6 @@ class Server extends SimpleContainer implements IServerContainer { $requesttoken = false; } - return new Request( array( 'get' => $_GET, @@ -46,7 +45,6 @@ class Server extends SimpleContainer implements IServerContainer { 'method' => (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD'])) ? $_SERVER['REQUEST_METHOD'] : null, - 'params' => $params, 'urlParams' => $urlParams, 'requesttoken' => $requesttoken, ) diff --git a/lib/urlgenerator.php b/lib/private/urlgenerator.php index 1db4c36cc58..5c1d9d825b6 100644 --- a/lib/urlgenerator.php +++ b/lib/private/urlgenerator.php @@ -8,18 +8,20 @@ */ namespace OC; +use OCP\IURLGenerator; +use RuntimeException; /** * Class to generate URLs */ -class URLGenerator { +class URLGenerator implements IURLGenerator { /** * @brief Creates an url using a defined route * @param $route * @param array $parameters * @return * @internal param array $args with param=>value, will be appended to the returned url - * @returns the url + * @returns string the url * * Returns a url to the given app and file. */ @@ -97,15 +99,13 @@ class URLGenerator { } } + /** - * @brief Makes an $url absolute - * @param string $url the url - * @return string the absolute url - * - * Returns a absolute url to the given app and file. + * Makes an URL absolute + * @param string $url the url in the owncloud host + * @return string the absolute version of the url */ - public function makeURLAbsolute($url) { + public function getAbsoluteURL($url) { return \OC_Request::serverProtocol() . '://' . \OC_Request::serverHost() . $url; } - } diff --git a/lib/public/appframework/app.php b/lib/public/appframework/app.php index d97c5c81848..6ac48bf102a 100644 --- a/lib/public/appframework/app.php +++ b/lib/public/appframework/app.php @@ -31,8 +31,11 @@ namespace OCP\AppFramework; * to be registered using IContainer::registerService */ class App { - public function __construct($appName) { - $this->container = new \OC\AppFramework\DependencyInjection\DIContainer($appName); + /** + * @param array $urlParams an array with variables extracted from the routes + */ + public function __construct($appName, $urlParams = array()) { + $this->container = new \OC\AppFramework\DependencyInjection\DIContainer($appName, $urlParams); } private $container; @@ -50,8 +53,8 @@ class App { * Example code in routes.php of the task app: * $this->create('tasks_index', '/')->get()->action( * function($params){ - * $app = new TaskApp(); - * $app->dispatch('PageController', 'index', $params); + * $app = new TaskApp($params); + * $app->dispatch('PageController', 'index'); * } * ); * @@ -59,8 +62,8 @@ class App { * Example for for TaskApp implementation: * class TaskApp extends \OCP\AppFramework\App { * - * public function __construct(){ - * parent::__construct('tasks'); + * public function __construct($params){ + * parent::__construct('tasks', $params); * * $this->getContainer()->registerService('PageController', function(IAppContainer $c){ * $a = $c->query('API'); @@ -73,9 +76,8 @@ class App { * @param string $controllerName the name of the controller under which it is * stored in the DI container * @param string $methodName the method that you want to call - * @param array $urlParams an array with variables extracted from the routes */ - public function dispatch($controllerName, $methodName, array $urlParams) { - \OC\AppFramework\App::main($controllerName, $methodName, $urlParams, $this->container); + public function dispatch($controllerName, $methodName) { + \OC\AppFramework\App::main($controllerName, $methodName, $this->container); } } diff --git a/lib/private/appframework/controller/controller.php b/lib/public/appframework/controller.php index 0ea0a38cc09..1642b508572 100644 --- a/lib/private/appframework/controller/controller.php +++ b/lib/public/appframework/controller.php @@ -22,11 +22,11 @@ */ -namespace OC\AppFramework\Controller; +namespace OCP\AppFramework; -use OC\AppFramework\Http\Request; -use OC\AppFramework\Core\API; use OCP\AppFramework\Http\TemplateResponse; +use OCP\AppFramework\IAppContainer; +use OCP\IRequest; /** @@ -35,18 +35,21 @@ use OCP\AppFramework\Http\TemplateResponse; abstract class Controller { /** - * @var API instance of the api layer + * @var \OCP\AppFramework\IAppContainer */ - protected $api; + protected $app; + /** + * @var \OCP\IRequest + */ protected $request; /** - * @param API $api an api wrapper instance - * @param Request $request an instance of the request + * @param IAppContainer $app interface to the app + * @param IRequest $request an instance of the request */ - public function __construct(API $api, Request $request){ - $this->api = $api; + public function __construct(IAppContainer $app, IRequest $request){ + $this->app = $app; $this->request = $request; } @@ -127,7 +130,7 @@ abstract class Controller { */ public function render($templateName, array $params=array(), $renderAs='user', array $headers=array()){ - $response = new TemplateResponse($this->api, $templateName); + $response = new TemplateResponse($this->app->getAppName(), $templateName); $response->setParams($params); $response->renderAs($renderAs); diff --git a/lib/public/appframework/http/templateresponse.php b/lib/public/appframework/http/templateresponse.php index 97678c96cba..594530651aa 100644 --- a/lib/public/appframework/http/templateresponse.php +++ b/lib/public/appframework/http/templateresponse.php @@ -24,8 +24,6 @@ namespace OCP\AppFramework\Http; -use OC\AppFramework\Core\API; - /** * Response for a normal template @@ -34,20 +32,16 @@ class TemplateResponse extends Response { protected $templateName; protected $params; - protected $api; protected $renderAs; protected $appName; /** - * @param API $api an API instance * @param string $templateName the name of the template - * @param string $appName optional if you want to include a template from - * a different app + * @param string $appName the name of the app to load the template from */ - public function __construct(API $api, $templateName, $appName=null) { + public function __construct($appName, $templateName) { $this->templateName = $templateName; $this->appName = $appName; - $this->api = $api; $this->params = array(); $this->renderAs = 'user'; } @@ -108,13 +102,7 @@ class TemplateResponse extends Response { */ public function render(){ - if($this->appName !== null){ - $appName = $this->appName; - } else { - $appName = $this->api->getAppName(); - } - - $template = $this->api->getTemplate($this->templateName, $this->renderAs, $appName); + $template = new \OCP\Template($this->appName, $this->templateName, $this->renderAs); foreach($this->params as $key => $value){ $template->assign($key, $value); diff --git a/lib/public/appframework/iapi.php b/lib/public/appframework/iapi.php index fa6af5f5965..3bde4c9d4e7 100644 --- a/lib/public/appframework/iapi.php +++ b/lib/public/appframework/iapi.php @@ -67,59 +67,6 @@ interface IApi { */ function add3rdPartyStyle($name); - /** - * Returns the translation object - * @return \OC_L10N the translation object - * - * FIXME: returns private object / should be retrieved from teh ServerContainer - */ - function getTrans(); - - - /** - * Returns the URL for a route - * @param string $routeName the name of the route - * @param array $arguments an array with arguments which will be filled into the url - * @return string the url - */ - function linkToRoute($routeName, $arguments=array()); - - - /** - * Returns an URL for an image or file - * @param string $file the name of the file - * @param string $appName the name of the app, defaults to the current one - */ - function linkTo($file, $appName=null); - - - /** - * Returns the link to an image, like link to but only with prepending img/ - * @param string $file the name of the file - * @param string $appName the name of the app, defaults to the current one - */ - function imagePath($file, $appName = null); - - - /** - * Makes an URL absolute - * @param string $url the url - * @return string the absolute url - * - * FIXME: function should live in Request / Response - */ - function getAbsoluteURL($url); - - - /** - * links to a file - * @param string $file the name of the file - * @param string $appName the name of the app, defaults to the current one - * @deprecated replaced with linkToRoute() - * @return string the url - */ - function linkToAbsolute($file, $appName = null); - /** * Checks if an app is enabled @@ -128,24 +75,4 @@ interface IApi { */ public function isAppEnabled($appName); - - /** - * Writes a function into the error log - * @param string $msg the error message to be logged - * @param int $level the error level - * - * FIXME: add logger instance to ServerContainer - */ - function log($msg, $level = null); - - - /** - * Returns a template - * @param string $templateName the name of the template - * @param string $renderAs how it should be rendered - * @param string $appName the name of the app - * @return \OCP\Template a new template - */ - function getTemplate($templateName, $renderAs='user', $appName=null); - } diff --git a/lib/public/appframework/iappcontainer.php b/lib/public/appframework/iappcontainer.php index 7d3b4b3bac7..7e6ec6016b7 100644 --- a/lib/public/appframework/iappcontainer.php +++ b/lib/public/appframework/iappcontainer.php @@ -50,8 +50,26 @@ interface IAppContainer extends IContainer{ function getServer(); /** - * @param IMiddleWare $middleWare + * @param Middleware $middleWare * @return boolean */ - function registerMiddleWare(IMiddleWare $middleWare); + function registerMiddleWare(Middleware $middleWare); + + /** + * @return boolean + */ + function isLoggedIn(); + + /** + * @return boolean + */ + function isAdminUser(); + + /** + * @param $message + * @param $level + * @return mixed + */ + function log($message, $level); + } diff --git a/lib/public/appframework/middleware.php b/lib/public/appframework/middleware.php index 12776c119c0..94f71ea8520 100644 --- a/lib/public/appframework/middleware.php +++ b/lib/public/appframework/middleware.php @@ -24,6 +24,7 @@ namespace OCP\AppFramework; +use OCP\AppFramework\Controller; use OCP\AppFramework\Http\Response; |