123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623 |
- <?php
- define('MAIL_LINE_ENDINGS', isset($_SERVER['WINDIR']) || (isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Win32') !== FALSE) ? "\r\n" : "\n");
- function drupal_mail($module, $key, $to, $language, $params = array(), $from = NULL, $send = TRUE) {
- $default_from = variable_get('site_mail', ini_get('sendmail_from'));
-
- $message = array(
- 'id' => $module . '_' . $key,
- 'module' => $module,
- 'key' => $key,
- 'to' => $to,
- 'from' => isset($from) ? $from : $default_from,
- 'language' => $language,
- 'params' => $params,
- 'send' => TRUE,
- 'subject' => '',
- 'body' => array()
- );
-
- $headers = array(
- 'MIME-Version' => '1.0',
- 'Content-Type' => 'text/plain; charset=UTF-8; format=flowed; delsp=yes',
- 'Content-Transfer-Encoding' => '8Bit',
- 'X-Mailer' => 'Drupal'
- );
- if ($default_from) {
-
-
-
- $headers['From'] = $headers['Sender'] = $headers['Return-Path'] = $default_from;
- }
- if ($from) {
- $headers['From'] = $from;
- }
- $message['headers'] = $headers;
-
-
-
- if (function_exists($function = $module . '_mail')) {
- $function($key, $message, $params);
- }
-
- drupal_alter('mail', $message);
-
- $system = drupal_mail_system($module, $key);
-
- $message = $system->format($message);
-
- if ($send) {
-
-
-
- if (empty($message['send'])) {
- $message['result'] = NULL;
- }
-
- else {
- $message['result'] = $system->mail($message);
-
- if (!$message['result']) {
- watchdog('mail', 'Error sending e-mail (from %from to %to).', array('%from' => $message['from'], '%to' => $message['to']), WATCHDOG_ERROR);
- drupal_set_message(t('Unable to send e-mail. Contact the site administrator if the problem persists.'), 'error');
- }
- }
- }
- return $message;
- }
- function drupal_mail_system($module, $key) {
- $instances = &drupal_static(__FUNCTION__, array());
- $id = $module . '_' . $key;
- $configuration = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
-
-
- if (isset($configuration[$id])) {
- $class = $configuration[$id];
- }
- elseif (isset($configuration[$module])) {
- $class = $configuration[$module];
- }
- else {
- $class = $configuration['default-system'];
- }
- if (empty($instances[$class])) {
- $interfaces = class_implements($class);
- if (isset($interfaces['MailSystemInterface'])) {
- $instances[$class] = new $class();
- }
- else {
- throw new Exception(t('Class %class does not implement interface %interface', array('%class' => $class, '%interface' => 'MailSystemInterface')));
- }
- }
- return $instances[$class];
- }
- interface MailSystemInterface {
-
- public function format(array $message);
-
- public function mail(array $message);
- }
- function drupal_wrap_mail($text, $indent = '') {
-
- $text = str_replace("\r", '', $text);
-
- $clean_indent = _drupal_html_to_text_clean($indent);
- $soft = strpos($clean_indent, ' ') === FALSE;
-
- if (strpos($text, "\n") !== FALSE) {
-
-
- $text = preg_replace('/(?(?<!^--) +\n| +\n)/m', "\n", $text);
-
- $lines = explode("\n", $text);
- array_walk($lines, '_drupal_wrap_mail_line', array('soft' => $soft, 'length' => strlen($indent)));
- $text = implode("\n", $lines);
- }
- else {
-
- _drupal_wrap_mail_line($text, 0, array('soft' => $soft, 'length' => strlen($indent)));
- }
-
- $text = preg_replace('/^ +\n/m', "\n", $text);
-
- $text = preg_replace('/^(>| |From)/m', ' $1', $text);
-
- $text = $indent . substr(preg_replace('/^/m', $clean_indent, $text), strlen($indent));
- return $text;
- }
- function drupal_html_to_text($string, $allowed_tags = NULL) {
-
- static $supported_tags;
- if (empty($supported_tags)) {
- $supported_tags = array('a', 'em', 'i', 'strong', 'b', 'br', 'p', 'blockquote', 'ul', 'ol', 'li', 'dl', 'dt', 'dd', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr');
- }
-
- $allowed_tags = isset($allowed_tags) ? array_intersect($supported_tags, $allowed_tags) : $supported_tags;
-
- $string = _filter_htmlcorrector(filter_xss($string, $allowed_tags));
-
- $string = preg_replace('!</?(em|i)((?> +)[^>]*)?>!i', '/', $string);
- $string = preg_replace('!</?(strong|b)((?> +)[^>]*)?>!i', '*', $string);
-
-
-
- _drupal_html_to_mail_urls(NULL, TRUE);
- $pattern = '@(<a[^>]+?href="([^"]*)"[^>]*?>(.+?)</a>)@i';
- $string = preg_replace_callback($pattern, '_drupal_html_to_mail_urls', $string);
- $urls = _drupal_html_to_mail_urls();
- $footnotes = '';
- if (count($urls)) {
- $footnotes .= "\n";
- for ($i = 0, $max = count($urls); $i < $max; $i++) {
- $footnotes .= '[' . ($i + 1) . '] ' . $urls[$i] . "\n";
- }
- }
-
- $split = preg_split('/<([^>]+?)>/', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
-
-
- $tag = FALSE;
- $casing = NULL;
- $output = '';
- $indent = array();
- $lists = array();
- foreach ($split as $value) {
- $chunk = NULL;
-
- if ($tag) {
- list($tagname) = explode(' ', strtolower($value), 2);
- switch ($tagname) {
-
- case 'ul':
- array_unshift($lists, '*');
- break;
- case 'ol':
- array_unshift($lists, 1);
- break;
- case '/ul':
- case '/ol':
- array_shift($lists);
- $chunk = '';
- break;
-
- case 'blockquote':
-
- $indent[] = count($lists) ? ' "' : '>';
- break;
- case 'li':
- $indent[] = isset($lists[0]) && is_numeric($lists[0]) ? ' ' . $lists[0]++ . ') ' : ' * ';
- break;
- case 'dd':
- $indent[] = ' ';
- break;
- case 'h3':
- $indent[] = '.... ';
- break;
- case 'h4':
- $indent[] = '.. ';
- break;
- case '/blockquote':
- if (count($lists)) {
-
- $output = rtrim($output, "> \n") . "\"\n";
- $chunk = '';
- }
-
- case '/li':
- case '/dd':
- array_pop($indent);
- break;
- case '/h3':
- case '/h4':
- array_pop($indent);
- case '/h5':
- case '/h6':
- $chunk = '';
- break;
-
- case 'h1':
- $indent[] = '======== ';
- $casing = 'drupal_strtoupper';
- break;
- case 'h2':
- $indent[] = '-------- ';
- $casing = 'drupal_strtoupper';
- break;
- case '/h1':
- case '/h2':
- $casing = NULL;
-
- $output = _drupal_html_to_text_pad($output, ($tagname == '/h1') ? '=' : '-', ' ');
- array_pop($indent);
- $chunk = '';
- break;
-
- case 'hr':
-
- $output .= drupal_wrap_mail('', implode('', $indent)) . "\n";
- $output = _drupal_html_to_text_pad($output, '-');
- break;
-
- case '/p':
- case '/dl':
- $chunk = '';
- break;
- }
- }
-
- else {
-
-
- $value = trim(decode_entities($value));
- if (drupal_strlen($value)) {
- $chunk = $value;
- }
- }
-
- if (isset($chunk)) {
-
- if (isset($casing)) {
- $chunk = $casing($chunk);
- }
-
- $output .= drupal_wrap_mail($chunk, implode('', $indent)) . MAIL_LINE_ENDINGS;
-
- $indent = array_map('_drupal_html_to_text_clean', $indent);
- }
- $tag = !$tag;
- }
- return $output . $footnotes;
- }
- function _drupal_wrap_mail_line(&$line, $key, $values) {
-
- $line = wordwrap($line, 77 - $values['length'], $values['soft'] ? " \n" : "\n");
-
- $line = wordwrap($line, 996 - $values['length'], $values['soft'] ? " \n" : "\n");
- }
- function _drupal_html_to_mail_urls($match = NULL, $reset = FALSE) {
- global $base_url, $base_path;
- static $urls = array(), $regexp;
- if ($reset) {
-
- $urls = array();
- }
- else {
- if (empty($regexp)) {
- $regexp = '@^' . preg_quote($base_path, '@') . '@';
- }
- if ($match) {
- list(, , $url, $label) = $match;
-
- $urls[] = strpos($url, '://') ? $url : preg_replace($regexp, $base_url . '/', $url);
- return $label . ' [' . count($urls) . ']';
- }
- }
- return $urls;
- }
- function _drupal_html_to_text_clean($indent) {
- return preg_replace('/[^>]/', ' ', $indent);
- }
- function _drupal_html_to_text_pad($text, $pad, $prefix = '') {
-
- $text = substr($text, 0, -1);
-
- if (($p = strrpos($text, "\n")) === FALSE) {
- $p = -1;
- }
- $n = max(0, 79 - (strlen($text) - $p) - strlen($prefix));
-
- return $text . $prefix . str_repeat($pad, $n) . "\n";
- }
|