Non puoi selezionare più di 25 argomenti
Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
76 righe
2.2 KiB
76 righe
2.2 KiB
<?php |
|
$configPath = substr(__DIR__, 0, strpos(__DIR__, "/public_html")) . "/config.php"; |
|
include_once($configPath); |
|
|
|
// Verifica che IDPS sia valido |
|
function __isValidIDPS($data) |
|
{ |
|
return is_array($data) && isset($data[0]["entity_id"]); |
|
} |
|
// Muore, ma in json |
|
function __json_die($data) |
|
{ |
|
die(json_encode($data, JSON_PRETTY_PRINT)); |
|
} |
|
|
|
// /media/idps.json |
|
$cachePath = $config["mediaFolder"] . DIRECTORY_SEPARATOR . "idps.json"; |
|
// Durata in secondi della cache |
|
$cacheTTL = 12*60*60; |
|
// Se fallisce la richiesta usa la cache anche se scaduta |
|
$useStaleCacheOnFail = true; |
|
|
|
// IDPS ottenuti da cache o da url |
|
$IDPS = null; |
|
// Sono validi gli IDPS? |
|
$valid = false; |
|
|
|
// Se abbiamo una cache |
|
if (file_exists($cachePath)) { |
|
// La carichiamo |
|
$contents = file_get_contents($cachePath); |
|
// Verifichiamo sia valida |
|
if (valid_json($contents, $parsedContents, true) && isset($parsedContents["data"])) { |
|
// Verifichiamo non sia scaduta |
|
$valid = !empty($parsedContents["updated"]) && $parsedContents["updated"] + $cacheTTL > time(); |
|
$IDPS = $parsedContents["data"]; |
|
} |
|
} |
|
if(!$valid) { |
|
$fetchIDPS = null; |
|
try { |
|
// Scarichiamo IDPS dal server |
|
$fetchIDPS = @file_get_contents("https://registry.spid.gov.it/entities-idp?&output=json&custom=info_display_base"); |
|
} catch (\Throwable $t) { |
|
$fetchIDPS = null; |
|
// Se fallisce la richiesta e abbiamo una cache anche vecchia e siamo autorizzati, la usiamo lo stesso |
|
if($useStaleCacheOnFail && $IDPS !== null) { |
|
$valid = true; |
|
} |
|
} |
|
|
|
// Se abbiamo ottenuto una risposta |
|
if($fetchIDPS !== null) { |
|
// Verifichiamo sia valida |
|
if (valid_json($fetchIDPS, $parsedIDPS, true) && __isValidIDPS($parsedIDPS)) { |
|
$IDPS = $parsedIDPS; |
|
// Salviamo in cache |
|
file_put_contents($cachePath, json_encode([ |
|
"updated" => time(), |
|
"data" => $parsedIDPS, |
|
])); |
|
// Flagghiamo come valido |
|
$valid = true; |
|
} |
|
} |
|
|
|
} |
|
|
|
|
|
header('Content-Type: application/json'); |
|
|
|
if ($valid && $IDPS !== null) { |
|
__json_die($IDPS); |
|
} |
|
http_response_code(500); |
|
__json_die(["error" => "Impossibile ottenere i provider di servizio"]);
|
|
|