5 //*************************************************************************
6 // Count the number of occurances of $needle are in $haystack.
7 //*************************************************************************
8 function countCharInString($haystack, $needle) {
9 $len = strlen($haystack);
10 for ($i = 0; $i < $len; $i++
) {
11 if ($haystack[$i] == $needle)
17 //*************************************************************************
18 // Read from the back of $haystack until $needle is found, or the begining
19 // of the $haystack is reached.
20 //*************************************************************************
21 function readShortMailboxName($haystack, $needle) {
22 if (substr($haystack, -1) == $needle)
23 $haystack = substr($haystack, 0, strlen($haystack) - 1);
25 if (strrpos($haystack, $needle)) {
26 $pos = strrpos($haystack, $needle) +
1;
27 $data = substr($haystack, $pos, strlen($haystack));
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 if ($i < count($words)) {
80 $line .= "\n$beginning_spaces";
86 // Does the opposite of sqWordWrap()
87 function sqUnWordWrap(&$body)
89 $lines = explode("\n", $body);
92 for ($i = 0; $i < count($lines); $i ++
)
94 preg_match("/^([\s>]*)([^\s>].*)?$/", $lines[$i], $regs);
95 $CurrentSpaces = $regs[1];
96 $CurrentRest = $regs[2];
99 $PreviousSpaces = $CurrentSpaces;
102 else if ($PreviousSpaces == $CurrentSpaces && // Do the beginnings match
103 strlen($lines[$i - 1]) > 65 && // Over 65 characters long
104 strlen($CurrentRest)) // and there's a line to continue with
106 $body .= ' ' . $CurrentRest;
110 $body .= "\n" . $lines[$i];
111 $PreviousSpaces = $CurrentSpaces;
118 /** Returns an array of email addresses **/
119 function parseAddrs($text) {
120 if (trim($text) == "") {
123 $text = str_replace(" ", "", $text);
124 $text = ereg_replace( '"[^"]*"', "", $text);
125 $text = str_replace(",", ";", $text);
126 $array = explode(";", $text);
127 for ($i = 0; $i < count ($array); $i++
) {
128 $array[$i] = eregi_replace ("^.*[<]", "", $array[$i]);
129 $array[$i] = eregi_replace ("[>].*$", "", $array[$i]);
134 /** Returns a line of comma separated email addresses from an array **/
135 function getLineOfAddrs($array) {
136 if (is_array($array)) {
137 $to_line = implode(", ", $array);
138 $to_line = trim(ereg_replace(",,+", ",", $to_line));
145 function translateText(&$body, $wrap_at, $charset) {
146 global $where, $what; // from searching
148 if (!isset($url_parser_php)) {
149 include "../functions/url_parser.php";
152 $body_ary = explode("\n", $body);
153 for ($i=0; $i < count($body_ary); $i++
) {
154 $line = $body_ary[$i];
155 if (strlen($line) - 2 >= $wrap_at) {
156 sqWordWrap($line, $wrap_at);
158 $line = charset_decode($charset, $line);
159 $line = str_replace("\t", ' ', $line);
161 $line = str_replace(' ', ' ', $line);
162 $line = nl2br($line);
170 if (strpos($line, ' ', $pos) === $pos)
174 else if (strpos($line, '>', $pos) === $pos)
186 $line = "<FONT COLOR=FF0000>$line</FONT>\n";
187 } else if ($Quotes) {
188 $line = "<FONT COLOR=800000>$line</FONT>\n";
193 $line = '<tt>' . $line . '</tt>';
196 $body_ary[$i] = $line . '<br>';
198 $body = implode("\n", $body_ary);
201 /* SquirrelMail version number -- DO NOT CHANGE */
202 $version = "0.6pre1 (cvs)";
205 function find_mailbox_name ($mailbox) {
206 $mailbox = trim($mailbox);
207 if (substr($mailbox, strlen($mailbox)-1, strlen($mailbox)) == "\"") {
208 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
209 $pos = strrpos ($mailbox, "\"")+
1;
210 $box = substr($mailbox, $pos);
212 $box = substr($mailbox, strrpos($mailbox, " ")+
1, strlen($mailbox));
217 function replace_spaces ($string) {
218 return str_replace(" ", " ", $string);
221 function replace_escaped_spaces ($string) {
222 return str_replace(" ", " ", $string);
225 function get_location () {
226 # This determines the location to forward to relative
227 # to your server. If this doesnt work correctly for
228 # you (although it should), you can remove all this
229 # code except the last two lines, and change the header()
230 # function to look something like this, customized to
231 # the location of SquirrelMail on your server:
233 # http://www.myhost.com/squirrelmail/src/login.php
235 global $PHP_SELF, $SERVER_NAME, $HTTPS, $HTTP_HOST, $SERVER_PORT;
238 $path = substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'));
240 // Check if this is a HTTPS or regular HTTP request
242 if(isset($HTTPS) && $HTTPS == 'on' ) {
247 if (isset($SERVER_PORT)) {
248 if ($SERVER_PORT != 80) {
249 $port = sprintf(':%d', $SERVER_PORT);
253 // Get the hostname from the Host header or server config.
254 // Fallback is to omit the server name and use a relative URI,
255 // although this is not RFC 2616 compliant.
256 if(isset($HTTP_HOST) && !empty($HTTP_HOST)) {
257 $location = $proto . $HTTP_HOST . $port . $path;
258 } else if(isset($SERVER_NAME) && !empty($SERVER_NAME)) {
259 $location = $proto . $SERVER_NAME . $port . $path;
266 function sqStripSlashes($string) {
267 if (get_magic_quotes_gpc()) {
268 $string = stripslashes($string);
274 // These functions are used to encrypt the passowrd before it is
275 // stored in a cookie.
276 function OneTimePadEncrypt ($string, $pad) {
277 for ($i = 0; $i < strlen ($string); $i++
) {
278 $encrypted .= chr (ord($string[$i]) ^
ord($pad[$i]));
281 return base64_encode($encrypted);
284 function OneTimePadDecrypt ($string, $pad) {
285 $encrypted = base64_decode ($string);
287 for ($i = 0; $i < strlen ($encrypted); $i++
) {
288 $decrypted .= chr (ord($encrypted[$i]) ^
ord($pad[$i]));
295 // Randomize the mt_rand() function. Toss this in strings or
296 // integers and it will seed the generator appropriately.
297 // With strings, it is better to get them long. Use md5() to
298 // lengthen smaller strings.
299 function sq_mt_seed($Val)
301 // if mt_getrandmax() does not return a 2^n - 1 number,
302 // this might not work well. This uses $Max as a bitmask.
303 $Max = mt_getrandmax();
307 if (function_exists("crc32"))
317 $HighBit = $Max ^
$Mask;
318 while ($Pos < strlen($Str))
322 $Val = (($Val & $Mask) << 1) +
1;
326 $Val = ($Val & $Mask) << 1;
339 mt_srand(($Val ^
mt_rand(0, $Max)) & $Max);
343 // This function initializes the random number generator fairly well.
344 // It also only initializes it once, so you don't accidentally get
345 // the same 'random' numbers twice in one session.
346 function sq_mt_randomize()
348 global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID;
355 sq_mt_seed((int)((double) microtime() * 1000000));
356 sq_mt_seed(md5($REMOTE_PORT . $REMOTE_ADDR . getmypid()));
359 if (function_exists("getrusage")) {
361 sq_mt_seed(md5($dat["ru_nswap"] . $dat["ru_majflt"] .
362 $dat["ru_utime.tv_sec"] . $dat["ru_utime.tv_usec"]));
366 sq_mt_seed(md5($UNIQUE_ID));
371 function OneTimePadCreate ($length=100) {
374 for ($i = 0; $i < $length; $i++
) {
375 $pad .= chr(mt_rand(0,255));
381 // Check if we have a required PHP-version. Return TRUE if we do,
382 // or FALSE if we don't.
383 // To check for 4.0.1, use sqCheckPHPVersion(4,0,1)
384 // To check for 4.0b3, use sqCheckPHPVersion(4,0,-3)
385 // Does not handle betas like 4.0.1b1 or development versions
386 function sqCheckPHPVersion($major, $minor, $release) {
389 eregi("^([0-9]+)\.([0-9]+)(.*)", $ver, $regs);
391 // Parse the version string
392 $vmajor = strval($regs[1]);
393 $vminor = strval($regs[2]);
396 $vrel = strval(substr($vrel, 1));
397 if($vrel[0] == "b" ||
$vrel[0] == "B")
398 $vrel = - strval(substr($vrel, 1));
399 if($vrel[0] == "r" ||
$vrel[0] == "R")
400 $vrel = - strval(substr($vrel, 2))/10;
402 // Compare major version
403 if($vmajor < $major) return false;
404 if($vmajor > $major) return true;
406 // Major is the same. Compare minor
407 if($vminor < $minor) return false;
408 if($vminor > $minor) return true;
410 // Major and minor is the same as the required one.
412 if($vrel >= 0 && $release >= 0) { // Neither are beta
413 if($vrel < $release) return false;
414 } else if($vrel >= 0 && $release < 0){ // This is not beta, required is beta
416 } else if($vrel < 0 && $release >= 0){ // This is beta, require not beta
418 } else { // Both are beta
419 if($vrel > $release) return false;
425 /* Returns a string showing the size of the message/attachment */
426 function show_readable_size($bytes)
431 if ($bytes / 1024 > 1)
440 settype($bytes, "integer");
444 settype($bytes, "integer");
446 return $bytes . '<small> ' . $type . '</small>';
449 /* Generates a random string from the caracter set you pass in
452 * 1 = add lowercase a-z to $chars
453 * 2 = add uppercase A-Z to $chars
454 * 4 = add numbers 0-9 to $chars
457 function GenerateRandomString($size, $chars, $flags = 0)
460 $chars .= 'abcdefghijklmnopqrstuvwxyz';
462 $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
464 $chars .= '0123456789';
466 if ($size < 1 ||
strlen($chars) < 1)
469 sq_mt_randomize(); // Initialize the random number generator
471 while (strlen($String) < $size) {
472 $String .= $chars[mt_rand(0, strlen($chars))];