dicontainer.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. /**
  3. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  4. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  5. * @author Lukas Reschke <lukas@owncloud.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Thomas Tanghus <thomas@tanghus.net>
  10. *
  11. * @copyright Copyright (c) 2015, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\AppFramework\DependencyInjection;
  28. use OC;
  29. use OC\AppFramework\Http;
  30. use OC\AppFramework\Http\Request;
  31. use OC\AppFramework\Http\Dispatcher;
  32. use OC\AppFramework\Http\Output;
  33. use OC\AppFramework\Core\API;
  34. use OC\AppFramework\Middleware\MiddlewareDispatcher;
  35. use OC\AppFramework\Middleware\Security\SecurityMiddleware;
  36. use OC\AppFramework\Middleware\Security\CORSMiddleware;
  37. use OC\AppFramework\Middleware\SessionMiddleware;
  38. use OC\AppFramework\Utility\SimpleContainer;
  39. use OC\AppFramework\Utility\TimeFactory;
  40. use OC\AppFramework\Utility\ControllerMethodReflector;
  41. use OCP\AppFramework\IApi;
  42. use OCP\AppFramework\IAppContainer;
  43. use OCP\AppFramework\Middleware;
  44. use OCP\IServerContainer;
  45. class DIContainer extends SimpleContainer implements IAppContainer {
  46. /**
  47. * @var array
  48. */
  49. private $middleWares = array();
  50. /**
  51. * Put your class dependencies in here
  52. * @param string $appName the name of the app
  53. */
  54. public function __construct($appName, $urlParams = array()){
  55. $this['AppName'] = $appName;
  56. $this['urlParams'] = $urlParams;
  57. // aliases
  58. $this->registerService('appName', function($c) {
  59. return $c->query('AppName');
  60. });
  61. $this->registerService('webRoot', function($c) {
  62. return $c->query('WebRoot');
  63. });
  64. $this->registerService('userId', function($c) {
  65. return $c->query('UserId');
  66. });
  67. /**
  68. * Core services
  69. */
  70. $this->registerService('OCP\\IAppConfig', function($c) {
  71. return $this->getServer()->getAppConfig();
  72. });
  73. $this->registerService('OCP\\App\\IAppManager', function($c) {
  74. return $this->getServer()->getAppManager();
  75. });
  76. $this->registerService('OCP\\AppFramework\\Http\\IOutput', function($c){
  77. return new Output();
  78. });
  79. $this->registerService('OCP\\IAvatarManager', function($c) {
  80. return $this->getServer()->getAvatarManager();
  81. });
  82. $this->registerService('OCP\\Activity\\IManager', function($c) {
  83. return $this->getServer()->getActivityManager();
  84. });
  85. $this->registerService('OCP\\ICache', function($c) {
  86. return $this->getServer()->getCache();
  87. });
  88. $this->registerService('OCP\\ICacheFactory', function($c) {
  89. return $this->getServer()->getMemCacheFactory();
  90. });
  91. $this->registerService('OCP\\IConfig', function($c) {
  92. return $this->getServer()->getConfig();
  93. });
  94. $this->registerService('OCP\\Contacts\\IManager', function($c) {
  95. return $this->getServer()->getContactsManager();
  96. });
  97. $this->registerService('OCP\\IDateTimeZone', function($c) {
  98. return $this->getServer()->getDateTimeZone();
  99. });
  100. $this->registerService('OCP\\IDb', function($c) {
  101. return $this->getServer()->getDb();
  102. });
  103. $this->registerService('OCP\\IDBConnection', function($c) {
  104. return $this->getServer()->getDatabaseConnection();
  105. });
  106. $this->registerService('OCP\\Diagnostics\\IEventLogger', function($c) {
  107. return $this->getServer()->getEventLogger();
  108. });
  109. $this->registerService('OCP\\Diagnostics\\IQueryLogger', function($c) {
  110. return $this->getServer()->getQueryLogger();
  111. });
  112. $this->registerService('OCP\\Files\\Config\\IMountProviderCollection', function($c) {
  113. return $this->getServer()->getMountProviderCollection();
  114. });
  115. $this->registerService('OCP\\Files\\IRootFolder', function($c) {
  116. return $this->getServer()->getRootFolder();
  117. });
  118. $this->registerService('OCP\\IGroupManager', function($c) {
  119. return $this->getServer()->getGroupManager();
  120. });
  121. $this->registerService('OCP\\IL10N', function($c) {
  122. return $this->getServer()->getL10N($c->query('AppName'));
  123. });
  124. $this->registerService('OCP\\ILogger', function($c) {
  125. return $this->getServer()->getLogger();
  126. });
  127. $this->registerService('OCP\\BackgroundJob\\IJobList', function($c) {
  128. return $this->getServer()->getJobList();
  129. });
  130. $this->registerService('OCP\\AppFramework\\Utility\\IControllerMethodReflector', function($c) {
  131. return $c->query('ControllerMethodReflector');
  132. });
  133. $this->registerService('OCP\\INavigationManager', function($c) {
  134. return $this->getServer()->getNavigationManager();
  135. });
  136. $this->registerService('OCP\\IPreview', function($c) {
  137. return $this->getServer()->getPreviewManager();
  138. });
  139. $this->registerService('OCP\\IRequest', function($c) {
  140. return $c->query('Request');
  141. });
  142. $this->registerService('OCP\\ITagManager', function($c) {
  143. return $this->getServer()->getTagManager();
  144. });
  145. $this->registerService('OCP\\ITempManager', function($c) {
  146. return $this->getServer()->getTempManager();
  147. });
  148. $this->registerService('OCP\\AppFramework\\Utility\\ITimeFactory', function($c) {
  149. return $c->query('TimeFactory');
  150. });
  151. $this->registerService('OCP\\Route\\IRouter', function($c) {
  152. return $this->getServer()->getRouter();
  153. });
  154. $this->registerService('OCP\\ISearch', function($c) {
  155. return $this->getServer()->getSearch();
  156. });
  157. $this->registerService('OCP\\ISearch', function($c) {
  158. return $this->getServer()->getSearch();
  159. });
  160. $this->registerService('OCP\\Security\\ICrypto', function($c) {
  161. return $this->getServer()->getCrypto();
  162. });
  163. $this->registerService('OCP\\Security\\IHasher', function($c) {
  164. return $this->getServer()->getHasher();
  165. });
  166. $this->registerService('OCP\\Security\\ISecureRandom', function($c) {
  167. return $this->getServer()->getSecureRandom();
  168. });
  169. $this->registerService('OCP\\IURLGenerator', function($c) {
  170. return $this->getServer()->getURLGenerator();
  171. });
  172. $this->registerService('OCP\\IUserManager', function($c) {
  173. return $this->getServer()->getUserManager();
  174. });
  175. $this->registerService('OCP\\IUserSession', function($c) {
  176. return $this->getServer()->getUserSession();
  177. });
  178. $this->registerService('ServerContainer', function ($c) {
  179. return $this->getServer();
  180. });
  181. // commonly used attributes
  182. $this->registerService('UserId', function ($c) {
  183. return $c->query('OCP\\IUserSession')->getSession()->get('user_id');
  184. });
  185. $this->registerService('WebRoot', function ($c) {
  186. return $c->query('ServerContainer')->getWebRoot();
  187. });
  188. /**
  189. * App Framework APIs
  190. */
  191. $this->registerService('API', function($c){
  192. $c->query('OCP\\ILogger')->debug(
  193. 'Accessing the API class is deprecated! Use the appropriate ' .
  194. 'services instead!'
  195. );
  196. return new API($c['AppName']);
  197. });
  198. $this->registerService('Request', function($c) {
  199. /** @var $c SimpleContainer */
  200. /** @var $server SimpleContainer */
  201. $server = $c->query('ServerContainer');
  202. /** @var $server IServerContainer */
  203. return $server->getRequest();
  204. });
  205. $this->registerService('Protocol', function($c){
  206. if(isset($_SERVER['SERVER_PROTOCOL'])) {
  207. return new Http($_SERVER, $_SERVER['SERVER_PROTOCOL']);
  208. } else {
  209. return new Http($_SERVER);
  210. }
  211. });
  212. $this->registerService('Dispatcher', function($c) {
  213. return new Dispatcher(
  214. $c['Protocol'],
  215. $c['MiddlewareDispatcher'],
  216. $c['ControllerMethodReflector'],
  217. $c['Request']
  218. );
  219. });
  220. /**
  221. * Middleware
  222. */
  223. $app = $this;
  224. $this->registerService('SecurityMiddleware', function($c) use ($app){
  225. return new SecurityMiddleware(
  226. $c['Request'],
  227. $c['ControllerMethodReflector'],
  228. $app->getServer()->getNavigationManager(),
  229. $app->getServer()->getURLGenerator(),
  230. $app->getServer()->getLogger(),
  231. $c['AppName'],
  232. $app->isLoggedIn(),
  233. $app->isAdminUser()
  234. );
  235. });
  236. $this->registerService('CORSMiddleware', function($c) {
  237. return new CORSMiddleware(
  238. $c['Request'],
  239. $c['ControllerMethodReflector'],
  240. $c['OCP\IUserSession']
  241. );
  242. });
  243. $this->registerService('SessionMiddleware', function($c) use ($app) {
  244. return new SessionMiddleware(
  245. $c['Request'],
  246. $c['ControllerMethodReflector'],
  247. $app->getServer()->getSession()
  248. );
  249. });
  250. $middleWares = &$this->middleWares;
  251. $this->registerService('MiddlewareDispatcher', function($c) use (&$middleWares) {
  252. $dispatcher = new MiddlewareDispatcher();
  253. $dispatcher->registerMiddleware($c['CORSMiddleware']);
  254. $dispatcher->registerMiddleware($c['SecurityMiddleware']);
  255. foreach($middleWares as $middleWare) {
  256. $dispatcher->registerMiddleware($c[$middleWare]);
  257. }
  258. $dispatcher->registerMiddleware($c['SessionMiddleware']);
  259. return $dispatcher;
  260. });
  261. /**
  262. * Utilities
  263. */
  264. $this->registerService('TimeFactory', function($c){
  265. return new TimeFactory();
  266. });
  267. $this->registerService('ControllerMethodReflector', function($c) {
  268. return new ControllerMethodReflector();
  269. });
  270. }
  271. /**
  272. * @deprecated implements only deprecated methods
  273. * @return IApi
  274. */
  275. function getCoreApi()
  276. {
  277. return $this->query('API');
  278. }
  279. /**
  280. * @return \OCP\IServerContainer
  281. */
  282. function getServer()
  283. {
  284. return OC::$server;
  285. }
  286. /**
  287. * @param string $middleWare
  288. * @return boolean|null
  289. */
  290. function registerMiddleWare($middleWare) {
  291. array_push($this->middleWares, $middleWare);
  292. }
  293. /**
  294. * used to return the appname of the set application
  295. * @return string the name of your application
  296. */
  297. function getAppName() {
  298. return $this->query('AppName');
  299. }
  300. /**
  301. * @deprecated use IUserSession->isLoggedIn()
  302. * @return boolean
  303. */
  304. function isLoggedIn() {
  305. return \OC_User::isLoggedIn();
  306. }
  307. /**
  308. * @deprecated use IGroupManager->isAdmin($userId)
  309. * @return boolean
  310. */
  311. function isAdminUser() {
  312. $uid = $this->getUserId();
  313. return \OC_User::isAdminUser($uid);
  314. }
  315. private function getUserId() {
  316. return $this->getServer()->getSession()->get('user_id');
  317. }
  318. /**
  319. * @deprecated use the ILogger instead
  320. * @param string $message
  321. * @param string $level
  322. * @return mixed
  323. */
  324. function log($message, $level) {
  325. switch($level){
  326. case 'debug':
  327. $level = \OCP\Util::DEBUG;
  328. break;
  329. case 'info':
  330. $level = \OCP\Util::INFO;
  331. break;
  332. case 'warn':
  333. $level = \OCP\Util::WARN;
  334. break;
  335. case 'fatal':
  336. $level = \OCP\Util::FATAL;
  337. break;
  338. default:
  339. $level = \OCP\Util::ERROR;
  340. break;
  341. }
  342. \OCP\Util::writeLog($this->getAppName(), $message, $level);
  343. }
  344. }