TYPO3  7.6
core/Classes/Http/AjaxRequestHandler.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Http;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
20 
26 {
30  protected $ajaxId = null;
31 
35  protected $errorMessage = null;
36 
40  protected $isError = false;
41 
45  protected $content = array();
46 
50  protected $contentFormat = 'plain';
51 
56  <script type="text/javascript">
57  /*<![CDATA[*/
58  response = |;
59  /*]]>*/
60  </script>
61  ';
62 
68  public function __construct($ajaxId)
69  {
70  $this->ajaxId = $ajaxId;
71  }
72 
78  public function getAjaxID()
79  {
80  return $this->ajaxId;
81  }
82 
89  public function setContent($content)
90  {
91  $oldcontent = false;
92  if (is_array($content)) {
93  $oldcontent = $this->content;
94  $this->content = $content;
95  }
96  return $oldcontent;
97  }
98 
106  public function addContent($key, $content)
107  {
108  $oldcontent = false;
109  if (array_key_exists($key, $this->content)) {
110  $oldcontent = $this->content[$key];
111  }
112  if (!isset($content) || empty($content)) {
113  unset($this->content[$key]);
114  } elseif (!isset($key) || empty($key)) {
115  $this->content[] = $content;
116  } else {
117  $this->content[$key] = $content;
118  }
119  return $oldcontent;
120  }
121 
127  public function getContent($key = '')
128  {
129  return $key && array_key_exists($key, $this->content) ? $this->content[$key] : $this->content;
130  }
131 
138  public function setContentFormat($format)
139  {
140  if (ArrayUtility::inArray(array('plain', 'xml', 'json', 'jsonhead', 'jsonbody', 'javascript'), $format)) {
141  $this->contentFormat = $format;
142  }
143  }
144 
154  {
155  $this->javascriptCallbackWrap = $javascriptCallbackWrap;
156  }
157 
164  public function setError($errorMsg = '')
165  {
166  $this->errorMessage = $errorMsg;
167  $this->isError = true;
168  }
169 
175  public function isError()
176  {
177  return $this->isError;
178  }
179 
185  public function render()
186  {
187  if ($this->isError) {
188  return $this->renderAsError();
189  }
190  switch ($this->contentFormat) {
191  case 'jsonhead':
192  case 'jsonbody':
193  case 'json':
194  return $this->renderAsJSON();
195  break;
196  case 'javascript':
197  return $this->renderAsJavascript();
198  break;
199  case 'xml':
200  return $this->renderAsXML();
201  break;
202  default:
203  return $this->renderAsPlain();
204  }
205  }
206 
213  protected function renderAsError()
214  {
216  $response = GeneralUtility::makeInstance(Response::class);
218  ->withStatus(500, ' (AJAX)')
219  ->withHeader('Content-type', 'text/xml; charset=utf-8')
220  ->withHeader('X-JSON', 'false');
221 
222  $response->getBody()->write('<t3err>' . htmlspecialchars($this->errorMessage) . '</t3err>');
223  return $response;
224  }
225 
233  protected function renderAsPlain()
234  {
236  $response = GeneralUtility::makeInstance(Response::class);
238  ->withHeader('Content-type', 'text/html; charset=utf-8')
239  ->withHeader('X-JSON', 'true');
240 
241  $response->getBody()->write(implode('', $this->content));
242  return $response;
243  }
244 
252  protected function renderAsXML()
253  {
255  $response = GeneralUtility::makeInstance(Response::class);
257  ->withHeader('Content-type', 'text/xml; charset=utf-8')
258  ->withHeader('X-JSON', 'true');
259 
260  $response->getBody()->write(implode('', $this->content));
261  return $response;
262  }
263 
277  protected function renderAsJSON()
278  {
280  $response = GeneralUtility::makeInstance(Response::class);
281  $response = $response->withHeader('Content-type', 'application/json; charset=utf-8');
282 
283  $content = json_encode($this->content);
284  // Bring content in xhr.responseText except when in "json head only" mode
285  if ($this->contentFormat === 'jsonhead') {
286  $response = $response->withHeader('X-JSON', $content);
287  } else {
288  $response = $response->withHeader('X-JSON', 'true');
289  $response->getBody()->write($content);
290  }
291  return $response;
292  }
293 
301  protected function renderAsJavascript()
302  {
303  $response = GeneralUtility::makeInstance(Response::class);
304  $response = $response->withHeader('Content-type', 'text/html; charset=utf-8');
305  $response->getBody()->write(str_replace('|', json_encode($this->content), $this->javascriptCallbackWrap));
306  return $response;
307  }
308 }