Пример #1
0
 /**
  * @dataProvider make_absolute_url_testcases
  */
 function test_make_absolute_url($relative_url, $absolute_url, $expected)
 {
     if (!is_callable(array('WP_HTTP', 'make_absolute_url'))) {
         $this->markTestSkipped("This version of WP_HTTP doesn't support WP_HTTP::make_absolute_url()");
         return;
     }
     $actual = WP_HTTP::make_absolute_url($relative_url, $absolute_url);
     $this->assertEquals($expected, $actual);
 }
Пример #2
0
 /**
  * Handles HTTP Redirects and follows them if appropriate.
  *
  * @since 3.7.0
  *
  * @static
  *
  * @param string $url The URL which was requested.
  * @param array $args The Arguments which were used to make the request.
  * @param array $response The Response of the HTTP request.
  * @return false|object False if no redirect is present, a WP_HTTP or WP_Error result otherwise.
  */
 public static function handle_redirects($url, $args, $response)
 {
     // If no redirects are present, or, redirects were not requested, perform no action.
     if (!isset($response['headers']['location']) || 0 === $args['_redirection']) {
         return false;
     }
     // Only perform redirections on redirection http codes.
     if ($response['response']['code'] > 399 || $response['response']['code'] < 300) {
         return false;
     }
     // Don't redirect if we've run out of redirects.
     if ($args['redirection']-- <= 0) {
         return new WP_Error('http_request_failed', __('Too many redirects.'));
     }
     $redirect_location = $response['headers']['location'];
     // If there were multiple Location headers, use the last header specified.
     if (is_array($redirect_location)) {
         $redirect_location = array_pop($redirect_location);
     }
     $redirect_location = WP_HTTP::make_absolute_url($redirect_location, $url);
     // POST requests should not POST to a redirected location.
     if ('POST' == $args['method']) {
         if (in_array($response['response']['code'], array(302, 303))) {
             $args['method'] = 'GET';
         }
     }
     // Include valid cookies in the redirect process.
     if (!empty($response['cookies'])) {
         foreach ($response['cookies'] as $cookie) {
             if ($cookie->test($redirect_location)) {
                 $args['cookies'][] = $cookie;
             }
         }
     }
     return wp_remote_request($redirect_location, $args);
 }
Пример #3
0
/**
 * Callback to add a base url to relative links in passed content.
 *
 * @since 2.7.0
 * @access private
 *
 * @global string $_links_add_base
 *
 * @param string $m The matched link.
 * @return string The processed link.
 */
