use PhpXmlRpc\PhpXmlRpc;
+/**
+ * @todo implement an interface
+ */
class Charset
{
// tables used for transcoding different charsets into us-ascii xml
/// IANA ISO-8859-1 does have well-defined 'C1' control codes for those - wikipedia's page on latin-1 says:
/// "ISO-8859-1 is the IANA preferred name for this standard when supplemented with the C0 and C1 control codes from ISO/IEC 6429."
/// Check what mbstring/iconv do by default with those?
- /*
- protected $xml_cp1252_Entities = array('in' => array(), out' => array(
- '€', '?', '‚', 'ƒ',
- '„', '…', '†', '‡',
- 'ˆ', '‰', 'Š', '‹',
- 'Œ', '?', 'Ž', '?',
- '?', '‘', '’', '“',
- '”', '•', '–', '—',
- '˜', '™', 'š', '›',
- 'œ', '?', 'ž', 'Ÿ'
- ));
- */
+ //
+ //protected $xml_cp1252_Entities = array('in' => array(), out' => array());
protected $charset_supersets = array(
'US-ASCII' => array('ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4',
'EUC-JP', 'EUC-', 'EUC-KR', 'EUC-CN',),
);
+ /** @var Charset $instance */
protected static $instance = null;
/**
* This class is singleton for performance reasons.
- * @todo can't we just make $xml_iso88591_Entities a static variable instead ?
+ * @todo should we just make $xml_iso88591_Entities a static variable instead ?
*
* @return Charset
*/
public static function instance()
{
if (self::$instance === null) {
- self::$instance = new self();
+ self::$instance = new static();
}
return self::$instance;
}
/**
- * @todo move the creation of the charset tables to be on-demand. This saves memory and time when latin-1 is not used at all
+ * Force usage as singleton
*/
- private function __construct()
+ protected function __construct()
{
- for ($i = 0; $i < 32; $i++) {
- $this->xml_iso88591_Entities["in"][] = chr($i);
- $this->xml_iso88591_Entities["out"][] = "&#{$i};";
- }
+ }
- for ($i = 160; $i < 256; $i++) {
- $this->xml_iso88591_Entities["in"][] = chr($i);
- $this->xml_iso88591_Entities["out"][] = "&#{$i};";
- }
+ /**
+ * @param string $tableName
+ * @throws \Exception for unsupported $tableName
+ */
+ protected function buildConversionTable($tableName)
+ {
+ switch($tableName) {
+ case 'xml_iso88591_Entities':
+ if (count($this->xml_iso88591_Entities['in'])) {
+ return;
+ }
+ for ($i = 0; $i < 32; $i++) {
+ $this->xml_iso88591_Entities["in"][] = chr($i);
+ $this->xml_iso88591_Entities["out"][] = "&#{$i};";
+ }
- /*for ($i = 128; $i < 160; $i++)
- {
- $this->xml_cp1252_Entities['in'][] = chr($i);
- }*/
+ for ($i = 160; $i < 256; $i++) {
+ $this->xml_iso88591_Entities["in"][] = chr($i);
+ $this->xml_iso88591_Entities["out"][] = "&#{$i};";
+ }
+ break;
+ /*case 'xml_cp1252_Entities':
+ if (count($this->xml_cp1252_Entities['in'])) {
+ return;
+ }
+ for ($i = 128; $i < 160; $i++)
+ {
+ $this->xml_cp1252_Entities['in'][] = chr($i);
+ }
+ $this->xml_cp1252_Entities['out'] = array(
+ '€', '?', '‚', 'ƒ',
+ '„', '…', '†', '‡',
+ 'ˆ', '‰', 'Š', '‹',
+ 'Œ', '?', 'Ž', '?',
+ '?', '‘', '’', '“',
+ '”', '•', '–', '—',
+ '˜', '™', 'š', '›',
+ 'œ', '?', 'ž', 'Ÿ'
+ );
+ $this->buildConversionTable('xml_iso88591_Entities');
+ break;*/
+ default:
+ throw new \Exception('Unsupported table: ' . $tableName);
+ }
}
/**
* Convert a string to the correct XML representation in a target charset.
+ * This involves:
+ * - character transformation for all characters which have a different representation in source and dest charsets
+ * - using 'charset entity' representation for all characters which are outside of the 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.
+ *
+ * Note that when not sending a charset encoding mime type along with http headers, we are bound by RFC 3023 to emit
+ * strict us-ascii for 'text/xml' payloads (but we should review RFC 7303, which seems to have changed the rules...)
*
* @todo do a bit of basic benchmarking (strtr vs. str_replace)
- * @todo make usage of iconv() or recode_string() or mb_string() where available
+ * @todo make usage of iconv() or mb_string() where available
+ * @todo support aliases for charset names, eg ASCII, LATIN1, ISO-88591 (see f.e. polyfill-iconv for a list)
*
* @param string $data
* @param string $srcEncoding
switch ($conversion) {
case 'ISO-8859-1_':
case 'ISO-8859-1_US-ASCII':
+ $this->buildConversionTable('xml_iso88591_Entities');
$escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data);
$escapedData = str_replace($this->xml_iso88591_Entities['in'], $this->xml_iso88591_Entities['out'], $escapedData);
break;
// when converting to latin-1, do not be so eager with using entities for characters 160-255
if ($conversion == 'UTF-8_ISO-8859-1') {
+ $this->buildConversionTable('xml_iso88591_Entities');
$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':
+ $this->buildConversionTable('xml_cp1252_Entities');
$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':
+ $this->buildConversionTable('xml_cp1252_Entities');
$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)
+ /// @todo we could use real UTF8 chars here instead of xml entities... (note that utf_8 encode all alone will NOT convert them)
$escapedData = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escapedData);
$escapedData = utf8_encode($escapedData);
break;
case 'CP1252_ISO-8859-1':
+ $this->buildConversionTable('xml_cp1252_Entities');
$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...
$escapedData = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escapedData);
throw new \Exception('Unsupported charset: ' . $charset);
}
}
-
}