';
- print htmlentities($buffer);
- print '
';
+ return makeInternalLink($url, $text, '_blank', $accesskey);
}
/**
* version of fwrite which checks for failure
+ * @param resource $fp
+ * @param string $string
+ * @return number of written bytes. false on failure
+ * @since 1.4.3
*/
function sq_fwrite($fp, $string) {
// write to file
@@ -919,6 +819,7 @@ function sq_fwrite($fp, $string) {
*
* @param string $charset charset used for encoding. default to us-ascii, 'auto' uses $default_charset global value.
* @return array html translation array
+ * @since 1.5.1
*/
function sq_get_html_translation_table($table,$quote_style=ENT_COMPAT,$charset='us-ascii') {
global $default_charset;
@@ -982,6 +883,7 @@ function sq_get_html_translation_table($table,$quote_style=ENT_COMPAT,$charset='
*
* @param string $charset charset used for encoding. defaults to 'us-ascii', 'auto' uses $default_charset global value.
* @return string sanitized string
+ * @since 1.5.1
*/
function sq_htmlentities($string,$quote_style=ENT_COMPAT,$charset='us-ascii') {
// get translation table
@@ -1034,12 +936,19 @@ function sq_is8bit($string,$charset='') {
* If you want to test all mbstring encodings - fill $list_of_encodings
* array.
* @return array list of encodings supported by php mbstring extension
- * @since 1.5.1
+ * @since 1.5.1 and 1.4.6
*/
function sq_mb_list_encodings() {
if (! function_exists('mb_internal_encoding'))
return array();
+ // php 5+ function
+ if (function_exists('mb_list_encodings')) {
+ $ret = mb_list_encodings();
+ array_walk($ret,'sq_lowercase_array_vals');
+ return $ret;
+ }
+
// don't try to test encodings, if they are already stored in session
if (sqgetGlobalVar('mb_supported_encodings',$mb_supported_encodings,SQ_SESSION))
return $mb_supported_encodings;
@@ -1070,7 +979,10 @@ function sq_mb_list_encodings() {
'windows-1256',
'tis-620',
'iso-2022-jp',
+ 'euc-cn',
'euc-kr',
+ 'euc-tw',
+ 'uhc',
'utf7-imap');
$supported_encodings=array();
@@ -1090,32 +1002,44 @@ function sq_mb_list_encodings() {
return $supported_encodings;
}
+/**
+ * Callback function used to lowercase array values.
+ * @param string $val array value
+ * @param mixed $key array key
+ * @since 1.5.1 and 1.4.6
+ */
+function sq_lowercase_array_vals(&$val,$key) {
+ $val = strtolower($val);
+}
+
+
/**
* Function returns number of characters in string.
*
* Returned number might be different from number of bytes in string,
- * if $charset is multibyte charset. Detection depends on mbstring
+ * if $charset is multibyte charset. Detection depends on mbstring
* functions. If mbstring does not support tested multibyte charset,
- * vanilla string length function is used.
+ * vanilla string length function is used.
* @param string $str string
* @param string $charset charset
- * @since 1.5.1
- * @return integer number of characters in string
+ * @since 1.5.1 and 1.4.6
+ * @return integer number of characters in string
*/
-function sq_strlen($str, $charset=''){
+function sq_strlen($str, $charset=null){
// default option
- if ($charset=='') return strlen($str);
+ if (is_null($charset)) return strlen($str);
+
+ // lowercase charset name
+ $charset=strtolower($charset);
// use automatic charset detection, if function call asks for it
if ($charset=='auto') {
- global $default_charset;
+ global $default_charset, $squirrelmail_language;
set_my_charset();
$charset=$default_charset;
+ if ($squirrelmail_language=='ja_JP') $charset='euc-jp';
}
- // lowercase charset name
- $charset=strtolower($charset);
-
// Use mbstring only with listed charsets
$aList_of_mb_charsets=array('utf-8','big5','gb2312','gb18030','euc-jp','euc-cn','euc-tw','euc-kr');
@@ -1123,7 +1047,7 @@ function sq_strlen($str, $charset=''){
if (in_array($charset,$aList_of_mb_charsets) && in_array($charset,sq_mb_list_encodings())) {
$real_length = mb_strlen($str,$charset);
} else {
- // own strlen detection code is removed because missing strpos,
+ // own strlen detection code is removed because missing strpos,
// strtoupper and substr implementations break string wrapping.
$real_length=strlen($str);
}
@@ -1137,7 +1061,7 @@ function sq_strlen($str, $charset=''){
* @param string $string original string
* @param integer $width padded string width
* @param string $pad padding symbols
- * @param integer $padtype padding type
+ * @param integer $padtype padding type
* (internal php defines, see str_pad() description)
* @param string $charset charset used in original string
* @return string padded string
@@ -1175,16 +1099,43 @@ function sq_str_pad($string, $width, $pad, $padtype, $charset='') {
* @link http://www.php.net/substr
* @link http://www.php.net/mb_substr
*/
-function sq_substr($string,$start,$length,$charset='auto') {
+function sq_substr($string,$start,$length=NULL,$charset='auto') {
+
+ // if $length is NULL, use the full string length...
+ // we have to do this to mimick the use of substr()
+ // where $length is not given
+ //
+ if (is_null($length))
+ $length = sq_strlen($length);
+
+
// use automatic charset detection, if function call asks for it
+ static $charset_auto, $bUse_mb;
+
if ($charset=='auto') {
- global $default_charset;
- set_my_charset();
- $charset=$default_charset;
+ if (!isset($charset_auto)) {
+ global $default_charset, $squirrelmail_language;
+ set_my_charset();
+ $charset=$default_charset;
+ if ($squirrelmail_language=='ja_JP') $charset='euc-jp';
+ $charset_auto = $charset;
+ } else {
+ $charset = $charset_auto;
+ }
}
$charset = strtolower($charset);
- if (function_exists('mb_internal_encoding') &&
- in_array($charset,sq_mb_list_encodings())) {
+
+ // in_array call is expensive => do it once and use a static var for
+ // storing the results
+ if (!isset($bUse_mb)) {
+ if (in_array($charset,sq_mb_list_encodings())) {
+ $bUse_mb = true;
+ } else {
+ $bUse_mb = false;
+ }
+ }
+
+ if ($bUse_mb) {
return mb_substr($string,$start,$length,$charset);
}
// TODO: add mbstring independent code
@@ -1193,6 +1144,125 @@ function sq_substr($string,$start,$length,$charset='auto') {
return substr($string,$start,$length);
}
+/**
+ * This is a replacement for PHP's substr_replace() that is
+ * multibyte-aware.
+ *
+ * @param string $string The string to operate upon
+ * @param string $replacement The string to be inserted
+ * @param int $start The offset at which to begin substring replacement
+ * @param int $length The number of characters after $start to remove
+ * NOTE that if you need to specify a charset but
+ * want to achieve normal substr_replace() behavior
+ * where $length is not specified, use NULL (OPTIONAL;
+ * default from $start to end of string)
+ * @param string $charset The charset of the given string. A value of NULL
+ * here will force the use of PHP's standard substr().
+ * (OPTIONAL; default is "auto", which indicates that
+ * the user's current charset should be used).
+ *
+ * @return string The manipulated string
+ *
+ * Of course, you can use more advanced (e.g., negative) values
+ * for $start and $length as needed - see the PHP manual for more
+ * information: http://www.php.net/manual/function.substr-replace.php
+ *
+ */
+function sq_substr_replace($string, $replacement, $start, $length=NULL,
+ $charset='auto')
+{
+
+ // NULL charset? Just use substr_replace()
+ //
+ if (is_null($charset))
+ return is_null($length) ? substr_replace($string, $replacement, $start)
+ : substr_replace($string, $replacement, $start, $length);
+
+
+ // use current character set?
+ //
+ if ($charset == 'auto')
+ {
+//FIXME: is there any reason why this cannot be a global flag used by all string wrapper functions?
+ static $auto_charset;
+ if (!isset($auto_charset))
+ {
+ global $default_charset;
+//FIXME - do we need this?
+global $squirrelmail_language;
+ set_my_charset();
+ $auto_charset = $default_charset;
+//FIXME - do we need this?
+if ($squirrelmail_language == 'ja_JP') $auto_charset = 'euc-jp';
+ }
+ $charset = $auto_charset;
+ }
+
+
+ // standardize character set name
+ //
+ $charset = strtolower($charset);
+
+
+/* ===== FIXME: this list is not used in 1.5.x, but if we need it, unless this differs between all our string function wrappers, we should store this info in the session
+ // only use mbstring with the following character sets
+ //
+ $sq_substr_replace_mb_charsets = array(
+ 'utf-8',
+ 'big5',
+ 'gb2312',
+ 'gb18030',
+ 'euc-jp',
+ 'euc-cn',
+ 'euc-tw',
+ 'euc-kr'
+ );
+
+
+ // now we can use our own implementation using
+ // mb_substr() and mb_strlen() if needed
+ //
+ if (in_array($charset, $sq_substr_replace_mb_charsets)
+ && in_array($charset, sq_mb_list_encodings()))
+===== */
+//FIXME: is there any reason why this cannot be a global array used by all string wrapper functions?
+ if (in_array($charset, sq_mb_list_encodings()))
+ {
+
+ $string_length = mb_strlen($string, $charset);
+
+ if ($start < 0)
+ $start = max(0, $string_length + $start);
+
+ else if ($start > $string_length)
+ $start = $string_length;
+
+ if ($length < 0)
+ $length = max(0, $string_length - $start + $length);
+
+ else if (is_null($length) || $length > $string_length)
+ $length = $string_length;
+
+ if ($start + $length > $string_length)
+ $length = $string_length - $start;
+
+ return mb_substr($string, 0, $start, $charset)
+ . $replacement
+ . mb_substr($string,
+ $start + $length,
+ $string_length, // FIXME: I can't see why this is needed: - $start - $length,
+ $charset);
+
+ }
+
+
+ // else use normal substr_replace()
+ //
+ return is_null($length) ? substr_replace($string, $replacement, $start)
+ : substr_replace($string, $replacement, $start, $length);
+
+}
+
/**
* Wrapper that is used to switch between vanilla and multibyte strpos
* functions.
@@ -1207,14 +1277,31 @@ function sq_substr($string,$start,$length,$charset='auto') {
*/
function sq_strpos($haystack,$needle,$offset,$charset='auto') {
// use automatic charset detection, if function call asks for it
+ static $charset_auto, $bUse_mb;
+
if ($charset=='auto') {
- global $default_charset;
- set_my_charset();
- $charset=$default_charset;
+ if (!isset($charset_auto)) {
+ global $default_charset, $squirrelmail_language;
+ set_my_charset();
+ $charset=$default_charset;
+ if ($squirrelmail_language=='ja_JP') $charset='euc-jp';
+ $charset_auto = $charset;
+ } else {
+ $charset = $charset_auto;
+ }
}
$charset = strtolower($charset);
- if (function_exists('mb_internal_encoding') &&
- in_array($charset,sq_mb_list_encodings())) {
+
+ // in_array call is expensive => do it once and use a static var for
+ // storing the results
+ if (!isset($bUse_mb)) {
+ if (in_array($charset,sq_mb_list_encodings())) {
+ $bUse_mb = true;
+ } else {
+ $bUse_mb = false;
+ }
+ }
+ if ($bUse_mb) {
return mb_strpos($haystack,$needle,$offset,$charset);
}
// TODO: add mbstring independent code
@@ -1235,14 +1322,33 @@ function sq_strpos($haystack,$needle,$offset,$charset='auto') {
*/
function sq_strtoupper($string,$charset='auto') {
// use automatic charset detection, if function call asks for it
+ static $charset_auto, $bUse_mb;
+
if ($charset=='auto') {
- global $default_charset;
- set_my_charset();
- $charset=$default_charset;
+ if (!isset($charset_auto)) {
+ global $default_charset, $squirrelmail_language;
+ set_my_charset();
+ $charset=$default_charset;
+ if ($squirrelmail_language=='ja_JP') $charset='euc-jp';
+ $charset_auto = $charset;
+ } else {
+ $charset = $charset_auto;
+ }
}
$charset = strtolower($charset);
- if (function_exists('mb_strtoupper') &&
- in_array($charset,sq_mb_list_encodings())) {
+
+ // in_array call is expensive => do it once and use a static var for
+ // storing the results
+ if (!isset($bUse_mb)) {
+ if (function_exists('mb_strtoupper') &&
+ in_array($charset,sq_mb_list_encodings())) {
+ $bUse_mb = true;
+ } else {
+ $bUse_mb = false;
+ }
+ }
+
+ if ($bUse_mb) {
return mb_strtoupper($string,$charset);
}
// TODO: add mbstring independent code
@@ -1250,5 +1356,415 @@ function sq_strtoupper($string,$charset='auto') {
// use vanilla string functions as last option
return strtoupper($string);
}
-$PHP_SELF = php_self();
-?>
+
+/**
+ * Counts 8bit bytes in string
+ * @param string $string tested string
+ * @return integer number of 8bit bytes
+ */
+function sq_count8bit($string) {
+ $count=0;
+ for ($i=0; $i