Changeset 702 for trunk/forum/includes/functions_messenger.php
- Timestamp:
- Mar 31, 2010, 6:32:40 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/forum/includes/functions_messenger.php
r400 r702 3 3 * 4 4 * @package phpBB3 5 * @version $Id : functions_messenger.php 9078 2008-11-22 19:55:00Z acydburn$5 * @version $Id$ 6 6 * @copyright (c) 2005 phpBB Group 7 7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License … … 28 28 var $mail_priority = MAIL_NORMAL_PRIORITY; 29 29 var $use_queue = true; 30 31 var $tpl_obj = NULL; 30 32 var $tpl_msg = array(); 33 var $eol = "\n"; 31 34 32 35 /** … … 39 42 $this->use_queue = (!$config['email_package_size']) ? false : $use_queue; 40 43 $this->subject = ''; 44 45 // Determine EOL character (\n for UNIX, \r\n for Windows and \r for Mac) 46 $this->eol = (!defined('PHP_EOL')) ? (($eol = strtolower(substr(PHP_OS, 0, 3))) == 'win') ? "\r\n" : (($eol == 'mac') ? "\r" : "\n") : PHP_EOL; 47 $this->eol = (!$this->eol) ? "\n" : $this->eol; 41 48 } 42 49 … … 58 65 global $config; 59 66 67 if (!trim($address)) 68 { 69 return; 70 } 71 60 72 $pos = isset($this->addresses['to']) ? sizeof($this->addresses['to']) : 0; 61 73 … … 78 90 function cc($address, $realname = '') 79 91 { 92 if (!trim($address)) 93 { 94 return; 95 } 96 80 97 $pos = isset($this->addresses['cc']) ? sizeof($this->addresses['cc']) : 0; 81 98 $this->addresses['cc'][$pos]['email'] = trim($address); … … 88 105 function bcc($address, $realname = '') 89 106 { 107 if (!trim($address)) 108 { 109 return; 110 } 111 90 112 $pos = isset($this->addresses['bcc']) ? sizeof($this->addresses['bcc']) : 0; 91 113 $this->addresses['bcc'][$pos]['email'] = trim($address); … … 99 121 { 100 122 // IM-Addresses could be empty 101 if (! $address)123 if (!trim($address)) 102 124 { 103 125 return; … … 152 174 * Set email template to use 153 175 */ 154 function template($template_file, $template_lang = '' )155 { 156 global $config, $phpbb_root_path ;176 function template($template_file, $template_lang = '', $template_path = '') 177 { 178 global $config, $phpbb_root_path, $user; 157 179 158 180 if (!trim($template_file)) 159 181 { 160 trigger_error('No template file set', E_USER_ERROR);182 trigger_error('No template file for emailing set.', E_USER_ERROR); 161 183 } 162 184 163 185 if (!trim($template_lang)) 164 186 { 187 // fall back to board default language if the user's language is 188 // missing $template_file. If this does not exist either, 189 // $tpl->set_custom_template will do a trigger_error 165 190 $template_lang = basename($config['default_lang']); 166 191 } 167 192 168 if (empty($this->tpl_msg[$template_lang . $template_file])) 169 { 170 $tpl_file = "{$phpbb_root_path}language/$template_lang/email/$template_file.txt"; 171 172 if (!file_exists($tpl_file)) 173 { 174 trigger_error("Could not find email template file [ $tpl_file ]", E_USER_ERROR); 175 } 176 177 if (($data = @file_get_contents($tpl_file)) === false) 178 { 179 trigger_error("Failed opening template file [ $tpl_file ]", E_USER_ERROR); 180 } 181 182 $this->tpl_msg[$template_lang . $template_file] = $data; 183 } 184 185 $this->msg = $this->tpl_msg[$template_lang . $template_file]; 193 // tpl_msg now holds a template object we can use to parse the template file 194 if (!isset($this->tpl_msg[$template_lang . $template_file])) 195 { 196 $this->tpl_msg[$template_lang . $template_file] = new template(); 197 $tpl = &$this->tpl_msg[$template_lang . $template_file]; 198 199 $fallback_template_path = false; 200 201 if (!$template_path) 202 { 203 $template_path = (!empty($user->lang_path)) ? $user->lang_path : $phpbb_root_path . 'language/'; 204 $template_path .= $template_lang . '/email'; 205 206 // we can only specify default language fallback when the path is not a custom one for which we 207 // do not know the default language alternative 208 if ($template_lang !== basename($config['default_lang'])) 209 { 210 $fallback_template_path = (!empty($user->lang_path)) ? $user->lang_path : $phpbb_root_path . 'language/'; 211 $fallback_template_path .= basename($config['default_lang']) . '/email'; 212 } 213 } 214 215 $tpl->set_custom_template($template_path, $template_lang . '_email', $fallback_template_path); 216 217 $tpl->set_filenames(array( 218 'body' => $template_file . '.txt', 219 )); 220 } 221 222 $this->tpl_obj = &$this->tpl_msg[$template_lang . $template_file]; 223 $this->vars = &$this->tpl_obj->_rootref; 224 $this->tpl_msg = ''; 186 225 187 226 return true; … … 193 232 function assign_vars($vars) 194 233 { 195 $this->vars = (empty($this->vars)) ? $vars : $this->vars + $vars; 234 if (!is_object($this->tpl_obj)) 235 { 236 return; 237 } 238 239 $this->tpl_obj->assign_vars($vars); 240 } 241 242 function assign_block_vars($blockname, $vars) 243 { 244 if (!is_object($this->tpl_obj)) 245 { 246 return; 247 } 248 249 $this->tpl_obj->assign_block_vars($blockname, $vars); 196 250 } 197 251 … … 204 258 205 259 // We add some standard variables we always use, no need to specify them always 206 $this->vars['U_BOARD'] = (!isset($this->vars['U_BOARD'])) ? generate_board_url() : $this->vars['U_BOARD']; 207 $this->vars['EMAIL_SIG'] = (!isset($this->vars['EMAIL_SIG'])) ? str_replace('<br />', "\n", "-- \n" . htmlspecialchars_decode($config['board_email_sig'])) : $this->vars['EMAIL_SIG']; 208 $this->vars['SITENAME'] = (!isset($this->vars['SITENAME'])) ? htmlspecialchars_decode($config['sitename']) : $this->vars['SITENAME']; 209 210 // Escape all quotes, else the eval will fail. 211 $this->msg = str_replace ("'", "\'", $this->msg); 212 $this->msg = preg_replace('#\{([a-z0-9\-_]*?)\}#is', "' . ((isset(\$this->vars['\\1'])) ? \$this->vars['\\1'] : '') . '", $this->msg); 213 214 eval("\$this->msg = '$this->msg';"); 260 if (!isset($this->vars['U_BOARD'])) 261 { 262 $this->assign_vars(array( 263 'U_BOARD' => generate_board_url(), 264 )); 265 } 266 267 if (!isset($this->vars['EMAIL_SIG'])) 268 { 269 $this->assign_vars(array( 270 'EMAIL_SIG' => str_replace('<br />', "\n", "-- \n" . htmlspecialchars_decode($config['board_email_sig'])), 271 )); 272 } 273 274 if (!isset($this->vars['SITENAME'])) 275 { 276 $this->assign_vars(array( 277 'SITENAME' => htmlspecialchars_decode($config['sitename']), 278 )); 279 } 280 281 // Parse message through template 282 $this->msg = trim($this->tpl_obj->assign_display('body')); 283 284 // Because we use \n for newlines in the body message we need to fix line encoding errors for those admins who uploaded email template files in the wrong encoding 285 $this->msg = str_replace("\r\n", "\n", $this->msg); 215 286 216 287 // We now try and pull a subject from the email body ... if it exists, … … 310 381 global $config; 311 382 383 // We could use keys here, but we won't do this for 3.0.x to retain backwards compatibility 312 384 $headers = array(); 313 385 … … 335 407 $headers[] = 'X-Priority: ' . $this->mail_priority; 336 408 $headers[] = 'X-MSMail-Priority: ' . (($this->mail_priority == MAIL_LOW_PRIORITY) ? 'Low' : (($this->mail_priority == MAIL_NORMAL_PRIORITY) ? 'Normal' : 'High')); 337 $headers[] = 'X-Mailer: PhpBB3';409 $headers[] = 'X-Mailer: phpBB3'; 338 410 $headers[] = 'X-MimeOLE: phpBB3'; 339 411 $headers[] = 'X-phpBB-Origin: phpbb://' . str_replace(array('http://', 'https://'), array('', ''), generate_board_url()); 340 412 341 // We use \n here instead of \r\n because our smtp mailer is adjusting it to \r\n automatically, whereby the php mail function only works342 // if using \n.343 344 413 if (sizeof($this->extra_headers)) 345 414 { 346 $headers [] = implode("\n", $this->extra_headers);347 } 348 349 return implode("\n", $headers);415 $headers = array_merge($headers, $this->extra_headers); 416 } 417 418 return $headers; 350 419 } 351 420 … … 360 429 { 361 430 return false; 431 } 432 433 // Addresses to send to? 434 if (empty($this->addresses) || (empty($this->addresses['to']) && empty($this->addresses['cc']) && empty($this->addresses['bcc']))) 435 { 436 // Send was successful. ;) 437 return true; 362 438 } 363 439 … … 382 458 $this->from = '<' . $config['board_contact'] . '>'; 383 459 } 460 461 $encode_eol = ($config['smtp_delivery']) ? "\r\n" : $this->eol; 384 462 385 463 // Build to, cc and bcc strings … … 394 472 foreach ($address_ary as $which_ary) 395 473 { 396 $$type .= (($$type != '') ? ', ' : '') . (($which_ary['name'] != '') ? '"' . mail_encode($which_ary['name']) . '"<' . $which_ary['email'] . '>' : $which_ary['email']);474 $$type .= (($$type != '') ? ', ' : '') . (($which_ary['name'] != '') ? mail_encode($which_ary['name'], $encode_eol) . ' <' . $which_ary['email'] . '>' : $which_ary['email']); 397 475 } 398 476 } … … 413 491 else 414 492 { 415 ob_start(); 416 $result = $config['email_function_name']($mail_to, mail_encode($this->subject), wordwrap(utf8_wordwrap($this->msg), 997, "\n", true), $headers); 417 $err_msg = ob_get_clean(); 493 $result = phpbb_mail($mail_to, $this->subject, $this->msg, $headers, $this->eol, $err_msg); 418 494 } 419 495 … … 452 528 if (empty($this->addresses['im'])) 453 529 { 454 return false; 530 // Send was successful. ;) 531 return true; 455 532 } 456 533 … … 520 597 var $package_size = 0; 521 598 var $cache_file = ''; 599 var $eol = "\n"; 522 600 523 601 /** … … 530 608 $this->data = array(); 531 609 $this->cache_file = "{$phpbb_root_path}cache/queue.$phpEx"; 610 611 // Determine EOL character (\n for UNIX, \r\n for Windows and \r for Mac) 612 $this->eol = (!defined('PHP_EOL')) ? (($eol = strtolower(substr(PHP_OS, 0, 3))) == 'win') ? "\r\n" : (($eol == 'mac') ? "\r" : "\n") : PHP_EOL; 613 $this->eol = (!$this->eol) ? "\n" : $this->eol; 532 614 } 533 615 … … 652 734 else 653 735 { 654 ob_start(); 655 $result = $config['email_function_name']($to, mail_encode($subject), wordwrap(utf8_wordwrap($msg), 997, "\n", true), $headers); 656 $err_msg = ob_get_clean(); 736 $result = phpbb_mail($to, $subject, $msg, $headers, $this->eol, $err_msg); 657 737 } 658 738 … … 705 785 { 706 786 @flock($fp, LOCK_EX); 707 fwrite($fp, "<?php\n \$this->queue_data = unserialize(" . var_export(serialize($this->queue_data), true) . ");\n\n?>");787 fwrite($fp, "<?php\nif (!defined('IN_PHPBB')) exit;\n\$this->queue_data = unserialize(" . var_export(serialize($this->queue_data), true) . ");\n\n?>"); 708 788 @flock($fp, LOCK_UN); 709 789 fclose($fp); 710 790 711 phpbb_chmod($this->cache_file, CHMOD_ WRITE);791 phpbb_chmod($this->cache_file, CHMOD_READ | CHMOD_WRITE); 712 792 } 713 793 } … … 746 826 { 747 827 @flock($fp, LOCK_EX); 748 fwrite($fp, "<?php\n \$this->queue_data = unserialize(" . var_export(serialize($this->data), true) . ");\n\n?>");828 fwrite($fp, "<?php\nif (!defined('IN_PHPBB')) exit;\n\$this->queue_data = unserialize(" . var_export(serialize($this->data), true) . ");\n\n?>"); 749 829 @flock($fp, LOCK_UN); 750 830 fclose($fp); 751 831 752 phpbb_chmod($this->cache_file, CHMOD_ WRITE);832 phpbb_chmod($this->cache_file, CHMOD_READ | CHMOD_WRITE); 753 833 } 754 834 } … … 758 838 * Replacement or substitute for PHP's mail command 759 839 */ 760 function smtpmail($addresses, $subject, $message, &$err_msg, $headers = '')840 function smtpmail($addresses, $subject, $message, &$err_msg, $headers = false) 761 841 { 762 842 global $config, $user; … … 765 845 $message = preg_replace("#(?<!\r)\n#si", "\r\n", $message); 766 846 767 if ($headers != '') 768 { 769 if (is_array($headers)) 770 { 771 $headers = (sizeof($headers) > 1) ? join("\n", $headers) : $headers[0]; 772 } 773 $headers = chop($headers); 774 775 // Make sure there are no bare linefeeds in the headers 776 $headers = preg_replace('#(?<!\r)\n#si', "\r\n", $headers); 847 if ($headers !== false) 848 { 849 if (!is_array($headers)) 850 { 851 // Make sure there are no bare linefeeds in the headers 852 $headers = preg_replace('#(?<!\r)\n#si', "\n", $headers); 853 $headers = explode("\n", $headers); 854 } 777 855 778 856 // Ok this is rather confusing all things considered, 779 857 // but we have to grab bcc and cc headers and treat them differently 780 858 // Something we really didn't take into consideration originally 781 $header_array = explode("\r\n", $headers); 782 $headers = ''; 783 784 foreach ($header_array as $header) 859 $headers_used = array(); 860 861 foreach ($headers as $header) 785 862 { 786 863 if (strpos(strtolower($header), 'cc:') === 0 || strpos(strtolower($header), 'bcc:') === 0) 787 864 { 788 $header = '';789 } 790 $headers .= ($header != '') ? $header . "\r\n" : '';791 } 792 793 $headers = chop( $headers);865 continue; 866 } 867 $headers_used[] = trim($header); 868 } 869 870 $headers = chop(implode("\r\n", $headers_used)); 794 871 } 795 872 … … 947 1024 948 1025 // Now any custom headers.... 949 $smtp->server_send("$headers\r\n"); 1026 if ($headers !== false) 1027 { 1028 $smtp->server_send("$headers\r\n"); 1029 } 950 1030 951 1031 // Ok now we are ready for the message... … … 1068 1148 1069 1149 $err_msg = ''; 1070 $local_host = (function_exists('php_uname')) ? php_uname('n') : $user->host; 1150 1151 // Here we try to determine the *real* hostname (reverse DNS entry preferrably) 1152 $local_host = $user->host; 1153 1154 if (function_exists('php_uname')) 1155 { 1156 $local_host = php_uname('n'); 1157 1158 // Able to resolve name to IP 1159 if (($addr = @gethostbyname($local_host)) !== $local_host) 1160 { 1161 // Able to resolve IP back to name 1162 if (($name = @gethostbyaddr($addr)) !== $addr) 1163 { 1164 $local_host = $name; 1165 } 1166 } 1167 } 1071 1168 1072 1169 // If we are authenticating through pop-before-smtp, we … … 1406 1503 * 1407 1504 * Please note that this version fully supports RFC 2045 section 6.8. 1505 * 1506 * @param string $eol End of line we are using (optional to be backwards compatible) 1408 1507 */ 1409 function mail_encode($str )1508 function mail_encode($str, $eol = "\r\n") 1410 1509 { 1411 1510 // define start delimimter, end delimiter and spacer 1412 1511 $start = "=?UTF-8?B?"; 1413 1512 $end = "?="; 1414 $spacer = $end . ' ' . $start; 1415 $split_length = 64; 1416 1513 $delimiter = "$eol "; 1514 1515 // Maximum length is 75. $split_length *must* be a multiple of 4, but <= 75 - strlen($start . $delimiter . $end)!!! 1516 $split_length = 60; 1417 1517 $encoded_str = base64_encode($str); 1418 1518 … … 1426 1526 if (strlen($str) === utf8_strlen($str)) 1427 1527 { 1428 return $start . implode($ spacer, str_split($encoded_str, $split_length)) . $end;1528 return $start . implode($end . $delimiter . $start, str_split($encoded_str, $split_length)) . $end; 1429 1529 } 1430 1530 … … 1442 1542 } 1443 1543 1444 $str .= $start . base64_encode($text) . $end . ' ';1445 } 1446 1447 return substr($str, 0, - 1);1544 $str .= $start . base64_encode($text) . $end . $delimiter; 1545 } 1546 1547 return substr($str, 0, -strlen($delimiter)); 1448 1548 } 1449 1549 1550 /** 1551 * Wrapper for sending out emails with the PHP's mail function 1552 */ 1553 function phpbb_mail($to, $subject, $msg, $headers, $eol, &$err_msg) 1554 { 1555 global $config; 1556 1557 // We use the EOL character for the OS here because the PHP mail function does not correctly transform line endings. On Windows SMTP is used (SMTP is \r\n), on UNIX a command is used... 1558 // Reference: http://bugs.php.net/bug.php?id=15841 1559 $headers = implode($eol, $headers); 1560 1561 ob_start(); 1562 // On some PHP Versions mail() *may* fail if there are newlines within the subject. 1563 // Newlines are used as a delimiter for lines in mail_encode() according to RFC 2045 section 6.8. 1564 // Because PHP can't decide what is wanted we revert back to the non-RFC-compliant way of separating by one space (Use '' as parameter to mail_encode() results in SPACE used) 1565 $result = $config['email_function_name']($to, mail_encode($subject, ''), wordwrap(utf8_wordwrap($msg), 997, "\n", true), $headers); 1566 $err_msg = ob_get_clean(); 1567 1568 return $result; 1569 } 1570 1450 1571 ?>
Note:
See TracChangeset
for help on using the changeset viewer.