|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pmochine\LaravelTongue\Misc; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Encryption\DecryptException; |
|
6
|
|
|
|
|
7
|
|
|
class Cookie |
|
8
|
|
|
{ |
|
9
|
|
|
protected $key; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Indicates if cookies should be serialized. |
|
13
|
|
|
* |
|
14
|
|
|
* @var bool |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $serialize; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(string $cookieKey, bool $serialize = false) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->key = $cookieKey; |
|
21
|
|
|
$this->serialize = $serialize; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function save(string $content): void |
|
25
|
|
|
{ |
|
26
|
|
|
cookie()->queue(cookie()->forever($this->key, $content)); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function get(): ?string |
|
30
|
|
|
{ |
|
31
|
|
|
if (! $this->has()) { |
|
32
|
|
|
return null; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
try { |
|
36
|
|
|
// If you read this. You could help me out. It's quite ugly and not sure how to do it better. |
|
37
|
|
|
// Normally, the middleware "EncryptCookies" would kick in. But since we are calling this |
|
38
|
|
|
// before the middleware even kicks in, we need to decrypt it manually like in: |
|
39
|
|
|
// https://github.com/laravel/framework/blob/7bb90039c5cb42a8f5f2dd489d9936d1ee2668d2/src/Illuminate/Cookie/Middleware/EncryptCookies.php |
|
40
|
|
|
// The thing is that Laravel is updating the algorithm to decrypt the cookie all the time. |
|
41
|
|
|
// And this breaks my code all the time |
|
42
|
|
|
// Now I'm using a "simple" version that needs to be changed in the future again. |
|
43
|
|
|
// Another bug that I used to get was: unserialize(): Error at offset 0 of 2 bytes, since the update 5.6.30 |
|
44
|
|
|
// https://laravel.com/docs/5.6/upgrade#upgrade-5.6.30 |
|
45
|
|
|
// I needed to change decrypt(value, unserialize = false); |
|
46
|
|
|
$value = app('encrypter')->decrypt($this->getCookie(), $this->serialize); |
|
47
|
|
|
// This part is new since Laravel 7.22.0 (Improve cookie encryption) |
|
48
|
|
|
// Not really sure, but I don't use the security improvement at all. |
|
49
|
|
|
// At the end why should I? It's just the locale |
|
50
|
|
|
$pos = strpos($value, '|'); |
|
51
|
|
|
|
|
52
|
|
|
return $pos !== false ? substr($value, $pos + 1) : null; |
|
53
|
|
|
} catch (DecryptException $e) { |
|
54
|
|
|
// Somehow the middleware for decrypting does not kick in here... |
|
55
|
|
|
// but it even fails if we use php artisan <something> (weird) |
|
56
|
|
|
// if it happes we can simply give it normally back |
|
57
|
|
|
return $this->getCookie(); |
|
58
|
|
|
} catch (\Exception $e) { |
|
59
|
|
|
// So I don't return a cookie in that case |
|
60
|
|
|
return null; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function getCookie(): ?string |
|
65
|
|
|
{ |
|
66
|
|
|
$result = request()->cookie($this->key); |
|
67
|
|
|
|
|
68
|
|
|
if (is_array($result)) { |
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $result; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function has(): bool |
|
76
|
|
|
{ |
|
77
|
|
|
return request()->hasCookie($this->key); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|