wp/wp-includes/compat.php
changeset 0 d970ebf37754
child 5 5e2f62d02dcd
equal deleted inserted replaced
-1:000000000000 0:d970ebf37754
       
     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, so in case of a different
       
    24 	// 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('hash_hmac') ):
       
    36 function hash_hmac($algo, $data, $key, $raw_output = false) {
       
    37 	return _hash_hmac($algo, $data, $key, $raw_output);
       
    38 }
       
    39 endif;
       
    40 
       
    41 function _hash_hmac($algo, $data, $key, $raw_output = false) {
       
    42 	$packs = array('md5' => 'H32', 'sha1' => 'H40');
       
    43 
       
    44 	if ( !isset($packs[$algo]) )
       
    45 		return false;
       
    46 
       
    47 	$pack = $packs[$algo];
       
    48 
       
    49 	if (strlen($key) > 64)
       
    50 		$key = pack($pack, $algo($key));
       
    51 
       
    52 	$key = str_pad($key, 64, chr(0));
       
    53 
       
    54 	$ipad = (substr($key, 0, 64) ^ str_repeat(chr(0x36), 64));
       
    55 	$opad = (substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64));
       
    56 
       
    57 	$hmac = $algo($opad . pack($pack, $algo($ipad . $data)));
       
    58 
       
    59 	if ( $raw_output )
       
    60 		return pack( $pack, $hmac );
       
    61 	return $hmac;
       
    62 }
       
    63 
       
    64 if ( !function_exists('json_encode') ) {
       
    65 	function json_encode( $string ) {
       
    66 		global $wp_json;
       
    67 
       
    68 		if ( !is_a($wp_json, 'Services_JSON') ) {
       
    69 			require_once( ABSPATH . WPINC . '/class-json.php' );
       
    70 			$wp_json = new Services_JSON();
       
    71 		}
       
    72 
       
    73 		return $wp_json->encodeUnsafe( $string );
       
    74 	}
       
    75 }
       
    76 
       
    77 if ( !function_exists('json_decode') ) {
       
    78 	function json_decode( $string, $assoc_array = false ) {
       
    79 		global $wp_json;
       
    80 
       
    81 		if ( !is_a($wp_json, 'Services_JSON') ) {
       
    82 			require_once( ABSPATH . WPINC . '/class-json.php' );
       
    83 			$wp_json = new Services_JSON();
       
    84 		}
       
    85 
       
    86 		$res = $wp_json->decode( $string );
       
    87 		if ( $assoc_array )
       
    88 			$res = _json_decode_object_helper( $res );
       
    89 		return $res;
       
    90 	}
       
    91 	function _json_decode_object_helper($data) {
       
    92 		if ( is_object($data) )
       
    93 			$data = get_object_vars($data);
       
    94 		return is_array($data) ? array_map(__FUNCTION__, $data) : $data;
       
    95 	}
       
    96 }