WordPress.org

Make WordPress Core

Ticket #4779: http.r6350.patch

File http.r6350.patch, 17.9 KB (added by darkdragon, 10 years ago)

Changes WordPress core functions and Admin over to new HTTP API, based off of r6350

  • wp-admin/includes/update.php

     
    4040}
    4141add_action( 'admin_notices', 'update_nag', 3 );
    4242
    43 function wp_update_plugins() {
     43function wp_update_plugins_user_agent($useragent) {
    4444        global $wp_version;
     45        return 'User-Agent: WordPress/' . $wp_version . '; ' . get_bloginfo('url') . "\r\n"
     46}
    4547
    46         if ( !function_exists('fsockopen') )
    47                 return false;
     48function wp_update_plugins() {
    4849
    4950        $plugins = get_plugins();
    5051        $active  = get_option( 'active_plugins' );
     
    7778        $to_send->active = $active;
    7879        $send = serialize( $to_send );
    7980
     81        add_filter('http_headers_useragent', 'wp_update_plugins_user_agent', 1);
     82
    8083        $request = 'plugins=' . urlencode( $send );
    81         $http_request  = "POST /plugins/update-check/1.0/ HTTP/1.0\r\n";
    82         $http_request .= "Host: api.wordpress.org\r\n";
    8384        $http_request .= "Content-Type: application/x-www-form-urlencoded; charset=" . get_option('blog_charset') . "\r\n";
    8485        $http_request .= "Content-Length: " . strlen($request) . "\r\n";
    85         $http_request .= 'User-Agent: WordPress/' . $wp_version . '; ' . get_bloginfo('url') . "\r\n";
    86         $http_request .= "\r\n";
    87         $http_request .= $request;
    8886
    89         $response = '';
    90         if( false != ( $fs = @fsockopen( 'api.wordpress.org', 80, $errno, $errstr, 3) ) && is_resource($fs) ) {
    91                 fwrite($fs, $http_request);
     87        $objResponse = wp_remote_post_object('api.wordpress.org/plugins/update-check/1.0/', $http_request, $request);
    9288
    93                 while ( !feof($fs) )
    94                         $response .= fgets($fs, 1160); // One TCP-IP packet
    95                 fclose($fs);
    96                 $response = explode("\r\n\r\n", $response, 2);
     89        remove_filter('http_headers_useragent', 'wp_update_plugins_user_agent', 1);
     90
     91        if( true === $objResponse->hasError() ) {
     92                unset($objResponse);
     93                return;
    9794        }
    9895
    99         $response = unserialize( $response[1] );
     96        $body = $objResponse->getBody();
     97        $response = unserialize( $body );
    10098
    10199        if ( $response )
    102100                $new_option->response = $response;
  • wp-admin/update-links.php

     
    11<?php
    22require_once('../wp-config.php');
    3 require_once( ABSPATH . 'wp-includes/class-snoopy.php');
    43
    54if ( !get_option('use_linksupdate') )
    65        wp_die(__('Feature disabled.'));
     
    1413
    1514$query_string = "uris=$link_uris";
    1615
    17 $http_request  = "POST /updated-batch/ HTTP/1.0\r\n";
    18 $http_request .= "Host: api.pingomatic.com\r\n";
    1916$http_request .= 'Content-Type: application/x-www-form-urlencoded; charset='.get_option('blog_charset')."\r\n";
    2017$http_request .= 'Content-Length: ' . strlen($query_string) . "\r\n";
    21 $http_request .= 'User-Agent: WordPress/' . $wp_version . "\r\n";
    22 $http_request .= "\r\n";
    23 $http_request .= $query_string;
    2418
    25 $response = '';
    26 if ( false !== ( $fs = @fsockopen('api.pingomatic.com', 80, $errno, $errstr, 5) ) ) {
    27         fwrite($fs, $http_request);
    28         while ( !feof($fs) )
    29                 $response .= fgets($fs, 1160); // One TCP-IP packet
    30         fclose($fs);
     19$objResponse = wp_remote_post_object('http://api.pingomatic.org/updated-batch/', $http_request, $query_string);
    3120
    32         $response = explode("\r\n\r\n", $response, 2);
    33         $body = trim( $response[1] );
    34         $body = str_replace(array("\r\n", "\r"), "\n", $body);
     21if( true === $objResponse->hasError() ) {
     22        wp_die(__('No Connection Established'));
     23}
    3524
    36         $returns = explode("\n", $body);
     25$body = trim( $objResponse->getBody() );
     26$returns = explode("\n", $body);
    3727
    38         foreach ($returns as $return) :
    39                 $time = $wpdb->escape( substr($return, 0, 19) );
    40                 $uri = $wpdb->escape( preg_replace('/(.*?) | (.*?)/', '$2', $return) );
    41                 $wpdb->query("UPDATE $wpdb->links SET link_updated = '$time' WHERE link_url = '$uri'");
    42         endforeach;
    43 }
    44 ?>
     28foreach ($returns as $return) :
     29        $time = $wpdb->escape( substr($return, 0, 19) );
     30        $uri = $wpdb->escape( preg_replace('/(.*?) | (.*?)/', '$2', $return) );
     31        $wpdb->query("UPDATE $wpdb->links SET link_updated = '$time' WHERE link_url = '$uri'");
     32endforeach;
     33?>
     34 No newline at end of file
  • wp-includes/comment.php

     
    532532        $headers = '';
    533533        $pingback_str_dquote = 'rel="pingback"';
    534534        $pingback_str_squote = 'rel=\'pingback\'';
    535         $x_pingback_str = 'x-pingback: ';
     535        $x_pingback_str = 'x-pingback';
    536536        $pingback_href_original_pos = 27;
    537537
    538         extract(parse_url($url), EXTR_SKIP);
     538        $objResponse = wp_remote_get_object($url, null, null, 2);
     539       
     540        $x_pingback_header = $objResponse->getHeaders($x_pingback_str);
     541        if( false === $x_pingback_header )
     542                foreach($objResponse->getHeaders() as $name => $value) {
     543                        if( 0 == strcasecmp($x_pingback_str, $name) )
     544                                $x_pingback_header = $value;
    539545
    540         if ( !isset($host) ) // Not an URL. This should never happen.
    541                 return false;
     546        $content_type = trim($objResponse->getHeaders('Content-type'));
    542547
    543         $path  = ( !isset($path) ) ? '/'          : $path;
    544         $path .= ( isset($query) ) ? '?' . $query : '';
    545         $port  = ( isset($port)  ) ? $port        : 80;
    546 
    547         // Try to connect to the server at $host
    548         $fp = @fsockopen($host, $port, $errno, $errstr, 2);
    549         if ( !$fp ) // Couldn't open a connection to $host
     548        if ( preg_match('#(image|audio|video|model)/#is', $content_type) ) // Not an (x)html, sgml, or xml page, no use going further
    550549                return false;
    551550
    552         // Send the GET request
    553         $request = "GET $path HTTP/1.1\r\nHost: $host\r\nUser-Agent: WordPress/$wp_version \r\n\r\n";
    554         // ob_end_flush();
    555         fputs($fp, $request);
    556 
    557         // Let's check for an X-Pingback header first
    558         while ( !feof($fp) ) {
    559                 $line = fgets($fp, 512);
    560                 if ( trim($line) == '' )
    561                         break;
    562                 $headers .= trim($line)."\n";
    563                 $x_pingback_header_offset = strpos(strtolower($headers), $x_pingback_str);
    564                 if ( $x_pingback_header_offset ) {
    565                         // We got it!
    566                         preg_match('#x-pingback: (.+)#is', $headers, $matches);
    567                         $pingback_server_url = trim($matches[1]);
     551        $contents .= trim($objResponse->getBody());
     552        $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
     553        $pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
     554        if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) {
     555                $quote = ($pingback_link_offset_dquote) ? '"' : '\'';
     556                $pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
     557                $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
     558                $pingback_href_start = $pingback_href_pos+6;
     559                $pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
     560                $pingback_server_url_len = $pingback_href_end - $pingback_href_start;
     561                $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
     562                // We may find rel="pingback" but an incomplete pingback URL
     563                if ( $pingback_server_url_len > 0 ) // We got it!
    568564                        return $pingback_server_url;
    569                 }
    570                 if ( strpos(strtolower($headers), 'content-type: ') ) {
    571                         preg_match('#content-type: (.+)#is', $headers, $matches);
    572                         $content_type = trim($matches[1]);
    573                 }
    574565        }
    575 
    576         if ( preg_match('#(image|audio|video|model)/#is', $content_type) ) // Not an (x)html, sgml, or xml page, no use going further
     566        $byte_count += strlen($line);
     567        if ( $byte_count > $timeout_bytes ) {
     568                // It's no use going further, there probably isn't any pingback
     569                // server to find in this file. (Prevents loading large files.)
    577570                return false;
    578 
    579         while ( !feof($fp) ) {
    580                 $line = fgets($fp, 1024);
    581                 $contents .= trim($line);
    582                 $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
    583                 $pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
    584                 if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) {
    585                         $quote = ($pingback_link_offset_dquote) ? '"' : '\'';
    586                         $pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
    587                         $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
    588                         $pingback_href_start = $pingback_href_pos+6;
    589                         $pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
    590                         $pingback_server_url_len = $pingback_href_end - $pingback_href_start;
    591                         $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
    592                         // We may find rel="pingback" but an incomplete pingback URL
    593                         if ( $pingback_server_url_len > 0 ) // We got it!
    594                                 return $pingback_server_url;
    595                 }
    596                 $byte_count += strlen($line);
    597                 if ( $byte_count > $timeout_bytes ) {
    598                         // It's no use going further, there probably isn't any pingback
    599                         // server to find in this file. (Prevents loading large files.)
    600                         return false;
    601                 }
    602571        }
    603572
    604573        // We didn't find anything.
     
    764733
    765734        if ( empty($trackback_url) )
    766735                return;
    767 
     736st);
    768737        $title = urlencode($title);
    769738        $excerpt = urlencode($excerpt);
    770739        $blog_name = urlencode(get_option('blogname'));
     
    772741        $url = urlencode(get_permalink($ID));
    773742        $query_string = "title=$title&url=$url&blog_name=$blog_name&excerpt=$excerpt";
    774743        $trackback_url = parse_url($trackback_url);
     744       
     745       
    775746        $http_request = 'POST ' . $trackback_url['path'] . ($trackback_url['query'] ? '?'.$trackback_url['query'] : '') . " HTTP/1.0\r\n";
    776747        $http_request .= 'Host: '.$trackback_url['host']."\r\n";
    777748        $http_request .= 'Content-Type: application/x-www-form-urlencoded; charset='.get_option('blog_charset')."\r\n";
     
    782753        if ( '' == $trackback_url['port'] )
    783754                $trackback_url['port'] = 80;
    784755        $fs = @fsockopen($trackback_url['host'], $trackback_url['port'], $errno, $errstr, 4);
    785         @fputs($fs, $http_request);
     756        @fputs($fs, $http_reque
    786757        @fclose($fs);
    787758
    788759        $tb_url = addslashes( $tb_url );
  • wp-includes/cron.php

     
    8282                return;
    8383
    8484        $cron_url = get_option( 'siteurl' ) . '/wp-cron.php';
    85         $parts = parse_url( $cron_url );
    8685
    87         if ($parts['scheme'] == 'https') {
    88                 // support for SSL was added in 4.3.0
    89                 if (version_compare(phpversion(), '4.3.0', '>=') && function_exists('openssl_open')) {
    90                         $port = isset($parts['port']) ? $parts['port'] : 443;
    91                         $argyle = @fsockopen('ssl://' . $parts['host'], $port, $errno, $errstr, 0.01);
    92                 } else {
    93                         return false;
    94                 }
    95         } else {
    96                 $port = isset($parts['port']) ? $parts['port'] : 80;
    97                 $argyle = @ fsockopen( $parts['host'], $port, $errno, $errstr, 0.01 );
    98         }
    99 
    100         if ( $argyle )
    101                 fputs( $argyle,
    102                           "GET {$parts['path']}?check=" . wp_hash('187425') . " HTTP/1.0\r\n"
    103                         . "Host: {$_SERVER['HTTP_HOST']}\r\n\r\n"
    104                 );
     86        wp_remote_post_object($cron_url.'?check='.wp_hash('187425'), null, null, 0.2);
    10587}
    10688
    10789function wp_cron() {
  • wp-includes/functions.php

     
    533533        }
    534534}
    535535
    536 
    537 function wp_get_http_headers( $url, $red = 1 ) {
    538         global $wp_version;
    539         @set_time_limit( 60 );
    540 
    541         if ( $red > 5 )
    542                  return false;
    543 
    544         $parts = parse_url( $url );
    545         $file = $parts['path'] . ( ( $parts['query'] ) ? '?' . $parts['query'] : '' );
    546         $host = $parts['host'];
    547         if ( !isset( $parts['port'] ) )
    548                 $parts['port'] = 80;
    549 
    550         $head = "HEAD $file HTTP/1.1\r\nHOST: $host\r\nUser-Agent: WordPress/" . $wp_version . "\r\n\r\n";
    551 
    552         $fp = @fsockopen( $host, $parts['port'], $err_num, $err_msg, 3 );
    553         if ( !$fp )
    554                 return false;
    555 
    556         $response = '';
    557         fputs( $fp, $head );
    558         while ( !feof( $fp ) && strpos( $response, "\r\n\r\n" ) == false )
    559                 $response .= fgets( $fp, 2048 );
    560         fclose( $fp );
    561         preg_match_all( '/(.*?): (.*)\r/', $response, $matches );
    562         $count = count( $matches[1] );
    563         for ( $i = 0; $i < $count; $i++ ) {
    564                 $key = strtolower( $matches[1][$i] );
    565                 $headers["$key"] = $matches[2][$i];
    566         }
    567 
    568         preg_match( '/.*([0-9]{3}).*/', $response, $return );
    569         $headers['response'] = $return[1]; // HTTP response code eg 204, 200, 404
    570 
    571                 $code = $headers['response'];
    572                 if ( ( '302' == $code || '301' == $code ) && isset( $headers['location'] ) )
    573                                 return wp_get_http_headers( $headers['location'], ++$red );
    574 
    575         return $headers;
     536/**
     537 * Returns only the headers from the URL
     538 *
     539 * @package WordPress
     540 * @since 1.5.1
     541 * @uses wp_remote_get_head() Is alias of wp_remote_get_head()
     542 * @deprecated Use wp_remote_get_head() instead.
     543 *
     544 * @param string $url The URL to retrieve the headers from.
     545 * @param int $deprecated Deprecated. Not used any more.
     546 * @returns array Each header has key/value pair with key as the header name.
     547 */
     548function wp_get_http_headers( $url, $deprecated = 1 ) {
     549        return wp_remote_get_head($url);
    576550}
    577551
    578552
     
    699673        return $array;
    700674}
    701675
     676/**
     677 * Returns only the body from the URL
     678 *
     679 * @package WordPress
     680 * @since 1.5.1
     681 * @uses wp_remote_get_body() Is alias of wp_remote_get_body()
     682 * @deprecated Use wp_remote_get_body() instead.
     683 *
     684 * @param string $url The URL to retrieve the headers from.
     685 * @returns string The body of hte response
     686 */
    702687function wp_remote_fopen( $uri ) {
    703         $timeout = 10;
    704         $parsed_url = @parse_url( $uri );
    705 
    706         if ( !$parsed_url || !is_array( $parsed_url ) )
    707                 return false;
    708 
    709         if ( !isset( $parsed_url['scheme'] ) || !in_array( $parsed_url['scheme'], array( 'http','https' ) ) )
    710                 $uri = 'http://' . $uri;
    711 
    712         if ( ini_get( 'allow_url_fopen' ) ) {
    713                 $fp = @fopen( $uri, 'r' );
    714                 if ( !$fp )
    715                         return false;
    716 
    717                 //stream_set_timeout($fp, $timeout); // Requires php 4.3
    718                 $linea = '';
    719                 while ( $remote_read = fread( $fp, 4096 ) )
    720                         $linea .= $remote_read;
    721                 fclose( $fp );
    722                 return $linea;
    723         } elseif ( function_exists( 'curl_init' ) ) {
    724                 $handle = curl_init();
    725                 curl_setopt( $handle, CURLOPT_URL, $uri);
    726                 curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, 1 );
    727                 curl_setopt( $handle, CURLOPT_RETURNTRANSFER, 1 );
    728                 curl_setopt( $handle, CURLOPT_TIMEOUT, $timeout );
    729                 $buffer = curl_exec( $handle );
    730                 curl_close( $handle );
    731                 return $buffer;
    732         } else {
    733                 return false;
    734         }
     688        return wp_remote_get_body($uri, null, null, 10);
    735689}
    736690
    737691
  • wp-includes/update.php

     
    77 */
    88
    99/**
    10  * wp_version_check() - Check WordPress version against the newest version.
     10 * Check WordPress version against the newest version.
    1111 *
    1212 * The WordPress version, PHP version, and Locale is sent. Checks against the WordPress server at
    13  * api.wordpress.org server. Will only check if PHP has fsockopen enabled and WordPress isn't installing.
     13 * api.wordpress.org server. Will only check if WordPress isn't installing.
    1414 *
    1515 * @package WordPress
    1616 * @since 2.3
     
    1919 * @return mixed Returns null if update is unsupported. Returns false if check is too soon.
    2020 */
    2121function wp_version_check() {
    22         if ( !function_exists('fsockopen') || strpos($_SERVER['PHP_SELF'], 'install.php') !== false || defined('WP_INSTALLING') )
     22        if ( strpos($_SERVER['PHP_SELF'], 'install.php') !== false || defined('WP_INSTALLING') )
    2323                return;
    2424
    2525        global $wp_version;
     
    3636                return false;
    3737
    3838        $new_option = '';
    39         $new_option->last_checked = time(); // this gets set whether we get a response or not, so if something is down or misconfigured it won't delay the page load for more than 3 seconds, twice a day
     39        // this gets set whether we get a response or not, so if something is down or misconfigured it won't delay the page load for more than 3 seconds, twice a day
     40        $new_option->last_checked = time();
    4041        $new_option->version_checked = $wp_version;
    4142
    42         $http_request  = "GET /core/version-check/1.0/?version=$wp_version&php=$php_version&locale=$locale HTTP/1.0\r\n";
    43         $http_request .= "Host: api.wordpress.org\r\n";
    4443        $http_request .= 'Content-Type: application/x-www-form-urlencoded; charset=' . get_option('blog_charset') . "\r\n";
    45         $http_request .= 'User-Agent: WordPress/' . $wp_version . '; ' . get_bloginfo('url') . "\r\n";
    46         $http_request .= "\r\n";
    4744
    48         $response = '';
    49         if ( false !== ( $fs = @fsockopen( 'api.wordpress.org', 80, $errno, $errstr, 3 ) ) && is_resource($fs) ) {
    50                 fwrite( $fs, $http_request );
    51                 while ( !feof( $fs ) )
    52                         $response .= fgets( $fs, 1160 ); // One TCP-IP packet
    53                 fclose( $fs );
     45        add_filter('http_headers_useragent', 'wp_version_check_useragent', 1);
     46        $body = wp_remote_get_body('http://api.wordpress.org/core/version-check/1.0/?version=$wp_version&php=$php_version&locale=$locale', $http_request, null, 3);
     47        remove_filter('http_headers_useragent', 'wp_version_check_useragent', 1);
    5448
    55                 $response = explode("\r\n\r\n", $response, 2);
    56                 $body = trim( $response[1] );
    57                 $body = str_replace(array("\r\n", "\r"), "\n", $body);
     49        $returns = explode("\n", trim($body) );
    5850
    59                 $returns = explode("\n", $body);
     51        $new_option->response = $returns[0];
     52        if ( isset( $returns[1] ) )
     53                $new_option->url = $returns[1];
    6054
    61                 $new_option->response = $returns[0];
    62                 if ( isset( $returns[1] ) )
    63                         $new_option->url = $returns[1];
    64         }
    6555        update_option( 'update_core', $new_option );
    6656}
    6757
     58/**
     59 * Changes filter for the wp_version_check useragent
     60 *
     61 * @package WordPress
     62 * @since Unknown
     63 * @uses $wp_version Used to check against the newest WordPress version.
     64 *
     65 * @param string $useragent Ignores passed useragent
     66 * @return string Bypasses string and returns new user agent.
     67 */
     68function wp_version_check_useragent($useragent) {
     69        global $wp_version;
     70        return 'User-Agent: WordPress/' . $wp_version . '; ' . get_bloginfo('url') . "\r\n";
     71}
     72
    6873add_action( 'init', 'wp_version_check' );
    6974
    7075?>
     76 No newline at end of file
  • wp-settings.php

     
    119119require (ABSPATH . WPINC . '/compat.php');
    120120require (ABSPATH . WPINC . '/functions.php');
    121121require (ABSPATH . WPINC . '/classes.php');
     122require (ABSPATH . WPINC . '/http.php');
    122123
    123124require_wp_db();
    124125$prefix = $wpdb->set_prefix($table_prefix);