3 namespace PhpXmlRpc\Helper;
5 use PhpXmlRpc\PhpXmlRpc;
9 // tables used for transcoding different charsets into us-ascii xml
10 protected $xml_iso88591_Entities = array("in" => array(), "out" => array());
12 /// @todo add to iso table the characters from cp_1252 range, i.e. 128 to 159?
13 /// These will NOT be present in true ISO-8859-1, but will save the unwary
14 /// windows user from sending junk (though no luck when receiving them...)
16 protected $xml_cp1252_Entities = array('in' => array(), out' => array(
17 '€', '?', '‚', 'ƒ',
18 '„', '…', '†', '‡',
19 'ˆ', '‰', 'Š', '‹',
20 'Œ', '?', 'Ž', '?',
21 '?', '‘', '’', '“',
22 '”', '•', '–', '—',
23 '˜', '™', 'š', '›',
24 'œ', '?', 'ž', 'Ÿ'
28 protected $charset_supersets = array(
29 'US-ASCII' => array('ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4',
30 'ISO-8859-5', 'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8',
31 'ISO-8859-9', 'ISO-8859-10', 'ISO-8859-11', 'ISO-8859-12',
32 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'UTF-8',
33 'EUC-JP', 'EUC-', 'EUC-KR', 'EUC-CN',),
36 protected static $instance = null;
39 * This class is singleton for performance reasons.
43 public static function instance()
45 if (self::$instance === null) {
46 self::$instance = new self();
49 return self::$instance;
52 private function __construct()
54 for ($i = 0; $i < 32; $i++) {
55 $this->xml_iso88591_Entities["in"][] = chr($i);
56 $this->xml_iso88591_Entities["out"][] = "&#{$i};";
59 for ($i = 160; $i < 256; $i++) {
60 $this->xml_iso88591_Entities["in"][] = chr($i);
61 $this->xml_iso88591_Entities["out"][] = "&#{$i};";
64 /*for ($i = 128; $i < 160; $i++)
66 $this->xml_cp1252_Entities['in'][] = chr($i);
71 * Convert a string to the correct XML representation in a target charset
72 * To help correct communication of non-ascii chars inside strings, regardless
73 * of the charset used when sending requests, parsing them, sending responses
74 * and parsing responses, an option is to convert all non-ascii chars present in the message
75 * into their equivalent 'charset entity'. Charset entities enumerated this way
76 * are independent of the charset encoding used to transmit them, and all XML
77 * parsers are bound to understand them.
78 * Note that in the std case we are not sending a charset encoding mime type
79 * along with http headers, so we are bound by RFC 3023 to emit strict us-ascii.
81 * @todo do a bit of basic benchmarking (strtr vs. str_replace)
82 * @todo make usage of iconv() or recode_string() or mb_string() where available
85 * @param string $srcEncoding
86 * @param string $destEncoding
90 public function encodeEntities($data, $srcEncoding = '', $destEncoding = '')
92 if ($srcEncoding == '') {
93 // lame, but we know no better...
94 $srcEncoding = PhpXmlRpc::$xmlrpc_internalencoding;
97 switch (strtoupper($srcEncoding . '_' . $destEncoding)) {
99 case 'ISO-8859-1_US-ASCII':
100 $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data);
101 $escapedData = str_replace($this->xml_iso88591_Entities['in'], $this->xml_iso88591_Entities['out'], $escapedData);
103 case 'ISO-8859-1_UTF-8':
104 $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data);
105 $escapedData = utf8_encode($escapedData);
107 case 'ISO-8859-1_ISO-8859-1':
108 case 'US-ASCII_US-ASCII':
109 case 'US-ASCII_UTF-8':
111 case 'US-ASCII_ISO-8859-1':
113 //case 'CP1252_CP1252':
114 $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data);
117 case 'UTF-8_US-ASCII':
118 case 'UTF-8_ISO-8859-1':
119 // NB: this will choke on invalid UTF-8, going most likely beyond EOF
121 // be kind to users creating string xmlrpc values out of different php types
122 $data = (string)$data;
124 for ($nn = 0; $nn < $ns; $nn++) {
129 /// @todo shall we replace this with a (supposedly) faster str_replace?
132 $escapedData .= '"';
135 $escapedData .= '&';
138 $escapedData .= ''';
141 $escapedData .= '<';
144 $escapedData .= '>';
149 } //2 11 110bbbbb 10bbbbbb (2047)
150 elseif ($ii >> 5 == 6) {
152 $ii = ord($data[$nn + 1]);
154 $ii = ($b1 * 64) + $b2;
155 $ent = sprintf('&#%d;', $ii);
156 $escapedData .= $ent;
158 } //3 16 1110bbbb 10bbbbbb 10bbbbbb
159 elseif ($ii >> 4 == 14) {
161 $ii = ord($data[$nn + 1]);
163 $ii = ord($data[$nn + 2]);
165 $ii = ((($b1 * 64) + $b2) * 64) + $b3;
166 $ent = sprintf('&#%d;', $ii);
167 $escapedData .= $ent;
169 } //4 21 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
170 elseif ($ii >> 3 == 30) {
172 $ii = ord($data[$nn + 1]);
174 $ii = ord($data[$nn + 2]);
176 $ii = ord($data[$nn + 3]);
178 $ii = ((((($b1 * 64) + $b2) * 64) + $b3) * 64) + $b4;
179 $ent = sprintf('&#%d;', $ii);
180 $escapedData .= $ent;
187 case 'CP1252_US-ASCII':
188 $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data);
189 $escapedData = str_replace($this->xml_iso88591_Entities']['in'], $this->xml_iso88591_Entities['out'], $escapedData);
190 $escapedData = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escapedData);
193 $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data);
194 /// @todo we could use real UTF8 chars here instead of xml entities... (note that utf_8 encode all allone will NOT convert them)
195 $escapedData = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escapedData);
196 $escapedData = utf8_encode($escapedData);
198 case 'CP1252_ISO-8859-1':
199 $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data);
200 // 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...
201 $escapedData = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escapedData);
206 error_log('XML-RPC: ' . __METHOD__ . ": Converting from $srcEncoding to $destEncoding: not supported...");
213 * Checks if a given charset encoding is present in a list of encodings or
214 * if it is a valid subset of any encoding in the list.
216 * @param string $encoding charset to be tested
217 * @param string|array $validList comma separated list of valid charsets (or array of charsets)
221 public function isValidCharset($encoding, $validList)
223 if (is_string($validList)) {
224 $validList = explode(',', $validList);
226 if (@in_array(strtoupper($encoding), $validList)) {
229 if (array_key_exists($encoding, $this->charset_supersets)) {
230 foreach ($validList as $allowed) {
231 if (in_array($allowed, $this->charset_supersets[$encoding])) {
242 * Used only for backwards compatibility
245 * @param string $charset
249 * @throws \Exception for unknown/unsupported charsets
251 public function getEntities($charset)
256 return $this->xml_iso88591_Entities;
258 throw new \Exception('Unsupported charset: ' . $charset);