author | Anthony Ly <anthonyly.com@gmail.com> |
Tue, 12 Mar 2013 18:21:39 +0100 | |
changeset 206 | 919b4ddb13fa |
parent 194 | 32102edaa81b |
permissions | -rw-r--r-- |
136 | 1 |
<?php |
2 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
3 |
* WordPress implementation for PHP functions either missing from older PHP versions or not included by default. |
136 | 4 |
* |
5 |
* @package PHP |
|
6 |
* @access private |
|
7 |
*/ |
|
8 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
9 |
// If gettext isn't available |
136 | 10 |
if ( !function_exists('_') ) { |
11 |
function _($string) { |
|
12 |
return $string; |
|
13 |
} |
|
14 |
} |
|
15 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
16 |
if ( !function_exists('mb_substr') ): |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
17 |
function mb_substr( $str, $start, $length=null, $encoding=null ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
18 |
return _mb_substr($str, $start, $length, $encoding); |
136 | 19 |
} |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
20 |
endif; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
21 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
22 |
function _mb_substr( $str, $start, $length=null, $encoding=null ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
23 |
// the solution below, works only for utf-8, so in case of a different |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
24 |
// charset, just use built-in substr |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
25 |
$charset = get_option( 'blog_charset' ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
26 |
if ( !in_array( $charset, array('utf8', 'utf-8', 'UTF8', 'UTF-8') ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
27 |
return is_null( $length )? substr( $str, $start ) : substr( $str, $start, $length); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
28 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
29 |
// use the regex unicode support to separate the UTF-8 characters into an array |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
30 |
preg_match_all( '/./us', $str, $match ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
31 |
$chars = is_null( $length )? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
32 |
return implode( '', $chars ); |
136 | 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') ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
69 |
require_once( ABSPATH . WPINC . '/class-json.php' ); |
136 | 70 |
$wp_json = new Services_JSON(); |
71 |
} |
|
72 |
||
73 |
return $wp_json->encodeUnsafe( $string ); |
|
74 |
} |
|
75 |
} |
|
76 |
||
77 |
if ( !function_exists('json_decode') ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
78 |
function json_decode( $string, $assoc_array = false ) { |
136 | 79 |
global $wp_json; |
80 |
||
81 |
if ( !is_a($wp_json, 'Services_JSON') ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
82 |
require_once( ABSPATH . WPINC . '/class-json.php' ); |
136 | 83 |
$wp_json = new Services_JSON(); |
84 |
} |
|
85 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
86 |
$res = $wp_json->decode( $string ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
87 |
if ( $assoc_array ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
88 |
$res = _json_decode_object_helper( $res ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
89 |
return $res; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
90 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
91 |
function _json_decode_object_helper($data) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
92 |
if ( is_object($data) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
93 |
$data = get_object_vars($data); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
94 |
return is_array($data) ? array_map(__FUNCTION__, $data) : $data; |
136 | 95 |
} |
96 |
} |