Ejegg has submitted this change and it was merged.

Change subject: Transliterate Worldpay post to ISO-8859-1
......................................................................


Transliterate Worldpay post to ISO-8859-1

Change-Id: I0d85caf2cd67b4326fdc158c5e0498bdb40c2c2e
---
M DonationInterface.php
A gateway_common/EncodingMangler.php
M gateway_common/gateway.adapter.php
M worldpay_gateway/worldpay.adapter.php
4 files changed, 86 insertions(+), 4 deletions(-)

Approvals:
  Ejegg: Looks good to me, approved



diff --git a/DonationInterface.php b/DonationInterface.php
index 2473898..14d44ac 100644
--- a/DonationInterface.php
+++ b/DonationInterface.php
@@ -101,6 +101,7 @@
 $wgDonationInterfaceClassMap = array();
 
 $wgAutoloadClasses['DonationData'] = $donationinterface_dir . 
'gateway_common/DonationData.php';
+$wgAutoloadClasses['EncodingMangler'] = $donationinterface_dir . 
'gateway_common/EncodingMangler.php';
 $wgAutoloadClasses['GatewayAdapter'] = $donationinterface_dir . 
'gateway_common/gateway.adapter.php';
 $wgAutoloadClasses['GatewayForm'] = $donationinterface_dir . 
'gateway_common/GatewayForm.php';
 $wgAutoloadClasses['DataValidator'] = $donationinterface_dir . 
'gateway_common/DataValidator.php';
diff --git a/gateway_common/EncodingMangler.php 
b/gateway_common/EncodingMangler.php
new file mode 100644
index 0000000..d2f5fae
--- /dev/null
+++ b/gateway_common/EncodingMangler.php
@@ -0,0 +1,62 @@
+<?php
+/**
+ * Wikimedia Foundation
+ *
+ * LICENSE
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ */
+
+/**
+ * Forces the languages of the world to fit into a teensy little codespace
+ * so we can send them in ISO-8859-1 encoding.
+ * Uses Transliterator if available to turn non-latin characters into something
+ * meaningful.  If not available, iconv will just replace em with question 
marks
+ */
+class EncodingMangler {
+       private static $instance = null;
+       private $use_transliterator = false;
+       private $transliterator;
+
+       private function __construct() {
+               if ( class_exists( 'Transliterator' ) ) {
+                       $this->use_transliterator = true;
+                       // Use Any-Latin to munge Cyrillic, Kanji, etc
+                       // Then convert anything outside the ISO-8859-1 range 
to nearest ASCII
+                       $this->transliterator = 
Transliterator::create('Any-Latin; [^a-ΓΏ] Latin-ASCII');
+               }
+       }
+
+       /**
+        * @return EncodingMangler
+        */
+       public static function singleton() {
+               if ( self::$instance == null ) {
+                       self::$instance = new EncodingMangler();
+               }
+               return self::$instance;
+       }
+
+       /**
+        * Forces string into ISO-8859-1 space
+        *
+        * @param string $value UTF-8
+        * @return string still UTF-8, but only includes chars from 00-ff
+        */
+       public function transliterate( $value ) {
+               if ( $this->use_transliterator ) {
+                       return $this->transliterator->transliterate( $value );
+               }
+               $iso = iconv( 'UTF-8', 'ISO-8859-1//TRANSLIT', $value );
+               return iconv( 'ISO-8859-1', 'UTF-8', $iso );
+       }
+}
\ No newline at end of file
diff --git a/gateway_common/gateway.adapter.php 
b/gateway_common/gateway.adapter.php
index ba58c25..cac2b51 100644
--- a/gateway_common/gateway.adapter.php
+++ b/gateway_common/gateway.adapter.php
@@ -821,8 +821,8 @@
         * @return string The raw transaction in xml format, ready to be 
         * curl'd off to the remote server. 
         */
-       protected function buildRequestXML( $rootElement = 'XML' ) {
-               $this->xmlDoc = new DomDocument( '1.0', 'UTF-8' );
+       protected function buildRequestXML( $rootElement = 'XML', $encoding = 
'UTF-8' ) {
+               $this->xmlDoc = new DomDocument( '1.0', $encoding );
                $node = $this->xmlDoc->createElement( $rootElement );
 
                // Look up the request structure for our current transaction 
type in the transactions array
diff --git a/worldpay_gateway/worldpay.adapter.php 
b/worldpay_gateway/worldpay.adapter.php
index 623c8cb..90124a0 100644
--- a/worldpay_gateway/worldpay.adapter.php
+++ b/worldpay_gateway/worldpay.adapter.php
@@ -836,8 +836,27 @@
                return $data;
        }
 
-       protected function buildRequestXML( $rootElement = 'TMSTN' ) {
-               return 'StringIn=' . str_replace( "\n", '', 
parent::buildRequestXML( $rootElement ) );
+       // WorldPay is apparently not very worldly in the ways of alphabets
+       protected function buildRequestXML( $rootElement = 'TMSTN', $encoding = 
'ISO-8859-1' ) {
+               $xml = parent::buildRequestXML( $rootElement, $encoding );
+               return 'StringIn=' . str_replace( "\n", '', $xml );
+       }
+
+       // override the charset from the parent function
+       protected function getTransactionSpecificValue( $gateway_field_name, 
$token = false ) {
+               $original = parent::getTransactionSpecificValue( 
$gateway_field_name, $token );
+               return EncodingMangler::singleton()->transliterate( $original );
+       }
+
+       // override the charset from the parent function
+       function getCurlBaseHeaders() {
+               $headers = parent::getCurlBaseHeaders();
+               foreach ( $headers as $index => $header ) {
+                       if ( substr( $header, 0, 13 ) == 'Content-Type:' ) {
+                               $headers[$index] = preg_replace( 
'/\bcharset=utf-8\b/', 'charset=iso-8859-1', $header, 1 );
+                       }
+               }
+               return $headers;
        }
 
        protected function stage_returnto( $type = 'request' ) {

-- 
To view, visit https://gerrit.wikimedia.org/r/156866
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I0d85caf2cd67b4326fdc158c5e0498bdb40c2c2e
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/DonationInterface
Gerrit-Branch: deploy-payments_1.22
Gerrit-Owner: Ejegg <[email protected]>
Gerrit-Reviewer: Awight <[email protected]>
Gerrit-Reviewer: Ejegg <[email protected]>
Gerrit-Reviewer: Ssmith <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to