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