[BUGFIX] Caching framework: Reduce code duplication in db backend
[Packages/TYPO3.CMS.git] / t3lib / extjs / class.t3lib_extjs_extdirectrouter.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 Sebastian Kurfürst <sebastian@typo3.org>
6 * (c) 2010-2011 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 $GLOBALS['error'] = t3lib_div::makeInstance('t3lib_extjs_ExtDirectDebug');
46
47 $isForm = FALSE;
48 $isUpload = FALSE;
49 $rawPostData = file_get_contents('php://input');
50 $postParameters = t3lib_div::_POST();
51 $namespace = t3lib_div::_GET('namespace');
52 $response = array();
53 $request = NULL;
54 $isValidRequest = TRUE;
55
56 if (!empty($postParameters['extAction'])) {
57 $isForm = TRUE;
58 $isUpload = $postParameters['extUpload'] === 'true';
59
60 $request = new stdClass;
61 $request->action = $postParameters['extAction'];
62 $request->method = $postParameters['extMethod'];
63 $request->tid = $postParameters['extTID'];
64
65 unset($_POST['securityToken']);
66 $request->data = array($_POST + $_FILES);
67 $request->data[] = $postParameters['securityToken'];
68 } elseif (!empty($rawPostData)) {
69 $request = json_decode($rawPostData);
70 } else {
71 $response[] = array(
72 'type' => 'exception',
73 'message' => 'Something went wrong with an ExtDirect call!',
74 'code' => 'router',
75 );
76 $isValidRequest = FALSE;
77 }
78
79 if (!is_array($request)) {
80 $request = array($request);
81 }
82
83 if ($isValidRequest) {
84 $validToken = FALSE;
85 $firstCall = TRUE;
86 foreach ($request as $index => $singleRequest) {
87 $response[$index] = array(
88 'tid' => $singleRequest->tid,
89 'action' => $singleRequest->action,
90 'method' => $singleRequest->method
91 );
92
93 $token = array_pop($singleRequest->data);
94 if ($firstCall) {
95 $firstCall = FALSE;
96 $formprotection = t3lib_formprotection_Factory::get();
97 $validToken = $formprotection->validateToken($token, 'extDirect');
98 }
99
100 try {
101 if (!$validToken) {
102 throw new t3lib_formprotection_InvalidTokenException('ExtDirect: Invalid Security Token!');
103 }
104
105 $response[$index]['type'] = 'rpc';
106 $response[$index]['result'] = $this->processRpc($singleRequest, $namespace);
107 $response[$index]['debug'] = $GLOBALS['error']->toString();
108
109 } catch (Exception $exception) {
110 $response[$index]['type'] = 'exception';
111 $response[$index]['message'] = $exception->getMessage();
112 $response[$index]['code'] = 'router';
113 }
114 }
115 }
116 if ($isForm && $isUpload) {
117 $ajaxObj->setContentFormat('plain');
118 $response = json_encode($response);
119 $response = preg_replace('/&quot;/', '\\&quot;', $response);
120
121 $response = array(
122 '<html><body><textarea>' .
123 $response .
124 '</textarea></body></html>'
125 );
126 } else {
127 $ajaxObj->setContentFormat('jsonbody');
128 }
129
130 $ajaxObj->setContent($response);
131 }
132
133
134 /**
135 * Processes an incoming extDirect call by executing the defined method. The configuration
136 * array "$GLOBALS['TYPO3_CONF_VARS']['BE']['ExtDirect']" is taken to find the class/method
137 * information.
138 *
139 * @param object $singleRequest request object from extJS
140 * @param string $namespace namespace like TYPO3.Backend
141 * @throws UnexpectedValueException if the remote method couldn't be found
142 * @return mixed return value of the called method
143 */
144 protected function processRpc($singleRequest, $namespace) {
145 $endpointName = $namespace . '.' . $singleRequest->action;
146
147 // theoretically this can never happen, because of an javascript error on
148 // the client side due the missing namespace/endpoint
149 if (!isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName])) {
150 throw new UnexpectedValueException('ExtDirect: Call to undefined endpoint: ' . $endpointName, 1294586450);
151 }
152
153 $endpointObject = t3lib_div::getUserObj(
154 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName],
155 FALSE
156 );
157
158 return call_user_func_array(
159 array($endpointObject, $singleRequest->method),
160 is_array($singleRequest->data) ? $singleRequest->data : array()
161 );
162 }
163 }
164
165 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/extjs/class.t3lib_extjs_extdirectrouter.php'])) {
166 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/extjs/class.t3lib_extjs_extdirectrouter.php']);
167 }
168
169 ?>