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