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