]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-compat.php
PHPdoc-ish-ification of older functions. Fixes issue 1244.
[Github/YOURLS.git] / includes / functions-compat.php
1 <?php\r
2 \r
3 /**\r
4  * json_encode for PHP prior to 5.2\r
5  *\r
6  */\r
7 if( !function_exists( 'json_encode' ) ) {\r
8         function json_encode( $array ) {\r
9                 return yourls_array_to_json( $array );\r
10         }\r
11 }\r
12 \r
13 /**\r
14  * Converts an associative array of arbitrary depth and dimension into JSON representation. Used for compatibility with older PHP builds.\r
15  *\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
20  */\r
21 function yourls_array_to_json( $array ){\r
22 \r
23     if( !is_array( $array ) ){\r
24         return false;\r
25     }\r
26 \r
27     $associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));\r
28     if( $associative ){\r
29 \r
30         $construct = array();\r
31         foreach( $array as $key => $value ){\r
32 \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
35 \r
36             // Format the key:\r
37             if( is_numeric( $key ) ){\r
38                 $key = "key_$key";\r
39             }\r
40             $key = '"'.addslashes( $key ).'"';\r
41 \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
47             }\r
48 \r
49             // Add to staging array:\r
50             $construct[] = "$key: $value";\r
51         }\r
52 \r
53         // Then we collapse the staging array into the JSON form:\r
54         $result = "{ " . implode( ", ", $construct ) . " }";\r
55 \r
56     } else { // If the array is a vector (not associative):\r
57 \r
58         $construct = array();\r
59         foreach( $array as $value ){\r
60 \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
66             }\r
67 \r
68             // Add to staging array:\r
69             $construct[] = $value;\r
70         }\r
71 \r
72         // Then we collapse the staging array into the JSON form:\r
73         $result = "[ " . implode( ", ", $construct ) . " ]";\r
74     }\r
75 \r
76     return $result;\r
77 }\r
78 \r
79 /**\r
80  * Compat http_build_query for PHP4\r
81  *\r
82  */\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
86         }\r
87 }\r
88 \r
89 /**\r
90  * Compat http_build_query for PHP4. Stolen from WP.\r
91  *\r
92  * from php.net (modified by Mark Jaquith to behave like the native PHP5 function)\r
93  * \r
94  */\r
95 function yourls_http_build_query( $data, $prefix=null, $sep=null, $key='', $urlencode=true ) {\r
96         $ret = array();\r
97 \r
98         foreach ( (array) $data as $k => $v ) {\r
99                 if ( $urlencode)\r
100                         $k = urlencode( $k );\r
101                 if ( is_int($k) && $prefix != null )\r
102                         $k = $prefix.$k;\r
103                 if ( !empty( $key ) )\r
104                         $k = $key . '%5B' . $k . '%5D';\r
105                 if ( $v === NULL )\r
106                         continue;\r
107                 elseif ( $v === FALSE )\r
108                         $v = '0';\r
109 \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
114                 else\r
115                         array_push( $ret, $k.'='.$v );\r
116         }\r
117 \r
118         if ( NULL === $sep )\r
119                 $sep = ini_get( 'arg_separator.output' );\r
120 \r
121         return implode( $sep, $ret );\r
122 }\r
123 \r
124 /**\r
125  * htmlspecialchars_decode for PHP < 5.1\r
126  *\r
127  */\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
131         }\r
132 }\r
133 \r
134 /**\r
135  * BC Math functions (assuming if one doesn't exist, none does)\r
136  *\r
137  */\r
138 if ( !function_exists( 'bcdiv' ) ) {\r
139         function bcdiv( $dividend, $divisor ) {\r
140                 $quotient = floor( $dividend/$divisor );\r
141                 return $quotient;\r
142         }\r
143         function bcmod( $dividend, $modulo ) {\r
144                 $remainder = $dividend%$modulo;\r
145                 return $remainder;\r
146         }\r
147         function bcmul( $left, $right ) {\r
148                 return $left * $right;\r
149         }\r
150         function bcadd( $left, $right ) {\r
151                 return $left + $right;\r
152         }\r
153         function bcpow( $base, $power ) {\r
154                 return pow( $base, $power );\r
155         }\r
156 }\r
157 \r
158 /**\r
159  * Replacement for property_exists() (5.1+)\r
160  *\r
161  */\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
166                 } else {\r
167                         $vars = get_class_vars( $class );\r
168                 }\r
169                 return array_key_exists( $property, $vars );\r
170         }\r
171 }\r