-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller copy.php
365 lines (316 loc) · 11.9 KB
/
controller copy.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
require_once './imap_client.php';
require_once './sqlite_connector.php';
function render_error($status, $msg)
{
@http_response_code($status);
die("{'result': 'error', 'error': '$msg'}");
}
class DisplayEmailsController
{
public static function matches()
{
// return !isset($_GET['action']) && !empty($_SERVER['QUERY_STRING'] ?? '');
return !isset($_GET['action']) && !empty($_GET['address'] ?? '');
}
public static function invoke(ImapClient $imapClient, array $config, DatabaseController $databaseClient)
{
$address = $_GET['address']?? '';
$filter_network = $_GET["network"] ?? '';
$filter_from_name = $_GET['from_name'] ?? '';
$action = $_GET['mode'] ?? '';
$user = User::parseDomain($address, $config['blocked_usernames']);
if ($user->isInvalid($config['domains'])) {
RedirectToRandomAddressController::invoke($imapClient, $config, '');
return;
}
if ($action != 'add_filter') {
DisplayEmailsController::render(array(), $config, $user, false);
return;
}
$filter_criteria = '';
$is_filter_network = true;
$panel_name = '';
if (strlen($filter_from_name) > 0) {
$is_filter_network = false;
$panel_name = $filter_from_name;
$filter_criteria = 'FROM "'.$filter_from_name.'"';
} else if (strlen($filter_network)) {
$panel_name = $filter_network;
$filter_criteria = 'FROM "'.$filter_network.' "';
}
$emails = $imapClient->get_emails($user, $filter_criteria);
if (!$is_filter_network) {
$new_emails = array_filter($emails, function($email) use ($filter_from_name) {
return $email->fromName == $filter_from_name;
});
$emails = $new_emails;
}
$databaseClient->insertEmailData($emails, $user->address);
$databaseClient->generateRssFeed($user->address, $emails, false);
$case = false;
if ($case) {
// read array from xml file
$file = "rss/" . strtolower(strstr($address, '@', true)) . ".xml";
if (file_exists($file)) {
$get = file_get_contents($file);
$arr = simplexml_load_file($file);
// Convert into json
$con = json_encode($arr);
// // Convert into associative array
$newArr = json_decode($con, true);
if (isset($newArr["channel"]["items"]) && count($newArr["channel"]["items"]) > 0) {
$itemArray = $newArr["channel"]["items"]["item"];
if (array_key_exists("id", $itemArray)) {
DisplayEmailsController::render(array($itemArray), $config, $user, $case);
} else
DisplayEmailsController::render($newArr["channel"]["items"]["item"], $config, $user, $case);
} else DisplayEmailsController::render(array(), $config, $user, $case);
} else
DisplayEmailsController::render(array(), $config, $user, $case);
} else {
DisplayEmailsController::render($emails, $config, $user, $case, $filter_from_name, $filter_network, strstr($address, '@', true) . ".xml");
}
}
public static function render($emails, $config, $user, $option, $filter_from_name = '', $filter_network = '', $rss_name = '')
{
// variables that have to be defined here for frontend template: $emails, $config
if ($option)
require "frontend.template.php";
else
require "frontend.template_2.php";
}
public static function rssFeedName() {
}
public static function xml2array($xmlObject, $out = array())
{
foreach ((array) $xmlObject as $index => $node)
$out[$index] = (is_object($node)) ? DisplayEmailsController::xml2array($node) : $node;
return $out;
}
}
class RedirectToAddressController
{
public static function matches()
{
return ($_GET['action'] ?? null) === "redirect"
&& isset($_POST['username'])
&& isset($_POST['domain']);
}
public static function invoke(ImapClient $imapClient, array $config)
{
$user = User::parseUsernameAndDomain($_POST['username'], $_POST['domain'], $config['blocked_usernames']);
RedirectToAddressController::render($user->username . "@" . $user->domain);
}
public static function render($address)
{
header("location: ?address=$address");
}
}
class RedirectToRandomAddressController
{
public static function matches()
{
return ($_GET['action'] ?? null) === 'random';
}
public static function invoke(ImapClient $imapClient, array $config, $ip)
{
$address = User::get_random_address($config['domains'], $ip);
RedirectToAddressController::render($address);
}
}
class HasNewMessagesControllerJson
{
public static function matches()
{
return ($_GET['action'] ?? null) === "has_new_messages"
&& isset($_GET['email_ids'])
&& isset($_GET['address']);
}
public static function invoke(ImapClient $imapClient, array $config, DatabaseController $databaseClient)
{
$email_ids = $_GET['email_ids'];
$address = $_GET['address'];
$user = User::parseDomain($address, $config['blocked_usernames']);
if ($user->isInvalid($config['domains'])) {
render_error(400, "invalid email address");
}
$emails = $imapClient->get_emails($user);
$knownMailIds = explode('|', $email_ids);
$newMailIds = array_map(function ($mail) {
return $mail->id;
}, $emails);
$onlyNewMailIds = array_diff($newMailIds, $knownMailIds);
// $newMails = array_filter( $emails, function ($mail) use ($onlyNewMailIds){
// if (array_key_exists($mail->id, $onlyNewMailIds))
// return $mail;
// }, ARRAY_FILTER_USE_KEY);
// foreach ($emails as $key => $value) {
// foreach ($emails as $email) {
// # code...
// if (in_array($email->id, $knownMailIds))
// unset($email);
// }
// print_r($emails);
// $databaseClient->insertEmailData($emails, $user->address);
// $databaseClient->generateRssFeed($user->address);
HasNewMessagesControllerJson::render(count($onlyNewMailIds));
}
public static function render($counter)
{
header('Content-Type: application/json');
print json_encode($counter);
}
}
class DownloadEmailController
{
public static function matches()
{
return ($_GET['action'] ?? null) === "download_email"
&& isset($_GET['email_id'])
&& isset($_GET['address']);
}
public static function invoke(ImapClient $imapClient, array $config)
{
$email_id = $_GET['email_id'];
$address = $_GET['address'];
$user = User::parseDomain($address, $config['blocked_usernames']);
if ($user->isInvalid($config['domains'])) {
RedirectToRandomAddressController::invoke($imapClient, $config, '');
return;
}
$download_email_id = filter_var($email_id, FILTER_SANITIZE_NUMBER_INT);
$full_email = $imapClient->load_one_email_fully($download_email_id, $user);
if ($full_email !== null) {
$filename = $user->address . "-" . $download_email_id . ".eml";
DownloadEmailController::renderDownloadEmailAsRfc822($full_email, $filename);
} else {
render_error(404, 'download error: invalid username/mailid combination');
}
}
public static function renderDownloadEmailAsRfc822($full_email, $filename)
{
header("Content-Type: message/rfc822; charset=utf-8");
header("Content-Disposition: attachment; filename=\"$filename\"");
print $full_email;
}
}
class DeleteEmailController
{
public static function matches()
{
return ($_GET['action'] ?? null) === "delete_email"
&& isset($_GET['email_id'])
&& isset($_GET['address']);
}
public static function invoke(ImapClient $imapClient, array $config)
{
$email_id = $_GET['email_id'];
$address = $_GET['address'];
$user = User::parseDomain($address, $config['blocked_usernames']);
if ($user->isInvalid($config['domains'])) {
RedirectToRandomAddressController::invoke($imapClient, $config, '');
return;
}
$delete_email_id = filter_var($email_id, FILTER_SANITIZE_NUMBER_INT);
if ($imapClient->delete_email($delete_email_id, $user)) {
RedirectToAddressController::render($address);
} else {
render_error(404, 'delete error: invalid username/mailid combination');
}
}
}
class GenerateRSSFeedController
{
public static function matches()
{
return ($_GET['action'] ?? null) === "generate_rss_feed" && isset($_GET['address']);
}
public static function invoke(ImapClient $imapClient, array $config, DatabaseController $databaseClient)
{
if (!isset($_GET['address'])) return;
$databaseClient->generateRssFeed($_GET['address']);
}
}
class xml_utils
{
/*object to array mapper */
public static function objectToArray($object)
{
if (!is_object($object) && !is_array($object)) {
return $object;
}
if (is_object($object)) {
$object = get_object_vars($object);
}
return array_map('objectToArray', $object);
}
/* xml DOM loader*/
public static function xml_to_array($xmlstr)
{
$doc = new DOMDocument();
$doc->loadXML($xmlstr);
return xml_utils::dom_to_array($doc->documentElement);
}
/* recursive XMl to array parser */
public static function dom_to_array($node)
{
$output = array();
switch ($node->nodeType) {
case XML_CDATA_SECTION_NODE:
case XML_TEXT_NODE:
$output = trim($node->textContent);
break;
case XML_ELEMENT_NODE:
for ($i = 0, $m = $node->childNodes->length; $i < $m; $i++) {
$child = $node->childNodes->item($i);
$v = xml_utils::dom_to_array($child);
if (isset($child->tagName)) {
$t = xml_utils::ConvertTypes($child->tagName);
if (!isset($output[$t])) {
$output[$t] = array();
}
$output[$t][] = $v;
} elseif ($v) {
$output = (string) $v;
}
}
if (is_array($output)) {
if ($node->attributes->length) {
$a = array();
foreach ($node->attributes as $attrName => $attrNode) {
$a[$attrName] = xml_utils::ConvertTypes($attrNode->value);
}
$output['@attr'] = $a;
}
foreach ($output as $t => $v) {
if (is_array($v) && count($v) == 1 && $t != '@attr') {
$output[$t] = $v[0];
}
}
}
break;
}
return $output;
}
/* elements converter */
public static function ConvertTypes($org)
{
if (is_numeric($org)) {
$val = floatval($org);
} else {
if ($org === 'true') {
$val = true;
} else if ($org === 'false') {
$val = false;
} else {
if ($org === '') {
$val = null;
} else {
$val = $org;
}
}
}
return $val;
}
}