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 ereg("^$needle?([^$needle]+)$needle*",strrev($haystack),$regs);
19 return strrev($regs[1]);
22 // Searches for the next position in a string minus white space
23 function next_pos_minus_white ($haystack, $pos) {
24 while (substr($haystack, $pos, 1) == " " ||
25 substr($haystack, $pos, 1) == "\t" ||
26 substr($haystack, $pos, 1) == "\n" ||
27 substr($haystack, $pos, 1) == "\r") {
28 if ($pos >= strlen($haystack))
35 // Wraps text at $wrap characters
36 // Has a problem with special HTML characters, so call this before
37 // you do character translation.
38 // Specifically, ' comes up as 5 characters instead of 1.
39 // This should not add newlines to the end of lines.
40 function sqWordWrap(&$line, $wrap) {
41 preg_match("/^([\s>]*)([^\s>].*)?$/", $line, $regs);
42 $beginning_spaces = $regs[1];
43 $words = explode(" ", $regs[2]);
46 $line = $beginning_spaces;
48 while ($i < count($words)) {
49 // Force one word to be on a line (minimum)
51 $line_len = strlen($beginning_spaces) +
strlen($words[$i]) +
52 strlen($words[$i +
1]) +
2;
55 // Add more words (as long as they fit)
56 while ($line_len < $wrap && $i < count($words)) {
57 $line .= ' ' . $words[$i];
59 $line_len +
= strlen($words[$i]) +
1;
62 // Skip spaces if they are the first thing on a continued line
63 while (!$words[$i] && $i < count($words)) {
67 if ($i < count($words)) {
68 $line .= "\n$beginning_spaces";
74 // Does the opposite of sqWordWrap()
75 function sqUnWordWrap(&$body)
77 $lines = explode("\n", $body);
80 for ($i = 0; $i < count($lines); $i ++
)
82 preg_match("/^([\s>]*)([^\s>].*)?$/", $lines[$i], $regs);
83 $CurrentSpaces = $regs[1];
84 $CurrentRest = $regs[2];
87 $PreviousSpaces = $CurrentSpaces;
90 else if ($PreviousSpaces == $CurrentSpaces && // Do the beginnings match
91 strlen($lines[$i - 1]) > 65 && // Over 65 characters long
92 strlen($CurrentRest)) // and there's a line to continue with
94 $body .= ' ' . $CurrentRest;
98 $body .= "\n" . $lines[$i];
99 $PreviousSpaces = $CurrentSpaces;
106 /** Returns an array of email addresses **/
107 function parseAddrs($text) {
108 if (trim($text) == "") {
111 $text=ereg_replace("[;,][^<]*<* *([^>]*) *>*",";\\1",",$text");
112 return split("[;]", substr($text,1));
115 /** Returns a line of comma separated email addresses from an array **/
116 function getLineOfAddrs($array) {
117 if (is_array($array)) {
118 $to_line = implode(", ", $array);
119 $to_line = trim(ereg_replace(",,+", ",", $to_line));
126 function translateText(&$body, $wrap_at, $charset) {
127 global $where, $what; // from searching
129 if (!isset($url_parser_php)) {
130 include "../functions/url_parser.php";
133 $body_ary = explode("\n", $body);
134 for ($i=0; $i < count($body_ary); $i++
) {
135 $line = $body_ary[$i];
136 if (strlen($line) - 2 >= $wrap_at) {
137 sqWordWrap($line, $wrap_at);
139 $line = charset_decode($charset, $line);
140 $line = str_replace("\t", ' ', $line);
142 // We need to do it twice to catch times where there
143 // are an odd number of spaces
144 $line = ereg_replace("^ ", " ", $line);
145 $line = str_replace(' ', ' ', $line);
146 $line = str_replace(' ', ' ', $line);
147 $line = nl2br($line);
155 if (strpos($line, ' ', $pos) === $pos)
159 else if (strpos($line, '>', $pos) === $pos)
171 $line = "<FONT COLOR=FF0000>$line</FONT>\n";
172 } else if ($Quotes) {
173 $line = "<FONT COLOR=800000>$line</FONT>\n";
178 $line = '<tt>' . $line . '</tt>';
181 $body_ary[$i] = $line . '<br>';
183 $body = implode("\n", $body_ary);
186 /* SquirrelMail version number -- DO NOT CHANGE */
187 $version = "0.6pre1 (cvs)";
190 function find_mailbox_name ($mailbox) {
192 $mailbox = trim($mailbox);
193 if (substr($mailbox, strlen($mailbox)-1, strlen($mailbox)) == "\"") {
194 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
195 $pos = strrpos ($mailbox, "\"")+1;
196 $box = substr($mailbox, $pos);
198 $box = substr($mailbox, strrpos($mailbox, " ")+1, strlen($mailbox));
203 if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
205 ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
210 function replace_spaces ($string) {
211 return str_replace(" ", " ", $string);
214 function replace_escaped_spaces ($string) {
215 return str_replace(" ", " ", $string);
218 function get_location () {
219 # This determines the location to forward to relative
220 # to your server. If this doesnt work correctly for
221 # you (although it should), you can remove all this
222 # code except the last two lines, and change the header()
223 # function to look something like this, customized to
224 # the location of SquirrelMail on your server:
226 # http://www.myhost.com/squirrelmail/src/login.php
228 global $PHP_SELF, $SERVER_NAME, $HTTPS, $HTTP_HOST, $SERVER_PORT;
231 $path = substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'));
233 // Check if this is a HTTPS or regular HTTP request
235 if(isset($HTTPS) && $HTTPS == 'on' ) {
239 // Get the hostname from the Host header or server config.
241 if (isset($HTTP_HOST) && !empty($HTTP_HOST))
245 else if (isset($SERVER_NAME) && !empty($SERVER_NAME))
247 $host = $SERVER_NAME;
251 if (! strstr($host, ':'))
253 if (isset($SERVER_PORT)) {
254 if ($SERVER_PORT != 80) {
255 $port = sprintf(':%d', $SERVER_PORT);
261 return $proto . $host . $port . $path;
263 // Fallback is to omit the server name and use a relative URI,
264 // although this is not RFC 2616 compliant.
268 function sqStripSlashes($string) {
269 if (get_magic_quotes_gpc()) {
270 $string = stripslashes($string);
276 // These functions are used to encrypt the passowrd before it is
277 // stored in a cookie.
278 function OneTimePadEncrypt ($string, $pad) {
279 for ($i = 0; $i < strlen ($string); $i++
) {
280 $encrypted .= chr (ord($string[$i]) ^
ord($pad[$i]));
283 return base64_encode($encrypted);
286 function OneTimePadDecrypt ($string, $pad) {
287 $encrypted = base64_decode ($string);
289 for ($i = 0; $i < strlen ($encrypted); $i++
) {
290 $decrypted .= chr (ord($encrypted[$i]) ^
ord($pad[$i]));
297 // Randomize the mt_rand() function. Toss this in strings or
298 // integers and it will seed the generator appropriately.
299 // With strings, it is better to get them long. Use md5() to
300 // lengthen smaller strings.
301 function sq_mt_seed($Val)
303 // if mt_getrandmax() does not return a 2^n - 1 number,
304 // this might not work well. This uses $Max as a bitmask.
305 $Max = mt_getrandmax();
309 if (function_exists("crc32"))
319 $HighBit = $Max ^
$Mask;
320 while ($Pos < strlen($Str))
324 $Val = (($Val & $Mask) << 1) +
1;
328 $Val = ($Val & $Mask) << 1;
341 mt_srand(($Val ^
mt_rand(0, $Max)) & $Max);
345 // This function initializes the random number generator fairly well.
346 // It also only initializes it once, so you don't accidentally get
347 // the same 'random' numbers twice in one session.
348 function sq_mt_randomize()
350 global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID;
357 sq_mt_seed((int)((double) microtime() * 1000000));
358 sq_mt_seed(md5($REMOTE_PORT . $REMOTE_ADDR . getmypid()));
361 if (function_exists("getrusage")) {
363 sq_mt_seed(md5($dat["ru_nswap"] . $dat["ru_majflt"] .
364 $dat["ru_utime.tv_sec"] . $dat["ru_utime.tv_usec"]));
368 sq_mt_seed(md5($UNIQUE_ID));
373 function OneTimePadCreate ($length=100) {
376 for ($i = 0; $i < $length; $i++
) {
377 $pad .= chr(mt_rand(0,255));
383 // Check if we have a required PHP-version. Return TRUE if we do,
384 // or FALSE if we don't.
385 // To check for 4.0.1, use sqCheckPHPVersion(4,0,1)
386 // To check for 4.0b3, use sqCheckPHPVersion(4,0,-3)
387 // Does not handle betas like 4.0.1b1 or development versions
388 function sqCheckPHPVersion($major, $minor, $release) {
391 eregi("^([0-9]+)\.([0-9]+)(.*)", $ver, $regs);
393 // Parse the version string
394 $vmajor = strval($regs[1]);
395 $vminor = strval($regs[2]);
398 $vrel = strval(substr($vrel, 1));
399 if($vrel[0] == "b" ||
$vrel[0] == "B")
400 $vrel = - strval(substr($vrel, 1));
401 if($vrel[0] == "r" ||
$vrel[0] == "R")
402 $vrel = - strval(substr($vrel, 2))/10;
404 // Compare major version
405 if($vmajor < $major) return false;
406 if($vmajor > $major) return true;
408 // Major is the same. Compare minor
409 if($vminor < $minor) return false;
410 if($vminor > $minor) return true;
412 // Major and minor is the same as the required one.
414 if($vrel >= 0 && $release >= 0) { // Neither are beta
415 if($vrel < $release) return false;
416 } else if($vrel >= 0 && $release < 0){ // This is not beta, required is beta
418 } else if($vrel < 0 && $release >= 0){ // This is beta, require not beta
420 } else { // Both are beta
421 if($vrel > $release) return false;
427 /* Returns a string showing the size of the message/attachment */
428 function show_readable_size($bytes)
433 if ($bytes / 1024 > 1)
442 settype($bytes, "integer");
446 settype($bytes, "integer");
448 return $bytes . '<small> ' . $type . '</small>';
451 /* Generates a random string from the caracter set you pass in
454 * 1 = add lowercase a-z to $chars
455 * 2 = add uppercase A-Z to $chars
456 * 4 = add numbers 0-9 to $chars
459 function GenerateRandomString($size, $chars, $flags = 0)
462 $chars .= 'abcdefghijklmnopqrstuvwxyz';
464 $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
466 $chars .= '0123456789';
468 if ($size < 1 ||
strlen($chars) < 1)
471 sq_mt_randomize(); // Initialize the random number generator
473 while (strlen($String) < $size) {
474 $String .= $chars[mt_rand(0, strlen($chars))];