1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Saito - The Threaded Web Forum |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
9
|
|
|
* @link https://github.com/Schlaefer/Saito |
10
|
|
|
* @license http://opensource.org/licenses/MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Saito\User\Cookie; |
14
|
|
|
|
15
|
|
|
use Cake\Chronos\Chronos; |
16
|
|
|
use Cake\Controller\Controller; |
17
|
|
|
use Cake\Core\Configure; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Handles the persistent cookie for cookie relogin |
21
|
|
|
*/ |
22
|
|
|
class CurrentUserCookie extends Storage |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritDoc} |
26
|
|
|
*/ |
27
|
|
|
public function __construct(Controller $controller, ?string $key = null, array $config = []) |
28
|
|
|
{ |
29
|
|
|
$key = $key ?: Configure::read('Security.cookieAuthName'); |
30
|
|
|
$config += ['expire' => '+30 days', 'refreshAfter' => '+23 days']; |
31
|
|
|
parent::__construct($controller, $key, $config); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritDoc} |
36
|
|
|
*/ |
37
|
|
|
public function write($id): void |
38
|
|
|
{ |
39
|
|
|
$refreshAfter = Chronos::parse($this->getConfig('refreshAfter')); |
40
|
|
|
$data = ['id' => $id, 'refreshAfter' => $refreshAfter->getTimestamp()]; |
41
|
|
|
parent::write($data); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Gets cookie values |
46
|
|
|
* |
47
|
|
|
* @return null|array cookie values if found, null otherwise |
48
|
|
|
*/ |
49
|
|
|
public function read(): ?array |
50
|
|
|
{ |
51
|
|
|
$cookie = parent::read(); |
52
|
|
|
|
53
|
|
|
if (!is_array($cookie) || empty($cookie['id'])) { |
54
|
|
|
if (!is_null($cookie)) { |
55
|
|
|
// cookie couldn't be deciphered correctly and is a meaningless string |
56
|
|
|
parent::delete(); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->refresh($cookie); |
63
|
|
|
unset($cookie['refreshAfter']); |
64
|
|
|
|
65
|
|
|
return $cookie; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Refreshs the cookie so that regularly visiting users aren't logged-out |
70
|
|
|
* |
71
|
|
|
* Cookie is valid for 30 days and is renewed if used for loggin-in within 7 |
72
|
|
|
* days before expiring. |
73
|
|
|
* |
74
|
|
|
* @param array $cookie cookie-data |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
private function refresh(array $cookie): void |
78
|
|
|
{ |
79
|
|
|
if (empty($cookie['refreshAfter'])) { |
80
|
|
|
/// previous forum version with the cookie missing this field |
81
|
|
|
$cookie['refreshAfter'] = 0; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ((int)$cookie['refreshAfter'] > time()) { |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->write($cookie['id']); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.