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