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