]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-compat.php
Merge branch 'master' into http-lib
[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 prior to 5.2
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  * Compat http_build_query for PHP4
85  *
86  */
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 );
90         }
91 }
92
93 /**
94  * Compat http_build_query for PHP4. Stolen from WP.
95  *
96  * from php.net (modified by Mark Jaquith to behave like the native PHP5 function)
97  * 
98  */
99 function yourls_http_build_query( $data, $prefix=null, $sep=null, $key='', $urlencode=true ) {
100         $ret = array();
101
102         foreach ( (array) $data as $k => $v ) {
103                 if ( $urlencode)
104                         $k = urlencode( $k );
105                 if ( is_int($k) && $prefix != null )
106                         $k = $prefix.$k;
107                 if ( !empty( $key ) )
108                         $k = $key . '%5B' . $k . '%5D';
109                 if ( $v === NULL )
110                         continue;
111                 elseif ( $v === FALSE )
112                         $v = '0';
113
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 ) );
118                 else
119                         array_push( $ret, $k.'='.$v );
120         }
121
122         if ( NULL === $sep )
123                 $sep = ini_get( 'arg_separator.output' );
124
125         return implode( $sep, $ret );
126 }
127
128 /**
129  * htmlspecialchars_decode for PHP < 5.1
130  *
131  */
132 if ( !function_exists( 'htmlspecialchars_decode' ) ) {
133         function htmlspecialchars_decode( $text ) {
134                 return strtr( $text, array_flip( get_html_translation_table( HTML_SPECIALCHARS ) ) );
135         }
136 }
137
138 /**
139  * BC Math functions (assuming if one doesn't exist, none does)
140  *
141  */
142 if ( !function_exists( 'bcdiv' ) ) {
143         function bcdiv( $dividend, $divisor ) {
144                 $quotient = floor( $dividend/$divisor );
145                 return $quotient;
146         }
147         function bcmod( $dividend, $modulo ) {
148                 $remainder = $dividend%$modulo;
149                 return $remainder;
150         }
151         function bcmul( $left, $right ) {
152                 return $left * $right;
153         }
154         function bcadd( $left, $right ) {
155                 return $left + $right;
156         }
157         function bcpow( $base, $power ) {
158                 return pow( $base, $power );
159         }
160 }
161
162 /**
163  * Replacement for property_exists() (5.1+)
164  *
165  */
166 if ( !function_exists( 'property_exists' ) ) {
167         function property_exists( $class, $property ) {
168                 if ( is_object( $class ) ) {
169                         $vars = get_object_vars( $class );
170                 } else {
171                         $vars = get_class_vars( $class );
172                 }
173                 return array_key_exists( $property, $vars );
174         }
175 }