WIP more support for xmlrpc-polyfill
[plcapi.git] / src / Helper / Charset.php
index a4064d8..22604d2 100644 (file)
@@ -9,9 +9,13 @@ class Charset
     // tables used for transcoding different charsets into us-ascii xml
     protected $xml_iso88591_Entities = 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
-    /// windows user from sending junk (though no luck when receiving them...)
+    /// @todo should we add to the latin-1 table the characters from cp_1252 range, i.e. 128 to 159 ?
+    ///       Those will NOT be present in true ISO-8859-1, but will save the unwary windows user from sending junk
+    ///       (though no luck when receiving them...)
+    ///       Note also that, apparently, while 'ISO/IEC 8859-1' has no characters defined for bytes 128 to 159,
+    ///       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(
         '€', '?',        '‚', 'ƒ',
@@ -37,6 +41,7 @@ class Charset
 
     /**
      * This class is singleton for performance reasons.
+     * @todo can't we just make $xml_iso88591_Entities a static variable instead ?
      *
      * @return Charset
      */
@@ -49,6 +54,9 @@ class Charset
         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
+     */
     private function __construct()
     {
         for ($i = 0; $i < 32; $i++) {
@@ -68,15 +76,14 @@ 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
@@ -94,16 +101,19 @@ class Charset
             $srcEncoding = PhpXmlRpc::$xmlrpc_internalencoding;
         }
 
-        switch (strtoupper($srcEncoding . '_' . $destEncoding)) {
+        $conversion = strtoupper($srcEncoding . '_' . $destEncoding);
+        switch ($conversion) {
             case 'ISO-8859-1_':
             case 'ISO-8859-1_US-ASCII':
                 $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $data);
                 $escapedData = str_replace($this->xml_iso88591_Entities['in'], $this->xml_iso88591_Entities['out'], $escapedData);
                 break;
+
             case 'ISO-8859-1_UTF-8':
                 $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $data);
                 $escapedData = utf8_encode($escapedData);
                 break;
+
             case 'ISO-8859-1_ISO-8859-1':
             case 'US-ASCII_US-ASCII':
             case 'US-ASCII_UTF-8':
@@ -113,6 +123,7 @@ class Charset
             //case 'CP1252_CP1252':
                 $escapedData = str_replace(array('&', '"', "'", '<', '>'), array('&amp;', '&quot;', '&apos;', '&lt;', '&gt;'), $data);
                 break;
+
             case 'UTF-8_':
             case 'UTF-8_US-ASCII':
             case 'UTF-8_ISO-8859-1':
@@ -124,7 +135,7 @@ class Charset
                 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) {
@@ -146,7 +157,7 @@ class Charset
                             default:
                                 $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]);
@@ -155,7 +166,7 @@ class Charset
                         $ent = sprintf('&#%d;', $ii);
                         $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]);
@@ -166,7 +177,7 @@ class Charset
                         $ent = sprintf('&#%d;', $ii);
                         $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]);
@@ -181,7 +192,13 @@ class Charset
                         $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':
@@ -201,17 +218,18 @@ class Charset
                 $escapedData = str_replace($this->xml_cp1252_Entities['in'], $this->xml_cp1252_Entities['out'], $escapedData);
                 break;
             */
+
             default:
                 $escapedData = '';
-                error_log("Converting from $srcEncoding to $destEncoding: not supported...");
+                Logger::instance()->errorLog('XML-RPC: ' . __METHOD__ . ": Converting from $srcEncoding to $destEncoding: not supported...");
         }
 
         return $escapedData;
     }
 
     /**
-     * Checks if a given charset encoding is present in a list of encodings or
-     * if it is a valid subset of any encoding in the list.
+     * Checks if a given charset encoding is present in a list of encodings or if it is a valid subset of any encoding
+     * in the list.
      *
      * @param string $encoding charset to be tested
      * @param string|array $validList comma separated list of valid charsets (or array of charsets)
@@ -250,6 +268,8 @@ class Charset
      */
     public function getEntities($charset)
     {
+        //trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
+
         switch ($charset)
         {
             case 'iso88591':