-
Notifications
You must be signed in to change notification settings - Fork 2
/
MailManagerWebService.php
291 lines (243 loc) · 8.19 KB
/
MailManagerWebService.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
require_once 'Zend/Mail.php';
require_once 'Zend/Mail/Transport/Smtp.php';
define('MM_WS_MYSQL_DATE_TIME', 'Y-m-d H:i:s');
define('MM_WS_STUDENT_LOG_SCHEMA_FILE', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'student-log.sql');
class MailManager_WebService
{
private $email_lookup_connection = null;
private $audit_log_connection = null;
private $student_log_connection = null;
private $student_username;
private $student_password;
private $student_host;
private $student_dbname;
private $recipient = '';
private $subject = '';
private $body = '';
private $student_email_address;
private $rate_limit_cutoff;
private $db_config;
private $smtp_config;
private $ws_config;
public function __construct($db_config, $smtp_config, $ws_config)
{
$this->db_config = $db_config;
$this->smtp_config = $smtp_config;
$this->ws_config = $ws_config;
$this->authenticate();
$this->validate();
$this->open_connections();
$this->set_student_email_address();
$this->rate_limit_cutoff = date(MM_WS_MYSQL_DATE_TIME, strtotime('-' . $this->ws_config['rate_limit_cutoff']));
}
private function authenticate()
{
$this->student_username = isset($_POST['username']) ? trim($_POST['username']) : null;
$this->student_password = isset($_POST['password']) ? trim($_POST['password']) : null;
$this->student_host = isset($_POST['host']) ? trim($_POST['host']) : null;
$this->student_dbname = isset($_POST['dbname']) ? trim($_POST['dbname']) : null;
// Don't even try to authenticate if we are missing a username/password combination
if (empty($this->student_username) || empty($this->student_password))
{
header('HTTP/1.1 403 Forbidden');
echo 'Could not authenticate student';
exit;
}
$this->student_log_connection = new mysqli($this->student_host, $this->student_username, $this->student_password, $this->student_dbname);
if ($this->student_log_connection->connect_error)
{
header('HTTP/1.1 403 Forbidden');
echo 'Could not authenticate student';
exit;
}
$this->create_student_log_table();
}
/**
* Create the student copy of the log table if it does not already
* exist. This is similar to the audit log, but can be accessed by the
* student and so cannot be used for auditing.
*/
private function create_student_log_table()
{
// First check if table exists
$sql = 'SELECT id FROM mail_message_log';
$result = $this->student_log_connection->query($sql);
// Table does not exist, so create it
if ($result === FALSE)
{
$schema = file_get_contents(MM_WS_STUDENT_LOG_SCHEMA_FILE);
if (!empty($schema))
{
$result = $this->student_log_connection->query($schema);
}
}
}
private function validate()
{
$this->recipient = isset($_POST['recipient']) ? trim($_POST['recipient']) : null;
$this->subject = isset($_POST['subject']) ? trim($_POST['subject']) : null;
$this->body = isset($_POST['body']) ? $_POST['body'] : null;
if (empty($this->recipient))
{
header('HTTP/1.1 400 Bad Request');
echo 'No recipient specified';
exit;
}
if (empty($this->subject))
{
header('HTTP/1.1 400 Bad Request');
echo 'No subject specified';
exit;
}
if (empty($this->body))
{
header('HTTP/1.1 400 Bad Request');
echo 'No body specified';
exit;
}
// Check that email address is valid
if (!filter_var($this->recipient, FILTER_VALIDATE_EMAIL))
{
header('HTTP/1.1 400 Bad Request');
echo 'Invalid recipient specified';
exit;
}
// Check that recipient is in list of permitted domains
// Recipient domains file is relative to the class file, not the executing script
$recipient_domains_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'recipient-domains';
$recipient_domains_data = file_get_contents($recipient_domains_file);
$recipient_domains = explode("\n", $recipient_domains_data);
list($user, $domain) = explode('@', $this->recipient);
if (!in_array($domain, $recipient_domains))
{
header('HTTP/1.1 400 Bad Request');
echo 'Recipient is not in list of permitted domains';
exit;
}
}
private function open_connections()
{
$this->email_lookup_connection = new mysqli($this->db_config['email_lookup']['host'], $this->db_config['email_lookup']['username'], $this->db_config['email_lookup']['password'], $this->db_config['email_lookup']['dbname']);
if ($this->email_lookup_connection->connect_error)
{
error_log('Could not establish email lookup connection');
header('HTTP/1.1 500 Server Error');
exit;
}
$this->audit_log_connection = new mysqli($this->db_config['audit_log']['host'], $this->db_config['audit_log']['username'], $this->db_config['audit_log']['password'], $this->db_config['audit_log']['dbname']);
if ($this->audit_log_connection->connect_error)
{
error_log('Could not establish audit log connection');
header('HTTP/1.1 500 Server Error');
exit;
}
}
private function get_current_date_time()
{
return date(MM_WS_MYSQL_DATE_TIME);
}
private function count_emails_sent()
{
$sql = 'SELECT id FROM audit_log WHERE log_time > ? AND username = ?';
$statement = $this->audit_log_connection->prepare($sql);
$statement->bind_param('ss', $this->rate_limit_cutoff, $this->student_username);
$statement->execute();
$result = $statement->get_result();
$emails_sent = $result->num_rows;
$statement->close();
return $emails_sent;
}
/**
* Set student email address based on their username. This involves a simple
* database lookup, although we may be able to replace this with LDAP at a later
* date.
*/
private function set_student_email_address()
{
$sql = 'SELECT email FROM users WHERE username = ? LIMIT 1';
$statement = $this->email_lookup_connection->prepare($sql);
$statement->bind_param('s', $this->student_username);
$statement->execute();
$result = $statement->get_result();
if ($result !== FALSE)
{
if ($result->num_rows === 1)
{
$data = $result->fetch_assoc();
if (is_array($data) && isset($data['email']) && !empty($data['email']) && filter_var($data['email'], FILTER_VALIDATE_EMAIL))
{
$this->student_email_address = $data['email'];
}
else
{
error_log('Could not find student email address');
header('HTTP/1.1 500 Server Error');
exit;
}
}
else
{
error_log('Could not find student email address');
header('HTTP/1.1 500 Server Error');
exit;
}
}
else
{
error_log('Could not find student email address');
header('HTTP/1.1 500 Server Error');
exit;
}
}
private function rate_limit()
{
$emails_sent = $this->count_emails_sent();
if ($emails_sent > $this->ws_config['rate_limit_max_emails'])
{
header('HTTP/1.1 429 Too Many Requests');
echo 'Rate limit exceeded, can send maximum of ' . $this->ws_config['rate_limit_max_emails'] . ' in ' . $this->ws_config['rate_limit_cutoff'];
exit;
}
}
public function send()
{
$this->rate_limit();
$sql = 'INSERT INTO audit_log (username, recipient, subject, body, log_time) VALUES (?, ?, ?, ?, ?)';
$statement = $this->audit_log_connection->prepare($sql);
if ($statement !== FALSE)
{
$current_date_time = $this->get_current_date_time();
$statement->bind_param('sssss', $this->student_username, $this->recipient, $this->subject, $this->body, $current_date_time);
$statement->execute();
}
else
{
error_log('Could not prepare audit log SQL query');
header('HTTP/1.1 500 Server Error');
exit;
}
$sql = 'INSERT INTO mail_manager_log (recipient, subject, body, log_time) VALUES (?, ?, ?, ?)';
$statement = $this->student_log_connection->prepare($sql);
if ($statement !== FALSE)
{
$current_date_time = $this->get_current_date_time();
$statement->bind_param('ssss', $this->recipient, $this->subject, $this->body, $current_date_time);
$statement->execute();
}
else
{
error_log('Could not prepare student log SQL query');
header('HTTP/1.1 500 Server Error');
exit;
}
$transport = new Zend_Mail_Transport_Smtp($this->smtp_config['host']);
Zend_Mail::setDefaultTransport($transport);
$mail = new Zend_Mail();
$mail->setFrom($this->student_email_address);
$mail->addTo($this->recipient);
$mail->setSubject($this->subject);
$mail->setBodyText($this->body);
$mail->send();
}
}