Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (8 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-includes/class-wp-http-streams.php

    r41901 r42343  
    2727     * @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error
    2828     */
    29     public function request($url, $args = array()) {
     29    public function request( $url, $args = array() ) {
    3030        $defaults = array(
    31             'method' => 'GET', 'timeout' => 5,
    32             'redirection' => 5, 'httpversion' => '1.0',
    33             'blocking' => true,
    34             'headers' => array(), 'body' => null, 'cookies' => array()
     31            'method'      => 'GET',
     32            'timeout'     => 5,
     33            'redirection' => 5,
     34            'httpversion' => '1.0',
     35            'blocking'    => true,
     36            'headers'     => array(),
     37            'body'        => null,
     38            'cookies'     => array(),
    3539        );
    3640
     
    4852        WP_Http::buildCookieHeader( $r );
    4953
    50         $arrURL = parse_url($url);
     54        $arrURL = parse_url( $url );
    5155
    5256        $connect_host = $arrURL['host'];
     
    5559        if ( ! isset( $arrURL['port'] ) ) {
    5660            if ( $arrURL['scheme'] == 'ssl' || $arrURL['scheme'] == 'https' ) {
    57                 $arrURL['port'] = 443;
     61                $arrURL['port']   = 443;
    5862                $secure_transport = true;
    5963            } else {
     
    6872
    6973        if ( isset( $r['headers']['Host'] ) || isset( $r['headers']['host'] ) ) {
    70             if ( isset( $r['headers']['Host'] ) )
     74            if ( isset( $r['headers']['Host'] ) ) {
    7175                $arrURL['host'] = $r['headers']['Host'];
    72             else
     76            } else {
    7377                $arrURL['host'] = $r['headers']['host'];
     78            }
    7479            unset( $r['headers']['Host'], $r['headers']['host'] );
    7580        }
     
    8085         * connect to the IPv4 address.
    8186         */
    82         if ( 'localhost' == strtolower( $connect_host ) )
     87        if ( 'localhost' == strtolower( $connect_host ) ) {
    8388            $connect_host = '127.0.0.1';
     89        }
    8490
    8591        $connect_host = $secure_transport ? 'ssl://' . $connect_host : 'tcp://' . $connect_host;
    8692
    87         $is_local = isset( $r['local'] ) && $r['local'];
     93        $is_local   = isset( $r['local'] ) && $r['local'];
    8894        $ssl_verify = isset( $r['sslverify'] ) && $r['sslverify'];
    8995        if ( $is_local ) {
     
    109115        $proxy = new WP_HTTP_Proxy();
    110116
    111         $context = stream_context_create( array(
    112             'ssl' => array(
    113                 'verify_peer' => $ssl_verify,
    114                 //'CN_match' => $arrURL['host'], // This is handled by self::verify_ssl_certificate()
    115                 'capture_peer_cert' => $ssl_verify,
    116                 'SNI_enabled' => true,
    117                 'cafile' => $r['sslcertificates'],
    118                 'allow_self_signed' => ! $ssl_verify,
     117        $context = stream_context_create(
     118            array(
     119                'ssl' => array(
     120                    'verify_peer'       => $ssl_verify,
     121                    //'CN_match' => $arrURL['host'], // This is handled by self::verify_ssl_certificate()
     122                    'capture_peer_cert' => $ssl_verify,
     123                    'SNI_enabled'       => true,
     124                    'cafile'            => $r['sslcertificates'],
     125                    'allow_self_signed' => ! $ssl_verify,
     126                ),
    119127            )
    120         ) );
    121 
    122         $timeout = (int) floor( $r['timeout'] );
    123         $utimeout = $timeout == $r['timeout'] ? 0 : 1000000 * $r['timeout'] % 1000000;
     128        );
     129
     130        $timeout         = (int) floor( $r['timeout'] );
     131        $utimeout        = $timeout == $r['timeout'] ? 0 : 1000000 * $r['timeout'] % 1000000;
    124132        $connect_timeout = max( $timeout, 1 );
    125133
     
    130138        $connection_error_str = null;
    131139
    132         if ( !WP_DEBUG ) {
     140        if ( ! WP_DEBUG ) {
    133141            // In the event that the SSL connection fails, silence the many PHP Warnings.
    134             if ( $secure_transport )
    135                 $error_reporting = error_reporting(0);
    136 
    137             if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) )
     142            if ( $secure_transport ) {
     143                $error_reporting = error_reporting( 0 );
     144            }
     145
     146            if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) {
    138147                $handle = @stream_socket_client( 'tcp://' . $proxy->host() . ':' . $proxy->port(), $connection_error, $connection_error_str, $connect_timeout, STREAM_CLIENT_CONNECT, $context );
    139             else
     148            } else {
    140149                $handle = @stream_socket_client( $connect_host . ':' . $arrURL['port'], $connection_error, $connection_error_str, $connect_timeout, STREAM_CLIENT_CONNECT, $context );
    141 
    142             if ( $secure_transport )
     150            }
     151
     152            if ( $secure_transport ) {
    143153                error_reporting( $error_reporting );
    144 
     154            }
    145155        } else {
    146             if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) )
     156            if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) {
    147157                $handle = stream_socket_client( 'tcp://' . $proxy->host() . ':' . $proxy->port(), $connection_error, $connection_error_str, $connect_timeout, STREAM_CLIENT_CONNECT, $context );
    148             else
     158            } else {
    149159                $handle = stream_socket_client( $connect_host . ':' . $arrURL['port'], $connection_error, $connection_error_str, $connect_timeout, STREAM_CLIENT_CONNECT, $context );
     160            }
    150161        }
    151162
    152163        if ( false === $handle ) {
    153164            // SSL connection failed due to expired/invalid cert, or, OpenSSL configuration is broken.
    154             if ( $secure_transport && 0 === $connection_error && '' === $connection_error_str )
     165            if ( $secure_transport && 0 === $connection_error && '' === $connection_error_str ) {
    155166                return new WP_Error( 'http_request_failed', __( 'The SSL certificate for the host could not be verified.' ) );
    156 
    157             return new WP_Error('http_request_failed', $connection_error . ': ' . $connection_error_str );
     167            }
     168
     169            return new WP_Error( 'http_request_failed', $connection_error . ': ' . $connection_error_str );
    158170        }
    159171
    160172        // Verify that the SSL certificate is valid for this request.
    161173        if ( $secure_transport && $ssl_verify && ! $proxy->is_enabled() ) {
    162             if ( ! self::verify_ssl_certificate( $handle, $arrURL['host'] ) )
     174            if ( ! self::verify_ssl_certificate( $handle, $arrURL['host'] ) ) {
    163175                return new WP_Error( 'http_request_failed', __( 'The SSL certificate for the host could not be verified.' ) );
     176            }
    164177        }
    165178
    166179        stream_set_timeout( $handle, $timeout, $utimeout );
    167180
    168         if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) //Some proxies require full URL in this field.
     181        if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) { //Some proxies require full URL in this field.
    169182            $requestPath = $url;
    170         else
    171             $requestPath = $arrURL['path'] . ( isset($arrURL['query']) ? '?' . $arrURL['query'] : '' );
    172 
    173         $strHeaders = strtoupper($r['method']) . ' ' . $requestPath . ' HTTP/' . $r['httpversion'] . "\r\n";
     183        } else {
     184            $requestPath = $arrURL['path'] . ( isset( $arrURL['query'] ) ? '?' . $arrURL['query'] : '' );
     185        }
     186
     187        $strHeaders = strtoupper( $r['method'] ) . ' ' . $requestPath . ' HTTP/' . $r['httpversion'] . "\r\n";
    174188
    175189        $include_port_in_host_header = (
    176190            ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) ||
    177             ( 'http'  == $arrURL['scheme'] && 80 != $arrURL['port'] ) ||
     191            ( 'http' == $arrURL['scheme'] && 80 != $arrURL['port'] ) ||
    178192            ( 'https' == $arrURL['scheme'] && 443 != $arrURL['port'] )
    179193        );
     
    185199        }
    186200
    187         if ( isset($r['user-agent']) )
     201        if ( isset( $r['user-agent'] ) ) {
    188202            $strHeaders .= 'User-agent: ' . $r['user-agent'] . "\r\n";
    189 
    190         if ( is_array($r['headers']) ) {
    191             foreach ( (array) $r['headers'] as $header => $headerValue )
     203        }
     204
     205        if ( is_array( $r['headers'] ) ) {
     206            foreach ( (array) $r['headers'] as $header => $headerValue ) {
    192207                $strHeaders .= $header . ': ' . $headerValue . "\r\n";
     208            }
    193209        } else {
    194210            $strHeaders .= $r['headers'];
    195211        }
    196212
    197         if ( $proxy->use_authentication() )
     213        if ( $proxy->use_authentication() ) {
    198214            $strHeaders .= $proxy->authentication_header() . "\r\n";
     215        }
    199216
    200217        $strHeaders .= "\r\n";
    201218
    202         if ( ! is_null($r['body']) )
     219        if ( ! is_null( $r['body'] ) ) {
    203220            $strHeaders .= $r['body'];
    204 
    205         fwrite($handle, $strHeaders);
     221        }
     222
     223        fwrite( $handle, $strHeaders );
    206224
    207225        if ( ! $r['blocking'] ) {
    208226            stream_set_blocking( $handle, 0 );
    209227            fclose( $handle );
    210             return array( 'headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array() );
    211         }
    212 
    213         $strResponse = '';
    214         $bodyStarted = false;
     228            return array(
     229                'headers'  => array(),
     230                'body'     => '',
     231                'response' => array(
     232                    'code'    => false,
     233                    'message' => false,
     234                ),
     235                'cookies'  => array(),
     236            );
     237        }
     238
     239        $strResponse  = '';
     240        $bodyStarted  = false;
    215241        $keep_reading = true;
    216         $block_size = 4096;
    217         if ( isset( $r['limit_response_size'] ) )
     242        $block_size   = 4096;
     243        if ( isset( $r['limit_response_size'] ) ) {
    218244            $block_size = min( $block_size, $r['limit_response_size'] );
     245        }
    219246
    220247        // If streaming to a file setup the file handle.
    221248        if ( $r['stream'] ) {
    222             if ( ! WP_DEBUG )
     249            if ( ! WP_DEBUG ) {
    223250                $stream_handle = @fopen( $r['filename'], 'w+' );
    224             else
     251            } else {
    225252                $stream_handle = fopen( $r['filename'], 'w+' );
     253            }
    226254            if ( ! $stream_handle ) {
    227                 return new WP_Error( 'http_request_failed', sprintf(
    228                     /* translators: 1: fopen() 2: file name */
    229                     __( 'Could not open handle for %1$s to %2$s.' ),
    230                     'fopen()',
    231                     $r['filename']
    232                 ) );
     255                return new WP_Error(
     256                    'http_request_failed', sprintf(
     257                        /* translators: 1: fopen() 2: file name */
     258                        __( 'Could not open handle for %1$s to %2$s.' ),
     259                        'fopen()',
     260                        $r['filename']
     261                    )
     262                );
    233263            }
    234264
    235265            $bytes_written = 0;
    236             while ( ! feof($handle) && $keep_reading ) {
     266            while ( ! feof( $handle ) && $keep_reading ) {
    237267                $block = fread( $handle, $block_size );
    238268                if ( ! $bodyStarted ) {
    239269                    $strResponse .= $block;
    240270                    if ( strpos( $strResponse, "\r\n\r\n" ) ) {
    241                         $process = WP_Http::processResponse( $strResponse );
     271                        $process     = WP_Http::processResponse( $strResponse );
    242272                        $bodyStarted = true;
    243                         $block = $process['body'];
     273                        $block       = $process['body'];
    244274                        unset( $strResponse );
    245275                        $process['body'] = '';
     
    251281                if ( isset( $r['limit_response_size'] ) && ( $bytes_written + $this_block_size ) > $r['limit_response_size'] ) {
    252282                    $this_block_size = ( $r['limit_response_size'] - $bytes_written );
    253                     $block = substr( $block, 0, $this_block_size );
     283                    $block           = substr( $block, 0, $this_block_size );
    254284                }
    255285
     
    264294                $bytes_written += $bytes_written_to_file;
    265295
    266                 $keep_reading = !isset( $r['limit_response_size'] ) || $bytes_written < $r['limit_response_size'];
     296                $keep_reading = ! isset( $r['limit_response_size'] ) || $bytes_written < $r['limit_response_size'];
    267297            }
    268298
     
    272302            $header_length = 0;
    273303            while ( ! feof( $handle ) && $keep_reading ) {
    274                 $block = fread( $handle, $block_size );
     304                $block        = fread( $handle, $block_size );
    275305                $strResponse .= $block;
    276306                if ( ! $bodyStarted && strpos( $strResponse, "\r\n\r\n" ) ) {
    277307                    $header_length = strpos( $strResponse, "\r\n\r\n" ) + 4;
    278                     $bodyStarted = true;
     308                    $bodyStarted   = true;
    279309                }
    280                 $keep_reading = ( ! $bodyStarted || !isset( $r['limit_response_size'] ) || strlen( $strResponse ) < ( $header_length + $r['limit_response_size'] ) );
     310                $keep_reading = ( ! $bodyStarted || ! isset( $r['limit_response_size'] ) || strlen( $strResponse ) < ( $header_length + $r['limit_response_size'] ) );
    281311            }
    282312
     
    291321
    292322        $response = array(
    293             'headers' => $arrHeaders['headers'],
     323            'headers'  => $arrHeaders['headers'],
    294324            // Not yet processed.
    295             'body' => null,
     325            'body'     => null,
    296326            'response' => $arrHeaders['response'],
    297             'cookies' => $arrHeaders['cookies'],
    298             'filename' => $r['filename']
     327            'cookies'  => $arrHeaders['cookies'],
     328            'filename' => $r['filename'],
    299329        );
    300330
    301331        // Handle redirects.
    302         if ( false !== ( $redirect_response = WP_Http::handle_redirects( $url, $r, $response ) ) )
     332        if ( false !== ( $redirect_response = WP_Http::handle_redirects( $url, $r, $response ) ) ) {
    303333            return $redirect_response;
     334        }
    304335
    305336        // If the body was chunk encoded, then decode it.
    306         if ( ! empty( $process['body'] ) && isset( $arrHeaders['headers']['transfer-encoding'] ) && 'chunked' == $arrHeaders['headers']['transfer-encoding'] )
    307             $process['body'] = WP_Http::chunkTransferDecode($process['body']);
    308 
    309         if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode($arrHeaders['headers']) )
     337        if ( ! empty( $process['body'] ) && isset( $arrHeaders['headers']['transfer-encoding'] ) && 'chunked' == $arrHeaders['headers']['transfer-encoding'] ) {
     338            $process['body'] = WP_Http::chunkTransferDecode( $process['body'] );
     339        }
     340
     341        if ( true === $r['decompress'] && true === WP_Http_Encoding::should_decode( $arrHeaders['headers'] ) ) {
    310342            $process['body'] = WP_Http_Encoding::decompress( $process['body'] );
    311 
    312         if ( isset( $r['limit_response_size'] ) && strlen( $process['body'] ) > $r['limit_response_size'] )
     343        }
     344
     345        if ( isset( $r['limit_response_size'] ) && strlen( $process['body'] ) > $r['limit_response_size'] ) {
    313346            $process['body'] = substr( $process['body'], 0, $r['limit_response_size'] );
     347        }
    314348
    315349        $response['body'] = $process['body'];
     
    338372        $context_options = stream_context_get_options( $stream );
    339373
    340         if ( empty( $context_options['ssl']['peer_certificate'] ) )
     374        if ( empty( $context_options['ssl']['peer_certificate'] ) ) {
    341375            return false;
     376        }
    342377
    343378        $cert = openssl_x509_parse( $context_options['ssl']['peer_certificate'] );
    344         if ( ! $cert )
     379        if ( ! $cert ) {
    345380            return false;
     381        }
    346382
    347383        /*
     
    356392            foreach ( $match_against as $match ) {
    357393                list( $match_type, $match_host ) = explode( ':', $match );
    358                 if ( $host_type == strtolower( trim( $match_type ) ) ) // IP: or DNS:
     394                if ( $host_type == strtolower( trim( $match_type ) ) ) { // IP: or DNS:
    359395                    $certificate_hostnames[] = strtolower( trim( $match_host ) );
    360             }
    361         } elseif ( !empty( $cert['subject']['CN'] ) ) {
     396                }
     397            }
     398        } elseif ( ! empty( $cert['subject']['CN'] ) ) {
    362399            // Only use the CN when the certificate includes no subjectAltName extension.
    363400            $certificate_hostnames[] = strtolower( $cert['subject']['CN'] );
     
    365402
    366403        // Exact hostname/IP matches.
    367         if ( in_array( strtolower( $host ), $certificate_hostnames ) )
     404        if ( in_array( strtolower( $host ), $certificate_hostnames ) ) {
    368405            return true;
     406        }
    369407
    370408        // IP's can't be wildcards, Stop processing.
    371         if ( 'ip' == $host_type )
     409        if ( 'ip' == $host_type ) {
    372410            return false;
     411        }
    373412
    374413        // Test to see if the domain is at least 2 deep for wildcard support.
    375         if ( substr_count( $host, '.' ) < 2 )
     414        if ( substr_count( $host, '.' ) < 2 ) {
    376415            return false;
     416        }
    377417
    378418        // Wildcard subdomains certs (*.example.com) are valid for a.example.com but not a.b.example.com.
     
    393433     */
    394434    public static function test( $args = array() ) {
    395         if ( ! function_exists( 'stream_socket_client' ) )
     435        if ( ! function_exists( 'stream_socket_client' ) ) {
    396436            return false;
     437        }
    397438
    398439        $is_ssl = isset( $args['ssl'] ) && $args['ssl'];
    399440
    400441        if ( $is_ssl ) {
    401             if ( ! extension_loaded( 'openssl' ) )
     442            if ( ! extension_loaded( 'openssl' ) ) {
    402443                return false;
    403             if ( ! function_exists( 'openssl_x509_parse' ) )
     444            }
     445            if ( ! function_exists( 'openssl_x509_parse' ) ) {
    404446                return false;
     447            }
    405448        }
    406449
Note: See TracChangeset for help on using the changeset viewer.