- /**
- * Retrieve header string for proxy authentication.
- *
- * @since 2.8
- *
- * @return string
- */
- function authentication_header() {
- return 'Proxy-Authorization: Basic ' . base64_encode( $this->authentication() );
- }
-
- /**
- * Whether URL should be sent through the proxy server.
- *
- * We want to keep localhost and the blog URL from being sent through the proxy server, because
- * some proxies can not handle this. We also have the constant available for defining other
- * hosts that won't be sent through the proxy.
- *
- * @uses WP_PROXY_BYPASS_HOSTS
- * @since 2.8.0
- *
- * @param string $uri URI to check.
- * @return bool True, to send through the proxy and false if, the proxy should not be used.
- */
- function send_through_proxy( $uri ) {
- // parse_url() only handles http, https type URLs, and will emit E_WARNING on failure.
- // This will be displayed on blogs, which is not reasonable.
- $check = @parse_url($uri);
-
- // Malformed URL, can not process, but this could mean ssl, so let through anyway.
- if ( $check === false )
- return true;
-
- $home = parse_url( get_option('siteurl') );
-
- if ( $check['host'] == 'localhost' || $check['host'] == $home['host'] )
- return false;
-
- if ( !defined('WP_PROXY_BYPASS_HOSTS') )
- return true;
-
- static $bypass_hosts;
- static $wildcard_regex = false;
- if ( null == $bypass_hosts ) {
- $bypass_hosts = preg_split('|,\s*|', WP_PROXY_BYPASS_HOSTS);
-
- if ( false !== strpos(WP_PROXY_BYPASS_HOSTS, '*') ) {
- $wildcard_regex = array();
- foreach ( $bypass_hosts as $host )
- $wildcard_regex[] = str_replace('\*', '[\w.]+?', preg_quote($host, '/'));
- $wildcard_regex = '/^(' . implode('|', $wildcard_regex) . ')$/i';
- }
- }
-
- if ( !empty($wildcard_regex) )
- return !preg_match($wildcard_regex, $check['host']);
- else
- return !in_array( $check['host'], $bypass_hosts );
- }
-}
-/**
- * Internal representation of a single cookie.
- *
- * Returned cookies are represented using this class, and when cookies are set, if they are not
- * already a WP_Http_Cookie() object, then they are turned into one.
- *
- * @todo The WordPress convention is to use underscores instead of camelCase for function and method
- * names. Need to switch to use underscores instead for the methods.
- *
- * @package WordPress
- * @subpackage HTTP
- * @since 2.8.0
- */
-class WP_Http_Cookie {
-
- /**
- * Cookie name.
- *
- * @since 2.8.0
- * @var string
- */
- var $name;
-
- /**
- * Cookie value.
- *
- * @since 2.8.0
- * @var string
- */
- var $value;
-
- /**
- * When the cookie expires.
- *
- * @since 2.8.0
- * @var string
- */
- var $expires;
-
- /**
- * Cookie URL path.
- *
- * @since 2.8.0
- * @var string
- */
- var $path;
-
- /**
- * Cookie Domain.
- *
- * @since 2.8.0
- * @var string
- */
- var $domain;
-
- /**
- * Sets up this cookie object.
- *
- * The parameter $data should be either an associative array containing the indices names below
- * or a header string detailing it.
- *
- * If it's an array, it should include the following elements:
- * <ol>
- * <li>Name</li>
- * <li>Value - should NOT be urlencoded already.</li>
- * <li>Expires - (optional) String or int (UNIX timestamp).</li>
- * <li>Path (optional)</li>
- * <li>Domain (optional)</li>
- * </ol>
- *
- * @access public
- * @since 2.8.0
- *
- * @param string|array $data Raw cookie data.
- */
- function __construct( $data ) {
- if ( is_string( $data ) ) {
- // Assume it's a header string direct from a previous request
- $pairs = explode( ';', $data );
-
- // Special handling for first pair; name=value. Also be careful of "=" in value
- $name = trim( substr( $pairs[0], 0, strpos( $pairs[0], '=' ) ) );
- $value = substr( $pairs[0], strpos( $pairs[0], '=' ) + 1 );
- $this->name = $name;
- $this->value = urldecode( $value );
- array_shift( $pairs ); //Removes name=value from items.
-
- // Set everything else as a property
- foreach ( $pairs as $pair ) {
- $pair = rtrim($pair);
- if ( empty($pair) ) //Handles the cookie ending in ; which results in a empty final pair
- continue;
-
- list( $key, $val ) = strpos( $pair, '=' ) ? explode( '=', $pair ) : array( $pair, '' );
- $key = strtolower( trim( $key ) );
- if ( 'expires' == $key )
- $val = strtotime( $val );
- $this->$key = $val;
- }
- } else {
- if ( !isset( $data['name'] ) )
- return false;
-
- // Set properties based directly on parameters
- $this->name = $data['name'];
- $this->value = isset( $data['value'] ) ? $data['value'] : '';
- $this->path = isset( $data['path'] ) ? $data['path'] : '';
- $this->domain = isset( $data['domain'] ) ? $data['domain'] : '';
-
- if ( isset( $data['expires'] ) )
- $this->expires = is_int( $data['expires'] ) ? $data['expires'] : strtotime( $data['expires'] );
- else
- $this->expires = null;
- }
- }
-
- /**
- * Confirms that it's OK to send this cookie to the URL checked against.
- *
- * Decision is based on RFC 2109/2965, so look there for details on validity.
- *
- * @access public
- * @since 2.8.0
- *
- * @param string $url URL you intend to send this cookie to
- * @return boolean TRUE if allowed, FALSE otherwise.
- */
- function test( $url ) {
- // Expires - if expired then nothing else matters
- if ( time() > $this->expires )
- return false;
-
- // Get details on the URL we're thinking about sending to
- $url = parse_url( $url );
- $url['port'] = isset( $url['port'] ) ? $url['port'] : 80;
- $url['path'] = isset( $url['path'] ) ? $url['path'] : '/';
-
- // Values to use for comparison against the URL
- $path = isset( $this->path ) ? $this->path : '/';
- $port = isset( $this->port ) ? $this->port : 80;
- $domain = isset( $this->domain ) ? strtolower( $this->domain ) : strtolower( $url['host'] );
- if ( false === stripos( $domain, '.' ) )
- $domain .= '.local';
-
- // Host - very basic check that the request URL ends with the domain restriction (minus leading dot)
- $domain = substr( $domain, 0, 1 ) == '.' ? substr( $domain, 1 ) : $domain;
- if ( substr( $url['host'], -strlen( $domain ) ) != $domain )
- return false;
-
- // Port - supports "port-lists" in the format: "80,8000,8080"
- if ( !in_array( $url['port'], explode( ',', $port) ) )
- return false;
-
- // Path - request path must start with path restriction
- if ( substr( $url['path'], 0, strlen( $path ) ) != $path )
- return false;
-
- return true;
- }
-
- /**
- * Convert cookie name and value back to header string.
- *
- * @access public
- * @since 2.8.0
- *
- * @return string Header encoded cookie name and value.
- */
- function getHeaderValue() {
- if ( empty( $this->name ) || empty( $this->value ) )
- return '';
-
- return $this->name . '=' . urlencode( $this->value );
- }
-
- /**
- * Retrieve cookie header for usage in the rest of the WordPress HTTP API.
- *
- * @access public
- * @since 2.8.0
- *
- * @return string
- */
- function getFullHeader() {
- return 'Cookie: ' . $this->getHeaderValue();
- }
-}
-
-/**
- * Implementation for deflate and gzip transfer encodings.
- *
- * Includes RFC 1950, RFC 1951, and RFC 1952.
- *
- * @since 2.8
- * @package WordPress
- * @subpackage HTTP
- */
-class WP_Http_Encoding {
-
- /**
- * Compress raw string using the deflate format.
- *
- * Supports the RFC 1951 standard.
- *
- * @since 2.8
- *
- * @param string $raw String to compress.
- * @param int $level Optional, default is 9. Compression level, 9 is highest.
- * @param string $supports Optional, not used. When implemented it will choose the right compression based on what the server supports.
- * @return string|bool False on failure.
- */
- public static function compress( $raw, $level = 9, $supports = null ) {
- return gzdeflate( $raw, $level );
- }
-
- /**
- * Decompression of deflated string.
- *
- * Will attempt to decompress using the RFC 1950 standard, and if that fails
- * then the RFC 1951 standard deflate will be attempted. Finally, the RFC
- * 1952 standard gzip decode will be attempted. If all fail, then the
- * original compressed string will be returned.
- *
- * @since 2.8
- *
- * @param string $compressed String to decompress.
- * @param int $length The optional length of the compressed data.
- * @return string|bool False on failure.
- */
- public static function decompress( $compressed, $length = null ) {
-
- if ( empty($compressed) )
- return $compressed;
-
- if ( false !== ( $decompressed = @gzinflate( $compressed ) ) )
- return $decompressed;
-
- if ( false !== ( $decompressed = WP_Http_Encoding::compatible_gzinflate( $compressed ) ) )
- return $decompressed;
-
- if ( false !== ( $decompressed = @gzuncompress( $compressed ) ) )
- return $decompressed;
-
- if ( function_exists('gzdecode') ) {
- $decompressed = @gzdecode( $compressed );
-
- if ( false !== $decompressed )
- return $decompressed;
- }
-
- return $compressed;
- }
-
- /**
- * Decompression of deflated string while staying compatible with the majority of servers.
- *
- * Certain Servers will return deflated data with headers which PHP's gziniflate()
- * function cannot handle out of the box. The following function has been created from
- * various snippets on the gzinflate() PHP documentation.
- *
- * Warning: Magic numbers within. Due to the potential different formats that the compressed
- * data may be returned in, some "magic offsets" are needed to ensure proper decompression
- * takes place. For a simple progmatic way to determine the magic offset in use, see:
- * http://core.trac.wordpress.org/ticket/18273
- *
- * @since 2.8.1
- * @link http://core.trac.wordpress.org/ticket/18273
- * @link http://au2.php.net/manual/en/function.gzinflate.php#70875
- * @link http://au2.php.net/manual/en/function.gzinflate.php#77336
- *
- * @param string $gzData String to decompress.
- * @return string|bool False on failure.
- */
- public static function compatible_gzinflate($gzData) {
-
- // Compressed data might contain a full header, if so strip it for gzinflate()
- if ( substr($gzData, 0, 3) == "\x1f\x8b\x08" ) {
- $i = 10;
- $flg = ord( substr($gzData, 3, 1) );
- if ( $flg > 0 ) {
- if ( $flg & 4 ) {
- list($xlen) = unpack('v', substr($gzData, $i, 2) );
- $i = $i + 2 + $xlen;
- }
- if ( $flg & 8 )
- $i = strpos($gzData, "\0", $i) + 1;
- if ( $flg & 16 )
- $i = strpos($gzData, "\0", $i) + 1;
- if ( $flg & 2 )
- $i = $i + 2;
- }
- $decompressed = @gzinflate( substr($gzData, $i, -8) );
- if ( false !== $decompressed )
- return $decompressed;
- }
-
- // Compressed data from java.util.zip.Deflater amongst others.
- $decompressed = @gzinflate( substr($gzData, 2) );
- if ( false !== $decompressed )
- return $decompressed;
-
- return false;
- }
-
- /**
- * What encoding types to accept and their priority values.
- *
- * @since 2.8
- *
- * @return string Types of encoding to accept.
- */
- public static function accept_encoding() {
- $type = array();
- if ( function_exists( 'gzinflate' ) )
- $type[] = 'deflate;q=1.0';
-
- if ( function_exists( 'gzuncompress' ) )
- $type[] = 'compress;q=0.5';
-
- if ( function_exists( 'gzdecode' ) )
- $type[] = 'gzip;q=0.5';
-
- return implode(', ', $type);
- }
-
- /**
- * What encoding the content used when it was compressed to send in the headers.
- *
- * @since 2.8
- *
- * @return string Content-Encoding string to send in the header.
- */
- public static function content_encoding() {
- return 'deflate';
- }
-
- /**
- * Whether the content be decoded based on the headers.
- *
- * @since 2.8
- *
- * @param array|string $headers All of the available headers.
- * @return bool
- */
- public static function should_decode($headers) {
- if ( is_array( $headers ) ) {
- if ( array_key_exists('content-encoding', $headers) && ! empty( $headers['content-encoding'] ) )
- return true;
- } else if ( is_string( $headers ) ) {
- return ( stripos($headers, 'content-encoding:') !== false );
- }
-
- return false;
- }
-
- /**
- * Whether decompression and compression are supported by the PHP version.
- *
- * Each function is tested instead of checking for the zlib extension, to
- * ensure that the functions all exist in the PHP version and aren't
- * disabled.
- *
- * @since 2.8
- *
- * @return bool
- */
- public static function is_available() {
- return ( function_exists('gzuncompress') || function_exists('gzdeflate') || function_exists('gzinflate') );
- }