8956baeded31ca12a29be400d8c32225423d780c
[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 $postParameters = t3lib_div::_POST();
49 $namespace = t3lib_div::_GET('namespace');
50
51 if (!empty($postParameters['extAction'])) {
52 $isForm = TRUE;
53 $isUpload = $postParameters['extUpload'] === 'true';
54
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);
61 } else {
62 throw new t3lib_error_Exception('ExtDirect: Missing Parameters!');
63 }
64
65 $response = NULL;
66 if (is_array($request)) {
67 $response = array();
68 foreach ($request as $singleRequest) {
69 $response[] = $this->processRpc($singleRequest, $namespace);
70 }
71 } else {
72 $response = $this->processRpc($request, $namespace);
73 }
74
75 if ($isForm && $isUpload) {
76 $ajaxObj->setContentFormat('plain');
77 $response = json_encode($response);
78 $response = preg_replace('/&quot;/', '\\&quot;', $response);
79
80 $response = array(
81 '<html><body><textarea>' .
82 $response .
83 '</textarea></body></html>'
84 );
85 } else {
86 $ajaxObj->setContentFormat('jsonbody');
87 }
88 } catch (t3lib_error_Exception $exception) {
89 $response = array(
90 'type' => 'exception',
91 'message' => $exception->getMessage(),
92 'where' => $exception->getTraceAsString()
93 );
94 }
95
96 $ajaxObj->setContent($response);
97 }
98
99
100 /**
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
103 * information.
104 *
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
108 */
109 protected function processRpc($singleRequest, $namespace) {
110 try {
111 $endpointName = $namespace . '.' . $singleRequest->action;
112
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);
117 }
118
119 $response = array(
120 'type' => 'rpc',
121 'tid' => $singleRequest->tid,
122 'action' => $singleRequest->action,
123 'method' => $singleRequest->method
124 );
125
126 $endpointObject = t3lib_div::getUserObj(
127 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName],
128 FALSE
129 );
130
131 $response['result'] = call_user_func_array(
132 array($endpointObject, $singleRequest->method),
133 is_array($singleRequest->data) ? $singleRequest->data : array()
134 );
135
136 } catch (t3lib_error_Exception $exception) {
137 throw $exception;
138 }
139
140 return $response;
141 }
142 }
143
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']);
146 }
147
148 ?>