TYPO3 CMS  TYPO3_7-6
ExtDirectApi.php
Go to the documentation of this file.
1 <?php
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 
21 
26 {
30  protected $settings = [];
31 
35  public function __construct()
36  {
37  if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect']) && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'])) {
38  $this->settings = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'];
39  }
40  }
41 
56  public function getAPI(ServerRequestInterface $request, ResponseInterface $response)
57  {
58  $response->getBody()->write(json_encode([]));
59  return $response;
60  }
61 
69  public function getApiPhp(array $filterNamespaces)
70  {
71  $javascriptNamespaces = $this->getExtDirectApi($filterNamespaces);
72  // Return the generated javascript API configuration
73  if (!empty($javascriptNamespaces)) {
74  return '
75  if (!Ext.isObject(Ext.app.ExtDirectAPI)) {
76  Ext.app.ExtDirectAPI = {};
77  }
78  Ext.apply(Ext.app.ExtDirectAPI, ' . json_encode($javascriptNamespaces) . ');
79  ';
80  } else {
81  $errorMessage = $this->getNamespaceError($filterNamespaces);
82  throw new \InvalidArgumentException($errorMessage, 1297645190);
83  }
84  }
85 
93  protected function generateAPI(array $filterNamespaces)
94  {
95  $javascriptNamespaces = [];
96  if (is_array($this->settings)) {
97  foreach ($this->settings as $javascriptName => $configuration) {
98  $splittedJavascriptName = explode('.', $javascriptName);
99  $javascriptObjectName = array_pop($splittedJavascriptName);
100  $javascriptNamespace = implode('.', $splittedJavascriptName);
101  // Only items inside the wanted namespace
102  if (!$this->findNamespace($javascriptNamespace, $filterNamespaces)) {
103  continue;
104  }
105  if (!isset($javascriptNamespaces[$javascriptNamespace])) {
106  $javascriptNamespaces[$javascriptNamespace] = [
107  'url' => $this->getRoutingUrl($javascriptNamespace),
108  'type' => 'remoting',
109  'actions' => [],
110  'namespace' => $javascriptNamespace
111  ];
112  }
113  if (is_array($configuration)) {
114  $className = $configuration['callbackClass'];
115  $serverObject = GeneralUtility::getUserObj($className);
116  $javascriptNamespaces[$javascriptNamespace]['actions'][$javascriptObjectName] = [];
117  foreach (get_class_methods($serverObject) as $methodName) {
118  $reflectionMethod = new \ReflectionMethod($serverObject, $methodName);
119  $numberOfParameters = $reflectionMethod->getNumberOfParameters();
120  $docHeader = $reflectionMethod->getDocComment();
121  $formHandler = strpos($docHeader, '@formHandler') !== false;
122  $javascriptNamespaces[$javascriptNamespace]['actions'][$javascriptObjectName][] = [
123  'name' => $methodName,
124  'len' => $numberOfParameters,
125  'formHandler' => $formHandler
126  ];
127  }
128  }
129  }
130  }
131  return $javascriptNamespaces;
132  }
133 
140  public function getRoutingUrl($namespace)
141  {
142  if (TYPO3_MODE === 'FE') {
143  $url = GeneralUtility::locationHeaderUrl('?eID=ExtDirect&action=route&namespace=' . rawurlencode($namespace));
144  } else {
145  $url = BackendUtility::getAjaxUrl('ext_direct_route', ['namespace' => $namespace]);
146  }
147  return $url;
148  }
149 
156  protected function getExtDirectApi(array $filterNamespaces)
157  {
158  $noCache = (bool)GeneralUtility::_GET('no_cache');
159  // Look up into the cache
160  $cacheIdentifier = 'ExtDirectApi';
161  $cacheHash = md5($cacheIdentifier . implode(',', $filterNamespaces) . GeneralUtility::getIndpEnv('TYPO3_SSL') . serialize($this->settings) . TYPO3_MODE . GeneralUtility::getIndpEnv('HTTP_HOST'));
162  // With no_cache always generate the javascript content
163  // Generate the javascript content if it wasn't found inside the cache and cache it!
164  if ($noCache || !is_array(($javascriptNamespaces = \TYPO3\CMS\Frontend\Page\PageRepository::getHash($cacheHash)))) {
165  $javascriptNamespaces = $this->generateAPI($filterNamespaces);
166  if (!empty($javascriptNamespaces)) {
167  \TYPO3\CMS\Frontend\Page\PageRepository::storeHash($cacheHash, $javascriptNamespaces, $cacheIdentifier);
168  }
169  }
170  return $javascriptNamespaces;
171  }
172 
179  protected function getNamespaceError(array $filterNamespaces)
180  {
181  if (!empty($filterNamespaces)) {
182  // Namespace error
183  $errorMessage = sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:ExtDirect.namespaceError'), __CLASS__, implode(',', $filterNamespaces));
184  } else {
185  // No namespace given
186  $errorMessage = sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:ExtDirect.noNamespace'), __CLASS__);
187  }
188  return $errorMessage;
189  }
190 
198  protected function findNamespace($namespace, array $filterNamespaces)
199  {
200  if ($filterNamespaces === ['TYPO3']) {
201  return true;
202  }
203  $found = false;
204  foreach ($filterNamespaces as $filter) {
205  if (GeneralUtility::isFirstPartOfStr($filter, $namespace)) {
206  $found = true;
207  break;
208  }
209  }
210  return $found;
211  }
212 }
static isFirstPartOfStr($str, $partStr)
getAPI(ServerRequestInterface $request, ResponseInterface $response)
getApiPhp(array $filterNamespaces)
getNamespaceError(array $filterNamespaces)
getExtDirectApi(array $filterNamespaces)
generateAPI(array $filterNamespaces)
findNamespace($namespace, array $filterNamespaces)
static storeHash($hash, $data, $ident, $lifetime=0)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']