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