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