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