You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

744 lines
22 KiB

  1. <?php
  2. /**
  3. * Core HTTP Request API
  4. *
  5. * Standardizes the HTTP requests for WordPress. Handles cookies, gzip encoding and decoding, chunk
  6. * decoding, if HTTP 1.1 and various other difficult HTTP protocol implementations.
  7. *
  8. * @package WordPress
  9. * @subpackage HTTP
  10. */
  11. /**
  12. * Returns the initialized WP_Http Object
  13. *
  14. * @since 2.7.0
  15. * @access private
  16. *
  17. * @staticvar WP_Http $http
  18. *
  19. * @return WP_Http HTTP Transport object.
  20. */
  21. function _wp_http_get_object() {
  22. static $http = null;
  23. if ( is_null( $http ) ) {
  24. $http = new WP_Http();
  25. }
  26. return $http;
  27. }
  28. /**
  29. * Retrieve the raw response from a safe HTTP request.
  30. *
  31. * This function is ideal when the HTTP request is being made to an arbitrary
  32. * URL. The URL is validated to avoid redirection and request forgery attacks.
  33. *
  34. * @since 3.6.0
  35. *
  36. * @see wp_remote_request() For more information on the response array format.
  37. * @see WP_Http::request() For default arguments information.
  38. *
  39. * @param string $url Site URL to retrieve.
  40. * @param array $args Optional. Request arguments. Default empty array.
  41. * @return WP_Error|array The response or WP_Error on failure.
  42. */
  43. function wp_safe_remote_request( $url, $args = array() ) {
  44. $args['reject_unsafe_urls'] = true;
  45. $http = _wp_http_get_object();
  46. return $http->request( $url, $args );
  47. }
  48. /**
  49. * Retrieve the raw response from a safe HTTP request using the GET method.
  50. *
  51. * This function is ideal when the HTTP request is being made to an arbitrary
  52. * URL. The URL is validated to avoid redirection and request forgery attacks.
  53. *
  54. * @since 3.6.0
  55. *
  56. * @see wp_remote_request() For more information on the response array format.
  57. * @see WP_Http::request() For default arguments information.
  58. *
  59. * @param string $url Site URL to retrieve.
  60. * @param array $args Optional. Request arguments. Default empty array.
  61. * @return WP_Error|array The response or WP_Error on failure.
  62. */
  63. function wp_safe_remote_get( $url, $args = array() ) {
  64. $args['reject_unsafe_urls'] = true;
  65. $http = _wp_http_get_object();
  66. return $http->get( $url, $args );
  67. }
  68. /**
  69. * Retrieve the raw response from a safe HTTP request using the POST method.
  70. *
  71. * This function is ideal when the HTTP request is being made to an arbitrary
  72. * URL. The URL is validated to avoid redirection and request forgery attacks.
  73. *
  74. * @since 3.6.0
  75. *
  76. * @see wp_remote_request() For more information on the response array format.
  77. * @see WP_Http::request() For default arguments information.
  78. *
  79. * @param string $url Site URL to retrieve.
  80. * @param array $args Optional. Request arguments. Default empty array.
  81. * @return WP_Error|array The response or WP_Error on failure.
  82. */
  83. function wp_safe_remote_post( $url, $args = array() ) {
  84. $args['reject_unsafe_urls'] = true;
  85. $http = _wp_http_get_object();
  86. return $http->post( $url, $args );
  87. }
  88. /**
  89. * Retrieve the raw response from a safe HTTP request using the HEAD method.
  90. *
  91. * This function is ideal when the HTTP request is being made to an arbitrary
  92. * URL. The URL is validated to avoid redirection and request forgery attacks.
  93. *
  94. * @since 3.6.0
  95. *
  96. * @see wp_remote_request() For more information on the response array format.
  97. * @see WP_Http::request() For default arguments information.
  98. *
  99. * @param string $url Site URL to retrieve.
  100. * @param array $args Optional. Request arguments. Default empty array.
  101. * @return WP_Error|array The response or WP_Error on failure.
  102. */
  103. function wp_safe_remote_head( $url, $args = array() ) {
  104. $args['reject_unsafe_urls'] = true;
  105. $http = _wp_http_get_object();
  106. return $http->head( $url, $args );
  107. }
  108. /**
  109. * Retrieve the raw response from the HTTP request.
  110. *
  111. * The array structure is a little complex:
  112. *
  113. * $res = array(
  114. * 'headers' => array(),
  115. * 'response' => array(
  116. * 'code' => int,
  117. * 'message' => string
  118. * )
  119. * );
  120. *
  121. * All of the headers in $res['headers'] are with the name as the key and the
  122. * value as the value. So to get the User-Agent, you would do the following.
  123. *
  124. * $user_agent = $res['headers']['user-agent'];
  125. *
  126. * The body is the raw response content and can be retrieved from $res['body'].
  127. *
  128. * This function is called first to make the request and there are other API
  129. * functions to abstract out the above convoluted setup.
  130. *
  131. * Request method defaults for helper functions:
  132. * - Default 'GET' for wp_remote_get()
  133. * - Default 'POST' for wp_remote_post()
  134. * - Default 'HEAD' for wp_remote_head()
  135. *
  136. * @since 2.7.0
  137. *
  138. * @see WP_Http::request() For additional information on default arguments.
  139. *
  140. * @param string $url Site URL to retrieve.
  141. * @param array $args Optional. Request arguments. Default empty array.
  142. * @return WP_Error|array The response or WP_Error on failure.
  143. */
  144. function wp_remote_request($url, $args = array()) {
  145. $http = _wp_http_get_object();
  146. return $http->request( $url, $args );
  147. }
  148. /**
  149. * Retrieve the raw response from the HTTP request using the GET method.
  150. *
  151. * @since 2.7.0
  152. *
  153. * @see wp_remote_request() For more information on the response array format.
  154. * @see WP_Http::request() For default arguments information.
  155. *
  156. * @param string $url Site URL to retrieve.
  157. * @param array $args Optional. Request arguments. Default empty array.
  158. * @return WP_Error|array The response or WP_Error on failure.
  159. */
  160. function wp_remote_get($url, $args = array()) {
  161. $http = _wp_http_get_object();
  162. return $http->get( $url, $args );
  163. }
  164. /**
  165. * Retrieve the raw response from the HTTP request using the POST method.
  166. *
  167. * @since 2.7.0
  168. *
  169. * @see wp_remote_request() For more information on the response array format.
  170. * @see WP_Http::request() For default arguments information.
  171. *
  172. * @param string $url Site URL to retrieve.
  173. * @param array $args Optional. Request arguments. Default empty array.
  174. * @return WP_Error|array The response or WP_Error on failure.
  175. */
  176. function wp_remote_post($url, $args = array()) {
  177. $http = _wp_http_get_object();
  178. return $http->post( $url, $args );
  179. }
  180. /**
  181. * Retrieve the raw response from the HTTP request using the HEAD method.
  182. *
  183. * @since 2.7.0
  184. *
  185. * @see wp_remote_request() For more information on the response array format.
  186. * @see WP_Http::request() For default arguments information.
  187. *
  188. * @param string $url Site URL to retrieve.
  189. * @param array $args Optional. Request arguments. Default empty array.
  190. * @return WP_Error|array The response or WP_Error on failure.
  191. */
  192. function wp_remote_head($url, $args = array()) {
  193. $http = _wp_http_get_object();
  194. return $http->head( $url, $args );
  195. }
  196. /**
  197. * Retrieve only the headers from the raw response.
  198. *
  199. * @since 2.7.0
  200. * @since 4.6.0 Return value changed from an array to an Requests_Utility_CaseInsensitiveDictionary instance.
  201. *
  202. * @see \Requests_Utility_CaseInsensitiveDictionary
  203. *
  204. * @param array $response HTTP response.
  205. * @return array|\Requests_Utility_CaseInsensitiveDictionary The headers of the response. Empty array if incorrect parameter given.
  206. */
  207. function wp_remote_retrieve_headers( $response ) {
  208. if ( is_wp_error( $response ) || ! isset( $response['headers'] ) ) {
  209. return array();
  210. }
  211. return $response['headers'];
  212. }
  213. /**
  214. * Retrieve a single header by name from the raw response.
  215. *
  216. * @since 2.7.0
  217. *
  218. * @param array $response
  219. * @param string $header Header name to retrieve value from.
  220. * @return string The header value. Empty string on if incorrect parameter given, or if the header doesn't exist.
  221. */
  222. function wp_remote_retrieve_header( $response, $header ) {
  223. if ( is_wp_error( $response ) || ! isset( $response['headers'] ) ) {
  224. return '';
  225. }
  226. if ( isset( $response['headers'][ $header ] ) ) {
  227. return $response['headers'][$header];
  228. }
  229. return '';
  230. }
  231. /**
  232. * Retrieve only the response code from the raw response.
  233. *
  234. * Will return an empty array if incorrect parameter value is given.
  235. *
  236. * @since 2.7.0
  237. *
  238. * @param array $response HTTP response.
  239. * @return int|string The response code as an integer. Empty string on incorrect parameter given.
  240. */
  241. function wp_remote_retrieve_response_code( $response ) {
  242. if ( is_wp_error($response) || ! isset($response['response']) || ! is_array($response['response']))
  243. return '';
  244. return $response['response']['code'];
  245. }
  246. /**
  247. * Retrieve only the response message from the raw response.
  248. *
  249. * Will return an empty array if incorrect parameter value is given.
  250. *
  251. * @since 2.7.0
  252. *
  253. * @param array $response HTTP response.
  254. * @return string The response message. Empty string on incorrect parameter given.
  255. */
  256. function wp_remote_retrieve_response_message( $response ) {
  257. if ( is_wp_error($response) || ! isset($response['response']) || ! is_array($response['response']))
  258. return '';
  259. return $response['response']['message'];
  260. }
  261. /**
  262. * Retrieve only the body from the raw response.
  263. *
  264. * @since 2.7.0
  265. *
  266. * @param array $response HTTP response.
  267. * @return string The body of the response. Empty string if no body or incorrect parameter given.
  268. */
  269. function wp_remote_retrieve_body( $response ) {
  270. if ( is_wp_error($response) || ! isset($response['body']) )
  271. return '';
  272. return $response['body'];
  273. }
  274. /**
  275. * Retrieve only the cookies from the raw response.
  276. *
  277. * @since 4.4.0
  278. *
  279. * @param array $response HTTP response.
  280. * @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.
  281. */
  282. function wp_remote_retrieve_cookies( $response ) {
  283. if ( is_wp_error( $response ) || empty( $response['cookies'] ) ) {
  284. return array();
  285. }
  286. return $response['cookies'];
  287. }
  288. /**
  289. * Retrieve a single cookie by name from the raw response.
  290. *
  291. * @since 4.4.0
  292. *
  293. * @param array $response HTTP response.
  294. * @param string $name The name of the cookie to retrieve.
  295. * @return WP_Http_Cookie|string The `WP_Http_Cookie` object. Empty string if the cookie isn't present in the response.
  296. */
  297. function wp_remote_retrieve_cookie( $response, $name ) {
  298. $cookies = wp_remote_retrieve_cookies( $response );
  299. if ( empty( $cookies ) ) {
  300. return '';
  301. }
  302. foreach ( $cookies as $cookie ) {
  303. if ( $cookie->name === $name ) {
  304. return $cookie;
  305. }
  306. }
  307. return '';
  308. }
  309. /**
  310. * Retrieve a single cookie's value by name from the raw response.
  311. *
  312. * @since 4.4.0
  313. *
  314. * @param array $response HTTP response.
  315. * @param string $name The name of the cookie to retrieve.
  316. * @return string The value of the cookie. Empty string if the cookie isn't present in the response.
  317. */
  318. function wp_remote_retrieve_cookie_value( $response, $name ) {
  319. $cookie = wp_remote_retrieve_cookie( $response, $name );
  320. if ( ! is_a( $cookie, 'WP_Http_Cookie' ) ) {
  321. return '';
  322. }
  323. return $cookie->value;
  324. }
  325. /**
  326. * Determines if there is an HTTP Transport that can process this request.
  327. *
  328. * @since 3.2.0
  329. *
  330. * @param array $capabilities Array of capabilities to test or a wp_remote_request() $args array.
  331. * @param string $url Optional. If given, will check if the URL requires SSL and adds
  332. * that requirement to the capabilities array.
  333. *
  334. * @return bool
  335. */
  336. function wp_http_supports( $capabilities = array(), $url = null ) {
  337. $http = _wp_http_get_object();
  338. $capabilities = wp_parse_args( $capabilities );
  339. $count = count( $capabilities );
  340. // If we have a numeric $capabilities array, spoof a wp_remote_request() associative $args array
  341. if ( $count && count( array_filter( array_keys( $capabilities ), 'is_numeric' ) ) == $count ) {
  342. $capabilities = array_combine( array_values( $capabilities ), array_fill( 0, $count, true ) );
  343. }
  344. if ( $url && !isset( $capabilities['ssl'] ) ) {
  345. $scheme = parse_url( $url, PHP_URL_SCHEME );
  346. if ( 'https' == $scheme || 'ssl' == $scheme ) {
  347. $capabilities['ssl'] = true;
  348. }
  349. }
  350. return (bool) $http->_get_first_available_transport( $capabilities );
  351. }
  352. /**
  353. * Get the HTTP Origin of the current request.
  354. *
  355. * @since 3.4.0
  356. *
  357. * @return string URL of the origin. Empty string if no origin.
  358. */
  359. function get_http_origin() {
  360. $origin = '';
  361. if ( ! empty ( $_SERVER[ 'HTTP_ORIGIN' ] ) )
  362. $origin = $_SERVER[ 'HTTP_ORIGIN' ];
  363. /**
  364. * Change the origin of an HTTP request.
  365. *
  366. * @since 3.4.0
  367. *
  368. * @param string $origin The original origin for the request.
  369. */
  370. return apply_filters( 'http_origin', $origin );
  371. }
  372. /**
  373. * Retrieve list of allowed HTTP origins.
  374. *
  375. * @since 3.4.0
  376. *
  377. * @return array Array of origin URLs.
  378. */
  379. function get_allowed_http_origins() {
  380. $admin_origin = parse_url( admin_url() );
  381. $home_origin = parse_url( home_url() );
  382. // @todo preserve port?
  383. $allowed_origins = array_unique( array(
  384. 'http://' . $admin_origin[ 'host' ],
  385. 'https://' . $admin_origin[ 'host' ],
  386. 'http://' . $home_origin[ 'host' ],
  387. 'https://' . $home_origin[ 'host' ],
  388. ) );
  389. /**
  390. * Change the origin types allowed for HTTP requests.
  391. *
  392. * @since 3.4.0
  393. *
  394. * @param array $allowed_origins {
  395. * Default allowed HTTP origins.
  396. * @type string Non-secure URL for admin origin.
  397. * @type string Secure URL for admin origin.
  398. * @type string Non-secure URL for home origin.
  399. * @type string Secure URL for home origin.
  400. * }
  401. */
  402. return apply_filters( 'allowed_http_origins' , $allowed_origins );
  403. }
  404. /**
  405. * Determines if the HTTP origin is an authorized one.
  406. *
  407. * @since 3.4.0
  408. *
  409. * @param null|string $origin Origin URL. If not provided, the value of get_http_origin() is used.
  410. * @return string Origin URL if allowed, empty string if not.
  411. */
  412. function is_allowed_http_origin( $origin = null ) {
  413. $origin_arg = $origin;
  414. if ( null === $origin )
  415. $origin = get_http_origin();
  416. if ( $origin && ! in_array( $origin, get_allowed_http_origins() ) )
  417. $origin = '';
  418. /**
  419. * Change the allowed HTTP origin result.
  420. *
  421. * @since 3.4.0
  422. *
  423. * @param string $origin Origin URL if allowed, empty string if not.
  424. * @param string $origin_arg Original origin string passed into is_allowed_http_origin function.
  425. */
  426. return apply_filters( 'allowed_http_origin', $origin, $origin_arg );
  427. }
  428. /**
  429. * Send Access-Control-Allow-Origin and related headers if the current request
  430. * is from an allowed origin.
  431. *
  432. * If the request is an OPTIONS request, the script exits with either access
  433. * control headers sent, or a 403 response if the origin is not allowed. For
  434. * other request methods, you will receive a return value.
  435. *
  436. * @since 3.4.0
  437. *
  438. * @return string|false Returns the origin URL if headers are sent. Returns false
  439. * if headers are not sent.
  440. */
  441. function send_origin_headers() {
  442. $origin = get_http_origin();
  443. if ( is_allowed_http_origin( $origin ) ) {
  444. @header( 'Access-Control-Allow-Origin: ' . $origin );
  445. @header( 'Access-Control-Allow-Credentials: true' );
  446. if ( 'OPTIONS' === $_SERVER['REQUEST_METHOD'] )
  447. exit;
  448. return $origin;
  449. }
  450. if ( 'OPTIONS' === $_SERVER['REQUEST_METHOD'] ) {
  451. status_header( 403 );
  452. exit;
  453. }
  454. return false;
  455. }
  456. /**
  457. * Validate a URL for safe use in the HTTP API.
  458. *
  459. * @since 3.5.2
  460. *
  461. * @param string $url
  462. * @return false|string URL or false on failure.
  463. */
  464. function wp_http_validate_url( $url ) {
  465. $original_url = $url;
  466. $url = wp_kses_bad_protocol( $url, array( 'http', 'https' ) );
  467. if ( ! $url || strtolower( $url ) !== strtolower( $original_url ) )
  468. return false;
  469. $parsed_url = @parse_url( $url );
  470. if ( ! $parsed_url || empty( $parsed_url['host'] ) )
  471. return false;
  472. if ( isset( $parsed_url['user'] ) || isset( $parsed_url['pass'] ) )
  473. return false;
  474. if ( false !== strpbrk( $parsed_url['host'], ':#?[]' ) )
  475. return false;
  476. $parsed_home = @parse_url( get_option( 'home' ) );
  477. if ( isset( $parsed_home['host'] ) ) {
  478. $same_host = ( strtolower( $parsed_home['host'] ) === strtolower( $parsed_url['host'] ) || 'localhost' === strtolower( $parsed_url['host'] ) );
  479. } else {
  480. $same_host = false;
  481. }
  482. if ( ! $same_host ) {
  483. $host = trim( $parsed_url['host'], '.' );
  484. 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 ) ) {
  485. $ip = $host;
  486. } else {
  487. $ip = gethostbyname( $host );
  488. if ( $ip === $host ) // Error condition for gethostbyname()
  489. $ip = false;
  490. }
  491. if ( $ip ) {
  492. $parts = array_map( 'intval', explode( '.', $ip ) );
  493. if ( 127 === $parts[0] || 10 === $parts[0] || 0 === $parts[0]
  494. || ( 172 === $parts[0] && 16 <= $parts[1] && 31 >= $parts[1] )
  495. || ( 192 === $parts[0] && 168 === $parts[1] )
  496. ) {
  497. // If host appears local, reject unless specifically allowed.
  498. /**
  499. * Check if HTTP request is external or not.
  500. *
  501. * Allows to change and allow external requests for the HTTP request.
  502. *
  503. * @since 3.6.0
  504. *
  505. * @param bool false Whether HTTP request is external or not.
  506. * @param string $host IP of the requested host.
  507. * @param string $url URL of the requested host.
  508. */
  509. if ( ! apply_filters( 'http_request_host_is_external', false, $host, $url ) )
  510. return false;
  511. }
  512. }
  513. }
  514. if ( empty( $parsed_url['port'] ) )
  515. return $url;
  516. $port = $parsed_url['port'];
  517. if ( 80 === $port || 443 === $port || 8080 === $port )
  518. return $url;
  519. if ( $parsed_home && $same_host && isset( $parsed_home['port'] ) && $parsed_home['port'] === $port )
  520. return $url;
  521. return false;
  522. }
  523. /**
  524. * Whitelists allowed redirect hosts for safe HTTP requests as well.
  525. *
  526. * Attached to the {@see 'http_request_host_is_external'} filter.
  527. *
  528. * @since 3.6.0
  529. *
  530. * @param bool $is_external
  531. * @param string $host
  532. * @return bool
  533. */
  534. function allowed_http_request_hosts( $is_external, $host ) {
  535. if ( ! $is_external && wp_validate_redirect( 'http://' . $host ) )
  536. $is_external = true;
  537. return $is_external;
  538. }
  539. /**
  540. * Whitelists any domain in a multisite installation for safe HTTP requests.
  541. *
  542. * Attached to the {@see 'http_request_host_is_external'} filter.
  543. *
  544. * @since 3.6.0
  545. *
  546. * @global wpdb $wpdb WordPress database abstraction object.
  547. * @staticvar array $queried
  548. *
  549. * @param bool $is_external
  550. * @param string $host
  551. * @return bool
  552. */
  553. function ms_allowed_http_request_hosts( $is_external, $host ) {
  554. global $wpdb;
  555. static $queried = array();
  556. if ( $is_external )
  557. return $is_external;
  558. if ( $host === get_network()->domain )
  559. return true;
  560. if ( isset( $queried[ $host ] ) )
  561. return $queried[ $host ];
  562. $queried[ $host ] = (bool) $wpdb->get_var( $wpdb->prepare( "SELECT domain FROM $wpdb->blogs WHERE domain = %s LIMIT 1", $host ) );
  563. return $queried[ $host ];
  564. }
  565. /**
  566. * A wrapper for PHP's parse_url() function that handles consistency in the return
  567. * values across PHP versions.
  568. *
  569. * PHP 5.4.7 expanded parse_url()'s ability to handle non-absolute url's, including
  570. * schemeless and relative url's with :// in the path. This function works around
  571. * those limitations providing a standard output on PHP 5.2~5.4+.
  572. *
  573. * Secondly, across various PHP versions, schemeless URLs starting containing a ":"
  574. * in the query are being handled inconsistently. This function works around those
  575. * differences as well.
  576. *
  577. * Error suppression is used as prior to PHP 5.3.3, an E_WARNING would be generated
  578. * when URL parsing failed.
  579. *
  580. * @since 4.4.0
  581. * @since 4.7.0 The $component parameter was added for parity with PHP's parse_url().
  582. *
  583. * @param string $url The URL to parse.
  584. * @param int $component The specific component to retrieve. Use one of the PHP
  585. * predefined constants to specify which one.
  586. * Defaults to -1 (= return all parts as an array).
  587. * @see http://php.net/manual/en/function.parse-url.php
  588. * @return mixed False on parse failure; Array of URL components on success;
  589. * When a specific component has been requested: null if the component
  590. * doesn't exist in the given URL; a sting or - in the case of
  591. * PHP_URL_PORT - integer when it does. See parse_url()'s return values.
  592. */
  593. function wp_parse_url( $url, $component = -1 ) {
  594. $to_unset = array();
  595. $url = strval( $url );
  596. if ( '//' === substr( $url, 0, 2 ) ) {
  597. $to_unset[] = 'scheme';
  598. $url = 'placeholder:' . $url;
  599. } elseif ( '/' === substr( $url, 0, 1 ) ) {
  600. $to_unset[] = 'scheme';
  601. $to_unset[] = 'host';
  602. $url = 'placeholder://placeholder' . $url;
  603. }
  604. $parts = @parse_url( $url );
  605. if ( false === $parts ) {
  606. // Parsing failure.
  607. return $parts;
  608. }
  609. // Remove the placeholder values.
  610. foreach ( $to_unset as $key ) {
  611. unset( $parts[ $key ] );
  612. }
  613. return _get_component_from_parsed_url_array( $parts, $component );
  614. }
  615. /**
  616. * Retrieve a specific component from a parsed URL array.
  617. *
  618. * @internal
  619. *
  620. * @since 4.7.0
  621. *
  622. * @param array|false $url_parts The parsed URL. Can be false if the URL failed to parse.
  623. * @param int $component The specific component to retrieve. Use one of the PHP
  624. * predefined constants to specify which one.
  625. * Defaults to -1 (= return all parts as an array).
  626. * @see http://php.net/manual/en/function.parse-url.php
  627. * @return mixed False on parse failure; Array of URL components on success;
  628. * When a specific component has been requested: null if the component
  629. * doesn't exist in the given URL; a sting or - in the case of
  630. * PHP_URL_PORT - integer when it does. See parse_url()'s return values.
  631. */
  632. function _get_component_from_parsed_url_array( $url_parts, $component = -1 ) {
  633. if ( -1 === $component ) {
  634. return $url_parts;
  635. }
  636. $key = _wp_translate_php_url_constant_to_key( $component );
  637. if ( false !== $key && is_array( $url_parts ) && isset( $url_parts[ $key ] ) ) {
  638. return $url_parts[ $key ];
  639. } else {
  640. return null;
  641. }
  642. }
  643. /**
  644. * Translate a PHP_URL_* constant to the named array keys PHP uses.
  645. *
  646. * @internal
  647. *
  648. * @since 4.7.0
  649. *
  650. * @see http://php.net/manual/en/url.constants.php
  651. *
  652. * @param int $constant PHP_URL_* constant.
  653. * @return string|bool The named key or false.
  654. */
  655. function _wp_translate_php_url_constant_to_key( $constant ) {
  656. $translation = array(
  657. PHP_URL_SCHEME => 'scheme',
  658. PHP_URL_HOST => 'host',
  659. PHP_URL_PORT => 'port',
  660. PHP_URL_USER => 'user',
  661. PHP_URL_PASS => 'pass',
  662. PHP_URL_PATH => 'path',
  663. PHP_URL_QUERY => 'query',
  664. PHP_URL_FRAGMENT => 'fragment',
  665. );
  666. if ( isset( $translation[ $constant ] ) ) {
  667. return $translation[ $constant ];
  668. } else {
  669. return false;
  670. }
  671. }