server.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. namespace OC;
  3. use OC\AppFramework\Http\Request;
  4. use OC\AppFramework\Utility\SimpleContainer;
  5. use OC\Cache\UserCache;
  6. use OC\DB\ConnectionWrapper;
  7. use OC\Files\Node\Root;
  8. use OC\Files\View;
  9. use OCP\IServerContainer;
  10. /**
  11. * Class Server
  12. * @package OC
  13. *
  14. * TODO: hookup all manager classes
  15. */
  16. class Server extends SimpleContainer implements IServerContainer {
  17. function __construct() {
  18. $this->registerService('ContactsManager', function($c) {
  19. return new ContactsManager();
  20. });
  21. $this->registerService('Request', function($c) {
  22. if (isset($c['urlParams'])) {
  23. $urlParams = $c['urlParams'];
  24. } else {
  25. $urlParams = array();
  26. }
  27. if (\OC::$session->exists('requesttoken')) {
  28. $requesttoken = \OC::$session->get('requesttoken');
  29. } else {
  30. $requesttoken = false;
  31. }
  32. return new Request(
  33. array(
  34. 'get' => $_GET,
  35. 'post' => $_POST,
  36. 'files' => $_FILES,
  37. 'server' => $_SERVER,
  38. 'env' => $_ENV,
  39. 'cookies' => $_COOKIE,
  40. 'method' => (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD']))
  41. ? $_SERVER['REQUEST_METHOD']
  42. : null,
  43. 'urlParams' => $urlParams,
  44. 'requesttoken' => $requesttoken,
  45. )
  46. );
  47. });
  48. $this->registerService('PreviewManager', function($c) {
  49. return new PreviewManager();
  50. });
  51. $this->registerService('TagManager', function($c) {
  52. $user = \OC_User::getUser();
  53. return new TagManager($user);
  54. });
  55. $this->registerService('RootFolder', function($c) {
  56. // TODO: get user and user manager from container as well
  57. $user = \OC_User::getUser();
  58. /** @var $c SimpleContainer */
  59. $userManager = $c->query('UserManager');
  60. $user = $userManager->get($user);
  61. $manager = \OC\Files\Filesystem::getMountManager();
  62. $view = new View();
  63. return new Root($manager, $view, $user);
  64. });
  65. $this->registerService('UserManager', function($c) {
  66. /**
  67. * @var SimpleContainer $c
  68. * @var \OC\AllConfig $config
  69. */
  70. $config = $c->query('AllConfig');
  71. return new \OC\User\Manager($config);
  72. });
  73. $this->registerService('UserSession', function($c) {
  74. /**
  75. * @var SimpleContainer $c
  76. * @var \OC\User\Manager $manager
  77. */
  78. $manager = $c->query('UserManager');
  79. $userSession = new \OC\User\Session($manager, \OC::$session);
  80. $userSession->listen('\OC\User', 'preCreateUser', function ($uid, $password) {
  81. \OC_Hook::emit('OC_User', 'pre_createUser', array('run' => true, 'uid' => $uid, 'password' => $password));
  82. });
  83. $userSession->listen('\OC\User', 'postCreateUser', function ($user, $password) {
  84. /** @var $user \OC\User\User */
  85. \OC_Hook::emit('OC_User', 'post_createUser', array('uid' => $user->getUID(), 'password' => $password));
  86. });
  87. $userSession->listen('\OC\User', 'preDelete', function ($user) {
  88. /** @var $user \OC\User\User */
  89. \OC_Hook::emit('OC_User', 'pre_deleteUser', array('run' => true, 'uid' => $user->getUID()));
  90. });
  91. $userSession->listen('\OC\User', 'postDelete', function ($user) {
  92. /** @var $user \OC\User\User */
  93. \OC_Hook::emit('OC_User', 'post_deleteUser', array('uid' => $user->getUID()));
  94. });
  95. $userSession->listen('\OC\User', 'preSetPassword', function ($user, $password, $recoveryPassword) {
  96. /** @var $user \OC\User\User */
  97. \OC_Hook::emit('OC_User', 'pre_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
  98. });
  99. $userSession->listen('\OC\User', 'postSetPassword', function ($user, $password, $recoveryPassword) {
  100. /** @var $user \OC\User\User */
  101. \OC_Hook::emit('OC_User', 'post_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
  102. });
  103. $userSession->listen('\OC\User', 'preLogin', function ($uid, $password) {
  104. \OC_Hook::emit('OC_User', 'pre_login', array('run' => true, 'uid' => $uid, 'password' => $password));
  105. });
  106. $userSession->listen('\OC\User', 'postLogin', function ($user, $password) {
  107. /** @var $user \OC\User\User */
  108. \OC_Hook::emit('OC_User', 'post_login', array('run' => true, 'uid' => $user->getUID(), 'password' => $password));
  109. });
  110. $userSession->listen('\OC\User', 'logout', function () {
  111. \OC_Hook::emit('OC_User', 'logout', array());
  112. });
  113. return $userSession;
  114. });
  115. $this->registerService('NavigationManager', function($c) {
  116. return new \OC\NavigationManager();
  117. });
  118. $this->registerService('AllConfig', function($c) {
  119. return new \OC\AllConfig();
  120. });
  121. $this->registerService('AppConfig', function ($c) {
  122. return new \OC\AppConfig(\OC_DB::getConnection());
  123. });
  124. $this->registerService('L10NFactory', function($c) {
  125. return new \OC\L10N\Factory();
  126. });
  127. $this->registerService('URLGenerator', function($c) {
  128. /** @var $c SimpleContainer */
  129. $config = $c->query('AllConfig');
  130. return new \OC\URLGenerator($config);
  131. });
  132. $this->registerService('AppHelper', function($c) {
  133. return new \OC\AppHelper();
  134. });
  135. $this->registerService('UserCache', function($c) {
  136. return new UserCache();
  137. });
  138. $this->registerService('MemCacheFactory', function ($c) {
  139. $instanceId = \OC_Util::getInstanceId();
  140. return new \OC\Memcache\Factory($instanceId);
  141. });
  142. $this->registerService('ActivityManager', function($c) {
  143. return new ActivityManager();
  144. });
  145. $this->registerService('AvatarManager', function($c) {
  146. return new AvatarManager();
  147. });
  148. $this->registerService('JobList', function ($c) {
  149. /**
  150. * @var Server $c
  151. */
  152. $config = $c->getConfig();
  153. return new \OC\BackgroundJob\JobList($c->getDatabaseConnection(), $config);
  154. });
  155. }
  156. /**
  157. * @return \OCP\Contacts\IManager
  158. */
  159. function getContactsManager() {
  160. return $this->query('ContactsManager');
  161. }
  162. /**
  163. * The current request object holding all information about the request
  164. * currently being processed is returned from this method.
  165. * In case the current execution was not initiated by a web request null is returned
  166. *
  167. * @return \OCP\IRequest|null
  168. */
  169. function getRequest() {
  170. return $this->query('Request');
  171. }
  172. /**
  173. * Returns the preview manager which can create preview images for a given file
  174. *
  175. * @return \OCP\IPreview
  176. */
  177. function getPreviewManager() {
  178. return $this->query('PreviewManager');
  179. }
  180. /**
  181. * Returns the tag manager which can get and set tags for different object types
  182. *
  183. * @see \OCP\ITagManager::load()
  184. * @return \OCP\ITagManager
  185. */
  186. function getTagManager() {
  187. return $this->query('TagManager');
  188. }
  189. /**
  190. * Returns the avatar manager, used for avatar functionality
  191. *
  192. * @return \OCP\IAvatarManager
  193. */
  194. function getAvatarManager() {
  195. return $this->query('AvatarManager');
  196. }
  197. /**
  198. * Returns the root folder of ownCloud's data directory
  199. *
  200. * @return \OCP\Files\Folder
  201. */
  202. function getRootFolder() {
  203. return $this->query('RootFolder');
  204. }
  205. /**
  206. * Returns a view to ownCloud's files folder
  207. *
  208. * @return \OCP\Files\Folder
  209. */
  210. function getUserFolder() {
  211. $dir = '/files';
  212. $root = $this->getRootFolder();
  213. $folder = null;
  214. if(!$root->nodeExists($dir)) {
  215. $folder = $root->newFolder($dir);
  216. } else {
  217. $folder = $root->get($dir);
  218. }
  219. return $folder;
  220. }
  221. /**
  222. * Returns an app-specific view in ownClouds data directory
  223. *
  224. * @return \OCP\Files\Folder
  225. */
  226. function getAppFolder() {
  227. $dir = '/' . \OC_App::getCurrentApp();
  228. $root = $this->getRootFolder();
  229. $folder = null;
  230. if(!$root->nodeExists($dir)) {
  231. $folder = $root->newFolder($dir);
  232. } else {
  233. $folder = $root->get($dir);
  234. }
  235. return $folder;
  236. }
  237. /**
  238. * @return \OC\User\Manager
  239. */
  240. function getUserManager() {
  241. return $this->query('UserManager');
  242. }
  243. /**
  244. * @return \OC\User\Session
  245. */
  246. function getUserSession() {
  247. return $this->query('UserSession');
  248. }
  249. /**
  250. * @return \OC\NavigationManager
  251. */
  252. function getNavigationManager() {
  253. return $this->query('NavigationManager');
  254. }
  255. /**
  256. * @return \OCP\IConfig
  257. */
  258. function getConfig() {
  259. return $this->query('AllConfig');
  260. }
  261. /**
  262. * Returns the app config manager
  263. *
  264. * @return \OCP\IAppConfig
  265. */
  266. function getAppConfig(){
  267. return $this->query('AppConfig');
  268. }
  269. /**
  270. * get an L10N instance
  271. * @param $app string appid
  272. * @return \OC_L10N
  273. */
  274. function getL10N($app) {
  275. return $this->query('L10NFactory')->get($app);
  276. }
  277. /**
  278. * @return \OC\URLGenerator
  279. */
  280. function getURLGenerator() {
  281. return $this->query('URLGenerator');
  282. }
  283. /**
  284. * @return \OC\Helper
  285. */
  286. function getHelper() {
  287. return $this->query('AppHelper');
  288. }
  289. /**
  290. * Returns an ICache instance
  291. *
  292. * @return \OCP\ICache
  293. */
  294. function getCache() {
  295. return $this->query('UserCache');
  296. }
  297. /**
  298. * Returns an \OCP\CacheFactory instance
  299. *
  300. * @return \OCP\CacheFactory
  301. */
  302. function getMemCacheFactory() {
  303. return $this->query('MemCacheFactory');
  304. }
  305. /**
  306. * Returns the current session
  307. *
  308. * @return \OCP\ISession
  309. */
  310. function getSession() {
  311. return \OC::$session;
  312. }
  313. /**
  314. * Returns the current session
  315. *
  316. * @return \OCP\IDBConnection
  317. */
  318. function getDatabaseConnection() {
  319. return new ConnectionWrapper(\OC_DB::getConnection());
  320. }
  321. /**
  322. * Returns the activity manager
  323. *
  324. * @return \OCP\Activity\IManager
  325. */
  326. function getActivityManager() {
  327. return $this->query('ActivityManager');
  328. }
  329. /**
  330. * Returns an job list for controlling background jobs
  331. *
  332. * @return \OCP\BackgroundJob\IJobList
  333. */
  334. function getJobList(){
  335. return $this->query('JobList');
  336. }
  337. }