Jetpack_Client

The Jetpack by WordPress.com Jetpack Client class.

Defined (1)

The class is defined in the following location(s).

/class.jetpack-client.php  
  1. class Jetpack_Client { 
  2. const WPCOM_JSON_API_HOST = 'public-api.wordpress.com'; 
  3. const WPCOM_JSON_API_VERSION = '1.1'; 
  4.  
  5. /** 
  6. * Makes an authorized remote request using Jetpack_Signature 
  7. * @return array|WP_Error WP HTTP response on success 
  8. */ 
  9. public static function remote_request( $args, $body = null ) { 
  10. $defaults = array( 
  11. 'url' => '',  
  12. 'user_id' => 0,  
  13. 'blog_id' => 0,  
  14. 'auth_location' => JETPACK_CLIENT__AUTH_LOCATION,  
  15. 'method' => 'POST',  
  16. 'timeout' => 10,  
  17. 'redirection' => 0,  
  18. ); 
  19.  
  20. $args = wp_parse_args( $args, $defaults ); 
  21.  
  22. $args['blog_id'] = (int) $args['blog_id']; 
  23.  
  24. if ( 'header' != $args['auth_location'] ) { 
  25. $args['auth_location'] = 'query_string'; 
  26.  
  27. $token = Jetpack_Data::get_access_token( $args['user_id'] ); 
  28. if ( !$token ) { 
  29. return new Jetpack_Error( 'missing_token' ); 
  30.  
  31. $method = strtoupper( $args['method'] ); 
  32.  
  33. $timeout = intval( $args['timeout'] ); 
  34.  
  35. $redirection = $args['redirection']; 
  36.  
  37. $request = compact( 'method', 'body', 'timeout', 'redirection' ); 
  38.  
  39. @list( $token_key, $secret ) = explode( '.', $token->secret ); 
  40. if ( empty( $token ) || empty( $secret ) ) { 
  41. return new Jetpack_Error( 'malformed_token' ); 
  42.  
  43. $token_key = sprintf( '%s:%d:%d', $token_key, JETPACK__API_VERSION, $token->external_user_id ); 
  44.  
  45. require_once JETPACK__PLUGIN_DIR . 'class.jetpack-signature.php'; 
  46.  
  47. $time_diff = (int) Jetpack_Options::get_option( 'time_diff' ); 
  48. $jetpack_signature = new Jetpack_Signature( $token->secret, $time_diff ); 
  49.  
  50. $timestamp = time() + $time_diff; 
  51.  
  52. if( function_exists( 'wp_generate_password' ) ) { 
  53. $nonce = wp_generate_password( 10, false ); 
  54. } else { 
  55. $nonce = substr( sha1( rand( 0, 1000000 ) ), 0, 10); 
  56.  
  57. // Kind of annoying. Maybe refactor Jetpack_Signature to handle body-hashing 
  58. if ( is_null( $body ) ) { 
  59. $body_hash = ''; 
  60. } else { 
  61. if ( !is_string( $body ) ) { 
  62. return new Jetpack_Error( 'invalid_body', 'Body is malformed.' ); 
  63. $body_hash = jetpack_sha1_base64( $body ); 
  64.  
  65. $auth = array( 
  66. 'token' => $token_key,  
  67. 'timestamp' => $timestamp,  
  68. 'nonce' => $nonce,  
  69. 'body-hash' => $body_hash,  
  70. ); 
  71.  
  72. if ( false !== strpos( $args['url'], 'xmlrpc.php' ) ) { 
  73. $url_args = array( 
  74. 'for' => 'jetpack',  
  75. 'wpcom_blog_id' => Jetpack_Options::get_option( 'id' ),  
  76. ); 
  77. } else { 
  78. $url_args = array(); 
  79.  
  80. if ( 'header' != $args['auth_location'] ) { 
  81. $url_args += $auth; 
  82.  
  83. $url = add_query_arg( urlencode_deep( $url_args ), $args['url'] ); 
  84. $url = Jetpack::fix_url_for_bad_hosts( $url ); 
  85.  
  86. $signature = $jetpack_signature->sign_request( $token_key, $timestamp, $nonce, $body_hash, $method, $url, $body, false ); 
  87.  
  88. if ( !$signature || is_wp_error( $signature ) ) { 
  89. return $signature; 
  90.  
  91. // Send an Authorization header so various caches/proxies do the right thing 
  92. $auth['signature'] = $signature; 
  93. $auth['version'] = JETPACK__VERSION; 
  94. $header_pieces = array(); 
  95. foreach ( $auth as $key => $value ) { 
  96. $header_pieces[] = sprintf( '%s="%s"', $key, $value ); 
  97. $request['headers'] = array( 
  98. 'Authorization' => "X_JETPACK " . join( ' ', $header_pieces ),  
  99. ); 
  100.  
  101. if ( 'header' != $args['auth_location'] ) { 
  102. $url = add_query_arg( 'signature', urlencode( $signature ), $url ); 
  103.  
  104. return Jetpack_Client::_wp_remote_request( $url, $request ); 
  105.  
  106. /** 
  107. * Wrapper for wp_remote_request(). Turns off SSL verification for certain SSL errors. 
  108. * This is lame, but many, many, many hosts have misconfigured SSL. 
  109. * When Jetpack is registered, the jetpack_fallback_no_verify_ssl_certs option is set to the current time if: 
  110. * 1. a certificate error is found AND 
  111. * 2. not verifying the certificate works around the problem. 
  112. * The option is checked on each request. 
  113. * @internal 
  114. * @see Jetpack::fix_url_for_bad_hosts() 
  115. * @return array|WP_Error WP HTTP response on success 
  116. */ 
  117. public static function _wp_remote_request( $url, $args, $set_fallback = false ) { 
  118. /** 
  119. * SSL verification (`sslverify`) for the JetpackClient remote request 
  120. * defaults to off, use this filter to force it on. 
  121. * Return `true` to ENABLE SSL verification, return `false` 
  122. * to DISABLE SSL verification. 
  123. * @since 3.6.0 
  124. * @param bool Whether to force `sslverify` or not. 
  125. */ 
  126. if ( apply_filters( 'jetpack_client_verify_ssl_certs', false ) ) { 
  127. return wp_remote_request( $url, $args ); 
  128.  
  129. $fallback = Jetpack_Options::get_option( 'fallback_no_verify_ssl_certs' ); 
  130. if ( false === $fallback ) { 
  131. Jetpack_Options::update_option( 'fallback_no_verify_ssl_certs', 0 ); 
  132.  
  133. if ( (int) $fallback ) { 
  134. // We're flagged to fallback 
  135. $args['sslverify'] = false; 
  136.  
  137. $response = wp_remote_request( $url, $args ); 
  138.  
  139. if ( 
  140. !$set_fallback // We're not allowed to set the flag on this request, so whatever happens happens 
  141. || 
  142. isset( $args['sslverify'] ) && !$args['sslverify'] // No verification - no point in doing it again 
  143. || 
  144. !is_wp_error( $response ) // Let it ride 
  145. ) { 
  146. Jetpack_Client::set_time_diff( $response, $set_fallback ); 
  147. return $response; 
  148.  
  149. // At this point, we're not flagged to fallback and we are allowed to set the flag on this request. 
  150.  
  151. $message = $response->get_error_message(); 
  152.  
  153. // Is it an SSL Certificate verification error? 
  154. if ( 
  155. false === strpos( $message, '14090086' ) // OpenSSL SSL3 certificate error 
  156. && 
  157. false === strpos( $message, '1407E086' ) // OpenSSL SSL2 certificate error 
  158. && 
  159. false === strpos( $message, 'error setting certificate verify locations' ) // cURL CA bundle not found 
  160. && 
  161. false === strpos( $message, 'Peer certificate cannot be authenticated with' ) // cURL CURLE_SSL_CACERT: CA bundle found, but not helpful 
  162. // different versions of curl have different error messages 
  163. // this string should catch them all 
  164. && 
  165. false === strpos( $message, 'Problem with the SSL CA cert' ) // cURL CURLE_SSL_CACERT_BADFILE: probably access rights 
  166. ) { 
  167. // No, it is not. 
  168. return $response; 
  169.  
  170. // Redo the request without SSL certificate verification. 
  171. $args['sslverify'] = false; 
  172. $response = wp_remote_request( $url, $args ); 
  173.  
  174. if ( !is_wp_error( $response ) ) { 
  175. // The request went through this time, flag for future fallbacks 
  176. Jetpack_Options::update_option( 'fallback_no_verify_ssl_certs', time() ); 
  177. Jetpack_Client::set_time_diff( $response, $set_fallback ); 
  178.  
  179. return $response; 
  180.  
  181. public static function set_time_diff( &$response, $force_set = false ) { 
  182. $code = wp_remote_retrieve_response_code( $response ); 
  183.  
  184. // Only trust the Date header on some responses 
  185. if ( 200 != $code && 304 != $code && 400 != $code && 401 != $code ) { 
  186. return; 
  187.  
  188. if ( !$date = wp_remote_retrieve_header( $response, 'date' ) ) { 
  189. return; 
  190.  
  191. if ( 0 >= $time = (int) strtotime( $date ) ) { 
  192. return; 
  193.  
  194. $time_diff = $time - time(); 
  195.  
  196. if ( $force_set ) { // during register 
  197. Jetpack_Options::update_option( 'time_diff', $time_diff ); 
  198. } else { // otherwise 
  199. $old_diff = Jetpack_Options::get_option( 'time_diff' ); 
  200. if ( false === $old_diff || abs( $time_diff - (int) $old_diff ) > 10 ) { 
  201. Jetpack_Options::update_option( 'time_diff', $time_diff ); 
  202.  
  203. /** 
  204. * Query the WordPress.com REST API using the blog token 
  205. * @param string $path 
  206. * @param string $version 
  207. * @param array $args 
  208. * @param string $body 
  209. * @return array|WP_Error $response Data. 
  210. */ 
  211. static function wpcom_json_api_request_as_blog( $path, $version = self::WPCOM_JSON_API_VERSION, $args = array(), $body = null ) { 
  212. $filtered_args = array_intersect_key( $args, array( 
  213. 'method' => 'string',  
  214. 'timeout' => 'int',  
  215. 'redirection' => 'int',  
  216. ) ); 
  217.  
  218. /** 
  219. * Determines whether Jetpack can send outbound https requests to the WPCOM api. 
  220. * @since 3.6.0 
  221. * @param bool $proto Defaults to true. 
  222. */ 
  223. $proto = apply_filters( 'jetpack_can_make_outbound_https', true ) ? 'https' : 'http'; 
  224.  
  225. // unprecedingslashit 
  226. $_path = preg_replace( '/^\//', '', $path ); 
  227.  
  228. // Use GET by default whereas `remote_request` uses POST 
  229. if ( isset( $filtered_args['method'] ) && strtoupper( $filtered_args['method'] === 'POST' ) ) { 
  230. $request_method = 'POST'; 
  231. } else { 
  232. $request_method = 'GET'; 
  233.  
  234. $validated_args = array_merge( $filtered_args, array( 
  235. 'url' => sprintf( '%s://%s/rest/v%s/%s', $proto, self::WPCOM_JSON_API_HOST, $version, $_path ),  
  236. 'blog_id' => (int) Jetpack_Options::get_option( 'id' ),  
  237. 'method' => $request_method,  
  238. ) ); 
  239.  
  240. return Jetpack_Client::remote_request( $validated_args, $body ); 
  241.