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