|
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\twigextensions; |
|
12
|
|
|
|
|
13
|
|
|
use nystudio107\cookies\Cookies; |
|
14
|
|
|
use Twig\Extension\AbstractExtension; |
|
15
|
|
|
use Twig\TwigFilter; |
|
16
|
|
|
use Twig\TwigFunction; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Cookies twig extension |
|
20
|
|
|
* |
|
21
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
22
|
|
|
* @package Cookies |
|
|
|
|
|
|
23
|
|
|
* @since 1.1.0 |
|
|
|
|
|
|
24
|
|
|
*/ |
|
|
|
|
|
|
25
|
|
|
class CookiesTwigExtension extends AbstractExtension |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Return our Twig Extension name |
|
29
|
|
|
*/ |
|
|
|
|
|
|
30
|
|
|
public function getName(): string |
|
31
|
|
|
{ |
|
32
|
|
|
return 'Cookies'; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
|
|
|
|
|
36
|
|
|
* @inheritdoc |
|
37
|
|
|
*/ |
|
|
|
|
|
|
38
|
|
|
public function getFilters(): array |
|
39
|
|
|
{ |
|
40
|
|
|
return [ |
|
41
|
|
|
new TwigFilter('setCookie', fn(string $name = "", string $value = "", int $expire = 0, string $path = "/", string $domain = "", bool $secure = false, bool $httpOnly = false, string $sameSite = 'Lax') => $this->setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite)), |
|
42
|
|
|
new TwigFilter('getCookie', fn($name) => $this->getCookie($name)), |
|
43
|
|
|
new TwigFilter('setSecureCookie', fn(string $name = "", string $value = "", int $expire = 0, string $path = "/", string $domain = "", bool $secure = false, bool $httpOnly = false, string $sameSite = 'Lax') => $this->setSecureCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite)), |
|
44
|
|
|
new TwigFilter('getSecureCookie', fn($name) => $this->getSecureCookie($name)), |
|
45
|
|
|
]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
|
|
|
|
|
49
|
|
|
* @inheritdoc |
|
50
|
|
|
*/ |
|
|
|
|
|
|
51
|
|
|
public function getFunctions(): array |
|
52
|
|
|
{ |
|
53
|
|
|
return [ |
|
54
|
|
|
new TwigFunction('setCookie', fn(string $name = "", string $value = "", int $expire = 0, string $path = "/", string $domain = "", bool $secure = false, bool $httpOnly = false, string $sameSite = 'Lax') => $this->setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite)), |
|
55
|
|
|
new TwigFunction('getCookie', fn($name) => $this->getCookie($name)), |
|
56
|
|
|
new TwigFunction('setSecureCookie', fn(string $name = "", string $value = "", int $expire = 0, string $path = "/", string $domain = "", bool $secure = false, bool $httpOnly = false, string $sameSite = 'Lax') => $this->setSecureCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite)), |
|
57
|
|
|
new TwigFunction('getSecureCookie', fn($name) => $this->getSecureCookie($name)), |
|
58
|
|
|
]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
|
|
|
|
|
62
|
|
|
* Set a cookie |
|
63
|
|
|
*/ |
|
|
|
|
|
|
64
|
|
|
public function setCookie( |
|
65
|
|
|
string $name = "", |
|
66
|
|
|
string $value = "", |
|
67
|
|
|
int $expire = 0, |
|
68
|
|
|
string $path = "/", |
|
69
|
|
|
string $domain = "", |
|
70
|
|
|
bool $secure = false, |
|
71
|
|
|
bool $httpOnly = false, |
|
72
|
|
|
string $sameSite = 'Lax', |
|
73
|
|
|
): void { |
|
74
|
|
|
Cookies::$plugin->cookies->set( |
|
75
|
|
|
$name, |
|
76
|
|
|
$value, |
|
77
|
|
|
$expire, |
|
78
|
|
|
$path, |
|
79
|
|
|
$domain, |
|
80
|
|
|
$secure, |
|
81
|
|
|
$httpOnly, |
|
82
|
|
|
$sameSite |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
|
|
|
|
|
87
|
|
|
* Get a cookie |
|
88
|
|
|
*/ |
|
|
|
|
|
|
89
|
|
|
public function getCookie(string $name): string |
|
90
|
|
|
{ |
|
91
|
|
|
return Cookies::$plugin->cookies->get($name); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
|
|
|
|
|
95
|
|
|
* Set a secure cookie |
|
96
|
|
|
*/ |
|
|
|
|
|
|
97
|
|
|
public function setSecureCookie( |
|
98
|
|
|
string $name = "", |
|
99
|
|
|
string $value = "", |
|
100
|
|
|
int $expire = 0, |
|
101
|
|
|
string $path = "/", |
|
102
|
|
|
string $domain = "", |
|
103
|
|
|
bool $secure = false, |
|
104
|
|
|
bool $httpOnly = false, |
|
105
|
|
|
string $sameSite = 'Lax', |
|
106
|
|
|
): void { |
|
107
|
|
|
Cookies::$plugin->cookies->setSecure( |
|
108
|
|
|
$name, |
|
109
|
|
|
$value, |
|
110
|
|
|
$expire, |
|
111
|
|
|
$path, |
|
112
|
|
|
$domain, |
|
113
|
|
|
$secure, |
|
114
|
|
|
$httpOnly, |
|
115
|
|
|
$sameSite |
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
|
|
|
|
|
120
|
|
|
* Get a secure cookie |
|
121
|
|
|
*/ |
|
|
|
|
|
|
122
|
|
|
public function getSecureCookie(string $name): string |
|
123
|
|
|
{ |
|
124
|
|
|
return Cookies::$plugin->cookies->getSecure($name); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|