Functions now pass message body by reference to save on memory.
[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
9eea179c 48 function sqWordWrap(&$line, $wrap) {
49 $line = str_replace("&lt;", "<", $line);
50 $line = str_replace("&gt;", ">", $line);
a8648d75 51
9eea179c 52 preg_match("/^(\s|>)+/", $line, $regs);
7aaa81fc 53 $beginning_spaces = $regs[0];
54
9eea179c 55 $words = explode(" ", $line);
7aaa81fc 56 $i = -1;
57 $line_len = strlen($words[0])+1;
8467bf00 58 $line = "";
d68a3926 59 if (count($words) > 1) {
7aaa81fc 60 while ($i++ < count($words)) {
9eea179c 61 while ($line_len < $wrap && $i < count($words)) {
d68a3926 62 $line = "$line$words[$i] ";
63 $i++;
7aaa81fc 64 $line_len = $line_len + strlen($words[$i]) + 1;
d68a3926 65 }
66 $line_len = strlen($words[$i])+1;
7aaa81fc 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;
d68a3926 75 } else {
7aaa81fc 76 /*
d68a3926 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 }
7aaa81fc 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]);
b8ea4ed6 90 }
b8ea4ed6 91 }
d68a3926 92 } else {
93 $line = $words[0];
e550d551 94 }
a8648d75 95
96 $line = str_replace(">", "&gt;", $line);
97 $line = str_replace("<", "&lt;", $line);
e550d551 98 }
40ee9452 99
100 /** Returns an array of email addresses **/
101 function parseAddrs($text) {
7831268e 102 if (trim($text) == "") {
103 return;
104 }
40ee9452 105 $text = str_replace(" ", "", $text);
cf8758c7 106 $text = ereg_replace( '"[^"]*"', "", $text);
40ee9452 107 $text = str_replace(",", ";", $text);
108 $array = explode(";", $text);
901a2b44 109 for ($i = 0; $i < count ($array); $i++) {
110 $array[$i] = eregi_replace ("^.*[<]", "", $array[$i]);
111 $array[$i] = eregi_replace ("[>].*$", "", $array[$i]);
112 }
40ee9452 113 return $array;
114 }
115
116 /** Returns a line of comma separated email addresses from an array **/
117 function getLineOfAddrs($array) {
b676ba7e 118 if (is_array($array)) {
119 $to_line = implode(", ", $array);
120 $to_line = trim(ereg_replace(",,+", ",", $to_line));
121 } else {
122 $to_line = "";
40ee9452 123 }
124 return $to_line;
125 }
7ce342dc 126
9eea179c 127 function translateText(&$body, $wrap_at, $charset) {
9297917e 128 global $where, $what; // from searching
129
8442ac08 130 if (!isset($url_parser_php)) {
131 include "../functions/url_parser.php";
132 }
133
a8648d75 134 $body_ary = explode("\n", $body);
8442ac08 135 for ($i=0; $i < count($body_ary); $i++) {
a8648d75 136 $line = $body_ary[$i];
17ce8467 137 $line = charset_decode($charset, $line);
e2ef6f4b 138 $line = str_replace("\t", ' ', $line);
9eea179c 139 chop($line);
8442ac08 140
a37f3771 141 if (strlen($line) - 2 >= $wrap_at) {
9eea179c 142 sqWordWrap($line, $wrap_at);
a37f3771 143 }
144
e2ef6f4b 145 $line = str_replace(' ', '&nbsp;', $line);
a8648d75 146 $line = nl2br($line);
147
9eea179c 148 parseUrl ($line);
e2ef6f4b 149
9eea179c 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) {
8442ac08 170 $line = "<FONT COLOR=FF0000>$line</FONT>\n";
9eea179c 171 } else if ($Quotes) {
8442ac08 172 $line = "<FONT COLOR=800000>$line</FONT>\n";
e2ef6f4b 173 }
174
175 if ($line)
176 {
177 $line = '<tt>' . $line . '</tt>';
178 }
179
180 $body_ary[$i] = $line . '<br>';
a8648d75 181 }
8442ac08 182 $body = implode("\n", $body_ary);
78509c54 183 }
184
7ce342dc 185 /* SquirrelMail version number -- DO NOT CHANGE */
7bfd4044 186 $version = "0.6pre1 (cvs)";
d29aac0e 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 }
1195c340 208
209 function get_location () {
210 # This determines the location to forward to relative
7e343a7d 211 # to your server. If this doesnt work correctly for
1195c340 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
00421cb6 219 global $PHP_SELF, $SERVER_NAME, $HTTPS, $HTTP_HOST, $SERVER_PORT;
1195c340 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
00421cb6 230 $port = "";
231 if (isset($SERVER_PORT)) {
232 if ($SERVER_PORT != 80) {
233 $port = sprintf(':%d', $SERVER_PORT);
2af48301 234 }
235 }
236
1195c340 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)) {
2af48301 241 $location = $proto . $HTTP_HOST . $port . $path;
1195c340 242 } else if(isset($SERVER_NAME) && !empty($SERVER_NAME)) {
2af48301 243 $location = $proto . $SERVER_NAME . $port . $path;
1195c340 244 } else {
245 $location = $path;
246 }
247 return $location;
248 }
7aaa81fc 249
250 function sqStripSlashes($string) {
251 if (get_magic_quotes_gpc()) {
252 $string = stripslashes($string);
253 }
254 return $string;
255 }
52eefafc 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
580e1793 278
dcaf2a49 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 }
580e1793 317
dcaf2a49 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();
52eefafc 357
358 for ($i = 0; $i < $length; $i++) {
dcaf2a49 359 $pad .= chr(mt_rand(0,255));
52eefafc 360 }
361
362 return $pad;
363 }
364
f79af863 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
b5afdff0 386 // Compare major version
f79af863 387 if($vmajor < $major) return false;
b5afdff0 388 if($vmajor > $major) return true;
389
390 // Major is the same. Compare minor
f79af863 391 if($vminor < $minor) return false;
b5afdff0 392 if($vminor > $minor) return true;
f79af863 393
b5afdff0 394 // Major and minor is the same as the required one.
f79af863 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
3302d0d4 409?>