Skip to content

Instantly share code, notes, and snippets.

@thekid
Created July 11, 2011 18:24
Show Gist options
  • Save thekid/1076443 to your computer and use it in GitHub Desktop.
Save thekid/1076443 to your computer and use it in GitHub Desktop.
XP Framework: Patch for Issue #35
diff --git a/core/src/main/php/io/archive/zip/AbstractZipReaderImpl.class.php b/core/src/main/php/io/archive/zip/AbstractZipReaderImpl.class.php
index a025b87..4b6ac7d 100644
--- a/core/src/main/php/io/archive/zip/AbstractZipReaderImpl.class.php
+++ b/core/src/main/php/io/archive/zip/AbstractZipReaderImpl.class.php
@@ -49,7 +49,7 @@
*/
public function setPassword($password) {
$this->password= new ZipCipher();
- $this->password->initialize(iconv('iso-8859-1', 'cp437', $password));
+ $this->password->initialize(iconv(xp::CHARSET, 'cp437', $password));
}
/**
@@ -153,9 +153,9 @@
// it as-is. Do this as certain vendors (Java e.g.) always use utf-8
// but do not indicate this via EFS.
if ($header['flags'] & 2048) {
- $decoded= iconv('utf-8', 'iso-8859-1', $name);
- } else if ('' === ($decoded= @iconv('utf-8', 'iso-8859-1', $name))) {
- if ('' === ($decoded= @iconv('cp437', 'iso-8859-1', $name))) {
+ $decoded= iconv('utf-8', xp::CHARSET, $name);
+ } else if ('' === ($decoded= @iconv('utf-8', xp::CHARSET, $name))) {
+ if ('' === ($decoded= @iconv('cp437', xp::CHARSET, $name))) {
$decoded= $name;
}
}
diff --git a/core/src/main/php/io/archive/zip/ZipArchiveWriter.class.php b/core/src/main/php/io/archive/zip/ZipArchiveWriter.class.php
index ae63359..5faf289 100644
--- a/core/src/main/php/io/archive/zip/ZipArchiveWriter.class.php
+++ b/core/src/main/php/io/archive/zip/ZipArchiveWriter.class.php
@@ -66,7 +66,7 @@
*/
public function usingPassword($password) {
$this->password= new ZipCipher();
- $this->password->initialize(iconv('iso-8859-1', 'cp437', $password));
+ $this->password->initialize(iconv(xp::CHARSET, 'cp437', $password));
return $this;
}
@@ -86,7 +86,7 @@
$this->out= NULL;
$mod= $entry->getLastModified();
- $name= iconv('iso-8859-1', $this->unicode ? 'utf-8' : 'cp437', str_replace('\\', '/', $entry->getName()));
+ $name= iconv(xp::CHARSET, $this->unicode ? 'utf-8' : 'cp437', str_replace('\\', '/', $entry->getName()));
$nameLength= strlen($name);
$extraLength= 0;
$extra= '';
@@ -191,7 +191,7 @@
*/
public function writeFile($file, $size, $compressed, $crc32, $flags) {
$mod= $file->getLastModified();
- $name= iconv('iso-8859-1', $this->unicode ? 'utf-8' : 'cp437', str_replace('\\', '/', $file->getName()));
+ $name= iconv(xp::CHARSET, $this->unicode ? 'utf-8' : 'cp437', str_replace('\\', '/', $file->getName()));
$nameLength= strlen($name);
$method= $file->getCompression()->ordinal();
$extraLength= 0;
diff --git a/core/src/main/php/io/streams/TextReader.class.php b/core/src/main/php/io/streams/TextReader.class.php
index 92c71c2..a10d885 100644
--- a/core/src/main/php/io/streams/TextReader.class.php
+++ b/core/src/main/php/io/streams/TextReader.class.php
@@ -91,7 +91,7 @@
// Fall back to ISO-8859-1
$this->buf= $c;
$this->bom= 0;
- return 'iso-8859-1';
+ return xp::CHARSET;
}
/**
diff --git a/core/src/main/php/io/streams/TextWriter.class.php b/core/src/main/php/io/streams/TextWriter.class.php
index 484b36e..086a330 100644
--- a/core/src/main/php/io/streams/TextWriter.class.php
+++ b/core/src/main/php/io/streams/TextWriter.class.php
@@ -25,7 +25,7 @@
* @param io.streams.OutputStream stream
* @param string charset the charset the stream is encoded in.
*/
- public function __construct(OutputStream $stream, $charset= 'iso-8859-1') {
+ public function __construct(OutputStream $stream, $charset= xp::CHARSET) {
parent::__construct($stream);
$this->charset= $charset;
}
@@ -83,7 +83,7 @@
public function write($text) {
$this->stream->write($text instanceof String || $text instanceof Character
? $text->getBytes($this->charset)
- : iconv('iso-8859-1', $this->charset, $text)
+ : iconv(xp::CHARSET, $this->charset, $text)
);
}
@@ -95,7 +95,7 @@
public function writeLine($text= '') {
$this->stream->write(($text instanceof String || $text instanceof Character
? $text->getBytes($this->charset)
- : iconv('iso-8859-1', $this->charset, $text)
+ : iconv(xp::CHARSET, $this->charset, $text)
).$this->newLine);
}
}
diff --git a/core/src/main/php/lang.base.php b/core/src/main/php/lang.base.php
index b2cdb9f..d015edb 100644
--- a/core/src/main/php/lang.base.php
+++ b/core/src/main/php/lang.base.php
@@ -7,8 +7,9 @@
// {{{ final class xp
final class xp {
const CLASS_FILE_EXT= '.class.php';
+ const CHARSET = 'iso-8859-1';
- public static $registry = array(
+ public static $registry = array(
'errors' => array(),
'sapi' => array(),
'class.xp' => '<xp>',
diff --git a/core/src/main/php/lang/types/Character.class.php b/core/src/main/php/lang/types/Character.class.php
index 3ec1a17..79dc422 100644
--- a/core/src/main/php/lang/types/Character.class.php
+++ b/core/src/main/php/lang/types/Character.class.php
@@ -13,9 +13,9 @@
* <code>
* $c= new Character(8364); // The EUR symbol (U+20AC)
* $c= new Character(0x20AC); // ...same, using hexadecimal
- * $c= new Character('Ä', 'ISO-8859-1'); // The German Umlaut A (capital)
+ * $c= new Character('Ä', xp::CHARSET); // The German Umlaut A (capital)
*
- * $s= new String('Übercoder', 'ISO-8859-1');
+ * $s= new String('Übercoder', xp::CHARSET);
* $c= $s->charAt(0); // The German Umlaut U (capital)
* $c= $s[0]; // ...same, via [] operator
*
diff --git a/core/src/main/php/peer/http/FormData.class.php b/core/src/main/php/peer/http/FormData.class.php
index 337d073..68cceda 100644
--- a/core/src/main/php/peer/http/FormData.class.php
+++ b/core/src/main/php/peer/http/FormData.class.php
@@ -15,7 +15,7 @@
const
CRLF = "\r\n",
DEFAULT_CONTENTTYPE = 'text/plain',
- DEFAULT_CHARSET = 'iso-8859-1';
+ DEFAULT_CHARSET = xp::CHARSET;
protected
$name = NULL,
diff --git a/core/src/main/php/peer/mail/InternetAddress.class.php b/core/src/main/php/peer/mail/InternetAddress.class.php
index 49d81f0..8055be0 100644
--- a/core/src/main/php/peer/mail/InternetAddress.class.php
+++ b/core/src/main/php/peer/mail/InternetAddress.class.php
@@ -129,10 +129,10 @@
* - Empty personal: <friebe@example.com>
* </pre>
*
- * @param string charset default 'iso-8859-1'
+ * @param string charset default xp::CHARSET
* @return string
*/
- public function toString($charset= 'iso-8859-1') {
+ public function toString($charset= xp::CHARSET) {
return (
empty($this->personal) ? '' :
QuotedPrintable::encode($this->personal, $charset).' '
diff --git a/core/src/main/php/peer/mail/Message.class.php b/core/src/main/php/peer/mail/Message.class.php
index 662031f..9c86d9f 100644
--- a/core/src/main/php/peer/mail/Message.class.php
+++ b/core/src/main/php/peer/mail/Message.class.php
@@ -78,7 +78,7 @@
$subject = '',
$priority = MAIL_PRIORITY_NORMAL,
$contenttype = 'text/plain',
- $charset = 'iso-8859-1',
+ $charset = xp::CHARSET,
$encoding = '8bit',
$folder = NULL,
$uid = 0,
diff --git a/core/src/main/php/peer/mail/MimePart.class.php b/core/src/main/php/peer/mail/MimePart.class.php
index 4b8a017..108dbf1 100644
--- a/core/src/main/php/peer/mail/MimePart.class.php
+++ b/core/src/main/php/peer/mail/MimePart.class.php
@@ -56,7 +56,7 @@
$this->disposition= MIME_DISPOSITION_ATTACHMENT;
$this->charset= '';
} else {
- $this->charset= 'iso-8859-1';
+ $this->charset= xp::CHARSET;
}
}
diff --git a/core/src/main/php/scriptlet/LocaleNegotiator.class.php b/core/src/main/php/scriptlet/LocaleNegotiator.class.php
index cf78ffe..d2f7cfa 100644
--- a/core/src/main/php/scriptlet/LocaleNegotiator.class.php
+++ b/core/src/main/php/scriptlet/LocaleNegotiator.class.php
@@ -25,8 +25,8 @@
* $default= 'de_DE'
* ),
* $negotiator->getCharset(
- * $supported= array('iso-8859-1', 'utf-8'),
- * $default= 'iso-8859-1'
+ * $supported= array(xp::CHARSET, 'utf-8'),
+ * $default= xp::CHARSET
* )
* );
* </code>
diff --git a/core/src/main/php/scriptlet/rpc/AbstractRpcRequest.class.php b/core/src/main/php/scriptlet/rpc/AbstractRpcRequest.class.php
index a9a4fee..f78e1ad 100644
--- a/core/src/main/php/scriptlet/rpc/AbstractRpcRequest.class.php
+++ b/core/src/main/php/scriptlet/rpc/AbstractRpcRequest.class.php
@@ -48,7 +48,7 @@
return substr($type, $pos+ 8);
}
- return 'iso-8859-1';
+ return xp::CHARSET;
}
}
?>
diff --git a/core/src/main/php/text/encode/QuotedPrintable.class.php b/core/src/main/php/text/encode/QuotedPrintable.class.php
index 3ad0a79..3660992 100644
--- a/core/src/main/php/text/encode/QuotedPrintable.class.php
+++ b/core/src/main/php/text/encode/QuotedPrintable.class.php
@@ -55,10 +55,10 @@
* Encode string
*
* @param string str
- * @param string charset default 'iso-8859-1'
+ * @param string charset default xp::CHARSET
* @return string
*/
- public static function encode($str, $charset= 'iso-8859-1') {
+ public static function encode($str, $charset= xp::CHARSET) {
$r= array(' ' => '_');
foreach (QuotedPrintable::getCharsToEncode() as $i) {
$r[chr($i)]= '='.strtoupper(dechex($i));
diff --git a/core/src/main/php/text/regex/Pattern.class.php b/core/src/main/php/text/regex/Pattern.class.php
index b6f7f55..a4cabae 100644
--- a/core/src/main/php/text/regex/Pattern.class.php
+++ b/core/src/main/php/text/regex/Pattern.class.php
@@ -93,7 +93,7 @@
*/
public function matches($input) {
if ($input instanceof String) {
- $n= preg_match($this->regex, (string)$input->getBytes($this->utf8 ? 'utf-8' : 'iso-8859-1'));
+ $n= preg_match($this->regex, (string)$input->getBytes($this->utf8 ? 'utf-8' : xp::CHARSET));
} else {
$n= preg_match($this->regex, (string)$input);
}
@@ -114,7 +114,7 @@
*/
public function match($input) {
if ($input instanceof String) {
- $n= preg_match_all($this->regex, (string)$input->getBytes($this->utf8 ? 'utf-8' : 'iso-8859-1'), $m, PREG_SET_ORDER);
+ $n= preg_match_all($this->regex, (string)$input->getBytes($this->utf8 ? 'utf-8' : xp::CHARSET), $m, PREG_SET_ORDER);
} else {
$n= preg_match_all($this->regex, (string)$input, $m, PREG_SET_ORDER);
}
@@ -136,7 +136,7 @@
*/
public function replaceWith($replacement, $input) {
if ($input instanceof String) {
- $r= preg_replace($this->regex, $replacement, (string)$input->getBytes($this->utf8 ? 'utf-8' : 'iso-8859-1'));
+ $r= preg_replace($this->regex, $replacement, (string)$input->getBytes($this->utf8 ? 'utf-8' : xp::CHARSET));
} else {
$r= preg_replace($this->regex, $replacement, (string)$input);
}
diff --git a/core/src/main/php/webservices/json/JsonDecoder.class.php b/core/src/main/php/webservices/json/JsonDecoder.class.php
index 4c0103e..b1661c6 100644
--- a/core/src/main/php/webservices/json/JsonDecoder.class.php
+++ b/core/src/main/php/webservices/json/JsonDecoder.class.php
@@ -34,8 +34,9 @@
public
$stream = NULL;
-
- public
+
+ protected
+ $charset = 'utf-8',
$_tokenValue = NULL;
/**
@@ -58,7 +59,7 @@
);
switch (gettype($data)) {
case 'string': {
- return '"'.strtr(utf8_encode($data), $controlChars).'"';
+ return '"'.strtr(iconv(xp::CHARSET, $this->charset, $data), $controlChars).'"';
}
case 'integer': {
return (string)$data;
@@ -90,7 +91,7 @@
$data= array_merge(
array(
'__jsonclass__' => array('__construct()'),
- '__xpclass__' => utf8_encode($data->getClassName())
+ '__xpclass__' => iconv(xp::CHARSET, $this->charset, $data->getClassName())
),
$vars
);
@@ -369,7 +370,9 @@
switch ($tok) {
case '"': {
$this->stream->seek($initpos + $offset);
- return (string)$ret->getBytes('ISO-8859-15');
+
+ // Not using xp::CHARSET so we support the EUR sign
+ return (string)$ret->getBytes('iso-8859-15');
}
case '\\': {
@@ -379,7 +382,7 @@
}
default: {
- $ret->concat(new String($tok, 'UTF-8'));
+ $ret->concat(new String($tok, $this->charset));
$tok= '';
break;
}
diff --git a/core/src/main/php/webservices/soap/native/NativeSoapClient.class.php b/core/src/main/php/webservices/soap/native/NativeSoapClient.class.php
index d780c50..272066d 100644
--- a/core/src/main/php/webservices/soap/native/NativeSoapClient.class.php
+++ b/core/src/main/php/webservices/soap/native/NativeSoapClient.class.php
@@ -27,7 +27,7 @@
$cat = NULL,
$version = NULL,
$location = NULL,
- $encoding = 'iso-8859-1',
+ $encoding = xp::CHARSET,
$style = SOAP_RPC,
$use = SOAP_ENCODED;
diff --git a/core/src/main/php/webservices/soap/xp/XPSoapClient.class.php b/core/src/main/php/webservices/soap/xp/XPSoapClient.class.php
index 91f572e..7efd3fd 100644
--- a/core/src/main/php/webservices/soap/xp/XPSoapClient.class.php
+++ b/core/src/main/php/webservices/soap/xp/XPSoapClient.class.php
@@ -22,7 +22,7 @@
*/
class XPSoapClient extends Object implements ISoapClient, Traceable {
protected
- $encoding = 'iso-8859-1',
+ $encoding = xp::CHARSET,
$transport = NULL,
$action = '',
$targetNamespace = NULL,
diff --git a/core/src/main/php/webservices/xmlrpc/XmlRpcMessage.class.php b/core/src/main/php/webservices/xmlrpc/XmlRpcMessage.class.php
index 9935e88..aa8d269 100644
--- a/core/src/main/php/webservices/xmlrpc/XmlRpcMessage.class.php
+++ b/core/src/main/php/webservices/xmlrpc/XmlRpcMessage.class.php
@@ -75,7 +75,7 @@
* @return string
*/
public function getEncoding() {
- return 'iso-8859-1';
+ return xp::CHARSET;
}
/**
diff --git a/core/src/main/php/xml/Node.class.php b/core/src/main/php/xml/Node.class.php
index cd4e510..765bb6a 100644
--- a/core/src/main/php/xml/Node.class.php
+++ b/core/src/main/php/xml/Node.class.php
@@ -227,17 +227,17 @@
* </pre>
*
* @param int indent default INDENT_WRAPPED
- * @param string encoding default 'iso-8859-1'
+ * @param string encoding default xp::CHARSET
* @param string inset default ''
* @return string XML
*/
- public function getSource($indent= INDENT_WRAPPED, $encoding= 'iso-8859-1', $inset= '') {
+ public function getSource($indent= INDENT_WRAPPED, $encoding= xp::CHARSET, $inset= '') {
$xml= $inset.'<'.$this->name;
- $conv= 'iso-8859-1' != $encoding;
+ $conv= xp::CHARSET != $encoding;
if ('string' == ($type= gettype($this->content))) {
$content= $conv
- ? iconv('iso-8859-1', $encoding, htmlspecialchars($this->content))
+ ? iconv(xp::CHARSET, $encoding, htmlspecialchars($this->content))
: htmlspecialchars($this->content)
;
} else if ('float' == $type) {
@@ -247,12 +247,12 @@
;
} else if ($this->content instanceof PCData) {
$content= $conv
- ? iconv('iso-8859-1', $encoding, $this->content->pcdata)
+ ? iconv(xp::CHARSET, $encoding, $this->content->pcdata)
: $this->content->pcdata
;
} else if ($this->content instanceof CData) {
$content= '<![CDATA['.str_replace(']]>', ']]]]><![CDATA[>', $conv
- ? iconv('iso-8859-1', $encoding, $this->content->cdata)
+ ? iconv(xp::CHARSET, $encoding, $this->content->cdata)
: $this->content->cdata
).']]>';
} else if ($this->content instanceof String) {
@@ -264,7 +264,7 @@
if (INDENT_NONE === $indent) {
foreach ($this->attribute as $key => $value) {
$xml.= ' '.$key.'="'.htmlspecialchars($conv
- ? iconv('iso-8859-1', $encoding, $value)
+ ? iconv(xp::CHARSET, $encoding, $value)
: $value
).'"';
}
@@ -278,7 +278,7 @@
$sep= (sizeof($this->attribute) < 3) ? '' : "\n".$inset;
foreach ($this->attribute as $key => $value) {
$xml.= $sep.' '.$key.'="'.htmlspecialchars($conv
- ? iconv('iso-8859-1', $encoding, $value)
+ ? iconv(xp::CHARSET, $encoding, $value)
: $value
).'"';
}
diff --git a/core/src/main/php/xml/Tree.class.php b/core/src/main/php/xml/Tree.class.php
index 4718d95..303dc74 100644
--- a/core/src/main/php/xml/Tree.class.php
+++ b/core/src/main/php/xml/Tree.class.php
@@ -30,7 +30,7 @@
protected
$version = '1.0',
- $encoding = 'iso-8859-1';
+ $encoding = xp::CHARSET;
/**
* Constructor
diff --git a/core/src/main/php/xml/io/XmlStreamWriter.class.php b/core/src/main/php/xml/io/XmlStreamWriter.class.php
index bb131df..9ce83ee 100644
--- a/core/src/main/php/xml/io/XmlStreamWriter.class.php
+++ b/core/src/main/php/xml/io/XmlStreamWriter.class.php
@@ -31,7 +31,7 @@
* @param string encoding default "iso-8859-1"
* @param bool standalone default FALSE
*/
- public function startDocument($version= '1.0', $encoding= 'iso-8859-1', $standalone= FALSE) {
+ public function startDocument($version= '1.0', $encoding= xp::CHARSET, $standalone= FALSE) {
$this->stream->write(sprintf(
'<?xml version="%s" encoding="%s"%s?>',
$version,
diff --git a/core/src/main/php/xml/io/XmlWriter.class.php b/core/src/main/php/xml/io/XmlWriter.class.php
index 71c3c11..d075c39 100644
--- a/core/src/main/php/xml/io/XmlWriter.class.php
+++ b/core/src/main/php/xml/io/XmlWriter.class.php
@@ -19,7 +19,7 @@
* @param string encoding default "iso-8859-1"
* @param bool standalone default FALSE
*/
- public abstract function startDocument($version= '1.0', $encoding= 'iso-8859-1', $standalone= FALSE);
+ public abstract function startDocument($version= '1.0', $encoding= xp::CHARSET, $standalone= FALSE);
/**
* Close document. Will close all opened tags.
diff --git a/core/src/main/php/xml/parser/XMLParser.class.php b/core/src/main/php/xml/parser/XMLParser.class.php
index 19433a4..dd8b5e4 100644
--- a/core/src/main/php/xml/parser/XMLParser.class.php
+++ b/core/src/main/php/xml/parser/XMLParser.class.php
@@ -35,9 +35,9 @@
/**
* Constructor
*
- * @param string encoding default 'ISO-8859-1'
+ * @param string encoding default xp::CHARSET
*/
- public function __construct($encoding= 'ISO-8859-1') {
+ public function __construct($encoding= xp::CHARSET) {
$this->encoding= $encoding;
}
diff --git a/core/src/test/php/net/xp_framework/unittest/xml/TreeTest.class.php b/core/src/test/php/net/xp_framework/unittest/xml/TreeTest.class.php
index 88dc75e..ee9c239 100644
--- a/core/src/test/php/net/xp_framework/unittest/xml/TreeTest.class.php
+++ b/core/src/test/php/net/xp_framework/unittest/xml/TreeTest.class.php
@@ -104,7 +104,7 @@
<document><node>Some umlauts: öäü</node></document>
');
- $this->assertEquals('ISO-8859-1', $tree->getEncoding());
+ $this->assertEquals('iso-8859-1', $tree->getEncoding());
$this->assertEquals(1, sizeof($tree->root->children));
$this->assertEquals('document', $tree->root->getName());
$this->assertEquals('Some umlauts: öäü', $tree->root->children[0]->getContent());
@@ -120,7 +120,7 @@
<document><node>Some umlauts: öäü</node></document>
');
- $this->assertEquals('ISO-8859-1', $tree->getEncoding());
+ $this->assertEquals('iso-8859-1', $tree->getEncoding());
$this->assertEquals(1, sizeof($tree->root->children));
$this->assertEquals('document', $tree->root->getName());
$this->assertEquals('Some umlauts: öäü', $tree->root->children[0]->getContent());
@thekid
Copy link
Author

thekid commented Jul 11, 2011

This only changes 'iso-8859-1' => xp::CHARSET (global search / replace), utf8_(en|de)code is missing but trivial to implement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment