diff options
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/appframework/controller/controller.php | 142 | ||||
-rw-r--r-- | lib/private/appframework/core/api.php | 164 | ||||
-rw-r--r-- | lib/private/appframework/dependencyinjection/dicontainer.php | 52 | ||||
-rw-r--r-- | lib/private/appframework/http/dispatcher.php | 2 | ||||
-rw-r--r-- | lib/private/appframework/middleware/middlewaredispatcher.php | 2 | ||||
-rw-r--r-- | lib/private/appframework/middleware/security/securitymiddleware.php | 34 | ||||
-rw-r--r-- | lib/private/apphelper.php | 25 | ||||
-rw-r--r-- | lib/private/helper.php | 2 | ||||
-rw-r--r-- | lib/private/l10n/factory.php | 2 | ||||
-rw-r--r-- | lib/private/server.php | 3 | ||||
-rw-r--r-- | lib/private/urlgenerator.php | 111 |
11 files changed, 209 insertions, 330 deletions
diff --git a/lib/private/appframework/controller/controller.php b/lib/private/appframework/controller/controller.php deleted file mode 100644 index 0ea0a38cc09..00000000000 --- a/lib/private/appframework/controller/controller.php +++ /dev/null @@ -1,142 +0,0 @@ -<?php - -/** - * ownCloud - App Framework - * - * @author Bernhard Posselt - * @copyright 2012 Bernhard Posselt nukeawhale@gmail.com - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE - * License as published by the Free Software Foundation; either - * version 3 of the License, or any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU AFFERO GENERAL PUBLIC LICENSE for more details. - * - * You should have received a copy of the GNU Affero General Public - * License along with this library. If not, see <http://www.gnu.org/licenses/>. - * - */ - - -namespace OC\AppFramework\Controller; - -use OC\AppFramework\Http\Request; -use OC\AppFramework\Core\API; -use OCP\AppFramework\Http\TemplateResponse; - - -/** - * Base class to inherit your controllers from - */ -abstract class Controller { - - /** - * @var API instance of the api layer - */ - protected $api; - - protected $request; - - /** - * @param API $api an api wrapper instance - * @param Request $request an instance of the request - */ - public function __construct(API $api, Request $request){ - $this->api = $api; - $this->request = $request; - } - - - /** - * Lets you access post and get parameters by the index - * @param string $key the key which you want to access in the URL Parameter - * placeholder, $_POST or $_GET array. - * The priority how they're returned is the following: - * 1. URL parameters - * 2. POST parameters - * 3. GET parameters - * @param mixed $default If the key is not found, this value will be returned - * @return mixed the content of the array - */ - public function params($key, $default=null){ - return $this->request->getParam($key, $default); - } - - - /** - * Returns all params that were received, be it from the request - * (as GET or POST) or throuh the URL by the route - * @return array the array with all parameters - */ - public function getParams() { - return $this->request->getParams(); - } - - - /** - * Returns the method of the request - * @return string the method of the request (POST, GET, etc) - */ - public function method() { - return $this->request->getMethod(); - } - - - /** - * Shortcut for accessing an uploaded file through the $_FILES array - * @param string $key the key that will be taken from the $_FILES array - * @return array the file in the $_FILES element - */ - public function getUploadedFile($key) { - return $this->request->getUploadedFile($key); - } - - - /** - * Shortcut for getting env variables - * @param string $key the key that will be taken from the $_ENV array - * @return array the value in the $_ENV element - */ - public function env($key) { - return $this->request->getEnv($key); - } - - - /** - * Shortcut for getting cookie variables - * @param string $key the key that will be taken from the $_COOKIE array - * @return array the value in the $_COOKIE element - */ - public function cookie($key) { - return $this->request->getCookie($key); - } - - - /** - * Shortcut for rendering a template - * @param string $templateName the name of the template - * @param array $params the template parameters in key => value structure - * @param string $renderAs user renders a full page, blank only your template - * admin an entry in the admin settings - * @param array $headers set additional headers in name/value pairs - * @return \OCP\AppFramework\Http\TemplateResponse containing the page - */ - public function render($templateName, array $params=array(), - $renderAs='user', array $headers=array()){ - $response = new TemplateResponse($this->api, $templateName); - $response->setParams($params); - $response->renderAs($renderAs); - - foreach($headers as $name => $value){ - $response->addHeader($name, $value); - } - - return $response; - } - - -} 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 3755d45fa09..7276a11e4d9 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; @@ -86,7 +87,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']); }); $this['MiddlewareDispatcher'] = $this->share(function($c){ @@ -129,10 +130,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); } @@ -143,4 +144,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..2a9ed121488 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\Controller; /** diff --git a/lib/private/appframework/middleware/middlewaredispatcher.php b/lib/private/appframework/middleware/middlewaredispatcher.php index c2377b8844b..c46ddc7cb02 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\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/private/apphelper.php b/lib/private/apphelper.php new file mode 100644 index 00000000000..bd02f3aabfa --- /dev/null +++ b/lib/private/apphelper.php @@ -0,0 +1,25 @@ +<?php +/** + * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + * + */ + +namespace OC; + +/** + * TODO: Description + */ +class AppHelper implements \OCP\IHelper { + /** + * 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); + } +} diff --git a/lib/private/helper.php b/lib/private/helper.php index a34640d8e36..f3f2ea9492a 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 73a0cbd6ce6..e55f59f6a1c 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, ) @@ -290,4 +288,5 @@ class Server extends SimpleContainer implements IServerContainer { function getDatabaseConnection() { return \OC_DB::getConnection(); } + } diff --git a/lib/private/urlgenerator.php b/lib/private/urlgenerator.php new file mode 100644 index 00000000000..5c1d9d825b6 --- /dev/null +++ b/lib/private/urlgenerator.php @@ -0,0 +1,111 @@ +<?php +/** + * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + * + */ + +namespace OC; +use OCP\IURLGenerator; +use RuntimeException; + +/** + * Class to generate URLs + */ +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 string the url + * + * Returns a url to the given app and file. + */ + public function linkToRoute($route, $parameters = array()) { + $urlLinkTo = \OC::getRouter()->generate($route, $parameters); + return $urlLinkTo; + } + + /** + * @brief Creates an url + * @param string $app app + * @param string $file file + * @param array $args array with param=>value, will be appended to the returned url + * The value of $args will be urlencoded + * @return string the url + * + * Returns a url to the given app and file. + */ + public function linkTo( $app, $file, $args = array() ) { + if( $app != '' ) { + $app_path = \OC_App::getAppPath($app); + // Check if the app is in the app folder + if ($app_path && file_exists($app_path . '/' . $file)) { + if (substr($file, -3) == 'php' || substr($file, -3) == 'css') { + $urlLinkTo = \OC::$WEBROOT . '/index.php/apps/' . $app; + $urlLinkTo .= ($file != 'index.php') ? '/' . $file : ''; + } else { + $urlLinkTo = \OC_App::getAppWebPath($app) . '/' . $file; + } + } else { + $urlLinkTo = \OC::$WEBROOT . '/' . $app . '/' . $file; + } + } else { + if (file_exists(\OC::$SERVERROOT . '/core/' . $file)) { + $urlLinkTo = \OC::$WEBROOT . '/core/' . $file; + } else { + $urlLinkTo = \OC::$WEBROOT . '/' . $file; + } + } + + if ($args && $query = http_build_query($args, '', '&')) { + $urlLinkTo .= '?' . $query; + } + + return $urlLinkTo; + } + + /** + * @brief Creates path to an image + * @param string $app app + * @param string $image image name + * @return string the url + * + * Returns the path to the image. + */ + public function imagePath($app, $image) { + // Read the selected theme from the config file + $theme = \OC_Util::getTheme(); + + // Check if the app is in the app folder + if (file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$image")) { + return \OC::$WEBROOT . "/themes/$theme/apps/$app/img/$image"; + } elseif (file_exists(\OC_App::getAppPath($app) . "/img/$image")) { + return \OC_App::getAppWebPath($app) . "/img/$image"; + } elseif (!empty($app) and file_exists(\OC::$SERVERROOT . "/themes/$theme/$app/img/$image")) { + return \OC::$WEBROOT . "/themes/$theme/$app/img/$image"; + } elseif (!empty($app) and file_exists(\OC::$SERVERROOT . "/$app/img/$image")) { + return \OC::$WEBROOT . "/$app/img/$image"; + } elseif (file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$image")) { + return \OC::$WEBROOT . "/themes/$theme/core/img/$image"; + } elseif (file_exists(\OC::$SERVERROOT . "/core/img/$image")) { + return \OC::$WEBROOT . "/core/img/$image"; + } else { + throw new RuntimeException('image not found: image:' . $image . ' webroot:' . \OC::$WEBROOT . ' serverroot:' . \OC::$SERVERROOT); + } + } + + + /** + * Makes an URL absolute + * @param string $url the url in the owncloud host + * @return string the absolute version of the url + */ + public function getAbsoluteURL($url) { + return \OC_Request::serverProtocol() . '://' . \OC_Request::serverHost() . $url; + } +} |