-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAjaxController.php
157 lines (145 loc) · 5.94 KB
/
AjaxController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
76
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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* tollwerk
*
* @category Tollwerk
* @package Tollwerk\TwBase
* @subpackage Tollwerk\TwBase\Controller
* @author Klaus Fiedler <[email protected]>
* @copyright Copyright © 2020 Klaus Fiedler <[email protected]>
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
*/
/***********************************************************************************
* The MIT License (MIT)
*
* Copyright © 2020 Klaus Fiedler <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
***********************************************************************************/
namespace Tollwerk\TwBase\Controller;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
/**
* AjaxController
*
* @package Tollwerk\TwBase
* @subpackage Tollwerk\TwBase\Controller
*/
class AjaxController extends ActionController
{
const STATUS_ERROR = 500;
const STATUS_NO_METHOD = 400;
const STATUS_SUCCESS = 200;
/**
* Available functions registered registered by hook
* @var array
*/
protected $registeredFunctions = [];
/**
* Return a JSON encoded array containing a status and the method result
*
* @param string $status
* @param mixed $result
* @param \Exception|null $exception
*
* @return string
*/
private function createJsonResponse(
string $status,
$result = null,
\Exception $exception = null,
\Error $error = null
) {
$return = [
'status' => intval($status),
'result' => $result
];
if ($exception || $error) {
$devIpMask = GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']);
if (in_array($_SERVER['REMOTE_ADDR'], $devIpMask) || in_array('*', $devIpMask)) {
if ($exception) {
$return['exception'] = [
'message' => $exception->getMessage(),
'code' => $exception->getCode(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
];
}
if ($error) {
$return['error'] = [
'message' => $error->getMessage(),
'code' => $error->getCode(),
'file' => $error->getFile(),
'line' => $error->getLine(),
];
}
}
}
return json_encode($return);
}
/**
* The central action to call via ajax.
* Will check if there is a class registered for the current call.
*
* Example:
* When you want to respond to an ajax call named "myAjaxCall" with arguments "x" and "y",
* the ajax URL would be /?type=4000call=myAjaxTest&args[x]=1&args[y]=2.
*
* You have to register a class responsible for this inside ext_localconf.php of your own extension:
* $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/tw_base']['ajax']['myAjaxTest'] =
* \Vendor\Extension\YourAjaxClass::class
*
* This class must contain a public method with the same name as the ajax call you are registering for,
* expecting a single parameter of type array. That array will contain the arguments "x" and "y".
* The function can return anything that can be encoded as JSON string with json_encode().
* Returning entire extbase objects is likely to fail or exhaust server memory limit!
*
* class MyAjaxClass {
* public function myAjaxTest($arguments) {
* $x = $arguments['x'];
* $y = $arguments['y'];
* // ...
* }
* }
*
* @return string Returns a JSON string with status information (error, success etc.)
* and the return value of the method registered for the ajax call.
*/
public function dispatchAction()
{
try {
$functionName = GeneralUtility::_GP('call');
$arguments = GeneralUtility::_GP('args') ?: [];
$ajaxConfig =& $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/tw_base']['ajax'];
if (array_key_exists($functionName, $ajaxConfig)) {
$_procObj = $this->objectManager->get($ajaxConfig[$functionName]);
if (is_callable([$_procObj, $functionName])) {
return $this->createJsonResponse(
self::STATUS_SUCCESS,
$_procObj->{$functionName}($arguments)
);
};
}
return $this->createJsonResponse(self::STATUS_NO_METHOD);
} catch (\Exception $exception) {
return $this->createJsonResponse(self::STATUS_ERROR, null, $exception);
} catch (\Error $error) {
return $this->createJsonResponse(self::STATUS_ERROR, null, null, $error);
}
}
}