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.
232 righe
8.2 KiB
232 righe
8.2 KiB
<? |
|
|
|
use PhpParser\Node\Stmt\Break_; |
|
|
|
Class cpn { |
|
|
|
private $endpoints; |
|
private $ente; |
|
private $clientKey; |
|
private $clientId; |
|
private $riferimento; |
|
private $codiceRiferimento; |
|
public $debug; |
|
public $token; |
|
|
|
public static function getEndpoints() { |
|
$endpoints = []; |
|
if ($_SESSION["ente"]->getInfo()["ambienteTest"] == "S" || $_SESSION["developEnv"] || $_SESSION["demoEnv"]) { |
|
$endpoints["login"] = "https://www.serviziocontrattipubblici.it/WSLoginCollaudo/rest/"; |
|
$endpoints["atti"] = "https://www.serviziocontrattipubblici.it/WSPubblicazioniCollaudo/rest/"; |
|
$endpoints["programmi"] = "https://www.serviziocontrattipubblici.it/WSProgrammiCollaudo/rest/"; |
|
$endpoints["tabelle"] = "https://www.serviziocontrattipubblici.it/WSTabelleDiContestoCollaudo/rest/"; |
|
} else { |
|
$endpoints["login"] = "https://www.serviziocontrattipubblici.it/WSLogin/rest/"; |
|
$endpoints["atti"] = "https://www.serviziocontrattipubblici.it/WSPubblicazioni/rest/"; |
|
$endpoints["programmi"] = "https://www.serviziocontrattipubblici.it/WSProgrammi/rest/"; |
|
$endpoints["tabelle"] = "https://www.serviziocontrattipubblici.it/WSTabelleDiContesto/rest/"; |
|
} |
|
return $endpoints; |
|
} |
|
|
|
public static function printLoginStatus() { |
|
include __DIR__ . "/views/loginInfo.php"; |
|
} |
|
|
|
public function __construct($codice_ente,$riferimento = "",$codiceRiferimento = 0) |
|
{ |
|
$this->debug = false; |
|
$this->endpoints = self::getEndpoints(); |
|
$this->ente = Ente::getInfoFromID($codice_ente)[0] ?? null; |
|
if (!empty($riferimento)) { |
|
$this->riferimento = $riferimento; |
|
} |
|
if (!empty($codiceRiferimento)) { |
|
$this->codiceRiferimento = $codiceRiferimento; |
|
} |
|
if (!empty($this->ente)) { |
|
$this->clientKey = $this->ente["cpnClientKey"]; |
|
$this->clientId = $this->ente["cpnClientId"]; |
|
if (!empty($_SESSION["cpn"]["token"])) { |
|
$this->token = $_SESSION["cpn"]["token"]; |
|
} |
|
} else { |
|
return false; |
|
} |
|
} |
|
|
|
public function login($username,$password) { |
|
$ep = $this->endpoints["login"] . "Account/LoginPubblica"; |
|
$data = [ |
|
"username" => $username, |
|
"password" => $password, |
|
"clientKey" => $this->clientKey, |
|
"clientId" => $this->clientId, |
|
]; |
|
$curl = curl_init(); |
|
curl_setopt_array($curl, array( |
|
CURLOPT_URL => $ep, |
|
CURLOPT_HTTPHEADER => [ |
|
"Content-Type: application/x-www-form-urlencoded" |
|
], |
|
CURLOPT_RETURNTRANSFER => true, |
|
CURLOPT_MAXREDIRS => 10, |
|
CURLOPT_SSL_VERIFYHOST => 0, |
|
CURLOPT_SSL_VERIFYPEER => 0, |
|
CURLOPT_FOLLOWLOCATION => true, |
|
CURLOPT_CUSTOMREQUEST => "POST", |
|
CURLOPT_POSTFIELDS => http_build_query($data), |
|
)); |
|
$return = curl_exec($curl); |
|
curl_close($curl); |
|
if (!empty($return)) { |
|
$return = json_decode($return,true); |
|
if (!empty($return["esito"]) && !empty($return["token"])) { |
|
$this->token = $return["token"]; |
|
return true; |
|
} |
|
} |
|
return false; |
|
} |
|
|
|
public function inviaGara($dati,$modalita = 2) { |
|
$ep = $this->endpoints["atti"] . "Anagrafiche/GaraLotti"; |
|
$data = json_encode(removeEmpty($dati)); |
|
return $this->sendCURL($ep,$modalita,$data); |
|
} |
|
|
|
public function inviaAtto($dati,$modalita = 2) { |
|
$ep = $this->endpoints["atti"] . "Atti/Pubblica"; |
|
$data = json_encode(removeEmpty($dati)); |
|
return $this->sendCURL($ep,$modalita,$data); |
|
} |
|
|
|
public function inviaProgramma($tipologia,$modalita,$dati) { |
|
$ep = $this->endpoints["programmi"]; |
|
switch($tipologia) { |
|
case "LV": |
|
$ep .= "Programmi/PubblicaLavori"; |
|
break; |
|
case "SF": |
|
$ep .= "Programmi/PubblicaFornitureServizi"; |
|
break; |
|
default: |
|
return false; |
|
} |
|
$data = json_encode(removeEmpty($dati)); |
|
return $this->sendCURL($ep,$modalita,$data); |
|
} |
|
|
|
public function downloadProgramma($id) { |
|
$ep = $this->endpoints["programmi"] . "Programmi/ScaricaPdf?idRicevuto={$id}&token={$this->token}"; |
|
$curl = curl_init(); |
|
curl_setopt_array($curl, array( |
|
CURLOPT_URL => $ep, |
|
CURLOPT_HTTPHEADER => [ |
|
"Content-Type: application/x-www-form-urlencoded", |
|
"Accept: application/octet-stream" |
|
], |
|
CURLOPT_RETURNTRANSFER => true, |
|
CURLOPT_MAXREDIRS => 10, |
|
CURLOPT_SSL_VERIFYHOST => 0, |
|
CURLOPT_SSL_VERIFYPEER => 0, |
|
CURLOPT_FOLLOWLOCATION => true, |
|
// CURLOPT_CUSTOMREQUEST => "GET", |
|
// CURLOPT_POSTFIELDS => http_build_query(["idRicevuto"=>$id]) |
|
)); |
|
$return = curl_exec($curl); |
|
$info = curl_getinfo($curl); |
|
curl_close($curl); |
|
if (!empty($return)) { |
|
if ($info["http_code"] == 401 && $this->login($_SESSION["cpn"]["username"],$_SESSION["cpn"]["password"])) { |
|
$_SESSION["cpn"]["token"] = $this->token; |
|
return $this->downloadProgramma($id); |
|
} |
|
return $return; |
|
} |
|
return false; |
|
} |
|
|
|
private function sendCURL($endpoint,$modalita,$data,$method = "POST") { |
|
$curl = curl_init(); |
|
if ($this->debug) { |
|
ob_start(); |
|
?> |
|
<strong>DEBUG CPN: Impostazioni</strong><br> |
|
<div readonly class="bg-black border-white border text-white my-2"> |
|
<?= date("Y-m-d H:i:s") ?><br> |
|
Endpoint: <?= $endpoint ?><br> |
|
Send mode: <?= ($modalita == 2) ? "Effettivo" : "Test" ?><br> |
|
Method: <?= $method ?><br> |
|
Token: <?= $this->token ?><br> |
|
URL: <a href="<?= $endpoint ?>&token=<?= $this->token ?>" target="_blank">Prova</a> |
|
</div> |
|
<strong>DEBUG CPN: Dati invio</strong><br> |
|
<textarea readonly class="bg-black border-white border text-white my-2 form-control" rows="4"> |
|
<?= (!is_array($data)) ? $data : json_encode($data,JSON_PRETTY_PRINT) ?><br> |
|
</textarea> |
|
<? |
|
echo ob_get_clean(); |
|
} |
|
$log = []; |
|
$log["riferimento"] = $this->riferimento; |
|
$log["codice_riferimento"] = $this->codiceRiferimento; |
|
$log["endpoint"] = "{$endpoint}?modalitaInvio={$modalita}"; |
|
$log["richiesta"] = (is_array($data)) ? json_encode($data) : $data; |
|
if (empty($data)) { |
|
$data = null; |
|
} |
|
$salva = new salva(); |
|
$salva->debug = false; |
|
|
|
$salva->nome_tabella = "b_cpn_log"; |
|
$salva->operazione = "INSERT"; |
|
$salva->oggetto = $log; |
|
$log = ["codice"=>$salva->save()]; |
|
curl_setopt_array($curl, array( |
|
CURLOPT_URL => "{$endpoint}?modalitaInvio={$modalita}&token={$this->token}", |
|
CURLOPT_HTTPHEADER => [ |
|
"Content-Type: application/json" |
|
], |
|
CURLOPT_RETURNTRANSFER => true, |
|
CURLOPT_MAXREDIRS => 10, |
|
CURLOPT_SSL_VERIFYHOST => 0, |
|
CURLOPT_SSL_VERIFYPEER => 0, |
|
CURLOPT_FOLLOWLOCATION => true, |
|
CURLOPT_CUSTOMREQUEST => $method, |
|
CURLOPT_POSTFIELDS => $data, |
|
)); |
|
$log["risposta"] = $risposta = curl_exec($curl); |
|
$salva->oggetto = $log; |
|
$salva->operazione = "UPDATE"; |
|
$salva->save(); |
|
if ($this->debug) { |
|
ob_start(); |
|
?> |
|
<strong>DEBUG CPN: Risposta</strong><br> |
|
<textarea readonly class="bg-black border-white border text-white my-2 form-control" rows="4"> |
|
<?= date("Y-m-d H:i:s") ?> |
|
<?= $risposta ?> |
|
</textarea> |
|
<strong>DEBUG CPN: Errori CURL</strong><br> |
|
<textarea readonly class="bg-black border-white border text-white my-2 form-control" rows="4"> |
|
<?= var_dump(curl_error($curl)) ?> |
|
</textarea> |
|
<? |
|
echo ob_get_clean(); |
|
} |
|
curl_close($curl); |
|
if (!empty($risposta)) { |
|
$risposta = json_decode($risposta,true); |
|
if (!empty($risposta["error"]) && $risposta["error"] == "Il token non è valido." && !empty($_SESSION["cpn"]["username"]) && !empty($_SESSION["cpn"]["password"])) { |
|
if ($this->login($_SESSION["cpn"]["username"],$_SESSION["cpn"]["password"])) { |
|
$_SESSION["cpn"]["token"] = $this->token; |
|
return $this->sendCURL($endpoint,$modalita,$data,$method); |
|
} |
|
} |
|
} |
|
return $risposta; |
|
} |
|
|
|
} |
|
?>
|