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.
17 * SquirrelMail version number -- DO NOT CHANGE
20 $version = '1.5.0 [CVS]';
23 * SquirrelMail internal version number -- DO NOT CHANGE
24 * $sm_internal_version = array (release, major, minor)
26 global $SQM_INTERNAL_VERSION;
27 $SQM_INTERNAL_VERSION = array(1,5,0);
31 * There can be a circular issue with includes, where the $version string is
32 * referenced by the include of global.php, etc. before it's defined.
33 * For that reason, bring in global.php AFTER we define the version strings.
35 require_once(SM_PATH
. 'functions/global.php');
38 * Wraps text at $wrap characters
40 * Has a problem with special HTML characters, so call this before
41 * you do character translation.
43 * Specifically, ' comes up as 5 characters instead of 1.
44 * This should not add newlines to the end of lines.
46 function sqWordWrap(&$line, $wrap) {
47 global $languages, $squirrelmail_language;
49 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
50 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
51 if (mb_detect_encoding($line) != 'ASCII') {
52 $line = $languages[$squirrelmail_language]['XTRA_CODE']('wordwrap', $line, $wrap);
57 ereg("^([\t >]*)([^\t >].*)?$", $line, $regs);
58 $beginning_spaces = $regs[1];
59 if (isset($regs[2])) {
60 $words = explode(' ', $regs[2]);
66 $line = $beginning_spaces;
68 while ($i < count($words)) {
69 /* Force one word to be on a line (minimum) */
71 $line_len = strlen($beginning_spaces) +
strlen($words[$i]) +
2;
72 if (isset($words[$i +
1]))
73 $line_len +
= strlen($words[$i +
1]);
76 /* Add more words (as long as they fit) */
77 while ($line_len < $wrap && $i < count($words)) {
78 $line .= ' ' . $words[$i];
80 if (isset($words[$i]))
81 $line_len +
= strlen($words[$i]) +
1;
86 /* Skip spaces if they are the first thing on a continued line */
87 while (!isset($words[$i]) && $i < count($words)) {
91 /* Go to the next line if we have more to process */
92 if ($i < count($words)) {
99 * Does the opposite of sqWordWrap()
101 function sqUnWordWrap(&$body) {
102 global $squirrelmail_language;
104 if ($squirrelmail_language == 'ja_JP') {
108 $lines = explode("\n", $body);
110 $PreviousSpaces = '';
111 $cnt = count($lines);
112 for ($i = 0; $i < $cnt; $i ++
) {
113 preg_match("/^([\t >]*)([^\t >].*)?$/", $lines[$i], $regs);
114 $CurrentSpaces = $regs[1];
115 if (isset($regs[2])) {
116 $CurrentRest = $regs[2];
122 $PreviousSpaces = $CurrentSpaces;
124 } else if (($PreviousSpaces == $CurrentSpaces) /* Do the beginnings match */
125 && (strlen($lines[$i - 1]) > 65) /* Over 65 characters long */
126 && strlen($CurrentRest)) { /* and there's a line to continue with */
127 $body .= ' ' . $CurrentRest;
129 $body .= "\n" . $lines[$i];
130 $PreviousSpaces = $CurrentSpaces;
137 * If $haystack is a full mailbox name and $needle is the mailbox
138 * separator character, returns the last part of the mailbox name.
140 function readShortMailboxName($haystack, $needle) {
145 $parts = explode($needle, $haystack);
146 $elem = array_pop($parts);
147 while ($elem == '' && count($parts)) {
148 $elem = array_pop($parts);
155 * Returns an array of email addresses.
156 * Be cautious of "user@host.com"
158 function parseAddrs($text) {
159 if (trim($text) == '')
161 $text = str_replace(' ', '', $text);
162 $text = ereg_replace('"[^"]*"', '', $text);
163 $text = ereg_replace('\\([^\\)]*\\)', '', $text);
164 $text = str_replace(',', ';', $text);
165 $array = explode(';', $text);
166 for ($i = 0; $i < count ($array); $i++
) {
167 $array[$i] = eregi_replace ('^.*[<]', '', $array[$i]);
168 $array[$i] = eregi_replace ('[>].*$', '', $array[$i]);
174 * Returns a line of comma separated email addresses from an array.
176 function getLineOfAddrs($array) {
177 if (is_array($array)) {
178 $to_line = implode(', ', $array);
179 $to_line = ereg_replace(', (, )+', ', ', $to_line);
180 $to_line = trim(ereg_replace('^, ', '', $to_line));
181 if( substr( $to_line, -1 ) == ',' )
182 $to_line = substr( $to_line, 0, -1 );
190 function php_self () {
191 if ( sqgetGlobalVar('REQUEST_URI', $req_uri, SQ_SERVER
) && !empty($req_uri) ) {
195 if ( sqgetGlobalVar('PHP_SELF', $php_self, SQ_SERVER
) && !empty($php_self) ) {
204 * This determines the location to forward to relative to your server.
205 * If this doesnt work correctly for you (although it should), you can
206 * remove all this code except the last two lines, and change the header()
207 * function to look something like this, customized to the location of
208 * SquirrelMail on your server:
210 * http://www.myhost.com/squirrelmail/src/login.php
212 function get_location () {
214 global $imap_server_type;
216 /* Get the path, handle virtual directories */
217 $path = substr(php_self(), 0, strrpos(php_self(), '/'));
219 if ( sqgetGlobalVar('sq_base_url', $full_url, SQ_SESSION
) ) {
220 return $full_url . $path;
223 /* Check if this is a HTTPS or regular HTTP request. */
227 * If you have 'SSLOptions +StdEnvVars' in your apache config
228 * OR if you have HTTPS=on in your HTTP_SERVER_VARS
229 * OR if you are on port 443
231 $getEnvVar = getenv('HTTPS');
232 if ((isset($getEnvVar) && !strcasecmp($getEnvVar, 'on')) ||
233 (sqgetGlobalVar('HTTPS', $https_on, SQ_SERVER
) && !strcasecmp($https_on, 'on')) ||
234 (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER
) && $server_port == 443)) {
238 /* Get the hostname from the Host header or server config. */
239 if ( !sqgetGlobalVar('HTTP_HOST', $host, SQ_SERVER
) ||
empty($host) ) {
240 if ( !sqgetGlobalVar('SERVER_NAME', $host, SQ_SERVER
) ||
empty($host) ) {
246 if (! strstr($host, ':')) {
247 if (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER
)) {
248 if (($server_port != 80 && $proto == 'http://') ||
249 ($server_port != 443 && $proto == 'https://')) {
250 $port = sprintf(':%d', $server_port);
255 /* this is a workaround for the weird macosx caching that
256 causes Apache to return 16080 as the port number, which causes
259 if ($imap_server_type == 'macosx' && $port == ':16080') {
263 /* Fallback is to omit the server name and use a relative */
264 /* URI, although this is not RFC 2616 compliant. */
265 $full_url = ($host ?
$proto . $host . $port : '');
266 sqsession_register($full_url, 'sq_base_url');
267 return $full_url . $path;
272 * These functions are used to encrypt the passowrd before it is
273 * stored in a cookie.
275 function OneTimePadEncrypt ($string, $epad) {
276 $pad = base64_decode($epad);
278 for ($i = 0; $i < strlen ($string); $i++
) {
279 $encrypted .= chr (ord($string[$i]) ^
ord($pad[$i]));
282 return base64_encode($encrypted);
285 function OneTimePadDecrypt ($string, $epad) {
286 $pad = base64_decode($epad);
287 $encrypted = base64_decode ($string);
289 for ($i = 0; $i < strlen ($encrypted); $i++
) {
290 $decrypted .= chr (ord($encrypted[$i]) ^
ord($pad[$i]));
298 * Randomize the mt_rand() function. Toss this in strings or integers
299 * and it will seed the generator appropriately. With strings, it is
300 * better to get them long. Use md5() to lengthen smaller strings.
302 function sq_mt_seed($Val) {
303 /* if mt_getrandmax() does not return a 2^n - 1 number,
304 this might not work well. This uses $Max as a bitmask. */
305 $Max = mt_getrandmax();
307 if (! is_int($Val)) {
319 mt_srand(($Val ^
mt_rand(0, $Max)) & $Max);
324 * This function initializes the random number generator fairly well.
325 * It also only initializes it once, so you don't accidentally get
326 * the same 'random' numbers twice in one session.
328 function sq_mt_randomize() {
336 sqgetGlobalVar('REMOTE_PORT', $remote_port, SQ_SERVER
);
337 sqgetGlobalVar('REMOTE_ADDR', $remote_addr, SQ_SERVER
);
338 sq_mt_seed((int)((double) microtime() * 1000000));
339 sq_mt_seed(md5($remote_port . $remote_addr . getmypid()));
342 if (function_exists('getrusage')) {
343 /* Avoid warnings with Win32 */
345 if (isset($dat) && is_array($dat)) {
347 foreach ($dat as $k => $v)
351 sq_mt_seed(md5($Str));
355 if(sqgetGlobalVar('UNIQUE_ID', $unique_id, SQ_SERVER
)) {
356 sq_mt_seed(md5($unique_id));
362 function OneTimePadCreate ($length=100) {
366 for ($i = 0; $i < $length; $i++
) {
367 $pad .= chr(mt_rand(0,255));
370 return base64_encode($pad);
374 * Returns a string showing the size of the message/attachment.
376 function show_readable_size($bytes) {
380 if ($bytes / 1024 > 1) {
387 settype($bytes, 'integer');
390 settype($bytes, 'integer');
393 return $bytes . '<small> ' . $type . '</small>';
397 * Generates a random string from the caracter set you pass in
400 * 1 = add lowercase a-z to $chars
401 * 2 = add uppercase A-Z to $chars
402 * 4 = add numbers 0-9 to $chars
405 function GenerateRandomString($size, $chars, $flags = 0) {
407 $chars .= 'abcdefghijklmnopqrstuvwxyz';
410 $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
413 $chars .= '0123456789';
416 if (($size < 1) ||
(strlen($chars) < 1)) {
420 sq_mt_randomize(); /* Initialize the random number generator */
423 $j = strlen( $chars ) - 1;
424 while (strlen($String) < $size) {
425 $String .= $chars{mt_rand(0, $j)};
431 function quoteimap($str) {
432 return ereg_replace('(["\\])', '\\\\1', $str);
436 * Trims every element in the array
438 function TrimArray(&$array) {
439 foreach ($array as $k => $v) {
442 foreach ($
$k as $k2 => $v2) {
443 $
$k[$k2] = substr($v2, 1);
449 /* Re-assign back to array. */
455 * Removes slashes from every element in the array
457 function RemoveSlashes(&$array) {
458 foreach ($array as $k => $v) {
461 foreach ($
$k as $k2 => $v2) {
462 $newArray[stripslashes($k2)] = stripslashes($v2);
466 $
$k = stripslashes($v);
469 /* Re-assign back to the array. */
474 $PHP_SELF = php_self();