6 * This code provides various string manipulation functions that are
7 * used by the rest of the SquirrelMail code.
9 * @copyright © 1999-2005 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @package squirrelmail
16 if (!defined('SM_PATH')) define('SM_PATH','../');
19 * SquirrelMail version number -- DO NOT CHANGE
22 $version = '1.5.1 [CVS]';
25 * SquirrelMail internal version number -- DO NOT CHANGE
26 * $sm_internal_version = array (release, major, minor)
28 global $SQM_INTERNAL_VERSION;
29 $SQM_INTERNAL_VERSION = array(1,5,1);
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.
36 include_once(SM_PATH
. 'functions/global.php');
39 * Appends citation markers to the string.
40 * Also appends a trailing space.
42 * @author Justus Pendleton
43 * @param string $str The string to append to
44 * @param int $citeLevel the number of markers to append
48 function sqMakeCite (&$str, $citeLevel) {
49 for ($i = 0; $i < $citeLevel; $i++
) {
52 if ($citeLevel != 0) {
58 * Create a newline in the string, adding citation
59 * markers to the newline as necessary.
61 * @author Justus Pendleton
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
68 function sqMakeNewLine (&$str, $citeLevel, &$column) {
72 sqMakeCite ($str, $citeLevel);
73 $column = $citeLevel +
1;
80 * Checks for spaces in strings - only used if PHP doesn't have native ctype support
82 * You might be able to rewrite the function by adding short evaluation form.
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.
89 * - no-break space ( ) - it is 8bit symbol, that depends on charset.
90 * there are at least three different charset groups that have nbsp in
93 * I don't see any charset/nbsp options in php ctype either.
95 * @param string $string tested string
96 * @return bool true when only whitespace symbols are present in test string
99 function sm_ctype_space($string) {
100 if ( preg_match('/^[\x09-\x0D]|^\x20/', $string) ||
$string=='') {
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.
113 * @author Justus Pendleton
114 * @param string $body the entire body of text
115 * @param int $wrap the maximum line length
116 * @return string the wrapped text
119 function &sqBodyWrap (&$body, $wrap) {
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);
127 // the newly wrapped text
129 // current column since the last newline in the outstring
131 $length = sq_strlen($body);
132 // where we are in the original string
134 // the number of >>> citation markers we are currently at
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
143 while (($pos < $length) && (sq_substr($body,$pos,1) == '>')) {
147 // skip over any spaces interleaved among the cite markers
148 while (($pos < $length) && (sq_substr($body,$pos,1) == ' ')) {
153 if ($pos >= $length) {
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
161 if ((sq_substr($body,$pos,1) == "\n" ) && (sq_strlen($outString) != 0)) {
162 $outStringLast = $outString{sq_strlen($outString) - 1};
163 if ($outStringLast != "\n") {
166 sqMakeCite ($outString, $newCiteLevel);
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);
179 $citeLevel = $newCiteLevel;
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.
195 // find the next newline -- we don't want to go further than that
196 $nextNewline = sq_strpos ($body, "\n", $pos);
197 if ($nextNewline === FALSE) {
198 $nextNewline = $length;
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) {
205 $outString .= sq_substr ($body, $pos, ($nextNewline - $pos));
206 $outStringCol = $nextNewline - $pos;
207 if ($nextNewline != $length) {
208 sqMakeNewLine ($outString, 0, $outStringCol);
210 $pos = $nextNewline +
1;
214 * Set this to false to stop appending short strings to previous lines
217 // inner loop, (obviously) handles wrapping up to
219 while ($pos < $nextNewline) {
220 // skip over initial spaces
221 while (($pos < $nextNewline) && (ctype_space (sq_substr($body,$pos,1)))) {
224 // if this is a short line then just append it and continue outer loop
225 if (($outStringCol +
$nextNewline - $pos) <= ($wrap - $citeLevel - 1) ) {
226 // if this is the final line in the input string then include
227 // any trailing newlines
228 // echo substr($body,$pos,$wrap). "<br />";
229 if (($nextNewline +
1 == $length) && (sq_substr($body,$nextNewline,1) == "\n")) {
233 // trim trailing spaces
234 $lastRealChar = $nextNewline;
235 while (($lastRealChar > $pos && $lastRealChar < $length) && (ctype_space (sq_substr($body,$lastRealChar,1)))) {
238 // decide if appending the short string is what we want
239 if (($nextNewline < $length && sq_substr($body,$nextNewline,1) == "\n") &&
240 isset($lastRealChar)) {
242 //check the first word:
243 while (($mypos < $length) && (sq_substr($body,$mypos,1) == '>')) {
245 // skip over any spaces interleaved among the cite markers
246 while (($mypos < $length) && (sq_substr($body,$mypos,1) == ' ')) {
252 if ($mypos == $nextNewline+1) {
253 while (($mypos < $length) && ($body{$mypos} == ' ')) {
259 $firstword = sq_substr($body,$mypos,sq_strpos($body,' ',$mypos) - $mypos);
260 //if ($dowrap || $ldnspacecnt > 1 || ($firstword && (
261 if (!$smartwrap ||
$firstword && (
262 $firstword{0} == '-' ||
263 $firstword{0} == '+' ||
264 $firstword{0} == '*' ||
265 sq_substr($firstword,0,1) == sq_strtoupper(sq_substr($firstword,0,1)) ||
266 strpos($firstword,':'))) {
267 $outString .= sq_substr($body,$pos,($lastRealChar - $pos+
1));
268 $outStringCol +
= ($lastRealChar - $pos);
269 sqMakeNewLine($outString,$citeLevel,$outStringCol);
278 $outString .= sq_substr ($body, $pos, ($lastRealChar - $pos +
1));
279 $outStringCol +
= ($lastRealChar - $pos);
280 $pos = $nextNewline +
1;
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
291 sqMakeNewLine ($outString, $citeLevel, $outStringCol);
295 // start looking backwards for whitespace to break at.
297 while (($breakPoint > $pos) && (! ctype_space (sq_substr($body,$breakPoint,1)))) {
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);
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.
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
324 if ((($outStringCol +
($breakPoint - $pos)) > ($wrap +
$SLOP)) && ($outStringCol > ($citeLevel +
1))) {
325 sqMakeNewLine ($outString, $citeLevel, $outStringCol);
329 // skip newlines or whitespace at the beginning of the string
330 $substring = sq_substr ($body, $pos, ($breakPoint - $pos));
331 $substring = rtrim ($substring); // do rtrim and ctype_space have the same ideas about whitespace?
332 $outString .= $substring;
333 $outStringCol +
= sq_strlen ($substring);
334 // advance past the whitespace which caused the wrap
336 while (($pos < $length) && (ctype_space (sq_substr($body,$pos,1)))) {
339 if ($pos < $length) {
340 sqMakeNewLine ($outString, $citeLevel, $outStringCol);
349 * Wraps text at $wrap characters
351 * Has a problem with special HTML characters, so call this before
352 * you do character translation.
354 * Specifically, &#039; comes up as 5 characters instead of 1.
355 * This should not add newlines to the end of lines.
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.
363 function sqWordWrap(&$line, $wrap, $charset='') {
364 global $languages, $squirrelmail_language;
366 // Use custom wrapping function, if translation provides it
367 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
368 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_wordwrap')) {
369 if (mb_detect_encoding($line) != 'ASCII') {
370 $line = call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_wordwrap', $line, $wrap);
375 ereg("^([\t >]*)([^\t >].*)?$", $line, $regs);
376 $beginning_spaces = $regs[1];
377 if (isset($regs[2])) {
378 $words = explode(' ', $regs[2]);
384 $line = $beginning_spaces;
386 while ($i < count($words)) {
387 /* Force one word to be on a line (minimum) */
389 $line_len = strlen($beginning_spaces) +
sq_strlen($words[$i],$charset) +
2;
390 if (isset($words[$i +
1]))
391 $line_len +
= sq_strlen($words[$i +
1],$charset);
394 /* Add more words (as long as they fit) */
395 while ($line_len < $wrap && $i < count($words)) {
396 $line .= ' ' . $words[$i];
398 if (isset($words[$i]))
399 $line_len +
= sq_strlen($words[$i],$charset) +
1;
404 /* Skip spaces if they are the first thing on a continued line */
405 while (!isset($words[$i]) && $i < count($words)) {
409 /* Go to the next line if we have more to process */
410 if ($i < count($words)) {
417 * Does the opposite of sqWordWrap()
418 * @param string $body the text to un-wordwrap
422 function sqUnWordWrap(&$body) {
423 global $squirrelmail_language;
425 if ($squirrelmail_language == 'ja_JP') {
429 $lines = explode("\n", $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];
443 $PreviousSpaces = $CurrentSpaces;
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;
450 $body .= "\n" . $lines[$i];
451 $PreviousSpaces = $CurrentSpaces;
458 * If $haystack is a full mailbox name and $needle is the mailbox
459 * separator character, returns the last part of the mailbox name.
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
466 function readShortMailboxName($haystack, $needle) {
471 $parts = explode($needle, $haystack);
472 $elem = array_pop($parts);
473 while ($elem == '' && count($parts)) {
474 $elem = array_pop($parts);
481 * Find out where SquirrelMail lives and try to be smart about it.
482 * The only problem would be when SquirrelMail lives in directories
483 * called "src", "functions", or "plugins", but people who do that need
484 * to be beaten with a steel pipe anyway.
486 * @return string the base uri of SquirrelMail installation.
489 function sqm_baseuri(){
490 global $base_uri, $PHP_SELF;
492 * If it is in the session, just return it.
494 if (isset($base_uri)){
497 $dirs = array('|src/.*|', '|plugins/.*|', '|functions/.*|');
498 $repl = array('', '', '');
499 $base_uri = preg_replace($dirs, $repl, $PHP_SELF);
506 * Determines the location to forward to, relative to your server.
507 * This is used in HTTP Location: redirects.
508 * If this doesnt work correctly for you (although it should), you can
509 * remove all this code except the last two lines, and have it return
510 * the right URL for your site, something like:
512 * http://www.example.com/squirrelmail/
514 * @return string the base url for this SquirrelMail installation
517 function get_location () {
519 global $imap_server_type;
521 /* Get the path, handle virtual directories */
522 if(strpos(php_self(), '?')) {
523 $path = substr(php_self(), 0, strpos(php_self(), '?'));
527 $path = substr($path, 0, strrpos($path, '/'));
528 if ( sqgetGlobalVar('sq_base_url', $full_url, SQ_SESSION
) ) {
529 return $full_url . $path;
532 /* Check if this is a HTTPS or regular HTTP request. */
536 * If you have 'SSLOptions +StdEnvVars' in your apache config
537 * OR if you have HTTPS=on in your HTTP_SERVER_VARS
538 * OR if you are on port 443
540 $getEnvVar = getenv('HTTPS');
541 if ((isset($getEnvVar) && !strcasecmp($getEnvVar, 'on')) ||
542 (sqgetGlobalVar('HTTPS', $https_on, SQ_SERVER
) && !strcasecmp($https_on, 'on')) ||
543 (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER
) && $server_port == 443)) {
547 /* Get the hostname from the Host header or server config. */
548 if ( !sqgetGlobalVar('HTTP_HOST', $host, SQ_SERVER
) ||
empty($host) ) {
549 if ( !sqgetGlobalVar('SERVER_NAME', $host, SQ_SERVER
) ||
empty($host) ) {
555 if (! strstr($host, ':')) {
556 if (sqgetGlobalVar('SERVER_PORT', $server_port, SQ_SERVER
)) {
557 if (($server_port != 80 && $proto == 'http://') ||
558 ($server_port != 443 && $proto == 'https://')) {
559 $port = sprintf(':%d', $server_port);
564 /* this is a workaround for the weird macosx caching that
565 causes Apache to return 16080 as the port number, which causes
568 if ($imap_server_type == 'macosx' && $port == ':16080') {
572 /* Fallback is to omit the server name and use a relative */
573 /* URI, although this is not RFC 2616 compliant. */
574 $full_url = ($host ?
$proto . $host . $port : '');
575 sqsession_register($full_url, 'sq_base_url');
576 return $full_url . $path;
583 * These functions are used to encrypt the password before it is
584 * stored in a cookie. The encryption key is generated by
585 * OneTimePadCreate();
587 * @param string $string the (password)string to encrypt
588 * @param string $epad the encryption key
589 * @return string the base64-encoded encrypted password
592 function OneTimePadEncrypt ($string, $epad) {
593 $pad = base64_decode($epad);
595 if (strlen($pad)>0) {
596 // make sure that pad is longer than string
597 while (strlen($string)>strlen($pad)) {
601 // FIXME: what should we do when $epad is not base64 encoded or empty.
605 for ($i = 0; $i < strlen ($string); $i++
) {
606 $encrypted .= chr (ord($string[$i]) ^
ord($pad[$i]));
609 return base64_encode($encrypted);
613 * Decrypts a password from the cookie
615 * Decrypts a password from the cookie, encrypted by OneTimePadEncrypt.
616 * This uses the encryption key that is stored in the session.
618 * @param string $string the string to decrypt
619 * @param string $epad the encryption key from the session
620 * @return string the decrypted password
623 function OneTimePadDecrypt ($string, $epad) {
624 $pad = base64_decode($epad);
626 if (strlen($pad)>0) {
627 // make sure that pad is longer than string
628 while (strlen($string)>strlen($pad)) {
632 // FIXME: what should we do when $epad is not base64 encoded or empty.
635 $encrypted = base64_decode ($string);
637 for ($i = 0; $i < strlen ($encrypted); $i++
) {
638 $decrypted .= chr (ord($encrypted[$i]) ^
ord($pad[$i]));
646 * Randomizes the mt_rand() function.
648 * Toss this in strings or integers and it will seed the generator
649 * appropriately. With strings, it is better to get them long.
650 * Use md5() to lengthen smaller strings.
652 * @param mixed $val a value to seed the random number generator. mixed = integer or string.
656 function sq_mt_seed($Val) {
657 /* if mt_getrandmax() does not return a 2^n - 1 number,
658 this might not work well. This uses $Max as a bitmask. */
659 $Max = mt_getrandmax();
661 if (! is_int($Val)) {
673 mt_srand(($Val ^
mt_rand(0, $Max)) & $Max);
678 * Init random number generator
680 * This function initializes the random number generator fairly well.
681 * It also only initializes it once, so you don't accidentally get
682 * the same 'random' numbers twice in one session.
687 function sq_mt_randomize() {
695 sqgetGlobalVar('REMOTE_PORT', $remote_port, SQ_SERVER
);
696 sqgetGlobalVar('REMOTE_ADDR', $remote_addr, SQ_SERVER
);
697 sq_mt_seed((int)((double) microtime() * 1000000));
698 sq_mt_seed(md5($remote_port . $remote_addr . getmypid()));
701 if (function_exists('getrusage')) {
702 /* Avoid warnings with Win32 */
704 if (isset($dat) && is_array($dat)) {
706 foreach ($dat as $k => $v)
710 sq_mt_seed(md5($Str));
714 if(sqgetGlobalVar('UNIQUE_ID', $unique_id, SQ_SERVER
)) {
715 sq_mt_seed(md5($unique_id));
722 * Creates encryption key
724 * Creates an encryption key for encrypting the password stored in the cookie.
725 * The encryption key itself is stored in the session.
727 * Pad must be longer or equal to encoded string length in 1.4.4/1.5.0 and older.
728 * @param int $length optional, length of the string to generate
729 * @return string the encryption key
732 function OneTimePadCreate ($length=100) {
736 for ($i = 0; $i < $length; $i++
) {
737 $pad .= chr(mt_rand(0,255));
740 return base64_encode($pad);
744 * Returns a string showing the size of the message/attachment.
746 * @param int $bytes the filesize in bytes
747 * @return string the filesize in human readable format
750 function show_readable_size($bytes) {
754 if ($bytes / 1024 > 1) {
761 settype($bytes, 'integer');
764 settype($bytes, 'integer');
767 return $bytes . ' ' . $type;
771 * Generates a random string from the character set you pass in
773 * @param int $size the length of the string to generate
774 * @param string $chars a string containing the characters to use
775 * @param int $flags a flag to add a specific set to the characters to use:
777 * 1 = add lowercase a-z to $chars
778 * 2 = add uppercase A-Z to $chars
779 * 4 = add numbers 0-9 to $chars
780 * @return string the random string
783 function GenerateRandomString($size, $chars, $flags = 0) {
785 $chars .= 'abcdefghijklmnopqrstuvwxyz';
788 $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
791 $chars .= '0123456789';
794 if (($size < 1) ||
(strlen($chars) < 1)) {
798 sq_mt_randomize(); /* Initialize the random number generator */
801 $j = strlen( $chars ) - 1;
802 while (strlen($String) < $size) {
803 $String .= $chars{mt_rand(0, $j)};
810 * Escapes special characters for use in IMAP commands.
812 * @param string $str the string to escape
813 * @return string the escaped string
816 function quoteimap($str) {
817 return preg_replace("/([\"\\\\])/", "\\\\$1", $str);
823 * Trims every element in the array, ie. remove the first char of each element
824 * @param array $array the array to trim
827 function TrimArray(&$array) {
828 foreach ($array as $k => $v) {
831 foreach ($
$k as $k2 => $v2) {
832 $
$k[$k2] = substr($v2, 1);
838 /* Re-assign back to array. */
844 * Create compose link
846 * Returns a link to the compose-page, taking in consideration
847 * the compose_in_new and javascript settings.
848 * @param string $url the URL to the compose page
849 * @param string $text the link text, default "Compose"
850 * @param string $target (since 1.4.3) url target
851 * @return string a link to the compose page
854 function makeComposeLink($url, $text = null, $target='') {
855 global $compose_new_win,$javascript_on, $compose_width, $compose_height;
858 $text = _("Compose");
861 // if not using "compose in new window", make
862 // regular link and be done with it
863 if($compose_new_win != '1') {
864 return makeInternalLink($url, $text, $target);
867 // build the compose in new window link...
870 // if javascript is on, use onclick event to handle it
872 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION
);
873 $compuri = $base_uri.$url;
874 return "<a href=\"javascript:void(0)\" onclick=\"comp_in_new('$compuri','$compose_width','$compose_height')\">$text</a>";
877 // otherwise, just open new window using regular HTML
878 return makeInternalLink($url, $text, '_blank');
884 * sm_print_r($some_variable, [$some_other_variable [, ...]]);
886 * Debugging function - does the same as print_r, but makes sure special
887 * characters are converted to htmlentities first. This will allow
888 * values like <some@email.address> to be displayed.
889 * The output is wrapped in <<pre>> and <</pre>> tags.
890 * Since 1.4.2 accepts unlimited number of arguments.
894 function sm_print_r() {
895 ob_start(); // Buffer output
896 foreach(func_get_args() as $var) {
899 // php has get_class_methods function that can print class methods
900 if (is_object($var)) {
901 // get class methods if $var is object
902 $aMethods=get_class_methods(get_class($var));
903 // make sure that $aMethods is array and array is not empty
904 if (is_array($aMethods) && $aMethods!=array()) {
905 echo "Object methods:\n";
906 foreach($aMethods as $method) {
907 echo '* ' . $method . "\n";
913 $buffer = ob_get_contents(); // Grab the print_r output
914 ob_end_clean(); // Silently discard the output & stop buffering
915 print '<div align="left"><pre>';
916 print htmlentities($buffer);
917 print '</pre></div>';
921 * version of fwrite which checks for failure
922 * @param resource $fp
923 * @param string $string
924 * @return number of written bytes. false on failure
927 function sq_fwrite($fp, $string) {
929 $count = @fwrite
($fp,$string);
930 // the number of bytes written should be the length of the string
931 if($count != strlen($string)) {
939 * sq_get_html_translation_table
941 * Returns the translation table used by sq_htmlentities()
943 * @param integer $table html translation table. Possible values (without quotes):
945 * <li>HTML_ENTITIES - full html entities table defined by charset</li>
946 * <li>HTML_SPECIALCHARS - html special characters table</li>
948 * @param integer $quote_style quote encoding style. Possible values (without quotes):
950 * <li>ENT_COMPAT - (default) encode double quotes</li>
951 * <li>ENT_NOQUOTES - don't encode double or single quotes</li>
952 * <li>ENT_QUOTES - encode double and single quotes</li>
954 * @param string $charset charset used for encoding. default to us-ascii, 'auto' uses $default_charset global value.
955 * @return array html translation array
958 function sq_get_html_translation_table($table,$quote_style=ENT_COMPAT
,$charset='us-ascii') {
959 global $default_charset;
961 if ($table == HTML_SPECIALCHARS
) $charset='us-ascii';
963 // Start array with ampersand
964 $sq_html_ent_table = array( "&" => '&' );
967 $sq_html_ent_table = array_merge($sq_html_ent_table,
972 if ($quote_style == ENT_COMPAT
)
973 $sq_html_ent_table = array_merge($sq_html_ent_table,
974 array("\"" => '"')
977 // double and single quotes
978 if ($quote_style == ENT_QUOTES
)
979 $sq_html_ent_table = array_merge($sq_html_ent_table,
980 array("\"" => '"',
984 if ($charset=='auto') $charset=$default_charset;
986 // add entities that depend on charset
989 include_once(SM_PATH
. 'functions/htmlentities/iso-8859-1.php');
992 include_once(SM_PATH
. 'functions/htmlentities/utf-8.php');
999 return $sq_html_ent_table;
1005 * Convert all applicable characters to HTML entities.
1006 * Minimal php requirement - v.4.0.5.
1008 * Function is designed for people that want to use full power of htmlentities() in
1011 * @param string $string string that has to be sanitized
1012 * @param integer $quote_style quote encoding style. Possible values (without quotes):
1014 * <li>ENT_COMPAT - (default) encode double quotes</li>
1015 * <li>ENT_NOQUOTES - don't encode double or single quotes</li>
1016 * <li>ENT_QUOTES - encode double and single quotes</li>
1018 * @param string $charset charset used for encoding. defaults to 'us-ascii', 'auto' uses $default_charset global value.
1019 * @return string sanitized string
1022 function sq_htmlentities($string,$quote_style=ENT_COMPAT
,$charset='us-ascii') {
1023 // get translation table
1024 $sq_html_ent_table=sq_get_html_translation_table(HTML_ENTITIES
,$quote_style,$charset);
1025 // convert characters
1026 return str_replace(array_keys($sq_html_ent_table),array_values($sq_html_ent_table),$string);
1030 * Tests if string contains 8bit symbols.
1032 * If charset is not set, function defaults to default_charset.
1033 * $default_charset global must be set correctly if $charset is
1035 * @param string $string tested string
1036 * @param string $charset charset used in a string
1037 * @return bool true if 8bit symbols are detected
1038 * @since 1.5.1 and 1.4.4
1040 function sq_is8bit($string,$charset='') {
1041 global $default_charset;
1043 if ($charset=='') $charset=$default_charset;
1046 * Don't use \240 in ranges. Sometimes RH 7.2 doesn't like it.
1047 * Don't use \200-\237 for iso-8859-x charsets. This range
1048 * stores control symbols in those charsets.
1049 * Use preg_match instead of ereg in order to avoid problems
1050 * with mbstring overloading
1052 if (preg_match("/^iso-8859/i",$charset)) {
1053 $needle='/\240|[\241-\377]/';
1055 $needle='/[\200-\237]|\240|[\241-\377]/';
1057 return preg_match("$needle",$string);
1061 * Replacement of mb_list_encodings function
1063 * This function provides replacement for function that is available only
1064 * in php 5.x. Function does not test all mbstring encodings. Only the ones
1065 * that might be used in SM translations.
1067 * Supported strings are stored in session in order to reduce number of
1068 * mb_internal_encoding function calls.
1070 * If you want to test all mbstring encodings - fill $list_of_encodings
1072 * @return array list of encodings supported by php mbstring extension
1073 * @since 1.5.1 and 1.4.6
1075 function sq_mb_list_encodings() {
1076 if (! function_exists('mb_internal_encoding'))
1080 if (function_exists('mb_list_encodings')) {
1081 $ret = mb_list_encodings();
1082 array_walk($ret,'sq_lowercase_array_vals');
1086 // don't try to test encodings, if they are already stored in session
1087 if (sqgetGlobalVar('mb_supported_encodings',$mb_supported_encodings,SQ_SESSION
))
1088 return $mb_supported_encodings;
1090 // save original encoding
1091 $orig_encoding=mb_internal_encoding();
1093 $list_of_encoding=array(
1122 $supported_encodings=array();
1124 foreach ($list_of_encoding as $encoding) {
1125 // try setting encodings. suppress warning messages
1126 if (@mb_internal_encoding
($encoding))
1127 $supported_encodings[]=$encoding;
1130 // restore original encoding
1131 mb_internal_encoding($orig_encoding);
1133 // register list in session
1134 sqsession_register($supported_encodings,'mb_supported_encodings');
1136 return $supported_encodings;
1140 * Callback function used to lowercase array values.
1141 * @param string $val array value
1142 * @param mixed $key array key
1143 * @since 1.5.1 and 1.4.6
1145 function sq_lowercase_array_vals(&$val,$key) {
1146 $val = strtolower($val);
1151 * Function returns number of characters in string.
1153 * Returned number might be different from number of bytes in string,
1154 * if $charset is multibyte charset. Detection depends on mbstring
1155 * functions. If mbstring does not support tested multibyte charset,
1156 * vanilla string length function is used.
1157 * @param string $str string
1158 * @param string $charset charset
1159 * @since 1.5.1 and 1.4.6
1160 * @return integer number of characters in string
1162 function sq_strlen($str, $charset=null){
1164 if (is_null($charset)) return strlen($str);
1166 // lowercase charset name
1167 $charset=strtolower($charset);
1169 // use automatic charset detection, if function call asks for it
1170 if ($charset=='auto') {
1171 global $default_charset;
1173 $charset=$default_charset;
1176 // Use mbstring only with listed charsets
1177 $aList_of_mb_charsets=array('utf-8','big5','gb2312','gb18030','euc-jp','euc-cn','euc-tw','euc-kr');
1179 // calculate string length according to charset
1180 if (in_array($charset,$aList_of_mb_charsets) && in_array($charset,sq_mb_list_encodings())) {
1181 $real_length = mb_strlen($str,$charset);
1183 // own strlen detection code is removed because missing strpos,
1184 // strtoupper and substr implementations break string wrapping.
1185 $real_length=strlen($str);
1187 return $real_length;
1191 * string padding with multibyte support
1193 * @link http://www.php.net/str_pad
1194 * @param string $string original string
1195 * @param integer $width padded string width
1196 * @param string $pad padding symbols
1197 * @param integer $padtype padding type
1198 * (internal php defines, see str_pad() description)
1199 * @param string $charset charset used in original string
1200 * @return string padded string
1202 function sq_str_pad($string, $width, $pad, $padtype, $charset='') {
1204 $charset = strtolower($charset);
1205 $padded_string = '';
1213 * all multibyte charsets try to increase width value by
1214 * adding difference between number of bytes and real length
1216 $width = $width - sq_strlen($string,$charset) +
strlen($string);
1218 $padded_string=str_pad($string,$width,$pad,$padtype);
1220 return $padded_string;
1224 * Wrapper that is used to switch between vanilla and multibyte substr
1226 * @param string $string
1227 * @param integer $start
1228 * @param integer $length
1229 * @param string $charset
1232 * @link http://www.php.net/substr
1233 * @link http://www.php.net/mb_substr
1235 function sq_substr($string,$start,$length,$charset='auto') {
1236 // use automatic charset detection, if function call asks for it
1237 if ($charset=='auto') {
1238 global $default_charset;
1240 $charset=$default_charset;
1242 $charset = strtolower($charset);
1243 if (function_exists('mb_internal_encoding') &&
1244 in_array($charset,sq_mb_list_encodings())) {
1245 return mb_substr($string,$start,$length,$charset);
1247 // TODO: add mbstring independent code
1249 // use vanilla string functions as last option
1250 return substr($string,$start,$length);
1254 * Wrapper that is used to switch between vanilla and multibyte strpos
1256 * @param string $haystack
1257 * @param mixed $needle
1258 * @param integer $offset
1259 * @param string $charset
1262 * @link http://www.php.net/strpos
1263 * @link http://www.php.net/mb_strpos
1265 function sq_strpos($haystack,$needle,$offset,$charset='auto') {
1266 // use automatic charset detection, if function call asks for it
1267 if ($charset=='auto') {
1268 global $default_charset;
1270 $charset=$default_charset;
1272 $charset = strtolower($charset);
1273 if (function_exists('mb_internal_encoding') &&
1274 in_array($charset,sq_mb_list_encodings())) {
1275 return mb_strpos($haystack,$needle,$offset,$charset);
1277 // TODO: add mbstring independent code
1279 // use vanilla string functions as last option
1280 return strpos($haystack,$needle,$offset);
1284 * Wrapper that is used to switch between vanilla and multibyte strtoupper
1286 * @param string $string
1287 * @param string $charset
1290 * @link http://www.php.net/strtoupper
1291 * @link http://www.php.net/mb_strtoupper
1293 function sq_strtoupper($string,$charset='auto') {
1294 // use automatic charset detection, if function call asks for it
1295 if ($charset=='auto') {
1296 global $default_charset;
1298 $charset=$default_charset;
1300 $charset = strtolower($charset);
1301 if (function_exists('mb_strtoupper') &&
1302 in_array($charset,sq_mb_list_encodings())) {
1303 return mb_strtoupper($string,$charset);
1305 // TODO: add mbstring independent code
1307 // use vanilla string functions as last option
1308 return strtoupper($string);
1312 * Counts 8bit bytes in string
1313 * @param string $string tested string
1314 * @return integer number of 8bit bytes
1316 function sq_count8bit($string) {
1318 for ($i=0; $i<strlen($string); $i++
) {
1319 if (ord($string[$i]) > 127) $count++
;