From 80a6b13f810760e7e73ef1c17abb485df2d2fb5c Mon Sep 17 00:00:00 2001 From: ryan Date: Thu, 5 Feb 2009 20:46:39 +0000 Subject: [PATCH] HTTP API updates and fixes. Props sivel. see #8702 git-svn-id: http://svn.automattic.com/wordpress/trunk@10507 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/functions.php | 73 +++++++++++++++++++++++++++++++++++++++ wp-includes/http.php | 72 +++++++++++++++++++++++++------------- wp-includes/rewrite.php | 6 ++-- wp-includes/rss.php | 16 +++------ wp-settings.php | 7 ++-- 5 files changed, 133 insertions(+), 41 deletions(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 6bcc79061..4838b84bb 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -624,6 +624,79 @@ function delete_option( $name ) { return true; } +/** + * Delete a transient + * + * @since 2.8.0 + * @package WordPress + * @subpackage Transient + * + * @param string $transient Transient name. Expected to not be SQL-escaped + * @return bool true if successful, false otherwise + */ +function delete_transient($transient) { + global $_wp_using_ext_object_cache, $wpdb; + + if ( $_wp_using_ext_object_cache ) { + return wp_cache_delete($transient, 'transient'); + } else { + $transient = $wpdb->escape($transient); + return delete_option($transient); + } +} + +/** + * Get the value of a transient + * + * If the transient does not exist or does not have a value, then the return value + * will be false. + * + * @since 2.8.0 + * @package WordPress + * @subpackage Transient + * + * @param string $transient Transient name. Expected to not be SQL-escaped + * @return mixed Value of transient + */ +function get_transient($transient) { + global $_wp_using_ext_object_cache, $wpdb; + + if ( $_wp_using_ext_object_cache ) { + return wp_cache_get($transient, 'transient'); + } else { + $transient = $wpdb->escape($transient); + return get_option($transient); + } +} + +/** + * Set/update the value of a transient + * + * You do not need to serialize values, if the value needs to be serialize, then + * it will be serialized before it is set. + * + * @since 2.8.0 + * @package WordPress + * @subpackage Transient + * + * @param string $transient Transient name. Expected to not be SQL-escaped + * @param mixed $value Transient value. + * @return bool False if value was not set and true if value was set. + */ +function set_transient($transient, $value) { + global $_wp_using_ext_object_cache, $wpdb; + + if ( $_wp_using_ext_object_cache ) { + return wp_cache_set($transient, $value, 'transient'); + } else { + $safe_transient = $wpdb->escape($transient); + if ( false === get_option( $safe_transient ) ) + return add_option($transient, $value, '', 'no'); + else + return update_option($transient, $value); + } +} + /** * Saves and restores user interface settings stored in a cookie. * diff --git a/wp-includes/http.php b/wp-includes/http.php index b16008ccc..a832e1748 100644 --- a/wp-includes/http.php +++ b/wp-includes/http.php @@ -237,10 +237,10 @@ class WP_Http { } else if ( true === WP_Http_Streams::test() && apply_filters('use_streams_transport', true) ) { $working_transport['streams'] = new WP_Http_Streams(); $blocking_transport[] = &$working_transport['streams']; - } else if ( true === WP_Http_Fopen::test() && apply_filters('use_fopen_transport', true) ) { + } else if ( true === WP_Http_Fopen::test() && apply_filters('use_fopen_transport', true) && ( isset($args['ssl']) && !$args['ssl'] ) ) { $working_transport['fopen'] = new WP_Http_Fopen(); $blocking_transport[] = &$working_transport['fopen']; - } else if ( true === WP_Http_Fsockopen::test() && apply_filters('use_fsockopen_transport', true) ) { + } else if ( true === WP_Http_Fsockopen::test() && apply_filters('use_fsockopen_transport', true) && ( isset($args['ssl']) && !$args['ssl'] ) ) { $working_transport['fsockopen'] = new WP_Http_Fsockopen(); $blocking_transport[] = &$working_transport['fsockopen']; } @@ -282,15 +282,18 @@ class WP_Http { if ( true === WP_Http_ExtHttp::test() && apply_filters('use_http_extension_transport', true) ) { $working_transport['exthttp'] = new WP_Http_ExtHttp(); $blocking_transport[] = &$working_transport['exthttp']; + } else if ( true === WP_Http_Curl::test() && apply_filters('use_curl_transport', true) ) { + $working_transport['curl'] = new WP_Http_Curl(); + $blocking_transport[] = &$working_transport['curl']; } else if ( true === WP_Http_Streams::test() && apply_filters('use_streams_transport', true) ) { $working_transport['streams'] = new WP_Http_Streams(); $blocking_transport[] = &$working_transport['streams']; - } else if ( true === WP_Http_Fsockopen::test() && apply_filters('use_fsockopen_transport', true) ) { + } else if ( true === WP_Http_Fsockopen::test() && apply_filters('use_fsockopen_transport', true) && ( isset($args['ssl']) && !$args['ssl'] ) ) { $working_transport['fsockopen'] = new WP_Http_Fsockopen(); $blocking_transport[] = &$working_transport['fsockopen']; } - foreach ( array('streams', 'fsockopen', 'exthttp') as $transport ) { + foreach ( array('curl', 'streams', 'fsockopen', 'exthttp') as $transport ) { if ( isset($working_transport[$transport]) ) $nonblocking_transport[] = &$working_transport[$transport]; } @@ -358,17 +361,27 @@ class WP_Http { 'timeout' => apply_filters( 'http_request_timeout', 5), 'redirection' => apply_filters( 'http_request_redirection_count', 5), 'httpversion' => apply_filters( 'http_request_version', '1.0'), - 'user-agent' => apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version ), + 'user-agent' => apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) ), 'blocking' => true, 'headers' => array(), 'body' => null, 'compress' => false, - 'decompress' => true + 'decompress' => true, + 'sslverify' => true ); $r = wp_parse_args( $args, $defaults ); $r = apply_filters( 'http_request_args', $r, $url ); + $arrURL = parse_url($url); + + // Determine if this is a https call and pass that on to the transport functions + // so that we can blacklist the transports that do not support ssl verification + if ( $arrURL['scheme'] == 'https' || $arrURL['scheme'] == 'ssl' ) + $r['ssl'] = true; + else + $r['ssl'] = false; + if ( is_null( $r['headers'] ) ) $r['headers'] = array(); @@ -927,7 +940,11 @@ class WP_Http_Streams { 'max_redirects' => $r['redirection'], 'protocol_version' => (float) $r['httpversion'], 'header' => $strHeaders, - 'timeout' => $r['timeout'] + 'timeout' => $r['timeout'], + 'ssl' => array( + 'verify_peer' => apply_filters('https_ssl_verify', $r['sslverify']), + 'verify_host' => apply_filters('https_ssl_verify', $r['sslverify']) + ) ) ); @@ -1060,6 +1077,10 @@ class WP_Http_ExtHTTP { 'redirect' => $r['redirection'], 'useragent' => $r['user-agent'], 'headers' => $r['headers'], + 'ssl' => array( + 'verifypeer' => apply_filters('https_ssl_verify', $r['sslverify']), + 'verifyhost' => apply_filters('https_ssl_verify', $r['sslverify']) + ) ); if ( !defined('WP_DEBUG') || ( defined('WP_DEBUG') && false === WP_DEBUG ) ) //Emits warning level notices for max redirects and timeouts @@ -1153,28 +1174,31 @@ class WP_Http_Curl { $r['timeout'] = 1; $handle = curl_init(); + curl_setopt( $handle, CURLOPT_URL, $url); - - // The cURL extension requires that the option be set for the HEAD to - // work properly. - if ( 'HEAD' === $r['method'] ) { - curl_setopt( $handle, CURLOPT_NOBODY, true ); - } - - if ( true === $r['blocking'] ) { - curl_setopt( $handle, CURLOPT_HEADER, true ); - curl_setopt( $handle, CURLOPT_RETURNTRANSFER, 1 ); - } else { - curl_setopt( $handle, CURLOPT_HEADER, false ); - curl_setopt( $handle, CURLOPT_NOBODY, true ); - curl_setopt( $handle, CURLOPT_RETURNTRANSFER, 0 ); - } - + curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true ); + curl_setopt( $handle, CURLOPT_SSL_VERIFYHOST, apply_filters('https_ssl_verify', $r['sslverify']) ); + curl_setopt( $handle, CURLOPT_SSL_VERIFYPEER, apply_filters('https_ssl_verify', $r['sslverify']) ); curl_setopt( $handle, CURLOPT_USERAGENT, $r['user-agent'] ); - curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, 1 ); + curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, $r['timeout'] ); curl_setopt( $handle, CURLOPT_TIMEOUT, $r['timeout'] ); curl_setopt( $handle, CURLOPT_MAXREDIRS, $r['redirection'] ); + switch ( $r['method'] ) { + case 'HEAD': + curl_setopt( $handle, CURLOPT_NOBODY, true ); + break; + case 'POST': + curl_setopt( $handle, CURLOPT_POST, true ); + curl_setopt( $handle, CURLOPT_POSTFIELDS, $r['body'] ); + break; + } + + if ( true === $r['blocking'] ) + curl_setopt( $handle, CURLOPT_HEADER, true ); + else + curl_setopt( $handle, CURLOPT_HEADER, false ); + // The option doesn't work with safe mode or when open_basedir is set. if ( !ini_get('safe_mode') && !ini_get('open_basedir') ) curl_setopt( $handle, CURLOPT_FOLLOWLOCATION, true ); diff --git a/wp-includes/rewrite.php b/wp-includes/rewrite.php index 04e94971e..7efb96ef8 100644 --- a/wp-includes/rewrite.php +++ b/wp-includes/rewrite.php @@ -1596,11 +1596,11 @@ class WP_Rewrite { * @return array Rewrite rules. */ function wp_rewrite_rules() { - $this->rules = get_option('rewrite_rules'); + $this->rules = get_transient('rewrite_rules'); if ( empty($this->rules) ) { $this->matches = 'matches'; $this->rewrite_rules(); - update_option('rewrite_rules', $this->rules); + set_transient('rewrite_rules', $this->rules); } return $this->rules; @@ -1783,7 +1783,7 @@ class WP_Rewrite { * @access public */ function flush_rules() { - delete_option('rewrite_rules'); + delete_transient('rewrite_rules'); $this->wp_rewrite_rules(); if ( function_exists('save_mod_rewrite_rules') ) save_mod_rewrite_rules(); diff --git a/wp-includes/rss.php b/wp-includes/rss.php index 996277348..a33c3a7dc 100644 --- a/wp-includes/rss.php +++ b/wp-includes/rss.php @@ -714,14 +714,8 @@ class RSSCache { $cache_option = 'rss_' . $this->file_name( $url ); $cache_timestamp = 'rss_' . $this->file_name( $url ) . '_ts'; - // shouldn't these be using get_option() ? - if ( !$wpdb->get_var( $wpdb->prepare( "SELECT option_name FROM $wpdb->options WHERE option_name = %s", $cache_option ) ) ) - add_option($cache_option, '', '', 'no'); - if ( !$wpdb->get_var( $wpdb->prepare( "SELECT option_name FROM $wpdb->options WHERE option_name = %s", $cache_timestamp ) ) ) - add_option($cache_timestamp, '', '', 'no'); - - update_option($cache_option, $rss); - update_option($cache_timestamp, time() ); + set_transient($cache_option, $rss); + set_transient($cache_timestamp, time() ); return $cache_option; } @@ -736,15 +730,13 @@ class RSSCache { $this->ERROR = ""; $cache_option = 'rss_' . $this->file_name( $url ); - if ( ! get_option( $cache_option ) ) { + if ( ! $rss = get_transient( $cache_option ) ) { $this->debug( "Cache doesn't contain: $url (cache option: $cache_option)" ); return 0; } - $rss = get_option( $cache_option ); - return $rss; } @@ -760,7 +752,7 @@ class RSSCache { $cache_option = $this->file_name( $url ); $cache_timestamp = 'rss_' . $this->file_name( $url ) . '_ts'; - if ( $mtime = get_option($cache_timestamp) ) { + if ( $mtime = get_transient($cache_timestamp) ) { // find how long ago the file was added to the cache // and whether that is longer then MAX_AGE $age = time() - $mtime; diff --git a/wp-settings.php b/wp-settings.php index 553c67d4b..4a35fb876 100644 --- a/wp-settings.php +++ b/wp-settings.php @@ -257,10 +257,13 @@ $prefix = $wpdb->set_prefix($table_prefix); if ( is_wp_error($prefix) ) wp_die(/*WP_I18N_BAD_PREFIX*/'ERROR: $table_prefix in wp-config.php can only contain numbers, letters, and underscores.'/*/WP_I18N_BAD_PREFIX*/); -if ( file_exists(WP_CONTENT_DIR . '/object-cache.php') ) +if ( file_exists(WP_CONTENT_DIR . '/object-cache.php') ) { require_once (WP_CONTENT_DIR . '/object-cache.php'); -else + $_wp_using_ext_object_cache = true; +} else { require_once (ABSPATH . WPINC . '/cache.php'); + $_wp_using_ext_object_cache = false; +} wp_cache_init(); if ( function_exists('wp_cache_add_global_groups') ) {