59177427 |
1 | <?php |
7350889b |
2 | |
f1ca21bd |
3 | /** |
35586184 |
4 | * strings.php |
5 | * |
76911253 |
6 | * Copyright (c) 1999-2003 The SquirrelMail Project Team |
35586184 |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * This code provides various string manipulation functions that are |
10 | * used by the rest of the Squirrelmail code. |
11 | * |
12 | * $Id$ |
13 | */ |
9374671f |
14 | |
35586184 |
15 | /** |
16 | * SquirrelMail version number -- DO NOT CHANGE |
17 | */ |
18 | global $version; |
87d8c725 |
19 | $version = '1.5.0 [CVS]'; |
d068c0ec |
20 | |
f1ca21bd |
21 | /** |
97bdc607 |
22 | * SquirrelMail internal version number -- DO NOT CHANGE |
23 | * $sm_internal_version = array (release, major, minor) |
24 | */ |
0567e4d5 |
25 | global $SQM_INTERNAL_VERSION; |
87d8c725 |
26 | $SQM_INTERNAL_VERSION = array(1,5,0); |
27 | |
87d8c725 |
28 | /** |
29 | * There can be a circular issue with includes, where the $version string is |
30 | * referenced by the include of global.php, etc. before it's defined. |
31 | * For that reason, bring in global.php AFTER we define the version strings. |
32 | */ |
33 | require_once(SM_PATH . 'functions/global.php'); |
97bdc607 |
34 | |
5cc0b70e |
35 | /** |
36 | * Wraps text at $wrap characters |
37 | * |
38 | * Has a problem with special HTML characters, so call this before |
39 | * you do character translation. |
40 | * |
41 | * Specifically, ' comes up as 5 characters instead of 1. |
42 | * This should not add newlines to the end of lines. |
43 | */ |
44 | function sqWordWrap(&$line, $wrap) { |
e842b215 |
45 | global $languages, $squirrelmail_language; |
46 | |
47 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
48 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { |
49 | if (mb_detect_encoding($line) != 'ASCII') { |
50 | $line = $languages[$squirrelmail_language]['XTRA_CODE']('wordwrap', $line, $wrap); |
51 | return; |
52 | } |
53 | } |
54 | |
5cc0b70e |
55 | ereg("^([\t >]*)([^\t >].*)?$", $line, $regs); |
56 | $beginning_spaces = $regs[1]; |
57 | if (isset($regs[2])) { |
58 | $words = explode(' ', $regs[2]); |
59 | } else { |
60 | $words = ''; |
61 | } |
f1ca21bd |
62 | |
5cc0b70e |
63 | $i = 0; |
64 | $line = $beginning_spaces; |
f1ca21bd |
65 | |
5cc0b70e |
66 | while ($i < count($words)) { |
67 | /* Force one word to be on a line (minimum) */ |
68 | $line .= $words[$i]; |
69 | $line_len = strlen($beginning_spaces) + strlen($words[$i]) + 2; |
70 | if (isset($words[$i + 1])) |
71 | $line_len += strlen($words[$i + 1]); |
72 | $i ++; |
f1ca21bd |
73 | |
5cc0b70e |
74 | /* Add more words (as long as they fit) */ |
75 | while ($line_len < $wrap && $i < count($words)) { |
76 | $line .= ' ' . $words[$i]; |
77 | $i++; |
78 | if (isset($words[$i])) |
79 | $line_len += strlen($words[$i]) + 1; |
80 | else |
81 | $line_len += 1; |
82 | } |
f1ca21bd |
83 | |
5cc0b70e |
84 | /* Skip spaces if they are the first thing on a continued line */ |
85 | while (!isset($words[$i]) && $i < count($words)) { |
86 | $i ++; |
87 | } |
f1ca21bd |
88 | |
5cc0b70e |
89 | /* Go to the next line if we have more to process */ |
90 | if ($i < count($words)) { |
e0858036 |
91 | $line .= "\n"; |
5cc0b70e |
92 | } |
93 | } |
94 | } |
95 | |
341abbd6 |
96 | /** |
97 | * Does the opposite of sqWordWrap() |
98 | */ |
99 | function sqUnWordWrap(&$body) { |
e842b215 |
100 | global $squirrelmail_language; |
f1ca21bd |
101 | |
e842b215 |
102 | if ($squirrelmail_language == 'ja_JP') { |
103 | return; |
104 | } |
105 | |
341abbd6 |
106 | $lines = explode("\n", $body); |
107 | $body = ''; |
108 | $PreviousSpaces = ''; |
109 | $cnt = count($lines); |
110 | for ($i = 0; $i < $cnt; $i ++) { |
111 | preg_match("/^([\t >]*)([^\t >].*)?$/", $lines[$i], $regs); |
112 | $CurrentSpaces = $regs[1]; |
113 | if (isset($regs[2])) { |
114 | $CurrentRest = $regs[2]; |
1e4a4feb |
115 | } else { |
f1ca21bd |
116 | $CurrentRest = ''; |
117 | } |
118 | |
341abbd6 |
119 | if ($i == 0) { |
120 | $PreviousSpaces = $CurrentSpaces; |
121 | $body = $lines[$i]; |
122 | } else if (($PreviousSpaces == $CurrentSpaces) /* Do the beginnings match */ |
123 | && (strlen($lines[$i - 1]) > 65) /* Over 65 characters long */ |
124 | && strlen($CurrentRest)) { /* and there's a line to continue with */ |
125 | $body .= ' ' . $CurrentRest; |
126 | } else { |
127 | $body .= "\n" . $lines[$i]; |
128 | $PreviousSpaces = $CurrentSpaces; |
129 | } |
130 | } |
131 | $body .= "\n"; |
132 | } |
133 | |
66239b65 |
134 | /** |
135 | * If $haystack is a full mailbox name and $needle is the mailbox |
136 | * separator character, returns the last part of the mailbox name. |
137 | */ |
138 | function readShortMailboxName($haystack, $needle) { |
97b1248c |
139 | |
66239b65 |
140 | if ($needle == '') { |
97b1248c |
141 | $elem = $haystack; |
142 | } else { |
f1ca21bd |
143 | $parts = explode($needle, $haystack); |
144 | $elem = array_pop($parts); |
145 | while ($elem == '' && count($parts)) { |
146 | $elem = array_pop($parts); |
147 | } |
66239b65 |
148 | } |
97b1248c |
149 | return( $elem ); |
66239b65 |
150 | } |
3302d0d4 |
151 | |
43fdb2a4 |
152 | function php_self () { |
961ca3d8 |
153 | if ( sqgetGlobalVar('REQUEST_URI', $req_uri, SQ_SERVER) && !empty($req_uri) ) { |
154 | return $req_uri; |
43fdb2a4 |
155 | } |
f1ca21bd |
156 | |
961ca3d8 |
157 | if ( sqgetGlobalVar('PHP_SELF', $php_self, SQ_SERVER) && !empty($php_self) ) { |
158 | return $php_self; |
f1ca21bd |
159 | } |
160 | |
961ca3d8 |
161 | return ''; |
43fdb2a4 |
162 | } |
163 | |
164 | |
66239b65 |
165 | /** |
166 | * This determines the location to forward to relative to your server. |
167 | * If this doesnt work correctly for you (although it should), you can |
168 | * remove all this code except the last two lines, and change the header() |
169 | * function to look something like this, customized to the location of |
170 | * SquirrelMail on your server: |
171 | * |
172 | * http://www.myhost.com/squirrelmail/src/login.php |
173 | */ |
174 | function get_location () { |
f1ca21bd |
175 | |
961ca3d8 |
176 | global $imap_server_type; |
238703be |
177 | |
4deb32f1 |
178 | /* Get the path, handle virtual directories */ |
f1ca21bd |
179 | if(strpos(php_self(), '?')) { |
180 | $path = substr(php_self(), 0, strpos(php_self(), '?')); |
181 | } else { |
182 | $path = php_self(); |
183 | } |
184 | $path = substr($path, 0, strrpos($path, '/')); |
238703be |
185 | if ( sqgetGlobalVar('sq_base_url', $full_url, SQ_SESSION) ) { |
186 | return $full_url . $path; |
187 | } |
188 | |
66239b65 |
189 | /* Check if this is a HTTPS or regular HTTP request. */ |
190 | $proto = 'http://'; |
f1ca21bd |
191 | |
66239b65 |
192 | /* |
193 | * If you have 'SSLOptions +StdEnvVars' in your apache config |
44827b4d |
194 | * OR if you have HTTPS=on in your HTTP_SERVER_VARS |
66239b65 |
195 | * OR if you are on port 443 |
196 | */ |
197 | $getEnvVar = getenv('HTTPS'); |
198 | if ((isset($getEnvVar) && !strcasecmp($getEnvVar, 'on')) || |
961ca3d8 |
199 | (sqgetGlobalVar('HTTPS', $https_on, SQ_SERVER) && !strcasecmp($https_on, 'on')) || |
200 | (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER) && $server_port == 443)) { |
8a549df2 |
201 | $proto = 'https://'; |
66239b65 |
202 | } |
f1ca21bd |
203 | |
4deb32f1 |
204 | /* Get the hostname from the Host header or server config. */ |
961ca3d8 |
205 | if ( !sqgetGlobalVar('HTTP_HOST', $host, SQ_SERVER) || empty($host) ) { |
206 | if ( !sqgetGlobalVar('SERVER_NAME', $host, SQ_SERVER) || empty($host) ) { |
207 | $host = ''; |
208 | } |
66239b65 |
209 | } |
f1ca21bd |
210 | |
66239b65 |
211 | $port = ''; |
212 | if (! strstr($host, ':')) { |
961ca3d8 |
213 | if (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER)) { |
f1ca21bd |
214 | if (($server_port != 80 && $proto == 'http://') || |
961ca3d8 |
215 | ($server_port != 443 && $proto == 'https://')) { |
216 | $port = sprintf(':%d', $server_port); |
66239b65 |
217 | } |
218 | } |
219 | } |
f1ca21bd |
220 | |
8de7f698 |
221 | /* this is a workaround for the weird macosx caching that |
222 | causes Apache to return 16080 as the port number, which causes |
223 | SM to bail */ |
f1ca21bd |
224 | |
8de7f698 |
225 | if ($imap_server_type == 'macosx' && $port == ':16080') { |
226 | $port = ''; |
227 | } |
f1ca21bd |
228 | |
238703be |
229 | /* Fallback is to omit the server name and use a relative */ |
230 | /* URI, although this is not RFC 2616 compliant. */ |
231 | $full_url = ($host ? $proto . $host . $port : ''); |
232 | sqsession_register($full_url, 'sq_base_url'); |
233 | return $full_url . $path; |
66239b65 |
234 | } |
dcaf2a49 |
235 | |
9374671f |
236 | |
66239b65 |
237 | /** |
238 | * These functions are used to encrypt the passowrd before it is |
239 | * stored in a cookie. |
240 | */ |
241 | function OneTimePadEncrypt ($string, $epad) { |
242 | $pad = base64_decode($epad); |
243 | $encrypted = ''; |
244 | for ($i = 0; $i < strlen ($string); $i++) { |
245 | $encrypted .= chr (ord($string[$i]) ^ ord($pad[$i])); |
246 | } |
f1ca21bd |
247 | |
66239b65 |
248 | return base64_encode($encrypted); |
249 | } |
250 | |
251 | function OneTimePadDecrypt ($string, $epad) { |
252 | $pad = base64_decode($epad); |
253 | $encrypted = base64_decode ($string); |
254 | $decrypted = ''; |
255 | for ($i = 0; $i < strlen ($encrypted); $i++) { |
256 | $decrypted .= chr (ord($encrypted[$i]) ^ ord($pad[$i])); |
257 | } |
f1ca21bd |
258 | |
66239b65 |
259 | return $decrypted; |
260 | } |
9374671f |
261 | |
9374671f |
262 | |
66239b65 |
263 | /** |
264 | * Randomize the mt_rand() function. Toss this in strings or integers |
265 | * and it will seed the generator appropriately. With strings, it is |
266 | * better to get them long. Use md5() to lengthen smaller strings. |
267 | */ |
268 | function sq_mt_seed($Val) { |
4deb32f1 |
269 | /* if mt_getrandmax() does not return a 2^n - 1 number, |
270 | this might not work well. This uses $Max as a bitmask. */ |
66239b65 |
271 | $Max = mt_getrandmax(); |
f1ca21bd |
272 | |
66239b65 |
273 | if (! is_int($Val)) { |
66239b65 |
274 | $Val = crc32($Val); |
66239b65 |
275 | } |
f1ca21bd |
276 | |
66239b65 |
277 | if ($Val < 0) { |
278 | $Val *= -1; |
279 | } |
f1ca21bd |
280 | |
66239b65 |
281 | if ($Val = 0) { |
282 | return; |
283 | } |
f1ca21bd |
284 | |
66239b65 |
285 | mt_srand(($Val ^ mt_rand(0, $Max)) & $Max); |
286 | } |
9374671f |
287 | |
9374671f |
288 | |
66239b65 |
289 | /** |
290 | * This function initializes the random number generator fairly well. |
291 | * It also only initializes it once, so you don't accidentally get |
292 | * the same 'random' numbers twice in one session. |
293 | */ |
294 | function sq_mt_randomize() { |
66239b65 |
295 | static $randomized; |
f1ca21bd |
296 | |
66239b65 |
297 | if ($randomized) { |
298 | return; |
299 | } |
f1ca21bd |
300 | |
66239b65 |
301 | /* Global. */ |
961ca3d8 |
302 | sqgetGlobalVar('REMOTE_PORT', $remote_port, SQ_SERVER); |
303 | sqgetGlobalVar('REMOTE_ADDR', $remote_addr, SQ_SERVER); |
66239b65 |
304 | sq_mt_seed((int)((double) microtime() * 1000000)); |
961ca3d8 |
305 | sq_mt_seed(md5($remote_port . $remote_addr . getmypid())); |
f1ca21bd |
306 | |
66239b65 |
307 | /* getrusage */ |
308 | if (function_exists('getrusage')) { |
4deb32f1 |
309 | /* Avoid warnings with Win32 */ |
66239b65 |
310 | $dat = @getrusage(); |
311 | if (isset($dat) && is_array($dat)) { |
821a8e9c |
312 | $Str = ''; |
313 | foreach ($dat as $k => $v) |
66239b65 |
314 | { |
315 | $Str .= $k . $v; |
316 | } |
821a8e9c |
317 | sq_mt_seed(md5($Str)); |
66239b65 |
318 | } |
319 | } |
f1ca21bd |
320 | |
961ca3d8 |
321 | if(sqgetGlobalVar('UNIQUE_ID', $unique_id, SQ_SERVER)) { |
322 | sq_mt_seed(md5($unique_id)); |
0b97a708 |
323 | } |
f1ca21bd |
324 | |
66239b65 |
325 | $randomized = 1; |
326 | } |
327 | |
328 | function OneTimePadCreate ($length=100) { |
329 | sq_mt_randomize(); |
f1ca21bd |
330 | |
66239b65 |
331 | $pad = ''; |
332 | for ($i = 0; $i < $length; $i++) { |
333 | $pad .= chr(mt_rand(0,255)); |
334 | } |
f1ca21bd |
335 | |
66239b65 |
336 | return base64_encode($pad); |
337 | } |
9374671f |
338 | |
66239b65 |
339 | /** |
340 | * Returns a string showing the size of the message/attachment. |
341 | */ |
342 | function show_readable_size($bytes) { |
343 | $bytes /= 1024; |
344 | $type = 'k'; |
f1ca21bd |
345 | |
66239b65 |
346 | if ($bytes / 1024 > 1) { |
347 | $bytes /= 1024; |
e5f1e71c |
348 | $type = 'M'; |
66239b65 |
349 | } |
f1ca21bd |
350 | |
66239b65 |
351 | if ($bytes < 10) { |
352 | $bytes *= 10; |
353 | settype($bytes, 'integer'); |
354 | $bytes /= 10; |
355 | } else { |
356 | settype($bytes, 'integer'); |
357 | } |
f1ca21bd |
358 | |
66239b65 |
359 | return $bytes . '<small> ' . $type . '</small>'; |
360 | } |
9374671f |
361 | |
66239b65 |
362 | /** |
363 | * Generates a random string from the caracter set you pass in |
364 | * |
365 | * Flags: |
366 | * 1 = add lowercase a-z to $chars |
367 | * 2 = add uppercase A-Z to $chars |
368 | * 4 = add numbers 0-9 to $chars |
369 | */ |
9374671f |
370 | |
66239b65 |
371 | function GenerateRandomString($size, $chars, $flags = 0) { |
372 | if ($flags & 0x1) { |
373 | $chars .= 'abcdefghijklmnopqrstuvwxyz'; |
374 | } |
375 | if ($flags & 0x2) { |
376 | $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
377 | } |
378 | if ($flags & 0x4) { |
379 | $chars .= '0123456789'; |
380 | } |
f1ca21bd |
381 | |
66239b65 |
382 | if (($size < 1) || (strlen($chars) < 1)) { |
383 | return ''; |
384 | } |
ff4f08ff |
385 | |
4deb32f1 |
386 | sq_mt_randomize(); /* Initialize the random number generator */ |
ff4f08ff |
387 | |
4deb32f1 |
388 | $String = ''; |
ff4f08ff |
389 | $j = strlen( $chars ) - 1; |
66239b65 |
390 | while (strlen($String) < $size) { |
ff4f08ff |
391 | $String .= $chars{mt_rand(0, $j)}; |
66239b65 |
392 | } |
ff4f08ff |
393 | |
66239b65 |
394 | return $String; |
395 | } |
9374671f |
396 | |
fbb76d0e |
397 | function quoteimap($str) { |
66239b65 |
398 | return ereg_replace('(["\\])', '\\\\1', $str); |
399 | } |
1899535f |
400 | |
66239b65 |
401 | /** |
402 | * Trims every element in the array |
403 | */ |
404 | function TrimArray(&$array) { |
405 | foreach ($array as $k => $v) { |
406 | global $$k; |
407 | if (is_array($$k)) { |
408 | foreach ($$k as $k2 => $v2) { |
409 | $$k[$k2] = substr($v2, 1); |
23d6bd09 |
410 | } |
66239b65 |
411 | } else { |
412 | $$k = substr($v, 1); |
23d6bd09 |
413 | } |
f1ca21bd |
414 | |
4deb32f1 |
415 | /* Re-assign back to array. */ |
66239b65 |
416 | $array[$k] = $$k; |
417 | } |
f1ca21bd |
418 | } |
23d6bd09 |
419 | |
d62c4938 |
420 | /* returns a link to the compose-page, taking in consideration |
421 | * the compose_in_new and javascript settings. */ |
422 | function makeComposeLink($url, $text = null) |
423 | { |
424 | global $compose_new_win,$javascript_on; |
425 | |
426 | if(!$text) { |
427 | $text = _("Compose"); |
428 | } |
429 | |
430 | if($compose_new_win != '1') { |
431 | return makeInternalLink($url, $text, 'right'); |
432 | } |
433 | |
434 | /* if we can use JS, use the fancy window, else just open a new one HTML-style */ |
435 | if($javascript_on) { |
436 | sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION); |
437 | return '<a href="javascript:void(0)" onclick="comp_in_new(\''.$base_uri.$url.'\')">'. $text.'</a>'; |
438 | } |
439 | |
440 | return makeInternalLink($url, $text, '_blank'); |
441 | } |
442 | |
f1ca21bd |
443 | /** |
7fe09a30 |
444 | * sm_print_r($some_variable, [$some_other_variable [, ...]]); |
50cc40fe |
445 | * Debugging function - does the same as print_r, but makes sure special |
446 | * characters are converted to htmlentities first. This will allow |
447 | * values like <some@email.address> to be displayed. |
448 | * The output is wrapped in <pre> and </pre> tags. |
449 | */ |
7fe09a30 |
450 | function sm_print_r() { |
50cc40fe |
451 | ob_start(); // Buffer output |
7fe09a30 |
452 | foreach(func_get_args() as $var) { |
453 | print_r($var); |
454 | echo "\n"; |
455 | } |
50cc40fe |
456 | $buffer = ob_get_contents(); // Grab the print_r output |
457 | ob_end_clean(); // Silently discard the output & stop buffering |
f1ca21bd |
458 | print '<pre>'; |
50cc40fe |
459 | print htmlentities($buffer); |
f1ca21bd |
460 | print '</pre>'; |
50cc40fe |
461 | } |
462 | |
43fdb2a4 |
463 | $PHP_SELF = php_self(); |
ef1932a4 |
464 | ?> |