Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 72cca12

Browse files
author
Jamie Snape
committed
Revise mail module configuration form and add install script
1 parent 6ccd95a commit 72cca12

File tree

7 files changed

+131
-40
lines changed

7 files changed

+131
-40
lines changed

modules/mail/Notification.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,22 @@ public function init()
4141
*/
4242
protected function handleSendMailMessage($params)
4343
{
44-
$provider = $this->Setting->getValueByName('provider', $this->moduleName);
44+
$provider = $this->Setting->getValueByName(MAIL_PROVIDER_KEY, $this->moduleName);
4545

46-
if ($provider === 'app_engine') {
46+
if ($provider === MAIL_PROVIDER_APP_ENGINE) {
4747
$service = new Midas_Service_AppEngine_Mail();
4848
$transport = new Midas_Mail_Transport_Service($service);
49-
} elseif ($provider === 'send_grid') {
50-
$username = $this->Setting->getValueByName('send_grid_username', $this->moduleName);
51-
$password = $this->Setting->getValueByName('send_grid_password', $this->moduleName);
49+
} elseif ($provider === MAIL_PROVIDER_SEND_GRID) {
50+
$username = $this->Setting->getValueByName(MAIL_SEND_GRID_USERNAME_KEY, $this->moduleName);
51+
$password = $this->Setting->getValueByName(MAIL_SEND_GRID_PASSWORD_KEY, $this->moduleName);
5252
$service = new Midas_Service_SendGrid_Mail($username, $password);
5353
$transport = new Midas_Mail_Transport_Service($service);
54-
} elseif ($provider = 'smtp') {
55-
$host = $this->Setting->getValueByName('smtp_host', $this->moduleName);
56-
$port = $this->Setting->getValueByName('smtp_port', $this->moduleName);
57-
$ssl = $this->Setting->getValueByName('smtp_use_ssl', $this->moduleName);
58-
$username = $this->Setting->getValueByName('smtp_username', $this->moduleName);
59-
$password = $this->Setting->getValueByName('smtp_password', $this->moduleName);
54+
} elseif ($provider === MAIL_PROVIDER_SMTP) {
55+
$host = $this->Setting->getValueByName(MAIL_SMTP_HOST_KEY, $this->moduleName);
56+
$port = $this->Setting->getValueByName(MAIL_SMTP_PORT_KEY, $this->moduleName);
57+
$ssl = $this->Setting->getValueByName(MAIL_SMTP_USE_SSL_KEY, $this->moduleName);
58+
$username = $this->Setting->getValueByName(MAIL_SMTP_USERNAME_KEY, $this->moduleName);
59+
$password = $this->Setting->getValueByName(MAIL_SMTP_PASSWORD_KEY, $this->moduleName);
6060
$config = array();
6161

6262
if (!empty($port)) {
@@ -80,7 +80,7 @@ protected function handleSendMailMessage($params)
8080
}
8181

8282
$mail = new Midas_Mail();
83-
$mail->setFrom($this->Setting->getValueByName('from_address', $this->moduleName));
83+
$mail->setFrom($this->Setting->getValueByName(MAIL_FROM_ADDRESS_KEY, $this->moduleName));
8484

8585
if (isset($params['bcc'])) {
8686
$mail->addBcc($params['bcc']);

modules/mail/configs/module.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,5 @@
44
fullname = "Mail"
55
description = "Send informational messages using email"
66
category = "Messaging"
7-
dependencies =
8-
conflicts =
97
uuid = b902fded-6820-4dad-8094-e9ef13143e32
108
version = "1.0.0"

modules/mail/constant/module.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
5+
All rights reserved.
6+
More information http://www.kitware.com
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0.txt
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
=========================================================================*/
20+
21+
define('MAIL_PROVIDER_KEY', 'provider');
22+
define('MAIL_PROVIDER_DEFAULT_VALUE', 'smtp');
23+
define('MAIL_PROVIDER_APP_ENGINE', 'app_engine');
24+
define('MAIL_PROVIDER_MAIL', 'mail');
25+
define('MAIL_PROVIDER_SEND_GRID', 'send_grid');
26+
define('MAIL_PROVIDER_SMTP', 'smtp');
27+
define('MAIL_FROM_ADDRESS_KEY', 'from_address');
28+
define('MAIL_FROM_ADDRESS_DEFAULT_VALUE', '');
29+
define('MAIL_ADDRESS_VERIFICATION_KEY', 'address_verification');
30+
define('MAIL_ADDRESS_VERIFICATION_DEFAULT_VALUE', 0);
31+
define('MAIL_SEND_GRID_USERNAME_KEY', 'send_grid_username');
32+
define('MAIL_SEND_GRID_USERNAME_DEFAULT_VALUE', '');
33+
define('MAIL_SEND_GRID_PASSWORD_KEY', 'send_grid_password');
34+
define('MAIL_SEND_GRID_PASSWORD_DEFAULT_VALUE', '');
35+
define('MAIL_SMTP_HOST_KEY', 'smtp_host');
36+
define('MAIL_SMTP_HOST_DEFAULT_VALUE', '');
37+
define('MAIL_SMTP_PORT_KEY', 'smtp_port');
38+
define('MAIL_SMTP_PORT_DEFAULT_VALUE', 587);
39+
define('MAIL_SMTP_USE_SSL_KEY', 'smtp_use_ssl');
40+
define('MAIL_SMTP_USE_SSL_DEFAULT_VALUE', 1);
41+
define('MAIL_SMTP_USERNAME_KEY', 'smtp_username');
42+
define('MAIL_SMTP_USERNAME_DEFAULT_VALUE', '');
43+
define('MAIL_SMTP_PASSWORD_KEY', 'smtp_password');
44+
define('MAIL_SMTP_PASSWORD_DEFAULT_VALUE', '');

modules/mail/controllers/AdminController.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ public function indexAction()
3939
$values = $form->getValues();
4040

4141
foreach ($values as $key => $value) {
42-
$this->Setting->setConfig($key, $value, $this->moduleName);
42+
if ($value !== null) {
43+
$this->Setting->setConfig($key, $value, $this->moduleName);
44+
}
4345
}
4446
}
4547

@@ -50,7 +52,7 @@ public function indexAction()
5052
foreach ($elements as $element) {
5153
$name = $element->getName();
5254

53-
if ($name !== 'submit') {
55+
if ($name !== 'csrf' && $name !== 'submit') {
5456
$value = $this->Setting->getValueByName($name, $this->moduleName);
5557

5658
if (!is_null($value)) {
@@ -61,5 +63,6 @@ public function indexAction()
6163
}
6264

6365
$this->view->form = $form;
66+
session_start();
6467
}
6568
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
5+
All rights reserved.
6+
More information http://www.kitware.com
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0.txt
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
=========================================================================*/
20+
21+
require_once BASE_PATH.'/modules/mail/constant/module.php';
22+
23+
/** Install the mail module. */
24+
class Mail_InstallScript extends MIDASModuleInstallScript
25+
{
26+
/** @var string */
27+
public $moduleName = 'mail';
28+
29+
/** Post database install. */
30+
public function postInstall()
31+
{
32+
/** @var SettingModel $settingModel */
33+
$settingModel = MidasLoader::loadModel('Setting');
34+
$settingModel->setConfig(MAIL_PROVIDER_KEY, MAIL_PROVIDER_DEFAULT_VALUE, $this->moduleName);
35+
$settingModel->setConfig(MAIL_FROM_ADDRESS_KEY, MAIL_FROM_ADDRESS_DEFAULT_VALUE, $this->moduleName);
36+
$settingModel->setConfig(MAIL_ADDRESS_VERIFICATION_KEY, MAIL_ADDRESS_VERIFICATION_DEFAULT_VALUE, $this->moduleName);
37+
$settingModel->setConfig(MAIL_SEND_GRID_USERNAME_KEY, MAIL_SEND_GRID_USERNAME_DEFAULT_VALUE, $this->moduleName);
38+
$settingModel->setConfig(MAIL_SEND_GRID_PASSWORD_KEY, MAIL_SEND_GRID_PASSWORD_DEFAULT_VALUE, $this->moduleName);
39+
$settingModel->setConfig(MAIL_SMTP_HOST_KEY, MAIL_SMTP_HOST_DEFAULT_VALUE, $this->moduleName);
40+
$settingModel->setConfig(MAIL_SMTP_PORT_KEY, MAIL_SMTP_PORT_DEFAULT_VALUE, $this->moduleName);
41+
$settingModel->setConfig(MAIL_SMTP_USE_SSL_KEY, MAIL_SMTP_USE_SSL_DEFAULT_VALUE, $this->moduleName);
42+
$settingModel->setConfig(MAIL_SMTP_USERNAME_KEY, MAIL_SMTP_USERNAME_DEFAULT_VALUE, $this->moduleName);
43+
$settingModel->setConfig(MAIL_SMTP_PASSWORD_KEY, MAIL_SMTP_PASSWORD_DEFAULT_VALUE, $this->moduleName);
44+
}
45+
}

modules/mail/forms/Admin.php

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,64 +24,65 @@ class Mail_Form_Admin extends Zend_Form
2424
/** Initialize this form. */
2525
public function init()
2626
{
27-
$this->setName('mail_config');
27+
$this->setName('mail_admin');
2828
$this->setMethod('POST');
2929

30-
$provider = new Zend_Form_Element_Select('provider');
30+
$csrf = new Midas_Form_Element_Hash('csrf');
31+
$csrf->setSalt('5qzSHzCdNuPfYaT99Jq5WcKe');
32+
$csrf->setDecorators(array('ViewHelper'));
33+
34+
$provider = new Zend_Form_Element_Select(MAIL_PROVIDER_KEY);
3135
$provider->setLabel('Provider');
3236
$provider->setRequired(true);
3337
$provider->addValidator('NotEmpty', true);
34-
35-
if (class_exists('\google\appengine\api\mail\Message', false)) {
36-
$provider->addMultiOption('app_engine', 'Google App Engine');
37-
}
38-
3938
$provider->addMultiOptions(array(
40-
'mail' => 'PHP Mail Function',
41-
'send_grid' => 'SendGrid Service',
42-
'smtp' => 'External SMTP Server',
39+
MAIL_PROVIDER_APP_ENGINE => 'Google App Engine',
40+
MAIL_PROVIDER_MAIL => 'PHP Mail Function',
41+
MAIL_PROVIDER_SEND_GRID => 'SendGrid Service',
42+
MAIL_PROVIDER_SMTP => 'External SMTP Server',
4343
));
4444

45-
$fromAddress = new Zend_Form_Element_Text('from_address');
45+
$fromAddress = new Zend_Form_Element_Text(MAIL_FROM_ADDRESS_KEY);
4646
$fromAddress->setLabel('From email address');
4747
$fromAddress->setRequired(true);
4848
$fromAddress->addValidator('NotEmpty', true);
4949
$fromAddress->addValidator('EmailAddress', true);
5050

51-
$addressVerification = new Zend_Form_Element_Checkbox('address_verification');
51+
$addressVerification = new Zend_Form_Element_Checkbox(MAIL_ADDRESS_VERIFICATION_KEY);
5252
$addressVerification->setLabel('Require email address verification');
5353

5454
$this->addDisplayGroup(array($provider, $fromAddress, $addressVerification), 'global');
5555

56-
$sendGridUsername = new Zend_Form_Element_Text('send_grid_username');
56+
$sendGridUsername = new Zend_Form_Element_Text(MAIL_SEND_GRID_USERNAME_KEY);
5757
$sendGridUsername->setLabel('SendGrid User Name');
5858
$sendGridUsername->addValidator('NotEmpty', true);
5959

60-
$sendGridPassword = new Zend_Form_Element_Text('send_grid_password');
60+
$sendGridPassword = new Zend_Form_Element_Text(MAIL_SEND_GRID_PASSWORD_KEY);
6161
$sendGridPassword->setLabel('SendGrid Password');
6262
$sendGridPassword->addValidator('NotEmpty', true);
6363

6464
$this->addDisplayGroup(array($sendGridUsername, $sendGridPassword), 'send_grid');
6565

66-
$smtpHost = new Zend_Form_Element_Text('smtp_host');
66+
$smtpHost = new Zend_Form_Element_Text(MAIL_SMTP_HOST_KEY);
6767
$smtpHost->setLabel('Server name');
6868
$smtpHost->addValidator('NotEmpty', true);
6969
$smtpHost->addValidator('Hostname', true);
7070

71-
$smtpPort = new Zend_Form_Element_Text('smtp_port');
71+
$smtpPort = new Zend_Form_Element_Text(MAIL_SMTP_PORT_KEY);
7272
$smtpPort->setLabel('Port');
7373
$smtpPort->addValidator('NotEmpty', true);
7474
$smtpPort->addValidator('Digits', true);
75-
$smtpPort->addValidator('Between', array('min' => 1, 'max' => 65535));
75+
$smtpPort->addValidator('Between', true, array('min' => 1, 'max' => 65535));
76+
$smtpPort->setAttrib('maxlength', 5);
7677

77-
$smtpUseSsl = new Zend_Form_Element_Checkbox('smtp_use_ssl');
78+
$smtpUseSsl = new Zend_Form_Element_Checkbox(MAIL_SMTP_USE_SSL_KEY);
7879
$smtpUseSsl->setLabel('Use SSL');
7980

80-
$smtpUsername = new Zend_Form_Element_Text('smtp_username');
81+
$smtpUsername = new Zend_Form_Element_Text(MAIL_SMTP_USERNAME_KEY);
8182
$smtpUsername->setLabel('User name');
8283
$smtpUsername->addValidator('NotEmpty', true);
8384

84-
$smtpPassword = new Zend_Form_Element_Text('smtp_password');
85+
$smtpPassword = new Zend_Form_Element_Password(MAIL_SMTP_PASSWORD_KEY);
8586
$smtpPassword->setLabel('Password');
8687
$smtpPassword->addValidator('NotEmpty', true);
8788

@@ -90,6 +91,6 @@ public function init()
9091
$submit = new Zend_Form_Element_Submit('submit');
9192
$submit->setLabel('Save');
9293

93-
$this->addElements(array($provider, $fromAddress, $addressVerification, $smtpHost, $smtpPort, $smtpUseSsl, $smtpUsername, $smtpPassword, $submit));
94+
$this->addElements(array($csrf, $provider, $fromAddress, $addressVerification, $smtpHost, $smtpPort, $smtpUseSsl, $smtpUsername, $smtpPassword, $submit));
9495
}
9596
}

modules/mail/views/admin/index.phtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
=========================================================================*/
2020

2121
$this->declareVars('form', 'pageTitle');
22-
$this->headTitle(htmlspecialchars($this->pageTitle, ENT_QUOTES, 'UTF-8'));
22+
$this->headTitle($this->escape($this->pageTitle));
2323
?>
2424

2525
<div class="viewMain">
26-
<h1><?php echo htmlspecialchars($this->pageTitle, ENT_QUOTES, 'UTF-8'); ?></h1>
26+
<h1><?php echo $this->escape($this->pageTitle); ?></h1>
2727
<?php echo $this->form; ?>
28-
<p><a href="<?php $this->url(array('controller' => 'admin', 'action' => 'index')); ?>#tabs-modules">&laquo; Back to Modules Administration</a></p>
28+
<p><a href="<?php echo $this->url(array('controller' => 'admin', 'action' => 'index'), 'default'); ?>#tabs-modules">&laquo; Back to Modules Administration</a></p>
2929
</div>

0 commit comments

Comments
 (0)