Ignore:
Timestamp:
Nov 14, 2011, 11:17:15 PM (13 years ago)
Author:
dj3c1t
Message:

passage a Fluxbb 1.4.7

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/rsr.v5.1.dev/web/punbb/include/email.php

    r1 r3  
    11<?php
    2 /***********************************************************************
    3 
    4   Copyright (C) 2002-2005  Rickard Andersson (rickard@punbb.org)
    5 
    6   This file is part of PunBB.
    7 
    8   PunBB is free software; you can redistribute it and/or modify it
    9   under the terms of the GNU General Public License as published
    10   by the Free Software Foundation; either version 2 of the License,
    11   or (at your option) any later version.
    12 
    13   PunBB is distributed in the hope that it will be useful, but
    14   WITHOUT ANY WARRANTY; without even the implied warranty of
    15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16   GNU General Public License for more details.
    17 
    18   You should have received a copy of the GNU General Public License
    19   along with this program; if not, write to the Free Software
    20   Foundation, Inc., 59 Temple Place, Suite 330, Boston,
    21   MA  02111-1307  USA
    22 
    23 ************************************************************************/
    24 
     2
     3/**
     4 * Copyright (C) 2008-2011 FluxBB
     5 * based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
     6 * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
     7 */
    258
    269// Make sure no one attempts to run this script "directly"
     
    2811        exit;
    2912
    30 
    31 //
    32 // Validate an e-mail address
     13require PUN_ROOT.'include/utf8/utils/ascii.php';
     14
     15//
     16// Validate an email address
    3317//
    3418function is_valid_email($email)
    3519{
    36         if (strlen($email) > 50)
     20        if (strlen($email) > 80)
    3721                return false;
    3822
    39         return preg_match('/^(([^<>()[\]\\.,;:\s@"\']+(\.[^<>()[\]\\.,;:\s@"\']+)*)|("[^"\']+"))@((\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\])|(([a-zA-Z\d\-]+\.)+[a-zA-Z]{2,}))$/', $email);
     23        return preg_match('%^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|("[^"]+"))@((\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\])|(([a-zA-Z\d\-]+\.)+[a-zA-Z]{2,}))$%', $email);
    4024}
    4125
     
    4630function is_banned_email($email)
    4731{
    48         global $db, $pun_bans;
     32        global $pun_bans;
    4933
    5034        foreach ($pun_bans as $cur_ban)
     
    6145
    6246//
     47// Only encode with base64, if there is at least one unicode character in the string
     48//
     49function encode_mail_text($str)
     50{
     51        if (utf8_is_ascii($str))
     52                return $str;
     53
     54        return '=?UTF-8?B?'.base64_encode($str).'?=';
     55}
     56
     57
     58//
     59// Make a post email safe
     60//
     61function bbcode2email($text, $wrap_length = 72)
     62{
     63    static $base_url;
     64
     65    if (!isset($base_url))
     66        $base_url = get_base_url();
     67
     68    $text = pun_trim($text, "\t\n ");
     69
     70    $shortcut_urls = array(
     71        'topic' => '/viewtopic.php?id=$1',
     72        'post' => '/viewtopic.php?pid=$1#p$1',
     73        'forum' => '/viewforum.php?id=$1',
     74        'user' => '/profile.php?id=$1',
     75    );
     76
     77    // Split code blocks and text so BBcode in codeblocks won't be touched
     78    list($code, $text) = extract_blocks($text, '[code]', '[/code]');
     79
     80    // Strip all bbcodes, except the quote, url, img, email, code and list items bbcodes
     81    $text = preg_replace(array(
     82        '%\[/?(?!(?:quote|url|topic|post|user|forum|img|email|code|list|\*))[a-z]+(?:=[^\]]+)?\]%i',
     83        '%\n\[/?list(?:=[^\]]+)?\]%i' // A separate regex for the list tags to get rid of some whitespace
     84    ), '', $text);
     85
     86    // Match the deepest nested bbcode
     87    // An adapted example from Mastering Regular Expressions
     88    $match_quote_regex = '%
     89        \[(quote|\*|url|img|email|topic|post|user|forum)(?:=([^\]]+))?\]
     90        (
     91            (?>[^\[]*)
     92            (?>
     93                (?!\[/?\1(?:=[^\]]+)?\])
     94                \[
     95                [^\[]*
     96            )*
     97        )
     98        \[/\1\]
     99    %ix';
     100
     101    $url_index = 1;
     102    $url_stack = array();
     103    while (preg_match($match_quote_regex, $text, $matches))
     104    {
     105        // Quotes
     106        if ($matches[1] == 'quote')
     107        {
     108            // Put '>' or '> ' at the start of a line
     109            $replacement = preg_replace(
     110                array('%^(?=\>)%m', '%^(?!\>)%m'),
     111                array('>', '> '),
     112                $matches[2]." said:\n".$matches[3]);
     113        }
     114
     115        // List items
     116        elseif ($matches[1] == '*')
     117        {
     118            $replacement = ' * '.$matches[3];
     119        }
     120
     121        // URLs and emails
     122        elseif (in_array($matches[1], array('url', 'email')))
     123        {
     124            if (!empty($matches[2]))
     125            {
     126                $replacement = '['.$matches[3].']['.$url_index.']';
     127                $url_stack[$url_index] = $matches[2];
     128                $url_index++;
     129            }
     130            else
     131                $replacement = '['.$matches[3].']';
     132        }
     133
     134        // Images
     135        elseif ($matches[1] == 'img')
     136        {
     137            if (!empty($matches[2]))
     138                $replacement = '['.$matches[2].']['.$url_index.']';
     139            else
     140                $replacement = '['.basename($matches[3]).']['.$url_index.']';
     141
     142            $url_stack[$url_index] = $matches[3];
     143            $url_index++;
     144        }
     145
     146        // Topic, post, forum and user URLs
     147        elseif (in_array($matches[1], array('topic', 'post', 'forum', 'user')))
     148        {
     149            $url = isset($shortcut_urls[$matches[1]]) ? $base_url.$shortcut_urls[$matches[1]] : '';
     150
     151            if (!empty($matches[2]))
     152            {
     153                $replacement = '['.$matches[3].']['.$url_index.']';
     154                $url_stack[$url_index] = str_replace('$1', $matches[2], $url);
     155                $url_index++;
     156            }
     157            else
     158                $replacement = '['.str_replace('$1', $matches[3], $url).']';
     159        }
     160
     161        // Update the main text if there is a replacment
     162        if (!is_null($replacement))
     163        {
     164            $text = str_replace($matches[0], $replacement, $text);
     165            $replacement = null;
     166        }
     167    }
     168
     169    // Put code blocks and text together
     170    if (isset($code))
     171    {
     172        $parts = explode("\1", $text);
     173        $text = '';
     174        foreach ($parts as $i => $part)
     175        {
     176            $text .= $part;
     177            if (isset($code[$i]))
     178                $text .= trim($code[$i], "\n\r");
     179        }
     180    }
     181
     182    // Put URLs at the bottom
     183    if ($url_stack)
     184    {
     185        $text .= "\n\n";
     186        foreach ($url_stack as $i => $url)
     187            $text .= "\n".' ['.$i.']: '.$url;
     188    }
     189
     190    // Wrap lines if $wrap_length is higher than -1
     191    if ($wrap_length > -1)
     192    {
     193        // Split all lines and wrap them individually
     194        $parts = explode("\n", $text);
     195        foreach ($parts as $k => $part)
     196        {
     197            preg_match('%^(>+ )?(.*)%', $part, $matches);
     198            $parts[$k] = wordwrap($matches[1].$matches[2], $wrap_length -
     199                strlen($matches[1]), "\n".$matches[1]);
     200        }
     201
     202        return implode("\n", $parts);
     203    }
     204    else
     205        return $text;
     206}
     207
     208
     209//
    63210// Wrapper for PHP's mail()
    64211//
    65 function pun_mail($to, $subject, $message, $from = '')
     212function pun_mail($to, $subject, $message, $reply_to_email = '', $reply_to_name = '')
    66213{
    67214        global $pun_config, $lang_common;
    68215
    69216        // Default sender/return address
    70         if (!$from)
    71                 $from = '"'.str_replace('"', '', $pun_config['o_board_title'].' '.$lang_common['Mailer']).'" <'.$pun_config['o_webmaster_email'].'>';
     217        $from_name = sprintf($lang_common['Mailer'], $pun_config['o_board_title']);
     218        $from_email = $pun_config['o_webmaster_email'];
    72219
    73220        // Do a little spring cleaning
    74         $to = trim(preg_replace('#[\n\r]+#s', '', $to));
    75         $subject = trim(preg_replace('#[\n\r]+#s', '', $subject));
    76         $from = trim(preg_replace('#[\n\r:]+#s', '', $from));
    77 
    78         $headers = 'From: '.$from."\r\n".'Date: '.date('r')."\r\n".'MIME-Version: 1.0'."\r\n".'Content-transfer-encoding: 8bit'."\r\n".'Content-type: text/plain; charset='.$lang_common['lang_encoding']."\r\n".'X-Mailer: PunBB Mailer';
    79 
    80         // Make sure all linebreaks are CRLF in message
    81         $message = str_replace("\n", "\r\n", pun_linebreaks($message));
     221        $to = pun_trim(preg_replace('%[\n\r]+%s', '', $to));
     222        $subject = pun_trim(preg_replace('%[\n\r]+%s', '', $subject));
     223        $from_email = pun_trim(preg_replace('%[\n\r:]+%s', '', $from_email));
     224        $from_name = pun_trim(preg_replace('%[\n\r:]+%s', '', str_replace('"', '', $from_name)));
     225        $reply_to_email = pun_trim(preg_replace('%[\n\r:]+%s', '', $reply_to_email));
     226        $reply_to_name = pun_trim(preg_replace('%[\n\r:]+%s', '', str_replace('"', '', $reply_to_name)));
     227
     228        // Set up some headers to take advantage of UTF-8
     229        $from = '"'.encode_mail_text($from_name).'" <'.$from_email.'>';
     230        $subject = encode_mail_text($subject);
     231
     232        $headers = 'From: '.$from."\r\n".'Date: '.gmdate('r')."\r\n".'MIME-Version: 1.0'."\r\n".'Content-transfer-encoding: 8bit'."\r\n".'Content-type: text/plain; charset=utf-8'."\r\n".'X-Mailer: FluxBB Mailer';
     233
     234        // If we specified a reply-to email, we deal with it here
     235        if (!empty($reply_to_email))
     236        {
     237                $reply_to = '"'.encode_mail_text($reply_to_name).'" <'.$reply_to_email.'>';
     238
     239                $headers .= "\r\n".'Reply-To: '.$reply_to;
     240        }
     241
     242        // Make sure all linebreaks are LF in message (and strip out any NULL bytes)
     243        $message = str_replace("\0", '', pun_linebreaks($message));
    82244
    83245        if ($pun_config['o_smtp_host'] != '')
     246        {
     247                // Headers should be \r\n
     248                // Message should be ??
     249                $message = str_replace("\n", "\r\n", $message);
    84250                smtp_mail($to, $subject, $message, $headers);
     251        }
    85252        else
    86253        {
    87                 // Change the linebreaks used in the headers according to OS
    88                 if (strtoupper(substr(PHP_OS, 0, 3)) == 'MAC')
    89                         $headers = str_replace("\r\n", "\r", $headers);
    90                 else if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN')
    91                         $headers = str_replace("\r\n", "\n", $headers);
    92                
     254                // Headers should be \r\n
     255                // Message should be \n
    93256                mail($to, $subject, $message, $headers);
    94257        }
     
    97260
    98261//
    99 // This function was originally a part of the phpBB Group forum software phpBB2 (http://www.phpbb.com).
    100 // They deserve all the credit for writing it. I made small modifications for it to suit PunBB and it's coding standards.
     262// This function was originally a part of the phpBB Group forum software phpBB2 (http://www.phpbb.com)
     263// They deserve all the credit for writing it. I made small modifications for it to suit PunBB and it's coding standards
    101264//
    102265function server_parse($socket, $expected_response)
     
    106269        {
    107270                if (!($server_response = fgets($socket, 256)))
    108                         error('N\'a pas pu obtenir les codes de réponse du serveur mail. Veuillez contacter l\'administrateur de forum', __FILE__, __LINE__);
     271                        error('Couldn\'t get mail server response codes. Please contact the forum administrator.', __FILE__, __LINE__);
    109272        }
    110273
    111274        if (!(substr($server_response, 0, 3) == $expected_response))
    112                 error('Impossible d\'envoyer l\'e-mail. Veuillez contacter l\'administrateur des forums avec le message d\'erreur suivant rapporté par le serveur SMTP : "'.$server_response.'"', __FILE__, __LINE__);
    113 }
    114 
    115 
    116 //
    117 // This function was originally a part of the phpBB Group forum software phpBB2 (http://www.phpbb.com).
     275                error('Unable to send email. Please contact the forum administrator with the following error message reported by the SMTP server: "'.$server_response.'"', __FILE__, __LINE__);
     276}
     277
     278
     279//
     280// This function was originally a part of the phpBB Group forum software phpBB2 (http://www.phpbb.com)
    118281// They deserve all the credit for writing it. I made small modifications for it to suit PunBB and it's coding standards.
    119282//
     
    123286
    124287        $recipients = explode(',', $to);
     288
     289        // Sanitize the message
     290        $message = str_replace("\r\n.", "\r\n..", $message);
     291        $message = (substr($message, 0, 1) == '.' ? '.'.$message : $message);
    125292
    126293        // Are we using port 25 or a custom port?
     
    133300        }
    134301
     302        if ($pun_config['o_smtp_ssl'] == '1')
     303                $smtp_host = 'ssl://'.$smtp_host;
     304
    135305        if (!($socket = fsockopen($smtp_host, $smtp_port, $errno, $errstr, 15)))
    136                 error('Impossible de joindre l\'hÃŽte SMTP "'.$pun_config['o_smtp_host'].'" ('.$errno.') ('.$errstr.')', __FILE__, __LINE__);
     306                error('Could not connect to smtp host "'.$pun_config['o_smtp_host'].'" ('.$errno.') ('.$errstr.')', __FILE__, __LINE__);
    137307
    138308        server_parse($socket, '220');
     
    161331        server_parse($socket, '250');
    162332
    163         $to_header = 'To: ';
    164 
    165         @reset($recipients);
    166         while (list(, $email) = @each($recipients))
     333        foreach ($recipients as $email)
    167334        {
    168335                fwrite($socket, 'RCPT TO: <'.$email.'>'."\r\n");
    169336                server_parse($socket, '250');
    170 
    171                 $to_header .= '<'.$email.'>, ';
    172337        }
    173338
     
    175340        server_parse($socket, '354');
    176341
    177         fwrite($socket, 'Subject: '.$subject."\r\n".$to_header."\r\n".$headers."\r\n\r\n".$message."\r\n");
     342        fwrite($socket, 'Subject: '.$subject."\r\n".'To: <'.implode('>, <', $recipients).'>'."\r\n".$headers."\r\n\r\n".$message."\r\n");
    178343
    179344        fwrite($socket, '.'."\r\n");
Note: See TracChangeset for help on using the changeset viewer.