1
|
|
|
<?php |
2
|
|
|
/*************************************************************************** |
3
|
|
|
* For license information see doc/license.txt |
4
|
|
|
* |
5
|
|
|
* Unicode Reminder メモ |
6
|
|
|
* |
7
|
|
|
* Session data handling with build-in php session |
8
|
|
|
***************************************************************************/ |
9
|
|
|
|
10
|
|
|
namespace Oc\Session; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class SessionDataNative |
14
|
|
|
* Not for productive usage!! Implementation not finished yet |
15
|
|
|
*/ |
16
|
|
|
class SessionDataNative implements SessionDataInterface |
17
|
|
|
{ |
18
|
|
|
public $changed = false; |
19
|
|
|
public $values = array(); |
20
|
|
|
public $session_initialized = false; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* SessionDataNative constructor. |
24
|
|
|
*/ |
25
|
|
|
public function __construct() |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
if (isset($_REQUEST['SESSION']) && $_REQUEST['SESSION'] !== '') { |
28
|
|
|
$this->initSession(); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
*/ |
35
|
|
|
private function initSession() |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
global $opt; |
38
|
|
|
|
39
|
|
|
if ($this->session_initialized !== true) { |
40
|
|
|
session_name('SESSION'); |
41
|
|
|
session_set_cookie_params( |
42
|
|
|
$opt['session']['expire']['cookie'], |
43
|
|
|
$opt['session']['path'], |
44
|
|
|
$opt['session']['domain'] |
45
|
|
|
); |
46
|
|
|
session_start(); |
47
|
|
|
|
48
|
|
|
if ($opt['session']['check_referer']) { |
49
|
|
|
if (isset($_SERVER['REFERER'])) { |
50
|
|
|
// TODO fix the following if statement, seems corrupted |
51
|
|
|
if (strtolower( |
52
|
|
|
substr( |
53
|
|
|
'http' + strstr($_SERVER['REFERER'], '://'), |
54
|
|
|
0, |
55
|
|
|
strlen($opt['page']['absolute_http_url']) |
56
|
|
|
) |
57
|
|
|
) != strtolower($opt['page']['absolute_http_url']) |
58
|
|
|
) { |
59
|
|
|
$this->createNewSession(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ((isset($_GET['SESSION']) || isset($_POST['SESSION'])) && count($_SESSION) > 0) { |
65
|
|
|
// compare and set timestamp |
66
|
|
|
if (isset($_SESSION['lastcall'])) { |
67
|
|
|
if (abs(time() - $_SESSION['lastcall']) > $opt['session']['expire']['url']) { |
68
|
|
|
$this->createNewSession(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$_SESSION['lastcall'] = time(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->session_initialized = true; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* |
81
|
|
|
*/ |
82
|
|
|
private function createNewSession() |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
session_regenerate_id(); |
85
|
|
|
$locale = isset($_SESSION['locale']) ? $_SESSION['locale'] : ''; |
86
|
|
|
foreach ($_SESSION as $k => $v) { |
87
|
|
|
unset($_SESSION[$k]); |
88
|
|
|
} |
89
|
|
|
if ($locale != '') { |
90
|
|
|
$_SESSION['locale'] = $locale; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $name |
96
|
|
|
* @param $value |
97
|
|
|
* @param null $default |
98
|
|
|
*/ |
99
|
|
|
public function set($name, $value, $default = null) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
if (!isset($_SESSION[$name]) || $_SESSION[$name] != $value) { |
102
|
|
|
if ($value == $default) { |
103
|
|
|
if (isset($_SESSION[$name])) { |
104
|
|
|
unset($_SESSION[$name]); |
105
|
|
|
$this->changed = true; |
106
|
|
|
} |
107
|
|
|
} else { |
108
|
|
|
$this->init_session(); |
|
|
|
|
109
|
|
|
$_SESSION[$name] = $value; |
110
|
|
|
$this->changed = true; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param $name |
117
|
|
|
* @param null $default |
118
|
|
|
* |
119
|
|
|
* @return null |
120
|
|
|
*/ |
121
|
|
|
public function get($name, $default = null) |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
return isset($_SESSION[$name]) ? $_SESSION[$name] : $default; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param $name |
128
|
|
|
* |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public function is_set($name) |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
return isset($_SESSION[$name]); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param $name |
138
|
|
|
*/ |
139
|
|
|
public function un_set($name) |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
if (isset($_SESSION[$name])) { |
142
|
|
|
unset($_SESSION[$name]); |
143
|
|
|
$this->changed = true; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function header() |
148
|
|
|
{ |
149
|
|
|
// is automatically sent |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function debug() |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
print_r($_SESSION); |
155
|
|
|
exit; |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function close() |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
if ($this->session_initialized === true) { |
161
|
|
|
if (count($_SESSION) === 0) { |
162
|
|
|
try { |
163
|
|
|
session_destroy(); |
164
|
|
|
} catch (\Exception $e) { |
165
|
|
|
// @todo implement logging |
166
|
|
|
} |
167
|
|
|
} else { |
168
|
|
|
session_write_close(); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: