TYPO3 CMS  TYPO3_8-7
TypoScriptReferenceLoader.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\T3editor;
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  */
19 
24 {
28  protected $xmlDoc;
29 
33  public function __construct()
34  {
35  $GLOBALS['LANG']->includeLLFile('EXT:t3editor/Resources/Private/Language/locallang.xlf');
36  }
37 
46  public function processAjaxRequest(ServerRequestInterface $request, ResponseInterface $response)
47  {
48  $parsedBody = $request->getParsedBody();
49  $queryParams = $request->getQueryParams();
50 
51  // Load the TSref XML information:
52  $this->loadFile(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('t3editor') . 'Resources/Private/tsref.xml');
53  $fetch = isset($parsedBody['fetch']) ? $parsedBody['fetch'] : $queryParams['fetch'];
54  $content = null;
55 
56  switch ($fetch) {
57  case 'types':
58  $response->getBody()->write(json_encode($this->getTypes()));
59  break;
60  case 'description':
61  $typeId = isset($parsedBody['typeId']) ? $parsedBody['typeId'] : $queryParams['typeId'];
62  $parameterName = isset($parsedBody['parameterName']) ? $parsedBody['parameterName'] : $queryParams['parameterName'];
63  $response = $this->getDescription($typeId, $parameterName);
64  $response->withHeader('Content-Type', 'text/html; charset=utf8');
65  break;
66  }
67  return $response;
68  }
69 
75  protected function loadFile($filepath)
76  {
77  // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
78  $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
79  $this->xmlDoc = new \DOMDocument('1.0', 'utf-8');
80  $this->xmlDoc->loadXML(file_get_contents($filepath));
81  libxml_disable_entity_loader($previousValueOfEntityLoader);
82  // @TODO: oliver@typo3.org: I guess this is not required here
83  $this->xmlDoc->saveXML();
84  }
85 
91  protected function getTypes()
92  {
93  $types = $this->xmlDoc->getElementsByTagName('type');
94  $typeArr = [];
95  foreach ($types as $type) {
96  $typeId = $type->getAttribute('id');
97  $typeName = $type->getAttribute('name');
98  if (!$typeName) {
99  $typeName = $typeId;
100  }
101  $properties = $type->getElementsByTagName('property');
102  $propArr = [];
103  foreach ($properties as $property) {
104  $p = [];
105  $p['name'] = $property->getAttribute('name');
106  $p['type'] = $property->getAttribute('type');
107  $propArr[$property->getAttribute('name')] = $p;
108  }
109  $typeArr[$typeId] = [];
110  $typeArr[$typeId]['properties'] = $propArr;
111  $typeArr[$typeId]['name'] = $typeName;
112  if ($type->hasAttribute('extends')) {
113  $typeArr[$typeId]['extends'] = $type->getAttribute('extends');
114  }
115  }
116  return $typeArr;
117  }
118 
126  protected function getDescription($typeId, $parameterName = '')
127  {
129  $response = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Http\Response::class);
130  if (!$typeId) {
131  $response->getBody()->write($GLOBALS['LANG']->getLL('typeIDMissing'));
132  $response = $response->withStatus(500);
133  return $response;
134  }
135  // getElementById does only work with schema
136  $type = $this->getType($typeId);
137  // Retrieve propertyDescription
138  if ($parameterName) {
139  $properties = $type->getElementsByTagName('property');
140  foreach ($properties as $propery) {
141  $propName = $propery->getAttribute('name');
142  if ($propName == $parameterName) {
143  $descriptions = $propery->getElementsByTagName('description');
144  if ($descriptions->length) {
145  $description = $descriptions->item(0)->textContent;
146  $description = htmlspecialchars($description);
147  $description = nl2br($description);
148  $response->getBody()->write($description);
149  break;
150  }
151  }
152  }
153  }
154  return $response;
155  }
156 
163  protected function getType($typeId)
164  {
165  $types = $this->xmlDoc->getElementsByTagName('type');
166  foreach ($types as $type) {
167  if ($type->getAttribute('id') == $typeId) {
168  return $type;
169  }
170  }
171  }
172 }
processAjaxRequest(ServerRequestInterface $request, ResponseInterface $response)
static makeInstance($className,... $constructorArguments)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']