]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/compat.php
WordPress 4.2-scripts
[autoinstalls/wordpress.git] / wp-includes / compat.php
1 <?php
2 /**
3  * WordPress implementation for PHP functions either missing from older PHP versions or not included by default.
4  *
5  * @package PHP
6  * @access private
7  */
8
9 // If gettext isn't available
10 if ( !function_exists('_') ) {
11         function _($string) {
12                 return $string;
13         }
14 }
15
16 if ( ! function_exists( 'mb_substr' ) ) :
17         function mb_substr( $str, $start, $length = null, $encoding = null ) {
18                 return _mb_substr( $str, $start, $length, $encoding );
19         }
20 endif;
21
22 function _mb_substr( $str, $start, $length = null, $encoding = null ) {
23         // The solution below works only for UTF-8,
24         // so in case of a different charset just use built-in substr()
25         $charset = get_option( 'blog_charset' );
26         if ( ! in_array( $charset, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
27                 return is_null( $length ) ? substr( $str, $start ) : substr( $str, $start, $length );
28         }
29         // Use the regex unicode support to separate the UTF-8 characters into an array
30         preg_match_all( '/./us', $str, $match );
31         $chars = is_null( $length ) ? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length );
32         return implode( '', $chars );
33 }
34
35 if ( ! function_exists( 'mb_strlen' ) ) :
36         function mb_strlen( $str, $encoding = null ) {
37                 return _mb_strlen( $str, $encoding );
38         }
39 endif;
40
41 function _mb_strlen( $str, $encoding = null ) {
42         // The solution below works only for UTF-8,
43         // so in case of a different charset just use built-in strlen()
44         $charset = get_option( 'blog_charset' );
45         if ( ! in_array( $charset, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
46                 return strlen( $str );
47         }
48         // Use the regex unicode support to separate the UTF-8 characters into an array
49         preg_match_all( '/./us', $str, $match );
50         return count( $match[0] );
51 }
52
53 if ( !function_exists('hash_hmac') ):
54 function hash_hmac($algo, $data, $key, $raw_output = false) {
55         return _hash_hmac($algo, $data, $key, $raw_output);
56 }
57 endif;
58
59 function _hash_hmac($algo, $data, $key, $raw_output = false) {
60         $packs = array('md5' => 'H32', 'sha1' => 'H40');
61
62         if ( !isset($packs[$algo]) )
63                 return false;
64
65         $pack = $packs[$algo];
66
67         if (strlen($key) > 64)
68                 $key = pack($pack, $algo($key));
69
70         $key = str_pad($key, 64, chr(0));
71
72         $ipad = (substr($key, 0, 64) ^ str_repeat(chr(0x36), 64));
73         $opad = (substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64));
74
75         $hmac = $algo($opad . pack($pack, $algo($ipad . $data)));
76
77         if ( $raw_output )
78                 return pack( $pack, $hmac );
79         return $hmac;
80 }
81
82 if ( !function_exists('json_encode') ) {
83         function json_encode( $string ) {
84                 global $wp_json;
85
86                 if ( ! ( $wp_json instanceof Services_JSON ) ) {
87                         require_once( ABSPATH . WPINC . '/class-json.php' );
88                         $wp_json = new Services_JSON();
89                 }
90
91                 return $wp_json->encodeUnsafe( $string );
92         }
93 }
94
95 if ( !function_exists('json_decode') ) {
96         function json_decode( $string, $assoc_array = false ) {
97                 global $wp_json;
98
99                 if ( ! ($wp_json instanceof Services_JSON ) ) {
100                         require_once( ABSPATH . WPINC . '/class-json.php' );
101                         $wp_json = new Services_JSON();
102                 }
103
104                 $res = $wp_json->decode( $string );
105                 if ( $assoc_array )
106                         $res = _json_decode_object_helper( $res );
107                 return $res;
108         }
109         function _json_decode_object_helper($data) {
110                 if ( is_object($data) )
111                         $data = get_object_vars($data);
112                 return is_array($data) ? array_map(__FUNCTION__, $data) : $data;
113         }
114 }
115
116 if ( ! function_exists( 'hash_equals' ) ) :
117 /**
118  * Compare two strings in constant time.
119  *
120  * This function was added in PHP 5.6.
121  * It can leak the length of a string.
122  *
123  * @since 3.9.2
124  *
125  * @param string $a Expected string.
126  * @param string $b Actual string.
127  * @return bool Whether strings are equal.
128  */
129 function hash_equals( $a, $b ) {
130         $a_length = strlen( $a );
131         if ( $a_length !== strlen( $b ) ) {
132                 return false;
133         }
134         $result = 0;
135
136         // Do not attempt to "optimize" this.
137         for ( $i = 0; $i < $a_length; $i++ ) {
138                 $result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] );
139         }
140
141         return $result === 0;
142 }
143 endif;
144
145 // JSON_PRETTY_PRINT was introduced in PHP 5.4
146 // Defined here to prevent a notice when using it with wp_json_encode()
147 if ( ! defined( 'JSON_PRETTY_PRINT' ) ) {
148         define( 'JSON_PRETTY_PRINT', 128 );
149 }