Fix: php_self() not working right for Windoze servers that don't have REQUEST_URI
[squirrelmail.git] / functions / strings.php
CommitLineData
59177427 1<?php
7350889b 2
f1ca21bd 3/**
35586184 4 * strings.php
5 *
76911253 6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This code provides various string manipulation functions that are
10 * used by the rest of the Squirrelmail code.
11 *
12 * $Id$
13 */
9374671f 14
35586184 15/**
16 * SquirrelMail version number -- DO NOT CHANGE
17 */
18global $version;
87d8c725 19$version = '1.5.0 [CVS]';
d068c0ec 20
f1ca21bd 21/**
97bdc607 22 * SquirrelMail internal version number -- DO NOT CHANGE
23 * $sm_internal_version = array (release, major, minor)
24 */
0567e4d5 25global $SQM_INTERNAL_VERSION;
87d8c725 26$SQM_INTERNAL_VERSION = array(1,5,0);
27
87d8c725 28/**
29 * There can be a circular issue with includes, where the $version string is
30 * referenced by the include of global.php, etc. before it's defined.
31 * For that reason, bring in global.php AFTER we define the version strings.
32 */
33require_once(SM_PATH . 'functions/global.php');
97bdc607 34
66239b65 35/**
5cc0b70e 36 * Wraps text at $wrap characters
37 *
38 * Has a problem with special HTML characters, so call this before
39 * you do character translation.
40 *
41 * Specifically, &#039 comes up as 5 characters instead of 1.
42 * This should not add newlines to the end of lines.
43 */
44function sqWordWrap(&$line, $wrap) {
e842b215 45 global $languages, $squirrelmail_language;
46
47 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
48 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
49 if (mb_detect_encoding($line) != 'ASCII') {
50 $line = $languages[$squirrelmail_language]['XTRA_CODE']('wordwrap', $line, $wrap);
51 return;
52 }
53 }
54
5cc0b70e 55 ereg("^([\t >]*)([^\t >].*)?$", $line, $regs);
56 $beginning_spaces = $regs[1];
57 if (isset($regs[2])) {
58 $words = explode(' ', $regs[2]);
59 } else {
60 $words = '';
61 }
f1ca21bd 62
5cc0b70e 63 $i = 0;
64 $line = $beginning_spaces;
f1ca21bd 65
5cc0b70e 66 while ($i < count($words)) {
67 /* Force one word to be on a line (minimum) */
68 $line .= $words[$i];
69 $line_len = strlen($beginning_spaces) + strlen($words[$i]) + 2;
70 if (isset($words[$i + 1]))
71 $line_len += strlen($words[$i + 1]);
72 $i ++;
f1ca21bd 73
5cc0b70e 74 /* Add more words (as long as they fit) */
75 while ($line_len < $wrap && $i < count($words)) {
76 $line .= ' ' . $words[$i];
77 $i++;
78 if (isset($words[$i]))
79 $line_len += strlen($words[$i]) + 1;
80 else
81 $line_len += 1;
82 }
f1ca21bd 83
5cc0b70e 84 /* Skip spaces if they are the first thing on a continued line */
85 while (!isset($words[$i]) && $i < count($words)) {
86 $i ++;
87 }
f1ca21bd 88
5cc0b70e 89 /* Go to the next line if we have more to process */
90 if ($i < count($words)) {
e0858036 91 $line .= "\n";
5cc0b70e 92 }
93 }
94}
95
96/**
341abbd6 97 * Does the opposite of sqWordWrap()
98 */
99function sqUnWordWrap(&$body) {
e842b215 100 global $squirrelmail_language;
f1ca21bd 101
e842b215 102 if ($squirrelmail_language == 'ja_JP') {
103 return;
104 }
105
341abbd6 106 $lines = explode("\n", $body);
107 $body = '';
108 $PreviousSpaces = '';
109 $cnt = count($lines);
110 for ($i = 0; $i < $cnt; $i ++) {
111 preg_match("/^([\t >]*)([^\t >].*)?$/", $lines[$i], $regs);
112 $CurrentSpaces = $regs[1];
113 if (isset($regs[2])) {
114 $CurrentRest = $regs[2];
1e4a4feb 115 } else {
f1ca21bd 116 $CurrentRest = '';
117 }
118
341abbd6 119 if ($i == 0) {
120 $PreviousSpaces = $CurrentSpaces;
121 $body = $lines[$i];
122 } else if (($PreviousSpaces == $CurrentSpaces) /* Do the beginnings match */
123 && (strlen($lines[$i - 1]) > 65) /* Over 65 characters long */
124 && strlen($CurrentRest)) { /* and there's a line to continue with */
125 $body .= ' ' . $CurrentRest;
126 } else {
127 $body .= "\n" . $lines[$i];
128 $PreviousSpaces = $CurrentSpaces;
129 }
130 }
131 $body .= "\n";
132}
133
134/**
66239b65 135 * If $haystack is a full mailbox name and $needle is the mailbox
136 * separator character, returns the last part of the mailbox name.
137 */
138function readShortMailboxName($haystack, $needle) {
97b1248c 139
66239b65 140 if ($needle == '') {
97b1248c 141 $elem = $haystack;
142 } else {
f1ca21bd 143 $parts = explode($needle, $haystack);
144 $elem = array_pop($parts);
145 while ($elem == '' && count($parts)) {
146 $elem = array_pop($parts);
147 }
66239b65 148 }
97b1248c 149 return( $elem );
66239b65 150}
3302d0d4 151
43fdb2a4 152function php_self () {
961ca3d8 153 if ( sqgetGlobalVar('REQUEST_URI', $req_uri, SQ_SERVER) && !empty($req_uri) ) {
154 return $req_uri;
43fdb2a4 155 }
f1ca21bd 156
961ca3d8 157 if ( sqgetGlobalVar('PHP_SELF', $php_self, SQ_SERVER) && !empty($php_self) ) {
f72f61d8 158
159 // need to add query string to end of PHP_SELF to match REQUEST_URI
160 //
161 if ( sqgetGlobalVar('QUERY_STRING', $query_string, SQ_SERVER) && !empty($query_string) ) {
162 $php_self .= '?' . $query_string;
163 }
164
961ca3d8 165 return $php_self;
f1ca21bd 166 }
167
961ca3d8 168 return '';
43fdb2a4 169}
170
171
66239b65 172/**
173 * This determines the location to forward to relative to your server.
174 * If this doesnt work correctly for you (although it should), you can
175 * remove all this code except the last two lines, and change the header()
176 * function to look something like this, customized to the location of
177 * SquirrelMail on your server:
178 *
179 * http://www.myhost.com/squirrelmail/src/login.php
180 */
181function get_location () {
f1ca21bd 182
961ca3d8 183 global $imap_server_type;
238703be 184
4deb32f1 185 /* Get the path, handle virtual directories */
f1ca21bd 186 if(strpos(php_self(), '?')) {
187 $path = substr(php_self(), 0, strpos(php_self(), '?'));
188 } else {
189 $path = php_self();
190 }
191 $path = substr($path, 0, strrpos($path, '/'));
238703be 192 if ( sqgetGlobalVar('sq_base_url', $full_url, SQ_SESSION) ) {
193 return $full_url . $path;
194 }
195
66239b65 196 /* Check if this is a HTTPS or regular HTTP request. */
197 $proto = 'http://';
f1ca21bd 198
66239b65 199 /*
200 * If you have 'SSLOptions +StdEnvVars' in your apache config
44827b4d 201 * OR if you have HTTPS=on in your HTTP_SERVER_VARS
66239b65 202 * OR if you are on port 443
203 */
204 $getEnvVar = getenv('HTTPS');
205 if ((isset($getEnvVar) && !strcasecmp($getEnvVar, 'on')) ||
961ca3d8 206 (sqgetGlobalVar('HTTPS', $https_on, SQ_SERVER) && !strcasecmp($https_on, 'on')) ||
207 (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER) && $server_port == 443)) {
8a549df2 208 $proto = 'https://';
66239b65 209 }
f1ca21bd 210
4deb32f1 211 /* Get the hostname from the Host header or server config. */
961ca3d8 212 if ( !sqgetGlobalVar('HTTP_HOST', $host, SQ_SERVER) || empty($host) ) {
213 if ( !sqgetGlobalVar('SERVER_NAME', $host, SQ_SERVER) || empty($host) ) {
214 $host = '';
215 }
66239b65 216 }
f1ca21bd 217
66239b65 218 $port = '';
219 if (! strstr($host, ':')) {
961ca3d8 220 if (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER)) {
f1ca21bd 221 if (($server_port != 80 && $proto == 'http://') ||
961ca3d8 222 ($server_port != 443 && $proto == 'https://')) {
223 $port = sprintf(':%d', $server_port);
66239b65 224 }
225 }
226 }
f1ca21bd 227
8de7f698 228 /* this is a workaround for the weird macosx caching that
229 causes Apache to return 16080 as the port number, which causes
230 SM to bail */
f1ca21bd 231
8de7f698 232 if ($imap_server_type == 'macosx' && $port == ':16080') {
233 $port = '';
234 }
f1ca21bd 235
238703be 236 /* Fallback is to omit the server name and use a relative */
237 /* URI, although this is not RFC 2616 compliant. */
238 $full_url = ($host ? $proto . $host . $port : '');
239 sqsession_register($full_url, 'sq_base_url');
240 return $full_url . $path;
66239b65 241}
dcaf2a49 242
9374671f 243
66239b65 244/**
245 * These functions are used to encrypt the passowrd before it is
246 * stored in a cookie.
247 */
248function OneTimePadEncrypt ($string, $epad) {
249 $pad = base64_decode($epad);
250 $encrypted = '';
251 for ($i = 0; $i < strlen ($string); $i++) {
252 $encrypted .= chr (ord($string[$i]) ^ ord($pad[$i]));
253 }
f1ca21bd 254
66239b65 255 return base64_encode($encrypted);
256}
257
258function OneTimePadDecrypt ($string, $epad) {
259 $pad = base64_decode($epad);
260 $encrypted = base64_decode ($string);
261 $decrypted = '';
262 for ($i = 0; $i < strlen ($encrypted); $i++) {
263 $decrypted .= chr (ord($encrypted[$i]) ^ ord($pad[$i]));
264 }
f1ca21bd 265
66239b65 266 return $decrypted;
267}
9374671f 268
9374671f 269
66239b65 270/**
271 * Randomize the mt_rand() function. Toss this in strings or integers
272 * and it will seed the generator appropriately. With strings, it is
273 * better to get them long. Use md5() to lengthen smaller strings.
274 */
275function sq_mt_seed($Val) {
4deb32f1 276 /* if mt_getrandmax() does not return a 2^n - 1 number,
277 this might not work well. This uses $Max as a bitmask. */
66239b65 278 $Max = mt_getrandmax();
f1ca21bd 279
66239b65 280 if (! is_int($Val)) {
66239b65 281 $Val = crc32($Val);
66239b65 282 }
f1ca21bd 283
66239b65 284 if ($Val < 0) {
285 $Val *= -1;
286 }
f1ca21bd 287
66239b65 288 if ($Val = 0) {
289 return;
290 }
f1ca21bd 291
66239b65 292 mt_srand(($Val ^ mt_rand(0, $Max)) & $Max);
293}
9374671f 294
9374671f 295
66239b65 296/**
297 * This function initializes the random number generator fairly well.
298 * It also only initializes it once, so you don't accidentally get
299 * the same 'random' numbers twice in one session.
300 */
301function sq_mt_randomize() {
66239b65 302 static $randomized;
f1ca21bd 303
66239b65 304 if ($randomized) {
305 return;
306 }
f1ca21bd 307
66239b65 308 /* Global. */
961ca3d8 309 sqgetGlobalVar('REMOTE_PORT', $remote_port, SQ_SERVER);
310 sqgetGlobalVar('REMOTE_ADDR', $remote_addr, SQ_SERVER);
66239b65 311 sq_mt_seed((int)((double) microtime() * 1000000));
961ca3d8 312 sq_mt_seed(md5($remote_port . $remote_addr . getmypid()));
f1ca21bd 313
66239b65 314 /* getrusage */
315 if (function_exists('getrusage')) {
4deb32f1 316 /* Avoid warnings with Win32 */
66239b65 317 $dat = @getrusage();
318 if (isset($dat) && is_array($dat)) {
821a8e9c 319 $Str = '';
320 foreach ($dat as $k => $v)
66239b65 321 {
322 $Str .= $k . $v;
323 }
821a8e9c 324 sq_mt_seed(md5($Str));
66239b65 325 }
326 }
f1ca21bd 327
961ca3d8 328 if(sqgetGlobalVar('UNIQUE_ID', $unique_id, SQ_SERVER)) {
329 sq_mt_seed(md5($unique_id));
0b97a708 330 }
f1ca21bd 331
66239b65 332 $randomized = 1;
333}
334
335function OneTimePadCreate ($length=100) {
336 sq_mt_randomize();
f1ca21bd 337
66239b65 338 $pad = '';
339 for ($i = 0; $i < $length; $i++) {
340 $pad .= chr(mt_rand(0,255));
341 }
f1ca21bd 342
66239b65 343 return base64_encode($pad);
344}
9374671f 345
66239b65 346/**
66239b65 347 * Returns a string showing the size of the message/attachment.
348 */
349function show_readable_size($bytes) {
350 $bytes /= 1024;
351 $type = 'k';
f1ca21bd 352
66239b65 353 if ($bytes / 1024 > 1) {
354 $bytes /= 1024;
e5f1e71c 355 $type = 'M';
66239b65 356 }
f1ca21bd 357
66239b65 358 if ($bytes < 10) {
359 $bytes *= 10;
360 settype($bytes, 'integer');
361 $bytes /= 10;
362 } else {
363 settype($bytes, 'integer');
364 }
f1ca21bd 365
66239b65 366 return $bytes . '<small>&nbsp;' . $type . '</small>';
367}
9374671f 368
66239b65 369/**
370 * Generates a random string from the caracter set you pass in
371 *
372 * Flags:
373 * 1 = add lowercase a-z to $chars
374 * 2 = add uppercase A-Z to $chars
375 * 4 = add numbers 0-9 to $chars
376 */
9374671f 377
66239b65 378function GenerateRandomString($size, $chars, $flags = 0) {
379 if ($flags & 0x1) {
380 $chars .= 'abcdefghijklmnopqrstuvwxyz';
381 }
382 if ($flags & 0x2) {
383 $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
384 }
385 if ($flags & 0x4) {
386 $chars .= '0123456789';
387 }
f1ca21bd 388
66239b65 389 if (($size < 1) || (strlen($chars) < 1)) {
390 return '';
391 }
ff4f08ff 392
4deb32f1 393 sq_mt_randomize(); /* Initialize the random number generator */
ff4f08ff 394
4deb32f1 395 $String = '';
ff4f08ff 396 $j = strlen( $chars ) - 1;
66239b65 397 while (strlen($String) < $size) {
ff4f08ff 398 $String .= $chars{mt_rand(0, $j)};
66239b65 399 }
ff4f08ff 400
66239b65 401 return $String;
402}
9374671f 403
fbb76d0e 404function quoteimap($str) {
66239b65 405 return ereg_replace('(["\\])', '\\\\1', $str);
406}
1899535f 407
66239b65 408/**
409 * Trims every element in the array
410 */
411function TrimArray(&$array) {
412 foreach ($array as $k => $v) {
413 global $$k;
414 if (is_array($$k)) {
415 foreach ($$k as $k2 => $v2) {
416 $$k[$k2] = substr($v2, 1);
23d6bd09 417 }
66239b65 418 } else {
419 $$k = substr($v, 1);
23d6bd09 420 }
f1ca21bd 421
4deb32f1 422 /* Re-assign back to array. */
66239b65 423 $array[$k] = $$k;
424 }
f1ca21bd 425}
23d6bd09 426
f72f61d8 427// Returns a link to the compose-page, taking in
428// consideration the compose_in_new and javascript
429// settings.
430//
d62c4938 431function makeComposeLink($url, $text = null)
432{
433 global $compose_new_win,$javascript_on;
434
435 if(!$text) {
436 $text = _("Compose");
437 }
438
f72f61d8 439
440 // if not using "compose in new window", make
441 // regular link and be done with it
442 //
d62c4938 443 if($compose_new_win != '1') {
444 return makeInternalLink($url, $text, 'right');
445 }
446
f72f61d8 447
448 //
449 // build the compose in new window link...
450 //
451
452
453 // if javascript is on, use onClick event to handle it
454 //
d62c4938 455 if($javascript_on) {
456 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
457 return '<a href="javascript:void(0)" onclick="comp_in_new(\''.$base_uri.$url.'\')">'. $text.'</a>';
458 }
459
f72f61d8 460
461 // otherwise, just open new window using regular HTML
462 //
d62c4938 463 return makeInternalLink($url, $text, '_blank');
f72f61d8 464
d62c4938 465}
466
f1ca21bd 467/**
7fe09a30 468* sm_print_r($some_variable, [$some_other_variable [, ...]]);
50cc40fe 469* Debugging function - does the same as print_r, but makes sure special
470* characters are converted to htmlentities first. This will allow
471* values like <some@email.address> to be displayed.
472* The output is wrapped in <pre> and </pre> tags.
473*/
7fe09a30 474function sm_print_r() {
50cc40fe 475 ob_start(); // Buffer output
7fe09a30 476 foreach(func_get_args() as $var) {
477 print_r($var);
478 echo "\n";
479 }
50cc40fe 480 $buffer = ob_get_contents(); // Grab the print_r output
481 ob_end_clean(); // Silently discard the output & stop buffering
f1ca21bd 482 print '<pre>';
50cc40fe 483 print htmlentities($buffer);
f1ca21bd 484 print '</pre>';
50cc40fe 485}
486
43fdb2a4 487$PHP_SELF = php_self();
ef1932a4 488?>