") !== false) { $search_from = ""; $search_to = ""; $allegati = []; if (strpos($request,$search_from) !== false) { while(strpos($request,$search_from) !== false) { $start = strpos($request,$search_from); $end = strpos($request,$search_to) + strlen($search_to); $allegato = substr($request,$start,$end - $start); $request = str_replace($allegato,"",$request); $allegati[] = $allegato; } } } $request = preg_replace('/(\S+)=/',"ns1:$1=",$request); $request = str_replace('ns1:xmlns','xmlns',$request); $request = str_replace('','',$request); if (!empty($allegati)) { $tag = ""; $offset = 0; foreach($allegati AS $allegato) { $pos = $offset = strpos($request,$tag,$offset) + strlen($tag); $request = substr_replace($request,$allegato,$pos,0); } } return $request; } function __getLastRequest() { $request = parent::__getLastRequest(); $request = $this->fixRequest($request); return $request; } function __doRequest($request, $location, $action, $version,$one_way = NULL) { $request = $this->fixRequest($request); // parent call return parent::__doRequest($request, $location, $action, $version, $one_way); } function logCall($function_name,$arguments) { $log = []; $log["codice_simog"] = $this->codice_simog; $log["codice_lotto"] = $this->codice_lotto; $log["metodo"] = $function_name; if ($function_name != "login") { $log["argomenti"] = json_encode($arguments); $log["richiesta"] = $this->__getLastRequest(); } $log["risposta"] = $this->__getLastResponse(); $salva = new salva(); $salva->debug = false; $salva->nome_tabella = "b_simog_log"; $salva->operazione = "INSERT"; $salva->oggetto = $log; $salva->save(); } function replaceTicket($array,$ticket) { $return = []; foreach($array AS $key => $value) { if ($key === "ticket") { $return[$key] = $ticket; } else if (is_array($value)) { $return[$key] = $this->replaceTicket($value,$ticket); } else { $return[$key] = $value; } } return $return; } function loginAndRepeat($function_name,$arguments) { $login = new simog(); $retry = $login->login($_SESSION["simog"]["username"],$_SESSION["simog"]["password"]); if ($retry === true) { $_SESSION["simog"]["ticket"] = $login->ticket; $arguments = simog::replaceUserInfo($arguments,"ticket",$login->ticket); return parent::__call($function_name,$arguments); } } function __call($function_name, $arguments) { try { $return = parent::__call($function_name,$arguments); } catch (\Throwable $th) { $message = $th->getMessage(); if ($message == "Login non effettuato") { if (!empty($_SESSION["simog"])) { $return = $this->loginAndRepeat($function_name,$arguments); } } else { $return = json_decode(json_encode(["return"=>["success"=>false,"error"=>$message]]),false); } } if (!empty($return->return->error)) { if ($return->return->error == "SIMOGWS_WSSMANAGER_NULL_15 - NON E' STATA TROVATA ALCUNA SESSIONE VALIDA") { $return = $this->loginAndRepeat($function_name,$arguments); } } else if (!empty($return->return->feedBack->AnomalieSchede->Anomalia->DESCRIZIONE)) { if (strpos($return->return->feedBack->AnomalieSchede->Anomalia->DESCRIZIONE,"SIMOGWS_WSSMANAGER_NULL_15 - NON E' STATA TROVATA ALCUNA SESSIONE VALIDA") !== false) { $return = $this->loginAndRepeat($function_name,$arguments); } } $this->logCall($function_name,$arguments); return $return; } } class simog { private $collaudo; private $db; private $cert; public $client; public $version; public $debug; public $codice_simog; public $codice_lotto; public $errors; public $collaborazioni; public $index; public $ticket; static function getEndpoint($type,$collaudo=true) { if ($collaudo) { switch($type) { case "loader": return "https://wstest.anticorruzione.it/COLL/SimogWSPDD/services/LoaderAppaltoWS"; break; case "smartcig": return "https://wstest.anticorruzione.it/COLL/TracciabilitaWS/ServizioGestioneComunicazioni"; break; case "tipologiche": return "https://wstest.anticorruzione.it/COLL/TracciabilitaWS/ServizioGestioneTipologiche"; break; case "simog": return "https://wstest.anticorruzione.it/COLL/SimogWSPDD/services/SimogWSPDD"; break; default: return ["esito"=>false,"errore"=>"Endpoint inesistente"]; } } else { switch($type) { case "loader": return "https://ws.anticorruzione.it/SimogWSPDD/services/LoaderAppaltoWS"; break; case "smartcig": return "https://ws.anticorruzione.it/TracciabilitaWS/ServizioGestioneComunicazioni"; break; case "tipologiche": return "https://ws.anticorruzione.it/TracciabilitaWS/ServizioGestioneTipologiche"; break; case "simog": return "https://ws.anticorruzione.it/SimogWSPDD/services/SimogWSPDD"; break; default: return ["esito"=>false,"errore"=>"Endpoint inesistente"]; } } } public function getMethodInfo($method) { $path = self::getPath($this->version)."/methods.json"; if (file_exists($path)) { $methods = jsonToArray($path); if (!empty($methods)) { if (!empty($methods[$method])) { return $methods[$method]; } } } return false; } static function getPathWSDL($type,$version) { $path = self::getPath($version); if (!empty($path)) { $path .= "/wsdl"; switch($type) { case "loader": $path .= "/LoaderAppaltoWS.wsdl"; break; case "smartcig": $path .= "/ServizioGestioneComunicazioni.wsdl"; break; case "tipologiche": $path .= "/ServizioGestioneTipologiche.wsdl"; break; case "simog": $path .= "/SimogWSPDD.wsdl"; break; default: $path .= "error"; } if (file_exists($path)) { return $path; } else { return ["esito"=>false,"errore"=>"WSDL non trovato"]; } } else { return ["esito"=>false,"errore"=>"Path versione inesistente"]; } } static function getPath($version) { $path = __DIR__."/versions/{$version}"; if (is_dir($path)) { return $path; } return false; } public function __construct() { global $config; global $pdo; $this->version = anac::getVersion(); $this->cert = $config["simog_folder"] . "/simog.client.includingkey.pem"; $this->errors = []; if ($config["demoEnv"]) { $this->collaudo = true; } } public function setClient($type,$version) { $wsdl = self::getPathWSDL($type,$version); if (!is_array($wsdl)) { $endpoint = self::getEndpoint($type,$this->collaudo); if (!is_array($endpoint)) { $this->client = new SimogClient($wsdl,array('location' => $endpoint, 'local_cert' => $this->cert, 'trace' => 1)); $this->client->codice_simog = $this->codice_simog; $this->client->codice_lotto = $this->codice_lotto; } else { $this->errors[] = $endpoint["errore"]; } } else { $this->errors[] = $wsdl["errore"]; } } public function login($username,$password) { $this->setClient("simog",$this->version); if (!empty($this->client)) { $return = $this->client->login(["login"=>$username,"password"=>$password]); if (isset($return->return)) { if (!$return->return->success) { return $return->return->error; } else { $this->collaborazioni = $return->return->coll; $this->ticket = $return->return->ticket; return true; } } else { return "Errore nella risposta"; } } else { $this->errors[] = "Client non inizializzato"; } } public function logout($ticket) { $this->setClient("simog",$this->version); if (!empty($this->client)) { $return = $this->client->chiudiSessione(["ticket"=>$ticket]); if (isset($return->return)) { if (!$return->return->success) { return $return->return->error; } else { return true; } } else { return "Errore nella risposta"; } } else { $this->errors[] = "Client non inizializzato"; } } public static function replaceUserInfo($array,$searchKey,$newValue) { $return = []; foreach($array AS $key => $value) { if ($key === $searchKey) { $return[$key] = $newValue; } else if (is_array($value)) { $return[$key] = self::replaceUserInfo($value,$searchKey,$newValue); } else { $return[$key] = $value; } } return $return; } public function sendData($method, $data) { if (!empty($this->ticket)) { if (!empty($this->index) || $this->index === "0") { $methodInfo = $this->getMethodInfo($method); if (!empty($methodInfo)) { $this->setClient($methodInfo["type"],$this->version); if (!empty($this->client)) { $data = $data + $methodInfo["sessionInfo"]; $data = self::replaceUserInfo($data,"ticket",$this->ticket); $data = self::replaceUserInfo($data,"index",$this->index); $data = self::replaceUserInfo($data,"indexCollaborazione",$this->index); $return = $this->client->$method($data); if (!empty($return)) { return $return; } else { return "Errore nella risposta"; } } } else { $this->errors[] = "Metodo sconosciuto"; } } else { $this->errors[] = "Collaborazione mancante"; } } else { $this->errors[] = "Login non effettutato"; } } } ?>