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