class.t3lib_extjs_extdirectrouter.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2010 Sebastian Kurfürst <sebastian@typo3.org>
00006 *  (c) 2010 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         $isForm = FALSE;
00046         $isUpload = FALSE;
00047         $rawPostData = file_get_contents('php://input');
00048         $postParameters = t3lib_div::_POST();
00049         $namespace = t3lib_div::_GET('namespace');
00050         $response = array();
00051         $request = NULL;
00052 
00053         if (!empty($postParameters['extAction'])) {
00054             $isForm = TRUE;
00055             $isUpload = $postParameters['extUpload'] === 'true';
00056 
00057             $request = new stdClass;
00058             $request->action = $postParameters['extAction'];
00059             $request->method = $postParameters['extMethod'];
00060             $request->tid = $postParameters['extTID'];
00061             $request->data = array($_POST + $_FILES);
00062         } elseif (!empty($rawPostData)) {
00063             $request = json_decode($rawPostData);
00064         } else {
00065             $response[] = array(
00066                 'type' => 'exception',
00067                 'message' => 'Something went wrong with an ExtDirect call!'
00068             );
00069         }
00070 
00071         if (!is_array($request)) {
00072             $request = array($request);
00073         }
00074 
00075         foreach ($request as $index => $singleRequest) {
00076             $response[$index] = array(
00077                 'tid' => $singleRequest->tid,
00078                 'action' => $singleRequest->action,
00079                 'method' => $singleRequest->method
00080             );
00081 
00082             try {
00083                 $response[$index]['type'] = 'rpc';
00084                 $response[$index]['result'] = $this->processRpc($singleRequest, $namespace);
00085                 $response[$index]['debug'] = t3lib_extjs_ExtDirectDebug::toString();
00086 
00087             } catch (Exception $exception) {
00088                 $response[$index]['type'] = 'exception';
00089                 $response[$index]['message'] = $exception->getMessage();
00090                 $response[$index]['where'] = $exception->getTraceAsString();
00091             }
00092         }
00093 
00094         if ($isForm && $isUpload) {
00095             $ajaxObj->setContentFormat('plain');
00096             $response = json_encode($response);
00097             $response = preg_replace('/&quot;/', '\\&quot;', $response);
00098 
00099             $response = array(
00100                 '<html><body><textarea>' .
00101                 $response .
00102                 '</textarea></body></html>'
00103             );
00104         } else {
00105             $ajaxObj->setContentFormat('jsonbody');
00106         }
00107 
00108         $ajaxObj->setContent($response);
00109     }
00110 
00111 
00112     /**
00113      * Processes an incoming extDirect call by executing the defined method. The configuration
00114      * array "$GLOBALS['TYPO3_CONF_VARS']['BE']['ExtDirect']" is taken to find the class/method
00115      * information.
00116      *
00117      * @param object $singleRequest request object from extJS
00118      * @param string $namespace namespace like TYPO3.Backend
00119      * @throws UnexpectedValueException if the remote method couldn't be found
00120      * @return mixed return value of the called method
00121      */
00122     protected function processRpc($singleRequest, $namespace) {
00123         $endpointName = $namespace . '.' . $singleRequest->action;
00124 
00125             // theoretically this can never happen, because of an javascript error on
00126             // the client side due the missing namespace/endpoint
00127         if (!isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName])) {
00128             throw new UnexpectedValueException('ExtDirect: Call to undefined endpoint: ' . $endpointName);
00129         }
00130 
00131         $endpointObject = t3lib_div::getUserObj(
00132             $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName],
00133             FALSE
00134         );
00135 
00136         return call_user_func_array(
00137             array($endpointObject, $singleRequest->method),
00138             is_array($singleRequest->data) ? $singleRequest->data : array()
00139         );
00140     }
00141 }
00142 
00143 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_extjs_extdirectrouter.php']) {
00144     include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_extjs_extdirectrouter.php']);
00145 }
00146 
00147 ?>

Generated on Sat Sep 4 04:17:12 2010 for TYPO3 API by  doxygen 1.4.7