PageRenderTime 92ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/http.php

https://bitbucket.org/space87/wordpressstart
PHP | 301 lines | 98 code | 36 blank | 167 comment | 30 complexity | e738cf81040d50498fc9c2339a4692f1 MD5 | raw file
  1. <?php
  2. /**
  3. * Simple and uniform HTTP request API.
  4. *
  5. * Will eventually replace and standardize the WordPress HTTP requests made.
  6. *
  7. * @link http://trac.wordpress.org/ticket/4779 HTTP API Proposal
  8. *
  9. * @package WordPress
  10. * @subpackage HTTP
  11. * @since 2.7.0
  12. */
  13. /**
  14. * Returns the initialized WP_Http Object
  15. *
  16. * @since 2.7.0
  17. * @access private
  18. *
  19. * @return WP_Http HTTP Transport object.
  20. */
  21. function &_wp_http_get_object() {
  22. static $http;
  23. if ( is_null($http) )
  24. $http = new WP_Http();
  25. return $http;
  26. }
  27. /**
  28. * Retrieve the raw response from the HTTP request.
  29. *
  30. * The array structure is a little complex.
  31. *
  32. * <code>
  33. * $res = array( 'headers' => array(), 'response' => array('code' => int, 'message' => string) );
  34. * </code>
  35. *
  36. * All of the headers in $res['headers'] are with the name as the key and the
  37. * value as the value. So to get the User-Agent, you would do the following.
  38. *
  39. * <code>
  40. * $user_agent = $res['headers']['user-agent'];
  41. * </code>
  42. *
  43. * The body is the raw response content and can be retrieved from $res['body'].
  44. *
  45. * This function is called first to make the request and there are other API
  46. * functions to abstract out the above convoluted setup.
  47. *
  48. * @since 2.7.0
  49. *
  50. * @param string $url Site URL to retrieve.
  51. * @param array $args Optional. Override the defaults.
  52. * @return WP_Error|array The response or WP_Error on failure.
  53. */
  54. function wp_remote_request($url, $args = array()) {
  55. $objFetchSite = _wp_http_get_object();
  56. return $objFetchSite->request($url, $args);
  57. }
  58. /**
  59. * Retrieve the raw response from the HTTP request using the GET method.
  60. *
  61. * @see wp_remote_request() For more information on the response array format.
  62. *
  63. * @since 2.7.0
  64. *
  65. * @param string $url Site URL to retrieve.
  66. * @param array $args Optional. Override the defaults.
  67. * @return WP_Error|array The response or WP_Error on failure.
  68. */
  69. function wp_remote_get($url, $args = array()) {
  70. $objFetchSite = _wp_http_get_object();
  71. return $objFetchSite->get($url, $args);
  72. }
  73. /**
  74. * Retrieve the raw response from the HTTP request using the POST method.
  75. *
  76. * @see wp_remote_request() For more information on the response array format.
  77. *
  78. * @since 2.7.0
  79. *
  80. * @param string $url Site URL to retrieve.
  81. * @param array $args Optional. Override the defaults.
  82. * @return WP_Error|array The response or WP_Error on failure.
  83. */
  84. function wp_remote_post($url, $args = array()) {
  85. $objFetchSite = _wp_http_get_object();
  86. return $objFetchSite->post($url, $args);
  87. }
  88. /**
  89. * Retrieve the raw response from the HTTP request using the HEAD method.
  90. *
  91. * @see wp_remote_request() For more information on the response array format.
  92. *
  93. * @since 2.7.0
  94. *
  95. * @param string $url Site URL to retrieve.
  96. * @param array $args Optional. Override the defaults.
  97. * @return WP_Error|array The response or WP_Error on failure.
  98. */
  99. function wp_remote_head($url, $args = array()) {
  100. $objFetchSite = _wp_http_get_object();
  101. return $objFetchSite->head($url, $args);
  102. }
  103. /**
  104. * Retrieve only the headers from the raw response.
  105. *
  106. * @since 2.7.0
  107. *
  108. * @param array $response HTTP response.
  109. * @return array The headers of the response. Empty array if incorrect parameter given.
  110. */
  111. function wp_remote_retrieve_headers(&$response) {
  112. if ( is_wp_error($response) || ! isset($response['headers']) || ! is_array($response['headers']))
  113. return array();
  114. return $response['headers'];
  115. }
  116. /**
  117. * Retrieve a single header by name from the raw response.
  118. *
  119. * @since 2.7.0
  120. *
  121. * @param array $response
  122. * @param string $header Header name to retrieve value from.
  123. * @return string The header value. Empty string on if incorrect parameter given, or if the header doesn't exist.
  124. */
  125. function wp_remote_retrieve_header(&$response, $header) {
  126. if ( is_wp_error($response) || ! isset($response['headers']) || ! is_array($response['headers']))
  127. return '';
  128. if ( array_key_exists($header, $response['headers']) )
  129. return $response['headers'][$header];
  130. return '';
  131. }
  132. /**
  133. * Retrieve only the response code from the raw response.
  134. *
  135. * Will return an empty array if incorrect parameter value is given.
  136. *
  137. * @since 2.7.0
  138. *
  139. * @param array $response HTTP response.
  140. * @return string the response code. Empty string on incorrect parameter given.
  141. */
  142. function wp_remote_retrieve_response_code(&$response) {
  143. if ( is_wp_error($response) || ! isset($response['response']) || ! is_array($response['response']))
  144. return '';
  145. return $response['response']['code'];
  146. }
  147. /**
  148. * Retrieve only the response message from the raw response.
  149. *
  150. * Will return an empty array if incorrect parameter value is given.
  151. *
  152. * @since 2.7.0
  153. *
  154. * @param array $response HTTP response.
  155. * @return string The response message. Empty string on incorrect parameter given.
  156. */
  157. function wp_remote_retrieve_response_message(&$response) {
  158. if ( is_wp_error($response) || ! isset($response['response']) || ! is_array($response['response']))
  159. return '';
  160. return $response['response']['message'];
  161. }
  162. /**
  163. * Retrieve only the body from the raw response.
  164. *
  165. * @since 2.7.0
  166. *
  167. * @param array $response HTTP response.
  168. * @return string The body of the response. Empty string if no body or incorrect parameter given.
  169. */
  170. function wp_remote_retrieve_body(&$response) {
  171. if ( is_wp_error($response) || ! isset($response['body']) )
  172. return '';
  173. return $response['body'];
  174. }
  175. /**
  176. * Determines if there is an HTTP Transport that can process this request.
  177. *
  178. * @since 3.2.0
  179. *
  180. * @param array $capabilities Array of capabilities to test or a wp_remote_request() $args array.
  181. * @param string $url Optional. If given, will check if the URL requires SSL and adds that requirement to the capabilities array.
  182. *
  183. * @return bool
  184. */
  185. function wp_http_supports( $capabilities = array(), $url = null ) {
  186. $objFetchSite = _wp_http_get_object();
  187. $capabilities = wp_parse_args( $capabilities );
  188. $count = count( $capabilities );
  189. // If we have a numeric $capabilities array, spoof a wp_remote_request() associative $args array
  190. if ( $count && count( array_filter( array_keys( $capabilities ), 'is_numeric' ) ) == $count ) {
  191. $capabilities = array_combine( array_values( $capabilities ), array_fill( 0, $count, true ) );
  192. }
  193. if ( $url && !isset( $capabilities['ssl'] ) ) {
  194. $scheme = parse_url( $url, PHP_URL_SCHEME );
  195. if ( 'https' == $scheme || 'ssl' == $scheme ) {
  196. $capabilities['ssl'] = true;
  197. }
  198. }
  199. return (bool) $objFetchSite->_get_first_available_transport( $capabilities );
  200. }
  201. /**
  202. * Get the HTTP Origin of the current request.
  203. *
  204. * @since 3.4.0
  205. *
  206. * @return string URL of the origin. Empty string if no origin.
  207. */
  208. function get_http_origin() {
  209. $origin = '';
  210. if ( ! empty ( $_SERVER[ 'HTTP_ORIGIN' ] ) )
  211. $origin = $_SERVER[ 'HTTP_ORIGIN' ];
  212. return apply_filters( 'http_origin', $origin );
  213. }
  214. /**
  215. * Retrieve list of allowed http origins.
  216. *
  217. * @since 3.4.0
  218. *
  219. * @return array Array of origin URLs.
  220. */
  221. function get_allowed_http_origins() {
  222. $admin_origin = parse_url( admin_url() );
  223. $home_origin = parse_url( home_url() );
  224. // @todo preserve port?
  225. $allowed_origins = array_unique( array(
  226. 'http://' . $admin_origin[ 'host' ],
  227. 'https://' . $admin_origin[ 'host' ],
  228. 'http://' . $home_origin[ 'host' ],
  229. 'https://' . $home_origin[ 'host' ],
  230. ) );
  231. return apply_filters( 'allowed_http_origins' , $allowed_origins );
  232. }
  233. /**
  234. * Determines if the http origin is an authorized one.
  235. *
  236. * @since 3.4.0
  237. *
  238. * @param string Origin URL. If not provided, the value of get_http_origin() is used.
  239. * @return bool True if the origin is allowed. False otherwise.
  240. */
  241. function is_allowed_http_origin( $origin = null ) {
  242. $origin_arg = $origin;
  243. if ( null === $origin )
  244. $origin = get_http_origin();
  245. if ( $origin && ! in_array( $origin, get_allowed_http_origins() ) )
  246. $origin = '';
  247. return apply_filters( 'allowed_http_origin', $origin, $origin_arg );
  248. }
  249. /**
  250. * Send Access-Control-Allow-Origin and related headers if the current request
  251. * is from an allowed origin.
  252. *
  253. * @since 3.4.0
  254. *
  255. * @return bool|string Returns the origin URL if headers are sent. Returns false
  256. * if headers are not sent.
  257. */
  258. function send_origin_headers() {
  259. $origin = get_http_origin();
  260. if ( ! is_allowed_http_origin( $origin ) )
  261. return false;
  262. @header( 'Access-Control-Allow-Origin: ' . $origin );
  263. @header( 'Access-Control-Allow-Credentials: true' );
  264. return $origin;
  265. }