|
1
|
|
|
<?php |
|
2
|
|
|
/*************************************************************************** |
|
3
|
|
|
* For license information see doc/license.txt |
|
4
|
|
|
* |
|
5
|
|
|
* Unicode Reminder メモ |
|
6
|
|
|
* |
|
7
|
|
|
* Session data handling with cookies |
|
8
|
|
|
* See doc/cookies.txt for more information in cookies. |
|
9
|
|
|
***************************************************************************/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Oc\Session; |
|
12
|
|
|
|
|
13
|
|
|
class SessionDataCookie implements SessionDataInterface |
|
14
|
|
|
{ |
|
15
|
|
|
public $changed = false; |
|
16
|
|
|
public $values = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* SessionDataCookie constructor. |
|
20
|
|
|
*/ |
|
21
|
|
View Code Duplication |
public function __construct() |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
global $opt; |
|
24
|
|
|
|
|
25
|
|
|
if (isset($_COOKIE[$opt['session']['cookiename'] . 'data'])) { |
|
26
|
|
|
//get the cookievars-array |
|
27
|
|
|
$decoded = base64_decode($_COOKIE[$opt['session']['cookiename'] . 'data'], true); |
|
28
|
|
|
|
|
29
|
|
|
if ($decoded !== false) { |
|
30
|
|
|
// TODO replace by safe function |
|
31
|
|
|
$this->values = @unserialize($decoded); // bad |
|
|
|
|
|
|
32
|
|
|
//$this->values = @json_decode($decoded, true); // safe |
|
|
|
|
|
|
33
|
|
|
//print_r($this->values); |
|
|
|
|
|
|
34
|
|
|
if (!is_array($this->values)) { |
|
35
|
|
|
$this->values = []; |
|
36
|
|
|
} |
|
37
|
|
|
} else { |
|
38
|
|
|
$this->values = []; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param $name |
|
45
|
|
|
* @param $value |
|
46
|
|
|
* @param null $default |
|
47
|
|
|
*/ |
|
48
|
|
|
public function set($name, $value, $default = null) |
|
49
|
|
|
{ |
|
50
|
|
|
// Store cookie value in internal array. OcSmarty will call this->header() |
|
51
|
|
|
// to actually set the cookie. |
|
52
|
|
|
if (!isset($this->values[$name]) || $this->values[$name] != $value) { |
|
53
|
|
|
if ($value == $default) { |
|
54
|
|
|
if (isset($this->values[$name])) { |
|
55
|
|
|
unset($this->values[$name]); |
|
56
|
|
|
$this->changed = true; |
|
57
|
|
|
} |
|
58
|
|
|
} else { |
|
59
|
|
|
$this->values[$name] = $value; |
|
60
|
|
|
$this->changed = true; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param $name |
|
67
|
|
|
* @param null $default |
|
68
|
|
|
* |
|
69
|
|
|
* @return mixed|null |
|
70
|
|
|
*/ |
|
71
|
|
|
public function get($name, $default = null) |
|
72
|
|
|
{ |
|
73
|
|
|
return isset($this->values[$name]) ? $this->values[$name] : $default; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param $name |
|
78
|
|
|
* |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
|
|
public function is_set($name) |
|
82
|
|
|
{ |
|
83
|
|
|
return isset($this->values[$name]); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param $name |
|
88
|
|
|
*/ |
|
89
|
|
View Code Duplication |
public function un_set($name) |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
|
|
if (isset($this->values[$name])) { |
|
92
|
|
|
unset($this->values[$name]); |
|
93
|
|
|
$this->changed = true; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function header() |
|
98
|
|
|
{ |
|
99
|
|
|
global $opt; |
|
100
|
|
|
|
|
101
|
|
|
if ($this->changed === true) { |
|
102
|
|
|
|
|
103
|
|
|
$value = false; |
|
104
|
|
|
if (count($this->values) > 0) { |
|
105
|
|
|
// TODO replace by safe function |
|
106
|
|
|
$value = base64_encode(serialize($this->values)); // bad |
|
107
|
|
|
//$value = base64_encode(json_encode($this->values)); // safe |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
// https used for request and https is available, then set cookie https only |
|
110
|
|
|
$https_session = $opt['page']['https']['active'] |
|
111
|
|
|
&& $opt['page']['https']['mode'] != HTTPS_DISABLED |
|
112
|
|
|
&& $this->is_set('sessionid') // only force https while login |
|
113
|
|
|
&& !empty($this->get('sessionid')); |
|
114
|
|
|
|
|
115
|
|
|
setcookie( |
|
116
|
|
|
$opt['session']['cookiename'] . 'data', |
|
117
|
|
|
$value, |
|
118
|
|
|
time() + 365 * 24 * 60 * 60, |
|
119
|
|
|
$opt['session']['path'], |
|
120
|
|
|
$opt['session']['domain'], |
|
121
|
|
|
$https_session // https only? |
|
122
|
|
|
); |
|
123
|
|
|
|
|
124
|
|
|
// if site is requested by http no session data is visible, so set cookie as flag to redirect to https |
|
125
|
|
|
setcookie( |
|
126
|
|
|
$opt['session']['cookiename'] . 'https_session', |
|
127
|
|
|
$https_session, |
|
128
|
|
|
time() + 365 * 24 * 60 * 60, |
|
129
|
|
|
$opt['session']['path'], |
|
130
|
|
|
$opt['session']['domain'], |
|
131
|
|
|
0, // must be available for http |
|
132
|
|
|
1 // communication only, no js |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function debug() |
|
138
|
|
|
{ |
|
139
|
|
|
print_r($this->values); |
|
140
|
|
|
exit; |
|
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function close() |
|
144
|
|
|
{ |
|
145
|
|
|
// TODO really nothing? |
|
146
|
|
|
// maybe destroy variables here |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
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: