5 //*************************************************************************
6 // Count the number of occurances of $needle are in $haystack.
7 //*************************************************************************
8 function countCharInString($haystack, $needle) {
9 $haystack = ereg_replace("[^$needle]","",$haystack);
10 return strlen($haystack);
13 //*************************************************************************
14 // Read from the back of $haystack until $needle is found, or the begining
15 // of the $haystack is reached. $needle is a single character
16 //*************************************************************************
17 function readShortMailboxName($haystack, $needle) {
18 if ($needle == "") return $haystack;
19 if ($needle == ".") $needle = "\.";
20 ereg("([^$needle]+)$needle?$", $haystack, $regs);
24 //*************************************************************************
25 // Read from the back of $haystack until $needle is found, or the begining
26 // of the $haystack is reached. $needle is a single character
27 //*************************************************************************
28 function readMailboxParent($haystack, $needle) {
29 if ($needle == ".") $needle = "\.";
30 ereg("^(.+)$needle([^$needle]+)$needle?$", $haystack, $regs);
34 // Searches for the next position in a string minus white space
35 function next_pos_minus_white ($haystack, $pos) {
36 while (substr($haystack, $pos, 1) == " " ||
37 substr($haystack, $pos, 1) == "\t" ||
38 substr($haystack, $pos, 1) == "\n" ||
39 substr($haystack, $pos, 1) == "\r") {
40 if ($pos >= strlen($haystack))
47 // Wraps text at $wrap characters
48 // Has a problem with special HTML characters, so call this before
49 // you do character translation.
50 // Specifically, ' comes up as 5 characters instead of 1.
51 // This should not add newlines to the end of lines.
52 function sqWordWrap(&$line, $wrap) {
53 preg_match("/^([\s>]*)([^\s>].*)?$/", $line, $regs);
54 $beginning_spaces = $regs[1];
55 $words = explode(" ", $regs[2]);
58 $line = $beginning_spaces;
60 while ($i < count($words)) {
61 // Force one word to be on a line (minimum)
63 $line_len = strlen($beginning_spaces) +
strlen($words[$i]) +
64 strlen($words[$i +
1]) +
2;
67 // Add more words (as long as they fit)
68 while ($line_len < $wrap && $i < count($words)) {
69 $line .= ' ' . $words[$i];
71 $line_len +
= strlen($words[$i]) +
1;
74 // Skip spaces if they are the first thing on a continued line
75 while (!$words[$i] && $i < count($words)) {
79 // Go to the next line if we have more to process
80 if ($i < count($words)) {
81 $line .= "\n$beginning_spaces";
87 // Does the opposite of sqWordWrap()
88 function sqUnWordWrap(&$body)
90 $lines = explode("\n", $body);
93 for ($i = 0; $i < count($lines); $i ++
)
95 preg_match("/^([\s>]*)([^\s>].*)?$/", $lines[$i], $regs);
96 $CurrentSpaces = $regs[1];
97 $CurrentRest = $regs[2];
100 $PreviousSpaces = $CurrentSpaces;
103 else if ($PreviousSpaces == $CurrentSpaces && // Do the beginnings match
104 strlen($lines[$i - 1]) > 65 && // Over 65 characters long
105 strlen($CurrentRest)) // and there's a line to continue with
107 $body .= ' ' . $CurrentRest;
111 $body .= "\n" . $lines[$i];
112 $PreviousSpaces = $CurrentSpaces;
119 /** Returns an array of email addresses **/
120 /* Be cautious of "user@host.com" */
121 function parseAddrs($text) {
122 if (trim($text) == "")
124 $text = str_replace(" ", "", $text);
125 $text = ereg_replace('"[^"]*"', "", $text);
126 $text = ereg_replace("\([^\)]*\)", "", $text);
127 $text = str_replace(",", ";", $text);
128 $array = explode(";", $text);
129 for ($i = 0; $i < count ($array); $i++
) {
130 $array[$i] = eregi_replace ("^.*[<]", "", $array[$i]);
131 $array[$i] = eregi_replace ("[>].*$", "", $array[$i]);
136 /** Returns a line of comma separated email addresses from an array **/
137 function getLineOfAddrs($array) {
138 if (is_array($array)) {
139 $to_line = implode(", ", $array);
140 $to_line = trim(ereg_replace(",,+", ",", $to_line));
147 function translateText(&$body, $wrap_at, $charset) {
148 global $where, $what; // from searching
149 global $url_parser_php;
151 if (!isset($url_parser_php)) {
152 include "../functions/url_parser.php";
155 $body_ary = explode("\n", $body);
157 for ($i=0; $i < count($body_ary); $i++
) {
158 $line = $body_ary[$i];
159 if (strlen($line) - 2 >= $wrap_at) {
160 sqWordWrap($line, $wrap_at);
162 $line = charset_decode($charset, $line);
163 $line = str_replace("\t", ' ', $line);
171 if ($line[$pos] == ' ')
175 else if (strpos($line, '>', $pos) === $pos)
187 $line = "<FONT COLOR=FF0000>$line</FONT>";
189 $line = "<FONT COLOR=800000>$line</FONT>";
191 $body_ary[$i] = $line;
193 $body = "<pre>" . implode("\n", $body_ary) . "</pre>";
196 /* SquirrelMail version number -- DO NOT CHANGE */
197 $version = "1.0pre2 (cvs)";
200 function find_mailbox_name ($mailbox) {
202 $mailbox = trim($mailbox);
203 if (substr($mailbox, strlen($mailbox)-1, strlen($mailbox)) == "\"") {
204 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
205 $pos = strrpos ($mailbox, "\"")+1;
206 $box = substr($mailbox, $pos);
208 $box = substr($mailbox, strrpos($mailbox, " ")+1, strlen($mailbox));
213 if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
215 ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
220 function replace_spaces ($string) {
221 return str_replace(" ", " ", $string);
224 function replace_escaped_spaces ($string) {
225 return str_replace(" ", " ", $string);
228 function get_location () {
229 # This determines the location to forward to relative
230 # to your server. If this doesnt work correctly for
231 # you (although it should), you can remove all this
232 # code except the last two lines, and change the header()
233 # function to look something like this, customized to
234 # the location of SquirrelMail on your server:
236 # http://www.myhost.com/squirrelmail/src/login.php
238 global $PHP_SELF, $SERVER_NAME, $HTTPS, $HTTP_HOST, $SERVER_PORT;
241 $path = substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'));
243 // Check if this is a HTTPS or regular HTTP request
245 if(isset($HTTPS) && $HTTPS == 'on' ) {
249 // Get the hostname from the Host header or server config.
251 if (isset($HTTP_HOST) && !empty($HTTP_HOST))
255 else if (isset($SERVER_NAME) && !empty($SERVER_NAME))
257 $host = $SERVER_NAME;
261 if (! strstr($host, ':'))
263 if (isset($SERVER_PORT)) {
264 if (($SERVER_PORT != 80 && $proto == "http://")
265 ||
($SERVER_PORT != 443 && $proto == "https://")) {
266 $port = sprintf(':%d', $SERVER_PORT);
272 return $proto . $host . $port . $path;
274 // Fallback is to omit the server name and use a relative URI,
275 // although this is not RFC 2616 compliant.
279 function sqStripSlashes($string) {
280 if (get_magic_quotes_gpc()) {
281 $string = stripslashes($string);
287 // These functions are used to encrypt the passowrd before it is
288 // stored in a cookie.
289 function OneTimePadEncrypt ($string, $pad) {
290 for ($i = 0; $i < strlen ($string); $i++
) {
291 $encrypted .= chr (ord($string[$i]) ^
ord($pad[$i]));
294 return base64_encode($encrypted);
297 function OneTimePadDecrypt ($string, $pad) {
298 $encrypted = base64_decode ($string);
300 for ($i = 0; $i < strlen ($encrypted); $i++
) {
301 $decrypted .= chr (ord($encrypted[$i]) ^
ord($pad[$i]));
308 // Randomize the mt_rand() function. Toss this in strings or
309 // integers and it will seed the generator appropriately.
310 // With strings, it is better to get them long. Use md5() to
311 // lengthen smaller strings.
312 function sq_mt_seed($Val)
314 // if mt_getrandmax() does not return a 2^n - 1 number,
315 // this might not work well. This uses $Max as a bitmask.
316 $Max = mt_getrandmax();
320 if (function_exists("crc32"))
330 $HighBit = $Max ^
$Mask;
331 while ($Pos < strlen($Str))
335 $Val = (($Val & $Mask) << 1) +
1;
339 $Val = ($Val & $Mask) << 1;
352 mt_srand(($Val ^
mt_rand(0, $Max)) & $Max);
356 // This function initializes the random number generator fairly well.
357 // It also only initializes it once, so you don't accidentally get
358 // the same 'random' numbers twice in one session.
359 function sq_mt_randomize()
361 global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID;
368 sq_mt_seed((int)((double) microtime() * 1000000));
369 sq_mt_seed(md5($REMOTE_PORT . $REMOTE_ADDR . getmypid()));
372 if (function_exists("getrusage")) {
374 sq_mt_seed(md5($dat["ru_nswap"] . $dat["ru_majflt"] .
375 $dat["ru_utime.tv_sec"] . $dat["ru_utime.tv_usec"]));
379 sq_mt_seed(md5($UNIQUE_ID));
384 function OneTimePadCreate ($length=100) {
387 for ($i = 0; $i < $length; $i++
) {
388 $pad .= chr(mt_rand(0,255));
394 // Check if we have a required PHP-version. Return TRUE if we do,
395 // or FALSE if we don't.
396 // To check for 4.0.1, use sqCheckPHPVersion(4,0,1)
397 // To check for 4.0b3, use sqCheckPHPVersion(4,0,-3)
398 // Does not handle betas like 4.0.1b1 or development versions
399 function sqCheckPHPVersion($major, $minor, $release) {
402 eregi("^([0-9]+)\.([0-9]+)(.*)", $ver, $regs);
404 // Parse the version string
405 $vmajor = strval($regs[1]);
406 $vminor = strval($regs[2]);
409 $vrel = strval(substr($vrel, 1));
410 if($vrel[0] == "b" ||
$vrel[0] == "B")
411 $vrel = - strval(substr($vrel, 1));
412 if($vrel[0] == "r" ||
$vrel[0] == "R")
413 $vrel = - strval(substr($vrel, 2))/10;
415 // Compare major version
416 if($vmajor < $major) return false;
417 if($vmajor > $major) return true;
419 // Major is the same. Compare minor
420 if($vminor < $minor) return false;
421 if($vminor > $minor) return true;
423 // Major and minor is the same as the required one.
425 if($vrel >= 0 && $release >= 0) { // Neither are beta
426 if($vrel < $release) return false;
427 } else if($vrel >= 0 && $release < 0){ // This is not beta, required is beta
429 } else if($vrel < 0 && $release >= 0){ // This is beta, require not beta
431 } else { // Both are beta
432 if($vrel > $release) return false;
438 /* Returns a string showing the size of the message/attachment */
439 function show_readable_size($bytes)
444 if ($bytes / 1024 > 1)
453 settype($bytes, "integer");
457 settype($bytes, "integer");
459 return $bytes . '<small> ' . $type . '</small>';
462 /* Generates a random string from the caracter set you pass in
465 * 1 = add lowercase a-z to $chars
466 * 2 = add uppercase A-Z to $chars
467 * 4 = add numbers 0-9 to $chars
470 function GenerateRandomString($size, $chars, $flags = 0)
473 $chars .= 'abcdefghijklmnopqrstuvwxyz';
475 $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
477 $chars .= '0123456789';
479 if ($size < 1 ||
strlen($chars) < 1)
482 sq_mt_randomize(); // Initialize the random number generator
484 while (strlen($String) < $size) {
485 $String .= $chars[mt_rand(0, strlen($chars))];