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