mail.inc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. * Composes and optionally sends 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 interface.
  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. * Performs 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. * @return
  326. * The content of the email as a string with formatting applied.
  327. */
  328. function drupal_wrap_mail($text, $indent = '') {
  329. // Convert CRLF into LF.
  330. $text = str_replace("\r", '', $text);
  331. // See if soft-wrapping is allowed.
  332. $clean_indent = _drupal_html_to_text_clean($indent);
  333. $soft = strpos($clean_indent, ' ') === FALSE;
  334. // Check if the string has line breaks.
  335. if (strpos($text, "\n") !== FALSE) {
  336. // Remove trailing spaces to make existing breaks hard.
  337. $text = preg_replace('/ +\n/m', "\n", $text);
  338. // Wrap each line at the needed width.
  339. $lines = explode("\n", $text);
  340. array_walk($lines, '_drupal_wrap_mail_line', array('soft' => $soft, 'length' => strlen($indent)));
  341. $text = implode("\n", $lines);
  342. }
  343. else {
  344. // Wrap this line.
  345. _drupal_wrap_mail_line($text, 0, array('soft' => $soft, 'length' => strlen($indent)));
  346. }
  347. // Empty lines with nothing but spaces.
  348. $text = preg_replace('/^ +\n/m', "\n", $text);
  349. // Space-stuff special lines.
  350. $text = preg_replace('/^(>| |From)/m', ' $1', $text);
  351. // Apply indentation. We only include non-'>' indentation on the first line.
  352. $text = $indent . substr(preg_replace('/^/m', $clean_indent, $text), strlen($indent));
  353. return $text;
  354. }
  355. /**
  356. * Transforms an HTML string into plain text, preserving its structure.
  357. *
  358. * The output will be suitable for use as 'format=flowed; delsp=yes' text
  359. * (RFC 3676) and can be passed directly to drupal_mail() for sending.
  360. *
  361. * We deliberately use LF rather than CRLF, see drupal_mail().
  362. *
  363. * This function provides suitable alternatives for the following tags:
  364. * <a> <em> <i> <strong> <b> <br> <p> <blockquote> <ul> <ol> <li> <dl> <dt>
  365. * <dd> <h1> <h2> <h3> <h4> <h5> <h6> <hr>
  366. *
  367. * @param $string
  368. * The string to be transformed.
  369. * @param $allowed_tags (optional)
  370. * If supplied, a list of tags that will be transformed. If omitted, all
  371. * all supported tags are transformed.
  372. *
  373. * @return
  374. * The transformed string.
  375. */
  376. function drupal_html_to_text($string, $allowed_tags = NULL) {
  377. // Cache list of supported tags.
  378. static $supported_tags;
  379. if (empty($supported_tags)) {
  380. $supported_tags = array('a', 'em', 'i', 'strong', 'b', 'br', 'p', 'blockquote', 'ul', 'ol', 'li', 'dl', 'dt', 'dd', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr');
  381. }
  382. // Make sure only supported tags are kept.
  383. $allowed_tags = isset($allowed_tags) ? array_intersect($supported_tags, $allowed_tags) : $supported_tags;
  384. // Make sure tags, entities and attributes are well-formed and properly nested.
  385. $string = _filter_htmlcorrector(filter_xss($string, $allowed_tags));
  386. // Apply inline styles.
  387. $string = preg_replace('!</?(em|i)((?> +)[^>]*)?>!i', '/', $string);
  388. $string = preg_replace('!</?(strong|b)((?> +)[^>]*)?>!i', '*', $string);
  389. // Replace inline <a> tags with the text of link and a footnote.
  390. // 'See <a href="http://drupal.org">the Drupal site</a>' becomes
  391. // 'See the Drupal site [1]' with the URL included as a footnote.
  392. _drupal_html_to_mail_urls(NULL, TRUE);
  393. $pattern = '@(<a[^>]+?href="([^"]*)"[^>]*?>(.+?)</a>)@i';
  394. $string = preg_replace_callback($pattern, '_drupal_html_to_mail_urls', $string);
  395. $urls = _drupal_html_to_mail_urls();
  396. $footnotes = '';
  397. if (count($urls)) {
  398. $footnotes .= "\n";
  399. for ($i = 0, $max = count($urls); $i < $max; $i++) {
  400. $footnotes .= '[' . ($i + 1) . '] ' . $urls[$i] . "\n";
  401. }
  402. }
  403. // Split tags from text.
  404. $split = preg_split('/<([^>]+?)>/', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
  405. // Note: PHP ensures the array consists of alternating delimiters and literals
  406. // and begins and ends with a literal (inserting $null as required).
  407. $tag = FALSE; // Odd/even counter (tag or no tag)
  408. $casing = NULL; // Case conversion function
  409. $output = '';
  410. $indent = array(); // All current indentation string chunks
  411. $lists = array(); // Array of counters for opened lists
  412. foreach ($split as $value) {
  413. $chunk = NULL; // Holds a string ready to be formatted and output.
  414. // Process HTML tags (but don't output any literally).
  415. if ($tag) {
  416. list($tagname) = explode(' ', strtolower($value), 2);
  417. switch ($tagname) {
  418. // List counters
  419. case 'ul':
  420. array_unshift($lists, '*');
  421. break;
  422. case 'ol':
  423. array_unshift($lists, 1);
  424. break;
  425. case '/ul':
  426. case '/ol':
  427. array_shift($lists);
  428. $chunk = ''; // Ensure blank new-line.
  429. break;
  430. // Quotation/list markers, non-fancy headers
  431. case 'blockquote':
  432. // Format=flowed indentation cannot be mixed with lists.
  433. $indent[] = count($lists) ? ' "' : '>';
  434. break;
  435. case 'li':
  436. $indent[] = isset($lists[0]) && is_numeric($lists[0]) ? ' ' . $lists[0]++ . ') ' : ' * ';
  437. break;
  438. case 'dd':
  439. $indent[] = ' ';
  440. break;
  441. case 'h3':
  442. $indent[] = '.... ';
  443. break;
  444. case 'h4':
  445. $indent[] = '.. ';
  446. break;
  447. case '/blockquote':
  448. if (count($lists)) {
  449. // Append closing quote for inline quotes (immediately).
  450. $output = rtrim($output, "> \n") . "\"\n";
  451. $chunk = ''; // Ensure blank new-line.
  452. }
  453. // Fall-through
  454. case '/li':
  455. case '/dd':
  456. array_pop($indent);
  457. break;
  458. case '/h3':
  459. case '/h4':
  460. array_pop($indent);
  461. case '/h5':
  462. case '/h6':
  463. $chunk = ''; // Ensure blank new-line.
  464. break;
  465. // Fancy headers
  466. case 'h1':
  467. $indent[] = '======== ';
  468. $casing = 'drupal_strtoupper';
  469. break;
  470. case 'h2':
  471. $indent[] = '-------- ';
  472. $casing = 'drupal_strtoupper';
  473. break;
  474. case '/h1':
  475. case '/h2':
  476. $casing = NULL;
  477. // Pad the line with dashes.
  478. $output = _drupal_html_to_text_pad($output, ($tagname == '/h1') ? '=' : '-', ' ');
  479. array_pop($indent);
  480. $chunk = ''; // Ensure blank new-line.
  481. break;
  482. // Horizontal rulers
  483. case 'hr':
  484. // Insert immediately.
  485. $output .= drupal_wrap_mail('', implode('', $indent)) . "\n";
  486. $output = _drupal_html_to_text_pad($output, '-');
  487. break;
  488. // Paragraphs and definition lists
  489. case '/p':
  490. case '/dl':
  491. $chunk = ''; // Ensure blank new-line.
  492. break;
  493. }
  494. }
  495. // Process blocks of text.
  496. else {
  497. // Convert inline HTML text to plain text; not removing line-breaks or
  498. // white-space, since that breaks newlines when sanitizing plain-text.
  499. $value = trim(decode_entities($value));
  500. if (drupal_strlen($value)) {
  501. $chunk = $value;
  502. }
  503. }
  504. // See if there is something waiting to be output.
  505. if (isset($chunk)) {
  506. // Apply any necessary case conversion.
  507. if (isset($casing)) {
  508. $chunk = $casing($chunk);
  509. }
  510. // Format it and apply the current indentation.
  511. $output .= drupal_wrap_mail($chunk, implode('', $indent)) . MAIL_LINE_ENDINGS;
  512. // Remove non-quotation markers from indentation.
  513. $indent = array_map('_drupal_html_to_text_clean', $indent);
  514. }
  515. $tag = !$tag;
  516. }
  517. return $output . $footnotes;
  518. }
  519. /**
  520. * Wraps words on a single line.
  521. *
  522. * Callback for array_walk() winthin drupal_wrap_mail().
  523. */
  524. function _drupal_wrap_mail_line(&$line, $key, $values) {
  525. // Use soft-breaks only for purely quoted or unindented text.
  526. $line = wordwrap($line, 77 - $values['length'], $values['soft'] ? " \n" : "\n");
  527. // Break really long words at the maximum width allowed.
  528. $line = wordwrap($line, 996 - $values['length'], $values['soft'] ? " \n" : "\n");
  529. }
  530. /**
  531. * Keeps track of URLs and replaces them with placeholder tokens.
  532. *
  533. * Callback for preg_replace_callback() within drupal_html_to_text().
  534. */
  535. function _drupal_html_to_mail_urls($match = NULL, $reset = FALSE) {
  536. global $base_url, $base_path;
  537. static $urls = array(), $regexp;
  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. * Replaces non-quotation markers from a given piece of indentation with spaces.
  557. *
  558. * Callback for array_map() within drupal_html_to_text().
  559. */
  560. function _drupal_html_to_text_clean($indent) {
  561. return preg_replace('/[^>]/', ' ', $indent);
  562. }
  563. /**
  564. * Pads the last line with the given character.
  565. *
  566. * @see drupal_html_to_text()
  567. */
  568. function _drupal_html_to_text_pad($text, $pad, $prefix = '') {
  569. // Remove last line break.
  570. $text = substr($text, 0, -1);
  571. // Calculate needed padding space and add it.
  572. if (($p = strrpos($text, "\n")) === FALSE) {
  573. $p = -1;
  574. }
  575. $n = max(0, 79 - (strlen($text) - $p) - strlen($prefix));
  576. // Add prefix and padding, and restore linebreak.
  577. return $text . $prefix . str_repeat($pad, $n) . "\n";
  578. }