changeset 16 | a86126ab1dd4 |
parent 13 | d255fe9cd479 |
child 18 | be944660c56a |
15:3d4e9c994f10 | 16:a86126ab1dd4 |
---|---|
13 * Returns the initialized WP_Http Object |
13 * Returns the initialized WP_Http Object |
14 * |
14 * |
15 * @since 2.7.0 |
15 * @since 2.7.0 |
16 * @access private |
16 * @access private |
17 * |
17 * |
18 * @staticvar WP_Http $http |
|
19 * |
|
20 * @return WP_Http HTTP Transport object. |
18 * @return WP_Http HTTP Transport object. |
21 */ |
19 */ |
22 function _wp_http_get_object() { |
20 function _wp_http_get_object() { |
23 static $http = null; |
21 static $http = null; |
24 |
22 |
37 * @since 3.6.0 |
35 * @since 3.6.0 |
38 * |
36 * |
39 * @see wp_remote_request() For more information on the response array format. |
37 * @see wp_remote_request() For more information on the response array format. |
40 * @see WP_Http::request() For default arguments information. |
38 * @see WP_Http::request() For default arguments information. |
41 * |
39 * |
42 * @param string $url Site URL to retrieve. |
40 * @param string $url URL to retrieve. |
43 * @param array $args Optional. Request arguments. Default empty array. |
41 * @param array $args Optional. Request arguments. Default empty array. |
44 * @return WP_Error|array The response or WP_Error on failure. |
42 * @return array|WP_Error The response or WP_Error on failure. |
45 */ |
43 */ |
46 function wp_safe_remote_request( $url, $args = array() ) { |
44 function wp_safe_remote_request( $url, $args = array() ) { |
47 $args['reject_unsafe_urls'] = true; |
45 $args['reject_unsafe_urls'] = true; |
48 $http = _wp_http_get_object(); |
46 $http = _wp_http_get_object(); |
49 return $http->request( $url, $args ); |
47 return $http->request( $url, $args ); |
58 * @since 3.6.0 |
56 * @since 3.6.0 |
59 * |
57 * |
60 * @see wp_remote_request() For more information on the response array format. |
58 * @see wp_remote_request() For more information on the response array format. |
61 * @see WP_Http::request() For default arguments information. |
59 * @see WP_Http::request() For default arguments information. |
62 * |
60 * |
63 * @param string $url Site URL to retrieve. |
61 * @param string $url URL to retrieve. |
64 * @param array $args Optional. Request arguments. Default empty array. |
62 * @param array $args Optional. Request arguments. Default empty array. |
65 * @return WP_Error|array The response or WP_Error on failure. |
63 * @return array|WP_Error The response or WP_Error on failure. |
66 */ |
64 */ |
67 function wp_safe_remote_get( $url, $args = array() ) { |
65 function wp_safe_remote_get( $url, $args = array() ) { |
68 $args['reject_unsafe_urls'] = true; |
66 $args['reject_unsafe_urls'] = true; |
69 $http = _wp_http_get_object(); |
67 $http = _wp_http_get_object(); |
70 return $http->get( $url, $args ); |
68 return $http->get( $url, $args ); |
79 * @since 3.6.0 |
77 * @since 3.6.0 |
80 * |
78 * |
81 * @see wp_remote_request() For more information on the response array format. |
79 * @see wp_remote_request() For more information on the response array format. |
82 * @see WP_Http::request() For default arguments information. |
80 * @see WP_Http::request() For default arguments information. |
83 * |
81 * |
84 * @param string $url Site URL to retrieve. |
82 * @param string $url URL to retrieve. |
85 * @param array $args Optional. Request arguments. Default empty array. |
83 * @param array $args Optional. Request arguments. Default empty array. |
86 * @return WP_Error|array The response or WP_Error on failure. |
84 * @return array|WP_Error The response or WP_Error on failure. |
87 */ |
85 */ |
88 function wp_safe_remote_post( $url, $args = array() ) { |
86 function wp_safe_remote_post( $url, $args = array() ) { |
89 $args['reject_unsafe_urls'] = true; |
87 $args['reject_unsafe_urls'] = true; |
90 $http = _wp_http_get_object(); |
88 $http = _wp_http_get_object(); |
91 return $http->post( $url, $args ); |
89 return $http->post( $url, $args ); |
100 * @since 3.6.0 |
98 * @since 3.6.0 |
101 * |
99 * |
102 * @see wp_remote_request() For more information on the response array format. |
100 * @see wp_remote_request() For more information on the response array format. |
103 * @see WP_Http::request() For default arguments information. |
101 * @see WP_Http::request() For default arguments information. |
104 * |
102 * |
105 * @param string $url Site URL to retrieve. |
103 * @param string $url URL to retrieve. |
106 * @param array $args Optional. Request arguments. Default empty array. |
104 * @param array $args Optional. Request arguments. Default empty array. |
107 * @return WP_Error|array The response or WP_Error on failure. |
105 * @return array|WP_Error The response or WP_Error on failure. |
108 */ |
106 */ |
109 function wp_safe_remote_head( $url, $args = array() ) { |
107 function wp_safe_remote_head( $url, $args = array() ) { |
110 $args['reject_unsafe_urls'] = true; |
108 $args['reject_unsafe_urls'] = true; |
111 $http = _wp_http_get_object(); |
109 $http = _wp_http_get_object(); |
112 return $http->head( $url, $args ); |
110 return $http->head( $url, $args ); |
113 } |
111 } |
114 |
112 |
115 /** |
113 /** |
116 * Retrieve the raw response from the HTTP request. |
114 * Performs an HTTP request and returns its response. |
117 * |
115 * |
118 * The array structure is a little complex: |
116 * There are other API functions available which abstract away the HTTP method: |
119 * |
117 * |
120 * $res = array( |
|
121 * 'headers' => array(), |
|
122 * 'response' => array( |
|
123 * 'code' => int, |
|
124 * 'message' => string |
|
125 * ) |
|
126 * ); |
|
127 * |
|
128 * All of the headers in $res['headers'] are with the name as the key and the |
|
129 * value as the value. So to get the User-Agent, you would do the following. |
|
130 * |
|
131 * $user_agent = $res['headers']['user-agent']; |
|
132 * |
|
133 * The body is the raw response content and can be retrieved from $res['body']. |
|
134 * |
|
135 * This function is called first to make the request and there are other API |
|
136 * functions to abstract out the above convoluted setup. |
|
137 * |
|
138 * Request method defaults for helper functions: |
|
139 * - Default 'GET' for wp_remote_get() |
118 * - Default 'GET' for wp_remote_get() |
140 * - Default 'POST' for wp_remote_post() |
119 * - Default 'POST' for wp_remote_post() |
141 * - Default 'HEAD' for wp_remote_head() |
120 * - Default 'HEAD' for wp_remote_head() |
142 * |
121 * |
143 * @since 2.7.0 |
122 * @since 2.7.0 |
144 * |
123 * |
145 * @see WP_Http::request() For additional information on default arguments. |
124 * @see WP_Http::request() For information on default arguments. |
146 * |
125 * |
147 * @param string $url Site URL to retrieve. |
126 * @param string $url URL to retrieve. |
148 * @param array $args Optional. Request arguments. Default empty array. |
127 * @param array $args Optional. Request arguments. Default empty array. |
149 * @return WP_Error|array The response or WP_Error on failure. |
128 * @return array|WP_Error { |
129 * The response array or a WP_Error on failure. |
|
130 * |
|
131 * @type string[] $headers Array of response headers keyed by their name. |
|
132 * @type string $body Response body. |
|
133 * @type array $response { |
|
134 * Data about the HTTP response. |
|
135 * |
|
136 * @type int|false $code HTTP response code. |
|
137 * @type string|false $message HTTP response message. |
|
138 * } |
|
139 * @type WP_HTTP_Cookie[] $cookies Array of response cookies. |
|
140 * @type WP_HTTP_Requests_Response|null $http_response Raw HTTP response object. |
|
141 * } |
|
150 */ |
142 */ |
151 function wp_remote_request( $url, $args = array() ) { |
143 function wp_remote_request( $url, $args = array() ) { |
152 $http = _wp_http_get_object(); |
144 $http = _wp_http_get_object(); |
153 return $http->request( $url, $args ); |
145 return $http->request( $url, $args ); |
154 } |
146 } |
155 |
147 |
156 /** |
148 /** |
157 * Retrieve the raw response from the HTTP request using the GET method. |
149 * Performs an HTTP request using the GET method and returns its response. |
158 * |
150 * |
159 * @since 2.7.0 |
151 * @since 2.7.0 |
160 * |
152 * |
161 * @see wp_remote_request() For more information on the response array format. |
153 * @see wp_remote_request() For more information on the response array format. |
162 * @see WP_Http::request() For default arguments information. |
154 * @see WP_Http::request() For default arguments information. |
163 * |
155 * |
164 * @param string $url Site URL to retrieve. |
156 * @param string $url URL to retrieve. |
165 * @param array $args Optional. Request arguments. Default empty array. |
157 * @param array $args Optional. Request arguments. Default empty array. |
166 * @return WP_Error|array The response or WP_Error on failure. |
158 * @return array|WP_Error The response or WP_Error on failure. |
167 */ |
159 */ |
168 function wp_remote_get( $url, $args = array() ) { |
160 function wp_remote_get( $url, $args = array() ) { |
169 $http = _wp_http_get_object(); |
161 $http = _wp_http_get_object(); |
170 return $http->get( $url, $args ); |
162 return $http->get( $url, $args ); |
171 } |
163 } |
172 |
164 |
173 /** |
165 /** |
174 * Retrieve the raw response from the HTTP request using the POST method. |
166 * Performs an HTTP request using the POST method and returns its response. |
175 * |
167 * |
176 * @since 2.7.0 |
168 * @since 2.7.0 |
177 * |
169 * |
178 * @see wp_remote_request() For more information on the response array format. |
170 * @see wp_remote_request() For more information on the response array format. |
179 * @see WP_Http::request() For default arguments information. |
171 * @see WP_Http::request() For default arguments information. |
180 * |
172 * |
181 * @param string $url Site URL to retrieve. |
173 * @param string $url URL to retrieve. |
182 * @param array $args Optional. Request arguments. Default empty array. |
174 * @param array $args Optional. Request arguments. Default empty array. |
183 * @return WP_Error|array The response or WP_Error on failure. |
175 * @return array|WP_Error The response or WP_Error on failure. |
184 */ |
176 */ |
185 function wp_remote_post( $url, $args = array() ) { |
177 function wp_remote_post( $url, $args = array() ) { |
186 $http = _wp_http_get_object(); |
178 $http = _wp_http_get_object(); |
187 return $http->post( $url, $args ); |
179 return $http->post( $url, $args ); |
188 } |
180 } |
189 |
181 |
190 /** |
182 /** |
191 * Retrieve the raw response from the HTTP request using the HEAD method. |
183 * Performs an HTTP request using the HEAD method and returns its response. |
192 * |
184 * |
193 * @since 2.7.0 |
185 * @since 2.7.0 |
194 * |
186 * |
195 * @see wp_remote_request() For more information on the response array format. |
187 * @see wp_remote_request() For more information on the response array format. |
196 * @see WP_Http::request() For default arguments information. |
188 * @see WP_Http::request() For default arguments information. |
197 * |
189 * |
198 * @param string $url Site URL to retrieve. |
190 * @param string $url URL to retrieve. |
199 * @param array $args Optional. Request arguments. Default empty array. |
191 * @param array $args Optional. Request arguments. Default empty array. |
200 * @return WP_Error|array The response or WP_Error on failure. |
192 * @return array|WP_Error The response or WP_Error on failure. |
201 */ |
193 */ |
202 function wp_remote_head( $url, $args = array() ) { |
194 function wp_remote_head( $url, $args = array() ) { |
203 $http = _wp_http_get_object(); |
195 $http = _wp_http_get_object(); |
204 return $http->head( $url, $args ); |
196 return $http->head( $url, $args ); |
205 } |
197 } |
210 * @since 2.7.0 |
202 * @since 2.7.0 |
211 * @since 4.6.0 Return value changed from an array to an Requests_Utility_CaseInsensitiveDictionary instance. |
203 * @since 4.6.0 Return value changed from an array to an Requests_Utility_CaseInsensitiveDictionary instance. |
212 * |
204 * |
213 * @see \Requests_Utility_CaseInsensitiveDictionary |
205 * @see \Requests_Utility_CaseInsensitiveDictionary |
214 * |
206 * |
215 * @param array $response HTTP response. |
207 * @param array|WP_Error $response HTTP response. |
216 * @return array|\Requests_Utility_CaseInsensitiveDictionary The headers of the response. Empty array if incorrect parameter given. |
208 * @return array|\Requests_Utility_CaseInsensitiveDictionary The headers of the response. Empty array if incorrect parameter given. |
217 */ |
209 */ |
218 function wp_remote_retrieve_headers( $response ) { |
210 function wp_remote_retrieve_headers( $response ) { |
219 if ( is_wp_error( $response ) || ! isset( $response['headers'] ) ) { |
211 if ( is_wp_error( $response ) || ! isset( $response['headers'] ) ) { |
220 return array(); |
212 return array(); |
226 /** |
218 /** |
227 * Retrieve a single header by name from the raw response. |
219 * Retrieve a single header by name from the raw response. |
228 * |
220 * |
229 * @since 2.7.0 |
221 * @since 2.7.0 |
230 * |
222 * |
231 * @param array $response |
223 * @param array|WP_Error $response HTTP response. |
232 * @param string $header Header name to retrieve value from. |
224 * @param string $header Header name to retrieve value from. |
233 * @return string The header value. Empty string on if incorrect parameter given, or if the header doesn't exist. |
225 * @return string The header value. Empty string on if incorrect parameter given, or if the header doesn't exist. |
234 */ |
226 */ |
235 function wp_remote_retrieve_header( $response, $header ) { |
227 function wp_remote_retrieve_header( $response, $header ) { |
236 if ( is_wp_error( $response ) || ! isset( $response['headers'] ) ) { |
228 if ( is_wp_error( $response ) || ! isset( $response['headers'] ) ) { |
237 return ''; |
229 return ''; |
249 * |
241 * |
250 * Will return an empty array if incorrect parameter value is given. |
242 * Will return an empty array if incorrect parameter value is given. |
251 * |
243 * |
252 * @since 2.7.0 |
244 * @since 2.7.0 |
253 * |
245 * |
254 * @param array $response HTTP response. |
246 * @param array|WP_Error $response HTTP response. |
255 * @return int|string The response code as an integer. Empty string on incorrect parameter given. |
247 * @return int|string The response code as an integer. Empty string on incorrect parameter given. |
256 */ |
248 */ |
257 function wp_remote_retrieve_response_code( $response ) { |
249 function wp_remote_retrieve_response_code( $response ) { |
258 if ( is_wp_error( $response ) || ! isset( $response['response'] ) || ! is_array( $response['response'] ) ) { |
250 if ( is_wp_error( $response ) || ! isset( $response['response'] ) || ! is_array( $response['response'] ) ) { |
259 return ''; |
251 return ''; |
267 * |
259 * |
268 * Will return an empty array if incorrect parameter value is given. |
260 * Will return an empty array if incorrect parameter value is given. |
269 * |
261 * |
270 * @since 2.7.0 |
262 * @since 2.7.0 |
271 * |
263 * |
272 * @param array $response HTTP response. |
264 * @param array|WP_Error $response HTTP response. |
273 * @return string The response message. Empty string on incorrect parameter given. |
265 * @return string The response message. Empty string on incorrect parameter given. |
274 */ |
266 */ |
275 function wp_remote_retrieve_response_message( $response ) { |
267 function wp_remote_retrieve_response_message( $response ) { |
276 if ( is_wp_error( $response ) || ! isset( $response['response'] ) || ! is_array( $response['response'] ) ) { |
268 if ( is_wp_error( $response ) || ! isset( $response['response'] ) || ! is_array( $response['response'] ) ) { |
277 return ''; |
269 return ''; |
283 /** |
275 /** |
284 * Retrieve only the body from the raw response. |
276 * Retrieve only the body from the raw response. |
285 * |
277 * |
286 * @since 2.7.0 |
278 * @since 2.7.0 |
287 * |
279 * |
288 * @param array $response HTTP response. |
280 * @param array|WP_Error $response HTTP response. |
289 * @return string The body of the response. Empty string if no body or incorrect parameter given. |
281 * @return string The body of the response. Empty string if no body or incorrect parameter given. |
290 */ |
282 */ |
291 function wp_remote_retrieve_body( $response ) { |
283 function wp_remote_retrieve_body( $response ) { |
292 if ( is_wp_error( $response ) || ! isset( $response['body'] ) ) { |
284 if ( is_wp_error( $response ) || ! isset( $response['body'] ) ) { |
293 return ''; |
285 return ''; |
299 /** |
291 /** |
300 * Retrieve only the cookies from the raw response. |
292 * Retrieve only the cookies from the raw response. |
301 * |
293 * |
302 * @since 4.4.0 |
294 * @since 4.4.0 |
303 * |
295 * |
304 * @param array $response HTTP response. |
296 * @param array|WP_Error $response HTTP response. |
305 * @return array An array of `WP_Http_Cookie` objects from the response. Empty array if there are none, or the response is a WP_Error. |
297 * @return WP_Http_Cookie[] An array of `WP_Http_Cookie` objects from the response. Empty array if there are none, or the response is a WP_Error. |
306 */ |
298 */ |
307 function wp_remote_retrieve_cookies( $response ) { |
299 function wp_remote_retrieve_cookies( $response ) { |
308 if ( is_wp_error( $response ) || empty( $response['cookies'] ) ) { |
300 if ( is_wp_error( $response ) || empty( $response['cookies'] ) ) { |
309 return array(); |
301 return array(); |
310 } |
302 } |
315 /** |
307 /** |
316 * Retrieve a single cookie by name from the raw response. |
308 * Retrieve a single cookie by name from the raw response. |
317 * |
309 * |
318 * @since 4.4.0 |
310 * @since 4.4.0 |
319 * |
311 * |
320 * @param array $response HTTP response. |
312 * @param array|WP_Error $response HTTP response. |
321 * @param string $name The name of the cookie to retrieve. |
313 * @param string $name The name of the cookie to retrieve. |
322 * @return WP_Http_Cookie|string The `WP_Http_Cookie` object. Empty string if the cookie isn't present in the response. |
314 * @return WP_Http_Cookie|string The `WP_Http_Cookie` object. Empty string if the cookie isn't present in the response. |
323 */ |
315 */ |
324 function wp_remote_retrieve_cookie( $response, $name ) { |
316 function wp_remote_retrieve_cookie( $response, $name ) { |
325 $cookies = wp_remote_retrieve_cookies( $response ); |
317 $cookies = wp_remote_retrieve_cookies( $response ); |
326 |
318 |
340 /** |
332 /** |
341 * Retrieve a single cookie's value by name from the raw response. |
333 * Retrieve a single cookie's value by name from the raw response. |
342 * |
334 * |
343 * @since 4.4.0 |
335 * @since 4.4.0 |
344 * |
336 * |
345 * @param array $response HTTP response. |
337 * @param array|WP_Error $response HTTP response. |
346 * @param string $name The name of the cookie to retrieve. |
338 * @param string $name The name of the cookie to retrieve. |
347 * @return string The value of the cookie. Empty string if the cookie isn't present in the response. |
339 * @return string The value of the cookie. Empty string if the cookie isn't present in the response. |
348 */ |
340 */ |
349 function wp_remote_retrieve_cookie_value( $response, $name ) { |
341 function wp_remote_retrieve_cookie_value( $response, $name ) { |
350 $cookie = wp_remote_retrieve_cookie( $response, $name ); |
342 $cookie = wp_remote_retrieve_cookie( $response, $name ); |
351 |
343 |
372 |
364 |
373 $capabilities = wp_parse_args( $capabilities ); |
365 $capabilities = wp_parse_args( $capabilities ); |
374 |
366 |
375 $count = count( $capabilities ); |
367 $count = count( $capabilities ); |
376 |
368 |
377 // If we have a numeric $capabilities array, spoof a wp_remote_request() associative $args array |
369 // If we have a numeric $capabilities array, spoof a wp_remote_request() associative $args array. |
378 if ( $count && count( array_filter( array_keys( $capabilities ), 'is_numeric' ) ) == $count ) { |
370 if ( $count && count( array_filter( array_keys( $capabilities ), 'is_numeric' ) ) == $count ) { |
379 $capabilities = array_combine( array_values( $capabilities ), array_fill( 0, $count, true ) ); |
371 $capabilities = array_combine( array_values( $capabilities ), array_fill( 0, $count, true ) ); |
380 } |
372 } |
381 |
373 |
382 if ( $url && ! isset( $capabilities['ssl'] ) ) { |
374 if ( $url && ! isset( $capabilities['ssl'] ) ) { |
383 $scheme = parse_url( $url, PHP_URL_SCHEME ); |
375 $scheme = parse_url( $url, PHP_URL_SCHEME ); |
384 if ( 'https' == $scheme || 'ssl' == $scheme ) { |
376 if ( 'https' === $scheme || 'ssl' === $scheme ) { |
385 $capabilities['ssl'] = true; |
377 $capabilities['ssl'] = true; |
386 } |
378 } |
387 } |
379 } |
388 |
380 |
389 return (bool) $http->_get_first_available_transport( $capabilities ); |
381 return (bool) $http->_get_first_available_transport( $capabilities ); |
415 /** |
407 /** |
416 * Retrieve list of allowed HTTP origins. |
408 * Retrieve list of allowed HTTP origins. |
417 * |
409 * |
418 * @since 3.4.0 |
410 * @since 3.4.0 |
419 * |
411 * |
420 * @return array Array of origin URLs. |
412 * @return string[] Array of origin URLs. |
421 */ |
413 */ |
422 function get_allowed_http_origins() { |
414 function get_allowed_http_origins() { |
423 $admin_origin = parse_url( admin_url() ); |
415 $admin_origin = parse_url( admin_url() ); |
424 $home_origin = parse_url( home_url() ); |
416 $home_origin = parse_url( home_url() ); |
425 |
417 |
426 // @todo preserve port? |
418 // @todo Preserve port? |
427 $allowed_origins = array_unique( |
419 $allowed_origins = array_unique( |
428 array( |
420 array( |
429 'http://' . $admin_origin['host'], |
421 'http://' . $admin_origin['host'], |
430 'https://' . $admin_origin['host'], |
422 'https://' . $admin_origin['host'], |
431 'http://' . $home_origin['host'], |
423 'http://' . $home_origin['host'], |
436 /** |
428 /** |
437 * Change the origin types allowed for HTTP requests. |
429 * Change the origin types allowed for HTTP requests. |
438 * |
430 * |
439 * @since 3.4.0 |
431 * @since 3.4.0 |
440 * |
432 * |
441 * @param array $allowed_origins { |
433 * @param string[] $allowed_origins { |
442 * Default allowed HTTP origins. |
434 * Array of default allowed HTTP origins. |
443 * @type string Non-secure URL for admin origin. |
435 * |
444 * @type string Secure URL for admin origin. |
436 * @type string $0 Non-secure URL for admin origin. |
445 * @type string Non-secure URL for home origin. |
437 * @type string $1 Secure URL for admin origin. |
446 * @type string Secure URL for home origin. |
438 * @type string $2 Non-secure URL for home origin. |
439 * @type string $3 Secure URL for home origin. |
|
447 * } |
440 * } |
448 */ |
441 */ |
449 return apply_filters( 'allowed_http_origins', $allowed_origins ); |
442 return apply_filters( 'allowed_http_origins', $allowed_origins ); |
450 } |
443 } |
451 |
444 |
462 |
455 |
463 if ( null === $origin ) { |
456 if ( null === $origin ) { |
464 $origin = get_http_origin(); |
457 $origin = get_http_origin(); |
465 } |
458 } |
466 |
459 |
467 if ( $origin && ! in_array( $origin, get_allowed_http_origins() ) ) { |
460 if ( $origin && ! in_array( $origin, get_allowed_http_origins(), true ) ) { |
468 $origin = ''; |
461 $origin = ''; |
469 } |
462 } |
470 |
463 |
471 /** |
464 /** |
472 * Change the allowed HTTP origin result. |
465 * Change the allowed HTTP origin result. |
494 */ |
487 */ |
495 function send_origin_headers() { |
488 function send_origin_headers() { |
496 $origin = get_http_origin(); |
489 $origin = get_http_origin(); |
497 |
490 |
498 if ( is_allowed_http_origin( $origin ) ) { |
491 if ( is_allowed_http_origin( $origin ) ) { |
499 @header( 'Access-Control-Allow-Origin: ' . $origin ); |
492 header( 'Access-Control-Allow-Origin: ' . $origin ); |
500 @header( 'Access-Control-Allow-Credentials: true' ); |
493 header( 'Access-Control-Allow-Credentials: true' ); |
501 if ( 'OPTIONS' === $_SERVER['REQUEST_METHOD'] ) { |
494 if ( 'OPTIONS' === $_SERVER['REQUEST_METHOD'] ) { |
502 exit; |
495 exit; |
503 } |
496 } |
504 return $origin; |
497 return $origin; |
505 } |
498 } |
515 /** |
508 /** |
516 * Validate a URL for safe use in the HTTP API. |
509 * Validate a URL for safe use in the HTTP API. |
517 * |
510 * |
518 * @since 3.5.2 |
511 * @since 3.5.2 |
519 * |
512 * |
520 * @param string $url |
513 * @param string $url Request URL. |
521 * @return false|string URL or false on failure. |
514 * @return string|false URL or false on failure. |
522 */ |
515 */ |
523 function wp_http_validate_url( $url ) { |
516 function wp_http_validate_url( $url ) { |
524 $original_url = $url; |
517 $original_url = $url; |
525 $url = wp_kses_bad_protocol( $url, array( 'http', 'https' ) ); |
518 $url = wp_kses_bad_protocol( $url, array( 'http', 'https' ) ); |
526 if ( ! $url || strtolower( $url ) !== strtolower( $original_url ) ) { |
519 if ( ! $url || strtolower( $url ) !== strtolower( $original_url ) ) { |
527 return false; |
520 return false; |
528 } |
521 } |
529 |
522 |
530 $parsed_url = @parse_url( $url ); |
523 $parsed_url = parse_url( $url ); |
531 if ( ! $parsed_url || empty( $parsed_url['host'] ) ) { |
524 if ( ! $parsed_url || empty( $parsed_url['host'] ) ) { |
532 return false; |
525 return false; |
533 } |
526 } |
534 |
527 |
535 if ( isset( $parsed_url['user'] ) || isset( $parsed_url['pass'] ) ) { |
528 if ( isset( $parsed_url['user'] ) || isset( $parsed_url['pass'] ) ) { |
538 |
531 |
539 if ( false !== strpbrk( $parsed_url['host'], ':#?[]' ) ) { |
532 if ( false !== strpbrk( $parsed_url['host'], ':#?[]' ) ) { |
540 return false; |
533 return false; |
541 } |
534 } |
542 |
535 |
543 $parsed_home = @parse_url( get_option( 'home' ) ); |
536 $parsed_home = parse_url( get_option( 'home' ) ); |
544 |
537 |
545 if ( isset( $parsed_home['host'] ) ) { |
538 if ( isset( $parsed_home['host'] ) ) { |
546 $same_host = strtolower( $parsed_home['host'] ) === strtolower( $parsed_url['host'] ); |
539 $same_host = strtolower( $parsed_home['host'] ) === strtolower( $parsed_url['host'] ); |
547 } else { |
540 } else { |
548 $same_host = false; |
541 $same_host = false; |
552 $host = trim( $parsed_url['host'], '.' ); |
545 $host = trim( $parsed_url['host'], '.' ); |
553 if ( preg_match( '#^(([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)\.){3}([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)$#', $host ) ) { |
546 if ( preg_match( '#^(([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)\.){3}([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)$#', $host ) ) { |
554 $ip = $host; |
547 $ip = $host; |
555 } else { |
548 } else { |
556 $ip = gethostbyname( $host ); |
549 $ip = gethostbyname( $host ); |
557 if ( $ip === $host ) { // Error condition for gethostbyname() |
550 if ( $ip === $host ) { // Error condition for gethostbyname(). |
558 return false; |
551 return false; |
559 } |
552 } |
560 } |
553 } |
561 if ( $ip ) { |
554 if ( $ip ) { |
562 $parts = array_map( 'intval', explode( '.', $ip ) ); |
555 $parts = array_map( 'intval', explode( '.', $ip ) ); |
570 * |
563 * |
571 * Allows to change and allow external requests for the HTTP request. |
564 * Allows to change and allow external requests for the HTTP request. |
572 * |
565 * |
573 * @since 3.6.0 |
566 * @since 3.6.0 |
574 * |
567 * |
575 * @param bool false Whether HTTP request is external or not. |
568 * @param bool $external Whether HTTP request is external or not. |
576 * @param string $host IP of the requested host. |
569 * @param string $host Host name of the requested URL. |
577 * @param string $url URL of the requested host. |
570 * @param string $url Requested URL. |
578 */ |
571 */ |
579 if ( ! apply_filters( 'http_request_host_is_external', false, $host, $url ) ) { |
572 if ( ! apply_filters( 'http_request_host_is_external', false, $host, $url ) ) { |
580 return false; |
573 return false; |
581 } |
574 } |
582 } |
575 } |
598 |
591 |
599 return false; |
592 return false; |
600 } |
593 } |
601 |
594 |
602 /** |
595 /** |
603 * Whitelists allowed redirect hosts for safe HTTP requests as well. |
596 * Mark allowed redirect hosts safe for HTTP requests as well. |
604 * |
597 * |
605 * Attached to the {@see 'http_request_host_is_external'} filter. |
598 * Attached to the {@see 'http_request_host_is_external'} filter. |
606 * |
599 * |
607 * @since 3.6.0 |
600 * @since 3.6.0 |
608 * |
601 * |
616 } |
609 } |
617 return $is_external; |
610 return $is_external; |
618 } |
611 } |
619 |
612 |
620 /** |
613 /** |
621 * Whitelists any domain in a multisite installation for safe HTTP requests. |
614 * Adds any domain in a multisite installation for safe HTTP requests to the |
615 * allowed list. |
|
622 * |
616 * |
623 * Attached to the {@see 'http_request_host_is_external'} filter. |
617 * Attached to the {@see 'http_request_host_is_external'} filter. |
624 * |
618 * |
625 * @since 3.6.0 |
619 * @since 3.6.0 |
626 * |
620 * |
627 * @global wpdb $wpdb WordPress database abstraction object. |
621 * @global wpdb $wpdb WordPress database abstraction object. |
628 * @staticvar array $queried |
|
629 * |
622 * |
630 * @param bool $is_external |
623 * @param bool $is_external |
631 * @param string $host |
624 * @param string $host |
632 * @return bool |
625 * @return bool |
633 */ |
626 */ |
635 global $wpdb; |
628 global $wpdb; |
636 static $queried = array(); |
629 static $queried = array(); |
637 if ( $is_external ) { |
630 if ( $is_external ) { |
638 return $is_external; |
631 return $is_external; |
639 } |
632 } |
640 if ( $host === get_network()->domain ) { |
633 if ( get_network()->domain === $host ) { |
641 return true; |
634 return true; |
642 } |
635 } |
643 if ( isset( $queried[ $host ] ) ) { |
636 if ( isset( $queried[ $host ] ) ) { |
644 return $queried[ $host ]; |
637 return $queried[ $host ]; |
645 } |
638 } |
657 * |
650 * |
658 * Secondly, across various PHP versions, schemeless URLs starting containing a ":" |
651 * Secondly, across various PHP versions, schemeless URLs starting containing a ":" |
659 * in the query are being handled inconsistently. This function works around those |
652 * in the query are being handled inconsistently. This function works around those |
660 * differences as well. |
653 * differences as well. |
661 * |
654 * |
662 * Error suppression is used as prior to PHP 5.3.3, an E_WARNING would be generated |
|
663 * when URL parsing failed. |
|
664 * |
|
665 * @since 4.4.0 |
655 * @since 4.4.0 |
666 * @since 4.7.0 The `$component` parameter was added for parity with PHP's `parse_url()`. |
656 * @since 4.7.0 The `$component` parameter was added for parity with PHP's `parse_url()`. |
667 * |
657 * |
668 * @link https://secure.php.net/manual/en/function.parse-url.php |
658 * @link https://www.php.net/manual/en/function.parse-url.php |
669 * |
659 * |
670 * @param string $url The URL to parse. |
660 * @param string $url The URL to parse. |
671 * @param int $component The specific component to retrieve. Use one of the PHP |
661 * @param int $component The specific component to retrieve. Use one of the PHP |
672 * predefined constants to specify which one. |
662 * predefined constants to specify which one. |
673 * Defaults to -1 (= return all parts as an array). |
663 * Defaults to -1 (= return all parts as an array). |
687 $to_unset[] = 'scheme'; |
677 $to_unset[] = 'scheme'; |
688 $to_unset[] = 'host'; |
678 $to_unset[] = 'host'; |
689 $url = 'placeholder://placeholder' . $url; |
679 $url = 'placeholder://placeholder' . $url; |
690 } |
680 } |
691 |
681 |
692 $parts = @parse_url( $url ); |
682 $parts = parse_url( $url ); |
693 |
683 |
694 if ( false === $parts ) { |
684 if ( false === $parts ) { |
695 // Parsing failure. |
685 // Parsing failure. |
696 return $parts; |
686 return $parts; |
697 } |
687 } |
710 * @internal |
700 * @internal |
711 * |
701 * |
712 * @since 4.7.0 |
702 * @since 4.7.0 |
713 * @access private |
703 * @access private |
714 * |
704 * |
715 * @link https://secure.php.net/manual/en/function.parse-url.php |
705 * @link https://www.php.net/manual/en/function.parse-url.php |
716 * |
706 * |
717 * @param array|false $url_parts The parsed URL. Can be false if the URL failed to parse. |
707 * @param array|false $url_parts The parsed URL. Can be false if the URL failed to parse. |
718 * @param int $component The specific component to retrieve. Use one of the PHP |
708 * @param int $component The specific component to retrieve. Use one of the PHP |
719 * predefined constants to specify which one. |
709 * predefined constants to specify which one. |
720 * Defaults to -1 (= return all parts as an array). |
710 * Defaults to -1 (= return all parts as an array). |
721 * @return mixed False on parse failure; Array of URL components on success; |
711 * @return mixed False on parse failure; Array of URL components on success; |
722 * When a specific component has been requested: null if the component |
712 * When a specific component has been requested: null if the component |
723 * doesn't exist in the given URL; a string or - in the case of |
713 * doesn't exist in the given URL; a string or - in the case of |
724 * PHP_URL_PORT - integer when it does. See parse_url()'s return values. |
714 * PHP_URL_PORT - integer when it does. See parse_url()'s return values. |
725 */ |
715 */ |
742 * @internal |
732 * @internal |
743 * |
733 * |
744 * @since 4.7.0 |
734 * @since 4.7.0 |
745 * @access private |
735 * @access private |
746 * |
736 * |
747 * @link https://secure.php.net/manual/en/url.constants.php |
737 * @link https://www.php.net/manual/en/url.constants.php |
748 * |
738 * |
749 * @param int $constant PHP_URL_* constant. |
739 * @param int $constant PHP_URL_* constant. |
750 * @return string|bool The named key or false. |
740 * @return string|false The named key or false. |
751 */ |
741 */ |
752 function _wp_translate_php_url_constant_to_key( $constant ) { |
742 function _wp_translate_php_url_constant_to_key( $constant ) { |
753 $translation = array( |
743 $translation = array( |
754 PHP_URL_SCHEME => 'scheme', |
744 PHP_URL_SCHEME => 'scheme', |
755 PHP_URL_HOST => 'host', |
745 PHP_URL_HOST => 'host', |