| 1 | <?php |
| 2 | |
| 3 | const BAD_REQUEST = 400; |
| 4 |
| 1 | <?php |
| 2 | |
| 3 | function creaToken(string $pagina, int $duracionEnMinutos) |
| 4 | { |
| 5 | $criptografiaFuerte = true; |
| 6 | |
| 7 | // Crea el token |
| 8 | $token = [ |
| 9 | "expiracion" => time() + 60 * $duracionEnMinutos, |
| 10 | // El token es de 80 caracteres, criptográficamente fuerte. |
| 11 | "texto" => bin2hex(openssl_random_pseudo_bytes(80, $criptografiaFuerte)) |
| 12 | ]; |
| 13 | |
| 14 | // Verifica que ya haya tokens $pagina. |
| 15 | if (isset($_SESSION[$pagina])) { |
| 16 | |
| 17 | $tokensParaPagina = $_SESSION[$pagina]; |
| 18 | |
| 19 | // Como ya existe el arreglo, elimina los tokens expirados para esta pagina. |
| 20 | foreach ($tokensParaPagina as $llave => $tokenParaPagina) { |
| 21 | if ($tokenParaPagina["expiracion"] > time()) { |
| 22 | unset($tokensParaPagina[$llave]); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | // Se puede usar uno o varios tokens por pagina. |
| 27 | $tokensParaPagina[] = $token; |
| 28 | $_SESSION[$pagina] = $tokensParaPagina; |
| 29 | } else { |
| 30 | |
| 31 | // Se puede usar uno o varios tokens por pagina |
| 32 | $_SESSION[$pagina] = [$token]; |
| 33 | } |
| 34 | |
| 35 | return $token["texto"]; |
| 36 | } |
| 37 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/INTERNAL_SERVER_ERROR.php"; |
| 4 | require_once __DIR__ . "/devuelveProblemDetails.php"; |
| 5 | require_once __DIR__ . "/devuelveProblemDetails.php"; |
| 6 | |
| 7 | function devuelveErrorInterno(Throwable $error) |
| 8 | { |
| 9 | devuelveProblemDetails(new ProblemDetails( |
| 10 | status: INTERNAL_SERVER_ERROR, |
| 11 | title: $error->getMessage(), |
| 12 | type: "/error/errorinterno.html" |
| 13 | )); |
| 14 | } |
| 15 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/devuelveResultadoNoJson.php"; |
| 4 | |
| 5 | function devuelveJson($resultado) |
| 6 | { |
| 7 | |
| 8 | $json = json_encode($resultado); |
| 9 | |
| 10 | if ($json === false) { |
| 11 | |
| 12 | devuelveResultadoNoJson(); |
| 13 | } else { |
| 14 | |
| 15 | http_response_code(200); |
| 16 | header("Content-Type: application/json"); |
| 17 | echo $json; |
| 18 | } |
| 19 | } |
| 20 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/devuelveResultadoNoJson.php"; |
| 4 | require_once __DIR__ . "/ProblemDetails.php"; |
| 5 | |
| 6 | function devuelveProblemDetails(ProblemDetails $details) |
| 7 | { |
| 8 | |
| 9 | $body = ["title" => $details->title]; |
| 10 | if ($details->type !== null) { |
| 11 | $body["type"] = $details->type; |
| 12 | } |
| 13 | if ($details->detail !== null) { |
| 14 | $body["detail"] = $details->detail; |
| 15 | } |
| 16 | |
| 17 | $json = json_encode($body); |
| 18 | |
| 19 | if ($json === false) { |
| 20 | |
| 21 | devuelveResultadoNoJson(); |
| 22 | } else { |
| 23 | |
| 24 | http_response_code($details->status); |
| 25 | header("Content-Type: application/problem+json"); |
| 26 | echo $json; |
| 27 | } |
| 28 | } |
| 29 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/INTERNAL_SERVER_ERROR.php"; |
| 4 | |
| 5 | function devuelveResultadoNoJson() |
| 6 | { |
| 7 | |
| 8 | http_response_code(INTERNAL_SERVER_ERROR); |
| 9 | header("Content-Type: application/problem+json"); |
| 10 | echo '{' . |
| 11 | '"title": "El resultado no puede representarse como JSON."' . |
| 12 | '"type": "/error/resultadonojson.html"' . |
| 13 | '}'; |
| 14 | } |
| 15 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/ProblemDetails.php"; |
| 4 | require_once __DIR__ . "/devuelveProblemDetails.php"; |
| 5 | require_once __DIR__ . "/devuelveErrorInterno.php"; |
| 6 | |
| 7 | function ejecutaServicio(callable $codigo) |
| 8 | { |
| 9 | try { |
| 10 | $codigo(); |
| 11 | } catch (ProblemDetails $details) { |
| 12 | devuelveProblemDetails($details); |
| 13 | } catch (Throwable $error) { |
| 14 | devuelveErrorInterno($error); |
| 15 | } |
| 16 | } |
| 17 |
| 1 | <?php |
| 2 | |
| 3 | const INTERNAL_SERVER_ERROR = 500; |
| 1 | <?php |
| 2 | |
| 3 | /** Detalle de los errores devueltos por un servicio. */ |
| 4 | class ProblemDetails extends Exception |
| 5 | { |
| 6 | |
| 7 | public int $status; |
| 8 | public string $title; |
| 9 | public ?string $type; |
| 10 | public ?string $detail; |
| 11 | |
| 12 | public function __construct( |
| 13 | int $status, |
| 14 | string $title, |
| 15 | ?string $type = null, |
| 16 | ?string $detail = null, |
| 17 | Throwable $previous = null |
| 18 | ) { |
| 19 | parent::__construct($title, $status, $previous); |
| 20 | $this->status = $status; |
| 21 | $this->type = $type; |
| 22 | $this->title = $title; |
| 23 | $this->detail = $detail; |
| 24 | } |
| 25 | } |
| 26 |
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Recupera el texto de un parámetro enviado al |
| 5 | * servidor por medio de GET, POST o cookie. |
| 6 | * |
| 7 | * Si el parámetro no se recibe, devuelve false. |
| 8 | */ |
| 9 | function recuperaTexto(string $parametro): false|string |
| 10 | { |
| 11 | /* Si el parámetro está asignado en $_REQUEST, |
| 12 | * devuelve su valor; de lo contrario, devuelve false. |
| 13 | */ |
| 14 | $valor = isset($_REQUEST[$parametro]) |
| 15 | ? $_REQUEST[$parametro] |
| 16 | : false; |
| 17 | return $valor; |
| 18 | } |
| 19 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/ProblemDetails.php"; |
| 4 | |
| 5 | const FORBIDDEN = 403; |
| 6 | |
| 7 | function validaToken(string $pagina, string $token) |
| 8 | { |
| 9 | |
| 10 | if (!isset($_SESSION[$pagina])) |
| 11 | throw new ProblemDetails( |
| 12 | status: FORBIDDEN, |
| 13 | title: "Página no registrada.", |
| 14 | type: "/error/paginanoregistrada.html", |
| 15 | ); |
| 16 | |
| 17 | $tokensParaPagina = $_SESSION[$pagina]; |
| 18 | |
| 19 | if (!is_array($tokensParaPagina)) |
| 20 | throw new ProblemDetails( |
| 21 | status: FORBIDDEN, |
| 22 | title: "No hay arereglo de tokens.", |
| 23 | type: "/error/sintokens.html", |
| 24 | ); |
| 25 | |
| 26 | $hallado = false; |
| 27 | |
| 28 | // Valida que el token se haya registrado. |
| 29 | foreach ($tokensParaPagina as $llave => $tokenParaPagina) { |
| 30 | |
| 31 | if (strcmp($token, $tokenParaPagina["texto"]) === 0) { |
| 32 | |
| 33 | if ($tokenParaPagina["expiracion"] < time()) { |
| 34 | unset($tokensParaPagina[$llave]); |
| 35 | $_SESSION[$pagina] = $tokensParaPagina; |
| 36 | throw new ProblemDetails( |
| 37 | status: FORBIDDEN, |
| 38 | title: "Tiempo de expiración excedido.", |
| 39 | type: "/error/paginaexpirada.html", |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | $hallado = true; |
| 44 | } elseif ($tokenParaPagina["expiracion"] > time()) { |
| 45 | |
| 46 | // Elimina tokens expirados |
| 47 | unset($tokensParaPagina[$llave]); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | $_SESSION[$pagina] = $tokensParaPagina; |
| 52 | |
| 53 | if ($hallado === false) |
| 54 | throw new ProblemDetails( |
| 55 | status: FORBIDDEN, |
| 56 | title: "Página no registrada.", |
| 57 | type: "/error/paginanoregistrada.html", |
| 58 | ); |
| 59 | } |
| 60 |