X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=src%2FHelper%2FCharset.php;h=f00087c9a7390f34c685b8eaa3fce38fecf37082;hb=7fbd194478e080d624bab075d189bf3ba215325d;hp=6d5eb6a24291beffdacf296a031dfc0327219a1f;hpb=35d2340eea9a168983b8f20d54c399422790f816;p=plcapi.git diff --git a/src/Helper/Charset.php b/src/Helper/Charset.php index 6d5eb6a..f00087c 100644 --- a/src/Helper/Charset.php +++ b/src/Helper/Charset.php @@ -8,6 +8,7 @@ class Charset { // tables used for transcoding different charsets into us-ascii xml protected $xml_iso88591_Entities = array("in" => array(), "out" => array()); + protected $xml_iso88591_utf8 = array("in" => array(), "out" => array()); /// @todo add to iso table the characters from cp_1252 range, i.e. 128 to 159? /// These will NOT be present in true ISO-8859-1, but will save the unwary @@ -68,94 +69,97 @@ class Charset } /** - * Convert a string to the correct XML representation in a target charset - * To help correct communication of non-ascii chars inside strings, regardless - * of the charset used when sending requests, parsing them, sending responses - * and parsing responses, an option is to convert all non-ascii chars present in the message - * into their equivalent 'charset entity'. Charset entities enumerated this way - * are independent of the charset encoding used to transmit them, and all XML - * parsers are bound to understand them. - * Note that in the std case we are not sending a charset encoding mime type - * along with http headers, so we are bound by RFC 3023 to emit strict us-ascii. + * Convert a string to the correct XML representation in a target charset. + * + * To help correct communication of non-ascii chars inside strings, regardless of the charset used when sending + * requests, parsing them, sending responses and parsing responses, an option is to convert all non-ascii chars + * present in the message into their equivalent 'charset entity'. Charset entities enumerated this way are + * independent of the charset encoding used to transmit them, and all XML parsers are bound to understand them. + * Note that in the std case we are not sending a charset encoding mime type along with http headers, so we are + * bound by RFC 3023 to emit strict us-ascii. * * @todo do a bit of basic benchmarking (strtr vs. str_replace) * @todo make usage of iconv() or recode_string() or mb_string() where available * * @param string $data - * @param string $src_encoding - * @param string $dest_encoding + * @param string $srcEncoding + * @param string $destEncoding * * @return string */ - public function encode_entities($data, $src_encoding = '', $dest_encoding = '') + public function encodeEntities($data, $srcEncoding = '', $destEncoding = '') { - if ($src_encoding == '') { + if ($srcEncoding == '') { // lame, but we know no better... - $src_encoding = PhpXmlRpc::$xmlrpc_internalencoding; + $srcEncoding = PhpXmlRpc::$xmlrpc_internalencoding; } - switch (strtoupper($src_encoding . '_' . $dest_encoding)) { + $conversion = strtoupper($srcEncoding . '_' . $destEncoding); + switch ($conversion) { case 'ISO-8859-1_': case 'ISO-8859-1_US-ASCII': - $escaped_data = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); - $escaped_data = str_replace($this->xml_iso88591_Entities['in'], $this->xml_iso88591_Entities['out'], $escaped_data); + $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); + $escapedData = str_replace($this->xml_iso88591_Entities['in'], $this->xml_iso88591_Entities['out'], $escapedData); break; + case 'ISO-8859-1_UTF-8': - $escaped_data = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); - $escaped_data = utf8_encode($escaped_data); + $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); + $escapedData = utf8_encode($escapedData); break; + case 'ISO-8859-1_ISO-8859-1': case 'US-ASCII_US-ASCII': case 'US-ASCII_UTF-8': case 'US-ASCII_': case 'US-ASCII_ISO-8859-1': case 'UTF-8_UTF-8': - //case 'CP1252_CP1252': - $escaped_data = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); + //case 'CP1252_CP1252': + $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); break; + case 'UTF-8_': case 'UTF-8_US-ASCII': case 'UTF-8_ISO-8859-1': // NB: this will choke on invalid UTF-8, going most likely beyond EOF - $escaped_data = ''; - // be kind to users creating string xmlrpcvals out of different php types + $escapedData = ''; + // be kind to users creating string xmlrpc values out of different php types $data = (string)$data; $ns = strlen($data); for ($nn = 0; $nn < $ns; $nn++) { $ch = $data[$nn]; $ii = ord($ch); - //1 7 0bbbbbbb (127) + // 7 bits: 0bbbbbbb (127) if ($ii < 128) { /// @todo shall we replace this with a (supposedly) faster str_replace? switch ($ii) { case 34: - $escaped_data .= '"'; + $escapedData .= '"'; break; case 38: - $escaped_data .= '&'; + $escapedData .= '&'; break; case 39: - $escaped_data .= '''; + $escapedData .= '''; break; case 60: - $escaped_data .= '<'; + $escapedData .= '<'; break; case 62: - $escaped_data .= '>'; + $escapedData .= '>'; break; default: - $escaped_data .= $ch; + $escapedData .= $ch; } // switch - } //2 11 110bbbbb 10bbbbbb (2047) + } // 11 bits: 110bbbbb 10bbbbbb (2047) elseif ($ii >> 5 == 6) { $b1 = ($ii & 31); $ii = ord($data[$nn + 1]); $b2 = ($ii & 63); $ii = ($b1 * 64) + $b2; $ent = sprintf('&#%d;', $ii); - $escaped_data .= $ent; + $escapedData .= $ent; $nn += 1; - } //3 16 1110bbbb 10bbbbbb 10bbbbbb + } // 16 bits: 1110bbbb 10bbbbbb 10bbbbbb elseif ($ii >> 4 == 14) { $b1 = ($ii & 15); $ii = ord($data[$nn + 1]); @@ -164,9 +168,9 @@ class Charset $b3 = ($ii & 63); $ii = ((($b1 * 64) + $b2) * 64) + $b3; $ent = sprintf('&#%d;', $ii); - $escaped_data .= $ent; + $escapedData .= $ent; $nn += 2; - } //4 21 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb + } // 21 bits: 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb elseif ($ii >> 3 == 30) { $b1 = ($ii & 7); $ii = ord($data[$nn + 1]); @@ -177,36 +181,43 @@ class Charset $b4 = ($ii & 63); $ii = ((((($b1 * 64) + $b2) * 64) + $b3) * 64) + $b4; $ent = sprintf('&#%d;', $ii); - $escaped_data .= $ent; + $escapedData .= $ent; $nn += 3; } } + + // when converting to latin-1, do not be so eager with using entities for characters 160-255 + if ($conversion == 'UTF-8_ISO-8859-1') { + $escapedData = str_replace(array_slice($this->xml_iso88591_Entities['out'], 32), array_slice($this->xml_iso88591_Entities['in'], 32), $escapedData); + } break; + /* case 'CP1252_': case 'CP1252_US-ASCII': - $escaped_data = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); - $escaped_data = str_replace($this->xml_iso88591_Entities']['in'], $this->xml_iso88591_Entities['out'], $escaped_data); - $escaped_data = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escaped_data); + $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); + $escapedData = str_replace($this->xml_iso88591_Entities']['in'], $this->xml_iso88591_Entities['out'], $escapedData); + $escapedData = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escapedData); break; case 'CP1252_UTF-8': - $escaped_data = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); + $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); /// @todo we could use real UTF8 chars here instead of xml entities... (note that utf_8 encode all allone will NOT convert them) - $escaped_data = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escaped_data); - $escaped_data = utf8_encode($escaped_data); + $escapedData = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escapedData); + $escapedData = utf8_encode($escapedData); break; case 'CP1252_ISO-8859-1': - $escaped_data = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); + $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data); // we might as well replace all funky chars with a '?' here, but we are kind and leave it to the receiving application layer to decide what to do with these weird entities... - $escaped_data = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escaped_data); + $escapedData = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escapedData); break; */ + default: - $escaped_data = ''; - error_log("Converting from $src_encoding to $dest_encoding: not supported..."); + $escapedData = ''; + error_log('XML-RPC: ' . __METHOD__ . ": Converting from $srcEncoding to $destEncoding: not supported..."); } - return $escaped_data; + return $escapedData; } /** @@ -218,7 +229,7 @@ class Charset * * @return bool */ - public function is_valid_charset($encoding, $validList) + public function isValidCharset($encoding, $validList) { if (is_string($validList)) { $validList = explode(',', $validList); @@ -237,4 +248,28 @@ class Charset return false; } } + + /** + * Used only for backwards compatibility + * @deprecated + * + * @param string $charset + * + * @return array + * + * @throws \Exception for unknown/unsupported charsets + */ + public function getEntities($charset) + { + //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED); + + switch ($charset) + { + case 'iso88591': + return $this->xml_iso88591_Entities; + default: + throw new \Exception('Unsupported charset: ' . $charset); + } + } + }