fa455664d023938a249559f2238efbf244dedf9b
[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($passed, $wrap) {
49 $passed = str_replace("&lt;", "<", $passed);
50 $passed = str_replace("&gt;", ">", $passed);
51
52 preg_match("/^(\s|>)+/", $passed, $regs);
53 $beginning_spaces = $regs[0];
54
55 $words = explode(" ", $passed);
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) {
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 return $line;
99 }
100
101 /** Returns an array of email addresses **/
102 function parseAddrs($text) {
103 if (trim($text) == "") {
104 return;
105 }
106 $text = str_replace(" ", "", $text);
107 $text = ereg_replace( '"[^"]*"', "", $text);
108 $text = str_replace(",", ";", $text);
109 $array = explode(";", $text);
110 for ($i = 0; $i < count ($array); $i++) {
111 $array[$i] = eregi_replace ("^.*[<]", "", $array[$i]);
112 $array[$i] = eregi_replace ("[>].*$", "", $array[$i]);
113 }
114 return $array;
115 }
116
117 /** Returns a line of comma separated email addresses from an array **/
118 function getLineOfAddrs($array) {
119 if (is_array($array)) {
120 $to_line = implode(", ", $array);
121 $to_line = trim(ereg_replace(",,+", ",", $to_line));
122 } else {
123 $to_line = "";
124 }
125 return $to_line;
126 }
127
128 function translateText($body, $wrap_at, $charset) {
129 global $where, $what; // from searching
130
131 if (!isset($url_parser_php)) {
132 include "../functions/url_parser.php";
133 }
134
135 $body_ary = explode("\n", $body);
136 for ($i=0; $i < count($body_ary); $i++) {
137 $line = $body_ary[$i];
138 $line = charset_decode($charset, $line);
139 $line = str_replace("\t", ' ', $line);
140
141 if (strlen($line) - 2 >= $wrap_at) {
142 $line = sqWordWrap($line, $wrap_at);
143 }
144
145 $line = str_replace(' ', '&nbsp;', $line);
146 $line = nl2br($line);
147
148 // Removed parseEmail and integrated it into parseUrl
149 // This line is no longer needed.
150 // $line = parseEmail ($line);
151 $line = parseUrl ($line);
152
153 $test_line = str_replace('&nbsp;', '', $line);
154 if (strpos($test_line, '&gt;&gt;') === 0) {
155 $line = "<FONT COLOR=FF0000>$line</FONT>\n";
156 } else if (strpos($test_line, '&gt;') === 0) {
157 $line = "<FONT COLOR=800000>$line</FONT>\n";
158 }
159
160 if ($line)
161 {
162 $line = '<tt>' . $line . '</tt>';
163 }
164
165 $body_ary[$i] = $line . '<br>';
166 }
167 $body = implode("\n", $body_ary);
168
169 return $body;
170 }
171
172 /* SquirrelMail version number -- DO NOT CHANGE */
173 $version = "0.5";
174
175
176 function find_mailbox_name ($mailbox) {
177 $mailbox = trim($mailbox);
178 if (substr($mailbox, strlen($mailbox)-1, strlen($mailbox)) == "\"") {
179 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
180 $pos = strrpos ($mailbox, "\"")+1;
181 $box = substr($mailbox, $pos);
182 } else {
183 $box = substr($mailbox, strrpos($mailbox, " ")+1, strlen($mailbox));
184 }
185 return $box;
186 }
187
188 function replace_spaces ($string) {
189 return str_replace(" ", "&nbsp;", $string);
190 }
191
192 function replace_escaped_spaces ($string) {
193 return str_replace("&nbsp;", " ", $string);
194 }
195
196 function get_location () {
197 # This determines the location to forward to relative
198 # to your server. If this doesnt work correctly for
199 # you (although it should), you can remove all this
200 # code except the last two lines, and change the header()
201 # function to look something like this, customized to
202 # the location of SquirrelMail on your server:
203 #
204 # http://www.myhost.com/squirrelmail/src/login.php
205
206 global $PHP_SELF, $SERVER_NAME, $HTTPS, $HTTP_HOST, $HTTP_SERVER_VARS;
207
208 // Get the path
209 $path = substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'));
210
211 // Check if this is a HTTPS or regular HTTP request
212 $proto = "http://";
213 if(isset($HTTPS) && $HTTPS == 'on' ) {
214 $proto = "https://";
215 }
216
217 if ($HTTP_SERVER_VARS['SERVER_PORT']) {
218 if ($HTTP_SERVER_VARS['SERVER_PORT'] != 80) {
219 $port = ':' . $HTTP_SERVER_VARS['SERVER_PORT'];
220 }
221 }
222
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)) {
227 $location = $proto . $HTTP_HOST . $port . $path;
228 } else if(isset($SERVER_NAME) && !empty($SERVER_NAME)) {
229 $location = $proto . $SERVER_NAME . $port . $path;
230 } else {
231 $location = $path;
232 }
233 return $location;
234 }
235
236 function sqStripSlashes($string) {
237 if (get_magic_quotes_gpc()) {
238 $string = stripslashes($string);
239 }
240 return $string;
241 }
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
264
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 }
303
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();
343
344 for ($i = 0; $i < $length; $i++) {
345 $pad .= chr(mt_rand(0,255));
346 }
347
348 return $pad;
349 }
350
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
372 // Compare major version
373 if($vmajor < $major) return false;
374 if($vmajor > $major) return true;
375
376 // Major is the same. Compare minor
377 if($vminor < $minor) return false;
378 if($vminor > $minor) return true;
379
380 // Major and minor is the same as the required one.
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
395 ?>