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: // Alias for delete() function
27: public static function del($key)
28: {
29: self::delete($key);
30: }
31:
32: // Delete a cookie
33: public static function delete($key)
34: {
35: // Change string representation array to key/value array
36: $key = self::_scrubKey($key);
37:
38: // Make sure the cookie exists
39: if (self::exists($key))
40: {
41: // Check for key array
42: if (is_array($key))
43: {
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: else if (is_array($_COOKIE[$key]))
59: {
60: foreach ($_COOKIE[$key] as $k => $v)
61: {
62: // Set string representation
63: $cookie = $key . '[' . $k . ']';
64:
65: // Set expiration time to -1hr (will cause browser deletion)
66: setcookie($cookie, false, time() - 3600);
67:
68: // Unset the cookie
69: unset($_COOKIE[$key][$k]);
70: }
71: }
72:
73: // Unset single cookie
74: else
75: {
76: // Set expiration time to -1hr (will cause browser deletion)
77: setcookie($key, false, time() - 3600);
78:
79: // Unset key
80: unset($_COOKIE[$key]);
81: }
82: }
83: }
84:
85: // See if a cookie key exists
86: public static function exists($key)
87: {
88: // Change string representation array to key/value array
89: $key = self::_scrubKey($key);
90:
91: // Check for array
92: if (is_array($key))
93: {
94: // Grab key/value pair
95: list ($k, $v) = each($key);
96:
97: // Check for key/value pair and return
98: if (isset($_COOKIE[$k][$v])) return true;
99: }
100:
101: // If key exists, return true
102: else if (isset($_COOKIE[$key])) return true;
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: {
117: // Grab key/value pair
118: list ($k, $v) = each($key);
119:
120: // Check for key/value pair and return
121: if (isset($_COOKIE[$k][$v])) return $_COOKIE[$k][$v];
122: }
123:
124: // Return single key if it's set
125: else if (isset($_COOKIE[$key])) return $_COOKIE[$key];
126:
127: // Otherwise return null
128: else return null;
129: }
130:
131: // Return the cookie array
132: public static function contents()
133: {
134: return $_COOKIE;
135: }
136:
137: // Set cookie information
138: public static function set(
139: $key,
140: $value,
141: $expire = 0, /* Default expire time (session, 1 week = 604800) */
142: $path = '', /* Default path */
143: $domain = '', /* Default domain */
144: $secure = false, /* Does this cookie need a secure HTTPS connection? */
145: $httponly = true /* Can non-HTTP services access this cookie (IE: javascript)? */
146: ){
147: // Make sure they aren't trying to set a reserved word
148: if (!in_array($key, self::$_reserved))
149: {
150: // If $key is in array format, change it to string representation
151: $key = self::_scrubKey($key, true);
152:
153: // Store the cookie
154: setcookie($key, $value, $expire, $path, $domain, $secure, $httponly);
155: }
156:
157: // Otherwise, throw an error
158: else Error::warning('Could not set key -- it is reserved.', __CLASS__);
159: }
160:
161: // Converts strings to arrays (or vice versa if toString = true)
162: private static function _scrubKey($key, $toString = false)
163: {
164: // Converting from array to string
165: if ($toString)
166: {
167: // If $key is in array format, change it to string representation
168: if (is_array($key))
169: {
170: // Grab key/value pair
171: list ($k, $v) = each($key);
172:
173: // Set string representation
174: $key = $k . '[' . $v . ']';
175: }
176: }
177:
178: // Converting from string to array
179: else if (!is_array($key))
180: {
181: // is this a string representation of an array?
182: if (preg_match('/([\w\d]+)\[([\w\d]+)\]$/i', $key, $matches))
183: {
184: // Store as key/value pair
185: $key = array($matches[1] => $matches[2]);
186: }
187: }
188:
189: // Return key
190: return $key;
191: }
192: }
193: