commit | author | age
|
4e17e6
|
1 |
<?php |
T |
2 |
|
|
3 |
/* |
|
4 |
+-----------------------------------------------------------------------+ |
|
5 |
| rcube_shared.inc | |
|
6 |
| | |
e019f2
|
7 |
| This file is part of the Roundcube PHP suite | |
f5e7b3
|
8 |
| Copyright (C) 2005-2007, The Roundcube Dev Team | |
30233b
|
9 |
| Licensed under the GNU GPL | |
4e17e6
|
10 |
| | |
T |
11 |
| CONTENTS: | |
|
12 |
| Shared functions and classes used in PHP projects | |
|
13 |
| | |
|
14 |
+-----------------------------------------------------------------------+ |
|
15 |
| Author: Thomas Bruederli <roundcube@gmail.com> | |
|
16 |
+-----------------------------------------------------------------------+ |
|
17 |
|
|
18 |
$Id$ |
|
19 |
|
|
20 |
*/ |
|
21 |
|
|
22 |
|
6d969b
|
23 |
/** |
e019f2
|
24 |
* Roundcube shared functions |
6d969b
|
25 |
* |
T |
26 |
* @package Core |
|
27 |
*/ |
4e17e6
|
28 |
|
a0109c
|
29 |
|
6d969b
|
30 |
/** |
T |
31 |
* Send HTTP headers to prevent caching this page |
|
32 |
*/ |
4e17e6
|
33 |
function send_nocacheing_headers() |
6d969b
|
34 |
{ |
37e467
|
35 |
global $OUTPUT; |
A |
36 |
|
4e17e6
|
37 |
if (headers_sent()) |
T |
38 |
return; |
|
39 |
|
|
40 |
header("Expires: ".gmdate("D, d M Y H:i:s")." GMT"); |
|
41 |
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); |
ebc619
|
42 |
// Request browser to disable DNS prefetching (CVE-2010-0464) |
A |
43 |
header("X-DNS-Prefetch-Control: off"); |
37e467
|
44 |
|
0dbac3
|
45 |
// We need to set the following headers to make downloads work using IE in HTTPS mode. |
37e467
|
46 |
if ($OUTPUT->browser->ie && rcube_https_check()) { |
A |
47 |
header('Pragma: private'); |
5e0999
|
48 |
header("Cache-Control: private, must-revalidate"); |
A |
49 |
} else { |
|
50 |
header("Cache-Control: private, no-cache, must-revalidate, post-check=0, pre-check=0"); |
|
51 |
header("Pragma: no-cache"); |
0dbac3
|
52 |
} |
6d969b
|
53 |
} |
4e17e6
|
54 |
|
T |
55 |
|
6d969b
|
56 |
/** |
T |
57 |
* Send header with expire date 30 days in future |
|
58 |
* |
|
59 |
* @param int Expiration time in seconds |
|
60 |
*/ |
ff52be
|
61 |
function send_future_expire_header($offset=2600000) |
6d969b
|
62 |
{ |
1a98a6
|
63 |
if (headers_sent()) |
T |
64 |
return; |
|
65 |
|
ff52be
|
66 |
header("Expires: ".gmdate("D, d M Y H:i:s", mktime()+$offset)." GMT"); |
T |
67 |
header("Cache-Control: max-age=$offset"); |
1a98a6
|
68 |
header("Pragma: "); |
6d969b
|
69 |
} |
4e17e6
|
70 |
|
T |
71 |
|
6d969b
|
72 |
/** |
T |
73 |
* Check request for If-Modified-Since and send an according response. |
|
74 |
* This will terminate the current script if headers match the given values |
|
75 |
* |
|
76 |
* @param int Modified date as unix timestamp |
|
77 |
* @param string Etag value for caching |
|
78 |
*/ |
3f5cef
|
79 |
function send_modified_header($mdate, $etag=null, $skip_check=false) |
ff52be
|
80 |
{ |
T |
81 |
if (headers_sent()) |
|
82 |
return; |
|
83 |
|
|
84 |
$iscached = false; |
|
85 |
$etag = $etag ? "\"$etag\"" : null; |
3f5cef
|
86 |
|
A |
87 |
if (!$skip_check) |
|
88 |
{ |
|
89 |
if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $mdate) |
|
90 |
$iscached = true; |
|
91 |
|
|
92 |
if ($etag) |
|
93 |
$iscached = ($_SERVER['HTTP_IF_NONE_MATCH'] == $etag); |
|
94 |
} |
ff52be
|
95 |
|
T |
96 |
if ($iscached) |
|
97 |
header("HTTP/1.x 304 Not Modified"); |
|
98 |
else |
|
99 |
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $mdate)." GMT"); |
|
100 |
|
496da6
|
101 |
header("Cache-Control: private, must-revalidate, max-age=0"); |
ff52be
|
102 |
header("Expires: "); |
T |
103 |
header("Pragma: "); |
|
104 |
|
|
105 |
if ($etag) |
|
106 |
header("Etag: $etag"); |
|
107 |
|
|
108 |
if ($iscached) |
1b4d73
|
109 |
{ |
T |
110 |
ob_end_clean(); |
ff52be
|
111 |
exit; |
1b4d73
|
112 |
} |
ff52be
|
113 |
} |
T |
114 |
|
|
115 |
|
f11541
|
116 |
/** |
T |
117 |
* Similar function as in_array() but case-insensitive |
6d969b
|
118 |
* |
T |
119 |
* @param mixed Needle value |
|
120 |
* @param array Array to search in |
|
121 |
* @return boolean True if found, False if not |
f11541
|
122 |
*/ |
4e17e6
|
123 |
function in_array_nocase($needle, $haystack) |
6d969b
|
124 |
{ |
ee258c
|
125 |
$needle = mb_strtolower($needle); |
4e17e6
|
126 |
foreach ($haystack as $value) |
ee258c
|
127 |
if ($needle===mb_strtolower($value)) |
6d969b
|
128 |
return true; |
T |
129 |
|
|
130 |
return false; |
|
131 |
} |
4e17e6
|
132 |
|
T |
133 |
|
f11541
|
134 |
/** |
T |
135 |
* Find out if the string content means TRUE or FALSE |
6d969b
|
136 |
* |
T |
137 |
* @param string Input value |
|
138 |
* @return boolean Imagine what! |
f11541
|
139 |
*/ |
4e17e6
|
140 |
function get_boolean($str) |
6d969b
|
141 |
{ |
4e17e6
|
142 |
$str = strtolower($str); |
ff10f5
|
143 |
if (in_array($str, array('false', '0', 'no', 'off', 'nein', ''), TRUE)) |
4e17e6
|
144 |
return FALSE; |
T |
145 |
else |
|
146 |
return TRUE; |
6d969b
|
147 |
} |
4e17e6
|
148 |
|
T |
149 |
|
6d969b
|
150 |
/** |
T |
151 |
* Parse a human readable string for a number of bytes |
|
152 |
* |
|
153 |
* @param string Input string |
47f072
|
154 |
* @return float Number of bytes |
6d969b
|
155 |
*/ |
86df15
|
156 |
function parse_bytes($str) |
6d969b
|
157 |
{ |
86df15
|
158 |
if (is_numeric($str)) |
47f072
|
159 |
return floatval($str); |
892af4
|
160 |
|
A |
161 |
if (preg_match('/([0-9\.]+)\s*([a-z]*)/i', $str, $regs)) |
6d969b
|
162 |
{ |
T |
163 |
$bytes = floatval($regs[1]); |
|
164 |
switch (strtolower($regs[2])) |
86df15
|
165 |
{ |
6d969b
|
166 |
case 'g': |
892af4
|
167 |
case 'gb': |
6d969b
|
168 |
$bytes *= 1073741824; |
T |
169 |
break; |
|
170 |
case 'm': |
892af4
|
171 |
case 'mb': |
6d969b
|
172 |
$bytes *= 1048576; |
T |
173 |
break; |
|
174 |
case 'k': |
892af4
|
175 |
case 'kb': |
6d969b
|
176 |
$bytes *= 1024; |
T |
177 |
break; |
86df15
|
178 |
} |
6d969b
|
179 |
} |
86df15
|
180 |
|
47f072
|
181 |
return floatval($bytes); |
6d969b
|
182 |
} |
1036f8
|
183 |
|
6d969b
|
184 |
/** |
T |
185 |
* Create a human readable string for a number of bytes |
|
186 |
* |
|
187 |
* @param int Number of bytes |
|
188 |
* @return string Byte string |
|
189 |
*/ |
3ea0e3
|
190 |
function show_bytes($bytes) |
6d969b
|
191 |
{ |
1036f8
|
192 |
if ($bytes >= 1073741824) |
6d969b
|
193 |
{ |
3ea0e3
|
194 |
$gb = $bytes/1073741824; |
f613a1
|
195 |
$str = sprintf($gb>=10 ? "%d " : "%.1f ", $gb) . rcube_label('GB'); |
6d969b
|
196 |
} |
1036f8
|
197 |
else if ($bytes >= 1048576) |
6d969b
|
198 |
{ |
3ea0e3
|
199 |
$mb = $bytes/1048576; |
f613a1
|
200 |
$str = sprintf($mb>=10 ? "%d " : "%.1f ", $mb) . rcube_label('MB'); |
6d969b
|
201 |
} |
1036f8
|
202 |
else if ($bytes >= 1024) |
f613a1
|
203 |
$str = sprintf("%d ", round($bytes/1024)) . rcube_label('KB'); |
4e17e6
|
204 |
else |
f613a1
|
205 |
$str = sprintf('%d ', $bytes) . rcube_label('B'); |
3ea0e3
|
206 |
|
T |
207 |
return $str; |
6d969b
|
208 |
} |
4e17e6
|
209 |
|
6d969b
|
210 |
/** |
T |
211 |
* Convert paths like ../xxx to an absolute path using a base url |
|
212 |
* |
|
213 |
* @param string Relative path |
|
214 |
* @param string Base URL |
|
215 |
* @return string Absolute URL |
|
216 |
*/ |
4e17e6
|
217 |
function make_absolute_url($path, $base_url) |
6d969b
|
218 |
{ |
T |
219 |
$host_url = $base_url; |
|
220 |
$abs_path = $path; |
97bd2c
|
221 |
|
T |
222 |
// check if path is an absolute URL |
|
223 |
if (preg_match('/^[fhtps]+:\/\//', $path)) |
|
224 |
return $path; |
4e17e6
|
225 |
|
6d969b
|
226 |
// cut base_url to the last directory |
aa055c
|
227 |
if (strrpos($base_url, '/')>7) |
6d969b
|
228 |
{ |
8603bb
|
229 |
$host_url = substr($base_url, 0, strpos($base_url, '/', 7)); |
6d969b
|
230 |
$base_url = substr($base_url, 0, strrpos($base_url, '/')); |
T |
231 |
} |
|
232 |
|
|
233 |
// $path is absolute |
c08b18
|
234 |
if ($path[0] == '/') |
6d969b
|
235 |
$abs_path = $host_url.$path; |
T |
236 |
else |
|
237 |
{ |
|
238 |
// strip './' because its the same as '' |
|
239 |
$path = preg_replace('/^\.\//', '', $path); |
|
240 |
|
|
241 |
if (preg_match_all('/\.\.\//', $path, $matches, PREG_SET_ORDER)) |
|
242 |
foreach ($matches as $a_match) |
4e17e6
|
243 |
{ |
6d969b
|
244 |
if (strrpos($base_url, '/')) |
T |
245 |
$base_url = substr($base_url, 0, strrpos($base_url, '/')); |
c08b18
|
246 |
|
6d969b
|
247 |
$path = substr($path, 3); |
4e17e6
|
248 |
} |
T |
249 |
|
6d969b
|
250 |
$abs_path = $base_url.'/'.$path; |
T |
251 |
} |
c08b18
|
252 |
|
6d969b
|
253 |
return $abs_path; |
T |
254 |
} |
4e17e6
|
255 |
|
7145e0
|
256 |
/** |
A |
257 |
* Wrapper function for wordwrap |
|
258 |
*/ |
|
259 |
function rc_wordwrap($string, $width=75, $break="\n", $cut=false) |
|
260 |
{ |
|
261 |
$para = explode($break, $string); |
|
262 |
$string = ''; |
|
263 |
while (count($para)) { |
8ad5c8
|
264 |
$line = array_shift($para); |
T |
265 |
if ($line[0] == '>') { |
|
266 |
$string .= $line.$break; |
|
267 |
continue; |
|
268 |
} |
|
269 |
$list = explode(' ', $line); |
7145e0
|
270 |
$len = 0; |
A |
271 |
while (count($list)) { |
|
272 |
$line = array_shift($list); |
|
273 |
$l = mb_strlen($line); |
|
274 |
$newlen = $len + $l + ($len ? 1 : 0); |
|
275 |
|
|
276 |
if ($newlen <= $width) { |
|
277 |
$string .= ($len ? ' ' : '').$line; |
78ebe7
|
278 |
$len += (1 + $l); |
7145e0
|
279 |
} else { |
8ad5c8
|
280 |
if ($l > $width) { |
T |
281 |
if ($cut) { |
|
282 |
$start = 0; |
|
283 |
while ($l) { |
|
284 |
$str = mb_substr($line, $start, $width); |
|
285 |
$strlen = mb_strlen($str); |
|
286 |
$string .= ($len ? $break : '').$str; |
|
287 |
$start += $strlen; |
|
288 |
$l -= $strlen; |
|
289 |
$len = $strlen; |
|
290 |
} |
|
291 |
} else { |
|
292 |
$string .= ($len ? $break : '').$line; |
|
293 |
if (count($list)) $string .= $break; |
|
294 |
$len = 0; |
|
295 |
} |
|
296 |
} else { |
7145e0
|
297 |
$string .= $break.$line; |
8ad5c8
|
298 |
$len = $l; |
7145e0
|
299 |
} |
A |
300 |
} |
|
301 |
} |
|
302 |
if (count($para)) $string .= $break; |
|
303 |
} |
|
304 |
return $string; |
|
305 |
} |
86df15
|
306 |
|
6d969b
|
307 |
/** |
88f66e
|
308 |
* Read a specific HTTP request header |
T |
309 |
* |
0144c5
|
310 |
* @access static |
T |
311 |
* @param string $name Header name |
|
312 |
* @return mixed Header value or null if not available |
88f66e
|
313 |
*/ |
T |
314 |
function rc_request_header($name) |
5a6eff
|
315 |
{ |
88f66e
|
316 |
if (function_exists('getallheaders')) |
5a6eff
|
317 |
{ |
T |
318 |
$hdrs = array_change_key_case(getallheaders(), CASE_UPPER); |
0144c5
|
319 |
$key = strtoupper($name); |
5a6eff
|
320 |
} |
88f66e
|
321 |
else |
5a6eff
|
322 |
{ |
0144c5
|
323 |
$key = 'HTTP_' . strtoupper(strtr($name, '-', '_')); |
db401b
|
324 |
$hdrs = array_change_key_case($_SERVER, CASE_UPPER); |
5a6eff
|
325 |
} |
T |
326 |
|
|
327 |
return $hdrs[$key]; |
88f66e
|
328 |
} |
1cded8
|
329 |
|
T |
330 |
|
6d969b
|
331 |
/** |
T |
332 |
* Make sure the string ends with a slash |
|
333 |
*/ |
bac7d1
|
334 |
function slashify($str) |
6d969b
|
335 |
{ |
bac7d1
|
336 |
return unslashify($str).'/'; |
6d969b
|
337 |
} |
bac7d1
|
338 |
|
T |
339 |
|
6d969b
|
340 |
/** |
T |
341 |
* Remove slash at the end of the string |
|
342 |
*/ |
bac7d1
|
343 |
function unslashify($str) |
6d969b
|
344 |
{ |
bac7d1
|
345 |
return preg_replace('/\/$/', '', $str); |
6d969b
|
346 |
} |
bac7d1
|
347 |
|
T |
348 |
|
6d969b
|
349 |
/** |
T |
350 |
* Delete all files within a folder |
|
351 |
* |
|
352 |
* @param string Path to directory |
|
353 |
* @return boolean True on success, False if directory was not found |
|
354 |
*/ |
1cded8
|
355 |
function clear_directory($dir_path) |
6d969b
|
356 |
{ |
1cded8
|
357 |
$dir = @opendir($dir_path); |
T |
358 |
if(!$dir) return FALSE; |
|
359 |
|
|
360 |
while ($file = readdir($dir)) |
|
361 |
if (strlen($file)>2) |
|
362 |
unlink("$dir_path/$file"); |
|
363 |
|
|
364 |
closedir($dir); |
|
365 |
return TRUE; |
6d969b
|
366 |
} |
9fee0e
|
367 |
|
T |
368 |
|
6d969b
|
369 |
/** |
T |
370 |
* Create a unix timestamp with a specified offset from now |
|
371 |
* |
|
372 |
* @param string String representation of the offset (e.g. 20min, 5h, 2days) |
|
373 |
* @param int Factor to multiply with the offset |
|
374 |
* @return int Unix timestamp |
|
375 |
*/ |
cc9570
|
376 |
function get_offset_time($offset_str, $factor=1) |
T |
377 |
{ |
|
378 |
if (preg_match('/^([0-9]+)\s*([smhdw])/i', $offset_str, $regs)) |
6d969b
|
379 |
{ |
cc9570
|
380 |
$amount = (int)$regs[1]; |
T |
381 |
$unit = strtolower($regs[2]); |
6d969b
|
382 |
} |
cc9570
|
383 |
else |
6d969b
|
384 |
{ |
cc9570
|
385 |
$amount = (int)$offset_str; |
T |
386 |
$unit = 's'; |
6d969b
|
387 |
} |
cc9570
|
388 |
|
T |
389 |
$ts = mktime(); |
|
390 |
switch ($unit) |
6d969b
|
391 |
{ |
cc9570
|
392 |
case 'w': |
T |
393 |
$amount *= 7; |
|
394 |
case 'd': |
|
395 |
$amount *= 24; |
|
396 |
case 'h': |
|
397 |
$amount *= 60; |
bd4959
|
398 |
case 'm': |
cc9570
|
399 |
$amount *= 60; |
T |
400 |
case 's': |
|
401 |
$ts += $amount * $factor; |
6d969b
|
402 |
} |
cc9570
|
403 |
|
T |
404 |
return $ts; |
6d969b
|
405 |
} |
cc9570
|
406 |
|
ee258c
|
407 |
|
A |
408 |
/** |
0911fa
|
409 |
* Truncate string if it is longer than the allowed length |
A |
410 |
* Replace the middle or the ending part of a string with a placeholder |
ee258c
|
411 |
* |
A |
412 |
* @param string Input string |
|
413 |
* @param int Max. length |
|
414 |
* @param string Replace removed chars with this |
0911fa
|
415 |
* @param bool Set to True if string should be truncated from the end |
ee258c
|
416 |
* @return string Abbreviated string |
A |
417 |
*/ |
0911fa
|
418 |
function abbreviate_string($str, $maxlength, $place_holder='...', $ending=false) |
ee258c
|
419 |
{ |
A |
420 |
$length = mb_strlen($str); |
|
421 |
|
|
422 |
if ($length > $maxlength) |
|
423 |
{ |
0911fa
|
424 |
if ($ending) |
A |
425 |
return mb_substr($str, 0, $maxlength) . $place_holder; |
|
426 |
|
cea5bc
|
427 |
$place_holder_length = mb_strlen($place_holder); |
A |
428 |
$first_part_length = floor(($maxlength - $place_holder_length)/2); |
|
429 |
$second_starting_location = $length - $maxlength + $first_part_length + $place_holder_length; |
|
430 |
$str = mb_substr($str, 0, $first_part_length) . $place_holder . mb_substr($str, $second_starting_location); |
ee258c
|
431 |
} |
A |
432 |
|
|
433 |
return $str; |
|
434 |
} |
cc9570
|
435 |
|
a0109c
|
436 |
/** |
734584
|
437 |
* A method to guess the mime_type of an attachment. |
T |
438 |
* |
d311d8
|
439 |
* @param string $path Path to the file. |
A |
440 |
* @param string $name File name (with suffix) |
|
441 |
* @param string $failover Mime type supplied for failover. |
|
442 |
* @param string $is_stream Set to True if $path contains file body |
734584
|
443 |
* |
T |
444 |
* @return string |
|
445 |
* @author Till Klampaeckel <till@php.net> |
|
446 |
* @see http://de2.php.net/manual/en/ref.fileinfo.php |
|
447 |
* @see http://de2.php.net/mime_content_type |
|
448 |
*/ |
d311d8
|
449 |
function rc_mime_content_type($path, $name, $failover = 'application/octet-stream', $is_stream=false) |
734584
|
450 |
{ |
6d5dba
|
451 |
$mime_type = null; |
T |
452 |
$mime_magic = rcmail::get_instance()->config->get('mime_magic'); |
0ea569
|
453 |
$mime_ext = @include(RCMAIL_CONFIG_DIR . '/mimetypes.php'); |
T |
454 |
$suffix = $name ? substr($name, strrpos($name, '.')+1) : '*'; |
a0109c
|
455 |
|
0ea569
|
456 |
// use file name suffix with hard-coded mime-type map |
T |
457 |
if (is_array($mime_ext)) { |
|
458 |
$mime_type = $mime_ext[$suffix]; |
734584
|
459 |
} |
0ea569
|
460 |
// try fileinfo extension if available |
3e6380
|
461 |
if (!$mime_type && function_exists('finfo_open')) { |
A |
462 |
if ($finfo = finfo_open(FILEINFO_MIME, $mime_magic)) { |
d311d8
|
463 |
if ($is_stream) |
A |
464 |
$mime_type = finfo_buffer($finfo, $path); |
|
465 |
else |
|
466 |
$mime_type = finfo_file($finfo, $path); |
3e6380
|
467 |
finfo_close($finfo); |
734584
|
468 |
} |
T |
469 |
} |
0ea569
|
470 |
// try PHP's mime_content_type |
d311d8
|
471 |
if (!$mime_type && !$is_stream && function_exists('mime_content_type')) { |
A |
472 |
$mime_type = @mime_content_type($path); |
6d5dba
|
473 |
} |
0ea569
|
474 |
// fall back to user-submitted string |
734584
|
475 |
if (!$mime_type) { |
6d5dba
|
476 |
$mime_type = $failover; |
734584
|
477 |
} |
bf13ba
|
478 |
else { |
0f9d8c
|
479 |
// Sometimes (PHP-5.3?) content-type contains charset definition, |
A |
480 |
// Remove it (#1487122) also "charset=binary" is useless |
|
481 |
$mime_type = array_shift(preg_split('/[; ]/', $mime_type)); |
bf13ba
|
482 |
} |
734584
|
483 |
|
T |
484 |
return $mime_type; |
|
485 |
} |
|
486 |
|
0501b6
|
487 |
|
T |
488 |
/** |
|
489 |
* Detect image type of the given binary data by checking magic numbers |
|
490 |
* |
|
491 |
* @param string Binary file content |
|
492 |
* @return string Detected mime-type or jpeg as fallback |
|
493 |
*/ |
|
494 |
function rc_image_content_type($data) |
|
495 |
{ |
|
496 |
$type = 'jpeg'; |
|
497 |
if (preg_match('/^\x89\x50\x4E\x47/', $data)) $type = 'png'; |
|
498 |
else if (preg_match('/^\x47\x49\x46\x38/', $data)) $type = 'gif'; |
|
499 |
else if (preg_match('/^\x00\x00\x01\x00/', $data)) $type = 'ico'; |
|
500 |
// else if (preg_match('/^\xFF\xD8\xFF\xE0/', $data)) $type = 'jpeg'; |
|
501 |
|
|
502 |
return 'image/' . $type; |
|
503 |
} |
|
504 |
|
|
505 |
|
b54121
|
506 |
/** |
A |
507 |
* A method to guess encoding of a string. |
|
508 |
* |
|
509 |
* @param string $string String. |
|
510 |
* @param string $failover Default result for failover. |
|
511 |
* |
|
512 |
* @return string |
|
513 |
*/ |
|
514 |
function rc_detect_encoding($string, $failover='') |
|
515 |
{ |
|
516 |
if (!function_exists('mb_detect_encoding')) { |
|
517 |
return $failover; |
|
518 |
} |
|
519 |
|
|
520 |
// FIXME: the order is important, because sometimes |
|
521 |
// iso string is detected as euc-jp and etc. |
|
522 |
$enc = array( |
b58f11
|
523 |
'UTF-8', 'SJIS', 'BIG5', 'GB2312', |
T |
524 |
'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4', |
|
525 |
'ISO-8859-5', 'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8', 'ISO-8859-9', |
|
526 |
'ISO-8859-10', 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'ISO-8859-16', |
|
527 |
'WINDOWS-1252', 'WINDOWS-1251', 'EUC-JP', 'EUC-TW', 'KOI8-R', |
|
528 |
'ISO-2022-KR', 'ISO-2022-JP' |
b54121
|
529 |
); |
A |
530 |
|
|
531 |
$result = mb_detect_encoding($string, join(',', $enc)); |
|
532 |
|
|
533 |
return $result ? $result : $failover; |
|
534 |
} |
|
535 |
|
1a00f1
|
536 |
/** |
A |
537 |
* Removes non-unicode characters from input |
|
538 |
* |
|
539 |
* @param mixed $input String or array. |
|
540 |
* @return string |
|
541 |
*/ |
|
542 |
function rc_utf8_clean($input) |
|
543 |
{ |
|
544 |
// handle input of type array |
|
545 |
if (is_array($input)) { |
|
546 |
foreach ($input as $idx => $val) |
|
547 |
$input[$idx] = rc_utf8_clean($val); |
|
548 |
return $input; |
|
549 |
} |
|
550 |
|
b6c512
|
551 |
if (!is_string($input) || $input == '') |
1a00f1
|
552 |
return $input; |
2717f9
|
553 |
|
b6c512
|
554 |
// iconv/mbstring are much faster (especially with long strings) |
6481d4
|
555 |
if (function_exists('mb_convert_encoding') && ($res = mb_convert_encoding($input, 'UTF-8', 'UTF-8')) !== false) |
b6c512
|
556 |
return $res; |
A |
557 |
|
ecbd5b
|
558 |
if (function_exists('iconv') && ($res = @iconv('UTF-8', 'UTF-8//IGNORE', $input)) !== false) |
b6c512
|
559 |
return $res; |
1a00f1
|
560 |
|
A |
561 |
$regexp = '/^('. |
|
562 |
// '[\x00-\x7F]'. // UTF8-1 |
|
563 |
'|[\xC2-\xDF][\x80-\xBF]'. // UTF8-2 |
|
564 |
'|\xE0[\xA0-\xBF][\x80-\xBF]'. // UTF8-3 |
|
565 |
'|[\xE1-\xEC][\x80-\xBF][\x80-\xBF]'. // UTF8-3 |
|
566 |
'|\xED[\x80-\x9F][\x80-\xBF]'. // UTF8-3 |
|
567 |
'|[\xEE-\xEF][\x80-\xBF][\x80-\xBF]'. // UTF8-3 |
|
568 |
'|\xF0[\x90-\xBF][\x80-\xBF][\x80-\xBF]'. // UTF8-4 |
|
569 |
'|[\xF1-\xF3][\x80-\xBF][\x80-\xBF][\x80-\xBF]'.// UTF8-4 |
|
570 |
'|\xF4[\x80-\x8F][\x80-\xBF][\x80-\xBF]'. // UTF8-4 |
|
571 |
')$/'; |
|
572 |
|
|
573 |
$seq = ''; |
|
574 |
$out = ''; |
|
575 |
|
6481d4
|
576 |
for ($i = 0, $len = strlen($input); $i < $len; $i++) { |
1a00f1
|
577 |
$chr = $input[$i]; |
A |
578 |
$ord = ord($chr); |
|
579 |
// 1-byte character |
|
580 |
if ($ord <= 0x7F) { |
|
581 |
if ($seq) |
|
582 |
$out .= preg_match($regexp, $seq) ? $seq : ''; |
|
583 |
$seq = ''; |
|
584 |
$out .= $chr; |
|
585 |
// first (or second) byte of multibyte sequence |
|
586 |
} else if ($ord >= 0xC0) { |
|
587 |
if (strlen($seq)>1) { |
|
588 |
$out .= preg_match($regexp, $seq) ? $seq : ''; |
|
589 |
$seq = ''; |
|
590 |
} else if ($seq && ord($seq) < 0xC0) { |
|
591 |
$seq = ''; |
|
592 |
} |
|
593 |
$seq .= $chr; |
|
594 |
// next byte of multibyte sequence |
|
595 |
} else if ($seq) { |
|
596 |
$seq .= $chr; |
|
597 |
} |
|
598 |
} |
|
599 |
|
|
600 |
if ($seq) |
|
601 |
$out .= preg_match($regexp, $seq) ? $seq : ''; |
|
602 |
|
|
603 |
return $out; |
|
604 |
} |
0ff635
|
605 |
|
2717f9
|
606 |
|
A |
607 |
/** |
|
608 |
* Convert a variable into a javascript object notation |
|
609 |
* |
|
610 |
* @param mixed Input value |
|
611 |
* @return string Serialized JSON string |
|
612 |
*/ |
|
613 |
function json_serialize($input) |
|
614 |
{ |
|
615 |
$input = rc_utf8_clean($input); |
63ffe3
|
616 |
|
a7dba8
|
617 |
// sometimes even using rc_utf8_clean() the input contains invalid UTF-8 sequences |
A |
618 |
// that's why we have @ here |
|
619 |
return @json_encode($input); |
2717f9
|
620 |
} |
A |
621 |
|
|
622 |
|
0ff635
|
623 |
/** |
A |
624 |
* Explode quoted string |
|
625 |
* |
|
626 |
* @param string Delimiter expression string for preg_match() |
|
627 |
* @param string Input string |
|
628 |
*/ |
|
629 |
function rcube_explode_quoted_string($delimiter, $string) |
|
630 |
{ |
|
631 |
$result = array(); |
|
632 |
$strlen = strlen($string); |
|
633 |
|
|
634 |
for ($q=$p=$i=0; $i < $strlen; $i++) { |
|
635 |
if ($string[$i] == "\"" && $string[$i-1] != "\\") { |
|
636 |
$q = $q ? false : true; |
|
637 |
} |
|
638 |
else if (!$q && preg_match("/$delimiter/", $string[$i])) { |
|
639 |
$result[] = substr($string, $p, $i - $p); |
|
640 |
$p = $i + 1; |
|
641 |
} |
|
642 |
} |
|
643 |
|
|
644 |
$result[] = substr($string, $p); |
|
645 |
return $result; |
|
646 |
} |
|
647 |
|
ee258c
|
648 |
|
A |
649 |
/** |
f52c93
|
650 |
* Get all keys from array (recursive) |
T |
651 |
* |
|
652 |
* @param array Input array |
|
653 |
* @return array |
|
654 |
*/ |
|
655 |
function array_keys_recursive($array) |
|
656 |
{ |
|
657 |
$keys = array(); |
|
658 |
|
|
659 |
if (!empty($array)) |
|
660 |
foreach ($array as $key => $child) { |
|
661 |
$keys[] = $key; |
d78564
|
662 |
foreach (array_keys_recursive($child) as $val) |
A |
663 |
$keys[] = $val; |
f52c93
|
664 |
} |
T |
665 |
return $keys; |
|
666 |
} |
|
667 |
|
|
668 |
|
|
669 |
/** |
ee258c
|
670 |
* mbstring replacement functions |
A |
671 |
*/ |
|
672 |
|
8f6a46
|
673 |
if (!extension_loaded('mbstring')) |
A |
674 |
{ |
ee258c
|
675 |
function mb_strlen($str) |
A |
676 |
{ |
|
677 |
return strlen($str); |
|
678 |
} |
|
679 |
|
|
680 |
function mb_strtolower($str) |
|
681 |
{ |
|
682 |
return strtolower($str); |
|
683 |
} |
|
684 |
|
|
685 |
function mb_strtoupper($str) |
|
686 |
{ |
|
687 |
return strtoupper($str); |
|
688 |
} |
|
689 |
|
|
690 |
function mb_substr($str, $start, $len=null) |
|
691 |
{ |
|
692 |
return substr($str, $start, $len); |
|
693 |
} |
|
694 |
|
|
695 |
function mb_strpos($haystack, $needle, $offset=0) |
|
696 |
{ |
|
697 |
return strpos($haystack, $needle, $offset); |
|
698 |
} |
|
699 |
|
|
700 |
function mb_strrpos($haystack, $needle, $offset=0) |
|
701 |
{ |
|
702 |
return strrpos($haystack, $needle, $offset); |
|
703 |
} |
|
704 |
} |
|
705 |
|
e99991
|
706 |
/** |
A |
707 |
* intl replacement functions |
|
708 |
*/ |
|
709 |
|
|
710 |
if (!function_exists('idn_to_utf8')) |
|
711 |
{ |
|
712 |
function idn_to_utf8($domain, $flags=null) |
|
713 |
{ |
|
714 |
static $idn, $loaded; |
|
715 |
|
|
716 |
if (!$loaded) { |
|
717 |
$idn = new Net_IDNA2(); |
|
718 |
$loaded = true; |
|
719 |
} |
|
720 |
|
e8d5bd
|
721 |
if ($idn && $domain && preg_match('/(^|\.)xn--/i', $domain)) { |
e99991
|
722 |
try { |
A |
723 |
$domain = $idn->decode($domain); |
|
724 |
} |
|
725 |
catch (Exception $e) { |
|
726 |
} |
|
727 |
} |
|
728 |
return $domain; |
|
729 |
} |
|
730 |
} |
|
731 |
|
|
732 |
if (!function_exists('idn_to_ascii')) |
|
733 |
{ |
|
734 |
function idn_to_ascii($domain, $flags=null) |
|
735 |
{ |
|
736 |
static $idn, $loaded; |
|
737 |
|
|
738 |
if (!$loaded) { |
|
739 |
$idn = new Net_IDNA2(); |
|
740 |
$loaded = true; |
|
741 |
} |
|
742 |
|
|
743 |
if ($idn && $domain && preg_match('/[^\x20-\x7E]/', $domain)) { |
|
744 |
try { |
|
745 |
$domain = $idn->encode($domain); |
|
746 |
} |
|
747 |
catch (Exception $e) { |
|
748 |
} |
|
749 |
} |
|
750 |
return $domain; |
|
751 |
} |
|
752 |
} |
|
753 |
|