|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BFW\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Helpers for http requests/responses |
|
7
|
|
|
*/ |
|
8
|
|
|
class Http |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Return the class name of the secure helper. |
|
12
|
|
|
* Allow to extends the secure helper used by method here |
|
13
|
|
|
* |
|
14
|
|
|
* @return string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected static function getSecureHelpersName(): string |
|
17
|
|
|
{ |
|
18
|
|
|
return '\BFW\Helpers\Secure'; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Create a http redirect and kill the script |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $page The page where is the redirect |
|
25
|
|
|
* @param boolean $permanent (default false) If the redirect is permanent |
|
26
|
|
|
* @param boolean $callExit (default false) If at true, the exit function |
|
27
|
|
|
* will be called. |
|
28
|
|
|
* |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function redirect( |
|
32
|
|
|
string $page, |
|
33
|
|
|
bool $permanent = false, |
|
34
|
|
|
bool $callExit = false |
|
35
|
|
|
) { |
|
36
|
|
|
$httpStatus = 302; |
|
37
|
|
|
if ($permanent === true) { |
|
38
|
|
|
$httpStatus = 301; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
http_response_code($httpStatus); |
|
42
|
|
|
header('Location: '.$page); |
|
43
|
|
|
|
|
44
|
|
|
if ($callExit === true) { |
|
45
|
|
|
exit; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get a securised value for a key in $_POST array |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $key The key where is the value to securize |
|
53
|
|
|
* @param string $type The type of data |
|
54
|
|
|
* @param boolean $htmlentities (default: false) If use htmlentities |
|
55
|
|
|
* function to a better security |
|
56
|
|
|
* @param boolean $inline (default: true) If array data are inline |
|
57
|
|
|
* |
|
58
|
|
|
* @return mixed |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function obtainPostKey( |
|
61
|
|
|
string $key, |
|
62
|
|
|
string $type, |
|
63
|
|
|
bool $htmlentities = false, |
|
64
|
|
|
bool $inline = true |
|
65
|
|
|
) { |
|
66
|
|
|
$currentClass = get_called_class(); |
|
67
|
|
|
$secure = $currentClass::getSecureHelpersName(); |
|
68
|
|
|
|
|
69
|
|
|
return $secure::getSecureKeyInArray( |
|
70
|
|
|
$_POST, |
|
71
|
|
|
$key, |
|
72
|
|
|
$type, |
|
73
|
|
|
$htmlentities, |
|
74
|
|
|
$inline |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get a securised value for a key in $_GET array |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $key The key where is the value to securize |
|
82
|
|
|
* @param string $type The type of data |
|
83
|
|
|
* @param boolean $htmlentities (default: false) If use htmlentities |
|
84
|
|
|
* function to a better security |
|
85
|
|
|
* @param boolean $inline (default: true) If array data are inline |
|
86
|
|
|
* |
|
87
|
|
|
* @return mixed |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function obtainGetKey( |
|
90
|
|
|
string $key, |
|
91
|
|
|
string $type, |
|
92
|
|
|
bool $htmlentities = false, |
|
93
|
|
|
bool $inline = true |
|
94
|
|
|
) { |
|
95
|
|
|
$currentClass = get_called_class(); |
|
96
|
|
|
$secure = $currentClass::getSecureHelpersName(); |
|
97
|
|
|
|
|
98
|
|
|
return $secure::getSecureKeyInArray( |
|
99
|
|
|
$_GET, |
|
100
|
|
|
$key, |
|
101
|
|
|
$type, |
|
102
|
|
|
$htmlentities, |
|
103
|
|
|
$inline |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Obtain many securised keys from $_POST array in one time |
|
109
|
|
|
* |
|
110
|
|
|
* @see \BFW\Helpers\Secure::getSecurisedManyKeys |
|
111
|
|
|
* |
|
112
|
|
|
* @param array $keysList The key list to obtain. |
|
113
|
|
|
* @param boolean $throwOnError (defaut true) If a key not exist, throw an |
|
114
|
|
|
* exception. If false, the value will be null into returned array |
|
115
|
|
|
* |
|
116
|
|
|
* @return array |
|
117
|
|
|
* |
|
118
|
|
|
* @throws \Exception If a key is not found and if $throwOnError is true |
|
119
|
|
|
*/ |
|
120
|
|
|
public static function obtainManyPostKeys( |
|
121
|
|
|
array $keysList, |
|
122
|
|
|
bool $throwOnError = true |
|
123
|
|
|
): array { |
|
124
|
|
|
$currentClass = get_called_class(); |
|
125
|
|
|
$secure = $currentClass::getSecureHelpersName(); |
|
126
|
|
|
|
|
127
|
|
|
return $secure::getManySecureKeys($_POST, $keysList, $throwOnError); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Obtain many securised keys from $_GET array in one time |
|
132
|
|
|
* |
|
133
|
|
|
* @see \BFW\Helpers\Secure::getSecurisedManyKeys |
|
134
|
|
|
* |
|
135
|
|
|
* @param array $keysList The key list to obtain. |
|
136
|
|
|
* @param boolean $throwOnError (defaut true) If a key not exist, throw an |
|
137
|
|
|
* exception. If false, the value will be null into returned array |
|
138
|
|
|
* |
|
139
|
|
|
* @return array |
|
140
|
|
|
* |
|
141
|
|
|
* @throws \Exception If a key is not found and if $throwOnError is true |
|
142
|
|
|
*/ |
|
143
|
|
|
public static function obtainManyGetKeys( |
|
144
|
|
|
array $keysList, |
|
145
|
|
|
bool $throwOnError = true |
|
146
|
|
|
): array { |
|
147
|
|
|
$currentClass = get_called_class(); |
|
148
|
|
|
$secure = $currentClass::getSecureHelpersName(); |
|
149
|
|
|
|
|
150
|
|
|
return $secure::getManySecureKeys($_GET, $keysList, $throwOnError); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|