8e1042294b302687cfdc4a995ee4124e3520d991
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.3.2 [CVS-DEVEL]';
22 * Wraps text at $wrap characters
24 * Has a problem with special HTML characters, so call this before
25 * you do character translation.
27 * Specifically, ' comes up as 5 characters instead of 1.
28 * This should not add newlines to the end of lines.
30 function sqWordWrap(&$line, $wrap) {
31 ereg("^([\t >]*)([^\t >].*)?$", $line, $regs);
32 $beginning_spaces = $regs[1];
33 if (isset($regs[2])) {
34 $words = explode(' ', $regs[2]);
40 $line = $beginning_spaces;
42 while ($i < count($words)) {
43 /* Force one word to be on a line (minimum) */
45 $line_len = strlen($beginning_spaces) +
strlen($words[$i]) +
2;
46 if (isset($words[$i +
1]))
47 $line_len +
= strlen($words[$i +
1]);
50 /* Add more words (as long as they fit) */
51 while ($line_len < $wrap && $i < count($words)) {
52 $line .= ' ' . $words[$i];
54 if (isset($words[$i]))
55 $line_len +
= strlen($words[$i]) +
1;
60 /* Skip spaces if they are the first thing on a continued line */
61 while (!isset($words[$i]) && $i < count($words)) {
65 /* Go to the next line if we have more to process */
66 if ($i < count($words)) {
73 * Does the opposite of sqWordWrap()
75 function sqUnWordWrap(&$body) {
76 $lines = explode("\n", $body);
80 for ($i = 0; $i < $cnt; $i ++
) {
81 preg_match("/^([\t >]*)([^\t >].*)?$/", $lines[$i], $regs);
82 $CurrentSpaces = $regs[1];
83 if (isset($regs[2])) {
84 $CurrentRest = $regs[2];
90 $PreviousSpaces = $CurrentSpaces;
92 } else if (($PreviousSpaces == $CurrentSpaces) /* Do the beginnings match */
93 && (strlen($lines[$i - 1]) > 65) /* Over 65 characters long */
94 && strlen($CurrentRest)) { /* and there's a line to continue with */
95 $body .= ' ' . $CurrentRest;
97 $body .= "\n" . $lines[$i];
98 $PreviousSpaces = $CurrentSpaces;
105 * If $haystack is a full mailbox name and $needle is the mailbox
106 * separator character, returns the last part of the mailbox name.
108 function readShortMailboxName($haystack, $needle) {
113 $parts = explode($needle, $haystack);
114 $elem = array_pop($parts);
115 while ($elem == '' && count($parts)) {
116 $elem = array_pop($parts);
123 * Returns an array of email addresses.
124 * Be cautious of "user@host.com"
126 function parseAddrs($text) {
127 if (trim($text) == '')
129 $text = str_replace(' ', '', $text);
130 $text = ereg_replace('"[^"]*"', '', $text);
131 $text = ereg_replace('\\([^\\)]*\\)', '', $text);
132 $text = str_replace(',', ';', $text);
133 $array = explode(';', $text);
134 for ($i = 0; $i < count ($array); $i++
) {
135 $array[$i] = eregi_replace ('^.*[<]', '', $array[$i]);
136 $array[$i] = eregi_replace ('[>].*$', '', $array[$i]);
142 * Returns a line of comma separated email addresses from an array.
144 function getLineOfAddrs($array) {
145 if (is_array($array)) {
146 $to_line = implode(', ', $array);
147 $to_line = ereg_replace(', (, )+', ', ', $to_line);
148 $to_line = trim(ereg_replace('^, ', '', $to_line));
149 if( substr( $to_line, -1 ) == ',' )
150 $to_line = substr( $to_line, 0, -1 );
158 function php_self () {
159 global $PHP_SELF, $HTTP_SERVER_VARS;
161 if (isset($HTTP_SERVER_VARS['REQUEST_URI']) && !empty($HTTP_SERVER_VARS['REQUEST_URI']) ) {
162 return $HTTP_SERVER_VARS['REQUEST_URI'];
165 if (isset($PHP_SELF) && !empty($PHP_SELF)) {
167 } else if (isset($HTTP_SERVER_VARS['PHP_SELF']) &&
168 !empty($HTTP_SERVER_VARS['PHP_SELF'])) {
169 return $HTTP_SERVER_VARS['PHP_SELF'];
177 * This determines the location to forward to relative to your server.
178 * If this doesnt work correctly for you (although it should), you can
179 * remove all this code except the last two lines, and change the header()
180 * function to look something like this, customized to the location of
181 * SquirrelMail on your server:
183 * http://www.myhost.com/squirrelmail/src/login.php
185 function get_location () {
187 global $PHP_SELF, $SERVER_NAME, $HTTP_HOST, $SERVER_PORT,
188 $HTTP_SERVER_VARS, $imap_server_type;
190 /* Get the path, handle virtual directories */
191 $path = substr(php_self(), 0, strrpos(php_self(), '/'));
193 /* Check if this is a HTTPS or regular HTTP request. */
197 * If you have 'SSLOptions +StdEnvVars' in your apache config
198 * OR if you have HTTPS in your HTTP_SERVER_VARS
199 * OR if you are on port 443
201 $getEnvVar = getenv('HTTPS');
202 if ((isset($getEnvVar) && !strcasecmp($getEnvVar, 'on')) ||
203 (isset($HTTP_SERVER_VARS['HTTPS'])) ||
204 (isset($HTTP_SERVER_VARS['SERVER_PORT']) &&
205 $HTTP_SERVER_VARS['SERVER_PORT'] == 443)) {
209 /* Get the hostname from the Host header or server config. */
211 if (isset($HTTP_HOST) && !empty($HTTP_HOST)) {
213 } else if (isset($SERVER_NAME) && !empty($SERVER_NAME)) {
214 $host = $SERVER_NAME;
215 } else if (isset($HTTP_SERVER_VARS['SERVER_NAME']) &&
216 !empty($HTTP_SERVER_VARS['SERVER_NAME'])) {
217 $host = $HTTP_SERVER_VARS['SERVER_NAME'];
222 if (! strstr($host, ':')) {
223 if (isset($SERVER_PORT)) {
224 if (($SERVER_PORT != 80 && $proto == 'http://')
225 ||
($SERVER_PORT != 443 && $proto == 'https://')) {
226 $port = sprintf(':%d', $SERVER_PORT);
231 /* this is a workaround for the weird macosx caching that
232 causes Apache to return 16080 as the port number, which causes
235 if ($imap_server_type == 'macosx' && $port == ':16080') {
239 /* Fallback is to omit the server name and use a relative */
240 /* URI, although this is not RFC 2616 compliant. */
241 return ($host ?
$proto . $host . $port . $path : $path);
246 * These functions are used to encrypt the passowrd before it is
247 * stored in a cookie.
249 function OneTimePadEncrypt ($string, $epad) {
250 $pad = base64_decode($epad);
252 for ($i = 0; $i < strlen ($string); $i++
) {
253 $encrypted .= chr (ord($string[$i]) ^
ord($pad[$i]));
256 return base64_encode($encrypted);
259 function OneTimePadDecrypt ($string, $epad) {
260 $pad = base64_decode($epad);
261 $encrypted = base64_decode ($string);
263 for ($i = 0; $i < strlen ($encrypted); $i++
) {
264 $decrypted .= chr (ord($encrypted[$i]) ^
ord($pad[$i]));
272 * Randomize the mt_rand() function. Toss this in strings or integers
273 * and it will seed the generator appropriately. With strings, it is
274 * better to get them long. Use md5() to lengthen smaller strings.
276 function sq_mt_seed($Val) {
277 /* if mt_getrandmax() does not return a 2^n - 1 number,
278 this might not work well. This uses $Max as a bitmask. */
279 $Max = mt_getrandmax();
281 if (! is_int($Val)) {
282 if (function_exists('crc32')) {
289 $HighBit = $Max ^
$Mask;
290 while ($Pos < strlen($Str)) {
291 if ($Val & $HighBit) {
292 $Val = (($Val & $Mask) << 1) +
1;
294 $Val = ($Val & $Mask) << 1;
310 mt_srand(($Val ^
mt_rand(0, $Max)) & $Max);
315 * This function initializes the random number generator fairly well.
316 * It also only initializes it once, so you don't accidentally get
317 * the same 'random' numbers twice in one session.
319 function sq_mt_randomize() {
320 global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID;
328 sq_mt_seed((int)((double) microtime() * 1000000));
329 sq_mt_seed(md5($REMOTE_PORT . $REMOTE_ADDR . getmypid()));
332 if (function_exists('getrusage')) {
333 /* Avoid warnings with Win32 */
335 if (isset($dat) && is_array($dat)) {
337 foreach ($dat as $k => $v)
341 sq_mt_seed(md5($Str));
345 /* Apache-specific */
346 sq_mt_seed(md5($UNIQUE_ID));
351 function OneTimePadCreate ($length=100) {
355 for ($i = 0; $i < $length; $i++
) {
356 $pad .= chr(mt_rand(0,255));
359 return base64_encode($pad);
363 * Check if we have a required PHP-version. Return TRUE if we do,
364 * or FALSE if we don't.
366 * To check for 4.0.1, use sqCheckPHPVersion(4,0,1)
367 * To check for 4.0b3, use sqCheckPHPVersion(4,0,-3)
369 * Does not handle betas like 4.0.1b1 or development versions
371 function sqCheckPHPVersion($major, $minor, $release) {
374 eregi('^([0-9]+)\\.([0-9]+)(.*)', $ver, $regs);
376 /* Parse the version string. */
377 $vmajor = strval($regs[1]);
378 $vminor = strval($regs[2]);
380 if($vrel[0] == '.') {
381 $vrel = strval(substr($vrel, 1));
383 if($vrel[0] == 'b' ||
$vrel[0] == 'B') {
384 $vrel = - strval(substr($vrel, 1));
386 if($vrel[0] == 'r' ||
$vrel[0] == 'R') {
387 $vrel = - strval(substr($vrel, 2))/10;
390 /* Compare major version. */
391 if ($vmajor < $major) { return false; }
392 if ($vmajor > $major) { return true; }
394 /* Major is the same. Compare minor. */
395 if ($vminor < $minor) { return false; }
396 if ($vminor > $minor) { return true; }
398 /* Major and minor is the same as the required one. Compare release */
399 if ($vrel >= 0 && $release >= 0) { /* Neither are beta */
400 if($vrel < $release) return false;
401 } else if($vrel >= 0 && $release < 0) { /* This is not beta, required is beta */
403 } else if($vrel < 0 && $release >= 0){ /* This is beta, require not beta */
405 } else { /* Both are beta */
406 if($vrel > $release) return false;
413 * Returns a string showing the size of the message/attachment.
415 function show_readable_size($bytes) {
419 if ($bytes / 1024 > 1) {
426 settype($bytes, 'integer');
429 settype($bytes, 'integer');
432 return $bytes . '<small> ' . $type . '</small>';
436 * Generates a random string from the caracter set you pass in
439 * 1 = add lowercase a-z to $chars
440 * 2 = add uppercase A-Z to $chars
441 * 4 = add numbers 0-9 to $chars
444 function GenerateRandomString($size, $chars, $flags = 0) {
446 $chars .= 'abcdefghijklmnopqrstuvwxyz';
449 $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
452 $chars .= '0123456789';
455 if (($size < 1) ||
(strlen($chars) < 1)) {
459 sq_mt_randomize(); /* Initialize the random number generator */
462 $j = strlen( $chars ) - 1;
463 while (strlen($String) < $size) {
464 $String .= $chars{mt_rand(0, $j)};
470 function quoteIMAP($str) {
471 return ereg_replace('(["\\])', '\\\\1', $str);
475 * Trims every element in the array
477 function TrimArray(&$array) {
478 foreach ($array as $k => $v) {
481 foreach ($
$k as $k2 => $v2) {
482 $
$k[$k2] = substr($v2, 1);
488 /* Re-assign back to array. */
494 * Removes slashes from every element in the array
496 function RemoveSlashes(&$array) {
497 foreach ($array as $k => $v) {
500 foreach ($
$k as $k2 => $v2) {
501 $newArray[stripslashes($k2)] = stripslashes($v2);
505 $
$k = stripslashes($v);
508 /* Re-assign back to the array. */
513 $PHP_SELF = php_self();