PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/blog.old/wp-content/plugins/jetpack/class.jetpack-client.php

https://github.com/chopsuei3/oscc
PHP | 215 lines | 143 code | 40 blank | 32 comment | 36 complexity | a5252c0930742c9e9ec3d0402497559b MD5 | raw file
  1. <?php
  2. class Jetpack_Client {
  3. /**
  4. * Makes an authorized remote request using Jetpack_Signature
  5. *
  6. * @return array|WP_Error WP HTTP response on success
  7. */
  8. public static function remote_request( $args, $body = null ) {
  9. $defaults = array(
  10. 'url' => '',
  11. 'user_id' => 0,
  12. 'blog_id' => 0,
  13. 'auth_location' => JETPACK_CLIENT__AUTH_LOCATION,
  14. 'method' => 'POST',
  15. 'timeout' => 10,
  16. 'redirection' => 0,
  17. );
  18. $args = wp_parse_args( $args, $defaults );
  19. $args['blog_id'] = (int) $args['blog_id'];
  20. if ( 'header' != $args['auth_location'] ) {
  21. $args['auth_location'] = 'query_string';
  22. }
  23. $token = Jetpack_Data::get_access_token( $args['user_id'] );
  24. if ( !$token ) {
  25. return new Jetpack_Error( 'missing_token' );
  26. }
  27. $method = strtoupper( $args['method'] );
  28. $timeout = intval( $args['timeout'] );
  29. $redirection = $args['redirection'];
  30. $request = compact( 'method', 'body', 'timeout', 'redirection' );
  31. @list( $token_key, $secret ) = explode( '.', $token->secret );
  32. if ( empty( $token ) || empty( $secret ) ) {
  33. return new Jetpack_Error( 'malformed_token' );
  34. }
  35. $token_key = sprintf( '%s:%d:%d', $token_key, JETPACK__API_VERSION, $token->external_user_id );
  36. require_once dirname( __FILE__ ) . '/class.jetpack-signature.php';
  37. $time_diff = (int) Jetpack_Options::get_option( 'time_diff' );
  38. $jetpack_signature = new Jetpack_Signature( $token->secret, $time_diff );
  39. $timestamp = time() + $time_diff;
  40. $nonce = wp_generate_password( 10, false );
  41. // Kind of annoying. Maybe refactor Jetpack_Signature to handle body-hashing
  42. if ( is_null( $body ) ) {
  43. $body_hash = '';
  44. } else {
  45. if ( !is_string( $body ) ) {
  46. return new Jetpack_Error( 'invalid_body', 'Body is malformed.' );
  47. }
  48. $body_hash = jetpack_sha1_base64( $body );
  49. }
  50. $auth = array(
  51. 'token' => $token_key,
  52. 'timestamp' => $timestamp,
  53. 'nonce' => $nonce,
  54. 'body-hash' => $body_hash,
  55. );
  56. if ( false !== strpos( $args['url'], 'xmlrpc.php' ) ) {
  57. $url_args = array(
  58. 'for' => 'jetpack',
  59. 'blog_id' => $args['blog_id'],
  60. );
  61. } else {
  62. $url_args = array();
  63. }
  64. if ( 'header' != $args['auth_location'] ) {
  65. $url_args += $auth;
  66. }
  67. $url = add_query_arg( urlencode_deep( $url_args ), $args['url'] );
  68. $url = Jetpack::fix_url_for_bad_hosts( $url );
  69. $signature = $jetpack_signature->sign_request( $token_key, $timestamp, $nonce, $body_hash, $method, $url, $body, false );
  70. if ( !$signature || is_wp_error( $signature ) ) {
  71. return $signature;
  72. }
  73. // Send an Authorization header so various caches/proxies do the right thing
  74. $auth['signature'] = $signature;
  75. $auth['version'] = JETPACK__VERSION;
  76. $header_pieces = array();
  77. foreach ( $auth as $key => $value ) {
  78. $header_pieces[] = sprintf( '%s="%s"', $key, $value );
  79. }
  80. $request['headers'] = array(
  81. 'Authorization' => "X_JETPACK " . join( ' ', $header_pieces ),
  82. );
  83. if ( 'header' != $args['auth_location'] ) {
  84. $url = add_query_arg( 'signature', urlencode( $signature ), $url );
  85. }
  86. return Jetpack_Client::_wp_remote_request( $url, $request );
  87. }
  88. /**
  89. * Wrapper for wp_remote_request(). Turns off SSL verification for certain SSL errors.
  90. * This is lame, but many, many, many hosts have misconfigured SSL.
  91. *
  92. * When Jetpack is registered, the jetpack_fallback_no_verify_ssl_certs option is set to the current time if:
  93. * 1. a certificate error is found AND
  94. * 2. not verifying the certificate works around the problem.
  95. *
  96. * The option is checked on each request.
  97. *
  98. * @internal
  99. * @todo: Better fallbacks (bundled certs?), feedback, UI, ....
  100. * @see Jetpack::fix_url_for_bad_hosts()
  101. *
  102. * @return array|WP_Error WP HTTP response on success
  103. */
  104. public static function _wp_remote_request( $url, $args, $set_fallback = false ) {
  105. $fallback = Jetpack_Options::get_option( 'fallback_no_verify_ssl_certs' );
  106. if ( false === $fallback ) {
  107. Jetpack_Options::update_option( 'fallback_no_verify_ssl_certs', 0 );
  108. }
  109. if ( (int) $fallback ) {
  110. // We're flagged to fallback
  111. $args['sslverify'] = false;
  112. }
  113. $response = wp_remote_request( $url, $args );
  114. if (
  115. !$set_fallback // We're not allowed to set the flag on this request, so whatever happens happens
  116. ||
  117. isset( $args['sslverify'] ) && !$args['sslverify'] // No verification - no point in doing it again
  118. ||
  119. !is_wp_error( $response ) // Let it ride
  120. ) {
  121. Jetpack_Client::set_time_diff( $response, $set_fallback );
  122. return $response;
  123. }
  124. // At this point, we're not flagged to fallback and we are allowed to set the flag on this request.
  125. $message = $response->get_error_message();
  126. // Is it an SSL Certificate verification error?
  127. if (
  128. false === strpos( $message, '14090086' ) // OpenSSL SSL3 certificate error
  129. &&
  130. false === strpos( $message, '1407E086' ) // OpenSSL SSL2 certificate error
  131. &&
  132. false === strpos( $message, 'error setting certificate verify locations' ) // cURL CA bundle not found
  133. &&
  134. false === strpos( $message, 'Peer certificate cannot be authenticated with' ) // cURL CURLE_SSL_CACERT: CA bundle found, but not helpful
  135. // different versions of curl have different error messages
  136. // this string should catch them all
  137. &&
  138. false === strpos( $message, 'Problem with the SSL CA cert' ) // cURL CURLE_SSL_CACERT_BADFILE: probably access rights
  139. ) {
  140. // No, it is not.
  141. return $response;
  142. }
  143. // Redo the request without SSL certificate verification.
  144. $args['sslverify'] = false;
  145. $response = wp_remote_request( $url, $args );
  146. if ( !is_wp_error( $response ) ) {
  147. // The request went through this time, flag for future fallbacks
  148. Jetpack_Options::update_option( 'fallback_no_verify_ssl_certs', time() );
  149. Jetpack_Client::set_time_diff( $response, $set_fallback );
  150. }
  151. return $response;
  152. }
  153. public static function set_time_diff( &$response, $force_set = false ) {
  154. $code = wp_remote_retrieve_response_code( $response );
  155. // Only trust the Date header on some responses
  156. if ( 200 != $code && 304 != $code && 400 != $code && 401 != $code ) {
  157. return;
  158. }
  159. if ( !$date = wp_remote_retrieve_header( $response, 'date' ) ) {
  160. return;
  161. }
  162. if ( 0 >= $time = (int) strtotime( $date ) ) {
  163. return;
  164. }
  165. $time_diff = $time - time();
  166. if ( $force_set ) { // during register
  167. Jetpack_Options::update_option( 'time_diff', $time_diff );
  168. } else { // otherwise
  169. $old_diff = Jetpack_Options::get_option( 'time_diff' );
  170. if ( false === $old_diff || abs( $time_diff - (int) $old_diff ) > 10 ) {
  171. Jetpack_Options::update_option( 'time_diff', $time_diff );
  172. }
  173. }
  174. }
  175. }