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: * @deprecated since 2.5.9 - will be removed in future versions
19: */
20: class Cookie
21: {
22: // Reserved session keys
23: private static $_reserved = array('XOLOGGERVIEW', 'xoops_user');
24:
25: // Static class cannot be initialized
26: /**
27: *
28: */
29: private function __construct()
30: {
31: }
32:
33: // Alias for delete() function
34: /**
35: * @param $key
36: */
37: public static function del($key)
38: {
39: self::delete($key);
40: }
41:
42: // Delete a cookie
43: /**
44: * @param $key
45: */
46: public static function delete($key)
47: {
48: // Change string representation array to key/value array
49: $key = self::_scrubKey($key);
50:
51: // Make sure the cookie exists
52: if (self::exists($key)) {
53: // Check for key array
54: if (is_array($key)) {
55: // Grab key/value pair
56: list($k, $v) = each($key);
57:
58: // Set string representation
59: $key = $k . '[' . $v . ']';
60:
61: // Set expiration time to -1hr (will cause browser deletion)
62: setcookie($key, false, time() - 3600);
63:
64: // Unset the cookie
65: unset($_COOKIE[$k][$v]);
66: } // Check for cookie array
67: elseif (is_array($_COOKIE[$key])) {
68: foreach ($_COOKIE[$key] as $k => $v) {
69: // Set string representation
70: $cookie = $key . '[' . $k . ']';
71:
72: // Set expiration time to -1hr (will cause browser deletion)
73: setcookie($cookie, false, time() - 3600);
74:
75: // Unset the cookie
76: unset($_COOKIE[$key][$k]);
77: }
78: } // Unset single cookie
79: else {
80: // Set expiration time to -1hr (will cause browser deletion)
81: setcookie($key, false, time() - 3600);
82:
83: // Unset key
84: unset($_COOKIE[$key]);
85: }
86: }
87: }
88:
89: // See if a cookie key exists
90: /**
91: * @param $key
92: *
93: * @return bool
94: */
95: public static function exists($key)
96: {
97: // Change string representation array to key/value array
98: $key = self::_scrubKey($key);
99:
100: // Check for array
101: if (is_array($key)) {
102: // Grab key/value pair
103: list($k, $v) = each($key);
104:
105: // Check for key/value pair and return
106: if (isset($_COOKIE[$k][$v])) {
107: return true;
108: }
109: } // If key exists, return true
110: elseif (isset($_COOKIE[$key])) {
111: return true;
112: }
113:
114: // Key does not exist
115: return false;
116: }
117:
118: // Get cookie information
119: /**
120: * @param $key
121: *
122: * @return null
123: */
124: public static function get($key)
125: {
126: // Change string representation array to key/value array
127: $key = self::_scrubKey($key);
128:
129: // Check for array
130: if (is_array($key)) {
131: // Grab key/value pair
132: list($k, $v) = each($key);
133:
134: // Check for key/value pair and return
135: if (isset($_COOKIE[$k][$v])) {
136: return $_COOKIE[$k][$v];
137: }
138: } // Return single key if it's set
139: elseif (isset($_COOKIE[$key])) {
140: return $_COOKIE[$key];
141: } // Otherwise return null
142: else {
143: return null;
144: }
145:
146: return null;
147: }
148:
149: // Return the cookie array
150: public static function contents()
151: {
152: return $_COOKIE;
153: }
154:
155: // Set cookie information
156: /**
157: * @param $key
158: * @param $value
159: * @param int $expire
160: * @param string $path
161: * @param string $domain
162: * @param bool $secure
163: * @param bool $httponly
164: */
165: public static function set($key, $value, $expire = 0, /* Default expire time (session, 1 week = 604800) */
166: $path = '', /* Default path */
167: $domain = '', /* Default domain */
168: $secure = false, /* Does this cookie need a secure HTTPS connection? */
169: $httponly = true /* Can non-HTTP services access this cookie (IE: javascript)? */
170: ) {
171: // Make sure they aren't trying to set a reserved word
172: if (!in_array($key, self::$_reserved)) {
173: // If $key is in array format, change it to string representation
174: $key = self::_scrubKey($key, true);
175:
176: // Store the cookie
177: setcookie($key, $value, $expire, $path, $domain, $secure, $httponly);
178: } // Otherwise, throw an error
179: else {
180: Error::warning('Could not set key -- it is reserved.', __CLASS__);
181: }
182: }
183:
184: // Converts strings to arrays (or vice versa if toString = true)
185: /**
186: * @param $key
187: * @param bool $toString
188: *
189: * @return array|string
190: */
191: private static function _scrubKey($key, $toString = false)
192: {
193: // Converting from array to string
194: if ($toString) {
195: // If $key is in array format, change it to string representation
196: if (is_array($key)) {
197: // Grab key/value pair
198: list($k, $v) = each($key);
199:
200: // Set string representation
201: $key = $k . '[' . $v . ']';
202: }
203: } // Converting from string to array
204: elseif (!is_array($key)) {
205: // is this a string representation of an array?
206: if (preg_match('/([\w\d]+)\[([\w\d]+)\]$/i', $key, $matches)) {
207: // Store as key/value pair
208: $key = array($matches[1] => $matches[2]);
209: }
210: }
211:
212: // Return key
213: return $key;
214: }
215: }
216: