From 2c151fd68c6ba43d0ff7351feba0a21cab2bd44e Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 4 Oct 2018 23:53:37 +0200 Subject: [PATCH] E-mailing: now more procedurally wrapped OOP than ever! --- misuzu.php | 2 + public/auth.php | 19 ++++---- src/Application.php | 74 ------------------------------- src/mail.php | 103 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 82 deletions(-) create mode 100644 src/mail.php diff --git a/misuzu.php b/misuzu.php index 0bdbd776..cf5b6237 100644 --- a/misuzu.php +++ b/misuzu.php @@ -35,6 +35,7 @@ require_once 'src/config.php'; require_once 'src/csrf.php'; require_once 'src/general.php'; require_once 'src/git.php'; +require_once 'src/mail.php'; require_once 'src/manage.php'; require_once 'src/news.php'; require_once 'src/perms.php'; @@ -58,6 +59,7 @@ require_once 'src/Users/user.php'; require_once 'src/Users/validation.php'; config_load(MSZ_ROOT . '/config/config.ini'); +mail_prepare(config_get_default([], 'Mail')); $app = new Application; diff --git a/public/auth.php b/public/auth.php index 9e7af21f..8dea54c1 100644 --- a/public/auth.php +++ b/public/auth.php @@ -1,5 +1,4 @@ disableRegistration(); -$preventPasswordReset = !($privateInfo['password_reset'] ?? true); +$preventPasswordReset = ($privateInfo['enabled'] ?? false) && !($privateInfo['password_reset'] ?? true); $authUsername = $isSubmission ? ($_POST['auth']['username'] ?? '') : ($_GET['username'] ?? ''); $authEmail = $isSubmission ? ($_POST['auth']['email'] ?? '') : ($_GET['email'] ?? ''); @@ -222,15 +221,19 @@ Your verification code is: {$verificationCode} If you weren't the person who requested this reset, please send a reply to this e-mail. MSG; - $message = (new Swift_Message('Flashii Password Reset')) - ->setFrom($app->getMailSender()) - ->setTo([$forgotUser['email'] => $forgotUser['username']]) - ->setBody($messageBody); + $message = mail_compose( + [$forgotUser['email'] => $forgotUser['username']], + 'Flashii Password Reset', + $messageBody + ); - Application::mailer()->send($message); + if (!mail_send($message)) { + tpl_var('auth_forgot_error', 'Failed to send reset email, please contact the administrator.'); + break; + } } - header("Location: ?m=reset&username={$forgotUser['user_id']}"); + header("Location: ?m=reset&u={$forgotUser['user_id']}"); break; } diff --git a/src/Application.php b/src/Application.php index 00aa1bc7..7bfd74dc 100644 --- a/src/Application.php +++ b/src/Application.php @@ -23,14 +23,6 @@ final class Application 'mysql-main', ]; - private const MAIL_TRANSPORT = [ - 'null' => Swift_NullTransport::class, - 'smtp' => Swift_SmtpTransport::class, - 'sendmail' => Swift_SendmailTransport::class, - ]; - - private $mailerInstance = null; - private $geoipInstance = null; public function __construct() @@ -87,72 +79,6 @@ final class Application ); } - public function startMailer(): void - { - if (!empty($this->mailerInstance)) { - return; - } - - $method = mb_strtolower(config_get('Mail', 'method')); - - if (!array_key_exists($method, self::MAIL_TRANSPORT)) { - $method = 'null'; - } - - $class = self::MAIL_TRANSPORT[$method]; - $transport = new $class; - - switch ($method) { - case 'sendmail': - $command = config_get('Mail', 'command'); - - if (!empty($command)) { - $transport->setCommand($command); - } - break; - - case 'smtp': - $transport->setHost(config_get_default('', 'Mail', 'host')); - $transport->setPort(intval(config_get_default(25, 'Mail', 'port'))); - - $extra = [ - 'setEncryption' => config_get('Mail', 'encryption'), - 'setUsername' => config_get('Mail', 'username'), - 'setPassword' => config_get('Mail', 'password'), - ]; - - foreach ($extra as $method => $value) { - if (!empty($value)) { - $transport->{$method}($value); - } - } - break; - } - - $this->mailerInstance = new Swift_Mailer($transport); - } - - public function getMailer(): Swift_Mailer - { - if (empty($this->mailerInstance)) { - $this->startMailer(); - } - - return $this->mailerInstance; - } - - public static function mailer(): Swift_Mailer - { - return self::getInstance()->getMailer(); - } - - public function getMailSender(): array - { - return [ - config_get_default('sys@msz.lh', 'Mail', 'sender_email') => config_get_default('Misuzu System', 'Mail', 'sender_name'), - ]; - } - public function startGeoIP(): void { if (!empty($this->geoipInstance)) { diff --git a/src/mail.php b/src/mail.php new file mode 100644 index 00000000..ba273190 --- /dev/null +++ b/src/mail.php @@ -0,0 +1,103 @@ + MSZ_MAIL_NULL, + 'smtp' => MSZ_MAIL_SMTP, + 'sendmail' => MSZ_MAIL_SENDMAIL, +]); + +define('MSZ_MAIL_DEFAULT_SENDER_NAME', 'Misuzu System'); +define('MSZ_MAIL_DEFAULT_SENDER_ADDRESS', 'sys@msz.lh'); + +function mail_prepare(array $options): void +{ + $GLOBALS[MSZ_MAIL_STORE_OPTIONS] = $options; +} + +function mail_init_if_prepared(): bool +{ + return !empty($GLOBALS[MSZ_MAIL_STORE_OBJECT]) || ( + !empty($GLOBALS[MSZ_MAIL_STORE_OPTIONS]) && mail_init($GLOBALS[MSZ_MAIL_STORE_OPTIONS]) + ); +} + +function mail_init(array $options = []): bool +{ + if (!empty($GLOBALS[MSZ_MAIL_STORE_OBJECT])) { + return true; + } + + $GLOBALS[MSZ_MAIL_STORE_OPTIONS] = $options; + $method = $options['method'] ?? ''; + + if (array_key_exists($method, MSZ_MAIL_METHODS)) { + $method = MSZ_MAIL_METHODS[$method]; + } + + if (!in_array($method, MSZ_MAIL_METHODS)) { + return false; + } + + $transport = new $method; + + switch ($method) { + case MSZ_MAIL_SENDMAIL: + if (!empty($options['command'])) { + $transport->setCommand($options['command']); + } + break; + + case MSZ_MAIL_SMTP: + $transport->setHost($options['host'] ?? ''); + $transport->setPort(intval($options['port'] ?? 25)); + + if (!empty($options['encryption'])) { + $transport->setEncryption($options['encryption']); + } + + if (!empty($options['username'])) { + $transport->setUsername($options['username']); + } + + if (!empty($options['password'])) { + $transport->setPassword($options['password']); + } + break; + } + + $GLOBALS[MSZ_MAIL_STORE_OBJECT] = $transport; + return true; +} + +function mail_default_sender(): array +{ + return [ + $GLOBALS[MSZ_MAIL_STORE_OPTIONS]['sender_email'] ?? MSZ_MAIL_DEFAULT_SENDER_ADDRESS => + $GLOBALS[MSZ_MAIL_STORE_OPTIONS]['sender_name'] ?? MSZ_MAIL_DEFAULT_SENDER_NAME + ]; +} + +function mail_send(Swift_Message $mail): int +{ + if (!mail_init_if_prepared()) { + return 0; + } + + return $GLOBALS[MSZ_MAIL_STORE_OBJECT]->send($mail); +} + +function mail_compose( + array $addressees, + string $subject, + string $body +): Swift_Message { + return (new Swift_Message($subject)) + ->setFrom(mail_default_sender()) + ->setTo($addressees) + ->setBody($body); +}