registerService($state); // Detect whether the request was made in the CMS area (or other admin-only areas) $isAdmin = $this->getIsAdmin($request); $state->setUseSessions($isAdmin); // Detect the subsite ID $subsiteId = $this->detectSubsiteId($request); $state->setSubsiteId($subsiteId); return $delegate($request); } catch (DatabaseException $ex) { $message = $ex->getMessage(); if (strpos($message, 'No database selected') !== false) { // Database is not ready, ignore and continue return $delegate($request); } throw $ex; } finally { // Persist to the session if using the CMS if ($state->getUseSessions()) { $request->getSession()->set('SubsiteID', $state->getSubsiteId()); } } } /** * Determine whether the website is being viewed from an admin protected area or not * * @param HTTPRequest $request * @return bool */ public function getIsAdmin(HTTPRequest $request) { $adminPaths = static::config()->get('admin_url_paths'); $adminPaths[] = AdminRootController::admin_url(); $currentPath = rtrim($request->getURL(), '/') . '/'; foreach ($adminPaths as $adminPath) { if (substr($currentPath, 0, strlen($adminPath)) === $adminPath) { return true; } } return false; } /** * Use the given request to detect the current subsite ID * * @param HTTPRequest $request * @return int */ protected function detectSubsiteId(HTTPRequest $request) { if ($request->getVar('SubsiteID') !== null) { return (int) $request->getVar('SubsiteID'); } if (SubsiteState::singleton()->getUseSessions() && $request->getSession()->get('SubsiteID') !== null) { return (int) $request->getSession()->get('SubsiteID'); } $subsiteIdFromDomain = Subsite::getSubsiteIDForDomain(); if ($subsiteIdFromDomain !== null) { return (int) $subsiteIdFromDomain; } // Default fallback return 0; } }