TYPO3 API  SVNRelease
class.t3lib_extjs_extdirectrouter.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2010-2011 Sebastian Kurfürst <sebastian@typo3.org>
00006  *  (c) 2010-2011 Stefan Galinski <stefan.galinski@gmail.com>
00007  *  All rights reserved
00008  *
00009  *  This script is part of the TYPO3 project. The TYPO3 project is
00010  *  free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  The GNU General Public License can be found at
00016  *  http://www.gnu.org/copyleft/gpl.html.
00017  *  A copy is found in the textfile GPL.txt and important notices to the license
00018  *  from the author is found in LICENSE.txt distributed with these scripts.
00019  *
00020  *
00021  *  This script is distributed in the hope that it will be useful,
00022  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024  *  GNU General Public License for more details.
00025  *
00026  *  This copyright notice MUST APPEAR in all copies of the script!
00027  ***************************************************************/
00028 
00029 /**
00030  * Ext Direct Router
00031  *
00032  * @author  Sebastian Kurfürst <sebastian@typo3.org>
00033  * @author  Stefan Galinski <stefan.galinski@gmail.com>
00034  * @package TYPO3
00035  */
00036 class t3lib_extjs_ExtDirectRouter {
00037     /**
00038      * Dispatches the incoming calls to methods about the ExtDirect API.
00039      *
00040      * @param aray $ajaxParams ajax parameters
00041      * @param TYPO3AJAX $ajaxObj typo3ajax instance
00042      * @return void
00043      */
00044     public function route($ajaxParams, TYPO3AJAX $ajaxObj) {
00045         $GLOBALS['error'] = t3lib_div::makeInstance('t3lib_extjs_ExtDirectDebug');
00046 
00047         $isForm = FALSE;
00048         $isUpload = FALSE;
00049         $rawPostData = file_get_contents('php://input');
00050         $postParameters = t3lib_div::_POST();
00051         $namespace = t3lib_div::_GET('namespace');
00052         $response = array();
00053         $request = NULL;
00054         $isValidRequest = TRUE;
00055 
00056         if (!empty($postParameters['extAction'])) {
00057             $isForm = TRUE;
00058             $isUpload = $postParameters['extUpload'] === 'true';
00059 
00060             $request = new stdClass;
00061             $request->action = $postParameters['extAction'];
00062             $request->method = $postParameters['extMethod'];
00063             $request->tid = $postParameters['extTID'];
00064 
00065             unset($_POST['securityToken']);
00066             $request->data = array($_POST + $_FILES);
00067             $request->data[] = $postParameters['securityToken'];
00068         } elseif (!empty($rawPostData)) {
00069             $request = json_decode($rawPostData);
00070         } else {
00071             $response[] = array(
00072                 'type' => 'exception',
00073                 'message' => 'Something went wrong with an ExtDirect call!',
00074                 'code' => 'router',
00075             );
00076             $isValidRequest = FALSE;
00077         }
00078 
00079         if (!is_array($request)) {
00080             $request = array($request);
00081         }
00082 
00083         if ($isValidRequest) {
00084             $validToken = FALSE;
00085             $firstCall = TRUE;
00086             foreach ($request as $index => $singleRequest) {
00087                 $response[$index] = array(
00088                     'tid' => $singleRequest->tid,
00089                     'action' => $singleRequest->action,
00090                     'method' => $singleRequest->method
00091                 );
00092 
00093                 $token = array_pop($singleRequest->data);
00094                 if ($firstCall) {
00095                     $firstCall = FALSE;
00096                     $formprotection = t3lib_formprotection_Factory::get();
00097                     $validToken = $formprotection->validateToken($token, 'extDirect');
00098                 }
00099 
00100                 try {
00101                     if (!$validToken) {
00102                         throw new t3lib_formprotection_InvalidTokenException('ExtDirect: Invalid Security Token!');
00103                     }
00104 
00105                     $response[$index]['type'] = 'rpc';
00106                     $response[$index]['result'] = $this->processRpc($singleRequest, $namespace);
00107                     $response[$index]['debug'] = $GLOBALS['error']->toString();
00108 
00109                 } catch (Exception $exception) {
00110                     $response[$index]['type'] = 'exception';
00111                     $response[$index]['message'] = $exception->getMessage();
00112                     $response[$index]['code'] = 'router';
00113                 }
00114             }
00115         }
00116         if ($isForm && $isUpload) {
00117             $ajaxObj->setContentFormat('plain');
00118             $response = json_encode($response);
00119             $response = preg_replace('/&quot;/', '\\&quot;', $response);
00120 
00121             $response = array(
00122                 '<html><body><textarea>' .
00123                 $response .
00124                 '</textarea></body></html>'
00125             );
00126         } else {
00127             $ajaxObj->setContentFormat('jsonbody');
00128         }
00129 
00130         $ajaxObj->setContent($response);
00131     }
00132 
00133 
00134     /**
00135      * Processes an incoming extDirect call by executing the defined method. The configuration
00136      * array "$GLOBALS['TYPO3_CONF_VARS']['BE']['ExtDirect']" is taken to find the class/method
00137      * information.
00138      *
00139      * @param object $singleRequest request object from extJS
00140      * @param string $namespace namespace like TYPO3.Backend
00141      * @throws UnexpectedValueException if the remote method couldn't be found
00142      * @return mixed return value of the called method
00143      */
00144     protected function processRpc($singleRequest, $namespace) {
00145         $endpointName = $namespace . '.' . $singleRequest->action;
00146 
00147             // theoretically this can never happen, because of an javascript error on
00148             // the client side due the missing namespace/endpoint
00149         if (!isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName])) {
00150             throw new UnexpectedValueException('ExtDirect: Call to undefined endpoint: ' . $endpointName);
00151         }
00152 
00153         $endpointObject = t3lib_div::getUserObj(
00154             $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName],
00155             FALSE
00156         );
00157 
00158         return call_user_func_array(
00159             array($endpointObject, $singleRequest->method),
00160             is_array($singleRequest->data) ? $singleRequest->data : array()
00161         );
00162     }
00163 }
00164 
00165 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/extjs/class.t3lib_extjs_extdirectrouter.php'])) {
00166     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/extjs/class.t3lib_extjs_extdirectrouter.php']);
00167 }
00168 
00169 ?>