]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-compat.php
Merge pull request #1336 from LeoColomb/Fix-Github
[Github/YOURLS.git] / includes / functions-compat.php
1 <?php
2
3 /**
4  * json_encode for PHP prior to 5.2
5  *
6  */
7 if( !function_exists( 'json_encode' ) ) {
8         function json_encode( $array ) {
9                 return yourls_array_to_json( $array );
10         }
11 }
12
13 /**
14  * Converts an associative array of arbitrary depth and dimension into JSON representation. Used for compatibility with older PHP builds.
15  *
16  * @param $array The array to convert.
17  * @return mixed The resulting JSON string, or false if the argument was not an array.
18  * @author Andy Rusterholz
19  * @link http://php.net/json_encode (see comments)
20  */
21 function yourls_array_to_json( $array ){
22
23     if( !is_array( $array ) ){
24         return false;
25     }
26
27     $associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
28     if( $associative ){
29
30         $construct = array();
31         foreach( $array as $key => $value ){
32
33             // We first copy each key/value pair into a staging array,
34             // formatting each key and value properly as we go.
35
36             // Format the key:
37             if( is_numeric( $key ) ){
38                 $key = "key_$key";
39             }
40             $key = '"'.addslashes( $key ).'"';
41
42             // Format the value:
43             if( is_array( $value )){
44                 $value = yourls_array_to_json( $value );
45             } else if( !is_numeric( $value ) || is_string( $value ) ){
46                 $value = '"'.addslashes( $value ).'"';
47             }
48
49             // Add to staging array:
50             $construct[] = "$key: $value";
51         }
52
53         // Then we collapse the staging array into the JSON form:
54         $result = "{ " . implode( ", ", $construct ) . " }";
55
56     } else { // If the array is a vector (not associative):
57
58         $construct = array();
59         foreach( $array as $value ){
60
61             // Format the value:
62             if( is_array( $value )){
63                 $value = yourls_array_to_json( $value );
64             } else if( !is_numeric( $value ) || is_string( $value ) ){
65                 $value = '"'.addslashes($value).'"';
66             }
67
68             // Add to staging array:
69             $construct[] = $value;
70         }
71
72         // Then we collapse the staging array into the JSON form:
73         $result = "[ " . implode( ", ", $construct ) . " ]";
74     }
75
76     return $result;
77 }
78
79 /**
80  * Compat http_build_query for PHP4
81  *
82  */
83 if ( !function_exists( 'http_build_query' ) ) {
84         function http_build_query( $data, $prefix=null, $sep=null ) {
85                 return yourls_http_build_query( $data, $prefix, $sep );
86         }
87 }
88
89 /**
90  * Compat http_build_query for PHP4. Stolen from WP.
91  *
92  * from php.net (modified by Mark Jaquith to behave like the native PHP5 function)
93  * 
94  */
95 function yourls_http_build_query( $data, $prefix=null, $sep=null, $key='', $urlencode=true ) {
96         $ret = array();
97
98         foreach ( (array) $data as $k => $v ) {
99                 if ( $urlencode)
100                         $k = urlencode( $k );
101                 if ( is_int($k) && $prefix != null )
102                         $k = $prefix.$k;
103                 if ( !empty( $key ) )
104                         $k = $key . '%5B' . $k . '%5D';
105                 if ( $v === NULL )
106                         continue;
107                 elseif ( $v === FALSE )
108                         $v = '0';
109
110                 if ( is_array( $v ) || is_object( $v ) )
111                         array_push( $ret,yourls_http_build_query( $v, '', $sep, $k, $urlencode ) );
112                 elseif ( $urlencode )
113                         array_push( $ret, $k.'='.urlencode( $v ) );
114                 else
115                         array_push( $ret, $k.'='.$v );
116         }
117
118         if ( NULL === $sep )
119                 $sep = ini_get( 'arg_separator.output' );
120
121         return implode( $sep, $ret );
122 }
123
124 /**
125  * htmlspecialchars_decode for PHP < 5.1
126  *
127  */
128 if ( !function_exists( 'htmlspecialchars_decode' ) ) {
129         function htmlspecialchars_decode( $text ) {
130                 return strtr( $text, array_flip( get_html_translation_table( HTML_SPECIALCHARS ) ) );
131         }
132 }
133
134 /**
135  * BC Math functions (assuming if one doesn't exist, none does)
136  *
137  */
138 if ( !function_exists( 'bcdiv' ) ) {
139         function bcdiv( $dividend, $divisor ) {
140                 $quotient = floor( $dividend/$divisor );
141                 return $quotient;
142         }
143         function bcmod( $dividend, $modulo ) {
144                 $remainder = $dividend%$modulo;
145                 return $remainder;
146         }
147         function bcmul( $left, $right ) {
148                 return $left * $right;
149         }
150         function bcadd( $left, $right ) {
151                 return $left + $right;
152         }
153         function bcpow( $base, $power ) {
154                 return pow( $base, $power );
155         }
156 }
157
158 /**
159  * Replacement for property_exists() (5.1+)
160  *
161  */
162 if ( !function_exists( 'property_exists' ) ) {
163         function property_exists( $class, $property ) {
164                 if ( is_object( $class ) ) {
165                         $vars = get_object_vars( $class );
166                 } else {
167                         $vars = get_class_vars( $class );
168                 }
169                 return array_key_exists( $property, $vars );
170         }
171 }