TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
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 
17 use Psr\Http\Message\ResponseInterface;
20 
27 {
31  protected $ajaxId = null;
32 
36  protected $errorMessage = null;
37 
41  protected $isError = false;
42 
46  protected $content = [];
47 
51  protected $contentFormat = 'plain';
52 
57  <script type="text/javascript">
58  /*<![CDATA[*/
59  response = |;
60  /*]]>*/
61  </script>
62  ';
63 
69  public function __construct($ajaxId)
70  {
71  $this->ajaxId = $ajaxId;
72  }
73 
79  public function getAjaxID()
80  {
81  return $this->ajaxId;
82  }
83 
90  public function setContent($content)
91  {
92  $oldcontent = false;
93  if (is_array($content)) {
94  $oldcontent = $this->content;
95  $this->content = $content;
96  }
97  return $oldcontent;
98  }
99 
107  public function addContent($key, $content)
108  {
109  $oldcontent = false;
110  if (array_key_exists($key, $this->content)) {
111  $oldcontent = $this->content[$key];
112  }
113  if (!isset($content) || empty($content)) {
114  unset($this->content[$key]);
115  } elseif (!isset($key) || empty($key)) {
116  $this->content[] = $content;
117  } else {
118  $this->content[$key] = $content;
119  }
120  return $oldcontent;
121  }
122 
128  public function getContent($key = '')
129  {
130  return $key && array_key_exists($key, $this->content) ? $this->content[$key] : $this->content;
131  }
132 
139  public function setContentFormat($format)
140  {
141  if (ArrayUtility::inArray(['plain', 'xml', 'json', 'jsonhead', 'jsonbody', 'javascript'], $format)) {
142  $this->contentFormat = $format;
143  }
144  }
145 
155  {
156  $this->javascriptCallbackWrap = $javascriptCallbackWrap;
157  }
158 
165  public function setError($errorMsg = '')
166  {
167  $this->errorMessage = $errorMsg;
168  $this->isError = true;
169  }
170 
176  public function isError()
177  {
178  return $this->isError;
179  }
180 
186  public function render()
187  {
188  if ($this->isError) {
189  return $this->renderAsError();
190  }
191  switch ($this->contentFormat) {
192  case 'jsonhead':
193  case 'jsonbody':
194  case 'json':
195  return $this->renderAsJSON();
196  break;
197  case 'javascript':
198  return $this->renderAsJavascript();
199  break;
200  case 'xml':
201  return $this->renderAsXML();
202  break;
203  default:
204  return $this->renderAsPlain();
205  }
206  }
207 
214  protected function renderAsError()
215  {
217  $response = GeneralUtility::makeInstance(Response::class);
218  $response = $response
219  ->withStatus(500, ' (AJAX)')
220  ->withHeader('Content-Type', 'text/xml; charset=utf-8')
221  ->withHeader('X-JSON', 'false');
222 
223  $response->getBody()->write('<t3err>' . htmlspecialchars($this->errorMessage) . '</t3err>');
224  return $response;
225  }
226 
234  protected function renderAsPlain()
235  {
237  $response = GeneralUtility::makeInstance(Response::class);
238  $response = $response
239  ->withHeader('Content-Type', 'text/html; charset=utf-8')
240  ->withHeader('X-JSON', 'true');
241 
242  $response->getBody()->write(implode('', $this->content));
243  return $response;
244  }
245 
253  protected function renderAsXML()
254  {
256  $response = GeneralUtility::makeInstance(Response::class);
257  $response = $response
258  ->withHeader('Content-Type', 'text/xml; charset=utf-8')
259  ->withHeader('X-JSON', 'true');
260 
261  $response->getBody()->write(implode('', $this->content));
262  return $response;
263  }
264 
278  protected function renderAsJSON()
279  {
281  $response = GeneralUtility::makeInstance(Response::class);
282  $response = $response->withHeader('Content-Type', 'application/json; charset=utf-8');
283 
284  $content = json_encode($this->content);
285  // Bring content in xhr.responseText except when in "json head only" mode
286  if ($this->contentFormat === 'jsonhead') {
287  $response = $response->withHeader('X-JSON', $content);
288  } else {
289  $response = $response->withHeader('X-JSON', 'true');
290  $response->getBody()->write($content);
291  }
292  return $response;
293  }
294 
302  protected function renderAsJavascript()
303  {
304  $response = GeneralUtility::makeInstance(Response::class);
305  $response = $response->withHeader('Content-Type', 'text/html; charset=utf-8');
306  $response->getBody()->write(str_replace('|', json_encode($this->content), $this->javascriptCallbackWrap));
307  return $response;
308  }
309 }
static makeInstance($className,...$constructorArguments)