3 * WordPress implementation for PHP functions either missing from older PHP versions or not included by default.
9 // If gettext isn't available
10 if ( !function_exists('_') ) {
16 if ( !function_exists('mb_substr') ):
17 function mb_substr( $str, $start, $length=null, $encoding=null ) {
18 return _mb_substr($str, $start, $length, $encoding);
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);
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 );
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);
41 function _hash_hmac($algo, $data, $key, $raw_output = false) {
42 $packs = array('md5' => 'H32', 'sha1' => 'H40');
44 if ( !isset($packs[$algo]) )
47 $pack = $packs[$algo];
49 if (strlen($key) > 64)
50 $key = pack($pack, $algo($key));
52 $key = str_pad($key, 64, chr(0));
54 $ipad = (substr($key, 0, 64) ^ str_repeat(chr(0x36), 64));
55 $opad = (substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64));
57 $hmac = $algo($opad . pack($pack, $algo($ipad . $data)));
60 return pack( $pack, $hmac );