mail.inc 22 KB

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