123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- <?php
- /**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
- * @author Bernhard Posselt <dev@bernhard-posselt.com>
- * @author Bjoern Schiessle <bjoern@schiessle.org>
- * @author Christoph Wurst <christoph@owncloud.com>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Jörn Friedrich Dreyer <jfd@butonic.de>
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Robin McCorkell <robin@mccorkell.me.uk>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Sebastian Wessalowski <sebastian@wessalowski.org>
- * @author Thomas Müller <thomas.mueller@tmit.eu>
- * @author Thomas Tanghus <thomas@tanghus.net>
- *
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program 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, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
- */
- namespace OC\AppFramework\DependencyInjection;
- use OC;
- use OC\AppFramework\Http;
- use OC\AppFramework\Http\Dispatcher;
- use OC\AppFramework\Http\Output;
- use OC\AppFramework\Middleware\MiddlewareDispatcher;
- use OC\AppFramework\Middleware\Security\CORSMiddleware;
- use OC\AppFramework\Middleware\OCSMiddleware;
- use OC\AppFramework\Middleware\Security\RateLimitingMiddleware;
- use OC\AppFramework\Middleware\Security\SecurityMiddleware;
- use OC\AppFramework\Middleware\SessionMiddleware;
- use OC\AppFramework\Utility\SimpleContainer;
- use OC\Core\Middleware\TwoFactorMiddleware;
- use OC\ServerContainer;
- use OCP\AppFramework\Http\IOutput;
- use OCP\AppFramework\IAppContainer;
- use OCP\AppFramework\QueryException;
- use OCP\AppFramework\Utility\IControllerMethodReflector;
- use OCP\AppFramework\Utility\ITimeFactory;
- use OCP\Files\Folder;
- use OCP\Files\IAppData;
- use OCP\GlobalScale\IConfig;
- use OCP\IL10N;
- use OCP\ILogger;
- use OCP\INavigationManager;
- use OCP\IRequest;
- use OCP\IServerContainer;
- use OCP\ISession;
- use OCP\IURLGenerator;
- use OCP\IUserSession;
- use OCA\WorkflowEngine\Manager;
- class DIContainer extends SimpleContainer implements IAppContainer {
- /**
- * @var array
- */
- private $middleWares = [];
- /** @var ServerContainer */
- private $server;
- /**
- * Put your class dependencies in here
- * @param string $appName the name of the app
- * @param array $urlParams
- * @param ServerContainer|null $server
- */
- public function __construct($appName, $urlParams = array(), ServerContainer $server = null){
- parent::__construct();
- $this['AppName'] = $appName;
- $this['urlParams'] = $urlParams;
- $this->registerAlias('Request', IRequest::class);
- /** @var \OC\ServerContainer $server */
- if ($server === null) {
- $server = \OC::$server;
- }
- $this->server = $server;
- $this->server->registerAppContainer($appName, $this);
- // aliases
- $this->registerAlias('appName', 'AppName');
- $this->registerAlias('webRoot', 'WebRoot');
- $this->registerAlias('userId', 'UserId');
- /**
- * Core services
- */
- $this->registerService(IOutput::class, function(){
- return new Output($this->getServer()->getWebRoot());
- });
- $this->registerService(Folder::class, function() {
- return $this->getServer()->getUserFolder();
- });
- $this->registerService(IAppData::class, function (SimpleContainer $c) {
- return $this->getServer()->getAppDataDir($c->query('AppName'));
- });
- $this->registerService(IL10N::class, function($c) {
- return $this->getServer()->getL10N($c->query('AppName'));
- });
- // Log wrapper
- $this->registerService(ILogger::class, function ($c) {
- return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->query('AppName'));
- });
- $this->registerService(IServerContainer::class, function () {
- return $this->getServer();
- });
- $this->registerAlias('ServerContainer', IServerContainer::class);
- $this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) {
- return $c->query(Manager::class);
- });
- $this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) {
- return $c;
- });
- // commonly used attributes
- $this->registerService('UserId', function ($c) {
- return $c->query(IUserSession::class)->getSession()->get('user_id');
- });
- $this->registerService('WebRoot', function ($c) {
- return $c->query('ServerContainer')->getWebRoot();
- });
- $this->registerService('OC_Defaults', function ($c) {
- return $c->getServer()->getThemingDefaults();
- });
- $this->registerService(IConfig::class, function ($c) {
- return $c->query(OC\GlobalScale\Config::class);
- });
- $this->registerService('Protocol', function($c){
- /** @var \OC\Server $server */
- $server = $c->query('ServerContainer');
- $protocol = $server->getRequest()->getHttpProtocol();
- return new Http($_SERVER, $protocol);
- });
- $this->registerService('Dispatcher', function($c) {
- return new Dispatcher(
- $c['Protocol'],
- $c['MiddlewareDispatcher'],
- $c->query(IControllerMethodReflector::class),
- $c['Request']
- );
- });
- /**
- * App Framework default arguments
- */
- $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH');
- $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept');
- $this->registerParameter('corsMaxAge', 1728000);
- /**
- * Middleware
- */
- $this->registerService('MiddlewareDispatcher', function(SimpleContainer $c) {
- $server = $this->getServer();
- $dispatcher = new MiddlewareDispatcher();
- $dispatcher->registerMiddleware(
- new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware(
- $c->query(IRequest::class),
- $c->query(IControllerMethodReflector::class)
- )
- );
- $dispatcher->registerMiddleware(
- new CORSMiddleware(
- $c->query(IRequest::class),
- $c->query(IControllerMethodReflector::class),
- $c->query(IUserSession::class),
- $c->query(OC\Security\Bruteforce\Throttler::class)
- )
- );
- $dispatcher->registerMiddleware(
- new OCSMiddleware(
- $c->query(IRequest::class)
- )
- );
- $securityMiddleware = new SecurityMiddleware(
- $c->query(IRequest::class),
- $c->query(IControllerMethodReflector::class),
- $c->query(INavigationManager::class),
- $c->query(IURLGenerator::class),
- $server->getLogger(),
- $c['AppName'],
- $server->getUserSession()->isLoggedIn(),
- $server->getGroupManager()->isAdmin($this->getUserId()),
- $server->getContentSecurityPolicyManager(),
- $server->getCsrfTokenManager(),
- $server->getContentSecurityPolicyNonceManager(),
- $server->getAppManager(),
- $server->getL10N('lib')
- );
- $dispatcher->registerMiddleware($securityMiddleware);
- $dispatcher->registerMiddleware(
- new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware(
- $c->query(IControllerMethodReflector::class),
- $c->query(ISession::class),
- $c->query(IUserSession::class),
- $c->query(ITimeFactory::class)
- )
- );
- $dispatcher->registerMiddleware(
- new TwoFactorMiddleware(
- $c->query(OC\Authentication\TwoFactorAuth\Manager::class),
- $c->query(IUserSession::class),
- $c->query(ISession::class),
- $c->query(IURLGenerator::class),
- $c->query(IControllerMethodReflector::class),
- $c->query(IRequest::class)
- )
- );
- $dispatcher->registerMiddleware(
- new OC\AppFramework\Middleware\Security\BruteForceMiddleware(
- $c->query(IControllerMethodReflector::class),
- $c->query(OC\Security\Bruteforce\Throttler::class),
- $c->query(IRequest::class)
- )
- );
- $dispatcher->registerMiddleware(
- new RateLimitingMiddleware(
- $c->query(IRequest::class),
- $c->query(IUserSession::class),
- $c->query(IControllerMethodReflector::class),
- $c->query(OC\Security\RateLimiting\Limiter::class)
- )
- );
- $dispatcher->registerMiddleware(
- new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware(
- $c->query(IRequest::class),
- $c->query(ISession::class),
- $c->query(\OCP\IConfig::class)
- )
- );
- $dispatcher->registerMiddleware(
- $c->query(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class)
- );
- foreach($this->middleWares as $middleWare) {
- $dispatcher->registerMiddleware($c[$middleWare]);
- }
- $dispatcher->registerMiddleware(
- new SessionMiddleware(
- $c->query(IRequest::class),
- $c->query(IControllerMethodReflector::class),
- $c->query(ISession::class)
- )
- );
- return $dispatcher;
- });
- }
- /**
- * @return \OCP\IServerContainer
- */
- public function getServer()
- {
- return $this->server;
- }
- /**
- * @param string $middleWare
- * @return boolean|null
- */
- public function registerMiddleWare($middleWare) {
- $this->middleWares[] = $middleWare;
- }
- /**
- * used to return the appname of the set application
- * @return string the name of your application
- */
- public function getAppName() {
- return $this->query('AppName');
- }
- /**
- * @deprecated use IUserSession->isLoggedIn()
- * @return boolean
- */
- public function isLoggedIn() {
- return \OC::$server->getUserSession()->isLoggedIn();
- }
- /**
- * @deprecated use IGroupManager->isAdmin($userId)
- * @return boolean
- */
- public function isAdminUser() {
- $uid = $this->getUserId();
- return \OC_User::isAdminUser($uid);
- }
- private function getUserId() {
- return $this->getServer()->getSession()->get('user_id');
- }
- /**
- * @deprecated use the ILogger instead
- * @param string $message
- * @param string $level
- * @return mixed
- */
- public function log($message, $level) {
- switch($level){
- case 'debug':
- $level = ILogger::DEBUG;
- break;
- case 'info':
- $level = ILogger::INFO;
- break;
- case 'warn':
- $level = ILogger::WARN;
- break;
- case 'fatal':
- $level = ILogger::FATAL;
- break;
- default:
- $level = ILogger::ERROR;
- break;
- }
- \OCP\Util::writeLog($this->getAppName(), $message, $level);
- }
- /**
- * Register a capability
- *
- * @param string $serviceName e.g. 'OCA\Files\Capabilities'
- */
- public function registerCapability($serviceName) {
- $this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) {
- return $this->query($serviceName);
- });
- }
- /**
- * @param string $name
- * @return mixed
- * @throws QueryException if the query could not be resolved
- */
- public function query($name) {
- try {
- return $this->queryNoFallback($name);
- } catch (QueryException $firstException) {
- try {
- return $this->getServer()->query($name);
- } catch (QueryException $secondException) {
- if ($firstException->getCode() === 1) {
- throw $secondException;
- }
- throw $firstException;
- }
- }
- }
- /**
- * @param string $name
- * @return mixed
- * @throws QueryException if the query could not be resolved
- */
- public function queryNoFallback($name) {
- $name = $this->sanitizeName($name);
- if ($this->offsetExists($name)) {
- return parent::query($name);
- } else {
- if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) {
- return parent::query($name);
- } else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) {
- return parent::query($name);
- } else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) {
- return parent::query($name);
- }
- }
- throw new QueryException('Could not resolve ' . $name . '!' .
- ' Class can not be instantiated', 1);
- }
- }
|