TYPO3 CMS  TYPO3_6-2
ExtDirectApi.php
Go to the documentation of this file.
1 <?php
3 
22 class ExtDirectApi {
23 
27  protected $settings = array();
28 
32  public function __construct() {
33  if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect']) && is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'])) {
34  $this->settings = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'];
35  }
36  }
37 
52  public function getAPI($ajaxParams, \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxObj) {
53  $ajaxObj->setContent(array());
54  }
55 
63  public function getApiPhp(array $filterNamespaces) {
64  $javascriptNamespaces = $this->getExtDirectApi($filterNamespaces);
65  // Return the generated javascript API configuration
66  if (count($javascriptNamespaces)) {
67  return '
68  if (!Ext.isObject(Ext.app.ExtDirectAPI)) {
69  Ext.app.ExtDirectAPI = {};
70  }
71  Ext.apply(Ext.app.ExtDirectAPI, ' . json_encode($javascriptNamespaces) . ');
72  ';
73  } else {
74  $errorMessage = $this->getNamespaceError($filterNamespaces);
75  throw new \InvalidArgumentException($errorMessage, 1297645190);
76  }
77  }
78 
86  protected function generateAPI(array $filterNamespaces) {
87  $javascriptNamespaces = array();
88  if (is_array($this->settings)) {
89  foreach ($this->settings as $javascriptName => $configuration) {
90  $splittedJavascriptName = explode('.', $javascriptName);
91  $javascriptObjectName = array_pop($splittedJavascriptName);
92  $javascriptNamespace = implode('.', $splittedJavascriptName);
93  // Only items inside the wanted namespace
94  if (!$this->findNamespace($javascriptNamespace, $filterNamespaces)) {
95  continue;
96  }
97  if (!isset($javascriptNamespaces[$javascriptNamespace])) {
98  $javascriptNamespaces[$javascriptNamespace] = array(
99  'url' => $this->getRoutingUrl($javascriptNamespace),
100  'type' => 'remoting',
101  'actions' => array(),
102  'namespace' => $javascriptNamespace
103  );
104  }
105  if (is_array($configuration)) {
106  $className = $configuration['callbackClass'];
107  $serverObject = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($className, FALSE);
108  $javascriptNamespaces[$javascriptNamespace]['actions'][$javascriptObjectName] = array();
109  foreach (get_class_methods($serverObject) as $methodName) {
110  $reflectionMethod = new \ReflectionMethod($serverObject, $methodName);
111  $numberOfParameters = $reflectionMethod->getNumberOfParameters();
112  $docHeader = $reflectionMethod->getDocComment();
113  $formHandler = strpos($docHeader, '@formHandler') !== FALSE;
114  $javascriptNamespaces[$javascriptNamespace]['actions'][$javascriptObjectName][] = array(
115  'name' => $methodName,
116  'len' => $numberOfParameters,
117  'formHandler' => $formHandler
118  );
119  }
120  }
121  }
122  }
123  return $javascriptNamespaces;
124  }
125 
132  public function getRoutingUrl($namespace) {
133  $url = '';
134  if (TYPO3_MODE === 'FE') {
135  $url = \TYPO3\CMS\Core\Utility\GeneralUtility::locationHeaderUrl('?eID=ExtDirect&action=route&namespace=');
136  } else {
137  $url = \TYPO3\CMS\Core\Utility\GeneralUtility::locationHeaderUrl(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . TYPO3_mainDir . 'ajax.php?ajaxID=ExtDirect::route&namespace=');
138  }
139  $url .= rawurlencode($namespace);
140  return $url;
141  }
142 
150  protected function getExtDirectApi(array $filterNamespaces) {
151  $noCache = \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('no_cache') ? TRUE : FALSE;
152  // Look up into the cache
153  $cacheIdentifier = 'ExtDirectApi';
154  $cacheHash = md5($cacheIdentifier . implode(',', $filterNamespaces) . \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SSL') . serialize($this->settings) . TYPO3_MODE . \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('HTTP_HOST'));
155  // With no_cache always generate the javascript content
156  // Generate the javascript content if it wasn't found inside the cache and cache it!
157  if ($noCache || !is_array(($javascriptNamespaces = \TYPO3\CMS\Frontend\Page\PageRepository::getHash($cacheHash)))) {
158  $javascriptNamespaces = $this->generateAPI($filterNamespaces);
159  if (count($javascriptNamespaces)) {
160  \TYPO3\CMS\Frontend\Page\PageRepository::storeHash($cacheHash, $javascriptNamespaces, $cacheIdentifier);
161  }
162  }
163  return $javascriptNamespaces;
164  }
165 
172  protected function getNamespaceError(array $filterNamespaces) {
173  if (count($filterNamespaces)) {
174  // Namespace error
175  $errorMessage = sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:ExtDirect.namespaceError'), __CLASS__, implode(',', $filterNamespaces));
176  } else {
177  // No namespace given
178  $errorMessage = sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:ExtDirect.noNamespace'), __CLASS__);
179  }
180  return $errorMessage;
181  }
182 
190  protected function findNamespace($namespace, array $filterNamespaces) {
191  if ($filterNamespaces === array('TYPO3')) {
192  return TRUE;
193  }
194  $found = FALSE;
195  foreach ($filterNamespaces as $filter) {
196  if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($filter, $namespace)) {
197  $found = TRUE;
198  break;
199  }
200  }
201  return $found;
202  }
203 
204 }
const TYPO3_MODE
Definition: init.php:40
static getUserObj($classRef, $checkPrefix='', $silent=FALSE)
getApiPhp(array $filterNamespaces)
getNamespaceError(array $filterNamespaces)
getExtDirectApi(array $filterNamespaces)
generateAPI(array $filterNamespaces)
$ajaxParams
Definition: ajax.php:64
findNamespace($namespace, array $filterNamespaces)
static storeHash($hash, $data, $ident, $lifetime=0)
getAPI($ajaxParams, \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxObj)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
if($ajaxRegistryEntry !==NULL) $ajaxObj
Definition: ajax.php:63