| 1 |
<?php |
<?php |
| 2 |
/* $Id: mimemail.inc,v 1.36 2009/02/22 00:04:10 jerdavis Exp $ */ |
/* $Id: mimemail.inc,v 1.40 2009/02/23 16:11:19 jerdavis Exp $ */ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 108 |
*/ |
*/ |
| 109 |
function _mimemail_file($url = NULL, $name = '', $type = '', $disposition = 'related') { |
function _mimemail_file($url = NULL, $name = '', $type = '', $disposition = 'related') { |
| 110 |
static $files = array(); |
static $files = array(); |
| 111 |
|
static $filenames = array(); |
| 112 |
|
|
| 113 |
if ($url) { |
if ($url) { |
| 114 |
$url = _mimemail_url($url, 'TRUE'); |
$url = _mimemail_url($url, 'TRUE'); |
| 124 |
} |
} |
| 125 |
|
|
| 126 |
if ($file && file_exists($file)) { |
if ($file && file_exists($file)) { |
| 127 |
|
// Prevent duplicate items |
| 128 |
|
if (isset($filenames[$file])) return 'cid:'. $filenames[$file]; |
| 129 |
|
|
| 130 |
$content_id = md5($file) .'@'. $_SERVER['HTTP_HOST']; |
$content_id = md5($file) .'@'. $_SERVER['HTTP_HOST']; |
| 131 |
|
|
| 132 |
if (!$name) $name = substr($file, strrpos($file, '/') + 1); |
if (!$name) $name = substr($file, strrpos($file, '/') + 1); |
| 137 |
'Content-ID' => $content_id, |
'Content-ID' => $content_id, |
| 138 |
'Content-Disposition' => $disposition, |
'Content-Disposition' => $disposition, |
| 139 |
); |
); |
| 140 |
$new_file['Content-Type'] = _mimemail_mimetype($file, $type); |
$new_file['Content-Type'] = file_get_mimetype($file); |
| 141 |
|
|
| 142 |
$files[] = $new_file; |
$files[] = $new_file; |
| 143 |
|
$filenames[$file] = $content_id; |
| 144 |
|
|
| 145 |
return 'cid:'. $content_id; |
return 'cid:'. $content_id; |
| 146 |
} |
} |
| 233 |
} |
} |
| 234 |
|
|
| 235 |
if (!isset($part['Content-Type'])) { |
if (!isset($part['Content-Type'])) { |
| 236 |
$part['Content-Type'] = _mimemail_mimetype($part['file'], $type); |
$part['Content-Type'] = file_get_mimetype($part['file']); |
| 237 |
} |
} |
| 238 |
|
|
| 239 |
if (isset($part['name'])) { |
if (isset($part['name'])) { |
| 273 |
// todo: remove this preg_replace once filter_xss() is properly handling |
// todo: remove this preg_replace once filter_xss() is properly handling |
| 274 |
// direct descendant css selectors '>' in inline CSS. For now this cleans |
// direct descendant css selectors '>' in inline CSS. For now this cleans |
| 275 |
// up our plain text part. See mimemail #364198, drupal #370903 |
// up our plain text part. See mimemail #364198, drupal #370903 |
| 276 |
$text = preg_replace('|<style.*</style>|', '', $body); |
$text = preg_replace('|<style.*</style>|mis', '', $body); |
| 277 |
$text = drupal_html_to_text($text); |
$text = drupal_html_to_text($text); |
| 278 |
} |
} |
| 279 |
if ($plaintext) { |
if ($plaintext) { |
| 374 |
|
|
| 375 |
// Make sure our text and html parts are accounted for |
// Make sure our text and html parts are accounted for |
| 376 |
if (isset($mail['html']) && !isset($mail['text'])) { |
if (isset($mail['html']) && !isset($mail['text'])) { |
| 377 |
$mail['text'] = preg_replace('|<style.*</style>|', '', $mail['html']); |
$mail['text'] = preg_replace('|<style.*</style>|mis', '', $mail['html']); |
| 378 |
$mail['text'] = drupal_html_to_text($mail['text']); |
$mail['text'] = drupal_html_to_text($mail['text']); |
| 379 |
} |
} |
| 380 |
elseif (isset($mail['text']) && !isset($mail['html'])) { |
elseif (isset($mail['text']) && !isset($mail['html'])) { |
| 528 |
} |
} |
| 529 |
|
|
| 530 |
/** |
/** |
| 531 |
* Attempt to determine the mimetime from or filename . While not ideal, |
* Formats an address string. |
| 532 |
* using the filename as a fallback ensures that images will appear inline |
* TODO could use some enhancement and stress testing. |
|
* in HTML messages |
|
| 533 |
* |
* |
| 534 |
* @param $name Name of the file |
* @param $address |
| 535 |
* |
* A user object, a text email address or an array containing name, mail. |
| 536 |
* @return best-guess mimetype string |
* @return |
| 537 |
|
* A formatted address string or FALSE. |
| 538 |
*/ |
*/ |
| 539 |
function _mimemail_mimetype($file, $type = '') { |
function mimemail_address($address) { |
| 540 |
if ($type) { |
|
| 541 |
return $type; |
if (is_array($address)) { |
| 542 |
|
|
| 543 |
|
// it's an array containing 'mail' and/or 'name' |
| 544 |
|
if (isset($address['mail'])) { |
| 545 |
|
$output = ''; |
| 546 |
|
if (empty($address['name'])) { |
| 547 |
|
return $address['mail']; |
| 548 |
|
} |
| 549 |
|
else { |
| 550 |
|
return '"'. addslashes(mime_header_encode($address['name'])) .'" <'. $address['mail'] .'>'; |
| 551 |
|
} |
| 552 |
|
} |
| 553 |
|
|
| 554 |
|
// it's an array of address items |
| 555 |
|
$addresses = array(); |
| 556 |
|
foreach ($address as $a) { |
| 557 |
|
$addresses[] = mimemail_address($a); |
| 558 |
|
} |
| 559 |
|
return $addresses; |
| 560 |
} |
} |
| 561 |
|
|
| 562 |
if (function_exists('mime_content_type')) { |
// it's a user object |
| 563 |
return mime_content_type($file); |
if (is_object($address) && isset($address->mail)) { |
| 564 |
|
return '"'. addslashes(mime_header_encode($address->name)) .'" <'. $address->mail .'>'; |
| 565 |
} |
} |
| 566 |
|
|
| 567 |
// some common embedded/attachment types |
// it's formatted or unformatted string |
| 568 |
$types = array( |
// TODO shouldn't assume it's valid - should try to re-parse |
| 569 |
'jpg' => 'image/jpeg', |
if (is_string($address)) { |
| 570 |
'jpeg' => 'image/jpeg', |
return $address; |
| 571 |
'gif' => 'image/gif', |
} |
|
'png' => 'image/png', |
|
|
); |
|
|
$ext = strtolower(substr(strrchr($file, '.'), 1)); |
|
| 572 |
|
|
| 573 |
if (isset($types[$ext])) { |
// it's null. return the site default address |
| 574 |
return $types[$ext]; |
if (is_null($address)) { |
| 575 |
|
return array( |
| 576 |
|
'name' => mime_header_encode(variable_get('site_name', 'Drupal')), |
| 577 |
|
'mail' => variable_get('site_mail', ini_get('sendmail_from')), |
| 578 |
|
); |
| 579 |
} |
} |
| 580 |
|
|
| 581 |
return 'application/octet-stream'; |
return FALSE; |
| 582 |
} |
} |