Completed
Push — master ( cf3ffb...1a07c5 )
by John
21:12 queued 13s
created
core/Controller/AvatarController.php 1 patch
Indentation   +294 added lines, -294 removed lines patch added patch discarded remove patch
@@ -37,322 +37,322 @@
 block discarded – undo
37 37
  * @package OC\Core\Controller
38 38
  */
39 39
 class AvatarController extends Controller {
40
-	public function __construct(
41
-		string $appName,
42
-		IRequest $request,
43
-		protected IAvatarManager $avatarManager,
44
-		protected ICache $cache,
45
-		protected IL10N $l10n,
46
-		protected IUserManager $userManager,
47
-		protected IRootFolder $rootFolder,
48
-		protected LoggerInterface $logger,
49
-		protected ?string $userId,
50
-		protected TimeFactory $timeFactory,
51
-		protected GuestAvatarController $guestAvatarController,
52
-	) {
53
-		parent::__construct($appName, $request);
54
-	}
40
+    public function __construct(
41
+        string $appName,
42
+        IRequest $request,
43
+        protected IAvatarManager $avatarManager,
44
+        protected ICache $cache,
45
+        protected IL10N $l10n,
46
+        protected IUserManager $userManager,
47
+        protected IRootFolder $rootFolder,
48
+        protected LoggerInterface $logger,
49
+        protected ?string $userId,
50
+        protected TimeFactory $timeFactory,
51
+        protected GuestAvatarController $guestAvatarController,
52
+    ) {
53
+        parent::__construct($appName, $request);
54
+    }
55 55
 
56
-	/**
57
-	 * @NoSameSiteCookieRequired
58
-	 *
59
-	 * Get the dark avatar
60
-	 *
61
-	 * @param string $userId ID of the user
62
-	 * @param 64|512 $size Size of the avatar
63
-	 * @param bool $guestFallback Fallback to guest avatar if not found
64
-	 * @return FileDisplayResponse<Http::STATUS_OK|Http::STATUS_CREATED, array{Content-Type: string, X-NC-IsCustomAvatar: int}>|JSONResponse<Http::STATUS_NOT_FOUND, list<empty>, array{}>|Response<Http::STATUS_INTERNAL_SERVER_ERROR, array{}>
65
-	 *
66
-	 * 200: Avatar returned
67
-	 * 201: Avatar returned
68
-	 * 404: Avatar not found
69
-	 */
70
-	#[NoCSRFRequired]
71
-	#[PublicPage]
72
-	#[FrontpageRoute(verb: 'GET', url: '/avatar/{userId}/{size}/dark')]
73
-	#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT)]
74
-	public function getAvatarDark(string $userId, int $size, bool $guestFallback = false) {
75
-		if ($size <= 64) {
76
-			if ($size !== 64) {
77
-				$this->logger->debug('Avatar requested in deprecated size ' . $size);
78
-			}
79
-			$size = 64;
80
-		} else {
81
-			if ($size !== 512) {
82
-				$this->logger->debug('Avatar requested in deprecated size ' . $size);
83
-			}
84
-			$size = 512;
85
-		}
56
+    /**
57
+     * @NoSameSiteCookieRequired
58
+     *
59
+     * Get the dark avatar
60
+     *
61
+     * @param string $userId ID of the user
62
+     * @param 64|512 $size Size of the avatar
63
+     * @param bool $guestFallback Fallback to guest avatar if not found
64
+     * @return FileDisplayResponse<Http::STATUS_OK|Http::STATUS_CREATED, array{Content-Type: string, X-NC-IsCustomAvatar: int}>|JSONResponse<Http::STATUS_NOT_FOUND, list<empty>, array{}>|Response<Http::STATUS_INTERNAL_SERVER_ERROR, array{}>
65
+     *
66
+     * 200: Avatar returned
67
+     * 201: Avatar returned
68
+     * 404: Avatar not found
69
+     */
70
+    #[NoCSRFRequired]
71
+    #[PublicPage]
72
+    #[FrontpageRoute(verb: 'GET', url: '/avatar/{userId}/{size}/dark')]
73
+    #[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT)]
74
+    public function getAvatarDark(string $userId, int $size, bool $guestFallback = false) {
75
+        if ($size <= 64) {
76
+            if ($size !== 64) {
77
+                $this->logger->debug('Avatar requested in deprecated size ' . $size);
78
+            }
79
+            $size = 64;
80
+        } else {
81
+            if ($size !== 512) {
82
+                $this->logger->debug('Avatar requested in deprecated size ' . $size);
83
+            }
84
+            $size = 512;
85
+        }
86 86
 
87
-		try {
88
-			$avatar = $this->avatarManager->getAvatar($userId);
89
-			$avatarFile = $avatar->getFile($size, true);
90
-			$response = new FileDisplayResponse(
91
-				$avatarFile,
92
-				Http::STATUS_OK,
93
-				['Content-Type' => $avatarFile->getMimeType(), 'X-NC-IsCustomAvatar' => (int)$avatar->isCustomAvatar()]
94
-			);
95
-		} catch (\Exception $e) {
96
-			if ($guestFallback) {
97
-				return $this->guestAvatarController->getAvatarDark($userId, $size);
98
-			}
99
-			return new JSONResponse([], Http::STATUS_NOT_FOUND);
100
-		}
87
+        try {
88
+            $avatar = $this->avatarManager->getAvatar($userId);
89
+            $avatarFile = $avatar->getFile($size, true);
90
+            $response = new FileDisplayResponse(
91
+                $avatarFile,
92
+                Http::STATUS_OK,
93
+                ['Content-Type' => $avatarFile->getMimeType(), 'X-NC-IsCustomAvatar' => (int)$avatar->isCustomAvatar()]
94
+            );
95
+        } catch (\Exception $e) {
96
+            if ($guestFallback) {
97
+                return $this->guestAvatarController->getAvatarDark($userId, $size);
98
+            }
99
+            return new JSONResponse([], Http::STATUS_NOT_FOUND);
100
+        }
101 101
 
102
-		// Cache for 1 day
103
-		$response->cacheFor(60 * 60 * 24, false, true);
104
-		return $response;
105
-	}
102
+        // Cache for 1 day
103
+        $response->cacheFor(60 * 60 * 24, false, true);
104
+        return $response;
105
+    }
106 106
 
107 107
 
108
-	/**
109
-	 * @NoSameSiteCookieRequired
110
-	 *
111
-	 * Get the avatar
112
-	 *
113
-	 * @param string $userId ID of the user
114
-	 * @param 64|512 $size Size of the avatar
115
-	 * @param bool $guestFallback Fallback to guest avatar if not found
116
-	 * @return FileDisplayResponse<Http::STATUS_OK|Http::STATUS_CREATED, array{Content-Type: string, X-NC-IsCustomAvatar: int}>|JSONResponse<Http::STATUS_NOT_FOUND, list<empty>, array{}>|Response<Http::STATUS_INTERNAL_SERVER_ERROR, array{}>
117
-	 *
118
-	 * 200: Avatar returned
119
-	 * 201: Avatar returned
120
-	 * 404: Avatar not found
121
-	 */
122
-	#[NoCSRFRequired]
123
-	#[PublicPage]
124
-	#[FrontpageRoute(verb: 'GET', url: '/avatar/{userId}/{size}')]
125
-	#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT)]
126
-	public function getAvatar(string $userId, int $size, bool $guestFallback = false) {
127
-		if ($size <= 64) {
128
-			if ($size !== 64) {
129
-				$this->logger->debug('Avatar requested in deprecated size ' . $size);
130
-			}
131
-			$size = 64;
132
-		} else {
133
-			if ($size !== 512) {
134
-				$this->logger->debug('Avatar requested in deprecated size ' . $size);
135
-			}
136
-			$size = 512;
137
-		}
108
+    /**
109
+     * @NoSameSiteCookieRequired
110
+     *
111
+     * Get the avatar
112
+     *
113
+     * @param string $userId ID of the user
114
+     * @param 64|512 $size Size of the avatar
115
+     * @param bool $guestFallback Fallback to guest avatar if not found
116
+     * @return FileDisplayResponse<Http::STATUS_OK|Http::STATUS_CREATED, array{Content-Type: string, X-NC-IsCustomAvatar: int}>|JSONResponse<Http::STATUS_NOT_FOUND, list<empty>, array{}>|Response<Http::STATUS_INTERNAL_SERVER_ERROR, array{}>
117
+     *
118
+     * 200: Avatar returned
119
+     * 201: Avatar returned
120
+     * 404: Avatar not found
121
+     */
122
+    #[NoCSRFRequired]
123
+    #[PublicPage]
124
+    #[FrontpageRoute(verb: 'GET', url: '/avatar/{userId}/{size}')]
125
+    #[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT)]
126
+    public function getAvatar(string $userId, int $size, bool $guestFallback = false) {
127
+        if ($size <= 64) {
128
+            if ($size !== 64) {
129
+                $this->logger->debug('Avatar requested in deprecated size ' . $size);
130
+            }
131
+            $size = 64;
132
+        } else {
133
+            if ($size !== 512) {
134
+                $this->logger->debug('Avatar requested in deprecated size ' . $size);
135
+            }
136
+            $size = 512;
137
+        }
138 138
 
139
-		try {
140
-			$avatar = $this->avatarManager->getAvatar($userId);
141
-			$avatarFile = $avatar->getFile($size);
142
-			$response = new FileDisplayResponse(
143
-				$avatarFile,
144
-				Http::STATUS_OK,
145
-				['Content-Type' => $avatarFile->getMimeType(), 'X-NC-IsCustomAvatar' => (int)$avatar->isCustomAvatar()]
146
-			);
147
-		} catch (\Exception $e) {
148
-			if ($guestFallback) {
149
-				return $this->guestAvatarController->getAvatar($userId, $size);
150
-			}
151
-			return new JSONResponse([], Http::STATUS_NOT_FOUND);
152
-		}
139
+        try {
140
+            $avatar = $this->avatarManager->getAvatar($userId);
141
+            $avatarFile = $avatar->getFile($size);
142
+            $response = new FileDisplayResponse(
143
+                $avatarFile,
144
+                Http::STATUS_OK,
145
+                ['Content-Type' => $avatarFile->getMimeType(), 'X-NC-IsCustomAvatar' => (int)$avatar->isCustomAvatar()]
146
+            );
147
+        } catch (\Exception $e) {
148
+            if ($guestFallback) {
149
+                return $this->guestAvatarController->getAvatar($userId, $size);
150
+            }
151
+            return new JSONResponse([], Http::STATUS_NOT_FOUND);
152
+        }
153 153
 
154
-		// Cache for 1 day
155
-		$response->cacheFor(60 * 60 * 24, false, true);
156
-		return $response;
157
-	}
154
+        // Cache for 1 day
155
+        $response->cacheFor(60 * 60 * 24, false, true);
156
+        return $response;
157
+    }
158 158
 
159
-	#[NoAdminRequired]
160
-	#[FrontpageRoute(verb: 'POST', url: '/avatar/')]
161
-	public function postAvatar(?string $path = null): JSONResponse {
162
-		$files = $this->request->getUploadedFile('files');
159
+    #[NoAdminRequired]
160
+    #[FrontpageRoute(verb: 'POST', url: '/avatar/')]
161
+    public function postAvatar(?string $path = null): JSONResponse {
162
+        $files = $this->request->getUploadedFile('files');
163 163
 
164
-		if (isset($path)) {
165
-			$path = stripslashes($path);
166
-			$userFolder = $this->rootFolder->getUserFolder($this->userId);
167
-			/** @var File $node */
168
-			$node = $userFolder->get($path);
169
-			if (!($node instanceof File)) {
170
-				return new JSONResponse(['data' => ['message' => $this->l10n->t('Please select a file.')]]);
171
-			}
172
-			if ($node->getSize() > 20 * 1024 * 1024) {
173
-				return new JSONResponse(
174
-					['data' => ['message' => $this->l10n->t('File is too big')]],
175
-					Http::STATUS_BAD_REQUEST
176
-				);
177
-			}
164
+        if (isset($path)) {
165
+            $path = stripslashes($path);
166
+            $userFolder = $this->rootFolder->getUserFolder($this->userId);
167
+            /** @var File $node */
168
+            $node = $userFolder->get($path);
169
+            if (!($node instanceof File)) {
170
+                return new JSONResponse(['data' => ['message' => $this->l10n->t('Please select a file.')]]);
171
+            }
172
+            if ($node->getSize() > 20 * 1024 * 1024) {
173
+                return new JSONResponse(
174
+                    ['data' => ['message' => $this->l10n->t('File is too big')]],
175
+                    Http::STATUS_BAD_REQUEST
176
+                );
177
+            }
178 178
 
179
-			if ($node->getMimeType() !== 'image/jpeg' && $node->getMimeType() !== 'image/png') {
180
-				return new JSONResponse(
181
-					['data' => ['message' => $this->l10n->t('The selected file is not an image.')]],
182
-					Http::STATUS_BAD_REQUEST
183
-				);
184
-			}
179
+            if ($node->getMimeType() !== 'image/jpeg' && $node->getMimeType() !== 'image/png') {
180
+                return new JSONResponse(
181
+                    ['data' => ['message' => $this->l10n->t('The selected file is not an image.')]],
182
+                    Http::STATUS_BAD_REQUEST
183
+                );
184
+            }
185 185
 
186
-			try {
187
-				$content = $node->getContent();
188
-			} catch (NotPermittedException $e) {
189
-				return new JSONResponse(
190
-					['data' => ['message' => $this->l10n->t('The selected file cannot be read.')]],
191
-					Http::STATUS_BAD_REQUEST
192
-				);
193
-			}
194
-		} elseif (!is_null($files)) {
195
-			if (
196
-				$files['error'][0] === 0
197
-				 && is_uploaded_file($files['tmp_name'][0])
198
-			) {
199
-				if ($files['size'][0] > 20 * 1024 * 1024) {
200
-					return new JSONResponse(
201
-						['data' => ['message' => $this->l10n->t('File is too big')]],
202
-						Http::STATUS_BAD_REQUEST
203
-					);
204
-				}
205
-				$this->cache->set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200);
206
-				$content = $this->cache->get('avatar_upload');
207
-				unlink($files['tmp_name'][0]);
208
-			} else {
209
-				$phpFileUploadErrors = [
210
-					UPLOAD_ERR_OK => $this->l10n->t('The file was uploaded'),
211
-					UPLOAD_ERR_INI_SIZE => $this->l10n->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'),
212
-					UPLOAD_ERR_FORM_SIZE => $this->l10n->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'),
213
-					UPLOAD_ERR_PARTIAL => $this->l10n->t('The file was only partially uploaded'),
214
-					UPLOAD_ERR_NO_FILE => $this->l10n->t('No file was uploaded'),
215
-					UPLOAD_ERR_NO_TMP_DIR => $this->l10n->t('Missing a temporary folder'),
216
-					UPLOAD_ERR_CANT_WRITE => $this->l10n->t('Could not write file to disk'),
217
-					UPLOAD_ERR_EXTENSION => $this->l10n->t('A PHP extension stopped the file upload'),
218
-				];
219
-				$message = $phpFileUploadErrors[$files['error'][0]] ?? $this->l10n->t('Invalid file provided');
220
-				$this->logger->warning($message, ['app' => 'core']);
221
-				return new JSONResponse(
222
-					['data' => ['message' => $message]],
223
-					Http::STATUS_BAD_REQUEST
224
-				);
225
-			}
226
-		} else {
227
-			//Add imgfile
228
-			return new JSONResponse(
229
-				['data' => ['message' => $this->l10n->t('No image or file provided')]],
230
-				Http::STATUS_BAD_REQUEST
231
-			);
232
-		}
186
+            try {
187
+                $content = $node->getContent();
188
+            } catch (NotPermittedException $e) {
189
+                return new JSONResponse(
190
+                    ['data' => ['message' => $this->l10n->t('The selected file cannot be read.')]],
191
+                    Http::STATUS_BAD_REQUEST
192
+                );
193
+            }
194
+        } elseif (!is_null($files)) {
195
+            if (
196
+                $files['error'][0] === 0
197
+                 && is_uploaded_file($files['tmp_name'][0])
198
+            ) {
199
+                if ($files['size'][0] > 20 * 1024 * 1024) {
200
+                    return new JSONResponse(
201
+                        ['data' => ['message' => $this->l10n->t('File is too big')]],
202
+                        Http::STATUS_BAD_REQUEST
203
+                    );
204
+                }
205
+                $this->cache->set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200);
206
+                $content = $this->cache->get('avatar_upload');
207
+                unlink($files['tmp_name'][0]);
208
+            } else {
209
+                $phpFileUploadErrors = [
210
+                    UPLOAD_ERR_OK => $this->l10n->t('The file was uploaded'),
211
+                    UPLOAD_ERR_INI_SIZE => $this->l10n->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'),
212
+                    UPLOAD_ERR_FORM_SIZE => $this->l10n->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'),
213
+                    UPLOAD_ERR_PARTIAL => $this->l10n->t('The file was only partially uploaded'),
214
+                    UPLOAD_ERR_NO_FILE => $this->l10n->t('No file was uploaded'),
215
+                    UPLOAD_ERR_NO_TMP_DIR => $this->l10n->t('Missing a temporary folder'),
216
+                    UPLOAD_ERR_CANT_WRITE => $this->l10n->t('Could not write file to disk'),
217
+                    UPLOAD_ERR_EXTENSION => $this->l10n->t('A PHP extension stopped the file upload'),
218
+                ];
219
+                $message = $phpFileUploadErrors[$files['error'][0]] ?? $this->l10n->t('Invalid file provided');
220
+                $this->logger->warning($message, ['app' => 'core']);
221
+                return new JSONResponse(
222
+                    ['data' => ['message' => $message]],
223
+                    Http::STATUS_BAD_REQUEST
224
+                );
225
+            }
226
+        } else {
227
+            //Add imgfile
228
+            return new JSONResponse(
229
+                ['data' => ['message' => $this->l10n->t('No image or file provided')]],
230
+                Http::STATUS_BAD_REQUEST
231
+            );
232
+        }
233 233
 
234
-		try {
235
-			$image = new Image();
236
-			$image->loadFromData($content);
237
-			$image->readExif($content);
238
-			$image->fixOrientation();
234
+        try {
235
+            $image = new Image();
236
+            $image->loadFromData($content);
237
+            $image->readExif($content);
238
+            $image->fixOrientation();
239 239
 
240
-			if ($image->valid()) {
241
-				$mimeType = $image->mimeType();
242
-				if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') {
243
-					return new JSONResponse(
244
-						['data' => ['message' => $this->l10n->t('Unknown filetype')]],
245
-						Http::STATUS_OK
246
-					);
247
-				}
240
+            if ($image->valid()) {
241
+                $mimeType = $image->mimeType();
242
+                if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') {
243
+                    return new JSONResponse(
244
+                        ['data' => ['message' => $this->l10n->t('Unknown filetype')]],
245
+                        Http::STATUS_OK
246
+                    );
247
+                }
248 248
 
249
-				if ($image->width() === $image->height()) {
250
-					try {
251
-						$avatar = $this->avatarManager->getAvatar($this->userId);
252
-						$avatar->set($image);
253
-						// Clean up
254
-						$this->cache->remove('tmpAvatar');
255
-						return new JSONResponse(['status' => 'success']);
256
-					} catch (\Throwable $e) {
257
-						$this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']);
258
-						return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
259
-					}
260
-				}
249
+                if ($image->width() === $image->height()) {
250
+                    try {
251
+                        $avatar = $this->avatarManager->getAvatar($this->userId);
252
+                        $avatar->set($image);
253
+                        // Clean up
254
+                        $this->cache->remove('tmpAvatar');
255
+                        return new JSONResponse(['status' => 'success']);
256
+                    } catch (\Throwable $e) {
257
+                        $this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']);
258
+                        return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
259
+                    }
260
+                }
261 261
 
262
-				$this->cache->set('tmpAvatar', $image->data(), 7200);
263
-				return new JSONResponse(
264
-					['data' => 'notsquare'],
265
-					Http::STATUS_OK
266
-				);
267
-			} else {
268
-				return new JSONResponse(
269
-					['data' => ['message' => $this->l10n->t('Invalid image')]],
270
-					Http::STATUS_OK
271
-				);
272
-			}
273
-		} catch (\Exception $e) {
274
-			$this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']);
275
-			return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK);
276
-		}
277
-	}
262
+                $this->cache->set('tmpAvatar', $image->data(), 7200);
263
+                return new JSONResponse(
264
+                    ['data' => 'notsquare'],
265
+                    Http::STATUS_OK
266
+                );
267
+            } else {
268
+                return new JSONResponse(
269
+                    ['data' => ['message' => $this->l10n->t('Invalid image')]],
270
+                    Http::STATUS_OK
271
+                );
272
+            }
273
+        } catch (\Exception $e) {
274
+            $this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']);
275
+            return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK);
276
+        }
277
+    }
278 278
 
279
-	#[NoAdminRequired]
280
-	#[FrontpageRoute(verb: 'DELETE', url: '/avatar/')]
281
-	public function deleteAvatar(): JSONResponse {
282
-		try {
283
-			$avatar = $this->avatarManager->getAvatar($this->userId);
284
-			$avatar->remove();
285
-			return new JSONResponse();
286
-		} catch (\Exception $e) {
287
-			$this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']);
288
-			return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
289
-		}
290
-	}
279
+    #[NoAdminRequired]
280
+    #[FrontpageRoute(verb: 'DELETE', url: '/avatar/')]
281
+    public function deleteAvatar(): JSONResponse {
282
+        try {
283
+            $avatar = $this->avatarManager->getAvatar($this->userId);
284
+            $avatar->remove();
285
+            return new JSONResponse();
286
+        } catch (\Exception $e) {
287
+            $this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']);
288
+            return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
289
+        }
290
+    }
291 291
 
292
-	/**
293
-	 * @return JSONResponse|DataDisplayResponse
294
-	 */
295
-	#[NoAdminRequired]
296
-	#[FrontpageRoute(verb: 'GET', url: '/avatar/tmp')]
297
-	public function getTmpAvatar() {
298
-		$tmpAvatar = $this->cache->get('tmpAvatar');
299
-		if (is_null($tmpAvatar)) {
300
-			return new JSONResponse(['data' => [
301
-				'message' => $this->l10n->t('No temporary profile picture available, try again')
302
-			]],
303
-				Http::STATUS_NOT_FOUND);
304
-		}
292
+    /**
293
+     * @return JSONResponse|DataDisplayResponse
294
+     */
295
+    #[NoAdminRequired]
296
+    #[FrontpageRoute(verb: 'GET', url: '/avatar/tmp')]
297
+    public function getTmpAvatar() {
298
+        $tmpAvatar = $this->cache->get('tmpAvatar');
299
+        if (is_null($tmpAvatar)) {
300
+            return new JSONResponse(['data' => [
301
+                'message' => $this->l10n->t('No temporary profile picture available, try again')
302
+            ]],
303
+                Http::STATUS_NOT_FOUND);
304
+        }
305 305
 
306
-		$image = new Image();
307
-		$image->loadFromData($tmpAvatar);
306
+        $image = new Image();
307
+        $image->loadFromData($tmpAvatar);
308 308
 
309
-		$resp = new DataDisplayResponse(
310
-			$image->data() ?? '',
311
-			Http::STATUS_OK,
312
-			['Content-Type' => $image->mimeType()]);
309
+        $resp = new DataDisplayResponse(
310
+            $image->data() ?? '',
311
+            Http::STATUS_OK,
312
+            ['Content-Type' => $image->mimeType()]);
313 313
 
314
-		$resp->setETag((string)crc32($image->data() ?? ''));
315
-		$resp->cacheFor(0);
316
-		$resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
317
-		return $resp;
318
-	}
314
+        $resp->setETag((string)crc32($image->data() ?? ''));
315
+        $resp->cacheFor(0);
316
+        $resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
317
+        return $resp;
318
+    }
319 319
 
320
-	#[NoAdminRequired]
321
-	#[FrontpageRoute(verb: 'POST', url: '/avatar/cropped')]
322
-	public function postCroppedAvatar(?array $crop = null): JSONResponse {
323
-		if (is_null($crop)) {
324
-			return new JSONResponse(['data' => ['message' => $this->l10n->t('No crop data provided')]],
325
-				Http::STATUS_BAD_REQUEST);
326
-		}
320
+    #[NoAdminRequired]
321
+    #[FrontpageRoute(verb: 'POST', url: '/avatar/cropped')]
322
+    public function postCroppedAvatar(?array $crop = null): JSONResponse {
323
+        if (is_null($crop)) {
324
+            return new JSONResponse(['data' => ['message' => $this->l10n->t('No crop data provided')]],
325
+                Http::STATUS_BAD_REQUEST);
326
+        }
327 327
 
328
-		if (!isset($crop['x'], $crop['y'], $crop['w'], $crop['h'])) {
329
-			return new JSONResponse(['data' => ['message' => $this->l10n->t('No valid crop data provided')]],
330
-				Http::STATUS_BAD_REQUEST);
331
-		}
328
+        if (!isset($crop['x'], $crop['y'], $crop['w'], $crop['h'])) {
329
+            return new JSONResponse(['data' => ['message' => $this->l10n->t('No valid crop data provided')]],
330
+                Http::STATUS_BAD_REQUEST);
331
+        }
332 332
 
333
-		$tmpAvatar = $this->cache->get('tmpAvatar');
334
-		if (is_null($tmpAvatar)) {
335
-			return new JSONResponse(['data' => [
336
-				'message' => $this->l10n->t('No temporary profile picture available, try again')
337
-			]],
338
-				Http::STATUS_BAD_REQUEST);
339
-		}
333
+        $tmpAvatar = $this->cache->get('tmpAvatar');
334
+        if (is_null($tmpAvatar)) {
335
+            return new JSONResponse(['data' => [
336
+                'message' => $this->l10n->t('No temporary profile picture available, try again')
337
+            ]],
338
+                Http::STATUS_BAD_REQUEST);
339
+        }
340 340
 
341
-		$image = new Image();
342
-		$image->loadFromData($tmpAvatar);
343
-		$image->crop($crop['x'], $crop['y'], (int)round($crop['w']), (int)round($crop['h']));
344
-		try {
345
-			$avatar = $this->avatarManager->getAvatar($this->userId);
346
-			$avatar->set($image);
347
-			// Clean up
348
-			$this->cache->remove('tmpAvatar');
349
-			return new JSONResponse(['status' => 'success']);
350
-		} catch (NotSquareException $e) {
351
-			return new JSONResponse(['data' => ['message' => $this->l10n->t('Crop is not square')]],
352
-				Http::STATUS_BAD_REQUEST);
353
-		} catch (\Exception $e) {
354
-			$this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']);
355
-			return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
356
-		}
357
-	}
341
+        $image = new Image();
342
+        $image->loadFromData($tmpAvatar);
343
+        $image->crop($crop['x'], $crop['y'], (int)round($crop['w']), (int)round($crop['h']));
344
+        try {
345
+            $avatar = $this->avatarManager->getAvatar($this->userId);
346
+            $avatar->set($image);
347
+            // Clean up
348
+            $this->cache->remove('tmpAvatar');
349
+            return new JSONResponse(['status' => 'success']);
350
+        } catch (NotSquareException $e) {
351
+            return new JSONResponse(['data' => ['message' => $this->l10n->t('Crop is not square')]],
352
+                Http::STATUS_BAD_REQUEST);
353
+        } catch (\Exception $e) {
354
+            $this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']);
355
+            return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
356
+        }
357
+    }
358 358
 }
Please login to merge, or discard this patch.
apps/dav/tests/unit/Connector/Sabre/AuthTest.php 1 patch
Indentation   +575 added lines, -575 removed lines patch added patch discarded remove patch
@@ -30,579 +30,579 @@
 block discarded – undo
30 30
  * @group DB
31 31
  */
32 32
 class AuthTest extends TestCase {
33
-	private ISession&MockObject $session;
34
-	private Session&MockObject $userSession;
35
-	private IRequest&MockObject $request;
36
-	private Manager&MockObject $twoFactorManager;
37
-	private IThrottler&MockObject $throttler;
38
-	private Auth $auth;
39
-
40
-	protected function setUp(): void {
41
-		parent::setUp();
42
-		$this->session = $this->createMock(ISession::class);
43
-		$this->userSession = $this->createMock(Session::class);
44
-		$this->request = $this->createMock(IRequest::class);
45
-		$this->twoFactorManager = $this->createMock(Manager::class);
46
-		$this->throttler = $this->createMock(IThrottler::class);
47
-		$this->auth = new Auth(
48
-			$this->session,
49
-			$this->userSession,
50
-			$this->request,
51
-			$this->twoFactorManager,
52
-			$this->throttler
53
-		);
54
-	}
55
-
56
-	public function testIsDavAuthenticatedWithoutDavSession(): void {
57
-		$this->session
58
-			->expects($this->once())
59
-			->method('get')
60
-			->with('AUTHENTICATED_TO_DAV_BACKEND')
61
-			->willReturn(null);
62
-
63
-		$this->assertFalse(self::invokePrivate($this->auth, 'isDavAuthenticated', ['MyTestUser']));
64
-	}
65
-
66
-	public function testIsDavAuthenticatedWithWrongDavSession(): void {
67
-		$this->session
68
-			->expects($this->exactly(2))
69
-			->method('get')
70
-			->with('AUTHENTICATED_TO_DAV_BACKEND')
71
-			->willReturn('AnotherUser');
72
-
73
-		$this->assertFalse(self::invokePrivate($this->auth, 'isDavAuthenticated', ['MyTestUser']));
74
-	}
75
-
76
-	public function testIsDavAuthenticatedWithCorrectDavSession(): void {
77
-		$this->session
78
-			->expects($this->exactly(2))
79
-			->method('get')
80
-			->with('AUTHENTICATED_TO_DAV_BACKEND')
81
-			->willReturn('MyTestUser');
82
-
83
-		$this->assertTrue(self::invokePrivate($this->auth, 'isDavAuthenticated', ['MyTestUser']));
84
-	}
85
-
86
-	public function testValidateUserPassOfAlreadyDAVAuthenticatedUser(): void {
87
-		$user = $this->createMock(IUser::class);
88
-		$user->expects($this->exactly(1))
89
-			->method('getUID')
90
-			->willReturn('MyTestUser');
91
-		$this->userSession
92
-			->expects($this->once())
93
-			->method('isLoggedIn')
94
-			->willReturn(true);
95
-		$this->userSession
96
-			->expects($this->exactly(1))
97
-			->method('getUser')
98
-			->willReturn($user);
99
-		$this->session
100
-			->expects($this->exactly(2))
101
-			->method('get')
102
-			->with('AUTHENTICATED_TO_DAV_BACKEND')
103
-			->willReturn('MyTestUser');
104
-		$this->session
105
-			->expects($this->once())
106
-			->method('close');
107
-
108
-		$this->assertTrue(self::invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword']));
109
-	}
110
-
111
-	public function testValidateUserPassOfInvalidDAVAuthenticatedUser(): void {
112
-		$user = $this->createMock(IUser::class);
113
-		$user->expects($this->once())
114
-			->method('getUID')
115
-			->willReturn('MyTestUser');
116
-		$this->userSession
117
-			->expects($this->once())
118
-			->method('isLoggedIn')
119
-			->willReturn(true);
120
-		$this->userSession
121
-			->expects($this->once())
122
-			->method('getUser')
123
-			->willReturn($user);
124
-		$this->session
125
-			->expects($this->exactly(2))
126
-			->method('get')
127
-			->with('AUTHENTICATED_TO_DAV_BACKEND')
128
-			->willReturn('AnotherUser');
129
-		$this->session
130
-			->expects($this->once())
131
-			->method('close');
132
-
133
-		$this->assertFalse(self::invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword']));
134
-	}
135
-
136
-	public function testValidateUserPassOfInvalidDAVAuthenticatedUserWithValidPassword(): void {
137
-		$user = $this->createMock(IUser::class);
138
-		$user->expects($this->exactly(2))
139
-			->method('getUID')
140
-			->willReturn('MyTestUser');
141
-		$this->userSession
142
-			->expects($this->once())
143
-			->method('isLoggedIn')
144
-			->willReturn(true);
145
-		$this->userSession
146
-			->expects($this->exactly(2))
147
-			->method('getUser')
148
-			->willReturn($user);
149
-		$this->session
150
-			->expects($this->exactly(2))
151
-			->method('get')
152
-			->with('AUTHENTICATED_TO_DAV_BACKEND')
153
-			->willReturn('AnotherUser');
154
-		$this->userSession
155
-			->expects($this->once())
156
-			->method('logClientIn')
157
-			->with('MyTestUser', 'MyTestPassword', $this->request)
158
-			->willReturn(true);
159
-		$this->session
160
-			->expects($this->once())
161
-			->method('set')
162
-			->with('AUTHENTICATED_TO_DAV_BACKEND', 'MyTestUser');
163
-		$this->session
164
-			->expects($this->once())
165
-			->method('close');
166
-
167
-		$this->assertTrue(self::invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword']));
168
-	}
169
-
170
-	public function testValidateUserPassWithInvalidPassword(): void {
171
-		$this->userSession
172
-			->expects($this->once())
173
-			->method('isLoggedIn')
174
-			->willReturn(false);
175
-		$this->userSession
176
-			->expects($this->once())
177
-			->method('logClientIn')
178
-			->with('MyTestUser', 'MyTestPassword')
179
-			->willReturn(false);
180
-		$this->session
181
-			->expects($this->once())
182
-			->method('close');
183
-
184
-		$this->assertFalse(self::invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword']));
185
-	}
186
-
187
-
188
-	public function testValidateUserPassWithPasswordLoginForbidden(): void {
189
-		$this->expectException(PasswordLoginForbidden::class);
190
-
191
-		$this->userSession
192
-			->expects($this->once())
193
-			->method('isLoggedIn')
194
-			->willReturn(false);
195
-		$this->userSession
196
-			->expects($this->once())
197
-			->method('logClientIn')
198
-			->with('MyTestUser', 'MyTestPassword')
199
-			->willThrowException(new PasswordLoginForbiddenException());
200
-		$this->session
201
-			->expects($this->once())
202
-			->method('close');
203
-
204
-		self::invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword']);
205
-	}
206
-
207
-	public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenForNonGet(): void {
208
-		$request = $this->createMock(RequestInterface::class);
209
-		$response = $this->createMock(ResponseInterface::class);
210
-		$this->userSession
211
-			->expects($this->any())
212
-			->method('isLoggedIn')
213
-			->willReturn(true);
214
-		$this->request
215
-			->expects($this->any())
216
-			->method('getMethod')
217
-			->willReturn('POST');
218
-		$this->session
219
-			->expects($this->any())
220
-			->method('get')
221
-			->with('AUTHENTICATED_TO_DAV_BACKEND')
222
-			->willReturn(null);
223
-		$user = $this->createMock(IUser::class);
224
-		$user->expects($this->any())
225
-			->method('getUID')
226
-			->willReturn('MyWrongDavUser');
227
-		$this->userSession
228
-			->expects($this->any())
229
-			->method('getUser')
230
-			->willReturn($user);
231
-		$this->request
232
-			->expects($this->once())
233
-			->method('passesCSRFCheck')
234
-			->willReturn(false);
235
-
236
-		$expectedResponse = [
237
-			false,
238
-			"No 'Authorization: Basic' header found. Either the client didn't send one, or the server is misconfigured",
239
-		];
240
-		$response = $this->auth->check($request, $response);
241
-		$this->assertSame($expectedResponse, $response);
242
-	}
243
-
244
-	public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenAndCorrectlyDavAuthenticated(): void {
245
-		$request = $this->createMock(RequestInterface::class);
246
-		$response = $this->createMock(ResponseInterface::class);
247
-		$this->userSession
248
-			->expects($this->any())
249
-			->method('isLoggedIn')
250
-			->willReturn(true);
251
-		$this->request
252
-			->expects($this->any())
253
-			->method('getMethod')
254
-			->willReturn('PROPFIND');
255
-		$this->request
256
-			->expects($this->any())
257
-			->method('isUserAgent')
258
-			->willReturn(false);
259
-		$this->session
260
-			->expects($this->any())
261
-			->method('get')
262
-			->with('AUTHENTICATED_TO_DAV_BACKEND')
263
-			->willReturn('LoggedInUser');
264
-		$user = $this->createMock(IUser::class);
265
-		$user->expects($this->any())
266
-			->method('getUID')
267
-			->willReturn('LoggedInUser');
268
-		$this->userSession
269
-			->expects($this->any())
270
-			->method('getUser')
271
-			->willReturn($user);
272
-		$this->request
273
-			->expects($this->once())
274
-			->method('passesCSRFCheck')
275
-			->willReturn(false);
276
-		$this->auth->check($request, $response);
277
-	}
278
-
279
-
280
-	public function testAuthenticateAlreadyLoggedInWithoutTwoFactorChallengePassed(): void {
281
-		$this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class);
282
-		$this->expectExceptionMessage('2FA challenge not passed.');
283
-
284
-		$request = $this->createMock(RequestInterface::class);
285
-		$response = $this->createMock(ResponseInterface::class);
286
-		$this->userSession
287
-			->expects($this->any())
288
-			->method('isLoggedIn')
289
-			->willReturn(true);
290
-		$this->request
291
-			->expects($this->any())
292
-			->method('getMethod')
293
-			->willReturn('PROPFIND');
294
-		$this->request
295
-			->expects($this->any())
296
-			->method('isUserAgent')
297
-			->willReturn(false);
298
-		$this->session
299
-			->expects($this->any())
300
-			->method('get')
301
-			->with('AUTHENTICATED_TO_DAV_BACKEND')
302
-			->willReturn('LoggedInUser');
303
-		$user = $this->createMock(IUser::class);
304
-		$user->expects($this->any())
305
-			->method('getUID')
306
-			->willReturn('LoggedInUser');
307
-		$this->userSession
308
-			->expects($this->any())
309
-			->method('getUser')
310
-			->willReturn($user);
311
-		$this->request
312
-			->expects($this->once())
313
-			->method('passesCSRFCheck')
314
-			->willReturn(true);
315
-		$this->twoFactorManager->expects($this->once())
316
-			->method('needsSecondFactor')
317
-			->with($user)
318
-			->willReturn(true);
319
-		$this->auth->check($request, $response);
320
-	}
321
-
322
-
323
-	public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenAndIncorrectlyDavAuthenticated(): void {
324
-		$this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class);
325
-		$this->expectExceptionMessage('CSRF check not passed.');
326
-
327
-		$request = $this->createMock(RequestInterface::class);
328
-		$response = $this->createMock(ResponseInterface::class);
329
-		$this->userSession
330
-			->expects($this->any())
331
-			->method('isLoggedIn')
332
-			->willReturn(true);
333
-		$this->request
334
-			->expects($this->any())
335
-			->method('getMethod')
336
-			->willReturn('PROPFIND');
337
-		$this->request
338
-			->expects($this->any())
339
-			->method('isUserAgent')
340
-			->willReturn(false);
341
-		$this->session
342
-			->expects($this->any())
343
-			->method('get')
344
-			->with('AUTHENTICATED_TO_DAV_BACKEND')
345
-			->willReturn('AnotherUser');
346
-		$user = $this->createMock(IUser::class);
347
-		$user->expects($this->any())
348
-			->method('getUID')
349
-			->willReturn('LoggedInUser');
350
-		$this->userSession
351
-			->expects($this->any())
352
-			->method('getUser')
353
-			->willReturn($user);
354
-		$this->request
355
-			->expects($this->once())
356
-			->method('passesCSRFCheck')
357
-			->willReturn(false);
358
-		$this->auth->check($request, $response);
359
-	}
360
-
361
-	public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenForNonGetAndDesktopClient(): void {
362
-		$request = $this->createMock(RequestInterface::class);
363
-		$response = $this->createMock(ResponseInterface::class);
364
-		$this->userSession
365
-			->expects($this->any())
366
-			->method('isLoggedIn')
367
-			->willReturn(true);
368
-		$this->request
369
-			->expects($this->any())
370
-			->method('getMethod')
371
-			->willReturn('POST');
372
-		$this->request
373
-			->expects($this->any())
374
-			->method('isUserAgent')
375
-			->willReturn(true);
376
-		$this->session
377
-			->expects($this->any())
378
-			->method('get')
379
-			->with('AUTHENTICATED_TO_DAV_BACKEND')
380
-			->willReturn(null);
381
-		$user = $this->createMock(IUser::class);
382
-		$user->expects($this->any())
383
-			->method('getUID')
384
-			->willReturn('MyWrongDavUser');
385
-		$this->userSession
386
-			->expects($this->any())
387
-			->method('getUser')
388
-			->willReturn($user);
389
-		$this->request
390
-			->expects($this->once())
391
-			->method('passesCSRFCheck')
392
-			->willReturn(false);
393
-
394
-		$this->auth->check($request, $response);
395
-	}
396
-
397
-	public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenForGet(): void {
398
-		$request = $this->createMock(RequestInterface::class);
399
-		$response = $this->createMock(ResponseInterface::class);
400
-		$this->userSession
401
-			->expects($this->any())
402
-			->method('isLoggedIn')
403
-			->willReturn(true);
404
-		$this->session
405
-			->expects($this->any())
406
-			->method('get')
407
-			->with('AUTHENTICATED_TO_DAV_BACKEND')
408
-			->willReturn(null);
409
-		$user = $this->createMock(IUser::class);
410
-		$user->expects($this->any())
411
-			->method('getUID')
412
-			->willReturn('MyWrongDavUser');
413
-		$this->userSession
414
-			->expects($this->any())
415
-			->method('getUser')
416
-			->willReturn($user);
417
-		$this->request
418
-			->expects($this->any())
419
-			->method('getMethod')
420
-			->willReturn('GET');
421
-
422
-		$response = $this->auth->check($request, $response);
423
-		$this->assertEquals([true, 'principals/users/MyWrongDavUser'], $response);
424
-	}
425
-
426
-	public function testAuthenticateAlreadyLoggedInWithCsrfTokenForGet(): void {
427
-		$request = $this->createMock(RequestInterface::class);
428
-		$response = $this->createMock(ResponseInterface::class);
429
-		$this->userSession
430
-			->expects($this->any())
431
-			->method('isLoggedIn')
432
-			->willReturn(true);
433
-		$this->session
434
-			->expects($this->any())
435
-			->method('get')
436
-			->with('AUTHENTICATED_TO_DAV_BACKEND')
437
-			->willReturn(null);
438
-		$user = $this->createMock(IUser::class);
439
-		$user->expects($this->any())
440
-			->method('getUID')
441
-			->willReturn('MyWrongDavUser');
442
-		$this->userSession
443
-			->expects($this->any())
444
-			->method('getUser')
445
-			->willReturn($user);
446
-		$this->request
447
-			->expects($this->once())
448
-			->method('passesCSRFCheck')
449
-			->willReturn(true);
450
-
451
-		$response = $this->auth->check($request, $response);
452
-		$this->assertEquals([true, 'principals/users/MyWrongDavUser'], $response);
453
-	}
454
-
455
-	public function testAuthenticateNoBasicAuthenticateHeadersProvided(): void {
456
-		$server = $this->createMock(Server::class);
457
-		$server->httpRequest = $this->createMock(RequestInterface::class);
458
-		$server->httpResponse = $this->createMock(ResponseInterface::class);
459
-		$response = $this->auth->check($server->httpRequest, $server->httpResponse);
460
-		$this->assertEquals([false, 'No \'Authorization: Basic\' header found. Either the client didn\'t send one, or the server is misconfigured'], $response);
461
-	}
462
-
463
-
464
-	public function testAuthenticateNoBasicAuthenticateHeadersProvidedWithAjax(): void {
465
-		$this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class);
466
-		$this->expectExceptionMessage('Cannot authenticate over ajax calls');
467
-
468
-		/** @var \Sabre\HTTP\RequestInterface&MockObject $httpRequest */
469
-		$httpRequest = $this->createMock(RequestInterface::class);
470
-		/** @var \Sabre\HTTP\ResponseInterface&MockObject $httpResponse */
471
-		$httpResponse = $this->createMock(ResponseInterface::class);
472
-		$this->userSession
473
-			->expects($this->any())
474
-			->method('isLoggedIn')
475
-			->willReturn(false);
476
-		$httpRequest
477
-			->expects($this->exactly(2))
478
-			->method('getHeader')
479
-			->willReturnMap([
480
-				['X-Requested-With', 'XMLHttpRequest'],
481
-				['Authorization', null],
482
-			]);
483
-
484
-		$this->auth->check($httpRequest, $httpResponse);
485
-	}
486
-
487
-	public function testAuthenticateWithBasicAuthenticateHeadersProvidedWithAjax(): void {
488
-		// No CSRF
489
-		$this->request
490
-			->expects($this->once())
491
-			->method('passesCSRFCheck')
492
-			->willReturn(false);
493
-
494
-		/** @var \Sabre\HTTP\RequestInterface&MockObject $httpRequest */
495
-		$httpRequest = $this->createMock(RequestInterface::class);
496
-		/** @var \Sabre\HTTP\ResponseInterface&MockObject $httpResponse */
497
-		$httpResponse = $this->createMock(ResponseInterface::class);
498
-		$httpRequest
499
-			->expects($this->any())
500
-			->method('getHeader')
501
-			->willReturnMap([
502
-				['X-Requested-With', 'XMLHttpRequest'],
503
-				['Authorization', 'basic dXNlcm5hbWU6cGFzc3dvcmQ='],
504
-			]);
505
-
506
-		$user = $this->createMock(IUser::class);
507
-		$user->expects($this->any())
508
-			->method('getUID')
509
-			->willReturn('MyDavUser');
510
-		$this->userSession
511
-			->expects($this->any())
512
-			->method('isLoggedIn')
513
-			->willReturn(false);
514
-		$this->userSession
515
-			->expects($this->once())
516
-			->method('logClientIn')
517
-			->with('username', 'password')
518
-			->willReturn(true);
519
-		$this->userSession
520
-			->expects($this->any())
521
-			->method('getUser')
522
-			->willReturn($user);
523
-
524
-		$this->auth->check($httpRequest, $httpResponse);
525
-	}
526
-
527
-	public function testAuthenticateNoBasicAuthenticateHeadersProvidedWithAjaxButUserIsStillLoggedIn(): void {
528
-		/** @var \Sabre\HTTP\RequestInterface $httpRequest */
529
-		$httpRequest = $this->createMock(RequestInterface::class);
530
-		/** @var \Sabre\HTTP\ResponseInterface $httpResponse */
531
-		$httpResponse = $this->createMock(ResponseInterface::class);
532
-		$user = $this->createMock(IUser::class);
533
-		$user->method('getUID')->willReturn('MyTestUser');
534
-		$this->userSession
535
-			->expects($this->any())
536
-			->method('isLoggedIn')
537
-			->willReturn(true);
538
-		$this->userSession
539
-			->expects($this->any())
540
-			->method('getUser')
541
-			->willReturn($user);
542
-		$this->session
543
-			->expects($this->atLeastOnce())
544
-			->method('get')
545
-			->with('AUTHENTICATED_TO_DAV_BACKEND')
546
-			->willReturn('MyTestUser');
547
-		$this->request
548
-			->expects($this->once())
549
-			->method('getMethod')
550
-			->willReturn('GET');
551
-		$httpRequest
552
-			->expects($this->atLeastOnce())
553
-			->method('getHeader')
554
-			->with('Authorization')
555
-			->willReturn(null);
556
-		$this->assertEquals(
557
-			[true, 'principals/users/MyTestUser'],
558
-			$this->auth->check($httpRequest, $httpResponse)
559
-		);
560
-	}
561
-
562
-	public function testAuthenticateValidCredentials(): void {
563
-		$server = $this->createMock(Server::class);
564
-		$server->httpRequest = $this->createMock(RequestInterface::class);
565
-		$server->httpRequest
566
-			->expects($this->once())
567
-			->method('getHeader')
568
-			->with('Authorization')
569
-			->willReturn('basic dXNlcm5hbWU6cGFzc3dvcmQ=');
570
-
571
-		$server->httpResponse = $this->createMock(ResponseInterface::class);
572
-		$this->userSession
573
-			->expects($this->once())
574
-			->method('logClientIn')
575
-			->with('username', 'password')
576
-			->willReturn(true);
577
-		$user = $this->createMock(IUser::class);
578
-		$user->expects($this->exactly(2))
579
-			->method('getUID')
580
-			->willReturn('MyTestUser');
581
-		$this->userSession
582
-			->expects($this->exactly(3))
583
-			->method('getUser')
584
-			->willReturn($user);
585
-		$response = $this->auth->check($server->httpRequest, $server->httpResponse);
586
-		$this->assertEquals([true, 'principals/users/MyTestUser'], $response);
587
-	}
588
-
589
-	public function testAuthenticateInvalidCredentials(): void {
590
-		$server = $this->createMock(Server::class);
591
-		$server->httpRequest = $this->createMock(RequestInterface::class);
592
-		$server->httpRequest
593
-			->expects($this->exactly(2))
594
-			->method('getHeader')
595
-			->willReturnMap([
596
-				['Authorization', 'basic dXNlcm5hbWU6cGFzc3dvcmQ='],
597
-				['X-Requested-With', null],
598
-			]);
599
-		$server->httpResponse = $this->createMock(ResponseInterface::class);
600
-		$this->userSession
601
-			->expects($this->once())
602
-			->method('logClientIn')
603
-			->with('username', 'password')
604
-			->willReturn(false);
605
-		$response = $this->auth->check($server->httpRequest, $server->httpResponse);
606
-		$this->assertEquals([false, 'Username or password was incorrect'], $response);
607
-	}
33
+    private ISession&MockObject $session;
34
+    private Session&MockObject $userSession;
35
+    private IRequest&MockObject $request;
36
+    private Manager&MockObject $twoFactorManager;
37
+    private IThrottler&MockObject $throttler;
38
+    private Auth $auth;
39
+
40
+    protected function setUp(): void {
41
+        parent::setUp();
42
+        $this->session = $this->createMock(ISession::class);
43
+        $this->userSession = $this->createMock(Session::class);
44
+        $this->request = $this->createMock(IRequest::class);
45
+        $this->twoFactorManager = $this->createMock(Manager::class);
46
+        $this->throttler = $this->createMock(IThrottler::class);
47
+        $this->auth = new Auth(
48
+            $this->session,
49
+            $this->userSession,
50
+            $this->request,
51
+            $this->twoFactorManager,
52
+            $this->throttler
53
+        );
54
+    }
55
+
56
+    public function testIsDavAuthenticatedWithoutDavSession(): void {
57
+        $this->session
58
+            ->expects($this->once())
59
+            ->method('get')
60
+            ->with('AUTHENTICATED_TO_DAV_BACKEND')
61
+            ->willReturn(null);
62
+
63
+        $this->assertFalse(self::invokePrivate($this->auth, 'isDavAuthenticated', ['MyTestUser']));
64
+    }
65
+
66
+    public function testIsDavAuthenticatedWithWrongDavSession(): void {
67
+        $this->session
68
+            ->expects($this->exactly(2))
69
+            ->method('get')
70
+            ->with('AUTHENTICATED_TO_DAV_BACKEND')
71
+            ->willReturn('AnotherUser');
72
+
73
+        $this->assertFalse(self::invokePrivate($this->auth, 'isDavAuthenticated', ['MyTestUser']));
74
+    }
75
+
76
+    public function testIsDavAuthenticatedWithCorrectDavSession(): void {
77
+        $this->session
78
+            ->expects($this->exactly(2))
79
+            ->method('get')
80
+            ->with('AUTHENTICATED_TO_DAV_BACKEND')
81
+            ->willReturn('MyTestUser');
82
+
83
+        $this->assertTrue(self::invokePrivate($this->auth, 'isDavAuthenticated', ['MyTestUser']));
84
+    }
85
+
86
+    public function testValidateUserPassOfAlreadyDAVAuthenticatedUser(): void {
87
+        $user = $this->createMock(IUser::class);
88
+        $user->expects($this->exactly(1))
89
+            ->method('getUID')
90
+            ->willReturn('MyTestUser');
91
+        $this->userSession
92
+            ->expects($this->once())
93
+            ->method('isLoggedIn')
94
+            ->willReturn(true);
95
+        $this->userSession
96
+            ->expects($this->exactly(1))
97
+            ->method('getUser')
98
+            ->willReturn($user);
99
+        $this->session
100
+            ->expects($this->exactly(2))
101
+            ->method('get')
102
+            ->with('AUTHENTICATED_TO_DAV_BACKEND')
103
+            ->willReturn('MyTestUser');
104
+        $this->session
105
+            ->expects($this->once())
106
+            ->method('close');
107
+
108
+        $this->assertTrue(self::invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword']));
109
+    }
110
+
111
+    public function testValidateUserPassOfInvalidDAVAuthenticatedUser(): void {
112
+        $user = $this->createMock(IUser::class);
113
+        $user->expects($this->once())
114
+            ->method('getUID')
115
+            ->willReturn('MyTestUser');
116
+        $this->userSession
117
+            ->expects($this->once())
118
+            ->method('isLoggedIn')
119
+            ->willReturn(true);
120
+        $this->userSession
121
+            ->expects($this->once())
122
+            ->method('getUser')
123
+            ->willReturn($user);
124
+        $this->session
125
+            ->expects($this->exactly(2))
126
+            ->method('get')
127
+            ->with('AUTHENTICATED_TO_DAV_BACKEND')
128
+            ->willReturn('AnotherUser');
129
+        $this->session
130
+            ->expects($this->once())
131
+            ->method('close');
132
+
133
+        $this->assertFalse(self::invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword']));
134
+    }
135
+
136
+    public function testValidateUserPassOfInvalidDAVAuthenticatedUserWithValidPassword(): void {
137
+        $user = $this->createMock(IUser::class);
138
+        $user->expects($this->exactly(2))
139
+            ->method('getUID')
140
+            ->willReturn('MyTestUser');
141
+        $this->userSession
142
+            ->expects($this->once())
143
+            ->method('isLoggedIn')
144
+            ->willReturn(true);
145
+        $this->userSession
146
+            ->expects($this->exactly(2))
147
+            ->method('getUser')
148
+            ->willReturn($user);
149
+        $this->session
150
+            ->expects($this->exactly(2))
151
+            ->method('get')
152
+            ->with('AUTHENTICATED_TO_DAV_BACKEND')
153
+            ->willReturn('AnotherUser');
154
+        $this->userSession
155
+            ->expects($this->once())
156
+            ->method('logClientIn')
157
+            ->with('MyTestUser', 'MyTestPassword', $this->request)
158
+            ->willReturn(true);
159
+        $this->session
160
+            ->expects($this->once())
161
+            ->method('set')
162
+            ->with('AUTHENTICATED_TO_DAV_BACKEND', 'MyTestUser');
163
+        $this->session
164
+            ->expects($this->once())
165
+            ->method('close');
166
+
167
+        $this->assertTrue(self::invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword']));
168
+    }
169
+
170
+    public function testValidateUserPassWithInvalidPassword(): void {
171
+        $this->userSession
172
+            ->expects($this->once())
173
+            ->method('isLoggedIn')
174
+            ->willReturn(false);
175
+        $this->userSession
176
+            ->expects($this->once())
177
+            ->method('logClientIn')
178
+            ->with('MyTestUser', 'MyTestPassword')
179
+            ->willReturn(false);
180
+        $this->session
181
+            ->expects($this->once())
182
+            ->method('close');
183
+
184
+        $this->assertFalse(self::invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword']));
185
+    }
186
+
187
+
188
+    public function testValidateUserPassWithPasswordLoginForbidden(): void {
189
+        $this->expectException(PasswordLoginForbidden::class);
190
+
191
+        $this->userSession
192
+            ->expects($this->once())
193
+            ->method('isLoggedIn')
194
+            ->willReturn(false);
195
+        $this->userSession
196
+            ->expects($this->once())
197
+            ->method('logClientIn')
198
+            ->with('MyTestUser', 'MyTestPassword')
199
+            ->willThrowException(new PasswordLoginForbiddenException());
200
+        $this->session
201
+            ->expects($this->once())
202
+            ->method('close');
203
+
204
+        self::invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword']);
205
+    }
206
+
207
+    public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenForNonGet(): void {
208
+        $request = $this->createMock(RequestInterface::class);
209
+        $response = $this->createMock(ResponseInterface::class);
210
+        $this->userSession
211
+            ->expects($this->any())
212
+            ->method('isLoggedIn')
213
+            ->willReturn(true);
214
+        $this->request
215
+            ->expects($this->any())
216
+            ->method('getMethod')
217
+            ->willReturn('POST');
218
+        $this->session
219
+            ->expects($this->any())
220
+            ->method('get')
221
+            ->with('AUTHENTICATED_TO_DAV_BACKEND')
222
+            ->willReturn(null);
223
+        $user = $this->createMock(IUser::class);
224
+        $user->expects($this->any())
225
+            ->method('getUID')
226
+            ->willReturn('MyWrongDavUser');
227
+        $this->userSession
228
+            ->expects($this->any())
229
+            ->method('getUser')
230
+            ->willReturn($user);
231
+        $this->request
232
+            ->expects($this->once())
233
+            ->method('passesCSRFCheck')
234
+            ->willReturn(false);
235
+
236
+        $expectedResponse = [
237
+            false,
238
+            "No 'Authorization: Basic' header found. Either the client didn't send one, or the server is misconfigured",
239
+        ];
240
+        $response = $this->auth->check($request, $response);
241
+        $this->assertSame($expectedResponse, $response);
242
+    }
243
+
244
+    public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenAndCorrectlyDavAuthenticated(): void {
245
+        $request = $this->createMock(RequestInterface::class);
246
+        $response = $this->createMock(ResponseInterface::class);
247
+        $this->userSession
248
+            ->expects($this->any())
249
+            ->method('isLoggedIn')
250
+            ->willReturn(true);
251
+        $this->request
252
+            ->expects($this->any())
253
+            ->method('getMethod')
254
+            ->willReturn('PROPFIND');
255
+        $this->request
256
+            ->expects($this->any())
257
+            ->method('isUserAgent')
258
+            ->willReturn(false);
259
+        $this->session
260
+            ->expects($this->any())
261
+            ->method('get')
262
+            ->with('AUTHENTICATED_TO_DAV_BACKEND')
263
+            ->willReturn('LoggedInUser');
264
+        $user = $this->createMock(IUser::class);
265
+        $user->expects($this->any())
266
+            ->method('getUID')
267
+            ->willReturn('LoggedInUser');
268
+        $this->userSession
269
+            ->expects($this->any())
270
+            ->method('getUser')
271
+            ->willReturn($user);
272
+        $this->request
273
+            ->expects($this->once())
274
+            ->method('passesCSRFCheck')
275
+            ->willReturn(false);
276
+        $this->auth->check($request, $response);
277
+    }
278
+
279
+
280
+    public function testAuthenticateAlreadyLoggedInWithoutTwoFactorChallengePassed(): void {
281
+        $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class);
282
+        $this->expectExceptionMessage('2FA challenge not passed.');
283
+
284
+        $request = $this->createMock(RequestInterface::class);
285
+        $response = $this->createMock(ResponseInterface::class);
286
+        $this->userSession
287
+            ->expects($this->any())
288
+            ->method('isLoggedIn')
289
+            ->willReturn(true);
290
+        $this->request
291
+            ->expects($this->any())
292
+            ->method('getMethod')
293
+            ->willReturn('PROPFIND');
294
+        $this->request
295
+            ->expects($this->any())
296
+            ->method('isUserAgent')
297
+            ->willReturn(false);
298
+        $this->session
299
+            ->expects($this->any())
300
+            ->method('get')
301
+            ->with('AUTHENTICATED_TO_DAV_BACKEND')
302
+            ->willReturn('LoggedInUser');
303
+        $user = $this->createMock(IUser::class);
304
+        $user->expects($this->any())
305
+            ->method('getUID')
306
+            ->willReturn('LoggedInUser');
307
+        $this->userSession
308
+            ->expects($this->any())
309
+            ->method('getUser')
310
+            ->willReturn($user);
311
+        $this->request
312
+            ->expects($this->once())
313
+            ->method('passesCSRFCheck')
314
+            ->willReturn(true);
315
+        $this->twoFactorManager->expects($this->once())
316
+            ->method('needsSecondFactor')
317
+            ->with($user)
318
+            ->willReturn(true);
319
+        $this->auth->check($request, $response);
320
+    }
321
+
322
+
323
+    public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenAndIncorrectlyDavAuthenticated(): void {
324
+        $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class);
325
+        $this->expectExceptionMessage('CSRF check not passed.');
326
+
327
+        $request = $this->createMock(RequestInterface::class);
328
+        $response = $this->createMock(ResponseInterface::class);
329
+        $this->userSession
330
+            ->expects($this->any())
331
+            ->method('isLoggedIn')
332
+            ->willReturn(true);
333
+        $this->request
334
+            ->expects($this->any())
335
+            ->method('getMethod')
336
+            ->willReturn('PROPFIND');
337
+        $this->request
338
+            ->expects($this->any())
339
+            ->method('isUserAgent')
340
+            ->willReturn(false);
341
+        $this->session
342
+            ->expects($this->any())
343
+            ->method('get')
344
+            ->with('AUTHENTICATED_TO_DAV_BACKEND')
345
+            ->willReturn('AnotherUser');
346
+        $user = $this->createMock(IUser::class);
347
+        $user->expects($this->any())
348
+            ->method('getUID')
349
+            ->willReturn('LoggedInUser');
350
+        $this->userSession
351
+            ->expects($this->any())
352
+            ->method('getUser')
353
+            ->willReturn($user);
354
+        $this->request
355
+            ->expects($this->once())
356
+            ->method('passesCSRFCheck')
357
+            ->willReturn(false);
358
+        $this->auth->check($request, $response);
359
+    }
360
+
361
+    public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenForNonGetAndDesktopClient(): void {
362
+        $request = $this->createMock(RequestInterface::class);
363
+        $response = $this->createMock(ResponseInterface::class);
364
+        $this->userSession
365
+            ->expects($this->any())
366
+            ->method('isLoggedIn')
367
+            ->willReturn(true);
368
+        $this->request
369
+            ->expects($this->any())
370
+            ->method('getMethod')
371
+            ->willReturn('POST');
372
+        $this->request
373
+            ->expects($this->any())
374
+            ->method('isUserAgent')
375
+            ->willReturn(true);
376
+        $this->session
377
+            ->expects($this->any())
378
+            ->method('get')
379
+            ->with('AUTHENTICATED_TO_DAV_BACKEND')
380
+            ->willReturn(null);
381
+        $user = $this->createMock(IUser::class);
382
+        $user->expects($this->any())
383
+            ->method('getUID')
384
+            ->willReturn('MyWrongDavUser');
385
+        $this->userSession
386
+            ->expects($this->any())
387
+            ->method('getUser')
388
+            ->willReturn($user);
389
+        $this->request
390
+            ->expects($this->once())
391
+            ->method('passesCSRFCheck')
392
+            ->willReturn(false);
393
+
394
+        $this->auth->check($request, $response);
395
+    }
396
+
397
+    public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenForGet(): void {
398
+        $request = $this->createMock(RequestInterface::class);
399
+        $response = $this->createMock(ResponseInterface::class);
400
+        $this->userSession
401
+            ->expects($this->any())
402
+            ->method('isLoggedIn')
403
+            ->willReturn(true);
404
+        $this->session
405
+            ->expects($this->any())
406
+            ->method('get')
407
+            ->with('AUTHENTICATED_TO_DAV_BACKEND')
408
+            ->willReturn(null);
409
+        $user = $this->createMock(IUser::class);
410
+        $user->expects($this->any())
411
+            ->method('getUID')
412
+            ->willReturn('MyWrongDavUser');
413
+        $this->userSession
414
+            ->expects($this->any())
415
+            ->method('getUser')
416
+            ->willReturn($user);
417
+        $this->request
418
+            ->expects($this->any())
419
+            ->method('getMethod')
420
+            ->willReturn('GET');
421
+
422
+        $response = $this->auth->check($request, $response);
423
+        $this->assertEquals([true, 'principals/users/MyWrongDavUser'], $response);
424
+    }
425
+
426
+    public function testAuthenticateAlreadyLoggedInWithCsrfTokenForGet(): void {
427
+        $request = $this->createMock(RequestInterface::class);
428
+        $response = $this->createMock(ResponseInterface::class);
429
+        $this->userSession
430
+            ->expects($this->any())
431
+            ->method('isLoggedIn')
432
+            ->willReturn(true);
433
+        $this->session
434
+            ->expects($this->any())
435
+            ->method('get')
436
+            ->with('AUTHENTICATED_TO_DAV_BACKEND')
437
+            ->willReturn(null);
438
+        $user = $this->createMock(IUser::class);
439
+        $user->expects($this->any())
440
+            ->method('getUID')
441
+            ->willReturn('MyWrongDavUser');
442
+        $this->userSession
443
+            ->expects($this->any())
444
+            ->method('getUser')
445
+            ->willReturn($user);
446
+        $this->request
447
+            ->expects($this->once())
448
+            ->method('passesCSRFCheck')
449
+            ->willReturn(true);
450
+
451
+        $response = $this->auth->check($request, $response);
452
+        $this->assertEquals([true, 'principals/users/MyWrongDavUser'], $response);
453
+    }
454
+
455
+    public function testAuthenticateNoBasicAuthenticateHeadersProvided(): void {
456
+        $server = $this->createMock(Server::class);
457
+        $server->httpRequest = $this->createMock(RequestInterface::class);
458
+        $server->httpResponse = $this->createMock(ResponseInterface::class);
459
+        $response = $this->auth->check($server->httpRequest, $server->httpResponse);
460
+        $this->assertEquals([false, 'No \'Authorization: Basic\' header found. Either the client didn\'t send one, or the server is misconfigured'], $response);
461
+    }
462
+
463
+
464
+    public function testAuthenticateNoBasicAuthenticateHeadersProvidedWithAjax(): void {
465
+        $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class);
466
+        $this->expectExceptionMessage('Cannot authenticate over ajax calls');
467
+
468
+        /** @var \Sabre\HTTP\RequestInterface&MockObject $httpRequest */
469
+        $httpRequest = $this->createMock(RequestInterface::class);
470
+        /** @var \Sabre\HTTP\ResponseInterface&MockObject $httpResponse */
471
+        $httpResponse = $this->createMock(ResponseInterface::class);
472
+        $this->userSession
473
+            ->expects($this->any())
474
+            ->method('isLoggedIn')
475
+            ->willReturn(false);
476
+        $httpRequest
477
+            ->expects($this->exactly(2))
478
+            ->method('getHeader')
479
+            ->willReturnMap([
480
+                ['X-Requested-With', 'XMLHttpRequest'],
481
+                ['Authorization', null],
482
+            ]);
483
+
484
+        $this->auth->check($httpRequest, $httpResponse);
485
+    }
486
+
487
+    public function testAuthenticateWithBasicAuthenticateHeadersProvidedWithAjax(): void {
488
+        // No CSRF
489
+        $this->request
490
+            ->expects($this->once())
491
+            ->method('passesCSRFCheck')
492
+            ->willReturn(false);
493
+
494
+        /** @var \Sabre\HTTP\RequestInterface&MockObject $httpRequest */
495
+        $httpRequest = $this->createMock(RequestInterface::class);
496
+        /** @var \Sabre\HTTP\ResponseInterface&MockObject $httpResponse */
497
+        $httpResponse = $this->createMock(ResponseInterface::class);
498
+        $httpRequest
499
+            ->expects($this->any())
500
+            ->method('getHeader')
501
+            ->willReturnMap([
502
+                ['X-Requested-With', 'XMLHttpRequest'],
503
+                ['Authorization', 'basic dXNlcm5hbWU6cGFzc3dvcmQ='],
504
+            ]);
505
+
506
+        $user = $this->createMock(IUser::class);
507
+        $user->expects($this->any())
508
+            ->method('getUID')
509
+            ->willReturn('MyDavUser');
510
+        $this->userSession
511
+            ->expects($this->any())
512
+            ->method('isLoggedIn')
513
+            ->willReturn(false);
514
+        $this->userSession
515
+            ->expects($this->once())
516
+            ->method('logClientIn')
517
+            ->with('username', 'password')
518
+            ->willReturn(true);
519
+        $this->userSession
520
+            ->expects($this->any())
521
+            ->method('getUser')
522
+            ->willReturn($user);
523
+
524
+        $this->auth->check($httpRequest, $httpResponse);
525
+    }
526
+
527
+    public function testAuthenticateNoBasicAuthenticateHeadersProvidedWithAjaxButUserIsStillLoggedIn(): void {
528
+        /** @var \Sabre\HTTP\RequestInterface $httpRequest */
529
+        $httpRequest = $this->createMock(RequestInterface::class);
530
+        /** @var \Sabre\HTTP\ResponseInterface $httpResponse */
531
+        $httpResponse = $this->createMock(ResponseInterface::class);
532
+        $user = $this->createMock(IUser::class);
533
+        $user->method('getUID')->willReturn('MyTestUser');
534
+        $this->userSession
535
+            ->expects($this->any())
536
+            ->method('isLoggedIn')
537
+            ->willReturn(true);
538
+        $this->userSession
539
+            ->expects($this->any())
540
+            ->method('getUser')
541
+            ->willReturn($user);
542
+        $this->session
543
+            ->expects($this->atLeastOnce())
544
+            ->method('get')
545
+            ->with('AUTHENTICATED_TO_DAV_BACKEND')
546
+            ->willReturn('MyTestUser');
547
+        $this->request
548
+            ->expects($this->once())
549
+            ->method('getMethod')
550
+            ->willReturn('GET');
551
+        $httpRequest
552
+            ->expects($this->atLeastOnce())
553
+            ->method('getHeader')
554
+            ->with('Authorization')
555
+            ->willReturn(null);
556
+        $this->assertEquals(
557
+            [true, 'principals/users/MyTestUser'],
558
+            $this->auth->check($httpRequest, $httpResponse)
559
+        );
560
+    }
561
+
562
+    public function testAuthenticateValidCredentials(): void {
563
+        $server = $this->createMock(Server::class);
564
+        $server->httpRequest = $this->createMock(RequestInterface::class);
565
+        $server->httpRequest
566
+            ->expects($this->once())
567
+            ->method('getHeader')
568
+            ->with('Authorization')
569
+            ->willReturn('basic dXNlcm5hbWU6cGFzc3dvcmQ=');
570
+
571
+        $server->httpResponse = $this->createMock(ResponseInterface::class);
572
+        $this->userSession
573
+            ->expects($this->once())
574
+            ->method('logClientIn')
575
+            ->with('username', 'password')
576
+            ->willReturn(true);
577
+        $user = $this->createMock(IUser::class);
578
+        $user->expects($this->exactly(2))
579
+            ->method('getUID')
580
+            ->willReturn('MyTestUser');
581
+        $this->userSession
582
+            ->expects($this->exactly(3))
583
+            ->method('getUser')
584
+            ->willReturn($user);
585
+        $response = $this->auth->check($server->httpRequest, $server->httpResponse);
586
+        $this->assertEquals([true, 'principals/users/MyTestUser'], $response);
587
+    }
588
+
589
+    public function testAuthenticateInvalidCredentials(): void {
590
+        $server = $this->createMock(Server::class);
591
+        $server->httpRequest = $this->createMock(RequestInterface::class);
592
+        $server->httpRequest
593
+            ->expects($this->exactly(2))
594
+            ->method('getHeader')
595
+            ->willReturnMap([
596
+                ['Authorization', 'basic dXNlcm5hbWU6cGFzc3dvcmQ='],
597
+                ['X-Requested-With', null],
598
+            ]);
599
+        $server->httpResponse = $this->createMock(ResponseInterface::class);
600
+        $this->userSession
601
+            ->expects($this->once())
602
+            ->method('logClientIn')
603
+            ->with('username', 'password')
604
+            ->willReturn(false);
605
+        $response = $this->auth->check($server->httpRequest, $server->httpResponse);
606
+        $this->assertEquals([false, 'Username or password was incorrect'], $response);
607
+    }
608 608
 }
Please login to merge, or discard this patch.
tests/lib/Cache/FileCacheTest.php 1 patch
Indentation   +126 added lines, -126 removed lines patch added patch discarded remove patch
@@ -29,132 +29,132 @@
 block discarded – undo
29 29
  * @package Test\Cache
30 30
  */
31 31
 class FileCacheTest extends TestCache {
32
-	use UserTrait;
33
-
34
-	/**
35
-	 * @var string
36
-	 * */
37
-	private $user;
38
-	/**
39
-	 * @var string
40
-	 * */
41
-	private $datadir;
42
-	/**
43
-	 * @var Storage
44
-	 * */
45
-	private $storage;
46
-	/**
47
-	 * @var View
48
-	 * */
49
-	private $rootView;
50
-
51
-	public function skip() {
52
-		//$this->skipUnless(OC_User::isLoggedIn());
53
-	}
54
-
55
-	protected function setUp(): void {
56
-		parent::setUp();
57
-
58
-		//login
59
-		$this->createUser('test', 'test');
60
-
61
-		$this->user = \OC_User::getUser();
62
-		\OC_User::setUserId('test');
63
-
64
-		//clear all proxies and hooks so we can do clean testing
65
-		\OC_Hook::clear('OC_Filesystem');
66
-
67
-		/** @var IMountManager $manager */
68
-		$manager = Server::get(IMountManager::class);
69
-		$manager->removeMount('/test');
70
-
71
-		$storage = new Temporary([]);
72
-		Filesystem::mount($storage, [], '/test/cache');
73
-
74
-		//set up the users dir
75
-		$this->rootView = new View('');
76
-		$this->rootView->mkdir('/test');
77
-
78
-		$this->instance = new File();
79
-
80
-		// forces creation of cache folder for subsequent tests
81
-		$this->instance->set('hack', 'hack');
82
-	}
83
-
84
-	protected function tearDown(): void {
85
-		if ($this->instance) {
86
-			$this->instance->remove('hack', 'hack');
87
-		}
88
-
89
-		\OC_User::setUserId($this->user);
90
-
91
-		if ($this->instance) {
92
-			$this->instance->clear();
93
-			$this->instance = null;
94
-		}
95
-
96
-		parent::tearDown();
97
-	}
98
-
99
-	private function setupMockStorage() {
100
-		$mockStorage = $this->getMockBuilder(Local::class)
101
-			->onlyMethods(['filemtime', 'unlink'])
102
-			->setConstructorArgs([['datadir' => Server::get(ITempManager::class)->getTemporaryFolder()]])
103
-			->getMock();
104
-
105
-		Filesystem::mount($mockStorage, [], '/test/cache');
106
-
107
-		return $mockStorage;
108
-	}
109
-
110
-	public function testGarbageCollectOldKeys(): void {
111
-		$mockStorage = $this->setupMockStorage();
112
-
113
-		$mockStorage->expects($this->atLeastOnce())
114
-			->method('filemtime')
115
-			->willReturn(100);
116
-		$mockStorage->expects($this->once())
117
-			->method('unlink')
118
-			->with('key1')
119
-			->willReturn(true);
120
-
121
-		$this->instance->set('key1', 'value1');
122
-		$this->instance->gc();
123
-	}
124
-
125
-	public function testGarbageCollectLeaveRecentKeys(): void {
126
-		$mockStorage = $this->setupMockStorage();
127
-
128
-		$mockStorage->expects($this->atLeastOnce())
129
-			->method('filemtime')
130
-			->willReturn(time() + 3600);
131
-		$mockStorage->expects($this->never())
132
-			->method('unlink')
133
-			->with('key1');
134
-		$this->instance->set('key1', 'value1');
135
-		$this->instance->gc();
136
-	}
137
-
138
-	public static function lockExceptionProvider(): array {
139
-		return [
140
-			[new LockedException('key1')],
141
-			[new LockNotAcquiredException('key1', 1)],
142
-		];
143
-	}
144
-
145
-	#[\PHPUnit\Framework\Attributes\DataProvider('lockExceptionProvider')]
146
-	public function testGarbageCollectIgnoreLockedKeys($testException): void {
147
-		$mockStorage = $this->setupMockStorage();
148
-
149
-		$mockStorage->expects($this->atLeastOnce())
150
-			->method('filemtime')
151
-			->willReturn(100);
152
-		$mockStorage->expects($this->atLeastOnce())
153
-			->method('unlink')->willReturnOnConsecutiveCalls($this->throwException($testException), $this->returnValue(true));
32
+    use UserTrait;
33
+
34
+    /**
35
+     * @var string
36
+     * */
37
+    private $user;
38
+    /**
39
+     * @var string
40
+     * */
41
+    private $datadir;
42
+    /**
43
+     * @var Storage
44
+     * */
45
+    private $storage;
46
+    /**
47
+     * @var View
48
+     * */
49
+    private $rootView;
50
+
51
+    public function skip() {
52
+        //$this->skipUnless(OC_User::isLoggedIn());
53
+    }
54
+
55
+    protected function setUp(): void {
56
+        parent::setUp();
57
+
58
+        //login
59
+        $this->createUser('test', 'test');
60
+
61
+        $this->user = \OC_User::getUser();
62
+        \OC_User::setUserId('test');
63
+
64
+        //clear all proxies and hooks so we can do clean testing
65
+        \OC_Hook::clear('OC_Filesystem');
66
+
67
+        /** @var IMountManager $manager */
68
+        $manager = Server::get(IMountManager::class);
69
+        $manager->removeMount('/test');
70
+
71
+        $storage = new Temporary([]);
72
+        Filesystem::mount($storage, [], '/test/cache');
73
+
74
+        //set up the users dir
75
+        $this->rootView = new View('');
76
+        $this->rootView->mkdir('/test');
77
+
78
+        $this->instance = new File();
79
+
80
+        // forces creation of cache folder for subsequent tests
81
+        $this->instance->set('hack', 'hack');
82
+    }
83
+
84
+    protected function tearDown(): void {
85
+        if ($this->instance) {
86
+            $this->instance->remove('hack', 'hack');
87
+        }
88
+
89
+        \OC_User::setUserId($this->user);
90
+
91
+        if ($this->instance) {
92
+            $this->instance->clear();
93
+            $this->instance = null;
94
+        }
95
+
96
+        parent::tearDown();
97
+    }
98
+
99
+    private function setupMockStorage() {
100
+        $mockStorage = $this->getMockBuilder(Local::class)
101
+            ->onlyMethods(['filemtime', 'unlink'])
102
+            ->setConstructorArgs([['datadir' => Server::get(ITempManager::class)->getTemporaryFolder()]])
103
+            ->getMock();
104
+
105
+        Filesystem::mount($mockStorage, [], '/test/cache');
106
+
107
+        return $mockStorage;
108
+    }
109
+
110
+    public function testGarbageCollectOldKeys(): void {
111
+        $mockStorage = $this->setupMockStorage();
112
+
113
+        $mockStorage->expects($this->atLeastOnce())
114
+            ->method('filemtime')
115
+            ->willReturn(100);
116
+        $mockStorage->expects($this->once())
117
+            ->method('unlink')
118
+            ->with('key1')
119
+            ->willReturn(true);
120
+
121
+        $this->instance->set('key1', 'value1');
122
+        $this->instance->gc();
123
+    }
124
+
125
+    public function testGarbageCollectLeaveRecentKeys(): void {
126
+        $mockStorage = $this->setupMockStorage();
127
+
128
+        $mockStorage->expects($this->atLeastOnce())
129
+            ->method('filemtime')
130
+            ->willReturn(time() + 3600);
131
+        $mockStorage->expects($this->never())
132
+            ->method('unlink')
133
+            ->with('key1');
134
+        $this->instance->set('key1', 'value1');
135
+        $this->instance->gc();
136
+    }
137
+
138
+    public static function lockExceptionProvider(): array {
139
+        return [
140
+            [new LockedException('key1')],
141
+            [new LockNotAcquiredException('key1', 1)],
142
+        ];
143
+    }
144
+
145
+    #[\PHPUnit\Framework\Attributes\DataProvider('lockExceptionProvider')]
146
+    public function testGarbageCollectIgnoreLockedKeys($testException): void {
147
+        $mockStorage = $this->setupMockStorage();
148
+
149
+        $mockStorage->expects($this->atLeastOnce())
150
+            ->method('filemtime')
151
+            ->willReturn(100);
152
+        $mockStorage->expects($this->atLeastOnce())
153
+            ->method('unlink')->willReturnOnConsecutiveCalls($this->throwException($testException), $this->returnValue(true));
154 154
 
155
-		$this->instance->set('key1', 'value1');
156
-		$this->instance->set('key2', 'value2');
155
+        $this->instance->set('key1', 'value1');
156
+        $this->instance->set('key2', 'value2');
157 157
 
158
-		$this->instance->gc();
159
-	}
158
+        $this->instance->gc();
159
+    }
160 160
 }
Please login to merge, or discard this patch.
lib/private/Server.php 1 patch
Indentation   +1405 added lines, -1405 removed lines patch added patch discarded remove patch
@@ -249,1446 +249,1446 @@
 block discarded – undo
249 249
  * TODO: hookup all manager classes
250 250
  */
251 251
 class Server extends ServerContainer implements IServerContainer {
252
-	/** @var string */
253
-	private $webRoot;
254
-
255
-	/**
256
-	 * @param string $webRoot
257
-	 * @param \OC\Config $config
258
-	 */
259
-	public function __construct($webRoot, \OC\Config $config) {
260
-		parent::__construct();
261
-		$this->webRoot = $webRoot;
262
-
263
-		// To find out if we are running from CLI or not
264
-		$this->registerParameter('isCLI', \OC::$CLI);
265
-		$this->registerParameter('serverRoot', \OC::$SERVERROOT);
266
-
267
-		$this->registerService(ContainerInterface::class, function (ContainerInterface $c) {
268
-			return $c;
269
-		});
270
-		$this->registerDeprecatedAlias(\OCP\IServerContainer::class, ContainerInterface::class);
271
-
272
-		$this->registerAlias(\OCP\Calendar\IManager::class, \OC\Calendar\Manager::class);
273
-
274
-		$this->registerAlias(\OCP\Calendar\Resource\IManager::class, \OC\Calendar\Resource\Manager::class);
275
-
276
-		$this->registerAlias(\OCP\Calendar\Room\IManager::class, \OC\Calendar\Room\Manager::class);
277
-
278
-		$this->registerAlias(\OCP\Contacts\IManager::class, \OC\ContactsManager::class);
279
-
280
-		$this->registerAlias(\OCP\DirectEditing\IManager::class, \OC\DirectEditing\Manager::class);
281
-		$this->registerAlias(ITemplateManager::class, TemplateManager::class);
282
-		$this->registerAlias(\OCP\Template\ITemplateManager::class, \OC\Template\TemplateManager::class);
283
-
284
-		$this->registerAlias(IActionFactory::class, ActionFactory::class);
285
-
286
-		$this->registerService(View::class, function (Server $c) {
287
-			return new View();
288
-		}, false);
289
-
290
-		$this->registerService(IPreview::class, function (ContainerInterface $c) {
291
-			return new PreviewManager(
292
-				$c->get(\OCP\IConfig::class),
293
-				$c->get(IRootFolder::class),
294
-				new \OC\Preview\Storage\Root(
295
-					$c->get(IRootFolder::class),
296
-					$c->get(SystemConfig::class)
297
-				),
298
-				$c->get(IEventDispatcher::class),
299
-				$c->get(GeneratorHelper::class),
300
-				$c->get(ISession::class)->get('user_id'),
301
-				$c->get(Coordinator::class),
302
-				$c->get(IServerContainer::class),
303
-				$c->get(IBinaryFinder::class),
304
-				$c->get(IMagickSupport::class)
305
-			);
306
-		});
307
-		$this->registerAlias(IMimeIconProvider::class, MimeIconProvider::class);
308
-
309
-		$this->registerService(\OC\Preview\Watcher::class, function (ContainerInterface $c) {
310
-			return new \OC\Preview\Watcher(
311
-				new \OC\Preview\Storage\Root(
312
-					$c->get(IRootFolder::class),
313
-					$c->get(SystemConfig::class)
314
-				)
315
-			);
316
-		});
317
-
318
-		$this->registerService(IProfiler::class, function (Server $c) {
319
-			return new Profiler($c->get(SystemConfig::class));
320
-		});
321
-
322
-		$this->registerService(Encryption\Manager::class, function (Server $c): Encryption\Manager {
323
-			$view = new View();
324
-			$util = new Encryption\Util(
325
-				$view,
326
-				$c->get(IUserManager::class),
327
-				$c->get(IGroupManager::class),
328
-				$c->get(\OCP\IConfig::class)
329
-			);
330
-			return new Encryption\Manager(
331
-				$c->get(\OCP\IConfig::class),
332
-				$c->get(LoggerInterface::class),
333
-				$c->getL10N('core'),
334
-				new View(),
335
-				$util,
336
-				new ArrayCache()
337
-			);
338
-		});
339
-		$this->registerAlias(\OCP\Encryption\IManager::class, Encryption\Manager::class);
340
-
341
-		$this->registerService(IFile::class, function (ContainerInterface $c) {
342
-			$util = new Encryption\Util(
343
-				new View(),
344
-				$c->get(IUserManager::class),
345
-				$c->get(IGroupManager::class),
346
-				$c->get(\OCP\IConfig::class)
347
-			);
348
-			return new Encryption\File(
349
-				$util,
350
-				$c->get(IRootFolder::class),
351
-				$c->get(\OCP\Share\IManager::class)
352
-			);
353
-		});
354
-
355
-		$this->registerService(IStorage::class, function (ContainerInterface $c) {
356
-			$view = new View();
357
-			$util = new Encryption\Util(
358
-				$view,
359
-				$c->get(IUserManager::class),
360
-				$c->get(IGroupManager::class),
361
-				$c->get(\OCP\IConfig::class)
362
-			);
363
-
364
-			return new Encryption\Keys\Storage(
365
-				$view,
366
-				$util,
367
-				$c->get(ICrypto::class),
368
-				$c->get(\OCP\IConfig::class)
369
-			);
370
-		});
371
-
372
-		$this->registerAlias(\OCP\ITagManager::class, TagManager::class);
373
-
374
-		$this->registerService('SystemTagManagerFactory', function (ContainerInterface $c) {
375
-			/** @var \OCP\IConfig $config */
376
-			$config = $c->get(\OCP\IConfig::class);
377
-			$factoryClass = $config->getSystemValue('systemtags.managerFactory', SystemTagManagerFactory::class);
378
-			return new $factoryClass($this);
379
-		});
380
-		$this->registerService(ISystemTagManager::class, function (ContainerInterface $c) {
381
-			return $c->get('SystemTagManagerFactory')->getManager();
382
-		});
383
-		/** @deprecated 19.0.0 */
384
-		$this->registerDeprecatedAlias('SystemTagManager', ISystemTagManager::class);
385
-
386
-		$this->registerService(ISystemTagObjectMapper::class, function (ContainerInterface $c) {
387
-			return $c->get('SystemTagManagerFactory')->getObjectMapper();
388
-		});
389
-		$this->registerAlias(IFileAccess::class, FileAccess::class);
390
-		$this->registerService('RootFolder', function (ContainerInterface $c) {
391
-			$manager = \OC\Files\Filesystem::getMountManager();
392
-			$view = new View();
393
-			/** @var IUserSession $userSession */
394
-			$userSession = $c->get(IUserSession::class);
395
-			$root = new Root(
396
-				$manager,
397
-				$view,
398
-				$userSession->getUser(),
399
-				$c->get(IUserMountCache::class),
400
-				$this->get(LoggerInterface::class),
401
-				$this->get(IUserManager::class),
402
-				$this->get(IEventDispatcher::class),
403
-				$this->get(ICacheFactory::class),
404
-			);
405
-
406
-			$previewConnector = new \OC\Preview\WatcherConnector(
407
-				$root,
408
-				$c->get(SystemConfig::class),
409
-				$this->get(IEventDispatcher::class)
410
-			);
411
-			$previewConnector->connectWatcher();
412
-
413
-			return $root;
414
-		});
415
-		$this->registerService(HookConnector::class, function (ContainerInterface $c) {
416
-			return new HookConnector(
417
-				$c->get(IRootFolder::class),
418
-				new View(),
419
-				$c->get(IEventDispatcher::class),
420
-				$c->get(LoggerInterface::class)
421
-			);
422
-		});
423
-
424
-		$this->registerService(IRootFolder::class, function (ContainerInterface $c) {
425
-			return new LazyRoot(function () use ($c) {
426
-				return $c->get('RootFolder');
427
-			});
428
-		});
429
-
430
-		$this->registerAlias(\OCP\IUserManager::class, \OC\User\Manager::class);
431
-
432
-		$this->registerService(DisplayNameCache::class, function (ContainerInterface $c) {
433
-			return $c->get(\OC\User\Manager::class)->getDisplayNameCache();
434
-		});
435
-
436
-		$this->registerService(\OCP\IGroupManager::class, function (ContainerInterface $c) {
437
-			$groupManager = new \OC\Group\Manager(
438
-				$this->get(IUserManager::class),
439
-				$this->get(IEventDispatcher::class),
440
-				$this->get(LoggerInterface::class),
441
-				$this->get(ICacheFactory::class),
442
-				$this->get(IRemoteAddress::class),
443
-			);
444
-			return $groupManager;
445
-		});
446
-
447
-		$this->registerService(Store::class, function (ContainerInterface $c) {
448
-			$session = $c->get(ISession::class);
449
-			if (\OC::$server->get(SystemConfig::class)->getValue('installed', false)) {
450
-				$tokenProvider = $c->get(IProvider::class);
451
-			} else {
452
-				$tokenProvider = null;
453
-			}
454
-			$logger = $c->get(LoggerInterface::class);
455
-			$crypto = $c->get(ICrypto::class);
456
-			return new Store($session, $logger, $crypto, $tokenProvider);
457
-		});
458
-		$this->registerAlias(IStore::class, Store::class);
459
-		$this->registerAlias(IProvider::class, Authentication\Token\Manager::class);
460
-		$this->registerAlias(OCPIProvider::class, Authentication\Token\Manager::class);
461
-
462
-		$this->registerService(\OC\User\Session::class, function (Server $c) {
463
-			$manager = $c->get(IUserManager::class);
464
-			$session = new \OC\Session\Memory();
465
-			$timeFactory = new TimeFactory();
466
-			// Token providers might require a working database. This code
467
-			// might however be called when Nextcloud is not yet setup.
468
-			if (\OC::$server->get(SystemConfig::class)->getValue('installed', false)) {
469
-				$provider = $c->get(IProvider::class);
470
-			} else {
471
-				$provider = null;
472
-			}
473
-
474
-			$userSession = new \OC\User\Session(
475
-				$manager,
476
-				$session,
477
-				$timeFactory,
478
-				$provider,
479
-				$c->get(\OCP\IConfig::class),
480
-				$c->get(ISecureRandom::class),
481
-				$c->get('LockdownManager'),
482
-				$c->get(LoggerInterface::class),
483
-				$c->get(IEventDispatcher::class),
484
-			);
485
-			/** @deprecated 21.0.0 use BeforeUserCreatedEvent event with the IEventDispatcher instead */
486
-			$userSession->listen('\OC\User', 'preCreateUser', function ($uid, $password) {
487
-				\OC_Hook::emit('OC_User', 'pre_createUser', ['run' => true, 'uid' => $uid, 'password' => $password]);
488
-			});
489
-			/** @deprecated 21.0.0 use UserCreatedEvent event with the IEventDispatcher instead */
490
-			$userSession->listen('\OC\User', 'postCreateUser', function ($user, $password) {
491
-				/** @var \OC\User\User $user */
492
-				\OC_Hook::emit('OC_User', 'post_createUser', ['uid' => $user->getUID(), 'password' => $password]);
493
-			});
494
-			/** @deprecated 21.0.0 use BeforeUserDeletedEvent event with the IEventDispatcher instead */
495
-			$userSession->listen('\OC\User', 'preDelete', function ($user) {
496
-				/** @var \OC\User\User $user */
497
-				\OC_Hook::emit('OC_User', 'pre_deleteUser', ['run' => true, 'uid' => $user->getUID()]);
498
-			});
499
-			/** @deprecated 21.0.0 use UserDeletedEvent event with the IEventDispatcher instead */
500
-			$userSession->listen('\OC\User', 'postDelete', function ($user) {
501
-				/** @var \OC\User\User $user */
502
-				\OC_Hook::emit('OC_User', 'post_deleteUser', ['uid' => $user->getUID()]);
503
-			});
504
-			$userSession->listen('\OC\User', 'preSetPassword', function ($user, $password, $recoveryPassword) {
505
-				/** @var \OC\User\User $user */
506
-				\OC_Hook::emit('OC_User', 'pre_setPassword', ['run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword]);
507
-			});
508
-			$userSession->listen('\OC\User', 'postSetPassword', function ($user, $password, $recoveryPassword) {
509
-				/** @var \OC\User\User $user */
510
-				\OC_Hook::emit('OC_User', 'post_setPassword', ['run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword]);
511
-			});
512
-			$userSession->listen('\OC\User', 'preLogin', function ($uid, $password) {
513
-				\OC_Hook::emit('OC_User', 'pre_login', ['run' => true, 'uid' => $uid, 'password' => $password]);
514
-
515
-				/** @var IEventDispatcher $dispatcher */
516
-				$dispatcher = $this->get(IEventDispatcher::class);
517
-				$dispatcher->dispatchTyped(new BeforeUserLoggedInEvent($uid, $password));
518
-			});
519
-			$userSession->listen('\OC\User', 'postLogin', function ($user, $loginName, $password, $isTokenLogin) {
520
-				/** @var \OC\User\User $user */
521
-				\OC_Hook::emit('OC_User', 'post_login', ['run' => true, 'uid' => $user->getUID(), 'loginName' => $loginName, 'password' => $password, 'isTokenLogin' => $isTokenLogin]);
522
-
523
-				/** @var IEventDispatcher $dispatcher */
524
-				$dispatcher = $this->get(IEventDispatcher::class);
525
-				$dispatcher->dispatchTyped(new UserLoggedInEvent($user, $loginName, $password, $isTokenLogin));
526
-			});
527
-			$userSession->listen('\OC\User', 'preRememberedLogin', function ($uid) {
528
-				/** @var IEventDispatcher $dispatcher */
529
-				$dispatcher = $this->get(IEventDispatcher::class);
530
-				$dispatcher->dispatchTyped(new BeforeUserLoggedInWithCookieEvent($uid));
531
-			});
532
-			$userSession->listen('\OC\User', 'postRememberedLogin', function ($user, $password) {
533
-				/** @var \OC\User\User $user */
534
-				\OC_Hook::emit('OC_User', 'post_login', ['run' => true, 'uid' => $user->getUID(), 'password' => $password]);
535
-
536
-				/** @var IEventDispatcher $dispatcher */
537
-				$dispatcher = $this->get(IEventDispatcher::class);
538
-				$dispatcher->dispatchTyped(new UserLoggedInWithCookieEvent($user, $password));
539
-			});
540
-			$userSession->listen('\OC\User', 'logout', function ($user) {
541
-				\OC_Hook::emit('OC_User', 'logout', []);
542
-
543
-				/** @var IEventDispatcher $dispatcher */
544
-				$dispatcher = $this->get(IEventDispatcher::class);
545
-				$dispatcher->dispatchTyped(new BeforeUserLoggedOutEvent($user));
546
-			});
547
-			$userSession->listen('\OC\User', 'postLogout', function ($user) {
548
-				/** @var IEventDispatcher $dispatcher */
549
-				$dispatcher = $this->get(IEventDispatcher::class);
550
-				$dispatcher->dispatchTyped(new UserLoggedOutEvent($user));
551
-			});
552
-			$userSession->listen('\OC\User', 'changeUser', function ($user, $feature, $value, $oldValue) {
553
-				/** @var \OC\User\User $user */
554
-				\OC_Hook::emit('OC_User', 'changeUser', ['run' => true, 'user' => $user, 'feature' => $feature, 'value' => $value, 'old_value' => $oldValue]);
555
-			});
556
-			return $userSession;
557
-		});
558
-		$this->registerAlias(\OCP\IUserSession::class, \OC\User\Session::class);
559
-
560
-		$this->registerAlias(\OCP\Authentication\TwoFactorAuth\IRegistry::class, \OC\Authentication\TwoFactorAuth\Registry::class);
561
-
562
-		$this->registerAlias(INavigationManager::class, \OC\NavigationManager::class);
563
-
564
-		$this->registerAlias(\OCP\IConfig::class, \OC\AllConfig::class);
565
-
566
-		$this->registerService(\OC\SystemConfig::class, function ($c) use ($config) {
567
-			return new \OC\SystemConfig($config);
568
-		});
569
-
570
-		$this->registerAlias(IAppConfig::class, \OC\AppConfig::class);
571
-		$this->registerAlias(IUserConfig::class, \OC\Config\UserConfig::class);
572
-		$this->registerAlias(IAppManager::class, AppManager::class);
573
-
574
-		$this->registerService(IFactory::class, function (Server $c) {
575
-			return new \OC\L10N\Factory(
576
-				$c->get(\OCP\IConfig::class),
577
-				$c->getRequest(),
578
-				$c->get(IUserSession::class),
579
-				$c->get(ICacheFactory::class),
580
-				\OC::$SERVERROOT,
581
-				$c->get(IAppManager::class),
582
-			);
583
-		});
584
-
585
-		$this->registerAlias(IURLGenerator::class, URLGenerator::class);
586
-
587
-		$this->registerService(ICache::class, function ($c) {
588
-			return new Cache\File();
589
-		});
590
-
591
-		$this->registerService(Factory::class, function (Server $c) {
592
-			$profiler = $c->get(IProfiler::class);
593
-			$arrayCacheFactory = new \OC\Memcache\Factory(fn () => '', $c->get(LoggerInterface::class),
594
-				$profiler,
595
-				ArrayCache::class,
596
-				ArrayCache::class,
597
-				ArrayCache::class
598
-			);
599
-			/** @var SystemConfig $config */
600
-			$config = $c->get(SystemConfig::class);
601
-			/** @var ServerVersion $serverVersion */
602
-			$serverVersion = $c->get(ServerVersion::class);
603
-
604
-			if ($config->getValue('installed', false) && !(defined('PHPUNIT_RUN') && PHPUNIT_RUN)) {
605
-				$logQuery = $config->getValue('log_query');
606
-				$prefixClosure = function () use ($logQuery, $serverVersion): ?string {
607
-					if (!$logQuery) {
608
-						try {
609
-							$v = \OCP\Server::get(IAppConfig::class)->getAppInstalledVersions(true);
610
-						} catch (\Doctrine\DBAL\Exception $e) {
611
-							// Database service probably unavailable
612
-							// Probably related to https://github.com/nextcloud/server/issues/37424
613
-							return null;
614
-						}
615
-					} else {
616
-						// If the log_query is enabled, we can not get the app versions
617
-						// as that does a query, which will be logged and the logging
618
-						// depends on redis and here we are back again in the same function.
619
-						$v = [
620
-							'log_query' => 'enabled',
621
-						];
622
-					}
623
-					$v['core'] = implode(',', $serverVersion->getVersion());
624
-					$version = implode(',', array_keys($v)) . implode(',', $v);
625
-					$instanceId = \OC_Util::getInstanceId();
626
-					$path = \OC::$SERVERROOT;
627
-					return md5($instanceId . '-' . $version . '-' . $path);
628
-				};
629
-				return new \OC\Memcache\Factory($prefixClosure,
630
-					$c->get(LoggerInterface::class),
631
-					$profiler,
632
-					/** @psalm-taint-escape callable */
633
-					$config->getValue('memcache.local', null),
634
-					/** @psalm-taint-escape callable */
635
-					$config->getValue('memcache.distributed', null),
636
-					/** @psalm-taint-escape callable */
637
-					$config->getValue('memcache.locking', null),
638
-					/** @psalm-taint-escape callable */
639
-					$config->getValue('redis_log_file')
640
-				);
641
-			}
642
-			return $arrayCacheFactory;
643
-		});
644
-		$this->registerAlias(ICacheFactory::class, Factory::class);
645
-
646
-		$this->registerService('RedisFactory', function (Server $c) {
647
-			$systemConfig = $c->get(SystemConfig::class);
648
-			return new RedisFactory($systemConfig, $c->get(IEventLogger::class));
649
-		});
650
-
651
-		$this->registerService(\OCP\Activity\IManager::class, function (Server $c) {
652
-			$l10n = $this->get(IFactory::class)->get('lib');
653
-			return new \OC\Activity\Manager(
654
-				$c->getRequest(),
655
-				$c->get(IUserSession::class),
656
-				$c->get(\OCP\IConfig::class),
657
-				$c->get(IValidator::class),
658
-				$c->get(IRichTextFormatter::class),
659
-				$l10n
660
-			);
661
-		});
662
-
663
-		$this->registerService(\OCP\Activity\IEventMerger::class, function (Server $c) {
664
-			return new \OC\Activity\EventMerger(
665
-				$c->getL10N('lib')
666
-			);
667
-		});
668
-		$this->registerAlias(IValidator::class, Validator::class);
669
-
670
-		$this->registerService(AvatarManager::class, function (Server $c) {
671
-			return new AvatarManager(
672
-				$c->get(IUserSession::class),
673
-				$c->get(\OC\User\Manager::class),
674
-				$c->getAppDataDir('avatar'),
675
-				$c->getL10N('lib'),
676
-				$c->get(LoggerInterface::class),
677
-				$c->get(\OCP\IConfig::class),
678
-				$c->get(IAccountManager::class),
679
-				$c->get(KnownUserService::class)
680
-			);
681
-		});
682
-
683
-		$this->registerAlias(IAvatarManager::class, AvatarManager::class);
684
-
685
-		$this->registerAlias(\OCP\Support\CrashReport\IRegistry::class, \OC\Support\CrashReport\Registry::class);
686
-		$this->registerAlias(\OCP\Support\Subscription\IRegistry::class, \OC\Support\Subscription\Registry::class);
687
-		$this->registerAlias(\OCP\Support\Subscription\IAssertion::class, \OC\Support\Subscription\Assertion::class);
688
-
689
-		/** Only used by the PsrLoggerAdapter should not be used by apps */
690
-		$this->registerService(\OC\Log::class, function (Server $c) {
691
-			$logType = $c->get(AllConfig::class)->getSystemValue('log_type', 'file');
692
-			$factory = new LogFactory($c, $this->get(SystemConfig::class));
693
-			$logger = $factory->get($logType);
694
-			$registry = $c->get(\OCP\Support\CrashReport\IRegistry::class);
695
-
696
-			return new Log($logger, $this->get(SystemConfig::class), crashReporters: $registry);
697
-		});
698
-		// PSR-3 logger
699
-		$this->registerAlias(LoggerInterface::class, PsrLoggerAdapter::class);
700
-
701
-		$this->registerService(ILogFactory::class, function (Server $c) {
702
-			return new LogFactory($c, $this->get(SystemConfig::class));
703
-		});
704
-
705
-		$this->registerAlias(IJobList::class, \OC\BackgroundJob\JobList::class);
706
-
707
-		$this->registerService(Router::class, function (Server $c) {
708
-			$cacheFactory = $c->get(ICacheFactory::class);
709
-			if ($cacheFactory->isLocalCacheAvailable()) {
710
-				$router = $c->resolve(CachingRouter::class);
711
-			} else {
712
-				$router = $c->resolve(Router::class);
713
-			}
714
-			return $router;
715
-		});
716
-		$this->registerAlias(IRouter::class, Router::class);
717
-
718
-		$this->registerService(\OC\Security\RateLimiting\Backend\IBackend::class, function ($c) {
719
-			$config = $c->get(\OCP\IConfig::class);
720
-			if (ltrim($config->getSystemValueString('memcache.distributed', ''), '\\') === \OC\Memcache\Redis::class) {
721
-				$backend = new \OC\Security\RateLimiting\Backend\MemoryCacheBackend(
722
-					$c->get(AllConfig::class),
723
-					$this->get(ICacheFactory::class),
724
-					new \OC\AppFramework\Utility\TimeFactory()
725
-				);
726
-			} else {
727
-				$backend = new \OC\Security\RateLimiting\Backend\DatabaseBackend(
728
-					$c->get(AllConfig::class),
729
-					$c->get(IDBConnection::class),
730
-					new \OC\AppFramework\Utility\TimeFactory()
731
-				);
732
-			}
733
-
734
-			return $backend;
735
-		});
736
-
737
-		$this->registerAlias(\OCP\Security\ISecureRandom::class, SecureRandom::class);
738
-		$this->registerAlias(\OCP\Security\IRemoteHostValidator::class, \OC\Security\RemoteHostValidator::class);
739
-		$this->registerAlias(IVerificationToken::class, VerificationToken::class);
740
-
741
-		$this->registerAlias(ICrypto::class, Crypto::class);
742
-
743
-		$this->registerAlias(IHasher::class, Hasher::class);
744
-
745
-		$this->registerAlias(ICredentialsManager::class, CredentialsManager::class);
746
-
747
-		$this->registerAlias(IDBConnection::class, ConnectionAdapter::class);
748
-		$this->registerService(Connection::class, function (Server $c) {
749
-			$systemConfig = $c->get(SystemConfig::class);
750
-			$factory = new \OC\DB\ConnectionFactory($systemConfig, $c->get(ICacheFactory::class));
751
-			$type = $systemConfig->getValue('dbtype', 'sqlite');
752
-			if (!$factory->isValidType($type)) {
753
-				throw new \OC\DatabaseException('Invalid database type');
754
-			}
755
-			$connection = $factory->getConnection($type, []);
756
-			return $connection;
757
-		});
758
-
759
-		$this->registerAlias(ICertificateManager::class, CertificateManager::class);
760
-		$this->registerAlias(IClientService::class, ClientService::class);
761
-		$this->registerService(NegativeDnsCache::class, function (ContainerInterface $c) {
762
-			return new NegativeDnsCache(
763
-				$c->get(ICacheFactory::class),
764
-			);
765
-		});
766
-		$this->registerDeprecatedAlias('HttpClientService', IClientService::class);
767
-		$this->registerService(IEventLogger::class, function (ContainerInterface $c) {
768
-			return new EventLogger($c->get(SystemConfig::class), $c->get(LoggerInterface::class), $c->get(Log::class));
769
-		});
770
-
771
-		$this->registerService(IQueryLogger::class, function (ContainerInterface $c) {
772
-			$queryLogger = new QueryLogger();
773
-			if ($c->get(SystemConfig::class)->getValue('debug', false)) {
774
-				// In debug mode, module is being activated by default
775
-				$queryLogger->activate();
776
-			}
777
-			return $queryLogger;
778
-		});
779
-
780
-		$this->registerAlias(ITempManager::class, TempManager::class);
781
-		$this->registerAlias(IDateTimeZone::class, DateTimeZone::class);
782
-
783
-		$this->registerService(IDateTimeFormatter::class, function (Server $c) {
784
-			$language = $c->get(\OCP\IConfig::class)->getUserValue($c->get(ISession::class)->get('user_id'), 'core', 'lang', null);
785
-
786
-			return new DateTimeFormatter(
787
-				$c->get(IDateTimeZone::class)->getTimeZone(),
788
-				$c->getL10N('lib', $language)
789
-			);
790
-		});
791
-
792
-		$this->registerService(IUserMountCache::class, function (ContainerInterface $c) {
793
-			$mountCache = $c->get(UserMountCache::class);
794
-			$listener = new UserMountCacheListener($mountCache);
795
-			$listener->listen($c->get(IUserManager::class));
796
-			return $mountCache;
797
-		});
798
-
799
-		$this->registerService(IMountProviderCollection::class, function (ContainerInterface $c) {
800
-			$loader = $c->get(IStorageFactory::class);
801
-			$mountCache = $c->get(IUserMountCache::class);
802
-			$eventLogger = $c->get(IEventLogger::class);
803
-			$manager = new MountProviderCollection($loader, $mountCache, $eventLogger);
804
-
805
-			// builtin providers
806
-
807
-			$config = $c->get(\OCP\IConfig::class);
808
-			$logger = $c->get(LoggerInterface::class);
809
-			$objectStoreConfig = $c->get(PrimaryObjectStoreConfig::class);
810
-			$manager->registerProvider(new CacheMountProvider($config));
811
-			$manager->registerHomeProvider(new LocalHomeMountProvider());
812
-			$manager->registerHomeProvider(new ObjectHomeMountProvider($objectStoreConfig));
813
-			$manager->registerRootProvider(new RootMountProvider($objectStoreConfig, $config));
814
-			$manager->registerRootProvider(new ObjectStorePreviewCacheMountProvider($logger, $config));
815
-
816
-			return $manager;
817
-		});
818
-
819
-		$this->registerService(IBus::class, function (ContainerInterface $c) {
820
-			$busClass = $c->get(\OCP\IConfig::class)->getSystemValueString('commandbus');
821
-			if ($busClass) {
822
-				[$app, $class] = explode('::', $busClass, 2);
823
-				if ($c->get(IAppManager::class)->isEnabledForUser($app)) {
824
-					$c->get(IAppManager::class)->loadApp($app);
825
-					return $c->get($class);
826
-				} else {
827
-					throw new ServiceUnavailableException("The app providing the command bus ($app) is not enabled");
828
-				}
829
-			} else {
830
-				$jobList = $c->get(IJobList::class);
831
-				return new CronBus($jobList);
832
-			}
833
-		});
834
-		$this->registerDeprecatedAlias('AsyncCommandBus', IBus::class);
835
-		$this->registerAlias(ITrustedDomainHelper::class, TrustedDomainHelper::class);
836
-		$this->registerAlias(IThrottler::class, Throttler::class);
837
-
838
-		$this->registerService(\OC\Security\Bruteforce\Backend\IBackend::class, function ($c) {
839
-			$config = $c->get(\OCP\IConfig::class);
840
-			if (!$config->getSystemValueBool('auth.bruteforce.protection.force.database', false)
841
-				&& ltrim($config->getSystemValueString('memcache.distributed', ''), '\\') === \OC\Memcache\Redis::class) {
842
-				$backend = $c->get(\OC\Security\Bruteforce\Backend\MemoryCacheBackend::class);
843
-			} else {
844
-				$backend = $c->get(\OC\Security\Bruteforce\Backend\DatabaseBackend::class);
845
-			}
846
-
847
-			return $backend;
848
-		});
849
-
850
-		$this->registerDeprecatedAlias('IntegrityCodeChecker', Checker::class);
851
-		$this->registerService(Checker::class, function (ContainerInterface $c) {
852
-			// IConfig requires a working database. This code
853
-			// might however be called when Nextcloud is not yet setup.
854
-			if (\OC::$server->get(SystemConfig::class)->getValue('installed', false)) {
855
-				$config = $c->get(\OCP\IConfig::class);
856
-				$appConfig = $c->get(\OCP\IAppConfig::class);
857
-			} else {
858
-				$config = null;
859
-				$appConfig = null;
860
-			}
861
-
862
-			return new Checker(
863
-				$c->get(ServerVersion::class),
864
-				$c->get(EnvironmentHelper::class),
865
-				new FileAccessHelper(),
866
-				new AppLocator(),
867
-				$config,
868
-				$appConfig,
869
-				$c->get(ICacheFactory::class),
870
-				$c->get(IAppManager::class),
871
-				$c->get(IMimeTypeDetector::class)
872
-			);
873
-		});
874
-		$this->registerService(Request::class, function (ContainerInterface $c) {
875
-			if (isset($this['urlParams'])) {
876
-				$urlParams = $this['urlParams'];
877
-			} else {
878
-				$urlParams = [];
879
-			}
880
-
881
-			if (defined('PHPUNIT_RUN') && PHPUNIT_RUN
882
-				&& in_array('fakeinput', stream_get_wrappers())
883
-			) {
884
-				$stream = 'fakeinput://data';
885
-			} else {
886
-				$stream = 'php://input';
887
-			}
888
-
889
-			return new Request(
890
-				[
891
-					'get' => $_GET,
892
-					'post' => $_POST,
893
-					'files' => $_FILES,
894
-					'server' => $_SERVER,
895
-					'env' => $_ENV,
896
-					'cookies' => $_COOKIE,
897
-					'method' => (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD']))
898
-						? $_SERVER['REQUEST_METHOD']
899
-						: '',
900
-					'urlParams' => $urlParams,
901
-				],
902
-				$this->get(IRequestId::class),
903
-				$this->get(\OCP\IConfig::class),
904
-				$this->get(CsrfTokenManager::class),
905
-				$stream
906
-			);
907
-		});
908
-		$this->registerAlias(\OCP\IRequest::class, Request::class);
909
-
910
-		$this->registerService(IRequestId::class, function (ContainerInterface $c): IRequestId {
911
-			return new RequestId(
912
-				$_SERVER['UNIQUE_ID'] ?? '',
913
-				$this->get(ISecureRandom::class)
914
-			);
915
-		});
916
-
917
-		$this->registerService(IMailer::class, function (Server $c) {
918
-			return new Mailer(
919
-				$c->get(\OCP\IConfig::class),
920
-				$c->get(LoggerInterface::class),
921
-				$c->get(Defaults::class),
922
-				$c->get(IURLGenerator::class),
923
-				$c->getL10N('lib'),
924
-				$c->get(IEventDispatcher::class),
925
-				$c->get(IFactory::class)
926
-			);
927
-		});
928
-
929
-		/** @since 30.0.0 */
930
-		$this->registerAlias(\OCP\Mail\Provider\IManager::class, \OC\Mail\Provider\Manager::class);
931
-
932
-		$this->registerService(ILDAPProviderFactory::class, function (ContainerInterface $c) {
933
-			$config = $c->get(\OCP\IConfig::class);
934
-			$factoryClass = $config->getSystemValue('ldapProviderFactory', null);
935
-			if (is_null($factoryClass) || !class_exists($factoryClass)) {
936
-				return new NullLDAPProviderFactory($this);
937
-			}
938
-			/** @var \OCP\LDAP\ILDAPProviderFactory $factory */
939
-			return new $factoryClass($this);
940
-		});
941
-		$this->registerService(ILDAPProvider::class, function (ContainerInterface $c) {
942
-			$factory = $c->get(ILDAPProviderFactory::class);
943
-			return $factory->getLDAPProvider();
944
-		});
945
-		$this->registerService(ILockingProvider::class, function (ContainerInterface $c) {
946
-			$ini = $c->get(IniGetWrapper::class);
947
-			$config = $c->get(\OCP\IConfig::class);
948
-			$ttl = $config->getSystemValueInt('filelocking.ttl', max(3600, $ini->getNumeric('max_execution_time')));
949
-			if ($config->getSystemValueBool('filelocking.enabled', true) or (defined('PHPUNIT_RUN') && PHPUNIT_RUN)) {
950
-				/** @var \OC\Memcache\Factory $memcacheFactory */
951
-				$memcacheFactory = $c->get(ICacheFactory::class);
952
-				$memcache = $memcacheFactory->createLocking('lock');
953
-				if (!($memcache instanceof \OC\Memcache\NullCache)) {
954
-					$timeFactory = $c->get(ITimeFactory::class);
955
-					return new MemcacheLockingProvider($memcache, $timeFactory, $ttl);
956
-				}
957
-				return new DBLockingProvider(
958
-					$c->get(IDBConnection::class),
959
-					new TimeFactory(),
960
-					$ttl,
961
-					!\OC::$CLI
962
-				);
963
-			}
964
-			return new NoopLockingProvider();
965
-		});
966
-
967
-		$this->registerService(ILockManager::class, function (Server $c): LockManager {
968
-			return new LockManager();
969
-		});
970
-
971
-		$this->registerAlias(ILockdownManager::class, 'LockdownManager');
972
-		$this->registerService(SetupManager::class, function ($c) {
973
-			// create the setupmanager through the mount manager to resolve the cyclic dependency
974
-			return $c->get(\OC\Files\Mount\Manager::class)->getSetupManager();
975
-		});
976
-		$this->registerAlias(IMountManager::class, \OC\Files\Mount\Manager::class);
977
-
978
-		$this->registerService(IMimeTypeDetector::class, function (ContainerInterface $c) {
979
-			return new \OC\Files\Type\Detection(
980
-				$c->get(IURLGenerator::class),
981
-				$c->get(LoggerInterface::class),
982
-				\OC::$configDir,
983
-				\OC::$SERVERROOT . '/resources/config/'
984
-			);
985
-		});
986
-
987
-		$this->registerAlias(IMimeTypeLoader::class, Loader::class);
988
-		$this->registerService(BundleFetcher::class, function () {
989
-			return new BundleFetcher($this->getL10N('lib'));
990
-		});
991
-		$this->registerAlias(\OCP\Notification\IManager::class, Manager::class);
992
-
993
-		$this->registerService(CapabilitiesManager::class, function (ContainerInterface $c) {
994
-			$manager = new CapabilitiesManager($c->get(LoggerInterface::class));
995
-			$manager->registerCapability(function () use ($c) {
996
-				return new \OC\OCS\CoreCapabilities($c->get(\OCP\IConfig::class));
997
-			});
998
-			$manager->registerCapability(function () use ($c) {
999
-				return $c->get(\OC\Security\Bruteforce\Capabilities::class);
1000
-			});
1001
-			return $manager;
1002
-		});
1003
-
1004
-		$this->registerService(ICommentsManager::class, function (Server $c) {
1005
-			$config = $c->get(\OCP\IConfig::class);
1006
-			$factoryClass = $config->getSystemValue('comments.managerFactory', CommentsManagerFactory::class);
1007
-			/** @var \OCP\Comments\ICommentsManagerFactory $factory */
1008
-			$factory = new $factoryClass($this);
1009
-			$manager = $factory->getManager();
1010
-
1011
-			$manager->registerDisplayNameResolver('user', function ($id) use ($c) {
1012
-				$manager = $c->get(IUserManager::class);
1013
-				$userDisplayName = $manager->getDisplayName($id);
1014
-				if ($userDisplayName === null) {
1015
-					$l = $c->get(IFactory::class)->get('core');
1016
-					return $l->t('Unknown account');
1017
-				}
1018
-				return $userDisplayName;
1019
-			});
1020
-
1021
-			return $manager;
1022
-		});
1023
-
1024
-		$this->registerAlias(\OC_Defaults::class, 'ThemingDefaults');
1025
-		$this->registerService('ThemingDefaults', function (Server $c) {
1026
-			try {
1027
-				$classExists = class_exists('OCA\Theming\ThemingDefaults');
1028
-			} catch (\OCP\AutoloadNotAllowedException $e) {
1029
-				// App disabled or in maintenance mode
1030
-				$classExists = false;
1031
-			}
1032
-
1033
-			if ($classExists && $c->get(\OCP\IConfig::class)->getSystemValueBool('installed', false) && $c->get(IAppManager::class)->isEnabledForAnyone('theming') && $c->get(TrustedDomainHelper::class)->isTrustedDomain($c->getRequest()->getInsecureServerHost())) {
1034
-				$backgroundService = new BackgroundService(
1035
-					$c->get(IRootFolder::class),
1036
-					$c->getAppDataDir('theming'),
1037
-					$c->get(IAppConfig::class),
1038
-					$c->get(\OCP\IConfig::class),
1039
-					$c->get(ISession::class)->get('user_id'),
1040
-				);
1041
-				$imageManager = new ImageManager(
1042
-					$c->get(\OCP\IConfig::class),
1043
-					$c->getAppDataDir('theming'),
1044
-					$c->get(IURLGenerator::class),
1045
-					$c->get(ICacheFactory::class),
1046
-					$c->get(LoggerInterface::class),
1047
-					$c->get(ITempManager::class),
1048
-					$backgroundService,
1049
-				);
1050
-				return new ThemingDefaults(
1051
-					$c->get(\OCP\IConfig::class),
1052
-					$c->get(\OCP\IAppConfig::class),
1053
-					$c->getL10N('theming'),
1054
-					$c->get(IUserSession::class),
1055
-					$c->get(IURLGenerator::class),
1056
-					$c->get(ICacheFactory::class),
1057
-					new Util($c->get(ServerVersion::class), $c->get(\OCP\IConfig::class), $this->get(IAppManager::class), $c->getAppDataDir('theming'), $imageManager),
1058
-					$imageManager,
1059
-					$c->get(IAppManager::class),
1060
-					$c->get(INavigationManager::class),
1061
-					$backgroundService,
1062
-				);
1063
-			}
1064
-			return new \OC_Defaults();
1065
-		});
1066
-		$this->registerService(JSCombiner::class, function (Server $c) {
1067
-			return new JSCombiner(
1068
-				$c->getAppDataDir('js'),
1069
-				$c->get(IURLGenerator::class),
1070
-				$this->get(ICacheFactory::class),
1071
-				$c->get(SystemConfig::class),
1072
-				$c->get(LoggerInterface::class)
1073
-			);
1074
-		});
1075
-		$this->registerAlias(\OCP\EventDispatcher\IEventDispatcher::class, \OC\EventDispatcher\EventDispatcher::class);
1076
-
1077
-		$this->registerService('CryptoWrapper', function (ContainerInterface $c) {
1078
-			// FIXME: Instantiated here due to cyclic dependency
1079
-			$request = new Request(
1080
-				[
1081
-					'get' => $_GET,
1082
-					'post' => $_POST,
1083
-					'files' => $_FILES,
1084
-					'server' => $_SERVER,
1085
-					'env' => $_ENV,
1086
-					'cookies' => $_COOKIE,
1087
-					'method' => (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD']))
1088
-						? $_SERVER['REQUEST_METHOD']
1089
-						: null,
1090
-				],
1091
-				$c->get(IRequestId::class),
1092
-				$c->get(\OCP\IConfig::class)
1093
-			);
1094
-
1095
-			return new CryptoWrapper(
1096
-				$c->get(ICrypto::class),
1097
-				$c->get(ISecureRandom::class),
1098
-				$request
1099
-			);
1100
-		});
1101
-		$this->registerService(SessionStorage::class, function (ContainerInterface $c) {
1102
-			return new SessionStorage($c->get(ISession::class));
1103
-		});
1104
-		$this->registerAlias(\OCP\Security\IContentSecurityPolicyManager::class, ContentSecurityPolicyManager::class);
1105
-
1106
-		$this->registerService(IProviderFactory::class, function (ContainerInterface $c) {
1107
-			$config = $c->get(\OCP\IConfig::class);
1108
-			$factoryClass = $config->getSystemValue('sharing.managerFactory', ProviderFactory::class);
1109
-			/** @var \OCP\Share\IProviderFactory $factory */
1110
-			return $c->get($factoryClass);
1111
-		});
1112
-
1113
-		$this->registerAlias(\OCP\Share\IManager::class, \OC\Share20\Manager::class);
1114
-
1115
-		$this->registerService(\OCP\Collaboration\Collaborators\ISearch::class, function (Server $c) {
1116
-			$instance = new Collaboration\Collaborators\Search($c);
1117
-
1118
-			// register default plugins
1119
-			$instance->registerPlugin(['shareType' => 'SHARE_TYPE_USER', 'class' => UserPlugin::class]);
1120
-			$instance->registerPlugin(['shareType' => 'SHARE_TYPE_GROUP', 'class' => GroupPlugin::class]);
1121
-			$instance->registerPlugin(['shareType' => 'SHARE_TYPE_EMAIL', 'class' => MailPlugin::class]);
1122
-			$instance->registerPlugin(['shareType' => 'SHARE_TYPE_REMOTE', 'class' => RemotePlugin::class]);
1123
-			$instance->registerPlugin(['shareType' => 'SHARE_TYPE_REMOTE_GROUP', 'class' => RemoteGroupPlugin::class]);
1124
-
1125
-			return $instance;
1126
-		});
1127
-		$this->registerAlias(\OCP\Collaboration\Collaborators\ISearchResult::class, \OC\Collaboration\Collaborators\SearchResult::class);
1128
-
1129
-		$this->registerAlias(\OCP\Collaboration\AutoComplete\IManager::class, \OC\Collaboration\AutoComplete\Manager::class);
1130
-
1131
-		$this->registerAlias(\OCP\Collaboration\Resources\IProviderManager::class, \OC\Collaboration\Resources\ProviderManager::class);
1132
-		$this->registerAlias(\OCP\Collaboration\Resources\IManager::class, \OC\Collaboration\Resources\Manager::class);
1133
-
1134
-		$this->registerAlias(IReferenceManager::class, ReferenceManager::class);
1135
-		$this->registerAlias(ITeamManager::class, TeamManager::class);
1136
-
1137
-		$this->registerDeprecatedAlias('SettingsManager', \OC\Settings\Manager::class);
1138
-		$this->registerAlias(\OCP\Settings\IManager::class, \OC\Settings\Manager::class);
1139
-		$this->registerService(\OC\Files\AppData\Factory::class, function (ContainerInterface $c) {
1140
-			return new \OC\Files\AppData\Factory(
1141
-				$c->get(IRootFolder::class),
1142
-				$c->get(SystemConfig::class)
1143
-			);
1144
-		});
1145
-
1146
-		$this->registerService('LockdownManager', function (ContainerInterface $c) {
1147
-			return new LockdownManager(function () use ($c) {
1148
-				return $c->get(ISession::class);
1149
-			});
1150
-		});
1151
-
1152
-		$this->registerService(\OCP\OCS\IDiscoveryService::class, function (ContainerInterface $c) {
1153
-			return new DiscoveryService(
1154
-				$c->get(ICacheFactory::class),
1155
-				$c->get(IClientService::class)
1156
-			);
1157
-		});
1158
-		$this->registerAlias(IOCMDiscoveryService::class, OCMDiscoveryService::class);
1159
-
1160
-		$this->registerService(ICloudIdManager::class, function (ContainerInterface $c) {
1161
-			return new CloudIdManager(
1162
-				$c->get(\OCP\Contacts\IManager::class),
1163
-				$c->get(IURLGenerator::class),
1164
-				$c->get(IUserManager::class),
1165
-				$c->get(ICacheFactory::class),
1166
-				$c->get(IEventDispatcher::class),
1167
-			);
1168
-		});
1169
-
1170
-		$this->registerAlias(\OCP\GlobalScale\IConfig::class, \OC\GlobalScale\Config::class);
1171
-		$this->registerAlias(ICloudFederationProviderManager::class, CloudFederationProviderManager::class);
1172
-		$this->registerService(ICloudFederationFactory::class, function (Server $c) {
1173
-			return new CloudFederationFactory();
1174
-		});
252
+    /** @var string */
253
+    private $webRoot;
254
+
255
+    /**
256
+     * @param string $webRoot
257
+     * @param \OC\Config $config
258
+     */
259
+    public function __construct($webRoot, \OC\Config $config) {
260
+        parent::__construct();
261
+        $this->webRoot = $webRoot;
262
+
263
+        // To find out if we are running from CLI or not
264
+        $this->registerParameter('isCLI', \OC::$CLI);
265
+        $this->registerParameter('serverRoot', \OC::$SERVERROOT);
266
+
267
+        $this->registerService(ContainerInterface::class, function (ContainerInterface $c) {
268
+            return $c;
269
+        });
270
+        $this->registerDeprecatedAlias(\OCP\IServerContainer::class, ContainerInterface::class);
271
+
272
+        $this->registerAlias(\OCP\Calendar\IManager::class, \OC\Calendar\Manager::class);
273
+
274
+        $this->registerAlias(\OCP\Calendar\Resource\IManager::class, \OC\Calendar\Resource\Manager::class);
275
+
276
+        $this->registerAlias(\OCP\Calendar\Room\IManager::class, \OC\Calendar\Room\Manager::class);
277
+
278
+        $this->registerAlias(\OCP\Contacts\IManager::class, \OC\ContactsManager::class);
279
+
280
+        $this->registerAlias(\OCP\DirectEditing\IManager::class, \OC\DirectEditing\Manager::class);
281
+        $this->registerAlias(ITemplateManager::class, TemplateManager::class);
282
+        $this->registerAlias(\OCP\Template\ITemplateManager::class, \OC\Template\TemplateManager::class);
283
+
284
+        $this->registerAlias(IActionFactory::class, ActionFactory::class);
285
+
286
+        $this->registerService(View::class, function (Server $c) {
287
+            return new View();
288
+        }, false);
289
+
290
+        $this->registerService(IPreview::class, function (ContainerInterface $c) {
291
+            return new PreviewManager(
292
+                $c->get(\OCP\IConfig::class),
293
+                $c->get(IRootFolder::class),
294
+                new \OC\Preview\Storage\Root(
295
+                    $c->get(IRootFolder::class),
296
+                    $c->get(SystemConfig::class)
297
+                ),
298
+                $c->get(IEventDispatcher::class),
299
+                $c->get(GeneratorHelper::class),
300
+                $c->get(ISession::class)->get('user_id'),
301
+                $c->get(Coordinator::class),
302
+                $c->get(IServerContainer::class),
303
+                $c->get(IBinaryFinder::class),
304
+                $c->get(IMagickSupport::class)
305
+            );
306
+        });
307
+        $this->registerAlias(IMimeIconProvider::class, MimeIconProvider::class);
308
+
309
+        $this->registerService(\OC\Preview\Watcher::class, function (ContainerInterface $c) {
310
+            return new \OC\Preview\Watcher(
311
+                new \OC\Preview\Storage\Root(
312
+                    $c->get(IRootFolder::class),
313
+                    $c->get(SystemConfig::class)
314
+                )
315
+            );
316
+        });
317
+
318
+        $this->registerService(IProfiler::class, function (Server $c) {
319
+            return new Profiler($c->get(SystemConfig::class));
320
+        });
321
+
322
+        $this->registerService(Encryption\Manager::class, function (Server $c): Encryption\Manager {
323
+            $view = new View();
324
+            $util = new Encryption\Util(
325
+                $view,
326
+                $c->get(IUserManager::class),
327
+                $c->get(IGroupManager::class),
328
+                $c->get(\OCP\IConfig::class)
329
+            );
330
+            return new Encryption\Manager(
331
+                $c->get(\OCP\IConfig::class),
332
+                $c->get(LoggerInterface::class),
333
+                $c->getL10N('core'),
334
+                new View(),
335
+                $util,
336
+                new ArrayCache()
337
+            );
338
+        });
339
+        $this->registerAlias(\OCP\Encryption\IManager::class, Encryption\Manager::class);
340
+
341
+        $this->registerService(IFile::class, function (ContainerInterface $c) {
342
+            $util = new Encryption\Util(
343
+                new View(),
344
+                $c->get(IUserManager::class),
345
+                $c->get(IGroupManager::class),
346
+                $c->get(\OCP\IConfig::class)
347
+            );
348
+            return new Encryption\File(
349
+                $util,
350
+                $c->get(IRootFolder::class),
351
+                $c->get(\OCP\Share\IManager::class)
352
+            );
353
+        });
354
+
355
+        $this->registerService(IStorage::class, function (ContainerInterface $c) {
356
+            $view = new View();
357
+            $util = new Encryption\Util(
358
+                $view,
359
+                $c->get(IUserManager::class),
360
+                $c->get(IGroupManager::class),
361
+                $c->get(\OCP\IConfig::class)
362
+            );
363
+
364
+            return new Encryption\Keys\Storage(
365
+                $view,
366
+                $util,
367
+                $c->get(ICrypto::class),
368
+                $c->get(\OCP\IConfig::class)
369
+            );
370
+        });
371
+
372
+        $this->registerAlias(\OCP\ITagManager::class, TagManager::class);
373
+
374
+        $this->registerService('SystemTagManagerFactory', function (ContainerInterface $c) {
375
+            /** @var \OCP\IConfig $config */
376
+            $config = $c->get(\OCP\IConfig::class);
377
+            $factoryClass = $config->getSystemValue('systemtags.managerFactory', SystemTagManagerFactory::class);
378
+            return new $factoryClass($this);
379
+        });
380
+        $this->registerService(ISystemTagManager::class, function (ContainerInterface $c) {
381
+            return $c->get('SystemTagManagerFactory')->getManager();
382
+        });
383
+        /** @deprecated 19.0.0 */
384
+        $this->registerDeprecatedAlias('SystemTagManager', ISystemTagManager::class);
385
+
386
+        $this->registerService(ISystemTagObjectMapper::class, function (ContainerInterface $c) {
387
+            return $c->get('SystemTagManagerFactory')->getObjectMapper();
388
+        });
389
+        $this->registerAlias(IFileAccess::class, FileAccess::class);
390
+        $this->registerService('RootFolder', function (ContainerInterface $c) {
391
+            $manager = \OC\Files\Filesystem::getMountManager();
392
+            $view = new View();
393
+            /** @var IUserSession $userSession */
394
+            $userSession = $c->get(IUserSession::class);
395
+            $root = new Root(
396
+                $manager,
397
+                $view,
398
+                $userSession->getUser(),
399
+                $c->get(IUserMountCache::class),
400
+                $this->get(LoggerInterface::class),
401
+                $this->get(IUserManager::class),
402
+                $this->get(IEventDispatcher::class),
403
+                $this->get(ICacheFactory::class),
404
+            );
405
+
406
+            $previewConnector = new \OC\Preview\WatcherConnector(
407
+                $root,
408
+                $c->get(SystemConfig::class),
409
+                $this->get(IEventDispatcher::class)
410
+            );
411
+            $previewConnector->connectWatcher();
412
+
413
+            return $root;
414
+        });
415
+        $this->registerService(HookConnector::class, function (ContainerInterface $c) {
416
+            return new HookConnector(
417
+                $c->get(IRootFolder::class),
418
+                new View(),
419
+                $c->get(IEventDispatcher::class),
420
+                $c->get(LoggerInterface::class)
421
+            );
422
+        });
423
+
424
+        $this->registerService(IRootFolder::class, function (ContainerInterface $c) {
425
+            return new LazyRoot(function () use ($c) {
426
+                return $c->get('RootFolder');
427
+            });
428
+        });
429
+
430
+        $this->registerAlias(\OCP\IUserManager::class, \OC\User\Manager::class);
431
+
432
+        $this->registerService(DisplayNameCache::class, function (ContainerInterface $c) {
433
+            return $c->get(\OC\User\Manager::class)->getDisplayNameCache();
434
+        });
435
+
436
+        $this->registerService(\OCP\IGroupManager::class, function (ContainerInterface $c) {
437
+            $groupManager = new \OC\Group\Manager(
438
+                $this->get(IUserManager::class),
439
+                $this->get(IEventDispatcher::class),
440
+                $this->get(LoggerInterface::class),
441
+                $this->get(ICacheFactory::class),
442
+                $this->get(IRemoteAddress::class),
443
+            );
444
+            return $groupManager;
445
+        });
446
+
447
+        $this->registerService(Store::class, function (ContainerInterface $c) {
448
+            $session = $c->get(ISession::class);
449
+            if (\OC::$server->get(SystemConfig::class)->getValue('installed', false)) {
450
+                $tokenProvider = $c->get(IProvider::class);
451
+            } else {
452
+                $tokenProvider = null;
453
+            }
454
+            $logger = $c->get(LoggerInterface::class);
455
+            $crypto = $c->get(ICrypto::class);
456
+            return new Store($session, $logger, $crypto, $tokenProvider);
457
+        });
458
+        $this->registerAlias(IStore::class, Store::class);
459
+        $this->registerAlias(IProvider::class, Authentication\Token\Manager::class);
460
+        $this->registerAlias(OCPIProvider::class, Authentication\Token\Manager::class);
461
+
462
+        $this->registerService(\OC\User\Session::class, function (Server $c) {
463
+            $manager = $c->get(IUserManager::class);
464
+            $session = new \OC\Session\Memory();
465
+            $timeFactory = new TimeFactory();
466
+            // Token providers might require a working database. This code
467
+            // might however be called when Nextcloud is not yet setup.
468
+            if (\OC::$server->get(SystemConfig::class)->getValue('installed', false)) {
469
+                $provider = $c->get(IProvider::class);
470
+            } else {
471
+                $provider = null;
472
+            }
473
+
474
+            $userSession = new \OC\User\Session(
475
+                $manager,
476
+                $session,
477
+                $timeFactory,
478
+                $provider,
479
+                $c->get(\OCP\IConfig::class),
480
+                $c->get(ISecureRandom::class),
481
+                $c->get('LockdownManager'),
482
+                $c->get(LoggerInterface::class),
483
+                $c->get(IEventDispatcher::class),
484
+            );
485
+            /** @deprecated 21.0.0 use BeforeUserCreatedEvent event with the IEventDispatcher instead */
486
+            $userSession->listen('\OC\User', 'preCreateUser', function ($uid, $password) {
487
+                \OC_Hook::emit('OC_User', 'pre_createUser', ['run' => true, 'uid' => $uid, 'password' => $password]);
488
+            });
489
+            /** @deprecated 21.0.0 use UserCreatedEvent event with the IEventDispatcher instead */
490
+            $userSession->listen('\OC\User', 'postCreateUser', function ($user, $password) {
491
+                /** @var \OC\User\User $user */
492
+                \OC_Hook::emit('OC_User', 'post_createUser', ['uid' => $user->getUID(), 'password' => $password]);
493
+            });
494
+            /** @deprecated 21.0.0 use BeforeUserDeletedEvent event with the IEventDispatcher instead */
495
+            $userSession->listen('\OC\User', 'preDelete', function ($user) {
496
+                /** @var \OC\User\User $user */
497
+                \OC_Hook::emit('OC_User', 'pre_deleteUser', ['run' => true, 'uid' => $user->getUID()]);
498
+            });
499
+            /** @deprecated 21.0.0 use UserDeletedEvent event with the IEventDispatcher instead */
500
+            $userSession->listen('\OC\User', 'postDelete', function ($user) {
501
+                /** @var \OC\User\User $user */
502
+                \OC_Hook::emit('OC_User', 'post_deleteUser', ['uid' => $user->getUID()]);
503
+            });
504
+            $userSession->listen('\OC\User', 'preSetPassword', function ($user, $password, $recoveryPassword) {
505
+                /** @var \OC\User\User $user */
506
+                \OC_Hook::emit('OC_User', 'pre_setPassword', ['run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword]);
507
+            });
508
+            $userSession->listen('\OC\User', 'postSetPassword', function ($user, $password, $recoveryPassword) {
509
+                /** @var \OC\User\User $user */
510
+                \OC_Hook::emit('OC_User', 'post_setPassword', ['run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword]);
511
+            });
512
+            $userSession->listen('\OC\User', 'preLogin', function ($uid, $password) {
513
+                \OC_Hook::emit('OC_User', 'pre_login', ['run' => true, 'uid' => $uid, 'password' => $password]);
514
+
515
+                /** @var IEventDispatcher $dispatcher */
516
+                $dispatcher = $this->get(IEventDispatcher::class);
517
+                $dispatcher->dispatchTyped(new BeforeUserLoggedInEvent($uid, $password));
518
+            });
519
+            $userSession->listen('\OC\User', 'postLogin', function ($user, $loginName, $password, $isTokenLogin) {
520
+                /** @var \OC\User\User $user */
521
+                \OC_Hook::emit('OC_User', 'post_login', ['run' => true, 'uid' => $user->getUID(), 'loginName' => $loginName, 'password' => $password, 'isTokenLogin' => $isTokenLogin]);
522
+
523
+                /** @var IEventDispatcher $dispatcher */
524
+                $dispatcher = $this->get(IEventDispatcher::class);
525
+                $dispatcher->dispatchTyped(new UserLoggedInEvent($user, $loginName, $password, $isTokenLogin));
526
+            });
527
+            $userSession->listen('\OC\User', 'preRememberedLogin', function ($uid) {
528
+                /** @var IEventDispatcher $dispatcher */
529
+                $dispatcher = $this->get(IEventDispatcher::class);
530
+                $dispatcher->dispatchTyped(new BeforeUserLoggedInWithCookieEvent($uid));
531
+            });
532
+            $userSession->listen('\OC\User', 'postRememberedLogin', function ($user, $password) {
533
+                /** @var \OC\User\User $user */
534
+                \OC_Hook::emit('OC_User', 'post_login', ['run' => true, 'uid' => $user->getUID(), 'password' => $password]);
535
+
536
+                /** @var IEventDispatcher $dispatcher */
537
+                $dispatcher = $this->get(IEventDispatcher::class);
538
+                $dispatcher->dispatchTyped(new UserLoggedInWithCookieEvent($user, $password));
539
+            });
540
+            $userSession->listen('\OC\User', 'logout', function ($user) {
541
+                \OC_Hook::emit('OC_User', 'logout', []);
542
+
543
+                /** @var IEventDispatcher $dispatcher */
544
+                $dispatcher = $this->get(IEventDispatcher::class);
545
+                $dispatcher->dispatchTyped(new BeforeUserLoggedOutEvent($user));
546
+            });
547
+            $userSession->listen('\OC\User', 'postLogout', function ($user) {
548
+                /** @var IEventDispatcher $dispatcher */
549
+                $dispatcher = $this->get(IEventDispatcher::class);
550
+                $dispatcher->dispatchTyped(new UserLoggedOutEvent($user));
551
+            });
552
+            $userSession->listen('\OC\User', 'changeUser', function ($user, $feature, $value, $oldValue) {
553
+                /** @var \OC\User\User $user */
554
+                \OC_Hook::emit('OC_User', 'changeUser', ['run' => true, 'user' => $user, 'feature' => $feature, 'value' => $value, 'old_value' => $oldValue]);
555
+            });
556
+            return $userSession;
557
+        });
558
+        $this->registerAlias(\OCP\IUserSession::class, \OC\User\Session::class);
559
+
560
+        $this->registerAlias(\OCP\Authentication\TwoFactorAuth\IRegistry::class, \OC\Authentication\TwoFactorAuth\Registry::class);
561
+
562
+        $this->registerAlias(INavigationManager::class, \OC\NavigationManager::class);
563
+
564
+        $this->registerAlias(\OCP\IConfig::class, \OC\AllConfig::class);
565
+
566
+        $this->registerService(\OC\SystemConfig::class, function ($c) use ($config) {
567
+            return new \OC\SystemConfig($config);
568
+        });
569
+
570
+        $this->registerAlias(IAppConfig::class, \OC\AppConfig::class);
571
+        $this->registerAlias(IUserConfig::class, \OC\Config\UserConfig::class);
572
+        $this->registerAlias(IAppManager::class, AppManager::class);
573
+
574
+        $this->registerService(IFactory::class, function (Server $c) {
575
+            return new \OC\L10N\Factory(
576
+                $c->get(\OCP\IConfig::class),
577
+                $c->getRequest(),
578
+                $c->get(IUserSession::class),
579
+                $c->get(ICacheFactory::class),
580
+                \OC::$SERVERROOT,
581
+                $c->get(IAppManager::class),
582
+            );
583
+        });
584
+
585
+        $this->registerAlias(IURLGenerator::class, URLGenerator::class);
586
+
587
+        $this->registerService(ICache::class, function ($c) {
588
+            return new Cache\File();
589
+        });
590
+
591
+        $this->registerService(Factory::class, function (Server $c) {
592
+            $profiler = $c->get(IProfiler::class);
593
+            $arrayCacheFactory = new \OC\Memcache\Factory(fn () => '', $c->get(LoggerInterface::class),
594
+                $profiler,
595
+                ArrayCache::class,
596
+                ArrayCache::class,
597
+                ArrayCache::class
598
+            );
599
+            /** @var SystemConfig $config */
600
+            $config = $c->get(SystemConfig::class);
601
+            /** @var ServerVersion $serverVersion */
602
+            $serverVersion = $c->get(ServerVersion::class);
603
+
604
+            if ($config->getValue('installed', false) && !(defined('PHPUNIT_RUN') && PHPUNIT_RUN)) {
605
+                $logQuery = $config->getValue('log_query');
606
+                $prefixClosure = function () use ($logQuery, $serverVersion): ?string {
607
+                    if (!$logQuery) {
608
+                        try {
609
+                            $v = \OCP\Server::get(IAppConfig::class)->getAppInstalledVersions(true);
610
+                        } catch (\Doctrine\DBAL\Exception $e) {
611
+                            // Database service probably unavailable
612
+                            // Probably related to https://github.com/nextcloud/server/issues/37424
613
+                            return null;
614
+                        }
615
+                    } else {
616
+                        // If the log_query is enabled, we can not get the app versions
617
+                        // as that does a query, which will be logged and the logging
618
+                        // depends on redis and here we are back again in the same function.
619
+                        $v = [
620
+                            'log_query' => 'enabled',
621
+                        ];
622
+                    }
623
+                    $v['core'] = implode(',', $serverVersion->getVersion());
624
+                    $version = implode(',', array_keys($v)) . implode(',', $v);
625
+                    $instanceId = \OC_Util::getInstanceId();
626
+                    $path = \OC::$SERVERROOT;
627
+                    return md5($instanceId . '-' . $version . '-' . $path);
628
+                };
629
+                return new \OC\Memcache\Factory($prefixClosure,
630
+                    $c->get(LoggerInterface::class),
631
+                    $profiler,
632
+                    /** @psalm-taint-escape callable */
633
+                    $config->getValue('memcache.local', null),
634
+                    /** @psalm-taint-escape callable */
635
+                    $config->getValue('memcache.distributed', null),
636
+                    /** @psalm-taint-escape callable */
637
+                    $config->getValue('memcache.locking', null),
638
+                    /** @psalm-taint-escape callable */
639
+                    $config->getValue('redis_log_file')
640
+                );
641
+            }
642
+            return $arrayCacheFactory;
643
+        });
644
+        $this->registerAlias(ICacheFactory::class, Factory::class);
645
+
646
+        $this->registerService('RedisFactory', function (Server $c) {
647
+            $systemConfig = $c->get(SystemConfig::class);
648
+            return new RedisFactory($systemConfig, $c->get(IEventLogger::class));
649
+        });
650
+
651
+        $this->registerService(\OCP\Activity\IManager::class, function (Server $c) {
652
+            $l10n = $this->get(IFactory::class)->get('lib');
653
+            return new \OC\Activity\Manager(
654
+                $c->getRequest(),
655
+                $c->get(IUserSession::class),
656
+                $c->get(\OCP\IConfig::class),
657
+                $c->get(IValidator::class),
658
+                $c->get(IRichTextFormatter::class),
659
+                $l10n
660
+            );
661
+        });
662
+
663
+        $this->registerService(\OCP\Activity\IEventMerger::class, function (Server $c) {
664
+            return new \OC\Activity\EventMerger(
665
+                $c->getL10N('lib')
666
+            );
667
+        });
668
+        $this->registerAlias(IValidator::class, Validator::class);
669
+
670
+        $this->registerService(AvatarManager::class, function (Server $c) {
671
+            return new AvatarManager(
672
+                $c->get(IUserSession::class),
673
+                $c->get(\OC\User\Manager::class),
674
+                $c->getAppDataDir('avatar'),
675
+                $c->getL10N('lib'),
676
+                $c->get(LoggerInterface::class),
677
+                $c->get(\OCP\IConfig::class),
678
+                $c->get(IAccountManager::class),
679
+                $c->get(KnownUserService::class)
680
+            );
681
+        });
682
+
683
+        $this->registerAlias(IAvatarManager::class, AvatarManager::class);
684
+
685
+        $this->registerAlias(\OCP\Support\CrashReport\IRegistry::class, \OC\Support\CrashReport\Registry::class);
686
+        $this->registerAlias(\OCP\Support\Subscription\IRegistry::class, \OC\Support\Subscription\Registry::class);
687
+        $this->registerAlias(\OCP\Support\Subscription\IAssertion::class, \OC\Support\Subscription\Assertion::class);
688
+
689
+        /** Only used by the PsrLoggerAdapter should not be used by apps */
690
+        $this->registerService(\OC\Log::class, function (Server $c) {
691
+            $logType = $c->get(AllConfig::class)->getSystemValue('log_type', 'file');
692
+            $factory = new LogFactory($c, $this->get(SystemConfig::class));
693
+            $logger = $factory->get($logType);
694
+            $registry = $c->get(\OCP\Support\CrashReport\IRegistry::class);
695
+
696
+            return new Log($logger, $this->get(SystemConfig::class), crashReporters: $registry);
697
+        });
698
+        // PSR-3 logger
699
+        $this->registerAlias(LoggerInterface::class, PsrLoggerAdapter::class);
700
+
701
+        $this->registerService(ILogFactory::class, function (Server $c) {
702
+            return new LogFactory($c, $this->get(SystemConfig::class));
703
+        });
704
+
705
+        $this->registerAlias(IJobList::class, \OC\BackgroundJob\JobList::class);
706
+
707
+        $this->registerService(Router::class, function (Server $c) {
708
+            $cacheFactory = $c->get(ICacheFactory::class);
709
+            if ($cacheFactory->isLocalCacheAvailable()) {
710
+                $router = $c->resolve(CachingRouter::class);
711
+            } else {
712
+                $router = $c->resolve(Router::class);
713
+            }
714
+            return $router;
715
+        });
716
+        $this->registerAlias(IRouter::class, Router::class);
717
+
718
+        $this->registerService(\OC\Security\RateLimiting\Backend\IBackend::class, function ($c) {
719
+            $config = $c->get(\OCP\IConfig::class);
720
+            if (ltrim($config->getSystemValueString('memcache.distributed', ''), '\\') === \OC\Memcache\Redis::class) {
721
+                $backend = new \OC\Security\RateLimiting\Backend\MemoryCacheBackend(
722
+                    $c->get(AllConfig::class),
723
+                    $this->get(ICacheFactory::class),
724
+                    new \OC\AppFramework\Utility\TimeFactory()
725
+                );
726
+            } else {
727
+                $backend = new \OC\Security\RateLimiting\Backend\DatabaseBackend(
728
+                    $c->get(AllConfig::class),
729
+                    $c->get(IDBConnection::class),
730
+                    new \OC\AppFramework\Utility\TimeFactory()
731
+                );
732
+            }
733
+
734
+            return $backend;
735
+        });
736
+
737
+        $this->registerAlias(\OCP\Security\ISecureRandom::class, SecureRandom::class);
738
+        $this->registerAlias(\OCP\Security\IRemoteHostValidator::class, \OC\Security\RemoteHostValidator::class);
739
+        $this->registerAlias(IVerificationToken::class, VerificationToken::class);
740
+
741
+        $this->registerAlias(ICrypto::class, Crypto::class);
742
+
743
+        $this->registerAlias(IHasher::class, Hasher::class);
744
+
745
+        $this->registerAlias(ICredentialsManager::class, CredentialsManager::class);
746
+
747
+        $this->registerAlias(IDBConnection::class, ConnectionAdapter::class);
748
+        $this->registerService(Connection::class, function (Server $c) {
749
+            $systemConfig = $c->get(SystemConfig::class);
750
+            $factory = new \OC\DB\ConnectionFactory($systemConfig, $c->get(ICacheFactory::class));
751
+            $type = $systemConfig->getValue('dbtype', 'sqlite');
752
+            if (!$factory->isValidType($type)) {
753
+                throw new \OC\DatabaseException('Invalid database type');
754
+            }
755
+            $connection = $factory->getConnection($type, []);
756
+            return $connection;
757
+        });
758
+
759
+        $this->registerAlias(ICertificateManager::class, CertificateManager::class);
760
+        $this->registerAlias(IClientService::class, ClientService::class);
761
+        $this->registerService(NegativeDnsCache::class, function (ContainerInterface $c) {
762
+            return new NegativeDnsCache(
763
+                $c->get(ICacheFactory::class),
764
+            );
765
+        });
766
+        $this->registerDeprecatedAlias('HttpClientService', IClientService::class);
767
+        $this->registerService(IEventLogger::class, function (ContainerInterface $c) {
768
+            return new EventLogger($c->get(SystemConfig::class), $c->get(LoggerInterface::class), $c->get(Log::class));
769
+        });
770
+
771
+        $this->registerService(IQueryLogger::class, function (ContainerInterface $c) {
772
+            $queryLogger = new QueryLogger();
773
+            if ($c->get(SystemConfig::class)->getValue('debug', false)) {
774
+                // In debug mode, module is being activated by default
775
+                $queryLogger->activate();
776
+            }
777
+            return $queryLogger;
778
+        });
779
+
780
+        $this->registerAlias(ITempManager::class, TempManager::class);
781
+        $this->registerAlias(IDateTimeZone::class, DateTimeZone::class);
782
+
783
+        $this->registerService(IDateTimeFormatter::class, function (Server $c) {
784
+            $language = $c->get(\OCP\IConfig::class)->getUserValue($c->get(ISession::class)->get('user_id'), 'core', 'lang', null);
785
+
786
+            return new DateTimeFormatter(
787
+                $c->get(IDateTimeZone::class)->getTimeZone(),
788
+                $c->getL10N('lib', $language)
789
+            );
790
+        });
791
+
792
+        $this->registerService(IUserMountCache::class, function (ContainerInterface $c) {
793
+            $mountCache = $c->get(UserMountCache::class);
794
+            $listener = new UserMountCacheListener($mountCache);
795
+            $listener->listen($c->get(IUserManager::class));
796
+            return $mountCache;
797
+        });
798
+
799
+        $this->registerService(IMountProviderCollection::class, function (ContainerInterface $c) {
800
+            $loader = $c->get(IStorageFactory::class);
801
+            $mountCache = $c->get(IUserMountCache::class);
802
+            $eventLogger = $c->get(IEventLogger::class);
803
+            $manager = new MountProviderCollection($loader, $mountCache, $eventLogger);
804
+
805
+            // builtin providers
806
+
807
+            $config = $c->get(\OCP\IConfig::class);
808
+            $logger = $c->get(LoggerInterface::class);
809
+            $objectStoreConfig = $c->get(PrimaryObjectStoreConfig::class);
810
+            $manager->registerProvider(new CacheMountProvider($config));
811
+            $manager->registerHomeProvider(new LocalHomeMountProvider());
812
+            $manager->registerHomeProvider(new ObjectHomeMountProvider($objectStoreConfig));
813
+            $manager->registerRootProvider(new RootMountProvider($objectStoreConfig, $config));
814
+            $manager->registerRootProvider(new ObjectStorePreviewCacheMountProvider($logger, $config));
815
+
816
+            return $manager;
817
+        });
818
+
819
+        $this->registerService(IBus::class, function (ContainerInterface $c) {
820
+            $busClass = $c->get(\OCP\IConfig::class)->getSystemValueString('commandbus');
821
+            if ($busClass) {
822
+                [$app, $class] = explode('::', $busClass, 2);
823
+                if ($c->get(IAppManager::class)->isEnabledForUser($app)) {
824
+                    $c->get(IAppManager::class)->loadApp($app);
825
+                    return $c->get($class);
826
+                } else {
827
+                    throw new ServiceUnavailableException("The app providing the command bus ($app) is not enabled");
828
+                }
829
+            } else {
830
+                $jobList = $c->get(IJobList::class);
831
+                return new CronBus($jobList);
832
+            }
833
+        });
834
+        $this->registerDeprecatedAlias('AsyncCommandBus', IBus::class);
835
+        $this->registerAlias(ITrustedDomainHelper::class, TrustedDomainHelper::class);
836
+        $this->registerAlias(IThrottler::class, Throttler::class);
837
+
838
+        $this->registerService(\OC\Security\Bruteforce\Backend\IBackend::class, function ($c) {
839
+            $config = $c->get(\OCP\IConfig::class);
840
+            if (!$config->getSystemValueBool('auth.bruteforce.protection.force.database', false)
841
+                && ltrim($config->getSystemValueString('memcache.distributed', ''), '\\') === \OC\Memcache\Redis::class) {
842
+                $backend = $c->get(\OC\Security\Bruteforce\Backend\MemoryCacheBackend::class);
843
+            } else {
844
+                $backend = $c->get(\OC\Security\Bruteforce\Backend\DatabaseBackend::class);
845
+            }
846
+
847
+            return $backend;
848
+        });
849
+
850
+        $this->registerDeprecatedAlias('IntegrityCodeChecker', Checker::class);
851
+        $this->registerService(Checker::class, function (ContainerInterface $c) {
852
+            // IConfig requires a working database. This code
853
+            // might however be called when Nextcloud is not yet setup.
854
+            if (\OC::$server->get(SystemConfig::class)->getValue('installed', false)) {
855
+                $config = $c->get(\OCP\IConfig::class);
856
+                $appConfig = $c->get(\OCP\IAppConfig::class);
857
+            } else {
858
+                $config = null;
859
+                $appConfig = null;
860
+            }
861
+
862
+            return new Checker(
863
+                $c->get(ServerVersion::class),
864
+                $c->get(EnvironmentHelper::class),
865
+                new FileAccessHelper(),
866
+                new AppLocator(),
867
+                $config,
868
+                $appConfig,
869
+                $c->get(ICacheFactory::class),
870
+                $c->get(IAppManager::class),
871
+                $c->get(IMimeTypeDetector::class)
872
+            );
873
+        });
874
+        $this->registerService(Request::class, function (ContainerInterface $c) {
875
+            if (isset($this['urlParams'])) {
876
+                $urlParams = $this['urlParams'];
877
+            } else {
878
+                $urlParams = [];
879
+            }
880
+
881
+            if (defined('PHPUNIT_RUN') && PHPUNIT_RUN
882
+                && in_array('fakeinput', stream_get_wrappers())
883
+            ) {
884
+                $stream = 'fakeinput://data';
885
+            } else {
886
+                $stream = 'php://input';
887
+            }
888
+
889
+            return new Request(
890
+                [
891
+                    'get' => $_GET,
892
+                    'post' => $_POST,
893
+                    'files' => $_FILES,
894
+                    'server' => $_SERVER,
895
+                    'env' => $_ENV,
896
+                    'cookies' => $_COOKIE,
897
+                    'method' => (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD']))
898
+                        ? $_SERVER['REQUEST_METHOD']
899
+                        : '',
900
+                    'urlParams' => $urlParams,
901
+                ],
902
+                $this->get(IRequestId::class),
903
+                $this->get(\OCP\IConfig::class),
904
+                $this->get(CsrfTokenManager::class),
905
+                $stream
906
+            );
907
+        });
908
+        $this->registerAlias(\OCP\IRequest::class, Request::class);
909
+
910
+        $this->registerService(IRequestId::class, function (ContainerInterface $c): IRequestId {
911
+            return new RequestId(
912
+                $_SERVER['UNIQUE_ID'] ?? '',
913
+                $this->get(ISecureRandom::class)
914
+            );
915
+        });
916
+
917
+        $this->registerService(IMailer::class, function (Server $c) {
918
+            return new Mailer(
919
+                $c->get(\OCP\IConfig::class),
920
+                $c->get(LoggerInterface::class),
921
+                $c->get(Defaults::class),
922
+                $c->get(IURLGenerator::class),
923
+                $c->getL10N('lib'),
924
+                $c->get(IEventDispatcher::class),
925
+                $c->get(IFactory::class)
926
+            );
927
+        });
928
+
929
+        /** @since 30.0.0 */
930
+        $this->registerAlias(\OCP\Mail\Provider\IManager::class, \OC\Mail\Provider\Manager::class);
931
+
932
+        $this->registerService(ILDAPProviderFactory::class, function (ContainerInterface $c) {
933
+            $config = $c->get(\OCP\IConfig::class);
934
+            $factoryClass = $config->getSystemValue('ldapProviderFactory', null);
935
+            if (is_null($factoryClass) || !class_exists($factoryClass)) {
936
+                return new NullLDAPProviderFactory($this);
937
+            }
938
+            /** @var \OCP\LDAP\ILDAPProviderFactory $factory */
939
+            return new $factoryClass($this);
940
+        });
941
+        $this->registerService(ILDAPProvider::class, function (ContainerInterface $c) {
942
+            $factory = $c->get(ILDAPProviderFactory::class);
943
+            return $factory->getLDAPProvider();
944
+        });
945
+        $this->registerService(ILockingProvider::class, function (ContainerInterface $c) {
946
+            $ini = $c->get(IniGetWrapper::class);
947
+            $config = $c->get(\OCP\IConfig::class);
948
+            $ttl = $config->getSystemValueInt('filelocking.ttl', max(3600, $ini->getNumeric('max_execution_time')));
949
+            if ($config->getSystemValueBool('filelocking.enabled', true) or (defined('PHPUNIT_RUN') && PHPUNIT_RUN)) {
950
+                /** @var \OC\Memcache\Factory $memcacheFactory */
951
+                $memcacheFactory = $c->get(ICacheFactory::class);
952
+                $memcache = $memcacheFactory->createLocking('lock');
953
+                if (!($memcache instanceof \OC\Memcache\NullCache)) {
954
+                    $timeFactory = $c->get(ITimeFactory::class);
955
+                    return new MemcacheLockingProvider($memcache, $timeFactory, $ttl);
956
+                }
957
+                return new DBLockingProvider(
958
+                    $c->get(IDBConnection::class),
959
+                    new TimeFactory(),
960
+                    $ttl,
961
+                    !\OC::$CLI
962
+                );
963
+            }
964
+            return new NoopLockingProvider();
965
+        });
966
+
967
+        $this->registerService(ILockManager::class, function (Server $c): LockManager {
968
+            return new LockManager();
969
+        });
970
+
971
+        $this->registerAlias(ILockdownManager::class, 'LockdownManager');
972
+        $this->registerService(SetupManager::class, function ($c) {
973
+            // create the setupmanager through the mount manager to resolve the cyclic dependency
974
+            return $c->get(\OC\Files\Mount\Manager::class)->getSetupManager();
975
+        });
976
+        $this->registerAlias(IMountManager::class, \OC\Files\Mount\Manager::class);
977
+
978
+        $this->registerService(IMimeTypeDetector::class, function (ContainerInterface $c) {
979
+            return new \OC\Files\Type\Detection(
980
+                $c->get(IURLGenerator::class),
981
+                $c->get(LoggerInterface::class),
982
+                \OC::$configDir,
983
+                \OC::$SERVERROOT . '/resources/config/'
984
+            );
985
+        });
986
+
987
+        $this->registerAlias(IMimeTypeLoader::class, Loader::class);
988
+        $this->registerService(BundleFetcher::class, function () {
989
+            return new BundleFetcher($this->getL10N('lib'));
990
+        });
991
+        $this->registerAlias(\OCP\Notification\IManager::class, Manager::class);
992
+
993
+        $this->registerService(CapabilitiesManager::class, function (ContainerInterface $c) {
994
+            $manager = new CapabilitiesManager($c->get(LoggerInterface::class));
995
+            $manager->registerCapability(function () use ($c) {
996
+                return new \OC\OCS\CoreCapabilities($c->get(\OCP\IConfig::class));
997
+            });
998
+            $manager->registerCapability(function () use ($c) {
999
+                return $c->get(\OC\Security\Bruteforce\Capabilities::class);
1000
+            });
1001
+            return $manager;
1002
+        });
1003
+
1004
+        $this->registerService(ICommentsManager::class, function (Server $c) {
1005
+            $config = $c->get(\OCP\IConfig::class);
1006
+            $factoryClass = $config->getSystemValue('comments.managerFactory', CommentsManagerFactory::class);
1007
+            /** @var \OCP\Comments\ICommentsManagerFactory $factory */
1008
+            $factory = new $factoryClass($this);
1009
+            $manager = $factory->getManager();
1010
+
1011
+            $manager->registerDisplayNameResolver('user', function ($id) use ($c) {
1012
+                $manager = $c->get(IUserManager::class);
1013
+                $userDisplayName = $manager->getDisplayName($id);
1014
+                if ($userDisplayName === null) {
1015
+                    $l = $c->get(IFactory::class)->get('core');
1016
+                    return $l->t('Unknown account');
1017
+                }
1018
+                return $userDisplayName;
1019
+            });
1020
+
1021
+            return $manager;
1022
+        });
1023
+
1024
+        $this->registerAlias(\OC_Defaults::class, 'ThemingDefaults');
1025
+        $this->registerService('ThemingDefaults', function (Server $c) {
1026
+            try {
1027
+                $classExists = class_exists('OCA\Theming\ThemingDefaults');
1028
+            } catch (\OCP\AutoloadNotAllowedException $e) {
1029
+                // App disabled or in maintenance mode
1030
+                $classExists = false;
1031
+            }
1032
+
1033
+            if ($classExists && $c->get(\OCP\IConfig::class)->getSystemValueBool('installed', false) && $c->get(IAppManager::class)->isEnabledForAnyone('theming') && $c->get(TrustedDomainHelper::class)->isTrustedDomain($c->getRequest()->getInsecureServerHost())) {
1034
+                $backgroundService = new BackgroundService(
1035
+                    $c->get(IRootFolder::class),
1036
+                    $c->getAppDataDir('theming'),
1037
+                    $c->get(IAppConfig::class),
1038
+                    $c->get(\OCP\IConfig::class),
1039
+                    $c->get(ISession::class)->get('user_id'),
1040
+                );
1041
+                $imageManager = new ImageManager(
1042
+                    $c->get(\OCP\IConfig::class),
1043
+                    $c->getAppDataDir('theming'),
1044
+                    $c->get(IURLGenerator::class),
1045
+                    $c->get(ICacheFactory::class),
1046
+                    $c->get(LoggerInterface::class),
1047
+                    $c->get(ITempManager::class),
1048
+                    $backgroundService,
1049
+                );
1050
+                return new ThemingDefaults(
1051
+                    $c->get(\OCP\IConfig::class),
1052
+                    $c->get(\OCP\IAppConfig::class),
1053
+                    $c->getL10N('theming'),
1054
+                    $c->get(IUserSession::class),
1055
+                    $c->get(IURLGenerator::class),
1056
+                    $c->get(ICacheFactory::class),
1057
+                    new Util($c->get(ServerVersion::class), $c->get(\OCP\IConfig::class), $this->get(IAppManager::class), $c->getAppDataDir('theming'), $imageManager),
1058
+                    $imageManager,
1059
+                    $c->get(IAppManager::class),
1060
+                    $c->get(INavigationManager::class),
1061
+                    $backgroundService,
1062
+                );
1063
+            }
1064
+            return new \OC_Defaults();
1065
+        });
1066
+        $this->registerService(JSCombiner::class, function (Server $c) {
1067
+            return new JSCombiner(
1068
+                $c->getAppDataDir('js'),
1069
+                $c->get(IURLGenerator::class),
1070
+                $this->get(ICacheFactory::class),
1071
+                $c->get(SystemConfig::class),
1072
+                $c->get(LoggerInterface::class)
1073
+            );
1074
+        });
1075
+        $this->registerAlias(\OCP\EventDispatcher\IEventDispatcher::class, \OC\EventDispatcher\EventDispatcher::class);
1076
+
1077
+        $this->registerService('CryptoWrapper', function (ContainerInterface $c) {
1078
+            // FIXME: Instantiated here due to cyclic dependency
1079
+            $request = new Request(
1080
+                [
1081
+                    'get' => $_GET,
1082
+                    'post' => $_POST,
1083
+                    'files' => $_FILES,
1084
+                    'server' => $_SERVER,
1085
+                    'env' => $_ENV,
1086
+                    'cookies' => $_COOKIE,
1087
+                    'method' => (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD']))
1088
+                        ? $_SERVER['REQUEST_METHOD']
1089
+                        : null,
1090
+                ],
1091
+                $c->get(IRequestId::class),
1092
+                $c->get(\OCP\IConfig::class)
1093
+            );
1094
+
1095
+            return new CryptoWrapper(
1096
+                $c->get(ICrypto::class),
1097
+                $c->get(ISecureRandom::class),
1098
+                $request
1099
+            );
1100
+        });
1101
+        $this->registerService(SessionStorage::class, function (ContainerInterface $c) {
1102
+            return new SessionStorage($c->get(ISession::class));
1103
+        });
1104
+        $this->registerAlias(\OCP\Security\IContentSecurityPolicyManager::class, ContentSecurityPolicyManager::class);
1105
+
1106
+        $this->registerService(IProviderFactory::class, function (ContainerInterface $c) {
1107
+            $config = $c->get(\OCP\IConfig::class);
1108
+            $factoryClass = $config->getSystemValue('sharing.managerFactory', ProviderFactory::class);
1109
+            /** @var \OCP\Share\IProviderFactory $factory */
1110
+            return $c->get($factoryClass);
1111
+        });
1112
+
1113
+        $this->registerAlias(\OCP\Share\IManager::class, \OC\Share20\Manager::class);
1114
+
1115
+        $this->registerService(\OCP\Collaboration\Collaborators\ISearch::class, function (Server $c) {
1116
+            $instance = new Collaboration\Collaborators\Search($c);
1117
+
1118
+            // register default plugins
1119
+            $instance->registerPlugin(['shareType' => 'SHARE_TYPE_USER', 'class' => UserPlugin::class]);
1120
+            $instance->registerPlugin(['shareType' => 'SHARE_TYPE_GROUP', 'class' => GroupPlugin::class]);
1121
+            $instance->registerPlugin(['shareType' => 'SHARE_TYPE_EMAIL', 'class' => MailPlugin::class]);
1122
+            $instance->registerPlugin(['shareType' => 'SHARE_TYPE_REMOTE', 'class' => RemotePlugin::class]);
1123
+            $instance->registerPlugin(['shareType' => 'SHARE_TYPE_REMOTE_GROUP', 'class' => RemoteGroupPlugin::class]);
1124
+
1125
+            return $instance;
1126
+        });
1127
+        $this->registerAlias(\OCP\Collaboration\Collaborators\ISearchResult::class, \OC\Collaboration\Collaborators\SearchResult::class);
1128
+
1129
+        $this->registerAlias(\OCP\Collaboration\AutoComplete\IManager::class, \OC\Collaboration\AutoComplete\Manager::class);
1130
+
1131
+        $this->registerAlias(\OCP\Collaboration\Resources\IProviderManager::class, \OC\Collaboration\Resources\ProviderManager::class);
1132
+        $this->registerAlias(\OCP\Collaboration\Resources\IManager::class, \OC\Collaboration\Resources\Manager::class);
1133
+
1134
+        $this->registerAlias(IReferenceManager::class, ReferenceManager::class);
1135
+        $this->registerAlias(ITeamManager::class, TeamManager::class);
1136
+
1137
+        $this->registerDeprecatedAlias('SettingsManager', \OC\Settings\Manager::class);
1138
+        $this->registerAlias(\OCP\Settings\IManager::class, \OC\Settings\Manager::class);
1139
+        $this->registerService(\OC\Files\AppData\Factory::class, function (ContainerInterface $c) {
1140
+            return new \OC\Files\AppData\Factory(
1141
+                $c->get(IRootFolder::class),
1142
+                $c->get(SystemConfig::class)
1143
+            );
1144
+        });
1145
+
1146
+        $this->registerService('LockdownManager', function (ContainerInterface $c) {
1147
+            return new LockdownManager(function () use ($c) {
1148
+                return $c->get(ISession::class);
1149
+            });
1150
+        });
1151
+
1152
+        $this->registerService(\OCP\OCS\IDiscoveryService::class, function (ContainerInterface $c) {
1153
+            return new DiscoveryService(
1154
+                $c->get(ICacheFactory::class),
1155
+                $c->get(IClientService::class)
1156
+            );
1157
+        });
1158
+        $this->registerAlias(IOCMDiscoveryService::class, OCMDiscoveryService::class);
1159
+
1160
+        $this->registerService(ICloudIdManager::class, function (ContainerInterface $c) {
1161
+            return new CloudIdManager(
1162
+                $c->get(\OCP\Contacts\IManager::class),
1163
+                $c->get(IURLGenerator::class),
1164
+                $c->get(IUserManager::class),
1165
+                $c->get(ICacheFactory::class),
1166
+                $c->get(IEventDispatcher::class),
1167
+            );
1168
+        });
1169
+
1170
+        $this->registerAlias(\OCP\GlobalScale\IConfig::class, \OC\GlobalScale\Config::class);
1171
+        $this->registerAlias(ICloudFederationProviderManager::class, CloudFederationProviderManager::class);
1172
+        $this->registerService(ICloudFederationFactory::class, function (Server $c) {
1173
+            return new CloudFederationFactory();
1174
+        });
1175 1175
 
1176
-		$this->registerAlias(\OCP\AppFramework\Utility\IControllerMethodReflector::class, \OC\AppFramework\Utility\ControllerMethodReflector::class);
1176
+        $this->registerAlias(\OCP\AppFramework\Utility\IControllerMethodReflector::class, \OC\AppFramework\Utility\ControllerMethodReflector::class);
1177 1177
 
1178
-		$this->registerAlias(\OCP\AppFramework\Utility\ITimeFactory::class, \OC\AppFramework\Utility\TimeFactory::class);
1179
-		$this->registerAlias(\Psr\Clock\ClockInterface::class, \OCP\AppFramework\Utility\ITimeFactory::class);
1178
+        $this->registerAlias(\OCP\AppFramework\Utility\ITimeFactory::class, \OC\AppFramework\Utility\TimeFactory::class);
1179
+        $this->registerAlias(\Psr\Clock\ClockInterface::class, \OCP\AppFramework\Utility\ITimeFactory::class);
1180 1180
 
1181
-		$this->registerService(Defaults::class, function (Server $c) {
1182
-			return new Defaults(
1183
-				$c->get('ThemingDefaults')
1184
-			);
1185
-		});
1181
+        $this->registerService(Defaults::class, function (Server $c) {
1182
+            return new Defaults(
1183
+                $c->get('ThemingDefaults')
1184
+            );
1185
+        });
1186 1186
 
1187
-		$this->registerService(\OCP\ISession::class, function (ContainerInterface $c) {
1188
-			return $c->get(\OCP\IUserSession::class)->getSession();
1189
-		}, false);
1187
+        $this->registerService(\OCP\ISession::class, function (ContainerInterface $c) {
1188
+            return $c->get(\OCP\IUserSession::class)->getSession();
1189
+        }, false);
1190 1190
 
1191
-		$this->registerService(IShareHelper::class, function (ContainerInterface $c) {
1192
-			return new ShareHelper(
1193
-				$c->get(\OCP\Share\IManager::class)
1194
-			);
1195
-		});
1191
+        $this->registerService(IShareHelper::class, function (ContainerInterface $c) {
1192
+            return new ShareHelper(
1193
+                $c->get(\OCP\Share\IManager::class)
1194
+            );
1195
+        });
1196 1196
 
1197
-		$this->registerService(Installer::class, function (ContainerInterface $c) {
1198
-			return new Installer(
1199
-				$c->get(AppFetcher::class),
1200
-				$c->get(IClientService::class),
1201
-				$c->get(ITempManager::class),
1202
-				$c->get(LoggerInterface::class),
1203
-				$c->get(\OCP\IConfig::class),
1204
-				\OC::$CLI
1205
-			);
1206
-		});
1197
+        $this->registerService(Installer::class, function (ContainerInterface $c) {
1198
+            return new Installer(
1199
+                $c->get(AppFetcher::class),
1200
+                $c->get(IClientService::class),
1201
+                $c->get(ITempManager::class),
1202
+                $c->get(LoggerInterface::class),
1203
+                $c->get(\OCP\IConfig::class),
1204
+                \OC::$CLI
1205
+            );
1206
+        });
1207 1207
 
1208
-		$this->registerService(IApiFactory::class, function (ContainerInterface $c) {
1209
-			return new ApiFactory($c->get(IClientService::class));
1210
-		});
1208
+        $this->registerService(IApiFactory::class, function (ContainerInterface $c) {
1209
+            return new ApiFactory($c->get(IClientService::class));
1210
+        });
1211 1211
 
1212
-		$this->registerService(IInstanceFactory::class, function (ContainerInterface $c) {
1213
-			$memcacheFactory = $c->get(ICacheFactory::class);
1214
-			return new InstanceFactory($memcacheFactory->createLocal('remoteinstance.'), $c->get(IClientService::class));
1215
-		});
1212
+        $this->registerService(IInstanceFactory::class, function (ContainerInterface $c) {
1213
+            $memcacheFactory = $c->get(ICacheFactory::class);
1214
+            return new InstanceFactory($memcacheFactory->createLocal('remoteinstance.'), $c->get(IClientService::class));
1215
+        });
1216 1216
 
1217
-		$this->registerAlias(IContactsStore::class, ContactsStore::class);
1218
-		$this->registerAlias(IAccountManager::class, AccountManager::class);
1217
+        $this->registerAlias(IContactsStore::class, ContactsStore::class);
1218
+        $this->registerAlias(IAccountManager::class, AccountManager::class);
1219 1219
 
1220
-		$this->registerAlias(IStorageFactory::class, StorageFactory::class);
1220
+        $this->registerAlias(IStorageFactory::class, StorageFactory::class);
1221 1221
 
1222
-		$this->registerAlias(\OCP\Dashboard\IManager::class, \OC\Dashboard\Manager::class);
1222
+        $this->registerAlias(\OCP\Dashboard\IManager::class, \OC\Dashboard\Manager::class);
1223 1223
 
1224
-		$this->registerAlias(IFullTextSearchManager::class, FullTextSearchManager::class);
1225
-		$this->registerAlias(IFilesMetadataManager::class, FilesMetadataManager::class);
1224
+        $this->registerAlias(IFullTextSearchManager::class, FullTextSearchManager::class);
1225
+        $this->registerAlias(IFilesMetadataManager::class, FilesMetadataManager::class);
1226 1226
 
1227
-		$this->registerAlias(ISubAdmin::class, SubAdmin::class);
1227
+        $this->registerAlias(ISubAdmin::class, SubAdmin::class);
1228 1228
 
1229
-		$this->registerAlias(IInitialStateService::class, InitialStateService::class);
1229
+        $this->registerAlias(IInitialStateService::class, InitialStateService::class);
1230 1230
 
1231
-		$this->registerAlias(\OCP\IEmojiHelper::class, \OC\EmojiHelper::class);
1231
+        $this->registerAlias(\OCP\IEmojiHelper::class, \OC\EmojiHelper::class);
1232 1232
 
1233
-		$this->registerAlias(\OCP\UserStatus\IManager::class, \OC\UserStatus\Manager::class);
1233
+        $this->registerAlias(\OCP\UserStatus\IManager::class, \OC\UserStatus\Manager::class);
1234 1234
 
1235
-		$this->registerAlias(IBroker::class, Broker::class);
1235
+        $this->registerAlias(IBroker::class, Broker::class);
1236 1236
 
1237
-		$this->registerAlias(\OCP\Files\AppData\IAppDataFactory::class, \OC\Files\AppData\Factory::class);
1237
+        $this->registerAlias(\OCP\Files\AppData\IAppDataFactory::class, \OC\Files\AppData\Factory::class);
1238 1238
 
1239
-		$this->registerAlias(\OCP\Files\IFilenameValidator::class, \OC\Files\FilenameValidator::class);
1239
+        $this->registerAlias(\OCP\Files\IFilenameValidator::class, \OC\Files\FilenameValidator::class);
1240 1240
 
1241
-		$this->registerAlias(IBinaryFinder::class, BinaryFinder::class);
1241
+        $this->registerAlias(IBinaryFinder::class, BinaryFinder::class);
1242 1242
 
1243
-		$this->registerAlias(\OCP\Share\IPublicShareTemplateFactory::class, \OC\Share20\PublicShareTemplateFactory::class);
1243
+        $this->registerAlias(\OCP\Share\IPublicShareTemplateFactory::class, \OC\Share20\PublicShareTemplateFactory::class);
1244 1244
 
1245
-		$this->registerAlias(ITranslationManager::class, TranslationManager::class);
1245
+        $this->registerAlias(ITranslationManager::class, TranslationManager::class);
1246 1246
 
1247
-		$this->registerAlias(IConversionManager::class, ConversionManager::class);
1247
+        $this->registerAlias(IConversionManager::class, ConversionManager::class);
1248 1248
 
1249
-		$this->registerAlias(ISpeechToTextManager::class, SpeechToTextManager::class);
1249
+        $this->registerAlias(ISpeechToTextManager::class, SpeechToTextManager::class);
1250 1250
 
1251
-		$this->registerAlias(IEventSourceFactory::class, EventSourceFactory::class);
1251
+        $this->registerAlias(IEventSourceFactory::class, EventSourceFactory::class);
1252 1252
 
1253
-		$this->registerAlias(\OCP\TextProcessing\IManager::class, \OC\TextProcessing\Manager::class);
1253
+        $this->registerAlias(\OCP\TextProcessing\IManager::class, \OC\TextProcessing\Manager::class);
1254 1254
 
1255
-		$this->registerAlias(\OCP\TextToImage\IManager::class, \OC\TextToImage\Manager::class);
1255
+        $this->registerAlias(\OCP\TextToImage\IManager::class, \OC\TextToImage\Manager::class);
1256 1256
 
1257
-		$this->registerAlias(ILimiter::class, Limiter::class);
1257
+        $this->registerAlias(ILimiter::class, Limiter::class);
1258 1258
 
1259
-		$this->registerAlias(IPhoneNumberUtil::class, PhoneNumberUtil::class);
1259
+        $this->registerAlias(IPhoneNumberUtil::class, PhoneNumberUtil::class);
1260 1260
 
1261
-		$this->registerAlias(ICapabilityAwareOCMProvider::class, OCMProvider::class);
1262
-		$this->registerDeprecatedAlias(IOCMProvider::class, OCMProvider::class);
1261
+        $this->registerAlias(ICapabilityAwareOCMProvider::class, OCMProvider::class);
1262
+        $this->registerDeprecatedAlias(IOCMProvider::class, OCMProvider::class);
1263 1263
 
1264
-		$this->registerAlias(ISetupCheckManager::class, SetupCheckManager::class);
1264
+        $this->registerAlias(ISetupCheckManager::class, SetupCheckManager::class);
1265 1265
 
1266
-		$this->registerAlias(IProfileManager::class, ProfileManager::class);
1266
+        $this->registerAlias(IProfileManager::class, ProfileManager::class);
1267 1267
 
1268
-		$this->registerAlias(IAvailabilityCoordinator::class, AvailabilityCoordinator::class);
1268
+        $this->registerAlias(IAvailabilityCoordinator::class, AvailabilityCoordinator::class);
1269 1269
 
1270
-		$this->registerAlias(IDeclarativeManager::class, DeclarativeManager::class);
1270
+        $this->registerAlias(IDeclarativeManager::class, DeclarativeManager::class);
1271 1271
 
1272
-		$this->registerAlias(\OCP\TaskProcessing\IManager::class, \OC\TaskProcessing\Manager::class);
1272
+        $this->registerAlias(\OCP\TaskProcessing\IManager::class, \OC\TaskProcessing\Manager::class);
1273 1273
 
1274
-		$this->registerAlias(IRemoteAddress::class, RemoteAddress::class);
1274
+        $this->registerAlias(IRemoteAddress::class, RemoteAddress::class);
1275 1275
 
1276
-		$this->registerAlias(\OCP\Security\Ip\IFactory::class, \OC\Security\Ip\Factory::class);
1277
-
1278
-		$this->registerAlias(IRichTextFormatter::class, \OC\RichObjectStrings\RichTextFormatter::class);
1279
-
1280
-		$this->registerAlias(ISignatureManager::class, SignatureManager::class);
1281
-
1282
-		$this->connectDispatcher();
1283
-	}
1284
-
1285
-	public function boot() {
1286
-		/** @var HookConnector $hookConnector */
1287
-		$hookConnector = $this->get(HookConnector::class);
1288
-		$hookConnector->viewToNode();
1289
-	}
1290
-
1291
-	private function connectDispatcher(): void {
1292
-		/** @var IEventDispatcher $eventDispatcher */
1293
-		$eventDispatcher = $this->get(IEventDispatcher::class);
1294
-		$eventDispatcher->addServiceListener(LoginFailed::class, LoginFailedListener::class);
1295
-		$eventDispatcher->addServiceListener(PostLoginEvent::class, UserLoggedInListener::class);
1296
-		$eventDispatcher->addServiceListener(UserChangedEvent::class, UserChangedListener::class);
1297
-		$eventDispatcher->addServiceListener(BeforeUserDeletedEvent::class, BeforeUserDeletedListener::class);
1298
-
1299
-		FilesMetadataManager::loadListeners($eventDispatcher);
1300
-		GenerateBlurhashMetadata::loadListeners($eventDispatcher);
1301
-	}
1302
-
1303
-	/**
1304
-	 * @return \OCP\Contacts\IManager
1305
-	 * @deprecated 20.0.0
1306
-	 */
1307
-	public function getContactsManager() {
1308
-		return $this->get(\OCP\Contacts\IManager::class);
1309
-	}
1310
-
1311
-	/**
1312
-	 * @return \OC\Encryption\Manager
1313
-	 * @deprecated 20.0.0
1314
-	 */
1315
-	public function getEncryptionManager() {
1316
-		return $this->get(\OCP\Encryption\IManager::class);
1317
-	}
1318
-
1319
-	/**
1320
-	 * @return \OC\Encryption\File
1321
-	 * @deprecated 20.0.0
1322
-	 */
1323
-	public function getEncryptionFilesHelper() {
1324
-		return $this->get(IFile::class);
1325
-	}
1326
-
1327
-	/**
1328
-	 * The current request object holding all information about the request
1329
-	 * currently being processed is returned from this method.
1330
-	 * In case the current execution was not initiated by a web request null is returned
1331
-	 *
1332
-	 * @return \OCP\IRequest
1333
-	 * @deprecated 20.0.0
1334
-	 */
1335
-	public function getRequest() {
1336
-		return $this->get(IRequest::class);
1337
-	}
1338
-
1339
-	/**
1340
-	 * Returns the root folder of ownCloud's data directory
1341
-	 *
1342
-	 * @return IRootFolder
1343
-	 * @deprecated 20.0.0
1344
-	 */
1345
-	public function getRootFolder() {
1346
-		return $this->get(IRootFolder::class);
1347
-	}
1348
-
1349
-	/**
1350
-	 * Returns the root folder of ownCloud's data directory
1351
-	 * This is the lazy variant so this gets only initialized once it
1352
-	 * is actually used.
1353
-	 *
1354
-	 * @return IRootFolder
1355
-	 * @deprecated 20.0.0
1356
-	 */
1357
-	public function getLazyRootFolder() {
1358
-		return $this->get(IRootFolder::class);
1359
-	}
1360
-
1361
-	/**
1362
-	 * Returns a view to ownCloud's files folder
1363
-	 *
1364
-	 * @param string $userId user ID
1365
-	 * @return \OCP\Files\Folder|null
1366
-	 * @deprecated 20.0.0
1367
-	 */
1368
-	public function getUserFolder($userId = null) {
1369
-		if ($userId === null) {
1370
-			$user = $this->get(IUserSession::class)->getUser();
1371
-			if (!$user) {
1372
-				return null;
1373
-			}
1374
-			$userId = $user->getUID();
1375
-		}
1376
-		$root = $this->get(IRootFolder::class);
1377
-		return $root->getUserFolder($userId);
1378
-	}
1379
-
1380
-	/**
1381
-	 * @return \OC\User\Manager
1382
-	 * @deprecated 20.0.0
1383
-	 */
1384
-	public function getUserManager() {
1385
-		return $this->get(IUserManager::class);
1386
-	}
1387
-
1388
-	/**
1389
-	 * @return \OC\Group\Manager
1390
-	 * @deprecated 20.0.0
1391
-	 */
1392
-	public function getGroupManager() {
1393
-		return $this->get(IGroupManager::class);
1394
-	}
1395
-
1396
-	/**
1397
-	 * @return \OC\User\Session
1398
-	 * @deprecated 20.0.0
1399
-	 */
1400
-	public function getUserSession() {
1401
-		return $this->get(IUserSession::class);
1402
-	}
1403
-
1404
-	/**
1405
-	 * @return \OCP\ISession
1406
-	 * @deprecated 20.0.0
1407
-	 */
1408
-	public function getSession() {
1409
-		return $this->get(Session::class)->getSession();
1410
-	}
1411
-
1412
-	/**
1413
-	 * @param \OCP\ISession $session
1414
-	 * @return void
1415
-	 */
1416
-	public function setSession(\OCP\ISession $session) {
1417
-		$this->get(SessionStorage::class)->setSession($session);
1418
-		$this->get(Session::class)->setSession($session);
1419
-		$this->get(Store::class)->setSession($session);
1420
-	}
1421
-
1422
-	/**
1423
-	 * @return \OCP\IConfig
1424
-	 * @deprecated 20.0.0
1425
-	 */
1426
-	public function getConfig() {
1427
-		return $this->get(AllConfig::class);
1428
-	}
1429
-
1430
-	/**
1431
-	 * @return \OC\SystemConfig
1432
-	 * @deprecated 20.0.0
1433
-	 */
1434
-	public function getSystemConfig() {
1435
-		return $this->get(SystemConfig::class);
1436
-	}
1437
-
1438
-	/**
1439
-	 * @return IFactory
1440
-	 * @deprecated 20.0.0
1441
-	 */
1442
-	public function getL10NFactory() {
1443
-		return $this->get(IFactory::class);
1444
-	}
1445
-
1446
-	/**
1447
-	 * get an L10N instance
1448
-	 *
1449
-	 * @param string $app appid
1450
-	 * @param string $lang
1451
-	 * @return IL10N
1452
-	 * @deprecated 20.0.0 use DI of {@see IL10N} or {@see IFactory} instead, or {@see \OCP\Util::getL10N()} as a last resort
1453
-	 */
1454
-	public function getL10N($app, $lang = null) {
1455
-		return $this->get(IFactory::class)->get($app, $lang);
1456
-	}
1457
-
1458
-	/**
1459
-	 * @return IURLGenerator
1460
-	 * @deprecated 20.0.0
1461
-	 */
1462
-	public function getURLGenerator() {
1463
-		return $this->get(IURLGenerator::class);
1464
-	}
1465
-
1466
-	/**
1467
-	 * Returns an ICache instance. Since 8.1.0 it returns a fake cache. Use
1468
-	 * getMemCacheFactory() instead.
1469
-	 *
1470
-	 * @return ICache
1471
-	 * @deprecated 8.1.0 use getMemCacheFactory to obtain a proper cache
1472
-	 */
1473
-	public function getCache() {
1474
-		return $this->get(ICache::class);
1475
-	}
1476
-
1477
-	/**
1478
-	 * Returns an \OCP\CacheFactory instance
1479
-	 *
1480
-	 * @return \OCP\ICacheFactory
1481
-	 * @deprecated 20.0.0
1482
-	 */
1483
-	public function getMemCacheFactory() {
1484
-		return $this->get(ICacheFactory::class);
1485
-	}
1486
-
1487
-	/**
1488
-	 * Returns the current session
1489
-	 *
1490
-	 * @return \OCP\IDBConnection
1491
-	 * @deprecated 20.0.0
1492
-	 */
1493
-	public function getDatabaseConnection() {
1494
-		return $this->get(IDBConnection::class);
1495
-	}
1496
-
1497
-	/**
1498
-	 * Returns the activity manager
1499
-	 *
1500
-	 * @return \OCP\Activity\IManager
1501
-	 * @deprecated 20.0.0
1502
-	 */
1503
-	public function getActivityManager() {
1504
-		return $this->get(\OCP\Activity\IManager::class);
1505
-	}
1506
-
1507
-	/**
1508
-	 * Returns an job list for controlling background jobs
1509
-	 *
1510
-	 * @return IJobList
1511
-	 * @deprecated 20.0.0
1512
-	 */
1513
-	public function getJobList() {
1514
-		return $this->get(IJobList::class);
1515
-	}
1516
-
1517
-	/**
1518
-	 * Returns a SecureRandom instance
1519
-	 *
1520
-	 * @return \OCP\Security\ISecureRandom
1521
-	 * @deprecated 20.0.0
1522
-	 */
1523
-	public function getSecureRandom() {
1524
-		return $this->get(ISecureRandom::class);
1525
-	}
1526
-
1527
-	/**
1528
-	 * Returns a Crypto instance
1529
-	 *
1530
-	 * @return ICrypto
1531
-	 * @deprecated 20.0.0
1532
-	 */
1533
-	public function getCrypto() {
1534
-		return $this->get(ICrypto::class);
1535
-	}
1536
-
1537
-	/**
1538
-	 * Returns a Hasher instance
1539
-	 *
1540
-	 * @return IHasher
1541
-	 * @deprecated 20.0.0
1542
-	 */
1543
-	public function getHasher() {
1544
-		return $this->get(IHasher::class);
1545
-	}
1546
-
1547
-	/**
1548
-	 * Get the certificate manager
1549
-	 *
1550
-	 * @return \OCP\ICertificateManager
1551
-	 */
1552
-	public function getCertificateManager() {
1553
-		return $this->get(ICertificateManager::class);
1554
-	}
1555
-
1556
-	/**
1557
-	 * Get the manager for temporary files and folders
1558
-	 *
1559
-	 * @return \OCP\ITempManager
1560
-	 * @deprecated 20.0.0
1561
-	 */
1562
-	public function getTempManager() {
1563
-		return $this->get(ITempManager::class);
1564
-	}
1565
-
1566
-	/**
1567
-	 * Get the app manager
1568
-	 *
1569
-	 * @return \OCP\App\IAppManager
1570
-	 * @deprecated 20.0.0
1571
-	 */
1572
-	public function getAppManager() {
1573
-		return $this->get(IAppManager::class);
1574
-	}
1575
-
1576
-	/**
1577
-	 * Creates a new mailer
1578
-	 *
1579
-	 * @return IMailer
1580
-	 * @deprecated 20.0.0
1581
-	 */
1582
-	public function getMailer() {
1583
-		return $this->get(IMailer::class);
1584
-	}
1585
-
1586
-	/**
1587
-	 * Get the webroot
1588
-	 *
1589
-	 * @return string
1590
-	 * @deprecated 20.0.0
1591
-	 */
1592
-	public function getWebRoot() {
1593
-		return $this->webRoot;
1594
-	}
1595
-
1596
-	/**
1597
-	 * Get the locking provider
1598
-	 *
1599
-	 * @return ILockingProvider
1600
-	 * @since 8.1.0
1601
-	 * @deprecated 20.0.0
1602
-	 */
1603
-	public function getLockingProvider() {
1604
-		return $this->get(ILockingProvider::class);
1605
-	}
1606
-
1607
-	/**
1608
-	 * Get the MimeTypeDetector
1609
-	 *
1610
-	 * @return IMimeTypeDetector
1611
-	 * @deprecated 20.0.0
1612
-	 */
1613
-	public function getMimeTypeDetector() {
1614
-		return $this->get(IMimeTypeDetector::class);
1615
-	}
1616
-
1617
-	/**
1618
-	 * Get the MimeTypeLoader
1619
-	 *
1620
-	 * @return IMimeTypeLoader
1621
-	 * @deprecated 20.0.0
1622
-	 */
1623
-	public function getMimeTypeLoader() {
1624
-		return $this->get(IMimeTypeLoader::class);
1625
-	}
1626
-
1627
-	/**
1628
-	 * Get the Notification Manager
1629
-	 *
1630
-	 * @return \OCP\Notification\IManager
1631
-	 * @since 8.2.0
1632
-	 * @deprecated 20.0.0
1633
-	 */
1634
-	public function getNotificationManager() {
1635
-		return $this->get(\OCP\Notification\IManager::class);
1636
-	}
1637
-
1638
-	/**
1639
-	 * @return \OCA\Theming\ThemingDefaults
1640
-	 * @deprecated 20.0.0
1641
-	 */
1642
-	public function getThemingDefaults() {
1643
-		return $this->get('ThemingDefaults');
1644
-	}
1645
-
1646
-	/**
1647
-	 * @return \OC\IntegrityCheck\Checker
1648
-	 * @deprecated 20.0.0
1649
-	 */
1650
-	public function getIntegrityCodeChecker() {
1651
-		return $this->get('IntegrityCodeChecker');
1652
-	}
1653
-
1654
-	/**
1655
-	 * @return CsrfTokenManager
1656
-	 * @deprecated 20.0.0
1657
-	 */
1658
-	public function getCsrfTokenManager() {
1659
-		return $this->get(CsrfTokenManager::class);
1660
-	}
1661
-
1662
-	/**
1663
-	 * @return ContentSecurityPolicyNonceManager
1664
-	 * @deprecated 20.0.0
1665
-	 */
1666
-	public function getContentSecurityPolicyNonceManager() {
1667
-		return $this->get(ContentSecurityPolicyNonceManager::class);
1668
-	}
1669
-
1670
-	/**
1671
-	 * @return \OCP\Settings\IManager
1672
-	 * @deprecated 20.0.0
1673
-	 */
1674
-	public function getSettingsManager() {
1675
-		return $this->get(\OC\Settings\Manager::class);
1676
-	}
1677
-
1678
-	/**
1679
-	 * @return \OCP\Files\IAppData
1680
-	 * @deprecated 20.0.0 Use get(\OCP\Files\AppData\IAppDataFactory::class)->get($app) instead
1681
-	 */
1682
-	public function getAppDataDir($app) {
1683
-		$factory = $this->get(\OC\Files\AppData\Factory::class);
1684
-		return $factory->get($app);
1685
-	}
1686
-
1687
-	/**
1688
-	 * @return \OCP\Federation\ICloudIdManager
1689
-	 * @deprecated 20.0.0
1690
-	 */
1691
-	public function getCloudIdManager() {
1692
-		return $this->get(ICloudIdManager::class);
1693
-	}
1276
+        $this->registerAlias(\OCP\Security\Ip\IFactory::class, \OC\Security\Ip\Factory::class);
1277
+
1278
+        $this->registerAlias(IRichTextFormatter::class, \OC\RichObjectStrings\RichTextFormatter::class);
1279
+
1280
+        $this->registerAlias(ISignatureManager::class, SignatureManager::class);
1281
+
1282
+        $this->connectDispatcher();
1283
+    }
1284
+
1285
+    public function boot() {
1286
+        /** @var HookConnector $hookConnector */
1287
+        $hookConnector = $this->get(HookConnector::class);
1288
+        $hookConnector->viewToNode();
1289
+    }
1290
+
1291
+    private function connectDispatcher(): void {
1292
+        /** @var IEventDispatcher $eventDispatcher */
1293
+        $eventDispatcher = $this->get(IEventDispatcher::class);
1294
+        $eventDispatcher->addServiceListener(LoginFailed::class, LoginFailedListener::class);
1295
+        $eventDispatcher->addServiceListener(PostLoginEvent::class, UserLoggedInListener::class);
1296
+        $eventDispatcher->addServiceListener(UserChangedEvent::class, UserChangedListener::class);
1297
+        $eventDispatcher->addServiceListener(BeforeUserDeletedEvent::class, BeforeUserDeletedListener::class);
1298
+
1299
+        FilesMetadataManager::loadListeners($eventDispatcher);
1300
+        GenerateBlurhashMetadata::loadListeners($eventDispatcher);
1301
+    }
1302
+
1303
+    /**
1304
+     * @return \OCP\Contacts\IManager
1305
+     * @deprecated 20.0.0
1306
+     */
1307
+    public function getContactsManager() {
1308
+        return $this->get(\OCP\Contacts\IManager::class);
1309
+    }
1310
+
1311
+    /**
1312
+     * @return \OC\Encryption\Manager
1313
+     * @deprecated 20.0.0
1314
+     */
1315
+    public function getEncryptionManager() {
1316
+        return $this->get(\OCP\Encryption\IManager::class);
1317
+    }
1318
+
1319
+    /**
1320
+     * @return \OC\Encryption\File
1321
+     * @deprecated 20.0.0
1322
+     */
1323
+    public function getEncryptionFilesHelper() {
1324
+        return $this->get(IFile::class);
1325
+    }
1326
+
1327
+    /**
1328
+     * The current request object holding all information about the request
1329
+     * currently being processed is returned from this method.
1330
+     * In case the current execution was not initiated by a web request null is returned
1331
+     *
1332
+     * @return \OCP\IRequest
1333
+     * @deprecated 20.0.0
1334
+     */
1335
+    public function getRequest() {
1336
+        return $this->get(IRequest::class);
1337
+    }
1338
+
1339
+    /**
1340
+     * Returns the root folder of ownCloud's data directory
1341
+     *
1342
+     * @return IRootFolder
1343
+     * @deprecated 20.0.0
1344
+     */
1345
+    public function getRootFolder() {
1346
+        return $this->get(IRootFolder::class);
1347
+    }
1348
+
1349
+    /**
1350
+     * Returns the root folder of ownCloud's data directory
1351
+     * This is the lazy variant so this gets only initialized once it
1352
+     * is actually used.
1353
+     *
1354
+     * @return IRootFolder
1355
+     * @deprecated 20.0.0
1356
+     */
1357
+    public function getLazyRootFolder() {
1358
+        return $this->get(IRootFolder::class);
1359
+    }
1360
+
1361
+    /**
1362
+     * Returns a view to ownCloud's files folder
1363
+     *
1364
+     * @param string $userId user ID
1365
+     * @return \OCP\Files\Folder|null
1366
+     * @deprecated 20.0.0
1367
+     */
1368
+    public function getUserFolder($userId = null) {
1369
+        if ($userId === null) {
1370
+            $user = $this->get(IUserSession::class)->getUser();
1371
+            if (!$user) {
1372
+                return null;
1373
+            }
1374
+            $userId = $user->getUID();
1375
+        }
1376
+        $root = $this->get(IRootFolder::class);
1377
+        return $root->getUserFolder($userId);
1378
+    }
1379
+
1380
+    /**
1381
+     * @return \OC\User\Manager
1382
+     * @deprecated 20.0.0
1383
+     */
1384
+    public function getUserManager() {
1385
+        return $this->get(IUserManager::class);
1386
+    }
1387
+
1388
+    /**
1389
+     * @return \OC\Group\Manager
1390
+     * @deprecated 20.0.0
1391
+     */
1392
+    public function getGroupManager() {
1393
+        return $this->get(IGroupManager::class);
1394
+    }
1395
+
1396
+    /**
1397
+     * @return \OC\User\Session
1398
+     * @deprecated 20.0.0
1399
+     */
1400
+    public function getUserSession() {
1401
+        return $this->get(IUserSession::class);
1402
+    }
1403
+
1404
+    /**
1405
+     * @return \OCP\ISession
1406
+     * @deprecated 20.0.0
1407
+     */
1408
+    public function getSession() {
1409
+        return $this->get(Session::class)->getSession();
1410
+    }
1411
+
1412
+    /**
1413
+     * @param \OCP\ISession $session
1414
+     * @return void
1415
+     */
1416
+    public function setSession(\OCP\ISession $session) {
1417
+        $this->get(SessionStorage::class)->setSession($session);
1418
+        $this->get(Session::class)->setSession($session);
1419
+        $this->get(Store::class)->setSession($session);
1420
+    }
1421
+
1422
+    /**
1423
+     * @return \OCP\IConfig
1424
+     * @deprecated 20.0.0
1425
+     */
1426
+    public function getConfig() {
1427
+        return $this->get(AllConfig::class);
1428
+    }
1429
+
1430
+    /**
1431
+     * @return \OC\SystemConfig
1432
+     * @deprecated 20.0.0
1433
+     */
1434
+    public function getSystemConfig() {
1435
+        return $this->get(SystemConfig::class);
1436
+    }
1437
+
1438
+    /**
1439
+     * @return IFactory
1440
+     * @deprecated 20.0.0
1441
+     */
1442
+    public function getL10NFactory() {
1443
+        return $this->get(IFactory::class);
1444
+    }
1445
+
1446
+    /**
1447
+     * get an L10N instance
1448
+     *
1449
+     * @param string $app appid
1450
+     * @param string $lang
1451
+     * @return IL10N
1452
+     * @deprecated 20.0.0 use DI of {@see IL10N} or {@see IFactory} instead, or {@see \OCP\Util::getL10N()} as a last resort
1453
+     */
1454
+    public function getL10N($app, $lang = null) {
1455
+        return $this->get(IFactory::class)->get($app, $lang);
1456
+    }
1457
+
1458
+    /**
1459
+     * @return IURLGenerator
1460
+     * @deprecated 20.0.0
1461
+     */
1462
+    public function getURLGenerator() {
1463
+        return $this->get(IURLGenerator::class);
1464
+    }
1465
+
1466
+    /**
1467
+     * Returns an ICache instance. Since 8.1.0 it returns a fake cache. Use
1468
+     * getMemCacheFactory() instead.
1469
+     *
1470
+     * @return ICache
1471
+     * @deprecated 8.1.0 use getMemCacheFactory to obtain a proper cache
1472
+     */
1473
+    public function getCache() {
1474
+        return $this->get(ICache::class);
1475
+    }
1476
+
1477
+    /**
1478
+     * Returns an \OCP\CacheFactory instance
1479
+     *
1480
+     * @return \OCP\ICacheFactory
1481
+     * @deprecated 20.0.0
1482
+     */
1483
+    public function getMemCacheFactory() {
1484
+        return $this->get(ICacheFactory::class);
1485
+    }
1486
+
1487
+    /**
1488
+     * Returns the current session
1489
+     *
1490
+     * @return \OCP\IDBConnection
1491
+     * @deprecated 20.0.0
1492
+     */
1493
+    public function getDatabaseConnection() {
1494
+        return $this->get(IDBConnection::class);
1495
+    }
1496
+
1497
+    /**
1498
+     * Returns the activity manager
1499
+     *
1500
+     * @return \OCP\Activity\IManager
1501
+     * @deprecated 20.0.0
1502
+     */
1503
+    public function getActivityManager() {
1504
+        return $this->get(\OCP\Activity\IManager::class);
1505
+    }
1506
+
1507
+    /**
1508
+     * Returns an job list for controlling background jobs
1509
+     *
1510
+     * @return IJobList
1511
+     * @deprecated 20.0.0
1512
+     */
1513
+    public function getJobList() {
1514
+        return $this->get(IJobList::class);
1515
+    }
1516
+
1517
+    /**
1518
+     * Returns a SecureRandom instance
1519
+     *
1520
+     * @return \OCP\Security\ISecureRandom
1521
+     * @deprecated 20.0.0
1522
+     */
1523
+    public function getSecureRandom() {
1524
+        return $this->get(ISecureRandom::class);
1525
+    }
1526
+
1527
+    /**
1528
+     * Returns a Crypto instance
1529
+     *
1530
+     * @return ICrypto
1531
+     * @deprecated 20.0.0
1532
+     */
1533
+    public function getCrypto() {
1534
+        return $this->get(ICrypto::class);
1535
+    }
1536
+
1537
+    /**
1538
+     * Returns a Hasher instance
1539
+     *
1540
+     * @return IHasher
1541
+     * @deprecated 20.0.0
1542
+     */
1543
+    public function getHasher() {
1544
+        return $this->get(IHasher::class);
1545
+    }
1546
+
1547
+    /**
1548
+     * Get the certificate manager
1549
+     *
1550
+     * @return \OCP\ICertificateManager
1551
+     */
1552
+    public function getCertificateManager() {
1553
+        return $this->get(ICertificateManager::class);
1554
+    }
1555
+
1556
+    /**
1557
+     * Get the manager for temporary files and folders
1558
+     *
1559
+     * @return \OCP\ITempManager
1560
+     * @deprecated 20.0.0
1561
+     */
1562
+    public function getTempManager() {
1563
+        return $this->get(ITempManager::class);
1564
+    }
1565
+
1566
+    /**
1567
+     * Get the app manager
1568
+     *
1569
+     * @return \OCP\App\IAppManager
1570
+     * @deprecated 20.0.0
1571
+     */
1572
+    public function getAppManager() {
1573
+        return $this->get(IAppManager::class);
1574
+    }
1575
+
1576
+    /**
1577
+     * Creates a new mailer
1578
+     *
1579
+     * @return IMailer
1580
+     * @deprecated 20.0.0
1581
+     */
1582
+    public function getMailer() {
1583
+        return $this->get(IMailer::class);
1584
+    }
1585
+
1586
+    /**
1587
+     * Get the webroot
1588
+     *
1589
+     * @return string
1590
+     * @deprecated 20.0.0
1591
+     */
1592
+    public function getWebRoot() {
1593
+        return $this->webRoot;
1594
+    }
1595
+
1596
+    /**
1597
+     * Get the locking provider
1598
+     *
1599
+     * @return ILockingProvider
1600
+     * @since 8.1.0
1601
+     * @deprecated 20.0.0
1602
+     */
1603
+    public function getLockingProvider() {
1604
+        return $this->get(ILockingProvider::class);
1605
+    }
1606
+
1607
+    /**
1608
+     * Get the MimeTypeDetector
1609
+     *
1610
+     * @return IMimeTypeDetector
1611
+     * @deprecated 20.0.0
1612
+     */
1613
+    public function getMimeTypeDetector() {
1614
+        return $this->get(IMimeTypeDetector::class);
1615
+    }
1616
+
1617
+    /**
1618
+     * Get the MimeTypeLoader
1619
+     *
1620
+     * @return IMimeTypeLoader
1621
+     * @deprecated 20.0.0
1622
+     */
1623
+    public function getMimeTypeLoader() {
1624
+        return $this->get(IMimeTypeLoader::class);
1625
+    }
1626
+
1627
+    /**
1628
+     * Get the Notification Manager
1629
+     *
1630
+     * @return \OCP\Notification\IManager
1631
+     * @since 8.2.0
1632
+     * @deprecated 20.0.0
1633
+     */
1634
+    public function getNotificationManager() {
1635
+        return $this->get(\OCP\Notification\IManager::class);
1636
+    }
1637
+
1638
+    /**
1639
+     * @return \OCA\Theming\ThemingDefaults
1640
+     * @deprecated 20.0.0
1641
+     */
1642
+    public function getThemingDefaults() {
1643
+        return $this->get('ThemingDefaults');
1644
+    }
1645
+
1646
+    /**
1647
+     * @return \OC\IntegrityCheck\Checker
1648
+     * @deprecated 20.0.0
1649
+     */
1650
+    public function getIntegrityCodeChecker() {
1651
+        return $this->get('IntegrityCodeChecker');
1652
+    }
1653
+
1654
+    /**
1655
+     * @return CsrfTokenManager
1656
+     * @deprecated 20.0.0
1657
+     */
1658
+    public function getCsrfTokenManager() {
1659
+        return $this->get(CsrfTokenManager::class);
1660
+    }
1661
+
1662
+    /**
1663
+     * @return ContentSecurityPolicyNonceManager
1664
+     * @deprecated 20.0.0
1665
+     */
1666
+    public function getContentSecurityPolicyNonceManager() {
1667
+        return $this->get(ContentSecurityPolicyNonceManager::class);
1668
+    }
1669
+
1670
+    /**
1671
+     * @return \OCP\Settings\IManager
1672
+     * @deprecated 20.0.0
1673
+     */
1674
+    public function getSettingsManager() {
1675
+        return $this->get(\OC\Settings\Manager::class);
1676
+    }
1677
+
1678
+    /**
1679
+     * @return \OCP\Files\IAppData
1680
+     * @deprecated 20.0.0 Use get(\OCP\Files\AppData\IAppDataFactory::class)->get($app) instead
1681
+     */
1682
+    public function getAppDataDir($app) {
1683
+        $factory = $this->get(\OC\Files\AppData\Factory::class);
1684
+        return $factory->get($app);
1685
+    }
1686
+
1687
+    /**
1688
+     * @return \OCP\Federation\ICloudIdManager
1689
+     * @deprecated 20.0.0
1690
+     */
1691
+    public function getCloudIdManager() {
1692
+        return $this->get(ICloudIdManager::class);
1693
+    }
1694 1694
 }
Please login to merge, or discard this patch.
lib/base.php 1 patch
Indentation   +1147 added lines, -1147 removed lines patch added patch discarded remove patch
@@ -40,1153 +40,1153 @@
 block discarded – undo
40 40
  * OC_autoload!
41 41
  */
42 42
 class OC {
43
-	/**
44
-	 * The installation path for Nextcloud  on the server (e.g. /srv/http/nextcloud)
45
-	 */
46
-	public static string $SERVERROOT = '';
47
-	/**
48
-	 * the current request path relative to the Nextcloud root (e.g. files/index.php)
49
-	 */
50
-	private static string $SUBURI = '';
51
-	/**
52
-	 * the Nextcloud root path for http requests (e.g. /nextcloud)
53
-	 */
54
-	public static string $WEBROOT = '';
55
-	/**
56
-	 * The installation path array of the apps folder on the server (e.g. /srv/http/nextcloud) 'path' and
57
-	 * web path in 'url'
58
-	 */
59
-	public static array $APPSROOTS = [];
60
-
61
-	public static string $configDir;
62
-
63
-	/**
64
-	 * requested app
65
-	 */
66
-	public static string $REQUESTEDAPP = '';
67
-
68
-	/**
69
-	 * check if Nextcloud runs in cli mode
70
-	 */
71
-	public static bool $CLI = false;
72
-
73
-	public static \Composer\Autoload\ClassLoader $composerAutoloader;
74
-
75
-	public static \OC\Server $server;
76
-
77
-	private static \OC\Config $config;
78
-
79
-	/**
80
-	 * @throws \RuntimeException when the 3rdparty directory is missing or
81
-	 *                           the app path list is empty or contains an invalid path
82
-	 */
83
-	public static function initPaths(): void {
84
-		if (defined('PHPUNIT_CONFIG_DIR')) {
85
-			self::$configDir = OC::$SERVERROOT . '/' . PHPUNIT_CONFIG_DIR . '/';
86
-		} elseif (defined('PHPUNIT_RUN') and PHPUNIT_RUN and is_dir(OC::$SERVERROOT . '/tests/config/')) {
87
-			self::$configDir = OC::$SERVERROOT . '/tests/config/';
88
-		} elseif ($dir = getenv('NEXTCLOUD_CONFIG_DIR')) {
89
-			self::$configDir = rtrim($dir, '/') . '/';
90
-		} else {
91
-			self::$configDir = OC::$SERVERROOT . '/config/';
92
-		}
93
-		self::$config = new \OC\Config(self::$configDir);
94
-
95
-		OC::$SUBURI = str_replace('\\', '/', substr(realpath($_SERVER['SCRIPT_FILENAME'] ?? ''), strlen(OC::$SERVERROOT)));
96
-		/**
97
-		 * FIXME: The following lines are required because we can't yet instantiate
98
-		 *        Server::get(\OCP\IRequest::class) since \OC::$server does not yet exist.
99
-		 */
100
-		$params = [
101
-			'server' => [
102
-				'SCRIPT_NAME' => $_SERVER['SCRIPT_NAME'] ?? null,
103
-				'SCRIPT_FILENAME' => $_SERVER['SCRIPT_FILENAME'] ?? null,
104
-			],
105
-		];
106
-		if (isset($_SERVER['REMOTE_ADDR'])) {
107
-			$params['server']['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR'];
108
-		}
109
-		$fakeRequest = new \OC\AppFramework\Http\Request(
110
-			$params,
111
-			new \OC\AppFramework\Http\RequestId($_SERVER['UNIQUE_ID'] ?? '', new \OC\Security\SecureRandom()),
112
-			new \OC\AllConfig(new \OC\SystemConfig(self::$config))
113
-		);
114
-		$scriptName = $fakeRequest->getScriptName();
115
-		if (substr($scriptName, -1) == '/') {
116
-			$scriptName .= 'index.php';
117
-			//make sure suburi follows the same rules as scriptName
118
-			if (substr(OC::$SUBURI, -9) != 'index.php') {
119
-				if (substr(OC::$SUBURI, -1) != '/') {
120
-					OC::$SUBURI = OC::$SUBURI . '/';
121
-				}
122
-				OC::$SUBURI = OC::$SUBURI . 'index.php';
123
-			}
124
-		}
125
-
126
-		if (OC::$CLI) {
127
-			OC::$WEBROOT = self::$config->getValue('overwritewebroot', '');
128
-		} else {
129
-			if (substr($scriptName, 0 - strlen(OC::$SUBURI)) === OC::$SUBURI) {
130
-				OC::$WEBROOT = substr($scriptName, 0, 0 - strlen(OC::$SUBURI));
131
-
132
-				if (OC::$WEBROOT != '' && OC::$WEBROOT[0] !== '/') {
133
-					OC::$WEBROOT = '/' . OC::$WEBROOT;
134
-				}
135
-			} else {
136
-				// The scriptName is not ending with OC::$SUBURI
137
-				// This most likely means that we are calling from CLI.
138
-				// However some cron jobs still need to generate
139
-				// a web URL, so we use overwritewebroot as a fallback.
140
-				OC::$WEBROOT = self::$config->getValue('overwritewebroot', '');
141
-			}
142
-
143
-			// Resolve /nextcloud to /nextcloud/ to ensure to always have a trailing
144
-			// slash which is required by URL generation.
145
-			if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] === \OC::$WEBROOT
146
-					&& substr($_SERVER['REQUEST_URI'], -1) !== '/') {
147
-				header('Location: ' . \OC::$WEBROOT . '/');
148
-				exit();
149
-			}
150
-		}
151
-
152
-		// search the apps folder
153
-		$config_paths = self::$config->getValue('apps_paths', []);
154
-		if (!empty($config_paths)) {
155
-			foreach ($config_paths as $paths) {
156
-				if (isset($paths['url']) && isset($paths['path'])) {
157
-					$paths['url'] = rtrim($paths['url'], '/');
158
-					$paths['path'] = rtrim($paths['path'], '/');
159
-					OC::$APPSROOTS[] = $paths;
160
-				}
161
-			}
162
-		} elseif (file_exists(OC::$SERVERROOT . '/apps')) {
163
-			OC::$APPSROOTS[] = ['path' => OC::$SERVERROOT . '/apps', 'url' => '/apps', 'writable' => true];
164
-		}
165
-
166
-		if (empty(OC::$APPSROOTS)) {
167
-			throw new \RuntimeException('apps directory not found! Please put the Nextcloud apps folder in the Nextcloud folder'
168
-				. '. You can also configure the location in the config.php file.');
169
-		}
170
-		$paths = [];
171
-		foreach (OC::$APPSROOTS as $path) {
172
-			$paths[] = $path['path'];
173
-			if (!is_dir($path['path'])) {
174
-				throw new \RuntimeException(sprintf('App directory "%s" not found! Please put the Nextcloud apps folder in the'
175
-					. ' Nextcloud folder. You can also configure the location in the config.php file.', $path['path']));
176
-			}
177
-		}
178
-
179
-		// set the right include path
180
-		set_include_path(
181
-			implode(PATH_SEPARATOR, $paths)
182
-		);
183
-	}
184
-
185
-	public static function checkConfig(): void {
186
-		// Create config if it does not already exist
187
-		$configFilePath = self::$configDir . '/config.php';
188
-		if (!file_exists($configFilePath)) {
189
-			@touch($configFilePath);
190
-		}
191
-
192
-		// Check if config is writable
193
-		$configFileWritable = is_writable($configFilePath);
194
-		$configReadOnly = Server::get(IConfig::class)->getSystemValueBool('config_is_read_only');
195
-		if (!$configFileWritable && !$configReadOnly
196
-			|| !$configFileWritable && \OCP\Util::needUpgrade()) {
197
-			$urlGenerator = Server::get(IURLGenerator::class);
198
-			$l = Server::get(\OCP\L10N\IFactory::class)->get('lib');
199
-
200
-			if (self::$CLI) {
201
-				echo $l->t('Cannot write into "config" directory!') . "\n";
202
-				echo $l->t('This can usually be fixed by giving the web server write access to the config directory.') . "\n";
203
-				echo "\n";
204
-				echo $l->t('But, if you prefer to keep config.php file read only, set the option "config_is_read_only" to true in it.') . "\n";
205
-				echo $l->t('See %s', [ $urlGenerator->linkToDocs('admin-config') ]) . "\n";
206
-				exit;
207
-			} else {
208
-				Server::get(ITemplateManager::class)->printErrorPage(
209
-					$l->t('Cannot write into "config" directory!'),
210
-					$l->t('This can usually be fixed by giving the web server write access to the config directory.') . ' '
211
-					. $l->t('But, if you prefer to keep config.php file read only, set the option "config_is_read_only" to true in it.') . ' '
212
-					. $l->t('See %s', [ $urlGenerator->linkToDocs('admin-config') ]),
213
-					503
214
-				);
215
-			}
216
-		}
217
-	}
218
-
219
-	public static function checkInstalled(\OC\SystemConfig $systemConfig): void {
220
-		if (defined('OC_CONSOLE')) {
221
-			return;
222
-		}
223
-		// Redirect to installer if not installed
224
-		if (!$systemConfig->getValue('installed', false) && OC::$SUBURI !== '/index.php' && OC::$SUBURI !== '/status.php') {
225
-			if (OC::$CLI) {
226
-				throw new Exception('Not installed');
227
-			} else {
228
-				$url = OC::$WEBROOT . '/index.php';
229
-				header('Location: ' . $url);
230
-			}
231
-			exit();
232
-		}
233
-	}
234
-
235
-	public static function checkMaintenanceMode(\OC\SystemConfig $systemConfig): void {
236
-		// Allow ajax update script to execute without being stopped
237
-		if (((bool)$systemConfig->getValue('maintenance', false)) && OC::$SUBURI != '/core/ajax/update.php') {
238
-			// send http status 503
239
-			http_response_code(503);
240
-			header('X-Nextcloud-Maintenance-Mode: 1');
241
-			header('Retry-After: 120');
242
-
243
-			// render error page
244
-			$template = Server::get(ITemplateManager::class)->getTemplate('', 'update.user', 'guest');
245
-			\OCP\Util::addScript('core', 'maintenance');
246
-			\OCP\Util::addStyle('core', 'guest');
247
-			$template->printPage();
248
-			die();
249
-		}
250
-	}
251
-
252
-	/**
253
-	 * Prints the upgrade page
254
-	 */
255
-	private static function printUpgradePage(\OC\SystemConfig $systemConfig): void {
256
-		$cliUpgradeLink = $systemConfig->getValue('upgrade.cli-upgrade-link', '');
257
-		$disableWebUpdater = $systemConfig->getValue('upgrade.disable-web', false);
258
-		$tooBig = false;
259
-		if (!$disableWebUpdater) {
260
-			$apps = Server::get(\OCP\App\IAppManager::class);
261
-			if ($apps->isEnabledForAnyone('user_ldap')) {
262
-				$qb = Server::get(\OCP\IDBConnection::class)->getQueryBuilder();
263
-
264
-				$result = $qb->select($qb->func()->count('*', 'user_count'))
265
-					->from('ldap_user_mapping')
266
-					->executeQuery();
267
-				$row = $result->fetch();
268
-				$result->closeCursor();
269
-
270
-				$tooBig = ($row['user_count'] > 50);
271
-			}
272
-			if (!$tooBig && $apps->isEnabledForAnyone('user_saml')) {
273
-				$qb = Server::get(\OCP\IDBConnection::class)->getQueryBuilder();
274
-
275
-				$result = $qb->select($qb->func()->count('*', 'user_count'))
276
-					->from('user_saml_users')
277
-					->executeQuery();
278
-				$row = $result->fetch();
279
-				$result->closeCursor();
280
-
281
-				$tooBig = ($row['user_count'] > 50);
282
-			}
283
-			if (!$tooBig) {
284
-				// count users
285
-				$totalUsers = Server::get(\OCP\IUserManager::class)->countUsersTotal(51);
286
-				$tooBig = ($totalUsers > 50);
287
-			}
288
-		}
289
-		$ignoreTooBigWarning = isset($_GET['IKnowThatThisIsABigInstanceAndTheUpdateRequestCouldRunIntoATimeoutAndHowToRestoreABackup'])
290
-			&& $_GET['IKnowThatThisIsABigInstanceAndTheUpdateRequestCouldRunIntoATimeoutAndHowToRestoreABackup'] === 'IAmSuperSureToDoThis';
291
-
292
-		if ($disableWebUpdater || ($tooBig && !$ignoreTooBigWarning)) {
293
-			// send http status 503
294
-			http_response_code(503);
295
-			header('Retry-After: 120');
296
-
297
-			$serverVersion = \OCP\Server::get(\OCP\ServerVersion::class);
298
-
299
-			// render error page
300
-			$template = Server::get(ITemplateManager::class)->getTemplate('', 'update.use-cli', 'guest');
301
-			$template->assign('productName', 'nextcloud'); // for now
302
-			$template->assign('version', $serverVersion->getVersionString());
303
-			$template->assign('tooBig', $tooBig);
304
-			$template->assign('cliUpgradeLink', $cliUpgradeLink);
305
-
306
-			$template->printPage();
307
-			die();
308
-		}
309
-
310
-		// check whether this is a core update or apps update
311
-		$installedVersion = $systemConfig->getValue('version', '0.0.0');
312
-		$currentVersion = implode('.', \OCP\Util::getVersion());
313
-
314
-		// if not a core upgrade, then it's apps upgrade
315
-		$isAppsOnlyUpgrade = version_compare($currentVersion, $installedVersion, '=');
316
-
317
-		$oldTheme = $systemConfig->getValue('theme');
318
-		$systemConfig->setValue('theme', '');
319
-		\OCP\Util::addScript('core', 'common');
320
-		\OCP\Util::addScript('core', 'main');
321
-		\OCP\Util::addTranslations('core');
322
-		\OCP\Util::addScript('core', 'update');
323
-
324
-		/** @var \OC\App\AppManager $appManager */
325
-		$appManager = Server::get(\OCP\App\IAppManager::class);
326
-
327
-		$tmpl = Server::get(ITemplateManager::class)->getTemplate('', 'update.admin', 'guest');
328
-		$tmpl->assign('version', \OCP\Server::get(\OCP\ServerVersion::class)->getVersionString());
329
-		$tmpl->assign('isAppsOnlyUpgrade', $isAppsOnlyUpgrade);
330
-
331
-		// get third party apps
332
-		$ocVersion = \OCP\Util::getVersion();
333
-		$ocVersion = implode('.', $ocVersion);
334
-		$incompatibleApps = $appManager->getIncompatibleApps($ocVersion);
335
-		$incompatibleOverwrites = $systemConfig->getValue('app_install_overwrite', []);
336
-		$incompatibleShippedApps = [];
337
-		$incompatibleDisabledApps = [];
338
-		foreach ($incompatibleApps as $appInfo) {
339
-			if ($appManager->isShipped($appInfo['id'])) {
340
-				$incompatibleShippedApps[] = $appInfo['name'] . ' (' . $appInfo['id'] . ')';
341
-			}
342
-			if (!in_array($appInfo['id'], $incompatibleOverwrites)) {
343
-				$incompatibleDisabledApps[] = $appInfo;
344
-			}
345
-		}
346
-
347
-		if (!empty($incompatibleShippedApps)) {
348
-			$l = Server::get(\OCP\L10N\IFactory::class)->get('core');
349
-			$hint = $l->t('Application %1$s is not present or has a non-compatible version with this server. Please check the apps directory.', [implode(', ', $incompatibleShippedApps)]);
350
-			throw new \OCP\HintException('Application ' . implode(', ', $incompatibleShippedApps) . ' is not present or has a non-compatible version with this server. Please check the apps directory.', $hint);
351
-		}
352
-
353
-		$tmpl->assign('appsToUpgrade', $appManager->getAppsNeedingUpgrade($ocVersion));
354
-		$tmpl->assign('incompatibleAppsList', $incompatibleDisabledApps);
355
-		try {
356
-			$defaults = new \OC_Defaults();
357
-			$tmpl->assign('productName', $defaults->getName());
358
-		} catch (Throwable $error) {
359
-			$tmpl->assign('productName', 'Nextcloud');
360
-		}
361
-		$tmpl->assign('oldTheme', $oldTheme);
362
-		$tmpl->printPage();
363
-	}
364
-
365
-	public static function initSession(): void {
366
-		$request = Server::get(IRequest::class);
367
-
368
-		// TODO: Temporary disabled again to solve issues with CalDAV/CardDAV clients like DAVx5 that use cookies
369
-		// TODO: See https://github.com/nextcloud/server/issues/37277#issuecomment-1476366147 and the other comments
370
-		// TODO: for further information.
371
-		// $isDavRequest = strpos($request->getRequestUri(), '/remote.php/dav') === 0 || strpos($request->getRequestUri(), '/remote.php/webdav') === 0;
372
-		// if ($request->getHeader('Authorization') !== '' && is_null($request->getCookie('cookie_test')) && $isDavRequest && !isset($_COOKIE['nc_session_id'])) {
373
-		// setcookie('cookie_test', 'test', time() + 3600);
374
-		// // Do not initialize the session if a request is authenticated directly
375
-		// // unless there is a session cookie already sent along
376
-		// return;
377
-		// }
378
-
379
-		if ($request->getServerProtocol() === 'https') {
380
-			ini_set('session.cookie_secure', 'true');
381
-		}
382
-
383
-		// prevents javascript from accessing php session cookies
384
-		ini_set('session.cookie_httponly', 'true');
385
-
386
-		// Do not initialize sessions for 'status.php' requests
387
-		// Monitoring endpoints can quickly flood session handlers
388
-		// and 'status.php' doesn't require sessions anyway
389
-		if (str_ends_with($request->getScriptName(), '/status.php')) {
390
-			return;
391
-		}
392
-
393
-		// set the cookie path to the Nextcloud directory
394
-		$cookie_path = OC::$WEBROOT ? : '/';
395
-		ini_set('session.cookie_path', $cookie_path);
396
-
397
-		// set the cookie domain to the Nextcloud domain
398
-		$cookie_domain = self::$config->getValue('cookie_domain', '');
399
-		if ($cookie_domain) {
400
-			ini_set('session.cookie_domain', $cookie_domain);
401
-		}
402
-
403
-		// Let the session name be changed in the initSession Hook
404
-		$sessionName = OC_Util::getInstanceId();
405
-
406
-		try {
407
-			$logger = null;
408
-			if (Server::get(\OC\SystemConfig::class)->getValue('installed', false)) {
409
-				$logger = logger('core');
410
-			}
411
-
412
-			// set the session name to the instance id - which is unique
413
-			$session = new \OC\Session\Internal(
414
-				$sessionName,
415
-				$logger,
416
-			);
417
-
418
-			$cryptoWrapper = Server::get(\OC\Session\CryptoWrapper::class);
419
-			$session = $cryptoWrapper->wrapSession($session);
420
-			self::$server->setSession($session);
421
-
422
-			// if session can't be started break with http 500 error
423
-		} catch (Exception $e) {
424
-			Server::get(LoggerInterface::class)->error($e->getMessage(), ['app' => 'base','exception' => $e]);
425
-			//show the user a detailed error page
426
-			Server::get(ITemplateManager::class)->printExceptionErrorPage($e, 500);
427
-			die();
428
-		}
429
-
430
-		//try to set the session lifetime
431
-		$sessionLifeTime = self::getSessionLifeTime();
432
-
433
-		// session timeout
434
-		if ($session->exists('LAST_ACTIVITY') && (time() - $session->get('LAST_ACTIVITY') > $sessionLifeTime)) {
435
-			if (isset($_COOKIE[session_name()])) {
436
-				setcookie(session_name(), '', -1, self::$WEBROOT ? : '/');
437
-			}
438
-			Server::get(IUserSession::class)->logout();
439
-		}
440
-
441
-		if (!self::hasSessionRelaxedExpiry()) {
442
-			$session->set('LAST_ACTIVITY', time());
443
-		}
444
-		$session->close();
445
-	}
446
-
447
-	private static function getSessionLifeTime(): int {
448
-		return Server::get(\OC\AllConfig::class)->getSystemValueInt('session_lifetime', 60 * 60 * 24);
449
-	}
450
-
451
-	/**
452
-	 * @return bool true if the session expiry should only be done by gc instead of an explicit timeout
453
-	 */
454
-	public static function hasSessionRelaxedExpiry(): bool {
455
-		return Server::get(\OC\AllConfig::class)->getSystemValueBool('session_relaxed_expiry', false);
456
-	}
457
-
458
-	/**
459
-	 * Try to set some values to the required Nextcloud default
460
-	 */
461
-	public static function setRequiredIniValues(): void {
462
-		// Don't display errors and log them
463
-		@ini_set('display_errors', '0');
464
-		@ini_set('log_errors', '1');
465
-
466
-		// Try to configure php to enable big file uploads.
467
-		// This doesn't work always depending on the webserver and php configuration.
468
-		// Let's try to overwrite some defaults if they are smaller than 1 hour
469
-
470
-		if (intval(@ini_get('max_execution_time') ?: 0) < 3600) {
471
-			@ini_set('max_execution_time', strval(3600));
472
-		}
473
-
474
-		if (intval(@ini_get('max_input_time') ?: 0) < 3600) {
475
-			@ini_set('max_input_time', strval(3600));
476
-		}
477
-
478
-		// Try to set the maximum execution time to the largest time limit we have
479
-		if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) {
480
-			@set_time_limit(max(intval(@ini_get('max_execution_time')), intval(@ini_get('max_input_time'))));
481
-		}
482
-
483
-		@ini_set('default_charset', 'UTF-8');
484
-		@ini_set('gd.jpeg_ignore_warning', '1');
485
-	}
486
-
487
-	/**
488
-	 * Send the same site cookies
489
-	 */
490
-	private static function sendSameSiteCookies(): void {
491
-		$cookieParams = session_get_cookie_params();
492
-		$secureCookie = ($cookieParams['secure'] === true) ? 'secure; ' : '';
493
-		$policies = [
494
-			'lax',
495
-			'strict',
496
-		];
497
-
498
-		// Append __Host to the cookie if it meets the requirements
499
-		$cookiePrefix = '';
500
-		if ($cookieParams['secure'] === true && $cookieParams['path'] === '/') {
501
-			$cookiePrefix = '__Host-';
502
-		}
503
-
504
-		foreach ($policies as $policy) {
505
-			header(
506
-				sprintf(
507
-					'Set-Cookie: %snc_sameSiteCookie%s=true; path=%s; httponly;' . $secureCookie . 'expires=Fri, 31-Dec-2100 23:59:59 GMT; SameSite=%s',
508
-					$cookiePrefix,
509
-					$policy,
510
-					$cookieParams['path'],
511
-					$policy
512
-				),
513
-				false
514
-			);
515
-		}
516
-	}
517
-
518
-	/**
519
-	 * Same Site cookie to further mitigate CSRF attacks. This cookie has to
520
-	 * be set in every request if cookies are sent to add a second level of
521
-	 * defense against CSRF.
522
-	 *
523
-	 * If the cookie is not sent this will set the cookie and reload the page.
524
-	 * We use an additional cookie since we want to protect logout CSRF and
525
-	 * also we can't directly interfere with PHP's session mechanism.
526
-	 */
527
-	private static function performSameSiteCookieProtection(IConfig $config): void {
528
-		$request = Server::get(IRequest::class);
529
-
530
-		// Some user agents are notorious and don't really properly follow HTTP
531
-		// specifications. For those, have an automated opt-out. Since the protection
532
-		// for remote.php is applied in base.php as starting point we need to opt out
533
-		// here.
534
-		$incompatibleUserAgents = $config->getSystemValue('csrf.optout');
535
-
536
-		// Fallback, if csrf.optout is unset
537
-		if (!is_array($incompatibleUserAgents)) {
538
-			$incompatibleUserAgents = [
539
-				// OS X Finder
540
-				'/^WebDAVFS/',
541
-				// Windows webdav drive
542
-				'/^Microsoft-WebDAV-MiniRedir/',
543
-			];
544
-		}
545
-
546
-		if ($request->isUserAgent($incompatibleUserAgents)) {
547
-			return;
548
-		}
549
-
550
-		if (count($_COOKIE) > 0) {
551
-			$requestUri = $request->getScriptName();
552
-			$processingScript = explode('/', $requestUri);
553
-			$processingScript = $processingScript[count($processingScript) - 1];
554
-
555
-			if ($processingScript === 'index.php' // index.php routes are handled in the middleware
556
-				|| $processingScript === 'cron.php' // and cron.php does not need any authentication at all
557
-				|| $processingScript === 'public.php' // For public.php, auth for password protected shares is done in the PublicAuth plugin
558
-			) {
559
-				return;
560
-			}
561
-
562
-			// All other endpoints require the lax and the strict cookie
563
-			if (!$request->passesStrictCookieCheck()) {
564
-				logger('core')->warning('Request does not pass strict cookie check');
565
-				self::sendSameSiteCookies();
566
-				// Debug mode gets access to the resources without strict cookie
567
-				// due to the fact that the SabreDAV browser also lives there.
568
-				if (!$config->getSystemValueBool('debug', false)) {
569
-					http_response_code(\OCP\AppFramework\Http::STATUS_PRECONDITION_FAILED);
570
-					header('Content-Type: application/json');
571
-					echo json_encode(['error' => 'Strict Cookie has not been found in request']);
572
-					exit();
573
-				}
574
-			}
575
-		} elseif (!isset($_COOKIE['nc_sameSiteCookielax']) || !isset($_COOKIE['nc_sameSiteCookiestrict'])) {
576
-			self::sendSameSiteCookies();
577
-		}
578
-	}
579
-
580
-	public static function init(): void {
581
-		// First handle PHP configuration and copy auth headers to the expected
582
-		// $_SERVER variable before doing anything Server object related
583
-		self::setRequiredIniValues();
584
-		self::handleAuthHeaders();
585
-
586
-		// prevent any XML processing from loading external entities
587
-		libxml_set_external_entity_loader(static function () {
588
-			return null;
589
-		});
590
-
591
-		// Set default timezone before the Server object is booted
592
-		if (!date_default_timezone_set('UTC')) {
593
-			throw new \RuntimeException('Could not set timezone to UTC');
594
-		}
595
-
596
-		// calculate the root directories
597
-		OC::$SERVERROOT = str_replace('\\', '/', substr(__DIR__, 0, -4));
598
-
599
-		// register autoloader
600
-		$loaderStart = microtime(true);
601
-
602
-		self::$CLI = (php_sapi_name() == 'cli');
603
-
604
-		// Add default composer PSR-4 autoloader, ensure apcu to be disabled
605
-		self::$composerAutoloader = require_once OC::$SERVERROOT . '/lib/composer/autoload.php';
606
-		self::$composerAutoloader->setApcuPrefix(null);
607
-
608
-
609
-		try {
610
-			self::initPaths();
611
-			// setup 3rdparty autoloader
612
-			$vendorAutoLoad = OC::$SERVERROOT . '/3rdparty/autoload.php';
613
-			if (!file_exists($vendorAutoLoad)) {
614
-				throw new \RuntimeException('Composer autoloader not found, unable to continue. Check the folder "3rdparty". Running "git submodule update --init" will initialize the git submodule that handles the subfolder "3rdparty".');
615
-			}
616
-			require_once $vendorAutoLoad;
617
-		} catch (\RuntimeException $e) {
618
-			if (!self::$CLI) {
619
-				http_response_code(503);
620
-			}
621
-			// we can't use the template error page here, because this needs the
622
-			// DI container which isn't available yet
623
-			print($e->getMessage());
624
-			exit();
625
-		}
626
-		$loaderEnd = microtime(true);
627
-
628
-		// Enable lazy loading if activated
629
-		\OC\AppFramework\Utility\SimpleContainer::$useLazyObjects = (bool)self::$config->getValue('enable_lazy_objects', true);
630
-
631
-		// setup the basic server
632
-		self::$server = new \OC\Server(\OC::$WEBROOT, self::$config);
633
-		self::$server->boot();
634
-
635
-		try {
636
-			$profiler = new BuiltInProfiler(
637
-				Server::get(IConfig::class),
638
-				Server::get(IRequest::class),
639
-			);
640
-			$profiler->start();
641
-		} catch (\Throwable $e) {
642
-			logger('core')->error('Failed to start profiler: ' . $e->getMessage(), ['app' => 'base']);
643
-		}
644
-
645
-		if (self::$CLI && in_array('--' . \OCP\Console\ReservedOptions::DEBUG_LOG, $_SERVER['argv'])) {
646
-			\OC\Core\Listener\BeforeMessageLoggedEventListener::setup();
647
-		}
648
-
649
-		$eventLogger = Server::get(\OCP\Diagnostics\IEventLogger::class);
650
-		$eventLogger->log('autoloader', 'Autoloader', $loaderStart, $loaderEnd);
651
-		$eventLogger->start('boot', 'Initialize');
652
-
653
-		// Override php.ini and log everything if we're troubleshooting
654
-		if (self::$config->getValue('loglevel') === ILogger::DEBUG) {
655
-			error_reporting(E_ALL);
656
-		}
657
-
658
-		// initialize intl fallback if necessary
659
-		OC_Util::isSetLocaleWorking();
660
-
661
-		$config = Server::get(IConfig::class);
662
-		if (!defined('PHPUNIT_RUN')) {
663
-			$errorHandler = new OC\Log\ErrorHandler(
664
-				\OCP\Server::get(\Psr\Log\LoggerInterface::class),
665
-			);
666
-			$exceptionHandler = [$errorHandler, 'onException'];
667
-			if ($config->getSystemValueBool('debug', false)) {
668
-				set_error_handler([$errorHandler, 'onAll'], E_ALL);
669
-				if (\OC::$CLI) {
670
-					$exceptionHandler = [Server::get(ITemplateManager::class), 'printExceptionErrorPage'];
671
-				}
672
-			} else {
673
-				set_error_handler([$errorHandler, 'onError']);
674
-			}
675
-			register_shutdown_function([$errorHandler, 'onShutdown']);
676
-			set_exception_handler($exceptionHandler);
677
-		}
678
-
679
-		/** @var \OC\AppFramework\Bootstrap\Coordinator $bootstrapCoordinator */
680
-		$bootstrapCoordinator = Server::get(\OC\AppFramework\Bootstrap\Coordinator::class);
681
-		$bootstrapCoordinator->runInitialRegistration();
682
-
683
-		$eventLogger->start('init_session', 'Initialize session');
684
-
685
-		// Check for PHP SimpleXML extension earlier since we need it before our other checks and want to provide a useful hint for web users
686
-		// see https://github.com/nextcloud/server/pull/2619
687
-		if (!function_exists('simplexml_load_file')) {
688
-			throw new \OCP\HintException('The PHP SimpleXML/PHP-XML extension is not installed.', 'Install the extension or make sure it is enabled.');
689
-		}
690
-
691
-		$systemConfig = Server::get(\OC\SystemConfig::class);
692
-		$appManager = Server::get(\OCP\App\IAppManager::class);
693
-		if ($systemConfig->getValue('installed', false)) {
694
-			$appManager->loadApps(['session']);
695
-		}
696
-		if (!self::$CLI) {
697
-			self::initSession();
698
-		}
699
-		$eventLogger->end('init_session');
700
-		self::checkConfig();
701
-		self::checkInstalled($systemConfig);
702
-
703
-		OC_Response::addSecurityHeaders();
704
-
705
-		self::performSameSiteCookieProtection($config);
706
-
707
-		if (!defined('OC_CONSOLE')) {
708
-			$eventLogger->start('check_server', 'Run a few configuration checks');
709
-			$errors = OC_Util::checkServer($systemConfig);
710
-			if (count($errors) > 0) {
711
-				if (!self::$CLI) {
712
-					http_response_code(503);
713
-					Util::addStyle('guest');
714
-					try {
715
-						Server::get(ITemplateManager::class)->printGuestPage('', 'error', ['errors' => $errors]);
716
-						exit;
717
-					} catch (\Exception $e) {
718
-						// In case any error happens when showing the error page, we simply fall back to posting the text.
719
-						// This might be the case when e.g. the data directory is broken and we can not load/write SCSS to/from it.
720
-					}
721
-				}
722
-
723
-				// Convert l10n string into regular string for usage in database
724
-				$staticErrors = [];
725
-				foreach ($errors as $error) {
726
-					echo $error['error'] . "\n";
727
-					echo $error['hint'] . "\n\n";
728
-					$staticErrors[] = [
729
-						'error' => (string)$error['error'],
730
-						'hint' => (string)$error['hint'],
731
-					];
732
-				}
733
-
734
-				try {
735
-					$config->setAppValue('core', 'cronErrors', json_encode($staticErrors));
736
-				} catch (\Exception $e) {
737
-					echo('Writing to database failed');
738
-				}
739
-				exit(1);
740
-			} elseif (self::$CLI && $config->getSystemValueBool('installed', false)) {
741
-				$config->deleteAppValue('core', 'cronErrors');
742
-			}
743
-			$eventLogger->end('check_server');
744
-		}
745
-
746
-		// User and Groups
747
-		if (!$systemConfig->getValue('installed', false)) {
748
-			self::$server->getSession()->set('user_id', '');
749
-		}
750
-
751
-		$eventLogger->start('setup_backends', 'Setup group and user backends');
752
-		Server::get(\OCP\IUserManager::class)->registerBackend(new \OC\User\Database());
753
-		Server::get(\OCP\IGroupManager::class)->addBackend(new \OC\Group\Database());
754
-
755
-		// Subscribe to the hook
756
-		\OCP\Util::connectHook(
757
-			'\OCA\Files_Sharing\API\Server2Server',
758
-			'preLoginNameUsedAsUserName',
759
-			'\OC\User\Database',
760
-			'preLoginNameUsedAsUserName'
761
-		);
762
-
763
-		//setup extra user backends
764
-		if (!\OCP\Util::needUpgrade()) {
765
-			OC_User::setupBackends();
766
-		} else {
767
-			// Run upgrades in incognito mode
768
-			OC_User::setIncognitoMode(true);
769
-		}
770
-		$eventLogger->end('setup_backends');
771
-
772
-		self::registerCleanupHooks($systemConfig);
773
-		self::registerShareHooks($systemConfig);
774
-		self::registerEncryptionWrapperAndHooks();
775
-		self::registerAccountHooks();
776
-		self::registerResourceCollectionHooks();
777
-		self::registerFileReferenceEventListener();
778
-		self::registerRenderReferenceEventListener();
779
-		self::registerAppRestrictionsHooks();
780
-
781
-		// Make sure that the application class is not loaded before the database is setup
782
-		if ($systemConfig->getValue('installed', false)) {
783
-			$appManager->loadApp('settings');
784
-			/* Run core application registration */
785
-			$bootstrapCoordinator->runLazyRegistration('core');
786
-		}
787
-
788
-		//make sure temporary files are cleaned up
789
-		$tmpManager = Server::get(\OCP\ITempManager::class);
790
-		register_shutdown_function([$tmpManager, 'clean']);
791
-		$lockProvider = Server::get(\OCP\Lock\ILockingProvider::class);
792
-		register_shutdown_function([$lockProvider, 'releaseAll']);
793
-
794
-		// Check whether the sample configuration has been copied
795
-		if ($systemConfig->getValue('copied_sample_config', false)) {
796
-			$l = Server::get(\OCP\L10N\IFactory::class)->get('lib');
797
-			Server::get(ITemplateManager::class)->printErrorPage(
798
-				$l->t('Sample configuration detected'),
799
-				$l->t('It has been detected that the sample configuration has been copied. This can break your installation and is unsupported. Please read the documentation before performing changes on config.php'),
800
-				503
801
-			);
802
-			return;
803
-		}
804
-
805
-		$request = Server::get(IRequest::class);
806
-		$host = $request->getInsecureServerHost();
807
-		/**
808
-		 * if the host passed in headers isn't trusted
809
-		 * FIXME: Should not be in here at all :see_no_evil:
810
-		 */
811
-		if (!OC::$CLI
812
-			&& !Server::get(\OC\Security\TrustedDomainHelper::class)->isTrustedDomain($host)
813
-			&& $config->getSystemValueBool('installed', false)
814
-		) {
815
-			// Allow access to CSS resources
816
-			$isScssRequest = false;
817
-			if (strpos($request->getPathInfo() ?: '', '/css/') === 0) {
818
-				$isScssRequest = true;
819
-			}
820
-
821
-			if (substr($request->getRequestUri(), -11) === '/status.php') {
822
-				http_response_code(400);
823
-				header('Content-Type: application/json');
824
-				echo '{"error": "Trusted domain error.", "code": 15}';
825
-				exit();
826
-			}
827
-
828
-			if (!$isScssRequest) {
829
-				http_response_code(400);
830
-				Server::get(LoggerInterface::class)->info(
831
-					'Trusted domain error. "{remoteAddress}" tried to access using "{host}" as host.',
832
-					[
833
-						'app' => 'core',
834
-						'remoteAddress' => $request->getRemoteAddress(),
835
-						'host' => $host,
836
-					]
837
-				);
838
-
839
-				$tmpl = Server::get(ITemplateManager::class)->getTemplate('core', 'untrustedDomain', 'guest');
840
-				$tmpl->assign('docUrl', Server::get(IURLGenerator::class)->linkToDocs('admin-trusted-domains'));
841
-				$tmpl->printPage();
842
-
843
-				exit();
844
-			}
845
-		}
846
-		$eventLogger->end('boot');
847
-		$eventLogger->log('init', 'OC::init', $loaderStart, microtime(true));
848
-		$eventLogger->start('runtime', 'Runtime');
849
-		$eventLogger->start('request', 'Full request after boot');
850
-		register_shutdown_function(function () use ($eventLogger) {
851
-			$eventLogger->end('request');
852
-		});
853
-
854
-		register_shutdown_function(function () {
855
-			$memoryPeak = memory_get_peak_usage();
856
-			$logLevel = match (true) {
857
-				$memoryPeak > 500_000_000 => ILogger::FATAL,
858
-				$memoryPeak > 400_000_000 => ILogger::ERROR,
859
-				$memoryPeak > 300_000_000 => ILogger::WARN,
860
-				default => null,
861
-			};
862
-			if ($logLevel !== null) {
863
-				$message = 'Request used more than 300 MB of RAM: ' . Util::humanFileSize($memoryPeak);
864
-				$logger = Server::get(LoggerInterface::class);
865
-				$logger->log($logLevel, $message, ['app' => 'core']);
866
-			}
867
-		});
868
-	}
869
-
870
-	/**
871
-	 * register hooks for the cleanup of cache and bruteforce protection
872
-	 */
873
-	public static function registerCleanupHooks(\OC\SystemConfig $systemConfig): void {
874
-		//don't try to do this before we are properly setup
875
-		if ($systemConfig->getValue('installed', false) && !\OCP\Util::needUpgrade()) {
876
-			// NOTE: This will be replaced to use OCP
877
-			$userSession = Server::get(\OC\User\Session::class);
878
-			$userSession->listen('\OC\User', 'postLogin', function () use ($userSession) {
879
-				if (!defined('PHPUNIT_RUN') && $userSession->isLoggedIn()) {
880
-					// reset brute force delay for this IP address and username
881
-					$uid = $userSession->getUser()->getUID();
882
-					$request = Server::get(IRequest::class);
883
-					$throttler = Server::get(IThrottler::class);
884
-					$throttler->resetDelay($request->getRemoteAddress(), 'login', ['user' => $uid]);
885
-				}
886
-
887
-				try {
888
-					$cache = new \OC\Cache\File();
889
-					$cache->gc();
890
-				} catch (\OC\ServerNotAvailableException $e) {
891
-					// not a GC exception, pass it on
892
-					throw $e;
893
-				} catch (\OC\ForbiddenException $e) {
894
-					// filesystem blocked for this request, ignore
895
-				} catch (\Exception $e) {
896
-					// a GC exception should not prevent users from using OC,
897
-					// so log the exception
898
-					Server::get(LoggerInterface::class)->warning('Exception when running cache gc.', [
899
-						'app' => 'core',
900
-						'exception' => $e,
901
-					]);
902
-				}
903
-			});
904
-		}
905
-	}
906
-
907
-	private static function registerEncryptionWrapperAndHooks(): void {
908
-		/** @var \OC\Encryption\Manager */
909
-		$manager = Server::get(\OCP\Encryption\IManager::class);
910
-		Server::get(IEventDispatcher::class)->addListener(
911
-			BeforeFileSystemSetupEvent::class,
912
-			$manager->setupStorage(...),
913
-		);
914
-
915
-		$enabled = $manager->isEnabled();
916
-		if ($enabled) {
917
-			\OC\Encryption\EncryptionEventListener::register(Server::get(IEventDispatcher::class));
918
-		}
919
-	}
920
-
921
-	private static function registerAccountHooks(): void {
922
-		/** @var IEventDispatcher $dispatcher */
923
-		$dispatcher = Server::get(IEventDispatcher::class);
924
-		$dispatcher->addServiceListener(UserChangedEvent::class, \OC\Accounts\Hooks::class);
925
-	}
926
-
927
-	private static function registerAppRestrictionsHooks(): void {
928
-		/** @var \OC\Group\Manager $groupManager */
929
-		$groupManager = Server::get(\OCP\IGroupManager::class);
930
-		$groupManager->listen('\OC\Group', 'postDelete', function (\OCP\IGroup $group) {
931
-			$appManager = Server::get(\OCP\App\IAppManager::class);
932
-			$apps = $appManager->getEnabledAppsForGroup($group);
933
-			foreach ($apps as $appId) {
934
-				$restrictions = $appManager->getAppRestriction($appId);
935
-				if (empty($restrictions)) {
936
-					continue;
937
-				}
938
-				$key = array_search($group->getGID(), $restrictions);
939
-				unset($restrictions[$key]);
940
-				$restrictions = array_values($restrictions);
941
-				if (empty($restrictions)) {
942
-					$appManager->disableApp($appId);
943
-				} else {
944
-					$appManager->enableAppForGroups($appId, $restrictions);
945
-				}
946
-			}
947
-		});
948
-	}
949
-
950
-	private static function registerResourceCollectionHooks(): void {
951
-		\OC\Collaboration\Resources\Listener::register(Server::get(IEventDispatcher::class));
952
-	}
953
-
954
-	private static function registerFileReferenceEventListener(): void {
955
-		\OC\Collaboration\Reference\File\FileReferenceEventListener::register(Server::get(IEventDispatcher::class));
956
-	}
957
-
958
-	private static function registerRenderReferenceEventListener() {
959
-		\OC\Collaboration\Reference\RenderReferenceEventListener::register(Server::get(IEventDispatcher::class));
960
-	}
961
-
962
-	/**
963
-	 * register hooks for sharing
964
-	 */
965
-	public static function registerShareHooks(\OC\SystemConfig $systemConfig): void {
966
-		if ($systemConfig->getValue('installed')) {
967
-
968
-			$dispatcher = Server::get(IEventDispatcher::class);
969
-			$dispatcher->addServiceListener(UserRemovedEvent::class, UserRemovedListener::class);
970
-			$dispatcher->addServiceListener(GroupDeletedEvent::class, GroupDeletedListener::class);
971
-			$dispatcher->addServiceListener(UserDeletedEvent::class, UserDeletedListener::class);
972
-		}
973
-	}
974
-
975
-	/**
976
-	 * Handle the request
977
-	 */
978
-	public static function handleRequest(): void {
979
-		Server::get(\OCP\Diagnostics\IEventLogger::class)->start('handle_request', 'Handle request');
980
-		$systemConfig = Server::get(\OC\SystemConfig::class);
981
-
982
-		// Check if Nextcloud is installed or in maintenance (update) mode
983
-		if (!$systemConfig->getValue('installed', false)) {
984
-			\OC::$server->getSession()->clear();
985
-			$controller = Server::get(\OC\Core\Controller\SetupController::class);
986
-			$controller->run($_POST);
987
-			exit();
988
-		}
989
-
990
-		$request = Server::get(IRequest::class);
991
-		$request->throwDecodingExceptionIfAny();
992
-		$requestPath = $request->getRawPathInfo();
993
-		if ($requestPath === '/heartbeat') {
994
-			return;
995
-		}
996
-		if (substr($requestPath, -3) !== '.js') { // we need these files during the upgrade
997
-			self::checkMaintenanceMode($systemConfig);
998
-
999
-			if (\OCP\Util::needUpgrade()) {
1000
-				if (function_exists('opcache_reset')) {
1001
-					opcache_reset();
1002
-				}
1003
-				if (!((bool)$systemConfig->getValue('maintenance', false))) {
1004
-					self::printUpgradePage($systemConfig);
1005
-					exit();
1006
-				}
1007
-			}
1008
-		}
1009
-
1010
-		$appManager = Server::get(\OCP\App\IAppManager::class);
1011
-
1012
-		// Always load authentication apps
1013
-		$appManager->loadApps(['authentication']);
1014
-		$appManager->loadApps(['extended_authentication']);
1015
-
1016
-		// Load minimum set of apps
1017
-		if (!\OCP\Util::needUpgrade()
1018
-			&& !((bool)$systemConfig->getValue('maintenance', false))) {
1019
-			// For logged-in users: Load everything
1020
-			if (Server::get(IUserSession::class)->isLoggedIn()) {
1021
-				$appManager->loadApps();
1022
-			} else {
1023
-				// For guests: Load only filesystem and logging
1024
-				$appManager->loadApps(['filesystem', 'logging']);
1025
-
1026
-				// Don't try to login when a client is trying to get a OAuth token.
1027
-				// OAuth needs to support basic auth too, so the login is not valid
1028
-				// inside Nextcloud and the Login exception would ruin it.
1029
-				if ($request->getRawPathInfo() !== '/apps/oauth2/api/v1/token') {
1030
-					try {
1031
-						self::handleLogin($request);
1032
-					} catch (DisabledUserException $e) {
1033
-						// Disabled users would not be seen as logged in and
1034
-						// trying to log them in would fail, so the login
1035
-						// exception is ignored for the themed stylesheets and
1036
-						// images.
1037
-						if ($request->getRawPathInfo() !== '/apps/theming/theme/default.css'
1038
-							&& $request->getRawPathInfo() !== '/apps/theming/theme/light.css'
1039
-							&& $request->getRawPathInfo() !== '/apps/theming/theme/dark.css'
1040
-							&& $request->getRawPathInfo() !== '/apps/theming/theme/light-highcontrast.css'
1041
-							&& $request->getRawPathInfo() !== '/apps/theming/theme/dark-highcontrast.css'
1042
-							&& $request->getRawPathInfo() !== '/apps/theming/theme/opendyslexic.css'
1043
-							&& $request->getRawPathInfo() !== '/apps/theming/image/background'
1044
-							&& $request->getRawPathInfo() !== '/apps/theming/image/logo'
1045
-							&& $request->getRawPathInfo() !== '/apps/theming/image/logoheader'
1046
-							&& !str_starts_with($request->getRawPathInfo(), '/apps/theming/favicon')
1047
-							&& !str_starts_with($request->getRawPathInfo(), '/apps/theming/icon')) {
1048
-							throw $e;
1049
-						}
1050
-					}
1051
-				}
1052
-			}
1053
-		}
1054
-
1055
-		if (!self::$CLI) {
1056
-			try {
1057
-				if (!\OCP\Util::needUpgrade()) {
1058
-					$appManager->loadApps(['filesystem', 'logging']);
1059
-					$appManager->loadApps();
1060
-				}
1061
-				Server::get(\OC\Route\Router::class)->match($request->getRawPathInfo());
1062
-				return;
1063
-			} catch (Symfony\Component\Routing\Exception\ResourceNotFoundException $e) {
1064
-				//header('HTTP/1.0 404 Not Found');
1065
-			} catch (Symfony\Component\Routing\Exception\MethodNotAllowedException $e) {
1066
-				http_response_code(405);
1067
-				return;
1068
-			}
1069
-		}
1070
-
1071
-		// Handle WebDAV
1072
-		if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND') {
1073
-			// not allowed any more to prevent people
1074
-			// mounting this root directly.
1075
-			// Users need to mount remote.php/webdav instead.
1076
-			http_response_code(405);
1077
-			return;
1078
-		}
1079
-
1080
-		// Handle requests for JSON or XML
1081
-		$acceptHeader = $request->getHeader('Accept');
1082
-		if (in_array($acceptHeader, ['application/json', 'application/xml'], true)) {
1083
-			http_response_code(404);
1084
-			return;
1085
-		}
1086
-
1087
-		// Handle resources that can't be found
1088
-		// This prevents browsers from redirecting to the default page and then
1089
-		// attempting to parse HTML as CSS and similar.
1090
-		$destinationHeader = $request->getHeader('Sec-Fetch-Dest');
1091
-		if (in_array($destinationHeader, ['font', 'script', 'style'])) {
1092
-			http_response_code(404);
1093
-			return;
1094
-		}
1095
-
1096
-		// Redirect to the default app or login only as an entry point
1097
-		if ($requestPath === '') {
1098
-			// Someone is logged in
1099
-			if (Server::get(IUserSession::class)->isLoggedIn()) {
1100
-				header('Location: ' . Server::get(IURLGenerator::class)->linkToDefaultPageUrl());
1101
-			} else {
1102
-				// Not handled and not logged in
1103
-				header('Location: ' . Server::get(IURLGenerator::class)->linkToRouteAbsolute('core.login.showLoginForm'));
1104
-			}
1105
-			return;
1106
-		}
1107
-
1108
-		try {
1109
-			Server::get(\OC\Route\Router::class)->match('/error/404');
1110
-		} catch (\Exception $e) {
1111
-			if (!$e instanceof MethodNotAllowedException) {
1112
-				logger('core')->emergency($e->getMessage(), ['exception' => $e]);
1113
-			}
1114
-			$l = Server::get(\OCP\L10N\IFactory::class)->get('lib');
1115
-			Server::get(ITemplateManager::class)->printErrorPage(
1116
-				'404',
1117
-				$l->t('The page could not be found on the server.'),
1118
-				404
1119
-			);
1120
-		}
1121
-	}
1122
-
1123
-	/**
1124
-	 * Check login: apache auth, auth token, basic auth
1125
-	 */
1126
-	public static function handleLogin(OCP\IRequest $request): bool {
1127
-		if ($request->getHeader('X-Nextcloud-Federation')) {
1128
-			return false;
1129
-		}
1130
-		$userSession = Server::get(\OC\User\Session::class);
1131
-		if (OC_User::handleApacheAuth()) {
1132
-			return true;
1133
-		}
1134
-		if (self::tryAppAPILogin($request)) {
1135
-			return true;
1136
-		}
1137
-		if ($userSession->tryTokenLogin($request)) {
1138
-			return true;
1139
-		}
1140
-		if (isset($_COOKIE['nc_username'])
1141
-			&& isset($_COOKIE['nc_token'])
1142
-			&& isset($_COOKIE['nc_session_id'])
1143
-			&& $userSession->loginWithCookie($_COOKIE['nc_username'], $_COOKIE['nc_token'], $_COOKIE['nc_session_id'])) {
1144
-			return true;
1145
-		}
1146
-		if ($userSession->tryBasicAuthLogin($request, Server::get(IThrottler::class))) {
1147
-			return true;
1148
-		}
1149
-		return false;
1150
-	}
1151
-
1152
-	protected static function handleAuthHeaders(): void {
1153
-		//copy http auth headers for apache+php-fcgid work around
1154
-		if (isset($_SERVER['HTTP_XAUTHORIZATION']) && !isset($_SERVER['HTTP_AUTHORIZATION'])) {
1155
-			$_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['HTTP_XAUTHORIZATION'];
1156
-		}
1157
-
1158
-		// Extract PHP_AUTH_USER/PHP_AUTH_PW from other headers if necessary.
1159
-		$vars = [
1160
-			'HTTP_AUTHORIZATION', // apache+php-cgi work around
1161
-			'REDIRECT_HTTP_AUTHORIZATION', // apache+php-cgi alternative
1162
-		];
1163
-		foreach ($vars as $var) {
1164
-			if (isset($_SERVER[$var]) && is_string($_SERVER[$var]) && preg_match('/Basic\s+(.*)$/i', $_SERVER[$var], $matches)) {
1165
-				$credentials = explode(':', base64_decode($matches[1]), 2);
1166
-				if (count($credentials) === 2) {
1167
-					$_SERVER['PHP_AUTH_USER'] = $credentials[0];
1168
-					$_SERVER['PHP_AUTH_PW'] = $credentials[1];
1169
-					break;
1170
-				}
1171
-			}
1172
-		}
1173
-	}
1174
-
1175
-	protected static function tryAppAPILogin(OCP\IRequest $request): bool {
1176
-		if (!$request->getHeader('AUTHORIZATION-APP-API')) {
1177
-			return false;
1178
-		}
1179
-		$appManager = Server::get(OCP\App\IAppManager::class);
1180
-		if (!$appManager->isEnabledForAnyone('app_api')) {
1181
-			return false;
1182
-		}
1183
-		try {
1184
-			$appAPIService = Server::get(OCA\AppAPI\Service\AppAPIService::class);
1185
-			return $appAPIService->validateExAppRequestToNC($request);
1186
-		} catch (\Psr\Container\NotFoundExceptionInterface|\Psr\Container\ContainerExceptionInterface $e) {
1187
-			return false;
1188
-		}
1189
-	}
43
+    /**
44
+     * The installation path for Nextcloud  on the server (e.g. /srv/http/nextcloud)
45
+     */
46
+    public static string $SERVERROOT = '';
47
+    /**
48
+     * the current request path relative to the Nextcloud root (e.g. files/index.php)
49
+     */
50
+    private static string $SUBURI = '';
51
+    /**
52
+     * the Nextcloud root path for http requests (e.g. /nextcloud)
53
+     */
54
+    public static string $WEBROOT = '';
55
+    /**
56
+     * The installation path array of the apps folder on the server (e.g. /srv/http/nextcloud) 'path' and
57
+     * web path in 'url'
58
+     */
59
+    public static array $APPSROOTS = [];
60
+
61
+    public static string $configDir;
62
+
63
+    /**
64
+     * requested app
65
+     */
66
+    public static string $REQUESTEDAPP = '';
67
+
68
+    /**
69
+     * check if Nextcloud runs in cli mode
70
+     */
71
+    public static bool $CLI = false;
72
+
73
+    public static \Composer\Autoload\ClassLoader $composerAutoloader;
74
+
75
+    public static \OC\Server $server;
76
+
77
+    private static \OC\Config $config;
78
+
79
+    /**
80
+     * @throws \RuntimeException when the 3rdparty directory is missing or
81
+     *                           the app path list is empty or contains an invalid path
82
+     */
83
+    public static function initPaths(): void {
84
+        if (defined('PHPUNIT_CONFIG_DIR')) {
85
+            self::$configDir = OC::$SERVERROOT . '/' . PHPUNIT_CONFIG_DIR . '/';
86
+        } elseif (defined('PHPUNIT_RUN') and PHPUNIT_RUN and is_dir(OC::$SERVERROOT . '/tests/config/')) {
87
+            self::$configDir = OC::$SERVERROOT . '/tests/config/';
88
+        } elseif ($dir = getenv('NEXTCLOUD_CONFIG_DIR')) {
89
+            self::$configDir = rtrim($dir, '/') . '/';
90
+        } else {
91
+            self::$configDir = OC::$SERVERROOT . '/config/';
92
+        }
93
+        self::$config = new \OC\Config(self::$configDir);
94
+
95
+        OC::$SUBURI = str_replace('\\', '/', substr(realpath($_SERVER['SCRIPT_FILENAME'] ?? ''), strlen(OC::$SERVERROOT)));
96
+        /**
97
+         * FIXME: The following lines are required because we can't yet instantiate
98
+         *        Server::get(\OCP\IRequest::class) since \OC::$server does not yet exist.
99
+         */
100
+        $params = [
101
+            'server' => [
102
+                'SCRIPT_NAME' => $_SERVER['SCRIPT_NAME'] ?? null,
103
+                'SCRIPT_FILENAME' => $_SERVER['SCRIPT_FILENAME'] ?? null,
104
+            ],
105
+        ];
106
+        if (isset($_SERVER['REMOTE_ADDR'])) {
107
+            $params['server']['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR'];
108
+        }
109
+        $fakeRequest = new \OC\AppFramework\Http\Request(
110
+            $params,
111
+            new \OC\AppFramework\Http\RequestId($_SERVER['UNIQUE_ID'] ?? '', new \OC\Security\SecureRandom()),
112
+            new \OC\AllConfig(new \OC\SystemConfig(self::$config))
113
+        );
114
+        $scriptName = $fakeRequest->getScriptName();
115
+        if (substr($scriptName, -1) == '/') {
116
+            $scriptName .= 'index.php';
117
+            //make sure suburi follows the same rules as scriptName
118
+            if (substr(OC::$SUBURI, -9) != 'index.php') {
119
+                if (substr(OC::$SUBURI, -1) != '/') {
120
+                    OC::$SUBURI = OC::$SUBURI . '/';
121
+                }
122
+                OC::$SUBURI = OC::$SUBURI . 'index.php';
123
+            }
124
+        }
125
+
126
+        if (OC::$CLI) {
127
+            OC::$WEBROOT = self::$config->getValue('overwritewebroot', '');
128
+        } else {
129
+            if (substr($scriptName, 0 - strlen(OC::$SUBURI)) === OC::$SUBURI) {
130
+                OC::$WEBROOT = substr($scriptName, 0, 0 - strlen(OC::$SUBURI));
131
+
132
+                if (OC::$WEBROOT != '' && OC::$WEBROOT[0] !== '/') {
133
+                    OC::$WEBROOT = '/' . OC::$WEBROOT;
134
+                }
135
+            } else {
136
+                // The scriptName is not ending with OC::$SUBURI
137
+                // This most likely means that we are calling from CLI.
138
+                // However some cron jobs still need to generate
139
+                // a web URL, so we use overwritewebroot as a fallback.
140
+                OC::$WEBROOT = self::$config->getValue('overwritewebroot', '');
141
+            }
142
+
143
+            // Resolve /nextcloud to /nextcloud/ to ensure to always have a trailing
144
+            // slash which is required by URL generation.
145
+            if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] === \OC::$WEBROOT
146
+                    && substr($_SERVER['REQUEST_URI'], -1) !== '/') {
147
+                header('Location: ' . \OC::$WEBROOT . '/');
148
+                exit();
149
+            }
150
+        }
151
+
152
+        // search the apps folder
153
+        $config_paths = self::$config->getValue('apps_paths', []);
154
+        if (!empty($config_paths)) {
155
+            foreach ($config_paths as $paths) {
156
+                if (isset($paths['url']) && isset($paths['path'])) {
157
+                    $paths['url'] = rtrim($paths['url'], '/');
158
+                    $paths['path'] = rtrim($paths['path'], '/');
159
+                    OC::$APPSROOTS[] = $paths;
160
+                }
161
+            }
162
+        } elseif (file_exists(OC::$SERVERROOT . '/apps')) {
163
+            OC::$APPSROOTS[] = ['path' => OC::$SERVERROOT . '/apps', 'url' => '/apps', 'writable' => true];
164
+        }
165
+
166
+        if (empty(OC::$APPSROOTS)) {
167
+            throw new \RuntimeException('apps directory not found! Please put the Nextcloud apps folder in the Nextcloud folder'
168
+                . '. You can also configure the location in the config.php file.');
169
+        }
170
+        $paths = [];
171
+        foreach (OC::$APPSROOTS as $path) {
172
+            $paths[] = $path['path'];
173
+            if (!is_dir($path['path'])) {
174
+                throw new \RuntimeException(sprintf('App directory "%s" not found! Please put the Nextcloud apps folder in the'
175
+                    . ' Nextcloud folder. You can also configure the location in the config.php file.', $path['path']));
176
+            }
177
+        }
178
+
179
+        // set the right include path
180
+        set_include_path(
181
+            implode(PATH_SEPARATOR, $paths)
182
+        );
183
+    }
184
+
185
+    public static function checkConfig(): void {
186
+        // Create config if it does not already exist
187
+        $configFilePath = self::$configDir . '/config.php';
188
+        if (!file_exists($configFilePath)) {
189
+            @touch($configFilePath);
190
+        }
191
+
192
+        // Check if config is writable
193
+        $configFileWritable = is_writable($configFilePath);
194
+        $configReadOnly = Server::get(IConfig::class)->getSystemValueBool('config_is_read_only');
195
+        if (!$configFileWritable && !$configReadOnly
196
+            || !$configFileWritable && \OCP\Util::needUpgrade()) {
197
+            $urlGenerator = Server::get(IURLGenerator::class);
198
+            $l = Server::get(\OCP\L10N\IFactory::class)->get('lib');
199
+
200
+            if (self::$CLI) {
201
+                echo $l->t('Cannot write into "config" directory!') . "\n";
202
+                echo $l->t('This can usually be fixed by giving the web server write access to the config directory.') . "\n";
203
+                echo "\n";
204
+                echo $l->t('But, if you prefer to keep config.php file read only, set the option "config_is_read_only" to true in it.') . "\n";
205
+                echo $l->t('See %s', [ $urlGenerator->linkToDocs('admin-config') ]) . "\n";
206
+                exit;
207
+            } else {
208
+                Server::get(ITemplateManager::class)->printErrorPage(
209
+                    $l->t('Cannot write into "config" directory!'),
210
+                    $l->t('This can usually be fixed by giving the web server write access to the config directory.') . ' '
211
+                    . $l->t('But, if you prefer to keep config.php file read only, set the option "config_is_read_only" to true in it.') . ' '
212
+                    . $l->t('See %s', [ $urlGenerator->linkToDocs('admin-config') ]),
213
+                    503
214
+                );
215
+            }
216
+        }
217
+    }
218
+
219
+    public static function checkInstalled(\OC\SystemConfig $systemConfig): void {
220
+        if (defined('OC_CONSOLE')) {
221
+            return;
222
+        }
223
+        // Redirect to installer if not installed
224
+        if (!$systemConfig->getValue('installed', false) && OC::$SUBURI !== '/index.php' && OC::$SUBURI !== '/status.php') {
225
+            if (OC::$CLI) {
226
+                throw new Exception('Not installed');
227
+            } else {
228
+                $url = OC::$WEBROOT . '/index.php';
229
+                header('Location: ' . $url);
230
+            }
231
+            exit();
232
+        }
233
+    }
234
+
235
+    public static function checkMaintenanceMode(\OC\SystemConfig $systemConfig): void {
236
+        // Allow ajax update script to execute without being stopped
237
+        if (((bool)$systemConfig->getValue('maintenance', false)) && OC::$SUBURI != '/core/ajax/update.php') {
238
+            // send http status 503
239
+            http_response_code(503);
240
+            header('X-Nextcloud-Maintenance-Mode: 1');
241
+            header('Retry-After: 120');
242
+
243
+            // render error page
244
+            $template = Server::get(ITemplateManager::class)->getTemplate('', 'update.user', 'guest');
245
+            \OCP\Util::addScript('core', 'maintenance');
246
+            \OCP\Util::addStyle('core', 'guest');
247
+            $template->printPage();
248
+            die();
249
+        }
250
+    }
251
+
252
+    /**
253
+     * Prints the upgrade page
254
+     */
255
+    private static function printUpgradePage(\OC\SystemConfig $systemConfig): void {
256
+        $cliUpgradeLink = $systemConfig->getValue('upgrade.cli-upgrade-link', '');
257
+        $disableWebUpdater = $systemConfig->getValue('upgrade.disable-web', false);
258
+        $tooBig = false;
259
+        if (!$disableWebUpdater) {
260
+            $apps = Server::get(\OCP\App\IAppManager::class);
261
+            if ($apps->isEnabledForAnyone('user_ldap')) {
262
+                $qb = Server::get(\OCP\IDBConnection::class)->getQueryBuilder();
263
+
264
+                $result = $qb->select($qb->func()->count('*', 'user_count'))
265
+                    ->from('ldap_user_mapping')
266
+                    ->executeQuery();
267
+                $row = $result->fetch();
268
+                $result->closeCursor();
269
+
270
+                $tooBig = ($row['user_count'] > 50);
271
+            }
272
+            if (!$tooBig && $apps->isEnabledForAnyone('user_saml')) {
273
+                $qb = Server::get(\OCP\IDBConnection::class)->getQueryBuilder();
274
+
275
+                $result = $qb->select($qb->func()->count('*', 'user_count'))
276
+                    ->from('user_saml_users')
277
+                    ->executeQuery();
278
+                $row = $result->fetch();
279
+                $result->closeCursor();
280
+
281
+                $tooBig = ($row['user_count'] > 50);
282
+            }
283
+            if (!$tooBig) {
284
+                // count users
285
+                $totalUsers = Server::get(\OCP\IUserManager::class)->countUsersTotal(51);
286
+                $tooBig = ($totalUsers > 50);
287
+            }
288
+        }
289
+        $ignoreTooBigWarning = isset($_GET['IKnowThatThisIsABigInstanceAndTheUpdateRequestCouldRunIntoATimeoutAndHowToRestoreABackup'])
290
+            && $_GET['IKnowThatThisIsABigInstanceAndTheUpdateRequestCouldRunIntoATimeoutAndHowToRestoreABackup'] === 'IAmSuperSureToDoThis';
291
+
292
+        if ($disableWebUpdater || ($tooBig && !$ignoreTooBigWarning)) {
293
+            // send http status 503
294
+            http_response_code(503);
295
+            header('Retry-After: 120');
296
+
297
+            $serverVersion = \OCP\Server::get(\OCP\ServerVersion::class);
298
+
299
+            // render error page
300
+            $template = Server::get(ITemplateManager::class)->getTemplate('', 'update.use-cli', 'guest');
301
+            $template->assign('productName', 'nextcloud'); // for now
302
+            $template->assign('version', $serverVersion->getVersionString());
303
+            $template->assign('tooBig', $tooBig);
304
+            $template->assign('cliUpgradeLink', $cliUpgradeLink);
305
+
306
+            $template->printPage();
307
+            die();
308
+        }
309
+
310
+        // check whether this is a core update or apps update
311
+        $installedVersion = $systemConfig->getValue('version', '0.0.0');
312
+        $currentVersion = implode('.', \OCP\Util::getVersion());
313
+
314
+        // if not a core upgrade, then it's apps upgrade
315
+        $isAppsOnlyUpgrade = version_compare($currentVersion, $installedVersion, '=');
316
+
317
+        $oldTheme = $systemConfig->getValue('theme');
318
+        $systemConfig->setValue('theme', '');
319
+        \OCP\Util::addScript('core', 'common');
320
+        \OCP\Util::addScript('core', 'main');
321
+        \OCP\Util::addTranslations('core');
322
+        \OCP\Util::addScript('core', 'update');
323
+
324
+        /** @var \OC\App\AppManager $appManager */
325
+        $appManager = Server::get(\OCP\App\IAppManager::class);
326
+
327
+        $tmpl = Server::get(ITemplateManager::class)->getTemplate('', 'update.admin', 'guest');
328
+        $tmpl->assign('version', \OCP\Server::get(\OCP\ServerVersion::class)->getVersionString());
329
+        $tmpl->assign('isAppsOnlyUpgrade', $isAppsOnlyUpgrade);
330
+
331
+        // get third party apps
332
+        $ocVersion = \OCP\Util::getVersion();
333
+        $ocVersion = implode('.', $ocVersion);
334
+        $incompatibleApps = $appManager->getIncompatibleApps($ocVersion);
335
+        $incompatibleOverwrites = $systemConfig->getValue('app_install_overwrite', []);
336
+        $incompatibleShippedApps = [];
337
+        $incompatibleDisabledApps = [];
338
+        foreach ($incompatibleApps as $appInfo) {
339
+            if ($appManager->isShipped($appInfo['id'])) {
340
+                $incompatibleShippedApps[] = $appInfo['name'] . ' (' . $appInfo['id'] . ')';
341
+            }
342
+            if (!in_array($appInfo['id'], $incompatibleOverwrites)) {
343
+                $incompatibleDisabledApps[] = $appInfo;
344
+            }
345
+        }
346
+
347
+        if (!empty($incompatibleShippedApps)) {
348
+            $l = Server::get(\OCP\L10N\IFactory::class)->get('core');
349
+            $hint = $l->t('Application %1$s is not present or has a non-compatible version with this server. Please check the apps directory.', [implode(', ', $incompatibleShippedApps)]);
350
+            throw new \OCP\HintException('Application ' . implode(', ', $incompatibleShippedApps) . ' is not present or has a non-compatible version with this server. Please check the apps directory.', $hint);
351
+        }
352
+
353
+        $tmpl->assign('appsToUpgrade', $appManager->getAppsNeedingUpgrade($ocVersion));
354
+        $tmpl->assign('incompatibleAppsList', $incompatibleDisabledApps);
355
+        try {
356
+            $defaults = new \OC_Defaults();
357
+            $tmpl->assign('productName', $defaults->getName());
358
+        } catch (Throwable $error) {
359
+            $tmpl->assign('productName', 'Nextcloud');
360
+        }
361
+        $tmpl->assign('oldTheme', $oldTheme);
362
+        $tmpl->printPage();
363
+    }
364
+
365
+    public static function initSession(): void {
366
+        $request = Server::get(IRequest::class);
367
+
368
+        // TODO: Temporary disabled again to solve issues with CalDAV/CardDAV clients like DAVx5 that use cookies
369
+        // TODO: See https://github.com/nextcloud/server/issues/37277#issuecomment-1476366147 and the other comments
370
+        // TODO: for further information.
371
+        // $isDavRequest = strpos($request->getRequestUri(), '/remote.php/dav') === 0 || strpos($request->getRequestUri(), '/remote.php/webdav') === 0;
372
+        // if ($request->getHeader('Authorization') !== '' && is_null($request->getCookie('cookie_test')) && $isDavRequest && !isset($_COOKIE['nc_session_id'])) {
373
+        // setcookie('cookie_test', 'test', time() + 3600);
374
+        // // Do not initialize the session if a request is authenticated directly
375
+        // // unless there is a session cookie already sent along
376
+        // return;
377
+        // }
378
+
379
+        if ($request->getServerProtocol() === 'https') {
380
+            ini_set('session.cookie_secure', 'true');
381
+        }
382
+
383
+        // prevents javascript from accessing php session cookies
384
+        ini_set('session.cookie_httponly', 'true');
385
+
386
+        // Do not initialize sessions for 'status.php' requests
387
+        // Monitoring endpoints can quickly flood session handlers
388
+        // and 'status.php' doesn't require sessions anyway
389
+        if (str_ends_with($request->getScriptName(), '/status.php')) {
390
+            return;
391
+        }
392
+
393
+        // set the cookie path to the Nextcloud directory
394
+        $cookie_path = OC::$WEBROOT ? : '/';
395
+        ini_set('session.cookie_path', $cookie_path);
396
+
397
+        // set the cookie domain to the Nextcloud domain
398
+        $cookie_domain = self::$config->getValue('cookie_domain', '');
399
+        if ($cookie_domain) {
400
+            ini_set('session.cookie_domain', $cookie_domain);
401
+        }
402
+
403
+        // Let the session name be changed in the initSession Hook
404
+        $sessionName = OC_Util::getInstanceId();
405
+
406
+        try {
407
+            $logger = null;
408
+            if (Server::get(\OC\SystemConfig::class)->getValue('installed', false)) {
409
+                $logger = logger('core');
410
+            }
411
+
412
+            // set the session name to the instance id - which is unique
413
+            $session = new \OC\Session\Internal(
414
+                $sessionName,
415
+                $logger,
416
+            );
417
+
418
+            $cryptoWrapper = Server::get(\OC\Session\CryptoWrapper::class);
419
+            $session = $cryptoWrapper->wrapSession($session);
420
+            self::$server->setSession($session);
421
+
422
+            // if session can't be started break with http 500 error
423
+        } catch (Exception $e) {
424
+            Server::get(LoggerInterface::class)->error($e->getMessage(), ['app' => 'base','exception' => $e]);
425
+            //show the user a detailed error page
426
+            Server::get(ITemplateManager::class)->printExceptionErrorPage($e, 500);
427
+            die();
428
+        }
429
+
430
+        //try to set the session lifetime
431
+        $sessionLifeTime = self::getSessionLifeTime();
432
+
433
+        // session timeout
434
+        if ($session->exists('LAST_ACTIVITY') && (time() - $session->get('LAST_ACTIVITY') > $sessionLifeTime)) {
435
+            if (isset($_COOKIE[session_name()])) {
436
+                setcookie(session_name(), '', -1, self::$WEBROOT ? : '/');
437
+            }
438
+            Server::get(IUserSession::class)->logout();
439
+        }
440
+
441
+        if (!self::hasSessionRelaxedExpiry()) {
442
+            $session->set('LAST_ACTIVITY', time());
443
+        }
444
+        $session->close();
445
+    }
446
+
447
+    private static function getSessionLifeTime(): int {
448
+        return Server::get(\OC\AllConfig::class)->getSystemValueInt('session_lifetime', 60 * 60 * 24);
449
+    }
450
+
451
+    /**
452
+     * @return bool true if the session expiry should only be done by gc instead of an explicit timeout
453
+     */
454
+    public static function hasSessionRelaxedExpiry(): bool {
455
+        return Server::get(\OC\AllConfig::class)->getSystemValueBool('session_relaxed_expiry', false);
456
+    }
457
+
458
+    /**
459
+     * Try to set some values to the required Nextcloud default
460
+     */
461
+    public static function setRequiredIniValues(): void {
462
+        // Don't display errors and log them
463
+        @ini_set('display_errors', '0');
464
+        @ini_set('log_errors', '1');
465
+
466
+        // Try to configure php to enable big file uploads.
467
+        // This doesn't work always depending on the webserver and php configuration.
468
+        // Let's try to overwrite some defaults if they are smaller than 1 hour
469
+
470
+        if (intval(@ini_get('max_execution_time') ?: 0) < 3600) {
471
+            @ini_set('max_execution_time', strval(3600));
472
+        }
473
+
474
+        if (intval(@ini_get('max_input_time') ?: 0) < 3600) {
475
+            @ini_set('max_input_time', strval(3600));
476
+        }
477
+
478
+        // Try to set the maximum execution time to the largest time limit we have
479
+        if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) {
480
+            @set_time_limit(max(intval(@ini_get('max_execution_time')), intval(@ini_get('max_input_time'))));
481
+        }
482
+
483
+        @ini_set('default_charset', 'UTF-8');
484
+        @ini_set('gd.jpeg_ignore_warning', '1');
485
+    }
486
+
487
+    /**
488
+     * Send the same site cookies
489
+     */
490
+    private static function sendSameSiteCookies(): void {
491
+        $cookieParams = session_get_cookie_params();
492
+        $secureCookie = ($cookieParams['secure'] === true) ? 'secure; ' : '';
493
+        $policies = [
494
+            'lax',
495
+            'strict',
496
+        ];
497
+
498
+        // Append __Host to the cookie if it meets the requirements
499
+        $cookiePrefix = '';
500
+        if ($cookieParams['secure'] === true && $cookieParams['path'] === '/') {
501
+            $cookiePrefix = '__Host-';
502
+        }
503
+
504
+        foreach ($policies as $policy) {
505
+            header(
506
+                sprintf(
507
+                    'Set-Cookie: %snc_sameSiteCookie%s=true; path=%s; httponly;' . $secureCookie . 'expires=Fri, 31-Dec-2100 23:59:59 GMT; SameSite=%s',
508
+                    $cookiePrefix,
509
+                    $policy,
510
+                    $cookieParams['path'],
511
+                    $policy
512
+                ),
513
+                false
514
+            );
515
+        }
516
+    }
517
+
518
+    /**
519
+     * Same Site cookie to further mitigate CSRF attacks. This cookie has to
520
+     * be set in every request if cookies are sent to add a second level of
521
+     * defense against CSRF.
522
+     *
523
+     * If the cookie is not sent this will set the cookie and reload the page.
524
+     * We use an additional cookie since we want to protect logout CSRF and
525
+     * also we can't directly interfere with PHP's session mechanism.
526
+     */
527
+    private static function performSameSiteCookieProtection(IConfig $config): void {
528
+        $request = Server::get(IRequest::class);
529
+
530
+        // Some user agents are notorious and don't really properly follow HTTP
531
+        // specifications. For those, have an automated opt-out. Since the protection
532
+        // for remote.php is applied in base.php as starting point we need to opt out
533
+        // here.
534
+        $incompatibleUserAgents = $config->getSystemValue('csrf.optout');
535
+
536
+        // Fallback, if csrf.optout is unset
537
+        if (!is_array($incompatibleUserAgents)) {
538
+            $incompatibleUserAgents = [
539
+                // OS X Finder
540
+                '/^WebDAVFS/',
541
+                // Windows webdav drive
542
+                '/^Microsoft-WebDAV-MiniRedir/',
543
+            ];
544
+        }
545
+
546
+        if ($request->isUserAgent($incompatibleUserAgents)) {
547
+            return;
548
+        }
549
+
550
+        if (count($_COOKIE) > 0) {
551
+            $requestUri = $request->getScriptName();
552
+            $processingScript = explode('/', $requestUri);
553
+            $processingScript = $processingScript[count($processingScript) - 1];
554
+
555
+            if ($processingScript === 'index.php' // index.php routes are handled in the middleware
556
+                || $processingScript === 'cron.php' // and cron.php does not need any authentication at all
557
+                || $processingScript === 'public.php' // For public.php, auth for password protected shares is done in the PublicAuth plugin
558
+            ) {
559
+                return;
560
+            }
561
+
562
+            // All other endpoints require the lax and the strict cookie
563
+            if (!$request->passesStrictCookieCheck()) {
564
+                logger('core')->warning('Request does not pass strict cookie check');
565
+                self::sendSameSiteCookies();
566
+                // Debug mode gets access to the resources without strict cookie
567
+                // due to the fact that the SabreDAV browser also lives there.
568
+                if (!$config->getSystemValueBool('debug', false)) {
569
+                    http_response_code(\OCP\AppFramework\Http::STATUS_PRECONDITION_FAILED);
570
+                    header('Content-Type: application/json');
571
+                    echo json_encode(['error' => 'Strict Cookie has not been found in request']);
572
+                    exit();
573
+                }
574
+            }
575
+        } elseif (!isset($_COOKIE['nc_sameSiteCookielax']) || !isset($_COOKIE['nc_sameSiteCookiestrict'])) {
576
+            self::sendSameSiteCookies();
577
+        }
578
+    }
579
+
580
+    public static function init(): void {
581
+        // First handle PHP configuration and copy auth headers to the expected
582
+        // $_SERVER variable before doing anything Server object related
583
+        self::setRequiredIniValues();
584
+        self::handleAuthHeaders();
585
+
586
+        // prevent any XML processing from loading external entities
587
+        libxml_set_external_entity_loader(static function () {
588
+            return null;
589
+        });
590
+
591
+        // Set default timezone before the Server object is booted
592
+        if (!date_default_timezone_set('UTC')) {
593
+            throw new \RuntimeException('Could not set timezone to UTC');
594
+        }
595
+
596
+        // calculate the root directories
597
+        OC::$SERVERROOT = str_replace('\\', '/', substr(__DIR__, 0, -4));
598
+
599
+        // register autoloader
600
+        $loaderStart = microtime(true);
601
+
602
+        self::$CLI = (php_sapi_name() == 'cli');
603
+
604
+        // Add default composer PSR-4 autoloader, ensure apcu to be disabled
605
+        self::$composerAutoloader = require_once OC::$SERVERROOT . '/lib/composer/autoload.php';
606
+        self::$composerAutoloader->setApcuPrefix(null);
607
+
608
+
609
+        try {
610
+            self::initPaths();
611
+            // setup 3rdparty autoloader
612
+            $vendorAutoLoad = OC::$SERVERROOT . '/3rdparty/autoload.php';
613
+            if (!file_exists($vendorAutoLoad)) {
614
+                throw new \RuntimeException('Composer autoloader not found, unable to continue. Check the folder "3rdparty". Running "git submodule update --init" will initialize the git submodule that handles the subfolder "3rdparty".');
615
+            }
616
+            require_once $vendorAutoLoad;
617
+        } catch (\RuntimeException $e) {
618
+            if (!self::$CLI) {
619
+                http_response_code(503);
620
+            }
621
+            // we can't use the template error page here, because this needs the
622
+            // DI container which isn't available yet
623
+            print($e->getMessage());
624
+            exit();
625
+        }
626
+        $loaderEnd = microtime(true);
627
+
628
+        // Enable lazy loading if activated
629
+        \OC\AppFramework\Utility\SimpleContainer::$useLazyObjects = (bool)self::$config->getValue('enable_lazy_objects', true);
630
+
631
+        // setup the basic server
632
+        self::$server = new \OC\Server(\OC::$WEBROOT, self::$config);
633
+        self::$server->boot();
634
+
635
+        try {
636
+            $profiler = new BuiltInProfiler(
637
+                Server::get(IConfig::class),
638
+                Server::get(IRequest::class),
639
+            );
640
+            $profiler->start();
641
+        } catch (\Throwable $e) {
642
+            logger('core')->error('Failed to start profiler: ' . $e->getMessage(), ['app' => 'base']);
643
+        }
644
+
645
+        if (self::$CLI && in_array('--' . \OCP\Console\ReservedOptions::DEBUG_LOG, $_SERVER['argv'])) {
646
+            \OC\Core\Listener\BeforeMessageLoggedEventListener::setup();
647
+        }
648
+
649
+        $eventLogger = Server::get(\OCP\Diagnostics\IEventLogger::class);
650
+        $eventLogger->log('autoloader', 'Autoloader', $loaderStart, $loaderEnd);
651
+        $eventLogger->start('boot', 'Initialize');
652
+
653
+        // Override php.ini and log everything if we're troubleshooting
654
+        if (self::$config->getValue('loglevel') === ILogger::DEBUG) {
655
+            error_reporting(E_ALL);
656
+        }
657
+
658
+        // initialize intl fallback if necessary
659
+        OC_Util::isSetLocaleWorking();
660
+
661
+        $config = Server::get(IConfig::class);
662
+        if (!defined('PHPUNIT_RUN')) {
663
+            $errorHandler = new OC\Log\ErrorHandler(
664
+                \OCP\Server::get(\Psr\Log\LoggerInterface::class),
665
+            );
666
+            $exceptionHandler = [$errorHandler, 'onException'];
667
+            if ($config->getSystemValueBool('debug', false)) {
668
+                set_error_handler([$errorHandler, 'onAll'], E_ALL);
669
+                if (\OC::$CLI) {
670
+                    $exceptionHandler = [Server::get(ITemplateManager::class), 'printExceptionErrorPage'];
671
+                }
672
+            } else {
673
+                set_error_handler([$errorHandler, 'onError']);
674
+            }
675
+            register_shutdown_function([$errorHandler, 'onShutdown']);
676
+            set_exception_handler($exceptionHandler);
677
+        }
678
+
679
+        /** @var \OC\AppFramework\Bootstrap\Coordinator $bootstrapCoordinator */
680
+        $bootstrapCoordinator = Server::get(\OC\AppFramework\Bootstrap\Coordinator::class);
681
+        $bootstrapCoordinator->runInitialRegistration();
682
+
683
+        $eventLogger->start('init_session', 'Initialize session');
684
+
685
+        // Check for PHP SimpleXML extension earlier since we need it before our other checks and want to provide a useful hint for web users
686
+        // see https://github.com/nextcloud/server/pull/2619
687
+        if (!function_exists('simplexml_load_file')) {
688
+            throw new \OCP\HintException('The PHP SimpleXML/PHP-XML extension is not installed.', 'Install the extension or make sure it is enabled.');
689
+        }
690
+
691
+        $systemConfig = Server::get(\OC\SystemConfig::class);
692
+        $appManager = Server::get(\OCP\App\IAppManager::class);
693
+        if ($systemConfig->getValue('installed', false)) {
694
+            $appManager->loadApps(['session']);
695
+        }
696
+        if (!self::$CLI) {
697
+            self::initSession();
698
+        }
699
+        $eventLogger->end('init_session');
700
+        self::checkConfig();
701
+        self::checkInstalled($systemConfig);
702
+
703
+        OC_Response::addSecurityHeaders();
704
+
705
+        self::performSameSiteCookieProtection($config);
706
+
707
+        if (!defined('OC_CONSOLE')) {
708
+            $eventLogger->start('check_server', 'Run a few configuration checks');
709
+            $errors = OC_Util::checkServer($systemConfig);
710
+            if (count($errors) > 0) {
711
+                if (!self::$CLI) {
712
+                    http_response_code(503);
713
+                    Util::addStyle('guest');
714
+                    try {
715
+                        Server::get(ITemplateManager::class)->printGuestPage('', 'error', ['errors' => $errors]);
716
+                        exit;
717
+                    } catch (\Exception $e) {
718
+                        // In case any error happens when showing the error page, we simply fall back to posting the text.
719
+                        // This might be the case when e.g. the data directory is broken and we can not load/write SCSS to/from it.
720
+                    }
721
+                }
722
+
723
+                // Convert l10n string into regular string for usage in database
724
+                $staticErrors = [];
725
+                foreach ($errors as $error) {
726
+                    echo $error['error'] . "\n";
727
+                    echo $error['hint'] . "\n\n";
728
+                    $staticErrors[] = [
729
+                        'error' => (string)$error['error'],
730
+                        'hint' => (string)$error['hint'],
731
+                    ];
732
+                }
733
+
734
+                try {
735
+                    $config->setAppValue('core', 'cronErrors', json_encode($staticErrors));
736
+                } catch (\Exception $e) {
737
+                    echo('Writing to database failed');
738
+                }
739
+                exit(1);
740
+            } elseif (self::$CLI && $config->getSystemValueBool('installed', false)) {
741
+                $config->deleteAppValue('core', 'cronErrors');
742
+            }
743
+            $eventLogger->end('check_server');
744
+        }
745
+
746
+        // User and Groups
747
+        if (!$systemConfig->getValue('installed', false)) {
748
+            self::$server->getSession()->set('user_id', '');
749
+        }
750
+
751
+        $eventLogger->start('setup_backends', 'Setup group and user backends');
752
+        Server::get(\OCP\IUserManager::class)->registerBackend(new \OC\User\Database());
753
+        Server::get(\OCP\IGroupManager::class)->addBackend(new \OC\Group\Database());
754
+
755
+        // Subscribe to the hook
756
+        \OCP\Util::connectHook(
757
+            '\OCA\Files_Sharing\API\Server2Server',
758
+            'preLoginNameUsedAsUserName',
759
+            '\OC\User\Database',
760
+            'preLoginNameUsedAsUserName'
761
+        );
762
+
763
+        //setup extra user backends
764
+        if (!\OCP\Util::needUpgrade()) {
765
+            OC_User::setupBackends();
766
+        } else {
767
+            // Run upgrades in incognito mode
768
+            OC_User::setIncognitoMode(true);
769
+        }
770
+        $eventLogger->end('setup_backends');
771
+
772
+        self::registerCleanupHooks($systemConfig);
773
+        self::registerShareHooks($systemConfig);
774
+        self::registerEncryptionWrapperAndHooks();
775
+        self::registerAccountHooks();
776
+        self::registerResourceCollectionHooks();
777
+        self::registerFileReferenceEventListener();
778
+        self::registerRenderReferenceEventListener();
779
+        self::registerAppRestrictionsHooks();
780
+
781
+        // Make sure that the application class is not loaded before the database is setup
782
+        if ($systemConfig->getValue('installed', false)) {
783
+            $appManager->loadApp('settings');
784
+            /* Run core application registration */
785
+            $bootstrapCoordinator->runLazyRegistration('core');
786
+        }
787
+
788
+        //make sure temporary files are cleaned up
789
+        $tmpManager = Server::get(\OCP\ITempManager::class);
790
+        register_shutdown_function([$tmpManager, 'clean']);
791
+        $lockProvider = Server::get(\OCP\Lock\ILockingProvider::class);
792
+        register_shutdown_function([$lockProvider, 'releaseAll']);
793
+
794
+        // Check whether the sample configuration has been copied
795
+        if ($systemConfig->getValue('copied_sample_config', false)) {
796
+            $l = Server::get(\OCP\L10N\IFactory::class)->get('lib');
797
+            Server::get(ITemplateManager::class)->printErrorPage(
798
+                $l->t('Sample configuration detected'),
799
+                $l->t('It has been detected that the sample configuration has been copied. This can break your installation and is unsupported. Please read the documentation before performing changes on config.php'),
800
+                503
801
+            );
802
+            return;
803
+        }
804
+
805
+        $request = Server::get(IRequest::class);
806
+        $host = $request->getInsecureServerHost();
807
+        /**
808
+         * if the host passed in headers isn't trusted
809
+         * FIXME: Should not be in here at all :see_no_evil:
810
+         */
811
+        if (!OC::$CLI
812
+            && !Server::get(\OC\Security\TrustedDomainHelper::class)->isTrustedDomain($host)
813
+            && $config->getSystemValueBool('installed', false)
814
+        ) {
815
+            // Allow access to CSS resources
816
+            $isScssRequest = false;
817
+            if (strpos($request->getPathInfo() ?: '', '/css/') === 0) {
818
+                $isScssRequest = true;
819
+            }
820
+
821
+            if (substr($request->getRequestUri(), -11) === '/status.php') {
822
+                http_response_code(400);
823
+                header('Content-Type: application/json');
824
+                echo '{"error": "Trusted domain error.", "code": 15}';
825
+                exit();
826
+            }
827
+
828
+            if (!$isScssRequest) {
829
+                http_response_code(400);
830
+                Server::get(LoggerInterface::class)->info(
831
+                    'Trusted domain error. "{remoteAddress}" tried to access using "{host}" as host.',
832
+                    [
833
+                        'app' => 'core',
834
+                        'remoteAddress' => $request->getRemoteAddress(),
835
+                        'host' => $host,
836
+                    ]
837
+                );
838
+
839
+                $tmpl = Server::get(ITemplateManager::class)->getTemplate('core', 'untrustedDomain', 'guest');
840
+                $tmpl->assign('docUrl', Server::get(IURLGenerator::class)->linkToDocs('admin-trusted-domains'));
841
+                $tmpl->printPage();
842
+
843
+                exit();
844
+            }
845
+        }
846
+        $eventLogger->end('boot');
847
+        $eventLogger->log('init', 'OC::init', $loaderStart, microtime(true));
848
+        $eventLogger->start('runtime', 'Runtime');
849
+        $eventLogger->start('request', 'Full request after boot');
850
+        register_shutdown_function(function () use ($eventLogger) {
851
+            $eventLogger->end('request');
852
+        });
853
+
854
+        register_shutdown_function(function () {
855
+            $memoryPeak = memory_get_peak_usage();
856
+            $logLevel = match (true) {
857
+                $memoryPeak > 500_000_000 => ILogger::FATAL,
858
+                $memoryPeak > 400_000_000 => ILogger::ERROR,
859
+                $memoryPeak > 300_000_000 => ILogger::WARN,
860
+                default => null,
861
+            };
862
+            if ($logLevel !== null) {
863
+                $message = 'Request used more than 300 MB of RAM: ' . Util::humanFileSize($memoryPeak);
864
+                $logger = Server::get(LoggerInterface::class);
865
+                $logger->log($logLevel, $message, ['app' => 'core']);
866
+            }
867
+        });
868
+    }
869
+
870
+    /**
871
+     * register hooks for the cleanup of cache and bruteforce protection
872
+     */
873
+    public static function registerCleanupHooks(\OC\SystemConfig $systemConfig): void {
874
+        //don't try to do this before we are properly setup
875
+        if ($systemConfig->getValue('installed', false) && !\OCP\Util::needUpgrade()) {
876
+            // NOTE: This will be replaced to use OCP
877
+            $userSession = Server::get(\OC\User\Session::class);
878
+            $userSession->listen('\OC\User', 'postLogin', function () use ($userSession) {
879
+                if (!defined('PHPUNIT_RUN') && $userSession->isLoggedIn()) {
880
+                    // reset brute force delay for this IP address and username
881
+                    $uid = $userSession->getUser()->getUID();
882
+                    $request = Server::get(IRequest::class);
883
+                    $throttler = Server::get(IThrottler::class);
884
+                    $throttler->resetDelay($request->getRemoteAddress(), 'login', ['user' => $uid]);
885
+                }
886
+
887
+                try {
888
+                    $cache = new \OC\Cache\File();
889
+                    $cache->gc();
890
+                } catch (\OC\ServerNotAvailableException $e) {
891
+                    // not a GC exception, pass it on
892
+                    throw $e;
893
+                } catch (\OC\ForbiddenException $e) {
894
+                    // filesystem blocked for this request, ignore
895
+                } catch (\Exception $e) {
896
+                    // a GC exception should not prevent users from using OC,
897
+                    // so log the exception
898
+                    Server::get(LoggerInterface::class)->warning('Exception when running cache gc.', [
899
+                        'app' => 'core',
900
+                        'exception' => $e,
901
+                    ]);
902
+                }
903
+            });
904
+        }
905
+    }
906
+
907
+    private static function registerEncryptionWrapperAndHooks(): void {
908
+        /** @var \OC\Encryption\Manager */
909
+        $manager = Server::get(\OCP\Encryption\IManager::class);
910
+        Server::get(IEventDispatcher::class)->addListener(
911
+            BeforeFileSystemSetupEvent::class,
912
+            $manager->setupStorage(...),
913
+        );
914
+
915
+        $enabled = $manager->isEnabled();
916
+        if ($enabled) {
917
+            \OC\Encryption\EncryptionEventListener::register(Server::get(IEventDispatcher::class));
918
+        }
919
+    }
920
+
921
+    private static function registerAccountHooks(): void {
922
+        /** @var IEventDispatcher $dispatcher */
923
+        $dispatcher = Server::get(IEventDispatcher::class);
924
+        $dispatcher->addServiceListener(UserChangedEvent::class, \OC\Accounts\Hooks::class);
925
+    }
926
+
927
+    private static function registerAppRestrictionsHooks(): void {
928
+        /** @var \OC\Group\Manager $groupManager */
929
+        $groupManager = Server::get(\OCP\IGroupManager::class);
930
+        $groupManager->listen('\OC\Group', 'postDelete', function (\OCP\IGroup $group) {
931
+            $appManager = Server::get(\OCP\App\IAppManager::class);
932
+            $apps = $appManager->getEnabledAppsForGroup($group);
933
+            foreach ($apps as $appId) {
934
+                $restrictions = $appManager->getAppRestriction($appId);
935
+                if (empty($restrictions)) {
936
+                    continue;
937
+                }
938
+                $key = array_search($group->getGID(), $restrictions);
939
+                unset($restrictions[$key]);
940
+                $restrictions = array_values($restrictions);
941
+                if (empty($restrictions)) {
942
+                    $appManager->disableApp($appId);
943
+                } else {
944
+                    $appManager->enableAppForGroups($appId, $restrictions);
945
+                }
946
+            }
947
+        });
948
+    }
949
+
950
+    private static function registerResourceCollectionHooks(): void {
951
+        \OC\Collaboration\Resources\Listener::register(Server::get(IEventDispatcher::class));
952
+    }
953
+
954
+    private static function registerFileReferenceEventListener(): void {
955
+        \OC\Collaboration\Reference\File\FileReferenceEventListener::register(Server::get(IEventDispatcher::class));
956
+    }
957
+
958
+    private static function registerRenderReferenceEventListener() {
959
+        \OC\Collaboration\Reference\RenderReferenceEventListener::register(Server::get(IEventDispatcher::class));
960
+    }
961
+
962
+    /**
963
+     * register hooks for sharing
964
+     */
965
+    public static function registerShareHooks(\OC\SystemConfig $systemConfig): void {
966
+        if ($systemConfig->getValue('installed')) {
967
+
968
+            $dispatcher = Server::get(IEventDispatcher::class);
969
+            $dispatcher->addServiceListener(UserRemovedEvent::class, UserRemovedListener::class);
970
+            $dispatcher->addServiceListener(GroupDeletedEvent::class, GroupDeletedListener::class);
971
+            $dispatcher->addServiceListener(UserDeletedEvent::class, UserDeletedListener::class);
972
+        }
973
+    }
974
+
975
+    /**
976
+     * Handle the request
977
+     */
978
+    public static function handleRequest(): void {
979
+        Server::get(\OCP\Diagnostics\IEventLogger::class)->start('handle_request', 'Handle request');
980
+        $systemConfig = Server::get(\OC\SystemConfig::class);
981
+
982
+        // Check if Nextcloud is installed or in maintenance (update) mode
983
+        if (!$systemConfig->getValue('installed', false)) {
984
+            \OC::$server->getSession()->clear();
985
+            $controller = Server::get(\OC\Core\Controller\SetupController::class);
986
+            $controller->run($_POST);
987
+            exit();
988
+        }
989
+
990
+        $request = Server::get(IRequest::class);
991
+        $request->throwDecodingExceptionIfAny();
992
+        $requestPath = $request->getRawPathInfo();
993
+        if ($requestPath === '/heartbeat') {
994
+            return;
995
+        }
996
+        if (substr($requestPath, -3) !== '.js') { // we need these files during the upgrade
997
+            self::checkMaintenanceMode($systemConfig);
998
+
999
+            if (\OCP\Util::needUpgrade()) {
1000
+                if (function_exists('opcache_reset')) {
1001
+                    opcache_reset();
1002
+                }
1003
+                if (!((bool)$systemConfig->getValue('maintenance', false))) {
1004
+                    self::printUpgradePage($systemConfig);
1005
+                    exit();
1006
+                }
1007
+            }
1008
+        }
1009
+
1010
+        $appManager = Server::get(\OCP\App\IAppManager::class);
1011
+
1012
+        // Always load authentication apps
1013
+        $appManager->loadApps(['authentication']);
1014
+        $appManager->loadApps(['extended_authentication']);
1015
+
1016
+        // Load minimum set of apps
1017
+        if (!\OCP\Util::needUpgrade()
1018
+            && !((bool)$systemConfig->getValue('maintenance', false))) {
1019
+            // For logged-in users: Load everything
1020
+            if (Server::get(IUserSession::class)->isLoggedIn()) {
1021
+                $appManager->loadApps();
1022
+            } else {
1023
+                // For guests: Load only filesystem and logging
1024
+                $appManager->loadApps(['filesystem', 'logging']);
1025
+
1026
+                // Don't try to login when a client is trying to get a OAuth token.
1027
+                // OAuth needs to support basic auth too, so the login is not valid
1028
+                // inside Nextcloud and the Login exception would ruin it.
1029
+                if ($request->getRawPathInfo() !== '/apps/oauth2/api/v1/token') {
1030
+                    try {
1031
+                        self::handleLogin($request);
1032
+                    } catch (DisabledUserException $e) {
1033
+                        // Disabled users would not be seen as logged in and
1034
+                        // trying to log them in would fail, so the login
1035
+                        // exception is ignored for the themed stylesheets and
1036
+                        // images.
1037
+                        if ($request->getRawPathInfo() !== '/apps/theming/theme/default.css'
1038
+                            && $request->getRawPathInfo() !== '/apps/theming/theme/light.css'
1039
+                            && $request->getRawPathInfo() !== '/apps/theming/theme/dark.css'
1040
+                            && $request->getRawPathInfo() !== '/apps/theming/theme/light-highcontrast.css'
1041
+                            && $request->getRawPathInfo() !== '/apps/theming/theme/dark-highcontrast.css'
1042
+                            && $request->getRawPathInfo() !== '/apps/theming/theme/opendyslexic.css'
1043
+                            && $request->getRawPathInfo() !== '/apps/theming/image/background'
1044
+                            && $request->getRawPathInfo() !== '/apps/theming/image/logo'
1045
+                            && $request->getRawPathInfo() !== '/apps/theming/image/logoheader'
1046
+                            && !str_starts_with($request->getRawPathInfo(), '/apps/theming/favicon')
1047
+                            && !str_starts_with($request->getRawPathInfo(), '/apps/theming/icon')) {
1048
+                            throw $e;
1049
+                        }
1050
+                    }
1051
+                }
1052
+            }
1053
+        }
1054
+
1055
+        if (!self::$CLI) {
1056
+            try {
1057
+                if (!\OCP\Util::needUpgrade()) {
1058
+                    $appManager->loadApps(['filesystem', 'logging']);
1059
+                    $appManager->loadApps();
1060
+                }
1061
+                Server::get(\OC\Route\Router::class)->match($request->getRawPathInfo());
1062
+                return;
1063
+            } catch (Symfony\Component\Routing\Exception\ResourceNotFoundException $e) {
1064
+                //header('HTTP/1.0 404 Not Found');
1065
+            } catch (Symfony\Component\Routing\Exception\MethodNotAllowedException $e) {
1066
+                http_response_code(405);
1067
+                return;
1068
+            }
1069
+        }
1070
+
1071
+        // Handle WebDAV
1072
+        if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND') {
1073
+            // not allowed any more to prevent people
1074
+            // mounting this root directly.
1075
+            // Users need to mount remote.php/webdav instead.
1076
+            http_response_code(405);
1077
+            return;
1078
+        }
1079
+
1080
+        // Handle requests for JSON or XML
1081
+        $acceptHeader = $request->getHeader('Accept');
1082
+        if (in_array($acceptHeader, ['application/json', 'application/xml'], true)) {
1083
+            http_response_code(404);
1084
+            return;
1085
+        }
1086
+
1087
+        // Handle resources that can't be found
1088
+        // This prevents browsers from redirecting to the default page and then
1089
+        // attempting to parse HTML as CSS and similar.
1090
+        $destinationHeader = $request->getHeader('Sec-Fetch-Dest');
1091
+        if (in_array($destinationHeader, ['font', 'script', 'style'])) {
1092
+            http_response_code(404);
1093
+            return;
1094
+        }
1095
+
1096
+        // Redirect to the default app or login only as an entry point
1097
+        if ($requestPath === '') {
1098
+            // Someone is logged in
1099
+            if (Server::get(IUserSession::class)->isLoggedIn()) {
1100
+                header('Location: ' . Server::get(IURLGenerator::class)->linkToDefaultPageUrl());
1101
+            } else {
1102
+                // Not handled and not logged in
1103
+                header('Location: ' . Server::get(IURLGenerator::class)->linkToRouteAbsolute('core.login.showLoginForm'));
1104
+            }
1105
+            return;
1106
+        }
1107
+
1108
+        try {
1109
+            Server::get(\OC\Route\Router::class)->match('/error/404');
1110
+        } catch (\Exception $e) {
1111
+            if (!$e instanceof MethodNotAllowedException) {
1112
+                logger('core')->emergency($e->getMessage(), ['exception' => $e]);
1113
+            }
1114
+            $l = Server::get(\OCP\L10N\IFactory::class)->get('lib');
1115
+            Server::get(ITemplateManager::class)->printErrorPage(
1116
+                '404',
1117
+                $l->t('The page could not be found on the server.'),
1118
+                404
1119
+            );
1120
+        }
1121
+    }
1122
+
1123
+    /**
1124
+     * Check login: apache auth, auth token, basic auth
1125
+     */
1126
+    public static function handleLogin(OCP\IRequest $request): bool {
1127
+        if ($request->getHeader('X-Nextcloud-Federation')) {
1128
+            return false;
1129
+        }
1130
+        $userSession = Server::get(\OC\User\Session::class);
1131
+        if (OC_User::handleApacheAuth()) {
1132
+            return true;
1133
+        }
1134
+        if (self::tryAppAPILogin($request)) {
1135
+            return true;
1136
+        }
1137
+        if ($userSession->tryTokenLogin($request)) {
1138
+            return true;
1139
+        }
1140
+        if (isset($_COOKIE['nc_username'])
1141
+            && isset($_COOKIE['nc_token'])
1142
+            && isset($_COOKIE['nc_session_id'])
1143
+            && $userSession->loginWithCookie($_COOKIE['nc_username'], $_COOKIE['nc_token'], $_COOKIE['nc_session_id'])) {
1144
+            return true;
1145
+        }
1146
+        if ($userSession->tryBasicAuthLogin($request, Server::get(IThrottler::class))) {
1147
+            return true;
1148
+        }
1149
+        return false;
1150
+    }
1151
+
1152
+    protected static function handleAuthHeaders(): void {
1153
+        //copy http auth headers for apache+php-fcgid work around
1154
+        if (isset($_SERVER['HTTP_XAUTHORIZATION']) && !isset($_SERVER['HTTP_AUTHORIZATION'])) {
1155
+            $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['HTTP_XAUTHORIZATION'];
1156
+        }
1157
+
1158
+        // Extract PHP_AUTH_USER/PHP_AUTH_PW from other headers if necessary.
1159
+        $vars = [
1160
+            'HTTP_AUTHORIZATION', // apache+php-cgi work around
1161
+            'REDIRECT_HTTP_AUTHORIZATION', // apache+php-cgi alternative
1162
+        ];
1163
+        foreach ($vars as $var) {
1164
+            if (isset($_SERVER[$var]) && is_string($_SERVER[$var]) && preg_match('/Basic\s+(.*)$/i', $_SERVER[$var], $matches)) {
1165
+                $credentials = explode(':', base64_decode($matches[1]), 2);
1166
+                if (count($credentials) === 2) {
1167
+                    $_SERVER['PHP_AUTH_USER'] = $credentials[0];
1168
+                    $_SERVER['PHP_AUTH_PW'] = $credentials[1];
1169
+                    break;
1170
+                }
1171
+            }
1172
+        }
1173
+    }
1174
+
1175
+    protected static function tryAppAPILogin(OCP\IRequest $request): bool {
1176
+        if (!$request->getHeader('AUTHORIZATION-APP-API')) {
1177
+            return false;
1178
+        }
1179
+        $appManager = Server::get(OCP\App\IAppManager::class);
1180
+        if (!$appManager->isEnabledForAnyone('app_api')) {
1181
+            return false;
1182
+        }
1183
+        try {
1184
+            $appAPIService = Server::get(OCA\AppAPI\Service\AppAPIService::class);
1185
+            return $appAPIService->validateExAppRequestToNC($request);
1186
+        } catch (\Psr\Container\NotFoundExceptionInterface|\Psr\Container\ContainerExceptionInterface $e) {
1187
+            return false;
1188
+        }
1189
+    }
1190 1190
 }
1191 1191
 
1192 1192
 OC::init();
Please login to merge, or discard this patch.