|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Cookie class |
|
5
|
|
|
* |
|
6
|
|
|
* You may not change or alter any portion of this comment or credits |
|
7
|
|
|
* of supporting developers from this source code or any supporting source code |
|
8
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
|
9
|
|
|
* This program is distributed in the hope that it will be useful, |
|
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
12
|
|
|
* |
|
13
|
|
|
* @copyright (c) 2000-2016 XOOPS Project (www.xoops.org) |
|
14
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
|
15
|
|
|
* @author Andricq Nicolas (AKA MusS) |
|
16
|
|
|
* @package system |
|
17
|
|
|
*/ |
|
18
|
|
|
class Cookie |
|
19
|
|
|
{ |
|
20
|
|
|
// Reserved session keys |
|
21
|
|
|
private static $_reserved = array('XOLOGGERVIEW', 'xoops_user'); |
|
22
|
|
|
|
|
23
|
|
|
// Static class cannot be initialized |
|
24
|
|
|
/** |
|
25
|
|
|
* |
|
26
|
|
|
*/ |
|
27
|
|
|
private function __construct() |
|
28
|
|
|
{ |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
// Alias for delete() function |
|
32
|
|
|
/** |
|
33
|
|
|
* @param $key |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function del($key) |
|
36
|
|
|
{ |
|
37
|
|
|
self::delete($key); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
// Delete a cookie |
|
41
|
|
|
/** |
|
42
|
|
|
* @param $key |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function delete($key) |
|
45
|
|
|
{ |
|
46
|
|
|
// Change string representation array to key/value array |
|
47
|
|
|
$key = self::_scrubKey($key); |
|
48
|
|
|
|
|
49
|
|
|
// Make sure the cookie exists |
|
50
|
|
|
if (self::exists($key)) { |
|
51
|
|
|
// Check for key array |
|
52
|
|
|
if (is_array($key)) { |
|
53
|
|
|
// Grab key/value pair |
|
54
|
|
|
list($k, $v) = each($key); |
|
55
|
|
|
|
|
56
|
|
|
// Set string representation |
|
57
|
|
|
$key = $k . '[' . $v . ']'; |
|
58
|
|
|
|
|
59
|
|
|
// Set expiration time to -1hr (will cause browser deletion) |
|
60
|
|
|
setcookie($key, false, time() - 3600); |
|
61
|
|
|
|
|
62
|
|
|
// Unset the cookie |
|
63
|
|
|
unset($_COOKIE[$k][$v]); |
|
64
|
|
|
} // Check for cookie array |
|
65
|
|
|
elseif (is_array($_COOKIE[$key])) { |
|
66
|
|
|
foreach ($_COOKIE[$key] as $k => $v) { |
|
67
|
|
|
// Set string representation |
|
68
|
|
|
$cookie = $key . '[' . $k . ']'; |
|
69
|
|
|
|
|
70
|
|
|
// Set expiration time to -1hr (will cause browser deletion) |
|
71
|
|
|
setcookie($cookie, false, time() - 3600); |
|
72
|
|
|
|
|
73
|
|
|
// Unset the cookie |
|
74
|
|
|
unset($_COOKIE[$key][$k]); |
|
75
|
|
|
} |
|
76
|
|
|
} // Unset single cookie |
|
77
|
|
|
else { |
|
78
|
|
|
// Set expiration time to -1hr (will cause browser deletion) |
|
79
|
|
|
setcookie($key, false, time() - 3600); |
|
80
|
|
|
|
|
81
|
|
|
// Unset key |
|
82
|
|
|
unset($_COOKIE[$key]); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// See if a cookie key exists |
|
88
|
|
|
/** |
|
89
|
|
|
* @param $key |
|
90
|
|
|
* |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function exists($key) |
|
94
|
|
|
{ |
|
95
|
|
|
// Change string representation array to key/value array |
|
96
|
|
|
$key = self::_scrubKey($key); |
|
97
|
|
|
|
|
98
|
|
|
// Check for array |
|
99
|
|
|
if (is_array($key)) { |
|
100
|
|
|
// Grab key/value pair |
|
101
|
|
|
list($k, $v) = each($key); |
|
102
|
|
|
|
|
103
|
|
|
// Check for key/value pair and return |
|
104
|
|
|
if (isset($_COOKIE[$k][$v])) { |
|
105
|
|
|
return true; |
|
106
|
|
|
} |
|
107
|
|
|
} // If key exists, return true |
|
108
|
|
|
elseif (isset($_COOKIE[$key])) { |
|
109
|
|
|
return true; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
// Key does not exist |
|
113
|
|
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// Get cookie information |
|
117
|
|
|
/** |
|
118
|
|
|
* @param $key |
|
119
|
|
|
* |
|
120
|
|
|
* @return null |
|
121
|
|
|
*/ |
|
122
|
|
|
public static function get($key) |
|
123
|
|
|
{ |
|
124
|
|
|
// Change string representation array to key/value array |
|
125
|
|
|
$key = self::_scrubKey($key); |
|
126
|
|
|
|
|
127
|
|
|
// Check for array |
|
128
|
|
|
if (is_array($key)) { |
|
129
|
|
|
// Grab key/value pair |
|
130
|
|
|
list($k, $v) = each($key); |
|
131
|
|
|
|
|
132
|
|
|
// Check for key/value pair and return |
|
133
|
|
|
if (isset($_COOKIE[$k][$v])) { |
|
134
|
|
|
return $_COOKIE[$k][$v]; |
|
135
|
|
|
} |
|
136
|
|
|
} // Return single key if it's set |
|
137
|
|
|
elseif (isset($_COOKIE[$key])) { |
|
138
|
|
|
return $_COOKIE[$key]; |
|
139
|
|
|
} // Otherwise return null |
|
140
|
|
|
else { |
|
141
|
|
|
return null; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return null; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
// Return the cookie array |
|
148
|
|
|
public static function contents() |
|
|
|
|
|
|
149
|
|
|
{ |
|
150
|
|
|
return $_COOKIE; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
// Set cookie information |
|
154
|
|
|
/** |
|
155
|
|
|
* @param $key |
|
156
|
|
|
* @param $value |
|
157
|
|
|
* @param int $expire |
|
158
|
|
|
* @param string $path |
|
159
|
|
|
* @param string $domain |
|
160
|
|
|
* @param bool $secure |
|
161
|
|
|
* @param bool $httponly |
|
162
|
|
|
*/ |
|
163
|
|
|
public static function set($key, $value, $expire = 0, /* Default expire time (session, 1 week = 604800) */ |
|
164
|
|
|
$path = '', /* Default path */ |
|
165
|
|
|
$domain = '', /* Default domain */ |
|
166
|
|
|
$secure = false, /* Does this cookie need a secure HTTPS connection? */ |
|
167
|
|
|
$httponly = true /* Can non-HTTP services access this cookie (IE: javascript)? */ |
|
168
|
|
|
) { |
|
169
|
|
|
// Make sure they aren't trying to set a reserved word |
|
170
|
|
|
if (!in_array($key, self::$_reserved)) { |
|
171
|
|
|
// If $key is in array format, change it to string representation |
|
172
|
|
|
$key = self::_scrubKey($key, true); |
|
173
|
|
|
|
|
174
|
|
|
// Store the cookie |
|
175
|
|
|
setcookie($key, $value, $expire, $path, $domain, $secure, $httponly); |
|
176
|
|
|
} // Otherwise, throw an error |
|
177
|
|
|
else { |
|
178
|
|
|
Error::warning('Could not set key -- it is reserved.', __CLASS__); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
// Converts strings to arrays (or vice versa if toString = true) |
|
183
|
|
|
/** |
|
184
|
|
|
* @param $key |
|
185
|
|
|
* @param bool $toString |
|
186
|
|
|
* |
|
187
|
|
|
* @return array|string |
|
188
|
|
|
*/ |
|
189
|
|
|
private static function _scrubKey($key, $toString = false) |
|
190
|
|
|
{ |
|
191
|
|
|
// Converting from array to string |
|
192
|
|
|
if ($toString) { |
|
193
|
|
|
// If $key is in array format, change it to string representation |
|
194
|
|
|
if (is_array($key)) { |
|
195
|
|
|
// Grab key/value pair |
|
196
|
|
|
list($k, $v) = each($key); |
|
197
|
|
|
|
|
198
|
|
|
// Set string representation |
|
199
|
|
|
$key = $k . '[' . $v . ']'; |
|
200
|
|
|
} |
|
201
|
|
|
} // Converting from string to array |
|
202
|
|
|
elseif (!is_array($key)) { |
|
203
|
|
|
// is this a string representation of an array? |
|
204
|
|
|
if (preg_match('/([\w\d]+)\[([\w\d]+)\]$/i', $key, $matches)) { |
|
205
|
|
|
// Store as key/value pair |
|
206
|
|
|
$key = array($matches[1] => $matches[2]); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
// Return key |
|
211
|
|
|
return $key; |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.