7 // Remove all slashes for form values
8 if (get_magic_quotes_gpc())
10 global $REQUEST_METHOD;
11 if ($REQUEST_METHOD == "POST")
13 global $HTTP_POST_VARS;
14 RemoveSlashes($HTTP_POST_VARS);
16 elseif ($REQUEST_METHOD == "GET")
18 global $HTTP_GET_VARS;
19 RemoveSlashes($HTTP_GET_VARS);
24 function RemoveSlashes($array)
26 foreach ($array as $k => $v)
31 foreach ($
$k as $k2 => $v2)
33 $newArray[stripslashes($k2)] = stripslashes($v2);
39 $
$k = stripslashes($v);
45 //*************************************************************************
46 // Count the number of occurances of $needle are in $haystack.
47 // $needle can be a character or string, and need not occur in $haystack
48 //*************************************************************************
49 function countCharInString($haystack, $needle) {
50 if ($needle == '') return 0;
51 return count(explode($needle, $haystack));
54 //*************************************************************************
55 // Read from the back of $haystack until $needle is found, or the begining
56 // of the $haystack is reached. $needle is a single character
57 //*************************************************************************
58 function readShortMailboxName($haystack, $needle) {
59 if ($needle == '') return $haystack;
60 $parts = explode($needle, $haystack);
61 $elem = array_pop($parts);
62 while ($elem == '' && count($parts))
64 $elem = array_pop($parts);
69 //*************************************************************************
70 // Read from the back of $haystack until $needle is found, or the begining
71 // of the $haystack is reached. $needle is a single character
72 //*************************************************************************
73 function readMailboxParent($haystack, $needle) {
74 if ($needle == '') return '';
75 $parts = explode($needle, $haystack);
76 $elem = array_pop($parts);
77 while ($elem == '' && count($parts))
79 $elem = array_pop($parts);
81 return join($needle, $parts);
84 // Searches for the next position in a string minus white space
85 function next_pos_minus_white ($haystack, $pos) {
86 while (substr($haystack, $pos, 1) == ' ' ||
87 substr($haystack, $pos, 1) == "\t" ||
88 substr($haystack, $pos, 1) == "\n" ||
89 substr($haystack, $pos, 1) == "\r") {
90 if ($pos >= strlen($haystack))
97 // Wraps text at $wrap characters
98 // Has a problem with special HTML characters, so call this before
99 // you do character translation.
100 // Specifically, ' comes up as 5 characters instead of 1.
101 // This should not add newlines to the end of lines.
102 function sqWordWrap(&$line, $wrap) {
103 ereg("^([\t >]*)([^\t >].*)?$", $line, $regs);
104 $beginning_spaces = $regs[1];
105 if (isset($regs[2])) {
106 $words = explode(' ', $regs[2]);
112 $line = $beginning_spaces;
114 while ($i < count($words)) {
115 // Force one word to be on a line (minimum)
117 $line_len = strlen($beginning_spaces) +
strlen($words[$i]) +
2;
118 if (isset($words[$i +
1]))
119 $line_len +
= strlen($words[$i +
1]);
122 // Add more words (as long as they fit)
123 while ($line_len < $wrap && $i < count($words)) {
124 $line .= ' ' . $words[$i];
126 if (isset($words[$i]))
127 $line_len +
= strlen($words[$i]) +
1;
132 // Skip spaces if they are the first thing on a continued line
133 while (!isset($words[$i]) && $i < count($words)) {
137 // Go to the next line if we have more to process
138 if ($i < count($words)) {
139 $line .= "\n" . $beginning_spaces;
145 // Does the opposite of sqWordWrap()
146 function sqUnWordWrap(&$body)
148 $lines = explode("\n", $body);
150 $PreviousSpaces = "";
151 for ($i = 0; $i < count($lines); $i ++
)
153 ereg("^([\t >]*)([^\t >].*)?$", $lines[$i], $regs);
154 $CurrentSpaces = $regs[1];
156 $CurrentRest = $regs[2];
159 $PreviousSpaces = $CurrentSpaces;
162 else if ($PreviousSpaces == $CurrentSpaces && // Do the beginnings match
163 strlen($lines[$i - 1]) > 65 && // Over 65 characters long
164 strlen($CurrentRest)) // and there's a line to continue with
166 $body .= ' ' . $CurrentRest;
170 $body .= "\n" . $lines[$i];
171 $PreviousSpaces = $CurrentSpaces;
178 /** Returns an array of email addresses **/
179 /* Be cautious of "user@host.com" */
180 function parseAddrs($text) {
181 if (trim($text) == "")
183 $text = str_replace(' ', '', $text);
184 $text = ereg_replace('"[^"]*"', '', $text);
185 $text = ereg_replace('\\([^\\)]*\\)', '', $text);
186 $text = str_replace(',', ';', $text);
187 $array = explode(';', $text);
188 for ($i = 0; $i < count ($array); $i++
) {
189 $array[$i] = eregi_replace ("^.*[<]", '', $array[$i]);
190 $array[$i] = eregi_replace ("[>].*$", '', $array[$i]);
195 /** Returns a line of comma separated email addresses from an array **/
196 function getLineOfAddrs($array) {
197 if (is_array($array)) {
198 $to_line = implode(', ', $array);
199 $to_line = trim(ereg_replace(',,+', ',', $to_line));
206 function translateText(&$body, $wrap_at, $charset) {
207 global $where, $what; // from searching
208 global $url_parser_php;
210 if (!isset($url_parser_php)) {
211 include '../functions/url_parser.php';
214 $body_ary = explode("\n", $body);
216 for ($i=0; $i < count($body_ary); $i++
) {
217 $line = $body_ary[$i];
218 if (strlen($line) - 2 >= $wrap_at) {
219 sqWordWrap($line, $wrap_at);
221 $line = charset_decode($charset, $line);
222 $line = str_replace("\t", ' ', $line);
230 if ($line[$pos] == ' ')
234 else if (strpos($line, '>', $pos) === $pos)
246 $line = '<FONT COLOR="FF0000">'.$line.'</FONT>';
248 $line = '<FONT COLOR="800000">'.$line.'</FONT>';
250 $body_ary[$i] = $line;
252 $body = '<pre>' . implode("\n", $body_ary) . '</pre>';
255 /* SquirrelMail version number -- DO NOT CHANGE */
256 $version = '1.0.2 [cvs]';
259 function find_mailbox_name ($mailbox) {
260 if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs))
262 ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs);
267 function replace_spaces ($string) {
268 return str_replace(' ', ' ', $string);
271 function replace_escaped_spaces ($string) {
272 return str_replace(' ', ' ', $string);
275 function get_location () {
276 # This determines the location to forward to relative
277 # to your server. If this doesnt work correctly for
278 # you (although it should), you can remove all this
279 # code except the last two lines, and change the header()
280 # function to look something like this, customized to
281 # the location of SquirrelMail on your server:
283 # http://www.myhost.com/squirrelmail/src/login.php
285 global $PHP_SELF, $SERVER_NAME, $HTTPS, $HTTP_HOST, $SERVER_PORT;
288 $path = substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'));
290 // Check if this is a HTTPS or regular HTTP request
292 if(isset($HTTPS) && !strcasecmp($HTTPS, 'on') ) {
296 // Get the hostname from the Host header or server config.
298 if (isset($HTTP_HOST) && !empty($HTTP_HOST))
302 else if (isset($SERVER_NAME) && !empty($SERVER_NAME))
304 $host = $SERVER_NAME;
308 if (! strstr($host, ':'))
310 if (isset($SERVER_PORT)) {
311 if (($SERVER_PORT != 80 && $proto == 'http://')
312 ||
($SERVER_PORT != 443 && $proto == 'https://')) {
313 $port = sprintf(':%d', $SERVER_PORT);
319 return $proto . $host . $port . $path;
321 // Fallback is to omit the server name and use a relative URI,
322 // although this is not RFC 2616 compliant.
327 // These functions are used to encrypt the passowrd before it is
328 // stored in a cookie.
329 function OneTimePadEncrypt ($string, $epad) {
330 $pad = base64_decode($epad);
332 for ($i = 0; $i < strlen ($string); $i++
) {
333 $encrypted .= chr (ord($string[$i]) ^
ord($pad[$i]));
336 return base64_encode($encrypted);
339 function OneTimePadDecrypt ($string, $epad) {
340 $pad = base64_decode($epad);
341 $encrypted = base64_decode ($string);
343 for ($i = 0; $i < strlen ($encrypted); $i++
) {
344 $decrypted .= chr (ord($encrypted[$i]) ^
ord($pad[$i]));
351 // Randomize the mt_rand() function. Toss this in strings or
352 // integers and it will seed the generator appropriately.
353 // With strings, it is better to get them long. Use md5() to
354 // lengthen smaller strings.
355 function sq_mt_seed($Val)
357 // if mt_getrandmax() does not return a 2^n - 1 number,
358 // this might not work well. This uses $Max as a bitmask.
359 $Max = mt_getrandmax();
363 if (function_exists('crc32'))
373 $HighBit = $Max ^
$Mask;
374 while ($Pos < strlen($Str))
378 $Val = (($Val & $Mask) << 1) +
1;
382 $Val = ($Val & $Mask) << 1;
395 mt_srand(($Val ^
mt_rand(0, $Max)) & $Max);
399 // This function initializes the random number generator fairly well.
400 // It also only initializes it once, so you don't accidentally get
401 // the same 'random' numbers twice in one session.
402 function sq_mt_randomize()
404 global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID;
411 sq_mt_seed((int)((double) microtime() * 1000000));
412 sq_mt_seed(md5($REMOTE_PORT . $REMOTE_ADDR . getmypid()));
415 if (function_exists('getrusage')) {
418 foreach ($dat as $k => $v)
422 sq_mt_seed(md5($Str));
426 sq_mt_seed(md5($UNIQUE_ID));
431 function OneTimePadCreate ($length=100) {
435 for ($i = 0; $i < $length; $i++
) {
436 $pad .= chr(mt_rand(0,255));
439 return base64_encode($pad);
442 // Check if we have a required PHP-version. Return TRUE if we do,
443 // or FALSE if we don't.
444 // To check for 4.0.1, use sqCheckPHPVersion(4,0,1)
445 // To check for 4.0b3, use sqCheckPHPVersion(4,0,-3)
446 // Does not handle betas like 4.0.1b1 or development versions
447 function sqCheckPHPVersion($major, $minor, $release) {
450 eregi('^([0-9]+)\\.([0-9]+)(.*)', $ver, $regs);
452 // Parse the version string
453 $vmajor = strval($regs[1]);
454 $vminor = strval($regs[2]);
457 $vrel = strval(substr($vrel, 1));
458 if($vrel[0] == 'b' ||
$vrel[0] == 'B')
459 $vrel = - strval(substr($vrel, 1));
460 if($vrel[0] == 'r' ||
$vrel[0] == 'R')
461 $vrel = - strval(substr($vrel, 2))/10;
463 // Compare major version
464 if($vmajor < $major) return false;
465 if($vmajor > $major) return true;
467 // Major is the same. Compare minor
468 if($vminor < $minor) return false;
469 if($vminor > $minor) return true;
471 // Major and minor is the same as the required one.
473 if($vrel >= 0 && $release >= 0) { // Neither are beta
474 if($vrel < $release) return false;
475 } else if($vrel >= 0 && $release < 0){ // This is not beta, required is beta
477 } else if($vrel < 0 && $release >= 0){ // This is beta, require not beta
479 } else { // Both are beta
480 if($vrel > $release) return false;
486 /* Returns a string showing the size of the message/attachment */
487 function show_readable_size($bytes)
492 if ($bytes / 1024 > 1)
501 settype($bytes, 'integer');
505 settype($bytes, 'integer');
507 return $bytes . '<small> ' . $type . '</small>';
510 /* Generates a random string from the caracter set you pass in
513 * 1 = add lowercase a-z to $chars
514 * 2 = add uppercase A-Z to $chars
515 * 4 = add numbers 0-9 to $chars
518 function GenerateRandomString($size, $chars, $flags = 0)
521 $chars .= 'abcdefghijklmnopqrstuvwxyz';
523 $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
525 $chars .= '0123456789';
527 if ($size < 1 ||
strlen($chars) < 1)
530 sq_mt_randomize(); // Initialize the random number generator
533 while (strlen($String) < $size) {
534 $String .= $chars[mt_rand(0, strlen($chars))];