82e8d0146329b845393f04f467f24e409709f07c
7 //*************************************************************************
8 // Count the number of occurances of $needle are in $haystack.
9 //*************************************************************************
10 function countCharInString($haystack, $needle) {
11 $haystack = ereg_replace("[^$needle]",'',$haystack);
12 return strlen($haystack);
15 //*************************************************************************
16 // Read from the back of $haystack until $needle is found, or the begining
17 // of the $haystack is reached. $needle is a single character
18 //*************************************************************************
19 function readShortMailboxName($haystack, $needle) {
20 if ($needle == '') return $haystack;
21 if ($needle == '.') $needle = '\.';
22 ereg("([^$needle]+)$needle?$", $haystack, $regs);
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);
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))
49 // Wraps text at $wrap characters
50 // Has a problem with special HTML characters, so call this before
51 // you do character translation.
52 // Specifically, ' comes up as 5 characters instead of 1.
53 // This should not add newlines to the end of lines.
54 function sqWordWrap(&$line, $wrap) {
55 preg_match("/^([\s>]*)([^\s>].*)?$/", $line, $regs);
56 $beginning_spaces = $regs[1];
57 $words = explode(' ', $regs[2]);
60 $line = $beginning_spaces;
62 while ($i < count($words)) {
63 // Force one word to be on a line (minimum)
65 $line_len = strlen($beginning_spaces) +
strlen($words[$i]) +
2;
66 if (isset($words[$i +
1]))
67 $line_len +
= strlen($words[$i +
1]);
70 // Add more words (as long as they fit)
71 while ($line_len < $wrap && $i < count($words)) {
72 $line .= ' ' . $words[$i];
74 if (isset($words[$i]))
75 $line_len +
= strlen($words[$i]) +
1;
80 // Skip spaces if they are the first thing on a continued line
81 while (!isset($words[$i]) && $i < count($words)) {
85 // Go to the next line if we have more to process
86 if ($i < count($words)) {
87 $line .= "\n$beginning_spaces";
93 // Does the opposite of sqWordWrap()
94 function sqUnWordWrap(&$body)
96 $lines = explode("\n", $body);
99 for ($i = 0; $i < count($lines); $i ++
)
101 preg_match("/^([\s>]*)([^\s>].*)?$/", $lines[$i], $regs);
102 $CurrentSpaces = $regs[1];
104 $CurrentRest = $regs[2];
107 $PreviousSpaces = $CurrentSpaces;
110 else if ($PreviousSpaces == $CurrentSpaces && // Do the beginnings match
111 strlen($lines[$i - 1]) > 65 && // Over 65 characters long
112 strlen($CurrentRest)) // and there's a line to continue with
114 $body .= ' ' . $CurrentRest;
118 $body .= "\n" . $lines[$i];
119 $PreviousSpaces = $CurrentSpaces;
126 /** Returns an array of email addresses **/
127 /* Be cautious of "user@host.com" */
128 function parseAddrs($text) {
129 if (trim($text) == "")
131 $text = str_replace(' ', '', $text);
132 $text = ereg_replace('"[^"]*"', '', $text);
133 $text = ereg_replace("\([^\)]*\)", '', $text);
134 $text = str_replace(',', ';', $text);
135 $array = explode(';', $text);
136 for ($i = 0; $i < count ($array); $i++
) {
137 $array[$i] = eregi_replace ("^.*[<]", '', $array[$i]);
138 $array[$i] = eregi_replace ("[>].*$", '', $array[$i]);
143 /** Returns a line of comma separated email addresses from an array **/
144 function getLineOfAddrs($array) {
145 if (is_array($array)) {
146 $to_line = implode(', ', $array);
147 $to_line = trim(ereg_replace(',,+', ',', $to_line));
154 function translateText(&$body, $wrap_at, $charset) {
155 global $where, $what; // from searching
156 global $url_parser_php;
158 if (!isset($url_parser_php)) {
159 include '../functions/url_parser.php';
162 $body_ary = explode("\n", $body);
164 for ($i=0; $i < count($body_ary); $i++
) {
165 $line = $body_ary[$i];
166 if (strlen($line) - 2 >= $wrap_at) {
167 sqWordWrap($line, $wrap_at);
169 $line = charset_decode($charset, $line);
170 $line = str_replace("\t", ' ', $line);
178 if ($line[$pos] == ' ')
182 else if (strpos($line, '>', $pos) === $pos)
194 $line = '<FONT COLOR="FF0000">$line</FONT>';
196 $line = '<FONT COLOR="800000">$line</FONT>';
198 $body_ary[$i] = $line;
200 $body = '<pre>' . implode("\n", $body_ary) . '</pre>';
203 /* SquirrelMail version number -- DO NOT CHANGE */
204 $version = '1.0.1 [cvs]';
207 function find_mailbox_name ($mailbox) {
209 $mailbox = trim($mailbox);
210 if (substr($mailbox, strlen($mailbox)-1, strlen($mailbox)) == '"') {
211 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
212 $pos = strrpos ($mailbox, '"')+1;
213 $box = substr($mailbox, $pos);
215 $box = substr($mailbox, strrpos($mailbox, ' ')+1, strlen($mailbox));
220 if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
222 ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
227 function replace_spaces ($string) {
228 return str_replace(' ', ' ', $string);
231 function replace_escaped_spaces ($string) {
232 return str_replace(' ', ' ', $string);
235 function get_location () {
236 # This determines the location to forward to relative
237 # to your server. If this doesnt work correctly for
238 # you (although it should), you can remove all this
239 # code except the last two lines, and change the header()
240 # function to look something like this, customized to
241 # the location of SquirrelMail on your server:
243 # http://www.myhost.com/squirrelmail/src/login.php
245 global $PHP_SELF, $SERVER_NAME, $HTTPS, $HTTP_HOST, $SERVER_PORT;
248 $path = substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'));
250 // Check if this is a HTTPS or regular HTTP request
252 if(isset($HTTPS) && !strcasecmp($HTTPS, 'on') ) {
256 // Get the hostname from the Host header or server config.
258 if (isset($HTTP_HOST) && !empty($HTTP_HOST))
262 else if (isset($SERVER_NAME) && !empty($SERVER_NAME))
264 $host = $SERVER_NAME;
268 if (! strstr($host, ':'))
270 if (isset($SERVER_PORT)) {
271 if (($SERVER_PORT != 80 && $proto == 'http://')
272 ||
($SERVER_PORT != 443 && $proto == 'https://')) {
273 $port = sprintf(':%d', $SERVER_PORT);
279 return $proto . $host . $port . $path;
281 // Fallback is to omit the server name and use a relative URI,
282 // although this is not RFC 2616 compliant.
286 function sqStripSlashes($string) {
287 if (get_magic_quotes_gpc()) {
288 $string = stripslashes($string);
294 // These functions are used to encrypt the passowrd before it is
295 // stored in a cookie.
296 function OneTimePadEncrypt ($string, $epad) {
297 $pad = base64_decode($epad);
299 for ($i = 0; $i < strlen ($string); $i++
) {
300 $encrypted .= chr (ord($string[$i]) ^
ord($pad[$i]));
303 return base64_encode($encrypted);
306 function OneTimePadDecrypt ($string, $epad) {
307 $pad = base64_decode($epad);
308 $encrypted = base64_decode ($string);
310 for ($i = 0; $i < strlen ($encrypted); $i++
) {
311 $decrypted .= chr (ord($encrypted[$i]) ^
ord($pad[$i]));
318 // Randomize the mt_rand() function. Toss this in strings or
319 // integers and it will seed the generator appropriately.
320 // With strings, it is better to get them long. Use md5() to
321 // lengthen smaller strings.
322 function sq_mt_seed($Val)
324 // if mt_getrandmax() does not return a 2^n - 1 number,
325 // this might not work well. This uses $Max as a bitmask.
326 $Max = mt_getrandmax();
330 if (function_exists('crc32'))
340 $HighBit = $Max ^
$Mask;
341 while ($Pos < strlen($Str))
345 $Val = (($Val & $Mask) << 1) +
1;
349 $Val = ($Val & $Mask) << 1;
362 mt_srand(($Val ^
mt_rand(0, $Max)) & $Max);
366 // This function initializes the random number generator fairly well.
367 // It also only initializes it once, so you don't accidentally get
368 // the same 'random' numbers twice in one session.
369 function sq_mt_randomize()
371 global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID;
378 sq_mt_seed((int)((double) microtime() * 1000000));
379 sq_mt_seed(md5($REMOTE_PORT . $REMOTE_ADDR . getmypid()));
382 if (function_exists('getrusage')) {
385 foreach ($dat as $k => $v)
389 sq_mt_seed(md5($Str));
393 sq_mt_seed(md5($UNIQUE_ID));
398 function OneTimePadCreate ($length=100) {
402 for ($i = 0; $i < $length; $i++
) {
403 $pad .= chr(mt_rand(0,255));
406 return base64_encode($pad);
409 // Check if we have a required PHP-version. Return TRUE if we do,
410 // or FALSE if we don't.
411 // To check for 4.0.1, use sqCheckPHPVersion(4,0,1)
412 // To check for 4.0b3, use sqCheckPHPVersion(4,0,-3)
413 // Does not handle betas like 4.0.1b1 or development versions
414 function sqCheckPHPVersion($major, $minor, $release) {
417 eregi("^([0-9]+)\.([0-9]+)(.*)", $ver, $regs);
419 // Parse the version string
420 $vmajor = strval($regs[1]);
421 $vminor = strval($regs[2]);
424 $vrel = strval(substr($vrel, 1));
425 if($vrel[0] == 'b' ||
$vrel[0] == 'B')
426 $vrel = - strval(substr($vrel, 1));
427 if($vrel[0] == 'r' ||
$vrel[0] == 'R')
428 $vrel = - strval(substr($vrel, 2))/10;
430 // Compare major version
431 if($vmajor < $major) return false;
432 if($vmajor > $major) return true;
434 // Major is the same. Compare minor
435 if($vminor < $minor) return false;
436 if($vminor > $minor) return true;
438 // Major and minor is the same as the required one.
440 if($vrel >= 0 && $release >= 0) { // Neither are beta
441 if($vrel < $release) return false;
442 } else if($vrel >= 0 && $release < 0){ // This is not beta, required is beta
444 } else if($vrel < 0 && $release >= 0){ // This is beta, require not beta
446 } else { // Both are beta
447 if($vrel > $release) return false;
453 /* Returns a string showing the size of the message/attachment */
454 function show_readable_size($bytes)
459 if ($bytes / 1024 > 1)
468 settype($bytes, 'integer');
472 settype($bytes, 'integer');
474 return $bytes . '<small> ' . $type . '</small>';
477 /* Generates a random string from the caracter set you pass in
480 * 1 = add lowercase a-z to $chars
481 * 2 = add uppercase A-Z to $chars
482 * 4 = add numbers 0-9 to $chars
485 function GenerateRandomString($size, $chars, $flags = 0)
488 $chars .= 'abcdefghijklmnopqrstuvwxyz';
490 $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
492 $chars .= '0123456789';
494 if ($size < 1 ||
strlen($chars) < 1)
497 sq_mt_randomize(); // Initialize the random number generator
499 while (strlen($String) < $size) {
500 $String .= $chars[mt_rand(0, strlen($chars))];