parseData(), true); if (!is_array($session_data)) { return ''; } foreach ($session_data as $key => $value) { CSessionHelper::set($key, $value); } return session_encode(); } /** * @inheritDoc * * @param string $id * @param string $data * * @return boolean */ public function write($id, $data): bool { session_decode($data); $data = $this->prepareData(CSessionHelper::getAll()); return CCookieHelper::set(self::COOKIE_NAME, $data, $this->isAutologinEnabled() ? time() + SEC_PER_MONTH : 0 ); } /** * Run session_start. * * @param string $sessionid * * @return boolean */ public function session_start(string $sessionid): bool { if (headers_sent() || session_status() !== PHP_SESSION_NONE) { return false; } session_id($sessionid); return session_start(); } /** * Extract session id from session data. * * @return string|null */ public function extractSessionId(): ?string { $session_data = $this->parseData(); if ($session_data === '') { return null; } $session_data = json_decode($session_data, true); if (!is_array($session_data) || !array_key_exists('sessionid', $session_data)) { return null; } return $session_data['sessionid']; } /** * Prepare session data. * * @param array $data * * @return string */ protected function prepareData(array $data): string { return base64_encode(json_encode($data)); } /** * Parse session data. * * @return string */ protected function parseData(): string { if (CCookieHelper::has(self::COOKIE_NAME)) { return base64_decode(CCookieHelper::get(self::COOKIE_NAME)); } return ''; } protected function isAutologinEnabled(): bool { return (CWebUser::$data['autologin'] === '1'); } }