1cf6be0d74acb36f46161f7e33b6679a68757e63
6 $fix_form_endlines = false;
8 // Remove all slashes for form values
9 if (get_magic_quotes_gpc())
11 global $REQUEST_METHOD;
12 if ($REQUEST_METHOD == "POST")
14 global $HTTP_POST_VARS;
15 RemoveSlashes($HTTP_POST_VARS);
17 elseif ($REQUEST_METHOD == "GET")
19 global $HTTP_GET_VARS;
20 RemoveSlashes($HTTP_GET_VARS);
26 // if $send (the form button's name) contains "\n" as the first char
27 // or "\r\n" as the first two (compensating for RedHat's flawed package
28 // and Konqueror, respectively), and the script is compose.php, then
29 // trim everything. Otherwise, we don't have to worry.
31 // If RedHat ever gets PHP officially upgraded past package php-4.0.4pl1-3
32 // or if Konqueror and PHP start working together, modify/remove this hack
33 global $send, $PHP_SELF;
35 if (isset($send) && substr($PHP_SELF, -12) == "/compose.php")
37 if (substr($send, 0, 1) == "\n")
39 if (substr($send, 0, 2) == "\r\n")
44 if ($REQUEST_METHOD == "POST") {
45 TrimArray($HTTP_POST_VARS, $trimChars);
47 TrimArray($HTTP_GET_VARS, $trimChars);
51 //**************************************************************************
52 // Trims every element in the array
53 //**************************************************************************
54 function TrimArray(&$array, $trimChars) {
55 foreach ($array as $k => $v) {
58 foreach ($
$k as $k2 => $v2) {
59 $
$k[$k2] = substr($v2, $trimChars);
62 $
$k = substr($v, $trimChars);
68 //**************************************************************************
69 // Removes slashes from every element in the array
70 //**************************************************************************
71 function RemoveSlashes($array)
73 foreach ($array as $k => $v)
78 foreach ($
$k as $k2 => $v2)
80 $newArray[stripslashes($k2)] = stripslashes($v2);
86 $
$k = stripslashes($v);
92 //*************************************************************************
93 // Count the number of occurances of $needle are in $haystack.
94 // $needle can be a character or string, and need not occur in $haystack
95 //*************************************************************************
96 function countCharInString($haystack, $needle) {
97 if ($needle == '') return 0;
98 return count(explode($needle, $haystack));
101 //*************************************************************************
102 // Read from the back of $haystack until $needle is found, or the begining
103 // of the $haystack is reached. $needle is a single character
104 //*************************************************************************
105 function readShortMailboxName($haystack, $needle) {
106 if ($needle == '') return $haystack;
107 $parts = explode($needle, $haystack);
108 $elem = array_pop($parts);
109 while ($elem == '' && count($parts))
111 $elem = array_pop($parts);
116 //*************************************************************************
117 // Read from the back of $haystack until $needle is found, or the begining
118 // of the $haystack is reached. $needle is a single character
119 //*************************************************************************
120 function readMailboxParent($haystack, $needle) {
121 if ($needle == '') return '';
122 $parts = explode($needle, $haystack);
123 $elem = array_pop($parts);
124 while ($elem == '' && count($parts))
126 $elem = array_pop($parts);
128 return join($needle, $parts);
131 // Searches for the next position in a string minus white space
132 function next_pos_minus_white ($haystack, $pos) {
133 while (substr($haystack, $pos, 1) == ' ' ||
134 substr($haystack, $pos, 1) == "\t" ||
135 substr($haystack, $pos, 1) == "\n" ||
136 substr($haystack, $pos, 1) == "\r") {
137 if ($pos >= strlen($haystack))
144 // Wraps text at $wrap characters
145 // Has a problem with special HTML characters, so call this before
146 // you do character translation.
147 // Specifically, ' comes up as 5 characters instead of 1.
148 // This should not add newlines to the end of lines.
149 function sqWordWrap(&$line, $wrap) {
150 ereg("^([\t >]*)([^\t >].*)?$", $line, $regs);
151 $beginning_spaces = $regs[1];
152 if (isset($regs[2])) {
153 $words = explode(' ', $regs[2]);
159 $line = $beginning_spaces;
161 while ($i < count($words)) {
162 // Force one word to be on a line (minimum)
164 $line_len = strlen($beginning_spaces) +
strlen($words[$i]) +
2;
165 if (isset($words[$i +
1]))
166 $line_len +
= strlen($words[$i +
1]);
169 // Add more words (as long as they fit)
170 while ($line_len < $wrap && $i < count($words)) {
171 $line .= ' ' . $words[$i];
173 if (isset($words[$i]))
174 $line_len +
= strlen($words[$i]) +
1;
179 // Skip spaces if they are the first thing on a continued line
180 while (!isset($words[$i]) && $i < count($words)) {
184 // Go to the next line if we have more to process
185 if ($i < count($words)) {
186 $line .= "\n" . $beginning_spaces;
192 // Does the opposite of sqWordWrap()
193 function sqUnWordWrap(&$body)
195 $lines = explode("\n", $body);
197 $PreviousSpaces = "";
198 for ($i = 0; $i < count($lines); $i ++
)
200 ereg("^([\t >]*)([^\t >].*)?$", $lines[$i], $regs);
201 $CurrentSpaces = $regs[1];
203 $CurrentRest = $regs[2];
206 $PreviousSpaces = $CurrentSpaces;
209 else if ($PreviousSpaces == $CurrentSpaces && // Do the beginnings match
210 strlen($lines[$i - 1]) > 65 && // Over 65 characters long
211 strlen($CurrentRest)) // and there's a line to continue with
213 $body .= ' ' . $CurrentRest;
217 $body .= "\n" . $lines[$i];
218 $PreviousSpaces = $CurrentSpaces;
225 /** Returns an array of email addresses **/
226 /* Be cautious of "user@host.com" */
227 function parseAddrs($text) {
228 if (trim($text) == "")
230 $text = str_replace(' ', '', $text);
231 $text = ereg_replace('"[^"]*"', '', $text);
232 $text = ereg_replace('\\([^\\)]*\\)', '', $text);
233 $text = str_replace(',', ';', $text);
234 $array = explode(';', $text);
235 for ($i = 0; $i < count ($array); $i++
) {
236 $array[$i] = eregi_replace ("^.*[<]", '', $array[$i]);
237 $array[$i] = eregi_replace ("[>].*$", '', $array[$i]);
242 /** Returns a line of comma separated email addresses from an array **/
243 function getLineOfAddrs($array) {
244 if (is_array($array)) {
245 $to_line = implode(', ', $array);
246 $to_line = trim(ereg_replace(', (, )+', ', ', $to_line));
253 function translateText(&$body, $wrap_at, $charset) {
254 global $where, $what; // from searching
255 global $url_parser_php;
257 if (!isset($url_parser_php)) {
258 include '../functions/url_parser.php';
261 $body_ary = explode("\n", $body);
263 for ($i=0; $i < count($body_ary); $i++
) {
264 $line = $body_ary[$i];
265 if (strlen($line) - 2 >= $wrap_at) {
266 sqWordWrap($line, $wrap_at);
268 $line = charset_decode($charset, $line);
269 $line = str_replace("\t", ' ', $line);
277 if ($line[$pos] == ' ')
281 else if (strpos($line, '>', $pos) === $pos)
293 $line = '<FONT COLOR="FF0000">'.$line.'</FONT>';
295 $line = '<FONT COLOR="800000">'.$line.'</FONT>';
297 $body_ary[$i] = $line;
299 $body = '<pre>' . implode("\n", $body_ary) . '</pre>';
302 /* SquirrelMail version number -- DO NOT CHANGE */
303 $version = '1.1.0 [cvs]';
306 function find_mailbox_name ($mailbox) {
307 if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
309 ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
314 // Depreciated. :-) I always wanted to say that.
315 function replace_spaces ($string) {
316 return str_replace(' ', ' ', $string);
319 function get_location () {
320 # This determines the location to forward to relative
321 # to your server. If this doesnt work correctly for
322 # you (although it should), you can remove all this
323 # code except the last two lines, and change the header()
324 # function to look something like this, customized to
325 # the location of SquirrelMail on your server:
327 # http://www.myhost.com/squirrelmail/src/login.php
329 global $PHP_SELF, $SERVER_NAME, $HTTPS, $HTTP_HOST, $SERVER_PORT;
332 $path = substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'));
334 // Check if this is a HTTPS or regular HTTP request
336 if(isset($HTTPS) && !strcasecmp($HTTPS, 'on') ) {
340 // Get the hostname from the Host header or server config.
342 if (isset($HTTP_HOST) && !empty($HTTP_HOST))
346 else if (isset($SERVER_NAME) && !empty($SERVER_NAME))
348 $host = $SERVER_NAME;
352 if (! strstr($host, ':'))
354 if (isset($SERVER_PORT)) {
355 if (($SERVER_PORT != 80 && $proto == 'http://')
356 ||
($SERVER_PORT != 443 && $proto == 'https://')) {
357 $port = sprintf(':%d', $SERVER_PORT);
363 return $proto . $host . $port . $path;
365 // Fallback is to omit the server name and use a relative URI,
366 // although this is not RFC 2616 compliant.
371 // These functions are used to encrypt the passowrd before it is
372 // stored in a cookie.
373 function OneTimePadEncrypt ($string, $epad) {
374 $pad = base64_decode($epad);
376 for ($i = 0; $i < strlen ($string); $i++
) {
377 $encrypted .= chr (ord($string[$i]) ^
ord($pad[$i]));
380 return base64_encode($encrypted);
383 function OneTimePadDecrypt ($string, $epad) {
384 $pad = base64_decode($epad);
385 $encrypted = base64_decode ($string);
387 for ($i = 0; $i < strlen ($encrypted); $i++
) {
388 $decrypted .= chr (ord($encrypted[$i]) ^
ord($pad[$i]));
395 // Randomize the mt_rand() function. Toss this in strings or
396 // integers and it will seed the generator appropriately.
397 // With strings, it is better to get them long. Use md5() to
398 // lengthen smaller strings.
399 function sq_mt_seed($Val)
401 // if mt_getrandmax() does not return a 2^n - 1 number,
402 // this might not work well. This uses $Max as a bitmask.
403 $Max = mt_getrandmax();
407 if (function_exists('crc32'))
417 $HighBit = $Max ^
$Mask;
418 while ($Pos < strlen($Str))
422 $Val = (($Val & $Mask) << 1) +
1;
426 $Val = ($Val & $Mask) << 1;
439 mt_srand(($Val ^
mt_rand(0, $Max)) & $Max);
443 // This function initializes the random number generator fairly well.
444 // It also only initializes it once, so you don't accidentally get
445 // the same 'random' numbers twice in one session.
446 function sq_mt_randomize()
448 global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID;
455 sq_mt_seed((int)((double) microtime() * 1000000));
456 sq_mt_seed(md5($REMOTE_PORT . $REMOTE_ADDR . getmypid()));
459 if (function_exists('getrusage')) {
462 foreach ($dat as $k => $v)
466 sq_mt_seed(md5($Str));
470 sq_mt_seed(md5($UNIQUE_ID));
475 function OneTimePadCreate ($length=100) {
479 for ($i = 0; $i < $length; $i++
) {
480 $pad .= chr(mt_rand(0,255));
483 return base64_encode($pad);
486 // Check if we have a required PHP-version. Return TRUE if we do,
487 // or FALSE if we don't.
488 // To check for 4.0.1, use sqCheckPHPVersion(4,0,1)
489 // To check for 4.0b3, use sqCheckPHPVersion(4,0,-3)
490 // Does not handle betas like 4.0.1b1 or development versions
491 function sqCheckPHPVersion($major, $minor, $release) {
494 eregi('^([0-9]+)\\.([0-9]+)(.*)', $ver, $regs);
496 // Parse the version string
497 $vmajor = strval($regs[1]);
498 $vminor = strval($regs[2]);
501 $vrel = strval(substr($vrel, 1));
502 if($vrel[0] == 'b' ||
$vrel[0] == 'B')
503 $vrel = - strval(substr($vrel, 1));
504 if($vrel[0] == 'r' ||
$vrel[0] == 'R')
505 $vrel = - strval(substr($vrel, 2))/10;
507 // Compare major version
508 if($vmajor < $major) return false;
509 if($vmajor > $major) return true;
511 // Major is the same. Compare minor
512 if($vminor < $minor) return false;
513 if($vminor > $minor) return true;
515 // Major and minor is the same as the required one.
517 if($vrel >= 0 && $release >= 0) { // Neither are beta
518 if($vrel < $release) return false;
519 } else if($vrel >= 0 && $release < 0){ // This is not beta, required is beta
521 } else if($vrel < 0 && $release >= 0){ // This is beta, require not beta
523 } else { // Both are beta
524 if($vrel > $release) return false;
530 /* Returns a string showing the size of the message/attachment */
531 function show_readable_size($bytes)
536 if ($bytes / 1024 > 1)
545 settype($bytes, 'integer');
549 settype($bytes, 'integer');
551 return $bytes . '<small> ' . $type . '</small>';
554 /* Generates a random string from the caracter set you pass in
557 * 1 = add lowercase a-z to $chars
558 * 2 = add uppercase A-Z to $chars
559 * 4 = add numbers 0-9 to $chars
562 function GenerateRandomString($size, $chars, $flags = 0)
565 $chars .= 'abcdefghijklmnopqrstuvwxyz';
567 $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
569 $chars .= '0123456789';
571 if ($size < 1 ||
strlen($chars) < 1)
574 sq_mt_randomize(); // Initialize the random number generator
577 while (strlen($String) < $size) {
578 $String .= $chars[mt_rand(0, strlen($chars))];
584 function quoteIMAP($str)
586 return ereg_replace('(["\\])', '\\\\1', $str);