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