1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BFW; |
4
|
|
|
|
5
|
|
|
use \Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Toutes les fonctions de base utilisé un peu partout dans les scripts |
9
|
|
|
* @author Vermeulen Maxime <[email protected]> |
10
|
|
|
* @package bfw |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Permet d'hasher une chaine de texte, par exemple un mot de passe |
15
|
|
|
* |
16
|
|
|
* @param string $val : la chaine à haser |
17
|
|
|
* |
18
|
|
|
* @return string la chaine hashé |
19
|
|
|
*/ |
20
|
|
|
function hashage($val) |
21
|
|
|
{ |
22
|
|
|
return substr(hash('sha256', md5($val)), 0, 32); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
function securiseKnownTypes($data, $type) |
26
|
|
|
{ |
27
|
|
|
//--- Gestion de type de data --- |
28
|
|
|
$filterType = 'text'; |
29
|
|
|
|
30
|
|
|
if ($type === 'int' || $type === 'integer') { |
31
|
|
|
$filterType = FILTER_VALIDATE_INT; |
32
|
|
|
} elseif ($type === 'float' || $type === 'double') { |
33
|
|
|
$filterType = FILTER_VALIDATE_FLOAT; |
34
|
|
|
} elseif ($type === 'bool' || $type === 'boolean') { |
35
|
|
|
$filterType = FILTER_VALIDATE_BOOLEAN; |
36
|
|
|
} elseif ($type === 'email') { |
37
|
|
|
$filterType = FILTER_VALIDATE_EMAIL; |
38
|
|
|
} |
39
|
|
|
//--- FIN Gestion de type de data --- |
40
|
|
|
|
41
|
|
|
if ($filterType === 'text') { |
42
|
|
|
throw new Exception('Unknown type'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return filter_var($data, $filterType); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
function securise($data, $type, $htmlentities) |
49
|
|
|
{ |
50
|
|
|
if (is_array($data)) { |
51
|
|
|
foreach ($data as $key => $val) { |
52
|
|
|
unset($data[$key]); |
53
|
|
|
|
54
|
|
|
$key = securise($key, true); |
55
|
|
|
$val = securise($val, $htmlentities); |
56
|
|
|
|
57
|
|
|
$data[$key] = $val; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $data; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
try { |
64
|
|
|
return securiseKnownTypes($data, $type); |
65
|
|
|
} catch (Exception $ex) { |
66
|
|
|
if ($ex->getMessage() !== 'Unknown type') { |
67
|
|
|
throw new Exception($ex->getCode(), $ex->getMessage()); |
68
|
|
|
} |
69
|
|
|
//Else : Use securise text type |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$sqlSecureMethod = getSqlSecureMethod(); |
73
|
|
|
if ($sqlSecureMethod !== false) { |
74
|
|
|
$data = $sqlSecureMethod($data); |
75
|
|
|
} else { |
76
|
|
|
$data = addslashes($data); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($htmlentities === false) { |
80
|
|
|
$data = htmlentities($data, ENT_COMPAT | ENT_HTML401, 'UTF-8'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $data; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
function getSqlSecureMethod() |
87
|
|
|
{ |
88
|
|
|
$app = \BFW\Application::getInstance(); |
89
|
|
|
$fct = $app->getConfig('sqlSecureMethod'); |
90
|
|
|
|
91
|
|
|
$callableName = ''; |
92
|
|
|
if (!is_callable($fct, true, $callableName)) { |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $callableName; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Fonction de création de cookie |
101
|
|
|
* |
102
|
|
|
* @param string $name : le nom du cookie |
103
|
|
|
* @param string $value : la valeur du cookie |
104
|
|
|
* @param int $expire : (default: 1209600) durée du cookie en seconde. |
105
|
|
|
* Par défault sur 2 semaines |
106
|
|
|
* |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
|
|
function createCookie($name, $value, $expire = 1209600) |
110
|
|
|
{ |
111
|
|
|
$expireTime = time() + $expire; //Durée d'existance du cookie |
112
|
|
|
setcookie($name, $value, $expireTime); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Fonction nl2br refait. |
117
|
|
|
* Celle de php AJOUTE <br/> APRES les \n, il ne les remplace pas. |
118
|
|
|
* |
119
|
|
|
* @param string $str : le texte à convertir |
120
|
|
|
* |
121
|
|
|
* @return string : le texte converti |
122
|
|
|
*/ |
123
|
|
|
function nl2brReplace($str) |
124
|
|
|
{ |
125
|
|
|
return str_replace("\n", '<br>', $str); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Permet de rediriger une page |
130
|
|
|
* |
131
|
|
|
* @param string $page : la page vers laquelle rediriger |
132
|
|
|
* @param bool $permaet : If it's a permanent redirection for this url or not |
|
|
|
|
133
|
|
|
*/ |
134
|
|
|
function redirection($page, $permanent = false) |
135
|
|
|
{ |
136
|
|
|
$httpStatus = 302; |
137
|
|
|
if ($permanent === true) { |
138
|
|
|
$httpStatus = 301; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
http_response_code($httpStatus); |
142
|
|
|
header('Location: '.$page); |
143
|
|
|
exit; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
function getSecurisedKeyInArray(&$array, $key, $type, $htmlentities = false) |
147
|
|
|
{ |
148
|
|
|
if (!isset($array[$key])) { |
149
|
|
|
throw new Exception('The key '.$key.' not exist'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return securise(trim($array[$key]), $type, $htmlentities); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
function getSecurisedPostKey($key, $type, $htmlentities = false) |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
return getSecurisedKeyInArray($_POST, $type, $htmlentities); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
function getSecurisedGetKey($key, $type, $htmlentities = false) |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
return getSecurisedKeyInArray($_GET, $type, $htmlentities); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Permet de savoir si le mail passé en paramètre est un e-mail valide ou non |
167
|
|
|
* |
168
|
|
|
* @param string $mail : L'adresse e-mail à vérifier |
169
|
|
|
* |
170
|
|
|
* @return integer : |
171
|
|
|
*/ |
172
|
|
|
function validMail($mail) |
173
|
|
|
{ |
174
|
|
|
return securise($mail, 'email'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Vérifie le type d'un ensemble de variable |
179
|
|
|
* |
180
|
|
|
* @param array $vars : Les variables à vérifier |
181
|
|
|
* array(array('type' => 'monType', 'data' => 'mesData), array(...)...) |
182
|
|
|
* |
183
|
|
|
* @return bool |
184
|
|
|
*/ |
185
|
|
|
function verifTypeData($vars) |
186
|
|
|
{ |
187
|
|
|
if (!is_array($vars)) { |
188
|
|
|
return false; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
foreach ($vars as $var) { |
192
|
|
|
if (!is_array($var)) { |
193
|
|
|
return false; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
if (!(!empty($var['type']) && isset($var['data']))) { |
197
|
|
|
return false; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
if (!is_string($var['type'])) { |
201
|
|
|
return false; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
if ($var['type'] === 'int') { |
205
|
|
|
$var['type'] = 'integer'; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
if ($var['type'] === 'float') { |
209
|
|
|
$var['type'] = 'double'; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
if (gettype($var['data']) !== $var['type']) { |
213
|
|
|
return false; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return true; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Retourne l'instance courrante du kernel. La créé si elle n'est pas trouvé. |
222
|
|
|
* |
223
|
|
|
* @return \BFW\Kernel |
224
|
|
|
*/ |
225
|
|
|
function getApplication() |
226
|
|
|
{ |
227
|
|
|
return \BFW\Application::getInstance(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Détermine si la session est démarré |
232
|
|
|
* |
233
|
|
|
* @link http://fr2.php.net/manual/fr/function.session-status.php#113468 |
234
|
|
|
* |
235
|
|
|
* @return bool |
236
|
|
|
*/ |
237
|
|
|
function sessionIsStarted() |
238
|
|
|
{ |
239
|
|
|
if (PHP_SAPI === 'cli') { |
240
|
|
|
return false; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
if (session_status() === PHP_SESSION_ACTIVE) { |
244
|
|
|
return true; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
return false; |
248
|
|
|
} |
249
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.