6 * Copyright (c) 1999-2003 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.
15 require_once(SM_PATH
. 'functions/global.php');
18 * SquirrelMail version number -- DO NOT CHANGE
21 $version = '1.4.0 [CVS-DEVEL]';
24 * SquirrelMail internal version number -- DO NOT CHANGE
25 * $sm_internal_version = array (release, major, minor)
27 global $SQM_INTERNAL_VERSION;
28 $SQM_INTERNAL_VERSION = array(1,4,0);
32 * Wraps text at $wrap characters
34 * Has a problem with special HTML characters, so call this before
35 * you do character translation.
37 * Specifically, ' comes up as 5 characters instead of 1.
38 * This should not add newlines to the end of lines.
40 function sqWordWrap(&$line, $wrap) {
41 ereg("^([\t >]*)([^\t >].*)?$", $line, $regs);
42 $beginning_spaces = $regs[1];
43 if (isset($regs[2])) {
44 $words = explode(' ', $regs[2]);
50 $line = $beginning_spaces;
52 while ($i < count($words)) {
53 /* Force one word to be on a line (minimum) */
55 $line_len = strlen($beginning_spaces) +
strlen($words[$i]) +
2;
56 if (isset($words[$i +
1]))
57 $line_len +
= strlen($words[$i +
1]);
60 /* Add more words (as long as they fit) */
61 while ($line_len < $wrap && $i < count($words)) {
62 $line .= ' ' . $words[$i];
64 if (isset($words[$i]))
65 $line_len +
= strlen($words[$i]) +
1;
70 /* Skip spaces if they are the first thing on a continued line */
71 while (!isset($words[$i]) && $i < count($words)) {
75 /* Go to the next line if we have more to process */
76 if ($i < count($words)) {
83 * Does the opposite of sqWordWrap()
85 function sqUnWordWrap(&$body) {
86 $lines = explode("\n", $body);
90 for ($i = 0; $i < $cnt; $i ++
) {
91 preg_match("/^([\t >]*)([^\t >].*)?$/", $lines[$i], $regs);
92 $CurrentSpaces = $regs[1];
93 if (isset($regs[2])) {
94 $CurrentRest = $regs[2];
100 $PreviousSpaces = $CurrentSpaces;
102 } else if (($PreviousSpaces == $CurrentSpaces) /* Do the beginnings match */
103 && (strlen($lines[$i - 1]) > 65) /* Over 65 characters long */
104 && strlen($CurrentRest)) { /* and there's a line to continue with */
105 $body .= ' ' . $CurrentRest;
107 $body .= "\n" . $lines[$i];
108 $PreviousSpaces = $CurrentSpaces;
115 * If $haystack is a full mailbox name and $needle is the mailbox
116 * separator character, returns the last part of the mailbox name.
118 function readShortMailboxName($haystack, $needle) {
123 $parts = explode($needle, $haystack);
124 $elem = array_pop($parts);
125 while ($elem == '' && count($parts)) {
126 $elem = array_pop($parts);
133 * Returns an array of email addresses.
134 * Be cautious of "user@host.com"
136 function parseAddrs($text) {
137 if (trim($text) == '')
139 $text = str_replace(' ', '', $text);
140 $text = ereg_replace('"[^"]*"', '', $text);
141 $text = ereg_replace('\\([^\\)]*\\)', '', $text);
142 $text = str_replace(',', ';', $text);
143 $array = explode(';', $text);
144 for ($i = 0; $i < count ($array); $i++
) {
145 $array[$i] = eregi_replace ('^.*[<]', '', $array[$i]);
146 $array[$i] = eregi_replace ('[>].*$', '', $array[$i]);
152 * Returns a line of comma separated email addresses from an array.
154 function getLineOfAddrs($array) {
155 if (is_array($array)) {
156 $to_line = implode(', ', $array);
157 $to_line = ereg_replace(', (, )+', ', ', $to_line);
158 $to_line = trim(ereg_replace('^, ', '', $to_line));
159 if( substr( $to_line, -1 ) == ',' )
160 $to_line = substr( $to_line, 0, -1 );
168 function php_self () {
169 if ( sqgetGlobalVar('REQUEST_URI', $req_uri, SQ_SERVER
) && !empty($req_uri) ) {
173 if ( sqgetGlobalVar('PHP_SELF', $php_self, SQ_SERVER
) && !empty($php_self) ) {
182 * This determines the location to forward to relative to your server.
183 * If this doesnt work correctly for you (although it should), you can
184 * remove all this code except the last two lines, and change the header()
185 * function to look something like this, customized to the location of
186 * SquirrelMail on your server:
188 * http://www.myhost.com/squirrelmail/src/login.php
190 function get_location () {
192 global $imap_server_type;
194 /* Get the path, handle virtual directories */
195 $path = substr(php_self(), 0, strrpos(php_self(), '/'));
197 /* Check if this is a HTTPS or regular HTTP request. */
201 * If you have 'SSLOptions +StdEnvVars' in your apache config
202 * OR if you have HTTPS=on in your HTTP_SERVER_VARS
203 * OR if you are on port 443
205 $getEnvVar = getenv('HTTPS');
206 if ((isset($getEnvVar) && !strcasecmp($getEnvVar, 'on')) ||
207 (sqgetGlobalVar('HTTPS', $https_on, SQ_SERVER
) && !strcasecmp($https_on, 'on')) ||
208 (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER
) && $server_port == 443)) {
212 /* Get the hostname from the Host header or server config. */
213 if ( !sqgetGlobalVar('HTTP_HOST', $host, SQ_SERVER
) ||
empty($host) ) {
214 if ( !sqgetGlobalVar('SERVER_NAME', $host, SQ_SERVER
) ||
empty($host) ) {
220 if (! strstr($host, ':')) {
221 if (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER
)) {
222 if (($server_port != 80 && $proto == 'http://') ||
223 ($server_port != 443 && $proto == 'https://')) {
224 $port = sprintf(':%d', $server_port);
229 /* this is a workaround for the weird macosx caching that
230 causes Apache to return 16080 as the port number, which causes
233 if ($imap_server_type == 'macosx' && $port == ':16080') {
237 /* Fallback is to omit the server name and use a relative */
238 /* URI, although this is not RFC 2616 compliant. */
239 return ($host ?
$proto . $host . $port . $path : $path);
244 * These functions are used to encrypt the passowrd before it is
245 * stored in a cookie.
247 function OneTimePadEncrypt ($string, $epad) {
248 $pad = base64_decode($epad);
250 for ($i = 0; $i < strlen ($string); $i++
) {
251 $encrypted .= chr (ord($string[$i]) ^
ord($pad[$i]));
254 return base64_encode($encrypted);
257 function OneTimePadDecrypt ($string, $epad) {
258 $pad = base64_decode($epad);
259 $encrypted = base64_decode ($string);
261 for ($i = 0; $i < strlen ($encrypted); $i++
) {
262 $decrypted .= chr (ord($encrypted[$i]) ^
ord($pad[$i]));
270 * Randomize the mt_rand() function. Toss this in strings or integers
271 * and it will seed the generator appropriately. With strings, it is
272 * better to get them long. Use md5() to lengthen smaller strings.
274 function sq_mt_seed($Val) {
275 /* if mt_getrandmax() does not return a 2^n - 1 number,
276 this might not work well. This uses $Max as a bitmask. */
277 $Max = mt_getrandmax();
279 if (! is_int($Val)) {
291 mt_srand(($Val ^
mt_rand(0, $Max)) & $Max);
296 * This function initializes the random number generator fairly well.
297 * It also only initializes it once, so you don't accidentally get
298 * the same 'random' numbers twice in one session.
300 function sq_mt_randomize() {
308 sqgetGlobalVar('REMOTE_PORT', $remote_port, SQ_SERVER
);
309 sqgetGlobalVar('REMOTE_ADDR', $remote_addr, SQ_SERVER
);
310 sq_mt_seed((int)((double) microtime() * 1000000));
311 sq_mt_seed(md5($remote_port . $remote_addr . getmypid()));
314 if (function_exists('getrusage')) {
315 /* Avoid warnings with Win32 */
317 if (isset($dat) && is_array($dat)) {
319 foreach ($dat as $k => $v)
323 sq_mt_seed(md5($Str));
327 if(sqgetGlobalVar('UNIQUE_ID', $unique_id, SQ_SERVER
)) {
328 sq_mt_seed(md5($unique_id));
334 function OneTimePadCreate ($length=100) {
338 for ($i = 0; $i < $length; $i++
) {
339 $pad .= chr(mt_rand(0,255));
342 return base64_encode($pad);
346 * Returns a string showing the size of the message/attachment.
348 function show_readable_size($bytes) {
352 if ($bytes / 1024 > 1) {
359 settype($bytes, 'integer');
362 settype($bytes, 'integer');
365 return $bytes . '<small> ' . $type . '</small>';
369 * Generates a random string from the caracter set you pass in
372 * 1 = add lowercase a-z to $chars
373 * 2 = add uppercase A-Z to $chars
374 * 4 = add numbers 0-9 to $chars
377 function GenerateRandomString($size, $chars, $flags = 0) {
379 $chars .= 'abcdefghijklmnopqrstuvwxyz';
382 $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
385 $chars .= '0123456789';
388 if (($size < 1) ||
(strlen($chars) < 1)) {
392 sq_mt_randomize(); /* Initialize the random number generator */
395 $j = strlen( $chars ) - 1;
396 while (strlen($String) < $size) {
397 $String .= $chars{mt_rand(0, $j)};
403 function quoteIMAP($str) {
404 return ereg_replace('(["\\])', '\\\\1', $str);
408 * Trims every element in the array
410 function TrimArray(&$array) {
411 foreach ($array as $k => $v) {
414 foreach ($
$k as $k2 => $v2) {
415 $
$k[$k2] = substr($v2, 1);
421 /* Re-assign back to array. */
427 * Removes slashes from every element in the array
429 function RemoveSlashes(&$array) {
430 foreach ($array as $k => $v) {
433 foreach ($
$k as $k2 => $v2) {
434 $newArray[stripslashes($k2)] = stripslashes($v2);
438 $
$k = stripslashes($v);
441 /* Re-assign back to the array. */
446 $PHP_SELF = php_self();