web/wp-includes/compat.php
changeset 1 0d28b7c10758
equal deleted inserted replaced
0:0d9a58d2c515 1:0d28b7c10758
       
     1 <?php
       
     2 /**
       
     3  * WordPress implementation for PHP functions missing from older PHP versions.
       
     4  *
       
     5  * @package PHP
       
     6  * @access private
       
     7  */
       
     8 
       
     9 // Added in PHP 5.0
       
    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('_') ) {
       
    48 	function _($string) {
       
    49 		return $string;
       
    50 	}
       
    51 }
       
    52 
       
    53 if (!function_exists('stripos')) {
       
    54 	function stripos($haystack, $needle, $offset = 0) {
       
    55 		return strpos(strtolower($haystack), strtolower($needle), $offset);
       
    56 	}
       
    57 }
       
    58 
       
    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);
       
    62 }
       
    63 endif;
       
    64 
       
    65 function _hash_hmac($algo, $data, $key, $raw_output = false) {
       
    66 	$packs = array('md5' => 'H32', 'sha1' => 'H40');
       
    67 
       
    68 	if ( !isset($packs[$algo]) )
       
    69 		return false;
       
    70 
       
    71 	$pack = $packs[$algo];
       
    72 
       
    73 	if (strlen($key) > 64)
       
    74 		$key = pack($pack, $algo($key));
       
    75 
       
    76 	$key = str_pad($key, 64, chr(0));
       
    77 
       
    78 	$ipad = (substr($key, 0, 64) ^ str_repeat(chr(0x36), 64));
       
    79 	$opad = (substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64));
       
    80 
       
    81 	$hmac = $algo($opad . pack($pack, $algo($ipad . $data)));
       
    82 
       
    83 	if ( $raw_output )
       
    84 		return pack( $pack, $hmac );
       
    85 	return $hmac;
       
    86 }
       
    87 
       
    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') ) {
       
   128 	function json_encode( $string ) {
       
   129 		global $wp_json;
       
   130 
       
   131 		if ( !is_a($wp_json, 'Services_JSON') ) {
       
   132 			require_once( 'class-json.php' );
       
   133 			$wp_json = new Services_JSON();
       
   134 		}
       
   135 
       
   136 		return $wp_json->encodeUnsafe( $string );
       
   137 	}
       
   138 }
       
   139 
       
   140 if ( !function_exists('json_decode') ) {
       
   141 	function json_decode( $string ) {
       
   142 		global $wp_json;
       
   143 
       
   144 		if ( !is_a($wp_json, 'Services_JSON') ) {
       
   145 			require_once( 'class-json.php' );
       
   146 			$wp_json = new Services_JSON();
       
   147 		}
       
   148 
       
   149 		return $wp_json->decode( $string );
       
   150 	}
       
   151 }
       
   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 }