Fixed bug #15829: ExtDirect wrong XCLASS registration code (Thanks to Stefan Galinski)
[Packages/TYPO3.CMS.git] / t3lib / extjs / class.t3lib_extjs_extdirectrouter.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 Sebastian Kurfürst <sebastian@typo3.org>
6 * (c) 2010 Stefan Galinski <stefan.galinski@gmail.com>
7 * All rights reserved
8 *
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.
14 *
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.
19 *
20 *
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.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28
29 /**
30 * Ext Direct Router
31 *
32 * @author Sebastian Kurfürst <sebastian@typo3.org>
33 * @author Stefan Galinski <stefan.galinski@gmail.com>
34 * @package TYPO3
35 */
36 class t3lib_extjs_ExtDirectRouter {
37 /**
38 * Dispatches the incoming calls to methods about the ExtDirect API.
39 *
40 * @param aray $ajaxParams ajax parameters
41 * @param TYPO3AJAX $ajaxObj typo3ajax instance
42 * @return void
43 */
44 public function route($ajaxParams, TYPO3AJAX $ajaxObj) {
45 $isForm = FALSE;
46 $isUpload = FALSE;
47 $rawPostData = file_get_contents('php://input');
48 $postParameters = t3lib_div::_POST();
49 $namespace = t3lib_div::_GET('namespace');
50 $response = array();
51 $request = NULL;
52
53 if (!empty($postParameters['extAction'])) {
54 $isForm = TRUE;
55 $isUpload = $postParameters['extUpload'] === 'true';
56
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);
64 } else {
65 $response[] = array(
66 'type' => 'exception',
67 'message' => 'Something went wrong with an ExtDirect call!'
68 );
69 }
70
71 if (!is_array($request)) {
72 $request = array($request);
73 }
74
75 foreach ($request as $index => $singleRequest) {
76 $response[$index] = array(
77 'tid' => $singleRequest->tid,
78 'action' => $singleRequest->action,
79 'method' => $singleRequest->method
80 );
81
82 try {
83 $response[$index]['type'] = 'rpc';
84 $response[$index]['result'] = $this->processRpc($singleRequest, $namespace);
85 $response[$index]['debug'] = t3lib_extjs_ExtDirectDebug::toString();
86
87 } catch (Exception $exception) {
88 $response[$index]['type'] = 'exception';
89 $response[$index]['message'] = $exception->getMessage();
90 $response[$index]['where'] = $exception->getTraceAsString();
91 }
92 }
93
94 if ($isForm && $isUpload) {
95 $ajaxObj->setContentFormat('plain');
96 $response = json_encode($response);
97 $response = preg_replace('/&quot;/', '\\&quot;', $response);
98
99 $response = array(
100 '<html><body><textarea>' .
101 $response .
102 '</textarea></body></html>'
103 );
104 } else {
105 $ajaxObj->setContentFormat('jsonbody');
106 }
107
108 $ajaxObj->setContent($response);
109 }
110
111
112 /**
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
115 * information.
116 *
117 * @param object $singleRequest request object from extJS
118 * @param string $namespace namespace like TYPO3.Backend
119 * @throws UnexpectedValueException if the remote method couldn't be found
120 * @return mixed return value of the called method
121 */
122 protected function processRpc($singleRequest, $namespace) {
123 $endpointName = $namespace . '.' . $singleRequest->action;
124
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 UnexpectedValueException('ExtDirect: Call to undefined endpoint: ' . $endpointName);
129 }
130
131 $endpointObject = t3lib_div::getUserObj(
132 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName],
133 FALSE
134 );
135
136 return call_user_func_array(
137 array($endpointObject, $singleRequest->method),
138 is_array($singleRequest->data) ? $singleRequest->data : array()
139 );
140 }
141 }
142
143 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/extjs/class.t3lib_extjs_extdirectrouter.php']) {
144 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/extjs/class.t3lib_extjs_extdirectrouter.php']);
145 }
146
147 ?>