TYPO3 CMS  TYPO3_6-2
AjaxRequestHandler.php
Go to the documentation of this file.
1 <?php
3 
23 
24  protected $ajaxId = NULL;
25 
26  protected $errorMessage = NULL;
27 
28  protected $isError = FALSE;
29 
30  protected $content = array();
31 
32  protected $contentFormat = 'plain';
33 
34  protected $charset = 'utf-8';
35 
36  protected $requestCharset = 'utf-8';
37 
39  <script type="text/javascript">
40  /*<![CDATA[*/
41  response = |;
42  /*]]>*/
43  </script>
44  ';
45 
55  public function __construct($ajaxId) {
56  // Get charset from current AJAX request (which is expected to be utf-8)
57  preg_match('/;\\s*charset\\s*=\\s*([a-zA-Z0-9_-]*)/i', $_SERVER['CONTENT_TYPE'], $contenttype);
58  $charset = $GLOBALS['LANG']->csConvObj->parse_charset($contenttype[1]);
59  if ($charset && $charset != $this->requestCharset) {
60  $this->requestCharset = $charset;
61  }
62  // If the AJAX request does not have the same encoding like the backend
63  // we need to convert the POST and GET parameters in the right charset
64  if ($this->charset != $this->requestCharset) {
65  $GLOBALS['LANG']->csConvObj->convArray($_POST, $this->requestCharset, $this->charset);
66  $GLOBALS['LANG']->csConvObj->convArray($_GET, $this->requestCharset, $this->charset);
67  }
68  $this->ajaxId = $ajaxId;
69  }
70 
76  public function getAjaxID() {
77  return $this->ajaxId;
78  }
79 
86  public function setContent($content) {
87  $oldcontent = FALSE;
88  if (is_array($content)) {
89  $oldcontent = $this->content;
90  $this->content = $content;
91  }
92  return $oldcontent;
93  }
94 
102  public function addContent($key, $content) {
103  $oldcontent = FALSE;
104  if (array_key_exists($key, $this->content)) {
105  $oldcontent = $this->content[$key];
106  }
107  if (!isset($content) || empty($content)) {
108  unset($this->content[$key]);
109  } elseif (!isset($key) || empty($key)) {
110  $this->content[] = $content;
111  } else {
112  $this->content[$key] = $content;
113  }
114  return $oldcontent;
115  }
116 
122  public function getContent($key = '') {
123  return $key && array_key_exists($key, $this->content) ? $this->content[$key] : $this->content;
124  }
125 
132  public function setContentFormat($format) {
133  if (\TYPO3\CMS\Core\Utility\GeneralUtility::inArray(array('plain', 'xml', 'json', 'jsonhead', 'jsonbody', 'javascript'), $format)) {
134  $this->contentFormat = $format;
135  }
136  }
137 
147  $this->javascriptCallbackWrap = $javascriptCallbackWrap;
148  }
149 
156  public function setError($errorMsg = '') {
157  $this->errorMessage = $errorMsg;
158  $this->isError = TRUE;
159  }
160 
166  public function isError() {
167  return $this->isError;
168  }
169 
175  public function render() {
176  if ($this->isError) {
177  $this->renderAsError();
178  die;
179  }
180  switch ($this->contentFormat) {
181  case 'jsonhead':
182 
183  case 'jsonbody':
184 
185  case 'json':
186  $this->renderAsJSON();
187  break;
188  case 'javascript':
189  $this->renderAsJavascript();
190  break;
191  case 'xml':
192  $this->renderAsXML();
193  break;
194  default:
195  $this->renderAsPlain();
196  }
197  die;
198  }
199 
206  protected function renderAsError() {
207  header(\TYPO3\CMS\Core\Utility\HttpUtility::HTTP_STATUS_500 . ' (AJAX)');
208  header('Content-type: text/xml; charset=' . $this->charset);
209  header('X-JSON: false');
210  die('<t3err>' . htmlspecialchars($this->errorMessage) . '</t3err>');
211  }
212 
219  protected function renderAsPlain() {
220  header('Content-type: text/html; charset=' . $this->charset);
221  header('X-JSON: true');
222  echo implode('', $this->content);
223  }
224 
231  protected function renderAsXML() {
232  header('Content-type: text/xml; charset=' . $this->charset);
233  header('X-JSON: true');
234  echo implode('', $this->content);
235  }
236 
249  protected function renderAsJSON() {
250  // If the backend does not run in UTF-8 then we need to convert it to unicode as
251  // the json_encode method will return empty otherwise
252  if ($this->charset != $this->requestCharset) {
253  $GLOBALS['LANG']->csConvObj->convArray($this->content, $this->charset, $this->requestCharset);
254  }
255  $content = json_encode($this->content);
256  header('Content-type: application/json; charset=' . $this->requestCharset);
257  header('X-JSON: ' . ($this->contentFormat != 'jsonbody' ? $content : TRUE));
258  // Bring content in xhr.responseText except when in "json head only" mode
259  if ($this->contentFormat != 'jsonhead') {
260  echo $content;
261  }
262  }
263 
270  protected function renderAsJavascript() {
271  // If the backend does not run in UTF-8 then we need to convert it to unicode as
272  // the json_encode method will return empty otherwise
273  if ($this->charset != $this->requestCharset) {
274  $GLOBALS['LANG']->csConvObj->convArray($this->content, $this->charset, $this->requestCharset);
275  }
276  $content = str_replace('|', json_encode($this->content), $this->javascriptCallbackWrap);
277  header('Content-type: text/html; charset=' . $this->requestCharset);
278  echo $content;
279  }
280 
281 }
setJavascriptCallbackWrap($javascriptCallbackWrap)
die
Definition: index.php:6
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]