ExtDirectRouter.php 6.26 KB
Newer Older
1
2
3
<?php
namespace TYPO3\CMS\Core\ExtDirect;

4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
8
9
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
10
 *
11
12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
15
 * The TYPO3 project - inspiring people to share!
 */
Wouter Wolters's avatar
Wouter Wolters committed
16

17
18
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Wouter Wolters's avatar
Wouter Wolters committed
19
20
use TYPO3\CMS\Core\Utility\GeneralUtility;

21
22
23
/**
 * Ext Direct Router
 */
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class ExtDirectRouter
{
    /**
     * Dispatches the incoming calls to methods about the ExtDirect API.
     *
     * @param ServerRequestInterface $request
     * @param ResponseInterface $response
     * @return ResponseInterface
     */
    public function routeAction(ServerRequestInterface $request, ResponseInterface $response)
    {
        $GLOBALS['error'] = GeneralUtility::makeInstance(\TYPO3\CMS\Core\ExtDirect\ExtDirectDebug::class);
        $isForm = false;
        $isUpload = false;
        $rawPostData = file_get_contents('php://input');
        $postParameters = $request->getParsedBody();
        $namespace = isset($request->getParsedBody()['namespace']) ? $request->getParsedBody()['namespace'] : $request->getQueryParams()['namespace'];
        $extResponse = array();
        $extRequest = null;
        $isValidRequest = true;
        if (!empty($postParameters['extAction'])) {
            $isForm = true;
            $isUpload = $postParameters['extUpload'] === 'true';
            $extRequest = new \stdClass();
            $extRequest->action = $postParameters['extAction'];
            $extRequest->method = $postParameters['extMethod'];
            $extRequest->tid = $postParameters['extTID'];
            unset($_POST['securityToken']);
            $extRequest->data = array($_POST + $_FILES);
            $extRequest->data[] = $postParameters['securityToken'];
        } elseif (!empty($rawPostData)) {
            $extRequest = json_decode($rawPostData);
        } else {
            $extResponse[] = array(
                'type' => 'exception',
                'message' => 'Something went wrong with an ExtDirect call!',
                'code' => 'router'
            );
            $isValidRequest = false;
        }
        if (!is_array($extRequest)) {
            $extRequest = array($extRequest);
        }
        if ($isValidRequest) {
            $validToken = false;
            $firstCall = true;
            foreach ($extRequest as $index => $singleRequest) {
                $extResponse[$index] = array(
                    'tid' => $singleRequest->tid,
                    'action' => $singleRequest->action,
                    'method' => $singleRequest->method
                );
76
                $token = is_array($singleRequest->data) ? array_pop($singleRequest->data) : null;
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
                if ($firstCall) {
                    $firstCall = false;
                    $formprotection = \TYPO3\CMS\Core\FormProtection\FormProtectionFactory::get();
                    $validToken = $formprotection->validateToken($token, 'extDirect');
                }
                try {
                    if (!$validToken) {
                        throw new \TYPO3\CMS\Core\FormProtection\Exception('ExtDirect: Invalid Security Token!');
                    }
                    $extResponse[$index]['type'] = 'rpc';
                    $extResponse[$index]['result'] = $this->processRpc($singleRequest, $namespace);
                    $extResponse[$index]['debug'] = $GLOBALS['error']->toString();
                } catch (\Exception $exception) {
                    $extResponse[$index]['type'] = 'exception';
                    $extResponse[$index]['message'] = $exception->getMessage();
                    $extResponse[$index]['code'] = 'router';
                }
            }
        }
        if ($isForm && $isUpload) {
            $extResponse = json_encode($extResponse);
            $extResponse = preg_replace('/&quot;/', '\\&quot;', $extResponse);
            $extResponse = array(
                '<html><body><textarea>' . $extResponse . '</textarea></body></html>'
            );
        } else {
            $extResponse = json_encode($extResponse);
        }
        $response->getBody()->write($extResponse);
        return $response;
    }
108

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    /**
     * Processes an incoming extDirect call by executing the defined method. The configuration
     * array "$GLOBALS['TYPO3_CONF_VARS']['BE']['ExtDirect']" is taken to find the class/method
     * information.
     *
     * @param \stdClass $singleRequest request object from extJS
     * @param string $namespace namespace like TYPO3.Backend
     * @return mixed return value of the called method
     * @throws \UnexpectedValueException if the remote method couldn't be found
     */
    protected function processRpc($singleRequest, $namespace)
    {
        $endpointName = $namespace . '.' . $singleRequest->action;
        if (!isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName])) {
            throw new \UnexpectedValueException('ExtDirect: Call to undefined endpoint: ' . $endpointName, 1294586450);
        }
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName])) {
            if (!isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName]['callbackClass'])) {
                throw new \UnexpectedValueException('ExtDirect: Call to undefined endpoint: ' . $endpointName, 1294586451);
            }
            $callbackClass = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName]['callbackClass'];
            $configuration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ExtDirect'][$endpointName];
            if (!is_null($configuration['moduleName']) && !is_null($configuration['accessLevel'])) {
                $GLOBALS['BE_USER']->modAccess(array(
                    'name' => $configuration['moduleName'],
                    'access' => $configuration['accessLevel']
                ), true);
            }
        }
        $endpointObject = GeneralUtility::getUserObj($callbackClass);
        return call_user_func_array(array($endpointObject, $singleRequest->method), is_array($singleRequest->data) ? $singleRequest->data : array());
    }
141
}