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