Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Application.php « AppInfo « lib - github.com/nextcloud/jsxc.nextcloud.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 64c09d40c042d70794a0da4cd66e59acf8a60ef2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php

namespace OCA\OJSXC\AppInfo;

use OCA\OJSXC\Controller\ManagedServerController;
use OCA\OJSXC\Controller\SettingsController;
use OCA\OJSXC\Controller\ExternalApiController;
use OCA\OJSXC\Controller\JavascriptController;
use OCA\OJSXC\Middleware\ExternalApiMiddleware;
use OCA\OJSXC\Command\RefreshRoster;
use OCA\OJSXC\Command\ServerSharing;
use OCA\OJSXC\Controller\HttpBindController;
use OCA\OJSXC\Db\IQRosterPushMapper;
use OCA\OJSXC\Db\MessageMapper;
use OCA\OJSXC\Db\PresenceMapper;
use OCA\OJSXC\Db\StanzaMapper;
use OCA\OJSXC\Migration\RefreshRoster as RefreshRosterMigration;
use OCA\OJSXC\NewContentContainer;
use OCA\OJSXC\RosterPush;
use OCA\OJSXC\StanzaHandlers\IQ;
use OCA\OJSXC\StanzaHandlers\Message;
use OCA\OJSXC\StanzaHandlers\Presence;
use OCA\OJSXC\StanzaLogger;
use OCA\OJSXC\RawRequest;
use OCA\OJSXC\DataRetriever;
use OCA\OJSXC\ILock;
use OCA\OJSXC\DbLock;
use OCA\OJSXC\MemLock;
use OCA\OJSXC\Hooks;
use OCA\OJSXC\UserManagerUserProvider;
use OCA\OJSXC\ContactsStoreUserProvider;
use OCA\OJSXC\Config;
use OCP\IContainer;
use OCP\IRequest;
use OCP\IUserBackend;
use OCA\OJSXC\Migration\MigrateConfig;
use OCP\AppFramework\App;

class Application extends App
{
	const INTERNAL = 'internal';
	const EXTERNAL = 'external';
	const MANAGED = 'managed';

	private static $config = [];

	public function __construct(array $urlParams = [])
	{
		parent::__construct('ojsxc', $urlParams);
		$container = $this->getContainer();

		/** @var $config \OCP\IConfig */
		$configManager = $container->query(\OCP\IConfig::class);

		self::$config['polling'] = $configManager->getSystemValue(
			'ojsxc.polling',
			['sleep_time' => 1, 'max_cycles' => 10]
		);

		self::$config['polling']['timeout'] = self::$config['polling']['sleep_time'] * self::$config['polling']['max_cycles'] + 5;

		self::$config['use_memcache'] = $configManager->getSystemValue(
			'ojsxc.use_memcache',
			['locking' => false]
		);


		/**
		 * Controllers
		 */
		$container->registerService('HttpBindController', function (IContainer $c) {
			return new HttpBindController(
				$c->query('AppName'),
				$c->query('Request'),
				$c->query('UserId'),
				$c->query('StanzaMapper'),
				$c->query('IQHandler'),
				$c->query('MessageHandler'),
				$c->query('Host'),
				$this->getLock(),
				$c->query(\OCP\ILogger::class),
				$c->query('PresenceHandler'),
				$c->query('PresenceMapper'),
				file_get_contents("php://input"),
				self::$config['polling']['sleep_time'],
				self::$config['polling']['max_cycles'],
				$c->query('NewContentContainer'),
				$c->query('StanzaLogger')
			);
		});

		$container->registerService('SettingsController', function (IContainer $c) {
			return new SettingsController(
				$c->query('AppName'),
				$c->query('Request'),
				$c->query('Config'),
				$c->query(\OCP\IUserManager::class),
				\OC::$server->getUserSession()
			);
		});

		$container->registerService('ExternalApiController', function (IContainer $c) {
			return new ExternalApiController(
				$c->query('AppName'),
				$c->query('Request'),
				$c->query(\OCP\IUserManager::class),
				$c->query(\OCP\IUserSession::class),
				$c->query(\OCP\IGroupManager::class),
				$c->query(\OCP\ILogger::class)
			);
		});

		$container->registerService('ManagedServerController', function (IContainer $c) {
			return new ManagedServerController(
				$c->query('AppName'),
				$c->query('Request'),
				$c->query(\OCP\IURLGenerator::class),
				$c->query('Config'),
				$c->query(\OCP\IUserSession::class),
				$c->query(\OCP\ILogger::class),
				$c->query('DataRetriever'),
				$c->query(\OCP\Security\ISecureRandom::class),
				$c->query(\OCP\App\IAppManager::class),
				'https://xmpp.jsxc.ch/registration'
			);
		});

		$container->registerService('JavascriptController', function (IContainer $c) {
			return new JavascriptController(
				$c->query('AppName'),
				$c->query('Request'),
				$c->query('Config')
			);
		});

		/**
		 * Middleware
		 */

		$container->registerService('ExternalApiMiddleware', function (IContainer $c) {
			return new ExternalApiMiddleware(
				$c->query('Request'),
				$c->query(\OCP\IConfig::class),
				$c->query('RawRequest')
			);
		});
		$container->registerMiddleware('ExternalApiMiddleware');

		/**
		 * Database Layer
		 */
		$container->registerService('MessageMapper', function (IContainer $c) use ($container) {
			return new MessageMapper(
				$container->getServer()->getDatabaseConnection(),
				$c->query('Host'),
				$c->query('StanzaLogger')
			);
		});

		$container->registerService('IQRosterPushMapper', function (IContainer $c) use ($container) {
			return new IQRosterPushMapper(
				$container->getServer()->getDatabaseConnection(),
				$c->query('Host'),
				$c->query('StanzaLogger')
			);
		});

		$container->registerService('StanzaMapper', function (IContainer $c) use ($container) {
			return new StanzaMapper(
				$container->getServer()->getDatabaseConnection(),
				$c->query('Host'),
				$c->query('StanzaLogger')
			);
		});

		$container->registerService('PresenceMapper', function (IContainer $c) use ($container) {
			return new PresenceMapper(
				$container->getServer()->getDatabaseConnection(),
				$c->query('Host'),
				$c->query('UserId'),
				$c->query('MessageMapper'),
				$c->query('NewContentContainer'),
				self::$config['polling']['timeout'],
				$c->query('UserProvider')
			);
		});


		/**
		 * XMPP Stanza Handlers
		 */
		$container->registerService('IQHandler', function (IContainer $c) {
			return new IQ(
				$c->query('UserId'),
				$c->query('Host'),
				$c->query(\OCP\IUserManager::class),
				$c->query(\OCP\IConfig::class),
				$c->query('UserProvider')
			);
		});

		/**
		 * Config values
		 */
		$container->registerService('Host', function (IContainer $c) {
			/** @var IRequest $request */
			$request = $c->query('Request');
			return preg_replace('/:\d+$/', '', $request->getServerHost());
		});

		/**
		 * Helpers
		 */
		$container->registerService('Config', function (IContainer $c) {
			return new Config(
				$c->query('AppName'),
				$c->query(\OCP\IConfig::class)
			);
		});

		$container->registerService('NewContentContainer', function () {
			return new NewContentContainer();
		});

		$container->registerService('StanzaLogger', function (IContainer $c) {
			return new StanzaLogger(
				$c->query('\OCP\ILogger'),
				$c->query('UserId')
			);
		});

		$container->registerService('UserProvider', function (IContainer $c) {
			if (self::contactsStoreApiSupported()) {
				return new ContactsStoreUserProvider(
					$c->query(\OCP\Contacts\ContactsMenu\IContactsStore::class),
					$c->query('ServerContainer')->getUserSession(),
					$c->query('ServerContainer')->getUserManager(),
					$c->query(\OCP\IGroupManager::class),
					$c->query(\OCP\IConfig::class)
				);
			} else {
				return new UserManagerUserProvider(
					$c->query('ServerContainer')->getUserManager()
				);
			}
		});


		/**
		 * Commands
		 */
		$container->registerService('RefreshRosterCommand', function ($c) {
			return new RefreshRoster(
				$c->query('ServerContainer')->getUserManager(),
				$c->query('RosterPush'),
				$c->query('PresenceMapper')
			);
		});

		$container->registerService('ServerSharingCommand', function ($c) {
			return new ServerSharing(
				$c->query(\OCP\IConfig::class)
			);
		});
	}

