4 * Compatibility functions when either missing from older PHP versions or not included by default
8 * json_encode for PHP prior to 5.2
11 if( !function_exists( 'json_encode' ) ) {
12 function json_encode( $array ) {
13 return yourls_array_to_json( $array );
18 * Converts an associative array of arbitrary depth and dimension into JSON representation. Used for compatibility with older PHP builds.
20 * @param array $array the array to convert.
21 * @return mixed The resulting JSON string, or false if the argument was not an array.
22 * @author Andy Rusterholz
23 * @link http://php.net/json_encode (see comments)
25 function yourls_array_to_json( $array ){
27 if( !is_array( $array ) ){
31 $associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
35 foreach( $array as $key => $value ){
37 // We first copy each key/value pair into a staging array,
38 // formatting each key and value properly as we go.
41 if( is_numeric( $key ) ){
44 $key = '"'.addslashes( $key ).'"';
47 if( is_array( $value )){
48 $value = yourls_array_to_json( $value );
49 } else if( !is_numeric( $value ) || is_string( $value ) ){
50 $value = '"'.addslashes( $value ).'"';
53 // Add to staging array:
54 $construct[] = "$key: $value";
57 // Then we collapse the staging array into the JSON form:
58 $result = "{ " . implode( ", ", $construct ) . " }";
60 } else { // If the array is a vector (not associative):
63 foreach( $array as $value ){
66 if( is_array( $value )){
67 $value = yourls_array_to_json( $value );
68 } else if( !is_numeric( $value ) || is_string( $value ) ){
69 $value = '"'.addslashes($value).'"';
72 // Add to staging array:
73 $construct[] = $value;
76 // Then we collapse the staging array into the JSON form:
77 $result = "[ " . implode( ", ", $construct ) . " ]";
84 * Compat http_build_query for PHP4
87 if ( !function_exists( 'http_build_query' ) ) {
88 function http_build_query( $data, $prefix=null, $sep=null ) {
89 return yourls_http_build_query( $data, $prefix, $sep );
94 * Compat http_build_query for PHP4. Stolen from WP.
96 * from php.net (modified by Mark Jaquith to behave like the native PHP5 function)
99 function yourls_http_build_query( $data, $prefix=null, $sep=null, $key='', $urlencode=true ) {
102 foreach ( (array) $data as $k => $v ) {
104 $k = urlencode( $k );
105 if ( is_int($k) && $prefix != null )
107 if ( !empty( $key ) )
108 $k = $key . '%5B' . $k . '%5D';
111 elseif ( $v === FALSE )
114 if ( is_array( $v ) || is_object( $v ) )
115 array_push( $ret,yourls_http_build_query( $v, '', $sep, $k, $urlencode ) );
116 elseif ( $urlencode )
117 array_push( $ret, $k.'='.urlencode( $v ) );
119 array_push( $ret, $k.'='.$v );
123 $sep = ini_get( 'arg_separator.output' );
125 return implode( $sep, $ret );
129 * htmlspecialchars_decode for PHP < 5.1
132 if ( !function_exists( 'htmlspecialchars_decode' ) ) {
133 function htmlspecialchars_decode( $text ) {
134 return strtr( $text, array_flip( get_html_translation_table( HTML_SPECIALCHARS ) ) );
139 * BC Math functions (assuming if one doesn't exist, none does)
142 if ( !function_exists( 'bcdiv' ) ) {
143 function bcdiv( $dividend, $divisor ) {
144 $quotient = floor( $dividend/$divisor );
147 function bcmod( $dividend, $modulo ) {
148 $remainder = $dividend%$modulo;
151 function bcmul( $left, $right ) {
152 return $left * $right;
154 function bcadd( $left, $right ) {
155 return $left + $right;
157 function bcpow( $base, $power ) {
158 return pow( $base, $power );
163 * Replacement for property_exists() (5.1+)
166 if ( !function_exists( 'property_exists' ) ) {
167 function property_exists( $class, $property ) {
168 if ( is_object( $class ) ) {
169 $vars = get_object_vars( $class );
171 $vars = get_class_vars( $class );
173 return array_key_exists( $property, $vars );