E-mailing: now more procedurally wrapped OOP than ever!
This commit is contained in:
parent
c6018d0a4d
commit
2c151fd68c
4 changed files with 116 additions and 82 deletions
|
@ -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;
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
<?php
|
||||
use Misuzu\Application;
|
||||
use Misuzu\Database;
|
||||
|
||||
$isSubmission = !empty($_POST['auth']) && is_array($_POST['auth']);
|
||||
|
@ -17,7 +16,7 @@ $usernameValidationErrors = [
|
|||
];
|
||||
|
||||
$preventRegistration = $app->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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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)) {
|
||||
|
|
103
src/mail.php
Normal file
103
src/mail.php
Normal file
|
@ -0,0 +1,103 @@
|
|||
<?php
|
||||
define('MSZ_MAIL_STORE_OBJECT', '_msz_mail_swiftmailer');
|
||||
define('MSZ_MAIL_STORE_OPTIONS', '_msz_mail_options');
|
||||
|
||||
define('MSZ_MAIL_NULL', Swift_NullTransport::class);
|
||||
define('MSZ_MAIL_SMTP', Swift_SmtpTransport::class);
|
||||
define('MSZ_MAIL_SENDMAIL', Swift_SendmailTransport::class);
|
||||
define('MSZ_MAIL_METHODS', [
|
||||
'null' => 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);
|
||||
}
|
Loading…
Reference in a new issue