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