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 if (isset($regs[2])) {
58 $words = explode(' ', $regs[2]);
64 $line = $beginning_spaces;
66 while ($i < count($words)) {
67 // Force one word to be on a line (minimum)
69 $line_len = strlen($beginning_spaces) +
strlen($words[$i]) +
2;
70 if (isset($words[$i +
1]))
71 $line_len +
= strlen($words[$i +
1]);
74 // Add more words (as long as they fit)
75 while ($line_len < $wrap && $i < count($words)) {
76 $line .= ' ' . $words[$i];
78 if (isset($words[$i]))
79 $line_len +
= strlen($words[$i]) +
1;
84 // Skip spaces if they are the first thing on a continued line
85 while (!isset($words[$i]) && $i < count($words)) {
89 // Go to the next line if we have more to process
90 if ($i < count($words)) {
91 $line .= "\n" . $beginning_spaces;
97 // Does the opposite of sqWordWrap()
98 function sqUnWordWrap(&$body)
100 $lines = explode("\n", $body);
102 $PreviousSpaces = "";
103 for ($i = 0; $i < count($lines); $i ++
)
105 preg_match('/^([\\s>]*)([^\\s>].*)?$/', $lines[$i], $regs);
106 $CurrentSpaces = $regs[1];
108 $CurrentRest = $regs[2];
111 $PreviousSpaces = $CurrentSpaces;
114 else if ($PreviousSpaces == $CurrentSpaces && // Do the beginnings match
115 strlen($lines[$i - 1]) > 65 && // Over 65 characters long
116 strlen($CurrentRest)) // and there's a line to continue with
118 $body .= ' ' . $CurrentRest;
122 $body .= "\n" . $lines[$i];
123 $PreviousSpaces = $CurrentSpaces;
130 /** Returns an array of email addresses **/
131 /* Be cautious of "user@host.com" */
132 function parseAddrs($text) {
133 if (trim($text) == "")
135 $text = str_replace(' ', '', $text);
136 $text = ereg_replace('"[^"]*"', '', $text);
137 $text = ereg_replace('\\([^\\)]*\\)', '', $text);
138 $text = str_replace(',', ';', $text);
139 $array = explode(';', $text);
140 for ($i = 0; $i < count ($array); $i++
) {
141 $array[$i] = eregi_replace ("^.*[<]", '', $array[$i]);
142 $array[$i] = eregi_replace ("[>].*$", '', $array[$i]);
147 /** Returns a line of comma separated email addresses from an array **/
148 function getLineOfAddrs($array) {
149 if (is_array($array)) {
150 $to_line = implode(', ', $array);
151 $to_line = trim(ereg_replace(',,+', ',', $to_line));
158 function translateText(&$body, $wrap_at, $charset) {
159 global $where, $what; // from searching
160 global $url_parser_php;
162 if (!isset($url_parser_php)) {
163 include '../functions/url_parser.php';
166 $body_ary = explode("\n", $body);
168 for ($i=0; $i < count($body_ary); $i++
) {
169 $line = $body_ary[$i];
170 if (strlen($line) - 2 >= $wrap_at) {
171 sqWordWrap($line, $wrap_at);
173 $line = charset_decode($charset, $line);
174 $line = str_replace("\t", ' ', $line);
182 if ($line[$pos] == ' ')
186 else if (strpos($line, '>', $pos) === $pos)
198 $line = '<FONT COLOR="FF0000">'.$line.'</FONT>';
200 $line = '<FONT COLOR="800000">'.$line.'</FONT>';
202 $body_ary[$i] = $line;
204 $body = '<pre>' . implode("\n", $body_ary) . '</pre>';
207 /* SquirrelMail version number -- DO NOT CHANGE */
208 $version = '1.0.1 [cvs]';
211 function find_mailbox_name ($mailbox) {
212 if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
214 ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
219 function replace_spaces ($string) {
220 return str_replace(' ', ' ', $string);
223 function replace_escaped_spaces ($string) {
224 return str_replace(' ', ' ', $string);
227 function get_location () {
228 # This determines the location to forward to relative
229 # to your server. If this doesnt work correctly for
230 # you (although it should), you can remove all this
231 # code except the last two lines, and change the header()
232 # function to look something like this, customized to
233 # the location of SquirrelMail on your server:
235 # http://www.myhost.com/squirrelmail/src/login.php
237 global $PHP_SELF, $SERVER_NAME, $HTTPS, $HTTP_HOST, $SERVER_PORT;
240 $path = substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'));
242 // Check if this is a HTTPS or regular HTTP request
244 if(isset($HTTPS) && !strcasecmp($HTTPS, 'on') ) {
248 // Get the hostname from the Host header or server config.
250 if (isset($HTTP_HOST) && !empty($HTTP_HOST))
254 else if (isset($SERVER_NAME) && !empty($SERVER_NAME))
256 $host = $SERVER_NAME;
260 if (! strstr($host, ':'))
262 if (isset($SERVER_PORT)) {
263 if (($SERVER_PORT != 80 && $proto == 'http://')
264 ||
($SERVER_PORT != 443 && $proto == 'https://')) {
265 $port = sprintf(':%d', $SERVER_PORT);
271 return $proto . $host . $port . $path;
273 // Fallback is to omit the server name and use a relative URI,
274 // although this is not RFC 2616 compliant.
278 function sqStripSlashes($string) {
279 if (get_magic_quotes_gpc()) {
280 $string = stripslashes($string);
286 // These functions are used to encrypt the passowrd before it is
287 // stored in a cookie.
288 function OneTimePadEncrypt ($string, $epad) {
289 $pad = base64_decode($epad);
291 for ($i = 0; $i < strlen ($string); $i++
) {
292 $encrypted .= chr (ord($string[$i]) ^
ord($pad[$i]));
295 return base64_encode($encrypted);
298 function OneTimePadDecrypt ($string, $epad) {
299 $pad = base64_decode($epad);
300 $encrypted = base64_decode ($string);
302 for ($i = 0; $i < strlen ($encrypted); $i++
) {
303 $decrypted .= chr (ord($encrypted[$i]) ^
ord($pad[$i]));
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)
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();
322 if (function_exists('crc32'))
332 $HighBit = $Max ^
$Mask;
333 while ($Pos < strlen($Str))
337 $Val = (($Val & $Mask) << 1) +
1;
341 $Val = ($Val & $Mask) << 1;
354 mt_srand(($Val ^
mt_rand(0, $Max)) & $Max);
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()
363 global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID;
370 sq_mt_seed((int)((double) microtime() * 1000000));
371 sq_mt_seed(md5($REMOTE_PORT . $REMOTE_ADDR . getmypid()));
374 if (function_exists('getrusage')) {
377 foreach ($dat as $k => $v)
381 sq_mt_seed(md5($Str));
385 sq_mt_seed(md5($UNIQUE_ID));
390 function OneTimePadCreate ($length=100) {
394 for ($i = 0; $i < $length; $i++
) {
395 $pad .= chr(mt_rand(0,255));
398 return base64_encode($pad);
401 // Check if we have a required PHP-version. Return TRUE if we do,
402 // or FALSE if we don't.
403 // To check for 4.0.1, use sqCheckPHPVersion(4,0,1)
404 // To check for 4.0b3, use sqCheckPHPVersion(4,0,-3)
405 // Does not handle betas like 4.0.1b1 or development versions
406 function sqCheckPHPVersion($major, $minor, $release) {
409 eregi('^([0-9]+)\\.([0-9]+)(.*)', $ver, $regs);
411 // Parse the version string
412 $vmajor = strval($regs[1]);
413 $vminor = strval($regs[2]);
416 $vrel = strval(substr($vrel, 1));
417 if($vrel[0] == 'b' ||
$vrel[0] == 'B')
418 $vrel = - strval(substr($vrel, 1));
419 if($vrel[0] == 'r' ||
$vrel[0] == 'R')
420 $vrel = - strval(substr($vrel, 2))/10;
422 // Compare major version
423 if($vmajor < $major) return false;
424 if($vmajor > $major) return true;
426 // Major is the same. Compare minor
427 if($vminor < $minor) return false;
428 if($vminor > $minor) return true;
430 // Major and minor is the same as the required one.
432 if($vrel >= 0 && $release >= 0) { // Neither are beta
433 if($vrel < $release) return false;
434 } else if($vrel >= 0 && $release < 0){ // This is not beta, required is beta
436 } else if($vrel < 0 && $release >= 0){ // This is beta, require not beta
438 } else { // Both are beta
439 if($vrel > $release) return false;
445 /* Returns a string showing the size of the message/attachment */
446 function show_readable_size($bytes)
451 if ($bytes / 1024 > 1)
460 settype($bytes, 'integer');
464 settype($bytes, 'integer');
466 return $bytes . '<small> ' . $type . '</small>';
469 /* Generates a random string from the caracter set you pass in
472 * 1 = add lowercase a-z to $chars
473 * 2 = add uppercase A-Z to $chars
474 * 4 = add numbers 0-9 to $chars
477 function GenerateRandomString($size, $chars, $flags = 0)
480 $chars .= 'abcdefghijklmnopqrstuvwxyz';
482 $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
484 $chars .= '0123456789';
486 if ($size < 1 ||
strlen($chars) < 1)
489 sq_mt_randomize(); // Initialize the random number generator
492 while (strlen($String) < $size) {
493 $String .= $chars[mt_rand(0, strlen($chars))];