2 /***************************************************************
5 * (c) 2010 Sebastian Kurfuerst <sebastian@typo3.org>
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
31 * @author Sebastian Kurfuerst <sebastian@typo3.org>
32 * @author Stefan Galinski <stefan.galinski@gmail.com>
35 class t3lib_extjs_ExtDirectRouter
{
37 * Dispatches the incoming calls to methods about the ExtDirect API.
39 * @param aray $ajaxParams ajax parameters
40 * @param TYPO3AJAX $ajaxObj typo3ajax instance
43 public function route($ajaxParams, TYPO3AJAX
$ajaxObj) {
47 $rawPostData = file_get_contents('php://input');
48 $postParameters = t3lib_div
::_POST();
49 $namespace = t3lib_div
::_GET('namespace');
51 if (!empty($postParameters['extAction'])) {
53 $isUpload = $postParameters['extUpload'] === 'true';
55 $request->action
= $postParameters['extAction'];
56 $request->method
= $postParameters['extMethod'];
57 $request->tid
= $postParameters['extTID'];
58 $request->data
= array($_POST +
$_FILES);
59 } elseif (!empty($rawPostData)) {
60 $request = json_decode($rawPostData);
62 throw new t3lib_error_Exception('ExtDirect: Missing Parameters!');
66 if (is_array($request)) {
68 foreach ($request as $singleRequest) {
69 $response[] = $this->processRpc($singleRequest, $namespace);
72 $response = $this->processRpc($request, $namespace);
75 if ($isForm && $isUpload) {
76 $ajaxObj->setContentFormat('plain');
77 $response = json_encode($response);
78 $response = preg_replace('/"/', '\\"', $response);
81 '<html><body><textarea>' .
83 '</textarea></body></html>'
86 $ajaxObj->setContentFormat('jsonbody');
88 } catch (t3lib_error_Exception
$exception) {
90 'type' => 'exception',
91 'message' => $exception->getMessage(),
92 'where' => $exception->getTraceAsString()
96 $ajaxObj->setContent($response);
101 * Processes an incoming extDirect call by executing the defined method. The configuration
102 * array "$GLOBALS['TYPO3_CONF_VARS']['BE']['ExtDirect']" is taken to find the class/method
105 * @param object $singleRequest request object from extJS
106 * @param string $namespace namespace like TYPO3.Backend
107 * @return mixed return value of the called method
109 protected function processRpc($singleRequest, $namespace) {
111 $endpointName = $namespace . '.' . $singleRequest->action
;
113 // theoretically this can never happen, because of an javascript error on
114 // the client side due the missing namespace/endpoint
115 if (!isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName])) {
116 throw new t3lib_error_Exception('ExtDirect: Call to undefined endpoint: ' . $endpointName);
121 'tid' => $singleRequest->tid
,
122 'action' => $singleRequest->action
,
123 'method' => $singleRequest->method
126 $endpointObject = t3lib_div
::getUserObj(
127 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName],
131 $response['result'] = call_user_func_array(
132 array($endpointObject, $singleRequest->method
),
133 is_array($singleRequest->data
) ?
$singleRequest->data
: array()
136 } catch (t3lib_error_Exception
$exception) {
144 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['t3lib/class.t3lib_extjs_extdirectrouter.php']) {
145 include_once($TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['t3lib/class.t3lib_extjs_extdirectrouter.php']);