server.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. return new \OC\User\Manager();
  67. });
  68. $this->registerService('UserSession', function($c) {
  69. /** @var $c SimpleContainer */
  70. $manager = $c->query('UserManager');
  71. $userSession = new \OC\User\Session($manager, \OC::$session);
  72. $userSession->listen('\OC\User', 'preCreateUser', function ($uid, $password) {
  73. \OC_Hook::emit('OC_User', 'pre_createUser', array('run' => true, 'uid' => $uid, 'password' => $password));
  74. });
  75. $userSession->listen('\OC\User', 'postCreateUser', function ($user, $password) {
  76. /** @var $user \OC\User\User */
  77. \OC_Hook::emit('OC_User', 'post_createUser', array('uid' => $user->getUID(), 'password' => $password));
  78. });
  79. $userSession->listen('\OC\User', 'preDelete', function ($user) {
  80. /** @var $user \OC\User\User */
  81. \OC_Hook::emit('OC_User', 'pre_deleteUser', array('run' => true, 'uid' => $user->getUID()));
  82. });
  83. $userSession->listen('\OC\User', 'postDelete', function ($user) {
  84. /** @var $user \OC\User\User */
  85. \OC_Hook::emit('OC_User', 'post_deleteUser', array('uid' => $user->getUID()));
  86. });
  87. $userSession->listen('\OC\User', 'preSetPassword', function ($user, $password, $recoveryPassword) {
  88. /** @var $user \OC\User\User */
  89. \OC_Hook::emit('OC_User', 'pre_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
  90. });
  91. $userSession->listen('\OC\User', 'postSetPassword', function ($user, $password, $recoveryPassword) {
  92. /** @var $user \OC\User\User */
  93. \OC_Hook::emit('OC_User', 'post_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
  94. });
  95. $userSession->listen('\OC\User', 'preLogin', function ($uid, $password) {
  96. \OC_Hook::emit('OC_User', 'pre_login', array('run' => true, 'uid' => $uid, 'password' => $password));
  97. });
  98. $userSession->listen('\OC\User', 'postLogin', function ($user, $password) {
  99. /** @var $user \OC\User\User */
  100. \OC_Hook::emit('OC_User', 'post_login', array('run' => true, 'uid' => $user->getUID(), 'password' => $password));
  101. });
  102. $userSession->listen('\OC\User', 'logout', function () {
  103. \OC_Hook::emit('OC_User', 'logout', array());
  104. });
  105. return $userSession;
  106. });
  107. $this->registerService('NavigationManager', function($c) {
  108. return new \OC\NavigationManager();
  109. });
  110. $this->registerService('AllConfig', function($c) {
  111. return new \OC\AllConfig();
  112. });
  113. $this->registerService('L10NFactory', function($c) {
  114. return new \OC\L10N\Factory();
  115. });
  116. $this->registerService('URLGenerator', function($c) {
  117. return new \OC\URLGenerator();
  118. });
  119. $this->registerService('AppHelper', function($c) {
  120. return new \OC\AppHelper();
  121. });
  122. $this->registerService('UserCache', function($c) {
  123. return new UserCache();
  124. });
  125. $this->registerService('ActivityManager', function($c) {
  126. return new ActivityManager();
  127. });
  128. $this->registerService('AvatarManager', function($c) {
  129. return new AvatarManager();
  130. });
  131. }
  132. /**
  133. * @return \OCP\Contacts\IManager
  134. */
  135. function getContactsManager() {
  136. return $this->query('ContactsManager');
  137. }
  138. /**
  139. * The current request object holding all information about the request
  140. * currently being processed is returned from this method.
  141. * In case the current execution was not initiated by a web request null is returned
  142. *
  143. * @return \OCP\IRequest|null
  144. */
  145. function getRequest() {
  146. return $this->query('Request');
  147. }
  148. /**
  149. * Returns the preview manager which can create preview images for a given file
  150. *
  151. * @return \OCP\IPreview
  152. */
  153. function getPreviewManager() {
  154. return $this->query('PreviewManager');
  155. }
  156. /**
  157. * Returns the tag manager which can get and set tags for different object types
  158. *
  159. * @see \OCP\ITagManager::load()
  160. * @return \OCP\ITagManager
  161. */
  162. function getTagManager() {
  163. return $this->query('TagManager');
  164. }
  165. /**
  166. * Returns the avatar manager, used for avatar functionality
  167. *
  168. * @return \OCP\IAvatarManager
  169. */
  170. function getAvatarManager() {
  171. return $this->query('AvatarManager');
  172. }
  173. /**
  174. * Returns the root folder of ownCloud's data directory
  175. *
  176. * @return \OCP\Files\Folder
  177. */
  178. function getRootFolder() {
  179. return $this->query('RootFolder');
  180. }
  181. /**
  182. * Returns a view to ownCloud's files folder
  183. *
  184. * @return \OCP\Files\Folder
  185. */
  186. function getUserFolder() {
  187. $dir = '/files';
  188. $root = $this->getRootFolder();
  189. $folder = null;
  190. if(!$root->nodeExists($dir)) {
  191. $folder = $root->newFolder($dir);
  192. } else {
  193. $folder = $root->get($dir);
  194. }
  195. return $folder;
  196. }
  197. /**
  198. * Returns an app-specific view in ownClouds data directory
  199. *
  200. * @return \OCP\Files\Folder
  201. */
  202. function getAppFolder() {
  203. $dir = '/' . \OC_App::getCurrentApp();
  204. $root = $this->getRootFolder();
  205. $folder = null;
  206. if(!$root->nodeExists($dir)) {
  207. $folder = $root->newFolder($dir);
  208. } else {
  209. $folder = $root->get($dir);
  210. }
  211. return $folder;
  212. }
  213. /**
  214. * @return \OC\User\Manager
  215. */
  216. function getUserManager() {
  217. return $this->query('UserManager');
  218. }
  219. /**
  220. * @return \OC\User\Session
  221. */
  222. function getUserSession() {
  223. return $this->query('UserSession');
  224. }
  225. /**
  226. * @return \OC\NavigationManager
  227. */
  228. function getNavigationManager() {
  229. return $this->query('NavigationManager');
  230. }
  231. /**
  232. * @return \OC\Config
  233. */
  234. function getConfig() {
  235. return $this->query('AllConfig');
  236. }
  237. /**
  238. * get an L10N instance
  239. * @param $app string appid
  240. * @return \OC_L10N
  241. */
  242. function getL10N($app) {
  243. return $this->query('L10NFactory')->get($app);
  244. }
  245. /**
  246. * @return \OC\URLGenerator
  247. */
  248. function getURLGenerator() {
  249. return $this->query('URLGenerator');
  250. }
  251. /**
  252. * @return \OC\Helper
  253. */
  254. function getHelper() {
  255. return $this->query('AppHelper');
  256. }
  257. /**
  258. * Returns an ICache instance
  259. *
  260. * @return \OCP\ICache
  261. */
  262. function getCache() {
  263. return $this->query('UserCache');
  264. }
  265. /**
  266. * Returns the current session
  267. *
  268. * @return \OCP\ISession
  269. */
  270. function getSession() {
  271. return \OC::$session;
  272. }
  273. /**
  274. * Returns the current session
  275. *
  276. * @return \OCP\IDBConnection
  277. */
  278. function getDatabaseConnection() {
  279. return new ConnectionWrapper(\OC_DB::getConnection());
  280. }
  281. /**
  282. * Returns the activity manager
  283. *
  284. * @return \OCP\Activity\IManager
  285. */
  286. function getActivityManager() {
  287. return $this->query('ActivityManager');
  288. }
  289. }