From 72cca1208775bb91ffa66b0f6ffff7fe289c0264 Mon Sep 17 00:00:00 2001 From: Jamie Snape Date: Mon, 17 Nov 2014 13:35:27 -0500 Subject: [PATCH] Revise mail module configuration form and add install script --- modules/mail/Notification.php | 24 +++++------ modules/mail/configs/module.ini | 2 - modules/mail/constant/module.php | 44 +++++++++++++++++++ modules/mail/controllers/AdminController.php | 7 ++- modules/mail/database/InstallScript.php | 45 ++++++++++++++++++++ modules/mail/forms/Admin.php | 43 ++++++++++--------- modules/mail/views/admin/index.phtml | 6 +-- 7 files changed, 131 insertions(+), 40 deletions(-) create mode 100644 modules/mail/constant/module.php create mode 100644 modules/mail/database/InstallScript.php diff --git a/modules/mail/Notification.php b/modules/mail/Notification.php index 51566d916..fb8feda3d 100644 --- a/modules/mail/Notification.php +++ b/modules/mail/Notification.php @@ -41,22 +41,22 @@ public function init() */ protected function handleSendMailMessage($params) { - $provider = $this->Setting->getValueByName('provider', $this->moduleName); + $provider = $this->Setting->getValueByName(MAIL_PROVIDER_KEY, $this->moduleName); - if ($provider === 'app_engine') { + if ($provider === MAIL_PROVIDER_APP_ENGINE) { $service = new Midas_Service_AppEngine_Mail(); $transport = new Midas_Mail_Transport_Service($service); - } elseif ($provider === 'send_grid') { - $username = $this->Setting->getValueByName('send_grid_username', $this->moduleName); - $password = $this->Setting->getValueByName('send_grid_password', $this->moduleName); + } elseif ($provider === MAIL_PROVIDER_SEND_GRID) { + $username = $this->Setting->getValueByName(MAIL_SEND_GRID_USERNAME_KEY, $this->moduleName); + $password = $this->Setting->getValueByName(MAIL_SEND_GRID_PASSWORD_KEY, $this->moduleName); $service = new Midas_Service_SendGrid_Mail($username, $password); $transport = new Midas_Mail_Transport_Service($service); - } elseif ($provider = 'smtp') { - $host = $this->Setting->getValueByName('smtp_host', $this->moduleName); - $port = $this->Setting->getValueByName('smtp_port', $this->moduleName); - $ssl = $this->Setting->getValueByName('smtp_use_ssl', $this->moduleName); - $username = $this->Setting->getValueByName('smtp_username', $this->moduleName); - $password = $this->Setting->getValueByName('smtp_password', $this->moduleName); + } elseif ($provider === MAIL_PROVIDER_SMTP) { + $host = $this->Setting->getValueByName(MAIL_SMTP_HOST_KEY, $this->moduleName); + $port = $this->Setting->getValueByName(MAIL_SMTP_PORT_KEY, $this->moduleName); + $ssl = $this->Setting->getValueByName(MAIL_SMTP_USE_SSL_KEY, $this->moduleName); + $username = $this->Setting->getValueByName(MAIL_SMTP_USERNAME_KEY, $this->moduleName); + $password = $this->Setting->getValueByName(MAIL_SMTP_PASSWORD_KEY, $this->moduleName); $config = array(); if (!empty($port)) { @@ -80,7 +80,7 @@ protected function handleSendMailMessage($params) } $mail = new Midas_Mail(); - $mail->setFrom($this->Setting->getValueByName('from_address', $this->moduleName)); + $mail->setFrom($this->Setting->getValueByName(MAIL_FROM_ADDRESS_KEY, $this->moduleName)); if (isset($params['bcc'])) { $mail->addBcc($params['bcc']); diff --git a/modules/mail/configs/module.ini b/modules/mail/configs/module.ini index 37b1faf11..23273d000 100644 --- a/modules/mail/configs/module.ini +++ b/modules/mail/configs/module.ini @@ -4,7 +4,5 @@ fullname = "Mail" description = "Send informational messages using email" category = "Messaging" -dependencies = -conflicts = uuid = b902fded-6820-4dad-8094-e9ef13143e32 version = "1.0.0" diff --git a/modules/mail/constant/module.php b/modules/mail/constant/module.php new file mode 100644 index 000000000..991c28514 --- /dev/null +++ b/modules/mail/constant/module.php @@ -0,0 +1,44 @@ +getValues(); foreach ($values as $key => $value) { - $this->Setting->setConfig($key, $value, $this->moduleName); + if ($value !== null) { + $this->Setting->setConfig($key, $value, $this->moduleName); + } } } @@ -50,7 +52,7 @@ public function indexAction() foreach ($elements as $element) { $name = $element->getName(); - if ($name !== 'submit') { + if ($name !== 'csrf' && $name !== 'submit') { $value = $this->Setting->getValueByName($name, $this->moduleName); if (!is_null($value)) { @@ -61,5 +63,6 @@ public function indexAction() } $this->view->form = $form; + session_start(); } } diff --git a/modules/mail/database/InstallScript.php b/modules/mail/database/InstallScript.php new file mode 100644 index 000000000..217610202 --- /dev/null +++ b/modules/mail/database/InstallScript.php @@ -0,0 +1,45 @@ +setConfig(MAIL_PROVIDER_KEY, MAIL_PROVIDER_DEFAULT_VALUE, $this->moduleName); + $settingModel->setConfig(MAIL_FROM_ADDRESS_KEY, MAIL_FROM_ADDRESS_DEFAULT_VALUE, $this->moduleName); + $settingModel->setConfig(MAIL_ADDRESS_VERIFICATION_KEY, MAIL_ADDRESS_VERIFICATION_DEFAULT_VALUE, $this->moduleName); + $settingModel->setConfig(MAIL_SEND_GRID_USERNAME_KEY, MAIL_SEND_GRID_USERNAME_DEFAULT_VALUE, $this->moduleName); + $settingModel->setConfig(MAIL_SEND_GRID_PASSWORD_KEY, MAIL_SEND_GRID_PASSWORD_DEFAULT_VALUE, $this->moduleName); + $settingModel->setConfig(MAIL_SMTP_HOST_KEY, MAIL_SMTP_HOST_DEFAULT_VALUE, $this->moduleName); + $settingModel->setConfig(MAIL_SMTP_PORT_KEY, MAIL_SMTP_PORT_DEFAULT_VALUE, $this->moduleName); + $settingModel->setConfig(MAIL_SMTP_USE_SSL_KEY, MAIL_SMTP_USE_SSL_DEFAULT_VALUE, $this->moduleName); + $settingModel->setConfig(MAIL_SMTP_USERNAME_KEY, MAIL_SMTP_USERNAME_DEFAULT_VALUE, $this->moduleName); + $settingModel->setConfig(MAIL_SMTP_PASSWORD_KEY, MAIL_SMTP_PASSWORD_DEFAULT_VALUE, $this->moduleName); + } +} diff --git a/modules/mail/forms/Admin.php b/modules/mail/forms/Admin.php index 472af71dc..8bd25addd 100644 --- a/modules/mail/forms/Admin.php +++ b/modules/mail/forms/Admin.php @@ -24,64 +24,65 @@ class Mail_Form_Admin extends Zend_Form /** Initialize this form. */ public function init() { - $this->setName('mail_config'); + $this->setName('mail_admin'); $this->setMethod('POST'); - $provider = new Zend_Form_Element_Select('provider'); + $csrf = new Midas_Form_Element_Hash('csrf'); + $csrf->setSalt('5qzSHzCdNuPfYaT99Jq5WcKe'); + $csrf->setDecorators(array('ViewHelper')); + + $provider = new Zend_Form_Element_Select(MAIL_PROVIDER_KEY); $provider->setLabel('Provider'); $provider->setRequired(true); $provider->addValidator('NotEmpty', true); - - if (class_exists('\google\appengine\api\mail\Message', false)) { - $provider->addMultiOption('app_engine', 'Google App Engine'); - } - $provider->addMultiOptions(array( - 'mail' => 'PHP Mail Function', - 'send_grid' => 'SendGrid Service', - 'smtp' => 'External SMTP Server', + MAIL_PROVIDER_APP_ENGINE => 'Google App Engine', + MAIL_PROVIDER_MAIL => 'PHP Mail Function', + MAIL_PROVIDER_SEND_GRID => 'SendGrid Service', + MAIL_PROVIDER_SMTP => 'External SMTP Server', )); - $fromAddress = new Zend_Form_Element_Text('from_address'); + $fromAddress = new Zend_Form_Element_Text(MAIL_FROM_ADDRESS_KEY); $fromAddress->setLabel('From email address'); $fromAddress->setRequired(true); $fromAddress->addValidator('NotEmpty', true); $fromAddress->addValidator('EmailAddress', true); - $addressVerification = new Zend_Form_Element_Checkbox('address_verification'); + $addressVerification = new Zend_Form_Element_Checkbox(MAIL_ADDRESS_VERIFICATION_KEY); $addressVerification->setLabel('Require email address verification'); $this->addDisplayGroup(array($provider, $fromAddress, $addressVerification), 'global'); - $sendGridUsername = new Zend_Form_Element_Text('send_grid_username'); + $sendGridUsername = new Zend_Form_Element_Text(MAIL_SEND_GRID_USERNAME_KEY); $sendGridUsername->setLabel('SendGrid User Name'); $sendGridUsername->addValidator('NotEmpty', true); - $sendGridPassword = new Zend_Form_Element_Text('send_grid_password'); + $sendGridPassword = new Zend_Form_Element_Text(MAIL_SEND_GRID_PASSWORD_KEY); $sendGridPassword->setLabel('SendGrid Password'); $sendGridPassword->addValidator('NotEmpty', true); $this->addDisplayGroup(array($sendGridUsername, $sendGridPassword), 'send_grid'); - $smtpHost = new Zend_Form_Element_Text('smtp_host'); + $smtpHost = new Zend_Form_Element_Text(MAIL_SMTP_HOST_KEY); $smtpHost->setLabel('Server name'); $smtpHost->addValidator('NotEmpty', true); $smtpHost->addValidator('Hostname', true); - $smtpPort = new Zend_Form_Element_Text('smtp_port'); + $smtpPort = new Zend_Form_Element_Text(MAIL_SMTP_PORT_KEY); $smtpPort->setLabel('Port'); $smtpPort->addValidator('NotEmpty', true); $smtpPort->addValidator('Digits', true); - $smtpPort->addValidator('Between', array('min' => 1, 'max' => 65535)); + $smtpPort->addValidator('Between', true, array('min' => 1, 'max' => 65535)); + $smtpPort->setAttrib('maxlength', 5); - $smtpUseSsl = new Zend_Form_Element_Checkbox('smtp_use_ssl'); + $smtpUseSsl = new Zend_Form_Element_Checkbox(MAIL_SMTP_USE_SSL_KEY); $smtpUseSsl->setLabel('Use SSL'); - $smtpUsername = new Zend_Form_Element_Text('smtp_username'); + $smtpUsername = new Zend_Form_Element_Text(MAIL_SMTP_USERNAME_KEY); $smtpUsername->setLabel('User name'); $smtpUsername->addValidator('NotEmpty', true); - $smtpPassword = new Zend_Form_Element_Text('smtp_password'); + $smtpPassword = new Zend_Form_Element_Password(MAIL_SMTP_PASSWORD_KEY); $smtpPassword->setLabel('Password'); $smtpPassword->addValidator('NotEmpty', true); @@ -90,6 +91,6 @@ public function init() $submit = new Zend_Form_Element_Submit('submit'); $submit->setLabel('Save'); - $this->addElements(array($provider, $fromAddress, $addressVerification, $smtpHost, $smtpPort, $smtpUseSsl, $smtpUsername, $smtpPassword, $submit)); + $this->addElements(array($csrf, $provider, $fromAddress, $addressVerification, $smtpHost, $smtpPort, $smtpUseSsl, $smtpUsername, $smtpPassword, $submit)); } } diff --git a/modules/mail/views/admin/index.phtml b/modules/mail/views/admin/index.phtml index bec217b16..bfd77d837 100644 --- a/modules/mail/views/admin/index.phtml +++ b/modules/mail/views/admin/index.phtml @@ -19,11 +19,11 @@ =========================================================================*/ $this->declareVars('form', 'pageTitle'); -$this->headTitle(htmlspecialchars($this->pageTitle, ENT_QUOTES, 'UTF-8')); +$this->headTitle($this->escape($this->pageTitle)); ?>
-

pageTitle, ENT_QUOTES, 'UTF-8'); ?>

+

escape($this->pageTitle); ?>

form; ?> -

« Back to Modules Administration

+

« Back to Modules Administration