|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Shieldon package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Terry L. <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* php version 7.1.0 |
|
11
|
|
|
* |
|
12
|
|
|
* @category Web-security |
|
13
|
|
|
* @package Shieldon |
|
14
|
|
|
* @author Terry Lin <[email protected]> |
|
15
|
|
|
* @copyright 2019 terrylinooo |
|
16
|
|
|
* @license https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT |
|
17
|
|
|
* @link https://github.com/terrylinooo/shieldon |
|
18
|
|
|
* @see https://shieldon.io |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
declare(strict_types=1); |
|
22
|
|
|
|
|
23
|
|
|
namespace Shieldon\Firewall\Panel; |
|
24
|
|
|
|
|
25
|
|
|
use function count; |
|
26
|
|
|
use function is_string; |
|
27
|
|
|
|
|
28
|
|
|
/* |
|
29
|
|
|
* Tradit for demonstration. |
|
30
|
|
|
*/ |
|
31
|
|
|
trait CsrfTrait |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* See $this->csrf() |
|
35
|
|
|
* |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $csrfField = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Most popular PHP framework has a built-in CSRF protection such as Laravel. |
|
42
|
|
|
* We need to pass the CSRF token for our form actions. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string|array ...$csrfparams The arguments. |
|
45
|
|
|
* |
|
46
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
|
|
public function csrf(...$csrfparams): void |
|
49
|
|
|
{ |
|
50
|
|
|
foreach ($csrfparams as $value) { |
|
51
|
|
|
foreach ($value as $k => $v) { |
|
52
|
|
|
$this->csrfField[] = [ |
|
53
|
|
|
'name' => $k, |
|
54
|
|
|
'value' => $v, |
|
55
|
|
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Output HTML input element with CSRF token. |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
public function fieldCsrf(): string |
|
66
|
|
|
{ |
|
67
|
|
|
$string = ''; |
|
68
|
|
|
if (!empty($this->csrfField)) { |
|
69
|
|
|
foreach ($this->csrfField as $value) { |
|
70
|
|
|
$string .= '<input type="hidden" name="' . $value['name'] . '" value="' . $value['value'] . '" id="csrf-field">'; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
return $string; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get CSRF input fields. |
|
78
|
|
|
* |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getCsrfField(): array |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->csrfField; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Set CSRF input fields. |
|
88
|
|
|
* |
|
89
|
|
|
* @param array $csrfParams |
|
90
|
|
|
* |
|
91
|
|
|
* @return void |
|
92
|
|
|
*/ |
|
93
|
|
|
public function setCsrfField(array $csrfParams): void |
|
94
|
|
|
{ |
|
95
|
|
|
$this->csrfField = $csrfParams; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|