| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * strings.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2003 The SquirrelMail Project 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$ |
| 13 | */ |
| 14 | |
| 15 | |
| 16 | /** |
| 17 | * SquirrelMail version number -- DO NOT CHANGE |
| 18 | */ |
| 19 | global $version; |
| 20 | $version = '1.5.0 [CVS]'; |
| 21 | |
| 22 | /** |
| 23 | * SquirrelMail internal version number -- DO NOT CHANGE |
| 24 | * $sm_internal_version = array (release, major, minor) |
| 25 | */ |
| 26 | global $SQM_INTERNAL_VERSION; |
| 27 | $SQM_INTERNAL_VERSION = array(1,5,0); |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * There can be a circular issue with includes, where the $version string is |
| 32 | * referenced by the include of global.php, etc. before it's defined. |
| 33 | * For that reason, bring in global.php AFTER we define the version strings. |
| 34 | */ |
| 35 | require_once(SM_PATH . 'functions/global.php'); |
| 36 | |
| 37 | /** |
| 38 | * Wraps text at $wrap characters |
| 39 | * |
| 40 | * Has a problem with special HTML characters, so call this before |
| 41 | * you do character translation. |
| 42 | * |
| 43 | * Specifically, ' comes up as 5 characters instead of 1. |
| 44 | * This should not add newlines to the end of lines. |
| 45 | */ |
| 46 | function sqWordWrap(&$line, $wrap) { |
| 47 | global $languages, $squirrelmail_language; |
| 48 | |
| 49 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
| 50 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { |
| 51 | if (mb_detect_encoding($line) != 'ASCII') { |
| 52 | $line = $languages[$squirrelmail_language]['XTRA_CODE']('wordwrap', $line, $wrap); |
| 53 | return; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | ereg("^([\t >]*)([^\t >].*)?$", $line, $regs); |
| 58 | $beginning_spaces = $regs[1]; |
| 59 | if (isset($regs[2])) { |
| 60 | $words = explode(' ', $regs[2]); |
| 61 | } else { |
| 62 | $words = ''; |
| 63 | } |
| 64 | |
| 65 | $i = 0; |
| 66 | $line = $beginning_spaces; |
| 67 | |
| 68 | while ($i < count($words)) { |
| 69 | /* Force one word to be on a line (minimum) */ |
| 70 | $line .= $words[$i]; |
| 71 | $line_len = strlen($beginning_spaces) + strlen($words[$i]) + 2; |
| 72 | if (isset($words[$i + 1])) |
| 73 | $line_len += strlen($words[$i + 1]); |
| 74 | $i ++; |
| 75 | |
| 76 | /* Add more words (as long as they fit) */ |
| 77 | while ($line_len < $wrap && $i < count($words)) { |
| 78 | $line .= ' ' . $words[$i]; |
| 79 | $i++; |
| 80 | if (isset($words[$i])) |
| 81 | $line_len += strlen($words[$i]) + 1; |
| 82 | else |
| 83 | $line_len += 1; |
| 84 | } |
| 85 | |
| 86 | /* Skip spaces if they are the first thing on a continued line */ |
| 87 | while (!isset($words[$i]) && $i < count($words)) { |
| 88 | $i ++; |
| 89 | } |
| 90 | |
| 91 | /* Go to the next line if we have more to process */ |
| 92 | if ($i < count($words)) { |
| 93 | $line .= "\n"; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Does the opposite of sqWordWrap() |
| 100 | */ |
| 101 | function sqUnWordWrap(&$body) { |
| 102 | global $squirrelmail_language; |
| 103 | |
| 104 | if ($squirrelmail_language == 'ja_JP') { |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | $lines = explode("\n", $body); |
| 109 | $body = ''; |
| 110 | $PreviousSpaces = ''; |
| 111 | $cnt = count($lines); |
| 112 | for ($i = 0; $i < $cnt; $i ++) { |
| 113 | preg_match("/^([\t >]*)([^\t >].*)?$/", $lines[$i], $regs); |
| 114 | $CurrentSpaces = $regs[1]; |
| 115 | if (isset($regs[2])) { |
| 116 | $CurrentRest = $regs[2]; |
| 117 | } else { |
| 118 | $CurrentRest = ''; |
| 119 | } |
| 120 | |
| 121 | if ($i == 0) { |
| 122 | $PreviousSpaces = $CurrentSpaces; |
| 123 | $body = $lines[$i]; |
| 124 | } else if (($PreviousSpaces == $CurrentSpaces) /* Do the beginnings match */ |
| 125 | && (strlen($lines[$i - 1]) > 65) /* Over 65 characters long */ |
| 126 | && strlen($CurrentRest)) { /* and there's a line to continue with */ |
| 127 | $body .= ' ' . $CurrentRest; |
| 128 | } else { |
| 129 | $body .= "\n" . $lines[$i]; |
| 130 | $PreviousSpaces = $CurrentSpaces; |
| 131 | } |
| 132 | } |
| 133 | $body .= "\n"; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * If $haystack is a full mailbox name and $needle is the mailbox |
| 138 | * separator character, returns the last part of the mailbox name. |
| 139 | */ |
| 140 | function readShortMailboxName($haystack, $needle) { |
| 141 | |
| 142 | if ($needle == '') { |
| 143 | $elem = $haystack; |
| 144 | } else { |
| 145 | $parts = explode($needle, $haystack); |
| 146 | $elem = array_pop($parts); |
| 147 | while ($elem == '' && count($parts)) { |
| 148 | $elem = array_pop($parts); |
| 149 | } |
| 150 | } |
| 151 | return( $elem ); |
| 152 | } |
| 153 | |
| 154 | function php_self () { |
| 155 | if ( sqgetGlobalVar('REQUEST_URI', $req_uri, SQ_SERVER) && !empty($req_uri) ) { |
| 156 | return $req_uri; |
| 157 | } |
| 158 | |
| 159 | if ( sqgetGlobalVar('PHP_SELF', $php_self, SQ_SERVER) && !empty($php_self) ) { |
| 160 | return $php_self; |
| 161 | } |
| 162 | |
| 163 | return ''; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | /** |
| 168 | * This determines the location to forward to relative to your server. |
| 169 | * If this doesnt work correctly for you (although it should), you can |
| 170 | * remove all this code except the last two lines, and change the header() |
| 171 | * function to look something like this, customized to the location of |
| 172 | * SquirrelMail on your server: |
| 173 | * |
| 174 | * http://www.myhost.com/squirrelmail/src/login.php |
| 175 | */ |
| 176 | function get_location () { |
| 177 | |
| 178 | global $imap_server_type; |
| 179 | |
| 180 | /* Get the path, handle virtual directories */ |
| 181 | $path = substr(php_self(), 0, strrpos(php_self(), '/')); |
| 182 | |
| 183 | if ( sqgetGlobalVar('sq_base_url', $full_url, SQ_SESSION) ) { |
| 184 | return $full_url . $path; |
| 185 | } |
| 186 | |
| 187 | /* Check if this is a HTTPS or regular HTTP request. */ |
| 188 | $proto = 'http://'; |
| 189 | |
| 190 | /* |
| 191 | * If you have 'SSLOptions +StdEnvVars' in your apache config |
| 192 | * OR if you have HTTPS=on in your HTTP_SERVER_VARS |
| 193 | * OR if you are on port 443 |
| 194 | */ |
| 195 | $getEnvVar = getenv('HTTPS'); |
| 196 | if ((isset($getEnvVar) && !strcasecmp($getEnvVar, 'on')) || |
| 197 | (sqgetGlobalVar('HTTPS', $https_on, SQ_SERVER) && !strcasecmp($https_on, 'on')) || |
| 198 | (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER) && $server_port == 443)) { |
| 199 | $proto = 'https://'; |
| 200 | } |
| 201 | |
| 202 | /* Get the hostname from the Host header or server config. */ |
| 203 | if ( !sqgetGlobalVar('HTTP_HOST', $host, SQ_SERVER) || empty($host) ) { |
| 204 | if ( !sqgetGlobalVar('SERVER_NAME', $host, SQ_SERVER) || empty($host) ) { |
| 205 | $host = ''; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | $port = ''; |
| 210 | if (! strstr($host, ':')) { |
| 211 | if (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER)) { |
| 212 | if (($server_port != 80 && $proto == 'http://') || |
| 213 | ($server_port != 443 && $proto == 'https://')) { |
| 214 | $port = sprintf(':%d', $server_port); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /* this is a workaround for the weird macosx caching that |
| 220 | causes Apache to return 16080 as the port number, which causes |
| 221 | SM to bail */ |
| 222 | |
| 223 | if ($imap_server_type == 'macosx' && $port == ':16080') { |
| 224 | $port = ''; |
| 225 | } |
| 226 | |
| 227 | /* Fallback is to omit the server name and use a relative */ |
| 228 | /* URI, although this is not RFC 2616 compliant. */ |
| 229 | $full_url = ($host ? $proto . $host . $port : ''); |
| 230 | sqsession_register($full_url, 'sq_base_url'); |
| 231 | return $full_url . $path; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /** |
| 236 | * These functions are used to encrypt the passowrd before it is |
| 237 | * stored in a cookie. |
| 238 | */ |
| 239 | function OneTimePadEncrypt ($string, $epad) { |
| 240 | $pad = base64_decode($epad); |
| 241 | $encrypted = ''; |
| 242 | for ($i = 0; $i < strlen ($string); $i++) { |
| 243 | $encrypted .= chr (ord($string[$i]) ^ ord($pad[$i])); |
| 244 | } |
| 245 | |
| 246 | return base64_encode($encrypted); |
| 247 | } |
| 248 | |
| 249 | function OneTimePadDecrypt ($string, $epad) { |
| 250 | $pad = base64_decode($epad); |
| 251 | $encrypted = base64_decode ($string); |
| 252 | $decrypted = ''; |
| 253 | for ($i = 0; $i < strlen ($encrypted); $i++) { |
| 254 | $decrypted .= chr (ord($encrypted[$i]) ^ ord($pad[$i])); |
| 255 | } |
| 256 | |
| 257 | return $decrypted; |
| 258 | } |
| 259 | |
| 260 | |
| 261 | /** |
| 262 | * Randomize the mt_rand() function. Toss this in strings or integers |
| 263 | * and it will seed the generator appropriately. With strings, it is |
| 264 | * better to get them long. Use md5() to lengthen smaller strings. |
| 265 | */ |
| 266 | function sq_mt_seed($Val) { |
| 267 | /* if mt_getrandmax() does not return a 2^n - 1 number, |
| 268 | this might not work well. This uses $Max as a bitmask. */ |
| 269 | $Max = mt_getrandmax(); |
| 270 | |
| 271 | if (! is_int($Val)) { |
| 272 | $Val = crc32($Val); |
| 273 | } |
| 274 | |
| 275 | if ($Val < 0) { |
| 276 | $Val *= -1; |
| 277 | } |
| 278 | |
| 279 | if ($Val = 0) { |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | mt_srand(($Val ^ mt_rand(0, $Max)) & $Max); |
| 284 | } |
| 285 | |
| 286 | |
| 287 | /** |
| 288 | * This function initializes the random number generator fairly well. |
| 289 | * It also only initializes it once, so you don't accidentally get |
| 290 | * the same 'random' numbers twice in one session. |
| 291 | */ |
| 292 | function sq_mt_randomize() { |
| 293 | static $randomized; |
| 294 | |
| 295 | if ($randomized) { |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | /* Global. */ |
| 300 | sqgetGlobalVar('REMOTE_PORT', $remote_port, SQ_SERVER); |
| 301 | sqgetGlobalVar('REMOTE_ADDR', $remote_addr, SQ_SERVER); |
| 302 | sq_mt_seed((int)((double) microtime() * 1000000)); |
| 303 | sq_mt_seed(md5($remote_port . $remote_addr . getmypid())); |
| 304 | |
| 305 | /* getrusage */ |
| 306 | if (function_exists('getrusage')) { |
| 307 | /* Avoid warnings with Win32 */ |
| 308 | $dat = @getrusage(); |
| 309 | if (isset($dat) && is_array($dat)) { |
| 310 | $Str = ''; |
| 311 | foreach ($dat as $k => $v) |
| 312 | { |
| 313 | $Str .= $k . $v; |
| 314 | } |
| 315 | sq_mt_seed(md5($Str)); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | if(sqgetGlobalVar('UNIQUE_ID', $unique_id, SQ_SERVER)) { |
| 320 | sq_mt_seed(md5($unique_id)); |
| 321 | } |
| 322 | |
| 323 | $randomized = 1; |
| 324 | } |
| 325 | |
| 326 | function OneTimePadCreate ($length=100) { |
| 327 | sq_mt_randomize(); |
| 328 | |
| 329 | $pad = ''; |
| 330 | for ($i = 0; $i < $length; $i++) { |
| 331 | $pad .= chr(mt_rand(0,255)); |
| 332 | } |
| 333 | |
| 334 | return base64_encode($pad); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Returns a string showing the size of the message/attachment. |
| 339 | */ |
| 340 | function show_readable_size($bytes) { |
| 341 | $bytes /= 1024; |
| 342 | $type = 'k'; |
| 343 | |
| 344 | if ($bytes / 1024 > 1) { |
| 345 | $bytes /= 1024; |
| 346 | $type = 'M'; |
| 347 | } |
| 348 | |
| 349 | if ($bytes < 10) { |
| 350 | $bytes *= 10; |
| 351 | settype($bytes, 'integer'); |
| 352 | $bytes /= 10; |
| 353 | } else { |
| 354 | settype($bytes, 'integer'); |
| 355 | } |
| 356 | |
| 357 | return $bytes . '<small> ' . $type . '</small>'; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Generates a random string from the caracter set you pass in |
| 362 | * |
| 363 | * Flags: |
| 364 | * 1 = add lowercase a-z to $chars |
| 365 | * 2 = add uppercase A-Z to $chars |
| 366 | * 4 = add numbers 0-9 to $chars |
| 367 | */ |
| 368 | |
| 369 | function GenerateRandomString($size, $chars, $flags = 0) { |
| 370 | if ($flags & 0x1) { |
| 371 | $chars .= 'abcdefghijklmnopqrstuvwxyz'; |
| 372 | } |
| 373 | if ($flags & 0x2) { |
| 374 | $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 375 | } |
| 376 | if ($flags & 0x4) { |
| 377 | $chars .= '0123456789'; |
| 378 | } |
| 379 | |
| 380 | if (($size < 1) || (strlen($chars) < 1)) { |
| 381 | return ''; |
| 382 | } |
| 383 | |
| 384 | sq_mt_randomize(); /* Initialize the random number generator */ |
| 385 | |
| 386 | $String = ''; |
| 387 | $j = strlen( $chars ) - 1; |
| 388 | while (strlen($String) < $size) { |
| 389 | $String .= $chars{mt_rand(0, $j)}; |
| 390 | } |
| 391 | |
| 392 | return $String; |
| 393 | } |
| 394 | |
| 395 | function quoteimap($str) { |
| 396 | return ereg_replace('(["\\])', '\\\\1', $str); |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Trims every element in the array |
| 401 | */ |
| 402 | function TrimArray(&$array) { |
| 403 | foreach ($array as $k => $v) { |
| 404 | global $$k; |
| 405 | if (is_array($$k)) { |
| 406 | foreach ($$k as $k2 => $v2) { |
| 407 | $$k[$k2] = substr($v2, 1); |
| 408 | } |
| 409 | } else { |
| 410 | $$k = substr($v, 1); |
| 411 | } |
| 412 | |
| 413 | /* Re-assign back to array. */ |
| 414 | $array[$k] = $$k; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /* returns a link to the compose-page, taking in consideration |
| 419 | * the compose_in_new and javascript settings. */ |
| 420 | function makeComposeLink($url, $text = null) |
| 421 | { |
| 422 | global $compose_new_win,$javascript_on; |
| 423 | |
| 424 | if(!$text) { |
| 425 | $text = _("Compose"); |
| 426 | } |
| 427 | |
| 428 | if($compose_new_win != '1') { |
| 429 | return makeInternalLink($url, $text, 'right'); |
| 430 | } |
| 431 | |
| 432 | /* if we can use JS, use the fancy window, else just open a new one HTML-style */ |
| 433 | if($javascript_on) { |
| 434 | sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION); |
| 435 | return '<a href="javascript:void(0)" onclick="comp_in_new(\''.$base_uri.$url.'\')">'. $text.'</a>'; |
| 436 | } |
| 437 | |
| 438 | return makeInternalLink($url, $text, '_blank'); |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * sm_print_r($some_variable, [$some_other_variable [, ...]]); |
| 443 | * Debugging function - does the same as print_r, but makes sure special |
| 444 | * characters are converted to htmlentities first. This will allow |
| 445 | * values like <some@email.address> to be displayed. |
| 446 | * The output is wrapped in <pre> and </pre> tags. |
| 447 | */ |
| 448 | function sm_print_r() { |
| 449 | ob_start(); // Buffer output |
| 450 | foreach(func_get_args() as $var) { |
| 451 | print_r($var); |
| 452 | echo "\n"; |
| 453 | } |
| 454 | $buffer = ob_get_contents(); // Grab the print_r output |
| 455 | ob_end_clean(); // Silently discard the output & stop buffering |
| 456 | print "<pre>"; |
| 457 | print htmlentities($buffer); |
| 458 | print "</pre>"; |
| 459 | } |
| 460 | |
| 461 | $PHP_SELF = php_self(); |
| 462 | |
| 463 | ?> |