diff options
author | Nils Adermann <naderman@naderman.de> | 2006-06-09 23:46:39 +0000 |
---|---|---|
committer | Nils Adermann <naderman@naderman.de> | 2006-06-09 23:46:39 +0000 |
commit | ace2ee4eeccbcf0f09633f6c60dbedc39da565e7 (patch) | |
tree | 04ad237b5ef08b09ade20c7c165eb5f31881585a /phpBB/includes | |
parent | 06981cb97bd6b8252f5ad5a65672390c33a12deb (diff) | |
download | forums-ace2ee4eeccbcf0f09633f6c60dbedc39da565e7.tar forums-ace2ee4eeccbcf0f09633f6c60dbedc39da565e7.tar.gz forums-ace2ee4eeccbcf0f09633f6c60dbedc39da565e7.tar.bz2 forums-ace2ee4eeccbcf0f09633f6c60dbedc39da565e7.tar.xz forums-ace2ee4eeccbcf0f09633f6c60dbedc39da565e7.zip |
This looks a bit like an ugly hack, I know :(
But hopefully it will solve most of our encoding problems with jabber [Bug #1585]
git-svn-id: file:///svn/phpbb/trunk@6034 89ea8834-ac86-4346-8a33-228a782c2dd0
Diffstat (limited to 'phpBB/includes')
-rw-r--r-- | phpBB/includes/functions_jabber.php | 93 | ||||
-rw-r--r-- | phpBB/includes/functions_messenger.php | 3 |
2 files changed, 89 insertions, 7 deletions
diff --git a/phpBB/includes/functions_jabber.php b/phpBB/includes/functions_jabber.php index 0f1a5df881..1646731731 100644 --- a/phpBB/includes/functions_jabber.php +++ b/phpBB/includes/functions_jabber.php @@ -27,6 +27,7 @@ */ class jabber { + var $encoding; var $server; var $port; var $username; @@ -69,6 +70,8 @@ class jabber $this->packet_queue = $this->subscription_queue = array(); $this->iq_sleep_timer = $this->delay_disconnect = 1; + $this->encoding = 'UTF-8'; + $this->returned_keep_alive = true; $this->txnid = 0; @@ -452,7 +455,8 @@ class jabber $id = $type . '_' . time(); } - $content = $this->_array_htmlspecialchars($content); + $this->_array_xmlspecialchars($content); + $this->_array_conv_utf8($content); $xml = "<message to='$to' type='$type' id='$id'>\n"; @@ -762,20 +766,97 @@ class jabber } /** - * _array_htmlspecialchars() - * applies htmlspecialchars() to all values in an array + * Recursively prepares the strings in an array to be used in XML data. + * @private */ - function _array_htmlspecialchars(&$array) + function _array_xmlspecialchars(&$array) { if (is_array($array)) { foreach ($array as $k => $v) { - $v = (is_array($v)) ? $this->_array_htmlspecialchars($v) : htmlspecialchars($v); + if (is_array($v)) + { + $this->_array_xmlspecialchars($array[$k]); + } + else + { + $this->_xmlspecialchars($array[$k]); + } } } + } - return $array; + /** + * Prepares a string for usage in XML data. + * @private + */ + function _xmlspecialchars(&$string) + { + // we only have a few entities in xml + $string = str_replace(array('&', '>', '<', '"', '\''), array('&', '>', '<', '"', '''), $string); + } + + /** + * Recursively converts all elements in an array to UTF-8 from the encoding stored in {@link encoding the encoding attribute}. + * @private + */ + function _array_conv_utf8(&$array) + { + // no need to do anything if the encoding already is UTF-8 + if (strtoupper($this->encoding) == 'UTF-8') + { + return true; + } + + if (is_array($array)) + { + foreach ($array as $k => $v) + { + if (is_array($v)) + { + $this->_array_conv_utf8($array[$k]); + } + else + { + $this->_conv_utf8($array[$k]); + } + } + } + } + + /** + * Converts a string to utf8 encoding. + * @private + * + * @param string $string has to have the same encoding as {@link encoding the encoding attribute} is set to. + * + * @return boolean True on success, false on failure. + */ + function _conv_utf8(&$string) + { + // no need to do anything if the encoding already is UTF-8 + if (strtoupper($this->encoding) == 'UTF-8') + { + return true; + } + + // first try iconv then mb_convert_encoding and as a last fall back try recode_string + if (function_exists('iconv') && (($string = iconv($this->encoding, 'UTF-8', $string)) !== false)) + { + return true; + } + elseif (function_exists('mb_convert_encoding') && (($string = mb_convert_encoding($string, 'UTF-8', $this->encoding)) !== false)) + { + return true; + } + elseif (function_exists('recode_string') && (($string = recode_string($this->encoding . '..UTF-8', $string)) !== false)) + { + return true; + } + + // if everything fails we will just have to live with what we have, good luck! + return false; } // ====================================================================== diff --git a/phpBB/includes/functions_messenger.php b/phpBB/includes/functions_messenger.php index e4e035a9a4..7d1334dcc1 100644 --- a/phpBB/includes/functions_messenger.php +++ b/phpBB/includes/functions_messenger.php @@ -425,6 +425,7 @@ class messenger $this->jabber->username = $config['jab_username']; $this->jabber->password = $config['jab_password']; $this->jabber->resource = ($config['jab_resource']) ? $config['jab_resource'] : ''; + $this->jabber->encoding = $this->encoding; if (!$this->jabber->connect()) { @@ -441,7 +442,7 @@ class messenger foreach ($addresses as $address) { - $this->jabber->send_message($address, 'normal', NULL, array('body' => htmlentities($this->msg))); + $this->jabber->send_message($address, 'normal', NULL, array('body' => $this->msg)); } sleep(1); |