Skip to content

Commit

Permalink
don't use global variables any more (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Coutadeur committed Apr 2, 2024
1 parent 70eacb6 commit 13958b9
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 139 deletions.
5 changes: 2 additions & 3 deletions src/Ltb/AttributeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,10 @@ public static function ldap_get_first_available_value($ldap, $entry, $attributes
* Get from ldap entry first value corresponding to $mail_attributes (globally configured)
* @param $ldap php_ldap connection object
* @param $entry ldap entry to parse
* @param $mail_attributes array containing mail attributes
* @return mail to use for notification or empty string if not found
*/
public static function ldap_get_mail_for_notification($ldap, $entry) {
# mail_attibutes are set globally in configuration
global $mail_attributes;
public static function ldap_get_mail_for_notification($ldap, $entry, $mail_attributes) {
$mailValue = \Ltb\AttributeValue::ldap_get_first_available_value($ldap, $entry, $mail_attributes);
$mail="";
if ( $mailValue ) {
Expand Down
135 changes: 62 additions & 73 deletions src/Ltb/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,86 +4,75 @@
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

final class Mail {
class Mail {

# Mail functions
# rely on global mail configured with $mail_XXXX variables
public $mailer = null;

# init $mailer from config $mail_XXXX variables
# legacy code to be compliant with self-service-password existing configuration
static function init_mailer()
# Mail functions
function __construct( $mail_priority,
$mail_charset,
$mail_contenttype,
$mail_wordwrap,
$mail_sendmailpath,
$mail_protocol,
$mail_smtp_debug,
$mail_debug_format,
$mail_smtp_host,
$mail_smtp_port,
$mail_smtp_secure,
$mail_smtp_autotls,
$mail_smtp_auth,
$mail_smtp_user,
$mail_smtp_pass,
$mail_smtp_keepalive,
$mail_smtp_options,
$mail_smtp_timeout
)
{

#==============================================================================
# Email Config
#==============================================================================

global $mailer;
global $mail_priority, $mail_charset, $mail_contenttype, $mail_wordwrap, $mail_sendmailpath;
global $mail_protocol, $mail_smtp_debug, $mail_debug_format, $mail_smtp_host, $mail_smtp_port;
global $mail_smtp_secure, $mail_smtp_autotls, $mail_smtp_auth, $mail_smtp_user, $mail_smtp_pass;
global $mail_smtp_keepalive, $mail_smtp_options, $mail_smtp_timeout;

$mailer= new PHPMailer;

$mailer->Priority = $mail_priority;
$mailer->CharSet = $mail_charset;
$mailer->ContentType = $mail_contenttype;
$mailer->WordWrap = $mail_wordwrap;
$mailer->Sendmail = $mail_sendmailpath;
$mailer->Mailer = $mail_protocol;
$mailer->SMTPDebug = $mail_smtp_debug;
$mailer->Debugoutput = $mail_debug_format;
$mailer->Host = $mail_smtp_host;
$mailer->Port = $mail_smtp_port;
$mailer->SMTPSecure = $mail_smtp_secure;
$mailer->SMTPAutoTLS = $mail_smtp_autotls;
$mailer->SMTPAuth = $mail_smtp_auth;
$mailer->Username = $mail_smtp_user;
$mailer->Password = $mail_smtp_pass;
$mailer->SMTPKeepAlive = $mail_smtp_keepalive;
$mailer->SMTPOptions = $mail_smtp_options;
$mailer->Timeout = $mail_smtp_timeout;

return $mailer;
$this->mailer = new PHPMailer;

$this->mailer->Priority = $mail_priority;
$this->mailer->CharSet = $mail_charset;
$this->mailer->ContentType = $mail_contenttype;
$this->mailer->WordWrap = $mail_wordwrap;
$this->mailer->Sendmail = $mail_sendmailpath;
$this->mailer->Mailer = $mail_protocol;
$this->mailer->SMTPDebug = $mail_smtp_debug;
$this->mailer->Debugoutput = $mail_debug_format;
$this->mailer->Host = $mail_smtp_host;
$this->mailer->Port = $mail_smtp_port;
$this->mailer->SMTPSecure = $mail_smtp_secure;
$this->mailer->SMTPAutoTLS = $mail_smtp_autotls;
$this->mailer->SMTPAuth = $mail_smtp_auth;
$this->mailer->Username = $mail_smtp_user;
$this->mailer->Password = $mail_smtp_pass;
$this->mailer->SMTPKeepAlive = $mail_smtp_keepalive;
$this->mailer->SMTPOptions = $mail_smtp_options;
$this->mailer->Timeout = $mail_smtp_timeout;

}

/* @function boolean send_mail_gloabl(PHPMailer $mailer, string $mail, string $mail_from, string $subject, string $body, array $data)
/* @function boolean send_mail(string $mail, string $mail_from, string $subject, string $body, array $data)
* Send a mail, replace strings in body
*
* use global PHPMailer $mailer, create one from mail_XXX configurations if needed.
#
* @param mail Destination
* @param mail_from Sender
* @param subject Subject
* @param body Body
* @param data Data for string replacement
* @return result
*/
static function send_mail_global($mail, $mail_from, $mail_from_name, $subject, $body, $data) {
global $mailer;
if ( ! isset($mailer) )
{
\Ltb\Mail::init_mailer();
}
return \Ltb\Mail::send_mail($mailer, $mail, $mail_from, $mail_from_name, $subject, $body, $data);
}

/* @function boolean send_mail(PHPMailer $mailer, string $mail, string $mail_from, string $subject, string $body, array $data)
* Send a mail, replace strings in body
* @param mailer PHPMailer object
* @param mail Destination or array of destinations.
* @param mail_from Sender
* @param subject Subject
* @param body Body
* @param data Data for string replacement
* @return result
*/
static function send_mail($mailer, $mail, $mail_from, $mail_from_name, $subject, $body, $data) {
public function send_mail($mail, $mail_from, $mail_from_name, $subject, $body, $data) {

$result = false;

if (!is_a($mailer, 'PHPMailer\PHPMailer\PHPMailer')) {
if( $this->mailer == null )
{
error_log("send_mail: Mail object not initialized!");
return $result;
}

if( ! is_a(($this->mailer), 'PHPMailer\PHPMailer\PHPMailer') )
{
error_log("send_mail: PHPMailer object required!");
return $result;
}
Expand All @@ -103,25 +92,25 @@ static function send_mail($mailer, $mail, $mail_from, $mail_from_name, $subject,
}

# if not done addAddress and addReplyTo are cumulated at each call
$mailer->clearAddresses();
$mailer->setFrom($mail_from, $mail_from_name);
$mailer->addReplyTo($mail_from, $mail_from_name);
$this->mailer->clearAddresses();
$this->mailer->setFrom($mail_from, $mail_from_name);
$this->mailer->addReplyTo($mail_from, $mail_from_name);
# support list of mails
if ( is_array($mail) ) {
foreach( $mail as $mailstr ) {
$mailer->addAddress($mailstr);
$this->mailer->addAddress($mailstr);
}
}
else {
$mailer->addAddress($mail);
$this->mailer->addAddress($mail);
}
$mailer->Subject = $subject;
$mailer->Body = $body;
$this->mailer->Subject = $subject;
$this->mailer->Body = $body;

$result = $mailer->send();
$result = $this->mailer->send();

if (!$result) {
error_log("send_mail: ".$mailer->ErrorInfo);
error_log("send_mail: ".$this->mailer->ErrorInfo);
}

return $result;
Expand Down
8 changes: 4 additions & 4 deletions tests/Ltb/AttributeValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function test_ldap_get_mail_for_notification(): void
{

// global variable for ldap_get_mail_for_notification function
$GLOBALS['mail_attributes'] = array("mail");
$mail_attributes = array("mail");

$phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP');
$phpLDAPMock->shouldreceive([
Expand All @@ -59,15 +59,15 @@ public function test_ldap_get_mail_for_notification(): void
]);

# Test ldap_get_mail_for_notification
$mail = Ltb\AttributeValue::ldap_get_mail_for_notification(null, null);
$mail = Ltb\AttributeValue::ldap_get_mail_for_notification(null, null, $mail_attributes);
$this->assertEquals('[email protected]', $mail, "not getting [email protected] as mail for notification");
}

public function test_ldap_get_proxy_for_notification(): void
{

// global variable for ldap_get_mail_for_notification function
$GLOBALS['mail_attributes'] = array("proxyAddresses");
$mail_attributes = array("proxyAddresses");

$phpLDAPMock = Mockery::mock('overload:Ltb\PhpLDAP');
$phpLDAPMock->shouldreceive([
Expand All @@ -80,7 +80,7 @@ public function test_ldap_get_proxy_for_notification(): void
]);

# Test ldap_get_mail_for_notification
$mail = Ltb\AttributeValue::ldap_get_mail_for_notification(null, null);
$mail = Ltb\AttributeValue::ldap_get_mail_for_notification(null, null, $mail_attributes);
$this->assertEquals('[email protected]', $mail, "not getting [email protected] as proxyAddress for notification");
}

Expand Down
Loading

0 comments on commit 13958b9

Please sign in to comment.