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