little fix
[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
138
8442ac08 139 if (!isset($url_parser_php)) {
140 include "../functions/url_parser.php";
141 }
142
a8648d75 143 $body_ary = explode("\n", $body);
8ceb637a 144 $PriorQuotes = 0;
8442ac08 145 for ($i=0; $i < count($body_ary); $i++) {
a8648d75 146 $line = $body_ary[$i];
a37f3771 147 if (strlen($line) - 2 >= $wrap_at) {
9eea179c 148 sqWordWrap($line, $wrap_at);
a37f3771 149 }
a95681a7 150 $line = charset_decode($charset, $line);
151 $line = str_replace("\t", ' ', $line);
a37f3771 152
9eea179c 153 parseUrl ($line);
e2ef6f4b 154
9eea179c 155 $Quotes = 0;
156 $pos = 0;
157 while (1)
158 {
8ceb637a 159 if ($line[$pos] == ' ')
9eea179c 160 {
8ceb637a 161 $pos ++;
9eea179c 162 }
163 else if (strpos($line, '&gt;', $pos) === $pos)
164 {
165 $pos += 4;
166 $Quotes ++;
167 }
168 else
169 {
170 break;
171 }
172 }
173
8ceb637a 174 if ($Quotes > 1)
175 $line = "<FONT COLOR=FF0000>$line</FONT>";
176 elseif ($Quotes)
177 $line = "<FONT COLOR=800000>$line</FONT>";
e2ef6f4b 178
8ceb637a 179 $body_ary[$i] = $line;
a8648d75 180 }
8ceb637a 181 $body = "<pre>" . implode("\n", $body_ary) . "</pre>";
78509c54 182 }
183
7ce342dc 184 /* SquirrelMail version number -- DO NOT CHANGE */
fd0a282a 185 $version = "1.0pre1 (cvs)";
d29aac0e 186
187
188 function find_mailbox_name ($mailbox) {
f9b3e5d9 189/*
d29aac0e 190 $mailbox = trim($mailbox);
191 if (substr($mailbox, strlen($mailbox)-1, strlen($mailbox)) == "\"") {
192 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
193 $pos = strrpos ($mailbox, "\"")+1;
194 $box = substr($mailbox, $pos);
195 } else {
196 $box = substr($mailbox, strrpos($mailbox, " ")+1, strlen($mailbox));
197 }
198 return $box;
f9b3e5d9 199*/
200
201 if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
202 return $regs[1];
203 ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
204 return $regs[1];
205
d29aac0e 206 }
207
208 function replace_spaces ($string) {
209 return str_replace(" ", "&nbsp;", $string);
210 }
211
212 function replace_escaped_spaces ($string) {
213 return str_replace("&nbsp;", " ", $string);
214 }
1195c340 215
216 function get_location () {
217 # This determines the location to forward to relative
7e343a7d 218 # to your server. If this doesnt work correctly for
1195c340 219 # you (although it should), you can remove all this
220 # code except the last two lines, and change the header()
221 # function to look something like this, customized to
222 # the location of SquirrelMail on your server:
223 #
224 # http://www.myhost.com/squirrelmail/src/login.php
225
00421cb6 226 global $PHP_SELF, $SERVER_NAME, $HTTPS, $HTTP_HOST, $SERVER_PORT;
1195c340 227
228 // Get the path
229 $path = substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'));
230
231 // Check if this is a HTTPS or regular HTTP request
232 $proto = "http://";
233 if(isset($HTTPS) && $HTTPS == 'on' ) {
234 $proto = "https://";
235 }
236
237 // Get the hostname from the Host header or server config.
b768a8eb 238 $host = "";
239 if (isset($HTTP_HOST) && !empty($HTTP_HOST))
240 {
241 $host = $HTTP_HOST;
242 }
243 else if (isset($SERVER_NAME) && !empty($SERVER_NAME))
244 {
245 $host = $SERVER_NAME;
246 }
247
b768a8eb 248 $port = '';
249 if (! strstr($host, ':'))
250 {
251 if (isset($SERVER_PORT)) {
d6f5495a 252 if (($SERVER_PORT != 80 && $proto == "http://")
253 || ($SERVER_PORT != 443 && $proto == "https://")) {
b768a8eb 254 $port = sprintf(':%d', $SERVER_PORT);
255 }
256 }
257 }
258
259 if ($host)
260 return $proto . $host . $port . $path;
261
1195c340 262 // Fallback is to omit the server name and use a relative URI,
263 // although this is not RFC 2616 compliant.
b768a8eb 264 return $path;
1195c340 265 }
7aaa81fc 266
267 function sqStripSlashes($string) {
268 if (get_magic_quotes_gpc()) {
269 $string = stripslashes($string);
270 }
271 return $string;
272 }
52eefafc 273
274
275 // These functions are used to encrypt the passowrd before it is
276 // stored in a cookie.
277 function OneTimePadEncrypt ($string, $pad) {
278 for ($i = 0; $i < strlen ($string); $i++) {
279 $encrypted .= chr (ord($string[$i]) ^ ord($pad[$i]));
280 }
281
282 return base64_encode($encrypted);
283 }
284
285 function OneTimePadDecrypt ($string, $pad) {
286 $encrypted = base64_decode ($string);
287
288 for ($i = 0; $i < strlen ($encrypted); $i++) {
289 $decrypted .= chr (ord($encrypted[$i]) ^ ord($pad[$i]));
290 }
291
292 return $decrypted;
293 }
294
580e1793 295
dcaf2a49 296 // Randomize the mt_rand() function. Toss this in strings or
297 // integers and it will seed the generator appropriately.
298 // With strings, it is better to get them long. Use md5() to
299 // lengthen smaller strings.
300 function sq_mt_seed($Val)
301 {
302 // if mt_getrandmax() does not return a 2^n - 1 number,
303 // this might not work well. This uses $Max as a bitmask.
304 $Max = mt_getrandmax();
305
306 if (! is_int($Val))
307 {
308 if (function_exists("crc32"))
309 {
310 $Val = crc32($Val);
311 }
312 else
313 {
314 $Str = $Val;
315 $Pos = 0;
316 $Val = 0;
317 $Mask = $Max / 2;
318 $HighBit = $Max ^ $Mask;
319 while ($Pos < strlen($Str))
320 {
321 if ($Val & $HighBit)
322 {
323 $Val = (($Val & $Mask) << 1) + 1;
324 }
325 else
326 {
327 $Val = ($Val & $Mask) << 1;
328 }
329 $Val ^= $Str[$Pos];
330 $Pos ++;
331 }
332 }
333 }
580e1793 334
dcaf2a49 335 if ($Val < 0)
336 $Val *= -1;
337 if ($Val = 0)
338 return;
339
340 mt_srand(($Val ^ mt_rand(0, $Max)) & $Max);
341 }
342
343
344 // This function initializes the random number generator fairly well.
345 // It also only initializes it once, so you don't accidentally get
346 // the same 'random' numbers twice in one session.
347 function sq_mt_randomize()
348 {
349 global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID;
350 static $randomized;
351
352 if ($randomized)
353 return;
354
355 // Global
356 sq_mt_seed((int)((double) microtime() * 1000000));
357 sq_mt_seed(md5($REMOTE_PORT . $REMOTE_ADDR . getmypid()));
358
359 // getrusage
360 if (function_exists("getrusage")) {
361 $dat = getrusage();
362 sq_mt_seed(md5($dat["ru_nswap"] . $dat["ru_majflt"] .
363 $dat["ru_utime.tv_sec"] . $dat["ru_utime.tv_usec"]));
364 }
365
366 // Apache-specific
367 sq_mt_seed(md5($UNIQUE_ID));
368
369 $randomized = 1;
370 }
371
372 function OneTimePadCreate ($length=100) {
373 sq_mt_randomize();
52eefafc 374
375 for ($i = 0; $i < $length; $i++) {
dcaf2a49 376 $pad .= chr(mt_rand(0,255));
52eefafc 377 }
378
379 return $pad;
380 }
381
f79af863 382 // Check if we have a required PHP-version. Return TRUE if we do,
383 // or FALSE if we don't.
384 // To check for 4.0.1, use sqCheckPHPVersion(4,0,1)
385 // To check for 4.0b3, use sqCheckPHPVersion(4,0,-3)
386 // Does not handle betas like 4.0.1b1 or development versions
387 function sqCheckPHPVersion($major, $minor, $release) {
388
389 $ver = phpversion();
390 eregi("^([0-9]+)\.([0-9]+)(.*)", $ver, $regs);
391
392 // Parse the version string
393 $vmajor = strval($regs[1]);
394 $vminor = strval($regs[2]);
395 $vrel = $regs[3];
396 if($vrel[0] == ".")
397 $vrel = strval(substr($vrel, 1));
398 if($vrel[0] == "b" || $vrel[0] == "B")
399 $vrel = - strval(substr($vrel, 1));
400 if($vrel[0] == "r" || $vrel[0] == "R")
401 $vrel = - strval(substr($vrel, 2))/10;
402
b5afdff0 403 // Compare major version
f79af863 404 if($vmajor < $major) return false;
b5afdff0 405 if($vmajor > $major) return true;
406
407 // Major is the same. Compare minor
f79af863 408 if($vminor < $minor) return false;
b5afdff0 409 if($vminor > $minor) return true;
f79af863 410
b5afdff0 411 // Major and minor is the same as the required one.
f79af863 412 // Compare release
413 if($vrel >= 0 && $release >= 0) { // Neither are beta
414 if($vrel < $release) return false;
415 } else if($vrel >= 0 && $release < 0){ // This is not beta, required is beta
416 return true;
417 } else if($vrel < 0 && $release >= 0){ // This is beta, require not beta
418 return false;
419 } else { // Both are beta
420 if($vrel > $release) return false;
421 }
422
423 return true;
424 }
6e7468f6 425
426 /* Returns a string showing the size of the message/attachment */
427 function show_readable_size($bytes)
428 {
429 $bytes /= 1024;
430 $type = 'k';
431
432 if ($bytes / 1024 > 1)
433 {
434 $bytes /= 1024;
435 $type = 'm';
436 }
437
438 if ($bytes < 10)
439 {
440 $bytes *= 10;
441 settype($bytes, "integer");
442 $bytes /= 10;
443 }
444 else
445 settype($bytes, "integer");
446
447 return $bytes . '<small>&nbsp;' . $type . '</small>';
448 }
f79af863 449
1899535f 450 /* Generates a random string from the caracter set you pass in
451 *
452 * Flags:
453 * 1 = add lowercase a-z to $chars
454 * 2 = add uppercase A-Z to $chars
455 * 4 = add numbers 0-9 to $chars
456 */
457
458 function GenerateRandomString($size, $chars, $flags = 0)
459 {
460 if ($flags & 0x1)
461 $chars .= 'abcdefghijklmnopqrstuvwxyz';
462 if ($flags & 0x2)
463 $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
464 if ($flags & 0x4)
465 $chars .= '0123456789';
466
467 if ($size < 1 || strlen($chars) < 1)
468 return "";
469
470 sq_mt_randomize(); // Initialize the random number generator
471
472 while (strlen($String) < $size) {
473 $String .= $chars[mt_rand(0, strlen($chars))];
474 }
475
476 return $String;
477 }
478
3302d0d4 479?>