59177427 |
1 | <?php |
7350889b |
2 | |
35586184 |
3 | /** |
4 | * strings.php |
5 | * |
15e6162e |
6 | * Copyright (c) 1999-2002 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 | /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/ |
17 | /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/ |
18 | /*** + Base level indent should begin at left margin, as ***/ |
19 | /*** the comment and $version stuff below. ***/ |
20 | /*** + All identation should consist of four space blocks ***/ |
21 | /*** + Tab characters are evil. ***/ |
22 | /*** + all comments should use "slash-star ... star-slash" ***/ |
23 | /*** style -- no pound characters, no slash-slash style ***/ |
24 | /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/ |
25 | /*** ALWAYS USE { AND } CHARACTERS!!! ***/ |
26 | /*** + Please use ' instead of ", when possible. Note " ***/ |
27 | /*** should always be used in _( ) function calls. ***/ |
28 | /*** Thank you for your help making the SM code more readable. ***/ |
29 | /*****************************************************************/ |
30 | |
31 | /** |
32 | * SquirrelMail version number -- DO NOT CHANGE |
33 | */ |
34 | global $version; |
ff4eb281 |
35 | $version = '1.2.2 [cvs]'; |
d068c0ec |
36 | |
66239b65 |
37 | /** |
38 | * If $haystack is a full mailbox name and $needle is the mailbox |
39 | * separator character, returns the last part of the mailbox name. |
40 | */ |
41 | function readShortMailboxName($haystack, $needle) { |
42 | if ($needle == '') { |
43 | return $haystack; |
44 | } |
45 | $parts = explode($needle, $haystack); |
46 | $elem = array_pop($parts); |
47 | while ($elem == '' && count($parts)) { |
48 | $elem = array_pop($parts); |
49 | } |
50 | return $elem; |
51 | } |
3302d0d4 |
52 | |
66239b65 |
53 | /** |
54 | * If $haystack is a full mailbox name, and $needle is the mailbox |
55 | * separator character, returns the second last part of the full |
56 | * mailbox name (i.e. the mailbox's parent mailbox) |
57 | */ |
58 | function readMailboxParent($haystack, $needle) { |
59 | if ($needle == '') return ''; |
60 | $parts = explode($needle, $haystack); |
61 | $elem = array_pop($parts); |
62 | while ($elem == '' && count($parts)) { |
63 | $elem = array_pop($parts); |
64 | } |
65 | return join($needle, $parts); |
66 | } |
5bdd7223 |
67 | |
66239b65 |
68 | /** |
d8943c57 |
69 | * Returns the index of the first chunk of string $haystack that |
70 | * starts with non-white-space character, starting at position $pos. |
71 | * If there is no such chunk, returns -1. |
66239b65 |
72 | */ |
73 | function next_pos_minus_white ($haystack, $pos) { |
d8943c57 |
74 | $len = strlen($haystack); |
e16d13af |
75 | while ($pos < $len) { |
76 | /* Get the next character. */ |
77 | $c = substr($haystack, $pos, 1); |
78 | |
79 | /* Check the next character. */ |
80 | if (($c != ' ') && ($c != "\t") && ($c != "\n") && ($c != "\r")) { |
d8943c57 |
81 | return $pos; |
82 | } |
e16d13af |
83 | |
84 | /* Increment position in string. */ |
85 | ++$pos; |
66239b65 |
86 | } |
d8943c57 |
87 | return -1; |
66239b65 |
88 | } |
9374671f |
89 | |
66239b65 |
90 | /** |
91 | * Wraps text at $wrap characters |
92 | * |
93 | * Has a problem with special HTML characters, so call this before |
94 | * you do character translation. |
95 | * |
96 | * Specifically, ' comes up as 5 characters instead of 1. |
97 | * This should not add newlines to the end of lines. |
98 | */ |
99 | function sqWordWrap(&$line, $wrap) { |
100 | ereg("^([\t >]*)([^\t >].*)?$", $line, $regs); |
101 | $beginning_spaces = $regs[1]; |
102 | if (isset($regs[2])) { |
103 | $words = explode(' ', $regs[2]); |
104 | } else { |
105 | $words = ""; |
106 | } |
107 | |
108 | $i = 0; |
109 | $line = $beginning_spaces; |
110 | |
111 | while ($i < count($words)) { |
112 | // Force one word to be on a line (minimum) |
113 | $line .= $words[$i]; |
114 | $line_len = strlen($beginning_spaces) + strlen($words[$i]) + 2; |
115 | if (isset($words[$i + 1])) |
116 | $line_len += strlen($words[$i + 1]); |
117 | $i ++; |
118 | |
119 | // Add more words (as long as they fit) |
120 | while ($line_len < $wrap && $i < count($words)) { |
bcad90fe |
121 | $line .= ' ' . $words[$i]; |
122 | $i++; |
66239b65 |
123 | if (isset($words[$i])) |
48f8ce2f |
124 | $line_len += strlen($words[$i]) + 1; |
66239b65 |
125 | else |
126 | $line_len += 1; |
127 | } |
128 | |
129 | // Skip spaces if they are the first thing on a continued line |
130 | while (!isset($words[$i]) && $i < count($words)) { |
bcad90fe |
131 | $i ++; |
66239b65 |
132 | } |
133 | |
134 | // Go to the next line if we have more to process |
135 | if ($i < count($words)) { |
5be8bbd8 |
136 | $line .= "\n" . $beginning_spaces; |
66239b65 |
137 | } |
138 | } |
139 | } |
9374671f |
140 | |
23d6bd09 |
141 | |
66239b65 |
142 | /** |
143 | * Does the opposite of sqWordWrap() |
144 | */ |
145 | function sqUnWordWrap(&$body) { |
146 | $lines = explode("\n", $body); |
147 | $body = ""; |
148 | $PreviousSpaces = ""; |
149 | for ($i = 0; $i < count($lines); $i ++) { |
150 | ereg("^([\t >]*)([^\t >].*)?$", $lines[$i], $regs); |
151 | $CurrentSpaces = $regs[1]; |
152 | if (isset($regs[2])) { |
153 | $CurrentRest = $regs[2]; |
154 | } |
155 | |
156 | if ($i == 0) { |
157 | $PreviousSpaces = $CurrentSpaces; |
158 | $body = $lines[$i]; |
159 | } else if (($PreviousSpaces == $CurrentSpaces) // Do the beginnings match |
160 | && (strlen($lines[$i - 1]) > 65) // Over 65 characters long |
161 | && strlen($CurrentRest)) { // and there's a line to continue with |
162 | $body .= ' ' . $CurrentRest; |
163 | } else { |
164 | $body .= "\n" . $lines[$i]; |
165 | $PreviousSpaces = $CurrentSpaces; |
166 | } |
167 | } |
168 | $body .= "\n"; |
169 | } |
9374671f |
170 | |
40ee9452 |
171 | |
66239b65 |
172 | /** |
173 | * Returns an array of email addresses. |
174 | * Be cautious of "user@host.com" |
175 | */ |
176 | function parseAddrs($text) { |
177 | if (trim($text) == "") |
178 | return array(); |
179 | $text = str_replace(' ', '', $text); |
180 | $text = ereg_replace('"[^"]*"', '', $text); |
181 | $text = ereg_replace('\\([^\\)]*\\)', '', $text); |
182 | $text = str_replace(',', ';', $text); |
183 | $array = explode(';', $text); |
184 | for ($i = 0; $i < count ($array); $i++) { |
185 | $array[$i] = eregi_replace ("^.*[<]", '', $array[$i]); |
186 | $array[$i] = eregi_replace ("[>].*$", '', $array[$i]); |
187 | } |
188 | return $array; |
189 | } |
40ee9452 |
190 | |
66239b65 |
191 | /** |
192 | * Returns a line of comma separated email addresses from an array. |
193 | */ |
194 | function getLineOfAddrs($array) { |
195 | if (is_array($array)) { |
8a549df2 |
196 | $to_line = implode(', ', $array); |
d46b103b |
197 | $to_line = ereg_replace(', (, )+', ', ', $to_line); |
198 | $to_line = trim(ereg_replace('^, ', '', $to_line)); |
199 | if( substr( $to_line, -1 ) == ',' ) |
200 | $to_line = substr( $to_line, 0, -1 ); |
66239b65 |
201 | } else { |
8a549df2 |
202 | $to_line = ''; |
66239b65 |
203 | } |
204 | |
205 | return( $to_line ); |
206 | } |
207 | |
208 | function translateText(&$body, $wrap_at, $charset) { |
209 | global $where, $what; // from searching |
210 | global $color; // color theme |
211 | |
212 | require_once('../functions/url_parser.php'); |
213 | |
214 | $body_ary = explode("\n", $body); |
215 | $PriorQuotes = 0; |
216 | for ($i=0; $i < count($body_ary); $i++) { |
217 | $line = $body_ary[$i]; |
218 | if (strlen($line) - 2 >= $wrap_at) { |
9374671f |
219 | sqWordWrap($line, $wrap_at); |
66239b65 |
220 | } |
221 | $line = charset_decode($charset, $line); |
222 | $line = str_replace("\t", ' ', $line); |
223 | |
224 | parseUrl ($line); |
225 | |
226 | $Quotes = 0; |
227 | $pos = 0; |
228 | while (1) { |
229 | if ($line[$pos] == ' ') { |
8ceb637a |
230 | $pos ++; |
66239b65 |
231 | } else if (strpos($line, '>', $pos) === $pos) { |
9eea179c |
232 | $pos += 4; |
233 | $Quotes ++; |
66239b65 |
234 | } else { |
235 | break; |
236 | } |
237 | } |
238 | |
239 | if ($Quotes > 1) { |
240 | if (! isset($color[14])) { |
241 | $color[14] = '#FF0000'; |
23d6bd09 |
242 | } |
0391864c |
243 | $line = '<FONT COLOR="' . $color[14] . '">' . $line . '</FONT>'; |
66239b65 |
244 | } elseif ($Quotes) { |
245 | if (! isset($color[13])) { |
246 | $color[13] = '#800000'; |
23d6bd09 |
247 | } |
24194051 |
248 | $line = '<FONT COLOR="' . $color[13] . '">' . $line . '</FONT>'; |
66239b65 |
249 | } |
250 | |
251 | $body_ary[$i] = $line; |
252 | } |
253 | $body = '<pre>' . implode("\n", $body_ary) . '</pre>'; |
254 | } |
9374671f |
255 | |
66239b65 |
256 | function find_mailbox_name ($mailbox) { |
257 | if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs)) |
258 | return $regs[1]; |
259 | ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs); |
260 | return $regs[1]; |
261 | |
262 | } |
23d6bd09 |
263 | |
66239b65 |
264 | /** |
265 | * This determines the location to forward to relative to your server. |
266 | * If this doesnt work correctly for you (although it should), you can |
267 | * remove all this code except the last two lines, and change the header() |
268 | * function to look something like this, customized to the location of |
269 | * SquirrelMail on your server: |
270 | * |
271 | * http://www.myhost.com/squirrelmail/src/login.php |
272 | */ |
273 | function get_location () { |
274 | |
275 | global $PHP_SELF, $SERVER_NAME, $HTTP_HOST, $SERVER_PORT, |
276 | $HTTP_SERVER_VARS; |
277 | |
278 | /* Get the path. */ |
279 | $path = substr($PHP_SELF, 0, strrpos($PHP_SELF, '/')); |
280 | |
281 | /* Check if this is a HTTPS or regular HTTP request. */ |
282 | $proto = 'http://'; |
283 | |
284 | /* |
285 | * If you have 'SSLOptions +StdEnvVars' in your apache config |
286 | * OR if you have HTTPS in your HTTP_SERVER_VARS |
287 | * OR if you are on port 443 |
288 | */ |
289 | $getEnvVar = getenv('HTTPS'); |
290 | if ((isset($getEnvVar) && !strcasecmp($getEnvVar, 'on')) || |
291 | (isset($HTTP_SERVER_VARS['HTTPS'])) || |
292 | (isset($HTTP_SERVER_VARS['SERVER_PORT']) && |
293 | $HTTP_SERVER_VARS['SERVER_PORT'] == 443)) { |
8a549df2 |
294 | $proto = 'https://'; |
66239b65 |
295 | } |
296 | |
297 | // Get the hostname from the Host header or server config. |
298 | $host = ''; |
299 | if (isset($HTTP_HOST) && !empty($HTTP_HOST)) { |
300 | $host = $HTTP_HOST; |
301 | } else if (isset($SERVER_NAME) && !empty($SERVER_NAME)) { |
302 | $host = $SERVER_NAME; |
303 | } |
304 | |
305 | $port = ''; |
306 | if (! strstr($host, ':')) { |
307 | if (isset($SERVER_PORT)) { |
308 | if (($SERVER_PORT != 80 && $proto == 'http://') |
309 | || ($SERVER_PORT != 443 && $proto == 'https://')) { |
310 | $port = sprintf(':%d', $SERVER_PORT); |
311 | } |
312 | } |
313 | } |
314 | |
315 | /* Fallback is to omit the server name and use a relative */ |
316 | /* URI, although this is not RFC 2616 compliant. */ |
317 | return ($host ? $proto . $host . $port . $path : $path); |
318 | } |
dcaf2a49 |
319 | |
9374671f |
320 | |
66239b65 |
321 | /** |
322 | * These functions are used to encrypt the passowrd before it is |
323 | * stored in a cookie. |
324 | */ |
325 | function OneTimePadEncrypt ($string, $epad) { |
326 | $pad = base64_decode($epad); |
327 | $encrypted = ''; |
328 | for ($i = 0; $i < strlen ($string); $i++) { |
329 | $encrypted .= chr (ord($string[$i]) ^ ord($pad[$i])); |
330 | } |
331 | |
332 | return base64_encode($encrypted); |
333 | } |
334 | |
335 | function OneTimePadDecrypt ($string, $epad) { |
336 | $pad = base64_decode($epad); |
337 | $encrypted = base64_decode ($string); |
338 | $decrypted = ''; |
339 | for ($i = 0; $i < strlen ($encrypted); $i++) { |
340 | $decrypted .= chr (ord($encrypted[$i]) ^ ord($pad[$i])); |
341 | } |
342 | |
343 | return $decrypted; |
344 | } |
9374671f |
345 | |
9374671f |
346 | |
66239b65 |
347 | /** |
348 | * Randomize the mt_rand() function. Toss this in strings or integers |
349 | * and it will seed the generator appropriately. With strings, it is |
350 | * better to get them long. Use md5() to lengthen smaller strings. |
351 | */ |
352 | function sq_mt_seed($Val) { |
353 | // if mt_getrandmax() does not return a 2^n - 1 number, |
354 | // this might not work well. This uses $Max as a bitmask. |
355 | $Max = mt_getrandmax(); |
356 | |
357 | if (! is_int($Val)) { |
358 | if (function_exists('crc32')) { |
359 | $Val = crc32($Val); |
360 | } else { |
361 | $Str = $Val; |
362 | $Pos = 0; |
363 | $Val = 0; |
364 | $Mask = $Max / 2; |
365 | $HighBit = $Max ^ $Mask; |
366 | while ($Pos < strlen($Str)) { |
367 | if ($Val & $HighBit) { |
368 | $Val = (($Val & $Mask) << 1) + 1; |
369 | } else { |
370 | $Val = ($Val & $Mask) << 1; |
371 | } |
372 | $Val ^= $Str[$Pos]; |
373 | $Pos ++; |
374 | } |
375 | } |
376 | } |
377 | |
378 | if ($Val < 0) { |
379 | $Val *= -1; |
380 | } |
381 | |
382 | if ($Val = 0) { |
383 | return; |
384 | } |
385 | |
386 | mt_srand(($Val ^ mt_rand(0, $Max)) & $Max); |
387 | } |
9374671f |
388 | |
9374671f |
389 | |
66239b65 |
390 | /** |
391 | * This function initializes the random number generator fairly well. |
392 | * It also only initializes it once, so you don't accidentally get |
393 | * the same 'random' numbers twice in one session. |
394 | */ |
395 | function sq_mt_randomize() { |
396 | global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID; |
397 | static $randomized; |
398 | |
399 | if ($randomized) { |
400 | return; |
401 | } |
402 | |
403 | /* Global. */ |
404 | sq_mt_seed((int)((double) microtime() * 1000000)); |
405 | sq_mt_seed(md5($REMOTE_PORT . $REMOTE_ADDR . getmypid())); |
406 | |
407 | /* getrusage */ |
408 | if (function_exists('getrusage')) { |
409 | // Avoid warnings with Win32 |
410 | $dat = @getrusage(); |
411 | if (isset($dat) && is_array($dat)) { |
821a8e9c |
412 | $Str = ''; |
413 | foreach ($dat as $k => $v) |
66239b65 |
414 | { |
415 | $Str .= $k . $v; |
416 | } |
821a8e9c |
417 | sq_mt_seed(md5($Str)); |
66239b65 |
418 | } |
419 | } |
420 | |
421 | // Apache-specific |
422 | sq_mt_seed(md5($UNIQUE_ID)); |
423 | |
424 | $randomized = 1; |
425 | } |
426 | |
427 | function OneTimePadCreate ($length=100) { |
428 | sq_mt_randomize(); |
429 | |
430 | $pad = ''; |
431 | for ($i = 0; $i < $length; $i++) { |
432 | $pad .= chr(mt_rand(0,255)); |
433 | } |
434 | |
435 | return base64_encode($pad); |
436 | } |
9374671f |
437 | |
66239b65 |
438 | /** |
439 | * Check if we have a required PHP-version. Return TRUE if we do, |
440 | * or FALSE if we don't. |
441 | * |
442 | * To check for 4.0.1, use sqCheckPHPVersion(4,0,1) |
443 | * To check for 4.0b3, use sqCheckPHPVersion(4,0,-3) |
444 | * |
445 | * Does not handle betas like 4.0.1b1 or development versions |
446 | */ |
447 | function sqCheckPHPVersion($major, $minor, $release) { |
448 | |
449 | $ver = phpversion(); |
450 | eregi('^([0-9]+)\\.([0-9]+)(.*)', $ver, $regs); |
451 | |
452 | /* Parse the version string. */ |
453 | $vmajor = strval($regs[1]); |
454 | $vminor = strval($regs[2]); |
455 | $vrel = $regs[3]; |
456 | if($vrel[0] == ".") { |
457 | $vrel = strval(substr($vrel, 1)); |
458 | } |
459 | if($vrel[0] == 'b' || $vrel[0] == 'B') { |
460 | $vrel = - strval(substr($vrel, 1)); |
461 | } |
462 | if($vrel[0] == 'r' || $vrel[0] == 'R') { |
463 | $vrel = - strval(substr($vrel, 2))/10; |
464 | } |
465 | |
466 | /* Compare major version. */ |
467 | if ($vmajor < $major) { return false; } |
468 | if ($vmajor > $major) { return true; } |
469 | |
470 | /* Major is the same. Compare minor. */ |
471 | if ($vminor < $minor) { return false; } |
472 | if ($vminor > $minor) { return true; } |
473 | |
474 | /* Major and minor is the same as the required one. Compare release */ |
475 | if ($vrel >= 0 && $release >= 0) { // Neither are beta |
476 | if($vrel < $release) return false; |
477 | } else if($vrel >= 0 && $release < 0) { // This is not beta, required is beta |
478 | return true; |
479 | } else if($vrel < 0 && $release >= 0){ // This is beta, require not beta |
480 | return false; |
481 | } else { // Both are beta |
482 | if($vrel > $release) return false; |
483 | } |
484 | |
485 | return true; |
486 | } |
9374671f |
487 | |
66239b65 |
488 | /** |
489 | * Returns a string showing the size of the message/attachment. |
490 | */ |
491 | function show_readable_size($bytes) { |
492 | $bytes /= 1024; |
493 | $type = 'k'; |
494 | |
495 | if ($bytes / 1024 > 1) { |
496 | $bytes /= 1024; |
497 | $type = 'm'; |
498 | } |
499 | |
500 | if ($bytes < 10) { |
501 | $bytes *= 10; |
502 | settype($bytes, 'integer'); |
503 | $bytes /= 10; |
504 | } else { |
505 | settype($bytes, 'integer'); |
506 | } |
507 | |
508 | return $bytes . '<small> ' . $type . '</small>'; |
509 | } |
9374671f |
510 | |
66239b65 |
511 | /** |
512 | * Generates a random string from the caracter set you pass in |
513 | * |
514 | * Flags: |
515 | * 1 = add lowercase a-z to $chars |
516 | * 2 = add uppercase A-Z to $chars |
517 | * 4 = add numbers 0-9 to $chars |
518 | */ |
9374671f |
519 | |
66239b65 |
520 | function GenerateRandomString($size, $chars, $flags = 0) { |
521 | if ($flags & 0x1) { |
522 | $chars .= 'abcdefghijklmnopqrstuvwxyz'; |
523 | } |
524 | if ($flags & 0x2) { |
525 | $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
526 | } |
527 | if ($flags & 0x4) { |
528 | $chars .= '0123456789'; |
529 | } |
530 | |
531 | if (($size < 1) || (strlen($chars) < 1)) { |
532 | return ''; |
533 | } |
534 | |
535 | sq_mt_randomize(); // Initialize the random number generator |
536 | |
537 | $String = ""; |
538 | while (strlen($String) < $size) { |
539 | $String .= $chars[mt_rand(0, strlen($chars))]; |
540 | } |
541 | |
542 | return $String; |
543 | } |
9374671f |
544 | |
66239b65 |
545 | function quoteIMAP($str) { |
546 | return ereg_replace('(["\\])', '\\\\1', $str); |
547 | } |
1899535f |
548 | |
66239b65 |
549 | /** |
550 | * Trims every element in the array |
551 | */ |
552 | function TrimArray(&$array) { |
553 | foreach ($array as $k => $v) { |
554 | global $$k; |
555 | if (is_array($$k)) { |
556 | foreach ($$k as $k2 => $v2) { |
557 | $$k[$k2] = substr($v2, 1); |
23d6bd09 |
558 | } |
66239b65 |
559 | } else { |
560 | $$k = substr($v, 1); |
23d6bd09 |
561 | } |
66239b65 |
562 | |
563 | /* Re-assign back to array. */ |
564 | $array[$k] = $$k; |
565 | } |
566 | } |
23d6bd09 |
567 | |
66239b65 |
568 | /** |
569 | * Removes slashes from every element in the array |
570 | */ |
571 | function RemoveSlashes(&$array) { |
572 | foreach ($array as $k => $v) { |
573 | global $$k; |
574 | if (is_array($$k)) { |
575 | foreach ($$k as $k2 => $v2) { |
576 | $newArray[stripslashes($k2)] = stripslashes($v2); |
577 | } |
578 | $$k = $newArray; |
579 | } else { |
580 | $$k = stripslashes($v); |
23d6bd09 |
581 | } |
66239b65 |
582 | |
583 | /* Re-assign back to the array. */ |
584 | $array[$k] = $$k; |
23d6bd09 |
585 | } |
66239b65 |
586 | } |
23d6bd09 |
587 | |
15e6162e |
588 | ?> |