@@ -50,360 +50,360 @@ |
||
50 | 50 | use function array_filter; |
51 | 51 | |
52 | 52 | class Manager { |
53 | - public const SESSION_UID_KEY = 'two_factor_auth_uid'; |
|
54 | - public const SESSION_UID_DONE = 'two_factor_auth_passed'; |
|
55 | - public const REMEMBER_LOGIN = 'two_factor_remember_login'; |
|
56 | - public const BACKUP_CODES_PROVIDER_ID = 'backup_codes'; |
|
57 | - |
|
58 | - /** @var ProviderLoader */ |
|
59 | - private $providerLoader; |
|
60 | - |
|
61 | - /** @var IRegistry */ |
|
62 | - private $providerRegistry; |
|
63 | - |
|
64 | - /** @var MandatoryTwoFactor */ |
|
65 | - private $mandatoryTwoFactor; |
|
66 | - |
|
67 | - /** @var ISession */ |
|
68 | - private $session; |
|
69 | - |
|
70 | - /** @var IConfig */ |
|
71 | - private $config; |
|
72 | - |
|
73 | - /** @var IManager */ |
|
74 | - private $activityManager; |
|
75 | - |
|
76 | - /** @var LoggerInterface */ |
|
77 | - private $logger; |
|
78 | - |
|
79 | - /** @var TokenProvider */ |
|
80 | - private $tokenProvider; |
|
81 | - |
|
82 | - /** @var ITimeFactory */ |
|
83 | - private $timeFactory; |
|
84 | - |
|
85 | - /** @var IEventDispatcher */ |
|
86 | - private $dispatcher; |
|
87 | - |
|
88 | - /** @var EventDispatcherInterface */ |
|
89 | - private $legacyDispatcher; |
|
90 | - |
|
91 | - /** @psalm-var array<string, bool> */ |
|
92 | - private $userIsTwoFactorAuthenticated = []; |
|
93 | - |
|
94 | - public function __construct(ProviderLoader $providerLoader, |
|
95 | - IRegistry $providerRegistry, |
|
96 | - MandatoryTwoFactor $mandatoryTwoFactor, |
|
97 | - ISession $session, |
|
98 | - IConfig $config, |
|
99 | - IManager $activityManager, |
|
100 | - LoggerInterface $logger, |
|
101 | - TokenProvider $tokenProvider, |
|
102 | - ITimeFactory $timeFactory, |
|
103 | - IEventDispatcher $eventDispatcher, |
|
104 | - EventDispatcherInterface $legacyDispatcher) { |
|
105 | - $this->providerLoader = $providerLoader; |
|
106 | - $this->providerRegistry = $providerRegistry; |
|
107 | - $this->mandatoryTwoFactor = $mandatoryTwoFactor; |
|
108 | - $this->session = $session; |
|
109 | - $this->config = $config; |
|
110 | - $this->activityManager = $activityManager; |
|
111 | - $this->logger = $logger; |
|
112 | - $this->tokenProvider = $tokenProvider; |
|
113 | - $this->timeFactory = $timeFactory; |
|
114 | - $this->dispatcher = $eventDispatcher; |
|
115 | - $this->legacyDispatcher = $legacyDispatcher; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Determine whether the user must provide a second factor challenge |
|
120 | - * |
|
121 | - * @param IUser $user |
|
122 | - * @return boolean |
|
123 | - */ |
|
124 | - public function isTwoFactorAuthenticated(IUser $user): bool { |
|
125 | - if (isset($this->userIsTwoFactorAuthenticated[$user->getUID()])) { |
|
126 | - return $this->userIsTwoFactorAuthenticated[$user->getUID()]; |
|
127 | - } |
|
128 | - |
|
129 | - if ($this->mandatoryTwoFactor->isEnforcedFor($user)) { |
|
130 | - return true; |
|
131 | - } |
|
132 | - |
|
133 | - $providerStates = $this->providerRegistry->getProviderStates($user); |
|
134 | - $providers = $this->providerLoader->getProviders($user); |
|
135 | - $fixedStates = $this->fixMissingProviderStates($providerStates, $providers, $user); |
|
136 | - $enabled = array_filter($fixedStates); |
|
137 | - $providerIds = array_keys($enabled); |
|
138 | - $providerIdsWithoutBackupCodes = array_diff($providerIds, [self::BACKUP_CODES_PROVIDER_ID]); |
|
139 | - |
|
140 | - $this->userIsTwoFactorAuthenticated[$user->getUID()] = !empty($providerIdsWithoutBackupCodes); |
|
141 | - return $this->userIsTwoFactorAuthenticated[$user->getUID()]; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Get a 2FA provider by its ID |
|
146 | - * |
|
147 | - * @param IUser $user |
|
148 | - * @param string $challengeProviderId |
|
149 | - * @return IProvider|null |
|
150 | - */ |
|
151 | - public function getProvider(IUser $user, string $challengeProviderId) { |
|
152 | - $providers = $this->getProviderSet($user)->getProviders(); |
|
153 | - return $providers[$challengeProviderId] ?? null; |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * @param IUser $user |
|
158 | - * @return IActivatableAtLogin[] |
|
159 | - * @throws Exception |
|
160 | - */ |
|
161 | - public function getLoginSetupProviders(IUser $user): array { |
|
162 | - $providers = $this->providerLoader->getProviders($user); |
|
163 | - return array_filter($providers, function (IProvider $provider) { |
|
164 | - return ($provider instanceof IActivatableAtLogin); |
|
165 | - }); |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Check if the persistant mapping of enabled/disabled state of each available |
|
170 | - * provider is missing an entry and add it to the registry in that case. |
|
171 | - * |
|
172 | - * @todo remove in Nextcloud 17 as by then all providers should have been updated |
|
173 | - * |
|
174 | - * @param array<string, bool> $providerStates |
|
175 | - * @param IProvider[] $providers |
|
176 | - * @param IUser $user |
|
177 | - * @return array<string, bool> the updated $providerStates variable |
|
178 | - */ |
|
179 | - private function fixMissingProviderStates(array $providerStates, |
|
180 | - array $providers, IUser $user): array { |
|
181 | - foreach ($providers as $provider) { |
|
182 | - if (isset($providerStates[$provider->getId()])) { |
|
183 | - // All good |
|
184 | - continue; |
|
185 | - } |
|
186 | - |
|
187 | - $enabled = $provider->isTwoFactorAuthEnabledForUser($user); |
|
188 | - if ($enabled) { |
|
189 | - $this->providerRegistry->enableProviderFor($provider, $user); |
|
190 | - } else { |
|
191 | - $this->providerRegistry->disableProviderFor($provider, $user); |
|
192 | - } |
|
193 | - $providerStates[$provider->getId()] = $enabled; |
|
194 | - } |
|
195 | - |
|
196 | - return $providerStates; |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * @param array $states |
|
201 | - * @param IProvider[] $providers |
|
202 | - */ |
|
203 | - private function isProviderMissing(array $states, array $providers): bool { |
|
204 | - $indexed = []; |
|
205 | - foreach ($providers as $provider) { |
|
206 | - $indexed[$provider->getId()] = $provider; |
|
207 | - } |
|
208 | - |
|
209 | - $missing = []; |
|
210 | - foreach ($states as $providerId => $enabled) { |
|
211 | - if (!$enabled) { |
|
212 | - // Don't care |
|
213 | - continue; |
|
214 | - } |
|
215 | - |
|
216 | - if (!isset($indexed[$providerId])) { |
|
217 | - $missing[] = $providerId; |
|
218 | - $this->logger->alert("two-factor auth provider '$providerId' failed to load", |
|
219 | - [ |
|
220 | - 'app' => 'core', |
|
221 | - ]); |
|
222 | - } |
|
223 | - } |
|
224 | - |
|
225 | - if (!empty($missing)) { |
|
226 | - // There was at least one provider missing |
|
227 | - $this->logger->alert(count($missing) . " two-factor auth providers failed to load", ['app' => 'core']); |
|
228 | - |
|
229 | - return true; |
|
230 | - } |
|
231 | - |
|
232 | - // If we reach this, there was not a single provider missing |
|
233 | - return false; |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * Get the list of 2FA providers for the given user |
|
238 | - * |
|
239 | - * @param IUser $user |
|
240 | - * @throws Exception |
|
241 | - */ |
|
242 | - public function getProviderSet(IUser $user): ProviderSet { |
|
243 | - $providerStates = $this->providerRegistry->getProviderStates($user); |
|
244 | - $providers = $this->providerLoader->getProviders($user); |
|
245 | - |
|
246 | - $fixedStates = $this->fixMissingProviderStates($providerStates, $providers, $user); |
|
247 | - $isProviderMissing = $this->isProviderMissing($fixedStates, $providers); |
|
248 | - |
|
249 | - $enabled = array_filter($providers, function (IProvider $provider) use ($fixedStates) { |
|
250 | - return $fixedStates[$provider->getId()]; |
|
251 | - }); |
|
252 | - return new ProviderSet($enabled, $isProviderMissing); |
|
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * Verify the given challenge |
|
257 | - * |
|
258 | - * @param string $providerId |
|
259 | - * @param IUser $user |
|
260 | - * @param string $challenge |
|
261 | - * @return boolean |
|
262 | - */ |
|
263 | - public function verifyChallenge(string $providerId, IUser $user, string $challenge): bool { |
|
264 | - $provider = $this->getProvider($user, $providerId); |
|
265 | - if ($provider === null) { |
|
266 | - return false; |
|
267 | - } |
|
268 | - |
|
269 | - $passed = $provider->verifyChallenge($user, $challenge); |
|
270 | - if ($passed) { |
|
271 | - if ($this->session->get(self::REMEMBER_LOGIN) === true) { |
|
272 | - // TODO: resolve cyclic dependency and use DI |
|
273 | - \OC::$server->getUserSession()->createRememberMeToken($user); |
|
274 | - } |
|
275 | - $this->session->remove(self::SESSION_UID_KEY); |
|
276 | - $this->session->remove(self::REMEMBER_LOGIN); |
|
277 | - $this->session->set(self::SESSION_UID_DONE, $user->getUID()); |
|
278 | - |
|
279 | - // Clear token from db |
|
280 | - $sessionId = $this->session->getId(); |
|
281 | - $token = $this->tokenProvider->getToken($sessionId); |
|
282 | - $tokenId = $token->getId(); |
|
283 | - $this->config->deleteUserValue($user->getUID(), 'login_token_2fa', $tokenId); |
|
284 | - |
|
285 | - $dispatchEvent = new GenericEvent($user, ['provider' => $provider->getDisplayName()]); |
|
286 | - $this->legacyDispatcher->dispatch(IProvider::EVENT_SUCCESS, $dispatchEvent); |
|
287 | - |
|
288 | - $this->dispatcher->dispatchTyped(new TwoFactorProviderForUserEnabled($user, $provider)); |
|
289 | - |
|
290 | - $this->publishEvent($user, 'twofactor_success', [ |
|
291 | - 'provider' => $provider->getDisplayName(), |
|
292 | - ]); |
|
293 | - } else { |
|
294 | - $dispatchEvent = new GenericEvent($user, ['provider' => $provider->getDisplayName()]); |
|
295 | - $this->legacyDispatcher->dispatch(IProvider::EVENT_FAILED, $dispatchEvent); |
|
296 | - |
|
297 | - $this->dispatcher->dispatchTyped(new TwoFactorProviderForUserDisabled($user, $provider)); |
|
298 | - |
|
299 | - $this->publishEvent($user, 'twofactor_failed', [ |
|
300 | - 'provider' => $provider->getDisplayName(), |
|
301 | - ]); |
|
302 | - } |
|
303 | - return $passed; |
|
304 | - } |
|
305 | - |
|
306 | - /** |
|
307 | - * Push a 2fa event the user's activity stream |
|
308 | - * |
|
309 | - * @param IUser $user |
|
310 | - * @param string $event |
|
311 | - * @param array $params |
|
312 | - */ |
|
313 | - private function publishEvent(IUser $user, string $event, array $params) { |
|
314 | - $activity = $this->activityManager->generateEvent(); |
|
315 | - $activity->setApp('core') |
|
316 | - ->setType('security') |
|
317 | - ->setAuthor($user->getUID()) |
|
318 | - ->setAffectedUser($user->getUID()) |
|
319 | - ->setSubject($event, $params); |
|
320 | - try { |
|
321 | - $this->activityManager->publish($activity); |
|
322 | - } catch (BadMethodCallException $e) { |
|
323 | - $this->logger->warning('could not publish activity', ['app' => 'core', 'exception' => $e]); |
|
324 | - } |
|
325 | - } |
|
326 | - |
|
327 | - /** |
|
328 | - * Check if the currently logged in user needs to pass 2FA |
|
329 | - * |
|
330 | - * @param IUser $user the currently logged in user |
|
331 | - * @return boolean |
|
332 | - */ |
|
333 | - public function needsSecondFactor(IUser $user = null): bool { |
|
334 | - if ($user === null) { |
|
335 | - return false; |
|
336 | - } |
|
337 | - |
|
338 | - // If we are authenticated using an app password skip all this |
|
339 | - if ($this->session->exists('app_password')) { |
|
340 | - return false; |
|
341 | - } |
|
342 | - |
|
343 | - // First check if the session tells us we should do 2FA (99% case) |
|
344 | - if (!$this->session->exists(self::SESSION_UID_KEY)) { |
|
345 | - |
|
346 | - // Check if the session tells us it is 2FA authenticated already |
|
347 | - if ($this->session->exists(self::SESSION_UID_DONE) && |
|
348 | - $this->session->get(self::SESSION_UID_DONE) === $user->getUID()) { |
|
349 | - return false; |
|
350 | - } |
|
351 | - |
|
352 | - /* |
|
53 | + public const SESSION_UID_KEY = 'two_factor_auth_uid'; |
|
54 | + public const SESSION_UID_DONE = 'two_factor_auth_passed'; |
|
55 | + public const REMEMBER_LOGIN = 'two_factor_remember_login'; |
|
56 | + public const BACKUP_CODES_PROVIDER_ID = 'backup_codes'; |
|
57 | + |
|
58 | + /** @var ProviderLoader */ |
|
59 | + private $providerLoader; |
|
60 | + |
|
61 | + /** @var IRegistry */ |
|
62 | + private $providerRegistry; |
|
63 | + |
|
64 | + /** @var MandatoryTwoFactor */ |
|
65 | + private $mandatoryTwoFactor; |
|
66 | + |
|
67 | + /** @var ISession */ |
|
68 | + private $session; |
|
69 | + |
|
70 | + /** @var IConfig */ |
|
71 | + private $config; |
|
72 | + |
|
73 | + /** @var IManager */ |
|
74 | + private $activityManager; |
|
75 | + |
|
76 | + /** @var LoggerInterface */ |
|
77 | + private $logger; |
|
78 | + |
|
79 | + /** @var TokenProvider */ |
|
80 | + private $tokenProvider; |
|
81 | + |
|
82 | + /** @var ITimeFactory */ |
|
83 | + private $timeFactory; |
|
84 | + |
|
85 | + /** @var IEventDispatcher */ |
|
86 | + private $dispatcher; |
|
87 | + |
|
88 | + /** @var EventDispatcherInterface */ |
|
89 | + private $legacyDispatcher; |
|
90 | + |
|
91 | + /** @psalm-var array<string, bool> */ |
|
92 | + private $userIsTwoFactorAuthenticated = []; |
|
93 | + |
|
94 | + public function __construct(ProviderLoader $providerLoader, |
|
95 | + IRegistry $providerRegistry, |
|
96 | + MandatoryTwoFactor $mandatoryTwoFactor, |
|
97 | + ISession $session, |
|
98 | + IConfig $config, |
|
99 | + IManager $activityManager, |
|
100 | + LoggerInterface $logger, |
|
101 | + TokenProvider $tokenProvider, |
|
102 | + ITimeFactory $timeFactory, |
|
103 | + IEventDispatcher $eventDispatcher, |
|
104 | + EventDispatcherInterface $legacyDispatcher) { |
|
105 | + $this->providerLoader = $providerLoader; |
|
106 | + $this->providerRegistry = $providerRegistry; |
|
107 | + $this->mandatoryTwoFactor = $mandatoryTwoFactor; |
|
108 | + $this->session = $session; |
|
109 | + $this->config = $config; |
|
110 | + $this->activityManager = $activityManager; |
|
111 | + $this->logger = $logger; |
|
112 | + $this->tokenProvider = $tokenProvider; |
|
113 | + $this->timeFactory = $timeFactory; |
|
114 | + $this->dispatcher = $eventDispatcher; |
|
115 | + $this->legacyDispatcher = $legacyDispatcher; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Determine whether the user must provide a second factor challenge |
|
120 | + * |
|
121 | + * @param IUser $user |
|
122 | + * @return boolean |
|
123 | + */ |
|
124 | + public function isTwoFactorAuthenticated(IUser $user): bool { |
|
125 | + if (isset($this->userIsTwoFactorAuthenticated[$user->getUID()])) { |
|
126 | + return $this->userIsTwoFactorAuthenticated[$user->getUID()]; |
|
127 | + } |
|
128 | + |
|
129 | + if ($this->mandatoryTwoFactor->isEnforcedFor($user)) { |
|
130 | + return true; |
|
131 | + } |
|
132 | + |
|
133 | + $providerStates = $this->providerRegistry->getProviderStates($user); |
|
134 | + $providers = $this->providerLoader->getProviders($user); |
|
135 | + $fixedStates = $this->fixMissingProviderStates($providerStates, $providers, $user); |
|
136 | + $enabled = array_filter($fixedStates); |
|
137 | + $providerIds = array_keys($enabled); |
|
138 | + $providerIdsWithoutBackupCodes = array_diff($providerIds, [self::BACKUP_CODES_PROVIDER_ID]); |
|
139 | + |
|
140 | + $this->userIsTwoFactorAuthenticated[$user->getUID()] = !empty($providerIdsWithoutBackupCodes); |
|
141 | + return $this->userIsTwoFactorAuthenticated[$user->getUID()]; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Get a 2FA provider by its ID |
|
146 | + * |
|
147 | + * @param IUser $user |
|
148 | + * @param string $challengeProviderId |
|
149 | + * @return IProvider|null |
|
150 | + */ |
|
151 | + public function getProvider(IUser $user, string $challengeProviderId) { |
|
152 | + $providers = $this->getProviderSet($user)->getProviders(); |
|
153 | + return $providers[$challengeProviderId] ?? null; |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * @param IUser $user |
|
158 | + * @return IActivatableAtLogin[] |
|
159 | + * @throws Exception |
|
160 | + */ |
|
161 | + public function getLoginSetupProviders(IUser $user): array { |
|
162 | + $providers = $this->providerLoader->getProviders($user); |
|
163 | + return array_filter($providers, function (IProvider $provider) { |
|
164 | + return ($provider instanceof IActivatableAtLogin); |
|
165 | + }); |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Check if the persistant mapping of enabled/disabled state of each available |
|
170 | + * provider is missing an entry and add it to the registry in that case. |
|
171 | + * |
|
172 | + * @todo remove in Nextcloud 17 as by then all providers should have been updated |
|
173 | + * |
|
174 | + * @param array<string, bool> $providerStates |
|
175 | + * @param IProvider[] $providers |
|
176 | + * @param IUser $user |
|
177 | + * @return array<string, bool> the updated $providerStates variable |
|
178 | + */ |
|
179 | + private function fixMissingProviderStates(array $providerStates, |
|
180 | + array $providers, IUser $user): array { |
|
181 | + foreach ($providers as $provider) { |
|
182 | + if (isset($providerStates[$provider->getId()])) { |
|
183 | + // All good |
|
184 | + continue; |
|
185 | + } |
|
186 | + |
|
187 | + $enabled = $provider->isTwoFactorAuthEnabledForUser($user); |
|
188 | + if ($enabled) { |
|
189 | + $this->providerRegistry->enableProviderFor($provider, $user); |
|
190 | + } else { |
|
191 | + $this->providerRegistry->disableProviderFor($provider, $user); |
|
192 | + } |
|
193 | + $providerStates[$provider->getId()] = $enabled; |
|
194 | + } |
|
195 | + |
|
196 | + return $providerStates; |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * @param array $states |
|
201 | + * @param IProvider[] $providers |
|
202 | + */ |
|
203 | + private function isProviderMissing(array $states, array $providers): bool { |
|
204 | + $indexed = []; |
|
205 | + foreach ($providers as $provider) { |
|
206 | + $indexed[$provider->getId()] = $provider; |
|
207 | + } |
|
208 | + |
|
209 | + $missing = []; |
|
210 | + foreach ($states as $providerId => $enabled) { |
|
211 | + if (!$enabled) { |
|
212 | + // Don't care |
|
213 | + continue; |
|
214 | + } |
|
215 | + |
|
216 | + if (!isset($indexed[$providerId])) { |
|
217 | + $missing[] = $providerId; |
|
218 | + $this->logger->alert("two-factor auth provider '$providerId' failed to load", |
|
219 | + [ |
|
220 | + 'app' => 'core', |
|
221 | + ]); |
|
222 | + } |
|
223 | + } |
|
224 | + |
|
225 | + if (!empty($missing)) { |
|
226 | + // There was at least one provider missing |
|
227 | + $this->logger->alert(count($missing) . " two-factor auth providers failed to load", ['app' => 'core']); |
|
228 | + |
|
229 | + return true; |
|
230 | + } |
|
231 | + |
|
232 | + // If we reach this, there was not a single provider missing |
|
233 | + return false; |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * Get the list of 2FA providers for the given user |
|
238 | + * |
|
239 | + * @param IUser $user |
|
240 | + * @throws Exception |
|
241 | + */ |
|
242 | + public function getProviderSet(IUser $user): ProviderSet { |
|
243 | + $providerStates = $this->providerRegistry->getProviderStates($user); |
|
244 | + $providers = $this->providerLoader->getProviders($user); |
|
245 | + |
|
246 | + $fixedStates = $this->fixMissingProviderStates($providerStates, $providers, $user); |
|
247 | + $isProviderMissing = $this->isProviderMissing($fixedStates, $providers); |
|
248 | + |
|
249 | + $enabled = array_filter($providers, function (IProvider $provider) use ($fixedStates) { |
|
250 | + return $fixedStates[$provider->getId()]; |
|
251 | + }); |
|
252 | + return new ProviderSet($enabled, $isProviderMissing); |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * Verify the given challenge |
|
257 | + * |
|
258 | + * @param string $providerId |
|
259 | + * @param IUser $user |
|
260 | + * @param string $challenge |
|
261 | + * @return boolean |
|
262 | + */ |
|
263 | + public function verifyChallenge(string $providerId, IUser $user, string $challenge): bool { |
|
264 | + $provider = $this->getProvider($user, $providerId); |
|
265 | + if ($provider === null) { |
|
266 | + return false; |
|
267 | + } |
|
268 | + |
|
269 | + $passed = $provider->verifyChallenge($user, $challenge); |
|
270 | + if ($passed) { |
|
271 | + if ($this->session->get(self::REMEMBER_LOGIN) === true) { |
|
272 | + // TODO: resolve cyclic dependency and use DI |
|
273 | + \OC::$server->getUserSession()->createRememberMeToken($user); |
|
274 | + } |
|
275 | + $this->session->remove(self::SESSION_UID_KEY); |
|
276 | + $this->session->remove(self::REMEMBER_LOGIN); |
|
277 | + $this->session->set(self::SESSION_UID_DONE, $user->getUID()); |
|
278 | + |
|
279 | + // Clear token from db |
|
280 | + $sessionId = $this->session->getId(); |
|
281 | + $token = $this->tokenProvider->getToken($sessionId); |
|
282 | + $tokenId = $token->getId(); |
|
283 | + $this->config->deleteUserValue($user->getUID(), 'login_token_2fa', $tokenId); |
|
284 | + |
|
285 | + $dispatchEvent = new GenericEvent($user, ['provider' => $provider->getDisplayName()]); |
|
286 | + $this->legacyDispatcher->dispatch(IProvider::EVENT_SUCCESS, $dispatchEvent); |
|
287 | + |
|
288 | + $this->dispatcher->dispatchTyped(new TwoFactorProviderForUserEnabled($user, $provider)); |
|
289 | + |
|
290 | + $this->publishEvent($user, 'twofactor_success', [ |
|
291 | + 'provider' => $provider->getDisplayName(), |
|
292 | + ]); |
|
293 | + } else { |
|
294 | + $dispatchEvent = new GenericEvent($user, ['provider' => $provider->getDisplayName()]); |
|
295 | + $this->legacyDispatcher->dispatch(IProvider::EVENT_FAILED, $dispatchEvent); |
|
296 | + |
|
297 | + $this->dispatcher->dispatchTyped(new TwoFactorProviderForUserDisabled($user, $provider)); |
|
298 | + |
|
299 | + $this->publishEvent($user, 'twofactor_failed', [ |
|
300 | + 'provider' => $provider->getDisplayName(), |
|
301 | + ]); |
|
302 | + } |
|
303 | + return $passed; |
|
304 | + } |
|
305 | + |
|
306 | + /** |
|
307 | + * Push a 2fa event the user's activity stream |
|
308 | + * |
|
309 | + * @param IUser $user |
|
310 | + * @param string $event |
|
311 | + * @param array $params |
|
312 | + */ |
|
313 | + private function publishEvent(IUser $user, string $event, array $params) { |
|
314 | + $activity = $this->activityManager->generateEvent(); |
|
315 | + $activity->setApp('core') |
|
316 | + ->setType('security') |
|
317 | + ->setAuthor($user->getUID()) |
|
318 | + ->setAffectedUser($user->getUID()) |
|
319 | + ->setSubject($event, $params); |
|
320 | + try { |
|
321 | + $this->activityManager->publish($activity); |
|
322 | + } catch (BadMethodCallException $e) { |
|
323 | + $this->logger->warning('could not publish activity', ['app' => 'core', 'exception' => $e]); |
|
324 | + } |
|
325 | + } |
|
326 | + |
|
327 | + /** |
|
328 | + * Check if the currently logged in user needs to pass 2FA |
|
329 | + * |
|
330 | + * @param IUser $user the currently logged in user |
|
331 | + * @return boolean |
|
332 | + */ |
|
333 | + public function needsSecondFactor(IUser $user = null): bool { |
|
334 | + if ($user === null) { |
|
335 | + return false; |
|
336 | + } |
|
337 | + |
|
338 | + // If we are authenticated using an app password skip all this |
|
339 | + if ($this->session->exists('app_password')) { |
|
340 | + return false; |
|
341 | + } |
|
342 | + |
|
343 | + // First check if the session tells us we should do 2FA (99% case) |
|
344 | + if (!$this->session->exists(self::SESSION_UID_KEY)) { |
|
345 | + |
|
346 | + // Check if the session tells us it is 2FA authenticated already |
|
347 | + if ($this->session->exists(self::SESSION_UID_DONE) && |
|
348 | + $this->session->get(self::SESSION_UID_DONE) === $user->getUID()) { |
|
349 | + return false; |
|
350 | + } |
|
351 | + |
|
352 | + /* |
|
353 | 353 | * If the session is expired check if we are not logged in by a token |
354 | 354 | * that still needs 2FA auth |
355 | 355 | */ |
356 | - try { |
|
357 | - $sessionId = $this->session->getId(); |
|
358 | - $token = $this->tokenProvider->getToken($sessionId); |
|
359 | - $tokenId = $token->getId(); |
|
360 | - $tokensNeeding2FA = $this->config->getUserKeys($user->getUID(), 'login_token_2fa'); |
|
361 | - |
|
362 | - if (!\in_array((string) $tokenId, $tokensNeeding2FA, true)) { |
|
363 | - $this->session->set(self::SESSION_UID_DONE, $user->getUID()); |
|
364 | - return false; |
|
365 | - } |
|
366 | - } catch (InvalidTokenException|SessionNotAvailableException $e) { |
|
367 | - } |
|
368 | - } |
|
369 | - |
|
370 | - if (!$this->isTwoFactorAuthenticated($user)) { |
|
371 | - // There is no second factor any more -> let the user pass |
|
372 | - // This prevents infinite redirect loops when a user is about |
|
373 | - // to solve the 2FA challenge, and the provider app is |
|
374 | - // disabled the same time |
|
375 | - $this->session->remove(self::SESSION_UID_KEY); |
|
376 | - |
|
377 | - $keys = $this->config->getUserKeys($user->getUID(), 'login_token_2fa'); |
|
378 | - foreach ($keys as $key) { |
|
379 | - $this->config->deleteUserValue($user->getUID(), 'login_token_2fa', $key); |
|
380 | - } |
|
381 | - return false; |
|
382 | - } |
|
383 | - |
|
384 | - return true; |
|
385 | - } |
|
386 | - |
|
387 | - /** |
|
388 | - * Prepare the 2FA login |
|
389 | - * |
|
390 | - * @param IUser $user |
|
391 | - * @param boolean $rememberMe |
|
392 | - */ |
|
393 | - public function prepareTwoFactorLogin(IUser $user, bool $rememberMe) { |
|
394 | - $this->session->set(self::SESSION_UID_KEY, $user->getUID()); |
|
395 | - $this->session->set(self::REMEMBER_LOGIN, $rememberMe); |
|
396 | - |
|
397 | - $id = $this->session->getId(); |
|
398 | - $token = $this->tokenProvider->getToken($id); |
|
399 | - $this->config->setUserValue($user->getUID(), 'login_token_2fa', (string) $token->getId(), $this->timeFactory->getTime()); |
|
400 | - } |
|
401 | - |
|
402 | - public function clearTwoFactorPending(string $userId) { |
|
403 | - $tokensNeeding2FA = $this->config->getUserKeys($userId, 'login_token_2fa'); |
|
404 | - |
|
405 | - foreach ($tokensNeeding2FA as $tokenId) { |
|
406 | - $this->tokenProvider->invalidateTokenById($userId, (int)$tokenId); |
|
407 | - } |
|
408 | - } |
|
356 | + try { |
|
357 | + $sessionId = $this->session->getId(); |
|
358 | + $token = $this->tokenProvider->getToken($sessionId); |
|
359 | + $tokenId = $token->getId(); |
|
360 | + $tokensNeeding2FA = $this->config->getUserKeys($user->getUID(), 'login_token_2fa'); |
|
361 | + |
|
362 | + if (!\in_array((string) $tokenId, $tokensNeeding2FA, true)) { |
|
363 | + $this->session->set(self::SESSION_UID_DONE, $user->getUID()); |
|
364 | + return false; |
|
365 | + } |
|
366 | + } catch (InvalidTokenException|SessionNotAvailableException $e) { |
|
367 | + } |
|
368 | + } |
|
369 | + |
|
370 | + if (!$this->isTwoFactorAuthenticated($user)) { |
|
371 | + // There is no second factor any more -> let the user pass |
|
372 | + // This prevents infinite redirect loops when a user is about |
|
373 | + // to solve the 2FA challenge, and the provider app is |
|
374 | + // disabled the same time |
|
375 | + $this->session->remove(self::SESSION_UID_KEY); |
|
376 | + |
|
377 | + $keys = $this->config->getUserKeys($user->getUID(), 'login_token_2fa'); |
|
378 | + foreach ($keys as $key) { |
|
379 | + $this->config->deleteUserValue($user->getUID(), 'login_token_2fa', $key); |
|
380 | + } |
|
381 | + return false; |
|
382 | + } |
|
383 | + |
|
384 | + return true; |
|
385 | + } |
|
386 | + |
|
387 | + /** |
|
388 | + * Prepare the 2FA login |
|
389 | + * |
|
390 | + * @param IUser $user |
|
391 | + * @param boolean $rememberMe |
|
392 | + */ |
|
393 | + public function prepareTwoFactorLogin(IUser $user, bool $rememberMe) { |
|
394 | + $this->session->set(self::SESSION_UID_KEY, $user->getUID()); |
|
395 | + $this->session->set(self::REMEMBER_LOGIN, $rememberMe); |
|
396 | + |
|
397 | + $id = $this->session->getId(); |
|
398 | + $token = $this->tokenProvider->getToken($id); |
|
399 | + $this->config->setUserValue($user->getUID(), 'login_token_2fa', (string) $token->getId(), $this->timeFactory->getTime()); |
|
400 | + } |
|
401 | + |
|
402 | + public function clearTwoFactorPending(string $userId) { |
|
403 | + $tokensNeeding2FA = $this->config->getUserKeys($userId, 'login_token_2fa'); |
|
404 | + |
|
405 | + foreach ($tokensNeeding2FA as $tokenId) { |
|
406 | + $this->tokenProvider->invalidateTokenById($userId, (int)$tokenId); |
|
407 | + } |
|
408 | + } |
|
409 | 409 | } |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | */ |
161 | 161 | public function getLoginSetupProviders(IUser $user): array { |
162 | 162 | $providers = $this->providerLoader->getProviders($user); |
163 | - return array_filter($providers, function (IProvider $provider) { |
|
163 | + return array_filter($providers, function(IProvider $provider) { |
|
164 | 164 | return ($provider instanceof IActivatableAtLogin); |
165 | 165 | }); |
166 | 166 | } |
@@ -224,7 +224,7 @@ discard block |
||
224 | 224 | |
225 | 225 | if (!empty($missing)) { |
226 | 226 | // There was at least one provider missing |
227 | - $this->logger->alert(count($missing) . " two-factor auth providers failed to load", ['app' => 'core']); |
|
227 | + $this->logger->alert(count($missing)." two-factor auth providers failed to load", ['app' => 'core']); |
|
228 | 228 | |
229 | 229 | return true; |
230 | 230 | } |
@@ -246,7 +246,7 @@ discard block |
||
246 | 246 | $fixedStates = $this->fixMissingProviderStates($providerStates, $providers, $user); |
247 | 247 | $isProviderMissing = $this->isProviderMissing($fixedStates, $providers); |
248 | 248 | |
249 | - $enabled = array_filter($providers, function (IProvider $provider) use ($fixedStates) { |
|
249 | + $enabled = array_filter($providers, function(IProvider $provider) use ($fixedStates) { |
|
250 | 250 | return $fixedStates[$provider->getId()]; |
251 | 251 | }); |
252 | 252 | return new ProviderSet($enabled, $isProviderMissing); |
@@ -363,7 +363,7 @@ discard block |
||
363 | 363 | $this->session->set(self::SESSION_UID_DONE, $user->getUID()); |
364 | 364 | return false; |
365 | 365 | } |
366 | - } catch (InvalidTokenException|SessionNotAvailableException $e) { |
|
366 | + } catch (InvalidTokenException | SessionNotAvailableException $e) { |
|
367 | 367 | } |
368 | 368 | } |
369 | 369 | |
@@ -403,7 +403,7 @@ discard block |
||
403 | 403 | $tokensNeeding2FA = $this->config->getUserKeys($userId, 'login_token_2fa'); |
404 | 404 | |
405 | 405 | foreach ($tokensNeeding2FA as $tokenId) { |
406 | - $this->tokenProvider->invalidateTokenById($userId, (int)$tokenId); |
|
406 | + $this->tokenProvider->invalidateTokenById($userId, (int) $tokenId); |
|
407 | 407 | } |
408 | 408 | } |
409 | 409 | } |
@@ -90,1063 +90,1063 @@ |
||
90 | 90 | * OC_autoload! |
91 | 91 | */ |
92 | 92 | class OC { |
93 | - /** |
|
94 | - * Associative array for autoloading. classname => filename |
|
95 | - */ |
|
96 | - public static array $CLASSPATH = []; |
|
97 | - /** |
|
98 | - * The installation path for Nextcloud on the server (e.g. /srv/http/nextcloud) |
|
99 | - */ |
|
100 | - public static string $SERVERROOT = ''; |
|
101 | - /** |
|
102 | - * the current request path relative to the Nextcloud root (e.g. files/index.php) |
|
103 | - */ |
|
104 | - private static string $SUBURI = ''; |
|
105 | - /** |
|
106 | - * the Nextcloud root path for http requests (e.g. nextcloud/) |
|
107 | - */ |
|
108 | - public static string $WEBROOT = ''; |
|
109 | - /** |
|
110 | - * The installation path array of the apps folder on the server (e.g. /srv/http/nextcloud) 'path' and |
|
111 | - * web path in 'url' |
|
112 | - */ |
|
113 | - public static array $APPSROOTS = []; |
|
114 | - |
|
115 | - public static string $configDir; |
|
116 | - |
|
117 | - /** |
|
118 | - * requested app |
|
119 | - */ |
|
120 | - public static string $REQUESTEDAPP = ''; |
|
121 | - |
|
122 | - /** |
|
123 | - * check if Nextcloud runs in cli mode |
|
124 | - */ |
|
125 | - public static bool $CLI = false; |
|
126 | - |
|
127 | - public static \OC\Autoloader $loader; |
|
128 | - |
|
129 | - public static \Composer\Autoload\ClassLoader $composerAutoloader; |
|
130 | - |
|
131 | - public static \OC\Server $server; |
|
132 | - |
|
133 | - private static \OC\Config $config; |
|
134 | - |
|
135 | - /** |
|
136 | - * @throws \RuntimeException when the 3rdparty directory is missing or |
|
137 | - * the app path list is empty or contains an invalid path |
|
138 | - */ |
|
139 | - public static function initPaths(): void { |
|
140 | - if (defined('PHPUNIT_CONFIG_DIR')) { |
|
141 | - self::$configDir = OC::$SERVERROOT . '/' . PHPUNIT_CONFIG_DIR . '/'; |
|
142 | - } elseif (defined('PHPUNIT_RUN') and PHPUNIT_RUN and is_dir(OC::$SERVERROOT . '/tests/config/')) { |
|
143 | - self::$configDir = OC::$SERVERROOT . '/tests/config/'; |
|
144 | - } elseif ($dir = getenv('NEXTCLOUD_CONFIG_DIR')) { |
|
145 | - self::$configDir = rtrim($dir, '/') . '/'; |
|
146 | - } else { |
|
147 | - self::$configDir = OC::$SERVERROOT . '/config/'; |
|
148 | - } |
|
149 | - self::$config = new \OC\Config(self::$configDir); |
|
150 | - |
|
151 | - OC::$SUBURI = str_replace("\\", "/", substr(realpath($_SERVER["SCRIPT_FILENAME"] ?? ''), strlen(OC::$SERVERROOT))); |
|
152 | - /** |
|
153 | - * FIXME: The following lines are required because we can't yet instantiate |
|
154 | - * Server::get(\OCP\IRequest::class) since \OC::$server does not yet exist. |
|
155 | - */ |
|
156 | - $params = [ |
|
157 | - 'server' => [ |
|
158 | - 'SCRIPT_NAME' => $_SERVER['SCRIPT_NAME'] ?? null, |
|
159 | - 'SCRIPT_FILENAME' => $_SERVER['SCRIPT_FILENAME'] ?? null, |
|
160 | - ], |
|
161 | - ]; |
|
162 | - $fakeRequest = new \OC\AppFramework\Http\Request( |
|
163 | - $params, |
|
164 | - new \OC\AppFramework\Http\RequestId($_SERVER['UNIQUE_ID'] ?? '', new \OC\Security\SecureRandom()), |
|
165 | - new \OC\AllConfig(new \OC\SystemConfig(self::$config)) |
|
166 | - ); |
|
167 | - $scriptName = $fakeRequest->getScriptName(); |
|
168 | - if (substr($scriptName, -1) == '/') { |
|
169 | - $scriptName .= 'index.php'; |
|
170 | - //make sure suburi follows the same rules as scriptName |
|
171 | - if (substr(OC::$SUBURI, -9) != 'index.php') { |
|
172 | - if (substr(OC::$SUBURI, -1) != '/') { |
|
173 | - OC::$SUBURI = OC::$SUBURI . '/'; |
|
174 | - } |
|
175 | - OC::$SUBURI = OC::$SUBURI . 'index.php'; |
|
176 | - } |
|
177 | - } |
|
178 | - |
|
179 | - |
|
180 | - if (OC::$CLI) { |
|
181 | - OC::$WEBROOT = self::$config->getValue('overwritewebroot', ''); |
|
182 | - } else { |
|
183 | - if (substr($scriptName, 0 - strlen(OC::$SUBURI)) === OC::$SUBURI) { |
|
184 | - OC::$WEBROOT = substr($scriptName, 0, 0 - strlen(OC::$SUBURI)); |
|
185 | - |
|
186 | - if (OC::$WEBROOT != '' && OC::$WEBROOT[0] !== '/') { |
|
187 | - OC::$WEBROOT = '/' . OC::$WEBROOT; |
|
188 | - } |
|
189 | - } else { |
|
190 | - // The scriptName is not ending with OC::$SUBURI |
|
191 | - // This most likely means that we are calling from CLI. |
|
192 | - // However some cron jobs still need to generate |
|
193 | - // a web URL, so we use overwritewebroot as a fallback. |
|
194 | - OC::$WEBROOT = self::$config->getValue('overwritewebroot', ''); |
|
195 | - } |
|
196 | - |
|
197 | - // Resolve /nextcloud to /nextcloud/ to ensure to always have a trailing |
|
198 | - // slash which is required by URL generation. |
|
199 | - if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] === \OC::$WEBROOT && |
|
200 | - substr($_SERVER['REQUEST_URI'], -1) !== '/') { |
|
201 | - header('Location: '.\OC::$WEBROOT.'/'); |
|
202 | - exit(); |
|
203 | - } |
|
204 | - } |
|
205 | - |
|
206 | - // search the apps folder |
|
207 | - $config_paths = self::$config->getValue('apps_paths', []); |
|
208 | - if (!empty($config_paths)) { |
|
209 | - foreach ($config_paths as $paths) { |
|
210 | - if (isset($paths['url']) && isset($paths['path'])) { |
|
211 | - $paths['url'] = rtrim($paths['url'], '/'); |
|
212 | - $paths['path'] = rtrim($paths['path'], '/'); |
|
213 | - OC::$APPSROOTS[] = $paths; |
|
214 | - } |
|
215 | - } |
|
216 | - } elseif (file_exists(OC::$SERVERROOT . '/apps')) { |
|
217 | - OC::$APPSROOTS[] = ['path' => OC::$SERVERROOT . '/apps', 'url' => '/apps', 'writable' => true]; |
|
218 | - } |
|
219 | - |
|
220 | - if (empty(OC::$APPSROOTS)) { |
|
221 | - throw new \RuntimeException('apps directory not found! Please put the Nextcloud apps folder in the Nextcloud folder' |
|
222 | - . '. You can also configure the location in the config.php file.'); |
|
223 | - } |
|
224 | - $paths = []; |
|
225 | - foreach (OC::$APPSROOTS as $path) { |
|
226 | - $paths[] = $path['path']; |
|
227 | - if (!is_dir($path['path'])) { |
|
228 | - throw new \RuntimeException(sprintf('App directory "%s" not found! Please put the Nextcloud apps folder in the' |
|
229 | - . ' Nextcloud folder. You can also configure the location in the config.php file.', $path['path'])); |
|
230 | - } |
|
231 | - } |
|
232 | - |
|
233 | - // set the right include path |
|
234 | - set_include_path( |
|
235 | - implode(PATH_SEPARATOR, $paths) |
|
236 | - ); |
|
237 | - } |
|
238 | - |
|
239 | - public static function checkConfig(): void { |
|
240 | - $l = Server::get(\OCP\L10N\IFactory::class)->get('lib'); |
|
241 | - |
|
242 | - // Create config if it does not already exist |
|
243 | - $configFilePath = self::$configDir .'/config.php'; |
|
244 | - if (!file_exists($configFilePath)) { |
|
245 | - @touch($configFilePath); |
|
246 | - } |
|
247 | - |
|
248 | - // Check if config is writable |
|
249 | - $configFileWritable = is_writable($configFilePath); |
|
250 | - if (!$configFileWritable && !OC_Helper::isReadOnlyConfigEnabled() |
|
251 | - || !$configFileWritable && \OCP\Util::needUpgrade()) { |
|
252 | - $urlGenerator = Server::get(IURLGenerator::class); |
|
253 | - |
|
254 | - if (self::$CLI) { |
|
255 | - echo $l->t('Cannot write into "config" directory!')."\n"; |
|
256 | - echo $l->t('This can usually be fixed by giving the web server write access to the config directory.')."\n"; |
|
257 | - echo "\n"; |
|
258 | - 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"; |
|
259 | - echo $l->t('See %s', [ $urlGenerator->linkToDocs('admin-config') ])."\n"; |
|
260 | - exit; |
|
261 | - } else { |
|
262 | - OC_Template::printErrorPage( |
|
263 | - $l->t('Cannot write into "config" directory!'), |
|
264 | - $l->t('This can usually be fixed by giving the web server write access to the config directory.') . ' ' |
|
265 | - . $l->t('But, if you prefer to keep config.php file read only, set the option "config_is_read_only" to true in it.') . ' ' |
|
266 | - . $l->t('See %s', [ $urlGenerator->linkToDocs('admin-config') ]), |
|
267 | - 503 |
|
268 | - ); |
|
269 | - } |
|
270 | - } |
|
271 | - } |
|
272 | - |
|
273 | - public static function checkInstalled(\OC\SystemConfig $systemConfig): void { |
|
274 | - if (defined('OC_CONSOLE')) { |
|
275 | - return; |
|
276 | - } |
|
277 | - // Redirect to installer if not installed |
|
278 | - if (!$systemConfig->getValue('installed', false) && OC::$SUBURI !== '/index.php' && OC::$SUBURI !== '/status.php') { |
|
279 | - if (OC::$CLI) { |
|
280 | - throw new Exception('Not installed'); |
|
281 | - } else { |
|
282 | - $url = OC::$WEBROOT . '/index.php'; |
|
283 | - header('Location: ' . $url); |
|
284 | - } |
|
285 | - exit(); |
|
286 | - } |
|
287 | - } |
|
288 | - |
|
289 | - public static function checkMaintenanceMode(\OC\SystemConfig $systemConfig): void { |
|
290 | - // Allow ajax update script to execute without being stopped |
|
291 | - if (((bool) $systemConfig->getValue('maintenance', false)) && OC::$SUBURI != '/core/ajax/update.php') { |
|
292 | - // send http status 503 |
|
293 | - http_response_code(503); |
|
294 | - header('X-Nextcloud-Maintenance-Mode: 1'); |
|
295 | - header('Retry-After: 120'); |
|
296 | - |
|
297 | - // render error page |
|
298 | - $template = new OC_Template('', 'update.user', 'guest'); |
|
299 | - \OCP\Util::addScript('core', 'maintenance'); |
|
300 | - \OCP\Util::addStyle('core', 'guest'); |
|
301 | - $template->printPage(); |
|
302 | - die(); |
|
303 | - } |
|
304 | - } |
|
305 | - |
|
306 | - /** |
|
307 | - * Prints the upgrade page |
|
308 | - */ |
|
309 | - private static function printUpgradePage(\OC\SystemConfig $systemConfig): void { |
|
310 | - $disableWebUpdater = $systemConfig->getValue('upgrade.disable-web', false); |
|
311 | - $tooBig = false; |
|
312 | - if (!$disableWebUpdater) { |
|
313 | - $apps = Server::get(\OCP\App\IAppManager::class); |
|
314 | - if ($apps->isInstalled('user_ldap')) { |
|
315 | - $qb = Server::get(\OCP\IDBConnection::class)->getQueryBuilder(); |
|
316 | - |
|
317 | - $result = $qb->select($qb->func()->count('*', 'user_count')) |
|
318 | - ->from('ldap_user_mapping') |
|
319 | - ->executeQuery(); |
|
320 | - $row = $result->fetch(); |
|
321 | - $result->closeCursor(); |
|
322 | - |
|
323 | - $tooBig = ($row['user_count'] > 50); |
|
324 | - } |
|
325 | - if (!$tooBig && $apps->isInstalled('user_saml')) { |
|
326 | - $qb = Server::get(\OCP\IDBConnection::class)->getQueryBuilder(); |
|
327 | - |
|
328 | - $result = $qb->select($qb->func()->count('*', 'user_count')) |
|
329 | - ->from('user_saml_users') |
|
330 | - ->executeQuery(); |
|
331 | - $row = $result->fetch(); |
|
332 | - $result->closeCursor(); |
|
333 | - |
|
334 | - $tooBig = ($row['user_count'] > 50); |
|
335 | - } |
|
336 | - if (!$tooBig) { |
|
337 | - // count users |
|
338 | - $stats = Server::get(\OCP\IUserManager::class)->countUsers(); |
|
339 | - $totalUsers = array_sum($stats); |
|
340 | - $tooBig = ($totalUsers > 50); |
|
341 | - } |
|
342 | - } |
|
343 | - $ignoreTooBigWarning = isset($_GET['IKnowThatThisIsABigInstanceAndTheUpdateRequestCouldRunIntoATimeoutAndHowToRestoreABackup']) && |
|
344 | - $_GET['IKnowThatThisIsABigInstanceAndTheUpdateRequestCouldRunIntoATimeoutAndHowToRestoreABackup'] === 'IAmSuperSureToDoThis'; |
|
345 | - |
|
346 | - if ($disableWebUpdater || ($tooBig && !$ignoreTooBigWarning)) { |
|
347 | - // send http status 503 |
|
348 | - http_response_code(503); |
|
349 | - header('Retry-After: 120'); |
|
350 | - |
|
351 | - // render error page |
|
352 | - $template = new OC_Template('', 'update.use-cli', 'guest'); |
|
353 | - $template->assign('productName', 'nextcloud'); // for now |
|
354 | - $template->assign('version', OC_Util::getVersionString()); |
|
355 | - $template->assign('tooBig', $tooBig); |
|
356 | - |
|
357 | - $template->printPage(); |
|
358 | - die(); |
|
359 | - } |
|
360 | - |
|
361 | - // check whether this is a core update or apps update |
|
362 | - $installedVersion = $systemConfig->getValue('version', '0.0.0'); |
|
363 | - $currentVersion = implode('.', \OCP\Util::getVersion()); |
|
364 | - |
|
365 | - // if not a core upgrade, then it's apps upgrade |
|
366 | - $isAppsOnlyUpgrade = version_compare($currentVersion, $installedVersion, '='); |
|
367 | - |
|
368 | - $oldTheme = $systemConfig->getValue('theme'); |
|
369 | - $systemConfig->setValue('theme', ''); |
|
370 | - \OCP\Util::addScript('core', 'common'); |
|
371 | - \OCP\Util::addScript('core', 'main'); |
|
372 | - \OCP\Util::addTranslations('core'); |
|
373 | - \OCP\Util::addScript('core', 'update'); |
|
374 | - |
|
375 | - /** @var \OC\App\AppManager $appManager */ |
|
376 | - $appManager = Server::get(\OCP\App\IAppManager::class); |
|
377 | - |
|
378 | - $tmpl = new OC_Template('', 'update.admin', 'guest'); |
|
379 | - $tmpl->assign('version', OC_Util::getVersionString()); |
|
380 | - $tmpl->assign('isAppsOnlyUpgrade', $isAppsOnlyUpgrade); |
|
381 | - |
|
382 | - // get third party apps |
|
383 | - $ocVersion = \OCP\Util::getVersion(); |
|
384 | - $ocVersion = implode('.', $ocVersion); |
|
385 | - $incompatibleApps = $appManager->getIncompatibleApps($ocVersion); |
|
386 | - $incompatibleShippedApps = []; |
|
387 | - foreach ($incompatibleApps as $appInfo) { |
|
388 | - if ($appManager->isShipped($appInfo['id'])) { |
|
389 | - $incompatibleShippedApps[] = $appInfo['name'] . ' (' . $appInfo['id'] . ')'; |
|
390 | - } |
|
391 | - } |
|
392 | - |
|
393 | - if (!empty($incompatibleShippedApps)) { |
|
394 | - $l = Server::get(\OCP\L10N\IFactory::class)->get('core'); |
|
395 | - $hint = $l->t('The files of the app %1$s were not replaced correctly. Make sure it is a version compatible with the server.', [implode(', ', $incompatibleShippedApps)]); |
|
396 | - throw new \OCP\HintException('The files of the app ' . implode(', ', $incompatibleShippedApps) . ' were not replaced correctly. Make sure it is a version compatible with the server.', $hint); |
|
397 | - } |
|
398 | - |
|
399 | - $tmpl->assign('appsToUpgrade', $appManager->getAppsNeedingUpgrade($ocVersion)); |
|
400 | - $tmpl->assign('incompatibleAppsList', $incompatibleApps); |
|
401 | - try { |
|
402 | - $defaults = new \OC_Defaults(); |
|
403 | - $tmpl->assign('productName', $defaults->getName()); |
|
404 | - } catch (Throwable $error) { |
|
405 | - $tmpl->assign('productName', 'Nextcloud'); |
|
406 | - } |
|
407 | - $tmpl->assign('oldTheme', $oldTheme); |
|
408 | - $tmpl->printPage(); |
|
409 | - } |
|
410 | - |
|
411 | - public static function initSession(): void { |
|
412 | - $request = Server::get(IRequest::class); |
|
413 | - $isDavRequest = strpos($request->getRequestUri(), '/remote.php/dav') === 0 || strpos($request->getRequestUri(), '/remote.php/webdav') === 0; |
|
414 | - if ($request->getHeader('Authorization') !== '' && is_null($request->getCookie('cookie_test')) && $isDavRequest) { |
|
415 | - setcookie('cookie_test', 'test', time() + 3600); |
|
416 | - // Do not initialize the session if a request is authenticated directly |
|
417 | - // unless there is a session cookie already sent along |
|
418 | - return; |
|
419 | - } |
|
420 | - |
|
421 | - if ($request->getServerProtocol() === 'https') { |
|
422 | - ini_set('session.cookie_secure', 'true'); |
|
423 | - } |
|
424 | - |
|
425 | - // prevents javascript from accessing php session cookies |
|
426 | - ini_set('session.cookie_httponly', 'true'); |
|
427 | - |
|
428 | - // set the cookie path to the Nextcloud directory |
|
429 | - $cookie_path = OC::$WEBROOT ? : '/'; |
|
430 | - ini_set('session.cookie_path', $cookie_path); |
|
431 | - |
|
432 | - // Let the session name be changed in the initSession Hook |
|
433 | - $sessionName = OC_Util::getInstanceId(); |
|
434 | - |
|
435 | - try { |
|
436 | - // set the session name to the instance id - which is unique |
|
437 | - $session = new \OC\Session\Internal($sessionName); |
|
438 | - |
|
439 | - $cryptoWrapper = Server::get(\OC\Session\CryptoWrapper::class); |
|
440 | - $session = $cryptoWrapper->wrapSession($session); |
|
441 | - self::$server->setSession($session); |
|
442 | - |
|
443 | - // if session can't be started break with http 500 error |
|
444 | - } catch (Exception $e) { |
|
445 | - Server::get(LoggerInterface::class)->error($e->getMessage(), ['app' => 'base','exception' => $e]); |
|
446 | - //show the user a detailed error page |
|
447 | - OC_Template::printExceptionErrorPage($e, 500); |
|
448 | - die(); |
|
449 | - } |
|
450 | - |
|
451 | - //try to set the session lifetime |
|
452 | - $sessionLifeTime = self::getSessionLifeTime(); |
|
453 | - @ini_set('gc_maxlifetime', (string)$sessionLifeTime); |
|
454 | - |
|
455 | - // session timeout |
|
456 | - if ($session->exists('LAST_ACTIVITY') && (time() - $session->get('LAST_ACTIVITY') > $sessionLifeTime)) { |
|
457 | - if (isset($_COOKIE[session_name()])) { |
|
458 | - setcookie(session_name(), '', -1, self::$WEBROOT ? : '/'); |
|
459 | - } |
|
460 | - Server::get(IUserSession::class)->logout(); |
|
461 | - } |
|
462 | - |
|
463 | - if (!self::hasSessionRelaxedExpiry()) { |
|
464 | - $session->set('LAST_ACTIVITY', time()); |
|
465 | - } |
|
466 | - $session->close(); |
|
467 | - } |
|
468 | - |
|
469 | - private static function getSessionLifeTime(): int { |
|
470 | - return Server::get(\OC\AllConfig::class)->getSystemValueInt('session_lifetime', 60 * 60 * 24); |
|
471 | - } |
|
472 | - |
|
473 | - /** |
|
474 | - * @return bool true if the session expiry should only be done by gc instead of an explicit timeout |
|
475 | - */ |
|
476 | - public static function hasSessionRelaxedExpiry(): bool { |
|
477 | - return Server::get(\OC\AllConfig::class)->getSystemValueBool('session_relaxed_expiry', false); |
|
478 | - } |
|
479 | - |
|
480 | - /** |
|
481 | - * Try to set some values to the required Nextcloud default |
|
482 | - */ |
|
483 | - public static function setRequiredIniValues(): void { |
|
484 | - @ini_set('default_charset', 'UTF-8'); |
|
485 | - @ini_set('gd.jpeg_ignore_warning', '1'); |
|
486 | - } |
|
487 | - |
|
488 | - /** |
|
489 | - * Send the same site cookies |
|
490 | - */ |
|
491 | - private static function sendSameSiteCookies(): void { |
|
492 | - $cookieParams = session_get_cookie_params(); |
|
493 | - $secureCookie = ($cookieParams['secure'] === true) ? 'secure; ' : ''; |
|
494 | - $policies = [ |
|
495 | - 'lax', |
|
496 | - 'strict', |
|
497 | - ]; |
|
498 | - |
|
499 | - // Append __Host to the cookie if it meets the requirements |
|
500 | - $cookiePrefix = ''; |
|
501 | - if ($cookieParams['secure'] === true && $cookieParams['path'] === '/') { |
|
502 | - $cookiePrefix = '__Host-'; |
|
503 | - } |
|
504 | - |
|
505 | - foreach ($policies as $policy) { |
|
506 | - header( |
|
507 | - sprintf( |
|
508 | - 'Set-Cookie: %snc_sameSiteCookie%s=true; path=%s; httponly;' . $secureCookie . 'expires=Fri, 31-Dec-2100 23:59:59 GMT; SameSite=%s', |
|
509 | - $cookiePrefix, |
|
510 | - $policy, |
|
511 | - $cookieParams['path'], |
|
512 | - $policy |
|
513 | - ), |
|
514 | - false |
|
515 | - ); |
|
516 | - } |
|
517 | - } |
|
518 | - |
|
519 | - /** |
|
520 | - * Same Site cookie to further mitigate CSRF attacks. This cookie has to |
|
521 | - * be set in every request if cookies are sent to add a second level of |
|
522 | - * defense against CSRF. |
|
523 | - * |
|
524 | - * If the cookie is not sent this will set the cookie and reload the page. |
|
525 | - * We use an additional cookie since we want to protect logout CSRF and |
|
526 | - * also we can't directly interfere with PHP's session mechanism. |
|
527 | - */ |
|
528 | - private static function performSameSiteCookieProtection(\OCP\IConfig $config): void { |
|
529 | - $request = Server::get(IRequest::class); |
|
530 | - |
|
531 | - // Some user agents are notorious and don't really properly follow HTTP |
|
532 | - // specifications. For those, have an automated opt-out. Since the protection |
|
533 | - // for remote.php is applied in base.php as starting point we need to opt out |
|
534 | - // here. |
|
535 | - $incompatibleUserAgents = $config->getSystemValue('csrf.optout'); |
|
536 | - |
|
537 | - // Fallback, if csrf.optout is unset |
|
538 | - if (!is_array($incompatibleUserAgents)) { |
|
539 | - $incompatibleUserAgents = [ |
|
540 | - // OS X Finder |
|
541 | - '/^WebDAVFS/', |
|
542 | - // Windows webdav drive |
|
543 | - '/^Microsoft-WebDAV-MiniRedir/', |
|
544 | - ]; |
|
545 | - } |
|
546 | - |
|
547 | - if ($request->isUserAgent($incompatibleUserAgents)) { |
|
548 | - return; |
|
549 | - } |
|
550 | - |
|
551 | - if (count($_COOKIE) > 0) { |
|
552 | - $requestUri = $request->getScriptName(); |
|
553 | - $processingScript = explode('/', $requestUri); |
|
554 | - $processingScript = $processingScript[count($processingScript) - 1]; |
|
555 | - |
|
556 | - // index.php routes are handled in the middleware |
|
557 | - if ($processingScript === 'index.php') { |
|
558 | - return; |
|
559 | - } |
|
560 | - |
|
561 | - // All other endpoints require the lax and the strict cookie |
|
562 | - if (!$request->passesStrictCookieCheck()) { |
|
563 | - self::sendSameSiteCookies(); |
|
564 | - // Debug mode gets access to the resources without strict cookie |
|
565 | - // due to the fact that the SabreDAV browser also lives there. |
|
566 | - if (!$config->getSystemValue('debug', false)) { |
|
567 | - http_response_code(\OCP\AppFramework\Http::STATUS_SERVICE_UNAVAILABLE); |
|
568 | - exit(); |
|
569 | - } |
|
570 | - } |
|
571 | - } elseif (!isset($_COOKIE['nc_sameSiteCookielax']) || !isset($_COOKIE['nc_sameSiteCookiestrict'])) { |
|
572 | - self::sendSameSiteCookies(); |
|
573 | - } |
|
574 | - } |
|
575 | - |
|
576 | - public static function init(): void { |
|
577 | - // calculate the root directories |
|
578 | - OC::$SERVERROOT = str_replace("\\", '/', substr(__DIR__, 0, -4)); |
|
579 | - |
|
580 | - // register autoloader |
|
581 | - $loaderStart = microtime(true); |
|
582 | - require_once __DIR__ . '/autoloader.php'; |
|
583 | - self::$loader = new \OC\Autoloader([ |
|
584 | - OC::$SERVERROOT . '/lib/private/legacy', |
|
585 | - ]); |
|
586 | - if (defined('PHPUNIT_RUN')) { |
|
587 | - self::$loader->addValidRoot(OC::$SERVERROOT . '/tests'); |
|
588 | - } |
|
589 | - spl_autoload_register([self::$loader, 'load']); |
|
590 | - $loaderEnd = microtime(true); |
|
591 | - |
|
592 | - self::$CLI = (php_sapi_name() == 'cli'); |
|
593 | - |
|
594 | - // Add default composer PSR-4 autoloader |
|
595 | - self::$composerAutoloader = require_once OC::$SERVERROOT . '/lib/composer/autoload.php'; |
|
596 | - self::$composerAutoloader->setApcuPrefix('composer_autoload'); |
|
597 | - |
|
598 | - try { |
|
599 | - self::initPaths(); |
|
600 | - // setup 3rdparty autoloader |
|
601 | - $vendorAutoLoad = OC::$SERVERROOT. '/3rdparty/autoload.php'; |
|
602 | - if (!file_exists($vendorAutoLoad)) { |
|
603 | - 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".'); |
|
604 | - } |
|
605 | - require_once $vendorAutoLoad; |
|
606 | - } catch (\RuntimeException $e) { |
|
607 | - if (!self::$CLI) { |
|
608 | - http_response_code(503); |
|
609 | - } |
|
610 | - // we can't use the template error page here, because this needs the |
|
611 | - // DI container which isn't available yet |
|
612 | - print($e->getMessage()); |
|
613 | - exit(); |
|
614 | - } |
|
615 | - |
|
616 | - // setup the basic server |
|
617 | - self::$server = new \OC\Server(\OC::$WEBROOT, self::$config); |
|
618 | - self::$server->boot(); |
|
619 | - |
|
620 | - $eventLogger = Server::get(\OCP\Diagnostics\IEventLogger::class); |
|
621 | - $eventLogger->log('autoloader', 'Autoloader', $loaderStart, $loaderEnd); |
|
622 | - $eventLogger->start('boot', 'Initialize'); |
|
623 | - |
|
624 | - // Override php.ini and log everything if we're troubleshooting |
|
625 | - if (self::$config->getValue('loglevel') === ILogger::DEBUG) { |
|
626 | - error_reporting(E_ALL); |
|
627 | - } |
|
628 | - |
|
629 | - // Don't display errors and log them |
|
630 | - @ini_set('display_errors', '0'); |
|
631 | - @ini_set('log_errors', '1'); |
|
632 | - |
|
633 | - if (!date_default_timezone_set('UTC')) { |
|
634 | - throw new \RuntimeException('Could not set timezone to UTC'); |
|
635 | - } |
|
636 | - |
|
637 | - |
|
638 | - //try to configure php to enable big file uploads. |
|
639 | - //this doesn´t work always depending on the webserver and php configuration. |
|
640 | - //Let´s try to overwrite some defaults if they are smaller than 1 hour |
|
641 | - |
|
642 | - if (intval(@ini_get('max_execution_time') ?? 0) < 3600) { |
|
643 | - @ini_set('max_execution_time', strval(3600)); |
|
644 | - } |
|
645 | - |
|
646 | - if (intval(@ini_get('max_input_time') ?? 0) < 3600) { |
|
647 | - @ini_set('max_input_time', strval(3600)); |
|
648 | - } |
|
649 | - |
|
650 | - //try to set the maximum execution time to the largest time limit we have |
|
651 | - if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) { |
|
652 | - @set_time_limit(max(intval(@ini_get('max_execution_time')), intval(@ini_get('max_input_time')))); |
|
653 | - } |
|
654 | - |
|
655 | - self::setRequiredIniValues(); |
|
656 | - self::handleAuthHeaders(); |
|
657 | - $systemConfig = Server::get(\OC\SystemConfig::class); |
|
658 | - self::registerAutoloaderCache($systemConfig); |
|
659 | - |
|
660 | - // initialize intl fallback if necessary |
|
661 | - OC_Util::isSetLocaleWorking(); |
|
662 | - |
|
663 | - $config = Server::get(\OCP\IConfig::class); |
|
664 | - if (!defined('PHPUNIT_RUN')) { |
|
665 | - $errorHandler = new OC\Log\ErrorHandler( |
|
666 | - \OCP\Server::get(\Psr\Log\LoggerInterface::class), |
|
667 | - ); |
|
668 | - $exceptionHandler = [$errorHandler, 'onException']; |
|
669 | - if ($config->getSystemValue('debug', false)) { |
|
670 | - set_error_handler([$errorHandler, 'onAll'], E_ALL); |
|
671 | - if (\OC::$CLI) { |
|
672 | - $exceptionHandler = ['OC_Template', 'printExceptionErrorPage']; |
|
673 | - } |
|
674 | - } else { |
|
675 | - set_error_handler([$errorHandler, 'onError']); |
|
676 | - } |
|
677 | - register_shutdown_function([$errorHandler, 'onShutdown']); |
|
678 | - set_exception_handler($exceptionHandler); |
|
679 | - } |
|
680 | - |
|
681 | - /** @var \OC\AppFramework\Bootstrap\Coordinator $bootstrapCoordinator */ |
|
682 | - $bootstrapCoordinator = Server::get(\OC\AppFramework\Bootstrap\Coordinator::class); |
|
683 | - $bootstrapCoordinator->runInitialRegistration(); |
|
684 | - |
|
685 | - $eventLogger->start('init_session', 'Initialize session'); |
|
686 | - OC_App::loadApps(['session']); |
|
687 | - if (!self::$CLI) { |
|
688 | - self::initSession(); |
|
689 | - } |
|
690 | - $eventLogger->end('init_session'); |
|
691 | - self::checkConfig(); |
|
692 | - self::checkInstalled($systemConfig); |
|
693 | - |
|
694 | - OC_Response::addSecurityHeaders(); |
|
695 | - |
|
696 | - self::performSameSiteCookieProtection($config); |
|
697 | - |
|
698 | - if (!defined('OC_CONSOLE')) { |
|
699 | - $errors = OC_Util::checkServer($systemConfig); |
|
700 | - if (count($errors) > 0) { |
|
701 | - if (!self::$CLI) { |
|
702 | - http_response_code(503); |
|
703 | - OC_Util::addStyle('guest'); |
|
704 | - try { |
|
705 | - OC_Template::printGuestPage('', 'error', ['errors' => $errors]); |
|
706 | - exit; |
|
707 | - } catch (\Exception $e) { |
|
708 | - // In case any error happens when showing the error page, we simply fall back to posting the text. |
|
709 | - // This might be the case when e.g. the data directory is broken and we can not load/write SCSS to/from it. |
|
710 | - } |
|
711 | - } |
|
712 | - |
|
713 | - // Convert l10n string into regular string for usage in database |
|
714 | - $staticErrors = []; |
|
715 | - foreach ($errors as $error) { |
|
716 | - echo $error['error'] . "\n"; |
|
717 | - echo $error['hint'] . "\n\n"; |
|
718 | - $staticErrors[] = [ |
|
719 | - 'error' => (string)$error['error'], |
|
720 | - 'hint' => (string)$error['hint'], |
|
721 | - ]; |
|
722 | - } |
|
723 | - |
|
724 | - try { |
|
725 | - $config->setAppValue('core', 'cronErrors', json_encode($staticErrors)); |
|
726 | - } catch (\Exception $e) { |
|
727 | - echo('Writing to database failed'); |
|
728 | - } |
|
729 | - exit(1); |
|
730 | - } elseif (self::$CLI && $config->getSystemValue('installed', false)) { |
|
731 | - $config->deleteAppValue('core', 'cronErrors'); |
|
732 | - } |
|
733 | - } |
|
734 | - |
|
735 | - // User and Groups |
|
736 | - if (!$systemConfig->getValue("installed", false)) { |
|
737 | - self::$server->getSession()->set('user_id', ''); |
|
738 | - } |
|
739 | - |
|
740 | - OC_User::useBackend(new \OC\User\Database()); |
|
741 | - Server::get(\OCP\IGroupManager::class)->addBackend(new \OC\Group\Database()); |
|
742 | - |
|
743 | - // Subscribe to the hook |
|
744 | - \OCP\Util::connectHook( |
|
745 | - '\OCA\Files_Sharing\API\Server2Server', |
|
746 | - 'preLoginNameUsedAsUserName', |
|
747 | - '\OC\User\Database', |
|
748 | - 'preLoginNameUsedAsUserName' |
|
749 | - ); |
|
750 | - |
|
751 | - //setup extra user backends |
|
752 | - if (!\OCP\Util::needUpgrade()) { |
|
753 | - OC_User::setupBackends(); |
|
754 | - } else { |
|
755 | - // Run upgrades in incognito mode |
|
756 | - OC_User::setIncognitoMode(true); |
|
757 | - } |
|
758 | - |
|
759 | - self::registerCleanupHooks($systemConfig); |
|
760 | - self::registerShareHooks($systemConfig); |
|
761 | - self::registerEncryptionWrapperAndHooks(); |
|
762 | - self::registerAccountHooks(); |
|
763 | - self::registerResourceCollectionHooks(); |
|
764 | - self::registerFileReferenceEventListener(); |
|
765 | - self::registerAppRestrictionsHooks(); |
|
766 | - |
|
767 | - // Make sure that the application class is not loaded before the database is setup |
|
768 | - if ($systemConfig->getValue("installed", false)) { |
|
769 | - OC_App::loadApp('settings'); |
|
770 | - /* Build core application to make sure that listeners are registered */ |
|
771 | - Server::get(\OC\Core\Application::class); |
|
772 | - } |
|
773 | - |
|
774 | - //make sure temporary files are cleaned up |
|
775 | - $tmpManager = Server::get(\OCP\ITempManager::class); |
|
776 | - register_shutdown_function([$tmpManager, 'clean']); |
|
777 | - $lockProvider = Server::get(\OCP\Lock\ILockingProvider::class); |
|
778 | - register_shutdown_function([$lockProvider, 'releaseAll']); |
|
779 | - |
|
780 | - // Check whether the sample configuration has been copied |
|
781 | - if ($systemConfig->getValue('copied_sample_config', false)) { |
|
782 | - $l = Server::get(\OCP\L10N\IFactory::class)->get('lib'); |
|
783 | - OC_Template::printErrorPage( |
|
784 | - $l->t('Sample configuration detected'), |
|
785 | - $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'), |
|
786 | - 503 |
|
787 | - ); |
|
788 | - return; |
|
789 | - } |
|
790 | - |
|
791 | - $request = Server::get(IRequest::class); |
|
792 | - $host = $request->getInsecureServerHost(); |
|
793 | - /** |
|
794 | - * if the host passed in headers isn't trusted |
|
795 | - * FIXME: Should not be in here at all :see_no_evil: |
|
796 | - */ |
|
797 | - if (!OC::$CLI |
|
798 | - && !Server::get(\OC\Security\TrustedDomainHelper::class)->isTrustedDomain($host) |
|
799 | - && $config->getSystemValue('installed', false) |
|
800 | - ) { |
|
801 | - // Allow access to CSS resources |
|
802 | - $isScssRequest = false; |
|
803 | - if (strpos($request->getPathInfo() ?: '', '/css/') === 0) { |
|
804 | - $isScssRequest = true; |
|
805 | - } |
|
806 | - |
|
807 | - if (substr($request->getRequestUri(), -11) === '/status.php') { |
|
808 | - http_response_code(400); |
|
809 | - header('Content-Type: application/json'); |
|
810 | - echo '{"error": "Trusted domain error.", "code": 15}'; |
|
811 | - exit(); |
|
812 | - } |
|
813 | - |
|
814 | - if (!$isScssRequest) { |
|
815 | - http_response_code(400); |
|
816 | - Server::get(LoggerInterface::class)->info( |
|
817 | - 'Trusted domain error. "{remoteAddress}" tried to access using "{host}" as host.', |
|
818 | - [ |
|
819 | - 'app' => 'core', |
|
820 | - 'remoteAddress' => $request->getRemoteAddress(), |
|
821 | - 'host' => $host, |
|
822 | - ] |
|
823 | - ); |
|
824 | - |
|
825 | - $tmpl = new OCP\Template('core', 'untrustedDomain', 'guest'); |
|
826 | - $tmpl->assign('docUrl', Server::get(IURLGenerator::class)->linkToDocs('admin-trusted-domains')); |
|
827 | - $tmpl->printPage(); |
|
828 | - |
|
829 | - exit(); |
|
830 | - } |
|
831 | - } |
|
832 | - $eventLogger->end('boot'); |
|
833 | - $eventLogger->log('init', 'OC::init', $loaderStart, microtime(true)); |
|
834 | - $eventLogger->start('runtime', 'Runtime'); |
|
835 | - $eventLogger->start('request', 'Full request after boot'); |
|
836 | - register_shutdown_function(function () use ($eventLogger) { |
|
837 | - $eventLogger->end('request'); |
|
838 | - }); |
|
839 | - } |
|
840 | - |
|
841 | - /** |
|
842 | - * register hooks for the cleanup of cache and bruteforce protection |
|
843 | - */ |
|
844 | - public static function registerCleanupHooks(\OC\SystemConfig $systemConfig): void { |
|
845 | - //don't try to do this before we are properly setup |
|
846 | - if ($systemConfig->getValue('installed', false) && !\OCP\Util::needUpgrade()) { |
|
847 | - // NOTE: This will be replaced to use OCP |
|
848 | - $userSession = Server::get(\OC\User\Session::class); |
|
849 | - $userSession->listen('\OC\User', 'postLogin', function () use ($userSession) { |
|
850 | - if (!defined('PHPUNIT_RUN') && $userSession->isLoggedIn()) { |
|
851 | - // reset brute force delay for this IP address and username |
|
852 | - $uid = $userSession->getUser()->getUID(); |
|
853 | - $request = Server::get(IRequest::class); |
|
854 | - $throttler = Server::get(\OC\Security\Bruteforce\Throttler::class); |
|
855 | - $throttler->resetDelay($request->getRemoteAddress(), 'login', ['user' => $uid]); |
|
856 | - } |
|
857 | - |
|
858 | - try { |
|
859 | - $cache = new \OC\Cache\File(); |
|
860 | - $cache->gc(); |
|
861 | - } catch (\OC\ServerNotAvailableException $e) { |
|
862 | - // not a GC exception, pass it on |
|
863 | - throw $e; |
|
864 | - } catch (\OC\ForbiddenException $e) { |
|
865 | - // filesystem blocked for this request, ignore |
|
866 | - } catch (\Exception $e) { |
|
867 | - // a GC exception should not prevent users from using OC, |
|
868 | - // so log the exception |
|
869 | - Server::get(LoggerInterface::class)->warning('Exception when running cache gc.', [ |
|
870 | - 'app' => 'core', |
|
871 | - 'exception' => $e, |
|
872 | - ]); |
|
873 | - } |
|
874 | - }); |
|
875 | - } |
|
876 | - } |
|
877 | - |
|
878 | - private static function registerEncryptionWrapperAndHooks(): void { |
|
879 | - $manager = Server::get(\OCP\Encryption\IManager::class); |
|
880 | - \OCP\Util::connectHook('OC_Filesystem', 'preSetup', $manager, 'setupStorage'); |
|
881 | - |
|
882 | - $enabled = $manager->isEnabled(); |
|
883 | - if ($enabled) { |
|
884 | - \OCP\Util::connectHook(Share::class, 'post_shared', HookManager::class, 'postShared'); |
|
885 | - \OCP\Util::connectHook(Share::class, 'post_unshare', HookManager::class, 'postUnshared'); |
|
886 | - \OCP\Util::connectHook('OC_Filesystem', 'post_rename', HookManager::class, 'postRename'); |
|
887 | - \OCP\Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', HookManager::class, 'postRestore'); |
|
888 | - } |
|
889 | - } |
|
890 | - |
|
891 | - private static function registerAccountHooks(): void { |
|
892 | - /** @var IEventDispatcher $dispatcher */ |
|
893 | - $dispatcher = Server::get(IEventDispatcher::class); |
|
894 | - $dispatcher->addServiceListener(UserChangedEvent::class, \OC\Accounts\Hooks::class); |
|
895 | - } |
|
896 | - |
|
897 | - private static function registerAppRestrictionsHooks(): void { |
|
898 | - /** @var \OC\Group\Manager $groupManager */ |
|
899 | - $groupManager = Server::get(\OCP\IGroupManager::class); |
|
900 | - $groupManager->listen('\OC\Group', 'postDelete', function (\OCP\IGroup $group) { |
|
901 | - $appManager = Server::get(\OCP\App\IAppManager::class); |
|
902 | - $apps = $appManager->getEnabledAppsForGroup($group); |
|
903 | - foreach ($apps as $appId) { |
|
904 | - $restrictions = $appManager->getAppRestriction($appId); |
|
905 | - if (empty($restrictions)) { |
|
906 | - continue; |
|
907 | - } |
|
908 | - $key = array_search($group->getGID(), $restrictions); |
|
909 | - unset($restrictions[$key]); |
|
910 | - $restrictions = array_values($restrictions); |
|
911 | - if (empty($restrictions)) { |
|
912 | - $appManager->disableApp($appId); |
|
913 | - } else { |
|
914 | - $appManager->enableAppForGroups($appId, $restrictions); |
|
915 | - } |
|
916 | - } |
|
917 | - }); |
|
918 | - } |
|
919 | - |
|
920 | - private static function registerResourceCollectionHooks(): void { |
|
921 | - \OC\Collaboration\Resources\Listener::register(Server::get(SymfonyAdapter::class), Server::get(IEventDispatcher::class)); |
|
922 | - } |
|
923 | - |
|
924 | - private static function registerFileReferenceEventListener(): void { |
|
925 | - \OC\Collaboration\Reference\File\FileReferenceEventListener::register(Server::get(IEventDispatcher::class)); |
|
926 | - } |
|
927 | - |
|
928 | - /** |
|
929 | - * register hooks for sharing |
|
930 | - */ |
|
931 | - public static function registerShareHooks(\OC\SystemConfig $systemConfig): void { |
|
932 | - if ($systemConfig->getValue('installed')) { |
|
933 | - OC_Hook::connect('OC_User', 'post_deleteUser', Hooks::class, 'post_deleteUser'); |
|
934 | - OC_Hook::connect('OC_User', 'post_deleteGroup', Hooks::class, 'post_deleteGroup'); |
|
935 | - |
|
936 | - /** @var IEventDispatcher $dispatcher */ |
|
937 | - $dispatcher = Server::get(IEventDispatcher::class); |
|
938 | - $dispatcher->addServiceListener(UserRemovedEvent::class, \OC\Share20\UserRemovedListener::class); |
|
939 | - } |
|
940 | - } |
|
941 | - |
|
942 | - protected static function registerAutoloaderCache(\OC\SystemConfig $systemConfig): void { |
|
943 | - // The class loader takes an optional low-latency cache, which MUST be |
|
944 | - // namespaced. The instanceid is used for namespacing, but might be |
|
945 | - // unavailable at this point. Furthermore, it might not be possible to |
|
946 | - // generate an instanceid via \OC_Util::getInstanceId() because the |
|
947 | - // config file may not be writable. As such, we only register a class |
|
948 | - // loader cache if instanceid is available without trying to create one. |
|
949 | - $instanceId = $systemConfig->getValue('instanceid', null); |
|
950 | - if ($instanceId) { |
|
951 | - try { |
|
952 | - $memcacheFactory = Server::get(\OCP\ICacheFactory::class); |
|
953 | - self::$loader->setMemoryCache($memcacheFactory->createLocal('Autoloader')); |
|
954 | - } catch (\Exception $ex) { |
|
955 | - } |
|
956 | - } |
|
957 | - } |
|
958 | - |
|
959 | - /** |
|
960 | - * Handle the request |
|
961 | - */ |
|
962 | - public static function handleRequest(): void { |
|
963 | - Server::get(\OCP\Diagnostics\IEventLogger::class)->start('handle_request', 'Handle request'); |
|
964 | - $systemConfig = Server::get(\OC\SystemConfig::class); |
|
965 | - |
|
966 | - // Check if Nextcloud is installed or in maintenance (update) mode |
|
967 | - if (!$systemConfig->getValue('installed', false)) { |
|
968 | - \OC::$server->getSession()->clear(); |
|
969 | - $setupHelper = new OC\Setup( |
|
970 | - $systemConfig, |
|
971 | - Server::get(\bantu\IniGetWrapper\IniGetWrapper::class), |
|
972 | - Server::get(\OCP\L10N\IFactory::class)->get('lib'), |
|
973 | - Server::get(\OCP\Defaults::class), |
|
974 | - Server::get(\Psr\Log\LoggerInterface::class), |
|
975 | - Server::get(\OCP\Security\ISecureRandom::class), |
|
976 | - Server::get(\OC\Installer::class) |
|
977 | - ); |
|
978 | - $controller = new OC\Core\Controller\SetupController($setupHelper); |
|
979 | - $controller->run($_POST); |
|
980 | - exit(); |
|
981 | - } |
|
982 | - |
|
983 | - $request = Server::get(IRequest::class); |
|
984 | - $requestPath = $request->getRawPathInfo(); |
|
985 | - if ($requestPath === '/heartbeat') { |
|
986 | - return; |
|
987 | - } |
|
988 | - if (substr($requestPath, -3) !== '.js') { // we need these files during the upgrade |
|
989 | - self::checkMaintenanceMode($systemConfig); |
|
990 | - |
|
991 | - if (\OCP\Util::needUpgrade()) { |
|
992 | - if (function_exists('opcache_reset')) { |
|
993 | - opcache_reset(); |
|
994 | - } |
|
995 | - if (!((bool) $systemConfig->getValue('maintenance', false))) { |
|
996 | - self::printUpgradePage($systemConfig); |
|
997 | - exit(); |
|
998 | - } |
|
999 | - } |
|
1000 | - } |
|
1001 | - |
|
1002 | - // emergency app disabling |
|
1003 | - if ($requestPath === '/disableapp' |
|
1004 | - && $request->getMethod() === 'POST' |
|
1005 | - ) { |
|
1006 | - \OC_JSON::callCheck(); |
|
1007 | - \OC_JSON::checkAdminUser(); |
|
1008 | - $appIds = (array)$request->getParam('appid'); |
|
1009 | - foreach ($appIds as $appId) { |
|
1010 | - $appId = \OC_App::cleanAppId($appId); |
|
1011 | - Server::get(\OCP\App\IAppManager::class)->disableApp($appId); |
|
1012 | - } |
|
1013 | - \OC_JSON::success(); |
|
1014 | - exit(); |
|
1015 | - } |
|
1016 | - |
|
1017 | - // Always load authentication apps |
|
1018 | - OC_App::loadApps(['authentication']); |
|
1019 | - |
|
1020 | - // Load minimum set of apps |
|
1021 | - if (!\OCP\Util::needUpgrade() |
|
1022 | - && !((bool) $systemConfig->getValue('maintenance', false))) { |
|
1023 | - // For logged-in users: Load everything |
|
1024 | - if (Server::get(IUserSession::class)->isLoggedIn()) { |
|
1025 | - OC_App::loadApps(); |
|
1026 | - } else { |
|
1027 | - // For guests: Load only filesystem and logging |
|
1028 | - OC_App::loadApps(['filesystem', 'logging']); |
|
1029 | - |
|
1030 | - // Don't try to login when a client is trying to get a OAuth token. |
|
1031 | - // OAuth needs to support basic auth too, so the login is not valid |
|
1032 | - // inside Nextcloud and the Login exception would ruin it. |
|
1033 | - if ($request->getRawPathInfo() !== '/apps/oauth2/api/v1/token') { |
|
1034 | - self::handleLogin($request); |
|
1035 | - } |
|
1036 | - } |
|
1037 | - } |
|
1038 | - |
|
1039 | - if (!self::$CLI) { |
|
1040 | - try { |
|
1041 | - if (!((bool) $systemConfig->getValue('maintenance', false)) && !\OCP\Util::needUpgrade()) { |
|
1042 | - OC_App::loadApps(['filesystem', 'logging']); |
|
1043 | - OC_App::loadApps(); |
|
1044 | - } |
|
1045 | - Server::get(\OC\Route\Router::class)->match($request->getRawPathInfo()); |
|
1046 | - return; |
|
1047 | - } catch (Symfony\Component\Routing\Exception\ResourceNotFoundException $e) { |
|
1048 | - //header('HTTP/1.0 404 Not Found'); |
|
1049 | - } catch (Symfony\Component\Routing\Exception\MethodNotAllowedException $e) { |
|
1050 | - http_response_code(405); |
|
1051 | - return; |
|
1052 | - } |
|
1053 | - } |
|
1054 | - |
|
1055 | - // Handle WebDAV |
|
1056 | - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND') { |
|
1057 | - // not allowed any more to prevent people |
|
1058 | - // mounting this root directly. |
|
1059 | - // Users need to mount remote.php/webdav instead. |
|
1060 | - http_response_code(405); |
|
1061 | - return; |
|
1062 | - } |
|
1063 | - |
|
1064 | - // Handle requests for JSON or XML |
|
1065 | - $acceptHeader = $request->getHeader('Accept'); |
|
1066 | - if (in_array($acceptHeader, ['application/json', 'application/xml'], true)) { |
|
1067 | - http_response_code(404); |
|
1068 | - return; |
|
1069 | - } |
|
1070 | - |
|
1071 | - // Handle resources that can't be found |
|
1072 | - // This prevents browsers from redirecting to the default page and then |
|
1073 | - // attempting to parse HTML as CSS and similar. |
|
1074 | - $destinationHeader = $request->getHeader('Sec-Fetch-Dest'); |
|
1075 | - if (in_array($destinationHeader, ['font', 'script', 'style'])) { |
|
1076 | - http_response_code(404); |
|
1077 | - return; |
|
1078 | - } |
|
1079 | - |
|
1080 | - // Redirect to the default app or login only as an entry point |
|
1081 | - if ($requestPath === '') { |
|
1082 | - // Someone is logged in |
|
1083 | - if (Server::get(IUserSession::class)->isLoggedIn()) { |
|
1084 | - header('Location: ' . Server::get(IURLGenerator::class)->linkToDefaultPageUrl()); |
|
1085 | - } else { |
|
1086 | - // Not handled and not logged in |
|
1087 | - header('Location: ' . Server::get(IURLGenerator::class)->linkToRouteAbsolute('core.login.showLoginForm')); |
|
1088 | - } |
|
1089 | - return; |
|
1090 | - } |
|
1091 | - |
|
1092 | - try { |
|
1093 | - Server::get(\OC\Route\Router::class)->match('/error/404'); |
|
1094 | - } catch (\Exception $e) { |
|
1095 | - logger('core')->emergency($e->getMessage(), ['exception' => $e]); |
|
1096 | - $l = Server::get(\OCP\L10N\IFactory::class)->get('lib'); |
|
1097 | - OC_Template::printErrorPage( |
|
1098 | - $l->t('404'), |
|
1099 | - $l->t('The page could not be found on the server.'), |
|
1100 | - 404 |
|
1101 | - ); |
|
1102 | - } |
|
1103 | - } |
|
1104 | - |
|
1105 | - /** |
|
1106 | - * Check login: apache auth, auth token, basic auth |
|
1107 | - */ |
|
1108 | - public static function handleLogin(OCP\IRequest $request): bool { |
|
1109 | - $userSession = Server::get(\OC\User\Session::class); |
|
1110 | - if (OC_User::handleApacheAuth()) { |
|
1111 | - return true; |
|
1112 | - } |
|
1113 | - if ($userSession->tryTokenLogin($request)) { |
|
1114 | - return true; |
|
1115 | - } |
|
1116 | - if (isset($_COOKIE['nc_username']) |
|
1117 | - && isset($_COOKIE['nc_token']) |
|
1118 | - && isset($_COOKIE['nc_session_id']) |
|
1119 | - && $userSession->loginWithCookie($_COOKIE['nc_username'], $_COOKIE['nc_token'], $_COOKIE['nc_session_id'])) { |
|
1120 | - return true; |
|
1121 | - } |
|
1122 | - if ($userSession->tryBasicAuthLogin($request, Server::get(\OC\Security\Bruteforce\Throttler::class))) { |
|
1123 | - return true; |
|
1124 | - } |
|
1125 | - return false; |
|
1126 | - } |
|
1127 | - |
|
1128 | - protected static function handleAuthHeaders(): void { |
|
1129 | - //copy http auth headers for apache+php-fcgid work around |
|
1130 | - if (isset($_SERVER['HTTP_XAUTHORIZATION']) && !isset($_SERVER['HTTP_AUTHORIZATION'])) { |
|
1131 | - $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['HTTP_XAUTHORIZATION']; |
|
1132 | - } |
|
1133 | - |
|
1134 | - // Extract PHP_AUTH_USER/PHP_AUTH_PW from other headers if necessary. |
|
1135 | - $vars = [ |
|
1136 | - 'HTTP_AUTHORIZATION', // apache+php-cgi work around |
|
1137 | - 'REDIRECT_HTTP_AUTHORIZATION', // apache+php-cgi alternative |
|
1138 | - ]; |
|
1139 | - foreach ($vars as $var) { |
|
1140 | - if (isset($_SERVER[$var]) && is_string($_SERVER[$var]) && preg_match('/Basic\s+(.*)$/i', $_SERVER[$var], $matches)) { |
|
1141 | - $credentials = explode(':', base64_decode($matches[1]), 2); |
|
1142 | - if (count($credentials) === 2) { |
|
1143 | - $_SERVER['PHP_AUTH_USER'] = $credentials[0]; |
|
1144 | - $_SERVER['PHP_AUTH_PW'] = $credentials[1]; |
|
1145 | - break; |
|
1146 | - } |
|
1147 | - } |
|
1148 | - } |
|
1149 | - } |
|
93 | + /** |
|
94 | + * Associative array for autoloading. classname => filename |
|
95 | + */ |
|
96 | + public static array $CLASSPATH = []; |
|
97 | + /** |
|
98 | + * The installation path for Nextcloud on the server (e.g. /srv/http/nextcloud) |
|
99 | + */ |
|
100 | + public static string $SERVERROOT = ''; |
|
101 | + /** |
|
102 | + * the current request path relative to the Nextcloud root (e.g. files/index.php) |
|
103 | + */ |
|
104 | + private static string $SUBURI = ''; |
|
105 | + /** |
|
106 | + * the Nextcloud root path for http requests (e.g. nextcloud/) |
|
107 | + */ |
|
108 | + public static string $WEBROOT = ''; |
|
109 | + /** |
|
110 | + * The installation path array of the apps folder on the server (e.g. /srv/http/nextcloud) 'path' and |
|
111 | + * web path in 'url' |
|
112 | + */ |
|
113 | + public static array $APPSROOTS = []; |
|
114 | + |
|
115 | + public static string $configDir; |
|
116 | + |
|
117 | + /** |
|
118 | + * requested app |
|
119 | + */ |
|
120 | + public static string $REQUESTEDAPP = ''; |
|
121 | + |
|
122 | + /** |
|
123 | + * check if Nextcloud runs in cli mode |
|
124 | + */ |
|
125 | + public static bool $CLI = false; |
|
126 | + |
|
127 | + public static \OC\Autoloader $loader; |
|
128 | + |
|
129 | + public static \Composer\Autoload\ClassLoader $composerAutoloader; |
|
130 | + |
|
131 | + public static \OC\Server $server; |
|
132 | + |
|
133 | + private static \OC\Config $config; |
|
134 | + |
|
135 | + /** |
|
136 | + * @throws \RuntimeException when the 3rdparty directory is missing or |
|
137 | + * the app path list is empty or contains an invalid path |
|
138 | + */ |
|
139 | + public static function initPaths(): void { |
|
140 | + if (defined('PHPUNIT_CONFIG_DIR')) { |
|
141 | + self::$configDir = OC::$SERVERROOT . '/' . PHPUNIT_CONFIG_DIR . '/'; |
|
142 | + } elseif (defined('PHPUNIT_RUN') and PHPUNIT_RUN and is_dir(OC::$SERVERROOT . '/tests/config/')) { |
|
143 | + self::$configDir = OC::$SERVERROOT . '/tests/config/'; |
|
144 | + } elseif ($dir = getenv('NEXTCLOUD_CONFIG_DIR')) { |
|
145 | + self::$configDir = rtrim($dir, '/') . '/'; |
|
146 | + } else { |
|
147 | + self::$configDir = OC::$SERVERROOT . '/config/'; |
|
148 | + } |
|
149 | + self::$config = new \OC\Config(self::$configDir); |
|
150 | + |
|
151 | + OC::$SUBURI = str_replace("\\", "/", substr(realpath($_SERVER["SCRIPT_FILENAME"] ?? ''), strlen(OC::$SERVERROOT))); |
|
152 | + /** |
|
153 | + * FIXME: The following lines are required because we can't yet instantiate |
|
154 | + * Server::get(\OCP\IRequest::class) since \OC::$server does not yet exist. |
|
155 | + */ |
|
156 | + $params = [ |
|
157 | + 'server' => [ |
|
158 | + 'SCRIPT_NAME' => $_SERVER['SCRIPT_NAME'] ?? null, |
|
159 | + 'SCRIPT_FILENAME' => $_SERVER['SCRIPT_FILENAME'] ?? null, |
|
160 | + ], |
|
161 | + ]; |
|
162 | + $fakeRequest = new \OC\AppFramework\Http\Request( |
|
163 | + $params, |
|
164 | + new \OC\AppFramework\Http\RequestId($_SERVER['UNIQUE_ID'] ?? '', new \OC\Security\SecureRandom()), |
|
165 | + new \OC\AllConfig(new \OC\SystemConfig(self::$config)) |
|
166 | + ); |
|
167 | + $scriptName = $fakeRequest->getScriptName(); |
|
168 | + if (substr($scriptName, -1) == '/') { |
|
169 | + $scriptName .= 'index.php'; |
|
170 | + //make sure suburi follows the same rules as scriptName |
|
171 | + if (substr(OC::$SUBURI, -9) != 'index.php') { |
|
172 | + if (substr(OC::$SUBURI, -1) != '/') { |
|
173 | + OC::$SUBURI = OC::$SUBURI . '/'; |
|
174 | + } |
|
175 | + OC::$SUBURI = OC::$SUBURI . 'index.php'; |
|
176 | + } |
|
177 | + } |
|
178 | + |
|
179 | + |
|
180 | + if (OC::$CLI) { |
|
181 | + OC::$WEBROOT = self::$config->getValue('overwritewebroot', ''); |
|
182 | + } else { |
|
183 | + if (substr($scriptName, 0 - strlen(OC::$SUBURI)) === OC::$SUBURI) { |
|
184 | + OC::$WEBROOT = substr($scriptName, 0, 0 - strlen(OC::$SUBURI)); |
|
185 | + |
|
186 | + if (OC::$WEBROOT != '' && OC::$WEBROOT[0] !== '/') { |
|
187 | + OC::$WEBROOT = '/' . OC::$WEBROOT; |
|
188 | + } |
|
189 | + } else { |
|
190 | + // The scriptName is not ending with OC::$SUBURI |
|
191 | + // This most likely means that we are calling from CLI. |
|
192 | + // However some cron jobs still need to generate |
|
193 | + // a web URL, so we use overwritewebroot as a fallback. |
|
194 | + OC::$WEBROOT = self::$config->getValue('overwritewebroot', ''); |
|
195 | + } |
|
196 | + |
|
197 | + // Resolve /nextcloud to /nextcloud/ to ensure to always have a trailing |
|
198 | + // slash which is required by URL generation. |
|
199 | + if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] === \OC::$WEBROOT && |
|
200 | + substr($_SERVER['REQUEST_URI'], -1) !== '/') { |
|
201 | + header('Location: '.\OC::$WEBROOT.'/'); |
|
202 | + exit(); |
|
203 | + } |
|
204 | + } |
|
205 | + |
|
206 | + // search the apps folder |
|
207 | + $config_paths = self::$config->getValue('apps_paths', []); |
|
208 | + if (!empty($config_paths)) { |
|
209 | + foreach ($config_paths as $paths) { |
|
210 | + if (isset($paths['url']) && isset($paths['path'])) { |
|
211 | + $paths['url'] = rtrim($paths['url'], '/'); |
|
212 | + $paths['path'] = rtrim($paths['path'], '/'); |
|
213 | + OC::$APPSROOTS[] = $paths; |
|
214 | + } |
|
215 | + } |
|
216 | + } elseif (file_exists(OC::$SERVERROOT . '/apps')) { |
|
217 | + OC::$APPSROOTS[] = ['path' => OC::$SERVERROOT . '/apps', 'url' => '/apps', 'writable' => true]; |
|
218 | + } |
|
219 | + |
|
220 | + if (empty(OC::$APPSROOTS)) { |
|
221 | + throw new \RuntimeException('apps directory not found! Please put the Nextcloud apps folder in the Nextcloud folder' |
|
222 | + . '. You can also configure the location in the config.php file.'); |
|
223 | + } |
|
224 | + $paths = []; |
|
225 | + foreach (OC::$APPSROOTS as $path) { |
|
226 | + $paths[] = $path['path']; |
|
227 | + if (!is_dir($path['path'])) { |
|
228 | + throw new \RuntimeException(sprintf('App directory "%s" not found! Please put the Nextcloud apps folder in the' |
|
229 | + . ' Nextcloud folder. You can also configure the location in the config.php file.', $path['path'])); |
|
230 | + } |
|
231 | + } |
|
232 | + |
|
233 | + // set the right include path |
|
234 | + set_include_path( |
|
235 | + implode(PATH_SEPARATOR, $paths) |
|
236 | + ); |
|
237 | + } |
|
238 | + |
|
239 | + public static function checkConfig(): void { |
|
240 | + $l = Server::get(\OCP\L10N\IFactory::class)->get('lib'); |
|
241 | + |
|
242 | + // Create config if it does not already exist |
|
243 | + $configFilePath = self::$configDir .'/config.php'; |
|
244 | + if (!file_exists($configFilePath)) { |
|
245 | + @touch($configFilePath); |
|
246 | + } |
|
247 | + |
|
248 | + // Check if config is writable |
|
249 | + $configFileWritable = is_writable($configFilePath); |
|
250 | + if (!$configFileWritable && !OC_Helper::isReadOnlyConfigEnabled() |
|
251 | + || !$configFileWritable && \OCP\Util::needUpgrade()) { |
|
252 | + $urlGenerator = Server::get(IURLGenerator::class); |
|
253 | + |
|
254 | + if (self::$CLI) { |
|
255 | + echo $l->t('Cannot write into "config" directory!')."\n"; |
|
256 | + echo $l->t('This can usually be fixed by giving the web server write access to the config directory.')."\n"; |
|
257 | + echo "\n"; |
|
258 | + 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"; |
|
259 | + echo $l->t('See %s', [ $urlGenerator->linkToDocs('admin-config') ])."\n"; |
|
260 | + exit; |
|
261 | + } else { |
|
262 | + OC_Template::printErrorPage( |
|
263 | + $l->t('Cannot write into "config" directory!'), |
|
264 | + $l->t('This can usually be fixed by giving the web server write access to the config directory.') . ' ' |
|
265 | + . $l->t('But, if you prefer to keep config.php file read only, set the option "config_is_read_only" to true in it.') . ' ' |
|
266 | + . $l->t('See %s', [ $urlGenerator->linkToDocs('admin-config') ]), |
|
267 | + 503 |
|
268 | + ); |
|
269 | + } |
|
270 | + } |
|
271 | + } |
|
272 | + |
|
273 | + public static function checkInstalled(\OC\SystemConfig $systemConfig): void { |
|
274 | + if (defined('OC_CONSOLE')) { |
|
275 | + return; |
|
276 | + } |
|
277 | + // Redirect to installer if not installed |
|
278 | + if (!$systemConfig->getValue('installed', false) && OC::$SUBURI !== '/index.php' && OC::$SUBURI !== '/status.php') { |
|
279 | + if (OC::$CLI) { |
|
280 | + throw new Exception('Not installed'); |
|
281 | + } else { |
|
282 | + $url = OC::$WEBROOT . '/index.php'; |
|
283 | + header('Location: ' . $url); |
|
284 | + } |
|
285 | + exit(); |
|
286 | + } |
|
287 | + } |
|
288 | + |
|
289 | + public static function checkMaintenanceMode(\OC\SystemConfig $systemConfig): void { |
|
290 | + // Allow ajax update script to execute without being stopped |
|
291 | + if (((bool) $systemConfig->getValue('maintenance', false)) && OC::$SUBURI != '/core/ajax/update.php') { |
|
292 | + // send http status 503 |
|
293 | + http_response_code(503); |
|
294 | + header('X-Nextcloud-Maintenance-Mode: 1'); |
|
295 | + header('Retry-After: 120'); |
|
296 | + |
|
297 | + // render error page |
|
298 | + $template = new OC_Template('', 'update.user', 'guest'); |
|
299 | + \OCP\Util::addScript('core', 'maintenance'); |
|
300 | + \OCP\Util::addStyle('core', 'guest'); |
|
301 | + $template->printPage(); |
|
302 | + die(); |
|
303 | + } |
|
304 | + } |
|
305 | + |
|
306 | + /** |
|
307 | + * Prints the upgrade page |
|
308 | + */ |
|
309 | + private static function printUpgradePage(\OC\SystemConfig $systemConfig): void { |
|
310 | + $disableWebUpdater = $systemConfig->getValue('upgrade.disable-web', false); |
|
311 | + $tooBig = false; |
|
312 | + if (!$disableWebUpdater) { |
|
313 | + $apps = Server::get(\OCP\App\IAppManager::class); |
|
314 | + if ($apps->isInstalled('user_ldap')) { |
|
315 | + $qb = Server::get(\OCP\IDBConnection::class)->getQueryBuilder(); |
|
316 | + |
|
317 | + $result = $qb->select($qb->func()->count('*', 'user_count')) |
|
318 | + ->from('ldap_user_mapping') |
|
319 | + ->executeQuery(); |
|
320 | + $row = $result->fetch(); |
|
321 | + $result->closeCursor(); |
|
322 | + |
|
323 | + $tooBig = ($row['user_count'] > 50); |
|
324 | + } |
|
325 | + if (!$tooBig && $apps->isInstalled('user_saml')) { |
|
326 | + $qb = Server::get(\OCP\IDBConnection::class)->getQueryBuilder(); |
|
327 | + |
|
328 | + $result = $qb->select($qb->func()->count('*', 'user_count')) |
|
329 | + ->from('user_saml_users') |
|
330 | + ->executeQuery(); |
|
331 | + $row = $result->fetch(); |
|
332 | + $result->closeCursor(); |
|
333 | + |
|
334 | + $tooBig = ($row['user_count'] > 50); |
|
335 | + } |
|
336 | + if (!$tooBig) { |
|
337 | + // count users |
|
338 | + $stats = Server::get(\OCP\IUserManager::class)->countUsers(); |
|
339 | + $totalUsers = array_sum($stats); |
|
340 | + $tooBig = ($totalUsers > 50); |
|
341 | + } |
|
342 | + } |
|
343 | + $ignoreTooBigWarning = isset($_GET['IKnowThatThisIsABigInstanceAndTheUpdateRequestCouldRunIntoATimeoutAndHowToRestoreABackup']) && |
|
344 | + $_GET['IKnowThatThisIsABigInstanceAndTheUpdateRequestCouldRunIntoATimeoutAndHowToRestoreABackup'] === 'IAmSuperSureToDoThis'; |
|
345 | + |
|
346 | + if ($disableWebUpdater || ($tooBig && !$ignoreTooBigWarning)) { |
|
347 | + // send http status 503 |
|
348 | + http_response_code(503); |
|
349 | + header('Retry-After: 120'); |
|
350 | + |
|
351 | + // render error page |
|
352 | + $template = new OC_Template('', 'update.use-cli', 'guest'); |
|
353 | + $template->assign('productName', 'nextcloud'); // for now |
|
354 | + $template->assign('version', OC_Util::getVersionString()); |
|
355 | + $template->assign('tooBig', $tooBig); |
|
356 | + |
|
357 | + $template->printPage(); |
|
358 | + die(); |
|
359 | + } |
|
360 | + |
|
361 | + // check whether this is a core update or apps update |
|
362 | + $installedVersion = $systemConfig->getValue('version', '0.0.0'); |
|
363 | + $currentVersion = implode('.', \OCP\Util::getVersion()); |
|
364 | + |
|
365 | + // if not a core upgrade, then it's apps upgrade |
|
366 | + $isAppsOnlyUpgrade = version_compare($currentVersion, $installedVersion, '='); |
|
367 | + |
|
368 | + $oldTheme = $systemConfig->getValue('theme'); |
|
369 | + $systemConfig->setValue('theme', ''); |
|
370 | + \OCP\Util::addScript('core', 'common'); |
|
371 | + \OCP\Util::addScript('core', 'main'); |
|
372 | + \OCP\Util::addTranslations('core'); |
|
373 | + \OCP\Util::addScript('core', 'update'); |
|
374 | + |
|
375 | + /** @var \OC\App\AppManager $appManager */ |
|
376 | + $appManager = Server::get(\OCP\App\IAppManager::class); |
|
377 | + |
|
378 | + $tmpl = new OC_Template('', 'update.admin', 'guest'); |
|
379 | + $tmpl->assign('version', OC_Util::getVersionString()); |
|
380 | + $tmpl->assign('isAppsOnlyUpgrade', $isAppsOnlyUpgrade); |
|
381 | + |
|
382 | + // get third party apps |
|
383 | + $ocVersion = \OCP\Util::getVersion(); |
|
384 | + $ocVersion = implode('.', $ocVersion); |
|
385 | + $incompatibleApps = $appManager->getIncompatibleApps($ocVersion); |
|
386 | + $incompatibleShippedApps = []; |
|
387 | + foreach ($incompatibleApps as $appInfo) { |
|
388 | + if ($appManager->isShipped($appInfo['id'])) { |
|
389 | + $incompatibleShippedApps[] = $appInfo['name'] . ' (' . $appInfo['id'] . ')'; |
|
390 | + } |
|
391 | + } |
|
392 | + |
|
393 | + if (!empty($incompatibleShippedApps)) { |
|
394 | + $l = Server::get(\OCP\L10N\IFactory::class)->get('core'); |
|
395 | + $hint = $l->t('The files of the app %1$s were not replaced correctly. Make sure it is a version compatible with the server.', [implode(', ', $incompatibleShippedApps)]); |
|
396 | + throw new \OCP\HintException('The files of the app ' . implode(', ', $incompatibleShippedApps) . ' were not replaced correctly. Make sure it is a version compatible with the server.', $hint); |
|
397 | + } |
|
398 | + |
|
399 | + $tmpl->assign('appsToUpgrade', $appManager->getAppsNeedingUpgrade($ocVersion)); |
|
400 | + $tmpl->assign('incompatibleAppsList', $incompatibleApps); |
|
401 | + try { |
|
402 | + $defaults = new \OC_Defaults(); |
|
403 | + $tmpl->assign('productName', $defaults->getName()); |
|
404 | + } catch (Throwable $error) { |
|
405 | + $tmpl->assign('productName', 'Nextcloud'); |
|
406 | + } |
|
407 | + $tmpl->assign('oldTheme', $oldTheme); |
|
408 | + $tmpl->printPage(); |
|
409 | + } |
|
410 | + |
|
411 | + public static function initSession(): void { |
|
412 | + $request = Server::get(IRequest::class); |
|
413 | + $isDavRequest = strpos($request->getRequestUri(), '/remote.php/dav') === 0 || strpos($request->getRequestUri(), '/remote.php/webdav') === 0; |
|
414 | + if ($request->getHeader('Authorization') !== '' && is_null($request->getCookie('cookie_test')) && $isDavRequest) { |
|
415 | + setcookie('cookie_test', 'test', time() + 3600); |
|
416 | + // Do not initialize the session if a request is authenticated directly |
|
417 | + // unless there is a session cookie already sent along |
|
418 | + return; |
|
419 | + } |
|
420 | + |
|
421 | + if ($request->getServerProtocol() === 'https') { |
|
422 | + ini_set('session.cookie_secure', 'true'); |
|
423 | + } |
|
424 | + |
|
425 | + // prevents javascript from accessing php session cookies |
|
426 | + ini_set('session.cookie_httponly', 'true'); |
|
427 | + |
|
428 | + // set the cookie path to the Nextcloud directory |
|
429 | + $cookie_path = OC::$WEBROOT ? : '/'; |
|
430 | + ini_set('session.cookie_path', $cookie_path); |
|
431 | + |
|
432 | + // Let the session name be changed in the initSession Hook |
|
433 | + $sessionName = OC_Util::getInstanceId(); |
|
434 | + |
|
435 | + try { |
|
436 | + // set the session name to the instance id - which is unique |
|
437 | + $session = new \OC\Session\Internal($sessionName); |
|
438 | + |
|
439 | + $cryptoWrapper = Server::get(\OC\Session\CryptoWrapper::class); |
|
440 | + $session = $cryptoWrapper->wrapSession($session); |
|
441 | + self::$server->setSession($session); |
|
442 | + |
|
443 | + // if session can't be started break with http 500 error |
|
444 | + } catch (Exception $e) { |
|
445 | + Server::get(LoggerInterface::class)->error($e->getMessage(), ['app' => 'base','exception' => $e]); |
|
446 | + //show the user a detailed error page |
|
447 | + OC_Template::printExceptionErrorPage($e, 500); |
|
448 | + die(); |
|
449 | + } |
|
450 | + |
|
451 | + //try to set the session lifetime |
|
452 | + $sessionLifeTime = self::getSessionLifeTime(); |
|
453 | + @ini_set('gc_maxlifetime', (string)$sessionLifeTime); |
|
454 | + |
|
455 | + // session timeout |
|
456 | + if ($session->exists('LAST_ACTIVITY') && (time() - $session->get('LAST_ACTIVITY') > $sessionLifeTime)) { |
|
457 | + if (isset($_COOKIE[session_name()])) { |
|
458 | + setcookie(session_name(), '', -1, self::$WEBROOT ? : '/'); |
|
459 | + } |
|
460 | + Server::get(IUserSession::class)->logout(); |
|
461 | + } |
|
462 | + |
|
463 | + if (!self::hasSessionRelaxedExpiry()) { |
|
464 | + $session->set('LAST_ACTIVITY', time()); |
|
465 | + } |
|
466 | + $session->close(); |
|
467 | + } |
|
468 | + |
|
469 | + private static function getSessionLifeTime(): int { |
|
470 | + return Server::get(\OC\AllConfig::class)->getSystemValueInt('session_lifetime', 60 * 60 * 24); |
|
471 | + } |
|
472 | + |
|
473 | + /** |
|
474 | + * @return bool true if the session expiry should only be done by gc instead of an explicit timeout |
|
475 | + */ |
|
476 | + public static function hasSessionRelaxedExpiry(): bool { |
|
477 | + return Server::get(\OC\AllConfig::class)->getSystemValueBool('session_relaxed_expiry', false); |
|
478 | + } |
|
479 | + |
|
480 | + /** |
|
481 | + * Try to set some values to the required Nextcloud default |
|
482 | + */ |
|
483 | + public static function setRequiredIniValues(): void { |
|
484 | + @ini_set('default_charset', 'UTF-8'); |
|
485 | + @ini_set('gd.jpeg_ignore_warning', '1'); |
|
486 | + } |
|
487 | + |
|
488 | + /** |
|
489 | + * Send the same site cookies |
|
490 | + */ |
|
491 | + private static function sendSameSiteCookies(): void { |
|
492 | + $cookieParams = session_get_cookie_params(); |
|
493 | + $secureCookie = ($cookieParams['secure'] === true) ? 'secure; ' : ''; |
|
494 | + $policies = [ |
|
495 | + 'lax', |
|
496 | + 'strict', |
|
497 | + ]; |
|
498 | + |
|
499 | + // Append __Host to the cookie if it meets the requirements |
|
500 | + $cookiePrefix = ''; |
|
501 | + if ($cookieParams['secure'] === true && $cookieParams['path'] === '/') { |
|
502 | + $cookiePrefix = '__Host-'; |
|
503 | + } |
|
504 | + |
|
505 | + foreach ($policies as $policy) { |
|
506 | + header( |
|
507 | + sprintf( |
|
508 | + 'Set-Cookie: %snc_sameSiteCookie%s=true; path=%s; httponly;' . $secureCookie . 'expires=Fri, 31-Dec-2100 23:59:59 GMT; SameSite=%s', |
|
509 | + $cookiePrefix, |
|
510 | + $policy, |
|
511 | + $cookieParams['path'], |
|
512 | + $policy |
|
513 | + ), |
|
514 | + false |
|
515 | + ); |
|
516 | + } |
|
517 | + } |
|
518 | + |
|
519 | + /** |
|
520 | + * Same Site cookie to further mitigate CSRF attacks. This cookie has to |
|
521 | + * be set in every request if cookies are sent to add a second level of |
|
522 | + * defense against CSRF. |
|
523 | + * |
|
524 | + * If the cookie is not sent this will set the cookie and reload the page. |
|
525 | + * We use an additional cookie since we want to protect logout CSRF and |
|
526 | + * also we can't directly interfere with PHP's session mechanism. |
|
527 | + */ |
|
528 | + private static function performSameSiteCookieProtection(\OCP\IConfig $config): void { |
|
529 | + $request = Server::get(IRequest::class); |
|
530 | + |
|
531 | + // Some user agents are notorious and don't really properly follow HTTP |
|
532 | + // specifications. For those, have an automated opt-out. Since the protection |
|
533 | + // for remote.php is applied in base.php as starting point we need to opt out |
|
534 | + // here. |
|
535 | + $incompatibleUserAgents = $config->getSystemValue('csrf.optout'); |
|
536 | + |
|
537 | + // Fallback, if csrf.optout is unset |
|
538 | + if (!is_array($incompatibleUserAgents)) { |
|
539 | + $incompatibleUserAgents = [ |
|
540 | + // OS X Finder |
|
541 | + '/^WebDAVFS/', |
|
542 | + // Windows webdav drive |
|
543 | + '/^Microsoft-WebDAV-MiniRedir/', |
|
544 | + ]; |
|
545 | + } |
|
546 | + |
|
547 | + if ($request->isUserAgent($incompatibleUserAgents)) { |
|
548 | + return; |
|
549 | + } |
|
550 | + |
|
551 | + if (count($_COOKIE) > 0) { |
|
552 | + $requestUri = $request->getScriptName(); |
|
553 | + $processingScript = explode('/', $requestUri); |
|
554 | + $processingScript = $processingScript[count($processingScript) - 1]; |
|
555 | + |
|
556 | + // index.php routes are handled in the middleware |
|
557 | + if ($processingScript === 'index.php') { |
|
558 | + return; |
|
559 | + } |
|
560 | + |
|
561 | + // All other endpoints require the lax and the strict cookie |
|
562 | + if (!$request->passesStrictCookieCheck()) { |
|
563 | + self::sendSameSiteCookies(); |
|
564 | + // Debug mode gets access to the resources without strict cookie |
|
565 | + // due to the fact that the SabreDAV browser also lives there. |
|
566 | + if (!$config->getSystemValue('debug', false)) { |
|
567 | + http_response_code(\OCP\AppFramework\Http::STATUS_SERVICE_UNAVAILABLE); |
|
568 | + exit(); |
|
569 | + } |
|
570 | + } |
|
571 | + } elseif (!isset($_COOKIE['nc_sameSiteCookielax']) || !isset($_COOKIE['nc_sameSiteCookiestrict'])) { |
|
572 | + self::sendSameSiteCookies(); |
|
573 | + } |
|
574 | + } |
|
575 | + |
|
576 | + public static function init(): void { |
|
577 | + // calculate the root directories |
|
578 | + OC::$SERVERROOT = str_replace("\\", '/', substr(__DIR__, 0, -4)); |
|
579 | + |
|
580 | + // register autoloader |
|
581 | + $loaderStart = microtime(true); |
|
582 | + require_once __DIR__ . '/autoloader.php'; |
|
583 | + self::$loader = new \OC\Autoloader([ |
|
584 | + OC::$SERVERROOT . '/lib/private/legacy', |
|
585 | + ]); |
|
586 | + if (defined('PHPUNIT_RUN')) { |
|
587 | + self::$loader->addValidRoot(OC::$SERVERROOT . '/tests'); |
|
588 | + } |
|
589 | + spl_autoload_register([self::$loader, 'load']); |
|
590 | + $loaderEnd = microtime(true); |
|
591 | + |
|
592 | + self::$CLI = (php_sapi_name() == 'cli'); |
|
593 | + |
|
594 | + // Add default composer PSR-4 autoloader |
|
595 | + self::$composerAutoloader = require_once OC::$SERVERROOT . '/lib/composer/autoload.php'; |
|
596 | + self::$composerAutoloader->setApcuPrefix('composer_autoload'); |
|
597 | + |
|
598 | + try { |
|
599 | + self::initPaths(); |
|
600 | + // setup 3rdparty autoloader |
|
601 | + $vendorAutoLoad = OC::$SERVERROOT. '/3rdparty/autoload.php'; |
|
602 | + if (!file_exists($vendorAutoLoad)) { |
|
603 | + 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".'); |
|
604 | + } |
|
605 | + require_once $vendorAutoLoad; |
|
606 | + } catch (\RuntimeException $e) { |
|
607 | + if (!self::$CLI) { |
|
608 | + http_response_code(503); |
|
609 | + } |
|
610 | + // we can't use the template error page here, because this needs the |
|
611 | + // DI container which isn't available yet |
|
612 | + print($e->getMessage()); |
|
613 | + exit(); |
|
614 | + } |
|
615 | + |
|
616 | + // setup the basic server |
|
617 | + self::$server = new \OC\Server(\OC::$WEBROOT, self::$config); |
|
618 | + self::$server->boot(); |
|
619 | + |
|
620 | + $eventLogger = Server::get(\OCP\Diagnostics\IEventLogger::class); |
|
621 | + $eventLogger->log('autoloader', 'Autoloader', $loaderStart, $loaderEnd); |
|
622 | + $eventLogger->start('boot', 'Initialize'); |
|
623 | + |
|
624 | + // Override php.ini and log everything if we're troubleshooting |
|
625 | + if (self::$config->getValue('loglevel') === ILogger::DEBUG) { |
|
626 | + error_reporting(E_ALL); |
|
627 | + } |
|
628 | + |
|
629 | + // Don't display errors and log them |
|
630 | + @ini_set('display_errors', '0'); |
|
631 | + @ini_set('log_errors', '1'); |
|
632 | + |
|
633 | + if (!date_default_timezone_set('UTC')) { |
|
634 | + throw new \RuntimeException('Could not set timezone to UTC'); |
|
635 | + } |
|
636 | + |
|
637 | + |
|
638 | + //try to configure php to enable big file uploads. |
|
639 | + //this doesn´t work always depending on the webserver and php configuration. |
|
640 | + //Let´s try to overwrite some defaults if they are smaller than 1 hour |
|
641 | + |
|
642 | + if (intval(@ini_get('max_execution_time') ?? 0) < 3600) { |
|
643 | + @ini_set('max_execution_time', strval(3600)); |
|
644 | + } |
|
645 | + |
|
646 | + if (intval(@ini_get('max_input_time') ?? 0) < 3600) { |
|
647 | + @ini_set('max_input_time', strval(3600)); |
|
648 | + } |
|
649 | + |
|
650 | + //try to set the maximum execution time to the largest time limit we have |
|
651 | + if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) { |
|
652 | + @set_time_limit(max(intval(@ini_get('max_execution_time')), intval(@ini_get('max_input_time')))); |
|
653 | + } |
|
654 | + |
|
655 | + self::setRequiredIniValues(); |
|
656 | + self::handleAuthHeaders(); |
|
657 | + $systemConfig = Server::get(\OC\SystemConfig::class); |
|
658 | + self::registerAutoloaderCache($systemConfig); |
|
659 | + |
|
660 | + // initialize intl fallback if necessary |
|
661 | + OC_Util::isSetLocaleWorking(); |
|
662 | + |
|
663 | + $config = Server::get(\OCP\IConfig::class); |
|
664 | + if (!defined('PHPUNIT_RUN')) { |
|
665 | + $errorHandler = new OC\Log\ErrorHandler( |
|
666 | + \OCP\Server::get(\Psr\Log\LoggerInterface::class), |
|
667 | + ); |
|
668 | + $exceptionHandler = [$errorHandler, 'onException']; |
|
669 | + if ($config->getSystemValue('debug', false)) { |
|
670 | + set_error_handler([$errorHandler, 'onAll'], E_ALL); |
|
671 | + if (\OC::$CLI) { |
|
672 | + $exceptionHandler = ['OC_Template', 'printExceptionErrorPage']; |
|
673 | + } |
|
674 | + } else { |
|
675 | + set_error_handler([$errorHandler, 'onError']); |
|
676 | + } |
|
677 | + register_shutdown_function([$errorHandler, 'onShutdown']); |
|
678 | + set_exception_handler($exceptionHandler); |
|
679 | + } |
|
680 | + |
|
681 | + /** @var \OC\AppFramework\Bootstrap\Coordinator $bootstrapCoordinator */ |
|
682 | + $bootstrapCoordinator = Server::get(\OC\AppFramework\Bootstrap\Coordinator::class); |
|
683 | + $bootstrapCoordinator->runInitialRegistration(); |
|
684 | + |
|
685 | + $eventLogger->start('init_session', 'Initialize session'); |
|
686 | + OC_App::loadApps(['session']); |
|
687 | + if (!self::$CLI) { |
|
688 | + self::initSession(); |
|
689 | + } |
|
690 | + $eventLogger->end('init_session'); |
|
691 | + self::checkConfig(); |
|
692 | + self::checkInstalled($systemConfig); |
|
693 | + |
|
694 | + OC_Response::addSecurityHeaders(); |
|
695 | + |
|
696 | + self::performSameSiteCookieProtection($config); |
|
697 | + |
|
698 | + if (!defined('OC_CONSOLE')) { |
|
699 | + $errors = OC_Util::checkServer($systemConfig); |
|
700 | + if (count($errors) > 0) { |
|
701 | + if (!self::$CLI) { |
|
702 | + http_response_code(503); |
|
703 | + OC_Util::addStyle('guest'); |
|
704 | + try { |
|
705 | + OC_Template::printGuestPage('', 'error', ['errors' => $errors]); |
|
706 | + exit; |
|
707 | + } catch (\Exception $e) { |
|
708 | + // In case any error happens when showing the error page, we simply fall back to posting the text. |
|
709 | + // This might be the case when e.g. the data directory is broken and we can not load/write SCSS to/from it. |
|
710 | + } |
|
711 | + } |
|
712 | + |
|
713 | + // Convert l10n string into regular string for usage in database |
|
714 | + $staticErrors = []; |
|
715 | + foreach ($errors as $error) { |
|
716 | + echo $error['error'] . "\n"; |
|
717 | + echo $error['hint'] . "\n\n"; |
|
718 | + $staticErrors[] = [ |
|
719 | + 'error' => (string)$error['error'], |
|
720 | + 'hint' => (string)$error['hint'], |
|
721 | + ]; |
|
722 | + } |
|
723 | + |
|
724 | + try { |
|
725 | + $config->setAppValue('core', 'cronErrors', json_encode($staticErrors)); |
|
726 | + } catch (\Exception $e) { |
|
727 | + echo('Writing to database failed'); |
|
728 | + } |
|
729 | + exit(1); |
|
730 | + } elseif (self::$CLI && $config->getSystemValue('installed', false)) { |
|
731 | + $config->deleteAppValue('core', 'cronErrors'); |
|
732 | + } |
|
733 | + } |
|
734 | + |
|
735 | + // User and Groups |
|
736 | + if (!$systemConfig->getValue("installed", false)) { |
|
737 | + self::$server->getSession()->set('user_id', ''); |
|
738 | + } |
|
739 | + |
|
740 | + OC_User::useBackend(new \OC\User\Database()); |
|
741 | + Server::get(\OCP\IGroupManager::class)->addBackend(new \OC\Group\Database()); |
|
742 | + |
|
743 | + // Subscribe to the hook |
|
744 | + \OCP\Util::connectHook( |
|
745 | + '\OCA\Files_Sharing\API\Server2Server', |
|
746 | + 'preLoginNameUsedAsUserName', |
|
747 | + '\OC\User\Database', |
|
748 | + 'preLoginNameUsedAsUserName' |
|
749 | + ); |
|
750 | + |
|
751 | + //setup extra user backends |
|
752 | + if (!\OCP\Util::needUpgrade()) { |
|
753 | + OC_User::setupBackends(); |
|
754 | + } else { |
|
755 | + // Run upgrades in incognito mode |
|
756 | + OC_User::setIncognitoMode(true); |
|
757 | + } |
|
758 | + |
|
759 | + self::registerCleanupHooks($systemConfig); |
|
760 | + self::registerShareHooks($systemConfig); |
|
761 | + self::registerEncryptionWrapperAndHooks(); |
|
762 | + self::registerAccountHooks(); |
|
763 | + self::registerResourceCollectionHooks(); |
|
764 | + self::registerFileReferenceEventListener(); |
|
765 | + self::registerAppRestrictionsHooks(); |
|
766 | + |
|
767 | + // Make sure that the application class is not loaded before the database is setup |
|
768 | + if ($systemConfig->getValue("installed", false)) { |
|
769 | + OC_App::loadApp('settings'); |
|
770 | + /* Build core application to make sure that listeners are registered */ |
|
771 | + Server::get(\OC\Core\Application::class); |
|
772 | + } |
|
773 | + |
|
774 | + //make sure temporary files are cleaned up |
|
775 | + $tmpManager = Server::get(\OCP\ITempManager::class); |
|
776 | + register_shutdown_function([$tmpManager, 'clean']); |
|
777 | + $lockProvider = Server::get(\OCP\Lock\ILockingProvider::class); |
|
778 | + register_shutdown_function([$lockProvider, 'releaseAll']); |
|
779 | + |
|
780 | + // Check whether the sample configuration has been copied |
|
781 | + if ($systemConfig->getValue('copied_sample_config', false)) { |
|
782 | + $l = Server::get(\OCP\L10N\IFactory::class)->get('lib'); |
|
783 | + OC_Template::printErrorPage( |
|
784 | + $l->t('Sample configuration detected'), |
|
785 | + $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'), |
|
786 | + 503 |
|
787 | + ); |
|
788 | + return; |
|
789 | + } |
|
790 | + |
|
791 | + $request = Server::get(IRequest::class); |
|
792 | + $host = $request->getInsecureServerHost(); |
|
793 | + /** |
|
794 | + * if the host passed in headers isn't trusted |
|
795 | + * FIXME: Should not be in here at all :see_no_evil: |
|
796 | + */ |
|
797 | + if (!OC::$CLI |
|
798 | + && !Server::get(\OC\Security\TrustedDomainHelper::class)->isTrustedDomain($host) |
|
799 | + && $config->getSystemValue('installed', false) |
|
800 | + ) { |
|
801 | + // Allow access to CSS resources |
|
802 | + $isScssRequest = false; |
|
803 | + if (strpos($request->getPathInfo() ?: '', '/css/') === 0) { |
|
804 | + $isScssRequest = true; |
|
805 | + } |
|
806 | + |
|
807 | + if (substr($request->getRequestUri(), -11) === '/status.php') { |
|
808 | + http_response_code(400); |
|
809 | + header('Content-Type: application/json'); |
|
810 | + echo '{"error": "Trusted domain error.", "code": 15}'; |
|
811 | + exit(); |
|
812 | + } |
|
813 | + |
|
814 | + if (!$isScssRequest) { |
|
815 | + http_response_code(400); |
|
816 | + Server::get(LoggerInterface::class)->info( |
|
817 | + 'Trusted domain error. "{remoteAddress}" tried to access using "{host}" as host.', |
|
818 | + [ |
|
819 | + 'app' => 'core', |
|
820 | + 'remoteAddress' => $request->getRemoteAddress(), |
|
821 | + 'host' => $host, |
|
822 | + ] |
|
823 | + ); |
|
824 | + |
|
825 | + $tmpl = new OCP\Template('core', 'untrustedDomain', 'guest'); |
|
826 | + $tmpl->assign('docUrl', Server::get(IURLGenerator::class)->linkToDocs('admin-trusted-domains')); |
|
827 | + $tmpl->printPage(); |
|
828 | + |
|
829 | + exit(); |
|
830 | + } |
|
831 | + } |
|
832 | + $eventLogger->end('boot'); |
|
833 | + $eventLogger->log('init', 'OC::init', $loaderStart, microtime(true)); |
|
834 | + $eventLogger->start('runtime', 'Runtime'); |
|
835 | + $eventLogger->start('request', 'Full request after boot'); |
|
836 | + register_shutdown_function(function () use ($eventLogger) { |
|
837 | + $eventLogger->end('request'); |
|
838 | + }); |
|
839 | + } |
|
840 | + |
|
841 | + /** |
|
842 | + * register hooks for the cleanup of cache and bruteforce protection |
|
843 | + */ |
|
844 | + public static function registerCleanupHooks(\OC\SystemConfig $systemConfig): void { |
|
845 | + //don't try to do this before we are properly setup |
|
846 | + if ($systemConfig->getValue('installed', false) && !\OCP\Util::needUpgrade()) { |
|
847 | + // NOTE: This will be replaced to use OCP |
|
848 | + $userSession = Server::get(\OC\User\Session::class); |
|
849 | + $userSession->listen('\OC\User', 'postLogin', function () use ($userSession) { |
|
850 | + if (!defined('PHPUNIT_RUN') && $userSession->isLoggedIn()) { |
|
851 | + // reset brute force delay for this IP address and username |
|
852 | + $uid = $userSession->getUser()->getUID(); |
|
853 | + $request = Server::get(IRequest::class); |
|
854 | + $throttler = Server::get(\OC\Security\Bruteforce\Throttler::class); |
|
855 | + $throttler->resetDelay($request->getRemoteAddress(), 'login', ['user' => $uid]); |
|
856 | + } |
|
857 | + |
|
858 | + try { |
|
859 | + $cache = new \OC\Cache\File(); |
|
860 | + $cache->gc(); |
|
861 | + } catch (\OC\ServerNotAvailableException $e) { |
|
862 | + // not a GC exception, pass it on |
|
863 | + throw $e; |
|
864 | + } catch (\OC\ForbiddenException $e) { |
|
865 | + // filesystem blocked for this request, ignore |
|
866 | + } catch (\Exception $e) { |
|
867 | + // a GC exception should not prevent users from using OC, |
|
868 | + // so log the exception |
|
869 | + Server::get(LoggerInterface::class)->warning('Exception when running cache gc.', [ |
|
870 | + 'app' => 'core', |
|
871 | + 'exception' => $e, |
|
872 | + ]); |
|
873 | + } |
|
874 | + }); |
|
875 | + } |
|
876 | + } |
|
877 | + |
|
878 | + private static function registerEncryptionWrapperAndHooks(): void { |
|
879 | + $manager = Server::get(\OCP\Encryption\IManager::class); |
|
880 | + \OCP\Util::connectHook('OC_Filesystem', 'preSetup', $manager, 'setupStorage'); |
|
881 | + |
|
882 | + $enabled = $manager->isEnabled(); |
|
883 | + if ($enabled) { |
|
884 | + \OCP\Util::connectHook(Share::class, 'post_shared', HookManager::class, 'postShared'); |
|
885 | + \OCP\Util::connectHook(Share::class, 'post_unshare', HookManager::class, 'postUnshared'); |
|
886 | + \OCP\Util::connectHook('OC_Filesystem', 'post_rename', HookManager::class, 'postRename'); |
|
887 | + \OCP\Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', HookManager::class, 'postRestore'); |
|
888 | + } |
|
889 | + } |
|
890 | + |
|
891 | + private static function registerAccountHooks(): void { |
|
892 | + /** @var IEventDispatcher $dispatcher */ |
|
893 | + $dispatcher = Server::get(IEventDispatcher::class); |
|
894 | + $dispatcher->addServiceListener(UserChangedEvent::class, \OC\Accounts\Hooks::class); |
|
895 | + } |
|
896 | + |
|
897 | + private static function registerAppRestrictionsHooks(): void { |
|
898 | + /** @var \OC\Group\Manager $groupManager */ |
|
899 | + $groupManager = Server::get(\OCP\IGroupManager::class); |
|
900 | + $groupManager->listen('\OC\Group', 'postDelete', function (\OCP\IGroup $group) { |
|
901 | + $appManager = Server::get(\OCP\App\IAppManager::class); |
|
902 | + $apps = $appManager->getEnabledAppsForGroup($group); |
|
903 | + foreach ($apps as $appId) { |
|
904 | + $restrictions = $appManager->getAppRestriction($appId); |
|
905 | + if (empty($restrictions)) { |
|
906 | + continue; |
|
907 | + } |
|
908 | + $key = array_search($group->getGID(), $restrictions); |
|
909 | + unset($restrictions[$key]); |
|
910 | + $restrictions = array_values($restrictions); |
|
911 | + if (empty($restrictions)) { |
|
912 | + $appManager->disableApp($appId); |
|
913 | + } else { |
|
914 | + $appManager->enableAppForGroups($appId, $restrictions); |
|
915 | + } |
|
916 | + } |
|
917 | + }); |
|
918 | + } |
|
919 | + |
|
920 | + private static function registerResourceCollectionHooks(): void { |
|
921 | + \OC\Collaboration\Resources\Listener::register(Server::get(SymfonyAdapter::class), Server::get(IEventDispatcher::class)); |
|
922 | + } |
|
923 | + |
|
924 | + private static function registerFileReferenceEventListener(): void { |
|
925 | + \OC\Collaboration\Reference\File\FileReferenceEventListener::register(Server::get(IEventDispatcher::class)); |
|
926 | + } |
|
927 | + |
|
928 | + /** |
|
929 | + * register hooks for sharing |
|
930 | + */ |
|
931 | + public static function registerShareHooks(\OC\SystemConfig $systemConfig): void { |
|
932 | + if ($systemConfig->getValue('installed')) { |
|
933 | + OC_Hook::connect('OC_User', 'post_deleteUser', Hooks::class, 'post_deleteUser'); |
|
934 | + OC_Hook::connect('OC_User', 'post_deleteGroup', Hooks::class, 'post_deleteGroup'); |
|
935 | + |
|
936 | + /** @var IEventDispatcher $dispatcher */ |
|
937 | + $dispatcher = Server::get(IEventDispatcher::class); |
|
938 | + $dispatcher->addServiceListener(UserRemovedEvent::class, \OC\Share20\UserRemovedListener::class); |
|
939 | + } |
|
940 | + } |
|
941 | + |
|
942 | + protected static function registerAutoloaderCache(\OC\SystemConfig $systemConfig): void { |
|
943 | + // The class loader takes an optional low-latency cache, which MUST be |
|
944 | + // namespaced. The instanceid is used for namespacing, but might be |
|
945 | + // unavailable at this point. Furthermore, it might not be possible to |
|
946 | + // generate an instanceid via \OC_Util::getInstanceId() because the |
|
947 | + // config file may not be writable. As such, we only register a class |
|
948 | + // loader cache if instanceid is available without trying to create one. |
|
949 | + $instanceId = $systemConfig->getValue('instanceid', null); |
|
950 | + if ($instanceId) { |
|
951 | + try { |
|
952 | + $memcacheFactory = Server::get(\OCP\ICacheFactory::class); |
|
953 | + self::$loader->setMemoryCache($memcacheFactory->createLocal('Autoloader')); |
|
954 | + } catch (\Exception $ex) { |
|
955 | + } |
|
956 | + } |
|
957 | + } |
|
958 | + |
|
959 | + /** |
|
960 | + * Handle the request |
|
961 | + */ |
|
962 | + public static function handleRequest(): void { |
|
963 | + Server::get(\OCP\Diagnostics\IEventLogger::class)->start('handle_request', 'Handle request'); |
|
964 | + $systemConfig = Server::get(\OC\SystemConfig::class); |
|
965 | + |
|
966 | + // Check if Nextcloud is installed or in maintenance (update) mode |
|
967 | + if (!$systemConfig->getValue('installed', false)) { |
|
968 | + \OC::$server->getSession()->clear(); |
|
969 | + $setupHelper = new OC\Setup( |
|
970 | + $systemConfig, |
|
971 | + Server::get(\bantu\IniGetWrapper\IniGetWrapper::class), |
|
972 | + Server::get(\OCP\L10N\IFactory::class)->get('lib'), |
|
973 | + Server::get(\OCP\Defaults::class), |
|
974 | + Server::get(\Psr\Log\LoggerInterface::class), |
|
975 | + Server::get(\OCP\Security\ISecureRandom::class), |
|
976 | + Server::get(\OC\Installer::class) |
|
977 | + ); |
|
978 | + $controller = new OC\Core\Controller\SetupController($setupHelper); |
|
979 | + $controller->run($_POST); |
|
980 | + exit(); |
|
981 | + } |
|
982 | + |
|
983 | + $request = Server::get(IRequest::class); |
|
984 | + $requestPath = $request->getRawPathInfo(); |
|
985 | + if ($requestPath === '/heartbeat') { |
|
986 | + return; |
|
987 | + } |
|
988 | + if (substr($requestPath, -3) !== '.js') { // we need these files during the upgrade |
|
989 | + self::checkMaintenanceMode($systemConfig); |
|
990 | + |
|
991 | + if (\OCP\Util::needUpgrade()) { |
|
992 | + if (function_exists('opcache_reset')) { |
|
993 | + opcache_reset(); |
|
994 | + } |
|
995 | + if (!((bool) $systemConfig->getValue('maintenance', false))) { |
|
996 | + self::printUpgradePage($systemConfig); |
|
997 | + exit(); |
|
998 | + } |
|
999 | + } |
|
1000 | + } |
|
1001 | + |
|
1002 | + // emergency app disabling |
|
1003 | + if ($requestPath === '/disableapp' |
|
1004 | + && $request->getMethod() === 'POST' |
|
1005 | + ) { |
|
1006 | + \OC_JSON::callCheck(); |
|
1007 | + \OC_JSON::checkAdminUser(); |
|
1008 | + $appIds = (array)$request->getParam('appid'); |
|
1009 | + foreach ($appIds as $appId) { |
|
1010 | + $appId = \OC_App::cleanAppId($appId); |
|
1011 | + Server::get(\OCP\App\IAppManager::class)->disableApp($appId); |
|
1012 | + } |
|
1013 | + \OC_JSON::success(); |
|
1014 | + exit(); |
|
1015 | + } |
|
1016 | + |
|
1017 | + // Always load authentication apps |
|
1018 | + OC_App::loadApps(['authentication']); |
|
1019 | + |
|
1020 | + // Load minimum set of apps |
|
1021 | + if (!\OCP\Util::needUpgrade() |
|
1022 | + && !((bool) $systemConfig->getValue('maintenance', false))) { |
|
1023 | + // For logged-in users: Load everything |
|
1024 | + if (Server::get(IUserSession::class)->isLoggedIn()) { |
|
1025 | + OC_App::loadApps(); |
|
1026 | + } else { |
|
1027 | + // For guests: Load only filesystem and logging |
|
1028 | + OC_App::loadApps(['filesystem', 'logging']); |
|
1029 | + |
|
1030 | + // Don't try to login when a client is trying to get a OAuth token. |
|
1031 | + // OAuth needs to support basic auth too, so the login is not valid |
|
1032 | + // inside Nextcloud and the Login exception would ruin it. |
|
1033 | + if ($request->getRawPathInfo() !== '/apps/oauth2/api/v1/token') { |
|
1034 | + self::handleLogin($request); |
|
1035 | + } |
|
1036 | + } |
|
1037 | + } |
|
1038 | + |
|
1039 | + if (!self::$CLI) { |
|
1040 | + try { |
|
1041 | + if (!((bool) $systemConfig->getValue('maintenance', false)) && !\OCP\Util::needUpgrade()) { |
|
1042 | + OC_App::loadApps(['filesystem', 'logging']); |
|
1043 | + OC_App::loadApps(); |
|
1044 | + } |
|
1045 | + Server::get(\OC\Route\Router::class)->match($request->getRawPathInfo()); |
|
1046 | + return; |
|
1047 | + } catch (Symfony\Component\Routing\Exception\ResourceNotFoundException $e) { |
|
1048 | + //header('HTTP/1.0 404 Not Found'); |
|
1049 | + } catch (Symfony\Component\Routing\Exception\MethodNotAllowedException $e) { |
|
1050 | + http_response_code(405); |
|
1051 | + return; |
|
1052 | + } |
|
1053 | + } |
|
1054 | + |
|
1055 | + // Handle WebDAV |
|
1056 | + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND') { |
|
1057 | + // not allowed any more to prevent people |
|
1058 | + // mounting this root directly. |
|
1059 | + // Users need to mount remote.php/webdav instead. |
|
1060 | + http_response_code(405); |
|
1061 | + return; |
|
1062 | + } |
|
1063 | + |
|
1064 | + // Handle requests for JSON or XML |
|
1065 | + $acceptHeader = $request->getHeader('Accept'); |
|
1066 | + if (in_array($acceptHeader, ['application/json', 'application/xml'], true)) { |
|
1067 | + http_response_code(404); |
|
1068 | + return; |
|
1069 | + } |
|
1070 | + |
|
1071 | + // Handle resources that can't be found |
|
1072 | + // This prevents browsers from redirecting to the default page and then |
|
1073 | + // attempting to parse HTML as CSS and similar. |
|
1074 | + $destinationHeader = $request->getHeader('Sec-Fetch-Dest'); |
|
1075 | + if (in_array($destinationHeader, ['font', 'script', 'style'])) { |
|
1076 | + http_response_code(404); |
|
1077 | + return; |
|
1078 | + } |
|
1079 | + |
|
1080 | + // Redirect to the default app or login only as an entry point |
|
1081 | + if ($requestPath === '') { |
|
1082 | + // Someone is logged in |
|
1083 | + if (Server::get(IUserSession::class)->isLoggedIn()) { |
|
1084 | + header('Location: ' . Server::get(IURLGenerator::class)->linkToDefaultPageUrl()); |
|
1085 | + } else { |
|
1086 | + // Not handled and not logged in |
|
1087 | + header('Location: ' . Server::get(IURLGenerator::class)->linkToRouteAbsolute('core.login.showLoginForm')); |
|
1088 | + } |
|
1089 | + return; |
|
1090 | + } |
|
1091 | + |
|
1092 | + try { |
|
1093 | + Server::get(\OC\Route\Router::class)->match('/error/404'); |
|
1094 | + } catch (\Exception $e) { |
|
1095 | + logger('core')->emergency($e->getMessage(), ['exception' => $e]); |
|
1096 | + $l = Server::get(\OCP\L10N\IFactory::class)->get('lib'); |
|
1097 | + OC_Template::printErrorPage( |
|
1098 | + $l->t('404'), |
|
1099 | + $l->t('The page could not be found on the server.'), |
|
1100 | + 404 |
|
1101 | + ); |
|
1102 | + } |
|
1103 | + } |
|
1104 | + |
|
1105 | + /** |
|
1106 | + * Check login: apache auth, auth token, basic auth |
|
1107 | + */ |
|
1108 | + public static function handleLogin(OCP\IRequest $request): bool { |
|
1109 | + $userSession = Server::get(\OC\User\Session::class); |
|
1110 | + if (OC_User::handleApacheAuth()) { |
|
1111 | + return true; |
|
1112 | + } |
|
1113 | + if ($userSession->tryTokenLogin($request)) { |
|
1114 | + return true; |
|
1115 | + } |
|
1116 | + if (isset($_COOKIE['nc_username']) |
|
1117 | + && isset($_COOKIE['nc_token']) |
|
1118 | + && isset($_COOKIE['nc_session_id']) |
|
1119 | + && $userSession->loginWithCookie($_COOKIE['nc_username'], $_COOKIE['nc_token'], $_COOKIE['nc_session_id'])) { |
|
1120 | + return true; |
|
1121 | + } |
|
1122 | + if ($userSession->tryBasicAuthLogin($request, Server::get(\OC\Security\Bruteforce\Throttler::class))) { |
|
1123 | + return true; |
|
1124 | + } |
|
1125 | + return false; |
|
1126 | + } |
|
1127 | + |
|
1128 | + protected static function handleAuthHeaders(): void { |
|
1129 | + //copy http auth headers for apache+php-fcgid work around |
|
1130 | + if (isset($_SERVER['HTTP_XAUTHORIZATION']) && !isset($_SERVER['HTTP_AUTHORIZATION'])) { |
|
1131 | + $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['HTTP_XAUTHORIZATION']; |
|
1132 | + } |
|
1133 | + |
|
1134 | + // Extract PHP_AUTH_USER/PHP_AUTH_PW from other headers if necessary. |
|
1135 | + $vars = [ |
|
1136 | + 'HTTP_AUTHORIZATION', // apache+php-cgi work around |
|
1137 | + 'REDIRECT_HTTP_AUTHORIZATION', // apache+php-cgi alternative |
|
1138 | + ]; |
|
1139 | + foreach ($vars as $var) { |
|
1140 | + if (isset($_SERVER[$var]) && is_string($_SERVER[$var]) && preg_match('/Basic\s+(.*)$/i', $_SERVER[$var], $matches)) { |
|
1141 | + $credentials = explode(':', base64_decode($matches[1]), 2); |
|
1142 | + if (count($credentials) === 2) { |
|
1143 | + $_SERVER['PHP_AUTH_USER'] = $credentials[0]; |
|
1144 | + $_SERVER['PHP_AUTH_PW'] = $credentials[1]; |
|
1145 | + break; |
|
1146 | + } |
|
1147 | + } |
|
1148 | + } |
|
1149 | + } |
|
1150 | 1150 | } |
1151 | 1151 | |
1152 | 1152 | OC::init(); |
@@ -65,370 +65,370 @@ |
||
65 | 65 | * @package OCA\Files\Controller |
66 | 66 | */ |
67 | 67 | class ViewController extends Controller { |
68 | - /** @var string */ |
|
69 | - protected $appName; |
|
70 | - /** @var IRequest */ |
|
71 | - protected $request; |
|
72 | - /** @var IURLGenerator */ |
|
73 | - protected $urlGenerator; |
|
74 | - /** @var IL10N */ |
|
75 | - protected $l10n; |
|
76 | - /** @var IConfig */ |
|
77 | - protected $config; |
|
78 | - /** @var IEventDispatcher */ |
|
79 | - protected $eventDispatcher; |
|
80 | - /** @var IUserSession */ |
|
81 | - protected $userSession; |
|
82 | - /** @var IAppManager */ |
|
83 | - protected $appManager; |
|
84 | - /** @var IRootFolder */ |
|
85 | - protected $rootFolder; |
|
86 | - /** @var Helper */ |
|
87 | - protected $activityHelper; |
|
88 | - /** @var IInitialState */ |
|
89 | - private $initialState; |
|
90 | - /** @var ITemplateManager */ |
|
91 | - private $templateManager; |
|
92 | - /** @var IManager */ |
|
93 | - private $shareManager; |
|
94 | - |
|
95 | - public function __construct(string $appName, |
|
96 | - IRequest $request, |
|
97 | - IURLGenerator $urlGenerator, |
|
98 | - IL10N $l10n, |
|
99 | - IConfig $config, |
|
100 | - IEventDispatcher $eventDispatcher, |
|
101 | - IUserSession $userSession, |
|
102 | - IAppManager $appManager, |
|
103 | - IRootFolder $rootFolder, |
|
104 | - Helper $activityHelper, |
|
105 | - IInitialState $initialState, |
|
106 | - ITemplateManager $templateManager, |
|
107 | - IManager $shareManager |
|
108 | - ) { |
|
109 | - parent::__construct($appName, $request); |
|
110 | - $this->appName = $appName; |
|
111 | - $this->request = $request; |
|
112 | - $this->urlGenerator = $urlGenerator; |
|
113 | - $this->l10n = $l10n; |
|
114 | - $this->config = $config; |
|
115 | - $this->eventDispatcher = $eventDispatcher; |
|
116 | - $this->userSession = $userSession; |
|
117 | - $this->appManager = $appManager; |
|
118 | - $this->rootFolder = $rootFolder; |
|
119 | - $this->activityHelper = $activityHelper; |
|
120 | - $this->initialState = $initialState; |
|
121 | - $this->templateManager = $templateManager; |
|
122 | - $this->shareManager = $shareManager; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @param string $appName |
|
127 | - * @param string $scriptName |
|
128 | - * @return string |
|
129 | - */ |
|
130 | - protected function renderScript($appName, $scriptName) { |
|
131 | - $content = ''; |
|
132 | - $appPath = \OC_App::getAppPath($appName); |
|
133 | - $scriptPath = $appPath . '/' . $scriptName; |
|
134 | - if (file_exists($scriptPath)) { |
|
135 | - // TODO: sanitize path / script name ? |
|
136 | - ob_start(); |
|
137 | - include $scriptPath; |
|
138 | - $content = ob_get_contents(); |
|
139 | - @ob_end_clean(); |
|
140 | - } |
|
141 | - |
|
142 | - return $content; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * FIXME: Replace with non static code |
|
147 | - * |
|
148 | - * @return array |
|
149 | - * @throws \OCP\Files\NotFoundException |
|
150 | - */ |
|
151 | - protected function getStorageInfo() { |
|
152 | - \OC_Util::setupFS(); |
|
153 | - $dirInfo = \OC\Files\Filesystem::getFileInfo('/', false); |
|
154 | - |
|
155 | - return \OC_Helper::getStorageInfo('/', $dirInfo); |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * @NoCSRFRequired |
|
160 | - * @NoAdminRequired |
|
161 | - * |
|
162 | - * @param string $fileid |
|
163 | - * @return TemplateResponse|RedirectResponse |
|
164 | - * @throws NotFoundException |
|
165 | - */ |
|
166 | - public function showFile(string $fileid = null, int $openfile = 1): Response { |
|
167 | - // This is the entry point from the `/f/{fileid}` URL which is hardcoded in the server. |
|
168 | - try { |
|
169 | - return $this->redirectToFile($fileid, $openfile !== 0); |
|
170 | - } catch (NotFoundException $e) { |
|
171 | - return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true])); |
|
172 | - } |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * @NoCSRFRequired |
|
177 | - * @NoAdminRequired |
|
178 | - * @UseSession |
|
179 | - * |
|
180 | - * @param string $dir |
|
181 | - * @param string $view |
|
182 | - * @param string $fileid |
|
183 | - * @param bool $fileNotFound |
|
184 | - * @param string $openfile - the openfile URL parameter if it was present in the initial request |
|
185 | - * @return TemplateResponse|RedirectResponse |
|
186 | - * @throws NotFoundException |
|
187 | - */ |
|
188 | - public function index($dir = '', $view = '', $fileid = null, $fileNotFound = false, $openfile = null) { |
|
189 | - if ($fileid !== null && $dir === '') { |
|
190 | - try { |
|
191 | - return $this->redirectToFile($fileid); |
|
192 | - } catch (NotFoundException $e) { |
|
193 | - return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true])); |
|
194 | - } |
|
195 | - } |
|
196 | - |
|
197 | - $nav = new \OCP\Template('files', 'appnavigation', ''); |
|
198 | - |
|
199 | - // Load the files we need |
|
200 | - \OCP\Util::addStyle('files', 'merged'); |
|
201 | - \OCP\Util::addScript('files', 'merged-index', 'files'); |
|
202 | - \OCP\Util::addScript('files', 'main'); |
|
203 | - |
|
204 | - // mostly for the home storage's free space |
|
205 | - // FIXME: Make non static |
|
206 | - $storageInfo = $this->getStorageInfo(); |
|
207 | - |
|
208 | - $user = $this->userSession->getUser()->getUID(); |
|
209 | - |
|
210 | - // Get all the user favorites to create a submenu |
|
211 | - try { |
|
212 | - $favElements = $this->activityHelper->getFavoriteFilePaths($this->userSession->getUser()->getUID()); |
|
213 | - } catch (\RuntimeException $e) { |
|
214 | - $favElements['folders'] = []; |
|
215 | - } |
|
216 | - |
|
217 | - $collapseClasses = ''; |
|
218 | - if (count($favElements['folders']) > 0) { |
|
219 | - $collapseClasses = 'collapsible'; |
|
220 | - } |
|
221 | - |
|
222 | - $favoritesSublistArray = []; |
|
223 | - |
|
224 | - $navBarPositionPosition = 6; |
|
225 | - $currentCount = 0; |
|
226 | - foreach ($favElements['folders'] as $favElement) { |
|
227 | - $link = $this->urlGenerator->linkToRoute('files.view.index', ['dir' => $favElement, 'view' => 'files']); |
|
228 | - $sortingValue = ++$currentCount; |
|
229 | - $element = [ |
|
230 | - 'id' => str_replace('/', '-', $favElement), |
|
231 | - 'view' => 'files', |
|
232 | - 'href' => $link, |
|
233 | - 'dir' => $favElement, |
|
234 | - 'order' => $navBarPositionPosition, |
|
235 | - 'folderPosition' => $sortingValue, |
|
236 | - 'name' => basename($favElement), |
|
237 | - 'icon' => 'files', |
|
238 | - 'quickaccesselement' => 'true' |
|
239 | - ]; |
|
240 | - |
|
241 | - array_push($favoritesSublistArray, $element); |
|
242 | - $navBarPositionPosition++; |
|
243 | - } |
|
244 | - |
|
245 | - $navItems = \OCA\Files\App::getNavigationManager()->getAll(); |
|
246 | - |
|
247 | - // add the favorites entry in menu |
|
248 | - $navItems['favorites']['sublist'] = $favoritesSublistArray; |
|
249 | - $navItems['favorites']['classes'] = $collapseClasses; |
|
250 | - |
|
251 | - // parse every menu and add the expandedState user value |
|
252 | - foreach ($navItems as $key => $item) { |
|
253 | - if (isset($item['expandedState'])) { |
|
254 | - $navItems[$key]['defaultExpandedState'] = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', $item['expandedState'], '0') === '1'; |
|
255 | - } |
|
256 | - } |
|
257 | - |
|
258 | - $nav->assign('navigationItems', $navItems); |
|
259 | - |
|
260 | - $nav->assign('usage', \OC_Helper::humanFileSize($storageInfo['used'])); |
|
261 | - if ($storageInfo['quota'] === \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
262 | - $totalSpace = $this->l10n->t('Unlimited'); |
|
263 | - } else { |
|
264 | - $totalSpace = \OC_Helper::humanFileSize($storageInfo['total']); |
|
265 | - } |
|
266 | - $nav->assign('total_space', $totalSpace); |
|
267 | - $nav->assign('quota', $storageInfo['quota']); |
|
268 | - $nav->assign('usage_relative', $storageInfo['relative']); |
|
269 | - |
|
270 | - $nav->assign('webdav_url', \OCP\Util::linkToRemote('dav/files/' . rawurlencode($user))); |
|
271 | - |
|
272 | - $contentItems = []; |
|
273 | - |
|
274 | - // render the container content for every navigation item |
|
275 | - foreach ($navItems as $item) { |
|
276 | - $content = ''; |
|
277 | - if (isset($item['script'])) { |
|
278 | - $content = $this->renderScript($item['appname'], $item['script']); |
|
279 | - } |
|
280 | - // parse submenus |
|
281 | - if (isset($item['sublist'])) { |
|
282 | - foreach ($item['sublist'] as $subitem) { |
|
283 | - $subcontent = ''; |
|
284 | - if (isset($subitem['script'])) { |
|
285 | - $subcontent = $this->renderScript($subitem['appname'], $subitem['script']); |
|
286 | - } |
|
287 | - $contentItems[$subitem['id']] = [ |
|
288 | - 'id' => $subitem['id'], |
|
289 | - 'content' => $subcontent |
|
290 | - ]; |
|
291 | - } |
|
292 | - } |
|
293 | - $contentItems[$item['id']] = [ |
|
294 | - 'id' => $item['id'], |
|
295 | - 'content' => $content |
|
296 | - ]; |
|
297 | - } |
|
298 | - |
|
299 | - $this->eventDispatcher->dispatchTyped(new ResourcesLoadAdditionalScriptsEvent()); |
|
300 | - $event = new LoadAdditionalScriptsEvent(); |
|
301 | - $this->eventDispatcher->dispatchTyped($event); |
|
302 | - $this->eventDispatcher->dispatchTyped(new LoadSidebar()); |
|
303 | - // Load Viewer scripts |
|
304 | - if (class_exists(LoadViewer::class)) { |
|
305 | - $this->eventDispatcher->dispatchTyped(new LoadViewer()); |
|
306 | - } |
|
307 | - |
|
308 | - $this->initialState->provideInitialState('templates_path', $this->templateManager->hasTemplateDirectory() ? $this->templateManager->getTemplatePath() : false); |
|
309 | - $this->initialState->provideInitialState('templates', $this->templateManager->listCreators()); |
|
310 | - |
|
311 | - $params = []; |
|
312 | - $params['usedSpacePercent'] = (int) $storageInfo['relative']; |
|
313 | - $params['owner'] = $storageInfo['owner'] ?? ''; |
|
314 | - $params['ownerDisplayName'] = $storageInfo['ownerDisplayName'] ?? ''; |
|
315 | - $params['isPublic'] = false; |
|
316 | - $params['allowShareWithLink'] = $this->shareManager->shareApiAllowLinks() ? 'yes' : 'no'; |
|
317 | - $params['defaultFileSorting'] = $this->config->getUserValue($user, 'files', 'file_sorting', 'name'); |
|
318 | - $params['defaultFileSortingDirection'] = $this->config->getUserValue($user, 'files', 'file_sorting_direction', 'asc'); |
|
319 | - $params['showgridview'] = $this->config->getUserValue($user, 'files', 'show_grid', false); |
|
320 | - $showHidden = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', false); |
|
321 | - $params['showHiddenFiles'] = $showHidden ? 1 : 0; |
|
322 | - $cropImagePreviews = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'crop_image_previews', true); |
|
323 | - $params['cropImagePreviews'] = $cropImagePreviews ? 1 : 0; |
|
324 | - $params['fileNotFound'] = $fileNotFound ? 1 : 0; |
|
325 | - $params['appNavigation'] = $nav; |
|
326 | - $params['appContents'] = $contentItems; |
|
327 | - $params['hiddenFields'] = $event->getHiddenFields(); |
|
328 | - |
|
329 | - $response = new TemplateResponse( |
|
330 | - $this->appName, |
|
331 | - 'index', |
|
332 | - $params |
|
333 | - ); |
|
334 | - $policy = new ContentSecurityPolicy(); |
|
335 | - $policy->addAllowedFrameDomain('\'self\''); |
|
336 | - $response->setContentSecurityPolicy($policy); |
|
337 | - |
|
338 | - $this->provideInitialState($dir, $openfile); |
|
339 | - |
|
340 | - return $response; |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * Add openFileInfo in initialState if $openfile is set. |
|
345 | - * @param string $dir - the ?dir= URL param |
|
346 | - * @param string $openfile - the ?openfile= URL param |
|
347 | - * @return void |
|
348 | - */ |
|
349 | - private function provideInitialState(string $dir, ?string $openfile): void { |
|
350 | - if ($openfile === null) { |
|
351 | - return; |
|
352 | - } |
|
353 | - |
|
354 | - $user = $this->userSession->getUser(); |
|
355 | - |
|
356 | - if ($user === null) { |
|
357 | - return; |
|
358 | - } |
|
359 | - |
|
360 | - $uid = $user->getUID(); |
|
361 | - $userFolder = $this->rootFolder->getUserFolder($uid); |
|
362 | - $nodes = $userFolder->getById((int) $openfile); |
|
363 | - $node = array_shift($nodes); |
|
364 | - |
|
365 | - if ($node === null) { |
|
366 | - return; |
|
367 | - } |
|
368 | - |
|
369 | - // properly format full path and make sure |
|
370 | - // we're relative to the user home folder |
|
371 | - $isRoot = $node === $userFolder; |
|
372 | - $path = $userFolder->getRelativePath($node->getPath()); |
|
373 | - $directory = $userFolder->getRelativePath($node->getParent()->getPath()); |
|
374 | - |
|
375 | - // Prevent opening a file from another folder. |
|
376 | - if ($dir !== $directory) { |
|
377 | - return; |
|
378 | - } |
|
379 | - |
|
380 | - $this->initialState->provideInitialState( |
|
381 | - 'openFileInfo', [ |
|
382 | - 'id' => $node->getId(), |
|
383 | - 'name' => $isRoot ? '' : $node->getName(), |
|
384 | - 'path' => $path, |
|
385 | - 'directory' => $directory, |
|
386 | - 'mime' => $node->getMimetype(), |
|
387 | - 'type' => $node->getType(), |
|
388 | - 'permissions' => $node->getPermissions(), |
|
389 | - ] |
|
390 | - ); |
|
391 | - } |
|
392 | - |
|
393 | - /** |
|
394 | - * Redirects to the file list and highlight the given file id |
|
395 | - * |
|
396 | - * @param string $fileId file id to show |
|
397 | - * @param bool $setOpenfile - whether or not to set the openfile URL parameter |
|
398 | - * @return RedirectResponse redirect response or not found response |
|
399 | - * @throws \OCP\Files\NotFoundException |
|
400 | - */ |
|
401 | - private function redirectToFile($fileId, bool $setOpenfile = false) { |
|
402 | - $uid = $this->userSession->getUser()->getUID(); |
|
403 | - $baseFolder = $this->rootFolder->getUserFolder($uid); |
|
404 | - $files = $baseFolder->getById($fileId); |
|
405 | - $params = []; |
|
406 | - |
|
407 | - if (empty($files) && $this->appManager->isEnabledForUser('files_trashbin')) { |
|
408 | - $baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/'); |
|
409 | - $files = $baseFolder->getById($fileId); |
|
410 | - $params['view'] = 'trashbin'; |
|
411 | - } |
|
412 | - |
|
413 | - if (!empty($files)) { |
|
414 | - $file = current($files); |
|
415 | - if ($file instanceof Folder) { |
|
416 | - // set the full path to enter the folder |
|
417 | - $params['dir'] = $baseFolder->getRelativePath($file->getPath()); |
|
418 | - } else { |
|
419 | - // set parent path as dir |
|
420 | - $params['dir'] = $baseFolder->getRelativePath($file->getParent()->getPath()); |
|
421 | - // and scroll to the entry |
|
422 | - $params['scrollto'] = $file->getName(); |
|
423 | - |
|
424 | - if ($setOpenfile) { |
|
425 | - // forward the openfile URL parameter. |
|
426 | - $params['openfile'] = $fileId; |
|
427 | - } |
|
428 | - } |
|
429 | - |
|
430 | - return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', $params)); |
|
431 | - } |
|
432 | - throw new \OCP\Files\NotFoundException(); |
|
433 | - } |
|
68 | + /** @var string */ |
|
69 | + protected $appName; |
|
70 | + /** @var IRequest */ |
|
71 | + protected $request; |
|
72 | + /** @var IURLGenerator */ |
|
73 | + protected $urlGenerator; |
|
74 | + /** @var IL10N */ |
|
75 | + protected $l10n; |
|
76 | + /** @var IConfig */ |
|
77 | + protected $config; |
|
78 | + /** @var IEventDispatcher */ |
|
79 | + protected $eventDispatcher; |
|
80 | + /** @var IUserSession */ |
|
81 | + protected $userSession; |
|
82 | + /** @var IAppManager */ |
|
83 | + protected $appManager; |
|
84 | + /** @var IRootFolder */ |
|
85 | + protected $rootFolder; |
|
86 | + /** @var Helper */ |
|
87 | + protected $activityHelper; |
|
88 | + /** @var IInitialState */ |
|
89 | + private $initialState; |
|
90 | + /** @var ITemplateManager */ |
|
91 | + private $templateManager; |
|
92 | + /** @var IManager */ |
|
93 | + private $shareManager; |
|
94 | + |
|
95 | + public function __construct(string $appName, |
|
96 | + IRequest $request, |
|
97 | + IURLGenerator $urlGenerator, |
|
98 | + IL10N $l10n, |
|
99 | + IConfig $config, |
|
100 | + IEventDispatcher $eventDispatcher, |
|
101 | + IUserSession $userSession, |
|
102 | + IAppManager $appManager, |
|
103 | + IRootFolder $rootFolder, |
|
104 | + Helper $activityHelper, |
|
105 | + IInitialState $initialState, |
|
106 | + ITemplateManager $templateManager, |
|
107 | + IManager $shareManager |
|
108 | + ) { |
|
109 | + parent::__construct($appName, $request); |
|
110 | + $this->appName = $appName; |
|
111 | + $this->request = $request; |
|
112 | + $this->urlGenerator = $urlGenerator; |
|
113 | + $this->l10n = $l10n; |
|
114 | + $this->config = $config; |
|
115 | + $this->eventDispatcher = $eventDispatcher; |
|
116 | + $this->userSession = $userSession; |
|
117 | + $this->appManager = $appManager; |
|
118 | + $this->rootFolder = $rootFolder; |
|
119 | + $this->activityHelper = $activityHelper; |
|
120 | + $this->initialState = $initialState; |
|
121 | + $this->templateManager = $templateManager; |
|
122 | + $this->shareManager = $shareManager; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @param string $appName |
|
127 | + * @param string $scriptName |
|
128 | + * @return string |
|
129 | + */ |
|
130 | + protected function renderScript($appName, $scriptName) { |
|
131 | + $content = ''; |
|
132 | + $appPath = \OC_App::getAppPath($appName); |
|
133 | + $scriptPath = $appPath . '/' . $scriptName; |
|
134 | + if (file_exists($scriptPath)) { |
|
135 | + // TODO: sanitize path / script name ? |
|
136 | + ob_start(); |
|
137 | + include $scriptPath; |
|
138 | + $content = ob_get_contents(); |
|
139 | + @ob_end_clean(); |
|
140 | + } |
|
141 | + |
|
142 | + return $content; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * FIXME: Replace with non static code |
|
147 | + * |
|
148 | + * @return array |
|
149 | + * @throws \OCP\Files\NotFoundException |
|
150 | + */ |
|
151 | + protected function getStorageInfo() { |
|
152 | + \OC_Util::setupFS(); |
|
153 | + $dirInfo = \OC\Files\Filesystem::getFileInfo('/', false); |
|
154 | + |
|
155 | + return \OC_Helper::getStorageInfo('/', $dirInfo); |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * @NoCSRFRequired |
|
160 | + * @NoAdminRequired |
|
161 | + * |
|
162 | + * @param string $fileid |
|
163 | + * @return TemplateResponse|RedirectResponse |
|
164 | + * @throws NotFoundException |
|
165 | + */ |
|
166 | + public function showFile(string $fileid = null, int $openfile = 1): Response { |
|
167 | + // This is the entry point from the `/f/{fileid}` URL which is hardcoded in the server. |
|
168 | + try { |
|
169 | + return $this->redirectToFile($fileid, $openfile !== 0); |
|
170 | + } catch (NotFoundException $e) { |
|
171 | + return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true])); |
|
172 | + } |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * @NoCSRFRequired |
|
177 | + * @NoAdminRequired |
|
178 | + * @UseSession |
|
179 | + * |
|
180 | + * @param string $dir |
|
181 | + * @param string $view |
|
182 | + * @param string $fileid |
|
183 | + * @param bool $fileNotFound |
|
184 | + * @param string $openfile - the openfile URL parameter if it was present in the initial request |
|
185 | + * @return TemplateResponse|RedirectResponse |
|
186 | + * @throws NotFoundException |
|
187 | + */ |
|
188 | + public function index($dir = '', $view = '', $fileid = null, $fileNotFound = false, $openfile = null) { |
|
189 | + if ($fileid !== null && $dir === '') { |
|
190 | + try { |
|
191 | + return $this->redirectToFile($fileid); |
|
192 | + } catch (NotFoundException $e) { |
|
193 | + return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true])); |
|
194 | + } |
|
195 | + } |
|
196 | + |
|
197 | + $nav = new \OCP\Template('files', 'appnavigation', ''); |
|
198 | + |
|
199 | + // Load the files we need |
|
200 | + \OCP\Util::addStyle('files', 'merged'); |
|
201 | + \OCP\Util::addScript('files', 'merged-index', 'files'); |
|
202 | + \OCP\Util::addScript('files', 'main'); |
|
203 | + |
|
204 | + // mostly for the home storage's free space |
|
205 | + // FIXME: Make non static |
|
206 | + $storageInfo = $this->getStorageInfo(); |
|
207 | + |
|
208 | + $user = $this->userSession->getUser()->getUID(); |
|
209 | + |
|
210 | + // Get all the user favorites to create a submenu |
|
211 | + try { |
|
212 | + $favElements = $this->activityHelper->getFavoriteFilePaths($this->userSession->getUser()->getUID()); |
|
213 | + } catch (\RuntimeException $e) { |
|
214 | + $favElements['folders'] = []; |
|
215 | + } |
|
216 | + |
|
217 | + $collapseClasses = ''; |
|
218 | + if (count($favElements['folders']) > 0) { |
|
219 | + $collapseClasses = 'collapsible'; |
|
220 | + } |
|
221 | + |
|
222 | + $favoritesSublistArray = []; |
|
223 | + |
|
224 | + $navBarPositionPosition = 6; |
|
225 | + $currentCount = 0; |
|
226 | + foreach ($favElements['folders'] as $favElement) { |
|
227 | + $link = $this->urlGenerator->linkToRoute('files.view.index', ['dir' => $favElement, 'view' => 'files']); |
|
228 | + $sortingValue = ++$currentCount; |
|
229 | + $element = [ |
|
230 | + 'id' => str_replace('/', '-', $favElement), |
|
231 | + 'view' => 'files', |
|
232 | + 'href' => $link, |
|
233 | + 'dir' => $favElement, |
|
234 | + 'order' => $navBarPositionPosition, |
|
235 | + 'folderPosition' => $sortingValue, |
|
236 | + 'name' => basename($favElement), |
|
237 | + 'icon' => 'files', |
|
238 | + 'quickaccesselement' => 'true' |
|
239 | + ]; |
|
240 | + |
|
241 | + array_push($favoritesSublistArray, $element); |
|
242 | + $navBarPositionPosition++; |
|
243 | + } |
|
244 | + |
|
245 | + $navItems = \OCA\Files\App::getNavigationManager()->getAll(); |
|
246 | + |
|
247 | + // add the favorites entry in menu |
|
248 | + $navItems['favorites']['sublist'] = $favoritesSublistArray; |
|
249 | + $navItems['favorites']['classes'] = $collapseClasses; |
|
250 | + |
|
251 | + // parse every menu and add the expandedState user value |
|
252 | + foreach ($navItems as $key => $item) { |
|
253 | + if (isset($item['expandedState'])) { |
|
254 | + $navItems[$key]['defaultExpandedState'] = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', $item['expandedState'], '0') === '1'; |
|
255 | + } |
|
256 | + } |
|
257 | + |
|
258 | + $nav->assign('navigationItems', $navItems); |
|
259 | + |
|
260 | + $nav->assign('usage', \OC_Helper::humanFileSize($storageInfo['used'])); |
|
261 | + if ($storageInfo['quota'] === \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
262 | + $totalSpace = $this->l10n->t('Unlimited'); |
|
263 | + } else { |
|
264 | + $totalSpace = \OC_Helper::humanFileSize($storageInfo['total']); |
|
265 | + } |
|
266 | + $nav->assign('total_space', $totalSpace); |
|
267 | + $nav->assign('quota', $storageInfo['quota']); |
|
268 | + $nav->assign('usage_relative', $storageInfo['relative']); |
|
269 | + |
|
270 | + $nav->assign('webdav_url', \OCP\Util::linkToRemote('dav/files/' . rawurlencode($user))); |
|
271 | + |
|
272 | + $contentItems = []; |
|
273 | + |
|
274 | + // render the container content for every navigation item |
|
275 | + foreach ($navItems as $item) { |
|
276 | + $content = ''; |
|
277 | + if (isset($item['script'])) { |
|
278 | + $content = $this->renderScript($item['appname'], $item['script']); |
|
279 | + } |
|
280 | + // parse submenus |
|
281 | + if (isset($item['sublist'])) { |
|
282 | + foreach ($item['sublist'] as $subitem) { |
|
283 | + $subcontent = ''; |
|
284 | + if (isset($subitem['script'])) { |
|
285 | + $subcontent = $this->renderScript($subitem['appname'], $subitem['script']); |
|
286 | + } |
|
287 | + $contentItems[$subitem['id']] = [ |
|
288 | + 'id' => $subitem['id'], |
|
289 | + 'content' => $subcontent |
|
290 | + ]; |
|
291 | + } |
|
292 | + } |
|
293 | + $contentItems[$item['id']] = [ |
|
294 | + 'id' => $item['id'], |
|
295 | + 'content' => $content |
|
296 | + ]; |
|
297 | + } |
|
298 | + |
|
299 | + $this->eventDispatcher->dispatchTyped(new ResourcesLoadAdditionalScriptsEvent()); |
|
300 | + $event = new LoadAdditionalScriptsEvent(); |
|
301 | + $this->eventDispatcher->dispatchTyped($event); |
|
302 | + $this->eventDispatcher->dispatchTyped(new LoadSidebar()); |
|
303 | + // Load Viewer scripts |
|
304 | + if (class_exists(LoadViewer::class)) { |
|
305 | + $this->eventDispatcher->dispatchTyped(new LoadViewer()); |
|
306 | + } |
|
307 | + |
|
308 | + $this->initialState->provideInitialState('templates_path', $this->templateManager->hasTemplateDirectory() ? $this->templateManager->getTemplatePath() : false); |
|
309 | + $this->initialState->provideInitialState('templates', $this->templateManager->listCreators()); |
|
310 | + |
|
311 | + $params = []; |
|
312 | + $params['usedSpacePercent'] = (int) $storageInfo['relative']; |
|
313 | + $params['owner'] = $storageInfo['owner'] ?? ''; |
|
314 | + $params['ownerDisplayName'] = $storageInfo['ownerDisplayName'] ?? ''; |
|
315 | + $params['isPublic'] = false; |
|
316 | + $params['allowShareWithLink'] = $this->shareManager->shareApiAllowLinks() ? 'yes' : 'no'; |
|
317 | + $params['defaultFileSorting'] = $this->config->getUserValue($user, 'files', 'file_sorting', 'name'); |
|
318 | + $params['defaultFileSortingDirection'] = $this->config->getUserValue($user, 'files', 'file_sorting_direction', 'asc'); |
|
319 | + $params['showgridview'] = $this->config->getUserValue($user, 'files', 'show_grid', false); |
|
320 | + $showHidden = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', false); |
|
321 | + $params['showHiddenFiles'] = $showHidden ? 1 : 0; |
|
322 | + $cropImagePreviews = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'crop_image_previews', true); |
|
323 | + $params['cropImagePreviews'] = $cropImagePreviews ? 1 : 0; |
|
324 | + $params['fileNotFound'] = $fileNotFound ? 1 : 0; |
|
325 | + $params['appNavigation'] = $nav; |
|
326 | + $params['appContents'] = $contentItems; |
|
327 | + $params['hiddenFields'] = $event->getHiddenFields(); |
|
328 | + |
|
329 | + $response = new TemplateResponse( |
|
330 | + $this->appName, |
|
331 | + 'index', |
|
332 | + $params |
|
333 | + ); |
|
334 | + $policy = new ContentSecurityPolicy(); |
|
335 | + $policy->addAllowedFrameDomain('\'self\''); |
|
336 | + $response->setContentSecurityPolicy($policy); |
|
337 | + |
|
338 | + $this->provideInitialState($dir, $openfile); |
|
339 | + |
|
340 | + return $response; |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * Add openFileInfo in initialState if $openfile is set. |
|
345 | + * @param string $dir - the ?dir= URL param |
|
346 | + * @param string $openfile - the ?openfile= URL param |
|
347 | + * @return void |
|
348 | + */ |
|
349 | + private function provideInitialState(string $dir, ?string $openfile): void { |
|
350 | + if ($openfile === null) { |
|
351 | + return; |
|
352 | + } |
|
353 | + |
|
354 | + $user = $this->userSession->getUser(); |
|
355 | + |
|
356 | + if ($user === null) { |
|
357 | + return; |
|
358 | + } |
|
359 | + |
|
360 | + $uid = $user->getUID(); |
|
361 | + $userFolder = $this->rootFolder->getUserFolder($uid); |
|
362 | + $nodes = $userFolder->getById((int) $openfile); |
|
363 | + $node = array_shift($nodes); |
|
364 | + |
|
365 | + if ($node === null) { |
|
366 | + return; |
|
367 | + } |
|
368 | + |
|
369 | + // properly format full path and make sure |
|
370 | + // we're relative to the user home folder |
|
371 | + $isRoot = $node === $userFolder; |
|
372 | + $path = $userFolder->getRelativePath($node->getPath()); |
|
373 | + $directory = $userFolder->getRelativePath($node->getParent()->getPath()); |
|
374 | + |
|
375 | + // Prevent opening a file from another folder. |
|
376 | + if ($dir !== $directory) { |
|
377 | + return; |
|
378 | + } |
|
379 | + |
|
380 | + $this->initialState->provideInitialState( |
|
381 | + 'openFileInfo', [ |
|
382 | + 'id' => $node->getId(), |
|
383 | + 'name' => $isRoot ? '' : $node->getName(), |
|
384 | + 'path' => $path, |
|
385 | + 'directory' => $directory, |
|
386 | + 'mime' => $node->getMimetype(), |
|
387 | + 'type' => $node->getType(), |
|
388 | + 'permissions' => $node->getPermissions(), |
|
389 | + ] |
|
390 | + ); |
|
391 | + } |
|
392 | + |
|
393 | + /** |
|
394 | + * Redirects to the file list and highlight the given file id |
|
395 | + * |
|
396 | + * @param string $fileId file id to show |
|
397 | + * @param bool $setOpenfile - whether or not to set the openfile URL parameter |
|
398 | + * @return RedirectResponse redirect response or not found response |
|
399 | + * @throws \OCP\Files\NotFoundException |
|
400 | + */ |
|
401 | + private function redirectToFile($fileId, bool $setOpenfile = false) { |
|
402 | + $uid = $this->userSession->getUser()->getUID(); |
|
403 | + $baseFolder = $this->rootFolder->getUserFolder($uid); |
|
404 | + $files = $baseFolder->getById($fileId); |
|
405 | + $params = []; |
|
406 | + |
|
407 | + if (empty($files) && $this->appManager->isEnabledForUser('files_trashbin')) { |
|
408 | + $baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/'); |
|
409 | + $files = $baseFolder->getById($fileId); |
|
410 | + $params['view'] = 'trashbin'; |
|
411 | + } |
|
412 | + |
|
413 | + if (!empty($files)) { |
|
414 | + $file = current($files); |
|
415 | + if ($file instanceof Folder) { |
|
416 | + // set the full path to enter the folder |
|
417 | + $params['dir'] = $baseFolder->getRelativePath($file->getPath()); |
|
418 | + } else { |
|
419 | + // set parent path as dir |
|
420 | + $params['dir'] = $baseFolder->getRelativePath($file->getParent()->getPath()); |
|
421 | + // and scroll to the entry |
|
422 | + $params['scrollto'] = $file->getName(); |
|
423 | + |
|
424 | + if ($setOpenfile) { |
|
425 | + // forward the openfile URL parameter. |
|
426 | + $params['openfile'] = $fileId; |
|
427 | + } |
|
428 | + } |
|
429 | + |
|
430 | + return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', $params)); |
|
431 | + } |
|
432 | + throw new \OCP\Files\NotFoundException(); |
|
433 | + } |
|
434 | 434 | } |