function _links_add_base($m)
{
    global $_links_add_base;
    //1 = attribute name  2 = quotation mark  3 = URL
    return $m[1] . '=' . $m[2] . (preg_match('#^(\\w{1,20}):#', $m[3], $protocol) && in_array($protocol[1], wp_allowed_protocols()) ? $m[3] : WP_HTTP::make_absolute_url($m[3], $_links_add_base)) . $m[2];
}
Пример #4
0
 /**
  * Send a HTTP request to a URI using cURL extension.
  *
  * @access public
  * @since 2.7.0
  *
  * @param string $url
  * @param str|array $args Optional. Override the defaults.
  * @return array 'headers', 'body', 'response', 'cookies' and 'filename' keys.
  */
 function request($url, $args = array())
 {
     $defaults = array('method' => 'GET', 'timeout' => 5, 'redirection' => 5, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'body' => null, 'cookies' => array());
     $r = wp_parse_args($args, $defaults);
     if (isset($r['headers']['User-Agent'])) {
         $r['user-agent'] = $r['headers']['User-Agent'];
         unset($r['headers']['User-Agent']);
     } else {
         if (isset($r['headers']['user-agent'])) {
             $r['user-agent'] = $r['headers']['user-agent'];
             unset($r['headers']['user-agent']);
         }
     }
     // Construct Cookie: header if any cookies are set.
     WP_Http::buildCookieHeader($r);
     $handle = curl_init();
     // cURL offers really easy proxy support.
     $proxy = new WP_HTTP_Proxy();
     if ($proxy->is_enabled() && $proxy->send_through_proxy($url)) {
         curl_setopt($handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
         curl_setopt($handle, CURLOPT_PROXY, $proxy->host());
         curl_setopt($handle, CURLOPT_PROXYPORT, $proxy->port());
         if ($proxy->use_authentication()) {
             curl_setopt($handle, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
             curl_setopt($handle, CURLOPT_PROXYUSERPWD, $proxy->authentication());
         }
     }
     $is_local = isset($r['local']) && $r['local'];
     $ssl_verify = isset($r['sslverify']) && $r['sslverify'];
     if ($is_local) {
         $ssl_verify = apply_filters('https_local_ssl_verify', $ssl_verify);
     } elseif (!$is_local) {
         $ssl_verify = apply_filters('https_ssl_verify', $ssl_verify);
     }
     // CURLOPT_TIMEOUT and CURLOPT_CONNECTTIMEOUT expect integers. Have to use ceil since
     // a value of 0 will allow an unlimited timeout.
     $timeout = (int) ceil($r['timeout']);
     curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, $timeout);
     curl_setopt($handle, CURLOPT_TIMEOUT, $timeout);
     curl_setopt($handle, CURLOPT_URL, $url);
     curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, $ssl_verify === true ? 2 : false);
     curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, $ssl_verify);
     curl_setopt($handle, CURLOPT_USERAGENT, $r['user-agent']);
     // The option doesn't work with safe mode or when open_basedir is set, and there's a
     // bug #17490 with redirected POST requests, so handle redirections outside Curl.
     curl_setopt($handle, CURLOPT_FOLLOWLOCATION, false);
     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;
         case 'PUT':
             curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
             curl_setopt($handle, CURLOPT_POSTFIELDS, $r['body']);
             break;
         default:
             curl_setopt($handle, CURLOPT_CUSTOMREQUEST, $r['method']);
             if (!is_null($r['body'])) {
                 curl_setopt($handle, CURLOPT_POSTFIELDS, $r['body']);
             }
             break;
     }
     if (true === $r['blocking']) {
         curl_setopt($handle, CURLOPT_HEADERFUNCTION, array($this, 'stream_headers'));
         curl_setopt($handle, CURLOPT_WRITEFUNCTION, array($this, 'stream_body'));
     }
     curl_setopt($handle, CURLOPT_HEADER, false);
     if (isset($r['limit-response-size'])) {
         $this->max_body_length = intval($r['limit-response-size']);
     } else {
         $this->max_body_length = false;
     }
     // If streaming to a file open a file handle, and setup our curl streaming handler
     if ($r['stream']) {
         if (!WP_DEBUG) {
             $this->stream_handle = @fopen($r['filename'], 'w+');
         } else {
             $this->stream_handle = fopen($r['filename'], 'w+');
         }
         if (!$this->stream_handle) {
             return new WP_Error('http_request_failed', sprintf(__('Could not open handle for fopen() to %s'), $r['filename']));
         }
     } else {
         $this->stream_handle = false;
     }
     if (!empty($r['headers'])) {
         // cURL expects full header strings in each element
         $headers = array();
         foreach ($r['headers'] as $name => $value) {
             $headers[] = "{$name}: {$value}";
         }
         curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
     }
     if ($r['httpversion'] == '1.0') {
         curl_setopt($handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
     } else {
         curl_setopt($handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
     }
     // Cookies are not handled by the HTTP API currently. Allow for plugin authors to handle it
     // themselves... Although, it is somewhat pointless without some reference.
     do_action_ref_array('http_api_curl', array(&$handle));
     // We don't need to return the body, so don't. Just execute request and return.
     if (!$r['blocking']) {
         curl_exec($handle);
         if ($curl_error = curl_error($handle)) {
             curl_close($handle);
             return new WP_Error('http_request_failed', $curl_error);
         }
         if (in_array(curl_getinfo($handle, CURLINFO_HTTP_CODE), array(301, 302))) {
             curl_close($handle);
             return new WP_Error('http_request_failed', __('Too many redirects.'));
         }
         curl_close($handle);
         return array('headers' => array(), 'body' => '', 'response' => array('code' => false, 'message' => false), 'cookies' => array());
     }
     $theResponse = curl_exec($handle);
     $theHeaders = WP_Http::processHeaders($this->headers);
     $theBody = $this->body;
     $this->headers = '';
     $this->body = '';
     // If no response
     if (0 == strlen($theBody) && empty($theHeaders['headers'])) {
         if ($curl_error = curl_error($handle)) {
             curl_close($handle);
             return new WP_Error('http_request_failed', $curl_error);
         }
         if (in_array(curl_getinfo($handle, CURLINFO_HTTP_CODE), array(301, 302))) {
             curl_close($handle);
             return new WP_Error('http_request_failed', __('Too many redirects.'));
         }
     }
     $response = array();
     $response['code'] = curl_getinfo($handle, CURLINFO_HTTP_CODE);
     $response['message'] = get_status_header_desc($response['code']);
     curl_close($handle);
     if ($r['stream']) {
         fclose($this->stream_handle);
     }
     // See #11305 - When running under safe mode, redirection is disabled above. Handle it manually.
     if (!empty($theHeaders['headers']['location']) && 0 !== $r['_redirection']) {
         // _redirection: The requested number of redirections
         if ($r['redirection']-- > 0) {
             return wp_remote_request(WP_HTTP::make_absolute_url($theHeaders['headers']['location'], $url), $r);
         } else {
             return new WP_Error('http_request_failed', __('Too many redirects.'));
         }
     }
     if (true === $r['decompress'] && true === WP_Http_Encoding::should_decode($theHeaders['headers'])) {
         $theBody = WP_Http_Encoding::decompress($theBody);
     }
     return array('headers' => $theHeaders['headers'], 'body' => $theBody, 'response' => $response, 'cookies' => $theHeaders['cookies'], 'filename' => $r['filename']);
 }
Пример #5
0
 /**
  * Process links in content
  * @global obj $wpdb DB instance
  * @global obj $post Current post
  * @param string $content Text containing links
  * @param string (optional) $group Group to add links to (Default: none)
  * @return string Content with processed links 
  */
 protected function process_links($content, $group = null)
 {
     // Extract links
     $links = $this->get_links($content, true);
     // Do not process content without links
     if (empty($links)) {
         return $content;
     }
     // Process links
     static $protocol = array('http://', 'https://');
     static $qv_att = 'attachment_id';
     static $uri_origin = null;
     if (!is_array($uri_origin)) {
         $uri_parts = array_fill_keys(array('scheme', 'host', 'path'), '');
         $uri_origin = wp_parse_args(parse_url(strtolower(home_url())), $uri_parts);
     }
     static $uri_proto = null;
     if (empty($uri_proto)) {
         $uri_proto = (object) array('raw' => '', 'source' => '', 'parts' => '');
     }
     $uri_parts_required = array('host' => '');
     // Setup group properties
     $g_props = (object) array('enabled' => $this->options->get_bool('group_links'), 'attr' => 'group', 'base' => '', 'legacy_prefix' => 'lightbox[', 'legacy_suffix' => ']');
     if ($g_props->enabled) {
         $g_props->base = is_scalar($group) ? trim(strval($group)) : '';
     }
     // Initialize content handlers
     if (!$this->handlers instanceof SLB_Content_Handlers) {
         $this->handlers = new SLB_Content_Handlers($this);
     }
     // Iterate through and activate supported links
     foreach ($links as $link) {
         // Init vars
         $pid = 0;
         $link_new = $link;
         $uri = clone $uri_proto;
         $type = false;
         $props_extra = array();
         $key = null;
         $internal = false;
         // Parse link attributes
         $attrs = $this->util->parse_attribute_string($link_new, array('href' => ''));
         // Get URI
         $uri->raw = $attrs['href'];
         // Stop processing invalid links
         if (!$this->validate_uri($uri->raw) || $this->has_attribute($attrs, 'active', false)) {
             continue;
         }
         // Normalize URI (make absolute)
         $uri->source = WP_HTTP::make_absolute_url($uri->raw, $uri_origin['scheme'] . '://' . $uri_origin['host']);
         // URI cached?
         $key = $this->get_media_item_id($uri->source);
         // Internal URI? (e.g. attachments)
         if (!$key) {
             $uri->parts = array_merge($uri_parts_required, (array) parse_url($uri->source));
             $internal = $uri->parts['host'] === $uri_origin['host'] ? true : false;
             // Attachment?
             if ($internal && is_local_attachment($uri->source)) {
                 $pid = url_to_postid($uri->source);
                 $src = wp_get_attachment_url($pid);
                 if (!!$src) {
                     $uri->source = $src;
                     $props_extra['id'] = $pid;
                     // Check cache for attachment source URI
                     $key = $this->get_media_item_id($uri->source);
                 }
                 unset($src);
             }
         }
         // Determine content type
         if (!$key) {
             // Get handler match
             $hdl_result = $this->handlers->match($uri->source);
             if (!!$hdl_result->handler) {
                 $type = $hdl_result->handler->get_id();
                 $props_extra = $hdl_result->props;
                 // Updated source URI
                 if (isset($props_extra['uri'])) {
                     $uri->source = $props_extra['uri'];
                     unset($props_extra['uri']);
                 }
             }
             // Cache valid item
             if (!!$type) {
                 $key = $this->cache_media_item($uri, $type, $internal, $props_extra);
             }
         }
         // Stop processing invalid links
         if (!$key) {
             // Cache invalid URI
             $this->validated_uris[$uri->source] = false;
             if ($uri->raw !== $uri->source) {
                 $this->validated_uris[$uri->raw] = false;
             }
             continue;
         }
         // Activate link
         $this->set_attribute($attrs, 'active');
         $this->set_attribute($attrs, 'asset', $key);
         // Mark internal links
         if ($internal) {
             $this->set_attribute($attrs, 'internal', $pid);
         }
         // Set group (if enabled)
         if ($g_props->enabled) {
             $group = array();
             // Get preset group attribute
             $g = $this->has_attribute($attrs, $g_props->attr) ? $this->get_attribute($attrs, $g_props->attr) : '';
             if (is_string($g) && ($g = trim($g)) && !empty($g)) {
                 $group[] = $g;
             } elseif (!empty($g_props->base)) {
                 $group[] = $g_props->base;
             }
             $group = $this->util->apply_filters('get_group_id', $group);
             // Default group
             if (empty($group) || !is_array($group)) {
                 $group = $this->get_prefix();
             } else {
                 $group = implode('_', $group);
             }
             // Set group attribute
             $this->set_attribute($attrs, $g_props->attr, $group);
             unset($g);
         }
         // Filter attributes
         $attrs = $this->util->apply_filters('process_link_attributes', $attrs);
         // Update link in content
         $link_new = '<a ' . $this->util->build_attribute_string($attrs) . '>';
         $content = str_replace($link, $link_new, $content);
     }
     // Handle widget content
     if (!!$this->widget_processing && 'the_content' == current_filter()) {
         $content = $this->exclude_wrap($content);
     }
     return $content;
 }