2 /***************************************************************
5 * (c) 2010 Sebastian Kurfuerst <sebastian@typo3.org>
6 * (c) 2010 Stefan Galinski <stefan.galinski@gmail.com>
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 * A copy is found in the textfile GPL.txt and important notices to the license
18 * from the author is found in LICENSE.txt distributed with these scripts.
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
32 * @author Sebastian Kurfuerst <sebastian@typo3.org>
33 * @author Stefan Galinski <stefan.galinski@gmail.com>
36 class t3lib_extjs_ExtDirectRouter
{
38 * Dispatches the incoming calls to methods about the ExtDirect API.
40 * @param aray $ajaxParams ajax parameters
41 * @param TYPO3AJAX $ajaxObj typo3ajax instance
44 public function route($ajaxParams, TYPO3AJAX
$ajaxObj) {
47 $rawPostData = file_get_contents('php://input');
48 $postParameters = t3lib_div
::_POST();
49 $namespace = t3lib_div
::_GET('namespace');
53 if (!empty($postParameters['extAction'])) {
55 $isUpload = $postParameters['extUpload'] === 'true';
57 $request = new stdClass
;
58 $request->action
= $postParameters['extAction'];
59 $request->method
= $postParameters['extMethod'];
60 $request->tid
= $postParameters['extTID'];
61 $request->data
= array($_POST +
$_FILES);
62 } elseif (!empty($rawPostData)) {
63 $request = json_decode($rawPostData);
66 'type' => 'exception',
67 'message' => 'Something went wrong with an ExtDirect call!'
71 if (!is_array($request)) {
72 $request = array($request);
75 foreach ($request as $index => $singleRequest) {
76 $response[$index] = array(
77 'tid' => $singleRequest->tid
,
78 'action' => $singleRequest->action
,
79 'method' => $singleRequest->method
83 $response[$index]['type'] = 'rpc';
84 $response[$index]['result'] = $this->processRpc($singleRequest, $namespace);
85 $response[$index]['debug'] = t3lib_extjs_ExtDirectDebug
::toString();
87 } catch (Exception
$exception) {
88 $response[$index]['type'] = 'exception';
89 $response[$index]['message'] = $exception->getMessage();
90 $response[$index]['where'] = $exception->getTraceAsString();
94 if ($isForm && $isUpload) {
95 $ajaxObj->setContentFormat('plain');
96 $response = json_encode($response);
97 $response = preg_replace('/"/', '\\"', $response);
100 '<html><body><textarea>' .
102 '</textarea></body></html>'
105 $ajaxObj->setContentFormat('jsonbody');
108 $ajaxObj->setContent($response);
113 * Processes an incoming extDirect call by executing the defined method. The configuration
114 * array "$GLOBALS['TYPO3_CONF_VARS']['BE']['ExtDirect']" is taken to find the class/method
117 * @param object $singleRequest request object from extJS
118 * @param string $namespace namespace like TYPO3.Backend
119 * @throws t3lib_error_exception if the remote method couldn't be found
120 * @return mixed return value of the called method
122 protected function processRpc($singleRequest, $namespace) {
123 $endpointName = $namespace . '.' . $singleRequest->action
;
125 // theoretically this can never happen, because of an javascript error on
126 // the client side due the missing namespace/endpoint
127 if (!isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName])) {
128 throw new t3lib_error_Exception('ExtDirect: Call to undefined endpoint: ' . $endpointName);
131 $endpointObject = t3lib_div
::getUserObj(
132 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName],
136 return call_user_func_array(
137 array($endpointObject, $singleRequest->method
),
138 is_array($singleRequest->data
) ?
$singleRequest->data
: array()
143 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['t3lib/class.t3lib_extjs_extdirectrouter.php']) {
144 include_once($TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['t3lib/class.t3lib_extjs_extdirectrouter.php']);