cb25f76ea776b87fba923227aaf244a6220fde33
[Packages/TYPO3.CMS.git] / t3lib / extjs / class.t3lib_extjs_extdirectrouter.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 Sebastian Kurfuerst <sebastian@typo3.org>
6 * All rights reserved
7 *
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.
13 *
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.
18 *
19 *
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.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Ext Direct Router
30 *
31 * @author Sebastian Kurfuerst <sebastian@typo3.org>
32 * @author Stefan Galinski <stefan.galinski@gmail.com>
33 * @package TYPO3
34 */
35 class t3lib_ExtJs_ExtDirectRouter {
36 /**
37 * Dispatches the incoming calls to methods about the ExtDirect API.
38 *
39 * @param aray $ajaxParams ajax parameters
40 * @param TYPO3AJAX $ajaxObj typo3ajax instance
41 * @return void
42 */
43 public function route($ajaxParams, TYPO3AJAX $ajaxObj) {
44 try {
45 $isForm = false;
46 $isUpload = false;
47 $rawPostData = file_get_contents('php://input');
48 $namespace = t3lib_div::_GET('namespace');
49
50 if (!empty($rawPostData)) {
51 $ajaxObj->setContentFormat('jsonbody');
52 $request = json_decode($rawPostData);
53 } else {
54 throw new t3lib_error_Exception('ExtDirect: Missing Parameters!');
55 }
56
57 $response = null;
58 if (is_array($request)) {
59 $response = array();
60 foreach ($request as $singleRequest) {
61 $response[] = $this->processRpc($singleRequest, $namespace);
62 }
63 } else {
64 $response = $this->processRpc($request, $namespace);
65 }
66
67 } catch (t3lib_error_Exception $exception) {
68 $response = array(
69 'type' => 'exception',
70 'message' => $exception->getMessage(),
71 'where' => $exception->getTraceAsString()
72 );
73 }
74
75 $ajaxObj->setContent($response);
76 }
77
78
79 /**
80 * Processes an incoming extDirect call by executing the defined method. The configuration
81 * array "$GLOBALS['TYPO3_CONF_VARS']['BE']['ExtDirect']" is taken to find the class/method
82 * information.
83 *
84 * @param object $singleRequest request object from extJS
85 * @param string $namespace namespace like TYPO3.Backend
86 * @return mixed return value of the called method
87 */
88 protected function processRpc($singleRequest, $namespace) {
89 try {
90 $endpointName = $namespace . '.' . $singleRequest->action;
91
92 // theoretically this can never happen, because of an javascript error on
93 // the client side due the missing namespace/endpoint
94 if (!isset($GLOBALS['TYPO3_CONF_VARS']['BE']['ExtDirect'][$endpointName])) {
95 throw new t3lib_error_Exception('ExtDirect: Call to undefined endpoint: ' . $endpointName);
96 }
97
98 $response = array(
99 'type' => 'rpc',
100 'tid' => $singleRequest->tid,
101 'action' => $singleRequest->action,
102 'method' => $singleRequest->method
103 );
104
105 $endpointObject = t3lib_div::getUserObj(
106 $GLOBALS['TYPO3_CONF_VARS']['BE']['ExtDirect'][$endpointName],
107 FALSE
108 );
109
110 $response['result'] = call_user_func_array(
111 array($endpointObject, $singleRequest->method),
112 is_array($singleRequest->data) ? $singleRequest->data : array()
113 );
114
115 } catch (t3lib_error_Exception $exception) {
116 $response = array(
117 'type' => 'exception',
118 'message' => $exception->getMessage(),
119 'where' => $exception->getTraceAsString()
120 );
121 }
122
123 return $response;
124 }
125 }
126
127 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_extjs_extdirectrouter.php']) {
128 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_extjs_extdirectrouter.php']);
129 }
130
131 ?>