Merge branch 'development' of https://github.com/ivandiazwm/opensupports into development

This commit is contained in:
ivan 2017-06-08 17:13:33 -03:00
commit 4d5ff5449d
18 changed files with 174 additions and 45 deletions

View File

@ -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 (
<div className="install-step-5">
<Header title={i18n('STEP_TITLE', {title: i18n('INITIAL_SETTINGS'), current: 5, total: 7})} description={i18n('STEP_4_DESCRIPTION')}/>
{this.renderMessage()}
<Form loading={this.state.loading} onSubmit={this.onSubmit.bind(this)} value={this.state.form} onChange={this.onFormChange.bind(this)}>
<FormField name="title" label={i18n('TITLE')} fieldProps={{size: 'large'}} required/>
<FormField name="allow-attachments" label={i18n('ALLOW_FILE_ATTACHMENTS')} fieldProps={{size: 'large'}} infoMessage={i18n('LEFT_EMPTY_DATABASE')}/>
<FormField name="no-reply-email" label={i18n('NO_REPLY_EMAIL')} fieldProps={{size: 'large'}} required/>
<FormField name="smtp-host" label={i18n('SMTP_SERVER')} fieldProps={{size: 'large'}} required/>
<FormField name="smtp-port" label={i18n('SMTP_PORT')} fieldProps={{size: 'small'}} required/>
<FormField name="smtp-user" label={i18n('SMTP_USER')} fieldProps={{size: 'large'}} required/>
<FormField name="smtp-password" label={i18n('SMTP_PASSWORD')} fieldProps={{size: 'large', password: true}}/>
<Button className="install-step-5__test-connection" size="medium" onClick={this.onTestSMTPClick.bind(this)}>
{i18n('TEST_SMTP_CONNECTION')}
</Button>
<div className="install-step-5__buttons">
<SubmitButton className="install-step-5__next" size="medium" type="secondary">{i18n('NEXT')}</SubmitButton>
<Button className="install-step-5__previous" size="medium" onClick={this.onPreviousClick.bind(this)}>{i18n('PREVIOUS')}</Button>
</div>
</Form>
</div>
);
}
renderMessage() {
let message = null;
if(this.state.error) {
message = (
<Message className="install-step-5__message" type="error">
{i18n('ERROR_UPDATING_SETTINGS')}: {this.state.errorMessage}
</Message>
);
}
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;

View File

@ -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,

View File

@ -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,

View File

@ -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 ')),

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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

View File

@ -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,

View File

@ -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,

View File

@ -102,7 +102,10 @@ class SignUpController extends Controller {
}
$userId = $this->createNewUserAndRetrieveId();
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,

View File

@ -2,8 +2,18 @@
class MailSender {
private $mailOptions = [];
private $mailerInstance;
private static $instance = NULL;
public function __construct() {
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 = [
$mailerInstance->addAddress($this->mailOptions['to']);
$mailerInstance->Subject = $this->mailOptions['subject'];
$mailerInstance->Body = $this->mailOptions['body'];
$mailerInstance->isHTML(true);
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
]
];
}
if ($mailer->smtpConnect()) {
$mailer->send();
}
return $this->mailerInstance;
}
}