From ec34acc0808410a77c5977e440c5b00253e21d19 Mon Sep 17 00:00:00 2001 From: Ivan Diaz Date: Mon, 5 Jun 2017 11:41:52 -0300 Subject: [PATCH 1/2] Ivan - Avoid verification if smtp is not set. Implement singleton pattern in MailSender class --- .../system/disable-user-system.php | 2 +- .../controllers/system/enable-user-system.php | 2 +- server/controllers/ticket/close.php | 2 +- server/controllers/ticket/comment.php | 2 +- server/controllers/ticket/create.php | 2 +- server/controllers/user/edit-email.php | 2 +- server/controllers/user/edit-password.php | 2 +- server/controllers/user/recover-password.php | 2 +- .../user/send-recover-password.php | 2 +- server/controllers/user/signup.php | 9 ++- server/libs/MailSender.php | 79 +++++++++++++------ 11 files changed, 69 insertions(+), 37 deletions(-) diff --git a/server/controllers/system/disable-user-system.php b/server/controllers/system/disable-user-system.php index 71286148..1e3bb066 100755 --- a/server/controllers/system/disable-user-system.php +++ b/server/controllers/system/disable-user-system.php @@ -62,7 +62,7 @@ class DisableUserSystemController extends Controller { $ticket->store(); } - $mailSender = new MailSender(); + $mailSender = MailSender::getInstance(); $mailSender->setTemplate(MailTemplate::USER_SYSTEM_DISABLED, [ 'to' => $user->email, diff --git a/server/controllers/system/enable-user-system.php b/server/controllers/system/enable-user-system.php index e745c658..37144935 100755 --- a/server/controllers/system/enable-user-system.php +++ b/server/controllers/system/enable-user-system.php @@ -86,7 +86,7 @@ class EnableUserSystemController extends Controller { $userInstance->store(); - $mailSender = new MailSender(); + $mailSender = MailSender::getInstance(); $mailSender->setTemplate(MailTemplate::USER_SYSTEM_ENABLED, [ 'to' => $email, 'name' => $name, diff --git a/server/controllers/ticket/close.php b/server/controllers/ticket/close.php index 6ac853d8..08faeace 100755 --- a/server/controllers/ticket/close.php +++ b/server/controllers/ticket/close.php @@ -91,7 +91,7 @@ class CloseController extends Controller { } private function sendMail() { - $mailSender = new MailSender(); + $mailSender = MailSender::getInstance(); $mailSender->setTemplate(MailTemplate::TICKET_CLOSED, [ 'to' => $this->ticket->author->email, diff --git a/server/controllers/ticket/comment.php b/server/controllers/ticket/comment.php index f536872d..4d5675a5 100755 --- a/server/controllers/ticket/comment.php +++ b/server/controllers/ticket/comment.php @@ -119,7 +119,7 @@ class CommentController extends Controller { } private function sendMail() { - $mailSender = new MailSender(); + $mailSender = MailSender::getInstance(); $mailSender->setTemplate(MailTemplate::TICKET_RESPONDED, [ 'to' => ($this->ticket->author) ? $this->ticket->author->email : $this->ticket->authorEmail, diff --git a/server/controllers/ticket/create.php b/server/controllers/ticket/create.php index b5f2e788..0dd1d600 100755 --- a/server/controllers/ticket/create.php +++ b/server/controllers/ticket/create.php @@ -136,7 +136,7 @@ class CreateController extends Controller { } private function sendMail() { - $mailSender = new MailSender(); + $mailSender = MailSender::getInstance(); $mailSender->setTemplate(MailTemplate::TICKET_CREATED, [ 'to' => $this->email, diff --git a/server/controllers/user/edit-email.php b/server/controllers/user/edit-email.php index d4f2b33d..d5100699 100755 --- a/server/controllers/user/edit-email.php +++ b/server/controllers/user/edit-email.php @@ -44,7 +44,7 @@ class EditEmail extends Controller{ $user->email = $newEmail; $user->store(); - $mailSender = new MailSender(); + $mailSender = MailSender::getInstance(); $mailSender->setTemplate('USER_EMAIL', [ 'to'=>$oldEmail, 'newemail'=>$user->email, diff --git a/server/controllers/user/edit-password.php b/server/controllers/user/edit-password.php index 037cd726..44ae257a 100755 --- a/server/controllers/user/edit-password.php +++ b/server/controllers/user/edit-password.php @@ -48,7 +48,7 @@ class EditPassword extends Controller { $user->password = Hashing::hashPassword($newPassword); $user->store(); - $mailSender = new MailSender(); + $mailSender = MailSender::getInstance(); $mailSender->setTemplate('USER_PASSWORD', [ 'to'=>$user->email, 'name'=>$user->name diff --git a/server/controllers/user/recover-password.php b/server/controllers/user/recover-password.php index 94d3d5f6..ee6ba1b5 100755 --- a/server/controllers/user/recover-password.php +++ b/server/controllers/user/recover-password.php @@ -85,7 +85,7 @@ class RecoverPasswordController extends Controller { } } public function sendMail() { - $mailSender = new MailSender(); + $mailSender = MailSender::getInstance(); $mailSender->setTemplate(MailTemplate::USER_PASSWORD, [ 'to' => $this->user->email, diff --git a/server/controllers/user/send-recover-password.php b/server/controllers/user/send-recover-password.php index 82e75f20..ea283964 100755 --- a/server/controllers/user/send-recover-password.php +++ b/server/controllers/user/send-recover-password.php @@ -70,7 +70,7 @@ class SendRecoverPasswordController extends Controller { } public function sendEmail() { - $mailSender = new MailSender(); + $mailSender = MailSender::getInstance(); $mailSender->setTemplate(MailTemplate::PASSWORD_FORGOT, [ 'to' => $this->user->email, diff --git a/server/controllers/user/signup.php b/server/controllers/user/signup.php index b36b96fe..ea566c5a 100755 --- a/server/controllers/user/signup.php +++ b/server/controllers/user/signup.php @@ -101,7 +101,10 @@ class SignUpController extends Controller { } $userId = $this->createNewUserAndRetrieveId(); - $this->sendRegistrationMail(); + + if(MailSender::getInstance()->isConnected()) { + $this->sendRegistrationMail(); + } Response::respondSuccess([ 'userId' => $userId, @@ -127,14 +130,14 @@ class SignUpController extends Controller { 'tickets' => 0, 'email' => $this->userEmail, 'password' => Hashing::hashPassword($this->userPassword), - 'verificationToken' => $this->verificationToken + 'verificationToken' => (MailSender::getInstance()->isConnected()) ? $this->verificationToken : null ]); return $userInstance->store(); } public function sendRegistrationMail() { - $mailSender = new MailSender(); + $mailSender = MailSender::getInstance(); $mailSender->setTemplate(MailTemplate::USER_SIGNUP, [ 'to' => $this->userEmail, diff --git a/server/libs/MailSender.php b/server/libs/MailSender.php index abe281d5..7eb70cd6 100755 --- a/server/libs/MailSender.php +++ b/server/libs/MailSender.php @@ -2,8 +2,18 @@ class MailSender { private $mailOptions = []; - - public function __construct() { + private $mailerInstance; + private static $instance = NULL; + + public static function getInstance() { + if(MailSender::$instance === NULL) { + MailSender::$instance = new MailSender(); + } + + return MailSender::$instance; + } + + private function __construct() { $this->mailOptions['from'] = Setting::getSetting('no-reply-email')->value; $this->mailOptions['fromName'] = 'OpenSupports'; @@ -21,32 +31,51 @@ class MailSender { } public function send() { - $mailer = new PHPMailer(); + $mailerInstance = $this->getMailerInstance(); - $mailer->From = $this->mailOptions['from']; - $mailer->FromName = $this->mailOptions['fromName']; - $mailer->addAddress($this->mailOptions['to']); - $mailer->Subject = $this->mailOptions['subject']; - $mailer->Body = $this->mailOptions['body']; - $mailer->isHTML(true); + if( !array_key_exists('to', $this->mailOptions) || + !array_key_exists('subject', $this->mailOptions) || + !array_key_exists('body', $this->mailOptions) ) { + throw new Exception('Mail sending data not available'); + } - $mailer->isSMTP(); - $mailer->SMTPAuth = true; - $mailer->Host = $this->mailOptions['smtp-host']; - $mailer->Port = $this->mailOptions['smtp-port']; - $mailer->Username = $this->mailOptions['smtp-user']; - $mailer->Password = $this->mailOptions['smtp-pass']; - $mailer->Timeout = 1000; - $mailer->SMTPOptions = [ - 'ssl' => [ - 'verify_peer' => false, - 'verify_peer_name' => false, - 'allow_self_signed' => true - ] - ]; + $mailerInstance->addAddress($this->mailOptions['to']); + $mailerInstance->Subject = $this->mailOptions['subject']; + $mailerInstance->Body = $this->mailOptions['body']; + $mailerInstance->isHTML(true); - if ($mailer->smtpConnect()) { - $mailer->send(); + if ($this->isConnected()) { + $mailerInstance->send(); } } + + public function isConnected() { + return $this->getMailerInstance()->smtpConnect(); + } + + private function getMailerInstance() { + if(!($this->mailerInstance instanceof PHPMailer)) { + $this->mailerInstance = new PHPMailer(); + + $this->mailerInstance->From = $this->mailOptions['from']; + $this->mailerInstance->FromName = $this->mailOptions['fromName']; + + $this->mailerInstance->isSMTP(); + $this->mailerInstance->SMTPAuth = true; + $this->mailerInstance->Host = $this->mailOptions['smtp-host']; + $this->mailerInstance->Port = $this->mailOptions['smtp-port']; + $this->mailerInstance->Username = $this->mailOptions['smtp-user']; + $this->mailerInstance->Password = $this->mailOptions['smtp-pass']; + $this->mailerInstance->Timeout = 1000; + $this->mailerInstance->SMTPOptions = [ + 'ssl' => [ + 'verify_peer' => false, + 'verify_peer_name' => false, + 'allow_self_signed' => true + ] + ]; + } + + return $this->mailerInstance; + } } \ No newline at end of file From 858add6443a6c2cb13322bf7792fec0c9efaa90c Mon Sep 17 00:00:00 2001 From: Ivan Diaz Date: Wed, 7 Jun 2017 02:29:25 -0300 Subject: [PATCH 2/2] Ivan - Create install step 5 WIP --- .../app/install/install-step-5-settings.js | 97 +++++++++++++++++++ .../app/install/install-step-5-settings.scss | 0 ...tep-5-admin.js => install-step-6-admin.js} | 0 ...5-admin.scss => install-step-6-admin.scss} | 0 ...mpleted.js => install-step-7-completed.js} | 0 ...ted.scss => install-step-7-completed.scss} | 0 server/controllers/system/init-settings.php | 16 +-- 7 files changed, 105 insertions(+), 8 deletions(-) create mode 100644 client/src/app/install/install-step-5-settings.js create mode 100644 client/src/app/install/install-step-5-settings.scss rename client/src/app/install/{install-step-5-admin.js => install-step-6-admin.js} (100%) rename client/src/app/install/{install-step-5-admin.scss => install-step-6-admin.scss} (100%) rename client/src/app/install/{install-step-6-completed.js => install-step-7-completed.js} (100%) rename client/src/app/install/{install-step-6-completed.scss => install-step-7-completed.scss} (100%) diff --git a/client/src/app/install/install-step-5-settings.js b/client/src/app/install/install-step-5-settings.js new file mode 100644 index 00000000..d92cdb84 --- /dev/null +++ b/client/src/app/install/install-step-5-settings.js @@ -0,0 +1,97 @@ +import React from 'react'; + +import history from 'lib-app/history'; +import i18n from 'lib-app/i18n'; +import API from 'lib-app/api-call'; + +import Button from 'core-components/button'; +import Header from 'core-components/header'; +import Form from 'core-components/form'; +import FormField from 'core-components/form-field'; +import SubmitButton from 'core-components/submit-button'; +import Message from 'core-components/message'; + +class InstallStep5Settings extends React.Component { + + state = { + loading: false, + smtpConnection: null, // ad a message + form: {}, + onFormChange: (form) => this.setState({form}), + error: false, + errorMessage: '' + }; + + render() { + return ( +
+
+ {this.renderMessage()} +
+ + + + + + + + +
+ {i18n('NEXT')} + +
+ +
+ ); + } + + renderMessage() { + let message = null; + + if(this.state.error) { + message = ( + + {i18n('ERROR_UPDATING_SETTINGS')}: {this.state.errorMessage} + + ); + } + + return message; + } + + onTestSMTPClick(event) { + event.preventDefault(); + + API.call({ + path: '/system/test-smtp', + data: this.state.form + }).then(() => this.setState({smtpConnection: true})) + .catch(() => this.setState({smtpConnection: false})); + } + + onPreviousClick(event) { + event.preventDefault(); + history.push('/install/step-4'); + } + + onSubmit(form) { + this.setState({ + loading: true + }, () => { + API.call({ + path: '/system/init-settings', + data: form // add step 4 to form + }) + .then(() => history.push('/install/step-6')) + .catch(({message}) => this.setState({ + loading: false, + error: true, + errorMessage: message + })); + }); + } +} + +export default InstallStep5Settings; \ No newline at end of file diff --git a/client/src/app/install/install-step-5-settings.scss b/client/src/app/install/install-step-5-settings.scss new file mode 100644 index 00000000..e69de29b diff --git a/client/src/app/install/install-step-5-admin.js b/client/src/app/install/install-step-6-admin.js similarity index 100% rename from client/src/app/install/install-step-5-admin.js rename to client/src/app/install/install-step-6-admin.js diff --git a/client/src/app/install/install-step-5-admin.scss b/client/src/app/install/install-step-6-admin.scss similarity index 100% rename from client/src/app/install/install-step-5-admin.scss rename to client/src/app/install/install-step-6-admin.scss diff --git a/client/src/app/install/install-step-6-completed.js b/client/src/app/install/install-step-7-completed.js similarity index 100% rename from client/src/app/install/install-step-6-completed.js rename to client/src/app/install/install-step-7-completed.js diff --git a/client/src/app/install/install-step-6-completed.scss b/client/src/app/install/install-step-7-completed.scss similarity index 100% rename from client/src/app/install/install-step-6-completed.scss rename to client/src/app/install/install-step-7-completed.scss diff --git a/server/controllers/system/init-settings.php b/server/controllers/system/init-settings.php index 49bf69c9..636c5ab2 100755 --- a/server/controllers/system/init-settings.php +++ b/server/controllers/system/init-settings.php @@ -36,18 +36,18 @@ class InitSettingsController extends Controller { 'language' => Controller::request('language'), 'recaptcha-public' => '', 'recaptcha-private' => '', - 'no-reply-email' => 'noreply@opensupports.com', - 'smtp-host' => 'localhost', - 'smtp-port' => 7070, - 'smtp-user' => '', - 'smtp-pass' => '', + 'no-reply-email' => Controller::request('no-reply-email') || 'noreply@opensupports.com', + 'smtp-host' => Controller::request('smtp-host') || 'localhost', + 'smtp-port' => Controller::request('smtp-port') || 7070, + 'smtp-user' => Controller::request('smtp-user') || '', + 'smtp-pass' => Controller::request('smtp-pass') || '', 'time-zone' => 0, 'maintenance-mode' => 0, 'layout' => 'boxed', - 'allow-attachments' => 0, + 'allow-attachments' => !!Controller::request('allow-attachments'), 'max-size' => 1024, - 'title' => 'Support Center', - 'url' => 'http://dev3.opensupports.com', + 'title' => Controller::request('title') || 'Support Center', + 'url' => Controller::request('url') || 'http://www.opensupports.com', 'registration' => !!Controller::request('registration'), 'user-system-enabled' => !!Controller::request('user-system-enabled'), 'last-stat-day' => date('YmdHi', strtotime(' -12 day ')),