parseAddrs now always returns an array (expected behavior)
[squirrelmail.git] / functions / strings.php
CommitLineData
59177427 1<?php
7ce342dc 2
d068c0ec 3 $strings_php = true;
4
3302d0d4 5 //*************************************************************************
6 // Count the number of occurances of $needle are in $haystack.
7 //*************************************************************************
8 function countCharInString($haystack, $needle) {
f9b3e5d9 9 $haystack = ereg_replace("[^$needle]","",$haystack);
10 return strlen($haystack);
3302d0d4 11 }
12
13 //*************************************************************************
14 // Read from the back of $haystack until $needle is found, or the begining
f9b3e5d9 15 // of the $haystack is reached. $needle is a single character
3302d0d4 16 //*************************************************************************
17 function readShortMailboxName($haystack, $needle) {
f4a3fd08 18 if ($needle == "") return $haystack;
1d2ce1c3 19 if ($needle == ".") $needle = "\.";
20 ereg("([^$needle]+)$needle?$", $haystack, $regs);
21 return $regs[1];
3302d0d4 22 }
23
5bdd7223 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
8beafbbc 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
8467bf00 47 // Wraps text at $wrap characters
a95681a7 48 // Has a problem with special HTML characters, so call this before
49 // you do character translation.
50 // Specifically, &#039 comes up as 5 characters instead of 1.
01aab860 51 // This should not add newlines to the end of lines.
9eea179c 52 function sqWordWrap(&$line, $wrap) {
bcad90fe 53 preg_match("/^([\s>]*)([^\s>].*)?$/", $line, $regs);
45f6dd68 54 $beginning_spaces = $regs[1];
55 $words = explode(" ", $regs[2]);
a95681a7 56
57 $i = 0;
58 $line = $beginning_spaces;
30f64711 59
bcad90fe 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 ++;
45f6dd68 66
bcad90fe 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 }
45f6dd68 73
bcad90fe 74 // Skip spaces if they are the first thing on a continued line
75 while (!$words[$i] && $i < count($words)) {
76 $i ++;
77 }
8ceb637a 78
79 // Go to the next line if we have more to process
bcad90fe 80 if ($i < count($words)) {
81 $line .= "\n$beginning_spaces";
b8ea4ed6 82 }
e550d551 83 }
e550d551 84 }
01aab860 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
40ee9452 118
119 /** Returns an array of email addresses **/
a7f3c40d 120 /* Be cautious of "user@host.com" */
40ee9452 121 function parseAddrs($text) {
1d2ce1c3 122 if (trim($text) == "")
c5edd369 123 return array();
1d2ce1c3 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;
40ee9452 134 }
135
136 /** Returns a line of comma separated email addresses from an array **/
137 function getLineOfAddrs($array) {
b676ba7e 138 if (is_array($array)) {
139 $to_line = implode(", ", $array);
140 $to_line = trim(ereg_replace(",,+", ",", $to_line));
141 } else {
142 $to_line = "";
40ee9452 143 }
144 return $to_line;
145 }
7ce342dc 146
9eea179c 147 function translateText(&$body, $wrap_at, $charset) {
9297917e 148 global $where, $what; // from searching
172fd718 149 global $url_parser_php;
9297917e 150
8442ac08 151 if (!isset($url_parser_php)) {
152 include "../functions/url_parser.php";
153 }
154
a8648d75 155 $body_ary = explode("\n", $body);
8ceb637a 156 $PriorQuotes = 0;
8442ac08 157 for ($i=0; $i < count($body_ary); $i++) {
a8648d75 158 $line = $body_ary[$i];
a37f3771 159 if (strlen($line) - 2 >= $wrap_at) {
9eea179c 160 sqWordWrap($line, $wrap_at);
a37f3771 161 }
a95681a7 162 $line = charset_decode($charset, $line);
163 $line = str_replace("\t", ' ', $line);
a37f3771 164
9eea179c 165 parseUrl ($line);
e2ef6f4b 166
9eea179c 167 $Quotes = 0;
168 $pos = 0;
169 while (1)
170 {
8ceb637a 171 if ($line[$pos] == ' ')
9eea179c 172 {
8ceb637a 173 $pos ++;
9eea179c 174 }
175 else if (strpos($line, '&gt;', $pos) === $pos)
176 {
177 $pos += 4;
178 $Quotes ++;
179 }
180 else
181 {
182 break;
183 }
184 }
185
8ceb637a 186 if ($Quotes > 1)
187 $line = "<FONT COLOR=FF0000>$line</FONT>";
188 elseif ($Quotes)
189 $line = "<FONT COLOR=800000>$line</FONT>";
e2ef6f4b 190
8ceb637a 191 $body_ary[$i] = $line;
a8648d75 192 }
8ceb637a 193 $body = "<pre>" . implode("\n", $body_ary) . "</pre>";
78509c54 194 }
195
7ce342dc 196 /* SquirrelMail version number -- DO NOT CHANGE */
1e06e00c 197 $version = "1.0pre3 [cvs]";
d29aac0e 198
199
200 function find_mailbox_name ($mailbox) {
f9b3e5d9 201/*
d29aac0e 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;
f9b3e5d9 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
d29aac0e 218 }
219
220 function replace_spaces ($string) {
221 return str_replace(" ", "&nbsp;", $string);
222 }
223
224 function replace_escaped_spaces ($string) {
225 return str_replace("&nbsp;", " ", $string);
226 }
1195c340 227
228 function get_location () {
229 # This determines the location to forward to relative
7e343a7d 230 # to your server. If this doesnt work correctly for
1195c340 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
00421cb6 238 global $PHP_SELF, $SERVER_NAME, $HTTPS, $HTTP_HOST, $SERVER_PORT;
1195c340 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://";
16f32d4a 245 if(isset($HTTPS) && !strcasecmp($HTTPS, 'on') ) {
1195c340 246 $proto = "https://";
247 }
248
249 // Get the hostname from the Host header or server config.
b768a8eb 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
b768a8eb 260 $port = '';
261 if (! strstr($host, ':'))
262 {
263 if (isset($SERVER_PORT)) {
d6f5495a 264 if (($SERVER_PORT != 80 && $proto == "http://")
265 || ($SERVER_PORT != 443 && $proto == "https://")) {
b768a8eb 266 $port = sprintf(':%d', $SERVER_PORT);
267 }
268 }
269 }
270
271 if ($host)
272 return $proto . $host . $port . $path;
273
1195c340 274 // Fallback is to omit the server name and use a relative URI,
275 // although this is not RFC 2616 compliant.
b768a8eb 276 return $path;
1195c340 277 }
7aaa81fc 278
279 function sqStripSlashes($string) {
280 if (get_magic_quotes_gpc()) {
281 $string = stripslashes($string);
282 }
283 return $string;
284 }
52eefafc 285
286
287 // These functions are used to encrypt the passowrd before it is
288 // stored in a cookie.
59edd854 289 function OneTimePadEncrypt ($string, $epad) {
290 $pad = base64_decode($epad);
52eefafc 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
59edd854 298 function OneTimePadDecrypt ($string, $epad) {
299 $pad = base64_decode($epad);
52eefafc 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
580e1793 309
dcaf2a49 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 }
580e1793 348
dcaf2a49 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();
52eefafc 388
389 for ($i = 0; $i < $length; $i++) {
dcaf2a49 390 $pad .= chr(mt_rand(0,255));
52eefafc 391 }
392
59edd854 393 return base64_encode($pad);
52eefafc 394 }
395
f79af863 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
b5afdff0 417 // Compare major version
f79af863 418 if($vmajor < $major) return false;
b5afdff0 419 if($vmajor > $major) return true;
420
421 // Major is the same. Compare minor
f79af863 422 if($vminor < $minor) return false;
b5afdff0 423 if($vminor > $minor) return true;
f79af863 424
b5afdff0 425 // Major and minor is the same as the required one.
f79af863 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 }
6e7468f6 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>&nbsp;' . $type . '</small>';
462 }
f79af863 463
1899535f 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
3302d0d4 493?>