Debian Codegraph @

classes/Session.php

raw symbols SCIP
1 <?php
2
3 /**
4 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
5 * Copyright (C) 2025 Aleksey Andreev (liuch)
6 *
7 * Available at:
8 * https://github.com/liuch/dmarc-srg
9 *
10 * This program is free software: you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation, either version 3 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * =========================
23 *
24 * This file contains the class Session
25 *
26 * @category API
27 * @package DmarcSrg
28 * @author Aleksey Andreev (liuch)
29 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
30 */
31
32 declare(strict_types=1);
33
34 namespace Liuch\DmarcSrg;
35
36 use Liuch\DmarcSrg\Exception\RuntimeException;
37
38 /**
39 * The class is for managing the state of the user session
40 */
41 class Session
42 {
43 private const COOKIE_NAME = 'session';
44 private const LIFETIME_DURATION = 900;
45 private const MIGRATE_DURATION = 120;
46 private const DATA_KEYS = [ 'user', 's_id', 's_time' ];
47
48 /**
49 * Returns data of the session
50 *
51 * @return array
52 */
53 public function getData(): array
54 {
55 if (!$this->isStarted()) {
56 $this->start();
57 }
58 return $this->userData();
59 }
60
61 /**
62 * Stores the passed data to the session
63 *
64 * @param array $data Array of data to store
65 *
66 * @return void
67 */
68 public function setData(array $data): void
69 {
70 if (!$this->isStarted()) {
71 $this->start();
72 }
73 foreach (self::DATA_KEYS as $key) {
74 if (isset($data[$key])) {
75 $_SESSION[$key] = $data[$key];
76 }
77 }
78 }
79
80 /**
81 * Saves and closes the user session
82 *
83 * @return void
84 */
85 public function commit(): void
86 {
87 if ($this->isStarted()) {
88 \session_write_close();
89 }
90 }
91
92 /**
93 * Closes the user session
94 *
95 * @return void
96 */
97 public function close(): void
98 {
99 if ($this->isStarted()) {
100 \session_abort();
101 }
102 }
103
104 /**
105 * Destroys the user session and its data
106 *
107 * @return void
108 */
109 public function destroy(): void
110 {
111 if (!$this->isStarted()) {
112 $this->strictStart();
113 }
114 if (\intval(\ini_get('session.use_cookies')) == 1) {
115 $scp = \session_get_cookie_params();
116 $cep = [
117 'expires' => time() - 42000,
118 'path' => $scp['path'],
119 'domain' => $scp['domain'],
120 'secure' => $scp['secure'],
121 'httponly' => $scp['httponly'],
122 'samesite' => $scp['samesite']
123 ];
124 \setcookie(self::COOKIE_NAME, '', $cep);
125 }
126 \session_unset();
127 \session_destroy();
128 \session_write_close();
129 }
130
131 /**
132 * Checks if the session is started
133 *
134 * @return bool
135 */
136 private function isStarted(): bool
137 {
138 return \session_status() === PHP_SESSION_ACTIVE;
139 }
140
141 /**
142 * Starts the user session
143 *
144 * @return void
145 */
146 private function start(): void
147 {
148 \ini_set('session.use_strict_mode', '1');
149 $this->strictStart();
150
151 // Session control
152 if (!isset($_SESSION['_started'])) {
153 // Looks like a new session
154 $_SESSION['_started'] = \time();
155 } elseif (isset($_SESSION['_expired'])) {
156 // Session is outdated
157 \session_abort();
158 if ($_SESSION['_expired'] > \time() - self::MIGRATE_DURATION && isset($_SESSION['_new_session_id'])) {
159 $s_id = $_SESSION['_new_session_id'];
160 } else {
161 $s_id = session_create_id();
162 \ini_set('session.use_strict_mode', '0');
163 }
164 \session_id($s_id);
165 $this->strictStart();
166 } elseif ($_SESSION['_started'] <= \time() - self::LIFETIME_DURATION) {
167 $this->migrate();
168 }
169 }
170
171 /**
172 * Starts a safety-optioned session
173 *
174 * @throws RuntimeException
175 *
176 * @return void
177 */
178 private function strictStart(): void
179 {
180 if (!\session_start([
181 'name' => self::COOKIE_NAME,
182 'cookie_path' => \dirname($_SERVER['REQUEST_URI']),
183 'cookie_httponly' => true,
184 'cookie_samesite' => 'Strict'
185 ])) {
186 throw new RuntimeException('Failed to start a new session');
187 }
188 }
189
190 /**
191 * Creates a new session and moves all data into it
192 *
193 * @return void
194 */
195 private function migrate(): void
196 {
197 $udata = $this->userData();
198 $ct = \time();
199 if (!isset($_SESSION['_expired'])) {
200 $_SESSION['_expired'] = $ct;
201 }
202 $new_id = \session_create_id();
203 $_SESSION['_new_session_id'] = $new_id;
204 \session_write_close();
205
206 \ini_set('session.use_strict_mode', '0');
207 \session_id($new_id);
208 $this->strictStart();
209
210 $_SESSION = $udata;
211 $_SESSION['_started'] = $ct;
212 }
213
214 /**
215 * Returns all session data except session control data
216 *
217 * @return array
218 */
219 private function userData(): array
220 {
221 $res = [];
222 foreach (self::DATA_KEYS as $key) {
223 if (isset($_SESSION[$key])) {
224 $res[$key] = $_SESSION[$key];
225 }
226 }
227 return $res;
228 }
229 }