9ce2eed38badc2b8b7679790a6a4b706fa29db6b
2 /***************************************************************
5 * (c) 2008-2009 Benjamin Mack <mack@xnos.org>
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
29 * class to hold all the information about an AJAX call and send
30 * the right headers for the request type
32 * @author Benjamin Mack <mack@xnos.org>
37 protected $ajaxId = null;
38 protected $errorMessage = null;
39 protected $isError = false;
40 protected $content = array();
41 protected $contentFormat = 'plain';
42 protected $charset = 'utf-8';
43 protected $requestCharset = 'utf-8';
46 * sets the charset and the ID for the AJAX call
47 * due some charset limitations in Javascript (prototype uses encodeURIcomponent, which converts
48 * all data to utf-8), we need to detect if the encoding of the request differs from the
49 * backend encoding (e.g. forceCharset), and then convert all incoming data (_GET and _POST)
50 * in the expected backend encoding.
52 * @param string the AJAX id
55 public function __construct($ajaxId) {
57 if ($GLOBALS['LANG']->charSet
!= $this->charset
) {
58 $this->charset
= $GLOBALS['LANG']->charSet
;
61 // get charset from current AJAX request (which is expected to be utf-8)
62 preg_match('/;\s*charset\s*=\s*([a-zA-Z0-9_-]*)/i', $_SERVER['CONTENT_TYPE'], $contenttype);
63 $charset = $GLOBALS['LANG']->csConvObj
->parse_charset($contenttype[1]);
64 if ($charset && $charset != $this->requestCharset
) {
65 $this->requestCharset
= $charset;
68 // if the AJAX request does not have the same encoding like the backend
69 // we need to convert the POST and GET parameters in the right charset
70 if ($this->charset
!= $this->requestCharset
) {
71 $GLOBALS['LANG']->csConvObj
->convArray($_POST, $this->requestCharset
, $this->charset
);
72 $GLOBALS['LANG']->csConvObj
->convArray($_GET, $this->requestCharset
, $this->charset
);
75 $this->ajaxId
= $ajaxId;
80 * returns the ID for the AJAX call
82 * @return string the AJAX id
84 public function getAjaxID() {
90 * overwrites the existing content with the first parameter
92 * @param array the new content
93 * @return mixed the old content as array; if the new content was not an array, false is returned
95 public function setContent($content) {
97 if (is_array($content)) {
98 $oldcontent = $this->content
;
99 $this->content
= $content;
108 * @param string the new content key where the content should be added in the content array
109 * @param string the new content to add
110 * @return mixed the old content; if the old content didn't exist before, false is returned
112 public function addContent($key, $content) {
114 if (array_key_exists($key, $this->content
)) {
115 $oldcontent = $this->content
[$key];
117 if (!isset($content) ||
!strlen($content)) {
118 unset($this->content
[$key]);
119 } elseif (!isset($key) ||
!strlen($key)) {
120 $this->content
[] = $content;
122 $this->content
[$key] = $content;
129 * returns the content for the ajax call
131 * @return mixed the content for a specific key or the whole content
133 public function getContent($key = '') {
134 return ($key && array_key_exists($key, $this->content
) ?
$this->content
[$key] : $this->content
);
139 * sets the content format for the ajax call
141 * @param string can be one of 'plain' (default), 'xml', 'json', 'jsonbody' or 'jsonhead'
144 public function setContentFormat($format) {
145 if (t3lib_div
::inArray(array('plain', 'xml', 'json', 'jsonhead', 'jsonbody'), $format)) {
146 $this->contentFormat
= $format;
152 * sets an error message and the error flag
154 * @param string the error message
157 public function setError($errorMsg = '') {
158 $this->errorMessage
= $errorMsg;
159 $this->isError
= true;
164 * checks whether an error occured during the execution or not
166 * @return boolean whether this AJAX call had errors
168 public function isError() {
169 return $this->isError
;
174 * renders the AJAX call based on the $contentFormat variable and exits the request
178 public function render() {
179 if ($this->isError
) {
180 $this->renderAsError();
183 switch ($this->contentFormat
) {
187 $this->renderAsJSON();
190 $this->renderAsXML();
193 $this->renderAsPlain();
200 * renders the AJAX call in XML error style to handle with JS
201 * the "responseXML" of the transport object will be filled with the error message then
205 protected function renderAsError() {
206 header(t3lib_div
::HTTP_STATUS_500
. ' (AJAX)');
207 header('Content-type: text/xml; charset='.$this->charset
);
208 header('X-JSON: false');
209 die('<t3err>'.htmlspecialchars($this->errorMessage
).'</t3err>');
214 * renders the AJAX call with text/html headers
215 * the content will be available in the "responseText" value of the transport object
219 protected function renderAsPlain() {
220 header('Content-type: text/html; charset='.$this->charset
);
221 header('X-JSON: true');
222 echo implode('', $this->content
);
227 * renders the AJAX call with text/xml headers
228 * the content will be available in the "responseXML" value of the transport object
232 protected function renderAsXML() {
233 header('Content-type: text/xml; charset='.$this->charset
);
234 header('X-JSON: true');
235 echo implode('', $this->content
);
240 * renders the AJAX call with JSON evaluated headers
241 * note that you need to have requestHeaders: {Accept: 'application/json'},
242 * in your AJAX options of your AJAX request object in JS
244 * the content will be available
245 * - in the second parameter of the onSuccess / onComplete callback (except when contentFormat = 'jsonbody')
246 * - and in the xhr.responseText as a string (except when contentFormat = 'jsonhead')
247 * you can evaluate this in JS with xhr.responseText.evalJSON();
251 protected function renderAsJSON() {
252 $content = json_encode($this->content
);
254 header('Content-type: application/json; charset='.$this->charset
);
255 header('X-JSON: '.($this->contentFormat
!= 'jsonbody' ?
$content : true));
257 // bring content in xhr.responseText except when in "json head only" mode
258 if ($this->contentFormat
!= 'jsonhead') {
265 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['typo3/classes/class.typo3ajax.php']) {
266 include_once($TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['typo3/classes/class.typo3ajax.php']);