	/**
	 * @return ILock
	 */
	private function getLock()
	{
		$c = $this->getContainer();
		if (self::$config['use_memcache']['locking'] === true) {
			$cache = $c->getServer()->getMemCacheFactory();
			$version = \OC::$server->getSession()->get('OC_Version');
			if ($version[0] === 8 && $version[1] === 0) {
				$c->getServer()->getLogger()->warning('OJSXC is configured to use memcache as backend for locking, but ownCloud version 8  doesn\'t suppor this.');
			} elseif ($cache->isAvailable()) {
				$memcache = $cache->create('ojsxc');
				return new MemLock(
					$c->query('UserId'),
					$memcache
				);
			} else {
				$c->getServer()->getLogger()->warning('OJSXC is configured to use memcache as backend for locking, but no memcache is available.');
			}
		}

		// default
		return new DbLock(
			$c->query('UserId'),
			$c->query(\OCP\IConfig::class),
			$c->getServer()->getDatabaseConnection()
		);
	}


	public static function sanitizeUserId($providedUid)
	{
		return str_replace(
			[" ", "'", "@"],
			["_ojsxc_esc_space_", "_ojsxc_squote_space_", "_ojsxc_esc_at_"],
			$providedUid
		);
	}

	public static function deSanitize($providedUid)
	{
		return str_replace(
			["_ojsxc_esc_space_", "_ojsxc_squote_space_", "_ojsxc_esc_at_"],
			[" ", "'", "@"],
			$providedUid
		);
	}


	public static function convertToRealUID($providedUid)
	{
		$user = \OC::$server->getUserManager()->get($providedUid);
		if (is_null($user)) {
			return $providedUid;
		}

		$backends = \OC::$server->getUserManager()->getBackends();
		foreach ($backends as $backend) {
			if ($backend instanceof IUserBackend) {
				$backendName = $backend->getBackendName();
			} else {
				$backendName = get_class($backend);
			}
			if ($backendName === $user->getBackendClassName()) {
				if (method_exists($backend, 'loginName2UserName')) {
					$uid = $backend->loginName2UserName($providedUid);
					if ($uid !== false) {
						return $uid;
					}
				}
			}
		}

		return $providedUid;
	}

	/**
	 * @return bool whether the ContactsStore API is enabled
	 */
	public static function contactsStoreApiSupported()
	{
		$version = \OCP\Util::getVersion();
		if ($version[0] >= 13 && \OC::$server->getConfig()->getAppValue('ojsxc', 'use_server_sharing_settings', 'no') === 'yes') {
			// ContactsStore API is supported and feature is enabled
			return true;
		}
		return false;
	}

	public static function getServerType()
	{
		return \OC::$server->getConfig()->getAppValue('ojsxc', Config::XMPP_SERVER_TYPE, self::INTERNAL);
	}
}