commit | author | age
|
4e17e6
|
1 |
<?php |
T |
2 |
|
|
3 |
/* |
|
4 |
+-----------------------------------------------------------------------+ |
0c2596
|
5 |
| program/include/rcube_shared.inc | |
4e17e6
|
6 |
| | |
e019f2
|
7 |
| This file is part of the Roundcube PHP suite | |
0c2596
|
8 |
| Copyright (C) 2005-2012, The Roundcube Dev Team | |
7fe381
|
9 |
| | |
T |
10 |
| Licensed under the GNU General Public License version 3 or | |
|
11 |
| any later version with exceptions for skins & plugins. | |
|
12 |
| See the README file for a full license statement. | |
4e17e6
|
13 |
| | |
T |
14 |
| CONTENTS: | |
0c2596
|
15 |
| Shared functions used by Roundcube Framework | |
4e17e6
|
16 |
| | |
T |
17 |
+-----------------------------------------------------------------------+ |
|
18 |
| Author: Thomas Bruederli <roundcube@gmail.com> | |
|
19 |
+-----------------------------------------------------------------------+ |
|
20 |
|
|
21 |
$Id$ |
|
22 |
|
|
23 |
*/ |
|
24 |
|
|
25 |
|
6d969b
|
26 |
/** |
e019f2
|
27 |
* Roundcube shared functions |
9e54e6
|
28 |
* |
6d969b
|
29 |
* @package Core |
T |
30 |
*/ |
4e17e6
|
31 |
|
a0109c
|
32 |
|
6d969b
|
33 |
/** |
f11541
|
34 |
* Similar function as in_array() but case-insensitive |
6d969b
|
35 |
* |
0c2596
|
36 |
* @param string $needle Needle value |
A |
37 |
* @param array $heystack Array to search in |
|
38 |
* |
6d969b
|
39 |
* @return boolean True if found, False if not |
f11541
|
40 |
*/ |
4e17e6
|
41 |
function in_array_nocase($needle, $haystack) |
6d969b
|
42 |
{ |
0c2596
|
43 |
$needle = mb_strtolower($needle); |
A |
44 |
foreach ($haystack as $value) { |
|
45 |
if ($needle === mb_strtolower($value)) { |
|
46 |
return true; |
|
47 |
} |
|
48 |
} |
9e54e6
|
49 |
|
0c2596
|
50 |
return false; |
6d969b
|
51 |
} |
4e17e6
|
52 |
|
T |
53 |
|
f11541
|
54 |
/** |
0c2596
|
55 |
* Find out if the string content means true or false |
6d969b
|
56 |
* |
0c2596
|
57 |
* @param string $str Input value |
A |
58 |
* |
|
59 |
* @return boolean Boolean value |
f11541
|
60 |
*/ |
4e17e6
|
61 |
function get_boolean($str) |
6d969b
|
62 |
{ |
0c2596
|
63 |
$str = strtolower($str); |
A |
64 |
|
|
65 |
return !in_array($str, array('false', '0', 'no', 'off', 'nein', ''), true); |
6d969b
|
66 |
} |
4e17e6
|
67 |
|
T |
68 |
|
6d969b
|
69 |
/** |
0c2596
|
70 |
* Parse a human readable string for a number of bytes. |
6d969b
|
71 |
* |
0c2596
|
72 |
* @param string $str Input string |
A |
73 |
* |
47f072
|
74 |
* @return float Number of bytes |
6d969b
|
75 |
*/ |
86df15
|
76 |
function parse_bytes($str) |
6d969b
|
77 |
{ |
0c2596
|
78 |
if (is_numeric($str)) { |
A |
79 |
return floatval($str); |
86df15
|
80 |
} |
T |
81 |
|
0c2596
|
82 |
if (preg_match('/([0-9\.]+)\s*([a-z]*)/i', $str, $regs)) { |
A |
83 |
$bytes = floatval($regs[1]); |
|
84 |
switch (strtolower($regs[2])) { |
|
85 |
case 'g': |
|
86 |
case 'gb': |
|
87 |
$bytes *= 1073741824; |
|
88 |
break; |
|
89 |
case 'm': |
|
90 |
case 'mb': |
|
91 |
$bytes *= 1048576; |
|
92 |
break; |
|
93 |
case 'k': |
|
94 |
case 'kb': |
|
95 |
$bytes *= 1024; |
|
96 |
break; |
7145e0
|
97 |
} |
A |
98 |
} |
0c2596
|
99 |
|
A |
100 |
return floatval($bytes); |
7145e0
|
101 |
} |
86df15
|
102 |
|
0c2596
|
103 |
|
6d969b
|
104 |
/** |
T |
105 |
* Make sure the string ends with a slash |
|
106 |
*/ |
bac7d1
|
107 |
function slashify($str) |
6d969b
|
108 |
{ |
bac7d1
|
109 |
return unslashify($str).'/'; |
6d969b
|
110 |
} |
bac7d1
|
111 |
|
T |
112 |
|
6d969b
|
113 |
/** |
T |
114 |
* Remove slash at the end of the string |
|
115 |
*/ |
bac7d1
|
116 |
function unslashify($str) |
6d969b
|
117 |
{ |
bac7d1
|
118 |
return preg_replace('/\/$/', '', $str); |
6d969b
|
119 |
} |
9e54e6
|
120 |
|
bac7d1
|
121 |
|
6d969b
|
122 |
/** |
T |
123 |
* Delete all files within a folder |
|
124 |
* |
|
125 |
* @param string Path to directory |
0c2596
|
126 |
* |
6d969b
|
127 |
* @return boolean True on success, False if directory was not found |
T |
128 |
*/ |
1cded8
|
129 |
function clear_directory($dir_path) |
6d969b
|
130 |
{ |
0c2596
|
131 |
$dir = @opendir($dir_path); |
A |
132 |
if (!$dir) { |
|
133 |
return false; |
|
134 |
} |
1cded8
|
135 |
|
0c2596
|
136 |
while ($file = readdir($dir)) { |
A |
137 |
if (strlen($file) > 2) { |
|
138 |
unlink("$dir_path/$file"); |
|
139 |
} |
|
140 |
} |
1cded8
|
141 |
|
0c2596
|
142 |
closedir($dir); |
A |
143 |
|
|
144 |
return true; |
6d969b
|
145 |
} |
9fee0e
|
146 |
|
T |
147 |
|
6d969b
|
148 |
/** |
5d66a4
|
149 |
* Returns number of seconds for a specified offset string. |
6d969b
|
150 |
* |
5d66a4
|
151 |
* @param string $str String representation of the offset (e.g. 20min, 5h, 2days, 1week) |
0c2596
|
152 |
* |
5d66a4
|
153 |
* @return int Number of seconds |
6d969b
|
154 |
*/ |
5d66a4
|
155 |
function get_offset_sec($str) |
9e54e6
|
156 |
{ |
5d66a4
|
157 |
if (preg_match('/^([0-9]+)\s*([smhdw])/i', $str, $regs)) { |
A |
158 |
$amount = (int) $regs[1]; |
0c2596
|
159 |
$unit = strtolower($regs[2]); |
734584
|
160 |
} |
bf13ba
|
161 |
else { |
5d66a4
|
162 |
$amount = (int) $str; |
0c2596
|
163 |
$unit = 's'; |
bf13ba
|
164 |
} |
734584
|
165 |
|
0c2596
|
166 |
switch ($unit) { |
A |
167 |
case 'w': |
|
168 |
$amount *= 7; |
|
169 |
case 'd': |
|
170 |
$amount *= 24; |
|
171 |
case 'h': |
|
172 |
$amount *= 60; |
|
173 |
case 'm': |
|
174 |
$amount *= 60; |
|
175 |
} |
|
176 |
|
5d66a4
|
177 |
return $amount; |
A |
178 |
} |
|
179 |
|
|
180 |
|
|
181 |
/** |
|
182 |
* Create a unix timestamp with a specified offset from now. |
|
183 |
* |
|
184 |
* @param string $offset_str String representation of the offset (e.g. 20min, 5h, 2days) |
|
185 |
* @param int $factor Factor to multiply with the offset |
|
186 |
* |
|
187 |
* @return int Unix timestamp |
|
188 |
*/ |
|
189 |
function get_offset_time($offset_str, $factor=1) |
|
190 |
{ |
|
191 |
return time() + get_offset_sec($offset_str) * $factor; |
734584
|
192 |
} |
T |
193 |
|
0501b6
|
194 |
|
T |
195 |
/** |
0c2596
|
196 |
* Truncate string if it is longer than the allowed length. |
A |
197 |
* Replace the middle or the ending part of a string with a placeholder. |
0501b6
|
198 |
* |
0c2596
|
199 |
* @param string $str Input string |
A |
200 |
* @param int $maxlength Max. length |
|
201 |
* @param string $placeholder Replace removed chars with this |
|
202 |
* @param bool $ending Set to True if string should be truncated from the end |
|
203 |
* |
|
204 |
* @return string Abbreviated string |
0501b6
|
205 |
*/ |
0c2596
|
206 |
function abbreviate_string($str, $maxlength, $placeholder='...', $ending=false) |
0501b6
|
207 |
{ |
0c2596
|
208 |
$length = mb_strlen($str); |
0501b6
|
209 |
|
0c2596
|
210 |
if ($length > $maxlength) { |
A |
211 |
if ($ending) { |
|
212 |
return mb_substr($str, 0, $maxlength) . $placeholder; |
|
213 |
} |
|
214 |
|
|
215 |
$placeholder_length = mb_strlen($placeholder); |
|
216 |
$first_part_length = floor(($maxlength - $placeholder_length)/2); |
|
217 |
$second_starting_location = $length - $maxlength + $first_part_length + $placeholder_length; |
|
218 |
|
|
219 |
$str = mb_substr($str, 0, $first_part_length) . $placeholder . mb_substr($str, $second_starting_location); |
|
220 |
} |
|
221 |
|
|
222 |
return $str; |
0501b6
|
223 |
} |
T |
224 |
|
|
225 |
|
b54121
|
226 |
/** |
0c2596
|
227 |
* Get all keys from array (recursive). |
A |
228 |
* |
|
229 |
* @param array $array Input array |
|
230 |
* |
|
231 |
* @return array List of array keys |
f52c93
|
232 |
*/ |
T |
233 |
function array_keys_recursive($array) |
|
234 |
{ |
0c2596
|
235 |
$keys = array(); |
9e54e6
|
236 |
|
0c2596
|
237 |
if (!empty($array)) { |
A |
238 |
foreach ($array as $key => $child) { |
|
239 |
$keys[] = $key; |
|
240 |
foreach (array_keys_recursive($child) as $val) { |
|
241 |
$keys[] = $val; |
|
242 |
} |
|
243 |
} |
f52c93
|
244 |
} |
0c2596
|
245 |
|
A |
246 |
return $keys; |
|
247 |
} |
|
248 |
|
|
249 |
|
|
250 |
/** |
|
251 |
* Remove all non-ascii and non-word chars except ., -, _ |
|
252 |
*/ |
|
253 |
function asciiwords($str, $css_id = false, $replace_with = '') |
|
254 |
{ |
|
255 |
$allowed = 'a-z0-9\_\-' . (!$css_id ? '\.' : ''); |
|
256 |
return preg_replace("/[^$allowed]/i", $replace_with, $str); |
|
257 |
} |
|
258 |
|
|
259 |
|
|
260 |
/** |
|
261 |
* Remove single and double quotes from given string |
|
262 |
* |
|
263 |
* @param string Input value |
|
264 |
* |
|
265 |
* @return string Dequoted string |
|
266 |
*/ |
|
267 |
function strip_quotes($str) |
|
268 |
{ |
|
269 |
return str_replace(array("'", '"'), '', $str); |
|
270 |
} |
|
271 |
|
|
272 |
|
|
273 |
/** |
|
274 |
* Remove new lines characters from given string |
|
275 |
* |
|
276 |
* @param string $str Input value |
|
277 |
* |
|
278 |
* @return string Stripped string |
|
279 |
*/ |
|
280 |
function strip_newlines($str) |
|
281 |
{ |
|
282 |
return preg_replace('/[\r\n]/', '', $str); |
|
283 |
} |
|
284 |
|
|
285 |
|
|
286 |
/** |
|
287 |
* Compose a valid representation of name and e-mail address |
|
288 |
* |
|
289 |
* @param string $email E-mail address |
|
290 |
* @param string $name Person name |
|
291 |
* |
|
292 |
* @return string Formatted string |
|
293 |
*/ |
|
294 |
function format_email_recipient($email, $name = '') |
|
295 |
{ |
|
296 |
$email = trim($email); |
|
297 |
|
|
298 |
if ($name && $name != $email) { |
|
299 |
// Special chars as defined by RFC 822 need to in quoted string (or escaped). |
|
300 |
if (preg_match('/[\(\)\<\>\\\.\[\]@,;:"]/', $name)) { |
|
301 |
$name = '"'.addcslashes($name, '"').'"'; |
|
302 |
} |
|
303 |
|
|
304 |
return "$name <$email>"; |
|
305 |
} |
|
306 |
|
|
307 |
return $email; |
f52c93
|
308 |
} |
T |
309 |
|
|
310 |
|
|
311 |
/** |
ee258c
|
312 |
* mbstring replacement functions |
A |
313 |
*/ |
8f6a46
|
314 |
if (!extension_loaded('mbstring')) |
A |
315 |
{ |
ee258c
|
316 |
function mb_strlen($str) |
A |
317 |
{ |
0c2596
|
318 |
return strlen($str); |
ee258c
|
319 |
} |
A |
320 |
|
|
321 |
function mb_strtolower($str) |
|
322 |
{ |
|
323 |
return strtolower($str); |
|
324 |
} |
|
325 |
|
|
326 |
function mb_strtoupper($str) |
|
327 |
{ |
|
328 |
return strtoupper($str); |
|
329 |
} |
|
330 |
|
|
331 |
function mb_substr($str, $start, $len=null) |
|
332 |
{ |
|
333 |
return substr($str, $start, $len); |
|
334 |
} |
|
335 |
|
|
336 |
function mb_strpos($haystack, $needle, $offset=0) |
|
337 |
{ |
|
338 |
return strpos($haystack, $needle, $offset); |
|
339 |
} |
|
340 |
|
|
341 |
function mb_strrpos($haystack, $needle, $offset=0) |
|
342 |
{ |
|
343 |
return strrpos($haystack, $needle, $offset); |
|
344 |
} |
|
345 |
} |
|
346 |
|
e99991
|
347 |
/** |
A |
348 |
* intl replacement functions |
|
349 |
*/ |
|
350 |
|
|
351 |
if (!function_exists('idn_to_utf8')) |
|
352 |
{ |
|
353 |
function idn_to_utf8($domain, $flags=null) |
|
354 |
{ |
|
355 |
static $idn, $loaded; |
|
356 |
|
|
357 |
if (!$loaded) { |
|
358 |
$idn = new Net_IDNA2(); |
|
359 |
$loaded = true; |
|
360 |
} |
|
361 |
|
e8d5bd
|
362 |
if ($idn && $domain && preg_match('/(^|\.)xn--/i', $domain)) { |
e99991
|
363 |
try { |
A |
364 |
$domain = $idn->decode($domain); |
|
365 |
} |
|
366 |
catch (Exception $e) { |
|
367 |
} |
|
368 |
} |
|
369 |
return $domain; |
|
370 |
} |
|
371 |
} |
|
372 |
|
|
373 |
if (!function_exists('idn_to_ascii')) |
|
374 |
{ |
|
375 |
function idn_to_ascii($domain, $flags=null) |
|
376 |
{ |
|
377 |
static $idn, $loaded; |
|
378 |
|
|
379 |
if (!$loaded) { |
|
380 |
$idn = new Net_IDNA2(); |
|
381 |
$loaded = true; |
|
382 |
} |
|
383 |
|
|
384 |
if ($idn && $domain && preg_match('/[^\x20-\x7E]/', $domain)) { |
|
385 |
try { |
|
386 |
$domain = $idn->encode($domain); |
|
387 |
} |
|
388 |
catch (Exception $e) { |
|
389 |
} |
|
390 |
} |
|
391 |
return $domain; |
|
392 |
} |
|
393 |
} |
|
394 |
|
0c2596
|
395 |
/** |
A |
396 |
* Use PHP5 autoload for dynamic class loading |
|
397 |
* |
|
398 |
* @todo Make Zend, PEAR etc play with this |
|
399 |
* @todo Make our classes conform to a more straight forward CS. |
|
400 |
*/ |
|
401 |
function rcube_autoload($classname) |
|
402 |
{ |
|
403 |
$filename = preg_replace( |
|
404 |
array( |
|
405 |
'/MDB2_(.+)/', |
|
406 |
'/Mail_(.+)/', |
|
407 |
'/Net_(.+)/', |
|
408 |
'/Auth_(.+)/', |
|
409 |
'/^html_.+/', |
|
410 |
'/^utf8$/', |
|
411 |
), |
|
412 |
array( |
|
413 |
'MDB2/\\1', |
|
414 |
'Mail/\\1', |
|
415 |
'Net/\\1', |
|
416 |
'Auth/\\1', |
|
417 |
'html', |
|
418 |
'utf8.class', |
|
419 |
), |
|
420 |
$classname |
|
421 |
); |
|
422 |
|
|
423 |
if ($fp = @fopen("$filename.php", 'r', true)) { |
|
424 |
fclose($fp); |
1aceb9
|
425 |
include_once "$filename.php"; |
0c2596
|
426 |
return true; |
A |
427 |
} |
|
428 |
|
|
429 |
return false; |
|
430 |
} |
|
431 |
|
|
432 |
/** |
|
433 |
* Local callback function for PEAR errors |
|
434 |
*/ |
|
435 |
function rcube_pear_error($err) |
|
436 |
{ |
|
437 |
error_log(sprintf("%s (%s): %s", |
|
438 |
$err->getMessage(), |
|
439 |
$err->getCode(), |
|
440 |
$err->getUserinfo()), 0); |
|
441 |
} |