4 * Compatibility functions when either missing from older PHP versions or not included by default
8 * json_encode for PHP, should someone run a distro without php-json -- see http://askubuntu.com/questions/361424/
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 ) . " ]";
85 * BC Math functions (assuming if one doesn't exist, none does)
88 if ( !function_exists( 'bcdiv' ) ) {
89 function bcdiv( $dividend, $divisor ) {
90 $quotient = floor( $dividend/$divisor );
93 function bcmod( $dividend, $modulo ) {
94 $remainder = $dividend%$modulo;
97 function bcmul( $left, $right ) {
98 return $left * $right;
100 function bcadd( $left, $right ) {
101 return $left + $right;
103 function bcpow( $base, $power ) {
104 return pow( $base, $power );
110 * http_build_url compatibility function
114 if ( !function_exists( 'http_build_url' ) ) {
115 include YOURLS_INC . '/http_build_url/http_build_url.php';