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.
89 righe
3.4 KiB
89 righe
3.4 KiB
<?php |
|
|
|
class ElenchiAPI { |
|
|
|
public static function getEndpoints() { |
|
$global = static::getGlobalEndpoints(); |
|
$local = static::getLocalEndpoints(); |
|
|
|
return [ |
|
"global" => $global, |
|
"local" => $local, |
|
"count" => count($global) + count($local) |
|
]; |
|
} |
|
public static function getGlobalEndpoints() : array { |
|
global $config; |
|
$entries = $config["albiAPI"]["endpoints"] ?? []; |
|
$enteToken = $_SESSION["ente"]->getInfo()["token"] ?? ""; |
|
|
|
if(!empty($entries)) { |
|
$entries = array_filter($entries, function($entry) use($enteToken) { |
|
return DEVELOP_ENV ? true : $entry["token"] != $enteToken; |
|
}); |
|
return $entries; |
|
} |
|
return []; |
|
} |
|
public static function createLocalEndpoint(array $params, ?string &$message = null) { |
|
global $pdo; |
|
$message = "Impossibile creare l'endpoint"; |
|
if(!isset($params["endpoint"])) return false; |
|
if(!isset($params["token"])) return false; |
|
if(!isset($params["displayName"])) return false; |
|
|
|
$endpoint = $params["endpoint"]; |
|
$token = $params["token"]; |
|
$displayName = $params["displayName"]; |
|
|
|
// Chiamata di prova |
|
$raw_response = tuttogare_api_call("{$endpoint}/getAlbi", $token); |
|
if(!valid_json($raw_response, $raw_response, TRUE)) { |
|
$message = "L'endpoint impostato non risponde correttamente. Verifica l'URL ed il token"; |
|
return false; |
|
} |
|
|
|
$message = "Impossibile aggiungere l'endpoint al database"; |
|
|
|
$result = $pdo->go( |
|
"INSERT INTO b_access_api_endpoints (endpoint, token, displayName, albi) VALUES (:endpoint, :token, :displayName, :albi)", |
|
[ |
|
":endpoint" => $endpoint, |
|
":token" => $token, |
|
":displayName" => $displayName, |
|
":albi" => "S", |
|
] |
|
); |
|
return $result->rowCount() > 0; |
|
} |
|
public static function deleteLocalEndpoint(array $params, ?string &$message = null) { |
|
global $pdo; |
|
$message = "Impossibile cancellare l'endpoint"; |
|
if(!isset($params["key"])) return false; |
|
$result = $pdo->go("DELETE FROM b_access_api_endpoints WHERE codice = :codice", [":codice" => $params["key"]]); |
|
return $result->rowCount() > 0; |
|
} |
|
public static function getLocalEndpoints() : array { |
|
global $pdo; |
|
$entries = $pdo->go("SELECT `codice`, `token`, `endpoint`, `displayName` FROM b_access_api_endpoints WHERE albi = 'S'")->fetchAll(PDO::FETCH_ASSOC); |
|
|
|
$enteToken = $_SESSION["ente"]->getInfo()["token"] ?? ""; |
|
if(!empty($entries)) { |
|
$entries = array_filter($entries, function($entry) use($enteToken) { |
|
return DEVELOP_ENV ? true : $entry["token"] != $enteToken; |
|
}); |
|
return array_column($entries, null, "codice"); |
|
} |
|
|
|
return []; |
|
} |
|
public static function getLocalEndpoint(int $codice) : ?array { |
|
global $pdo; |
|
$entry = $pdo->go("SELECT `codice`, `token`, `endpoint`, `displayName` FROM b_access_api_endpoints WHERE albi = 'S' AND codice = :codice", [":codice" => $codice])->fetch(PDO::FETCH_ASSOC); |
|
return $entry; |
|
} |
|
public static function getGlobalEndpoint(string $key) : ?array { |
|
global $config; |
|
return $config["albiAPI"]["endpoints"][$key] ?? null; |
|
} |
|
} |