1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_forms\Controls\Security; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use ArrayAccess; |
7
|
|
|
use kalanis\kw_forms\Controls\Hidden; |
8
|
|
|
use kalanis\kw_rules\Interfaces\IRules; |
9
|
|
|
use LogicException; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class MultiSend |
14
|
|
|
* @package kalanis\kw_forms\Controls\Security |
15
|
|
|
* Hidden entry which adds Multisend check |
16
|
|
|
* Must be child of hidden due necessity of pre-setting position in render |
17
|
|
|
* This one did not set another value to compare, on the other way csrf sets new value |
18
|
|
|
*/ |
19
|
|
|
class MultiSend extends Hidden |
20
|
|
|
{ |
21
|
|
|
protected ?ArrayAccess $cookie = null; |
22
|
|
|
|
23
|
2 |
|
public function setHidden(string $alias, ArrayAccess &$cookie, string $errorMessage): self |
24
|
|
|
{ |
25
|
2 |
|
$this->cookie = $cookie; |
26
|
2 |
|
$this->setEntry($alias); |
27
|
2 |
|
$this->setValue(uniqid('multisend', true)); |
28
|
2 |
|
$this->addCheckToStack(strval($this->getValue())); |
29
|
2 |
|
parent::addRule(IRules::SATISFIES_CALLBACK, $errorMessage, [$this, 'checkMulti']); |
30
|
2 |
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string|int|float $incomingValue |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
2 |
|
public function checkMulti($incomingValue): bool |
38
|
|
|
{ |
39
|
2 |
|
return $this->removeExistingCheckFromStack(strval($incomingValue)); |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
protected function addCheckToStack(string $value): void |
43
|
|
|
{ |
44
|
2 |
|
$hashStack = $this->hashStack(); |
45
|
2 |
|
$hashStack[$value] = 'FORM_SENDED'; |
46
|
2 |
|
$this->getCookie()->offsetSet($this->getKey() . 'SubmitCheck', json_encode($hashStack)); |
47
|
2 |
|
} |
48
|
|
|
|
49
|
2 |
|
protected function removeExistingCheckFromStack(string $value): bool |
50
|
|
|
{ |
51
|
2 |
|
$hashStack = $this->hashStack(); |
52
|
1 |
|
if (isset($hashStack[$value])) { |
53
|
1 |
|
unset($hashStack[$value]); |
54
|
1 |
|
$this->getCookie()->offsetSet($this->getKey() . 'SubmitCheck', json_encode($hashStack)); |
55
|
1 |
|
return true; |
56
|
|
|
} |
57
|
1 |
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array<string, string> |
62
|
|
|
*/ |
63
|
3 |
|
protected function hashStack(): array |
64
|
|
|
{ |
65
|
3 |
|
$hashStack = $this->getCookie()->offsetExists($this->getKey() . 'SubmitCheck') |
66
|
1 |
|
? (array) json_decode($this->getCookie()->offsetGet($this->getKey() . 'SubmitCheck'), true) |
67
|
2 |
|
: null ; |
68
|
2 |
|
if (is_null($hashStack)) { |
69
|
2 |
|
$hashStack = []; |
70
|
|
|
} |
71
|
2 |
|
return $hashStack; |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
protected function getCookie(): ArrayAccess |
75
|
|
|
{ |
76
|
3 |
|
if (empty($this->cookie)) { |
77
|
1 |
|
throw new LogicException('Initialize the element first!'); |
78
|
|
|
} |
79
|
2 |
|
return $this->cookie; |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
public function addRule(/** @scrutinizer ignore-unused */ string $ruleName, /** @scrutinizer ignore-unused */ string $errorText, /** @scrutinizer ignore-unused */ ...$args): void |
83
|
|
|
{ |
84
|
|
|
// no additional rules applicable |
85
|
2 |
|
} |
86
|
|
|
|
87
|
2 |
|
public function addRules(/** @scrutinizer ignore-unused */ iterable $rules = []): void |
88
|
|
|
{ |
89
|
|
|
// no rules add applicable |
90
|
2 |
|
} |
91
|
|
|
|
92
|
2 |
|
public function removeRules(): void |
93
|
|
|
{ |
94
|
|
|
// no rules removal applicable |
95
|
2 |
|
} |
96
|
|
|
|
97
|
2 |
|
public function renderErrors($errors): string |
98
|
|
|
{ |
99
|
2 |
|
return ''; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|