/
Session.php
144 lines (120 loc) · 3.14 KB
/
Session.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace Gt\Session;
use SessionHandlerInterface;
class Session implements SessionContainer {
const DEFAULT_SESSION_NAME = "PHPSESSID";
const DEFAULT_SESSION_LIFETIME = 0;
const DEFAULT_SESSION_PATH = "/tmp";
const DEFAULT_SESSION_DOMAIN = "";
const DEFAULT_SESSION_SECURE = false;
const DEFAULT_SESSION_HTTPONLY = true;
const DEFAULT_COOKIE_PATH = "/";
/** @var string */
protected $id;
/** @var SessionHandlerInterface */
protected $sessionHandler;
/** @var SessionStore */
protected $store;
public function __construct(
SessionHandlerInterface $sessionHandler,
iterable $config = [],
string $id = null
) {
$this->sessionHandler = $sessionHandler;
if(is_null($id)) {
$id = $this->getId();
}
$this->id = $id;
$sessionPath = $this->getAbsolutePath(
$config["save_path"] ?? self::DEFAULT_SESSION_PATH
);
$sessionName = $config["name"] ?? self::DEFAULT_SESSION_NAME;
do {
$success = @session_start([
"save_path" => $sessionPath,
"name" => $sessionName,
"cookie_lifetime" => $config["cookie_lifetime"] ?? self::DEFAULT_SESSION_LIFETIME,
"cookie_path" => $config["cookie_path"] ?? self::DEFAULT_COOKIE_PATH,
"cookie_domain" => $config["cookie_domain"] ?? self::DEFAULT_SESSION_DOMAIN,
"cookie_secure" => $config["cookie_secure"] ?? self::DEFAULT_SESSION_SECURE,
"cookie_httponly" => $config["cookie_httponly"] ?? self::DEFAULT_SESSION_HTTPONLY,
]);
if(!$success) {
session_destroy();
}
}
while(!$success);
$this->sessionHandler->open($sessionPath, $sessionName);
$this->store = $this->readSessionData() ?: null;
if(is_null($this->store)) {
$this->store = new SessionStore(__NAMESPACE__, $this);
}
}
public function kill():void {
$this->sessionHandler->destroy($this->getId());
$params = session_get_cookie_params();
setcookie(
session_name(),
"",
-1,
$params["path"],
$params["domain"],
$params["secure"],
$params["httponly"]
);
}
public function getStore(
string $namespace,
bool $createIfNotExists = false
):?SessionStore {
return $this->store->getStore(
$namespace,
$createIfNotExists
);
}
public function get(string $key) {
return $this->store->get($key);
}
public function set(string $key, $value):void {
$this->store->set($key, $value);
}
public function contains(string $key):bool {
return $this->store->contains($key);
}
public function remove(string $key):void {
$this->store->remove($key);
}
public function getId():string {
$id = session_id();
if(empty($id)) {
session_id($this->createNewId());
}
return session_id();
}
protected function getAbsolutePath(string $path):string {
$path = str_replace(
["/", "\\"],
DIRECTORY_SEPARATOR,
$path
);
if($path[0] !== DIRECTORY_SEPARATOR) {
$path = implode(DIRECTORY_SEPARATOR, [
sys_get_temp_dir(),
$path,
]);
}
return $path;
}
protected function createNewId():string {
return session_create_id();
}
protected function readSessionData() {
return unserialize($this->sessionHandler->read($this->id));
}
public function write() {
$this->sessionHandler->write(
$this->id,
serialize($this->store)
);
}
}