← All changes
|
jetpack_vendor/automattic/jetpack-waf/src/functions.php
+61
-11
12.4.2
→
16.3-beta
View file →
| @@ -46,25 +46,75 @@ | ||
| 46 | 46 | * [ "field2[1]", "e" ], |
| 47 | 47 | * [ "field2[2]", "f" ], |
| 48 | 48 | * ] |
| 49 | 49 | * |
| 50 | - * @param array $array An array that resembles one of the PHP superglobals like $_GET or $_POST. | |
| 51 | - * @param string $key_prefix String that should be prepended to the keys output by this function. | |
| 52 | - * Usually only used internally as part of recursion when flattening a nested array. | |
| 53 | - * @return array{ 0: string, 1: scalar }[] $key_prefix An array of key/value tuples, one for each distinct value in the input array. | |
| 50 | + * @param array $array An array that resembles one of the PHP superglobals like $_GET or $_POST. | |
| 51 | + * @param string $key_prefix String that should be prepended to the keys output by this function. | |
| 52 | + * Usually only used internally as part of recursion when flattening a nested array. | |
| 53 | + * @param bool|null $dot_notation Whether to use dot notation instead of bracket notation. | |
| 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. | |
| 54 | 56 | */ |
| 55 | -function flatten_array( $array, $key_prefix = '' ) { | |
| 57 | +function flatten_array( $array, $key_prefix = '', $dot_notation = null ) { | |
| 56 | 58 | $return = array(); |
| 57 | 59 | foreach ( $array as $source_key => $source_value ) { |
| 58 | - $key = ( '' === $key_prefix ) | |
| 59 | - // if this is the first level, the key name isn't enclosed in brackets | |
| 60 | - ? $source_key | |
| 61 | - // for every level after the first, enclose the key name in brackets. | |
| 62 | - : $key_prefix . '[' . $source_key . ']'; | |
| 60 | + $key = $source_key; | |
| 61 | + if ( ! empty( $key_prefix ) ) { | |
| 62 | + $key = $dot_notation ? "$key_prefix.$source_key" : $key_prefix . "[$source_key]"; | |
| 63 | + } | |
| 64 | + | |
| 63 | 65 | if ( ! is_array( $source_value ) ) { |
| 64 | 66 | $return[] = array( $key, $source_value ); |
| 65 | 67 | } else { |
| 66 | - $return = array_merge( $return, flatten_array( $source_value, $key ) ); | |
| 68 | + $return = array_merge( $return, flatten_array( $source_value, $key, $dot_notation ) ); | |
| 67 | 69 | } |
| 68 | 70 | } |
| 69 | 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 | + } | |
| 70 | 120 | } |