Document the change in capabilities array
[squirrelmail.git] / functions / strings.php
CommitLineData
59177427 1<?php
7350889b 2
f1ca21bd 3/**
35586184 4 * strings.php
5 *
35586184 6 * This code provides various string manipulation functions that are
598294a7 7 * used by the rest of the SquirrelMail code.
35586184 8 *
4b4abf93 9 * @copyright &copy; 1999-2005 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
31841a9e 11 * @version $Id$
d6c32258 12 * @package squirrelmail
35586184 13 */
9374671f 14
31310ecd 15/** @ignore */
16if (!defined('SM_PATH')) define('SM_PATH','../');
17
35586184 18/**
19 * SquirrelMail version number -- DO NOT CHANGE
20 */
21global $version;
509fc585 22$version = '1.5.1 [CVS]';
d068c0ec 23
f1ca21bd 24/**
97bdc607 25 * SquirrelMail internal version number -- DO NOT CHANGE
26 * $sm_internal_version = array (release, major, minor)
27 */
0567e4d5 28global $SQM_INTERNAL_VERSION;
a959e855 29$SQM_INTERNAL_VERSION = array(1,5,1);
87d8c725 30
87d8c725 31/**
32 * There can be a circular issue with includes, where the $version string is
33 * referenced by the include of global.php, etc. before it's defined.
34 * For that reason, bring in global.php AFTER we define the version strings.
35 */
31310ecd 36include_once(SM_PATH . 'functions/global.php');
97bdc607 37
66239b65 38/**
c9d61baf 39 * Appends citation markers to the string.
40 * Also appends a trailing space.
41 *
42 * @author Justus Pendleton
31310ecd 43 * @param string $str The string to append to
44 * @param int $citeLevel the number of markers to append
c9d61baf 45 * @return null
31310ecd 46 * @since 1.5.1
c9d61baf 47 */
48function sqMakeCite (&$str, $citeLevel) {
49 for ($i = 0; $i < $citeLevel; $i++) {
50 $str .= '>';
51 }
52 if ($citeLevel != 0) {
53 $str .= ' ';
54 }
55}
56
57/**
58 * Create a newline in the string, adding citation
59 * markers to the newline as necessary.
60 *
61 * @author Justus Pendleton
31310ecd 62 * @param string $str the string to make a newline in
63 * @param int $citeLevel the citation level the newline is at
64 * @param int $column starting column of the newline
c9d61baf 65 * @return null
31310ecd 66 * @since 1.5.1
c9d61baf 67 */
68function sqMakeNewLine (&$str, $citeLevel, &$column) {
69 $str .= "\n";
70 $column = 0;
71 if ($citeLevel > 0) {
72 sqMakeCite ($str, $citeLevel);
73 $column = $citeLevel + 1;
74 } else {
75 $column = 0;
76 }
77}
78
79/**
5e7ae713 80 * Checks for spaces in strings - only used if PHP doesn't have native ctype support
81 *
326727cf 82 * You might be able to rewrite the function by adding short evaluation form.
5e7ae713 83 *
84 * possible problems:
85 * - iso-2022-xx charsets - hex 20 might be part of other symbol. I might
86 * be wrong. 0x20 is not used in iso-2022-jp. I haven't checked iso-2022-kr
87 * and iso-2022-cn mappings.
88 *
89 * - no-break space (&nbsp;) - it is 8bit symbol, that depends on charset.
90 * there are at least three different charset groups that have nbsp in
91 * different places.
92 *
93 * I don't see any charset/nbsp options in php ctype either.
94 *
95 * @param string $string tested string
326727cf 96 * @return bool true when only whitespace symbols are present in test string
31310ecd 97 * @since 1.5.1
5e7ae713 98 */
99function sm_ctype_space($string) {
c7aff938 100 if ( preg_match('/^[\x09-\x0D]|^\x20/', $string) || $string=='') {
101 return true;
102 } else {
103 return false;
104 }
5e7ae713 105}
106
107/**
c9d61baf 108 * Wraps text at $wrap characters. While sqWordWrap takes
109 * a single line of text and wraps it, this function works
110 * on the entire corpus at once, this allows it to be a little
111 * bit smarter and when and how to wrap.
112 *
113 * @author Justus Pendleton
31310ecd 114 * @param string $body the entire body of text
115 * @param int $wrap the maximum line length
c9d61baf 116 * @return string the wrapped text
31310ecd 117 * @since 1.5.1
c9d61baf 118 */
119function &sqBodyWrap (&$body, $wrap) {
5e7ae713 120 //check for ctype support, and fake it if it doesn't exist
121 if (!function_exists('ctype_space')) {
122 function ctype_space ($string) {
123 return sm_ctype_space($string);
124 }
125 }
126
c9d61baf 127 // the newly wrapped text
128 $outString = '';
129 // current column since the last newline in the outstring
130 $outStringCol = 0;
98abf408 131 $length = sq_strlen($body);
c9d61baf 132 // where we are in the original string
133 $pos = 0;
134 // the number of >>> citation markers we are currently at
135 $citeLevel = 0;
136
137 // the main loop, whenever we start a newline of input text
138 // we start from here
139 while ($pos < $length) {
140 // we're at the beginning of a line, get the new cite level
141 $newCiteLevel = 0;
142
98abf408 143 while (($pos < $length) && (sq_substr($body,$pos,1) == '>')) {
c9d61baf 144 $newCiteLevel++;
145 $pos++;
146
147 // skip over any spaces interleaved among the cite markers
98abf408 148 while (($pos < $length) && (sq_substr($body,$pos,1) == ' ')) {
bb977394 149
c9d61baf 150 $pos++;
bb977394 151
c9d61baf 152 }
153 if ($pos >= $length) {
154 break;
155 }
156 }
157
158 // special case: if this is a blank line then maintain it
159 // (i.e. try to preserve original paragraph breaks)
160 // unless they occur at the very beginning of the text
98abf408 161 if ((sq_substr($body,$pos,1) == "\n" ) && (sq_strlen($outString) != 0)) {
162 $outStringLast = $outString{sq_strlen($outString) - 1};
c9d61baf 163 if ($outStringLast != "\n") {
164 $outString .= "\n";
165 }
166 sqMakeCite ($outString, $newCiteLevel);
167 $outString .= "\n";
168 $pos++;
169 $outStringCol = 0;
170 continue;
171 }
172
173 // if the cite level has changed, then start a new line
174 // with the new cite level.
175 if (($citeLevel != $newCiteLevel) && ($pos > ($newCiteLevel + 1)) && ($outStringCol != 0)) {
176 sqMakeNewLine ($outString, 0, $outStringCol);
177 }
178
179 $citeLevel = $newCiteLevel;
180
181 // prepend the quote level if necessary
182 if ($outStringCol == 0) {
183 sqMakeCite ($outString, $citeLevel);
184 // if we added a citation then move the column
185 // out by citelevel + 1 (the cite markers + the space)
186 $outStringCol = $citeLevel + ($citeLevel ? 1 : 0);
187 } else if ($outStringCol > $citeLevel) {
188 // not a cite and we're not at the beginning of a line
189 // in the output. add a space to separate the new text
190 // from previous text.
191 $outString .= ' ';
192 $outStringCol++;
193 }
194
195 // find the next newline -- we don't want to go further than that
98abf408 196 $nextNewline = sq_strpos ($body, "\n", $pos);
c9d61baf 197 if ($nextNewline === FALSE) {
198 $nextNewline = $length;
199 }
200
201 // Don't wrap unquoted lines at all. For now the textarea
202 // will work fine for this. Maybe revisit this later though
203 // (for completeness more than anything else, I think)
204 if ($citeLevel == 0) {
98abf408 205 $outString .= sq_substr ($body, $pos, ($nextNewline - $pos));
c9d61baf 206 $outStringCol = $nextNewline - $pos;
207 if ($nextNewline != $length) {
208 sqMakeNewLine ($outString, 0, $outStringCol);
209 }
210 $pos = $nextNewline + 1;
211 continue;
212 }
bb977394 213 /**
214 * Set this to false to stop appending short strings to previous lines
215 */
216 $smartwrap = true;
c9d61baf 217 // inner loop, (obviously) handles wrapping up to
218 // the next newline
219 while ($pos < $nextNewline) {
220 // skip over initial spaces
98abf408 221 while (($pos < $nextNewline) && (ctype_space (sq_substr($body,$pos,1)))) {
c9d61baf 222 $pos++;
223 }
c9d61baf 224 // if this is a short line then just append it and continue outer loop
6eaf5320 225 if (($outStringCol + $nextNewline - $pos) <= ($wrap - $citeLevel - 1) ) {
c9d61baf 226 // if this is the final line in the input string then include
227 // any trailing newlines
6eaf5320 228 // echo substr($body,$pos,$wrap). "<br />";
98abf408 229 if (($nextNewline + 1 == $length) && (sq_substr($body,$nextNewline,1) == "\n")) {
c9d61baf 230 $nextNewline++;
231 }
232
bb977394 233 // trim trailing spaces
234 $lastRealChar = $nextNewline;
98abf408 235 while (($lastRealChar > $pos && $lastRealChar < $length) && (ctype_space (sq_substr($body,$lastRealChar,1)))) {
bb977394 236 $lastRealChar--;
237 }
bb977394 238 // decide if appending the short string is what we want
98abf408 239 if (($nextNewline < $length && sq_substr($body,$nextNewline,1) == "\n") &&
bb977394 240 isset($lastRealChar)) {
be86a35a 241 $mypos = $pos;
242 //check the first word:
98abf408 243 while (($mypos < $length) && (sq_substr($body,$mypos,1) == '>')) {
bb977394 244 $mypos++;
245 // skip over any spaces interleaved among the cite markers
98abf408 246 while (($mypos < $length) && (sq_substr($body,$mypos,1) == ' ')) {
6eaf5320 247 $mypos++;
bb977394 248 }
249 }
250/*
251 $ldnspacecnt = 0;
252 if ($mypos == $nextNewline+1) {
253 while (($mypos < $length) && ($body{$mypos} == ' ')) {
254 $ldnspacecnt++;
6eaf5320 255 }
bb977394 256 }
257*/
258
98abf408 259 $firstword = sq_substr($body,$mypos,sq_strpos($body,' ',$mypos) - $mypos);
bb977394 260 //if ($dowrap || $ldnspacecnt > 1 || ($firstword && (
261 if (!$smartwrap || $firstword && (
262 $firstword{0} == '-' ||
6eaf5320 263 $firstword{0} == '+' ||
264 $firstword{0} == '*' ||
98abf408 265 sq_substr($firstword,0,1) == sq_strtoupper(sq_substr($firstword,0,1)) ||
6eaf5320 266 strpos($firstword,':'))) {
98abf408 267 $outString .= sq_substr($body,$pos,($lastRealChar - $pos+1));
bb977394 268 $outStringCol += ($lastRealChar - $pos);
269 sqMakeNewLine($outString,$citeLevel,$outStringCol);
270 $nextNewline++;
271 $pos = $nextNewline;
272 $outStringCol--;
273 continue;
274 }
6eaf5320 275
c9d61baf 276 }
bb977394 277
98abf408 278 $outString .= sq_substr ($body, $pos, ($lastRealChar - $pos + 1));
c9d61baf 279 $outStringCol += ($lastRealChar - $pos);
280 $pos = $nextNewline + 1;
281 continue;
282 }
bb977394 283
c9d61baf 284 $eol = $pos + $wrap - $citeLevel - $outStringCol;
285 // eol is the tentative end of line.
286 // look backwards for there for a whitespace to break at.
287 // if it's already less than our current position then
288 // our current line is already too long, break immediately
289 // and restart outer loop
290 if ($eol <= $pos) {
6eaf5320 291 sqMakeNewLine ($outString, $citeLevel, $outStringCol);
c9d61baf 292 continue;
293 }
294
295 // start looking backwards for whitespace to break at.
296 $breakPoint = $eol;
98abf408 297 while (($breakPoint > $pos) && (! ctype_space (sq_substr($body,$breakPoint,1)))) {
c9d61baf 298 $breakPoint--;
299 }
300
301 // if we didn't find a breakpoint by looking backward then we
302 // need to figure out what to do about that
303 if ($breakPoint == $pos) {
304 // if we are not at the beginning then end this line
305 // and start a new loop
306 if ($outStringCol > ($citeLevel + 1)) {
307 sqMakeNewLine ($outString, $citeLevel, $outStringCol);
308 continue;
309 } else {
310 // just hard break here. most likely we are breaking
311 // a really long URL. could also try searching
312 // forward for a break point, which is what Mozilla
313 // does. don't bother for now.
314 $breakPoint = $eol;
315 }
316 }
317
318 // special case: maybe we should have wrapped last
319 // time. if the first breakpoint here makes the
320 // current line too long and there is already text on
321 // the current line, break and loop again if at
322 // beginning of current line, don't force break
323 $SLOP = 6;
324 if ((($outStringCol + ($breakPoint - $pos)) > ($wrap + $SLOP)) && ($outStringCol > ($citeLevel + 1))) {
325 sqMakeNewLine ($outString, $citeLevel, $outStringCol);
326 continue;
327 }
328
329 // skip newlines or whitespace at the beginning of the string
98abf408 330 $substring = sq_substr ($body, $pos, ($breakPoint - $pos));
c9d61baf 331 $substring = rtrim ($substring); // do rtrim and ctype_space have the same ideas about whitespace?
332 $outString .= $substring;
98abf408 333 $outStringCol += sq_strlen ($substring);
c9d61baf 334 // advance past the whitespace which caused the wrap
335 $pos = $breakPoint;
98abf408 336 while (($pos < $length) && (ctype_space (sq_substr($body,$pos,1)))) {
c9d61baf 337 $pos++;
338 }
339 if ($pos < $length) {
340 sqMakeNewLine ($outString, $citeLevel, $outStringCol);
341 }
342 }
343 }
344
345 return $outString;
346}
347
348/**
5cc0b70e 349 * Wraps text at $wrap characters
350 *
351 * Has a problem with special HTML characters, so call this before
352 * you do character translation.
353 *
17886554 354 * Specifically, &amp;#039; comes up as 5 characters instead of 1.
5cc0b70e 355 * This should not add newlines to the end of lines.
8b096f0a 356 *
31310ecd 357 * @param string $line the line of text to wrap, by ref
358 * @param int $wrap the maximum line lenth
359 * @param string $charset name of charset used in $line string. Available since v.1.5.1.
8b096f0a 360 * @return void
31310ecd 361 * @since 1.0
5cc0b70e 362 */
c7aff938 363function sqWordWrap(&$line, $wrap, $charset='') {
e842b215 364 global $languages, $squirrelmail_language;
365
17886554 366 // Use custom wrapping function, if translation provides it
e842b215 367 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
1b45fe31 368 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_wordwrap')) {
e842b215 369 if (mb_detect_encoding($line) != 'ASCII') {
1b45fe31 370 $line = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_wordwrap', $line, $wrap);
e842b215 371 return;
372 }
373 }
374
5cc0b70e 375 ereg("^([\t >]*)([^\t >].*)?$", $line, $regs);
376 $beginning_spaces = $regs[1];
377 if (isset($regs[2])) {
378 $words = explode(' ', $regs[2]);
379 } else {
380 $words = '';
381 }
f1ca21bd 382
5cc0b70e 383 $i = 0;
384 $line = $beginning_spaces;
f1ca21bd 385
5cc0b70e 386 while ($i < count($words)) {
387 /* Force one word to be on a line (minimum) */
388 $line .= $words[$i];
c7aff938 389 $line_len = strlen($beginning_spaces) + sq_strlen($words[$i],$charset) + 2;
5cc0b70e 390 if (isset($words[$i + 1]))
c7aff938 391 $line_len += sq_strlen($words[$i + 1],$charset);
5cc0b70e 392 $i ++;
f1ca21bd 393
5cc0b70e 394 /* Add more words (as long as they fit) */
395 while ($line_len < $wrap && $i < count($words)) {
396 $line .= ' ' . $words[$i];
397 $i++;
398 if (isset($words[$i]))
c7aff938 399 $line_len += sq_strlen($words[$i],$charset) + 1;
5cc0b70e 400 else
401 $line_len += 1;
402 }
f1ca21bd 403
5cc0b70e 404 /* Skip spaces if they are the first thing on a continued line */
405 while (!isset($words[$i]) && $i < count($words)) {
406 $i ++;
407 }
f1ca21bd 408
5cc0b70e 409 /* Go to the next line if we have more to process */
410 if ($i < count($words)) {
e0858036 411 $line .= "\n";
5cc0b70e 412 }
413 }
414}
415
416/**
341abbd6 417 * Does the opposite of sqWordWrap()
31310ecd 418 * @param string $body the text to un-wordwrap
8b096f0a 419 * @return void
31310ecd 420 * @since 1.0
341abbd6 421 */
422function sqUnWordWrap(&$body) {
e842b215 423 global $squirrelmail_language;
f1ca21bd 424
e842b215 425 if ($squirrelmail_language == 'ja_JP') {
426 return;
427 }
428
341abbd6 429 $lines = explode("\n", $body);
430 $body = '';
431 $PreviousSpaces = '';
432 $cnt = count($lines);
433 for ($i = 0; $i < $cnt; $i ++) {
434 preg_match("/^([\t >]*)([^\t >].*)?$/", $lines[$i], $regs);
435 $CurrentSpaces = $regs[1];
436 if (isset($regs[2])) {
437 $CurrentRest = $regs[2];
1e4a4feb 438 } else {
f1ca21bd 439 $CurrentRest = '';
440 }
441
341abbd6 442 if ($i == 0) {
443 $PreviousSpaces = $CurrentSpaces;
444 $body = $lines[$i];
445 } else if (($PreviousSpaces == $CurrentSpaces) /* Do the beginnings match */
446 && (strlen($lines[$i - 1]) > 65) /* Over 65 characters long */
447 && strlen($CurrentRest)) { /* and there's a line to continue with */
448 $body .= ' ' . $CurrentRest;
449 } else {
450 $body .= "\n" . $lines[$i];
451 $PreviousSpaces = $CurrentSpaces;
452 }
453 }
454 $body .= "\n";
455}
456
457/**
66239b65 458 * If $haystack is a full mailbox name and $needle is the mailbox
459 * separator character, returns the last part of the mailbox name.
8b096f0a 460 *
461 * @param string haystack full mailbox name to search
462 * @param string needle the mailbox separator character
463 * @return string the last part of the mailbox name
31310ecd 464 * @since 1.0
66239b65 465 */
466function readShortMailboxName($haystack, $needle) {
97b1248c 467
66239b65 468 if ($needle == '') {
97b1248c 469 $elem = $haystack;
470 } else {
f1ca21bd 471 $parts = explode($needle, $haystack);
472 $elem = array_pop($parts);
473 while ($elem == '' && count($parts)) {
474 $elem = array_pop($parts);
475 }
66239b65 476 }
97b1248c 477 return( $elem );
66239b65 478}
3302d0d4 479
8b096f0a 480/**
4445e6b3 481 * get_location
482 *
8b096f0a 483 * Determines the location to forward to, relative to your server.
484 * This is used in HTTP Location: redirects.
66239b65 485 * If this doesnt work correctly for you (although it should), you can
8b096f0a 486 * remove all this code except the last two lines, and have it return
487 * the right URL for your site, something like:
488 *
489 * http://www.example.com/squirrelmail/
66239b65 490 *
8b096f0a 491 * @return string the base url for this SquirrelMail installation
31310ecd 492 * @since 1.0
66239b65 493 */
494function get_location () {
f1ca21bd 495
961ca3d8 496 global $imap_server_type;
238703be 497
4deb32f1 498 /* Get the path, handle virtual directories */
f1ca21bd 499 if(strpos(php_self(), '?')) {
500 $path = substr(php_self(), 0, strpos(php_self(), '?'));
501 } else {
502 $path = php_self();
503 }
504 $path = substr($path, 0, strrpos($path, '/'));
238703be 505 if ( sqgetGlobalVar('sq_base_url', $full_url, SQ_SESSION) ) {
506 return $full_url . $path;
507 }
508
66239b65 509 /* Check if this is a HTTPS or regular HTTP request. */
510 $proto = 'http://';
f1ca21bd 511
66239b65 512 /*
513 * If you have 'SSLOptions +StdEnvVars' in your apache config
44827b4d 514 * OR if you have HTTPS=on in your HTTP_SERVER_VARS
66239b65 515 * OR if you are on port 443
516 */
517 $getEnvVar = getenv('HTTPS');
518 if ((isset($getEnvVar) && !strcasecmp($getEnvVar, 'on')) ||
961ca3d8 519 (sqgetGlobalVar('HTTPS', $https_on, SQ_SERVER) && !strcasecmp($https_on, 'on')) ||
520 (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER) && $server_port == 443)) {
8a549df2 521 $proto = 'https://';
66239b65 522 }
f1ca21bd 523
4deb32f1 524 /* Get the hostname from the Host header or server config. */
961ca3d8 525 if ( !sqgetGlobalVar('HTTP_HOST', $host, SQ_SERVER) || empty($host) ) {
526 if ( !sqgetGlobalVar('SERVER_NAME', $host, SQ_SERVER) || empty($host) ) {
527 $host = '';
528 }
66239b65 529 }
f1ca21bd 530
66239b65 531 $port = '';
532 if (! strstr($host, ':')) {
961ca3d8 533 if (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER)) {
f1ca21bd 534 if (($server_port != 80 && $proto == 'http://') ||
961ca3d8 535 ($server_port != 443 && $proto == 'https://')) {
536 $port = sprintf(':%d', $server_port);
66239b65 537 }
538 }
539 }
f1ca21bd 540
8de7f698 541 /* this is a workaround for the weird macosx caching that
542 causes Apache to return 16080 as the port number, which causes
543 SM to bail */
f1ca21bd 544
8de7f698 545 if ($imap_server_type == 'macosx' && $port == ':16080') {
546 $port = '';
547 }
f1ca21bd 548
238703be 549 /* Fallback is to omit the server name and use a relative */
550 /* URI, although this is not RFC 2616 compliant. */
551 $full_url = ($host ? $proto . $host . $port : '');
552 sqsession_register($full_url, 'sq_base_url');
553 return $full_url . $path;
66239b65 554}
dcaf2a49 555
9374671f 556
66239b65 557/**
4445e6b3 558 * Encrypts password
559 *
8b096f0a 560 * These functions are used to encrypt the password before it is
561 * stored in a cookie. The encryption key is generated by
562 * OneTimePadCreate();
563 *
31310ecd 564 * @param string $string the (password)string to encrypt
565 * @param string $epad the encryption key
8b096f0a 566 * @return string the base64-encoded encrypted password
31310ecd 567 * @since 1.0
66239b65 568 */
569function OneTimePadEncrypt ($string, $epad) {
570 $pad = base64_decode($epad);
432db2fc 571
572 if (strlen($pad)>0) {
573 // make sure that pad is longer than string
574 while (strlen($string)>strlen($pad)) {
575 $pad.=$pad;
576 }
577 } else {
578 // FIXME: what should we do when $epad is not base64 encoded or empty.
579 }
580
66239b65 581 $encrypted = '';
582 for ($i = 0; $i < strlen ($string); $i++) {
583 $encrypted .= chr (ord($string[$i]) ^ ord($pad[$i]));
584 }
f1ca21bd 585
66239b65 586 return base64_encode($encrypted);
587}
588
8b096f0a 589/**
4445e6b3 590 * Decrypts a password from the cookie
591 *
592 * Decrypts a password from the cookie, encrypted by OneTimePadEncrypt.
8b096f0a 593 * This uses the encryption key that is stored in the session.
594 *
31310ecd 595 * @param string $string the string to decrypt
596 * @param string $epad the encryption key from the session
8b096f0a 597 * @return string the decrypted password
31310ecd 598 * @since 1.0
8b096f0a 599 */
66239b65 600function OneTimePadDecrypt ($string, $epad) {
601 $pad = base64_decode($epad);
432db2fc 602
603 if (strlen($pad)>0) {
604 // make sure that pad is longer than string
605 while (strlen($string)>strlen($pad)) {
606 $pad.=$pad;
607 }
608 } else {
609 // FIXME: what should we do when $epad is not base64 encoded or empty.
610 }
611
66239b65 612 $encrypted = base64_decode ($string);
613 $decrypted = '';
614 for ($i = 0; $i < strlen ($encrypted); $i++) {
615 $decrypted .= chr (ord($encrypted[$i]) ^ ord($pad[$i]));
616 }
f1ca21bd 617
66239b65 618 return $decrypted;
619}
9374671f 620
9374671f 621
66239b65 622/**
4445e6b3 623 * Randomizes the mt_rand() function.
624 *
c9d61baf 625 * Toss this in strings or integers and it will seed the generator
626 * appropriately. With strings, it is better to get them long.
4445e6b3 627 * Use md5() to lengthen smaller strings.
8b096f0a 628 *
31310ecd 629 * @param mixed $val a value to seed the random number generator. mixed = integer or string.
8b096f0a 630 * @return void
31310ecd 631 * @since 1.0
66239b65 632 */
633function sq_mt_seed($Val) {
4deb32f1 634 /* if mt_getrandmax() does not return a 2^n - 1 number,
635 this might not work well. This uses $Max as a bitmask. */
66239b65 636 $Max = mt_getrandmax();
f1ca21bd 637
66239b65 638 if (! is_int($Val)) {
66239b65 639 $Val = crc32($Val);
66239b65 640 }
f1ca21bd 641
66239b65 642 if ($Val < 0) {
643 $Val *= -1;
644 }
f1ca21bd 645
8d2155e5 646 if ($Val == 0) {
66239b65 647 return;
648 }
f1ca21bd 649
66239b65 650 mt_srand(($Val ^ mt_rand(0, $Max)) & $Max);
651}
9374671f 652
9374671f 653
66239b65 654/**
4445e6b3 655 * Init random number generator
656 *
66239b65 657 * This function initializes the random number generator fairly well.
658 * It also only initializes it once, so you don't accidentally get
659 * the same 'random' numbers twice in one session.
8b096f0a 660 *
661 * @return void
31310ecd 662 * @since 1.0
66239b65 663 */
664function sq_mt_randomize() {
66239b65 665 static $randomized;
f1ca21bd 666
66239b65 667 if ($randomized) {
668 return;
669 }
f1ca21bd 670
66239b65 671 /* Global. */
961ca3d8 672 sqgetGlobalVar('REMOTE_PORT', $remote_port, SQ_SERVER);
673 sqgetGlobalVar('REMOTE_ADDR', $remote_addr, SQ_SERVER);
66239b65 674 sq_mt_seed((int)((double) microtime() * 1000000));
961ca3d8 675 sq_mt_seed(md5($remote_port . $remote_addr . getmypid()));
f1ca21bd 676
66239b65 677 /* getrusage */
678 if (function_exists('getrusage')) {
4deb32f1 679 /* Avoid warnings with Win32 */
66239b65 680 $dat = @getrusage();
681 if (isset($dat) && is_array($dat)) {
821a8e9c 682 $Str = '';
683 foreach ($dat as $k => $v)
66239b65 684 {
685 $Str .= $k . $v;
686 }
821a8e9c 687 sq_mt_seed(md5($Str));
66239b65 688 }
689 }
f1ca21bd 690
961ca3d8 691 if(sqgetGlobalVar('UNIQUE_ID', $unique_id, SQ_SERVER)) {
692 sq_mt_seed(md5($unique_id));
0b97a708 693 }
f1ca21bd 694
66239b65 695 $randomized = 1;
696}
697
8b096f0a 698/**
4445e6b3 699 * Creates encryption key
700 *
8b096f0a 701 * Creates an encryption key for encrypting the password stored in the cookie.
702 * The encryption key itself is stored in the session.
703 *
31310ecd 704 * Pad must be longer or equal to encoded string length in 1.4.4/1.5.0 and older.
705 * @param int $length optional, length of the string to generate
8b096f0a 706 * @return string the encryption key
31310ecd 707 * @since 1.0
8b096f0a 708 */
66239b65 709function OneTimePadCreate ($length=100) {
710 sq_mt_randomize();
f1ca21bd 711
66239b65 712 $pad = '';
713 for ($i = 0; $i < $length; $i++) {
714 $pad .= chr(mt_rand(0,255));
715 }
f1ca21bd 716
66239b65 717 return base64_encode($pad);
718}
9374671f 719
66239b65 720/**
8b096f0a 721 * Returns a string showing the size of the message/attachment.
722 *
31310ecd 723 * @param int $bytes the filesize in bytes
8b096f0a 724 * @return string the filesize in human readable format
31310ecd 725 * @since 1.0
66239b65 726 */
727function show_readable_size($bytes) {
728 $bytes /= 1024;
729 $type = 'k';
f1ca21bd 730
66239b65 731 if ($bytes / 1024 > 1) {
732 $bytes /= 1024;
e5f1e71c 733 $type = 'M';
66239b65 734 }
f1ca21bd 735
66239b65 736 if ($bytes < 10) {
737 $bytes *= 10;
738 settype($bytes, 'integer');
739 $bytes /= 10;
740 } else {
741 settype($bytes, 'integer');
742 }
f1ca21bd 743
91c27aee 744 return $bytes . '&nbsp;' . $type;
66239b65 745}
9374671f 746
66239b65 747/**
c7aff938 748 * Generates a random string from the character set you pass in
66239b65 749 *
31310ecd 750 * @param int $size the length of the string to generate
751 * @param string $chars a string containing the characters to use
752 * @param int $flags a flag to add a specific set to the characters to use:
8b096f0a 753 * Flags:
754 * 1 = add lowercase a-z to $chars
755 * 2 = add uppercase A-Z to $chars
756 * 4 = add numbers 0-9 to $chars
757 * @return string the random string
31310ecd 758 * @since 1.0
66239b65 759 */
66239b65 760function GenerateRandomString($size, $chars, $flags = 0) {
761 if ($flags & 0x1) {
762 $chars .= 'abcdefghijklmnopqrstuvwxyz';
763 }
764 if ($flags & 0x2) {
765 $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
766 }
767 if ($flags & 0x4) {
768 $chars .= '0123456789';
769 }
f1ca21bd 770
66239b65 771 if (($size < 1) || (strlen($chars) < 1)) {
772 return '';
773 }
ff4f08ff 774
4deb32f1 775 sq_mt_randomize(); /* Initialize the random number generator */
ff4f08ff 776
4deb32f1 777 $String = '';
ff4f08ff 778 $j = strlen( $chars ) - 1;
66239b65 779 while (strlen($String) < $size) {
ff4f08ff 780 $String .= $chars{mt_rand(0, $j)};
66239b65 781 }
ff4f08ff 782
66239b65 783 return $String;
784}
9374671f 785
8b096f0a 786/**
787 * Escapes special characters for use in IMAP commands.
4445e6b3 788 *
31310ecd 789 * @param string $str the string to escape
8b096f0a 790 * @return string the escaped string
31310ecd 791 * @since 1.0.3
8b096f0a 792 */
fbb76d0e 793function quoteimap($str) {
ab1df059 794 return preg_replace("/([\"\\\\])/", "\\\\$1", $str);
66239b65 795}
1899535f 796
66239b65 797/**
4445e6b3 798 * Trims array
799 *
8b096f0a 800 * Trims every element in the array, ie. remove the first char of each element
31310ecd 801 * @param array $array the array to trim
802 * @since 1.2.0
66239b65 803 */
804function TrimArray(&$array) {
805 foreach ($array as $k => $v) {
806 global $$k;
807 if (is_array($$k)) {
808 foreach ($$k as $k2 => $v2) {
809 $$k[$k2] = substr($v2, 1);
23d6bd09 810 }
66239b65 811 } else {
812 $$k = substr($v, 1);
23d6bd09 813 }
f1ca21bd 814
4deb32f1 815 /* Re-assign back to array. */
66239b65 816 $array[$k] = $$k;
817 }
f1ca21bd 818}
23d6bd09 819
8b096f0a 820/**
4445e6b3 821 * Create compose link
822 *
8b096f0a 823 * Returns a link to the compose-page, taking in consideration
824 * the compose_in_new and javascript settings.
31310ecd 825 * @param string $url the URL to the compose page
826 * @param string $text the link text, default "Compose"
827 * @param string $target (since 1.4.3) url target
8b096f0a 828 * @return string a link to the compose page
31310ecd 829 * @since 1.4.2
8b096f0a 830 */
d0b119a5 831function makeComposeLink($url, $text = null, $target='') {
832 global $compose_new_win,$javascript_on, $compose_width, $compose_height;
d62c4938 833
834 if(!$text) {
835 $text = _("Compose");
836 }
837
c9d61baf 838 // if not using "compose in new window", make
f72f61d8 839 // regular link and be done with it
d62c4938 840 if($compose_new_win != '1') {
21a957a9 841 return makeInternalLink($url, $text, $target);
d62c4938 842 }
843
c9d61baf 844 // build the compose in new window link...
f72f61d8 845
846
c435f076 847 // if javascript is on, use onclick event to handle it
d62c4938 848 if($javascript_on) {
849 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
d0b119a5 850 $compuri = $base_uri.$url;
851 return "<a href=\"javascript:void(0)\" onclick=\"comp_in_new('$compuri','$compose_width','$compose_height')\">$text</a>";
d62c4938 852 }
853
f72f61d8 854 // otherwise, just open new window using regular HTML
d62c4938 855 return makeInternalLink($url, $text, '_blank');
856}
857
f1ca21bd 858/**
4445e6b3 859 * Print variable
860 *
8b096f0a 861 * sm_print_r($some_variable, [$some_other_variable [, ...]]);
4445e6b3 862 *
8b096f0a 863 * Debugging function - does the same as print_r, but makes sure special
864 * characters are converted to htmlentities first. This will allow
865 * values like <some@email.address> to be displayed.
4445e6b3 866 * The output is wrapped in <<pre>> and <</pre>> tags.
69567a46 867 * Since 1.4.2 accepts unlimited number of arguments.
868 * @since 1.4.1
8b096f0a 869 * @return void
870 */
7fe09a30 871function sm_print_r() {
50cc40fe 872 ob_start(); // Buffer output
7fe09a30 873 foreach(func_get_args() as $var) {
874 print_r($var);
875 echo "\n";
3896acce 876 // php has get_class_methods function that can print class methods
877 if (is_object($var)) {
878 // get class methods if $var is object
879 $aMethods=get_class_methods(get_class($var));
351ee7ca 880 // make sure that $aMethods is array and array is not empty
881 if (is_array($aMethods) && $aMethods!=array()) {
3896acce 882 echo "Object methods:\n";
883 foreach($aMethods as $method) {
884 echo '* ' . $method . "\n";
885 }
886 }
887 echo "\n";
888 }
7fe09a30 889 }
50cc40fe 890 $buffer = ob_get_contents(); // Grab the print_r output
891 ob_end_clean(); // Silently discard the output & stop buffering
7297873e 892 print '<div align="left"><pre>';
50cc40fe 893 print htmlentities($buffer);
7297873e 894 print '</pre></div>';
50cc40fe 895}
896
3ecad5e6 897/**
898 * version of fwrite which checks for failure
31310ecd 899 * @param resource $fp
900 * @param string $string
901 * @return number of written bytes. false on failure
902 * @since 1.4.3
3ecad5e6 903 */
904function sq_fwrite($fp, $string) {
c9d61baf 905 // write to file
906 $count = @fwrite($fp,$string);
907 // the number of bytes written should be the length of the string
908 if($count != strlen($string)) {
909 return FALSE;
910 }
911
912 return $count;
3ecad5e6 913}
914
36e1180b 915/**
916 * sq_get_html_translation_table
917 *
918 * Returns the translation table used by sq_htmlentities()
919 *
920 * @param integer $table html translation table. Possible values (without quotes):
deb22cec 921 * <ul>
922 * <li>HTML_ENTITIES - full html entities table defined by charset</li>
923 * <li>HTML_SPECIALCHARS - html special characters table</li>
924 * </ul>
36e1180b 925 * @param integer $quote_style quote encoding style. Possible values (without quotes):
c9d61baf 926 * <ul>
deb22cec 927 * <li>ENT_COMPAT - (default) encode double quotes</li>
928 * <li>ENT_NOQUOTES - don't encode double or single quotes</li>
929 * <li>ENT_QUOTES - encode double and single quotes</li>
c9d61baf 930 * </ul>
36e1180b 931 * @param string $charset charset used for encoding. default to us-ascii, 'auto' uses $default_charset global value.
932 * @return array html translation array
31310ecd 933 * @since 1.5.1
36e1180b 934 */
935function sq_get_html_translation_table($table,$quote_style=ENT_COMPAT,$charset='us-ascii') {
936 global $default_charset;
937
938 if ($table == HTML_SPECIALCHARS) $charset='us-ascii';
939
940 // Start array with ampersand
941 $sq_html_ent_table = array( "&" => '&amp;' );
942
943 // < and >
944 $sq_html_ent_table = array_merge($sq_html_ent_table,
c9d61baf 945 array("<" => '&lt;',
946 ">" => '&gt;')
947 );
36e1180b 948 // double quotes
949 if ($quote_style == ENT_COMPAT)
950 $sq_html_ent_table = array_merge($sq_html_ent_table,
c9d61baf 951 array("\"" => '&quot;')
952 );
36e1180b 953
954 // double and single quotes
955 if ($quote_style == ENT_QUOTES)
956 $sq_html_ent_table = array_merge($sq_html_ent_table,
c9d61baf 957 array("\"" => '&quot;',
958 "'" => '&#39;')
959 );
36e1180b 960
961 if ($charset=='auto') $charset=$default_charset;
962
963 // add entities that depend on charset
964 switch($charset){
965 case 'iso-8859-1':
966 include_once(SM_PATH . 'functions/htmlentities/iso-8859-1.php');
967 break;
968 case 'utf-8':
969 include_once(SM_PATH . 'functions/htmlentities/utf-8.php');
970 break;
971 case 'us-ascii':
972 default:
973 break;
974 }
975 // return table
976 return $sq_html_ent_table;
977}
978
979/**
980 * sq_htmlentities
981 *
982 * Convert all applicable characters to HTML entities.
17886554 983 * Minimal php requirement - v.4.0.5.
984 *
985 * Function is designed for people that want to use full power of htmlentities() in
986 * i18n environment.
36e1180b 987 *
988 * @param string $string string that has to be sanitized
989 * @param integer $quote_style quote encoding style. Possible values (without quotes):
c9d61baf 990 * <ul>
deb22cec 991 * <li>ENT_COMPAT - (default) encode double quotes</li>
992 * <li>ENT_NOQUOTES - don't encode double or single quotes</li>
993 * <li>ENT_QUOTES - encode double and single quotes</li>
c9d61baf 994 * </ul>
36e1180b 995 * @param string $charset charset used for encoding. defaults to 'us-ascii', 'auto' uses $default_charset global value.
996 * @return string sanitized string
31310ecd 997 * @since 1.5.1
36e1180b 998 */
999function sq_htmlentities($string,$quote_style=ENT_COMPAT,$charset='us-ascii') {
1000 // get translation table
1001 $sq_html_ent_table=sq_get_html_translation_table(HTML_ENTITIES,$quote_style,$charset);
1002 // convert characters
1003 return str_replace(array_keys($sq_html_ent_table),array_values($sq_html_ent_table),$string);
1004}
1005
b54acf3f 1006/**
1007 * Tests if string contains 8bit symbols.
1008 *
1009 * If charset is not set, function defaults to default_charset.
91e0dccc 1010 * $default_charset global must be set correctly if $charset is
b54acf3f 1011 * not used.
1012 * @param string $string tested string
1013 * @param string $charset charset used in a string
1014 * @return bool true if 8bit symbols are detected
17886554 1015 * @since 1.5.1 and 1.4.4
b54acf3f 1016 */
1017function sq_is8bit($string,$charset='') {
1018 global $default_charset;
1019
1020 if ($charset=='') $charset=$default_charset;
1021
1022 /**
1023 * Don't use \240 in ranges. Sometimes RH 7.2 doesn't like it.
17886554 1024 * Don't use \200-\237 for iso-8859-x charsets. This range
b54acf3f 1025 * stores control symbols in those charsets.
1026 * Use preg_match instead of ereg in order to avoid problems
1027 * with mbstring overloading
1028 */
1029 if (preg_match("/^iso-8859/i",$charset)) {
1030 $needle='/\240|[\241-\377]/';
1031 } else {
1032 $needle='/[\200-\237]|\240|[\241-\377]/';
1033 }
1034 return preg_match("$needle",$string);
1035}
1036
1037/**
1038 * Replacement of mb_list_encodings function
1039 *
1040 * This function provides replacement for function that is available only
1041 * in php 5.x. Function does not test all mbstring encodings. Only the ones
1042 * that might be used in SM translations.
1043 *
17886554 1044 * Supported strings are stored in session in order to reduce number of
b54acf3f 1045 * mb_internal_encoding function calls.
1046 *
91e0dccc 1047 * If you want to test all mbstring encodings - fill $list_of_encodings
b54acf3f 1048 * array.
17886554 1049 * @return array list of encodings supported by php mbstring extension
7fe4b6ed 1050 * @since 1.5.1 and 1.4.6
b54acf3f 1051 */
1052function sq_mb_list_encodings() {
1053 if (! function_exists('mb_internal_encoding'))
1054 return array();
1055
31310ecd 1056 // php 5+ function
1057 if (function_exists('mb_list_encodings')) {
1058 $ret = mb_list_encodings();
1059 array_walk($ret,'sq_lowercase_array_vals');
1060 return $ret;
1061 }
1062
b54acf3f 1063 // don't try to test encodings, if they are already stored in session
1064 if (sqgetGlobalVar('mb_supported_encodings',$mb_supported_encodings,SQ_SESSION))
1065 return $mb_supported_encodings;
1066
1067 // save original encoding
1068 $orig_encoding=mb_internal_encoding();
1069
1070 $list_of_encoding=array(
1071 'pass',
1072 'auto',
1073 'ascii',
1074 'jis',
1075 'utf-8',
1076 'sjis',
1077 'euc-jp',
1078 'iso-8859-1',
1079 'iso-8859-2',
1080 'iso-8859-7',
1081 'iso-8859-9',
1082 'iso-8859-15',
1083 'koi8-r',
1084 'koi8-u',
1085 'big5',
1086 'gb2312',
98abf408 1087 'gb18030',
b54acf3f 1088 'windows-1251',
1089 'windows-1255',
1090 'windows-1256',
1091 'tis-620',
1092 'iso-2022-jp',
ba40ff8b 1093 'euc-cn',
b54acf3f 1094 'euc-kr',
ba40ff8b 1095 'euc-tw',
1096 'uhc',
b54acf3f 1097 'utf7-imap');
1098
1099 $supported_encodings=array();
1100
1101 foreach ($list_of_encoding as $encoding) {
1102 // try setting encodings. suppress warning messages
1103 if (@mb_internal_encoding($encoding))
1104 $supported_encodings[]=$encoding;
1105 }
1106
1107 // restore original encoding
1108 mb_internal_encoding($orig_encoding);
1109
1110 // register list in session
1111 sqsession_register($supported_encodings,'mb_supported_encodings');
1112
1113 return $supported_encodings;
1114}
1115
c7aff938 1116/**
31310ecd 1117 * Callback function used to lowercase array values.
1118 * @param string $val array value
1119 * @param mixed $key array key
7fe4b6ed 1120 * @since 1.5.1 and 1.4.6
31310ecd 1121 */
1122function sq_lowercase_array_vals(&$val,$key) {
1123 $val = strtolower($val);
1124}
1125
1126
1127/**
c7aff938 1128 * Function returns number of characters in string.
1129 *
1130 * Returned number might be different from number of bytes in string,
91c27aee 1131 * if $charset is multibyte charset. Detection depends on mbstring
98abf408 1132 * functions. If mbstring does not support tested multibyte charset,
91c27aee 1133 * vanilla string length function is used.
c7aff938 1134 * @param string $str string
1135 * @param string $charset charset
7fe4b6ed 1136 * @since 1.5.1 and 1.4.6
91c27aee 1137 * @return integer number of characters in string
c7aff938 1138 */
31310ecd 1139function sq_strlen($str, $charset=null){
c7aff938 1140 // default option
31310ecd 1141 if (is_null($charset)) return strlen($str);
1142
1143 // lowercase charset name
1144 $charset=strtolower($charset);
c7aff938 1145
1146 // use automatic charset detection, if function call asks for it
1147 if ($charset=='auto') {
1148 global $default_charset;
1149 set_my_charset();
1150 $charset=$default_charset;
1151 }
1152
98abf408 1153 // Use mbstring only with listed charsets
1154 $aList_of_mb_charsets=array('utf-8','big5','gb2312','gb18030','euc-jp','euc-cn','euc-tw','euc-kr');
c7aff938 1155
1156 // calculate string length according to charset
98abf408 1157 if (in_array($charset,$aList_of_mb_charsets) && in_array($charset,sq_mb_list_encodings())) {
1158 $real_length = mb_strlen($str,$charset);
c7aff938 1159 } else {
91c27aee 1160 // own strlen detection code is removed because missing strpos,
98abf408 1161 // strtoupper and substr implementations break string wrapping.
c7aff938 1162 $real_length=strlen($str);
1163 }
1164 return $real_length;
1165}
1166
17886554 1167/**
1168 * string padding with multibyte support
1169 *
1170 * @link http://www.php.net/str_pad
1171 * @param string $string original string
1172 * @param integer $width padded string width
1173 * @param string $pad padding symbols
91c27aee 1174 * @param integer $padtype padding type
17886554 1175 * (internal php defines, see str_pad() description)
1176 * @param string $charset charset used in original string
1177 * @return string padded string
1178 */
1179function sq_str_pad($string, $width, $pad, $padtype, $charset='') {
1180
1181 $charset = strtolower($charset);
1182 $padded_string = '';
1183
1184 switch ($charset) {
1185 case 'utf-8':
1186 case 'big5':
1187 case 'gb2312':
1188 case 'euc-kr':
1189 /*
1190 * all multibyte charsets try to increase width value by
1191 * adding difference between number of bytes and real length
1192 */
1193 $width = $width - sq_strlen($string,$charset) + strlen($string);
1194 default:
1195 $padded_string=str_pad($string,$width,$pad,$padtype);
1196 }
1197 return $padded_string;
1198}
98abf408 1199
1200/**
1201 * Wrapper that is used to switch between vanilla and multibyte substr
1202 * functions.
1203 * @param string $string
1204 * @param integer $start
1205 * @param integer $length
1206 * @param string $charset
1207 * @return string
1208 * @since 1.5.1
1209 * @link http://www.php.net/substr
1210 * @link http://www.php.net/mb_substr
1211 */
1212function sq_substr($string,$start,$length,$charset='auto') {
1213 // use automatic charset detection, if function call asks for it
1214 if ($charset=='auto') {
1215 global $default_charset;
1216 set_my_charset();
1217 $charset=$default_charset;
1218 }
1219 $charset = strtolower($charset);
91c27aee 1220 if (function_exists('mb_internal_encoding') &&
98abf408 1221 in_array($charset,sq_mb_list_encodings())) {
1222 return mb_substr($string,$start,$length,$charset);
1223 }
1224 // TODO: add mbstring independent code
1225
1226 // use vanilla string functions as last option
1227 return substr($string,$start,$length);
1228}
1229
1230/**
1231 * Wrapper that is used to switch between vanilla and multibyte strpos
1232 * functions.
1233 * @param string $haystack
1234 * @param mixed $needle
1235 * @param integer $offset
1236 * @param string $charset
1237 * @return string
1238 * @since 1.5.1
1239 * @link http://www.php.net/strpos
1240 * @link http://www.php.net/mb_strpos
1241 */
1242function sq_strpos($haystack,$needle,$offset,$charset='auto') {
1243 // use automatic charset detection, if function call asks for it
1244 if ($charset=='auto') {
1245 global $default_charset;
1246 set_my_charset();
1247 $charset=$default_charset;
1248 }
1249 $charset = strtolower($charset);
91c27aee 1250 if (function_exists('mb_internal_encoding') &&
98abf408 1251 in_array($charset,sq_mb_list_encodings())) {
1252 return mb_strpos($haystack,$needle,$offset,$charset);
1253 }
1254 // TODO: add mbstring independent code
1255
1256 // use vanilla string functions as last option
1257 return strpos($haystack,$needle,$offset);
1258}
1259
1260/**
1261 * Wrapper that is used to switch between vanilla and multibyte strtoupper
1262 * functions.
1263 * @param string $string
1264 * @param string $charset
1265 * @return string
1266 * @since 1.5.1
1267 * @link http://www.php.net/strtoupper
1268 * @link http://www.php.net/mb_strtoupper
1269 */
1270function sq_strtoupper($string,$charset='auto') {
1271 // use automatic charset detection, if function call asks for it
1272 if ($charset=='auto') {
1273 global $default_charset;
1274 set_my_charset();
1275 $charset=$default_charset;
1276 }
1277 $charset = strtolower($charset);
91c27aee 1278 if (function_exists('mb_strtoupper') &&
98abf408 1279 in_array($charset,sq_mb_list_encodings())) {
1280 return mb_strtoupper($string,$charset);
1281 }
1282 // TODO: add mbstring independent code
1283
1284 // use vanilla string functions as last option
1285 return strtoupper($string);
1286}
a24cf710 1287
1288/**
1289 * Counts 8bit bytes in string
1290 * @param string $string tested string
1291 * @return integer number of 8bit bytes
1292 */
1293function sq_count8bit($string) {
1294 $count=0;
1295 for ($i=0; $i<strlen($string); $i++) {
1296 if (ord($string[$i]) > 127) $count++;
1297 }
1298 return $count;
1299}
7f62aaef 1300
f8a1ed5a 1301?>