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/disable-user-system.php b/server/controllers/system/disable-user-system.php index 8f81ec2e..6256a1f8 100755 --- a/server/controllers/system/disable-user-system.php +++ b/server/controllers/system/disable-user-system.php @@ -63,7 +63,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 cbe644d0..c431fe39 100755 --- a/server/controllers/system/enable-user-system.php +++ b/server/controllers/system/enable-user-system.php @@ -87,7 +87,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/system/init-settings.php b/server/controllers/system/init-settings.php index 1b4efc1b..9ea63761 100755 --- a/server/controllers/system/init-settings.php +++ b/server/controllers/system/init-settings.php @@ -60,18 +60,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://' . $_SERVER['HTTP_HOST'], + 'title' => Controller::request('title') || 'Support Center', + 'url' => Controller::request('url') || ('http://' . $_SERVER['HTTP_HOST']), 'registration' => !!Controller::request('registration'), 'user-system-enabled' => !!Controller::request('user-system-enabled'), 'last-stat-day' => date('YmdHi', strtotime(' -12 day ')), diff --git a/server/controllers/ticket/close.php b/server/controllers/ticket/close.php index d63b63ef..55382d89 100755 --- a/server/controllers/ticket/close.php +++ b/server/controllers/ticket/close.php @@ -92,7 +92,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 d9b1f672..8d08bdf1 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 1a28bb6b..9b6c36df 100755 --- a/server/controllers/ticket/create.php +++ b/server/controllers/ticket/create.php @@ -142,7 +142,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 534a9f91..da0c4cfc 100755 --- a/server/controllers/user/edit-email.php +++ b/server/controllers/user/edit-email.php @@ -45,7 +45,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 8ae1e048..99f3013e 100755 --- a/server/controllers/user/edit-password.php +++ b/server/controllers/user/edit-password.php @@ -49,7 +49,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 f6fcc98d..501ff816 100755 --- a/server/controllers/user/recover-password.php +++ b/server/controllers/user/recover-password.php @@ -86,7 +86,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 c1d90734..28a427e2 100755 --- a/server/controllers/user/send-recover-password.php +++ b/server/controllers/user/send-recover-password.php @@ -71,7 +71,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 ccf639ea..1fcaf2a0 100755 --- a/server/controllers/user/signup.php +++ b/server/controllers/user/signup.php @@ -102,7 +102,10 @@ class SignUpController extends Controller { } $userId = $this->createNewUserAndRetrieveId(); - $this->sendRegistrationMail(); + + if(MailSender::getInstance()->isConnected()) { + $this->sendRegistrationMail(); + } Response::respondSuccess([ 'userId' => $userId, @@ -128,14 +131,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