3 * WordPress implementation for PHP functions missing from older PHP versions.
11 if (!function_exists('http_build_query')) {
12 function http_build_query($data, $prefix=null, $sep=null) {
13 return _http_build_query($data, $prefix, $sep);
17 // from php.net (modified by Mark Jaquith to behave like the native PHP5 function)
18 function _http_build_query($data, $prefix=null, $sep=null, $key='', $urlencode=true) {
21 foreach ( (array) $data as $k => $v ) {
24 if ( is_int($k) && $prefix != null )
27 $k = $key . '%5B' . $k . '%5D';
30 elseif ( $v === FALSE )
33 if ( is_array($v) || is_object($v) )
34 array_push($ret,_http_build_query($v, '', $sep, $k, $urlencode));
36 array_push($ret, $k.'='.urlencode($v));
38 array_push($ret, $k.'='.$v);
42 $sep = ini_get('arg_separator.output');
44 return implode($sep, $ret);
47 if ( !function_exists('_') ) {
53 if (!function_exists('stripos')) {
54 function stripos($haystack, $needle, $offset = 0) {
55 return strpos(strtolower($haystack), strtolower($needle), $offset);
59 if ( !function_exists('hash_hmac') ):
60 function hash_hmac($algo, $data, $key, $raw_output = false) {
61 return _hash_hmac($algo, $data, $key, $raw_output);
65 function _hash_hmac($algo, $data, $key, $raw_output = false) {
66 $packs = array('md5' => 'H32', 'sha1' => 'H40');
68 if ( !isset($packs[$algo]) )
71 $pack = $packs[$algo];
73 if (strlen($key) > 64)
74 $key = pack($pack, $algo($key));
76 $key = str_pad($key, 64, chr(0));
78 $ipad = (substr($key, 0, 64) ^ str_repeat(chr(0x36), 64));
79 $opad = (substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64));
81 $hmac = $algo($opad . pack($pack, $algo($ipad . $data)));
84 return pack( $pack, $hmac );
88 if ( !function_exists('mb_substr') ):
89 function mb_substr( $str, $start, $length=null, $encoding=null ) {
90 return _mb_substr($str, $start, $length, $encoding);
94 function _mb_substr( $str, $start, $length=null, $encoding=null ) {
95 // the solution below, works only for utf-8, so in case of a different
96 // charset, just use built-in substr
97 $charset = get_option( 'blog_charset' );
98 if ( !in_array( $charset, array('utf8', 'utf-8', 'UTF8', 'UTF-8') ) ) {
99 return is_null( $length )? substr( $str, $start ) : substr( $str, $start, $length);
101 // use the regex unicode support to separate the UTF-8 characters into an array
102 preg_match_all( '/./us', $str, $match );
103 $chars = is_null( $length )? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length );
104 return implode( '', $chars );
107 if ( !function_exists( 'htmlspecialchars_decode' ) ) {
108 // Added in PHP 5.1.0
109 // Error checks from PEAR::PHP_Compat
110 function htmlspecialchars_decode( $string, $quote_style = ENT_COMPAT )
112 if ( !is_scalar( $string ) ) {
113 trigger_error( 'htmlspecialchars_decode() expects parameter 1 to be string, ' . gettype( $string ) . ' given', E_USER_WARNING );
117 if ( !is_int( $quote_style ) && $quote_style !== null ) {
118 trigger_error( 'htmlspecialchars_decode() expects parameter 2 to be integer, ' . gettype( $quote_style ) . ' given', E_USER_WARNING );
122 return wp_specialchars_decode( $string, $quote_style );
127 if ( !function_exists('json_encode') ) {
128 function json_encode( $string ) {
131 if ( !is_a($wp_json, 'Services_JSON') ) {
132 require_once( ABSPATH . WPINC . '/class-json.php' );
133 $wp_json = new Services_JSON();
136 return $wp_json->encodeUnsafe( $string );
140 if ( !function_exists('json_decode') ) {
141 function json_decode( $string, $assoc_array = false ) {
144 if ( !is_a($wp_json, 'Services_JSON') ) {
145 require_once( ABSPATH . WPINC . '/class-json.php' );
146 $wp_json = new Services_JSON();
149 $res = $wp_json->decode( $string );
151 $res = _json_decode_object_helper( $res );
154 function _json_decode_object_helper($data) {
155 if ( is_object($data) )
156 $data = get_object_vars($data);
157 return is_array($data) ? array_map(__FUNCTION__, $data) : $data;
161 // pathinfo that fills 'filename' without extension like in PHP 5.2+
162 function pathinfo52($path) {
163 $parts = pathinfo($path);
164 if ( !isset($parts['filename']) ) {
165 $parts['filename'] = substr( $parts['basename'], 0, strrpos($parts['basename'], '.') );
166 if ( empty($parts['filename']) ) // there's no extension
167 $parts['filename'] = $parts['basename'];