|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Jitamin. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (C) Jitamin Team |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Jitamin\Foundation\Http; |
|
13
|
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Base; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Remember Me Cookie. |
|
18
|
|
|
*/ |
|
19
|
|
|
class RememberMeCookie extends Base |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Cookie name. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
const COOKIE_NAME = 'JM_RM'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Encode the cookie. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $token Session token |
|
32
|
|
|
* @param string $sequence Sequence token |
|
33
|
|
|
* |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
|
|
public function encode($token, $sequence) |
|
37
|
|
|
{ |
|
38
|
|
|
return implode('|', [$token, $sequence]); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Decode the value of a cookie. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $value Raw cookie data |
|
45
|
|
|
* |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public function decode($value) |
|
49
|
|
|
{ |
|
50
|
|
|
list($token, $sequence) = explode('|', $value); |
|
51
|
|
|
|
|
52
|
|
|
return [ |
|
53
|
|
|
'token' => $token, |
|
54
|
|
|
'sequence' => $sequence, |
|
55
|
|
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Return true if the current user has a RememberMe cookie. |
|
60
|
|
|
* |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
|
|
public function hasCookie() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->request->getCookie(self::COOKIE_NAME) !== ''; |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Write and encode the cookie. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $token Session token |
|
72
|
|
|
* @param string $sequence Sequence token |
|
73
|
|
|
* @param string $expiration Cookie expiration |
|
74
|
|
|
* |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
|
|
public function write($token, $sequence, $expiration) |
|
78
|
|
|
{ |
|
79
|
|
|
return setcookie( |
|
80
|
|
|
self::COOKIE_NAME, |
|
81
|
|
|
$this->encode($token, $sequence), |
|
82
|
|
|
$expiration, |
|
83
|
|
|
$this->helper->url->dir(), |
|
|
|
|
|
|
84
|
|
|
null, |
|
85
|
|
|
$this->request->isHTTPS(), |
|
|
|
|
|
|
86
|
|
|
true |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Read and decode the cookie. |
|
92
|
|
|
* |
|
93
|
|
|
* @return mixed |
|
94
|
|
|
*/ |
|
95
|
|
|
public function read() |
|
96
|
|
|
{ |
|
97
|
|
|
$cookie = $this->request->getCookie(self::COOKIE_NAME); |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
if (empty($cookie)) { |
|
100
|
|
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $this->decode($cookie); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Remove the cookie. |
|
108
|
|
|
* |
|
109
|
|
|
* @return bool |
|
110
|
|
|
*/ |
|
111
|
|
|
public function remove() |
|
112
|
|
|
{ |
|
113
|
|
|
return setcookie( |
|
114
|
|
|
self::COOKIE_NAME, |
|
115
|
|
|
'', |
|
116
|
|
|
time() - 3600, |
|
117
|
|
|
$this->helper->url->dir(), |
|
|
|
|
|
|
118
|
|
|
null, |
|
119
|
|
|
$this->request->isHTTPS(), |
|
|
|
|
|
|
120
|
|
|
true |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.