This commit is contained in:
Paula Quispe
2019-03-07 11:45:22 -04:00
parent ce63bc91e8
commit 614efc490b

View File

@@ -15,18 +15,19 @@ use ProcessMaker\Core\System;
*/ */
class SpoolRun class SpoolRun
{ {
public $config; private $appUid;
private $appMsgUid;
private $warnings = []; //Array to store the warning that were throws by the class
private $exceptionCode = []; //Array to define the Exception codes
private $fileData; private $fileData;
private $spool_id;
public $status;
public $error;
private $ExceptionCode = Array(); //Array to define the Expetion codes
private $aWarnings = Array(); //Array to store the warning that were throws by the class
private $longMailEreg; private $longMailEreg;
private $mailEreg; private $mailEreg;
private $spoolId;
public $config;
public $error;
public $status;
/** /**
* Class constructor - iniatilize default values * Class constructor - iniatilize default values
@@ -38,18 +39,58 @@ class SpoolRun
{ {
$this->config = array(); $this->config = array();
$this->fileData = array(); $this->fileData = array();
$this->spool_id = ''; $this->spoolId = '';
$this->status = 'pending'; $this->status = 'pending';
$this->error = ''; $this->error = '';
$this->ExceptionCode['FATAL'] = 1; $this->exceptionCode['FATAL'] = 1;
$this->ExceptionCode['WARNING'] = 2; $this->exceptionCode['WARNING'] = 2;
$this->ExceptionCode['NOTICE'] = 3; $this->exceptionCode['NOTICE'] = 3;
$this->longMailEreg = "/(.*)(<([\w\-\+\.']+@[\w\-_\.]+\.\w{2,5})+>)/"; $this->longMailEreg = "/(.*)(<([\w\-\+\.']+@[\w\-_\.]+\.\w{2,5})+>)/";
$this->mailEreg = "/^([\w\-_\+\.']+@[\w\-_\.]+\.\w{2,5}+)$/"; $this->mailEreg = "/^([\w\-_\+\.']+@[\w\-_\.]+\.\w{2,5}+)$/";
} }
/**
* Get the appUid
*
* @return string
*/
public function getAppUid()
{
return $this->appUid;
}
/**
* Set the appUid
*
* @param string $v
*/
public function setAppUid($v)
{
$this->appUid = $v;
}
/**
* Get the appMsgUid
*
* @return string
*/
public function getAppMsgUid()
{
return $this->appMsgUid;
}
/**
* Set the appMsgUid
*
* @param string $v
*/
public function setAppMsgUid($v)
{
$this->appMsgUid = $v;
}
/** /**
* get all files into spool in a list * get all files into spool in a list
* *
@@ -65,7 +106,7 @@ class SpoolRun
$rs = $stmt->executeQuery(); $rs = $stmt->executeQuery();
while ($rs->next()) { while ($rs->next()) {
$this->spool_id = $rs->getString('APP_MSG_UID'); $this->spoolId = $rs->getString('APP_MSG_UID');
$this->fileData['subject'] = $rs->getString('APP_MSG_SUBJECT'); $this->fileData['subject'] = $rs->getString('APP_MSG_SUBJECT');
$this->fileData['from'] = $rs->getString('APP_MSG_FROM'); $this->fileData['from'] = $rs->getString('APP_MSG_FROM');
$this->fileData['to'] = $rs->getString('APP_MSG_TO'); $this->fileData['to'] = $rs->getString('APP_MSG_TO');
@@ -110,7 +151,7 @@ class SpoolRun
$aData['app_msg_attach'] = serialize($attachment); $aData['app_msg_attach'] = serialize($attachment);
$aData['app_msg_show_message'] = (isset($aData['app_msg_show_message'])) ? $aData['app_msg_show_message'] : 1; $aData['app_msg_show_message'] = (isset($aData['app_msg_show_message'])) ? $aData['app_msg_show_message'] : 1;
$aData["app_msg_error"] = (isset($aData["app_msg_error"])) ? $aData["app_msg_error"] : ''; $aData["app_msg_error"] = (isset($aData["app_msg_error"])) ? $aData["app_msg_error"] : '';
$sUID = $this->db_insert($aData); $sUID = $this->dbInsert($aData);
$aData['app_msg_date'] = isset($aData['app_msg_date']) ? $aData['app_msg_date'] : ''; $aData['app_msg_date'] = isset($aData['app_msg_date']) ? $aData['app_msg_date'] : '';
@@ -178,7 +219,7 @@ class SpoolRun
*/ */
public function setData($sAppMsgUid, $sSubject, $sFrom, $sTo, $sBody, $sDate = "", $sCC = "", $sBCC = "", $sTemplate = "", $aAttachment = array(), $bContentTypeIsHtml = true, $sError = "") public function setData($sAppMsgUid, $sSubject, $sFrom, $sTo, $sBody, $sDate = "", $sCC = "", $sBCC = "", $sTemplate = "", $aAttachment = array(), $bContentTypeIsHtml = true, $sError = "")
{ {
$this->spool_id = $sAppMsgUid; $this->spoolId = $sAppMsgUid;
$this->fileData['subject'] = $sSubject; $this->fileData['subject'] = $sSubject;
$this->fileData['from'] = $sFrom; $this->fileData['from'] = $sFrom;
$this->fileData['to'] = $sTo; $this->fileData['to'] = $sTo;
@@ -234,17 +275,40 @@ class SpoolRun
*/ */
private function updateSpoolStatus() private function updateSpoolStatus()
{ {
$oAppMessage = AppMessagePeer::retrieveByPK($this->spool_id); $oAppMessage = AppMessagePeer::retrieveByPK($this->spoolId);
if (is_array($this->fileData['attachments'])) { if (is_array($this->fileData['attachments'])) {
$attachment = implode(",", $this->fileData['attachments']); $attachment = implode(",", $this->fileData['attachments']);
$oAppMessage->setappMsgAttach($attachment); $oAppMessage->setappMsgAttach($attachment);
} }
$oAppMessage->setAppMsgStatus($this->status); $oAppMessage->setAppMsgStatus($this->status);
$oAppMessage->setAppMsgStatusId(isset(AppMessage::$app_msg_status_values[$this->status]) ? AppMessage::$app_msg_status_values[$this->status] : 0); $oAppMessage->setAppMsgStatusId(isset(AppMessage::$app_msg_status_values[$this->status]) ? AppMessage::$app_msg_status_values[$this->status] : 0);
$oAppMessage->setappMsgsenddate(date('Y-m-d H:i:s')); $oAppMessage->setAppMsgSendDate(date('Y-m-d H:i:s'));
$oAppMessage->save(); $oAppMessage->save();
} }
/**
* Update the error
*
* @param string $msgError
*
* @return void
*
* @see SpoolRun::handleMail()
*/
private function updateSpoolError($msgError)
{
$appMessage = AppMessagePeer::retrieveByPK($this->spoolId);
$appMessage->setAppMsgError($msgError);
$appMessage->setAppMsgSendDate(date('Y-m-d H:i:s'));
$appMessage->save();
$context = Bootstrap::getDefaultContextLog();
$context["action"] = "Send email";
$context["appMsgUid"] = $this->getAppMsgUid();
$context["appUid"] = $this->getAppUid();
Bootstrap::registerMonolog("SendEmail", 400, $msgError, $context);
}
/** /**
* handle the email that was set in "TO" parameter * handle the email that was set in "TO" parameter
* *
@@ -269,7 +333,7 @@ class SpoolRun
} }
if (!isset($matches[3])) { if (!isset($matches[3])) {
throw new Exception('Invalid email address in FROM parameter (' . $this->fileData['from'] . ')', $this->ExceptionCode['WARNING']); throw new Exception('Invalid email address in FROM parameter (' . $this->fileData['from'] . ')', $this->exceptionCode['WARNING']);
} }
$this->fileData['from_email'] = trim($matches[3]); $this->fileData['from_email'] = trim($matches[3]);
@@ -279,7 +343,7 @@ class SpoolRun
preg_match($ereg, $this->fileData["from"], $matches); preg_match($ereg, $this->fileData["from"], $matches);
if (!isset($matches[0])) { if (!isset($matches[0])) {
throw new Exception('Invalid email address in FROM parameter (' . $this->fileData['from'] . ')', $this->ExceptionCode['WARNING']); throw new Exception('Invalid email address in FROM parameter (' . $this->fileData['from'] . ')', $this->exceptionCode['WARNING']);
} }
$this->fileData['from_name'] = ''; $this->fileData['from_name'] = '';
@@ -374,10 +438,13 @@ class SpoolRun
} }
/** /**
* handle and compose the email content and parameters * Handle and compose the email content and parameters
* *
* @param none * @return void
* @return none *
* @throws Exception
*
* @see SpoolRun::sendMail()
*/ */
private function handleMail() private function handleMail()
{ {
@@ -386,20 +453,18 @@ class SpoolRun
switch ($this->config['MESS_ENGINE']) { switch ($this->config['MESS_ENGINE']) {
case 'MAIL': case 'MAIL':
case 'PHPMAILER': case 'PHPMAILER':
switch ($this->config['MESS_ENGINE']) { switch ($this->config['MESS_ENGINE']) {
case 'MAIL': case 'MAIL':
$oPHPMailer = new PHPMailer(); $phpMailer = new PHPMailer();
$oPHPMailer->Mailer = 'mail'; $phpMailer->Mailer = 'mail';
break; break;
case 'PHPMAILER': case 'PHPMAILER':
$oPHPMailer = new PHPMailer(true); $phpMailer = new PHPMailer(true);
$oPHPMailer->Mailer = 'smtp'; $phpMailer->Mailer = 'smtp';
break; break;
} }
$oPHPMailer->SMTPAuth = (isset($this->config['SMTPAuth']) ? $this->config['SMTPAuth'] : ''); $phpMailer->SMTPAuth = (isset($this->config['SMTPAuth']) ? $this->config['SMTPAuth'] : '');
switch ($this->config['MESS_ENGINE']) { switch ($this->config['MESS_ENGINE']) {
case 'MAIL': case 'MAIL':
@@ -407,101 +472,103 @@ class SpoolRun
case 'PHPMAILER': case 'PHPMAILER':
//Posible Options for SMTPSecure are: "", "ssl" or "tls" //Posible Options for SMTPSecure are: "", "ssl" or "tls"
if (isset($this->config['SMTPSecure']) && preg_match('/^(ssl|tls)$/', $this->config['SMTPSecure'])) { if (isset($this->config['SMTPSecure']) && preg_match('/^(ssl|tls)$/', $this->config['SMTPSecure'])) {
$oPHPMailer->SMTPSecure = $this->config['SMTPSecure']; $phpMailer->SMTPSecure = $this->config['SMTPSecure'];
} }
break; break;
} }
$systemConfiguration = System::getSystemConfiguration();
$oPHPMailer->Timeout = is_numeric($systemConfiguration['smtp_timeout']) ? $systemConfiguration['smtp_timeout'] : 20;
$oPHPMailer->CharSet = "UTF-8";
$oPHPMailer->Encoding = "8bit";
$oPHPMailer->Host = $this->config['MESS_SERVER'];
$oPHPMailer->Port = $this->config['MESS_PORT'];
$oPHPMailer->Username = $this->config['MESS_ACCOUNT'];
$oPHPMailer->Password = $this->config['MESS_PASSWORD'];
$oPHPMailer->SetFrom($this->fileData['from_email'], utf8_decode($this->fileData['from_name']));
if (isset($this->fileData['reply_to'])) { try {
if ($this->fileData['reply_to'] != '') { $systemConfiguration = System::getSystemConfiguration();
$oPHPMailer->AddReplyTo($this->fileData['reply_to'], $this->fileData['reply_to_name']); $phpMailer->Timeout = is_numeric($systemConfiguration['smtp_timeout']) ? $systemConfiguration['smtp_timeout'] : 20;
} $phpMailer->CharSet = "UTF-8";
} $phpMailer->Encoding = "8bit";
$phpMailer->Host = $this->config['MESS_SERVER'];
$phpMailer->Port = $this->config['MESS_PORT'];
$phpMailer->Username = $this->config['MESS_ACCOUNT'];
$phpMailer->Password = $this->config['MESS_PASSWORD'];
$msSubject = $this->fileData['subject']; //From
$phpMailer->SetFrom($this->fileData['from_email'], utf8_decode($this->fileData['from_name']));
if (!(mb_detect_encoding($msSubject, "UTF-8") == "UTF-8")) { //Reply to
$msSubject = utf8_encode($msSubject); if (isset($this->fileData['reply_to'])) {
} if ($this->fileData['reply_to'] != '') {
$phpMailer->AddReplyTo($this->fileData['reply_to'], $this->fileData['reply_to_name']);
$oPHPMailer->Subject = $msSubject;
$msBody = $this->fileData['body'];
if (!(mb_detect_encoding($msBody, "UTF-8") == "UTF-8")) {
$msBody = utf8_encode($msBody);
}
$oPHPMailer->Body = $msBody;
$attachment = @unserialize($this->fileData['attachments']);
if ($attachment === false) {
$attachment = $this->fileData['attachments'];
}
if (is_array($attachment)) {
foreach ($attachment as $key => $fileAttach) {
if (file_exists($fileAttach)) {
$oPHPMailer->AddAttachment($fileAttach, is_int($key) ? '' : $key);
} }
} }
} //Subject
$msSubject = $this->fileData['subject'];
foreach ($this->fileData['envelope_to'] as $sEmail) { if (!(mb_detect_encoding($msSubject, "UTF-8") == "UTF-8")) {
if (strpos($sEmail, '<') !== false) { $msSubject = utf8_encode($msSubject);
preg_match($this->longMailEreg, $sEmail, $matches);
$sTo = trim($matches[3]);
$sToName = trim($matches[1]);
$oPHPMailer->AddAddress($sTo, $sToName);
} else {
$oPHPMailer->AddAddress($sEmail);
} }
} $phpMailer->Subject = $msSubject;
//Body
//CC $msBody = $this->fileData['body'];
foreach ($this->fileData['envelope_cc'] as $sEmail) { if (!(mb_detect_encoding($msBody, "UTF-8") == "UTF-8")) {
if (strpos($sEmail, '<') !== false) { $msBody = utf8_encode($msBody);
preg_match($this->longMailEreg, $sEmail, $matches);
$sTo = trim($matches[3]);
$sToName = trim($matches[1]);
$oPHPMailer->AddCC($sTo, $sToName);
} else {
$oPHPMailer->AddCC($sEmail);
} }
} $phpMailer->Body = $msBody;
//Attachments
//BCC $attachment = @unserialize($this->fileData['attachments']);
foreach ($this->fileData['envelope_bcc'] as $sEmail) { if ($attachment === false) {
if (strpos($sEmail, '<') !== false) { $attachment = $this->fileData['attachments'];
preg_match($this->longMailEreg, $sEmail, $matches);
$sTo = trim($matches[3]);
$sToName = trim($matches[1]);
$oPHPMailer->AddBCC($sTo, $sToName);
} else {
$oPHPMailer->AddBCC($sEmail);
} }
} if (is_array($attachment)) {
foreach ($attachment as $key => $fileAttach) {
if (file_exists($fileAttach)) {
$phpMailer->AddAttachment($fileAttach, is_int($key) ? '' : $key);
}
}
}
//To
foreach ($this->fileData['envelope_to'] as $sEmail) {
if (strpos($sEmail, '<') !== false) {
preg_match($this->longMailEreg, $sEmail, $matches);
$sTo = trim($matches[3]);
$sToName = trim($matches[1]);
$phpMailer->AddAddress($sTo, $sToName);
} else {
$phpMailer->AddAddress($sEmail);
}
}
//CC
foreach ($this->fileData['envelope_cc'] as $sEmail) {
if (strpos($sEmail, '<') !== false) {
preg_match($this->longMailEreg, $sEmail, $matches);
$sTo = trim($matches[3]);
$sToName = trim($matches[1]);
$phpMailer->AddCC($sTo, $sToName);
} else {
$phpMailer->AddCC($sEmail);
}
}
//BCC
foreach ($this->fileData['envelope_bcc'] as $sEmail) {
if (strpos($sEmail, '<') !== false) {
preg_match($this->longMailEreg, $sEmail, $matches);
$sTo = trim($matches[3]);
$sToName = trim($matches[1]);
$phpMailer->AddBCC($sTo, $sToName);
} else {
$phpMailer->AddBCC($sEmail);
}
}
//IsHtml
$phpMailer->IsHTML($this->fileData["contentTypeIsHtml"]);
$oPHPMailer->IsHTML($this->fileData["contentTypeIsHtml"]); if ($this->config['MESS_ENGINE'] == 'MAIL') {
$phpMailer->WordWrap = 300;
}
if ($this->config['MESS_ENGINE'] == 'MAIL') { if ($phpMailer->Send()) {
$oPHPMailer->WordWrap = 300; $this->error = '';
} $this->status = 'sent';
} else {
if ($oPHPMailer->Send()) { $this->error = $phpMailer->ErrorInfo;
$this->error = ''; $this->status = 'failed';
$this->status = 'sent'; $this->updateSpoolError($this->error);
} else { }
$this->error = $oPHPMailer->ErrorInfo; } catch (Exception $error) {
$this->status = 'failed'; $this->updateSpoolError($error->getMessage());
} }
break; break;
case 'OPENMAIL': case 'OPENMAIL':
@@ -605,8 +672,8 @@ class SpoolRun
} catch (Exception $e) { } catch (Exception $e) {
$strAux = "Spool::resendEmails(): Using " . $configuration["MESS_ENGINE"] . " for APP_MGS_UID=" . $row["APP_MSG_UID"] . " -> With message: " . $e->getMessage(); $strAux = "Spool::resendEmails(): Using " . $configuration["MESS_ENGINE"] . " for APP_MGS_UID=" . $row["APP_MSG_UID"] . " -> With message: " . $e->getMessage();
if ($e->getCode() == $this->ExceptionCode["WARNING"]) { if ($e->getCode() == $this->exceptionCode["WARNING"]) {
array_push($this->aWarnings, $strAux); array_push($this->warnings, $strAux);
continue; continue;
} else { } else {
error_log('<400> ' . $strAux); error_log('<400> ' . $strAux);
@@ -625,47 +692,54 @@ class SpoolRun
*/ */
public function getWarnings() public function getWarnings()
{ {
if (sizeof($this->aWarnings) != 0) { if (sizeof($this->warnings) != 0) {
return $this->aWarnings; return $this->warnings;
} }
return false; return false;
} }
/** /**
* db_insert * Insert the record in the AppMessage
* *
* @param array $db_spool * @param array $dbSpool
* @return string $sUID; *
* @return string
*
* @see SpoolRun::create()
*/ */
public function db_insert($db_spool) public function dbInsert($dbSpool)
{ {
$sUID = G::generateUniqueID(); $appMsgUid = G::generateUniqueID();
//Set some values for generate the log
$this->setAppMsgUid($appMsgUid);
$this->setAppUid($dbSpool['app_uid']);
//Set values for register the record
$spool = new AppMessage(); $spool = new AppMessage();
$spool->setAppMsgUid($sUID); $spool->setAppMsgUid($appMsgUid);
$spool->setMsgUid($db_spool['msg_uid']); $spool->setMsgUid($dbSpool['msg_uid']);
$spool->setAppUid($db_spool['app_uid']); $spool->setAppUid($dbSpool['app_uid']);
$spool->setDelIndex($db_spool['del_index']); $spool->setDelIndex($dbSpool['del_index']);
$spool->setAppMsgType($db_spool['app_msg_type']); $spool->setAppMsgType($dbSpool['app_msg_type']);
$spool->setAppMsgTypeId(isset(AppMessage::$app_msg_type_values[$db_spool['app_msg_type']]) ? AppMessage::$app_msg_type_values[$db_spool['app_msg_type']] : 0); $spool->setAppMsgTypeId(isset(AppMessage::$app_msg_type_values[$dbSpool['app_msg_type']]) ? AppMessage::$app_msg_type_values[$dbSpool['app_msg_type']] : 0);
$spool->setAppMsgSubject($db_spool['app_msg_subject']); $spool->setAppMsgSubject($dbSpool['app_msg_subject']);
$spool->setAppMsgFrom($db_spool['app_msg_from']); $spool->setAppMsgFrom($dbSpool['app_msg_from']);
$spool->setAppMsgTo($db_spool['app_msg_to']); $spool->setAppMsgTo($dbSpool['app_msg_to']);
$spool->setAppMsgBody($db_spool['app_msg_body']); $spool->setAppMsgBody($dbSpool['app_msg_body']);
$spool->setAppMsgDate(date('Y-m-d H:i:s')); $spool->setAppMsgDate(date('Y-m-d H:i:s'));
$spool->setAppMsgCc($db_spool['app_msg_cc']); $spool->setAppMsgCc($dbSpool['app_msg_cc']);
$spool->setAppMsgBcc($db_spool['app_msg_bcc']); $spool->setAppMsgBcc($dbSpool['app_msg_bcc']);
$spool->setappMsgAttach($db_spool['app_msg_attach']); $spool->setappMsgAttach($dbSpool['app_msg_attach']);
$spool->setAppMsgTemplate($db_spool['app_msg_template']); $spool->setAppMsgTemplate($dbSpool['app_msg_template']);
$spool->setAppMsgStatus($db_spool['app_msg_status']); $spool->setAppMsgStatus($dbSpool['app_msg_status']);
$spool->setAppMsgStatusId(isset(AppMessage::$app_msg_status_values[$db_spool['app_msg_status']]) ? AppMessage::$app_msg_status_values[$db_spool['app_msg_status']] : 0); $spool->setAppMsgStatusId(isset(AppMessage::$app_msg_status_values[$dbSpool['app_msg_status']]) ? AppMessage::$app_msg_status_values[$dbSpool['app_msg_status']] : 0);
$spool->setAppMsgSendDate(date('Y-m-d H:i:s')); $spool->setAppMsgSendDate(date('Y-m-d H:i:s'));
$spool->setAppMsgShowMessage($db_spool['app_msg_show_message']); $spool->setAppMsgShowMessage($dbSpool['app_msg_show_message']);
$spool->setAppMsgError($db_spool['app_msg_error']); $spool->setAppMsgError($dbSpool['app_msg_error']);
$appDelegation = new AppDelegation(); $appDelegation = new AppDelegation();
if (empty($db_spool['app_number'])) { if (empty($dbSpool['app_number'])) {
$delegationIds = $appDelegation->getColumnIds($db_spool['app_uid'], $db_spool['del_index']); $delegationIds = $appDelegation->getColumnIds($dbSpool['app_uid'], $dbSpool['del_index']);
if (is_array($delegationIds) && count($delegationIds) > 0) { if (is_array($delegationIds) && count($delegationIds) > 0) {
$delegationIds = array_change_key_case($delegationIds); $delegationIds = array_change_key_case($delegationIds);
$appNumber = $delegationIds['app_number']; $appNumber = $delegationIds['app_number'];
@@ -674,19 +748,19 @@ class SpoolRun
$appNumber = 0; $appNumber = 0;
} }
} else { } else {
$appNumber = $db_spool['app_number']; $appNumber = $dbSpool['app_number'];
} }
if (empty($db_spool['tas_id'])) { if (empty($dbSpool['tas_id'])) {
$tasId = isset($delegationIds['tas_id']) ? $delegationIds['tas_id'] : 0; $tasId = isset($delegationIds['tas_id']) ? $delegationIds['tas_id'] : 0;
} else { } else {
$tasId = $db_spool['tas_id']; $tasId = $dbSpool['tas_id'];
} }
if (empty($db_spool['pro_id'])) { if (empty($dbSpool['pro_id'])) {
$proId = isset($delegationIds['pro_id']) ? $delegationIds['pro_id'] : $appDelegation->getProcessId($appNumber); $proId = isset($delegationIds['pro_id']) ? $delegationIds['pro_id'] : $appDelegation->getProcessId($appNumber);
} else { } else {
$proId = $db_spool['pro_id']; $proId = $dbSpool['pro_id'];
} }
$spool->setAppNumber($appNumber); $spool->setAppNumber($appNumber);
@@ -706,6 +780,6 @@ class SpoolRun
$spool->save(); $spool->save();
} }
return $sUID; return $appMsgUid;
} }
} }