1 | <?php |
||||||
2 | |||||||
3 | /** |
||||||
4 | * Cookies plugin for Craft CMS |
||||||
5 | * |
||||||
6 | * @link https://nystudio107.com/ |
||||||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||||||
7 | * @copyright Copyright (c) nystudio107 |
||||||
0 ignored issues
–
show
|
|||||||
8 | * @license MIT License https://opensource.org/licenses/MIT |
||||||
9 | */ |
||||||
0 ignored issues
–
show
|
|||||||
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 |
||||||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||||||
23 | * @package Cookies |
||||||
0 ignored issues
–
show
|
|||||||
24 | * @since 1.1.0 |
||||||
0 ignored issues
–
show
|
|||||||
25 | */ |
||||||
0 ignored issues
–
show
|
|||||||
26 | class CookiesService extends Component |
||||||
27 | { |
||||||
28 | /** |
||||||
0 ignored issues
–
show
|
|||||||
29 | * Set a cookie |
||||||
30 | */ |
||||||
0 ignored issues
–
show
|
|||||||
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, [ |
||||||
0 ignored issues
–
show
|
|||||||
47 | 'expires' => $expire, |
||||||
48 | 'path' => $path, |
||||||
49 | 'domain' => $domain, |
||||||
50 | 'secure' => $secure, |
||||||
51 | 'httponly' => $httpOnly, |
||||||
52 | 'samesite' => $sameSite, |
||||||
53 | ]); |
||||||
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||||||
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 | /** |
||||||
0 ignored issues
–
show
|
|||||||
63 | * Get a cookie |
||||||
64 | */ |
||||||
0 ignored issues
–
show
|
|||||||
65 | public function get(string $name = ''): string |
||||||
66 | { |
||||||
67 | return $_COOKIE[$name] ?? ''; |
||||||
68 | } |
||||||
69 | |||||||
70 | /** |
||||||
0 ignored issues
–
show
|
|||||||
71 | * Set a secure cookie |
||||||
72 | */ |
||||||
0 ignored issues
–
show
|
|||||||
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))); |
||||||
0 ignored issues
–
show
The call to
yii\base\Security::hashData() has too few arguments starting with key .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. ![]() |
|||||||
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 | /** |
||||||
0 ignored issues
–
show
|
|||||||
114 | * Get a secure cookie |
||||||
115 | */ |
||||||
0 ignored issues
–
show
|
|||||||
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); |
||||||
0 ignored issues
–
show
The call to
yii\base\Security::validateData() has too few arguments starting with key .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. ![]() |
|||||||
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 ( |
||||||
0 ignored issues
–
show
|
|||||||
132 | !empty($cookie->value) |
||||||
0 ignored issues
–
show
|
|||||||
133 | && $data !== false |
||||||
134 | ) { |
||||||
135 | $result = unserialize(base64_decode($data), ['allowed_classes' => false]); |
||||||
136 | } |
||||||
137 | } |
||||||
138 | |||||||
139 | return $result; |
||||||
140 | } |
||||||
141 | } |
||||||
142 |