]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-compat.php
Merge pull request #1894 from YOURLS/atlowercase
[Github/YOURLS.git] / includes / functions-compat.php
1 <?php
2 /*
3  * YOURLS
4  * Compatibility functions when either missing from older PHP versions or not included by default
5  */
6
7 /**
8  * json_encode for PHP, should someone run a distro without php-json -- see http://askubuntu.com/questions/361424/
9  *
10  */
11 if( !function_exists( 'json_encode' ) ) {
12         function json_encode( $array ) {
13                 return yourls_array_to_json( $array );
14         }
15 }
16
17 /**
18  * Converts an associative array of arbitrary depth and dimension into JSON representation. Used for compatibility with older PHP builds.
19  *
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)
24  */
25 function yourls_array_to_json( $array ){
26
27         if( !is_array( $array ) ){
28                 return false;
29         }
30
31         $associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
32         if( $associative ){
33
34                 $construct = array();
35                 foreach( $array as $key => $value ){
36
37                         // We first copy each key/value pair into a staging array,
38                         // formatting each key and value properly as we go.
39
40                         // Format the key:
41                         if( is_numeric( $key ) ){
42                                 $key = "key_$key";
43                         }
44                         $key = '"'.addslashes( $key ).'"';
45
46                         // Format the value:
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 ).'"';
51                         }
52
53                         // Add to staging array:
54                         $construct[] = "$key: $value";
55                 }
56
57                 // Then we collapse the staging array into the JSON form:
58                 $result = "{ " . implode( ", ", $construct ) . " }";
59
60         } else { // If the array is a vector (not associative):
61
62                 $construct = array();
63                 foreach( $array as $value ){
64
65                         // Format the 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).'"';
70                         }
71
72                         // Add to staging array:
73                         $construct[] = $value;
74                 }
75
76                 // Then we collapse the staging array into the JSON form:
77                 $result = "[ " . implode( ", ", $construct ) . " ]";
78         }
79
80         return $result;
81 }
82
83
84 /**
85  * BC Math functions (assuming if one doesn't exist, none does)
86  *
87  */
88 if ( !function_exists( 'bcdiv' ) ) {
89         function bcdiv( $dividend, $divisor ) {
90                 $quotient = floor( $dividend/$divisor );
91                 return $quotient;
92         }
93         function bcmod( $dividend, $modulo ) {
94                 $remainder = $dividend%$modulo;
95                 return $remainder;
96         }
97         function bcmul( $left, $right ) {
98                 return $left * $right;
99         }
100         function bcadd( $left, $right ) {
101                 return $left + $right;
102         }
103         function bcpow( $base, $power ) {
104                 return pow( $base, $power );
105         }
106 }
107
108
109 /**
110  * http_build_url compatibility function
111  *
112  * @since 1.7.1
113  */
114 if ( !function_exists( 'http_build_url' ) ) {
115     include YOURLS_INC . '/http_build_url/http_build_url.php';
116 }