|
1
|
|
|
<?php /** MicroCookie */ |
|
2
|
|
|
|
|
3
|
|
|
namespace Micro\Web; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Cookie class file. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Oleg Lunegov <[email protected]> |
|
9
|
|
|
* @link https://github.com/linpax/microphp-framework |
|
10
|
|
|
* @copyright Copyright © 2013 Oleg Lunegov |
|
11
|
|
|
* @license /LICENSE |
|
12
|
|
|
* @package Micro |
|
13
|
|
|
* @subpackage Web |
|
14
|
|
|
* @version 1.0 |
|
15
|
|
|
* @since 1.0 |
|
16
|
|
|
*/ |
|
17
|
|
|
class Cookie implements ICookie |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
/** @var IRequest $request */ |
|
20
|
|
|
protected $request; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Constructor of object |
|
25
|
|
|
* |
|
26
|
|
|
* @access public |
|
27
|
|
|
* |
|
28
|
|
|
* @param IRequest $request |
|
29
|
|
|
* |
|
30
|
|
|
* @result void |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(IRequest $request) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->request = $request; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Get cookie |
|
39
|
|
|
* |
|
40
|
|
|
* @access public |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $name cookie name |
|
43
|
|
|
* |
|
44
|
|
|
* @return mixed|bool |
|
45
|
|
|
*/ |
|
46
|
|
|
public function get($name) |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->request->cookie($name); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Delete cookie |
|
53
|
|
|
* |
|
54
|
|
|
* @access public |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $name cookie name |
|
57
|
|
|
* |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
|
|
public function del($name) |
|
61
|
|
|
{ |
|
62
|
|
|
if ($this->exists($name)) { |
|
63
|
|
|
return $this->set($name, false, time() - 3600); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Exists cookie |
|
71
|
|
|
* |
|
72
|
|
|
* @access public |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $name cookie name |
|
75
|
|
|
* |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
|
|
public function exists($name) |
|
79
|
|
|
{ |
|
80
|
|
|
return (bool)$this->request->cookie($name); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Set cookie |
|
85
|
|
|
* |
|
86
|
|
|
* @access public |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $name cookie name |
|
89
|
|
|
* @param mixed $value data value |
|
90
|
|
|
* @param int $expire life time |
|
91
|
|
|
* @param string $path path access cookie |
|
92
|
|
|
* @param string $domain domain access cookie |
|
93
|
|
|
* @param bool $secure use SSL? |
|
94
|
|
|
* @param bool $httponly disable on JS? |
|
95
|
|
|
* |
|
96
|
|
|
* @return bool |
|
97
|
|
|
*/ |
|
98
|
|
|
public function set($name, $value, $expire = 0, $path = '/', $domain = '', $secure = false, $httponly = true) |
|
99
|
|
|
{ |
|
100
|
|
|
return setcookie($name, $value, $expire, $path, $domain, $secure, $httponly); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|