WordPress.org

Make WordPress Core

Changeset 10011


Ignore:
Timestamp:
12/02/08 18:40:16 (5 years ago)
Author:
westi
Message:

Improve wp-mail utf8 support. Fixes #6788 props filosofo.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-mail.php

    r9504 r10011  
    22/** 
    33 * Gets the email message from the user's mailbox to add as 
    4  * a WordPress post. Will only run if this is setup and enabled. 
     4 * a WordPress post. Mailbox connection information must be 
     5 * configured under Settings > Writing 
    56 * 
    67 * @package WordPress 
    78 */ 
    89 
    9 /** Make sure that the WordPress bootstrap has ran before continuing. */ 
     10/** Make sure that the WordPress bootstrap has run before continuing. */ 
    1011require(dirname(__FILE__) . '/wp-load.php'); 
    1112 
    12 /** Get the POP3 class for which to access the mailbox. */ 
    13 require_once(ABSPATH.WPINC.'/class-pop3.php'); 
    14  
    15 $time_difference = get_option('gmt_offset') * 3600; 
     13/** Get the POP3 class with which to access the mailbox. */ 
     14require_once( ABSPATH . WPINC . '/class-pop3.php' ); 
     15 
     16$time_difference = absint(get_option('gmt_offset')) * 3600; 
    1617 
    1718$phone_delim = '::'; 
     
    1920$pop3 = new POP3(); 
    2021 
    21 if (!$pop3->connect(get_option('mailserver_url'), get_option('mailserver_port'))) 
    22     wp_die(wp_specialchars($pop3->ERROR)); 
    23  
    24 if (!$pop3->user(get_option('mailserver_login'))) 
    25     wp_die(wp_specialchars($pop3->ERROR)); 
    26  
    27 $count = $pop3->pass(get_option('mailserver_pass')); 
    28 if (false === $count) 
    29     wp_die(wp_specialchars($pop3->ERROR)); 
    30 if (0 == $count) 
    31     echo '<p>' . __("There doesn't seem to be any new mail.") . "</p>\n"; // will fall-through to end of for loop 
    32  
    33 for ($i=1; $i <= $count; $i++) : 
     22if ( ! $pop3->connect(get_option('mailserver_url'), get_option('mailserver_port') ) || 
     23    ! $pop3->user(get_option('mailserver_login')) || 
     24    ( ! $count = $pop3->pass(get_option('mailserver_pass')) ) ) { 
     25        $pop3->quit(); 
     26        wp_die( ( 0 === $count ) ? __("There doesn't seem to be any new mail.") : wp_specialchars($pop3->ERROR) ); 
     27} 
     28 
     29for ( $i = 1; $i <= $count; $i++ ) { 
    3430 
    3531    $message = $pop3->get($i); 
    3632 
     33    $bodysignal = false; 
     34    $boundary = ''; 
     35    $charset = ''; 
    3736    $content = ''; 
    3837    $content_type = ''; 
    3938    $content_transfer_encoding = ''; 
    40     $boundary = ''; 
    41     $bodysignal = 0; 
    4239    $post_author = 1; 
    4340    $author_found = false; 
    4441    $dmonths = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'); 
    45     foreach ($message as $line) : 
    46         if (strlen($line) < 3) $bodysignal = 1; 
    47  
    48         if ($bodysignal) { 
     42    foreach ($message as $line) { 
     43        // body signal 
     44        if ( strlen($line) < 3 ) 
     45            $bodysignal = true; 
     46        if ( $bodysignal ) { 
    4947            $content .= $line; 
    5048        } else { 
    51             if (preg_match('/Content-Type: /i', $line)) { 
     49            if ( preg_match('/Content-Type: /i', $line) ) { 
    5250                $content_type = trim($line); 
    53                 $content_type = substr($content_type, 14, strlen($content_type)-14); 
     51                $content_type = substr($content_type, 14, strlen($content_type) - 14); 
    5452                $content_type = explode(';', $content_type); 
     53                if ( ! empty( $content_type[1] ) ) { 
     54                    $charset = explode('=', $content_type[1]); 
     55                    $charset = ( ! empty( $charset[1] ) ) ? trim($charset[1]) : ''; 
     56                } 
    5557                $content_type = $content_type[0]; 
    5658            } 
    57             if (preg_match('/Content-Transfer-Encoding: /i', $line)) { 
     59            if ( preg_match('/Content-Transfer-Encoding: /i', $line) ) { 
    5860                $content_transfer_encoding = trim($line); 
    59                 $content_transfer_encoding = substr($content_transfer_encoding, 27, strlen($content_transfer_encoding)-14); 
     61                $content_transfer_encoding = substr($content_transfer_encoding, 27, strlen($content_transfer_encoding) - 27); 
    6062                $content_transfer_encoding = explode(';', $content_transfer_encoding); 
    6163                $content_transfer_encoding = $content_transfer_encoding[0]; 
    6264            } 
    63             if (($content_type == 'multipart/alternative') && (false !== strpos($line, 'boundary="')) && ($boundary == '')) { 
     65            if ( ( $content_type == 'multipart/alternative' ) && ( false !== strpos($line, 'boundary="') ) && ( '' == $boundary ) ) { 
    6466                $boundary = trim($line); 
    6567                $boundary = explode('"', $boundary); 
     
    6870            if (preg_match('/Subject: /i', $line)) { 
    6971                $subject = trim($line); 
    70                 $subject = substr($subject, 9, strlen($subject)-9); 
    71                 $subject = wp_iso_descrambler($subject); 
     72                $subject = substr($subject, 9, strlen($subject) - 9); 
    7273                // Captures any text in the subject before $phone_delim as the subject 
     74                if ( function_exists('iconv_mime_decode') ) { 
     75                    $subject = iconv_mime_decode($subject, 2, get_option('blog_charset')); 
     76                } else { 
     77                    $subject = wp_iso_descrambler($subject); 
     78                } 
    7379                $subject = explode($phone_delim, $subject); 
    7480                $subject = $subject[0]; 
     
    8490                $author = sanitize_email($author); 
    8591                if ( is_email($author) ) { 
    86                     echo "Author = {$author} <p>"; 
     92                    echo '<p>' . sprintf(__('Author is %s'), $author) . '</p>'; 
    8793                    $userdata = get_user_by_email($author); 
    88                     if (!$userdata) { 
    89                         $post_author = 1; 
     94                    if ( empty($userdata) ) { 
    9095                        $author_found = false; 
    9196                    } else { 
     
    9499                    } 
    95100                } else { 
    96                     $post_author = 1; 
    97101                    $author_found = false; 
    98102                } 
     
    103107                $ddate = str_replace('Date: ', '', $ddate); 
    104108                if (strpos($ddate, ',')) { 
    105                     $ddate = trim(substr($ddate, strpos($ddate, ',')+1, strlen($ddate))); 
     109                    $ddate = trim(substr($ddate, strpos($ddate, ',') + 1, strlen($ddate))); 
    106110                } 
    107111                $date_arr = explode(' ', $ddate); 
     
    115119                $ddate_d = $date_arr[0]; 
    116120                $ddate_Y = $date_arr[2]; 
    117                 for ($j=0; $j<12; $j++) { 
    118                     if ($ddate_m == $dmonths[$j]) { 
     121                for ( $j = 0; $j < 12; $j++ ) { 
     122                    if ( $ddate_m == $dmonths[$j] ) { 
    119123                        $ddate_m = $j+1; 
    120124                    } 
     
    128132            } 
    129133        } 
    130     endforeach; 
     134    } 
    131135 
    132136    // Set $post_status based on $author_found and on author's publish_posts capability 
    133     if ($author_found) { 
     137    if ( $author_found ) { 
    134138        $user = new WP_User($post_author); 
    135         if ($user->has_cap('publish_posts')) 
    136             $post_status = 'publish'; 
    137         else 
    138             $post_status = 'pending'; 
     139        $post_status = ( $user->has_cap('publish_posts') ) ? 'publish' : 'pending'; 
    139140    } else { 
    140141        // Author not found in DB, set status to pending.  Author already set to admin. 
     
    144145    $subject = trim($subject); 
    145146 
    146     if ($content_type == 'multipart/alternative') { 
     147    if ( $content_type == 'multipart/alternative' ) { 
    147148        $content = explode('--'.$boundary, $content); 
    148149        $content = $content[2]; 
    149         $content = explode('Content-Transfer-Encoding: quoted-printable', $content); 
    150         $content = strip_tags($content[1], '<img><p><br><i><b><u><em><strong><strike><font><span><div>'); 
     150        // match case-insensitive content-transfer-encoding 
     151        if ( preg_match( '/Content-Transfer-Encoding: quoted-printable/i', $content, $delim) ) { 
     152            $content = explode($delim[0], $content); 
     153            $content = $content[1]; 
     154        } 
     155        $content = strip_tags($content, '<img><p><br><i><b><u><em><strong><strike><font><span><div>'); 
    151156    } 
    152157    $content = trim($content); 
    153158 
    154     if (stripos($content_transfer_encoding, "quoted-printable") !== false) { 
     159    if ( false !== stripos($content_transfer_encoding, "quoted-printable") ) { 
    155160        $content = quoted_printable_decode($content); 
    156161    } 
    157162 
     163    if ( function_exists('iconv') && ! empty( $charset ) ) { 
     164        $content = iconv($charset, get_option('blog_charset'), $content); 
     165    } 
     166     
    158167    // Captures any text in the body after $phone_delim as the body 
    159168    $content = explode($phone_delim, $content); 
    160     $content[1] ? $content = $content[1] : $content = $content[0]; 
     169    $content = empty( $content[1] ) ? $content[0] : $content[1]; 
    161170 
    162171    $content = trim($content); 
     
    168177    if ($post_title == '') $post_title = $subject; 
    169178 
    170     if (empty($post_categories)) $post_categories[] = get_option('default_email_category'); 
    171  
    172     $post_category = $post_categories; 
     179    $post_category = array(get_option('default_email_category')); 
    173180 
    174181    $post_data = compact('post_content','post_title','post_date','post_date_gmt','post_author','post_category', 'post_status'); 
     
    179186        echo "\n" . $post_ID->get_error_message(); 
    180187 
    181     if (!$post_ID) { 
    182         // we couldn't post, for whatever reason. better move forward to the next email 
     188    // We couldn't post, for whatever reason. Better move forward to the next email. 
     189    if ( empty( $post_ID ) ) 
    183190        continue; 
    184     } 
    185191 
    186192    do_action('publish_phone', $post_ID); 
     
    197203    } 
    198204 
    199 endfor; 
     205} 
    200206 
    201207$pop3->quit(); 
Note: See TracChangeset for help on using the changeset viewer.