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