Navigation | Overlay |
---|---|
t Navigate files | h Toggle hits |
y Change url to tip of branch | m Toggle misses |
b / v Jump to prev/next hit line | p Toggle partial |
z / x Jump to prev/next missed or partial line | 1..9 Toggle flags |
shift + o Open current page in GitHub | a Toggle all on |
/ or ? Show keyboard shortcuts dialog | c Toggle context lines or commits |
1 |
<?php
|
|
2 |
/**
|
|
3 |
* This file is part of the Shieldon package.
|
|
4 |
*
|
|
5 |
* (c) Terry L. <contact@terryl.in>
|
|
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 <contact@terryl.in>
|
|
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 |
/*
|
|
26 |
* Tradit for handling CSRF function.
|
|
27 |
*/
|
|
28 |
trait CsrfTrait |
|
29 |
{
|
|
30 |
/**
|
|
31 |
* Public methods | Desctiotion
|
|
32 |
* ----------------------|---------------------------------------------
|
|
33 |
* csrf | Receive the CSRF name and token from the App.
|
|
34 |
* setCsrfField | Set CSRF input fields.
|
|
35 |
* fieldCsrf | Output HTML input element with CSRF token.
|
|
36 |
* ----------------------|---------------------------------------------
|
|
37 |
*/
|
|
38 |
|
|
39 |
/**
|
|
40 |
* See $this->csrf()
|
|
41 |
*
|
|
42 |
* @var array
|
|
43 |
*/
|
|
44 |
protected $csrfField = []; |
|
45 |
|
|
46 |
/**
|
|
47 |
* Most popular PHP framework has a built-in CSRF protection such as Laravel.
|
|
48 |
* We need to pass the CSRF token for our form actions.
|
|
49 |
*
|
|
50 |
* @param array ...$csrfparams The arguments.
|
|
51 |
*
|
|
52 |
* @return void
|
|
53 |
*/
|
|
54 | 3 |
public function csrf(...$csrfparams): void |
55 |
{
|
|
56 | 3 |
foreach ($csrfparams as $value) { |
57 | 3 |
foreach ($value as $k => $v) { |
58 | 3 |
$this->csrfField[] = [ |
59 | 3 |
'name' => $k, |
60 | 3 |
'value' => $v, |
61 |
];
|
|
62 |
}
|
|
63 |
}
|
|
64 |
}
|
|
65 |
|
|
66 |
/**
|
|
67 |
* Set CSRF input fields.
|
|
68 |
*
|
|
69 |
* @param array $csrfParams
|
|
70 |
*
|
|
71 |
* @return void
|
|
72 |
*/
|
|
73 | 3 |
public function setCsrfField(array $csrfParams): void |
74 |
{
|
|
75 | 3 |
$this->csrfField = $csrfParams; |
76 |
}
|
|
77 |
|
|
78 |
/**
|
|
79 |
* Output HTML input element with CSRF token.
|
|
80 |
*
|
|
81 |
* @return string
|
|
82 |
*/
|
|
83 | 3 |
public function fieldCsrf(): string |
84 |
{
|
|
85 | 3 |
$string = ''; |
86 | 3 |
if (!empty($this->csrfField)) { |
87 | 3 |
foreach ($this->csrfField as $value) { |
88 | 3 |
$string .= '<input type="hidden" name="' . $value['name'] . '" value="' . $value['value'] . '" id="csrf-field">'; |
89 |
}
|
|
90 |
}
|
|
91 | 3 |
return $string; |
92 |
}
|
|
93 |
|
|
94 |
/**
|
|
95 |
* Get CSRF input fields.
|
|
96 |
*
|
|
97 |
* @return array
|
|
98 |
*/
|
|
99 | 3 |
protected function getCsrfField(): array |
100 |
{
|
|
101 | 3 |
return $this->csrfField; |
102 |
}
|
|
103 |
}
|
Read our documentation on viewing source code .