3 // json_encode for PHP prior to 5.2
\r
4 if( !function_exists( 'json_encode' ) ) {
\r
5 function json_encode( $array ) {
\r
6 return yourls_array_to_json( $array );
\r
11 * Converts an associative array of arbitrary depth and dimension into JSON representation. Used for compatibility with older PHP builds.
\r
13 * @param $array The array to convert.
\r
14 * @return mixed The resulting JSON string, or false if the argument was not an array.
\r
15 * @author Andy Rusterholz
\r
16 * @link http://php.net/json_encode (see comments)
\r
18 function yourls_array_to_json( $array ){
\r
20 if( !is_array( $array ) ){
\r
24 $associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
\r
27 $construct = array();
\r
28 foreach( $array as $key => $value ){
\r
30 // We first copy each key/value pair into a staging array,
\r
31 // formatting each key and value properly as we go.
\r
34 if( is_numeric( $key ) ){
\r
37 $key = '"'.addslashes( $key ).'"';
\r
39 // Format the value:
\r
40 if( is_array( $value )){
\r
41 $value = yourls_array_to_json( $value );
\r
42 } else if( !is_numeric( $value ) || is_string( $value ) ){
\r
43 $value = '"'.addslashes( $value ).'"';
\r
46 // Add to staging array:
\r
47 $construct[] = "$key: $value";
\r
50 // Then we collapse the staging array into the JSON form:
\r
51 $result = "{ " . implode( ", ", $construct ) . " }";
\r
53 } else { // If the array is a vector (not associative):
\r
55 $construct = array();
\r
56 foreach( $array as $value ){
\r
58 // Format the value:
\r
59 if( is_array( $value )){
\r
60 $value = yourls_array_to_json( $value );
\r
61 } else if( !is_numeric( $value ) || is_string( $value ) ){
\r
62 $value = '"'.addslashes($value).'"';
\r
65 // Add to staging array:
\r
66 $construct[] = $value;
\r
69 // Then we collapse the staging array into the JSON form:
\r
70 $result = "[ " . implode( ", ", $construct ) . " ]";
\r
76 // Compat http_build_query for PHP4
\r
77 if ( !function_exists( 'http_build_query' ) ) {
\r
78 function http_build_query( $data, $prefix=null, $sep=null ) {
\r
79 return yourls_http_build_query( $data, $prefix, $sep );
\r
83 // from php.net (modified by Mark Jaquith to behave like the native PHP5 function)
\r
84 function yourls_http_build_query( $data, $prefix=null, $sep=null, $key='', $urlencode=true ) {
\r
87 foreach ( (array) $data as $k => $v ) {
\r
89 $k = urlencode( $k );
\r
90 if ( is_int($k) && $prefix != null )
\r
92 if ( !empty( $key ) )
\r
93 $k = $key . '%5B' . $k . '%5D';
\r
96 elseif ( $v === FALSE )
\r
99 if ( is_array( $v ) || is_object( $v ) )
\r
100 array_push( $ret,yourls_http_build_query( $v, '', $sep, $k, $urlencode ) );
\r
101 elseif ( $urlencode )
\r
102 array_push( $ret, $k.'='.urlencode( $v ) );
\r
104 array_push( $ret, $k.'='.$v );
\r
107 if ( NULL === $sep )
\r
108 $sep = ini_get( 'arg_separator.output' );
\r
110 return implode( $sep, $ret );
\r
113 // htmlspecialchars_decode for PHP < 5.1
\r
114 if ( !function_exists( 'htmlspecialchars_decode' ) ) {
\r
115 function htmlspecialchars_decode( $text ) {
\r
116 return strtr( $text, array_flip( get_html_translation_table( HTML_SPECIALCHARS ) ) );
\r
120 // BC Math functions (assuming if one doesn't exist, none does)
\r
121 if ( !function_exists( 'bcdiv' ) ) {
\r
122 function bcdiv( $dividend, $divisor ) {
\r
123 $quotient = floor( $dividend/$divisor );
\r
126 function bcmod( $dividend, $modulo ) {
\r
127 $remainder = $dividend%$modulo;
\r
130 function bcmul( $left, $right ) {
\r
131 return $left * $right;
\r
133 function bcadd( $left, $right ) {
\r
134 return $left + $right;
\r
136 function bcpow( $base, $power ) {
\r
137 return pow( $base, $power );
\r
141 // Replacement for property_exists() (5.1+)
\r
142 if ( !function_exists( 'property_exists' ) ) {
\r
143 function property_exists( $class, $property ) {
\r
144 if ( is_object( $class ) ) {
\r
145 $vars = get_object_vars( $class );
\r
147 $vars = get_class_vars( $class );
\r
149 return array_key_exists( $property, $vars );
\r