Total Complexity | 72 |
Total Lines | 538 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Session 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 Session, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
90 | class Session |
||
91 | { |
||
92 | use Configurable; |
||
93 | |||
94 | /** |
||
95 | * Set session timeout in seconds. |
||
96 | * |
||
97 | * @var int |
||
98 | * @config |
||
99 | */ |
||
100 | private static $timeout = 0; |
||
|
|||
101 | |||
102 | /** |
||
103 | * @config |
||
104 | * @var array |
||
105 | */ |
||
106 | private static $session_ips = array(); |
||
107 | |||
108 | /** |
||
109 | * @config |
||
110 | * @var string |
||
111 | */ |
||
112 | private static $cookie_domain; |
||
113 | |||
114 | /** |
||
115 | * @config |
||
116 | * @var string |
||
117 | */ |
||
118 | private static $cookie_path; |
||
119 | |||
120 | /** |
||
121 | * @config |
||
122 | * @var string |
||
123 | */ |
||
124 | private static $session_store_path; |
||
125 | |||
126 | /** |
||
127 | * @config |
||
128 | * @var boolean |
||
129 | */ |
||
130 | private static $cookie_secure = false; |
||
131 | |||
132 | /** |
||
133 | * @config |
||
134 | * @var string |
||
135 | */ |
||
136 | private static $cookie_name_secure = 'SECSESSID'; |
||
137 | |||
138 | /** |
||
139 | * Name of session cache limiter to use. |
||
140 | * Defaults to '' to disable cache limiter entirely. |
||
141 | * |
||
142 | * @see https://secure.php.net/manual/en/function.session-cache-limiter.php |
||
143 | * @var string|null |
||
144 | */ |
||
145 | private static $sessionCacheLimiter = ''; |
||
146 | |||
147 | /** |
||
148 | * Session data. |
||
149 | * Will be null if session has not been started |
||
150 | * |
||
151 | * @var array|null |
||
152 | */ |
||
153 | protected $data = null; |
||
154 | |||
155 | /** |
||
156 | * @var bool |
||
157 | */ |
||
158 | protected $started = false; |
||
159 | |||
160 | /** |
||
161 | * List of keys changed. This is a nested array which represents the |
||
162 | * keys modified in $this->data. The value of each item is either "true" |
||
163 | * or a nested array. |
||
164 | * |
||
165 | * If a value is in changedData but not in data, it must be removed |
||
166 | * from the destination during save(). |
||
167 | * |
||
168 | * Only highest level changes are stored. E.g. changes to `Base.Sub` |
||
169 | * and then `Base` only records `Base` as the change. |
||
170 | * |
||
171 | * E.g. |
||
172 | * [ |
||
173 | * 'Base' => true, |
||
174 | * 'Key' => [ |
||
175 | * 'Nested' => true, |
||
176 | * ], |
||
177 | * ] |
||
178 | * |
||
179 | * @var array |
||
180 | */ |
||
181 | protected $changedData = array(); |
||
182 | |||
183 | /** |
||
184 | * Get user agent for this request |
||
185 | * |
||
186 | * @param HTTPRequest $request |
||
187 | * @return string |
||
188 | */ |
||
189 | protected function userAgent(HTTPRequest $request) |
||
190 | { |
||
191 | return $request->getHeader('User-Agent'); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Start PHP session, then create a new Session object with the given start data. |
||
196 | * |
||
197 | * @param array|null|Session $data Can be an array of data (such as $_SESSION) or another Session object to clone. |
||
198 | * If null, this session is treated as unstarted. |
||
199 | */ |
||
200 | public function __construct($data) |
||
201 | { |
||
202 | if ($data instanceof Session) { |
||
203 | $data = $data->getAll(); |
||
204 | } |
||
205 | |||
206 | $this->data = $data; |
||
207 | $this->started = isset($data); |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Init this session instance before usage, |
||
212 | * if a session identifier is part of the passed in request. |
||
213 | * Otherwise, a session might be started in {@link save()} |
||
214 | * if session data needs to be written with a new session identifier. |
||
215 | * |
||
216 | * @param HTTPRequest $request |
||
217 | */ |
||
218 | public function init(HTTPRequest $request) |
||
219 | { |
||
220 | |||
221 | if (!$this->isStarted() && $this->requestContainsSessionId($request)) { |
||
222 | $this->start($request); |
||
223 | } |
||
224 | |||
225 | // Funny business detected! |
||
226 | if (isset($this->data['HTTP_USER_AGENT'])) { |
||
227 | if ($this->data['HTTP_USER_AGENT'] !== $this->userAgent($request)) { |
||
228 | $this->clearAll(); |
||
229 | $this->destroy(); |
||
230 | $this->started = false; |
||
231 | $this->start($request); |
||
232 | } |
||
233 | } |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Destroy existing session and restart |
||
238 | * |
||
239 | * @param HTTPRequest $request |
||
240 | */ |
||
241 | public function restart(HTTPRequest $request) |
||
242 | { |
||
243 | $this->destroy(); |
||
244 | $this->init($request); |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Determine if this session has started |
||
249 | * |
||
250 | * @return bool |
||
251 | */ |
||
252 | public function isStarted() |
||
253 | { |
||
254 | return $this->started; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @param HTTPRequest $request |
||
259 | * @return bool |
||
260 | */ |
||
261 | public function requestContainsSessionId(HTTPRequest $request) |
||
262 | { |
||
263 | $secure = Director::is_https($request) && $this->config()->get('cookie_secure'); |
||
264 | $name = $secure ? $this->config()->get('cookie_name_secure') : session_name(); |
||
265 | return (bool)Cookie::get($name); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Begin session, regardless if a session identifier is present in the request, |
||
270 | * or whether any session data needs to be written. |
||
271 | * See {@link init()} if you want to "lazy start" a session. |
||
272 | * |
||
273 | * @param HTTPRequest $request The request for which to start a session |
||
274 | */ |
||
275 | public function start(HTTPRequest $request) |
||
276 | { |
||
277 | if ($this->isStarted()) { |
||
278 | throw new BadMethodCallException("Session has already started"); |
||
279 | } |
||
280 | |||
281 | $path = $this->config()->get('cookie_path'); |
||
282 | if (!$path) { |
||
283 | $path = Director::baseURL(); |
||
284 | } |
||
285 | $domain = $this->config()->get('cookie_domain'); |
||
286 | $secure = Director::is_https($request) && $this->config()->get('cookie_secure'); |
||
287 | $session_path = $this->config()->get('session_store_path'); |
||
288 | $timeout = $this->config()->get('timeout'); |
||
289 | |||
290 | // Director::baseURL can return absolute domain names - this extracts the relevant parts |
||
291 | // for the session otherwise we can get broken session cookies |
||
292 | if (Director::is_absolute_url($path)) { |
||
293 | $urlParts = parse_url($path); |
||
294 | $path = $urlParts['path']; |
||
295 | if (!$domain) { |
||
296 | $domain = $urlParts['host']; |
||
297 | } |
||
298 | } |
||
299 | |||
300 | // If the session cookie is already set, then the session can be read even if headers_sent() = true |
||
301 | // This helps with edge-case such as debugging. |
||
302 | $data = []; |
||
303 | if (!session_id() && (!headers_sent() || $this->requestContainsSessionId($request))) { |
||
304 | if (!headers_sent()) { |
||
305 | session_set_cookie_params($timeout ?: 0, $path, $domain ?: null, $secure, true); |
||
306 | |||
307 | $limiter = $this->config()->get('sessionCacheLimiter'); |
||
308 | if (isset($limiter)) { |
||
309 | session_cache_limiter($limiter); |
||
310 | } |
||
311 | |||
312 | // Allow storing the session in a non standard location |
||
313 | if ($session_path) { |
||
314 | session_save_path($session_path); |
||
315 | } |
||
316 | |||
317 | // If we want a secure cookie for HTTPS, use a separate session name. This lets us have a |
||
318 | // separate (less secure) session for non-HTTPS requests |
||
319 | // if headers_sent() is true then it's best to throw the resulting error rather than risk |
||
320 | // a security hole. |
||
321 | if ($secure) { |
||
322 | session_name($this->config()->get('cookie_name_secure')); |
||
323 | } |
||
324 | |||
325 | session_start(); |
||
326 | } else { |
||
327 | // If headers are sent then we can't have a session_cache_limiter otherwise we'll get a warning |
||
328 | session_cache_limiter(null); |
||
329 | } |
||
330 | |||
331 | if (isset($_SESSION)) { |
||
332 | // Initialise data from session store if present |
||
333 | $data = $_SESSION; |
||
334 | |||
335 | // Merge in existing in-memory data, taking priority over session store data |
||
336 | $this->recursivelyApply((array)$this->data, $data); |
||
337 | } |
||
338 | } |
||
339 | |||
340 | // Save any modified session data back to the session store if present, otherwise initialise it to an array. |
||
341 | $this->data = $data; |
||
342 | |||
343 | $this->started = true; |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Destroy this session |
||
348 | * |
||
349 | * @param bool $removeCookie |
||
350 | */ |
||
351 | public function destroy($removeCookie = true) |
||
352 | { |
||
353 | if (session_id()) { |
||
354 | if ($removeCookie) { |
||
355 | $path = $this->config()->get('cookie_path') ?: Director::baseURL(); |
||
356 | $domain = $this->config()->get('cookie_domain'); |
||
357 | $secure = $this->config()->get('cookie_secure'); |
||
358 | Cookie::force_expiry(session_name(), $path, $domain, $secure, true); |
||
359 | } |
||
360 | session_destroy(); |
||
361 | } |
||
362 | // Clean up the superglobal - session_destroy does not do it. |
||
363 | // http://nz1.php.net/manual/en/function.session-destroy.php |
||
364 | unset($_SESSION); |
||
365 | $this->data = null; |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * Set session value |
||
370 | * |
||
371 | * @param string $name |
||
372 | * @param mixed $val |
||
373 | * @return $this |
||
374 | */ |
||
375 | public function set($name, $val) |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Mark key as changed |
||
389 | * |
||
390 | * @internal |
||
391 | * @param string $name |
||
392 | */ |
||
393 | protected function markChanged($name) |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * Merge value with array |
||
413 | * |
||
414 | * @param string $name |
||
415 | * @param mixed $val |
||
416 | */ |
||
417 | public function addToArray($name, $val) |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * Get session value |
||
436 | * |
||
437 | * @param string $name |
||
438 | * @return mixed |
||
439 | */ |
||
440 | public function get($name) |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * Clear session value |
||
447 | * |
||
448 | * @param string $name |
||
449 | * @return $this |
||
450 | */ |
||
451 | public function clear($name) |
||
452 | { |
||
453 | // Get var by path |
||
454 | $var = $this->nestedValue($name, $this->data); |
||
455 | |||
456 | // Unset var |
||
457 | if ($var !== null) { |
||
458 | // Unset parent key |
||
459 | $parentParts = explode('.', $name); |
||
460 | $basePart = array_pop($parentParts); |
||
461 | if ($parentParts) { |
||
462 | $parent = &$this->nestedValueRef(implode('.', $parentParts), $this->data); |
||
463 | unset($parent[$basePart]); |
||
464 | } else { |
||
465 | unset($this->data[$name]); |
||
466 | } |
||
467 | $this->markChanged($name); |
||
468 | } |
||
469 | return $this; |
||
470 | } |
||
471 | |||
472 | /** |
||
473 | * Clear all values |
||
474 | */ |
||
475 | public function clearAll() |
||
480 | } |
||
481 | } |
||
482 | } |
||
483 | |||
484 | /** |
||
485 | * Get all values |
||
486 | * |
||
487 | * @return array|null |
||
488 | */ |
||
489 | public function getAll() |
||
492 | } |
||
493 | |||
494 | /** |
||
495 | * Set user agent key |
||
496 | * |
||
497 | * @param HTTPRequest $request |
||
498 | */ |
||
499 | public function finalize(HTTPRequest $request) |
||
500 | { |
||
501 | $this->set('HTTP_USER_AGENT', $this->userAgent($request)); |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * Save data to session |
||
506 | * Only save the changes, so that anyone manipulating $_SESSION directly doesn't get burned. |
||
507 | * |
||
508 | * @param HTTPRequest $request |
||
509 | */ |
||
510 | public function save(HTTPRequest $request) |
||
521 | } |
||
522 | } |
||
523 | |||
524 | /** |
||
525 | * Recursively apply the changes represented in $data to $dest. |
||
526 | * Used to update $_SESSION |
||
527 | * |
||
528 | * @deprecated 4.1.0:5.0.0 Use recursivelyApplyChanges() instead |
||
529 | * @param array $data |
||
530 | * @param array $dest |
||
531 | */ |
||
532 | protected function recursivelyApply($data, &$dest) |
||
533 | { |
||
534 | Deprecation::notice('5.0', 'Use recursivelyApplyChanges() instead'); |
||
535 | foreach ($data as $k => $v) { |
||
536 | if (is_array($v)) { |
||
537 | if (!isset($dest[$k]) || !is_array($dest[$k])) { |
||
538 | $dest[$k] = array(); |
||
539 | } |
||
540 | $this->recursivelyApply($v, $dest[$k]); |
||
541 | } else { |
||
542 | $dest[$k] = $v; |
||
543 | } |
||
544 | } |
||
545 | } |
||
546 | |||
547 | /** |
||
548 | * Returns the list of changed keys |
||
549 | * |
||
550 | * @return array |
||
551 | */ |
||
552 | public function changedData() |
||
555 | } |
||
556 | |||
557 | /** |
||
558 | * Navigate to nested value in source array by name, |
||
559 | * creating a null placeholder if it doesn't exist. |
||
560 | * |
||
561 | * @internal |
||
562 | * @param string $name |
||
563 | * @param array $source |
||
564 | * @return mixed Reference to value in $source |
||
565 | */ |
||
566 | protected function &nestedValueRef($name, &$source) |
||
580 | } |
||
581 | |||
582 | /** |
||
583 | * Navigate to nested value in source array by name, |
||
584 | * returning null if it doesn't exist. |
||
585 | * |
||
586 | * @internal |
||
587 | * @param string $name |
||
588 | * @param array $source |
||
589 | * @return mixed Value in array in $source |
||
590 | */ |
||
591 | protected function nestedValue($name, $source) |
||
592 | { |
||
593 | // Find var to change |
||
594 | $var = $source; |
||
595 | foreach (explode('.', $name) as $namePart) { |
||
596 | if (!isset($var[$namePart])) { |
||
597 | return null; |
||
598 | } |
||
599 | $var = $var[$namePart]; |
||
600 | } |
||
601 | return $var; |
||
602 | } |
||
603 | |||
604 | /** |
||
605 | * Apply all changes using separate keys and data sources and a destination |
||
606 | * |
||
607 | * @internal |
||
608 | * @param array $changes |
||
609 | * @param array $source |
||
610 | * @param array $destination |
||
611 | */ |
||
612 | protected function recursivelyApplyChanges($changes, $source, &$destination) |
||
628 | } |
||
629 | } |
||
630 | } |
||
632 |