| 1 |
<?php
|
| 2 |
// $Id: mail.inc,v 1.27 2009/11/02 02:37:36 webchick Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* API functions for processing and sending e-mail.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Auto-detect appropriate line endings for e-mails.
|
| 11 |
*
|
| 12 |
* $conf['mail_line_endings'] will override this setting.
|
| 13 |
*/
|
| 14 |
define('MAIL_LINE_ENDINGS', isset($_SERVER['WINDIR']) || strpos($_SERVER['SERVER_SOFTWARE'], 'Win32') !== FALSE ? "\r\n" : "\n");
|
| 15 |
|
| 16 |
/**
|
| 17 |
* Compose and optionally send an e-mail message.
|
| 18 |
*
|
| 19 |
* Sending an e-mail works with defining an e-mail template (subject, text
|
| 20 |
* and possibly e-mail headers) and the replacement values to use in the
|
| 21 |
* appropriate places in the template. Processed e-mail templates are
|
| 22 |
* requested from hook_mail() from the module sending the e-mail. Any module
|
| 23 |
* can modify the composed e-mail message array using hook_mail_alter().
|
| 24 |
* Finally drupal_mail_system()->mail() sends the e-mail, which can
|
| 25 |
* be reused if the exact same composed e-mail is to be sent to multiple
|
| 26 |
* recipients.
|
| 27 |
*
|
| 28 |
* Finding out what language to send the e-mail with needs some consideration.
|
| 29 |
* If you send e-mail to a user, her preferred language should be fine, so
|
| 30 |
* use user_preferred_language(). If you send email based on form values
|
| 31 |
* filled on the page, there are two additional choices if you are not
|
| 32 |
* sending the e-mail to a user on the site. You can either use the language
|
| 33 |
* used to generate the page ($language global variable) or the site default
|
| 34 |
* language. See language_default(). The former is good if sending e-mail to
|
| 35 |
* the person filling the form, the later is good if you send e-mail to an
|
| 36 |
* address previously set up (like contact addresses in a contact form).
|
| 37 |
*
|
| 38 |
* Taking care of always using the proper language is even more important
|
| 39 |
* when sending e-mails in a row to multiple users. Hook_mail() abstracts
|
| 40 |
* whether the mail text comes from an administrator setting or is
|
| 41 |
* static in the source code. It should also deal with common mail tokens,
|
| 42 |
* only receiving $params which are unique to the actual e-mail at hand.
|
| 43 |
*
|
| 44 |
* An example:
|
| 45 |
*
|
| 46 |
* @code
|
| 47 |
* function example_notify($accounts) {
|
| 48 |
* foreach ($accounts as $account) {
|
| 49 |
* $params['account'] = $account;
|
| 50 |
* // example_mail() will be called based on the first drupal_mail() parameter.
|
| 51 |
* drupal_mail('example', 'notice', $account->mail, user_preferred_language($account), $params);
|
| 52 |
* }
|
| 53 |
* }
|
| 54 |
*
|
| 55 |
* function example_mail($key, &$message, $params) {
|
| 56 |
* $language = $message['language'];
|
| 57 |
* $variables = user_mail_tokens($params['account'], $language);
|
| 58 |
* switch($key) {
|
| 59 |
* case 'notice':
|
| 60 |
* $message['subject'] = t('Notification from !site', $variables, $language->language);
|
| 61 |
* $message['body'][] = t("Dear !username\n\nThere is new content available on the site.", $variables, $language->language);
|
| 62 |
* break;
|
| 63 |
* }
|
| 64 |
* }
|
| 65 |
* @endcode
|
| 66 |
*
|
| 67 |
* @param $module
|
| 68 |
* A module name to invoke hook_mail() on. The {$module}_mail() hook will be
|
| 69 |
* called to complete the $message structure which will already contain common
|
| 70 |
* defaults.
|
| 71 |
* @param $key
|
| 72 |
* A key to identify the e-mail sent. The final e-mail id for e-mail altering
|
| 73 |
* will be {$module}_{$key}.
|
| 74 |
* @param $to
|
| 75 |
* The e-mail address or addresses where the message will be sent to. The
|
| 76 |
* formatting of this string must comply with RFC 2822. Some examples are:
|
| 77 |
* - user@example.com
|
| 78 |
* - user@example.com, anotheruser@example.com
|
| 79 |
* - User <user@example.com>
|
| 80 |
* - User <user@example.com>, Another User <anotheruser@example.com>
|
| 81 |
* @param $language
|
| 82 |
* Language object to use to compose the e-mail.
|
| 83 |
* @param $params
|
| 84 |
* Optional parameters to build the e-mail.
|
| 85 |
* @param $from
|
| 86 |
* Sets From to this value, if given.
|
| 87 |
* @param $send
|
| 88 |
* Send the message directly, without calling drupal_mail_system()->mail()
|
| 89 |
* manually.
|
| 90 |
* @return
|
| 91 |
* The $message array structure containing all details of the
|
| 92 |
* message. If already sent ($send = TRUE), then the 'result' element
|
| 93 |
* will contain the success indicator of the e-mail, failure being already
|
| 94 |
* written to the watchdog. (Success means nothing more than the message being
|
| 95 |
* accepted at php-level, which still doesn't guarantee it to be delivered.)
|
| 96 |
*/
|
| 97 |
function drupal_mail($module, $key, $to, $language, $params = array(), $from = NULL, $send = TRUE) {
|
| 98 |
$default_from = variable_get('site_mail', ini_get('sendmail_from'));
|
| 99 |
|
| 100 |
// Bundle up the variables into a structured array for altering.
|
| 101 |
$message = array(
|
| 102 |
'id' => $module . '_' . $key,
|
| 103 |
'module' => $module,
|
| 104 |
'key' => $key,
|
| 105 |
'to' => $to,
|
| 106 |
'from' => isset($from) ? $from : $default_from,
|
| 107 |
'language' => $language,
|
| 108 |
'params' => $params,
|
| 109 |
'subject' => '',
|
| 110 |
'body' => array()
|
| 111 |
);
|
| 112 |
|
| 113 |
// Build the default headers
|
| 114 |
$headers = array(
|
| 115 |
'MIME-Version' => '1.0',
|
| 116 |
'Content-Type' => 'text/plain; charset=UTF-8; format=flowed; delsp=yes',
|
| 117 |
'Content-Transfer-Encoding' => '8Bit',
|
| 118 |
'X-Mailer' => 'Drupal'
|
| 119 |
);
|
| 120 |
if ($default_from) {
|
| 121 |
// To prevent e-mail from looking like spam, the addresses in the Sender and
|
| 122 |
// Return-Path headers should have a domain authorized to use the originating
|
| 123 |
// SMTP server. Errors-To is redundant, but shouldn't hurt.
|
| 124 |
$headers['From'] = $headers['Sender'] = $headers['Return-Path'] = $headers['Errors-To'] = $default_from;
|
| 125 |
}
|
| 126 |
if ($from) {
|
| 127 |
$headers['From'] = $from;
|
| 128 |
}
|
| 129 |
$message['headers'] = $headers;
|
| 130 |
|
| 131 |
// Build the e-mail (get subject and body, allow additional headers) by
|
| 132 |
// invoking hook_mail() on this module. We cannot use module_invoke() as
|
| 133 |
// we need to have $message by reference in hook_mail().
|
| 134 |
if (function_exists($function = $module . '_mail')) {
|
| 135 |
$function($key, $message, $params);
|
| 136 |
}
|
| 137 |
|
| 138 |
// Invoke hook_mail_alter() to allow all modules to alter the resulting e-mail.
|
| 139 |
drupal_alter('mail', $message);
|
| 140 |
|
| 141 |
// Retrieve the responsible implementation for this message.
|
| 142 |
$system = drupal_mail_system($module, $key);
|
| 143 |
|
| 144 |
// Format the message body.
|
| 145 |
$message = $system->format($message);
|
| 146 |
|
| 147 |
// Optionally send e-mail.
|
| 148 |
if ($send) {
|
| 149 |
$message['result'] = $system->mail($message);
|
| 150 |
|
| 151 |
// Log errors
|
| 152 |
if (!$message['result']) {
|
| 153 |
watchdog('mail', 'Error sending e-mail (from %from to %to).', array('%from' => $message['from'], '%to' => $message['to']), WATCHDOG_ERROR);
|
| 154 |
drupal_set_message(t('Unable to send e-mail. Please contact the site administrator if the problem persists.'), 'error');
|
| 155 |
}
|
| 156 |
}
|
| 157 |
|
| 158 |
return $message;
|
| 159 |
}
|
| 160 |
|
| 161 |
/**
|
| 162 |
* Returns an object that implements the MailSystemInterface.
|
| 163 |
*
|
| 164 |
* Allows for one or more custom mail backends to format and send mail messages
|
| 165 |
* composed using drupal_mail().
|
| 166 |
*
|
| 167 |
* An implementation needs to implement the following methods:
|
| 168 |
* - format: Allows to preprocess, format, and postprocess a mail
|
| 169 |
* message before it is passed to the sending system. By default, all messages
|
| 170 |
* may contain HTML and are converted to plain-text by the DefaultMailSystem
|
| 171 |
* implementation. For example, an alternative implementation could override
|
| 172 |
* the default implementation and additionally sanitize the HTML for usage in
|
| 173 |
* a MIME-encoded e-mail, but still invoking the DefaultMailSystem
|
| 174 |
* implementation to generate an alternate plain-text version for sending.
|
| 175 |
* - mail: Sends a message through a custom mail sending engine.
|
| 176 |
* By default, all messages are sent via PHP's mail() function by the
|
| 177 |
* DefaultMailSystem implementation.
|
| 178 |
*
|
| 179 |
* The selection of a particular implementation is controlled via the variable
|
| 180 |
* 'mail_system', which is a keyed array. The default implementation
|
| 181 |
* is the class whose name is the value of 'default-system' key. A more specific
|
| 182 |
* match first to key and then to module will be used in preference to the
|
| 183 |
* default. To specificy a different class for all mail sent by one module, set
|
| 184 |
* the class name as the value for the key corresponding to the module name. To
|
| 185 |
* specificy a class for a particular message sent by one module, set the class
|
| 186 |
* name as the value for the array key that is the message id, which is
|
| 187 |
* "${module}_${key}".
|
| 188 |
*
|
| 189 |
* For example to debug all mail sent by the user module by logging it to a
|
| 190 |
* file, you might set the variable as something like:
|
| 191 |
*
|
| 192 |
* @code
|
| 193 |
* array(
|
| 194 |
* 'default-system' => 'DefaultMailSystem',
|
| 195 |
* 'user' => 'DevelMailLog',
|
| 196 |
* );
|
| 197 |
* @endcode
|
| 198 |
*
|
| 199 |
* Finally, a different system can be specified for a specific e-mail ID (see
|
| 200 |
* the $key param), such as one of the keys used by the contact module:
|
| 201 |
*
|
| 202 |
* @code
|
| 203 |
* array(
|
| 204 |
* 'default-system' => 'DefaultMailSystem',
|
| 205 |
* 'user' => 'DevelMailLog',
|
| 206 |
* 'contact_page_autoreply' => 'DrupalDevNullMailSend',
|
| 207 |
* );
|
| 208 |
* @endcode
|
| 209 |
*
|
| 210 |
* Other possible uses for system include a mail-sending class that actually
|
| 211 |
* sends (or duplicates) each message to SMS, Twitter, instant message, etc, or
|
| 212 |
* a class that queues up a large number of messages for more efficient bulk
|
| 213 |
* sending or for sending via a remote gateway so as to reduce the load
|
| 214 |
* on the local server.
|
| 215 |
*
|
| 216 |
* @param $module
|
| 217 |
* The module name which was used by drupal_mail() to invoke hook_mail().
|
| 218 |
* @param $key
|
| 219 |
* A key to identify the e-mail sent. The final e-mail ID for the e-mail
|
| 220 |
* alter hook in drupal_mail() would have been {$module}_{$key}.
|
| 221 |
*/
|
| 222 |
function drupal_mail_system($module, $key) {
|
| 223 |
$instances = &drupal_static(__FUNCTION__, array());
|
| 224 |
|
| 225 |
$id = $module . '_' . $key;
|
| 226 |
|
| 227 |
$configuration = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
|
| 228 |
|
| 229 |
// Look for overrides for the default class, starting from the most specific
|
| 230 |
// id, and falling back to the module name.
|
| 231 |
if (isset($configuration[$id])) {
|
| 232 |
$class = $configuration[$id];
|
| 233 |
}
|
| 234 |
elseif (isset($configuration[$module])) {
|
| 235 |
$class = $configuration[$module];
|
| 236 |
}
|
| 237 |
else {
|
| 238 |
$class = $configuration['default-system'];
|
| 239 |
}
|
| 240 |
|
| 241 |
if (empty($instances[$class])) {
|
| 242 |
$interfaces = class_implements($class);
|
| 243 |
if (isset($interfaces['MailSystemInterface'])) {
|
| 244 |
$instances[$class] = new $class;
|
| 245 |
}
|
| 246 |
else {
|
| 247 |
throw new Exception(t('Class %class does not implement interface %interface', array('%class' => $class, '%interface' => 'MailSystemInterface')));
|
| 248 |
}
|
| 249 |
}
|
| 250 |
return $instances[$class];
|
| 251 |
}
|
| 252 |
|
| 253 |
/**
|
| 254 |
* An interface for pluggable mail back-ends.
|
| 255 |
*/
|
| 256 |
interface MailSystemInterface {
|
| 257 |
/**
|
| 258 |
* Format a message composed by drupal_mail() prior sending.
|
| 259 |
*
|
| 260 |
* @param $message
|
| 261 |
* A message array, as described in hook_mail_alter().
|
| 262 |
*
|
| 263 |
* @return
|
| 264 |
* The formatted $message.
|
| 265 |
*/
|
| 266 |
public function format(array $message);
|
| 267 |
|
| 268 |
/**
|
| 269 |
* Send a message composed by drupal_mail().
|
| 270 |
*
|
| 271 |
* @param $message
|
| 272 |
* Message array with at least the following elements:
|
| 273 |
* - id: A unique identifier of the e-mail type. Examples: 'contact_user_copy',
|
| 274 |
* 'user_password_reset'.
|
| 275 |
* - to: The mail address or addresses where the message will be sent to.
|
| 276 |
* The formatting of this string must comply with RFC 2822. Some examples:
|
| 277 |
* - user@example.com
|
| 278 |
* - user@example.com, anotheruser@example.com
|
| 279 |
* - User <user@example.com>
|
| 280 |
* - User <user@example.com>, Another User <anotheruser@example.com>
|
| 281 |
* - subject: Subject of the e-mail to be sent. This must not contain any
|
| 282 |
* newline characters, or the mail may not be sent properly.
|
| 283 |
* - body: Message to be sent. Accepts both CRLF and LF line-endings.
|
| 284 |
* E-mail bodies must be wrapped. You can use drupal_wrap_mail() for
|
| 285 |
* smart plain text wrapping.
|
| 286 |
* - headers: Associative array containing all additional mail headers not
|
| 287 |
* defined by one of the other parameters. PHP's mail() looks for Cc
|
| 288 |
* and Bcc headers and sends the mail to addresses in these headers too.
|
| 289 |
* @return
|
| 290 |
* TRUE if the mail was successfully accepted for delivery, otherwise FALSE.
|
| 291 |
*/
|
| 292 |
public function mail(array $message);
|
| 293 |
}
|
| 294 |
|
| 295 |
/**
|
| 296 |
* Perform format=flowed soft wrapping for mail (RFC 3676).
|
| 297 |
*
|
| 298 |
* We use delsp=yes wrapping, but only break non-spaced languages when
|
| 299 |
* absolutely necessary to avoid compatibility issues.
|
| 300 |
*
|
| 301 |
* We deliberately use LF rather than CRLF, see drupal_mail().
|
| 302 |
*
|
| 303 |
* @param $text
|
| 304 |
* The plain text to process.
|
| 305 |
* @param $indent (optional)
|
| 306 |
* A string to indent the text with. Only '>' characters are repeated on
|
| 307 |
* subsequent wrapped lines. Others are replaced by spaces.
|
| 308 |
*/
|
| 309 |
function drupal_wrap_mail($text, $indent = '') {
|
| 310 |
// Convert CRLF into LF.
|
| 311 |
$text = str_replace("\r", '', $text);
|
| 312 |
// See if soft-wrapping is allowed.
|
| 313 |
$clean_indent = _drupal_html_to_text_clean($indent);
|
| 314 |
$soft = strpos($clean_indent, ' ') === FALSE;
|
| 315 |
// Check if the string has line breaks.
|
| 316 |
if (strpos($text, "\n") !== FALSE) {
|
| 317 |
// Remove trailing spaces to make existing breaks hard.
|
| 318 |
$text = preg_replace('/ +\n/m', "\n", $text);
|
| 319 |
// Wrap each line at the needed width.
|
| 320 |
$lines = explode("\n", $text);
|
| 321 |
array_walk($lines, '_drupal_wrap_mail_line', array('soft' => $soft, 'length' => strlen($indent)));
|
| 322 |
$text = implode("\n", $lines);
|
| 323 |
}
|
| 324 |
else {
|
| 325 |
// Wrap this line.
|
| 326 |
_drupal_wrap_mail_line($text, 0, array('soft' => $soft, 'length' => strlen($indent)));
|
| 327 |
}
|
| 328 |
// Empty lines with nothing but spaces.
|
| 329 |
$text = preg_replace('/^ +\n/m', "\n", $text);
|
| 330 |
// Space-stuff special lines.
|
| 331 |
$text = preg_replace('/^(>| |From)/m', ' $1', $text);
|
| 332 |
// Apply indentation. We only include non-'>' indentation on the first line.
|
| 333 |
$text = $indent . substr(preg_replace('/^/m', $clean_indent, $text), strlen($indent));
|
| 334 |
|
| 335 |
return $text;
|
| 336 |
}
|
| 337 |
|
| 338 |
/**
|
| 339 |
* Transform an HTML string into plain text, preserving the structure of the
|
| 340 |
* markup. Useful for preparing the body of a node to be sent by e-mail.
|
| 341 |
*
|
| 342 |
* The output will be suitable for use as 'format=flowed; delsp=yes' text
|
| 343 |
* (RFC 3676) and can be passed directly to drupal_mail() for sending.
|
| 344 |
*
|
| 345 |
* We deliberately use LF rather than CRLF, see drupal_mail().
|
| 346 |
*
|
| 347 |
* This function provides suitable alternatives for the following tags:
|
| 348 |
* <a> <em> <i> <strong> <b> <br> <p> <blockquote> <ul> <ol> <li> <dl> <dt>
|
| 349 |
* <dd> <h1> <h2> <h3> <h4> <h5> <h6> <hr>
|
| 350 |
*
|
| 351 |
* @param $string
|
| 352 |
* The string to be transformed.
|
| 353 |
* @param $allowed_tags (optional)
|
| 354 |
* If supplied, a list of tags that will be transformed. If omitted, all
|
| 355 |
* all supported tags are transformed.
|
| 356 |
* @return
|
| 357 |
* The transformed string.
|
| 358 |
*/
|
| 359 |
function drupal_html_to_text($string, $allowed_tags = NULL) {
|
| 360 |
// Cache list of supported tags.
|
| 361 |
static $supported_tags;
|
| 362 |
if (empty($supported_tags)) {
|
| 363 |
$supported_tags = array('a', 'em', 'i', 'strong', 'b', 'br', 'p', 'blockquote', 'ul', 'ol', 'li', 'dl', 'dt', 'dd', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr');
|
| 364 |
}
|
| 365 |
|
| 366 |
// Make sure only supported tags are kept.
|
| 367 |
$allowed_tags = isset($allowed_tags) ? array_intersect($supported_tags, $allowed_tags) : $supported_tags;
|
| 368 |
|
| 369 |
// Make sure tags, entities and attributes are well-formed and properly nested.
|
| 370 |
$string = _filter_htmlcorrector(filter_xss($string, $allowed_tags));
|
| 371 |
|
| 372 |
// Apply inline styles.
|
| 373 |
$string = preg_replace('!</?(em|i)((?> +)[^>]*)?>!i', '/', $string);
|
| 374 |
$string = preg_replace('!</?(strong|b)((?> +)[^>]*)?>!i', '*', $string);
|
| 375 |
|
| 376 |
// Replace inline <a> tags with the text of link and a footnote.
|
| 377 |
// 'See <a href="http://drupal.org">the Drupal site</a>' becomes
|
| 378 |
// 'See the Drupal site [1]' with the URL included as a footnote.
|
| 379 |
_drupal_html_to_mail_urls(NULL, TRUE);
|
| 380 |
$pattern = '@(<a[^>]+?href="([^"]*)"[^>]*?>(.+?)</a>)@i';
|
| 381 |
$string = preg_replace_callback($pattern, '_drupal_html_to_mail_urls', $string);
|
| 382 |
$urls = _drupal_html_to_mail_urls();
|
| 383 |
$footnotes = '';
|
| 384 |
if (count($urls)) {
|
| 385 |
$footnotes .= "\n";
|
| 386 |
for ($i = 0, $max = count($urls); $i < $max; $i++) {
|
| 387 |
$footnotes .= '[' . ($i + 1) . '] ' . $urls[$i] . "\n";
|
| 388 |
}
|
| 389 |
}
|
| 390 |
|
| 391 |
// Split tags from text.
|
| 392 |
$split = preg_split('/<([^>]+?)>/', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
|
| 393 |
// Note: PHP ensures the array consists of alternating delimiters and literals
|
| 394 |
// and begins and ends with a literal (inserting $null as required).
|
| 395 |
|
| 396 |
$tag = FALSE; // Odd/even counter (tag or no tag)
|
| 397 |
$casing = NULL; // Case conversion function
|
| 398 |
$output = '';
|
| 399 |
$indent = array(); // All current indentation string chunks
|
| 400 |
$lists = array(); // Array of counters for opened lists
|
| 401 |
foreach ($split as $value) {
|
| 402 |
$chunk = NULL; // Holds a string ready to be formatted and output.
|
| 403 |
|
| 404 |
// Process HTML tags (but don't output any literally).
|
| 405 |
if ($tag) {
|
| 406 |
list($tagname) = explode(' ', strtolower($value), 2);
|
| 407 |
switch ($tagname) {
|
| 408 |
// List counters
|
| 409 |
case 'ul':
|
| 410 |
array_unshift($lists, '*');
|
| 411 |
break;
|
| 412 |
case 'ol':
|
| 413 |
array_unshift($lists, 1);
|
| 414 |
break;
|
| 415 |
case '/ul':
|
| 416 |
case '/ol':
|
| 417 |
array_shift($lists);
|
| 418 |
$chunk = ''; // Ensure blank new-line.
|
| 419 |
break;
|
| 420 |
|
| 421 |
// Quotation/list markers, non-fancy headers
|
| 422 |
case 'blockquote':
|
| 423 |
// Format=flowed indentation cannot be mixed with lists.
|
| 424 |
$indent[] = count($lists) ? ' "' : '>';
|
| 425 |
break;
|
| 426 |
case 'li':
|
| 427 |
$indent[] = is_numeric($lists[0]) ? ' ' . $lists[0]++ . ') ' : ' * ';
|
| 428 |
break;
|
| 429 |
case 'dd':
|
| 430 |
$indent[] = ' ';
|
| 431 |
break;
|
| 432 |
case 'h3':
|
| 433 |
$indent[] = '.... ';
|
| 434 |
break;
|
| 435 |
case 'h4':
|
| 436 |
$indent[] = '.. ';
|
| 437 |
break;
|
| 438 |
case '/blockquote':
|
| 439 |
if (count($lists)) {
|
| 440 |
// Append closing quote for inline quotes (immediately).
|
| 441 |
$output = rtrim($output, "> \n") . "\"\n";
|
| 442 |
$chunk = ''; // Ensure blank new-line.
|
| 443 |
}
|
| 444 |
// Fall-through
|
| 445 |
case '/li':
|
| 446 |
case '/dd':
|
| 447 |
array_pop($indent);
|
| 448 |
break;
|
| 449 |
case '/h3':
|
| 450 |
case '/h4':
|
| 451 |
array_pop($indent);
|
| 452 |
case '/h5':
|
| 453 |
case '/h6':
|
| 454 |
$chunk = ''; // Ensure blank new-line.
|
| 455 |
break;
|
| 456 |
|
| 457 |
// Fancy headers
|
| 458 |
case 'h1':
|
| 459 |
$indent[] = '======== ';
|
| 460 |
$casing = 'drupal_strtoupper';
|
| 461 |
break;
|
| 462 |
case 'h2':
|
| 463 |
$indent[] = '-------- ';
|
| 464 |
$casing = 'drupal_strtoupper';
|
| 465 |
break;
|
| 466 |
case '/h1':
|
| 467 |
case '/h2':
|
| 468 |
$casing = NULL;
|
| 469 |
// Pad the line with dashes.
|
| 470 |
$output = _drupal_html_to_text_pad($output, ($tagname == '/h1') ? '=' : '-', ' ');
|
| 471 |
array_pop($indent);
|
| 472 |
$chunk = ''; // Ensure blank new-line.
|
| 473 |
break;
|
| 474 |
|
| 475 |
// Horizontal rulers
|
| 476 |
case 'hr':
|
| 477 |
// Insert immediately.
|
| 478 |
$output .= drupal_wrap_mail('', implode('', $indent)) . "\n";
|
| 479 |
$output = _drupal_html_to_text_pad($output, '-');
|
| 480 |
break;
|
| 481 |
|
| 482 |
// Paragraphs and definition lists
|
| 483 |
case '/p':
|
| 484 |
case '/dl':
|
| 485 |
$chunk = ''; // Ensure blank new-line.
|
| 486 |
break;
|
| 487 |
}
|
| 488 |
}
|
| 489 |
// Process blocks of text.
|
| 490 |
else {
|
| 491 |
// Convert inline HTML text to plain text; not removing line-breaks or
|
| 492 |
// white-space, since that breaks newlines when sanitizing plain-text.
|
| 493 |
$value = trim(decode_entities($value));
|
| 494 |
if (drupal_strlen($value)) {
|
| 495 |
$chunk = $value;
|
| 496 |
}
|
| 497 |
}
|
| 498 |
|
| 499 |
// See if there is something waiting to be output.
|
| 500 |
if (isset($chunk)) {
|
| 501 |
// Apply any necessary case conversion.
|
| 502 |
if (isset($casing)) {
|
| 503 |
$chunk = $casing($chunk);
|
| 504 |
}
|
| 505 |
// Format it and apply the current indentation.
|
| 506 |
$output .= drupal_wrap_mail($chunk, implode('', $indent));
|
| 507 |
// Remove non-quotation markers from indentation.
|
| 508 |
$indent = array_map('_drupal_html_to_text_clean', $indent);
|
| 509 |
}
|
| 510 |
|
| 511 |
$tag = !$tag;
|
| 512 |
}
|
| 513 |
|
| 514 |
return $output . $footnotes;
|
| 515 |
}
|
| 516 |
|
| 517 |
/**
|
| 518 |
* Helper function for array_walk in drupal_wrap_mail().
|
| 519 |
*
|
| 520 |
* Wraps words on a single line.
|
| 521 |
*/
|
| 522 |
function _drupal_wrap_mail_line(&$line, $key, $values) {
|
| 523 |
// Use soft-breaks only for purely quoted or unindented text.
|
| 524 |
$line = wordwrap($line, 77 - $values['length'], $values['soft'] ? " \n" : "\n");
|
| 525 |
// Break really long words at the maximum width allowed.
|
| 526 |
$line = wordwrap($line, 996 - $values['length'], $values['soft'] ? " \n" : "\n");
|
| 527 |
}
|
| 528 |
|
| 529 |
/**
|
| 530 |
* Helper function for drupal_html_to_text().
|
| 531 |
*
|
| 532 |
* Keeps track of URLs and replaces them with placeholder tokens.
|
| 533 |
*/
|
| 534 |
function _drupal_html_to_mail_urls($match = NULL, $reset = FALSE) {
|
| 535 |
global $base_url, $base_path;
|
| 536 |
static $urls = array(), $regexp;
|
| 537 |
|
| 538 |
if ($reset) {
|
| 539 |
// Reset internal URL list.
|
| 540 |
$urls = array();
|
| 541 |
}
|
| 542 |
else {
|
| 543 |
if (empty($regexp)) {
|
| 544 |
$regexp = '@^' . preg_quote($base_path, '@') . '@';
|
| 545 |
}
|
| 546 |
if ($match) {
|
| 547 |
list(, , $url, $label) = $match;
|
| 548 |
// Ensure all URLs are absolute.
|
| 549 |
$urls[] = strpos($url, '://') ? $url : preg_replace($regexp, $base_url . '/', $url);
|
| 550 |
return $label . ' [' . count($urls) . ']';
|
| 551 |
}
|
| 552 |
}
|
| 553 |
return $urls;
|
| 554 |
}
|
| 555 |
|
| 556 |
/**
|
| 557 |
* Helper function for drupal_wrap_mail() and drupal_html_to_text().
|
| 558 |
*
|
| 559 |
* Replace all non-quotation markers from a given piece of indentation with spaces.
|
| 560 |
*/
|
| 561 |
function _drupal_html_to_text_clean($indent) {
|
| 562 |
return preg_replace('/[^>]/', ' ', $indent);
|
| 563 |
}
|
| 564 |
|
| 565 |
/**
|
| 566 |
* Helper function for drupal_html_to_text().
|
| 567 |
*
|
| 568 |
* Pad the last line with the given character.
|
| 569 |
*/
|
| 570 |
function _drupal_html_to_text_pad($text, $pad, $prefix = '') {
|
| 571 |
// Remove last line break.
|
| 572 |
$text = substr($text, 0, -1);
|
| 573 |
// Calculate needed padding space and add it.
|
| 574 |
if (($p = strrpos($text, "\n")) === FALSE) {
|
| 575 |
$p = -1;
|
| 576 |
}
|
| 577 |
$n = max(0, 79 - (strlen($text) - $p));
|
| 578 |
// Add prefix and padding, and restore linebreak.
|
| 579 |
return $text . $prefix . str_repeat($pad, $n - strlen($prefix)) . "\n";
|
| 580 |
}
|