Sin descripción

http.php 22KB

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