-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.php
150 lines (133 loc) · 4.05 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* Leafpub: Simple, beautiful publishing. (https://leafpub.org)
*
* @link https://github.com/Leafpub/leafpub
* @copyright Copyright (c) 2016 Leafpub Team
* @license https://github.com/Leafpub/leafpub/blob/master/LICENSE.md (GPL License)
*/
namespace Leafpub\Plugins\SwiftMailer;
use Leafpub\Leafpub,
Leafpub\Plugin\APlugin,
Leafpub\Mailer;
class Plugin extends APlugin {
const NAME = 'SwiftMailer';
/**
* Construct our plugin
*
* Add the SwiftMailer to the possible mailers and add a menu icon
*
* @param \Slim\App $app
* @return void
*
*/
public function __construct($app){
parent::__construct($app);
Mailer::addMailer('SwiftMailer', 'Leafpub\\Plugins\\SwiftMailer\\SwiftMailer');
Leafpub::on('navigation.admin', function($event){
$menu = $event->getEventData();
$menu[] = [
'title' => 'SwiftMailer',
'link' => $this->url('options'),
'icon' => 'fa fa-envelope'
];
$event->setEventData($menu);
});
}
/**
* Set a standard transport
*
*/
public static function afterActivation(){
self::setSetting('SwiftMailer.transport', 'sendmail');
}
/**
* Set the standard mailer
*
*/
public static function afterDeActivation(){
$mailer = self::getSetting('mailer');
if ($mailer === 'SwiftMailer'){
self::setSetting('mailer', 'default');
}
}
/**
* Show the options
*
* @param $req
* @param $res
* @param $args
*
*/
public static function showOptions($req, $res, $args){
$swiftSettings = self::getSwiftSettings();
$html = self::render('options', [
'title' => 'Swift Mailer options',
'plugin_scripts' => [
self::NAME => '/scripts/options.js'
],
'transports' => [
'mail',
'sendmail',
'smtp'
],
'actualTransport' => self::getSetting('SwiftMailer.transport'),
'SwiftOptions' => self::getSwiftSettings()
], self::NAME);
return $res->write($html);
}
/**
* Save the options
*
* @param $req
* @param $res
* @param $args
*
*/
public static function saveOptions($req, $res, $args){
$params = $req->getParams();
$transport = $params['SwiftMailer_transport'];
unset($params['SwiftMailer_transport']);
self::setSetting('SwiftMailer.transport', $transport);
if ($transport === 'smtp'){
if ($params['SwiftMailer_host'] === null){
$invalid[] = 'SwiftMailer.host';
} elseif (!filter_var($params['SwiftMailer_host'], FILTER_VALIDATE_URL)){
$invalid[] = 'SwiftMailer.host';
}
if ($params['SwiftMailer_port'] === null){
$invalid[] = 'SwiftMailer.port';
}
return $res->withJson([
'success' => false,
'invalid' => $invalid,
'message' => 'Fields need to be set'
]);
foreach($params as $key => $val){
if ($val !== null){
self::setSetting(str_replace('_', '.', $key), $val);
}
}
}
// Send response
return $res->withJson([
'success' => true,
]);
}
/**
* Get the SwiftMailer saveOptions
*
* @return array
*
*/
public static function getSwiftSettings(){
return [
'SwiftMailer.transport' => self::getSetting('SwiftMailer.transport'),
'SwiftMailer.host' => self::getSetting('SwiftMailer.host'),
'SwiftMailer.port' => self::getSetting('SwiftMailer.port'),
'SwiftMailer.user' => self::getSetting('SwiftMailer.user'),
'SwiftMailer.pass' => self::getSetting('SwiftMailer.pass'),
'SwiftMailer.encryption' => self::getSetting('SwiftMailer.encryption')
];
}
}