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() |
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
|
|
|
$page, |
33
|
|
|
$permanent = false, |
34
|
|
|
$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
|
|
|
* |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public static function obtainPostKey( |
|
|
|
|
60
|
|
|
$key, |
61
|
|
|
$type, |
62
|
|
|
$htmlentities = false |
63
|
|
|
) { |
64
|
|
|
$currentClass = get_called_class(); |
65
|
|
|
$secure = $currentClass::getSecureHelpersName(); |
66
|
|
|
|
67
|
|
|
return $secure::getSecurisedKeyInArray( |
68
|
|
|
$_POST, |
69
|
|
|
$key, |
70
|
|
|
$type, |
71
|
|
|
$htmlentities |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get a securised value for a key in $_GET array |
77
|
|
|
* |
78
|
|
|
* @param string $key The key where is the value to securize |
79
|
|
|
* @param string $type The type of data |
80
|
|
|
* @param boolean $htmlentities (default: false) If use htmlentities |
81
|
|
|
* function to a better security |
82
|
|
|
* |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
public static function obtainGetKey( |
|
|
|
|
86
|
|
|
$key, |
87
|
|
|
$type, |
88
|
|
|
$htmlentities = false |
89
|
|
|
) { |
90
|
|
|
$currentClass = get_called_class(); |
91
|
|
|
$secure = $currentClass::getSecureHelpersName(); |
92
|
|
|
|
93
|
|
|
return $secure::getSecurisedKeyInArray( |
94
|
|
|
$_GET, |
95
|
|
|
$key, |
96
|
|
|
$type, |
97
|
|
|
$htmlentities |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Obtain many securised keys from $_POST array in one time |
103
|
|
|
* |
104
|
|
|
* @see \BFW\Helpers\Secure::getSecurisedManyKeys |
105
|
|
|
* |
106
|
|
|
* @param array &$arraySrc The source array |
107
|
|
|
* @param array $keysList The key list to obtain. |
108
|
|
|
* @param boolean $throwOnError (defaut true) If a key not exist, throw an |
109
|
|
|
* exception. If false, the value will be null into returned array |
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
* |
113
|
|
|
* @throws Exception If a key is not found and if $throwOnError is true |
114
|
|
|
*/ |
115
|
|
View Code Duplication |
public static function obtainManyPostKeys($keysList, $throwOnError = true) |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$currentClass = get_called_class(); |
118
|
|
|
$secure = $currentClass::getSecureHelpersName(); |
119
|
|
|
|
120
|
|
|
return $secure::getSecurisedManyKeys($_POST, $keysList, $throwOnError); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Obtain many securised keys from $_GET array in one time |
125
|
|
|
* |
126
|
|
|
* @see \BFW\Helpers\Secure::getSecurisedManyKeys |
127
|
|
|
* |
128
|
|
|
* @param array $keysList The key list to obtain. |
129
|
|
|
* @param boolean $throwOnError (defaut true) If a key not exist, throw an |
130
|
|
|
* exception. If false, the value will be null into returned array |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
* |
134
|
|
|
* @throws Exception If a key is not found and if $throwOnError is true |
135
|
|
|
*/ |
136
|
|
View Code Duplication |
public static function obtainManyGetKeys($keysList, $throwOnError = true) |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
$currentClass = get_called_class(); |
139
|
|
|
$secure = $currentClass::getSecureHelpersName(); |
140
|
|
|
|
141
|
|
|
return $secure::getSecurisedManyKeys($_GET, $keysList, $throwOnError); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.