1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* sessionware (https://github.com/juliangut/sessionware). |
5
|
|
|
* PSR7 compatible session management. |
6
|
|
|
* |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @link https://github.com/juliangut/sessionware |
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Jgut\Sessionware\Handler; |
15
|
|
|
|
16
|
|
|
use Jgut\Sessionware\Traits\FileHandlerTrait; |
17
|
|
|
use Jgut\Sessionware\Traits\HandlerTrait; |
18
|
|
|
use Jgut\Sessionware\Traits\NativeSessionTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Native PHP session handler. |
22
|
|
|
*/ |
23
|
|
|
class Native extends \SessionHandler implements Handler |
24
|
|
|
{ |
25
|
|
|
use HandlerTrait; |
26
|
|
|
use FileHandlerTrait; |
27
|
|
|
use NativeSessionTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
protected $useFilesHandler; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Native session handler constructor. |
36
|
|
|
*/ |
37
|
|
|
public function __construct() |
38
|
|
|
{ |
39
|
|
|
$this->useFilesHandler = $this->getStringIniSetting('save_handler') === 'files'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
* |
45
|
|
|
* @throws \RuntimeException |
46
|
|
|
* |
47
|
|
|
* @SuppressWarnings(PMD.UnusedFormalParameter) |
48
|
|
|
*/ |
49
|
|
|
public function open($savePath, $sessionName) |
50
|
|
|
{ |
51
|
|
|
$this->testConfiguration(); |
52
|
|
|
|
53
|
|
|
$savePath = $this->configuration->getSavePath(); |
54
|
|
|
$sessionName = $this->configuration->getName(); |
55
|
|
|
|
56
|
|
|
if ($this->useFilesHandler) { |
57
|
|
|
$savePathParts = explode(';', $savePath); |
58
|
|
|
|
59
|
|
|
$savePathParts[] = $this->createSavePath(array_pop($savePathParts), $sessionName); |
60
|
|
|
|
61
|
|
|
$savePath = implode(';', $savePathParts); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return parent::open($savePath, $sessionName); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function read($sessionId) |
71
|
|
|
{ |
72
|
|
|
$sessionData = parent::read($sessionId); |
73
|
|
|
|
74
|
|
|
return $sessionData ? $this->decryptSessionData($sessionData) : serialize([]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function write($sessionId, $sessionData) |
81
|
|
|
{ |
82
|
|
|
return parent::write($sessionId, $this->encryptSessionData($sessionData)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
* |
88
|
|
|
* @SuppressWarnings(PMD.ShortMethodName) |
89
|
|
|
* @SuppressWarnings(PMD.UnusedFormalParameter) |
90
|
|
|
*/ |
91
|
|
|
public function gc($maxLifetime) |
92
|
|
|
{ |
93
|
|
|
return parent::gc($this->configuration->getLifetime()); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|