Total Complexity | 165 |
Total Lines | 1010 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like OC often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use OC, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
75 | class OC { |
||
76 | /** |
||
77 | * Associative array for autoloading. classname => filename |
||
78 | */ |
||
79 | public static $CLASSPATH = array(); |
||
80 | /** |
||
81 | * The installation path for Nextcloud on the server (e.g. /srv/http/nextcloud) |
||
82 | */ |
||
83 | public static $SERVERROOT = ''; |
||
84 | /** |
||
85 | * the current request path relative to the Nextcloud root (e.g. files/index.php) |
||
86 | */ |
||
87 | private static $SUBURI = ''; |
||
88 | /** |
||
89 | * the Nextcloud root path for http requests (e.g. nextcloud/) |
||
90 | */ |
||
91 | public static $WEBROOT = ''; |
||
92 | /** |
||
93 | * The installation path array of the apps folder on the server (e.g. /srv/http/nextcloud) 'path' and |
||
94 | * web path in 'url' |
||
95 | */ |
||
96 | public static $APPSROOTS = array(); |
||
97 | |||
98 | /** |
||
99 | * @var string |
||
100 | */ |
||
101 | public static $configDir; |
||
102 | |||
103 | /** |
||
104 | * requested app |
||
105 | */ |
||
106 | public static $REQUESTEDAPP = ''; |
||
107 | |||
108 | /** |
||
109 | * check if Nextcloud runs in cli mode |
||
110 | */ |
||
111 | public static $CLI = false; |
||
112 | |||
113 | /** |
||
114 | * @var \OC\Autoloader $loader |
||
115 | */ |
||
116 | public static $loader = null; |
||
117 | |||
118 | /** @var \Composer\Autoload\ClassLoader $composerAutoloader */ |
||
119 | public static $composerAutoloader = null; |
||
120 | |||
121 | /** |
||
122 | * @var \OC\Server |
||
123 | */ |
||
124 | public static $server = null; |
||
125 | |||
126 | /** |
||
127 | * @var \OC\Config |
||
128 | */ |
||
129 | private static $config = null; |
||
130 | |||
131 | /** |
||
132 | * @throws \RuntimeException when the 3rdparty directory is missing or |
||
133 | * the app path list is empty or contains an invalid path |
||
134 | */ |
||
135 | public static function initPaths() { |
||
136 | if(defined('PHPUNIT_CONFIG_DIR')) { |
||
137 | self::$configDir = OC::$SERVERROOT . '/' . PHPUNIT_CONFIG_DIR . '/'; |
||
|
|||
138 | } elseif(defined('PHPUNIT_RUN') and PHPUNIT_RUN and is_dir(OC::$SERVERROOT . '/tests/config/')) { |
||
139 | self::$configDir = OC::$SERVERROOT . '/tests/config/'; |
||
140 | } elseif($dir = getenv('NEXTCLOUD_CONFIG_DIR')) { |
||
141 | self::$configDir = rtrim($dir, '/') . '/'; |
||
142 | } else { |
||
143 | self::$configDir = OC::$SERVERROOT . '/config/'; |
||
144 | } |
||
145 | self::$config = new \OC\Config(self::$configDir); |
||
146 | |||
147 | OC::$SUBURI = str_replace("\\", "/", substr(realpath($_SERVER["SCRIPT_FILENAME"]), strlen(OC::$SERVERROOT))); |
||
148 | /** |
||
149 | * FIXME: The following lines are required because we can't yet instantiate |
||
150 | * \OC::$server->getRequest() since \OC::$server does not yet exist. |
||
151 | */ |
||
152 | $params = [ |
||
153 | 'server' => [ |
||
154 | 'SCRIPT_NAME' => $_SERVER['SCRIPT_NAME'], |
||
155 | 'SCRIPT_FILENAME' => $_SERVER['SCRIPT_FILENAME'], |
||
156 | ], |
||
157 | ]; |
||
158 | $fakeRequest = new \OC\AppFramework\Http\Request($params, null, new \OC\AllConfig(new \OC\SystemConfig(self::$config))); |
||
159 | $scriptName = $fakeRequest->getScriptName(); |
||
160 | if (substr($scriptName, -1) == '/') { |
||
161 | $scriptName .= 'index.php'; |
||
162 | //make sure suburi follows the same rules as scriptName |
||
163 | if (substr(OC::$SUBURI, -9) != 'index.php') { |
||
164 | if (substr(OC::$SUBURI, -1) != '/') { |
||
165 | OC::$SUBURI = OC::$SUBURI . '/'; |
||
166 | } |
||
167 | OC::$SUBURI = OC::$SUBURI . 'index.php'; |
||
168 | } |
||
169 | } |
||
170 | |||
171 | |||
172 | if (OC::$CLI) { |
||
173 | OC::$WEBROOT = self::$config->getValue('overwritewebroot', ''); |
||
174 | } else { |
||
175 | if (substr($scriptName, 0 - strlen(OC::$SUBURI)) === OC::$SUBURI) { |
||
176 | OC::$WEBROOT = substr($scriptName, 0, 0 - strlen(OC::$SUBURI)); |
||
177 | |||
178 | if (OC::$WEBROOT != '' && OC::$WEBROOT[0] !== '/') { |
||
179 | OC::$WEBROOT = '/' . OC::$WEBROOT; |
||
180 | } |
||
181 | } else { |
||
182 | // The scriptName is not ending with OC::$SUBURI |
||
183 | // This most likely means that we are calling from CLI. |
||
184 | // However some cron jobs still need to generate |
||
185 | // a web URL, so we use overwritewebroot as a fallback. |
||
186 | OC::$WEBROOT = self::$config->getValue('overwritewebroot', ''); |
||
187 | } |
||
188 | |||
189 | // Resolve /nextcloud to /nextcloud/ to ensure to always have a trailing |
||
190 | // slash which is required by URL generation. |
||
191 | if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] === \OC::$WEBROOT && |
||
192 | substr($_SERVER['REQUEST_URI'], -1) !== '/') { |
||
193 | header('Location: '.\OC::$WEBROOT.'/'); |
||
194 | exit(); |
||
195 | } |
||
196 | } |
||
197 | |||
198 | // search the apps folder |
||
199 | $config_paths = self::$config->getValue('apps_paths', array()); |
||
200 | if (!empty($config_paths)) { |
||
201 | foreach ($config_paths as $paths) { |
||
202 | if (isset($paths['url']) && isset($paths['path'])) { |
||
203 | $paths['url'] = rtrim($paths['url'], '/'); |
||
204 | $paths['path'] = rtrim($paths['path'], '/'); |
||
205 | OC::$APPSROOTS[] = $paths; |
||
206 | } |
||
207 | } |
||
208 | } elseif (file_exists(OC::$SERVERROOT . '/apps')) { |
||
209 | OC::$APPSROOTS[] = array('path' => OC::$SERVERROOT . '/apps', 'url' => '/apps', 'writable' => true); |
||
210 | } elseif (file_exists(OC::$SERVERROOT . '/../apps')) { |
||
211 | OC::$APPSROOTS[] = array( |
||
212 | 'path' => rtrim(dirname(OC::$SERVERROOT), '/') . '/apps', |
||
213 | 'url' => '/apps', |
||
214 | 'writable' => true |
||
215 | ); |
||
216 | } |
||
217 | |||
218 | if (empty(OC::$APPSROOTS)) { |
||
219 | throw new \RuntimeException('apps directory not found! Please put the Nextcloud apps folder in the Nextcloud folder' |
||
220 | . ' or the folder above. You can also configure the location in the config.php file.'); |
||
221 | } |
||
222 | $paths = array(); |
||
223 | foreach (OC::$APPSROOTS as $path) { |
||
224 | $paths[] = $path['path']; |
||
225 | if (!is_dir($path['path'])) { |
||
226 | throw new \RuntimeException(sprintf('App directory "%s" not found! Please put the Nextcloud apps folder in the' |
||
227 | . ' Nextcloud folder or the folder above. You can also configure the location in the' |
||
228 | . ' config.php file.', $path['path'])); |
||
229 | } |
||
230 | } |
||
231 | |||
232 | // set the right include path |
||
233 | set_include_path( |
||
234 | implode(PATH_SEPARATOR, $paths) |
||
235 | ); |
||
236 | } |
||
237 | |||
238 | public static function checkConfig() { |
||
239 | $l = \OC::$server->getL10N('lib'); |
||
240 | |||
241 | // Create config if it does not already exist |
||
242 | $configFilePath = self::$configDir .'/config.php'; |
||
243 | if(!file_exists($configFilePath)) { |
||
244 | @touch($configFilePath); |
||
245 | } |
||
246 | |||
247 | // Check if config is writable |
||
248 | $configFileWritable = is_writable($configFilePath); |
||
249 | if (!$configFileWritable && !OC_Helper::isReadOnlyConfigEnabled() |
||
250 | || !$configFileWritable && \OCP\Util::needUpgrade()) { |
||
251 | |||
252 | $urlGenerator = \OC::$server->getURLGenerator(); |
||
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 webserver write access to the config directory')."\n"; |
||
257 | echo $l->t('See %s', [ $urlGenerator->linkToDocs('admin-dir_permissions') ])."\n"; |
||
258 | echo "\n"; |
||
259 | echo $l->t('Or, if you prefer to keep config.php file read only, set the option "config_is_read_only" to true in it.')."\n"; |
||
260 | echo $l->t('See %s', [ $urlGenerator->linkToDocs('admin-config') ])."\n"; |
||
261 | exit; |
||
262 | } else { |
||
263 | OC_Template::printErrorPage( |
||
264 | $l->t('Cannot write into "config" directory!'), |
||
265 | $l->t('This can usually be fixed by giving the webserver write access to the config directory. See %s', |
||
266 | [ $urlGenerator->linkToDocs('admin-dir_permissions') ]) . '. ' |
||
267 | . $l->t('Or, if you prefer to keep config.php file read only, set the option "config_is_read_only" to true in it. See %s', |
||
268 | [ $urlGenerator->linkToDocs('admin-config') ] ), |
||
269 | 503 |
||
270 | ); |
||
271 | } |
||
272 | } |
||
273 | } |
||
274 | |||
275 | public static function checkInstalled() { |
||
276 | if (defined('OC_CONSOLE')) { |
||
277 | return; |
||
278 | } |
||
279 | // Redirect to installer if not installed |
||
280 | if (!\OC::$server->getSystemConfig()->getValue('installed', false) && OC::$SUBURI !== '/index.php' && OC::$SUBURI !== '/status.php') { |
||
281 | if (OC::$CLI) { |
||
282 | throw new Exception('Not installed'); |
||
283 | } else { |
||
284 | $url = OC::$WEBROOT . '/index.php'; |
||
285 | header('Location: ' . $url); |
||
286 | } |
||
287 | exit(); |
||
288 | } |
||
289 | } |
||
290 | |||
291 | public static function checkMaintenanceMode() { |
||
292 | // Allow ajax update script to execute without being stopped |
||
293 | if (((bool) \OC::$server->getSystemConfig()->getValue('maintenance', false)) && OC::$SUBURI != '/core/ajax/update.php') { |
||
294 | // send http status 503 |
||
295 | http_response_code(503); |
||
296 | header('Retry-After: 120'); |
||
297 | |||
298 | // render error page |
||
299 | $template = new OC_Template('', 'update.user', 'guest'); |
||
300 | OC_Util::addScript('dist/maintenance'); |
||
301 | OC_Util::addStyle('core', 'guest'); |
||
302 | $template->printPage(); |
||
303 | die(); |
||
304 | } |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Prints the upgrade page |
||
309 | * |
||
310 | * @param \OC\SystemConfig $systemConfig |
||
311 | */ |
||
312 | private static function printUpgradePage(\OC\SystemConfig $systemConfig) { |
||
313 | $disableWebUpdater = $systemConfig->getValue('upgrade.disable-web', false); |
||
314 | $tooBig = false; |
||
315 | if (!$disableWebUpdater) { |
||
316 | $apps = \OC::$server->getAppManager(); |
||
317 | if ($apps->isInstalled('user_ldap')) { |
||
318 | $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
||
319 | |||
320 | $result = $qb->select($qb->func()->count('*', 'user_count')) |
||
321 | ->from('ldap_user_mapping') |
||
322 | ->execute(); |
||
323 | $row = $result->fetch(); |
||
324 | $result->closeCursor(); |
||
325 | |||
326 | $tooBig = ($row['user_count'] > 50); |
||
327 | } |
||
328 | if (!$tooBig && $apps->isInstalled('user_saml')) { |
||
329 | $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
||
330 | |||
331 | $result = $qb->select($qb->func()->count('*', 'user_count')) |
||
332 | ->from('user_saml_users') |
||
333 | ->execute(); |
||
334 | $row = $result->fetch(); |
||
335 | $result->closeCursor(); |
||
336 | |||
337 | $tooBig = ($row['user_count'] > 50); |
||
338 | } |
||
339 | if (!$tooBig) { |
||
340 | // count users |
||
341 | $stats = \OC::$server->getUserManager()->countUsers(); |
||
342 | $totalUsers = array_sum($stats); |
||
343 | $tooBig = ($totalUsers > 50); |
||
344 | } |
||
345 | } |
||
346 | $ignoreTooBigWarning = isset($_GET['IKnowThatThisIsABigInstanceAndTheUpdateRequestCouldRunIntoATimeoutAndHowToRestoreABackup']) && |
||
347 | $_GET['IKnowThatThisIsABigInstanceAndTheUpdateRequestCouldRunIntoATimeoutAndHowToRestoreABackup'] === 'IAmSuperSureToDoThis'; |
||
348 | |||
349 | if ($disableWebUpdater || ($tooBig && !$ignoreTooBigWarning)) { |
||
350 | // send http status 503 |
||
351 | http_response_code(503); |
||
352 | header('Retry-After: 120'); |
||
353 | |||
354 | // render error page |
||
355 | $template = new OC_Template('', 'update.use-cli', 'guest'); |
||
356 | $template->assign('productName', 'nextcloud'); // for now |
||
357 | $template->assign('version', OC_Util::getVersionString()); |
||
358 | $template->assign('tooBig', $tooBig); |
||
359 | |||
360 | $template->printPage(); |
||
361 | die(); |
||
362 | } |
||
363 | |||
364 | // check whether this is a core update or apps update |
||
365 | $installedVersion = $systemConfig->getValue('version', '0.0.0'); |
||
366 | $currentVersion = implode('.', \OCP\Util::getVersion()); |
||
367 | |||
368 | // if not a core upgrade, then it's apps upgrade |
||
369 | $isAppsOnlyUpgrade = version_compare($currentVersion, $installedVersion, '='); |
||
370 | |||
371 | $oldTheme = $systemConfig->getValue('theme'); |
||
372 | $systemConfig->setValue('theme', ''); |
||
373 | OC_Util::addScript('config'); // needed for web root |
||
374 | OC_Util::addScript('update'); |
||
375 | |||
376 | /** @var \OC\App\AppManager $appManager */ |
||
377 | $appManager = \OC::$server->getAppManager(); |
||
378 | |||
379 | $tmpl = new OC_Template('', 'update.admin', 'guest'); |
||
380 | $tmpl->assign('version', OC_Util::getVersionString()); |
||
381 | $tmpl->assign('isAppsOnlyUpgrade', $isAppsOnlyUpgrade); |
||
382 | |||
383 | // get third party apps |
||
384 | $ocVersion = \OCP\Util::getVersion(); |
||
385 | $ocVersion = implode('.', $ocVersion); |
||
386 | $incompatibleApps = $appManager->getIncompatibleApps($ocVersion); |
||
387 | $incompatibleShippedApps = []; |
||
388 | foreach ($incompatibleApps as $appInfo) { |
||
389 | if ($appManager->isShipped($appInfo['id'])) { |
||
390 | $incompatibleShippedApps[] = $appInfo['name'] . ' (' . $appInfo['id'] . ')'; |
||
391 | } |
||
392 | } |
||
393 | |||
394 | if (!empty($incompatibleShippedApps)) { |
||
395 | $l = \OC::$server->getL10N('core'); |
||
396 | $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)]); |
||
397 | throw new \OC\HintException('The files of the app ' . implode(', ', $incompatibleShippedApps) . ' were not replaced correctly. Make sure it is a version compatible with the server.', $hint); |
||
398 | } |
||
399 | |||
400 | $tmpl->assign('appsToUpgrade', $appManager->getAppsNeedingUpgrade($ocVersion)); |
||
401 | $tmpl->assign('incompatibleAppsList', $incompatibleApps); |
||
402 | $tmpl->assign('productName', 'Nextcloud'); // for now |
||
403 | $tmpl->assign('oldTheme', $oldTheme); |
||
404 | $tmpl->printPage(); |
||
405 | } |
||
406 | |||
407 | public static function initSession() { |
||
408 | if(self::$server->getRequest()->getServerProtocol() === 'https') { |
||
409 | ini_set('session.cookie_secure', true); |
||
410 | } |
||
411 | |||
412 | // prevents javascript from accessing php session cookies |
||
413 | ini_set('session.cookie_httponly', 'true'); |
||
414 | |||
415 | // set the cookie path to the Nextcloud directory |
||
416 | $cookie_path = OC::$WEBROOT ? : '/'; |
||
417 | ini_set('session.cookie_path', $cookie_path); |
||
418 | |||
419 | // Let the session name be changed in the initSession Hook |
||
420 | $sessionName = OC_Util::getInstanceId(); |
||
421 | |||
422 | try { |
||
423 | // Allow session apps to create a custom session object |
||
424 | $useCustomSession = false; |
||
425 | $session = self::$server->getSession(); |
||
426 | OC_Hook::emit('OC', 'initSession', array('session' => &$session, 'sessionName' => &$sessionName, 'useCustomSession' => &$useCustomSession)); |
||
427 | if (!$useCustomSession) { |
||
428 | // set the session name to the instance id - which is unique |
||
429 | $session = new \OC\Session\Internal($sessionName); |
||
430 | } |
||
431 | |||
432 | $cryptoWrapper = \OC::$server->getSessionCryptoWrapper(); |
||
433 | $session = $cryptoWrapper->wrapSession($session); |
||
434 | self::$server->setSession($session); |
||
435 | |||
436 | // if session can't be started break with http 500 error |
||
437 | } catch (Exception $e) { |
||
438 | \OC::$server->getLogger()->logException($e, ['app' => 'base']); |
||
439 | //show the user a detailed error page |
||
440 | OC_Template::printExceptionErrorPage($e, 500); |
||
441 | die(); |
||
442 | } |
||
443 | |||
444 | $sessionLifeTime = self::getSessionLifeTime(); |
||
445 | |||
446 | // session timeout |
||
447 | if ($session->exists('LAST_ACTIVITY') && (time() - $session->get('LAST_ACTIVITY') > $sessionLifeTime)) { |
||
448 | if (isset($_COOKIE[session_name()])) { |
||
449 | setcookie(session_name(), '', -1, self::$WEBROOT ? : '/'); |
||
450 | } |
||
451 | \OC::$server->getUserSession()->logout(); |
||
452 | } |
||
453 | |||
454 | $session->set('LAST_ACTIVITY', time()); |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * @return string |
||
459 | */ |
||
460 | private static function getSessionLifeTime() { |
||
462 | } |
||
463 | |||
464 | /** |
||
465 | * Try to set some values to the required Nextcloud default |
||
466 | */ |
||
467 | public static function setRequiredIniValues() { |
||
470 | } |
||
471 | |||
472 | /** |
||
473 | * Send the same site cookies |
||
474 | */ |
||
475 | private static function sendSameSiteCookies() { |
||
499 | ); |
||
500 | } |
||
501 | } |
||
502 | |||
503 | /** |
||
504 | * Same Site cookie to further mitigate CSRF attacks. This cookie has to |
||
505 | * be set in every request if cookies are sent to add a second level of |
||
506 | * defense against CSRF. |
||
507 | * |
||
508 | * If the cookie is not sent this will set the cookie and reload the page. |
||
509 | * We use an additional cookie since we want to protect logout CSRF and |
||
510 | * also we can't directly interfere with PHP's session mechanism. |
||
511 | */ |
||
512 | private static function performSameSiteCookieProtection() { |
||
557 | } |
||
558 | } |
||
559 | |||
560 | public static function init() { |
||
799 | } |
||
800 | |||
801 | /** |
||
802 | * register hooks for the cleanup of cache and bruteforce protection |
||
803 | */ |
||
804 | public static function registerCleanupHooks() { |
||
805 | //don't try to do this before we are properly setup |
||
806 | if (\OC::$server->getSystemConfig()->getValue('installed', false) && !\OCP\Util::needUpgrade()) { |
||
807 | |||
808 | // NOTE: This will be replaced to use OCP |
||
809 | $userSession = self::$server->getUserSession(); |
||
810 | $userSession->listen('\OC\User', 'postLogin', function () use ($userSession) { |
||
811 | if (!defined('PHPUNIT_RUN')) { |
||
812 | // reset brute force delay for this IP address and username |
||
813 | $uid = \OC::$server->getUserSession()->getUser()->getUID(); |
||
814 | $request = \OC::$server->getRequest(); |
||
815 | $throttler = \OC::$server->getBruteForceThrottler(); |
||
816 | $throttler->resetDelay($request->getRemoteAddress(), 'login', ['user' => $uid]); |
||
817 | } |
||
818 | |||
819 | try { |
||
820 | $cache = new \OC\Cache\File(); |
||
821 | $cache->gc(); |
||
822 | } catch (\OC\ServerNotAvailableException $e) { |
||
823 | // not a GC exception, pass it on |
||
824 | throw $e; |
||
825 | } catch (\OC\ForbiddenException $e) { |
||
826 | // filesystem blocked for this request, ignore |
||
827 | } catch (\Exception $e) { |
||
828 | // a GC exception should not prevent users from using OC, |
||
829 | // so log the exception |
||
830 | \OC::$server->getLogger()->logException($e, [ |
||
831 | 'message' => 'Exception when running cache gc.', |
||
832 | 'level' => ILogger::WARN, |
||
833 | 'app' => 'core', |
||
834 | ]); |
||
835 | } |
||
836 | }); |
||
837 | } |
||
838 | } |
||
839 | |||
840 | private static function registerEncryptionWrapper() { |
||
841 | $manager = self::$server->getEncryptionManager(); |
||
842 | \OCP\Util::connectHook('OC_Filesystem', 'preSetup', $manager, 'setupStorage'); |
||
843 | } |
||
844 | |||
845 | private static function registerEncryptionHooks() { |
||
846 | $enabled = self::$server->getEncryptionManager()->isEnabled(); |
||
847 | if ($enabled) { |
||
848 | \OCP\Util::connectHook(Share::class, 'post_shared', HookManager::class, 'postShared'); |
||
849 | \OCP\Util::connectHook(Share::class, 'post_unshare', HookManager::class, 'postUnshared'); |
||
850 | \OCP\Util::connectHook('OC_Filesystem', 'post_rename', HookManager::class, 'postRename'); |
||
851 | \OCP\Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', HookManager::class, 'postRestore'); |
||
852 | } |
||
853 | } |
||
854 | |||
855 | private static function registerAccountHooks() { |
||
856 | $hookHandler = new \OC\Accounts\Hooks(\OC::$server->getLogger()); |
||
857 | \OCP\Util::connectHook('OC_User', 'changeUser', $hookHandler, 'changeUserHook'); |
||
858 | } |
||
859 | |||
860 | private static function registerAppRestrictionsHooks() { |
||
861 | $groupManager = self::$server->query(\OCP\IGroupManager::class); |
||
862 | $groupManager->listen ('\OC\Group', 'postDelete', function (\OCP\IGroup $group) { |
||
863 | $appManager = self::$server->getAppManager(); |
||
864 | $apps = $appManager->getEnabledAppsForGroup($group); |
||
865 | foreach ($apps as $appId) { |
||
866 | $restrictions = $appManager->getAppRestriction($appId); |
||
867 | if (empty($restrictions)) { |
||
868 | continue; |
||
869 | } |
||
870 | $key = array_search($group->getGID(), $restrictions); |
||
871 | unset($restrictions[$key]); |
||
872 | $restrictions = array_values($restrictions); |
||
873 | if (empty($restrictions)) { |
||
874 | $appManager->disableApp($appId); |
||
875 | } |
||
876 | else{ |
||
877 | $appManager->enableAppForGroups($appId, $restrictions); |
||
878 | } |
||
879 | |||
880 | } |
||
881 | }); |
||
882 | } |
||
883 | |||
884 | private static function registerResourceCollectionHooks() { |
||
885 | \OC\Collaboration\Resources\Listener::register(\OC::$server->getEventDispatcher()); |
||
886 | } |
||
887 | |||
888 | /** |
||
889 | * register hooks for the filesystem |
||
890 | */ |
||
891 | public static function registerFilesystemHooks() { |
||
895 | } |
||
896 | |||
897 | /** |
||
898 | * register hooks for sharing |
||
899 | */ |
||
900 | public static function registerShareHooks() { |
||
901 | if (\OC::$server->getSystemConfig()->getValue('installed')) { |
||
902 | OC_Hook::connect('OC_User', 'post_deleteUser', Hooks::class, 'post_deleteUser'); |
||
903 | OC_Hook::connect('OC_User', 'post_removeFromGroup', Hooks::class, 'post_removeFromGroup'); |
||
904 | OC_Hook::connect('OC_User', 'post_deleteGroup', Hooks::class, 'post_deleteGroup'); |
||
905 | } |
||
906 | } |
||
907 | |||
908 | protected static function registerAutoloaderCache() { |
||
909 | // The class loader takes an optional low-latency cache, which MUST be |
||
910 | // namespaced. The instanceid is used for namespacing, but might be |
||
911 | // unavailable at this point. Furthermore, it might not be possible to |
||
912 | // generate an instanceid via \OC_Util::getInstanceId() because the |
||
913 | // config file may not be writable. As such, we only register a class |
||
914 | // loader cache if instanceid is available without trying to create one. |
||
915 | $instanceId = \OC::$server->getSystemConfig()->getValue('instanceid', null); |
||
916 | if ($instanceId) { |
||
917 | try { |
||
918 | $memcacheFactory = \OC::$server->getMemCacheFactory(); |
||
919 | self::$loader->setMemoryCache($memcacheFactory->createLocal('Autoloader')); |
||
920 | } catch (\Exception $ex) { |
||
921 | } |
||
922 | } |
||
923 | } |
||
924 | |||
925 | /** |
||
926 | * Handle the request |
||
927 | */ |
||
928 | public static function handleRequest() { |
||
929 | |||
930 | \OC::$server->getEventLogger()->start('handle_request', 'Handle request'); |
||
931 | $systemConfig = \OC::$server->getSystemConfig(); |
||
932 | |||
933 | // Check if Nextcloud is installed or in maintenance (update) mode |
||
934 | if (!$systemConfig->getValue('installed', false)) { |
||
935 | \OC::$server->getSession()->clear(); |
||
936 | $setupHelper = new OC\Setup( |
||
937 | $systemConfig, |
||
938 | \OC::$server->getIniWrapper(), |
||
939 | \OC::$server->getL10N('lib'), |
||
940 | \OC::$server->query(\OCP\Defaults::class), |
||
941 | \OC::$server->getLogger(), |
||
942 | \OC::$server->getSecureRandom(), |
||
943 | \OC::$server->query(\OC\Installer::class) |
||
944 | ); |
||
945 | $controller = new OC\Core\Controller\SetupController($setupHelper); |
||
946 | $controller->run($_POST); |
||
947 | exit(); |
||
948 | } |
||
949 | |||
950 | $request = \OC::$server->getRequest(); |
||
951 | $requestPath = $request->getRawPathInfo(); |
||
952 | if ($requestPath === '/heartbeat') { |
||
953 | return; |
||
954 | } |
||
955 | if (substr($requestPath, -3) !== '.js') { // we need these files during the upgrade |
||
956 | self::checkMaintenanceMode(); |
||
957 | |||
958 | if (\OCP\Util::needUpgrade()) { |
||
959 | if (function_exists('opcache_reset')) { |
||
960 | opcache_reset(); |
||
961 | } |
||
962 | if (!((bool) $systemConfig->getValue('maintenance', false))) { |
||
963 | self::printUpgradePage($systemConfig); |
||
964 | exit(); |
||
965 | } |
||
966 | } |
||
967 | } |
||
968 | |||
969 | // emergency app disabling |
||
970 | if ($requestPath === '/disableapp' |
||
971 | && $request->getMethod() === 'POST' |
||
972 | && ((array)$request->getParam('appid')) !== '' |
||
973 | ) { |
||
974 | \OC_JSON::callCheck(); |
||
975 | \OC_JSON::checkAdminUser(); |
||
976 | $appIds = (array)$request->getParam('appid'); |
||
977 | foreach($appIds as $appId) { |
||
978 | $appId = \OC_App::cleanAppId($appId); |
||
979 | \OC::$server->getAppManager()->disableApp($appId); |
||
980 | } |
||
981 | \OC_JSON::success(); |
||
982 | exit(); |
||
983 | } |
||
984 | |||
985 | // Always load authentication apps |
||
986 | OC_App::loadApps(['authentication']); |
||
987 | |||
988 | // Load minimum set of apps |
||
989 | if (!\OCP\Util::needUpgrade() |
||
990 | && !((bool) $systemConfig->getValue('maintenance', false))) { |
||
991 | // For logged-in users: Load everything |
||
992 | if(\OC::$server->getUserSession()->isLoggedIn()) { |
||
993 | OC_App::loadApps(); |
||
994 | } else { |
||
995 | // For guests: Load only filesystem and logging |
||
996 | OC_App::loadApps(array('filesystem', 'logging')); |
||
997 | self::handleLogin($request); |
||
998 | } |
||
999 | } |
||
1000 | |||
1001 | if (!self::$CLI) { |
||
1002 | try { |
||
1003 | if (!((bool) $systemConfig->getValue('maintenance', false)) && !\OCP\Util::needUpgrade()) { |
||
1004 | OC_App::loadApps(array('filesystem', 'logging')); |
||
1005 | OC_App::loadApps(); |
||
1006 | } |
||
1007 | OC_Util::setupFS(); |
||
1008 | OC::$server->getRouter()->match(\OC::$server->getRequest()->getRawPathInfo()); |
||
1009 | return; |
||
1010 | } catch (Symfony\Component\Routing\Exception\ResourceNotFoundException $e) { |
||
1011 | //header('HTTP/1.0 404 Not Found'); |
||
1012 | } catch (Symfony\Component\Routing\Exception\MethodNotAllowedException $e) { |
||
1013 | http_response_code(405); |
||
1014 | return; |
||
1015 | } |
||
1016 | } |
||
1017 | |||
1018 | // Handle WebDAV |
||
1019 | if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND') { |
||
1020 | // not allowed any more to prevent people |
||
1021 | // mounting this root directly. |
||
1022 | // Users need to mount remote.php/webdav instead. |
||
1023 | http_response_code(405); |
||
1024 | return; |
||
1025 | } |
||
1026 | |||
1027 | // Someone is logged in |
||
1028 | if (\OC::$server->getUserSession()->isLoggedIn()) { |
||
1029 | OC_App::loadApps(); |
||
1030 | OC_User::setupBackends(); |
||
1031 | OC_Util::setupFS(); |
||
1032 | // FIXME |
||
1033 | // Redirect to default application |
||
1034 | OC_Util::redirectToDefaultPage(); |
||
1035 | } else { |
||
1036 | // Not handled and not logged in |
||
1037 | header('Location: '.\OC::$server->getURLGenerator()->linkToRouteAbsolute('core.login.showLoginForm')); |
||
1038 | } |
||
1039 | } |
||
1040 | |||
1041 | /** |
||
1042 | * Check login: apache auth, auth token, basic auth |
||
1043 | * |
||
1044 | * @param OCP\IRequest $request |
||
1045 | * @return boolean |
||
1046 | */ |
||
1047 | static function handleLogin(OCP\IRequest $request) { |
||
1065 | } |
||
1066 | |||
1067 | protected static function handleAuthHeaders() { |
||
1068 | //copy http auth headers for apache+php-fcgid work around |
||
1069 | if (isset($_SERVER['HTTP_XAUTHORIZATION']) && !isset($_SERVER['HTTP_AUTHORIZATION'])) { |
||
1070 | $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['HTTP_XAUTHORIZATION']; |
||
1071 | } |
||
1072 | |||
1073 | // Extract PHP_AUTH_USER/PHP_AUTH_PW from other headers if necessary. |
||
1092 |