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