TYPO3 CMS  TYPO3_7-6
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 
76  protected function loadFile($filepath)
77  {
78  // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
79  $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
80  $this->xmlDoc = new \DOMDocument('1.0', 'utf-8');
81  $this->xmlDoc->loadXML(file_get_contents($filepath));
82  libxml_disable_entity_loader($previousValueOfEntityLoader);
83  // @TODO: oliver@typo3.org: I guess this is not required here
84  $this->xmlDoc->saveXML();
85  }
86 
92  protected function getTypes()
93  {
94  $types = $this->xmlDoc->getElementsByTagName('type');
95  $typeArr = [];
96  foreach ($types as $type) {
97  $typeId = $type->getAttribute('id');
98  $typeName = $type->getAttribute('name');
99  if (!$typeName) {
100  $typeName = $typeId;
101  }
102  $properties = $type->getElementsByTagName('property');
103  $propArr = [];
104  foreach ($properties as $property) {
105  $p = [];
106  $p['name'] = $property->getAttribute('name');
107  $p['type'] = $property->getAttribute('type');
108  $propArr[$property->getAttribute('name')] = $p;
109  }
110  $typeArr[$typeId] = [];
111  $typeArr[$typeId]['properties'] = $propArr;
112  $typeArr[$typeId]['name'] = $typeName;
113  if ($type->hasAttribute('extends')) {
114  $typeArr[$typeId]['extends'] = $type->getAttribute('extends');
115  }
116  }
117  return $typeArr;
118  }
119 
127  protected function getDescription($typeId, $parameterName = '')
128  {
130  $response = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Http\Response::class);
131  if (!$typeId) {
132  $response->getBody()->write($GLOBALS['LANG']->getLL('typeIDMissing'));
133  $response = $response->withStatus(500);
134  return $response;
135  }
136  // getElementById does only work with schema
137  $type = $this->getType($typeId);
138  // Retrieve propertyDescription
139  if ($parameterName) {
140  $properties = $type->getElementsByTagName('property');
141  foreach ($properties as $propery) {
142  $propName = $propery->getAttribute('name');
143  if ($propName == $parameterName) {
144  $descriptions = $propery->getElementsByTagName('description');
145  if ($descriptions->length) {
146  $description = $descriptions->item(0)->textContent;
147  $description = htmlspecialchars($description);
148  $description = nl2br($description);
149  $response->getBody()->write($description);
150  break;
151  }
152  }
153  }
154  }
155  return $response;
156  }
157 
164  protected function getType($typeId)
165  {
166  $types = $this->xmlDoc->getElementsByTagName('type');
167  foreach ($types as $type) {
168  if ($type->getAttribute('id') == $typeId) {
169  return $type;
170  }
171  }
172  }
173 }
processAjaxRequest(ServerRequestInterface $request, ResponseInterface $response)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']