Open Monograph Press  3.3.0
Charset.php
1 <?php
2 
3 namespace PhpXmlRpc\Helper;
4 
6 
7 class Charset
8 {
9  // tables used for transcoding different charsets into us-ascii xml
10  protected $xml_iso88591_Entities = array("in" => array(), "out" => array());
11 
15  /*
16  protected $xml_cp1252_Entities = array('in' => array(), out' => array(
17  '&#x20AC;', '?', '&#x201A;', '&#x0192;',
18  '&#x201E;', '&#x2026;', '&#x2020;', '&#x2021;',
19  '&#x02C6;', '&#x2030;', '&#x0160;', '&#x2039;',
20  '&#x0152;', '?', '&#x017D;', '?',
21  '?', '&#x2018;', '&#x2019;', '&#x201C;',
22  '&#x201D;', '&#x2022;', '&#x2013;', '&#x2014;',
23  '&#x02DC;', '&#x2122;', '&#x0161;', '&#x203A;',
24  '&#x0153;', '?', '&#x017E;', '&#x0178;'
25  ));
26  */
27 
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',),
34  );
35 
36  protected static $instance = null;
37 
43  public static function instance()
44  {
45  if (self::$instance === null) {
46  self::$instance = new self();
47  }
48 
49  return self::$instance;
50  }
51 
52  private function __construct()
53  {
54  for ($i = 0; $i < 32; $i++) {
55  $this->xml_iso88591_Entities["in"][] = chr($i);
56  $this->xml_iso88591_Entities["out"][] = "&#{$i};";
57  }
58 
59  for ($i = 160; $i < 256; $i++) {
60  $this->xml_iso88591_Entities["in"][] = chr($i);
61  $this->xml_iso88591_Entities["out"][] = "&#{$i};";
62  }
63 
64  /*for ($i = 128; $i < 160; $i++)
65  {
66  $this->xml_cp1252_Entities['in'][] = chr($i);
67  }*/
68  }
69 
90  public function encodeEntities($data, $srcEncoding = '', $destEncoding = '')
91  {
92  if ($srcEncoding == '') {
93  // lame, but we know no better...
95  }
96 
97  switch (strtoupper($srcEncoding . '_' . $destEncoding)) {
98  case 'ISO-8859-1_':
99  case 'ISO-8859-1_US-ASCII':
100  $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $data);
101  $escapedData = str_replace($this->xml_iso88591_Entities['in'], $this->xml_iso88591_Entities['out'], $escapedData);
102  break;
103  case 'ISO-8859-1_UTF-8':
104  $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $data);
105  $escapedData = utf8_encode($escapedData);
106  break;
107  case 'ISO-8859-1_ISO-8859-1':
108  case 'US-ASCII_US-ASCII':
109  case 'US-ASCII_UTF-8':
110  case 'US-ASCII_':
111  case 'US-ASCII_ISO-8859-1':
112  case 'UTF-8_UTF-8':
113  //case 'CP1252_CP1252':
114  $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $data);
115  break;
116  case 'UTF-8_':
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
120  $escapedData = '';
121  // be kind to users creating string xmlrpc values out of different php types
122  $data = (string)$data;
123  $ns = strlen($data);
124  for ($nn = 0; $nn < $ns; $nn++) {
125  $ch = $data[$nn];
126  $ii = ord($ch);
127  //1 7 0bbbbbbb (127)
128  if ($ii < 128) {
130  switch ($ii) {
131  case 34:
132  $escapedData .= '&quot;';
133  break;
134  case 38:
135  $escapedData .= '&amp;';
136  break;
137  case 39:
138  $escapedData .= '&apos;';
139  break;
140  case 60:
141  $escapedData .= '&lt;';
142  break;
143  case 62:
144  $escapedData .= '&gt;';
145  break;
146  default:
147  $escapedData .= $ch;
148  } // switch
149  } //2 11 110bbbbb 10bbbbbb (2047)
150  elseif ($ii >> 5 == 6) {
151  $b1 = ($ii & 31);
152  $ii = ord($data[$nn + 1]);
153  $b2 = ($ii & 63);
154  $ii = ($b1 * 64) + $b2;
155  $ent = sprintf('&#%d;', $ii);
156  $escapedData .= $ent;
157  $nn += 1;
158  } //3 16 1110bbbb 10bbbbbb 10bbbbbb
159  elseif ($ii >> 4 == 14) {
160  $b1 = ($ii & 15);
161  $ii = ord($data[$nn + 1]);
162  $b2 = ($ii & 63);
163  $ii = ord($data[$nn + 2]);
164  $b3 = ($ii & 63);
165  $ii = ((($b1 * 64) + $b2) * 64) + $b3;
166  $ent = sprintf('&#%d;', $ii);
167  $escapedData .= $ent;
168  $nn += 2;
169  } //4 21 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
170  elseif ($ii >> 3 == 30) {
171  $b1 = ($ii & 7);
172  $ii = ord($data[$nn + 1]);
173  $b2 = ($ii & 63);
174  $ii = ord($data[$nn + 2]);
175  $b3 = ($ii & 63);
176  $ii = ord($data[$nn + 3]);
177  $b4 = ($ii & 63);
178  $ii = ((((($b1 * 64) + $b2) * 64) + $b3) * 64) + $b4;
179  $ent = sprintf('&#%d;', $ii);
180  $escapedData .= $ent;
181  $nn += 3;
182  }
183  }
184  break;
185  /*
186  case 'CP1252_':
187  case 'CP1252_US-ASCII':
188  $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $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);
191  break;
192  case 'CP1252_UTF-8':
193  $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $data);
195  $escapedData = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escapedData);
196  $escapedData = utf8_encode($escapedData);
197  break;
198  case 'CP1252_ISO-8859-1':
199  $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $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);
202  break;
203  */
204  default:
205  $escapedData = '';
206  error_log('XML-RPC: ' . __METHOD__ . ": Converting from $srcEncoding to $destEncoding: not supported...");
207  }
208 
209  return $escapedData;
210  }
211 
221  public function isValidCharset($encoding, $validList)
222  {
223  if (is_string($validList)) {
224  $validList = explode(',', $validList);
225  }
226  if (@in_array(strtoupper($encoding), $validList)) {
227  return true;
228  } else {
229  if (array_key_exists($encoding, $this->charset_supersets)) {
230  foreach ($validList as $allowed) {
231  if (in_array($allowed, $this->charset_supersets[$encoding])) {
232  return true;
233  }
234  }
235  }
236 
237  return false;
238  }
239  }
240 
251  public function getEntities($charset)
252  {
253  switch ($charset)
254  {
255  case 'iso88591':
257  default:
258  throw new \Exception('Unsupported charset: ' . $charset);
259  }
260  }
261 
262 }
PhpXmlRpc\Helper\Charset
Definition: Charset.php:7
PhpXmlRpc\Helper
Definition: Charset.php:3
PhpXmlRpc\Helper\Charset\instance
static instance()
Definition: Charset.php:43
PhpXmlRpc\Helper\Charset\getEntities
getEntities($charset)
Definition: Charset.php:251
PhpXmlRpc\Helper\Charset\$instance
static $instance
Definition: Charset.php:36
PhpXmlRpc\Helper\Charset\isValidCharset
isValidCharset($encoding, $validList)
Definition: Charset.php:221
PhpXmlRpc\PhpXmlRpc\$xmlrpc_internalencoding
static $xmlrpc_internalencoding
Definition: PhpXmlRpc.php:76
PhpXmlRpc\Helper\Charset\$charset_supersets
$charset_supersets
Definition: Charset.php:28
PhpXmlRpc\PhpXmlRpc
Definition: PhpXmlRpc.php:5
PhpXmlRpc\Helper\Charset\encodeEntities
encodeEntities($data, $srcEncoding='', $destEncoding='')
Definition: Charset.php:90
PhpXmlRpc\Helper\Charset\$xml_iso88591_Entities
$xml_iso88591_Entities
Definition: Charset.php:10