4 * json_encode for PHP prior to 5.2
7 if( !function_exists( 'json_encode' ) ) {
8 function json_encode( $array ) {
9 return yourls_array_to_json( $array );
14 * Converts an associative array of arbitrary depth and dimension into JSON representation. Used for compatibility with older PHP builds.
16 * @param $array The array to convert.
17 * @return mixed The resulting JSON string, or false if the argument was not an array.
18 * @author Andy Rusterholz
19 * @link http://php.net/json_encode (see comments)
21 function yourls_array_to_json( $array ){
23 if( !is_array( $array ) ){
27 $associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
31 foreach( $array as $key => $value ){
33 // We first copy each key/value pair into a staging array,
34 // formatting each key and value properly as we go.
37 if( is_numeric( $key ) ){
40 $key = '"'.addslashes( $key ).'"';
43 if( is_array( $value )){
44 $value = yourls_array_to_json( $value );
45 } else if( !is_numeric( $value ) || is_string( $value ) ){
46 $value = '"'.addslashes( $value ).'"';
49 // Add to staging array:
50 $construct[] = "$key: $value";
53 // Then we collapse the staging array into the JSON form:
54 $result = "{ " . implode( ", ", $construct ) . " }";
56 } else { // If the array is a vector (not associative):
59 foreach( $array as $value ){
62 if( is_array( $value )){
63 $value = yourls_array_to_json( $value );
64 } else if( !is_numeric( $value ) || is_string( $value ) ){
65 $value = '"'.addslashes($value).'"';
68 // Add to staging array:
69 $construct[] = $value;
72 // Then we collapse the staging array into the JSON form:
73 $result = "[ " . implode( ", ", $construct ) . " ]";
80 * Compat http_build_query for PHP4
83 if ( !function_exists( 'http_build_query' ) ) {
84 function http_build_query( $data, $prefix=null, $sep=null ) {
85 return yourls_http_build_query( $data, $prefix, $sep );
90 * Compat http_build_query for PHP4. Stolen from WP.
92 * from php.net (modified by Mark Jaquith to behave like the native PHP5 function)
95 function yourls_http_build_query( $data, $prefix=null, $sep=null, $key='', $urlencode=true ) {
98 foreach ( (array) $data as $k => $v ) {
100 $k = urlencode( $k );
101 if ( is_int($k) && $prefix != null )
103 if ( !empty( $key ) )
104 $k = $key . '%5B' . $k . '%5D';
107 elseif ( $v === FALSE )
110 if ( is_array( $v ) || is_object( $v ) )
111 array_push( $ret,yourls_http_build_query( $v, '', $sep, $k, $urlencode ) );
112 elseif ( $urlencode )
113 array_push( $ret, $k.'='.urlencode( $v ) );
115 array_push( $ret, $k.'='.$v );
119 $sep = ini_get( 'arg_separator.output' );
121 return implode( $sep, $ret );
125 * htmlspecialchars_decode for PHP < 5.1
128 if ( !function_exists( 'htmlspecialchars_decode' ) ) {
129 function htmlspecialchars_decode( $text ) {
130 return strtr( $text, array_flip( get_html_translation_table( HTML_SPECIALCHARS ) ) );
135 * BC Math functions (assuming if one doesn't exist, none does)
138 if ( !function_exists( 'bcdiv' ) ) {
139 function bcdiv( $dividend, $divisor ) {
140 $quotient = floor( $dividend/$divisor );
143 function bcmod( $dividend, $modulo ) {
144 $remainder = $dividend%$modulo;
147 function bcmul( $left, $right ) {
148 return $left * $right;
150 function bcadd( $left, $right ) {
151 return $left + $right;
153 function bcpow( $base, $power ) {
154 return pow( $base, $power );
159 * Replacement for property_exists() (5.1+)
162 if ( !function_exists( 'property_exists' ) ) {
163 function property_exists( $class, $property ) {
164 if ( is_object( $class ) ) {
165 $vars = get_object_vars( $class );
167 $vars = get_class_vars( $class );
169 return array_key_exists( $property, $vars );