← All changes
|
jetpack_vendor/automattic/jetpack-waf/src/functions.php
+49
-1
13.3.3
→
16.3
View file →
| @@ -51,9 +51,9 @@ | ||
| 51 | 51 | * @param string $key_prefix String that should be prepended to the keys output by this function. |
| 52 | 52 | * Usually only used internally as part of recursion when flattening a nested array. |
| 53 | 53 | * @param bool|null $dot_notation Whether to use dot notation instead of bracket notation. |
| 54 | 54 | * |
| 55 | - * @return array{ 0: string, 1: scalar }[] $key_prefix An array of key/value tuples, one for each distinct value in the input array. | |
| 55 | + * @return array{0: string, 1: scalar}[] $key_prefix An array of key/value tuples, one for each distinct value in the input array. | |
| 56 | 56 | */ |
| 57 | 57 | function flatten_array( $array, $key_prefix = '', $dot_notation = null ) { |
| 58 | 58 | $return = array(); |
| 59 | 59 | foreach ( $array as $source_key => $source_value ) { |
| @@ -68,5 +68,53 @@ | ||
| 68 | 68 | $return = array_merge( $return, flatten_array( $source_value, $key, $dot_notation ) ); |
| 69 | 69 | } |
| 70 | 70 | } |
| 71 | 71 | return $return; |
| 72 | +} | |
| 73 | + | |
| 74 | +/** | |
| 75 | + * Polyfill for getallheaders, which is not available in all PHP environments. | |
| 76 | + * | |
| 77 | + * @link https://github.com/ralouphie/getallheaders | |
| 78 | + */ | |
| 79 | +if ( ! function_exists( 'getallheaders' ) ) { | |
| 80 | + /** | |
| 81 | + * Get all HTTP header key/values as an associative array for the current request. | |
| 82 | + * | |
| 83 | + * @return array The HTTP header key/value pairs. | |
| 84 | + */ | |
| 85 | + function getallheaders() { | |
| 86 | + // phpcs:disable WordPress.Security.ValidatedSanitizedInput | |
| 87 | + $headers = array(); | |
| 88 | + | |
| 89 | + $copy_server = array( | |
| 90 | + 'CONTENT_TYPE' => 'Content-Type', | |
| 91 | + 'CONTENT_LENGTH' => 'Content-Length', | |
| 92 | + 'CONTENT_MD5' => 'Content-Md5', | |
| 93 | + ); | |
| 94 | + | |
| 95 | + foreach ( $_SERVER as $key => $value ) { | |
| 96 | + if ( substr( $key, 0, 5 ) === 'HTTP_' ) { | |
| 97 | + $key = substr( $key, 5 ); | |
| 98 | + if ( ! isset( $copy_server[ $key ] ) || ! isset( $_SERVER[ $key ] ) ) { | |
| 99 | + $key = str_replace( ' ', '-', ucwords( strtolower( str_replace( '_', ' ', $key ) ) ) ); | |
| 100 | + $headers[ $key ] = $value; | |
| 101 | + } | |
| 102 | + } elseif ( isset( $copy_server[ $key ] ) ) { | |
| 103 | + $headers[ $copy_server[ $key ] ] = $value; | |
| 104 | + } | |
| 105 | + } | |
| 106 | + | |
| 107 | + if ( ! isset( $headers['Authorization'] ) ) { | |
| 108 | + if ( isset( $_SERVER['REDIRECT_HTTP_AUTHORIZATION'] ) ) { | |
| 109 | + $headers['Authorization'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION']; | |
| 110 | + } elseif ( isset( $_SERVER['PHP_AUTH_USER'] ) ) { | |
| 111 | + $basic_pass = $_SERVER['PHP_AUTH_PW'] ?? ''; | |
| 112 | + $headers['Authorization'] = 'Basic ' . base64_encode( $_SERVER['PHP_AUTH_USER'] . ':' . $basic_pass ); | |
| 113 | + } elseif ( isset( $_SERVER['PHP_AUTH_DIGEST'] ) ) { | |
| 114 | + $headers['Authorization'] = $_SERVER['PHP_AUTH_DIGEST']; | |
| 115 | + } | |
| 116 | + } | |
| 117 | + | |
| 118 | + return $headers; | |
| 119 | + } | |
| 72 | 120 | } |