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