Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (7 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-mail.php

    r42201 r42343  
    99
    1010/** Make sure that the WordPress bootstrap has run before continuing. */
    11 require(dirname(__FILE__) . '/wp-load.php');
     11require( dirname( __FILE__ ) . '/wp-load.php' );
    1212
    1313/** This filter is documented in wp-admin/options.php */
    14 if ( ! apply_filters( 'enable_post_by_email_configuration', true ) )
     14if ( ! apply_filters( 'enable_post_by_email_configuration', true ) ) {
    1515    wp_die( __( 'This action has been disabled by the administrator.' ), 403 );
     16}
    1617
    1718$mailserver_url = get_option( 'mailserver_url' );
     
    3233
    3334/** Only check at this interval for new messages. */
    34 if ( !defined('WP_MAIL_INTERVAL') )
    35     define('WP_MAIL_INTERVAL', 300); // 5 minutes
    36 
    37 $last_checked = get_transient('mailserver_last_checked');
    38 
    39 if ( $last_checked )
    40     wp_die(__('Slow down cowboy, no need to check for new mails so often!'));
    41 
    42 set_transient('mailserver_last_checked', true, WP_MAIL_INTERVAL);
    43 
    44 $time_difference = get_option('gmt_offset') * HOUR_IN_SECONDS;
     35if ( ! defined( 'WP_MAIL_INTERVAL' ) ) {
     36    define( 'WP_MAIL_INTERVAL', 300 ); // 5 minutes
     37}
     38
     39$last_checked = get_transient( 'mailserver_last_checked' );
     40
     41if ( $last_checked ) {
     42    wp_die( __( 'Slow down cowboy, no need to check for new mails so often!' ) );
     43}
     44
     45set_transient( 'mailserver_last_checked', true, WP_MAIL_INTERVAL );
     46
     47$time_difference = get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
    4548
    4649$phone_delim = '::';
     
    4851$pop3 = new POP3();
    4952
    50 if ( !$pop3->connect( get_option('mailserver_url'), get_option('mailserver_port') ) || !$pop3->user( get_option('mailserver_login') ) )
     53if ( ! $pop3->connect( get_option( 'mailserver_url' ), get_option( 'mailserver_port' ) ) || ! $pop3->user( get_option( 'mailserver_login' ) ) ) {
    5154    wp_die( esc_html( $pop3->ERROR ) );
    52 
    53 $count = $pop3->pass( get_option('mailserver_pass') );
    54 
    55 if( false === $count )
     55}
     56
     57$count = $pop3->pass( get_option( 'mailserver_pass' ) );
     58
     59if ( false === $count ) {
    5660    wp_die( esc_html( $pop3->ERROR ) );
    57 
    58 if( 0 === $count ) {
     61}
     62
     63if ( 0 === $count ) {
    5964    $pop3->quit();
    60     wp_die( __('There doesn’t seem to be any new mail.') );
     65    wp_die( __( 'There doesn’t seem to be any new mail.' ) );
    6166}
    6267
    6368for ( $i = 1; $i <= $count; $i++ ) {
    6469
    65     $message = $pop3->get($i);
    66 
    67     $bodysignal = false;
    68     $boundary = '';
    69     $charset = '';
    70     $content = '';
    71     $content_type = '';
     70    $message = $pop3->get( $i );
     71
     72    $bodysignal                = false;
     73    $boundary                  = '';
     74    $charset                   = '';
     75    $content                   = '';
     76    $content_type              = '';
    7277    $content_transfer_encoding = '';
    73     $post_author = 1;
    74     $author_found = false;
    75     foreach ($message as $line) {
     78    $post_author               = 1;
     79    $author_found              = false;
     80    foreach ( $message as $line ) {
    7681        // Body signal.
    77         if ( strlen($line) < 3 )
     82        if ( strlen( $line ) < 3 ) {
    7883            $bodysignal = true;
     84        }
    7985        if ( $bodysignal ) {
    8086            $content .= $line;
    8187        } else {
    82             if ( preg_match('/Content-Type: /i', $line) ) {
    83                 $content_type = trim($line);
    84                 $content_type = substr($content_type, 14, strlen($content_type) - 14);
    85                 $content_type = explode(';', $content_type);
     88            if ( preg_match( '/Content-Type: /i', $line ) ) {
     89                $content_type = trim( $line );
     90                $content_type = substr( $content_type, 14, strlen( $content_type ) - 14 );
     91                $content_type = explode( ';', $content_type );
    8692                if ( ! empty( $content_type[1] ) ) {
    87                     $charset = explode('=', $content_type[1]);
    88                     $charset = ( ! empty( $charset[1] ) ) ? trim($charset[1]) : '';
     93                    $charset = explode( '=', $content_type[1] );
     94                    $charset = ( ! empty( $charset[1] ) ) ? trim( $charset[1] ) : '';
    8995                }
    9096                $content_type = $content_type[0];
    9197            }
    92             if ( preg_match('/Content-Transfer-Encoding: /i', $line) ) {
    93                 $content_transfer_encoding = trim($line);
    94                 $content_transfer_encoding = substr($content_transfer_encoding, 27, strlen($content_transfer_encoding) - 27);
    95                 $content_transfer_encoding = explode(';', $content_transfer_encoding);
     98            if ( preg_match( '/Content-Transfer-Encoding: /i', $line ) ) {
     99                $content_transfer_encoding = trim( $line );
     100                $content_transfer_encoding = substr( $content_transfer_encoding, 27, strlen( $content_transfer_encoding ) - 27 );
     101                $content_transfer_encoding = explode( ';', $content_transfer_encoding );
    96102                $content_transfer_encoding = $content_transfer_encoding[0];
    97103            }
    98             if ( ( $content_type == 'multipart/alternative' ) && ( false !== strpos($line, 'boundary="') ) && ( '' == $boundary ) ) {
    99                 $boundary = trim($line);
    100                 $boundary = explode('"', $boundary);
     104            if ( ( $content_type == 'multipart/alternative' ) && ( false !== strpos( $line, 'boundary="' ) ) && ( '' == $boundary ) ) {
     105                $boundary = trim( $line );
     106                $boundary = explode( '"', $boundary );
    101107                $boundary = $boundary[1];
    102108            }
    103             if (preg_match('/Subject: /i', $line)) {
    104                 $subject = trim($line);
    105                 $subject = substr($subject, 9, strlen($subject) - 9);
     109            if ( preg_match( '/Subject: /i', $line ) ) {
     110                $subject = trim( $line );
     111                $subject = substr( $subject, 9, strlen( $subject ) - 9 );
    106112                // Captures any text in the subject before $phone_delim as the subject
    107                 if ( function_exists('iconv_mime_decode') ) {
    108                     $subject = iconv_mime_decode($subject, 2, get_option('blog_charset'));
     113                if ( function_exists( 'iconv_mime_decode' ) ) {
     114                    $subject = iconv_mime_decode( $subject, 2, get_option( 'blog_charset' ) );
    109115                } else {
    110                     $subject = wp_iso_descrambler($subject);
    111                 }
    112                 $subject = explode($phone_delim, $subject);
     116                    $subject = wp_iso_descrambler( $subject );
     117                }
     118                $subject = explode( $phone_delim, $subject );
    113119                $subject = $subject[0];
    114120            }
     
    119125             */
    120126            if ( ! $author_found && preg_match( '/^(From|Reply-To): /', $line ) ) {
    121                 if ( preg_match('|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $line, $matches) )
     127                if ( preg_match( '|[a-z0-9_.-]+@[a-z0-9_.-]+(?!.*<)|i', $line, $matches ) ) {
    122128                    $author = $matches[0];
    123                 else
    124                     $author = trim($line);
    125                 $author = sanitize_email($author);
    126                 if ( is_email($author) ) {
     129                } else {
     130                    $author = trim( $line );
     131                }
     132                $author = sanitize_email( $author );
     133                if ( is_email( $author ) ) {
    127134                    /* translators: Post author email address */
    128                     echo '<p>' . sprintf(__('Author is %s'), $author) . '</p>';
    129                     $userdata = get_user_by('email', $author);
     135                    echo '<p>' . sprintf( __( 'Author is %s' ), $author ) . '</p>';
     136                    $userdata = get_user_by( 'email', $author );
    130137                    if ( ! empty( $userdata ) ) {
    131                         $post_author = $userdata->ID;
     138                        $post_author  = $userdata->ID;
    132139                        $author_found = true;
    133140                    }
     
    136143
    137144            if ( preg_match( '/Date: /i', $line ) ) { // of the form '20 Mar 2002 20:32:37 +0100'
    138                 $ddate = str_replace( 'Date: ', '', trim( $line ) );
    139                 $ddate = preg_replace( '!\s*\(.+\)\s*$!', '', $ddate ); // remove parenthesised timezone string if it exists, as this confuses strtotime
    140                 $ddate_U = strtotime( $ddate );
    141                 $post_date = gmdate( 'Y-m-d H:i:s', $ddate_U + $time_difference );
     145                $ddate         = str_replace( 'Date: ', '', trim( $line ) );
     146                $ddate         = preg_replace( '!\s*\(.+\)\s*$!', '', $ddate ); // remove parenthesised timezone string if it exists, as this confuses strtotime
     147                $ddate_U       = strtotime( $ddate );
     148                $post_date     = gmdate( 'Y-m-d H:i:s', $ddate_U + $time_difference );
    142149                $post_date_gmt = gmdate( 'Y-m-d H:i:s', $ddate_U );
    143150            }
     
    147154    // Set $post_status based on $author_found and on author's publish_posts capability
    148155    if ( $author_found ) {
    149         $user = new WP_User($post_author);
    150         $post_status = ( $user->has_cap('publish_posts') ) ? 'publish' : 'pending';
     156        $user        = new WP_User( $post_author );
     157        $post_status = ( $user->has_cap( 'publish_posts' ) ) ? 'publish' : 'pending';
    151158    } else {
    152159        // Author not found in DB, set status to pending. Author already set to admin.
     
    154161    }
    155162
    156     $subject = trim($subject);
     163    $subject = trim( $subject );
    157164
    158165    if ( $content_type == 'multipart/alternative' ) {
    159         $content = explode('--'.$boundary, $content);
     166        $content = explode( '--' . $boundary, $content );
    160167        $content = $content[2];
    161168
    162169        // Match case-insensitive content-transfer-encoding.
    163         if ( preg_match( '/Content-Transfer-Encoding: quoted-printable/i', $content, $delim) ) {
    164             $content = explode($delim[0], $content);
     170        if ( preg_match( '/Content-Transfer-Encoding: quoted-printable/i', $content, $delim ) ) {
     171            $content = explode( $delim[0], $content );
    165172            $content = $content[1];
    166173        }
    167         $content = strip_tags($content, '<img><p><br><i><b><u><em><strong><strike><font><span><div>');
    168     }
    169     $content = trim($content);
     174        $content = strip_tags( $content, '<img><p><br><i><b><u><em><strong><strike><font><span><div>' );
     175    }
     176    $content = trim( $content );
    170177
    171178    /**
     
    181188    $content = apply_filters( 'wp_mail_original_content', $content );
    182189
    183     if ( false !== stripos($content_transfer_encoding, "quoted-printable") ) {
    184         $content = quoted_printable_decode($content);
    185     }
    186 
    187     if ( function_exists('iconv') && ! empty( $charset ) ) {
    188         $content = iconv($charset, get_option('blog_charset'), $content);
     190    if ( false !== stripos( $content_transfer_encoding, 'quoted-printable' ) ) {
     191        $content = quoted_printable_decode( $content );
     192    }
     193
     194    if ( function_exists( 'iconv' ) && ! empty( $charset ) ) {
     195        $content = iconv( $charset, get_option( 'blog_charset' ), $content );
    189196    }
    190197
    191198    // Captures any text in the body after $phone_delim as the body
    192     $content = explode($phone_delim, $content);
     199    $content = explode( $phone_delim, $content );
    193200    $content = empty( $content[1] ) ? $content[0] : $content[1];
    194201
    195     $content = trim($content);
     202    $content = trim( $content );
    196203
    197204    /**
     
    204211    $post_content = apply_filters( 'phone_content', $content );
    205212
    206     $post_title = xmlrpc_getposttitle($content);
    207 
    208     if ($post_title == '') $post_title = $subject;
    209 
    210     $post_category = array(get_option('default_email_category'));
    211 
    212     $post_data = compact('post_content','post_title','post_date','post_date_gmt','post_author','post_category', 'post_status');
    213     $post_data = wp_slash($post_data);
    214 
    215     $post_ID = wp_insert_post($post_data);
    216     if ( is_wp_error( $post_ID ) )
     213    $post_title = xmlrpc_getposttitle( $content );
     214
     215    if ( $post_title == '' ) {
     216        $post_title = $subject;
     217    }
     218
     219    $post_category = array( get_option( 'default_email_category' ) );
     220
     221    $post_data = compact( 'post_content', 'post_title', 'post_date', 'post_date_gmt', 'post_author', 'post_category', 'post_status' );
     222    $post_data = wp_slash( $post_data );
     223
     224    $post_ID = wp_insert_post( $post_data );
     225    if ( is_wp_error( $post_ID ) ) {
    217226        echo "\n" . $post_ID->get_error_message();
     227    }
    218228
    219229    // We couldn't post, for whatever reason. Better move forward to the next email.
    220     if ( empty( $post_ID ) )
     230    if ( empty( $post_ID ) ) {
    221231        continue;
     232    }
    222233
    223234    /**
     
    233244    echo "\n<p><strong>" . __( 'Posted title:' ) . '</strong> ' . esc_html( $post_title ) . '</p>';
    234245
    235     if(!$pop3->delete($i)) {
     246    if ( ! $pop3->delete( $i ) ) {
    236247        echo '<p>' . sprintf(
    237248            /* translators: %s: POP3 error */
     
    248259        ) . '</p>';
    249260    }
    250 
    251261}
    252262
Note: See TracChangeset for help on using the changeset viewer.