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