]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/compat.php
Wordpress 3.2
[autoinstalls/wordpress.git] / wp-includes / compat.php
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 }