|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* CallableFunctionPlugin.php - Jaxon user function plugin |
|
5
|
|
|
* |
|
6
|
|
|
* This class registers user defined functions, generates client side javascript code, |
|
7
|
|
|
* and calls them on user request |
|
8
|
|
|
* |
|
9
|
|
|
* @package jaxon-core |
|
|
|
|
|
|
10
|
|
|
* @author Jared White |
|
|
|
|
|
|
11
|
|
|
* @author J. Max Wilson |
|
|
|
|
|
|
12
|
|
|
* @author Joseph Woolley |
|
|
|
|
|
|
13
|
|
|
* @author Steffen Konerow |
|
|
|
|
|
|
14
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
15
|
|
|
* @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson |
|
|
|
|
|
|
16
|
|
|
* @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White & J. Max Wilson |
|
|
|
|
|
|
17
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
|
18
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
19
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
20
|
|
|
*/ |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
namespace Jaxon\Request\Plugin\CallableFunction; |
|
23
|
|
|
|
|
24
|
|
|
use Jaxon\Jaxon; |
|
25
|
|
|
use Jaxon\Container\Container; |
|
26
|
|
|
use Jaxon\Plugin\RequestPlugin; |
|
27
|
|
|
use Jaxon\Request\Handler\RequestHandler; |
|
28
|
|
|
use Jaxon\Request\Target; |
|
29
|
|
|
use Jaxon\Request\Validator; |
|
30
|
|
|
use Jaxon\Response\ResponseManager; |
|
31
|
|
|
use Jaxon\Utils\Template\Engine as TemplateEngine; |
|
32
|
|
|
use Jaxon\Utils\Translation\Translator; |
|
33
|
|
|
use Jaxon\Exception\RequestException; |
|
34
|
|
|
use Jaxon\Exception\SetupException; |
|
35
|
|
|
|
|
36
|
|
|
use function array_keys; |
|
37
|
|
|
use function implode; |
|
38
|
|
|
use function is_array; |
|
39
|
|
|
use function is_string; |
|
40
|
|
|
use function md5; |
|
41
|
|
|
use function trim; |
|
42
|
|
|
|
|
43
|
|
|
class CallableFunctionPlugin extends RequestPlugin |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
private $sPrefix; |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* The request handler |
|
52
|
|
|
* |
|
53
|
|
|
* @var RequestHandler |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $xRequestHandler; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* The response manager |
|
59
|
|
|
* |
|
60
|
|
|
* @var ResponseManager |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $xResponseManager; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* The request data validator |
|
66
|
|
|
* |
|
67
|
|
|
* @var Validator |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $xValidator; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @var TemplateEngine |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $xTemplateEngine; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @var Translator |
|
78
|
|
|
*/ |
|
79
|
|
|
protected $xTranslator; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* The registered functions names |
|
83
|
|
|
* |
|
84
|
|
|
* @var array |
|
85
|
|
|
*/ |
|
86
|
|
|
protected $aFunctions = []; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* The registered functions options |
|
90
|
|
|
* |
|
91
|
|
|
* @var array |
|
92
|
|
|
*/ |
|
93
|
|
|
protected $aOptions = []; |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* The name of the function that is being requested (during the request processing phase) |
|
97
|
|
|
* |
|
98
|
|
|
* @var string |
|
99
|
|
|
*/ |
|
100
|
|
|
protected $sRequestedFunction = null; |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* The constructor |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $sPrefix |
|
|
|
|
|
|
106
|
|
|
* @param RequestHandler $xRequestHandler |
|
|
|
|
|
|
107
|
|
|
* @param ResponseManager $xResponseManager |
|
|
|
|
|
|
108
|
|
|
* @param TemplateEngine $xTemplateEngine |
|
|
|
|
|
|
109
|
|
|
* @param Translator $xTranslator |
|
|
|
|
|
|
110
|
|
|
* @param Validator $xValidator |
|
|
|
|
|
|
111
|
|
|
*/ |
|
112
|
|
|
public function __construct(string $sPrefix, RequestHandler $xRequestHandler, |
|
|
|
|
|
|
113
|
|
|
ResponseManager $xResponseManager, TemplateEngine $xTemplateEngine, |
|
114
|
|
|
Translator $xTranslator, Validator $xValidator) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->sPrefix = $sPrefix; |
|
|
|
|
|
|
117
|
|
|
$this->xRequestHandler = $xRequestHandler; |
|
118
|
|
|
$this->xResponseManager = $xResponseManager; |
|
119
|
|
|
$this->xTemplateEngine = $xTemplateEngine; |
|
120
|
|
|
$this->xTranslator = $xTranslator; |
|
|
|
|
|
|
121
|
|
|
$this->xValidator = $xValidator; |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
if(isset($_GET['jxnfun'])) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->sRequestedFunction = $_GET['jxnfun']; |
|
126
|
|
|
} |
|
127
|
|
|
if(isset($_POST['jxnfun'])) |
|
128
|
|
|
{ |
|
129
|
|
|
$this->sRequestedFunction = $_POST['jxnfun']; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @inheritDoc |
|
135
|
|
|
*/ |
|
|
|
|
|
|
136
|
|
|
public function getName(): string |
|
137
|
|
|
{ |
|
138
|
|
|
return Jaxon::CALLABLE_FUNCTION; |
|
139
|
|
|
} |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @inheritDoc |
|
143
|
|
|
*/ |
|
|
|
|
|
|
144
|
|
|
public function getTarget(): ?Target |
|
145
|
|
|
{ |
|
146
|
|
|
if(!$this->sRequestedFunction) |
|
147
|
|
|
{ |
|
148
|
|
|
return null; |
|
149
|
|
|
} |
|
150
|
|
|
return Target::makeFunction($this->sRequestedFunction); |
|
151
|
|
|
} |
|
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
/** |
|
|
|
|
|
|
154
|
|
|
* @inheritDoc |
|
155
|
|
|
* @throws SetupException |
|
156
|
|
|
*/ |
|
|
|
|
|
|
157
|
|
|
public function checkOptions(string $sCallable, $xOptions): array |
|
158
|
|
|
{ |
|
159
|
|
|
if(!$this->xValidator->validateFunction(trim($sCallable))) |
|
160
|
|
|
{ |
|
161
|
|
|
throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration')); |
|
162
|
|
|
} |
|
163
|
|
|
if(is_string($xOptions)) |
|
164
|
|
|
{ |
|
165
|
|
|
$xOptions = ['include' => $xOptions]; |
|
166
|
|
|
} |
|
167
|
|
|
elseif(!is_array($xOptions)) |
|
168
|
|
|
{ |
|
169
|
|
|
throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration')); |
|
170
|
|
|
} |
|
171
|
|
|
return $xOptions; |
|
172
|
|
|
} |
|
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Register a user defined function |
|
176
|
|
|
* |
|
177
|
|
|
* @param string $sType The type of request handler being registered |
|
|
|
|
|
|
178
|
|
|
* @param string $sCallable The name of the function being registered |
|
|
|
|
|
|
179
|
|
|
* @param array $aOptions The associated options |
|
|
|
|
|
|
180
|
|
|
* |
|
181
|
|
|
* @return bool |
|
182
|
|
|
*/ |
|
183
|
|
|
public function register(string $sType, string $sCallable, array $aOptions): bool |
|
184
|
|
|
{ |
|
185
|
|
|
$sPhpFunction = trim($sCallable); |
|
186
|
|
|
$sFunction = $sPhpFunction; |
|
|
|
|
|
|
187
|
|
|
// Check if an alias is defined |
|
188
|
|
|
if(isset($aOptions['alias'])) |
|
189
|
|
|
{ |
|
190
|
|
|
$sFunction = (string)$aOptions['alias']; |
|
191
|
|
|
unset($aOptions['alias']); |
|
192
|
|
|
} |
|
193
|
|
|
$this->aFunctions[$sFunction] = $sPhpFunction; |
|
194
|
|
|
$this->aOptions[$sFunction] = $aOptions; |
|
|
|
|
|
|
195
|
|
|
return true; |
|
196
|
|
|
} |
|
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @inheritDoc |
|
200
|
|
|
*/ |
|
|
|
|
|
|
201
|
|
|
public function getHash(): string |
|
202
|
|
|
{ |
|
203
|
|
|
return md5(implode('', array_keys($this->aFunctions))); |
|
204
|
|
|
} |
|
|
|
|
|
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Get the callable object for a registered function |
|
208
|
|
|
* |
|
209
|
|
|
* @param string $sFunction |
|
|
|
|
|
|
210
|
|
|
* |
|
211
|
|
|
* @return CallableFunction|null |
|
212
|
|
|
*/ |
|
213
|
|
|
public function getCallable(string $sFunction): ?CallableFunction |
|
214
|
|
|
{ |
|
215
|
|
|
if(!isset($this->aFunctions[$sFunction])) |
|
216
|
|
|
{ |
|
217
|
|
|
return null; |
|
218
|
|
|
} |
|
219
|
|
|
$xCallable = new CallableFunction($sFunction, $this->sPrefix . $sFunction, $this->aFunctions[$sFunction]); |
|
220
|
|
|
foreach($this->aOptions[$sFunction] as $sName => $sValue) |
|
221
|
|
|
{ |
|
222
|
|
|
$xCallable->configure($sName, $sValue); |
|
223
|
|
|
} |
|
224
|
|
|
return $xCallable; |
|
225
|
|
|
} |
|
|
|
|
|
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Generate the javascript function stub that is sent to the browser on initial page load |
|
229
|
|
|
* |
|
230
|
|
|
* @param CallableFunction $xFunction |
|
|
|
|
|
|
231
|
|
|
* |
|
232
|
|
|
* @return string |
|
233
|
|
|
*/ |
|
234
|
|
|
private function getCallableScript(CallableFunction $xFunction): string |
|
235
|
|
|
{ |
|
236
|
|
|
return $this->xTemplateEngine->render('jaxon::callables/function.js', [ |
|
237
|
|
|
'sName' => $xFunction->getName(), |
|
238
|
|
|
'sJsName' => $xFunction->getJsName(), |
|
239
|
|
|
'aOptions' => $xFunction->getOptions(), |
|
240
|
|
|
]); |
|
241
|
|
|
} |
|
|
|
|
|
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @inheritDoc |
|
245
|
|
|
*/ |
|
|
|
|
|
|
246
|
|
|
public function getScript(): string |
|
247
|
|
|
{ |
|
248
|
|
|
$code = ''; |
|
249
|
|
|
foreach(array_keys($this->aFunctions) as $sFunction) |
|
250
|
|
|
{ |
|
251
|
|
|
$xFunction = $this->getCallable($sFunction); |
|
252
|
|
|
$code .= $this->getCallableScript($xFunction); |
|
|
|
|
|
|
253
|
|
|
} |
|
254
|
|
|
return $code; |
|
255
|
|
|
} |
|
|
|
|
|
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* @inheritDoc |
|
259
|
|
|
*/ |
|
|
|
|
|
|
260
|
|
|
public function canProcessRequest(): bool |
|
261
|
|
|
{ |
|
262
|
|
|
// Check the validity of the function name |
|
263
|
|
|
if(($this->sRequestedFunction) && !$this->xValidator->validateFunction($this->sRequestedFunction)) |
|
264
|
|
|
{ |
|
265
|
|
|
$this->sRequestedFunction = null; |
|
266
|
|
|
} |
|
267
|
|
|
return ($this->sRequestedFunction != null); |
|
268
|
|
|
} |
|
|
|
|
|
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* @inheritDoc |
|
272
|
|
|
* @throws RequestException |
|
273
|
|
|
*/ |
|
|
|
|
|
|
274
|
|
|
public function processRequest(): bool |
|
275
|
|
|
{ |
|
276
|
|
|
// Security check: make sure the requested function was registered. |
|
277
|
|
|
if(!isset($this->aFunctions[$this->sRequestedFunction])) |
|
278
|
|
|
{ |
|
279
|
|
|
// Unable to find the requested function |
|
280
|
|
|
throw new RequestException($this->xTranslator->trans('errors.functions.invalid', |
|
281
|
|
|
['name' => $this->sRequestedFunction])); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
$xFunction = $this->getCallable($this->sRequestedFunction); |
|
285
|
|
|
$aArgs = $this->xRequestHandler->processArguments(); |
|
|
|
|
|
|
286
|
|
|
$xResponse = $xFunction->call($aArgs); |
|
287
|
|
|
if(($xResponse)) |
|
288
|
|
|
{ |
|
289
|
|
|
$this->xResponseManager->append($xResponse); |
|
290
|
|
|
} |
|
291
|
|
|
return true; |
|
292
|
|
|
} |
|
|
|
|
|
|
293
|
|
|
} |
|
294
|
|
|
|