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.
453 righe
19 KiB
453 righe
19 KiB
<? |
|
use PHPMailer\PHPMailer\PHPMailer; |
|
use PHPMailer\PHPMailer\Exception; |
|
/** |
|
* CommunicatorDefault |
|
*/ |
|
class CommunicatorDefault |
|
{ |
|
|
|
protected $root; |
|
protected $config; |
|
protected $pdo; |
|
protected $error; |
|
protected $rel_comunicazioni; |
|
public $attachment; |
|
public $codice_relazione; |
|
public $destinatari; |
|
public $comunicazione; |
|
public $codice_pec; |
|
public $oggetto; |
|
public $corpo; |
|
public $allegati; |
|
public $coda; |
|
public $codice_gara; |
|
public $codice_lotto; |
|
public $codice_ente; |
|
public $ente; |
|
public $sezione; |
|
public $intestazione; |
|
public $elaborazione_coda; |
|
public $configurazione; |
|
public $address; |
|
public $identifiers; |
|
public $utenti; |
|
public $codice_coda; |
|
public $comunicazione_tecnica; |
|
public $utenti_pec; |
|
public $enable_smtp; |
|
public $type; |
|
public $force_coda; |
|
|
|
function __construct($parameters, $pdo=null) |
|
{ |
|
global $root, $config; |
|
$this->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<br>"; } |
|
//Verifico che l'oggetto del messaggio non sia vuoto |
|
if(empty($this->oggetto)) $this->error .= "Oggetto vuoto<br>"; |
|
// 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<br>"; } |
|
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 = "<general-{$r}-{$m}@tuttogare-pa-3.it>"; |
|
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 . "<br>"; |
|
} |
|
} |
|
$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<br>"; |
|
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 = '<html><head><style>body { font-family: Tahoma, Geneva, sans-serif; margin:0px; padding:0px } .padding { padding:20px; } tr.odd { background-color:#F6F6F6;} tr.even { background-color:#ECECEC; } #bottom { padding:20px; background-color: #DDD; text-align:right }</style></head><body>'; |
|
|
|
if (isset($this->ente)) { |
|
if (!empty($this->ente["interfaccia_menu"])) { |
|
$textColor = (evaluateColorContrast($this->ente["interfaccia_menu"],"000000")["ratio"] > 7) ? "black" : "white"; |
|
$head .= '<div style="background-color:#'.$this->ente["interfaccia_menu"].';" class="padding" style="width:100%"><table style="width:100%; color:'.$textColor.'">'; |
|
} else { |
|
$head .= '<div class="padding" style="width:100%"><table style="width:100%">'; |
|
} |
|
$head .= '<tr><td style="width:150px"><img src="'.$this->config["link_sito"].'/documenti/enti/'.$this->ente["logo"].'" width="150"></td>'; |
|
$head .= '<td style="width:700px"><div class="padding">'; |
|
$head .= '<h1>'.$this->ente["denominazione"].'</h1>'; |
|
$head .= '<strong>'.$this->ente["indirizzo"].' - '.$this->ente["citta"].' ('.$this->ente["provincia"].')</strong><br>'; |
|
if ($this->ente["telefono"] != "") { $head .= 'Tel. '.$this->ente["telefono"].'<br>'; } |
|
if ($this->ente["fax"] != "") { $head .= 'Fax. '.$this->ente["fax"].'<br>'; } |
|
if ($this->ente["email"] != "") { $head .= 'Email: <a href="mailto:'.$this->ente["email"].'">'.$this->ente["email"].'</a><br>'; } |
|
if(! empty($this->ente["pec_footer"])) { |
|
if ($this->ente["pec_footer"] != "") { $head .= 'PEC: <a href="mailto:'.$this->ente["pec_footer"].'">'.$this->ente["pec_footer"].'</a><br>'; } |
|
} else { |
|
if ($this->ente["pec"] != "") { $head .= 'PEC: <a href="mailto:'.$this->ente["pec"].'">'.$this->ente["pec"].'</a><br>'; } |
|
} |
|
$head .= "</div></td></tr>"; |
|
} else { |
|
$head .= '<div class="padding" style="width:100%"><table style="width:100%">'; |
|
$head .= '<tr><td style="width:10%"><img src="'.$this->config["link_sito"].'/src/img/logo-srl-blu.png" width="150" alt="Tutto Gare"></td>'; |
|
$head .= '<td style="width:90%">'; |
|
$head .= '<h1>Tutto Gare</h1>'; |
|
$head .= '</td></tr>'; |
|
} |
|
$head .= '</table></div>'; |
|
$head .= '<hr><div class="padding">'; |
|
|
|
$bottom = "</div>"; |
|
if(! empty($this->ente["messaggio_comunicazioni"])) { |
|
$bottom .= '<hr><div class="padding" style="font-style: italic; color: #808080 !important">'; |
|
$bottom .= $this->ente["messaggio_comunicazioni"]; |
|
$bottom .= "</div>"; |
|
} |
|
$bottom .= '<div id="bottom">'; |
|
$bottom .= '<img src="'.$this->config["link_sito"].'/src/img/logo-srl-blu.png" width="50" alt="Tutto Gare">'; |
|
$bottom .= '</div>'; |
|
$bottom .= '</body></html>'; |
|
} |
|
$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 . "<br>"; |
|
//if ($codice_coda == false) { $this->error .= "Errore nel salvataggio nella coda dell'indirizzo " . $destinatario . "<br>"; } |
|
} |
|
} |
|
|
|
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 . "<br>"; |
|
} 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 <br>"; |
|
} |
|
} |
|
} |
|
|
|
} |
|
|
|
?>
|
|
|