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