-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailboxreader.class.php
493 lines (417 loc) · 16.3 KB
/
mailboxreader.class.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
<?php
/** POP3 / IMAP Mailbox reader
* @author Elias Toft Hansen
*
*/
define('MBREADER_ATTACHMENT_DIR','/srv/www/example.com/attachments/');
define('MBREADER_ATTACHMENTS_URI_PATH', 'http://example.com/attachments/');
class MBMessage {
/**
*
* @var MailboxReader
*/
private $mailboxReader;
/**
* The message ID
* @var string
*/
public $msgId;
/**
* Subject of the message
* @var string
*/
public $subject;
/**
* Plain text message body
* @var string
*/
public $plain;
/**
* HTML message body
* @var string
*/
public $html;
/**
* Other parts such as attachments and images embedded in the HTML version of the mail
* format is part_id => part_data
* eg.:
* array('1.2' => stdObj)
* @var array
*/
public $parts = array();
public $attachments = array();
public $attachmentDownloads = array();
/**
* assoc array containing the parts of the mail (But only relevant parts, multiparts will be left out.)
* @var array
*/
public $partIds = array();
public $sender;
public $from;
public $to = array();
public $header;
/**
* Constructs a new MBMessage.
* @param MailboxReader $mbreader
*/
public function __construct($mbreader){
$this->mailboxReader = $mbreader;
}
/**
* Save an attachment specified by partId
* @param string $partId
* @param string $filename
* @param string $path
* @return string returns the filename of the downloaded attachment.
* @throws MailboxReaderException
*/
public function saveAttachment($partId,$filename=false,$path=MBREADER_ATTACHMENT_DIR){
if(!isset($this->parts[$partId])) throw new MailboxReaderException ('Invalid partId.');
$part = $this->parts[$partId];
if($part->ifparameters)
$params = MailboxReader::parseParametersAssoc($part->parameters);
if(!$filename){
$fname = $params['name']
.($part->ifid ? md5($part->id) : ($part->bytes.'_'.rand(0,99999))).'.'.$part->subtype;
$filename = md5(time().'_'.$fname).'.'.$part->subtype;
}
//Treat as tainted
$filename = str_replace(array('/','\\',"\0"),'_',$filename);
$data = imap_fetchbody($mbreader->getIMAPRessource(),$this->msgId,$partId);
switch($part->encoding){
case 3 /*'base64'*/:
$data = imap_base64($data);
break;
case 4 /*'quoted-printable'*/:
$data = imap_qprint($data);
break;
}
file_put_contents($path.'/'.$filename, $data);
return $filename;
}
/**
* Sets flags on messages
* Default is \\SEEN.
* See http://php.net/manual/en/function.imap-setflag-full.php
* @param type $flag
*/
public function setFlag($flag){
$this->mailboxReader->setFlag($this->msgId,$flag);
}
/**
* Mark a message for deletion from current mailbox
* see http://php.net/manual/en/function.imap-delete.php
* @param int $options
*/
public function delete($options = 0){
$this->mailboxReader->delete($this->msgid,$options);
}
/**
* Mark message as seen;
*/
public function setFlagSeen(){
$this->mailboxReader->setFlag($this->msgId, "\\SEEN");
}
}
class MailboxReader {
/**
* IMAP Stream
* @var ressource IMAP Stream
*/
private $mail;
private $options;
private $sanitizerCallback;
/**
*
* @param string $mailbox
* @param string $username
* @param string $password
* @param array $options
* @throws MailboxReaderException
*/
public function __construct($mailbox, $username, $password, $options=array()){
$this->mail = imap_open($mailbox, $username, $password);
if(!$this->mail){
/* Something went wrong, throw an exception with debug info */
$message = 'IMAP ERROR: '.print_r(imap_errors(),true);
throw new MailboxReaderException($message, 0);
}
$this->options = array_merge($this->getDefaultOptions(),$options);
}
/**
* Adds an optional callback handler for sanitizing HTML output.
* Using something like HTMLawed or similar is strongly recommended.
* @param callable $callbackFunc
*/
public function addSanitizerCallback($callbackFunc){
$this->sanitizerCallback = $callbackFunc;
}
/**
* Defines the default options
* @return array
*/
private function getDefaultOptions(){
return array(
'charset' => 'UTF-8'
);
}
/**
* Returns the IMAP ressorce used in this mailboxreader.
* @return ressource IMAP Ressource identifier
*/
public function getIMAPRessource(){
return $this->mail;
}
/**
* Fetches mail headers from the IMAP/POP3 account.
* @param bool $unread Default:true, only fetch new mails (This may, and may NOT work on POP3, depending on the daemon, so be careful)
* @return array Returns an array of StdObject with header info. See imap_header()
*/
public function fetchHeaders($unread=true){
$headerInfo = imap_headers($this->mail);
$return = array();
foreach($headerInfo as $n => $str){
$msgid = $n+1;
$header = imap_header($this->mail, $msgid);
if(!$unread || ($unread && ($header->Recent == 'N' || $header->Unseen == 'U')))
$return[$msgid] = $header;
}
return $return;
}
/**
* See PHP documentation for imap_search
* http://php.net/manual/en/function.imap-search.php
* @param string $criteria
* @return boolean Return FALSE if it does not understand the search criteria or no messages have been found.
*/
public function search($criteria){
return imap_search($this->mail, $criteria);
}
/**
* Transforms the parameters from imap_fetchstructure into an assoc key => value type array
* See http://php.net/manual/en/function.imap-fetchstructure.php
* @param array $paramArray The parameters to parse.
*/
static public function parseParametersAssoc($paramArray){
$return = array();
foreach($paramArray as $paramObj){
$return[$paramObj->attribute] = $paramObj->value;
}
return $return;
}
/**
* Returns child parts which is not multipart (This should filter out all the garbage and only return content elements)
* @param type $part stdObject
* @param mixed $prefix
* $param array $exludeTypes Types to exclude from the result (eg. 2 for Exclude message type.)
* @return array Assoc array with part-id => partinfo
*/
static private function findChildPartsRecurse($part,$prefix=false,$excludeTypes=array()){
$parts = array();
if($part->type == 1){
$i = 1;
if($prefix === false) $i = 1;
foreach($part->parts as $p){
/* Continue on excluded type, Excluding a type is useful for filtering out attached messages etc. */
if(in_array($p->type,$excludeTypes)) continue;
$id = ($prefix !== false ? $prefix.'.' : '') . $i;
$childParts = MailboxReader::findChildPartsRecurse($p,$id,$excludeTypes);
if($p->type != 1){
$parts[$id] = $childParts;
} else {
$parts = $childParts;
}
$i++;
}
} else {
/* If there is no parts at all (eg. PLAINTEXT mail), then return as an assoc array anyways. */
$parts = $prefix === false
? array('1' => $part)
: $part;
}
return $parts;
}
/**
* converts HTML messages to a prettier clear text alternative.
* @param string $document
* @return string
*/
static private function html2txt($document){
$search = array(
'@\r\n|\n@' => ' ',
'@<script[^>]*?>.*?</script>@si' => '', // Strip out javascript
'@<![\s\S]*?--[ \t\n\r]*>@' => '', // Strip multi-line comments including CDATA
'@<style[^>]*?>.*?</style>@siU' => '', // Strip style tags properly,
'@<[\/\!]*?(br|p)[^<>]*?>@si' => "\r\n",
'@<[\/\!]*?[^<>]*?>@si' => '', // Strip out HTML tags
'@[ \t]+@' => ' '
);
$text = preg_replace(array_keys($search), array_values($search), $document);
return $text;
}
/**
* fetches the mail $msgid returns a MBMessage object with the message.
* @param string $msgid
* @param boolean $getAttachments
* @return MBMessage
*/
public function fetchMail($msgid,$getAttachments=false){
/**
* Array used to determine the encoding. This is based of the PHP documentation
* at http://dk1.php.net/manual/en/function.imap-fetchstructure.php
* which states that theese may vary!
*/
$encodings = array("7bit","8bit","binary","base64","quoted-printable","other");
/**
* Array used to determine the mime type. This is based on the documentation mentioned above
*
*/
$mime = array("Text","Multipart","Message","Application","Audio","Image","Video","Other");
/* Fetch mail structure */
$structure = imap_fetchstructure($this->mail, $msgid);
$header = imap_header($this->mail, $msgid);
$parts = MailboxReader::findChildPartsRecurse($structure);
$nparts = count($parts);
$isMultipart = ($nparts > 0);
$message = new MBMessage($this->mail);
$message->msgId = $msgid;
$message->header = $header;
$subject = imap_utf8($header->subject);
//Run sanitizer, if set.
if($this->sanitizerCallback !== null && is_callable($this->sanitizerCallback)){
$cb = $this->sanitizerCallback;
$subject = $cb($subject);
}
$message->subject = $subject;
foreach($parts as $pid => $part){
$it = $mime[$part->type];
$is = strtolower($part->subtype);
$ie = $encodings[$part->encoding];
$mimeType = "$it/$is";
/* If part is mail body or alternative... */
if(!$part->ifdisposition && ($is == 'plain' || $is == 'html')){
$content = imap_fetchbody($this->mail,$msgid,$pid);
if(!$content) throw new MailboxReaderException (print_r(imap_last_error (),true));
//Assume default charset is used in message encoding. Used as a fallback
$encoding = $this->options['charset'];
if($part->ifparameters){
$params = MailboxReader::parseParametersAssoc($part->parameters);
if($params['charset']) $encoding = $params['charset'];
}
/*
* Convert transfer encoding
*/
switch($ie){
case 'base64':
$content = imap_base64($content);
break;
case 'quoted-printable':
$content = imap_qprint($content);
break;
}
/**
* Attemps to use iconv to convert the message body into the desired encoding.
*
*/
if(0 !== strcasecmp($encoding,$this->options['charset']) && @!$this->options['disable_iconv']){
$detectedEncoding = mb_detect_encoding($content,'UTF-8, ASCII, Windows-1252, ISO-8859-1, ISO-8859-2',true);
if($detectedEncoding != $this->options['charset']){
$content = iconv($detectedEncoding, $this->options['charset'].'//TRANSLIT//IGNORE', $content);
}
}
//Run sanitizer, if set.
if($this->sanitizerCallback !== null && is_callable($this->sanitizerCallback)){
$cb = $this->sanitizerCallback;
$content = $cb($content);
}
/**
* Convert plain into HTML and vice versa. So both are always availible,
*/
if($is == 'plain'){
$message->plain = $content;
if(!$message->html)
$message->html = nl2br($content);
} else {
$message->html = $content;
if(!$message->plain)
$message->plain = MailboxReader::html2txt($content);
}
} else {
$message->parts[$pid] = $part;
if($part->ifid) $message->partIds[str_replace(array('<','>'),'',$part->id)] = $pid;
if($part->ifdisposition && $part->disposition == 'attachment'){
$dparams = array();
$params = array();
if($part->ifdparameters)
$dparams = MailboxReader::parseParametersAssoc($part->dparameters);
if($part->ifparameters)
$params = MailboxReader::parseParametersAssoc($part->parameters);
$message->attachments[$pid] = ($dparams['filename']
? $dparams['filename']
: ($params['name'] ? $params['name'] : 'unknown-filename'));
}
}
}
$message->from = $header->from;
$message->to = $header->to;
if($getAttachments){
foreach($message->attachments as $k => $attachment){
$message->attachmentDownloads[$k] = MBREADER_ATTACHMENTS_URI_PATH
.$message->saveAttachment($k, false, MBREADER_ATTACHMENT_DIR);
}
foreach($message->partIds as $id => $part){
$message->attachmentDownloads[$part] = MBREADER_ATTACHMENTS_URI_PATH
.$message->saveAttachment($part, false, MBREADER_ATTACHMENT_DIR);
}
if(count($message->partIds) > 0){
foreach($message->partIds as $id => $part){
$fp=$message->attachmentDownloads[$part];
/**
* Replace cid: attachment references with paths determined by
* MBREADER_ATTACHMENTS_URI_PATH
*/
$message->html = str_replace('cid:'.$id, $fp, $message->html);
}
}
}
return $message;
}
/**
* Closes the IMAP stream
* see http://php.net/manual/en/function.imap-close.php
* @param int $flag If set to CL_EXPUNGE, the function will silently expunge
* the mailbox before closing, removing all messages marked for deletion.
* You can achieve the same thing by using imap_expunge()
* @return boolean
*/
public function close($flag = null){
return imap_close($this->mail,$flag);
}
/**
* Delete all messages marked for deletion
*/
public function expunge(){
imap_expunge($this->mail);
}
/**
* Mark a message for deletion from current mailbox
* see http://php.net/manual/en/function.imap-delete.php
* @param string $msgid
* @param int $options
*/
public function delete($msgid,$options=0){
imap_delete($this->mail,trim($msgid),$options);
}
/**
* Sets flags on messages
* Default is \\SEEN.
* See http://php.net/manual/en/function.imap-setflag-full.php
* @param string $msgId
* @param string $flag See PHP manual
*/
public function setFlag($msgId,$flag="\\SEEN"){
imap_setflag_full($this->mail, (string) $msgId, $flag);
}
}
class MailboxReaderException extends Exception { }
?>