/***************************************************************
* Copyright notice
*
-* (c) 2008 Benjamin Mack <mack@xnos.org>
+* (c) 2008-2009 Benjamin Mack <mack@xnos.org>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
protected $content = array();
protected $contentFormat = 'plain';
protected $charset = 'utf-8';
+ protected $requestCharset = 'utf-8';
/**
* sets the charset and the ID for the AJAX call
+ * due some charset limitations in Javascript (prototype uses encodeURIcomponent, which converts
+ * all data to utf-8), we need to detect if the encoding of the request differs from the
+ * backend encoding (e.g. forceCharset), and then convert all incoming data (_GET and _POST)
+ * in the expected backend encoding.
*
* @param string the AJAX id
* @return void
*/
public function __construct($ajaxId) {
- global $LANG;
- if($LANG->charSet != $this->charset) {
- $this->charset = $LANG->charSet;
+ if ($GLOBALS['LANG']->charSet != $this->charset) {
+ $this->charset = $GLOBALS['LANG']->charSet;
+ }
+
+ // get charset from current AJAX request (which is expected to be utf-8)
+ preg_match('/;\s*charset\s*=\s*([a-zA-Z0-9_-]*)/i', $_SERVER['CONTENT_TYPE'], $contenttype);
+ $charset = $GLOBALS['LANG']->csConvObj->parse_charset($contenttype[1]);
+ if ($charset && $charset != $this->requestCharset) {
+ $this->requestCharset = $charset;
+ }
+
+ // if the AJAX request does not have the same encoding like the backend
+ // we need to convert the POST and GET parameters in the right charset
+ if ($this->charset != $this->requestCharset) {
+ $GLOBALS['LANG']->csConvObj->convArray($_POST, $this->requestCharset, $this->charset);
+ $GLOBALS['LANG']->csConvObj->convArray($_GET, $this->requestCharset, $this->charset);
}
$this->ajaxId = $ajaxId;
* @return void
*/
public function setContentFormat($format) {
- if (t3lib_div::inArray(array('plain', 'xml', 'json', 'jsonhead', 'jsonbody'), $format)) {
+ if (t3lib_div::inArray(array('plain', 'xml', 'json', 'jsonhead', 'jsonbody', 'javascript'), $format)) {
$this->contentFormat = $format;
}
}
case 'json':
$this->renderAsJSON();
break;
+ case 'javascript':
+ $this->renderAsJavascript();
+ break;
case 'xml':
$this->renderAsXML();
break;
* @return void
*/
protected function renderAsError() {
+ header(t3lib_div::HTTP_STATUS_500 . ' (AJAX)');
header('Content-type: text/xml; charset='.$this->charset);
header('X-JSON: false');
die('<t3err>'.htmlspecialchars($this->errorMessage).'</t3err>');
* @return void
*/
protected function renderAsJSON() {
- $content = t3lib_div::array2json($this->content);
+ $content = json_encode($this->content);
header('Content-type: application/json; charset='.$this->charset);
header('X-JSON: '.($this->contentFormat != 'jsonbody' ? $content : true));
echo $content;
}
}
+
+ /**
+ * Renders the AJAX call as inline JSON inside a script tag. This is useful
+ * when an iframe is used as the AJAX transport.
+ *
+ * @return void
+ */
+ protected function renderAsJavascript() {
+ $content = '<script type="text/javascript">
+ /*<![CDATA[*/
+
+ response = ' . json_encode($this->content) . ';
+
+ /*]]>*/
+ </script>';
+
+ header('Content-type: text/html; charset=' . $this->charset);
+ echo $content;
+ }
}
include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/classes/class.typo3ajax.php']);
}
-?>
+?>
\ No newline at end of file