cookie.php
Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 App::import('Core', 'Security');
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 class CookieComponent extends Object {
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 var $name = 'CakeCookie';
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 var $time = null;
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 var $path = '/';
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 var $domain = '';
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 var $secure = false;
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 var $key = null;
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 var $__values = array();
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 var $__type = 'cipher';
00139
00140
00141
00142
00143
00144
00145 var $__reset = null;
00146
00147
00148
00149
00150
00151
00152
00153
00154 var $__expires = 0;
00155
00156
00157
00158
00159
00160
00161 function initialize(&$controller, $settings) {
00162 $this->key = Configure::read('Security.salt');
00163 $this->_set($settings);
00164 }
00165
00166
00167
00168
00169
00170 function startup() {
00171 $this->__expire($this->time);
00172
00173 if (isset($_COOKIE[$this->name])) {
00174 $this->__values = $this->__decrypt($_COOKIE[$this->name]);
00175 }
00176 }
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195 function write($key, $value = null, $encrypt = true, $expires = null) {
00196 if (is_null($encrypt)) {
00197 $encrypt = true;
00198 }
00199
00200 $this->__encrypted = $encrypt;
00201 $this->__expire($expires);
00202
00203 if (!is_array($key) && $value !== null) {
00204 $name = $this->__cookieVarNames($key);
00205
00206 if (count($name) > 1) {
00207 $this->__values[$name[0]][$name[1]] = $value;
00208 $this->__write("[".$name[0]."][".$name[1]."]", $value);
00209 } else {
00210 $this->__values[$name[0]] = $value;
00211 $this->__write("[".$name[0]."]", $value);
00212 }
00213 } else {
00214 foreach ($key as $names => $value) {
00215 $name = $this->__cookieVarNames($names);
00216
00217 if (count($name) > 1) {
00218 $this->__values[$name[0]][$name[1]] = $value;
00219 $this->__write("[".$name[0]."][".$name[1]."]", $value);
00220 } else {
00221 $this->__values[$name[0]] = $value;
00222 $this->__write("[".$name[0]."]", $value);
00223 }
00224 }
00225 }
00226 $this->__encrypted = true;
00227 }
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 function read($key = null) {
00239 if (empty($this->__values) && isset($_COOKIE[$this->name])) {
00240 $this->__values = $this->__decrypt($_COOKIE[$this->name]);
00241 }
00242
00243 if (is_null($key)) {
00244 return $this->__values;
00245 }
00246 $name = $this->__cookieVarNames($key);
00247
00248 if (count($name) > 1) {
00249 if (isset($this->__values[$name[0]])) {
00250 if(isset($this->__values[$name[0]][$name[1]])) {
00251 return $this->__values[$name[0]][$name[1]];
00252 }
00253 }
00254 return null;
00255 } else {
00256 if (isset($this->__values[$name[0]])) {
00257 $value = $this->__values[$name[0]];
00258 return $value;
00259 }
00260 return null;
00261 }
00262 }
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275 function del($key) {
00276 $name = $this->__cookieVarNames($key);
00277
00278 if (count($name) > 1) {
00279 if (isset($this->__values[$name[0]])) {
00280 unset($this->__values[$name[0]][$name[1]]);
00281 $this->__delete("[".$name[0]."][".$name[1]."]");
00282 }
00283 } else {
00284 if (isset($this->__values[$name[0]])) {
00285 if (is_array($this->__values[$name[0]])) {
00286 foreach ($this->__values[$name[0]] as $key => $value) {
00287 $this->__delete("[".$name[0]."][".$key."]");
00288 }
00289 }
00290 $this->__delete("[".$name[0]."]");
00291 unset($this->__values[$name[0]]);
00292 }
00293 }
00294 }
00295
00296
00297
00298
00299
00300
00301
00302
00303 function destroy() {
00304 if (isset($_COOKIE[$this->name])) {
00305 $this->__values = $this->__decrypt($_COOKIE[$this->name]);
00306 }
00307
00308 foreach ($this->__values as $name => $value) {
00309 if (is_array($value)) {
00310 foreach ($value as $key => $val) {
00311 unset($this->__values[$name][$key]);
00312 $this->__delete("[$name][$key]");
00313 }
00314 }
00315 unset($this->__values[$name]);
00316 $this->__delete("[$name]");
00317 }
00318 }
00319
00320
00321
00322
00323
00324
00325
00326 function type($type = 'cipher') {
00327 $this->__type = 'cipher';
00328 }
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343 function __expire($expires = null) {
00344 $now = time();
00345 if (is_null($expires)) {
00346 return $this->__expires;
00347 }
00348 $this->__reset = $this->__expires;
00349 if (is_integer($expires) || is_numeric($expires)) {
00350 return $this->__expires = $now + intval($expires);
00351 } else {
00352 return $this->__expires = strtotime($expires, $now);
00353 }
00354 }
00355
00356
00357
00358
00359
00360
00361
00362 function __write($name, $value) {
00363 setcookie($this->name . "$name", $this->__encrypt($value), $this->__expires, $this->path, $this->domain, $this->secure);
00364
00365 if (!is_null($this->__reset)) {
00366 $this->__expires = $this->__reset;
00367 $this->__reset = null;
00368 }
00369 }
00370
00371
00372
00373
00374
00375
00376 function __delete($name) {
00377 setcookie($this->name . $name, '', time() - 42000, $this->path, $this->domain, $this->secure);
00378 }
00379
00380
00381
00382
00383
00384
00385
00386 function __encrypt($value) {
00387 if (is_array($value)) {
00388 $value = $this->__implode($value);
00389 }
00390
00391 if ($this->__encrypted === true) {
00392 $type = $this->__type;
00393 $value = "Q2FrZQ==." .base64_encode(Security::$type($value, $this->key));
00394 }
00395 return($value);
00396 }
00397
00398
00399
00400
00401
00402
00403
00404 function __decrypt($values) {
00405 $decrypted = array();
00406 $type = $this->__type;
00407
00408 foreach ($values as $name => $value) {
00409 if (is_array($value)) {
00410 foreach ($value as $key => $val) {
00411 $pos = strpos($val, 'Q2FrZQ==.');
00412 $decrypted[$name][$key] = $this->__explode($val);
00413
00414 if ($pos !== false) {
00415 $val = substr($val, 8);
00416 $decrypted[$name][$key] = $this->__explode(Security::$type(base64_decode($val), $this->key));
00417 }
00418 }
00419 } else {
00420 $pos = strpos($value, 'Q2FrZQ==.');
00421 $decrypted[$name] = $this->__explode($value);
00422
00423 if ($pos !== false) {
00424 $value = substr($value, 8);
00425 $decrypted[$name] = $this->__explode(Security::$type(base64_decode($value), $this->key));
00426 }
00427 }
00428 }
00429
00430 return($decrypted);
00431 }
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441 function __cookieVarNames($name) {
00442 if (is_string($name)) {
00443 if (strpos($name, ".")) {
00444 $name = explode(".", $name);
00445 } else {
00446 $name = array($name);
00447 }
00448 }
00449 return $name;
00450 }
00451
00452
00453
00454
00455
00456
00457
00458 function __implode($array) {
00459 $string = '';
00460 foreach ($array as $key => $value) {
00461 $string .= ',' . $key . '|' . $value;
00462 }
00463 return substr($string, 1);
00464 }
00465
00466
00467
00468
00469
00470
00471
00472 function __explode($string) {
00473 $array = array();
00474 foreach (explode(',', $string) as $pair) {
00475 $key = explode('|', $pair);
00476 if (!isset($key[1])) {
00477 return $key[0];
00478 }
00479 $array[$key[0]] = $key[1];
00480 }
00481 return $array;
00482 }
00483 }
00484 ?>