-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailfeeds.inc
235 lines (215 loc) · 7.81 KB
/
mailfeeds.inc
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
<?php
// TODO: refactor mailhandler.inc and use it instead
/**
* Establish a connection to the mailbox specified by the array $mailbox
*/
function mailfeeds_open_mailbox($mailbox) {
if ($mailbox['domain']) {
if ($mailbox['imap'] == 1) {
$box = '{'. $mailbox['domain'] .':'. $mailbox['port'] . $mailbox['extraimap'] .'}'. $mailbox['folder'];
}
else {
$box = '{'. $mailbox['domain'] .':'. $mailbox['port'] .'/pop3'. $mailbox['extraimap'] .'}'. $mailbox['folder'];
}
$result = imap_open($box, $mailbox['name'], $mailbox['pass']);
$err = 'domain';
}
else {
$box = $mailbox['folder'];
$result = imap_open($box, '', '');
}
return $result;
}
function mailfeeds_get_unread_messages($result) {
$unread_messages = array();
$number_of_messages = imap_num_msg($result);
for ($i = 1; $i <= $number_of_messages; $i++) {
$header = imap_header($result, $i);
// only process new messages
if ($header->Unseen != 'U' && $header->Recent != 'N') {
continue;
}
$unread_messages[] = imap_uid($result, $i);
}
return $unread_messages;
}
function mailfeeds_retrieve_message(&$result, $i, $mailbox) {
$item = array();
$header = imap_header($result, imap_msgno($result, $i));
// Initialize the subject in case it's missing.
if (!isset($header->subject)) {
$header->subject = '';
}
$mime = explode(',', $mailbox['mime']);
// Get the first text part - this will be the node body
$origbody = mailfeeds_get_part($result, $i, $mime[0]);
// If we didn't get a body from our first attempt, try the alternate format (HTML or PLAIN)
if (!$origbody) {
$origbody = mailfeeds_get_part($result, $i, $mime[1]);
}
// Parse MIME parts, so all mailhandler modules have access to
// the full array of mime parts without having to process the email.
$mimeparts = mailfeeds_get_parts($result, $i);
$item['header'] = $header;
$item['mime'] = $mime;
$item['origbody'] = $origbody;
$item['mimeparts'] = $mimeparts;
return $item;
}
/**
* Returns the first part with the specified mime_type
*
* USAGE EXAMPLES - from php manual: imap_fetch_structure() comments
* $data = get_part($stream, $msg_number, "TEXT/PLAIN"); // get plain text
* $data = get_part($stream, $msg_number, "TEXT/HTML"); // get HTML text
*/
function mailfeeds_get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false) {
if (!$structure) {
$structure = imap_fetchstructure($stream, $msg_number, FT_UID);
}
if ($structure) {
$encoding = variable_get('mailhandler_default_encoding', 'UTF-8');
foreach ($structure->parameters as $parameter) {
if (strtoupper($parameter->attribute) == 'CHARSET') {
$encoding = $parameter->value;
}
}
if ($mime_type == mailfeeds_get_mime_type($structure)) {
if (!$part_number) {
$part_number = '1';
}
$text = imap_fetchbody($stream, $msg_number, $part_number, FT_UID);
if ($structure->encoding == ENCBASE64) {
return drupal_convert_to_utf8(imap_base64($text), $encoding);
}
else if ($structure->encoding == ENCQUOTEDPRINTABLE) {
return drupal_convert_to_utf8(quoted_printable_decode($text), $encoding);
}
else {
return drupal_convert_to_utf8($text, $encoding);
}
}
if ($structure->type == TYPEMULTIPART) { /* multipart */
$prefix = '';
while (list($index, $sub_structure) = each ($structure->parts)) {
if ($part_number) {
$prefix = $part_number .'.';
}
$data = mailfeeds_get_part($stream, $msg_number, $mime_type, $sub_structure, $prefix . ($index + 1));
if ($data) {
return $data;
}
}
}
}
return false;
}
/**
* Returns an array of parts as file objects
*
* @param
* @param $structure
* A message structure, usually used to recurse into specific parts
* @param $max_depth
* Maximum Depth to recurse into parts.
* @param $depth
* The current recursion depth.
* @param $part_number
* A message part number to track position in a message during recursion.
* @return
* An array of file objects.
*/
function mailfeeds_get_parts($stream, $msg_number, $max_depth = 10, $depth = 0, $structure = FALSE, $part_number = FALSE) {
$parts = array();
// Load Structure.
if (!$structure && !$structure = imap_fetchstructure($stream, $msg_number, FT_UID)) {
watchdog('mailhandler', 'Could not fetch structure for message number %msg_number', array('%msg_number' => $msg_number), WATCHDOG_NOTICE);
return $parts;
}
// Recurse into multipart messages.
if ($structure->type == TYPEMULTIPART) {
// Restrict recursion depth.
if ($depth >= $max_depth) {
watchdog('mailhandler', 'Maximum recursion depths met in mailhander_get_structure_part for message number %msg_number.', array('%msg_number' => $msg_number), WATCHDOG_NOTICE);
return $parts;
}
$prefix = '';
foreach($structure->parts as $index => $sub_structure) {
// If a part number was passed in and we are a multitype message, prefix the
// the part number for the recursive call to match the imap4 dot seperated part indexing.
if ($part_number) {
$prefix = $part_number .'.';
}
$sub_parts = mailfeeds_get_parts($stream, $msg_number, $max_depth, $depth + 1,
$sub_structure, $prefix . ($index + 1));
$parts = array_merge($parts, $sub_parts);
}
return $parts;
}
// Per Part Parsing.
// Initalize file object like part structure.
$part = new StdClass();
$part->attributes = array();
$part->filename = 'unnamed_attachment';
if (!$part->filemime = mailfeeds_get_mime_type($structure)) {
watchdog('mailhandler', 'Could not fetch mime type for message part. Defaulting to application/octet-stream.', array(), WATCHDOG_NOTICE);
$part->filemime = 'application/octet-stream';
}
if ($structure->ifparameters) {
foreach ($structure->parameters as $parameter) {
switch (strtoupper($parameter->attribute)) {
case 'NAME':
case 'FILENAME':
$part->filename = $parameter->value;
break;
default:
// put every thing else in the attributes array;
$part->attributes[$parameter->attribute] = $parameter->value;
}
}
}
// Handle Content-Disposition parameters for non-text types.
if ($structure->type != TYPETEXT && $structure->ifdparameters) {
foreach ($structure->dparameters as $parameter) {
switch (strtoupper($parameter->attribute)) {
case 'NAME':
case 'FILENAME':
$part->filename = $parameter->value;
break;
// put every thing else in the attributes array;
default:
$part->attributes[$parameter->attribute] = $parameter->value;
}
}
}
// Retrieve part and convert MIME encoding to UTF-8
if(!$part->data = imap_fetchbody($stream, $msg_number, $part_number, FT_UID)) {
watchdog('mailhandler', 'No Data!!', array(), WATCHDOG_ERROR);
return $parts;
}
// Decode as necessary.
if ($structure->encoding == ENCBASE64) {
$part->data = imap_base64($part->data);
}
elseif ($structure->encoding == ENCQUOTEDPRINTABLE) {
$part->data = quoted_printable_decode($part->data);
}
// Convert text attachment to UTF-8.
elseif ($structure->type == TYPETEXT) {
$part->data = imap_utf8($part->data);
}
//always return an array to satisfy array_merge in recursion catch, and array return value.
$parts[] = $part;
return $parts;
}
/**
* Retrieve MIME type of the message structure.
*/
function mailfeeds_get_mime_type(&$structure) {
static $primary_mime_type = array('TEXT', 'MULTIPART', 'MESSAGE', 'APPLICATION', 'AUDIO', 'IMAGE', 'VIDEO', 'OTHER');
$type_id = (int)$structure->type;
if (isset($primary_mime_type[$type_id]) && !empty($structure->subtype)) {
return $primary_mime_type[$type_id] .'/'. $structure->subtype;
}
return 'TEXT/PLAIN';
}