59177427 |
1 | <?php |
2ef37ed4 |
2 | |
3 | /* $Id$ */ |
7ce342dc |
4 | |
d068c0ec |
5 | $strings_php = true; |
65f5b1c4 |
6 | $fix_form_endlines = false; |
d51894be |
7 | |
8 | // Remove all slashes for form values |
9 | if (get_magic_quotes_gpc()) |
10 | { |
11 | global $REQUEST_METHOD; |
12 | if ($REQUEST_METHOD == "POST") |
13 | { |
14 | global $HTTP_POST_VARS; |
65f5b1c4 |
15 | RemoveSlashes($HTTP_POST_VARS); |
d51894be |
16 | } |
17 | elseif ($REQUEST_METHOD == "GET") |
18 | { |
19 | global $HTTP_GET_VARS; |
65f5b1c4 |
20 | RemoveSlashes($HTTP_GET_VARS); |
d51894be |
21 | } |
22 | } |
23 | |
65f5b1c4 |
24 | if (isset($fix_form_endlines) && $fix_form_endlines) { |
25 | if ($REQUEST_METHOD == "POST") { |
26 | TrimArray($HTTP_POST_VARS); |
27 | } else { |
28 | TrimArray($HTTP_GET_VARS); |
29 | } |
30 | } |
31 | |
32 | //************************************************************************** |
33 | // Trims every element in the array and returns a new array. |
34 | //************************************************************************** |
35 | function TrimArray(&$array) { |
36 | foreach ($array as $k => $v) { |
37 | global $$k; |
38 | if (is_array($$k)) { |
39 | foreach ($$k as $k2 => $v2) { |
40 | $newArray[trim($k2)] = trim($v2); |
41 | } |
42 | $$k = $newArray; |
43 | } else { |
44 | $$k = trim($v); |
45 | } |
46 | } |
47 | } |
48 | |
d51894be |
49 | |
65f5b1c4 |
50 | //************************************************************************** |
51 | // Removes slashes from every element in the array |
52 | //************************************************************************** |
d51894be |
53 | function RemoveSlashes($array) |
54 | { |
55 | foreach ($array as $k => $v) |
56 | { |
57 | global $$k; |
65f5b1c4 |
58 | if (is_array($$k)) |
59 | { |
60 | foreach ($$k as $k2 => $v2) |
61 | { |
62 | $newArray[stripslashes($k2)] = stripslashes($v2); |
63 | } |
64 | $$k = $newArray; |
65 | } |
66 | else |
67 | { |
68 | $$k = stripslashes($v); |
69 | } |
d51894be |
70 | } |
71 | } |
72 | |
d068c0ec |
73 | |
3302d0d4 |
74 | //************************************************************************* |
75 | // Count the number of occurances of $needle are in $haystack. |
37a17ace |
76 | // $needle can be a character or string, and need not occur in $haystack |
3302d0d4 |
77 | //************************************************************************* |
78 | function countCharInString($haystack, $needle) { |
37a17ace |
79 | if ($needle == '') return 0; |
80 | return count(explode($needle, $haystack)); |
3302d0d4 |
81 | } |
82 | |
83 | //************************************************************************* |
84 | // Read from the back of $haystack until $needle is found, or the begining |
f9b3e5d9 |
85 | // of the $haystack is reached. $needle is a single character |
3302d0d4 |
86 | //************************************************************************* |
87 | function readShortMailboxName($haystack, $needle) { |
8a549df2 |
88 | if ($needle == '') return $haystack; |
37a17ace |
89 | $parts = explode($needle, $haystack); |
90 | $elem = array_pop($parts); |
91 | while ($elem == '' && count($parts)) |
92 | { |
93 | $elem = array_pop($parts); |
94 | } |
95 | return $elem; |
3302d0d4 |
96 | } |
97 | |
5bdd7223 |
98 | //************************************************************************* |
99 | // Read from the back of $haystack until $needle is found, or the begining |
100 | // of the $haystack is reached. $needle is a single character |
101 | //************************************************************************* |
102 | function readMailboxParent($haystack, $needle) { |
37a17ace |
103 | if ($needle == '') return ''; |
104 | $parts = explode($needle, $haystack); |
105 | $elem = array_pop($parts); |
106 | while ($elem == '' && count($parts)) |
107 | { |
108 | $elem = array_pop($parts); |
109 | } |
110 | return join($needle, $parts); |
5bdd7223 |
111 | } |
112 | |
8beafbbc |
113 | // Searches for the next position in a string minus white space |
114 | function next_pos_minus_white ($haystack, $pos) { |
8a549df2 |
115 | while (substr($haystack, $pos, 1) == ' ' || |
8beafbbc |
116 | substr($haystack, $pos, 1) == "\t" || |
117 | substr($haystack, $pos, 1) == "\n" || |
118 | substr($haystack, $pos, 1) == "\r") { |
119 | if ($pos >= strlen($haystack)) |
120 | return -1; |
121 | $pos++; |
122 | } |
123 | return $pos; |
124 | } |
125 | |
8467bf00 |
126 | // Wraps text at $wrap characters |
a95681a7 |
127 | // Has a problem with special HTML characters, so call this before |
128 | // you do character translation. |
129 | // Specifically, ' comes up as 5 characters instead of 1. |
01aab860 |
130 | // This should not add newlines to the end of lines. |
9eea179c |
131 | function sqWordWrap(&$line, $wrap) { |
146e0c45 |
132 | ereg("^([\t >]*)([^\t >].*)?$", $line, $regs); |
45f6dd68 |
133 | $beginning_spaces = $regs[1]; |
65f5b1c4 |
134 | if (isset($regs[2])) { |
23fd3c8e |
135 | $words = explode(' ', $regs[2]); |
136 | } else { |
137 | $words = ""; |
65f5b1c4 |
138 | } |
a95681a7 |
139 | |
140 | $i = 0; |
141 | $line = $beginning_spaces; |
30f64711 |
142 | |
bcad90fe |
143 | while ($i < count($words)) { |
144 | // Force one word to be on a line (minimum) |
145 | $line .= $words[$i]; |
48f8ce2f |
146 | $line_len = strlen($beginning_spaces) + strlen($words[$i]) + 2; |
65f5b1c4 |
147 | if (isset($words[$i + 1])) |
148 | $line_len += strlen($words[$i + 1]); |
bcad90fe |
149 | $i ++; |
45f6dd68 |
150 | |
bcad90fe |
151 | // Add more words (as long as they fit) |
152 | while ($line_len < $wrap && $i < count($words)) { |
153 | $line .= ' ' . $words[$i]; |
154 | $i++; |
65f5b1c4 |
155 | if (isset($words[$i])) |
48f8ce2f |
156 | $line_len += strlen($words[$i]) + 1; |
65f5b1c4 |
157 | else |
158 | $line_len += 1; |
bcad90fe |
159 | } |
45f6dd68 |
160 | |
bcad90fe |
161 | // Skip spaces if they are the first thing on a continued line |
48f8ce2f |
162 | while (!isset($words[$i]) && $i < count($words)) { |
bcad90fe |
163 | $i ++; |
164 | } |
8ceb637a |
165 | |
166 | // Go to the next line if we have more to process |
bcad90fe |
167 | if ($i < count($words)) { |
5be8bbd8 |
168 | $line .= "\n" . $beginning_spaces; |
b8ea4ed6 |
169 | } |
e550d551 |
170 | } |
e550d551 |
171 | } |
01aab860 |
172 | |
173 | |
174 | // Does the opposite of sqWordWrap() |
175 | function sqUnWordWrap(&$body) |
176 | { |
177 | $lines = explode("\n", $body); |
178 | $body = ""; |
179 | $PreviousSpaces = ""; |
180 | for ($i = 0; $i < count($lines); $i ++) |
181 | { |
146e0c45 |
182 | ereg("^([\t >]*)([^\t >].*)?$", $lines[$i], $regs); |
01aab860 |
183 | $CurrentSpaces = $regs[1]; |
65f5b1c4 |
184 | if (isset($regs[2])) |
48f8ce2f |
185 | $CurrentRest = $regs[2]; |
01aab860 |
186 | if ($i == 0) |
187 | { |
188 | $PreviousSpaces = $CurrentSpaces; |
189 | $body = $lines[$i]; |
190 | } |
191 | else if ($PreviousSpaces == $CurrentSpaces && // Do the beginnings match |
192 | strlen($lines[$i - 1]) > 65 && // Over 65 characters long |
193 | strlen($CurrentRest)) // and there's a line to continue with |
194 | { |
195 | $body .= ' ' . $CurrentRest; |
196 | } |
197 | else |
198 | { |
199 | $body .= "\n" . $lines[$i]; |
200 | $PreviousSpaces = $CurrentSpaces; |
201 | } |
202 | } |
203 | $body .= "\n"; |
204 | } |
205 | |
40ee9452 |
206 | |
207 | /** Returns an array of email addresses **/ |
a7f3c40d |
208 | /* Be cautious of "user@host.com" */ |
40ee9452 |
209 | function parseAddrs($text) { |
1d2ce1c3 |
210 | if (trim($text) == "") |
c5edd369 |
211 | return array(); |
8a549df2 |
212 | $text = str_replace(' ', '', $text); |
213 | $text = ereg_replace('"[^"]*"', '', $text); |
5be8bbd8 |
214 | $text = ereg_replace('\\([^\\)]*\\)', '', $text); |
8a549df2 |
215 | $text = str_replace(',', ';', $text); |
216 | $array = explode(';', $text); |
1d2ce1c3 |
217 | for ($i = 0; $i < count ($array); $i++) { |
65f5b1c4 |
218 | $array[$i] = eregi_replace ("^.*[<]", '', $array[$i]); |
219 | $array[$i] = eregi_replace ("[>].*$", '', $array[$i]); |
220 | } |
1d2ce1c3 |
221 | return $array; |
40ee9452 |
222 | } |
223 | |
224 | /** Returns a line of comma separated email addresses from an array **/ |
225 | function getLineOfAddrs($array) { |
b676ba7e |
226 | if (is_array($array)) { |
8a549df2 |
227 | $to_line = implode(', ', $array); |
228 | $to_line = trim(ereg_replace(',,+', ',', $to_line)); |
b676ba7e |
229 | } else { |
8a549df2 |
230 | $to_line = ''; |
40ee9452 |
231 | } |
232 | return $to_line; |
233 | } |
7ce342dc |
234 | |
9eea179c |
235 | function translateText(&$body, $wrap_at, $charset) { |
9297917e |
236 | global $where, $what; // from searching |
65f5b1c4 |
237 | global $url_parser_php; |
9297917e |
238 | |
8442ac08 |
239 | if (!isset($url_parser_php)) { |
8a549df2 |
240 | include '../functions/url_parser.php'; |
8442ac08 |
241 | } |
242 | |
a8648d75 |
243 | $body_ary = explode("\n", $body); |
8ceb637a |
244 | $PriorQuotes = 0; |
8442ac08 |
245 | for ($i=0; $i < count($body_ary); $i++) { |
a8648d75 |
246 | $line = $body_ary[$i]; |
a37f3771 |
247 | if (strlen($line) - 2 >= $wrap_at) { |
9eea179c |
248 | sqWordWrap($line, $wrap_at); |
a37f3771 |
249 | } |
a95681a7 |
250 | $line = charset_decode($charset, $line); |
251 | $line = str_replace("\t", ' ', $line); |
a37f3771 |
252 | |
9eea179c |
253 | parseUrl ($line); |
e2ef6f4b |
254 | |
9eea179c |
255 | $Quotes = 0; |
256 | $pos = 0; |
257 | while (1) |
258 | { |
8ceb637a |
259 | if ($line[$pos] == ' ') |
9eea179c |
260 | { |
8ceb637a |
261 | $pos ++; |
9eea179c |
262 | } |
263 | else if (strpos($line, '>', $pos) === $pos) |
264 | { |
265 | $pos += 4; |
266 | $Quotes ++; |
267 | } |
268 | else |
269 | { |
270 | break; |
271 | } |
272 | } |
273 | |
8ceb637a |
274 | if ($Quotes > 1) |
ccc4b58b |
275 | $line = '<FONT COLOR="FF0000">'.$line.'</FONT>'; |
8ceb637a |
276 | elseif ($Quotes) |
ccc4b58b |
277 | $line = '<FONT COLOR="800000">'.$line.'</FONT>'; |
e2ef6f4b |
278 | |
8ceb637a |
279 | $body_ary[$i] = $line; |
a8648d75 |
280 | } |
8a549df2 |
281 | $body = '<pre>' . implode("\n", $body_ary) . '</pre>'; |
78509c54 |
282 | } |
283 | |
7ce342dc |
284 | /* SquirrelMail version number -- DO NOT CHANGE */ |
68b6d237 |
285 | $version = '1.0.2 [cvs]'; |
d29aac0e |
286 | |
287 | |
288 | function find_mailbox_name ($mailbox) { |
f9b3e5d9 |
289 | if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs)) |
290 | return $regs[1]; |
291 | ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs); |
292 | return $regs[1]; |
293 | |
d29aac0e |
294 | } |
295 | |
296 | function replace_spaces ($string) { |
8a549df2 |
297 | return str_replace(' ', ' ', $string); |
d29aac0e |
298 | } |
299 | |
300 | function replace_escaped_spaces ($string) { |
8a549df2 |
301 | return str_replace(' ', ' ', $string); |
d29aac0e |
302 | } |
1195c340 |
303 | |
304 | function get_location () { |
305 | # This determines the location to forward to relative |
7e343a7d |
306 | # to your server. If this doesnt work correctly for |
1195c340 |
307 | # you (although it should), you can remove all this |
308 | # code except the last two lines, and change the header() |
309 | # function to look something like this, customized to |
310 | # the location of SquirrelMail on your server: |
311 | # |
312 | # http://www.myhost.com/squirrelmail/src/login.php |
313 | |
00421cb6 |
314 | global $PHP_SELF, $SERVER_NAME, $HTTPS, $HTTP_HOST, $SERVER_PORT; |
1195c340 |
315 | |
316 | // Get the path |
317 | $path = substr($PHP_SELF, 0, strrpos($PHP_SELF, '/')); |
318 | |
319 | // Check if this is a HTTPS or regular HTTP request |
8a549df2 |
320 | $proto = 'http://'; |
16f32d4a |
321 | if(isset($HTTPS) && !strcasecmp($HTTPS, 'on') ) { |
8a549df2 |
322 | $proto = 'https://'; |
1195c340 |
323 | } |
324 | |
325 | // Get the hostname from the Host header or server config. |
8a549df2 |
326 | $host = ''; |
b768a8eb |
327 | if (isset($HTTP_HOST) && !empty($HTTP_HOST)) |
328 | { |
329 | $host = $HTTP_HOST; |
330 | } |
331 | else if (isset($SERVER_NAME) && !empty($SERVER_NAME)) |
332 | { |
333 | $host = $SERVER_NAME; |
334 | } |
335 | |
b768a8eb |
336 | $port = ''; |
337 | if (! strstr($host, ':')) |
338 | { |
339 | if (isset($SERVER_PORT)) { |
8a549df2 |
340 | if (($SERVER_PORT != 80 && $proto == 'http://') |
341 | || ($SERVER_PORT != 443 && $proto == 'https://')) { |
b768a8eb |
342 | $port = sprintf(':%d', $SERVER_PORT); |
343 | } |
344 | } |
345 | } |
346 | |
347 | if ($host) |
348 | return $proto . $host . $port . $path; |
349 | |
1195c340 |
350 | // Fallback is to omit the server name and use a relative URI, |
351 | // although this is not RFC 2616 compliant. |
b768a8eb |
352 | return $path; |
1195c340 |
353 | } |
7aaa81fc |
354 | |
52eefafc |
355 | |
356 | // These functions are used to encrypt the passowrd before it is |
357 | // stored in a cookie. |
59edd854 |
358 | function OneTimePadEncrypt ($string, $epad) { |
359 | $pad = base64_decode($epad); |
8a549df2 |
360 | $encrypted = ''; |
52eefafc |
361 | for ($i = 0; $i < strlen ($string); $i++) { |
65f5b1c4 |
362 | $encrypted .= chr (ord($string[$i]) ^ ord($pad[$i])); |
52eefafc |
363 | } |
364 | |
365 | return base64_encode($encrypted); |
366 | } |
367 | |
59edd854 |
368 | function OneTimePadDecrypt ($string, $epad) { |
369 | $pad = base64_decode($epad); |
52eefafc |
370 | $encrypted = base64_decode ($string); |
8a549df2 |
371 | $decrypted = ''; |
52eefafc |
372 | for ($i = 0; $i < strlen ($encrypted); $i++) { |
65f5b1c4 |
373 | $decrypted .= chr (ord($encrypted[$i]) ^ ord($pad[$i])); |
52eefafc |
374 | } |
375 | |
376 | return $decrypted; |
377 | } |
378 | |
580e1793 |
379 | |
dcaf2a49 |
380 | // Randomize the mt_rand() function. Toss this in strings or |
381 | // integers and it will seed the generator appropriately. |
382 | // With strings, it is better to get them long. Use md5() to |
383 | // lengthen smaller strings. |
384 | function sq_mt_seed($Val) |
385 | { |
386 | // if mt_getrandmax() does not return a 2^n - 1 number, |
387 | // this might not work well. This uses $Max as a bitmask. |
388 | $Max = mt_getrandmax(); |
389 | |
390 | if (! is_int($Val)) |
391 | { |
8a549df2 |
392 | if (function_exists('crc32')) |
dcaf2a49 |
393 | { |
394 | $Val = crc32($Val); |
395 | } |
396 | else |
397 | { |
398 | $Str = $Val; |
399 | $Pos = 0; |
400 | $Val = 0; |
401 | $Mask = $Max / 2; |
402 | $HighBit = $Max ^ $Mask; |
403 | while ($Pos < strlen($Str)) |
404 | { |
405 | if ($Val & $HighBit) |
406 | { |
407 | $Val = (($Val & $Mask) << 1) + 1; |
408 | } |
409 | else |
410 | { |
411 | $Val = ($Val & $Mask) << 1; |
412 | } |
413 | $Val ^= $Str[$Pos]; |
414 | $Pos ++; |
415 | } |
416 | } |
417 | } |
580e1793 |
418 | |
dcaf2a49 |
419 | if ($Val < 0) |
420 | $Val *= -1; |
421 | if ($Val = 0) |
422 | return; |
423 | |
424 | mt_srand(($Val ^ mt_rand(0, $Max)) & $Max); |
425 | } |
426 | |
427 | |
428 | // This function initializes the random number generator fairly well. |
429 | // It also only initializes it once, so you don't accidentally get |
430 | // the same 'random' numbers twice in one session. |
431 | function sq_mt_randomize() |
432 | { |
433 | global $REMOTE_PORT, $REMOTE_ADDR, $UNIQUE_ID; |
434 | static $randomized; |
435 | |
436 | if ($randomized) |
437 | return; |
438 | |
439 | // Global |
440 | sq_mt_seed((int)((double) microtime() * 1000000)); |
441 | sq_mt_seed(md5($REMOTE_PORT . $REMOTE_ADDR . getmypid())); |
442 | |
443 | // getrusage |
8a549df2 |
444 | if (function_exists('getrusage')) { |
dcaf2a49 |
445 | $dat = getrusage(); |
65f5b1c4 |
446 | $Str = ''; |
447 | foreach ($dat as $k => $v) |
448 | { |
449 | $Str .= $k . $v; |
450 | } |
451 | sq_mt_seed(md5($Str)); |
dcaf2a49 |
452 | } |
453 | |
454 | // Apache-specific |
455 | sq_mt_seed(md5($UNIQUE_ID)); |
456 | |
457 | $randomized = 1; |
458 | } |
459 | |
460 | function OneTimePadCreate ($length=100) { |
461 | sq_mt_randomize(); |
245a6892 |
462 | |
8a549df2 |
463 | $pad = ''; |
52eefafc |
464 | for ($i = 0; $i < $length; $i++) { |
65f5b1c4 |
465 | $pad .= chr(mt_rand(0,255)); |
52eefafc |
466 | } |
467 | |
59edd854 |
468 | return base64_encode($pad); |
52eefafc |
469 | } |
470 | |
f79af863 |
471 | // Check if we have a required PHP-version. Return TRUE if we do, |
472 | // or FALSE if we don't. |
473 | // To check for 4.0.1, use sqCheckPHPVersion(4,0,1) |
474 | // To check for 4.0b3, use sqCheckPHPVersion(4,0,-3) |
475 | // Does not handle betas like 4.0.1b1 or development versions |
476 | function sqCheckPHPVersion($major, $minor, $release) { |
477 | |
478 | $ver = phpversion(); |
5be8bbd8 |
479 | eregi('^([0-9]+)\\.([0-9]+)(.*)', $ver, $regs); |
f79af863 |
480 | |
481 | // Parse the version string |
482 | $vmajor = strval($regs[1]); |
483 | $vminor = strval($regs[2]); |
484 | $vrel = $regs[3]; |
485 | if($vrel[0] == ".") |
65f5b1c4 |
486 | $vrel = strval(substr($vrel, 1)); |
8a549df2 |
487 | if($vrel[0] == 'b' || $vrel[0] == 'B') |
65f5b1c4 |
488 | $vrel = - strval(substr($vrel, 1)); |
8a549df2 |
489 | if($vrel[0] == 'r' || $vrel[0] == 'R') |
65f5b1c4 |
490 | $vrel = - strval(substr($vrel, 2))/10; |
f79af863 |
491 | |
b5afdff0 |
492 | // Compare major version |
f79af863 |
493 | if($vmajor < $major) return false; |
b5afdff0 |
494 | if($vmajor > $major) return true; |
495 | |
496 | // Major is the same. Compare minor |
f79af863 |
497 | if($vminor < $minor) return false; |
b5afdff0 |
498 | if($vminor > $minor) return true; |
f79af863 |
499 | |
b5afdff0 |
500 | // Major and minor is the same as the required one. |
f79af863 |
501 | // Compare release |
502 | if($vrel >= 0 && $release >= 0) { // Neither are beta |
65f5b1c4 |
503 | if($vrel < $release) return false; |
f79af863 |
504 | } else if($vrel >= 0 && $release < 0){ // This is not beta, required is beta |
65f5b1c4 |
505 | return true; |
f79af863 |
506 | } else if($vrel < 0 && $release >= 0){ // This is beta, require not beta |
65f5b1c4 |
507 | return false; |
f79af863 |
508 | } else { // Both are beta |
65f5b1c4 |
509 | if($vrel > $release) return false; |
f79af863 |
510 | } |
511 | |
512 | return true; |
513 | } |
6e7468f6 |
514 | |
515 | /* Returns a string showing the size of the message/attachment */ |
516 | function show_readable_size($bytes) |
517 | { |
518 | $bytes /= 1024; |
519 | $type = 'k'; |
520 | |
521 | if ($bytes / 1024 > 1) |
522 | { |
523 | $bytes /= 1024; |
524 | $type = 'm'; |
525 | } |
526 | |
527 | if ($bytes < 10) |
528 | { |
529 | $bytes *= 10; |
8a549df2 |
530 | settype($bytes, 'integer'); |
6e7468f6 |
531 | $bytes /= 10; |
532 | } |
533 | else |
8a549df2 |
534 | settype($bytes, 'integer'); |
6e7468f6 |
535 | |
536 | return $bytes . '<small> ' . $type . '</small>'; |
537 | } |
f79af863 |
538 | |
1899535f |
539 | /* Generates a random string from the caracter set you pass in |
540 | * |
541 | * Flags: |
542 | * 1 = add lowercase a-z to $chars |
543 | * 2 = add uppercase A-Z to $chars |
544 | * 4 = add numbers 0-9 to $chars |
545 | */ |
546 | |
547 | function GenerateRandomString($size, $chars, $flags = 0) |
548 | { |
549 | if ($flags & 0x1) |
550 | $chars .= 'abcdefghijklmnopqrstuvwxyz'; |
551 | if ($flags & 0x2) |
552 | $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
553 | if ($flags & 0x4) |
554 | $chars .= '0123456789'; |
555 | |
556 | if ($size < 1 || strlen($chars) < 1) |
8a549df2 |
557 | return ''; |
1899535f |
558 | |
559 | sq_mt_randomize(); // Initialize the random number generator |
560 | |
65f5b1c4 |
561 | $String = ""; |
1899535f |
562 | while (strlen($String) < $size) { |
563 | $String .= $chars[mt_rand(0, strlen($chars))]; |
564 | } |
565 | |
566 | return $String; |
567 | } |
568 | |
ccc4b58b |
569 | ?> |