-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utiles.php
42 lines (36 loc) · 1.14 KB
/
Utiles.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
class Utiles
{
public static function redireccionar($lugar)
{
printf("<script>window.location.href='%s';</script>", BASE_URL . "/?p=$lugar");
exit;
}
public static function salirSiTokenCSRFNoCoincide($token)
{
if (!self::esTokenCSRFValido($token)) {
exit("<h1>El token CSRF no coincide</h1>");
}
}
public static function generar_token_seguro($longitud)
{
if ($longitud < 4) {
$longitud = 4;
}
return bin2hex(openssl_random_pseudo_bytes(($longitud - ($longitud % 2)) / 2));
}
public static function esTokenCSRFValido($token)
{
#Aquí iniciamos la sesión si no está iniciada, esto es para leer el token que guardamos anteriormente
if (session_status() !== PHP_SESSION_ACTIVE) {
session_start();
}
return hash_equals(SesionService::leer("token_csrf"), $token);
}
public static function obtenerTokenCSRF()
{
$token = self::generar_token_seguro(40);
SesionService::escribir("token_csrf", $token, true);
return SesionService::leer("token_csrf");
}
}