-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpb_entry.php
executable file
·189 lines (166 loc) · 8.77 KB
/
pb_entry.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace Module;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
use Library\ModuleConfig;
use Library\Controller;
use Library\Policy;
use Library\Mailer;
use Registry\Action;
use Helper\ApiResponse as Respond;
use Helper\Request;
use Helper\Validate as Validator;
require 'vendor/autoload.php';
class BasicMailer {
public function initialize() {
$policy = new Policy();
$config = new ModuleConfig('basic-mailer');
$config->defaults(array(
"enabled" => 0,
"host" => "",
"smtp_auth" => 0,
"username" => "",
"password" => "",
"smtp_secure" => null,
"port" => 25,
"from" => $policy->get('site-email'),
"from_name" => $policy->get('site-title'),
"is_html" => 1
));
if ($config->get('enabled') == 1) {
Action::register('send-mail', function($options) {
$config = new ModuleConfig('basic-mailer');
$mail = new PHPMailer(true);
if (!isset($options['isHTML'])) $options['isHTML'] = $config->get('is_html') == '1' ? true : false;
try {
$mail->isSMTP();
$mail->Host = $config->get('host');
$mail->SMTPAuth = ($config->get('smtp_auth') == '1' ? true : false);
$mail->Username = $config->get('username');
$mail->Password = $config->get('password');
switch($config->get('smtp_secure')) {
case 'tls':
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
break;
case 'starttls':
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
break;
}
$mail->Port = $config->get('port');
$mail->setFrom($config->get('from'), $config->get('from_name'));
$mail->isHTML($options['isHTML']);
$mail->Subject = $options['subject'];
$mail->Body = $options['message'];
if (is_array($options['recipient'])) {
foreach($options['recipient'] as $recipient) {
$mail->addAddress($recipient);
}
} else {
$mail->addAddress($options['recipient']);
}
$mail->send();
return (object) array(
"success" => true
);
} catch (Exception $e) {
return (object) array(
"success" => false,
"message" => $mail->ErrorInfo
);
}
});
}
}
public function requestHandler($params) {
if (isset($params[0])) {
switch($params[0]) {
case 'test-mail':
if (!Request::requireMethod('post')) die();
if (!Request::requireAuthentication()) die();
$controller = new Controller;
$userModel = $controller->__model('user');
if (!$userModel->check('module.basic-mailer.test-mail')) {
Respond::error("missing_privileges", "You are lacking the permission to send a test E-mail.");
} else {
$body = Request::parseBody();
if (isset($body->recipient)) {
$mailer = new Mailer;
$content = file_get_contents(DYNAMIC_DIR . '/modules/basic-mailer/test-mail.html');
$content = str_replace("{{SITE_LOCATION}}", SITE_LOCATION, $content);
$res = $mailer->send(array(
"recipient" => $body->recipient,
"subject" => SITE_TITLE . ": Testing E-mail.",
"message" => $content,
"headers" => array(
'Mime-Version' => '1.0',
'Content-Type' => 'text/html;charset=UTF-8'
),
//Optimal options for common mailer plugins.
"isHTML" => true
));
if ($res->success) {
Respond::success();
} else {
Respond::error($res->error, $res->message);
}
} else {
Respond::error('missing_recipient', "The recipient is missing from your request.");
}
}
break;
case 'retrieve-settings':
if (!Request::requireMethod('get')) die();
if (!Request::requireAuthentication()) die();
$controller = new Controller;
$userModel = $controller->__model('user');
if (!$userModel->check('module.basic-mailer.retrieve-settings')) {
Respond::error("missing_privileges", "You are lacking the permission to retrieve mailer settings.");
} else {
$config = new ModuleConfig('basic-mailer');
Respond::success(array(
"settings" => array(
"enabled" => intval($config->get('enabled')) == 1,
"is_html" => intval($config->get('is_html')) == 1,
"host" => $config->get('host'),
"port" => intval($config->get('port')),
"smtp_secure" => $config->get('smtp_secure'),
"from" => $config->get('from'),
"from_name" => $config->get('from_name'),
"smtp_auth" => intval($config->get('smtp_auth')) == 1,
"username" => $config->get('username'),
"password" => $config->get('password')
)
));
}
break;
case 'save-settings':
if (!Request::requireMethod('post')) die();
if (!Request::requireAuthentication()) die();
$controller = new Controller;
$userModel = $controller->__model('user');
if (!$userModel->check('module.basic-mailer.save-settings')) {
Respond::error("missing_privileges", "You are lacking the permission to retrieve mailer settings.");
} else {
$body = Request::parseBody();
$config = new ModuleConfig('basic-mailer');
$allowed = ["enabled", "is_html", "host", "port", "smtp_secure", "from", "from_name", "smtp_auth", "username", "password"];
$body = Validator::removeUnlisted($allowed, $body);
foreach($body as $setting => $value) {
if (in_array($setting, ["enabled", "is_html", "smtp_auth"])) $value = $value ? 1 : 0;
$config->set($setting, $value);
}
Respond::success();
}
break;
default:
Respond::error('unknown_action', "The requested action does not exist.");
}
} else {
Respond::error('missing_action', "The action was missing from the url.");
}
}
public function configurator($params) {
require_once DYNAMIC_DIR . '/modules/basic-mailer/configurator.php';
}
}