Make WordPress Core


Ignore:
Timestamp:
08/12/2008 09:21:11 PM (16 years ago)
Author:
westi
Message:

HTTP API improvements. Implements chunked transfer decoding. Moves plugin update checker over to api. see #4779 props santosj.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/update.php

    r8595 r8630  
    11<?php
    22/**
    3  * A simple set of functions to check our version 1.0 update service
     3 * A simple set of functions to check our version 1.0 update service.
    44 *
    55 * @package WordPress
     
    88
    99/**
    10  * Check WordPress version against the newest version.   *
     10 * Check WordPress version against the newest version.
     11 *
    1112 * The WordPress version, PHP version, and Locale is sent. Checks against the
    1213 * WordPress server at api.wordpress.org server. Will only check if WordPress
    1314 * isn't installing.
    14 
    1515 *
    1616 * @package WordPress
     
    4242
    4343    $url = "http://api.wordpress.org/core/version-check/1.2/?version=$wp_version&php=$php_version&locale=$locale";
     44
    4445    $options = array('timeout' => 3);
    45 
    46     $headers = array(
     46    $options['headers'] = array(
    4747        'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset'),
    4848        'User-Agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo('url')
    4949    );
    5050
    51     $response = wp_remote_request($url, $options, $headers);
     51    $response = wp_remote_request($url, $options);
     52
     53    if ( is_wp_error( $response ) )
     54        return false;
    5255
    5356    if ( 200 != $response['response']['code'] )
    5457        return false;
    5558
    56     $body = $response['body'];
    57     $body = trim( $body );
     59    $body = trim( $response['body'] );
    5860    $body = str_replace(array("\r\n", "\r"), "\n", $body);
    5961    $returns = explode("\n", $body);
     
    7476
    7577/**
    76  * wp_update_plugins() - Check plugin versions against the latest versions hosted on WordPress.org.
     78 * Check plugin versions against the latest versions hosted on WordPress.org.
    7779 *
    78  * The WordPress version, PHP version, and Locale is sent along with a list of all plugins installed.
    79  * Checks against the WordPress server at api.wordpress.org.
    80  * Will only check if PHP has fsockopen enabled and WordPress isn't installing.
     80 * The WordPress version, PHP version, and Locale is sent along with a list of
     81 * all plugins installed. Checks against the WordPress server at
     82 * api.wordpress.org. Will only check if PHP has fsockopen enabled and WordPress
     83 * isn't installing.
    8184 *
    8285 * @package WordPress
     
    8992    global $wp_version;
    9093
    91     if ( !function_exists('fsockopen') || defined('WP_INSTALLING') )
     94    if ( defined('WP_INSTALLING') )
    9295        return false;
    9396
     
    117120    }
    118121
    119     foreach ( (array) $current->response as $plugin_file => $update_details ) {
    120         if ( ! isset($plugins[ $plugin_file ]) ) {
    121             $plugin_changed = true;
     122    if ( isset ( $current->response ) && is_array( $current->response ) ) {
     123        foreach ( $current->response as $plugin_file => $update_details ) {
     124            if ( ! isset($plugins[ $plugin_file ]) ) {
     125                $plugin_changed = true;
     126            }
    122127        }
    123128    }
     
    130135    $to_send->active = $active;
    131136    $send = serialize( $to_send );
     137    $body = 'plugins=' . urlencode( $send );
    132138
    133     $request = 'plugins=' . urlencode( $send );
    134     $http_request  = "POST /plugins/update-check/1.0/ HTTP/1.0\r\n";
    135     $http_request .= "Host: api.wordpress.org\r\n";
    136     $http_request .= "Content-Type: application/x-www-form-urlencoded; charset=" . get_option('blog_charset') . "\r\n";
    137     $http_request .= "Content-Length: " . strlen($request) . "\r\n";
    138     $http_request .= 'User-Agent: WordPress/' . $wp_version . '; ' . get_bloginfo('url') . "\r\n";
    139     $http_request .= "\r\n";
    140     $http_request .= $request;
     139    $options = array('method' => 'POST', 'timeout' => 3, 'body' => $body);
     140    $options['headers'] = array(
     141        'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option('blog_charset'),
     142        'Content-Length' => strlen($body),
     143        'User-Agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo('url')
     144    );
    141145
    142     $response = '';
    143     if( false != ( $fs = @fsockopen( 'api.wordpress.org', 80, $errno, $errstr, 3) ) && is_resource($fs) ) {
    144         fwrite($fs, $http_request);
     146    $raw_response = wp_remote_request('http://api.wordpress.org/plugins/update-check/1.0/', $options);
    145147
    146         while ( !feof($fs) )
    147             $response .= fgets($fs, 1160); // One TCP-IP packet
    148         fclose($fs);
    149         $response = explode("\r\n\r\n", $response, 2);
     148    if( 200 != $raw_response['response']['code'] ) {
     149        return false;
    150150    }
    151151
    152     $response = unserialize( $response[1] );
     152    $response = unserialize( $raw_response['body'] );
    153153
    154154    if ( $response )
Note: See TracChangeset for help on using the changeset viewer.