3 namespace PhpXmlRpc\Helper;
5 use PhpXmlRpc\PhpXmlRpc;
10 // tables used for transcoding different charsets into us-ascii xml
11 protected $xml_iso88591_Entities = array("in" => array(), "out" => array());
13 /// @todo add to iso table the characters from cp_1252 range, i.e. 128 to 159?
14 /// These will NOT be present in true ISO-8859-1, but will save the unwary
15 /// windows user from sending junk (though no luck when receiving them...)
17 protected $xml_cp1252_Entities = array('in' => array(), out' => array(
18 '€', '?', '‚', 'ƒ',
19 '„', '…', '†', '‡',
20 'ˆ', '‰', 'Š', '‹',
21 'Œ', '?', 'Ž', '?',
22 '?', '‘', '’', '“',
23 '”', '•', '–', '—',
24 '˜', '™', 'š', '›',
25 'œ', '?', 'ž', 'Ÿ'
29 protected $charset_supersets = array(
30 'US-ASCII' => array ('ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4',
31 'ISO-8859-5', 'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8',
32 'ISO-8859-9', 'ISO-8859-10', 'ISO-8859-11', 'ISO-8859-12',
33 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'UTF-8',
34 'EUC-JP', 'EUC-', 'EUC-KR', 'EUC-CN')
37 protected static $instance = null;
40 * This class is singleton for performance reasons
43 public static function instance()
45 if(self::$instance === null)
47 self::$instance = new self();
50 return self::$instance;
53 private function __construct()
55 for($i = 0; $i < 32; $i++) {
56 $this->xml_iso88591_Entities["in"][] = chr($i);
57 $this->xml_iso88591_Entities["out"][] = "&#{$i};";
60 for($i = 160; $i < 256; $i++) {
61 $this->xml_iso88591_Entities["in"][] = chr($i);
62 $this->xml_iso88591_Entities["out"][] = "&#{$i};";
65 /*for ($i = 128; $i < 160; $i++)
67 $this->xml_cp1252_Entities['in'][] = chr($i);
73 * Convert a string to the correct XML representation in a target charset
74 * To help correct communication of non-ascii chars inside strings, regardless
75 * of the charset used when sending requests, parsing them, sending responses
76 * and parsing responses, an option is to convert all non-ascii chars present in the message
77 * into their equivalent 'charset entity'. Charset entities enumerated this way
78 * are independent of the charset encoding used to transmit them, and all XML
79 * parsers are bound to understand them.
80 * Note that in the std case we are not sending a charset encoding mime type
81 * along with http headers, so we are bound by RFC 3023 to emit strict us-ascii.
83 * @todo do a bit of basic benchmarking (strtr vs. str_replace)
84 * @todo make usage of iconv() or recode_string() or mb_string() where available
87 * @param string $src_encoding
88 * @param string $dest_encoding
91 public function encode_entities($data, $src_encoding='', $dest_encoding='')
93 if ($src_encoding == '')
95 // lame, but we know no better...
96 $src_encoding = PhpXmlRpc::$xmlrpc_internalencoding;
99 switch(strtoupper($src_encoding.'_'.$dest_encoding))
102 case 'ISO-8859-1_US-ASCII':
103 $escaped_data = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data);
104 $escaped_data = str_replace($this->xml_iso88591_Entities['in'], $this->xml_iso88591_Entities['out'], $escaped_data);
106 case 'ISO-8859-1_UTF-8':
107 $escaped_data = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data);
108 $escaped_data = utf8_encode($escaped_data);
110 case 'ISO-8859-1_ISO-8859-1':
111 case 'US-ASCII_US-ASCII':
112 case 'US-ASCII_UTF-8':
114 case 'US-ASCII_ISO-8859-1':
116 //case 'CP1252_CP1252':
117 $escaped_data = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data);
120 case 'UTF-8_US-ASCII':
121 case 'UTF-8_ISO-8859-1':
122 // NB: this will choke on invalid UTF-8, going most likely beyond EOF
124 // be kind to users creating string xmlrpcvals out of different php types
125 $data = (string) $data;
126 $ns = strlen ($data);
127 for ($nn = 0; $nn < $ns; $nn++)
134 /// @todo shall we replace this with a (supposedly) faster str_replace?
137 $escaped_data .= '"';
140 $escaped_data .= '&';
143 $escaped_data .= ''';
146 $escaped_data .= '<';
149 $escaped_data .= '>';
152 $escaped_data .= $ch;
155 //2 11 110bbbbb 10bbbbbb (2047)
156 else if ($ii>>5 == 6)
159 $ii = ord($data[$nn+1]);
161 $ii = ($b1 * 64) + $b2;
162 $ent = sprintf ('&#%d;', $ii);
163 $escaped_data .= $ent;
166 //3 16 1110bbbb 10bbbbbb 10bbbbbb
167 else if ($ii>>4 == 14)
170 $ii = ord($data[$nn+1]);
172 $ii = ord($data[$nn+2]);
174 $ii = ((($b1 * 64) + $b2) * 64) + $b3;
175 $ent = sprintf ('&#%d;', $ii);
176 $escaped_data .= $ent;
179 //4 21 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
180 else if ($ii>>3 == 30)
183 $ii = ord($data[$nn+1]);
185 $ii = ord($data[$nn+2]);
187 $ii = ord($data[$nn+3]);
189 $ii = ((((($b1 * 64) + $b2) * 64) + $b3) * 64) + $b4;
190 $ent = sprintf ('&#%d;', $ii);
191 $escaped_data .= $ent;
198 case 'CP1252_US-ASCII':
199 $escaped_data = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data);
200 $escaped_data = str_replace($this->xml_iso88591_Entities']['in'], $this->xml_iso88591_Entities['out'], $escaped_data);
201 $escaped_data = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escaped_data);
204 $escaped_data = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data);
205 /// @todo we could use real UTF8 chars here instead of xml entities... (note that utf_8 encode all allone will NOT convert them)
206 $escaped_data = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escaped_data);
207 $escaped_data = utf8_encode($escaped_data);
209 case 'CP1252_ISO-8859-1':
210 $escaped_data = str_replace(array('&', '"', "'", '<', '>'), array('&', '"', ''', '<', '>'), $data);
211 // 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...
212 $escaped_data = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escaped_data);
217 error_log("Converting from $src_encoding to $dest_encoding: not supported...");
219 return $escaped_data;
223 * Checks if a given charset encoding is present in a list of encodings or
224 * if it is a valid subset of any encoding in the list
225 * @param string $encoding charset to be tested
226 * @param string|array $validList comma separated list of valid charsets (or array of charsets)
229 public function is_valid_charset($encoding, $validList)
232 if (is_string($validList))
233 $validList = explode(',', $validList);
234 if (@in_array(strtoupper($encoding), $validList))
238 if (array_key_exists($encoding, $this->charset_supersets))
239 foreach ($validList as $allowed)
240 if (in_array($allowed, $this->charset_supersets[$encoding]))