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