Redirect the wrong access to the root pf the folder as index.php are in place
[squirrelmail.git] / functions / strings.php
CommitLineData
59177427 1<?php
7350889b 2
e080b080 3/**
35586184 4 * strings.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This code provides various string manipulation functions that are
10 * used by the rest of the Squirrelmail code.
11 *
12 * $Id$
13 */
9374671f 14
35586184 15/**
16 * SquirrelMail version number -- DO NOT CHANGE
17 */
18global $version;
03ffa719 19$version = '1.2.6 [cvs]';
d068c0ec 20
66239b65 21/**
22 * If $haystack is a full mailbox name and $needle is the mailbox
23 * separator character, returns the last part of the mailbox name.
24 */
25function readShortMailboxName($haystack, $needle) {
97b1248c 26
66239b65 27 if ($needle == '') {
97b1248c 28 $elem = $haystack;
29 } else {
30 $parts = explode($needle, $haystack);
31 $elem = array_pop($parts);
32 while ($elem == '' && count($parts)) {
33 $elem = array_pop($parts);
34 }
66239b65 35 }
97b1248c 36 return( $elem );
66239b65 37}
3302d0d4 38
66239b65 39/**
66239b65 40 * Returns an array of email addresses.
41 * Be cautious of "user@host.com"
42 */
43function parseAddrs($text) {
4deb32f1 44 if (trim($text) == '')
66239b65 45 return array();
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++) {
4deb32f1 52 $array[$i] = eregi_replace ('^.*[<]', '', $array[$i]);
53 $array[$i] = eregi_replace ('[>].*$', '', $array[$i]);
66239b65 54 }
55 return $array;
56}
40ee9452 57
66239b65 58/**
59 * Returns a line of comma separated email addresses from an array.
60 */
61function getLineOfAddrs($array) {
62 if (is_array($array)) {
8a549df2 63 $to_line = implode(', ', $array);
d46b103b 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 );
66239b65 68 } else {
8a549df2 69 $to_line = '';
66239b65 70 }
71
72 return( $to_line );
73}
74
43fdb2a4 75function php_self () {
76 global $PHP_SELF, $HTTP_SERVER_VARS;
77
4deb32f1 78 if (isset($HTTP_SERVER_VARS['REQUEST_URI']) && !empty($HTTP_SERVER_VARS['REQUEST_URI']) ) {
79 return $HTTP_SERVER_VARS['REQUEST_URI'];
80 }
81
43fdb2a4 82 if (isset($PHP_SELF) && !empty($PHP_SELF)) {
83 return $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'];
87 } else {
88 return '';
89 }
90}
91
92
66239b65 93/**
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:
99 *
100 * http://www.myhost.com/squirrelmail/src/login.php
101 */
102function get_location () {
103
104 global $PHP_SELF, $SERVER_NAME, $HTTP_HOST, $SERVER_PORT,
105 $HTTP_SERVER_VARS;
106
4deb32f1 107 /* Get the path, handle virtual directories */
108 $path = substr(php_self(), 0, strrpos(php_self(), '/'));
66239b65 109
110 /* Check if this is a HTTPS or regular HTTP request. */
111 $proto = 'http://';
112
113 /*
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
117 */
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)) {
8a549df2 123 $proto = 'https://';
66239b65 124 }
125
4deb32f1 126 /* Get the hostname from the Host header or server config. */
66239b65 127 $host = '';
128 if (isset($HTTP_HOST) && !empty($HTTP_HOST)) {
129 $host = $HTTP_HOST;
130 } else if (isset($SERVER_NAME) && !empty($SERVER_NAME)) {
131 $host = $SERVER_NAME;
4dc9a365 132 } else if (isset($HTTP_SERVER_VARS['SERVER_NAME']) &&
133 !empty($HTTP_SERVER_VARS['SERVER_NAME'])) {
134 $host = $HTTP_SERVER_VARS['SERVER_NAME'];
66239b65 135 }
4dc9a365 136
66239b65 137
138 $port = '';
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);
144 }
145 }
146 }
147
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);
151}
dcaf2a49 152
9374671f 153
66239b65 154/**
155 * These functions are used to encrypt the passowrd before it is
156 * stored in a cookie.
157 */
158function OneTimePadEncrypt ($string, $epad) {
159 $pad = base64_decode($epad);
160 $encrypted = '';
161 for ($i = 0; $i < strlen ($string); $i++) {
162 $encrypted .= chr (ord($string[$i]) ^ ord($pad[$i]));
163 }
164
165 return base64_encode($encrypted);
166}
167
168function OneTimePadDecrypt ($string, $epad) {
169 $pad = base64_decode($epad);
170 $encrypted = base64_decode ($string);
171 $decrypted = '';
172 for ($i = 0; $i < strlen ($encrypted); $i++) {
173 $decrypted .= chr (ord($encrypted[$i]) ^ ord($pad[$i]));
174 }
175
176 return $decrypted;
177}
9374671f 178
9374671f 179
66239b65 180/**
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.
184 */
185function sq_mt_seed($Val) {
4deb32f1 186 /* if mt_getrandmax() does not return a 2^n - 1 number,
187 this might not work well. This uses $Max as a bitmask. */
66239b65 188 $Max = mt_getrandmax();
189
190 if (! is_int($Val)) {
191 if (function_exists('crc32')) {
192 $Val = crc32($Val);
193 } else {
194 $Str = $Val;
195 $Pos = 0;
196 $Val = 0;
197 $Mask = $Max / 2;
198 $HighBit = $Max ^ $Mask;
199 while ($Pos < strlen($Str)) {
200 if ($Val & $HighBit) {
201 $Val = (($Val & $Mask) << 1) + 1;
202 } else {
203 $Val = ($Val & $Mask) << 1;
204 }
205 $Val ^= $Str[$Pos];
206 $Pos ++;
207 }
208 }
209 }
210
211 if ($Val < 0) {
212 $Val *= -1;
213 }
214
215 if ($Val = 0) {
216 return;
217 }
218
219 mt_srand(($Val ^ mt_rand(0, $Max)) & $Max);
220}
9374671f 221
9374671f 222
66239b65 223/**
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.
227 */
228function sq_mt_randomize() {
229 global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID;
230 static $randomized;
231
232 if ($randomized) {
233 return;
234 }
235
236 /* Global. */
237 sq_mt_seed((int)((double) microtime() * 1000000));
238 sq_mt_seed(md5($REMOTE_PORT . $REMOTE_ADDR . getmypid()));
239
240 /* getrusage */
241 if (function_exists('getrusage')) {
4deb32f1 242 /* Avoid warnings with Win32 */
66239b65 243 $dat = @getrusage();
244 if (isset($dat) && is_array($dat)) {
821a8e9c 245 $Str = '';
246 foreach ($dat as $k => $v)
66239b65 247 {
248 $Str .= $k . $v;
249 }
821a8e9c 250 sq_mt_seed(md5($Str));
66239b65 251 }
252 }
253
4deb32f1 254 /* Apache-specific */
66239b65 255 sq_mt_seed(md5($UNIQUE_ID));
256
257 $randomized = 1;
258}
259
260function OneTimePadCreate ($length=100) {
261 sq_mt_randomize();
262
263 $pad = '';
264 for ($i = 0; $i < $length; $i++) {
265 $pad .= chr(mt_rand(0,255));
266 }
267
268 return base64_encode($pad);
269}
9374671f 270
66239b65 271/**
272 * Check if we have a required PHP-version. Return TRUE if we do,
273 * or FALSE if we don't.
274 *
275 * To check for 4.0.1, use sqCheckPHPVersion(4,0,1)
276 * To check for 4.0b3, use sqCheckPHPVersion(4,0,-3)
277 *
278 * Does not handle betas like 4.0.1b1 or development versions
279 */
280function sqCheckPHPVersion($major, $minor, $release) {
281
282 $ver = phpversion();
283 eregi('^([0-9]+)\\.([0-9]+)(.*)', $ver, $regs);
284
285 /* Parse the version string. */
286 $vmajor = strval($regs[1]);
287 $vminor = strval($regs[2]);
288 $vrel = $regs[3];
4deb32f1 289 if($vrel[0] == '.') {
66239b65 290 $vrel = strval(substr($vrel, 1));
291 }
292 if($vrel[0] == 'b' || $vrel[0] == 'B') {
293 $vrel = - strval(substr($vrel, 1));
294 }
295 if($vrel[0] == 'r' || $vrel[0] == 'R') {
296 $vrel = - strval(substr($vrel, 2))/10;
297 }
298
299 /* Compare major version. */
300 if ($vmajor < $major) { return false; }
301 if ($vmajor > $major) { return true; }
302
303 /* Major is the same. Compare minor. */
304 if ($vminor < $minor) { return false; }
305 if ($vminor > $minor) { return true; }
306
307 /* Major and minor is the same as the required one. Compare release */
4deb32f1 308 if ($vrel >= 0 && $release >= 0) { /* Neither are beta */
66239b65 309 if($vrel < $release) return false;
4deb32f1 310 } else if($vrel >= 0 && $release < 0) { /* This is not beta, required is beta */
66239b65 311 return true;
4deb32f1 312 } else if($vrel < 0 && $release >= 0){ /* This is beta, require not beta */
66239b65 313 return false;
4deb32f1 314 } else { /* Both are beta */
66239b65 315 if($vrel > $release) return false;
316 }
317
318 return true;
319}
9374671f 320
66239b65 321/**
322 * Returns a string showing the size of the message/attachment.
323 */
324function show_readable_size($bytes) {
325 $bytes /= 1024;
326 $type = 'k';
327
328 if ($bytes / 1024 > 1) {
329 $bytes /= 1024;
330 $type = 'm';
331 }
332
333 if ($bytes < 10) {
334 $bytes *= 10;
335 settype($bytes, 'integer');
336 $bytes /= 10;
337 } else {
338 settype($bytes, 'integer');
339 }
340
341 return $bytes . '<small>&nbsp;' . $type . '</small>';
342}
9374671f 343
66239b65 344/**
345 * Generates a random string from the caracter set you pass in
346 *
347 * Flags:
348 * 1 = add lowercase a-z to $chars
349 * 2 = add uppercase A-Z to $chars
350 * 4 = add numbers 0-9 to $chars
351 */
9374671f 352
66239b65 353function GenerateRandomString($size, $chars, $flags = 0) {
354 if ($flags & 0x1) {
355 $chars .= 'abcdefghijklmnopqrstuvwxyz';
356 }
357 if ($flags & 0x2) {
358 $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
359 }
360 if ($flags & 0x4) {
361 $chars .= '0123456789';
362 }
363
364 if (($size < 1) || (strlen($chars) < 1)) {
365 return '';
366 }
ff4f08ff 367
4deb32f1 368 sq_mt_randomize(); /* Initialize the random number generator */
ff4f08ff 369
4deb32f1 370 $String = '';
ff4f08ff 371 $j = strlen( $chars ) - 1;
66239b65 372 while (strlen($String) < $size) {
ff4f08ff 373 $String .= $chars{mt_rand(0, $j)};
66239b65 374 }
ff4f08ff 375
66239b65 376 return $String;
377}
9374671f 378
66239b65 379function quoteIMAP($str) {
380 return ereg_replace('(["\\])', '\\\\1', $str);
381}
1899535f 382
66239b65 383/**
384 * Trims every element in the array
385 */
386function TrimArray(&$array) {
387 foreach ($array as $k => $v) {
388 global $$k;
389 if (is_array($$k)) {
390 foreach ($$k as $k2 => $v2) {
391 $$k[$k2] = substr($v2, 1);
23d6bd09 392 }
66239b65 393 } else {
394 $$k = substr($v, 1);
23d6bd09 395 }
66239b65 396
4deb32f1 397 /* Re-assign back to array. */
66239b65 398 $array[$k] = $$k;
399 }
400}
23d6bd09 401
66239b65 402/**
403 * Removes slashes from every element in the array
404 */
405function RemoveSlashes(&$array) {
406 foreach ($array as $k => $v) {
407 global $$k;
408 if (is_array($$k)) {
409 foreach ($$k as $k2 => $v2) {
410 $newArray[stripslashes($k2)] = stripslashes($v2);
411 }
412 $$k = $newArray;
413 } else {
414 $$k = stripslashes($v);
23d6bd09 415 }
66239b65 416
4deb32f1 417 /* Re-assign back to the array. */
66239b65 418 $array[$k] = $$k;
23d6bd09 419 }
66239b65 420}
23d6bd09 421
43fdb2a4 422$PHP_SELF = php_self();
423
97b1248c 424?>