Files
luos/workflow/engine/classes/SpoolRun.php

903 lines
33 KiB
PHP
Raw Normal View History

2010-12-02 23:34:41 +00:00
<?php
use Illuminate\Support\Facades\Log;
use PHPMailer\PHPMailer\OAuth;
use PHPMailer\PHPMailer\PHPMailer;
2017-08-14 16:13:46 -04:00
use ProcessMaker\Core\System;
use ProcessMaker\Office365OAuth\Office365OAuth;
2017-08-14 16:13:46 -04:00
2010-12-02 23:34:41 +00:00
/**
2012-10-09 13:24:35 -04:00
* @package workflow.engine.ProcessMaker
2017-08-14 10:56:14 -04:00
*/
class SpoolRun
2012-10-09 13:24:35 -04:00
{
2019-03-07 11:45:22 -04:00
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
2012-10-09 13:24:35 -04:00
private $fileData;
private $longMailEreg;
private $mailEreg;
2019-03-07 11:45:22 -04:00
private $spoolId;
public $config;
public $error;
public $status;
2012-10-09 13:24:35 -04:00
/**
* Class constructor - iniatilize default values
*
* @param none
* @return none
*/
2017-08-14 10:56:14 -04:00
public function __construct()
2012-10-09 13:24:35 -04:00
{
2017-08-14 10:56:14 -04:00
$this->config = array();
$this->fileData = array();
2019-03-07 11:45:22 -04:00
$this->spoolId = '';
2012-10-09 13:24:35 -04:00
$this->status = 'pending';
$this->error = '';
2019-03-07 11:45:22 -04:00
$this->exceptionCode['FATAL'] = 1;
$this->exceptionCode['WARNING'] = 2;
$this->exceptionCode['NOTICE'] = 3;
2012-10-09 13:24:35 -04:00
$this->longMailEreg = "/(.*)(<([\w\-\+\.']+@[\w\-_\.]+\.\w{2,5})+>)/";
$this->mailEreg = "/^([\w\-_\+\.']+@[\w\-_\.]+\.\w{2,5}+)$/";
2010-12-02 23:34:41 +00:00
}
/**
2019-03-07 11:45:22 -04:00
* 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;
}
2019-05-07 14:39:05 -04:00
/**
* Set the $spoolId
*
* @param string
*/
public function setSpoolId($v)
{
2019-05-07 14:39:05 -04:00
$this->spoolId = $v;
}
/**
2019-05-07 14:39:05 -04:00
* Get the $spoolId
*
* @return string
*/
public function getSpoolId()
{
2019-05-07 14:39:05 -04:00
return $this->spoolId;
}
2020-02-26 16:23:08 -04:00
/**
* Set the fileData property
*
* @param array $fileData
*/
public function setFileData($fileData)
{
$this->fileData = $fileData;
}
2019-06-27 11:29:48 -04:00
/**
* Get the fileData property
*
* @return array
*/
public function getFileData()
{
return $this->fileData;
}
2012-10-09 13:24:35 -04:00
/**
* get all files into spool in a list
*
* @param none
* @return none
*/
2017-08-14 10:56:14 -04:00
public function getSpoolFilesList()
2012-10-09 13:24:35 -04:00
{
$sql = "SELECT * FROM APP_MESSAGE WHERE APP_MSG_STATUS ='pending'";
2017-08-14 10:56:14 -04:00
$con = Propel::getConnection("workflow");
$stmt = $con->prepareStatement($sql);
2012-10-09 13:24:35 -04:00
$rs = $stmt->executeQuery();
while ($rs->next()) {
2019-03-07 11:45:22 -04:00
$this->spoolId = $rs->getString('APP_MSG_UID');
2017-08-14 10:56:14 -04:00
$this->fileData['subject'] = $rs->getString('APP_MSG_SUBJECT');
$this->fileData['from'] = $rs->getString('APP_MSG_FROM');
$this->fileData['to'] = $rs->getString('APP_MSG_TO');
$this->fileData['body'] = $rs->getString('APP_MSG_BODY');
$this->fileData['date'] = $rs->getString('APP_MSG_DATE');
$this->fileData['cc'] = $rs->getString('APP_MSG_CC');
$this->fileData['bcc'] = $rs->getString('APP_MSG_BCC');
$this->fileData['template'] = $rs->getString('APP_MSG_TEMPLATE');
$this->fileData['attachments'] = array(); //$rs->getString('APP_MSG_ATTACH');
$this->fileData['error'] = $rs->getString('APP_MSG_ERROR');
2012-10-09 13:24:35 -04:00
if ($this->config['MESS_ENGINE'] == 'OPENMAIL') {
if ($this->config['MESS_SERVER'] != '') {
2017-08-14 10:56:14 -04:00
if (($sAux = @gethostbyaddr($this->config['MESS_SERVER']))) {
2012-10-09 13:24:35 -04:00
$this->fileData['domain'] = $sAux;
} else {
$this->fileData['domain'] = $this->config['MESS_SERVER'];
}
} else {
2017-08-14 10:56:14 -04:00
$this->fileData['domain'] = gethostbyaddr('127.0.0.1');
2012-10-09 13:24:35 -04:00
}
}
$this->sendMail();
2010-12-02 23:34:41 +00:00
}
}
2012-10-09 13:24:35 -04:00
/**
* create a msg record for spool
*
* @param array $aData
* @return none
*/
2017-08-14 10:56:14 -04:00
public function create($aData)
2012-10-09 13:24:35 -04:00
{
if (is_array($aData['app_msg_attach'])) {
$attachment = $aData['app_msg_attach'];
} else {
$attachment = @unserialize($aData['app_msg_attach']);
if ($attachment === false) {
$attachment = explode(',', $aData['app_msg_attach']);
}
}
$aData['app_msg_attach'] = serialize($attachment);
$aData['app_msg_show_message'] = (isset($aData['app_msg_show_message'])) ? $aData['app_msg_show_message'] : 1;
2017-08-14 10:56:14 -04:00
$aData["app_msg_error"] = (isset($aData["app_msg_error"])) ? $aData["app_msg_error"] : '';
2019-03-07 11:45:22 -04:00
$sUID = $this->dbInsert($aData);
2012-10-09 13:24:35 -04:00
2017-08-14 10:56:14 -04:00
$aData['app_msg_date'] = isset($aData['app_msg_date']) ? $aData['app_msg_date'] : '';
2012-10-09 13:24:35 -04:00
2017-08-14 10:56:14 -04:00
if (isset($aData['app_msg_status'])) {
$this->status = strtolower($aData['app_msg_status']);
2012-10-09 13:24:35 -04:00
}
2017-08-14 10:56:14 -04:00
$aData["contentTypeIsHtml"] = (isset($aData["contentTypeIsHtml"])) ? $aData["contentTypeIsHtml"] : true;
$this->setData($sUID, $aData["app_msg_subject"], $aData["app_msg_from"], $aData["app_msg_to"], $aData["app_msg_body"], $aData["app_msg_date"], $aData["app_msg_cc"], $aData["app_msg_bcc"], $aData["app_msg_template"], $aData["app_msg_attach"], $aData["contentTypeIsHtml"], $aData["app_msg_error"]);
2010-12-02 23:34:41 +00:00
}
2012-10-09 13:24:35 -04:00
/**
* set configuration
*
* @param array $aConfig
* @return none
*/
2017-08-14 10:56:14 -04:00
public function setConfig($aConfig)
2012-10-09 13:24:35 -04:00
{
// Processing password
$passwd = isset($aConfig['MESS_PASSWORD']) ? $aConfig['MESS_PASSWORD'] : '';
$passwdDec = G::decrypt($passwd, 'EMAILENCRYPT');
$auxPass = explode('hash:', $passwdDec);
if (count($auxPass) > 1) {
if (count($auxPass) == 2) {
$passwd = $auxPass[1];
} else {
array_shift($auxPass);
$passwd = implode('', $auxPass);
}
}
$aConfig['MESS_PASSWORD'] = $passwd;
// Validating authorization flag
2017-08-14 10:56:14 -04:00
if (!isset($aConfig['SMTPAuth'])) {
if (isset($aConfig['MESS_RAUTH'])) {
if ($aConfig['MESS_RAUTH'] == false || (is_string($aConfig['MESS_RAUTH']) && $aConfig['MESS_RAUTH'] == 'false')) {
$aConfig['MESS_RAUTH'] = 0;
} else {
$aConfig['MESS_RAUTH'] = 1;
}
} else {
$aConfig['MESS_RAUTH'] = 0;
}
$aConfig['SMTPAuth'] = $aConfig['MESS_RAUTH'];
}
// Validating for old configurations
if (!isset($aConfig['MESS_FROM_NAME'])) {
$aConfig['MESS_FROM_NAME'] = '';
}
if (!isset($aConfig['MESS_FROM_MAIL'])) {
$aConfig['MESS_FROM_MAIL'] = '';
}
2012-10-09 13:24:35 -04:00
$this->config = $aConfig;
2010-12-02 23:34:41 +00:00
}
2012-10-09 13:24:35 -04:00
/**
2019-06-27 11:29:48 -04:00
* Set email parameters
*
2019-06-27 11:29:48 -04:00
* @param string $appMsgUid
* @param string $subject
* @param string $from
* @param string $to
* @param string $body
* @param string $date
* @param string $cc
* @param string $bcc
* @param string $template
* @param array $attachments
* @param bool $contentTypeIsHtml
* @param string $error
*
* @see SpoolRun->create()
* @see SpoolRun->resendEmails()
2012-10-09 13:24:35 -04:00
*/
2019-06-27 11:29:48 -04:00
public function setData($appMsgUid, $subject, $from, $to, $body, $date = '', $cc = '', $bcc = '', $template = '', $attachments = [],
$contentTypeIsHtml = true, $error = '')
2012-10-09 13:24:35 -04:00
{
2019-06-27 11:29:48 -04:00
// Fill "fileData" property
$this->spoolId = $appMsgUid;
$this->fileData['subject'] = $subject;
$this->fileData['from'] = $from;
$this->fileData['to'] = $to;
$this->fileData['body'] = $body;
$this->fileData['date'] = (!empty($date) ? $date : date('Y-m-d H:i:s'));
$this->fileData['cc'] = $cc;
$this->fileData['bcc'] = $bcc;
$this->fileData['template'] = $template;
$this->fileData['attachments'] = $attachments;
$this->fileData["contentTypeIsHtml"] = $contentTypeIsHtml;
$this->fileData["error"] = $error;
// Initialize some values used internally
$this->fileData['envelope_to'] = [];
$this->fileData['envelope_cc'] = [];
$this->fileData['envelope_bcc'] = [];
// Domain validation when the email engine is "OpenMail"
2017-08-14 10:56:14 -04:00
if (array_key_exists('MESS_ENGINE', $this->config)) {
2019-06-27 11:29:48 -04:00
if ($this->config['MESS_ENGINE'] === 'OPENMAIL') {
if (!empty($this->config['MESS_SERVER'])) {
if (($domain = @gethostbyaddr($this->config['MESS_SERVER']))) {
$this->fileData['domain'] = $domain;
} else {
$this->fileData['domain'] = $this->config['MESS_SERVER'];
}
2012-10-09 13:24:35 -04:00
} else {
2017-08-14 10:56:14 -04:00
$this->fileData['domain'] = gethostbyaddr('127.0.0.1');
2012-10-09 13:24:35 -04:00
}
}
}
2010-12-02 23:34:41 +00:00
}
2012-10-09 13:24:35 -04:00
/**
* send mail
*
* @param none
* @return boolean true or exception
*/
2017-08-14 10:56:14 -04:00
public function sendMail()
2012-10-09 13:24:35 -04:00
{
try {
$this->handleFrom();
$this->handleEnvelopeTo();
$this->handleMail();
$this->updateSpoolStatus();
return true;
} catch (Exception $e) {
throw $e;
}
}
2012-10-09 13:24:35 -04:00
/**
* update the status to spool
*
* @param none
* @return none
*/
2017-08-14 10:56:14 -04:00
private function updateSpoolStatus()
2012-10-09 13:24:35 -04:00
{
2019-03-07 11:45:22 -04:00
$oAppMessage = AppMessagePeer::retrieveByPK($this->spoolId);
if (empty($oAppMessage)) {
return;
}
2017-08-14 10:56:14 -04:00
if (is_array($this->fileData['attachments'])) {
$attachment = implode(",", $this->fileData['attachments']);
$oAppMessage->setappMsgAttach($attachment);
2010-12-02 23:34:41 +00:00
}
2018-04-04 09:21:59 -04:00
$oAppMessage->setAppMsgStatus($this->status);
$oAppMessage->setAppMsgStatusId(isset(AppMessage::$app_msg_status_values[$this->status]) ? AppMessage::$app_msg_status_values[$this->status] : 0);
2019-03-07 11:45:22 -04:00
$oAppMessage->setAppMsgSendDate(date('Y-m-d H:i:s'));
2012-10-09 13:24:35 -04:00
$oAppMessage->save();
2010-12-02 23:34:41 +00:00
}
2019-03-07 11:45:22 -04:00
/**
* 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();
$message = $msgError;
$context = [
"action" => "Send email",
"appMsgUid" => $this->getAppMsgUid(),
"appUid" => $this->getAppUid()
];
Log::channel(':SendEmail')->error($message, Bootstrap::context($context));
2019-03-07 11:45:22 -04:00
}
2012-10-09 13:24:35 -04:00
/**
* handle the email that was set in "TO" parameter
*
* @param none
* @return boolean true or exception
*/
2017-08-14 10:56:14 -04:00
private function handleFrom()
2012-10-09 13:24:35 -04:00
{
$eregA = "/^'.*@.*$/";
2017-08-14 10:56:14 -04:00
if (strpos($this->fileData['from'], '<') !== false) {
2012-10-09 13:24:35 -04:00
//to validate complex email address i.e. Erik A. O <erik@colosa.com>
2017-08-14 10:56:14 -04:00
$ereg = (preg_match($eregA, $this->fileData["from"])) ? $this->longMailEreg : "/^(.*)(<(.*)>)$/";
preg_match($ereg, $this->fileData["from"], $matches);
2017-08-14 10:56:14 -04:00
if (isset($matches[1]) && $matches[1] != '') {
2012-10-09 13:24:35 -04:00
//drop the " characters if they exist
2017-08-14 10:56:14 -04:00
$this->fileData['from_name'] = trim(str_replace('"', '', $matches[1]));
2012-10-09 13:24:35 -04:00
} else {
//if the from name was not set
$this->fileData['from_name'] = '';
}
2017-08-14 10:56:14 -04:00
if (!isset($matches[3])) {
2019-03-07 11:45:22 -04:00
throw new Exception('Invalid email address in FROM parameter (' . $this->fileData['from'] . ')', $this->exceptionCode['WARNING']);
2012-10-09 13:24:35 -04:00
}
2017-08-14 10:56:14 -04:00
$this->fileData['from_email'] = trim($matches[3]);
2012-10-09 13:24:35 -04:00
} else {
//to validate simple email address i.e. erik@colosa.com
2017-08-14 10:56:14 -04:00
$ereg = (preg_match($eregA, $this->fileData["from"])) ? $this->mailEreg : "/^(.*)$/";
preg_match($ereg, $this->fileData["from"], $matches);
2012-10-09 13:24:35 -04:00
2017-08-14 10:56:14 -04:00
if (!isset($matches[0])) {
2019-03-07 11:45:22 -04:00
throw new Exception('Invalid email address in FROM parameter (' . $this->fileData['from'] . ')', $this->exceptionCode['WARNING']);
2012-10-09 13:24:35 -04:00
}
$this->fileData['from_name'] = '';
$this->fileData['from_email'] = $matches[0];
}
// Set reply to
2017-08-14 10:56:14 -04:00
preg_match($this->longMailEreg, $this->fileData['from_name'], $matches);
if (isset($matches[3])) {
$this->fileData['reply_to'] = $matches[3];
$this->fileData['reply_to_name'] = isset($matches[1]) ? $matches[1] : $this->fileData['from_name'];
} else {
2017-08-14 10:56:14 -04:00
preg_match($this->mailEreg, $this->fileData['from_name'], $matches);
if (isset($matches[1])) {
$this->fileData['reply_to'] = $matches[1];
$this->fileData['reply_to_name'] = '';
} else {
$this->fileData['reply_to'] = '';
$this->fileData['reply_to_name'] = '';
}
}
2012-10-09 13:24:35 -04:00
}
2012-10-09 13:24:35 -04:00
/**
2019-03-26 10:59:00 -04:00
* Handle all recipients to compose the mail
2012-10-09 13:24:35 -04:00
*
2019-03-26 10:59:00 -04:00
* @return void
*
* @see SpoolRun::sendMail()
2012-10-09 13:24:35 -04:00
*/
2017-08-14 10:56:14 -04:00
private function handleEnvelopeTo()
2012-10-09 13:24:35 -04:00
{
2019-03-26 10:59:00 -04:00
$hold = [];
$holdcc = [];
$holdbcc = [];
2017-08-14 10:56:14 -04:00
$text = trim($this->fileData['to']);
2012-10-09 13:24:35 -04:00
$textcc = '';
$textbcc = '';
2017-08-14 10:56:14 -04:00
if (isset($this->fileData['cc']) && trim($this->fileData['cc']) != '') {
$textcc = trim($this->fileData['cc']);
2012-10-09 13:24:35 -04:00
}
2017-08-14 10:56:14 -04:00
if (isset($this->fileData['bcc']) && trim($this->fileData['bcc']) != '') {
$textbcc = trim($this->fileData['bcc']);
}
2017-08-14 10:56:14 -04:00
if (false !== (strpos($text, ','))) {
$hold = explode(',', $text);
2012-10-09 13:24:35 -04:00
foreach ($hold as $val) {
2017-08-14 10:56:14 -04:00
if (strlen($val) > 0) {
2012-10-09 13:24:35 -04:00
$this->fileData['envelope_to'][] = "$val";
}
}
2019-03-26 10:59:00 -04:00
} elseif ($text != '') {
2012-10-09 13:24:35 -04:00
$this->fileData['envelope_to'][] = "$text";
} else {
2019-03-26 10:59:00 -04:00
$this->fileData['envelope_to'] = [];
}
if (empty($this->fileData['envelope_to'])){
$this->updateSpoolError('Invalid address: ' . $text);
2012-10-09 13:24:35 -04:00
}
//CC
2017-08-14 10:56:14 -04:00
if (false !== (strpos($textcc, ','))) {
$holdcc = explode(',', $textcc);
2012-10-09 13:24:35 -04:00
foreach ($holdcc as $valcc) {
2017-08-14 10:56:14 -04:00
if (strlen($valcc) > 0) {
2012-10-09 13:24:35 -04:00
$this->fileData['envelope_cc'][] = "$valcc";
}
2010-12-02 23:34:41 +00:00
}
} elseif ($textcc != '') {
2012-10-09 13:24:35 -04:00
$this->fileData['envelope_cc'][] = "$textcc";
} else {
2019-03-26 10:59:00 -04:00
$this->fileData['envelope_cc'] = [];
2012-10-09 13:24:35 -04:00
}
//BCC
2017-08-14 10:56:14 -04:00
if (false !== (strpos($textbcc, ','))) {
$holdbcc = explode(',', $textbcc);
2012-10-09 13:24:35 -04:00
foreach ($holdbcc as $valbcc) {
2017-08-14 10:56:14 -04:00
if (strlen($valbcc) > 0) {
2012-10-09 13:24:35 -04:00
$this->fileData['envelope_bcc'][] = "$valbcc";
}
}
} elseif ($textbcc != '') {
2012-10-09 13:24:35 -04:00
$this->fileData['envelope_bcc'][] = "$textbcc";
} else {
2019-03-26 10:59:00 -04:00
$this->fileData['envelope_bcc'] = [];
2012-10-09 13:24:35 -04:00
}
}
/**
2019-03-07 11:45:22 -04:00
* Handle and compose the email content and parameters
2012-10-09 13:24:35 -04:00
*
2019-03-07 11:45:22 -04:00
* @return void
*
* @throws Exception
*
* @see SpoolRun::sendMail()
2012-10-09 13:24:35 -04:00
*/
2017-08-14 10:56:14 -04:00
private function handleMail()
2012-10-09 13:24:35 -04:00
{
2017-08-14 10:56:14 -04:00
if (count($this->fileData['envelope_to']) > 0) {
if (array_key_exists('MESS_ENGINE', $this->config)) {
switch ($this->config['MESS_ENGINE']) {
case 'MAIL':
case 'PHPMAILER':
2018-11-29 12:10:35 -04:00
case 'IMAP':
2019-11-15 10:41:38 -04:00
case 'GMAILAPI':
case 'OFFICE365API':
$phpMailer = new PHPMailer();
switch ($this->config['MESS_ENGINE']) {
case 'MAIL':
2019-03-07 11:45:22 -04:00
$phpMailer->Mailer = 'mail';
break;
2018-11-29 12:10:35 -04:00
case 'IMAP':
case 'PHPMAILER':
2019-03-07 11:45:22 -04:00
$phpMailer->Mailer = 'smtp';
break;
2019-11-15 10:41:38 -04:00
case 'GMAILAPI':
case 'OFFICE365API':
2019-11-15 10:41:38 -04:00
$phpMailer->AuthType = 'XOAUTH2';
$phpMailer->isSMTP();
break;
}
2017-08-14 10:56:14 -04:00
2019-03-07 11:45:22 -04:00
$phpMailer->SMTPAuth = (isset($this->config['SMTPAuth']) ? $this->config['SMTPAuth'] : '');
2017-08-14 10:56:14 -04:00
switch ($this->config['MESS_ENGINE']) {
case 'MAIL':
break;
2018-11-29 12:10:35 -04:00
case 'IMAP':
case 'PHPMAILER':
2019-11-15 10:41:38 -04:00
case 'GMAILAPI':
case 'OFFICE365API':
//Posible Options for SMTPSecure are: "", "ssl" or "tls"
2017-08-14 10:56:14 -04:00
if (isset($this->config['SMTPSecure']) && preg_match('/^(ssl|tls)$/', $this->config['SMTPSecure'])) {
2019-03-07 11:45:22 -04:00
$phpMailer->SMTPSecure = $this->config['SMTPSecure'];
}
break;
2017-08-14 10:56:14 -04:00
}
2019-03-07 11:45:22 -04:00
try {
$systemConfiguration = System::getSystemConfiguration();
$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'];
if (!in_array($this->config['MESS_ENGINE'], ['GMAILAPI', 'OFFICE365API'])) {
2019-11-15 10:41:38 -04:00
$phpMailer->Username = $this->config['MESS_ACCOUNT'];
$phpMailer->Password = $this->config['MESS_PASSWORD'];
} else {
// Define initial options for the provider
$options = [
'clientId' => $this->config['OAUTH_CLIENT_ID'],
'clientSecret' => $this->config['OAUTH_CLIENT_SECRET'],
'accessType' => 'offline'
];
// Get provider
switch ($this->config['MESS_ENGINE']) {
case 'GMAILAPI':
$providerClass = '\League\OAuth2\Client\Provider\Google';
break;
case 'OFFICE365API':
$providerClass = '\Stevenmaguire\OAuth2\Client\Provider\Microsoft';
$options['urlAuthorize'] = Office365OAuth::URL_AUTHORIZE;
$options['urlAccessToken'] = Office365OAuth::URL_ACCESS_TOKEN;
break;
default:
throw new Exception('Only Google and Microsoft OAuth2 providers are currently supported.');
break;
}
$provider = new $providerClass($options);
// Set OAuth to use
$phpMailer->setOAuth(
new OAuth(
[
'provider' => $provider,
'clientId' => $this->config['OAUTH_CLIENT_ID'],
'clientSecret' => $this->config['OAUTH_CLIENT_SECRET'],
'refreshToken' => $this->config['OAUTH_REFRESH_TOKEN'],
'userName' => $this->config['MESS_ACCOUNT']
]
)
);
2019-11-15 10:41:38 -04:00
}
2019-03-07 11:45:22 -04:00
//From
$phpMailer->SetFrom($this->fileData['from_email'], utf8_decode($this->fileData['from_name']));
//Reply to
if (isset($this->fileData['reply_to'])) {
if ($this->fileData['reply_to'] != '') {
$phpMailer->AddReplyTo($this->fileData['reply_to'], $this->fileData['reply_to_name']);
}
2012-10-09 13:24:35 -04:00
}
2019-03-07 11:45:22 -04:00
//Subject
$msSubject = $this->fileData['subject'];
if (!(mb_detect_encoding($msSubject, "UTF-8") == "UTF-8")) {
$msSubject = utf8_encode($msSubject);
}
2019-03-07 11:45:22 -04:00
$phpMailer->Subject = $msSubject;
//Body
$msBody = $this->fileData['body'];
if (!(mb_detect_encoding($msBody, "UTF-8") == "UTF-8")) {
$msBody = utf8_encode($msBody);
}
$phpMailer->Body = $msBody;
//Attachments
$attachment = $this->fileData['attachments'];
if (is_string($attachment)) {
$attachment = @unserialize($attachment);
2019-03-07 11:45:22 -04:00
}
if (is_array($attachment)) {
foreach ($attachment as $key => $fileAttach) {
if (file_exists($fileAttach)) {
$phpMailer->AddAttachment($fileAttach, is_int($key) ? '' : $key);
}
}
}
//To
2019-03-26 10:59:00 -04:00
foreach ($this->fileData['envelope_to'] as $email) {
if (strpos($email, '<') !== false) {
preg_match($this->longMailEreg, $email, $matches);
$toAddress = '';
if (!empty($matches[3])) {
$toAddress = trim($matches[3]);
}
$toName = '';
if (!empty($matches[1])) {
$toName = trim($matches[1]);
}
if (!empty($toAddress)) {
$phpMailer->AddAddress($toAddress, $toName);
} else {
throw new Exception('Invalid address: ' . $email);
}
2019-03-07 11:45:22 -04:00
} else {
2019-03-26 10:59:00 -04:00
$phpMailer->AddAddress($email);
2019-03-07 11:45:22 -04:00
}
}
//CC
2019-03-26 10:59:00 -04:00
foreach ($this->fileData['envelope_cc'] as $email) {
if (strpos($email, '<') !== false) {
preg_match($this->longMailEreg, $email, $matches);
$ccAddress = '';
if (!empty($matches[3])) {
$ccAddress = trim($matches[3]);
}
$ccName = '';
if (!empty($matches[1])) {
$ccName = trim($matches[1]);
}
if (!empty($ccAddress)) {
$phpMailer->AddCC($ccAddress, $ccName);
} else {
throw new Exception('Invalid address: ' . $email);
}
2019-03-07 11:45:22 -04:00
} else {
2019-03-26 10:59:00 -04:00
$phpMailer->AddCC($email);
2019-03-07 11:45:22 -04:00
}
}
//BCC
2019-03-26 10:59:00 -04:00
foreach ($this->fileData['envelope_bcc'] as $email) {
if (strpos($email, '<') !== false) {
preg_match($this->longMailEreg, $email, $matches);
$bccAddress = '';
if (!empty($matches[3])) {
$bccAddress = trim($matches[3]);
}
$bccName = '';
if (!empty($matches[1])) {
$bccName = trim($matches[1]);
}
if (!empty($bccAddress)) {
$phpMailer->AddBCC($bccAddress, $bccName);
} else {
throw new Exception('Invalid address: ' . $email);
}
2019-03-07 11:45:22 -04:00
} else {
2019-03-26 10:59:00 -04:00
$phpMailer->AddBCC($email);
2019-03-07 11:45:22 -04:00
}
}
//IsHtml
$phpMailer->IsHTML($this->fileData["contentTypeIsHtml"]);
2017-08-14 10:56:14 -04:00
2019-03-07 11:45:22 -04:00
if ($this->config['MESS_ENGINE'] == 'MAIL') {
$phpMailer->WordWrap = 300;
}
2017-08-14 10:56:14 -04:00
2019-03-07 11:45:22 -04:00
if ($phpMailer->Send()) {
$this->error = '';
$this->status = 'sent';
} else {
2019-03-07 11:45:22 -04:00
$this->error = $phpMailer->ErrorInfo;
$this->status = 'failed';
$this->updateSpoolError($this->error);
}
2019-03-07 11:45:22 -04:00
} catch (Exception $error) {
$this->updateSpoolError($error->getMessage());
2012-10-09 13:24:35 -04:00
}
break;
}
}
2012-10-09 13:24:35 -04:00
}
2010-12-02 23:34:41 +00:00
}
/**
2018-04-04 09:21:59 -04:00
* Try to resend the emails from spool
2012-10-09 13:24:35 -04:00
*
* @param string $dateResend
2018-04-04 09:21:59 -04:00
* @param integer $cron
*
* @return none or exception
*/
2017-08-14 10:56:14 -04:00
public function resendEmails($dateResend = null, $cron = 0)
{
2018-04-04 09:21:59 -04:00
$configuration = System::getEmailConfiguration();
2018-04-04 09:21:59 -04:00
if (!isset($configuration["MESS_ENABLED"])) {
$configuration["MESS_ENABLED"] = '0';
}
2018-04-04 09:21:59 -04:00
if ($configuration["MESS_ENABLED"] == "1") {
2017-08-14 10:56:14 -04:00
require_once("classes/model/AppMessage.php");
2018-04-04 09:21:59 -04:00
$this->setConfig($configuration);
2017-08-14 10:56:14 -04:00
$criteria = new Criteria("workflow");
2018-04-04 09:21:59 -04:00
$criteria->add(AppMessagePeer::APP_MSG_STATUS_ID, AppMessage::MESSAGE_STATUS_PENDING, Criteria::EQUAL);
if ($dateResend != null) {
2017-08-14 10:56:14 -04:00
$criteria->add(AppMessagePeer::APP_MSG_DATE, $dateResend, Criteria::GREATER_EQUAL);
}
2017-08-14 10:56:14 -04:00
$rsCriteria = AppMessagePeer::doSelectRS($criteria);
$rsCriteria->setFetchmode(ResultSet::FETCHMODE_ASSOC);
while ($rsCriteria->next()) {
if ($cron == 1) {
2017-08-14 10:56:14 -04:00
$arrayCron = unserialize(trim(@file_get_contents(PATH_DATA . "cron")));
$arrayCron["processcTimeStart"] = time();
2017-08-14 10:56:14 -04:00
@file_put_contents(PATH_DATA . "cron", serialize($arrayCron));
}
$row = $rsCriteria->getRow();
try {
2018-04-04 09:21:59 -04:00
$from = G::buildFrom($configuration, $row["APP_MSG_FROM"]);
$this->setData(
$row["APP_MSG_UID"],
$row["APP_MSG_SUBJECT"],
$from,
$row["APP_MSG_TO"],
$row["APP_MSG_BODY"],
date("Y-m-d H:i:s"),
$row["APP_MSG_CC"],
$row["APP_MSG_BCC"],
$row["APP_MSG_TEMPLATE"],
$row["APP_MSG_ATTACH"]
);
$this->sendMail();
} catch (Exception $e) {
2018-04-04 09:21:59 -04:00
$strAux = "Spool::resendEmails(): Using " . $configuration["MESS_ENGINE"] . " for APP_MGS_UID=" . $row["APP_MSG_UID"] . " -> With message: " . $e->getMessage();
2019-03-07 11:45:22 -04:00
if ($e->getCode() == $this->exceptionCode["WARNING"]) {
array_push($this->warnings, $strAux);
continue;
} else {
2017-08-14 10:56:14 -04:00
error_log('<400> ' . $strAux);
2016-07-15 13:32:39 -04:00
continue;
}
}
}
2010-12-02 23:34:41 +00:00
}
}
/**
* gets all warnings
2012-10-09 13:24:35 -04:00
*
* @param none
* @return string $this->aWarnings
*/
2017-08-14 10:56:14 -04:00
public function getWarnings()
{
2019-03-07 11:45:22 -04:00
if (sizeof($this->warnings) != 0) {
return $this->warnings;
}
return false;
2010-12-02 23:34:41 +00:00
}
/**
2019-03-07 11:45:22 -04:00
* Insert the record in the AppMessage
*
* @param array $dbSpool
*
* @return string
*
2019-03-07 11:45:22 -04:00
* @see SpoolRun::create()
*/
2019-03-07 11:45:22 -04:00
public function dbInsert($dbSpool)
{
2019-03-07 11:45:22 -04:00
$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();
2019-03-07 11:45:22 -04:00
$spool->setAppMsgUid($appMsgUid);
$spool->setMsgUid($dbSpool['msg_uid']);
$spool->setAppUid($dbSpool['app_uid']);
$spool->setDelIndex($dbSpool['del_index']);
$spool->setAppMsgType($dbSpool['app_msg_type']);
$spool->setAppMsgTypeId(isset(AppMessage::$app_msg_type_values[$dbSpool['app_msg_type']]) ? AppMessage::$app_msg_type_values[$dbSpool['app_msg_type']] : 0);
$spool->setAppMsgSubject($dbSpool['app_msg_subject']);
$spool->setAppMsgFrom($dbSpool['app_msg_from']);
$spool->setAppMsgTo($dbSpool['app_msg_to']);
$spool->setAppMsgBody($dbSpool['app_msg_body']);
2017-08-14 10:56:14 -04:00
$spool->setAppMsgDate(date('Y-m-d H:i:s'));
2019-03-07 11:45:22 -04:00
$spool->setAppMsgCc($dbSpool['app_msg_cc']);
$spool->setAppMsgBcc($dbSpool['app_msg_bcc']);
$spool->setappMsgAttach($dbSpool['app_msg_attach']);
$spool->setAppMsgTemplate($dbSpool['app_msg_template']);
$spool->setAppMsgStatus($dbSpool['app_msg_status']);
$spool->setAppMsgStatusId(isset(AppMessage::$app_msg_status_values[$dbSpool['app_msg_status']]) ? AppMessage::$app_msg_status_values[$dbSpool['app_msg_status']] : 0);
2018-04-04 09:21:59 -04:00
$spool->setAppMsgSendDate(date('Y-m-d H:i:s'));
2019-03-07 11:45:22 -04:00
$spool->setAppMsgShowMessage($dbSpool['app_msg_show_message']);
$spool->setAppMsgError($dbSpool['app_msg_error']);
2017-08-14 10:56:14 -04:00
2018-04-04 09:21:59 -04:00
$appDelegation = new AppDelegation();
2019-03-07 11:45:22 -04:00
if (empty($dbSpool['app_number'])) {
$delegationIds = $appDelegation->getColumnIds($dbSpool['app_uid'], $dbSpool['del_index']);
2017-11-06 14:27:06 -04:00
if (is_array($delegationIds) && count($delegationIds) > 0) {
$delegationIds = array_change_key_case($delegationIds);
$appNumber = $delegationIds['app_number'];
} else {
//The notification is not related to case
$appNumber = 0;
}
} else {
2019-03-07 11:45:22 -04:00
$appNumber = $dbSpool['app_number'];
2017-11-06 14:27:06 -04:00
}
2019-03-07 11:45:22 -04:00
if (empty($dbSpool['tas_id'])) {
2017-11-06 14:27:06 -04:00
$tasId = isset($delegationIds['tas_id']) ? $delegationIds['tas_id'] : 0;
} else {
2019-03-07 11:45:22 -04:00
$tasId = $dbSpool['tas_id'];
2017-11-06 14:27:06 -04:00
}
2019-03-07 11:45:22 -04:00
if (empty($dbSpool['pro_id'])) {
2018-04-04 09:21:59 -04:00
$proId = isset($delegationIds['pro_id']) ? $delegationIds['pro_id'] : $appDelegation->getProcessId($appNumber);
} else {
2019-03-07 11:45:22 -04:00
$proId = $dbSpool['pro_id'];
2018-04-04 09:21:59 -04:00
}
2017-11-06 14:27:06 -04:00
$spool->setAppNumber($appNumber);
$spool->setTasId($tasId);
2018-04-04 09:21:59 -04:00
$spool->setProId($proId);
2017-08-14 10:56:14 -04:00
if (!$spool->validate()) {
$errors = $spool->getValidationFailures();
$this->status = 'error';
foreach ($errors as $key => $value) {
2017-08-14 10:56:14 -04:00
echo "Validation error - " . $value->getMessage($key) . "\n";
}
} else {
//echo "Saving - validation ok\n";
$this->status = 'success';
$spool->save();
}
2019-03-07 11:45:22 -04:00
return $appMsgUid;
}
2019-06-27 11:29:48 -04:00
/**
* Run the private method "handleEnvelopeTo", this method was created in order to use in the unit tests
*/
public function runHandleEnvelopeTo()
{
$this->handleEnvelopeTo();
}
2010-12-02 23:34:41 +00:00
}