1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Cookies plugin for Craft CMS |
5
|
|
|
* |
6
|
|
|
* @link https://nystudio107.com/ |
|
|
|
|
7
|
|
|
* @copyright Copyright (c) nystudio107 |
|
|
|
|
8
|
|
|
* @license MIT License https://opensource.org/licenses/MIT |
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\cookies\services; |
12
|
|
|
|
13
|
|
|
use Craft; |
14
|
|
|
use craft\base\Component; |
15
|
|
|
use yii\base\Exception; |
16
|
|
|
use yii\base\InvalidConfigException; |
17
|
|
|
use yii\web\Cookie; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Cookies service |
21
|
|
|
* |
22
|
|
|
* @author nystudio107 |
|
|
|
|
23
|
|
|
* @package Cookies |
|
|
|
|
24
|
|
|
* @since 1.1.0 |
|
|
|
|
25
|
|
|
*/ |
|
|
|
|
26
|
|
|
class CookiesService extends Component |
27
|
|
|
{ |
28
|
|
|
/** |
|
|
|
|
29
|
|
|
* Set a cookie |
30
|
|
|
*/ |
|
|
|
|
31
|
|
|
public function set( |
32
|
|
|
string $name = '', |
33
|
|
|
string $value = '', |
34
|
|
|
int $expire = 0, |
35
|
|
|
string $path = '/', |
36
|
|
|
string $domain = '', |
37
|
|
|
bool $secure = false, |
38
|
|
|
bool $httpOnly = false, |
39
|
|
|
string $sameSite = 'Lax', |
40
|
|
|
): void { |
41
|
|
|
if (empty($value)) { |
42
|
|
|
Craft::$app->response->cookies->remove($name); |
43
|
|
|
} else { |
44
|
|
|
$domain = empty($domain) ? Craft::$app->getConfig()->getGeneral()->defaultCookieDomain : $domain; |
45
|
|
|
if (PHP_VERSION_ID >= 70300) { |
46
|
|
|
setcookie($name, $value, [ |
|
|
|
|
47
|
|
|
'expires' => $expire, |
48
|
|
|
'path' => $path, |
49
|
|
|
'domain' => $domain, |
50
|
|
|
'secure' => $secure, |
51
|
|
|
'httponly' => $httpOnly, |
52
|
|
|
'samesite' => $sameSite, |
53
|
|
|
]); |
|
|
|
|
54
|
|
|
} else { |
55
|
|
|
setcookie($name, $value, ['expires' => $expire, 'path' => $path, 'domain' => $domain, 'secure' => $secure, 'httponly' => $httpOnly]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$_COOKIE[$name] = $value; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
|
|
|
|
63
|
|
|
* Get a cookie |
64
|
|
|
*/ |
|
|
|
|
65
|
|
|
public function get(string $name = ''): string |
66
|
|
|
{ |
67
|
|
|
return $_COOKIE[$name] ?? ''; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
|
|
|
|
71
|
|
|
* Set a secure cookie |
72
|
|
|
*/ |
|
|
|
|
73
|
|
|
public function setSecure( |
74
|
|
|
string $name = '', |
75
|
|
|
string $value = '', |
76
|
|
|
int $expire = 0, |
77
|
|
|
string $path = '/', |
78
|
|
|
string $domain = '', |
79
|
|
|
bool $secure = false, |
80
|
|
|
bool $httpOnly = false, |
81
|
|
|
string $sameSite = 'Lax', |
82
|
|
|
): void { |
83
|
|
|
if (empty($value)) { |
84
|
|
|
Craft::$app->response->cookies->remove($name); |
85
|
|
|
} else { |
86
|
|
|
$domain = empty($domain) ? Craft::$app->getConfig()->getGeneral()->defaultCookieDomain : $domain; |
87
|
|
|
$cookie = new Cookie(['name' => $name, 'value' => '']); |
88
|
|
|
|
89
|
|
|
try { |
90
|
|
|
$cookie->value = Craft::$app->security->hashData(base64_encode(serialize($value))); |
|
|
|
|
91
|
|
|
} catch (InvalidConfigException|Exception $e) { |
92
|
|
|
Craft::error( |
93
|
|
|
'Error setting secure cookie: ' . $e->getMessage(), |
94
|
|
|
__METHOD__ |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$cookie->expire = $expire; |
101
|
|
|
$cookie->path = $path; |
102
|
|
|
$cookie->domain = $domain; |
103
|
|
|
$cookie->secure = $secure; |
104
|
|
|
$cookie->httpOnly = $httpOnly; |
105
|
|
|
if (PHP_VERSION_ID >= 70300) { |
106
|
|
|
$cookie->sameSite = $sameSite; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
Craft::$app->response->cookies->add($cookie); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
|
|
|
|
114
|
|
|
* Get a secure cookie |
115
|
|
|
*/ |
|
|
|
|
116
|
|
|
public function getSecure(string $name = ''): string |
117
|
|
|
{ |
118
|
|
|
$result = ''; |
119
|
|
|
$cookie = Craft::$app->request->cookies->get($name); |
120
|
|
|
if ($cookie !== null) { |
121
|
|
|
try { |
122
|
|
|
$data = Craft::$app->security->validateData($cookie->value); |
|
|
|
|
123
|
|
|
} catch (InvalidConfigException|Exception $e) { |
124
|
|
|
Craft::error( |
125
|
|
|
'Error getting secure cookie: ' . $e->getMessage(), |
126
|
|
|
__METHOD__ |
127
|
|
|
); |
128
|
|
|
$data = false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ( |
|
|
|
|
132
|
|
|
!empty($cookie->value) |
|
|
|
|
133
|
|
|
&& $data !== false |
134
|
|
|
) { |
135
|
|
|
$result = unserialize(base64_decode($data), ['allowed_classes' => false]); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $result; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|