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.
221 righe
8.0 KiB
221 righe
8.0 KiB
<? |
|
|
|
class apiManager { |
|
public $query; |
|
public $get; |
|
public $post; |
|
public $body; |
|
public $method; |
|
public $spec; |
|
public $errorCode; |
|
public $errorDescription; |
|
public $errorHeader; |
|
public $endPoint; |
|
public $codiceLog; |
|
public $debug; |
|
public $init; |
|
public $root; |
|
private $ente; |
|
private $db; |
|
private $config; |
|
|
|
public static function getAPISpecs() { |
|
$api = json_decode(file_get_contents(__DIR__."/api.json"),true); |
|
return $api; |
|
} |
|
|
|
public function __construct($token) |
|
{ |
|
global $pdo; |
|
global $config; |
|
global $root; |
|
$this->spec = self::getAPISpecs(); |
|
$this->errorCode = "500"; |
|
$this->errorDescription = "Errore generico"; |
|
$this->errorHeader = "500"; |
|
$this->endPoint = "/api/rest/service.php"; |
|
$this->debug = false; |
|
$this->root = $root; |
|
$this->init = false; |
|
if (!empty($pdo)) { |
|
$ente = $pdo->go("SELECT * FROM b_enti WHERE token = :token",[":token"=>$token])->fetch(PDO::FETCH_ASSOC); |
|
if (!empty($ente)) { |
|
$obj = new Ente(); |
|
$obj->init($ente["dominio"]); |
|
if (!empty($obj->codice)) { |
|
$this->init = true; |
|
$this->db = $pdo; |
|
$this->config = $config; |
|
$this->ente = $obj; |
|
} |
|
} else { |
|
return false; |
|
} |
|
} else { |
|
return false; |
|
} |
|
} |
|
|
|
public static function validateRequest($parameters,$values, $required = null) { |
|
$return = false; |
|
if (!empty($parameters)) { |
|
$errors = []; |
|
$found = 0; |
|
$countRequired = 0; |
|
foreach($parameters AS $pKey => $parametro) { |
|
if (!empty($parametro["name"]) && is_numeric($pKey)) { $pKey = $parametro["name"]; } |
|
if (isset($values[$pKey])) { $found++; } |
|
$valore = (isset($values[$pKey])) ? $values[$pKey] : ""; |
|
if (!empty($required)) { |
|
if (is_array($required)) { |
|
if (in_array($pKey,$required)) { $parametro["required"] = true; } |
|
} else if ($required === true) { |
|
$parametro["required"] = true; |
|
} |
|
} |
|
if (!empty($parametro["required"])) { $countRequired++; } |
|
if (!empty($parametro["required"]) && empty($valore) && $valore != "0" && $parametro["type"] != "array" && $parametro["type"] != "object") { |
|
$errors[$pKey] = "Obbligatorio"; |
|
} else { |
|
if (isset($parametro["type"]) && ($parametro["type"] == "array" || $parametro["type"] == "object")) { |
|
if ($parametro["type"] == "array" && !isset($parametro["properties"])) { |
|
if (isset($parametro["items"][0]["properties"])) { |
|
$innerPar = $parametro["items"][0]["properties"]; |
|
} |
|
} else if (isset($parametro["properties"])) { |
|
$innerPar = $parametro["properties"]; |
|
} |
|
if (isset($innerPar)) { |
|
$keys = array_keys($innerPar); |
|
$required = (!empty($parametro["required"])) ? $parametro["required"] : null; |
|
if (!empty($values[$pKey])) { |
|
if (is_array($values[$pKey]) && isset($values[$pKey][$keys[0]])) { $values[$pKey] = [$values[$pKey]]; } |
|
foreach($values[$pKey] AS $subValues) { |
|
$result = SELF::validateRequest($innerPar,$subValues,$required); |
|
if ($result !== true) { |
|
$errors = array_merge($errors,$result); |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
if (isset($parametro["type"])) { |
|
if ($parametro["type"]=="number" && !is_numeric($valore) && !empty($valore)) { |
|
$errors[$pKey] = "Formato errato"; |
|
} |
|
} |
|
// TODO: CONTROLLARE $ref |
|
if ((!empty($parametro["schema"]["pattern"]) || !empty($parametro["pattern"])) && !empty($valore)) { |
|
$pattern = (isset($parametro["schema"]["pattern"])) ? $parametro["schema"]["pattern"] : $parametro["pattern"]; |
|
if (!preg_match("/".$pattern."/",$valore)) { |
|
$errors[$pKey] = "Formato errato"; |
|
} |
|
} |
|
if (!empty($parametro["schema"]["enum"]) || !empty($parametro["enum"]) && !empty($valore)) { |
|
$enum = (isset($parametro["schema"]["enum"])) ? $parametro["schema"]["enum"] : $parametro["enum"]; |
|
$chiavi = array_keys($enum); |
|
foreach($chiavi AS $chiave)if (!is_numeric($chiave)) { $enum = $chiavi; break; } |
|
if (in_array($valore,$enum) === false) { |
|
var_dump($enum); |
|
$errors[$pKey] = "Valore non previsto"; |
|
} |
|
} |
|
} |
|
} |
|
if ($found == 0 && count($errors) == 0 && $countRequired > 0) { |
|
$errors = "Richiesta errata"; |
|
} |
|
if (count($errors) == 0) { |
|
$return = true; |
|
} else { |
|
$return = $errors; |
|
} |
|
} |
|
return $return; |
|
} |
|
|
|
public function handleRequest() { |
|
$return = false; |
|
|
|
$log = []; |
|
$log["endPoint"] = $this->endPoint; |
|
$log["method"] = $this->method; |
|
$log["request"] = json_encode($this->query,true); |
|
$log["timestamp_creazione"] = date('Y-m-d H:i:s'); |
|
$log["codice_ente"] = $this->ente->codice; |
|
$salva = new salva(); |
|
|
|
$salva->nome_tabella = "b_log_api"; |
|
$salva->operazione = 'INSERT'; |
|
$salva->oggetto = $log; |
|
$log["codice"] = $salva->save(); |
|
$this->codiceLog = $log["codice"]; |
|
$metodi = get_class_methods("apiManager"); |
|
if (in_array($this->method,$metodi)!==false) { |
|
$return = $this->{$this->method}(); |
|
} else { |
|
$this->errorCode = "404"; |
|
$this->errorDescription = "Metodo sconosciuto"; |
|
$this->errorHeader = "404"; |
|
} |
|
$log["header"] = 200; |
|
if ($return === false) { |
|
$error = ["errorHeader" => $this->errorHeader,"errorCode"=>$this->errorCode,"errorDescription"=>$this->errorDescription]; |
|
$log["header"] = $this->errorHeader; |
|
$log["errors"] = json_encode($error,true); |
|
} |
|
$salva->operazione = 'UPDATE'; |
|
$salva->oggetto = $log; |
|
$salva->save(); |
|
return $return; |
|
} |
|
|
|
public function getAlbi() { |
|
return publicAlboFornitori::API_getAlbi($this->get["codice"] ?? 0); |
|
} |
|
public function getFiltriAlbo() { |
|
return publicAlboFornitori::API_getFiltriAlbo($this->get["codice"]); |
|
} |
|
public function getPartecipantiAlbo() { |
|
return publicAlboFornitori::API_getPartecipantiAlbo($this->get["codice"], $this->body["filters"] ?? [], $this->body["order"] ?? [], $this->body["limit"] ?? []); |
|
} |
|
public function listPCP() { |
|
$risultati = publicPCP::getList($this->ente->codice,[],["b_pcp.id"=>"DESC"],(int)$this->query["start"],(int)$this->query["start"]); |
|
return $risultati; |
|
} |
|
|
|
public function getPCP() { |
|
if (!empty($this->query["codice"])) { |
|
$_SESSION["ente"] = $this->ente; |
|
$pcp = publicPCP::init($this->query["codice"]); |
|
if (!empty($pcp->info["codice"]) && $pcp->info["codice"] == $this->query["codice"]) { |
|
$return = $pcp->info; |
|
$fasi = $pcp->getRounds(); |
|
$return["fasi"] = []; |
|
foreach($fasi AS $fase) { |
|
$fase["extraInfo"] = json_decode($fase["extraInfo"],true); |
|
unset($fase["codice_pcp"]); |
|
unset($fase["aperturaSequenziale"]); |
|
unset($fase["utente_creazione"]); |
|
unset($fase["utente_modifica"]); |
|
$return["fasi"][] = $fase; |
|
} |
|
unset($return["public_key"]); |
|
unset($return["private_verified"]); |
|
unset($return["riferimento_consultazione"]); |
|
unset($return["utente_creazione"]); |
|
unset($return["utente_modifica"]); |
|
$return["extraInfo"] = json_decode($return["extraInfo"],true); |
|
return $return; |
|
} |
|
} else { |
|
return ["codice"=>"Campo obbligatorio"]; |
|
} |
|
} |
|
|
|
public function getStatiPCP() { |
|
return publicPCP::getStati(); |
|
} |
|
} |
|
?>
|