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