web/wp-includes/compat.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
     1 <?php
     1 <?php
     2 /**
     2 /**
     3  * WordPress implementation for PHP functions missing from older PHP versions.
     3  * WordPress implementation for PHP functions either missing from older PHP versions or not included by default.
     4  *
     4  *
     5  * @package PHP
     5  * @package PHP
     6  * @access private
     6  * @access private
     7  */
     7  */
     8 
     8 
     9 // Added in PHP 5.0
     9 // If gettext isn't available
    10 
       
    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);
       
    14 	}
       
    15 }
       
    16 
       
    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) {
       
    19 	$ret = array();
       
    20 
       
    21 	foreach ( (array) $data as $k => $v ) {
       
    22 		if ( $urlencode)
       
    23 			$k = urlencode($k);
       
    24 		if ( is_int($k) && $prefix != null )
       
    25 			$k = $prefix.$k;
       
    26 		if ( !empty($key) )
       
    27 			$k = $key . '%5B' . $k . '%5D';
       
    28 		if ( $v === NULL )
       
    29 			continue;
       
    30 		elseif ( $v === FALSE )
       
    31 			$v = '0';
       
    32 
       
    33 		if ( is_array($v) || is_object($v) )
       
    34 			array_push($ret,_http_build_query($v, '', $sep, $k, $urlencode));
       
    35 		elseif ( $urlencode )
       
    36 			array_push($ret, $k.'='.urlencode($v));
       
    37 		else
       
    38 			array_push($ret, $k.'='.$v);
       
    39 	}
       
    40 
       
    41 	if ( NULL === $sep )
       
    42 		$sep = ini_get('arg_separator.output');
       
    43 
       
    44 	return implode($sep, $ret);
       
    45 }
       
    46 
       
    47 if ( !function_exists('_') ) {
    10 if ( !function_exists('_') ) {
    48 	function _($string) {
    11 	function _($string) {
    49 		return $string;
    12 		return $string;
    50 	}
    13 	}
    51 }
    14 }
    52 
    15 
    53 if (!function_exists('stripos')) {
    16 if ( !function_exists('mb_substr') ):
    54 	function stripos($haystack, $needle, $offset = 0) {
    17 	function mb_substr( $str, $start, $length=null, $encoding=null ) {
    55 		return strpos(strtolower($haystack), strtolower($needle), $offset);
    18 		return _mb_substr($str, $start, $length, $encoding);
    56 	}
    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 );
    57 }
    33 }
    58 
    34 
    59 if ( !function_exists('hash_hmac') ):
    35 if ( !function_exists('hash_hmac') ):
    60 function hash_hmac($algo, $data, $key, $raw_output = false) {
    36 function hash_hmac($algo, $data, $key, $raw_output = false) {
    61 	return _hash_hmac($algo, $data, $key, $raw_output);
    37 	return _hash_hmac($algo, $data, $key, $raw_output);
    83 	if ( $raw_output )
    59 	if ( $raw_output )
    84 		return pack( $pack, $hmac );
    60 		return pack( $pack, $hmac );
    85 	return $hmac;
    61 	return $hmac;
    86 }
    62 }
    87 
    63 
    88 if ( !function_exists('mb_substr') ):
       
    89 	function mb_substr( $str, $start, $length=null, $encoding=null ) {
       
    90 		return _mb_substr($str, $start, $length, $encoding);
       
    91 	}
       
    92 endif;
       
    93 
       
    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);
       
   100 	}
       
   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 );
       
   105 }
       
   106 
       
   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 )
       
   111 	{
       
   112 		if ( !is_scalar( $string ) ) {
       
   113 			trigger_error( 'htmlspecialchars_decode() expects parameter 1 to be string, ' . gettype( $string ) . ' given', E_USER_WARNING );
       
   114 			return;
       
   115 		}
       
   116 
       
   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 );
       
   119 			return;
       
   120 		}
       
   121 
       
   122 		return wp_specialchars_decode( $string, $quote_style );
       
   123 	}
       
   124 }
       
   125 
       
   126 // For PHP < 5.2.0
       
   127 if ( !function_exists('json_encode') ) {
    64 if ( !function_exists('json_encode') ) {
   128 	function json_encode( $string ) {
    65 	function json_encode( $string ) {
   129 		global $wp_json;
    66 		global $wp_json;
   130 
    67 
   131 		if ( !is_a($wp_json, 'Services_JSON') ) {
    68 		if ( !is_a($wp_json, 'Services_JSON') ) {
   132 			require_once( 'class-json.php' );
    69 			require_once( ABSPATH . WPINC . '/class-json.php' );
   133 			$wp_json = new Services_JSON();
    70 			$wp_json = new Services_JSON();
   134 		}
    71 		}
   135 
    72 
   136 		return $wp_json->encodeUnsafe( $string );
    73 		return $wp_json->encodeUnsafe( $string );
   137 	}
    74 	}
   138 }
    75 }
   139 
    76 
   140 if ( !function_exists('json_decode') ) {
    77 if ( !function_exists('json_decode') ) {
   141 	function json_decode( $string ) {
    78 	function json_decode( $string, $assoc_array = false ) {
   142 		global $wp_json;
    79 		global $wp_json;
   143 
    80 
   144 		if ( !is_a($wp_json, 'Services_JSON') ) {
    81 		if ( !is_a($wp_json, 'Services_JSON') ) {
   145 			require_once( 'class-json.php' );
    82 			require_once( ABSPATH . WPINC . '/class-json.php' );
   146 			$wp_json = new Services_JSON();
    83 			$wp_json = new Services_JSON();
   147 		}
    84 		}
   148 
    85 
   149 		return $wp_json->decode( $string );
    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;
   150 	}
    95 	}
   151 }
    96 }
   152 
       
   153 // pathinfo that fills 'filename' without extension like in PHP 5.2+
       
   154 function pathinfo52($path) {
       
   155 	$parts = pathinfo($path);
       
   156 	if ( !isset($parts['filename']) ) {
       
   157 		$parts['filename'] = substr( $parts['basename'], 0, strrpos($parts['basename'], '.') );
       
   158 		if ( empty($parts['filename']) ) // there's no extension
       
   159 			$parts['filename'] = $parts['basename'];
       
   160 	}
       
   161 	return $parts;
       
   162 }