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 | */ |
18 | global $version; |
24d6ceca |
19 | $version = '1.3.0 [CVS-DEVEL]'; |
d068c0ec |
20 | |
5cc0b70e |
21 | /** |
22 | * Wraps text at $wrap characters |
23 | * |
24 | * Has a problem with special HTML characters, so call this before |
25 | * you do character translation. |
26 | * |
27 | * Specifically, ' comes up as 5 characters instead of 1. |
28 | * This should not add newlines to the end of lines. |
29 | */ |
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]); |
35 | } else { |
36 | $words = ''; |
37 | } |
38 | |
39 | $i = 0; |
40 | $line = $beginning_spaces; |
41 | |
42 | while ($i < count($words)) { |
43 | /* Force one word to be on a line (minimum) */ |
44 | $line .= $words[$i]; |
45 | $line_len = strlen($beginning_spaces) + strlen($words[$i]) + 2; |
46 | if (isset($words[$i + 1])) |
47 | $line_len += strlen($words[$i + 1]); |
48 | $i ++; |
49 | |
50 | /* Add more words (as long as they fit) */ |
51 | while ($line_len < $wrap && $i < count($words)) { |
52 | $line .= ' ' . $words[$i]; |
53 | $i++; |
54 | if (isset($words[$i])) |
55 | $line_len += strlen($words[$i]) + 1; |
56 | else |
57 | $line_len += 1; |
58 | } |
59 | |
60 | /* Skip spaces if they are the first thing on a continued line */ |
61 | while (!isset($words[$i]) && $i < count($words)) { |
62 | $i ++; |
63 | } |
64 | |
65 | /* Go to the next line if we have more to process */ |
66 | if ($i < count($words)) { |
e0858036 |
67 | $line .= "\n"; |
5cc0b70e |
68 | } |
69 | } |
70 | } |
71 | |
66239b65 |
72 | /** |
73 | * If $haystack is a full mailbox name and $needle is the mailbox |
74 | * separator character, returns the last part of the mailbox name. |
75 | */ |
76 | function readShortMailboxName($haystack, $needle) { |
97b1248c |
77 | |
66239b65 |
78 | if ($needle == '') { |
97b1248c |
79 | $elem = $haystack; |
80 | } else { |
81 | $parts = explode($needle, $haystack); |
82 | $elem = array_pop($parts); |
83 | while ($elem == '' && count($parts)) { |
84 | $elem = array_pop($parts); |
85 | } |
66239b65 |
86 | } |
97b1248c |
87 | return( $elem ); |
66239b65 |
88 | } |
3302d0d4 |
89 | |
66239b65 |
90 | /** |
91 | * Returns an array of email addresses. |
92 | * Be cautious of "user@host.com" |
93 | */ |
94 | function parseAddrs($text) { |
4deb32f1 |
95 | if (trim($text) == '') |
66239b65 |
96 | return array(); |
97 | $text = str_replace(' ', '', $text); |
98 | $text = ereg_replace('"[^"]*"', '', $text); |
99 | $text = ereg_replace('\\([^\\)]*\\)', '', $text); |
100 | $text = str_replace(',', ';', $text); |
101 | $array = explode(';', $text); |
102 | for ($i = 0; $i < count ($array); $i++) { |
4deb32f1 |
103 | $array[$i] = eregi_replace ('^.*[<]', '', $array[$i]); |
104 | $array[$i] = eregi_replace ('[>].*$', '', $array[$i]); |
66239b65 |
105 | } |
106 | return $array; |
107 | } |
40ee9452 |
108 | |
66239b65 |
109 | /** |
110 | * Returns a line of comma separated email addresses from an array. |
111 | */ |
112 | function getLineOfAddrs($array) { |
113 | if (is_array($array)) { |
8a549df2 |
114 | $to_line = implode(', ', $array); |
d46b103b |
115 | $to_line = ereg_replace(', (, )+', ', ', $to_line); |
116 | $to_line = trim(ereg_replace('^, ', '', $to_line)); |
117 | if( substr( $to_line, -1 ) == ',' ) |
118 | $to_line = substr( $to_line, 0, -1 ); |
66239b65 |
119 | } else { |
8a549df2 |
120 | $to_line = ''; |
66239b65 |
121 | } |
122 | |
123 | return( $to_line ); |
124 | } |
125 | |
43fdb2a4 |
126 | function php_self () { |
127 | global $PHP_SELF, $HTTP_SERVER_VARS; |
128 | |
4deb32f1 |
129 | if (isset($HTTP_SERVER_VARS['REQUEST_URI']) && !empty($HTTP_SERVER_VARS['REQUEST_URI']) ) { |
130 | return $HTTP_SERVER_VARS['REQUEST_URI']; |
131 | } |
132 | |
43fdb2a4 |
133 | if (isset($PHP_SELF) && !empty($PHP_SELF)) { |
134 | return $PHP_SELF; |
135 | } else if (isset($HTTP_SERVER_VARS['PHP_SELF']) && |
136 | !empty($HTTP_SERVER_VARS['PHP_SELF'])) { |
137 | return $HTTP_SERVER_VARS['PHP_SELF']; |
138 | } else { |
139 | return ''; |
140 | } |
141 | } |
142 | |
143 | |
66239b65 |
144 | /** |
145 | * This determines the location to forward to relative to your server. |
146 | * If this doesnt work correctly for you (although it should), you can |
147 | * remove all this code except the last two lines, and change the header() |
148 | * function to look something like this, customized to the location of |
149 | * SquirrelMail on your server: |
150 | * |
151 | * http://www.myhost.com/squirrelmail/src/login.php |
152 | */ |
153 | function get_location () { |
154 | |
155 | global $PHP_SELF, $SERVER_NAME, $HTTP_HOST, $SERVER_PORT, |
8de7f698 |
156 | $HTTP_SERVER_VARS, $imap_server_type; |
66239b65 |
157 | |
4deb32f1 |
158 | /* Get the path, handle virtual directories */ |
159 | $path = substr(php_self(), 0, strrpos(php_self(), '/')); |
66239b65 |
160 | |
161 | /* Check if this is a HTTPS or regular HTTP request. */ |
162 | $proto = 'http://'; |
163 | |
164 | /* |
165 | * If you have 'SSLOptions +StdEnvVars' in your apache config |
166 | * OR if you have HTTPS in your HTTP_SERVER_VARS |
167 | * OR if you are on port 443 |
168 | */ |
169 | $getEnvVar = getenv('HTTPS'); |
170 | if ((isset($getEnvVar) && !strcasecmp($getEnvVar, 'on')) || |
171 | (isset($HTTP_SERVER_VARS['HTTPS'])) || |
172 | (isset($HTTP_SERVER_VARS['SERVER_PORT']) && |
173 | $HTTP_SERVER_VARS['SERVER_PORT'] == 443)) { |
8a549df2 |
174 | $proto = 'https://'; |
66239b65 |
175 | } |
176 | |
4deb32f1 |
177 | /* Get the hostname from the Host header or server config. */ |
66239b65 |
178 | $host = ''; |
179 | if (isset($HTTP_HOST) && !empty($HTTP_HOST)) { |
180 | $host = $HTTP_HOST; |
181 | } else if (isset($SERVER_NAME) && !empty($SERVER_NAME)) { |
182 | $host = $SERVER_NAME; |
4dc9a365 |
183 | } else if (isset($HTTP_SERVER_VARS['SERVER_NAME']) && |
184 | !empty($HTTP_SERVER_VARS['SERVER_NAME'])) { |
185 | $host = $HTTP_SERVER_VARS['SERVER_NAME']; |
66239b65 |
186 | } |
4dc9a365 |
187 | |
66239b65 |
188 | |
189 | $port = ''; |
190 | if (! strstr($host, ':')) { |
191 | if (isset($SERVER_PORT)) { |
192 | if (($SERVER_PORT != 80 && $proto == 'http://') |
193 | || ($SERVER_PORT != 443 && $proto == 'https://')) { |
194 | $port = sprintf(':%d', $SERVER_PORT); |
195 | } |
196 | } |
197 | } |
198 | |
8de7f698 |
199 | /* this is a workaround for the weird macosx caching that |
200 | causes Apache to return 16080 as the port number, which causes |
201 | SM to bail */ |
202 | |
203 | if ($imap_server_type == 'macosx' && $port == ':16080') { |
204 | $port = ''; |
205 | } |
206 | |
66239b65 |
207 | /* Fallback is to omit the server name and use a relative */ |
208 | /* URI, although this is not RFC 2616 compliant. */ |
209 | return ($host ? $proto . $host . $port . $path : $path); |
210 | } |
dcaf2a49 |
211 | |
9374671f |
212 | |
66239b65 |
213 | /** |
214 | * These functions are used to encrypt the passowrd before it is |
215 | * stored in a cookie. |
216 | */ |
217 | function OneTimePadEncrypt ($string, $epad) { |
218 | $pad = base64_decode($epad); |
219 | $encrypted = ''; |
220 | for ($i = 0; $i < strlen ($string); $i++) { |
221 | $encrypted .= chr (ord($string[$i]) ^ ord($pad[$i])); |
222 | } |
223 | |
224 | return base64_encode($encrypted); |
225 | } |
226 | |
227 | function OneTimePadDecrypt ($string, $epad) { |
228 | $pad = base64_decode($epad); |
229 | $encrypted = base64_decode ($string); |
230 | $decrypted = ''; |
231 | for ($i = 0; $i < strlen ($encrypted); $i++) { |
232 | $decrypted .= chr (ord($encrypted[$i]) ^ ord($pad[$i])); |
233 | } |
234 | |
235 | return $decrypted; |
236 | } |
9374671f |
237 | |
9374671f |
238 | |
66239b65 |
239 | /** |
240 | * Randomize the mt_rand() function. Toss this in strings or integers |
241 | * and it will seed the generator appropriately. With strings, it is |
242 | * better to get them long. Use md5() to lengthen smaller strings. |
243 | */ |
244 | function sq_mt_seed($Val) { |
4deb32f1 |
245 | /* if mt_getrandmax() does not return a 2^n - 1 number, |
246 | this might not work well. This uses $Max as a bitmask. */ |
66239b65 |
247 | $Max = mt_getrandmax(); |
248 | |
249 | if (! is_int($Val)) { |
250 | if (function_exists('crc32')) { |
251 | $Val = crc32($Val); |
252 | } else { |
253 | $Str = $Val; |
254 | $Pos = 0; |
255 | $Val = 0; |
256 | $Mask = $Max / 2; |
257 | $HighBit = $Max ^ $Mask; |
258 | while ($Pos < strlen($Str)) { |
259 | if ($Val & $HighBit) { |
260 | $Val = (($Val & $Mask) << 1) + 1; |
261 | } else { |
262 | $Val = ($Val & $Mask) << 1; |
263 | } |
264 | $Val ^= $Str[$Pos]; |
265 | $Pos ++; |
266 | } |
267 | } |
268 | } |
269 | |
270 | if ($Val < 0) { |
271 | $Val *= -1; |
272 | } |
273 | |
274 | if ($Val = 0) { |
275 | return; |
276 | } |
277 | |
278 | mt_srand(($Val ^ mt_rand(0, $Max)) & $Max); |
279 | } |
9374671f |
280 | |
9374671f |
281 | |
66239b65 |
282 | /** |
283 | * This function initializes the random number generator fairly well. |
284 | * It also only initializes it once, so you don't accidentally get |
285 | * the same 'random' numbers twice in one session. |
286 | */ |
287 | function sq_mt_randomize() { |
288 | global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID; |
289 | static $randomized; |
290 | |
291 | if ($randomized) { |
292 | return; |
293 | } |
294 | |
295 | /* Global. */ |
296 | sq_mt_seed((int)((double) microtime() * 1000000)); |
297 | sq_mt_seed(md5($REMOTE_PORT . $REMOTE_ADDR . getmypid())); |
298 | |
299 | /* getrusage */ |
300 | if (function_exists('getrusage')) { |
4deb32f1 |
301 | /* Avoid warnings with Win32 */ |
66239b65 |
302 | $dat = @getrusage(); |
303 | if (isset($dat) && is_array($dat)) { |
821a8e9c |
304 | $Str = ''; |
305 | foreach ($dat as $k => $v) |
66239b65 |
306 | { |
307 | $Str .= $k . $v; |
308 | } |
821a8e9c |
309 | sq_mt_seed(md5($Str)); |
66239b65 |
310 | } |
311 | } |
312 | |
4deb32f1 |
313 | /* Apache-specific */ |
66239b65 |
314 | sq_mt_seed(md5($UNIQUE_ID)); |
315 | |
316 | $randomized = 1; |
317 | } |
318 | |
319 | function OneTimePadCreate ($length=100) { |
320 | sq_mt_randomize(); |
321 | |
322 | $pad = ''; |
323 | for ($i = 0; $i < $length; $i++) { |
324 | $pad .= chr(mt_rand(0,255)); |
325 | } |
326 | |
327 | return base64_encode($pad); |
328 | } |
9374671f |
329 | |
66239b65 |
330 | /** |
331 | * Check if we have a required PHP-version. Return TRUE if we do, |
332 | * or FALSE if we don't. |
333 | * |
334 | * To check for 4.0.1, use sqCheckPHPVersion(4,0,1) |
335 | * To check for 4.0b3, use sqCheckPHPVersion(4,0,-3) |
336 | * |
337 | * Does not handle betas like 4.0.1b1 or development versions |
338 | */ |
339 | function sqCheckPHPVersion($major, $minor, $release) { |
340 | |
341 | $ver = phpversion(); |
342 | eregi('^([0-9]+)\\.([0-9]+)(.*)', $ver, $regs); |
343 | |
344 | /* Parse the version string. */ |
345 | $vmajor = strval($regs[1]); |
346 | $vminor = strval($regs[2]); |
347 | $vrel = $regs[3]; |
4deb32f1 |
348 | if($vrel[0] == '.') { |
66239b65 |
349 | $vrel = strval(substr($vrel, 1)); |
350 | } |
351 | if($vrel[0] == 'b' || $vrel[0] == 'B') { |
352 | $vrel = - strval(substr($vrel, 1)); |
353 | } |
354 | if($vrel[0] == 'r' || $vrel[0] == 'R') { |
355 | $vrel = - strval(substr($vrel, 2))/10; |
356 | } |
357 | |
358 | /* Compare major version. */ |
359 | if ($vmajor < $major) { return false; } |
360 | if ($vmajor > $major) { return true; } |
361 | |
362 | /* Major is the same. Compare minor. */ |
363 | if ($vminor < $minor) { return false; } |
364 | if ($vminor > $minor) { return true; } |
365 | |
366 | /* Major and minor is the same as the required one. Compare release */ |
4deb32f1 |
367 | if ($vrel >= 0 && $release >= 0) { /* Neither are beta */ |
66239b65 |
368 | if($vrel < $release) return false; |
4deb32f1 |
369 | } else if($vrel >= 0 && $release < 0) { /* This is not beta, required is beta */ |
66239b65 |
370 | return true; |
4deb32f1 |
371 | } else if($vrel < 0 && $release >= 0){ /* This is beta, require not beta */ |
66239b65 |
372 | return false; |
4deb32f1 |
373 | } else { /* Both are beta */ |
66239b65 |
374 | if($vrel > $release) return false; |
375 | } |
376 | |
377 | return true; |
378 | } |
9374671f |
379 | |
66239b65 |
380 | /** |
381 | * Returns a string showing the size of the message/attachment. |
382 | */ |
383 | function show_readable_size($bytes) { |
384 | $bytes /= 1024; |
385 | $type = 'k'; |
386 | |
387 | if ($bytes / 1024 > 1) { |
388 | $bytes /= 1024; |
389 | $type = 'm'; |
390 | } |
391 | |
392 | if ($bytes < 10) { |
393 | $bytes *= 10; |
394 | settype($bytes, 'integer'); |
395 | $bytes /= 10; |
396 | } else { |
397 | settype($bytes, 'integer'); |
398 | } |
399 | |
400 | return $bytes . '<small> ' . $type . '</small>'; |
401 | } |
9374671f |
402 | |
66239b65 |
403 | /** |
404 | * Generates a random string from the caracter set you pass in |
405 | * |
406 | * Flags: |
407 | * 1 = add lowercase a-z to $chars |
408 | * 2 = add uppercase A-Z to $chars |
409 | * 4 = add numbers 0-9 to $chars |
410 | */ |
9374671f |
411 | |
66239b65 |
412 | function GenerateRandomString($size, $chars, $flags = 0) { |
413 | if ($flags & 0x1) { |
414 | $chars .= 'abcdefghijklmnopqrstuvwxyz'; |
415 | } |
416 | if ($flags & 0x2) { |
417 | $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
418 | } |
419 | if ($flags & 0x4) { |
420 | $chars .= '0123456789'; |
421 | } |
422 | |
423 | if (($size < 1) || (strlen($chars) < 1)) { |
424 | return ''; |
425 | } |
ff4f08ff |
426 | |
4deb32f1 |
427 | sq_mt_randomize(); /* Initialize the random number generator */ |
ff4f08ff |
428 | |
4deb32f1 |
429 | $String = ''; |
ff4f08ff |
430 | $j = strlen( $chars ) - 1; |
66239b65 |
431 | while (strlen($String) < $size) { |
ff4f08ff |
432 | $String .= $chars{mt_rand(0, $j)}; |
66239b65 |
433 | } |
ff4f08ff |
434 | |
66239b65 |
435 | return $String; |
436 | } |
9374671f |
437 | |
66239b65 |
438 | function quoteIMAP($str) { |
439 | return ereg_replace('(["\\])', '\\\\1', $str); |
440 | } |
1899535f |
441 | |
66239b65 |
442 | /** |
443 | * Trims every element in the array |
444 | */ |
445 | function TrimArray(&$array) { |
446 | foreach ($array as $k => $v) { |
447 | global $$k; |
448 | if (is_array($$k)) { |
449 | foreach ($$k as $k2 => $v2) { |
450 | $$k[$k2] = substr($v2, 1); |
23d6bd09 |
451 | } |
66239b65 |
452 | } else { |
453 | $$k = substr($v, 1); |
23d6bd09 |
454 | } |
66239b65 |
455 | |
4deb32f1 |
456 | /* Re-assign back to array. */ |
66239b65 |
457 | $array[$k] = $$k; |
458 | } |
459 | } |
23d6bd09 |
460 | |
66239b65 |
461 | /** |
462 | * Removes slashes from every element in the array |
463 | */ |
464 | function RemoveSlashes(&$array) { |
465 | foreach ($array as $k => $v) { |
466 | global $$k; |
467 | if (is_array($$k)) { |
468 | foreach ($$k as $k2 => $v2) { |
469 | $newArray[stripslashes($k2)] = stripslashes($v2); |
470 | } |
471 | $$k = $newArray; |
472 | } else { |
473 | $$k = stripslashes($v); |
23d6bd09 |
474 | } |
66239b65 |
475 | |
4deb32f1 |
476 | /* Re-assign back to the array. */ |
66239b65 |
477 | $array[$k] = $$k; |
23d6bd09 |
478 | } |
66239b65 |
479 | } |
23d6bd09 |
480 | |
43fdb2a4 |
481 | $PHP_SELF = php_self(); |
482 | |
1885d093 |
483 | ?> |