forked from BigBlueHat/atmailopen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reademail.php
457 lines (368 loc) · 13.8 KB
/
reademail.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<?php
// +----------------------------------------------------------------+
// | reademail.php |
// | Function: Read an email message |
// +----------------------------------------------------------------+
// | AtMail Open - Licensed under the Apache 2.0 Open-source License|
// | http://opensource.org/licenses/apache2.0.php |
// +----------------------------------------------------------------+
require_once('header.php');
require_once('Global.php');
require_once('ReadMsg.php');
require_once('SendMsg.php');
require_once('Session.php');
session_start();
if (isset($_REQUEST['DisplayImages']) && $_REQUEST['DisplayImages'] == 1) {
$_SESSION['DisplayImages'][] = $_REQUEST['id'];
}
$var = array();
$atmail = new AtmailGlobal();
$auth =& $atmail->getAuthObj();
// If we are exporting an email, pring the correct header, otherwise proceed as normal
if (isset($_REQUEST['rawemail']))
{
require_once('GetMail.php');
$getmail = new GetMail();
$time = time($getmail->calc_timezone(time()));
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: message/rfc822; name=\"rawemail-$time.eml");
header("Content-Disposition: attachment; filename=\"rawemail-$time.eml");
header('Content-Transfer-Encoding: binary');
header("Pragma: ");
}
else
{
$atmail->httpheaders();
}
$atmail->status = $auth->getuser($atmail->SessionID);
$atmail->username = $auth->username;
$atmail->pop3host = $auth->pop3host;
// check for language version
if (!$atmail->Language)
$atmail->Language = $pref['Language'];
// Print the error screen if the account has auth errors, or session timeout.
if ( $atmail->status == 1 )
$atmail->auth_error();
if ( $atmail->status == 2 )
$atmail->session_error();
// Load the account preferences
$atmail->loadprefs();
// Parse the users custom stylesheet
$var['atmailstyle'] = $atmail->parse("html/$atmail->Language/$atmail->LoginType/atmailstyle.css");
$var['atmailstyle'] .= $atmail->parse("html/$atmail->Language/$atmail->LoginType/atmailstyle-form.css");
// Which email to read
$var['id'] = $_REQUEST['id'];
$var['folder'] = urldecode($_REQUEST['folder']);
$var['print'] = $_REQUEST['print'];
$var['cache'] = $_REQUEST['cache'];
$var['head'] = $_REQUEST['head'];
$var['rawemail'] = $_REQUEST['rawemail'];
if (file_exists($atmail->tmpdir . ".ht.".$auth->SessionID) || $_REQUEST['pgppass'])
$var['PgpPass'] = 1 ;
// LeaveMsgs = 0 is not spported as yet
$atmail->LeaveMsgs = 1;
// Specify to open mail messages in new window, or parent location
$type = ( $atmail->LoginType == "simple") ? 1 : 0;
$head = ($var['head']) ? $var['head'] : $atmail->Ajax;
// Build a new ReadMsg object
$email = new ReadMsg(array(
'Username' => $atmail->username,
'Pop3host' => $atmail->pop3host,
'Password' => $auth->password,
'Type' => $atmail->MailType,
'Mode' => $atmail->Mode,
'SessionID' => $auth->SessionID,
'Language' => $atmail->Language,
'DateFormat' => $atmail->DateFormat,
'TimeFormat' => $atmail->TimeFormat,
'LoginType' => $type,
'head' => $head,
'rawemail' => $var['rawemail'])
);
// Fetch the message
$email->reademail( $var['id'], $var['folder'], '', $var['cache'] );
// An error occured while logging into the server
if ( $email->status )
print $atmail->parse( "html/$atmail->Language/auth_misc.html", array('status' => "Remote mail-server not responding - Check connection - $email->status"));
// Escape any < > chars
foreach ( array('to', 'cc', 'from', 'subject', 'priority', 'date', 'type') as $name)
{
if (!$atmail->Ajax) {
$email->$name = str_replace(array('<', '>', '"'), array('<', '>', '"'), $email->$name);
}
// Escape any newlines, they will mess up the javascript
$email->$name = trim($email->$name);
}
if ($atmail->LoginType == 'xul') {
$email->subject = str_replace("'", '', $email->subject);
}
$email->ToSelect = $email->to;
$email->CcSelect = $email->cc;
parse_attachments($email);
// Only display the text message if multipart/alternative
// encoding. Such as Microsoft Outlook sends the messages
if ($email->mimetype == 'multipart/alternative' && $email->html || $email->mimetype == 'multipart/mixed' && $email->html )
$email->txt = '';
// If reading a message from the drafts, use the correct template
//if ( $var['folder'] == "Drafts" && $atmail->LoginType != "simple")
// $var['misc'] = "_drafts" ;
$var['filename'] = "html/$atmail->Language/$atmail->LoginType/readmail_ajax.html";
foreach(array('cc', 'to') as $type)
{
$name = "emaillist";
if ($type == "cc")
$name = $name."cc" ;
$var[$name] = $email->$type;
if (!$atmail->Ajax)
{
if ( strlen($var[$name]) > 103 )
{
$var[$name] = substr($var[$name], 0, 100 );
$var[$name] .= " ...";
}
}
}
// display mail headers
if ( $_REQUEST['head'] )
{
print "<pre>$email->headers</pre>";
$atmail->end();
}
elseif ( $_REQUEST['rawemail'] )
{
print $email->rawbody;
$atmail->end();
}
elseif( $email->Virus )
{
print $atmail->parse("html/$atmail->Language/virusmessage.html",
array(
'VirusName' => $email->VirusName,
'EmailFrom' => $email->emailfrom,
'atmailstyle' => $var['atmailstyle'])
);
}
else
{
// parse any email forwarded as attachment (message/rfc822)
// and append to the original message
foreach ($email->attachedemails as $path) {
extract_eml($path);
}
// Translate the email-priority
if ($email->priority == "Normal")
$email->priority = $atmail->parse("html/$atmail->Language/msg/normalpriority.html");
elseif($email->priority == "Low")
$email->priority = $atmail->parse("html/$atmail->Language/msg/lowpriority.html");
elseif($email->priority == "High")
$email->priority = $atmail->parse("html/$atmail->Language/msg/highpriority.html");
// Take out ' characters, they break the Javascript for the Ajax abook
$fromaddress = str_replace("'", '', $email->from);
if ($atmail->Ajax)
{
$email->subject = str_replace(array('>', '<'), array('>', '<'), $email->subject);
$email->from = str_replace(array('>', '<'), array('>', '<'), $email->from);
$var['emaillist'] = str_replace(array('>', '<'), array('>', '<'), $var['emaillist']);
$var['emaillistcc'] = str_replace(array('>', '<'), array('>', '<'), $var['emaillistcc']);
}
// Load the replyto field
$var['EmailReplyTo'] = ($email->replyto)? $email->replyto : $email->from;
// If using IMAP, get the message flag from the ReadMsg.php module ( need to get it before the message for the correct flag )
if($atmail->MailType == 'imap')
$MessageState = $email->MessageState;
else
$MessageState = $email->mail->getuidl( $email->UIDL, $var['id'] );
if($email->txt)
$HTMLtoTextReply = $email->clean_html_to_text($email->txt);
else
$HTMLtoTextReply = $email->clean_html_to_text($email->html);
print $atmail->parse(
$var['filename'],
array(
'id' => $var['id'],
'folder' => $var['folder'],
'folderstate' => str_replace(' ', '_', $var['folder']), // Required for IE Adv int, unread count on JS tree
'EmailSubject' => cleanText($email->subject),
'EmailSubjectRaw' => $email->decode_htmlspecialchars($email->subject),
'EmailTo' => cleanText($email->ToSelect),
'EmailToList' => cleanText($var['emaillist']),
'EmailCc' => cleanText($email->CcSelect),
'EmailCcList' => cleanText($var['emaillistcc']),
'EmailFrom' => cleanText($email->from),
'EmailFromAddress' => cleanText($fromaddress),
'EmailAddress' => cleanText($email->emailfrom),
'EmailHtml' => ReadMsg::strip_style(cleanText($email->html)),
'EmailTxt' => cleanText($email->txt),
'EmailDate' => $email->date,
'EmailType' => $email->type,
'Virus' => $email->Virus,
'EmailPriority' => $email->priority,
'atmailstyle' => $var['atmailstyle'],
'Attachments' => $var['attachments'],
'print' => $var['print'],
'MailEncrypt' => $email->MailEncrypt,
'SmimeEncrypt' => $email->SmimeEncrypt,
'Encrypt' => $email->Encrypt,
'PgpPass' => $var['PgpPass'],
'EmailUIDL' => $email->UIDL,
'EmailCache' => $email->EmailCache,
'Charset' => $email->Charset,
'BlockImages' => $atmail->BlockImages,
'RawAttachments' => $var['rawattachments'],
'VideoMail' => $email->VideoMail,
'headers' => nl2br(cleanText($email->headers)),
//'headers' => nl2br($email->headers),
'MessageState' => $MessageState,
'EmailReplyTo' => cleanText($var['EmailReplyTo']),
'ImageAttachments' => $var['image_attachments'],
'HTMLtoTextReply' => cleanText($HTMLtoTextReply)));
// If mail account POP3/SQL or maildir, update the flag as read ( IMAP will do auto )
if($atmail->MailType != 'imap')
$email->mail->updateuidl( $email->UIDL, 'o', '', $var['folder'], $var['id'] );
}
$email->mail->quit();
$atmail->end();
function parse_attachments(&$obj)
{
global $var, $atmail;
// Add links to download messages forwarded as
// attachments (they are also displayed inline)
foreach ($obj->attachedemails as $path) {
if (!file_exists($path)) {
continue;
}
$name = basename($path);
// don't allow .htaccess execution
if ($name == '.htaccess') {
chmod($path, 0444);
}
$size = filesize($path);
// Make a string of raw attachments for JS functions
$var['rawattachments'] .= "$name::";
$var['attachments'] .= $atmail->parse(
"html/$atmail->Language/readmail-attachment.html",
array(
'Path' => $path,
'FileName' => rawurlencode($name),
'Size' => $size,
'Desc' => '',
'Download' => $obj->myescape($name),
'Name' => $name,
'Icon' => 'txt.gif',
'Target' => '_blank')
);
}
foreach ($obj->attachname as $k => $v)
{
if ( !$v['path'] || $v['inline'])
continue;
// Unescape the filename if required
$name = $obj->myescape($v['name']);
// Take away the number extension, if the attachment name already exists on the server
$name = preg_replace('/-\d+\.(\w+)/', '\.$1', $name);
if ( $v['type'] == "file")
{
if ($v['fwdmsg']) {
$var['icon'] = "txt.gif";
} else {
$icon = $atmail->icon_hash();
if ( preg_match('/\.(\w+)$/', $v['name'], $match))
$var['ext'] = $match[1];
// Load the icon for the filename
$var['icon'] = $icon[strtolower($var['ext'])];
if ( !$var['icon'] )
$var['icon'] = "txt.gif";
}
// Find the target to open the message
$target = '';
if (!preg_match('/html|htm|text|txt/', $var['ext']) || $atmail->LoginType == "simple" || $atmail->LoginType == "blue_pane" )
$target = "_blank" ;
$obj->attachname[$k]['name'] = $obj->myescape( $obj->attachname[$k]['name'] );
if (!$v['mime'])
$obj->attachname[$k]['mime'] = $name;
// Make a string of raw attachments for JS functions
$var['rawattachments'] .= "{$obj->attachname[$k]['name']}::";
// don't allow .htaccess execution
if (preg_match('/.htaccess/', $v['path'])) {
chmod($v['path'], 0444);
}
$var['attachments'] .= $atmail->parse(
"html/$atmail->Language/readmail-attachment.html",
array(
'Path' => $v['path'],
'FileName' => rawurlencode($v['rawname']),
'Size' => $v['size'],
'Desc' => $v['desc'],
'Download' => $obj->myescape($v['name']),
'Name' => $v['name'],
'Icon' => $var['icon'],
'Target' => $target)
);
}
else
{
//$displayname = $v['mime'] ? $v['mime'] : $name; // Correctly read the MIME header for the real filename , not yet implemented
$displayname = $obj->myunescape($name);
$download = $obj->myescape($v['name']);
// Make a string of raw attachments for JS functions
$var['rawattachments'] .= "{$obj->attachname[$k]['name']}::";
$var['image_attachments'] .= "<hr noshade width='95%'><font class='sw'>{$v['desc']} ($displayname):</font><br>
<a target=\"_blank\" href=\"mime.php?file={$v['rawname']}&name=$download\" title=\"download image ({$v['size']}kb)\">
<img src='mime.php?file=" . rawurlencode($v['rawname']) . "&name=" . $obj->myescape($v['desc']) . "' style='max-width: 95%;'>
</a>";
}
}
}
function cleanText($txt)
{
foreach (range(0, 31) as $chr) {
// Leave in linefeeds
if ($chr == 10) {
continue;
}
$txt = str_replace(chr($chr), '', $txt);
}
return str_replace(']]>', '', $txt);
}
function extract_eml($path)
{
global $email, $atmail, $auth;
$email2 = new ReadMsg(array(
'Username' => $atmail->username,
'Pop3host' => $atmail->pop3host,
'Password' => $auth->password,
'Type' => $atmail->MailType,
'Mode' => $atmail->Mode,
'SessionID' => $auth->SessionID,
'Language' => $atmail->Language,
'DateFormat' => $atmail->DateFormat,
'TimeFormat' => $atmail->TimeFormat,
'LoginType' => $type,
'head' => 1,
'rawemail' => $var['rawemail'])
);
$email2->reademail('', '' ,'', '', $path);
if ($email2->html)
$type = 'html';
elseif ($email2->txt)
$type = 'txt';
elseif ($email2->multiparttxt)
$type = 'multiparttxt';
else {
$type = 'html';
}
$myvars = array('subject' => $email2->subject, 'from' => htmlentities($email2->from), 'to' => htmlentities($email2->to), 'date' => $email2->date, 'body' => $email2->$type);
$fwdmsg = $atmail->parse("html/$atmail->Language/fwdmsg.html", $myvars);
if ($email->html)
$email->html .= $fwdmsg;
elseif ($email->txt)
$email->txt .= $fwdmsg;
else
$email->html .= $fwdmsg;
unset($fwdmsg);
parse_attachments($email2);
foreach ($email2->attachedemails as $path2) {
extract_eml($path2);
}
}
?>