Blame view

sources/lib/private/server.php 11.3 KB
31b7f2792   Kload   Upgrade to ownclo...
1
2
3
4
5
  <?php
  
  namespace OC;
  
  use OC\AppFramework\Http\Request;
6d9380f96   Cédric Dupont   Update sources OC...
6
  use OC\AppFramework\Db\Db;
31b7f2792   Kload   Upgrade to ownclo...
7
8
9
10
11
12
13
14
15
  use OC\AppFramework\Utility\SimpleContainer;
  use OC\Cache\UserCache;
  use OC\DB\ConnectionWrapper;
  use OC\Files\Node\Root;
  use OC\Files\View;
  use OCP\IServerContainer;
  
  /**
   * Class Server
f7d878ff1   kload   [enh] Update to 7...
16
   *
31b7f2792   Kload   Upgrade to ownclo...
17
18
19
20
21
22
23
   * @package OC
   *
   * TODO: hookup all manager classes
   */
  class Server extends SimpleContainer implements IServerContainer {
  
  	function __construct() {
f7d878ff1   kload   [enh] Update to 7...
24
  		$this->registerService('ContactsManager', function ($c) {
31b7f2792   Kload   Upgrade to ownclo...
25
26
  			return new ContactsManager();
  		});
f7d878ff1   kload   [enh] Update to 7...
27
  		$this->registerService('Request', function ($c) {
31b7f2792   Kload   Upgrade to ownclo...
28
29
30
31
32
33
34
  			if (isset($c['urlParams'])) {
  				$urlParams = $c['urlParams'];
  			} else {
  				$urlParams = array();
  			}
  
  			if (\OC::$session->exists('requesttoken')) {
6d9380f96   Cédric Dupont   Update sources OC...
35
  				$requestToken = \OC::$session->get('requesttoken');
31b7f2792   Kload   Upgrade to ownclo...
36
  			} else {
6d9380f96   Cédric Dupont   Update sources OC...
37
  				$requestToken = false;
31b7f2792   Kload   Upgrade to ownclo...
38
  			}
837968727   Kload   [enh] Upgrade to ...
39
  			if (defined('PHPUNIT_RUN') && PHPUNIT_RUN
f7d878ff1   kload   [enh] Update to 7...
40
41
  				&& in_array('fakeinput', stream_get_wrappers())
  			) {
837968727   Kload   [enh] Upgrade to ...
42
43
44
45
  				$stream = 'fakeinput://data';
  			} else {
  				$stream = 'php://input';
  			}
31b7f2792   Kload   Upgrade to ownclo...
46
47
48
49
50
51
52
53
54
  			return new Request(
  				array(
  					'get' => $_GET,
  					'post' => $_POST,
  					'files' => $_FILES,
  					'server' => $_SERVER,
  					'env' => $_ENV,
  					'cookies' => $_COOKIE,
  					'method' => (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD']))
f7d878ff1   kload   [enh] Update to 7...
55
56
  							? $_SERVER['REQUEST_METHOD']
  							: null,
31b7f2792   Kload   Upgrade to ownclo...
57
  					'urlParams' => $urlParams,
6d9380f96   Cédric Dupont   Update sources OC...
58
  					'requesttoken' => $requestToken,
837968727   Kload   [enh] Upgrade to ...
59
  				), $stream
31b7f2792   Kload   Upgrade to ownclo...
60
61
  			);
  		});
f7d878ff1   kload   [enh] Update to 7...
62
  		$this->registerService('PreviewManager', function ($c) {
31b7f2792   Kload   Upgrade to ownclo...
63
64
  			return new PreviewManager();
  		});
f7d878ff1   kload   [enh] Update to 7...
65
  		$this->registerService('TagManager', function ($c) {
31b7f2792   Kload   Upgrade to ownclo...
66
67
68
  			$user = \OC_User::getUser();
  			return new TagManager($user);
  		});
f7d878ff1   kload   [enh] Update to 7...
69
  		$this->registerService('RootFolder', function ($c) {
31b7f2792   Kload   Upgrade to ownclo...
70
71
72
73
74
75
76
77
78
  			// TODO: get user and user manager from container as well
  			$user = \OC_User::getUser();
  			/** @var $c SimpleContainer */
  			$userManager = $c->query('UserManager');
  			$user = $userManager->get($user);
  			$manager = \OC\Files\Filesystem::getMountManager();
  			$view = new View();
  			return new Root($manager, $view, $user);
  		});
f7d878ff1   kload   [enh] Update to 7...
79
  		$this->registerService('UserManager', function ($c) {
a293d369c   Kload   Update sources to...
80
81
82
83
84
85
  			/**
  			 * @var SimpleContainer $c
  			 * @var \OC\AllConfig $config
  			 */
  			$config = $c->query('AllConfig');
  			return new \OC\User\Manager($config);
31b7f2792   Kload   Upgrade to ownclo...
86
  		});
f7d878ff1   kload   [enh] Update to 7...
87
88
89
90
91
92
93
94
95
  		$this->registerService('GroupManager', function ($c) {
  			/**
  			 * @var SimpleContainer $c
  			 * @var \OC\User\Manager $userManager
  			 */
  			$userManager = $c->query('UserManager');
  			return new \OC\Group\Manager($userManager);
  		});
  		$this->registerService('UserSession', function ($c) {
a293d369c   Kload   Update sources to...
96
97
98
99
  			/**
  			 * @var SimpleContainer $c
  			 * @var \OC\User\Manager $manager
  			 */
31b7f2792   Kload   Upgrade to ownclo...
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  			$manager = $c->query('UserManager');
  			$userSession = new \OC\User\Session($manager, \OC::$session);
  			$userSession->listen('\OC\User', 'preCreateUser', function ($uid, $password) {
  				\OC_Hook::emit('OC_User', 'pre_createUser', array('run' => true, 'uid' => $uid, 'password' => $password));
  			});
  			$userSession->listen('\OC\User', 'postCreateUser', function ($user, $password) {
  				/** @var $user \OC\User\User */
  				\OC_Hook::emit('OC_User', 'post_createUser', array('uid' => $user->getUID(), 'password' => $password));
  			});
  			$userSession->listen('\OC\User', 'preDelete', function ($user) {
  				/** @var $user \OC\User\User */
  				\OC_Hook::emit('OC_User', 'pre_deleteUser', array('run' => true, 'uid' => $user->getUID()));
  			});
  			$userSession->listen('\OC\User', 'postDelete', function ($user) {
  				/** @var $user \OC\User\User */
  				\OC_Hook::emit('OC_User', 'post_deleteUser', array('uid' => $user->getUID()));
  			});
  			$userSession->listen('\OC\User', 'preSetPassword', function ($user, $password, $recoveryPassword) {
  				/** @var $user \OC\User\User */
  				\OC_Hook::emit('OC_User', 'pre_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
  			});
  			$userSession->listen('\OC\User', 'postSetPassword', function ($user, $password, $recoveryPassword) {
  				/** @var $user \OC\User\User */
  				\OC_Hook::emit('OC_User', 'post_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
  			});
  			$userSession->listen('\OC\User', 'preLogin', function ($uid, $password) {
  				\OC_Hook::emit('OC_User', 'pre_login', array('run' => true, 'uid' => $uid, 'password' => $password));
  			});
  			$userSession->listen('\OC\User', 'postLogin', function ($user, $password) {
  				/** @var $user \OC\User\User */
  				\OC_Hook::emit('OC_User', 'post_login', array('run' => true, 'uid' => $user->getUID(), 'password' => $password));
  			});
  			$userSession->listen('\OC\User', 'logout', function () {
  				\OC_Hook::emit('OC_User', 'logout', array());
  			});
  			return $userSession;
  		});
f7d878ff1   kload   [enh] Update to 7...
137
  		$this->registerService('NavigationManager', function ($c) {
31b7f2792   Kload   Upgrade to ownclo...
138
139
  			return new \OC\NavigationManager();
  		});
f7d878ff1   kload   [enh] Update to 7...
140
  		$this->registerService('AllConfig', function ($c) {
31b7f2792   Kload   Upgrade to ownclo...
141
142
  			return new \OC\AllConfig();
  		});
6d9380f96   Cédric Dupont   Update sources OC...
143
144
145
  		$this->registerService('AppConfig', function ($c) {
  			return new \OC\AppConfig(\OC_DB::getConnection());
  		});
f7d878ff1   kload   [enh] Update to 7...
146
  		$this->registerService('L10NFactory', function ($c) {
31b7f2792   Kload   Upgrade to ownclo...
147
148
  			return new \OC\L10N\Factory();
  		});
f7d878ff1   kload   [enh] Update to 7...
149
  		$this->registerService('URLGenerator', function ($c) {
6d9380f96   Cédric Dupont   Update sources OC...
150
151
152
  			/** @var $c SimpleContainer */
  			$config = $c->query('AllConfig');
  			return new \OC\URLGenerator($config);
31b7f2792   Kload   Upgrade to ownclo...
153
  		});
f7d878ff1   kload   [enh] Update to 7...
154
  		$this->registerService('AppHelper', function ($c) {
31b7f2792   Kload   Upgrade to ownclo...
155
156
  			return new \OC\AppHelper();
  		});
f7d878ff1   kload   [enh] Update to 7...
157
  		$this->registerService('UserCache', function ($c) {
31b7f2792   Kload   Upgrade to ownclo...
158
159
  			return new UserCache();
  		});
6d9380f96   Cédric Dupont   Update sources OC...
160
161
162
163
  		$this->registerService('MemCacheFactory', function ($c) {
  			$instanceId = \OC_Util::getInstanceId();
  			return new \OC\Memcache\Factory($instanceId);
  		});
f7d878ff1   kload   [enh] Update to 7...
164
  		$this->registerService('ActivityManager', function ($c) {
31b7f2792   Kload   Upgrade to ownclo...
165
166
  			return new ActivityManager();
  		});
f7d878ff1   kload   [enh] Update to 7...
167
  		$this->registerService('AvatarManager', function ($c) {
31b7f2792   Kload   Upgrade to ownclo...
168
169
  			return new AvatarManager();
  		});
f7d878ff1   kload   [enh] Update to 7...
170
  		$this->registerService('Logger', function ($c) {
6d9380f96   Cédric Dupont   Update sources OC...
171
172
173
174
175
176
177
178
179
180
181
182
183
184
  			/** @var $c SimpleContainer */
  			$logClass = $c->query('AllConfig')->getSystemValue('log_type', 'owncloud');
  			$logger = 'OC_Log_' . ucfirst($logClass);
  			call_user_func(array($logger, 'init'));
  
  			return new Log($logger);
  		});
  		$this->registerService('JobList', function ($c) {
  			/**
  			 * @var Server $c
  			 */
  			$config = $c->getConfig();
  			return new \OC\BackgroundJob\JobList($c->getDatabaseConnection(), $config);
  		});
f7d878ff1   kload   [enh] Update to 7...
185
  		$this->registerService('Router', function ($c) {
6d9380f96   Cédric Dupont   Update sources OC...
186
187
188
189
190
191
192
193
194
195
196
  			/**
  			 * @var Server $c
  			 */
  			$cacheFactory = $c->getMemCacheFactory();
  			if ($cacheFactory->isAvailable()) {
  				$router = new \OC\Route\CachingRouter($cacheFactory->create('route'));
  			} else {
  				$router = new \OC\Route\Router();
  			}
  			return $router;
  		});
f7d878ff1   kload   [enh] Update to 7...
197
  		$this->registerService('Search', function ($c) {
6d9380f96   Cédric Dupont   Update sources OC...
198
199
  			return new Search();
  		});
f7d878ff1   kload   [enh] Update to 7...
200
  		$this->registerService('Db', function ($c) {
6d9380f96   Cédric Dupont   Update sources OC...
201
202
  			return new Db();
  		});
31b7f2792   Kload   Upgrade to ownclo...
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  	}
  
  	/**
  	 * @return \OCP\Contacts\IManager
  	 */
  	function getContactsManager() {
  		return $this->query('ContactsManager');
  	}
  
  	/**
  	 * The current request object holding all information about the request
  	 * currently being processed is returned from this method.
  	 * In case the current execution was not initiated by a web request null is returned
  	 *
  	 * @return \OCP\IRequest|null
  	 */
  	function getRequest() {
  		return $this->query('Request');
  	}
  
  	/**
  	 * Returns the preview manager which can create preview images for a given file
  	 *
  	 * @return \OCP\IPreview
  	 */
  	function getPreviewManager() {
  		return $this->query('PreviewManager');
  	}
  
  	/**
  	 * Returns the tag manager which can get and set tags for different object types
  	 *
  	 * @see \OCP\ITagManager::load()
  	 * @return \OCP\ITagManager
  	 */
  	function getTagManager() {
  		return $this->query('TagManager');
  	}
  
  	/**
  	 * Returns the avatar manager, used for avatar functionality
  	 *
  	 * @return \OCP\IAvatarManager
  	 */
  	function getAvatarManager() {
  		return $this->query('AvatarManager');
  	}
  
  	/**
  	 * Returns the root folder of ownCloud's data directory
  	 *
  	 * @return \OCP\Files\Folder
  	 */
  	function getRootFolder() {
  		return $this->query('RootFolder');
  	}
  
  	/**
  	 * Returns a view to ownCloud's files folder
  	 *
  	 * @return \OCP\Files\Folder
  	 */
  	function getUserFolder() {
6d9380f96   Cédric Dupont   Update sources OC...
266
267
268
269
270
  		$user = $this->getUserSession()->getUser();
  		if (!$user) {
  			return null;
  		}
  		$dir = '/' . $user->getUID();
31b7f2792   Kload   Upgrade to ownclo...
271
272
  		$root = $this->getRootFolder();
  		$folder = null;
6d9380f96   Cédric Dupont   Update sources OC...
273

f7d878ff1   kload   [enh] Update to 7...
274
  		if (!$root->nodeExists($dir)) {
31b7f2792   Kload   Upgrade to ownclo...
275
276
277
278
  			$folder = $root->newFolder($dir);
  		} else {
  			$folder = $root->get($dir);
  		}
6d9380f96   Cédric Dupont   Update sources OC...
279
280
  
  		$dir = '/files';
f7d878ff1   kload   [enh] Update to 7...
281
  		if (!$folder->nodeExists($dir)) {
6d9380f96   Cédric Dupont   Update sources OC...
282
283
284
285
  			$folder = $folder->newFolder($dir);
  		} else {
  			$folder = $folder->get($dir);
  		}
31b7f2792   Kload   Upgrade to ownclo...
286
287
288
289
290
291
292
293
294
295
296
297
298
  		return $folder;
  	}
  
  	/**
  	 * Returns an app-specific view in ownClouds data directory
  	 *
  	 * @return \OCP\Files\Folder
  	 */
  	function getAppFolder() {
  
  		$dir = '/' . \OC_App::getCurrentApp();
  		$root = $this->getRootFolder();
  		$folder = null;
f7d878ff1   kload   [enh] Update to 7...
299
  		if (!$root->nodeExists($dir)) {
31b7f2792   Kload   Upgrade to ownclo...
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
  			$folder = $root->newFolder($dir);
  		} else {
  			$folder = $root->get($dir);
  		}
  		return $folder;
  	}
  
  	/**
  	 * @return \OC\User\Manager
  	 */
  	function getUserManager() {
  		return $this->query('UserManager');
  	}
  
  	/**
f7d878ff1   kload   [enh] Update to 7...
315
316
317
318
319
320
321
  	 * @return \OC\Group\Manager
  	 */
  	function getGroupManager() {
  		return $this->query('GroupManager');
  	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
322
323
324
325
326
327
328
329
330
331
332
333
334
335
  	 * @return \OC\User\Session
  	 */
  	function getUserSession() {
  		return $this->query('UserSession');
  	}
  
  	/**
  	 * @return \OC\NavigationManager
  	 */
  	function getNavigationManager() {
  		return $this->query('NavigationManager');
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
336
  	 * @return \OCP\IConfig
31b7f2792   Kload   Upgrade to ownclo...
337
338
339
340
341
342
  	 */
  	function getConfig() {
  		return $this->query('AllConfig');
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
343
344
345
346
  	 * Returns the app config manager
  	 *
  	 * @return \OCP\IAppConfig
  	 */
f7d878ff1   kload   [enh] Update to 7...
347
  	function getAppConfig() {
6d9380f96   Cédric Dupont   Update sources OC...
348
349
350
351
  		return $this->query('AppConfig');
  	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
352
  	 * get an L10N instance
f7d878ff1   kload   [enh] Update to 7...
353
  	 *
6d9380f96   Cédric Dupont   Update sources OC...
354
  	 * @param string $app appid
31b7f2792   Kload   Upgrade to ownclo...
355
356
357
358
359
360
361
  	 * @return \OC_L10N
  	 */
  	function getL10N($app) {
  		return $this->query('L10NFactory')->get($app);
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
362
  	 * @return \OCP\IURLGenerator
31b7f2792   Kload   Upgrade to ownclo...
363
364
365
366
367
368
  	 */
  	function getURLGenerator() {
  		return $this->query('URLGenerator');
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
369
  	 * @return \OCP\IHelper
31b7f2792   Kload   Upgrade to ownclo...
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
  	 */
  	function getHelper() {
  		return $this->query('AppHelper');
  	}
  
  	/**
  	 * Returns an ICache instance
  	 *
  	 * @return \OCP\ICache
  	 */
  	function getCache() {
  		return $this->query('UserCache');
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
385
386
387
388
389
390
391
392
393
  	 * Returns an \OCP\CacheFactory instance
  	 *
  	 * @return \OCP\ICacheFactory
  	 */
  	function getMemCacheFactory() {
  		return $this->query('MemCacheFactory');
  	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
  	 * Returns the current session
  	 *
  	 * @return \OCP\ISession
  	 */
  	function getSession() {
  		return \OC::$session;
  	}
  
  	/**
  	 * Returns the current session
  	 *
  	 * @return \OCP\IDBConnection
  	 */
  	function getDatabaseConnection() {
  		return new ConnectionWrapper(\OC_DB::getConnection());
  	}
  
  	/**
  	 * Returns the activity manager
  	 *
  	 * @return \OCP\Activity\IManager
  	 */
  	function getActivityManager() {
  		return $this->query('ActivityManager');
  	}
6d9380f96   Cédric Dupont   Update sources OC...
419
420
421
422
423
424
  
  	/**
  	 * Returns an job list for controlling background jobs
  	 *
  	 * @return \OCP\BackgroundJob\IJobList
  	 */
f7d878ff1   kload   [enh] Update to 7...
425
  	function getJobList() {
6d9380f96   Cédric Dupont   Update sources OC...
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
  		return $this->query('JobList');
  	}
  
  	/**
  	 * Returns a logger instance
  	 *
  	 * @return \OCP\ILogger
  	 */
  	function getLogger() {
  		return $this->query('Logger');
  	}
  
  	/**
  	 * Returns a router for generating and matching urls
  	 *
  	 * @return \OCP\Route\IRouter
  	 */
f7d878ff1   kload   [enh] Update to 7...
443
  	function getRouter() {
6d9380f96   Cédric Dupont   Update sources OC...
444
445
446
447
448
  		return $this->query('Router');
  	}
  
  	/**
  	 * Returns a search instance
f7d878ff1   kload   [enh] Update to 7...
449
  	 *
6d9380f96   Cédric Dupont   Update sources OC...
450
451
452
453
454
455
456
457
  	 * @return \OCP\ISearch
  	 */
  	function getSearch() {
  		return $this->query('Search');
  	}
  
  	/**
  	 * Returns an instance of the db facade
f7d878ff1   kload   [enh] Update to 7...
458
  	 *
6d9380f96   Cédric Dupont   Update sources OC...
459
460
461
462
463
  	 * @return \OCP\IDb
  	 */
  	function getDb() {
  		return $this->query('Db');
  	}
31b7f2792   Kload   Upgrade to ownclo...
464
  }