server.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. }
  129. /**
  130. * @return \OCP\Contacts\IManager
  131. */
  132. function getContactsManager() {
  133. return $this->query('ContactsManager');
  134. }
  135. /**
  136. * The current request object holding all information about the request
  137. * currently being processed is returned from this method.
  138. * In case the current execution was not initiated by a web request null is returned
  139. *
  140. * @return \OCP\IRequest|null
  141. */
  142. function getRequest() {
  143. return $this->query('Request');
  144. }
  145. /**
  146. * Returns the preview manager which can create preview images for a given file
  147. *
  148. * @return \OCP\IPreview
  149. */
  150. function getPreviewManager() {
  151. return $this->query('PreviewManager');
  152. }
  153. /**
  154. * Returns the tag manager which can get and set tags for different object types
  155. *
  156. * @see \OCP\ITagManager::load()
  157. * @return \OCP\ITagManager
  158. */
  159. function getTagManager() {
  160. return $this->query('TagManager');
  161. }
  162. /**
  163. * Returns the root folder of ownCloud's data directory
  164. *
  165. * @return \OCP\Files\Folder
  166. */
  167. function getRootFolder() {
  168. return $this->query('RootFolder');
  169. }
  170. /**
  171. * Returns a view to ownCloud's files folder
  172. *
  173. * @return \OCP\Files\Folder
  174. */
  175. function getUserFolder() {
  176. $dir = '/files';
  177. $root = $this->getRootFolder();
  178. $folder = null;
  179. if(!$root->nodeExists($dir)) {
  180. $folder = $root->newFolder($dir);
  181. } else {
  182. $folder = $root->get($dir);
  183. }
  184. return $folder;
  185. }
  186. /**
  187. * Returns an app-specific view in ownClouds data directory
  188. *
  189. * @return \OCP\Files\Folder
  190. */
  191. function getAppFolder() {
  192. $dir = '/' . \OC_App::getCurrentApp();
  193. $root = $this->getRootFolder();
  194. $folder = null;
  195. if(!$root->nodeExists($dir)) {
  196. $folder = $root->newFolder($dir);
  197. } else {
  198. $folder = $root->get($dir);
  199. }
  200. return $folder;
  201. }
  202. /**
  203. * @return \OC\User\Manager
  204. */
  205. function getUserManager() {
  206. return $this->query('UserManager');
  207. }
  208. /**
  209. * @return \OC\User\Session
  210. */
  211. function getUserSession() {
  212. return $this->query('UserSession');
  213. }
  214. /**
  215. * @return \OC\NavigationManager
  216. */
  217. function getNavigationManager() {
  218. return $this->query('NavigationManager');
  219. }
  220. /**
  221. * @return \OC\Config
  222. */
  223. function getConfig() {
  224. return $this->query('AllConfig');
  225. }
  226. /**
  227. * get an L10N instance
  228. * @param $app string appid
  229. * @return \OC_L10N
  230. */
  231. function getL10N($app) {
  232. return $this->query('L10NFactory')->get($app);
  233. }
  234. /**
  235. * @return \OC\URLGenerator
  236. */
  237. function getURLGenerator() {
  238. return $this->query('URLGenerator');
  239. }
  240. /**
  241. * @return \OC\Helper
  242. */
  243. function getHelper() {
  244. return $this->query('AppHelper');
  245. }
  246. /**
  247. * Returns an ICache instance
  248. *
  249. * @return \OCP\ICache
  250. */
  251. function getCache() {
  252. return $this->query('UserCache');
  253. }
  254. /**
  255. * Returns the current session
  256. *
  257. * @return \OCP\ISession
  258. */
  259. function getSession() {
  260. return \OC::$session;
  261. }
  262. /**
  263. * Returns the current session
  264. *
  265. * @return \OCP\IDBConnection
  266. */
  267. function getDatabaseConnection() {
  268. return new ConnectionWrapper(\OC_DB::getConnection());
  269. }
  270. /**
  271. * Returns the activity manager
  272. *
  273. * @return \OCP\Activity\IManager
  274. */
  275. function getActivityManager() {
  276. return $this->query('ActivityManager');
  277. }
  278. }