root = $root; $this->config = $config; $this->rel_comunicazioni = array(); $this->error = ""; if($pdo !== null) $this->pdo = $pdo; else $this->pdo = new myPDO($config["db_host"],$config["db_user"],$config["db_pass"],$config["db_name"], $config['socket']); foreach ($parameters as $key => $value) { $this->{$key} = $value; } if (!empty($this->ente)) { $this->pdo->setConnessioneEnte($this->ente["codice"]); } } public function send() { if($this->init() === TRUE) { $this->saveComunicazione(); if($this->coda === TRUE) { $this->saveToQueue(); } else { $this->sendSMTP(); } } if (!empty($this->error)) { if(!$this->elaborazione_coda) $this->saveToQueue(); return $this->error; } else { return TRUE; } } protected function init() { //Verifico che il corpo del messaggio non sia vuoto if (empty($this->corpo)) { $this->error .= "Corpo vuoto
"; } //Verifico che l'oggetto del messaggio non sia vuoto if(empty($this->oggetto)) $this->error .= "Oggetto vuoto
"; // Imposto gli indirizzi dei destinatari e verifico che l'elenco non sia vuoto $this->checkAddress(); if (count($this->address) < 1) { $this->error .= "Nessun destinatario indicato
"; } return !empty($this->error) ? FALSE : TRUE; } protected function sendSMTP() { if($this->setSMTP() === TRUE) { $html = $this->getHTML(); $mail = null; $mail = new PHPMailer(); $mail->setLanguage('it'); $mail->IsSMTP(); $mail->Timeout = 30; $mail->Host = $this->configurazione["host"]; $mail->Port = $this->configurazione["smtp_port"]; if ($this->configurazione["smtp_ssl"] == true) { $mail->SMTPSecure = 'ssl'; } $mail->SMTPAuth = true; $mail->Username = (isset($this->configurazione["smtp_username"])) ? $this->configurazione["smtp_username"] : $this->configurazione["mittente_mail"]; $mail->Password = $this->configurazione["smtp_password"]; $mail->CharSet = 'UTF-8'; if (isset($this->ente) && $this->codice_pec >= 0) { $mail->Password = simple_decrypt($this->configurazione["smtp_password"], $this->ente["cf"]); $mail->Password = str_replace("&","&",$mail->Password); } $nome_sito = $this->config["nome_sito"]; if (isset($this->ente)) { $nome_sito = "Portale Gare - " . $this->ente["denominazione"]; } $mail->SetFrom($this->configurazione["mittente_mail"], $nome_sito); $mail->Subject = strip_tags($nome_sito . " - " . $this->oggetto); $mail->MsgHTML($html); if(!empty($this->attachment)) { if(!is_array($this->attachment)) $this->attachment = array($this->attachment); foreach ($this->attachment as $attachment) { if(file_exists($attachment)) { $mail->addAttachment($attachment); } } } $uc = $this->pdo->prepare('UPDATE r_comunicazioni_utenti SET identificativo_messaggio = :identificativo_messaggio WHERE codice = :codice'); foreach($this->address AS $destinatario) { $codice_relazione = $this->codice_relazione; if(! $this->elaborazione_coda && ! empty($this->utenti_pec[$destinatario]) && ! empty($this->rel_comunicazioni[$this->utenti_pec[$destinatario]])) { $codice_relazione = $this->rel_comunicazioni[$this->utenti_pec[$destinatario]]; die($codice_relazione); } $m = microtime(true); $r = uniqid('', true); $identificativo_messaggio = ""; if(! empty($codice_relazione) && $this->codice_pec >= 0) { $identificativo_messaggio = "<{$codice_relazione}-{$r}-{$m}@tuttogare-pa-3.it>"; $uc->bindValue(':identificativo_messaggio', $identificativo_messaggio); $uc->bindValue(':codice', $codice_relazione); $uc->execute(); } $mail->MessageID = $identificativo_messaggio; $mail->AddAddress($destinatario); $mail->preSend(); $mimemessage = $mail->getSentMIMEMessage(); if ($this->enable_smtp) { $mimemessage = gzcompress($mimemessage); } if (!is_dir($this->config["mailFolder"]."/{$this->codice_ente}")) { mkdir($this->config["mailFolder"]."/{$this->codice_ente}",0777,true); } file_put_contents($this->config["mailFolder"]."/{$this->codice_ente}/{$identificativo_messaggio}.eml",$mimemessage); if ($this->enable_smtp) { if (!$mail->Send()) { $this->error .= "Problema durante invio. - Errore classe: ".$mail->ErrorInfo . "
"; } } $mail->clearAllRecipients(); } } } protected function setSMTP() { if ($this->codice_pec >= 0 && isset($this->ente)) { $bind = array(":codice_ente"=>$this->ente["codice"]); $strsql = "SELECT * FROM b_pec WHERE codice_ente = :codice_ente AND predefinita = 'S'"; $risultato = $this->pdo->go($strsql,$bind); if ($risultato->rowCount() > 0) { $configurazione = $risultato->fetch(PDO::FETCH_ASSOC); $this->configurazione["host"] = $configurazione["smtp"]; $this->configurazione["smtp_port"] = $configurazione["smtp_port"]; $this->configurazione["smtp_ssl"] = $configurazione["usa_ssl"]; $this->configurazione["mittente_mail"] = $configurazione["pec"]; $this->configurazione["smtp_password"] = $configurazione["password"]; } if ($this->codice_pec != 0) { $bind = array(":codice_pec"=>$this->codice_pec,":codice_ente"=>$this->ente["codice"]); $strsql = "SELECT * FROM b_pec WHERE codice = :codice_pec AND codice_ente = :codice_ente AND attivo = 'S' AND eliminato = 'N'"; $risultato = $this->pdo->go($strsql,$bind); if ($risultato->rowCount() > 0) { $record_pec = $risultato->fetch(PDO::FETCH_ASSOC); $this->configurazione["host"] = $record_pec["smtp"]; $this->configurazione["smtp_port"] = $record_pec["smtp_port"]; $this->configurazione["smtp_ssl"] = $record_pec["usa_ssl"]; $this->configurazione["mittente_mail"] = $record_pec["pec"]; $this->configurazione["smtp_password"] = $record_pec["password"]; } } } else if ($this->codice_pec < 0) { if ($this->codice_pec == -2) { $this->configurazione["host"] = $this->config["smtp_server_newsletter"]; $this->configurazione["smtp_port"] = $this->config["smtp_port_newsletter"]; $this->configurazione["smtp_ssl"] = $this->config["smtp_ssl_newsletter"]; $this->configurazione["smtp_username"] = $this->config["smtp_username_newsletter"] ?? $this->config["mittente_newsletter"]; $this->configurazione["mittente_mail"] = $this->config["mittente_newsletter"]; $this->configurazione["smtp_password"] = $this->config["smtp_password_newsletter"]; } else if ($this->codice_pec == -3) { $this->configurazione["host"] = $this->config["smtp_server_reset"]; $this->configurazione["smtp_port"] = $this->config["smtp_port_reset"]; $this->configurazione["smtp_ssl"] = $this->config["smtp_ssl_reset"]; $this->configurazione["smtp_username"] = $this->config["smtp_username_reset"] ?? $this->config["mittente_reset"]; $this->configurazione["mittente_mail"] = $this->config["mittente_reset"]; $this->configurazione["smtp_password"] = $this->config["smtp_password_reset"]; } else { $this->configurazione["host"] = $this->config["smtp_server"]; $this->configurazione["smtp_port"] = $this->config["smtp_port"]; $this->configurazione["smtp_ssl"] = $this->config["smtp_ssl"]; $this->configurazione["smtp_username"] = $this->config["smtp_username"] ?? $this->config["mittente_mail"]; $this->configurazione["mittente_mail"] = $this->config["mittente_mail"]; $this->configurazione["smtp_password"] = $this->config["smtp_password"]; } } if (count($this->configurazione) > 0) { return true; } else { $this->error .= "Errore configurazione PEC
"; return FALSE; } } protected function checkAddress() { if(!empty($this->destinatari)) { if (!is_array($this->destinatari)) { $this->destinatari = array($this->destinatari); } foreach($this->destinatari as $destinatario) { if (!is_numeric($destinatario)) { $this->address[] = $destinatario; $user_code = $this->findCode($destinatario); if ($user_code != false) { $this->utenti[] = $user_code; $this->utenti_pec[$destinatario] = $user_code; } } else if (is_numeric($destinatario)) { $user_pec = $this->findAddress($destinatario); if ($user_pec != false) { $this->address[] = $user_pec; $this->utenti[] = $destinatario; $this->utenti_pec[$user_pec] = $destinatario; } } } $this->address = array_unique($this->address); $this->utenti = array_unique($this->utenti); $this->utenti_pec = array_unique($this->utenti_pec); $this->address = array_filter($this->address); $this->utenti = array_filter($this->utenti); $this->utenti_pec = array_filter($this->utenti_pec); } } protected function findAddress($id) { $sql = "SELECT pec FROM b_operatori_economici WHERE codice = :id AND pec <> ''"; $ris = $this->pdo->go($sql, array(":id"=>$id)); if ($ris->rowCount() == 1) { $rec = $ris->fetch(PDO::FETCH_ASSOC); return trim($rec["pec"]); } return false; } protected function findCode($pec) { return oeManager::getIdFromPEC($pec); } protected function getHTML() { $head = ""; $bottom = ""; $this->oggetto = $this->oggetto; $this->corpo = mb_convert_encoding($this->corpo, "HTML-ENTITIES", "UTF-8"); if ($this->intestazione) { $head = ''; if (isset($this->ente)) { if (!empty($this->ente["interfaccia_menu"])) { $textColor = (evaluateColorContrast($this->ente["interfaccia_menu"],"000000")["ratio"] > 7) ? "black" : "white"; $head .= '
'; } else { $head .= '
'; } $head .= ''; $head .= '"; } else { $head .= '
ente["logo"].'" width="150">
'; $head .= '

'.$this->ente["denominazione"].'

'; $head .= ''.$this->ente["indirizzo"].' - '.$this->ente["citta"].' ('.$this->ente["provincia"].')
'; if ($this->ente["telefono"] != "") { $head .= 'Tel. '.$this->ente["telefono"].'
'; } if ($this->ente["fax"] != "") { $head .= 'Fax. '.$this->ente["fax"].'
'; } if ($this->ente["email"] != "") { $head .= 'Email: '.$this->ente["email"].'
'; } if(! empty($this->ente["pec_footer"])) { if ($this->ente["pec_footer"] != "") { $head .= 'PEC: '.$this->ente["pec_footer"].'
'; } } else { if ($this->ente["pec"] != "") { $head .= 'PEC: '.$this->ente["pec"].'
'; } } $head .= "
'; $head .= ''; $head .= ''; } $head .= '
Tutto Gare'; $head .= '

Tutto Gare

'; $head .= '
'; $head .= '
'; $bottom = "
"; if(! empty($this->ente["messaggio_comunicazioni"])) { $bottom .= '
'; $bottom .= $this->ente["messaggio_comunicazioni"]; $bottom .= "
"; } $bottom .= '
'; $bottom .= 'Tutto Gare'; $bottom .= '
'; $bottom .= ''; } $corpo = $this->corpo; $re = '/href=(\'|\")(?!http)(.*)(\'|\")>/m'; preg_match_all($re, $corpo, $matches, PREG_SET_ORDER, 0); foreach($matches AS $match) { $corpo = str_replace($match[2],$this->config["link_sito"].$match[2],$corpo); } return $head.$corpo.$bottom; } protected function saveToQueue() { $sql = "INSERT INTO b_coda (`codice_relazione`,`indirizzo`,`oggetto`,`corpo`,`codice_pec`,`codice_ente`,`inviata`,`comunicazione_tecnica`,`utente_modifica`,`timestamp`,`timestamp_creazione`) VALUES (:codice_relazione,:indirizzo,:oggetto,:corpo,:codice_pec,:codice_ente,'N',:comunicazione_tecnica,:utente_modifica,:timestamp,:timestamp_creazione)"; $ris = $this->pdo->prepare($sql); $ris->bindValue(":utente_modifica",!empty($_SESSION["utente"]->codice) ? $_SESSION["utente"]->codice : -1); $ris->bindValue(":timestamp",date('Y-m-d H:i:s')); $ris->bindValue(":timestamp_creazione",date('Y-m-d H:i:s')); $ris->bindValue(':comunicazione_tecnica', $this->comunicazione_tecnica ? 1 : 0); // $salva = new salva(); // $salva->debug = false; // // $salva->nome_tabella = "b_coda"; // $salva->operazione = "INSERT"; // $nome_sito = $this->config["nome_sito"]; // if (isset($this->ente)) { $nome_sito = "Portale Gare - " . $this->ente["denominazione"]; } foreach($this->address AS $destinatario) { // $coda = array(); $ris->bindValue(":indirizzo",$destinatario); $ris->bindValue(":oggetto", $this->oggetto); $ris->bindValue(":corpo",$this->getHTML()); $ris->bindValue(":codice_pec" ,$this->codice_pec); $codice_relazione = 0; if(!empty($this->rel_comunicazioni[$destinatario])) { $codice_relazione = $this->rel_comunicazioni[$destinatario]; } $ris->bindValue(":codice_relazione",$codice_relazione); //if (!empty($codice_relazione)) { $coda["codice_relazione"] = $codice_relazione; } if (!empty($this->ente)) { $ris->bindValue(":codice_ente",$this->ente["codice"]); } else { $ris->bindValue(":codice_ente",0); } // $coda["inviata"] = "N"; // $salva->oggetto = $coda; // $salva->debug = FALSE; // $codice_coda = $salva->save(); if (!$ris->execute()) $this->error .= "Errore nel salvataggio nella coda dell'indirizzo " . $destinatario . "
"; //if ($codice_coda == false) { $this->error .= "Errore nel salvataggio nella coda dell'indirizzo " . $destinatario . "
"; } } } protected function saveComunicazione() { if ($this->comunicazione && !empty($this->ente) && !empty($this->address)) { $comunicazione = array(); $comunicazione["codice_ente"] = $this->ente["codice"]; if (!empty($this->codice_gara)) { $comunicazione["codice_gara"] = $this->codice_gara; if (!empty($this->sezione)) { $comunicazione["sezione"] = $this->sezione; } } if($this->codice_pec >= 0) { $comunicazione["codice_pec"] = $this->codice_pec; } $comunicazione["oggetto"] = $this->oggetto; $comunicazione["corpo"] = $this->corpo; $salva = new salva(); $salva->debug = false; $salva->nome_tabella = "b_comunicazioni"; $salva->operazione = "INSERT"; $salva->oggetto = $comunicazione; $codice_comunicazione = $salva->save(); if ($codice_comunicazione != false) { if (!empty($this->allegati)) { $cod_allegati = []; if (isset($this->allegati)) { foreach($this->allegati as $allegato) { $cod_allegato = filesManager::saveFile($allegato["filename"],$codice_comunicazione,"comunicazioni",$allegato["nome"]); if ($cod_allegato != false) { $cod_allegati[] = $cod_allegato; } } if (!empty($cod_allegati)) { ob_start(); filesManager::printlist($codice_comunicazione,"comunicazioni","A",$cod_allegati,false); $corpo_alleagati = ob_get_clean(); $this->corpo .= $corpo_alleagati; $this->corpo = str_replace("/allegati/open.php",$_SESSION["config"]["link_sito"]."/allegati/open.php",$this->corpo); } } } $salva->operazione = "UPDATE"; $salva->oggetto = ["codice"=>$codice_comunicazione,"corpo"=>$this->corpo]; $salva->save(); $salva->operazione = "INSERT"; $salva->nome_tabella = "r_comunicazioni_utenti"; $uc = $this->pdo->prepare('UPDATE r_comunicazioni_utenti SET identificativo_messaggio = :identificativo_messaggio WHERE codice = :codice'); foreach($this->address as $pec) { if ($pec != "") { $r_comunicazione = array(); $r_comunicazione["codice_comunicazione"] = $codice_comunicazione; $r_comunicazione["codice_ente"] = $this->ente["codice"]; $r_comunicazione["codice_operatore"] = $this->utenti_pec[$pec] ?? 0; $r_comunicazione["pec"] = $pec; $salva->oggetto = $r_comunicazione; $codice_relazione = $salva->save(); if ($codice_relazione == false) { $this->error .= "Errore nel salvataggio della comunicazione per la PEC #" . $pec . "
"; } else { $m = microtime(true); $r = uniqid('', true); $uc->bindValue(':codice', $codice_relazione); $uc->bindValue(':identificativo_messaggio', "<{$codice_relazione}-{$r}-{$m}@tuttogare-pa.it>"); $uc->execute(); $this->rel_comunicazioni[$pec] = $codice_relazione; $this->identifiers[$pec] = "<{$codice_relazione}-{$r}-{$m}@tuttogare-pa.it>"; } } } } else { $this->error .= "Errore generale nel salvataggio della comunicazione
"; } } } } ?>