6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This code provides various string manipulation functions that are
10 * used by the rest of the Squirrelmail code.
16 * SquirrelMail version number -- DO NOT CHANGE
19 $version = '1.2.6 [cvs]';
22 * If $haystack is a full mailbox name and $needle is the mailbox
23 * separator character, returns the last part of the mailbox name.
25 function readShortMailboxName($haystack, $needle) {
30 $parts = explode($needle, $haystack);
31 $elem = array_pop($parts);
32 while ($elem == '' && count($parts)) {
33 $elem = array_pop($parts);
40 * Returns an array of email addresses.
41 * Be cautious of "user@host.com"
43 function parseAddrs($text) {
44 if (trim($text) == '')
46 $text = str_replace(' ', '', $text);
47 $text = ereg_replace('"[^"]*"', '', $text);
48 $text = ereg_replace('\\([^\\)]*\\)', '', $text);
49 $text = str_replace(',', ';', $text);
50 $array = explode(';', $text);
51 for ($i = 0; $i < count ($array); $i++
) {
52 $array[$i] = eregi_replace ('^.*[<]', '', $array[$i]);
53 $array[$i] = eregi_replace ('[>].*$', '', $array[$i]);
59 * Returns a line of comma separated email addresses from an array.
61 function getLineOfAddrs($array) {
62 if (is_array($array)) {
63 $to_line = implode(', ', $array);
64 $to_line = ereg_replace(', (, )+', ', ', $to_line);
65 $to_line = trim(ereg_replace('^, ', '', $to_line));
66 if( substr( $to_line, -1 ) == ',' )
67 $to_line = substr( $to_line, 0, -1 );
75 function php_self () {
76 global $PHP_SELF, $HTTP_SERVER_VARS;
78 if (isset($HTTP_SERVER_VARS['REQUEST_URI']) && !empty($HTTP_SERVER_VARS['REQUEST_URI']) ) {
79 return $HTTP_SERVER_VARS['REQUEST_URI'];
82 if (isset($PHP_SELF) && !empty($PHP_SELF)) {
84 } else if (isset($HTTP_SERVER_VARS['PHP_SELF']) &&
85 !empty($HTTP_SERVER_VARS['PHP_SELF'])) {
86 return $HTTP_SERVER_VARS['PHP_SELF'];
94 * This determines the location to forward to relative to your server.
95 * If this doesnt work correctly for you (although it should), you can
96 * remove all this code except the last two lines, and change the header()
97 * function to look something like this, customized to the location of
98 * SquirrelMail on your server:
100 * http://www.myhost.com/squirrelmail/src/login.php
102 function get_location () {
104 global $PHP_SELF, $SERVER_NAME, $HTTP_HOST, $SERVER_PORT,
107 /* Get the path, handle virtual directories */
108 $path = substr(php_self(), 0, strrpos(php_self(), '/'));
110 /* Check if this is a HTTPS or regular HTTP request. */
114 * If you have 'SSLOptions +StdEnvVars' in your apache config
115 * OR if you have HTTPS in your HTTP_SERVER_VARS
116 * OR if you are on port 443
118 $getEnvVar = getenv('HTTPS');
119 if ((isset($getEnvVar) && !strcasecmp($getEnvVar, 'on')) ||
120 (isset($HTTP_SERVER_VARS['HTTPS'])) ||
121 (isset($HTTP_SERVER_VARS['SERVER_PORT']) &&
122 $HTTP_SERVER_VARS['SERVER_PORT'] == 443)) {
126 /* Get the hostname from the Host header or server config. */
128 if (isset($HTTP_HOST) && !empty($HTTP_HOST)) {
130 } else if (isset($SERVER_NAME) && !empty($SERVER_NAME)) {
131 $host = $SERVER_NAME;
132 } else if (isset($HTTP_SERVER_VARS['SERVER_NAME']) &&
133 !empty($HTTP_SERVER_VARS['SERVER_NAME'])) {
134 $host = $HTTP_SERVER_VARS['SERVER_NAME'];
139 if (! strstr($host, ':')) {
140 if (isset($SERVER_PORT)) {
141 if (($SERVER_PORT != 80 && $proto == 'http://')
142 ||
($SERVER_PORT != 443 && $proto == 'https://')) {
143 $port = sprintf(':%d', $SERVER_PORT);
148 /* Fallback is to omit the server name and use a relative */
149 /* URI, although this is not RFC 2616 compliant. */
150 return ($host ?
$proto . $host . $port . $path : $path);
155 * These functions are used to encrypt the passowrd before it is
156 * stored in a cookie.
158 function OneTimePadEncrypt ($string, $epad) {
159 $pad = base64_decode($epad);
161 for ($i = 0; $i < strlen ($string); $i++
) {
162 $encrypted .= chr (ord($string[$i]) ^
ord($pad[$i]));
165 return base64_encode($encrypted);
168 function OneTimePadDecrypt ($string, $epad) {
169 $pad = base64_decode($epad);
170 $encrypted = base64_decode ($string);
172 for ($i = 0; $i < strlen ($encrypted); $i++
) {
173 $decrypted .= chr (ord($encrypted[$i]) ^
ord($pad[$i]));
181 * Randomize the mt_rand() function. Toss this in strings or integers
182 * and it will seed the generator appropriately. With strings, it is
183 * better to get them long. Use md5() to lengthen smaller strings.
185 function sq_mt_seed($Val) {
186 /* if mt_getrandmax() does not return a 2^n - 1 number,
187 this might not work well. This uses $Max as a bitmask. */
188 $Max = mt_getrandmax();
190 if (! is_int($Val)) {
191 if (function_exists('crc32')) {
198 $HighBit = $Max ^
$Mask;
199 while ($Pos < strlen($Str)) {
200 if ($Val & $HighBit) {
201 $Val = (($Val & $Mask) << 1) +
1;
203 $Val = ($Val & $Mask) << 1;
219 mt_srand(($Val ^
mt_rand(0, $Max)) & $Max);
224 * This function initializes the random number generator fairly well.
225 * It also only initializes it once, so you don't accidentally get
226 * the same 'random' numbers twice in one session.
228 function sq_mt_randomize() {
229 global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID;
237 sq_mt_seed((int)((double) microtime() * 1000000));
238 sq_mt_seed(md5($REMOTE_PORT . $REMOTE_ADDR . getmypid()));
241 if (function_exists('getrusage')) {
242 /* Avoid warnings with Win32 */
244 if (isset($dat) && is_array($dat)) {
246 foreach ($dat as $k => $v)
250 sq_mt_seed(md5($Str));
254 /* Apache-specific */
255 sq_mt_seed(md5($UNIQUE_ID));
260 function OneTimePadCreate ($length=100) {
264 for ($i = 0; $i < $length; $i++
) {
265 $pad .= chr(mt_rand(0,255));
268 return base64_encode($pad);
272 * Check if we have a required PHP-version. Return TRUE if we do,
273 * or FALSE if we don't.
275 * To check for 4.0.1, use sqCheckPHPVersion(4,0,1)
276 * To check for 4.0b3, use sqCheckPHPVersion(4,0,-3)
278 * Does not handle betas like 4.0.1b1 or development versions
280 function sqCheckPHPVersion($major, $minor, $release) {
283 eregi('^([0-9]+)\\.([0-9]+)(.*)', $ver, $regs);
285 /* Parse the version string. */
286 $vmajor = strval($regs[1]);
287 $vminor = strval($regs[2]);
289 if($vrel[0] == '.') {
290 $vrel = strval(substr($vrel, 1));
292 if($vrel[0] == 'b' ||
$vrel[0] == 'B') {
293 $vrel = - strval(substr($vrel, 1));
295 if($vrel[0] == 'r' ||
$vrel[0] == 'R') {
296 $vrel = - strval(substr($vrel, 2))/10;
299 /* Compare major version. */
300 if ($vmajor < $major) { return false; }
301 if ($vmajor > $major) { return true; }
303 /* Major is the same. Compare minor. */
304 if ($vminor < $minor) { return false; }
305 if ($vminor > $minor) { return true; }
307 /* Major and minor is the same as the required one. Compare release */
308 if ($vrel >= 0 && $release >= 0) { /* Neither are beta */
309 if($vrel < $release) return false;
310 } else if($vrel >= 0 && $release < 0) { /* This is not beta, required is beta */
312 } else if($vrel < 0 && $release >= 0){ /* This is beta, require not beta */
314 } else { /* Both are beta */
315 if($vrel > $release) return false;
322 * Returns a string showing the size of the message/attachment.
324 function show_readable_size($bytes) {
328 if ($bytes / 1024 > 1) {
335 settype($bytes, 'integer');
338 settype($bytes, 'integer');
341 return $bytes . '<small> ' . $type . '</small>';
345 * Generates a random string from the caracter set you pass in
348 * 1 = add lowercase a-z to $chars
349 * 2 = add uppercase A-Z to $chars
350 * 4 = add numbers 0-9 to $chars
353 function GenerateRandomString($size, $chars, $flags = 0) {
355 $chars .= 'abcdefghijklmnopqrstuvwxyz';
358 $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
361 $chars .= '0123456789';
364 if (($size < 1) ||
(strlen($chars) < 1)) {
368 sq_mt_randomize(); /* Initialize the random number generator */
371 $j = strlen( $chars ) - 1;
372 while (strlen($String) < $size) {
373 $String .= $chars{mt_rand(0, $j)};
379 function quoteIMAP($str) {
380 return ereg_replace('(["\\])', '\\\\1', $str);
384 * Trims every element in the array
386 function TrimArray(&$array) {
387 foreach ($array as $k => $v) {
390 foreach ($
$k as $k2 => $v2) {
391 $
$k[$k2] = substr($v2, 1);
397 /* Re-assign back to array. */
403 * Removes slashes from every element in the array
405 function RemoveSlashes(&$array) {
406 foreach ($array as $k => $v) {
409 foreach ($
$k as $k2 => $v2) {
410 $newArray[stripslashes($k2)] = stripslashes($v2);
414 $
$k = stripslashes($v);
417 /* Re-assign back to the array. */
422 $PHP_SELF = php_self();