Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Utils_Mail_Incoming { | |
36 | CONST | |
37 | EMAILPROCESSOR_CREATE_INDIVIDUAL = 1, | |
38 | EMAILPROCESSOR_OVERRIDE = 2, | |
39 | EMAILPROCESSOR_IGNORE = 3; | |
40 | ||
5bc392e6 EM |
41 | /** |
42 | * @param $mail | |
43 | * @param $attachments | |
44 | * | |
45 | * @return string | |
46 | */ | |
6a488035 TO |
47 | function formatMail($mail, &$attachments) { |
48 | $t = ''; | |
49 | $t .= "From: " . self::formatAddress($mail->from) . "\n"; | |
50 | $t .= "To: " . self::formatAddresses($mail->to) . "\n"; | |
51 | $t .= "Cc: " . self::formatAddresses($mail->cc) . "\n"; | |
52 | $t .= "Bcc: " . self::formatAddresses($mail->bcc) . "\n"; | |
53 | $t .= 'Date: ' . date(DATE_RFC822, $mail->timestamp) . "\n"; | |
54 | $t .= 'Subject: ' . $mail->subject . "\n"; | |
55 | $t .= "MessageId: " . $mail->messageId . "\n"; | |
56 | $t .= "\n"; | |
57 | $t .= self::formatMailPart($mail->body, $attachments); | |
58 | return $t; | |
59 | } | |
60 | ||
5bc392e6 EM |
61 | /** |
62 | * @param $part | |
63 | * @param $attachments | |
64 | * | |
65 | * @throws Exception | |
66 | */ | |
a5989abf | 67 | public static function formatMailPart($part, &$attachments) { |
6a488035 TO |
68 | if ($part instanceof ezcMail) { |
69 | return self::formatMail($part, $attachments); | |
70 | } | |
71 | ||
72 | if ($part instanceof ezcMailText) { | |
73 | return self::formatMailText($part, $attachments); | |
74 | } | |
75 | ||
76 | if ($part instanceof ezcMailFile) { | |
77 | return self::formatMailFile($part, $attachments); | |
78 | } | |
79 | ||
80 | if ($part instanceof ezcMailRfc822Digest) { | |
81 | return self::formatMailRfc822Digest($part, $attachments); | |
82 | } | |
83 | ||
84 | if ($part instanceof ezcMailMultiPart) { | |
85 | return self::formatMailMultipart($part, $attachments); | |
86 | } | |
87 | ||
88 | CRM_Core_Error::fatal(ts("No clue about the %1", array(1 => get_class($part)))); | |
89 | } | |
90 | ||
5bc392e6 EM |
91 | /** |
92 | * @param $part | |
93 | * @param $attachments | |
94 | * | |
95 | * @throws Exception | |
96 | */ | |
6a488035 TO |
97 | function formatMailMultipart($part, &$attachments) { |
98 | if ($part instanceof ezcMailMultiPartAlternative) { | |
99 | return self::formatMailMultipartAlternative($part, $attachments); | |
100 | } | |
101 | ||
102 | if ($part instanceof ezcMailMultiPartDigest) { | |
103 | return self::formatMailMultipartDigest($part, $attachments); | |
104 | } | |
105 | ||
106 | if ($part instanceof ezcMailMultiPartRelated) { | |
107 | return self::formatMailMultipartRelated($part, $attachments); | |
108 | } | |
109 | ||
110 | if ($part instanceof ezcMailMultiPartMixed) { | |
111 | return self::formatMailMultipartMixed($part, $attachments); | |
112 | } | |
113 | ||
114 | if ($part instanceof ezcMailMultipartReport) { | |
115 | return self::formatMailMultipartReport($part, $attachments); | |
116 | } | |
117 | ||
118 | CRM_Core_Error::fatal(ts("No clue about the %1", array(1 => get_class($part)))); | |
119 | } | |
120 | ||
5bc392e6 EM |
121 | /** |
122 | * @param $part | |
123 | * @param $attachments | |
124 | * | |
125 | * @return string | |
126 | */ | |
6a488035 TO |
127 | function formatMailMultipartMixed($part, &$attachments) { |
128 | $t = ''; | |
129 | foreach ($part->getParts() as $key => $alternativePart) { | |
130 | $t .= self::formatMailPart($alternativePart, $attachments); | |
131 | } | |
132 | return $t; | |
133 | } | |
134 | ||
5bc392e6 EM |
135 | /** |
136 | * @param $part | |
137 | * @param $attachments | |
138 | * | |
139 | * @return string | |
140 | */ | |
6a488035 TO |
141 | function formatMailMultipartRelated($part, &$attachments) { |
142 | $t = ''; | |
143 | $t .= "-RELATED MAIN PART-\n"; | |
144 | $t .= self::formatMailPart($part->getMainPart(), $attachments); | |
145 | foreach ($part->getRelatedParts() as $key => $alternativePart) { | |
146 | $t .= "-RELATED PART $key-\n"; | |
147 | $t .= self::formatMailPart($alternativePart, $attachments); | |
148 | } | |
149 | $t .= "-RELATED END-\n"; | |
150 | return $t; | |
151 | } | |
152 | ||
5bc392e6 EM |
153 | /** |
154 | * @param $part | |
155 | * @param $attachments | |
156 | * | |
157 | * @return string | |
158 | */ | |
6a488035 TO |
159 | function formatMailMultipartDigest($part, &$attachments) { |
160 | $t = ''; | |
161 | foreach ($part->getParts() as $key => $alternativePart) { | |
162 | $t .= "-DIGEST-$key-\n"; | |
163 | $t .= self::formatMailPart($alternativePart, $attachments); | |
164 | } | |
165 | $t .= "-DIGEST END---\n"; | |
166 | return $t; | |
167 | } | |
168 | ||
5bc392e6 EM |
169 | /** |
170 | * @param $part | |
171 | * @param $attachments | |
172 | * | |
173 | * @return string | |
174 | */ | |
6a488035 TO |
175 | function formatMailRfc822Digest($part, &$attachments) { |
176 | $t = ''; | |
177 | $t .= "-DIGEST-ITEM-\n"; | |
178 | $t .= "Item:\n\n"; | |
179 | $t .= self::formatMailpart($part->mail, $attachments); | |
180 | $t .= "-DIGEST ITEM END-\n"; | |
181 | return $t; | |
182 | } | |
183 | ||
5bc392e6 EM |
184 | /** |
185 | * @param $part | |
186 | * @param $attachments | |
187 | * | |
188 | * @return string | |
189 | */ | |
6a488035 TO |
190 | function formatMailMultipartAlternative($part, &$attachments) { |
191 | $t = ''; | |
192 | foreach ($part->getParts() as $key => $alternativePart) { | |
193 | $t .= "-ALTERNATIVE ITEM $key-\n"; | |
194 | $t .= self::formatMailPart($alternativePart, $attachments); | |
195 | } | |
196 | $t .= "-ALTERNATIVE END-\n"; | |
197 | return $t; | |
198 | } | |
199 | ||
5bc392e6 EM |
200 | /** |
201 | * @param $part | |
202 | * @param $attachments | |
203 | * | |
204 | * @return string | |
205 | */ | |
a5989abf | 206 | public static function formatMailText($part, &$attachments) { |
6a488035 TO |
207 | $t = "\n{$part->text}\n"; |
208 | return $t; | |
209 | } | |
210 | ||
5bc392e6 EM |
211 | /** |
212 | * @param $part | |
213 | * @param $attachments | |
214 | * | |
215 | * @return string | |
216 | */ | |
6a488035 TO |
217 | function formatMailMultipartReport($part, &$attachments) { |
218 | $t = ''; | |
219 | foreach ($part->getParts() as $key => $reportPart) { | |
220 | $t .= "-REPORT-$key-\n"; | |
221 | $t .= self::formatMailPart($reportPart, $attachments); | |
222 | } | |
223 | $t .= "-REPORT END---\n"; | |
224 | return $t; | |
225 | } | |
226 | ||
5bc392e6 EM |
227 | /** |
228 | * @param $part | |
229 | * @param $attachments | |
230 | * | |
231 | * @return null | |
232 | */ | |
6a488035 TO |
233 | function formatMailFile($part, &$attachments) { |
234 | $attachments[] = array( | |
235 | 'dispositionType' => $part->dispositionType, | |
236 | 'contentType' => $part->contentType, | |
237 | 'mimeType' => $part->mimeType, | |
238 | 'contentID' => $part->contentId, | |
239 | 'fullName' => $part->fileName, | |
240 | ); | |
241 | return NULL; | |
242 | } | |
243 | ||
5bc392e6 EM |
244 | /** |
245 | * @param $addresses | |
246 | * | |
247 | * @return string | |
248 | */ | |
6a488035 TO |
249 | function formatAddresses($addresses) { |
250 | $fa = array(); | |
251 | foreach ($addresses as $address) { | |
252 | $fa[] = self::formatAddress($address); | |
253 | } | |
254 | return implode(', ', $fa); | |
255 | } | |
256 | ||
5bc392e6 EM |
257 | /** |
258 | * @param $address | |
259 | * | |
260 | * @return string | |
261 | */ | |
6a488035 TO |
262 | function formatAddress($address) { |
263 | $name = ''; | |
264 | if (!empty($address->name)) { | |
265 | $name = "{$address->name} "; | |
266 | } | |
267 | return $name . "<{$address->email}>"; | |
268 | } | |
269 | ||
5bc392e6 EM |
270 | /** |
271 | * @param $file | |
272 | * | |
273 | * @return array | |
274 | * @throws Exception | |
275 | */ | |
6a488035 TO |
276 | function &parse(&$file) { |
277 | ||
278 | // check that the file exists and has some content | |
279 | if (!file_exists($file) || | |
280 | !trim(file_get_contents($file)) | |
281 | ) { | |
282 | return CRM_Core_Error::createAPIError(ts('%1 does not exists or is empty', | |
283 | array(1 => $file) | |
284 | )); | |
285 | } | |
286 | ||
287 | require_once 'ezc/Base/src/ezc_bootstrap.php'; | |
288 | require_once 'ezc/autoload/mail_autoload.php'; | |
289 | ||
290 | // explode email to digestable format | |
291 | $set = new ezcMailFileSet(array($file)); | |
292 | $parser = new ezcMailParser(); | |
293 | $mail = $parser->parseMail($set); | |
294 | ||
295 | if (!$mail) { | |
296 | return CRM_Core_Error::createAPIError(ts('%1 could not be parsed', | |
297 | array(1 => $file) | |
298 | )); | |
299 | } | |
300 | ||
301 | // since we only have one fileset | |
302 | $mail = $mail[0]; | |
303 | ||
304 | $mailParams = self::parseMailingObject($mail); | |
305 | return $mailParams; | |
306 | } | |
307 | ||
5bc392e6 EM |
308 | /** |
309 | * @param $mail | |
310 | * | |
311 | * @return array | |
312 | */ | |
a5989abf | 313 | public static function parseMailingObject(&$mail) { |
6a488035 TO |
314 | |
315 | $config = CRM_Core_Config::singleton(); | |
316 | ||
317 | // get ready for collecting data about this email | |
318 | // and put it in a standardized format | |
319 | $params = array('is_error' => 0); | |
320 | ||
321 | $params['from'] = array(); | |
322 | self::parseAddress($mail->from, $field, $params['from'], $mail); | |
323 | ||
324 | // we definitely need a contact id for the from address | |
325 | // if we dont have one, skip this email | |
326 | if (empty($params['from']['id'])) { | |
327 | return; | |
328 | } | |
329 | ||
330 | $emailFields = array('to', 'cc', 'bcc'); | |
331 | foreach ($emailFields as $field) { | |
332 | $value = $mail->$field; | |
333 | self::parseAddresses($value, $field, $params, $mail); | |
334 | } | |
335 | ||
336 | // define other parameters | |
337 | $params['subject'] = $mail->subject; | |
338 | $params['date'] = date("YmdHi00", | |
339 | strtotime($mail->getHeader("Date")) | |
340 | ); | |
341 | $attachments = array(); | |
342 | $params['body'] = self::formatMailPart($mail->body, $attachments); | |
343 | ||
344 | // format and move attachments to the civicrm area | |
345 | if (!empty($attachments)) { | |
346 | $date = date('Ymdhis'); | |
347 | $config = CRM_Core_Config::singleton(); | |
348 | for ($i = 0; $i < count($attachments); $i++) { | |
349 | $attachNum = $i + 1; | |
350 | $fileName = basename($attachments[$i]['fullName']); | |
351 | $newName = CRM_Utils_File::makeFileName($fileName); | |
352 | $location = $config->uploadDir . $newName; | |
353 | ||
354 | // move file to the civicrm upload directory | |
355 | rename($attachments[$i]['fullName'], $location); | |
356 | ||
357 | $mimeType = "{$attachments[$i]['contentType']}/{$attachments[$i]['mimeType']}"; | |
358 | ||
359 | $params["attachFile_$attachNum"] = array( | |
360 | 'uri' => $fileName, | |
361 | 'type' => $mimeType, | |
362 | 'upload_date' => $date, | |
363 | 'location' => $location, | |
364 | ); | |
365 | } | |
366 | } | |
367 | ||
368 | return $params; | |
369 | } | |
370 | ||
5bc392e6 EM |
371 | /** |
372 | * @param $address | |
c490a46a | 373 | * @param array $params |
5bc392e6 EM |
374 | * @param $subParam |
375 | * @param $mail | |
376 | */ | |
a5989abf | 377 | public static function parseAddress(&$address, &$params, &$subParam, &$mail) { |
6a488035 TO |
378 | // CRM-9484 |
379 | if (empty($address->email)) { | |
380 | return; | |
381 | } | |
382 | ||
383 | $subParam['email'] = $address->email; | |
384 | $subParam['name'] = $address->name; | |
385 | ||
386 | ||
387 | $contactID = self::getContactID($subParam['email'], | |
388 | $subParam['name'], | |
389 | TRUE, | |
390 | ||
391 | ); | |
392 | $subParam['id'] = $contactID ? $contactID : NULL; | |
393 | } | |
394 | ||
5bc392e6 EM |
395 | /** |
396 | * @param $addresses | |
397 | * @param $token | |
c490a46a | 398 | * @param array $params |
5bc392e6 EM |
399 | * @param $mail |
400 | */ | |
a5989abf | 401 | public static function parseAddresses(&$addresses, $token, &$params, &$mail) { |
6a488035 TO |
402 | $params[$token] = array(); |
403 | ||
404 | foreach ($addresses as $address) { | |
405 | $subParam = array(); | |
406 | self::parseAddress($address, $params, $subParam, $mail); | |
407 | $params[$token][] = $subParam; | |
408 | } | |
409 | } | |
410 | ||
411 | /** | |
100fef9d | 412 | * Retrieve a contact ID and if not present |
6a488035 TO |
413 | * create one with this email |
414 | */ | |
a5989abf | 415 | public static function getContactID($email, $name = NULL, $create = TRUE, &$mail) { |
6a488035 TO |
416 | $dao = CRM_Contact_BAO_Contact::matchContactOnEmail($email, 'Individual'); |
417 | ||
418 | $contactID = NULL; | |
419 | if ($dao) { | |
420 | $contactID = $dao->contact_id; | |
421 | } | |
422 | ||
423 | $result = NULL; | |
424 | CRM_Utils_Hook::emailProcessorContact($email, $contactID, $result); | |
425 | ||
426 | if (!empty($result)) { | |
427 | if ($result['action'] == self::EMAILPROCESSOR_IGNORE) { | |
428 | return NULL; | |
429 | } | |
430 | if ($result['action'] == self::EMAILPROCESSOR_OVERRIDE) { | |
431 | return $result['contactID']; | |
432 | } | |
433 | ||
434 | // else this is now create individual | |
435 | // so we just fall out and do what we normally do | |
436 | } | |
437 | ||
438 | if ($contactID) { | |
439 | return $contactID; | |
440 | } | |
441 | ||
442 | if (!$create) { | |
443 | return NULL; | |
444 | } | |
445 | ||
446 | // contact does not exist, lets create it | |
447 | $params = array( | |
448 | 'contact_type' => 'Individual', | |
449 | 'email-Primary' => $email, | |
450 | ); | |
451 | ||
452 | CRM_Utils_String::extractName($name, $params); | |
453 | ||
454 | return CRM_Contact_BAO_Contact::createProfileContact($params, | |
455 | CRM_Core_DAO::$_nullArray | |
456 | ); | |
457 | } | |
458 | } | |
459 |