From d431811725455dec60b6fb07714f4625eba8c2f1 Mon Sep 17 00:00:00 2001 From: Pierre Goiffon Date: Wed, 14 Sep 2022 15:33:48 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B04947=20Fix=20Email=20always=20picking=20?= =?UTF-8?q?"production"=20env=20config=20file=20(#331)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Note that the code was duplicated in both Email* impl, this is refactored : LoadConfig and m_oConfig are pulled up in Email, and SetRecipientFrom() calls are also refactored in Email --- core/email.class.inc.php | 44 ++++++++++++++- sources/Core/Email/EmailLaminas.php | 16 ++---- sources/Core/Email/EmailSwiftMailer.php | 75 +++++++++++-------------- 3 files changed, 82 insertions(+), 53 deletions(-) diff --git a/core/email.class.inc.php b/core/email.class.inc.php index 669e79bd5..3bbe3db1f 100644 --- a/core/email.class.inc.php +++ b/core/email.class.inc.php @@ -34,6 +34,13 @@ define ('EMAIL_SEND_ERROR', 2); class EMail implements iEMail { + /** + * @see self::LoadConfig() + * @var Config + * @since 2.7.7 3.0.2 3.1.0 N°3169 N°5102 Move attribute to children classes + * @since 2.7.8 3.0.3 3.1.0 N°4947 pull up the attribute back to the Email class as config init is done here + */ + protected static $m_oConfig = null; protected $oMailer; // Serialization formats @@ -46,6 +53,42 @@ class EMail implements iEMail $this->oMailer = EmailFactory::GetMailer(); } + /** + * Sets {@see m_oConfig} if current attribute is null + * + * @returns \Config the current {@see m_oConfig} value + * @throws \ConfigException + * @throws \CoreException + * + * @uses utils::GetConfig() + * + * @since 2.7.7 3.0.2 3.1.0 N°3169 N°5102 Move method to children classes + * @since 2.7.8 3.0.3 3.1.0 N°4947 Pull up to the parent class, and remove `$sConfigFile` param + */ + public function LoadConfig() + { + if (is_null(static::$m_oConfig)) { + static::$m_oConfig = utils::GetConfig(); + } + + return static::$m_oConfig; + } + + /** + * @return void + * @throws \ConfigException + * @throws \CoreException + * @since 2.7.>8 3.0.3 3.1.0 N°4947 Method creation, to factorize same code in children classes + */ + protected function InitRecipientFrom() + { + $oConfig = $this->LoadConfig(); + $this->SetRecipientFrom( + $oConfig->Get('email_default_sender_address'), + $oConfig->Get('email_default_sender_label') + ); + } + /** * Custom serialization method * No longer use the brute force "serialize" method since @@ -146,5 +189,4 @@ class EMail implements iEMail { $this->oMailer->SetRecipientReplyTo($sAddress); } - } \ No newline at end of file diff --git a/sources/Core/Email/EmailLaminas.php b/sources/Core/Email/EmailLaminas.php index 53031d383..eadbf984f 100644 --- a/sources/Core/Email/EmailLaminas.php +++ b/sources/Core/Email/EmailLaminas.php @@ -45,25 +45,21 @@ class EMailLaminas extends Email // Did not work with attachements since their binary representation cannot be stored as a valid UTF-8 string const FORMAT_V2 = 2; // New format, only the raw data are serialized (base64 encoded if needed) - protected static $m_oConfig = null; protected $m_aData; // For storing data to serialize - public static function LoadConfig($sConfigFile = ITOP_DEFAULT_CONFIG_FILE) - { - if (is_null(self::$m_oConfig)) { - self::$m_oConfig = new Config($sConfigFile); - } - } - protected $m_oMessage; - /** @noinspection PhpMissingParentConstructorInspection */ + /** + * @noinspection PhpMissingParentConstructorInspection + * @noinspection MagicMethodsValidityInspection + */ public function __construct() { $this->m_aData = array(); $this->m_oMessage = new Message(); $this->m_oMessage->setEncoding('UTF-8'); - $this->SetRecipientFrom(MetaModel::GetConfig()->Get('email_default_sender_address'), MetaModel::GetConfig()->Get('email_default_sender_label')); + + $this->InitRecipientFrom(); } /** diff --git a/sources/Core/Email/EmailSwiftMailer.php b/sources/Core/Email/EmailSwiftMailer.php index 08edf28d7..01d8b5d24 100644 --- a/sources/Core/Email/EmailSwiftMailer.php +++ b/sources/Core/Email/EmailSwiftMailer.php @@ -32,25 +32,20 @@ Swift_Preferences::getInstance()->setCharset('UTF-8'); class EmailSwiftMailer extends EMail { - protected static $m_oConfig = null; protected $m_aData; // For storing data to serialize - public function LoadConfig($sConfigFile = ITOP_DEFAULT_CONFIG_FILE) - { - if (is_null(self::$m_oConfig)) - { - self::$m_oConfig = new Config($sConfigFile); - } - } - protected $m_oMessage; - /** @noinspection PhpMissingParentConstructorInspection */ + /** + * @noinspection PhpMissingParentConstructorInspection + * @noinspection MagicMethodsValidityInspection + */ public function __construct() { $this->m_aData = array(); $this->m_oMessage = new Swift_Message(); - $this->SetRecipientFrom(MetaModel::GetConfig()->Get('email_default_sender_address'), MetaModel::GetConfig()->Get('email_default_sender_label')); + + $this->InitRecipientFrom(); } /** @@ -63,7 +58,7 @@ class EmailSwiftMailer extends EMail { return serialize($this->m_aData); } - + /** * Custom de-serialization method * @param string $sSerializedMessage The serialized representation of the message @@ -155,39 +150,35 @@ class EmailSwiftMailer extends EMail { // If the body of the message is in HTML, embed all images based on attachments $this->EmbedInlineImages(); - - $this->LoadConfig(); - $sTransport = self::$m_oConfig->Get('email_transport'); - switch ($sTransport) - { - case 'SMTP': - $sHost = self::$m_oConfig->Get('email_transport_smtp.host'); - $sPort = self::$m_oConfig->Get('email_transport_smtp.port'); - $sEncryption = self::$m_oConfig->Get('email_transport_smtp.encryption'); - $sUserName = self::$m_oConfig->Get('email_transport_smtp.username'); - $sPassword = self::$m_oConfig->Get('email_transport_smtp.password'); + $sTransport = static::$m_oConfig->Get('email_transport'); + switch ($sTransport) { + case 'SMTP': + $sHost = static::$m_oConfig->Get('email_transport_smtp.host'); + $sPort = static::$m_oConfig->Get('email_transport_smtp.port'); + $sEncryption = static::$m_oConfig->Get('email_transport_smtp.encryption'); + $sUserName = static::$m_oConfig->Get('email_transport_smtp.username'); + $sPassword = static::$m_oConfig->Get('email_transport_smtp.password'); - $oTransport = new Swift_SmtpTransport($sHost, $sPort, $sEncryption); - if (strlen($sUserName) > 0) - { - $oTransport->setUsername($sUserName); - $oTransport->setPassword($sPassword); - } - break; + $oTransport = new Swift_SmtpTransport($sHost, $sPort, $sEncryption); + if (strlen($sUserName) > 0) { + $oTransport->setUsername($sUserName); + $oTransport->setPassword($sPassword); + } + break; - case 'Null': - $oTransport = new Swift_NullTransport(); - break; - - case 'LogFile': - $oTransport = new Swift_LogFileTransport(new Swift_Events_SimpleEventDispatcher()); - $oTransport->setLogFile(APPROOT.'log/mail.log'); - break; - - case 'PHPMail': - default: - $oTransport = new Swift_SendmailTransport(); + case 'Null': + $oTransport = new Swift_NullTransport(); + break; + + case 'LogFile': + $oTransport = new Swift_LogFileTransport(new Swift_Events_SimpleEventDispatcher()); + $oTransport->setLogFile(APPROOT.'log/mail.log'); + break; + + case 'PHPMail': + default: + $oTransport = new Swift_SendmailTransport(); } $oMailer = new Swift_Mailer($oTransport);