Non-ascii characters caused problems with SVN function and were replaced
[squirrelmail.git] / functions / strings.php
1 <?php
2
3 /**
4 * strings.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
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 */
14
15 /**
16 * SquirrelMail version number -- DO NOT CHANGE
17 */
18 global $version;
19 $version = '1.2.6 [cvs]';
20
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 */
25 function readShortMailboxName($haystack, $needle) {
26
27 if ($needle == '') {
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 }
35 }
36 return( $elem );
37 }
38
39 /**
40 * Returns an array of email addresses.
41 * Be cautious of "user@host.com"
42 */
43 function parseAddrs($text) {
44 if (trim($text) == '')
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++) {
52 $array[$i] = eregi_replace ('^.*[<]', '', $array[$i]);
53 $array[$i] = eregi_replace ('[>].*$', '', $array[$i]);
54 }
55 return $array;
56 }
57
58 /**
59 * Returns a line of comma separated email addresses from an array.
60 */
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 );
68 } else {
69 $to_line = '';
70 }
71
72 return( $to_line );
73 }
74
75 function php_self () {
76 global $PHP_SELF, $HTTP_SERVER_VARS;
77
78 if (isset($HTTP_SERVER_VARS['REQUEST_URI']) && !empty($HTTP_SERVER_VARS['REQUEST_URI']) ) {
79 return $HTTP_SERVER_VARS['REQUEST_URI'];
80 }
81
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
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 */
102 function get_location () {
103
104 global $PHP_SELF, $SERVER_NAME, $HTTP_HOST, $SERVER_PORT,
105 $HTTP_SERVER_VARS;
106
107 /* Get the path, handle virtual directories */
108 $path = substr(php_self(), 0, strrpos(php_self(), '/'));
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)) {
123 $proto = 'https://';
124 }
125
126 /* Get the hostname from the Host header or server config. */
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;
132 } else if (isset($HTTP_SERVER_VARS['SERVER_NAME']) &&
133 !empty($HTTP_SERVER_VARS['SERVER_NAME'])) {
134 $host = $HTTP_SERVER_VARS['SERVER_NAME'];
135 }
136
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 }
152
153
154 /**
155 * These functions are used to encrypt the passowrd before it is
156 * stored in a cookie.
157 */
158 function 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
168 function 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 }
178
179
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 */
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();
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 }
221
222
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 */
228 function 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')) {
242 /* Avoid warnings with Win32 */
243 $dat = @getrusage();
244 if (isset($dat) && is_array($dat)) {
245 $Str = '';
246 foreach ($dat as $k => $v)
247 {
248 $Str .= $k . $v;
249 }
250 sq_mt_seed(md5($Str));
251 }
252 }
253
254 /* Apache-specific */
255 sq_mt_seed(md5($UNIQUE_ID));
256
257 $randomized = 1;
258 }
259
260 function 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 }
270
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 */
280 function 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];
289 if($vrel[0] == '.') {
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 */
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 */
311 return true;
312 } else if($vrel < 0 && $release >= 0){ /* This is beta, require not beta */
313 return false;
314 } else { /* Both are beta */
315 if($vrel > $release) return false;
316 }
317
318 return true;
319 }
320
321 /**
322 * Returns a string showing the size of the message/attachment.
323 */
324 function 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 }
343
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 */
352
353 function 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 }
367
368 sq_mt_randomize(); /* Initialize the random number generator */
369
370 $String = '';
371 $j = strlen( $chars ) - 1;
372 while (strlen($String) < $size) {
373 $String .= $chars{mt_rand(0, $j)};
374 }
375
376 return $String;
377 }
378
379 function quoteIMAP($str) {
380 return ereg_replace('(["\\])', '\\\\1', $str);
381 }
382
383 /**
384 * Trims every element in the array
385 */
386 function 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);
392 }
393 } else {
394 $$k = substr($v, 1);
395 }
396
397 /* Re-assign back to array. */
398 $array[$k] = $$k;
399 }
400 }
401
402 /**
403 * Removes slashes from every element in the array
404 */
405 function 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);
415 }
416
417 /* Re-assign back to the array. */
418 $array[$k] = $$k;
419 }
420 }
421
422 $PHP_SELF = php_self();
423
424 ?>