4 * Compatibility functions when either missing from older PHP versions or not included by default
12 * json_encode for PHP, should someone run a distro without php-json -- see http://askubuntu.com/questions/361424/
15 if( !function_exists( 'json_encode' ) ) {
16 function json_encode( $array ) {
17 return yourls_array_to_json( $array );
22 * Converts an associative array of arbitrary depth and dimension into JSON representation. Used for compatibility with older PHP builds.
24 * @param array $array the array to convert.
25 * @return mixed The resulting JSON string, or false if the argument was not an array.
26 * @author Andy Rusterholz
27 * @link http://php.net/json_encode (see comments)
29 function yourls_array_to_json( $array ){
31 if( !is_array( $array ) ){
35 $associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
39 foreach( $array as $key => $value ){
41 // We first copy each key/value pair into a staging array,
42 // formatting each key and value properly as we go.
45 if( is_numeric( $key ) ){
48 $key = '"'.addslashes( $key ).'"';
51 if( is_array( $value )){
52 $value = yourls_array_to_json( $value );
53 } else if( !is_numeric( $value ) || is_string( $value ) ){
54 $value = '"'.addslashes( $value ).'"';
57 // Add to staging array:
58 $construct[] = "$key: $value";
61 // Then we collapse the staging array into the JSON form:
62 $result = "{ " . implode( ", ", $construct ) . " }";
64 } else { // If the array is a vector (not associative):
67 foreach( $array as $value ){
70 if( is_array( $value )){
71 $value = yourls_array_to_json( $value );
72 } else if( !is_numeric( $value ) || is_string( $value ) ){
73 $value = '"'.addslashes($value).'"';
76 // Add to staging array:
77 $construct[] = $value;
80 // Then we collapse the staging array into the JSON form:
81 $result = "[ " . implode( ", ", $construct ) . " ]";
89 * BC Math functions (assuming if one doesn't exist, none does)
92 if ( !function_exists( 'bcdiv' ) ) {
93 function bcdiv( $dividend, $divisor ) {
94 $quotient = floor( $dividend/$divisor );
97 function bcmod( $dividend, $modulo ) {
98 $remainder = $dividend%$modulo;
101 function bcmul( $left, $right ) {
102 return $left * $right;
104 function bcadd( $left, $right ) {
105 return $left + $right;
107 function bcpow( $base, $power ) {
108 return pow( $base, $power );
114 * http_build_url compatibility function
118 if ( !function_exists( 'http_build_url' ) ) {
119 include YOURLS_INC . '/http_build_url/http_build_url.php';
124 * mb_substr compatibility function. Stolen from WP
126 * Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
127 * For $encoding === UTF-8, the $str input is expected to be a valid UTF-8 byte sequence.
128 * The behavior of this function for invalid inputs is undefined.
132 if ( ! function_exists( 'mb_substr' ) ) :
133 function mb_substr( $str, $start, $length = null, $encoding = null ) {
134 return yourls_mb_substr( $str, $start, $length, $encoding );
137 function yourls_mb_substr( $str, $start, $length = null, $encoding = null ) {
138 if ( null === $encoding ) {
141 // The solution below works only for UTF-8,
142 // so in case of a different charset just use built-in substr()
143 if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
144 return is_null( $length ) ? substr( $str, $start ) : substr( $str, $start, $length );
146 if ( yourls_supports_pcre_u() ) {
147 // Use the regex unicode support to separate the UTF-8 characters into an array
148 preg_match_all( '/./us', $str, $match );
149 $chars = is_null( $length ) ? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length );
150 return implode( '', $chars );
153 [\x00-\x7F] # single-byte sequences 0xxxxxxx
154 | [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
155 | \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
156 | [\xE1-\xEC][\x80-\xBF]{2}
157 | \xED[\x80-\x9F][\x80-\xBF]
158 | [\xEE-\xEF][\x80-\xBF]{2}
159 | \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3
160 | [\xF1-\xF3][\x80-\xBF]{3}
161 | \xF4[\x80-\x8F][\x80-\xBF]{2}
163 $chars = array( '' ); // Start with 1 element instead of 0 since the first thing we do is pop
165 // We had some string left over from the last round, but we counted it in that last round.
167 // Split by UTF-8 character, limit to 1000 characters (last array element will contain the rest of the string)
168 $pieces = preg_split( $regex, $str, 1000, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
169 $chars = array_merge( $chars, $pieces );
170 } while ( count( $pieces ) > 1 && $str = array_pop( $pieces ) ); // If there's anything left over, repeat the loop.
171 return join( '', array_slice( $chars, $start, $length ) );
175 * mb_strlen compatibility function. Stolen from WP
177 * Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
178 * For $encoding === UTF-8, the $str input is expected to be a valid UTF-8 byte sequence.
179 * The behavior of this function for invalid inputs is undefined.
183 if ( ! function_exists( 'mb_strlen' ) ) :
184 function mb_strlen( $str, $encoding = null ) {
185 return yourls_mb_strlen( $str, $encoding );
188 function yourls_mb_strlen( $str, $encoding = null ) {
189 if ( null === $encoding ) {
192 // The solution below works only for UTF-8,
193 // so in case of a different charset just use built-in strlen()
194 if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
195 return strlen( $str );
197 if ( yourls_supports_pcre_u() ) {
198 // Use the regex unicode support to separate the UTF-8 characters into an array
199 preg_match_all( '/./us', $str, $match );
200 return count( $match[0] );
203 [\x00-\x7F] # single-byte sequences 0xxxxxxx
204 | [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
205 | \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
206 | [\xE1-\xEC][\x80-\xBF]{2}
207 | \xED[\x80-\x9F][\x80-\xBF]
208 | [\xEE-\xEF][\x80-\xBF]{2}
209 | \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3
210 | [\xF1-\xF3][\x80-\xBF]{3}
211 | \xF4[\x80-\x8F][\x80-\xBF]{2}
213 $count = 1; // Start at 1 instead of 0 since the first thing we do is decrement
215 // We had some string left over from the last round, but we counted it in that last round.
217 // Split by UTF-8 character, limit to 1000 characters (last array element will contain the rest of the string)
218 $pieces = preg_split( $regex, $str, 1000 );
220 $count += count( $pieces );
221 } while ( $str = array_pop( $pieces ) ); // If there's anything left over, repeat the loop.
222 // Fencepost: preg_split() always returns one extra item in the array