02e5fc3ad741096fab8c091ba539bc2f10d387e7
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 function sqWordWrap(&$line, $wrap) {
49 $line = str_replace("<", "<", $line);
50 $line = str_replace(">", ">", $line);
52 preg_match("/^(\s|>)+/", $line, $regs);
53 $beginning_spaces = $regs[0];
55 $words = explode(" ", $line);
57 $line_len = strlen($words[0])+
1;
59 if (count($words) > 1) {
60 while ($i++
< count($words)) {
61 while ($line_len < $wrap && $i < count($words)) {
62 $line = "$line$words[$i] ";
64 $line_len = $line_len +
strlen($words[$i]) +
1;
66 $line_len = strlen($words[$i])+
1;
67 if ($line_len <= $wrap) {
68 if (strlen($beginning_spaces) +
2 >= $wrap)
69 $beginning_spaces = "";
70 if ($i < count($words)) { // don't <BR> the last line
71 $line = "$line\n$beginning_spaces";
73 $line = "$line$words[$i] ";
74 $line_len = strlen($beginning_spaces) +
strlen($words[$i]) +
1;
77 $endline = $words[$i];
78 while ($line_len >= $wrap) {
79 $bigline = substr($endline, 0, $wrap);
80 $endline = substr($endline, $wrap, strlen($endline));
81 $line_len = strlen($endline);
82 $line = "$line$bigline<BR>";
85 if (strlen($line) > $wrap)
86 $line = "$line\n$words[$i]";
88 $line = "$line$words[$i]";
89 $line_len = strlen($words[$i]);
96 $line = str_replace(">", ">", $line);
97 $line = str_replace("<", "<", $line);
100 /** Returns an array of email addresses **/
101 function parseAddrs($text) {
102 if (trim($text) == "") {
105 $text = str_replace(" ", "", $text);
106 $text = ereg_replace( '"[^"]*"', "", $text);
107 $text = str_replace(",", ";", $text);
108 $array = explode(";", $text);
109 for ($i = 0; $i < count ($array); $i++
) {
110 $array[$i] = eregi_replace ("^.*[<]", "", $array[$i]);
111 $array[$i] = eregi_replace ("[>].*$", "", $array[$i]);
116 /** Returns a line of comma separated email addresses from an array **/
117 function getLineOfAddrs($array) {
118 if (is_array($array)) {
119 $to_line = implode(", ", $array);
120 $to_line = trim(ereg_replace(",,+", ",", $to_line));
127 function translateText(&$body, $wrap_at, $charset) {
128 global $where, $what; // from searching
130 if (!isset($url_parser_php)) {
131 include "../functions/url_parser.php";
134 $body_ary = explode("\n", $body);
135 for ($i=0; $i < count($body_ary); $i++
) {
136 $line = $body_ary[$i];
137 $line = charset_decode($charset, $line);
138 $line = str_replace("\t", ' ', $line);
141 if (strlen($line) - 2 >= $wrap_at) {
142 sqWordWrap($line, $wrap_at);
145 $line = str_replace(' ', ' ', $line);
146 $line = nl2br($line);
154 if (strpos($line, ' ', $pos) === $pos)
158 else if (strpos($line, '>', $pos) === $pos)
170 $line = "<FONT COLOR=FF0000>$line</FONT>\n";
171 } else if ($Quotes) {
172 $line = "<FONT COLOR=800000>$line</FONT>\n";
177 $line = '<tt>' . $line . '</tt>';
180 $body_ary[$i] = $line . '<br>';
182 $body = implode("\n", $body_ary);
185 /* SquirrelMail version number -- DO NOT CHANGE */
186 $version = "0.6pre1 (cvs)";
189 function find_mailbox_name ($mailbox) {
190 $mailbox = trim($mailbox);
191 if (substr($mailbox, strlen($mailbox)-1, strlen($mailbox)) == "\"") {
192 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
193 $pos = strrpos ($mailbox, "\"")+
1;
194 $box = substr($mailbox, $pos);
196 $box = substr($mailbox, strrpos($mailbox, " ")+
1, strlen($mailbox));
201 function replace_spaces ($string) {
202 return str_replace(" ", " ", $string);
205 function replace_escaped_spaces ($string) {
206 return str_replace(" ", " ", $string);
209 function get_location () {
210 # This determines the location to forward to relative
211 # to your server. If this doesnt work correctly for
212 # you (although it should), you can remove all this
213 # code except the last two lines, and change the header()
214 # function to look something like this, customized to
215 # the location of SquirrelMail on your server:
217 # http://www.myhost.com/squirrelmail/src/login.php
219 global $PHP_SELF, $SERVER_NAME, $HTTPS, $HTTP_HOST, $SERVER_PORT;
222 $path = substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'));
224 // Check if this is a HTTPS or regular HTTP request
226 if(isset($HTTPS) && $HTTPS == 'on' ) {
231 if (isset($SERVER_PORT)) {
232 if ($SERVER_PORT != 80) {
233 $port = sprintf(':%d', $SERVER_PORT);
237 // Get the hostname from the Host header or server config.
238 // Fallback is to omit the server name and use a relative URI,
239 // although this is not RFC 2616 compliant.
240 if(isset($HTTP_HOST) && !empty($HTTP_HOST)) {
241 $location = $proto . $HTTP_HOST . $port . $path;
242 } else if(isset($SERVER_NAME) && !empty($SERVER_NAME)) {
243 $location = $proto . $SERVER_NAME . $port . $path;
250 function sqStripSlashes($string) {
251 if (get_magic_quotes_gpc()) {
252 $string = stripslashes($string);
258 // These functions are used to encrypt the passowrd before it is
259 // stored in a cookie.
260 function OneTimePadEncrypt ($string, $pad) {
261 for ($i = 0; $i < strlen ($string); $i++
) {
262 $encrypted .= chr (ord($string[$i]) ^
ord($pad[$i]));
265 return base64_encode($encrypted);
268 function OneTimePadDecrypt ($string, $pad) {
269 $encrypted = base64_decode ($string);
271 for ($i = 0; $i < strlen ($encrypted); $i++
) {
272 $decrypted .= chr (ord($encrypted[$i]) ^
ord($pad[$i]));
279 // Randomize the mt_rand() function. Toss this in strings or
280 // integers and it will seed the generator appropriately.
281 // With strings, it is better to get them long. Use md5() to
282 // lengthen smaller strings.
283 function sq_mt_seed($Val)
285 // if mt_getrandmax() does not return a 2^n - 1 number,
286 // this might not work well. This uses $Max as a bitmask.
287 $Max = mt_getrandmax();
291 if (function_exists("crc32"))
301 $HighBit = $Max ^
$Mask;
302 while ($Pos < strlen($Str))
306 $Val = (($Val & $Mask) << 1) +
1;
310 $Val = ($Val & $Mask) << 1;
323 mt_srand(($Val ^
mt_rand(0, $Max)) & $Max);
327 // This function initializes the random number generator fairly well.
328 // It also only initializes it once, so you don't accidentally get
329 // the same 'random' numbers twice in one session.
330 function sq_mt_randomize()
332 global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID;
339 sq_mt_seed((int)((double) microtime() * 1000000));
340 sq_mt_seed(md5($REMOTE_PORT . $REMOTE_ADDR . getmypid()));
343 if (function_exists("getrusage")) {
345 sq_mt_seed(md5($dat["ru_nswap"] . $dat["ru_majflt"] .
346 $dat["ru_utime.tv_sec"] . $dat["ru_utime.tv_usec"]));
350 sq_mt_seed(md5($UNIQUE_ID));
355 function OneTimePadCreate ($length=100) {
358 for ($i = 0; $i < $length; $i++
) {
359 $pad .= chr(mt_rand(0,255));
365 // Check if we have a required PHP-version. Return TRUE if we do,
366 // or FALSE if we don't.
367 // To check for 4.0.1, use sqCheckPHPVersion(4,0,1)
368 // To check for 4.0b3, use sqCheckPHPVersion(4,0,-3)
369 // Does not handle betas like 4.0.1b1 or development versions
370 function sqCheckPHPVersion($major, $minor, $release) {
373 eregi("^([0-9]+)\.([0-9]+)(.*)", $ver, $regs);
375 // Parse the version string
376 $vmajor = strval($regs[1]);
377 $vminor = strval($regs[2]);
380 $vrel = strval(substr($vrel, 1));
381 if($vrel[0] == "b" ||
$vrel[0] == "B")
382 $vrel = - strval(substr($vrel, 1));
383 if($vrel[0] == "r" ||
$vrel[0] == "R")
384 $vrel = - strval(substr($vrel, 2))/10;
386 // Compare major version
387 if($vmajor < $major) return false;
388 if($vmajor > $major) return true;
390 // Major is the same. Compare minor
391 if($vminor < $minor) return false;
392 if($vminor > $minor) return true;
394 // Major and minor is the same as the required one.
396 if($vrel >= 0 && $release >= 0) { // Neither are beta
397 if($vrel < $release) return false;
398 } else if($vrel >= 0 && $release < 0){ // This is not beta, required is beta
400 } else if($vrel < 0 && $release >= 0){ // This is beta, require not beta
402 } else { // Both are beta
403 if($vrel > $release) return false;