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