Ignore:
Timestamp:
Mar 31, 2010, 6:32:40 PM (14 years ago)
Author:
george
Message:
  • Upraveno: Aktualizace fóra.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/forum/includes/functions_messenger.php

    r400 r702  
    33*
    44* @package phpBB3
    5 * @version $Id: functions_messenger.php 9078 2008-11-22 19:55:00Z acydburn $
     5* @version $Id$
    66* @copyright (c) 2005 phpBB Group
    77* @license http://opensource.org/licenses/gpl-license.php GNU Public License
     
    2828        var $mail_priority = MAIL_NORMAL_PRIORITY;
    2929        var $use_queue = true;
     30
     31        var $tpl_obj = NULL;
    3032        var $tpl_msg = array();
     33        var $eol = "\n";
    3134
    3235        /**
     
    3942                $this->use_queue = (!$config['email_package_size']) ? false : $use_queue;
    4043                $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;
    4148        }
    4249
     
    5865                global $config;
    5966
     67                if (!trim($address))
     68                {
     69                        return;
     70                }
     71
    6072                $pos = isset($this->addresses['to']) ? sizeof($this->addresses['to']) : 0;
    6173
     
    7890        function cc($address, $realname = '')
    7991        {
     92                if (!trim($address))
     93                {
     94                        return;
     95                }
     96
    8097                $pos = isset($this->addresses['cc']) ? sizeof($this->addresses['cc']) : 0;
    8198                $this->addresses['cc'][$pos]['email'] = trim($address);
     
    88105        function bcc($address, $realname = '')
    89106        {
     107                if (!trim($address))
     108                {
     109                        return;
     110                }
     111
    90112                $pos = isset($this->addresses['bcc']) ? sizeof($this->addresses['bcc']) : 0;
    91113                $this->addresses['bcc'][$pos]['email'] = trim($address);
     
    99121        {
    100122                // IM-Addresses could be empty
    101                 if (!$address)
     123                if (!trim($address))
    102124                {
    103125                        return;
     
    152174        * Set email template to use
    153175        */
    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;
    157179
    158180                if (!trim($template_file))
    159181                {
    160                         trigger_error('No template file set', E_USER_ERROR);
     182                        trigger_error('No template file for emailing set.', E_USER_ERROR);
    161183                }
    162184
    163185                if (!trim($template_lang))
    164186                {
     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
    165190                        $template_lang = basename($config['default_lang']);
    166191                }
    167192
    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 = '';
    186225
    187226                return true;
     
    193232        function assign_vars($vars)
    194233        {
    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);
    196250        }
    197251
     
    204258
    205259                // 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);
    215286
    216287                // We now try and pull a subject from the email body ... if it exists,
     
    310381                global $config;
    311382
     383                // We could use keys here, but we won't do this for 3.0.x to retain backwards compatibility
    312384                $headers = array();
    313385
     
    335407                $headers[] = 'X-Priority: ' . $this->mail_priority;
    336408                $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';
    338410                $headers[] = 'X-MimeOLE: phpBB3';
    339411                $headers[] = 'X-phpBB-Origin: phpbb://' . str_replace(array('http://', 'https://'), array('', ''), generate_board_url());
    340412
    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 works
    342                 // if using \n.
    343 
    344413                if (sizeof($this->extra_headers))
    345414                {
    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;
    350419        }
    351420
     
    360429                {
    361430                        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;
    362438                }
    363439
     
    382458                        $this->from = '<' . $config['board_contact'] . '>';
    383459                }
     460
     461                $encode_eol = ($config['smtp_delivery']) ? "\r\n" : $this->eol;
    384462
    385463                // Build to, cc and bcc strings
     
    394472                        foreach ($address_ary as $which_ary)
    395473                        {
    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']);
    397475                        }
    398476                }
     
    413491                        else
    414492                        {
    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);
    418494                        }
    419495
     
    452528                if (empty($this->addresses['im']))
    453529                {
    454                         return false;
     530                        // Send was successful. ;)
     531                        return true;
    455532                }
    456533
     
    520597        var $package_size = 0;
    521598        var $cache_file = '';
     599        var $eol = "\n";
    522600
    523601        /**
     
    530608                $this->data = array();
    531609                $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;
    532614        }
    533615
     
    652734                                                else
    653735                                                {
    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);
    657737                                                }
    658738
     
    705785                        {
    706786                                @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?>");
    708788                                @flock($fp, LOCK_UN);
    709789                                fclose($fp);
    710790
    711                                 phpbb_chmod($this->cache_file, CHMOD_WRITE);
     791                                phpbb_chmod($this->cache_file, CHMOD_READ | CHMOD_WRITE);
    712792                        }
    713793                }
     
    746826                {
    747827                        @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?>");
    749829                        @flock($fp, LOCK_UN);
    750830                        fclose($fp);
    751831
    752                         phpbb_chmod($this->cache_file, CHMOD_WRITE);
     832                        phpbb_chmod($this->cache_file, CHMOD_READ | CHMOD_WRITE);
    753833                }
    754834        }
     
    758838* Replacement or substitute for PHP's mail command
    759839*/
    760 function smtpmail($addresses, $subject, $message, &$err_msg, $headers = '')
     840function smtpmail($addresses, $subject, $message, &$err_msg, $headers = false)
    761841{
    762842        global $config, $user;
     
    765845        $message = preg_replace("#(?<!\r)\n#si", "\r\n", $message);
    766846
    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                }
    777855
    778856                // Ok this is rather confusing all things considered,
    779857                // but we have to grab bcc and cc headers and treat them differently
    780858                // 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)
    785862                {
    786863                        if (strpos(strtolower($header), 'cc:') === 0 || strpos(strtolower($header), 'bcc:') === 0)
    787864                        {
    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));
    794871        }
    795872
     
    9471024
    9481025        // Now any custom headers....
    949         $smtp->server_send("$headers\r\n");
     1026        if ($headers !== false)
     1027        {
     1028                $smtp->server_send("$headers\r\n");
     1029        }
    9501030
    9511031        // Ok now we are ready for the message...
     
    10681148
    10691149                $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                }
    10711168
    10721169                // If we are authenticating through pop-before-smtp, we
     
    14061503*
    14071504* 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)
    14081507*/
    1409 function mail_encode($str)
     1508function mail_encode($str, $eol = "\r\n")
    14101509{
    14111510        // define start delimimter, end delimiter and spacer
    14121511        $start = "=?UTF-8?B?";
    14131512        $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;
    14171517        $encoded_str = base64_encode($str);
    14181518
     
    14261526        if (strlen($str) === utf8_strlen($str))
    14271527        {
    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;
    14291529        }
    14301530
     
    14421542                }
    14431543
    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));
    14481548}
    14491549
     1550/**
     1551* Wrapper for sending out emails with the PHP's mail function
     1552*/
     1553function 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
    14501571?>
Note: See TracChangeset for help on using the changeset viewer.