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); // Persist to the session if using the CMS if ($state->getUseSessions()) { $original = $request->getSession()->get('SubsiteID'); $request->getSession()->set('SubsiteID', $subsiteId); // Track session changes $state->setSessionWasChanged($subsiteId === $original); } return $delegate($request); } /** * 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::config()->get('url_base') . '/'; $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; } }