Complex classes like ControllerProvider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ControllerProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | class ControllerProvider implements ControllerProviderInterface { |
||
42 | |||
43 | /** |
||
44 | * Generates the not found page. |
||
45 | * |
||
46 | * @param Application $app |
||
47 | * the Silex application |
||
48 | * @param string $error |
||
49 | * the cause of the not found error |
||
50 | * |
||
51 | * @return Response |
||
52 | * the rendered not found page with the status code 404 |
||
53 | */ |
||
54 | protected function getNotFoundPage(Application $app, $error) { |
||
61 | |||
62 | /** |
||
63 | * Postprocesses the entity after modification by handling the uploaded |
||
64 | * files and setting the flash. |
||
65 | * |
||
66 | * @param Application $app |
||
67 | * the current application |
||
68 | * @param AbstractData $crudData |
||
69 | * the data instance of the entity |
||
70 | * @param Entity $instance |
||
71 | * the entity |
||
72 | * @param string $entity |
||
73 | * the name of the entity |
||
74 | * @param string $mode |
||
75 | * whether to 'edit' or to 'create' the entity |
||
76 | * |
||
77 | * @return null|\Symfony\Component\HttpFoundation\RedirectResponse |
||
78 | * the HTTP response of this modification |
||
79 | */ |
||
80 | protected function modifyFilesAndSetFlashBag(Application $app, AbstractData $crudData, Entity $instance, $entity, $mode) { |
||
81 | $id = $instance->get('id'); |
||
82 | $request = $app['request_stack']->getCurrentRequest(); |
||
83 | $result = $mode == 'edit' ? $crudData->updateFiles($request, $instance, $entity) : $crudData->createFiles($request, $instance, $entity); |
||
84 | if (!$result) { |
||
85 | return null; |
||
86 | } |
||
87 | $app['session']->getFlashBag()->add('success', $app['translator']->trans('crudlex.'.$mode.'.success', [ |
||
88 | '%label%' => $crudData->getDefinition()->getLabel(), |
||
89 | '%id%' => $id |
||
90 | ])); |
||
91 | return $app->redirect($app['url_generator']->generate('crudShow', ['entity' => $entity, 'id' => $id])); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Sets the flashes of a failed entity modification. |
||
96 | * |
||
97 | * @param Application $app |
||
98 | * the current application |
||
99 | * @param boolean $optimisticLocking |
||
100 | * whether the optimistic locking failed |
||
101 | * @param string $mode |
||
102 | * the modification mode, either 'create' or 'edit' |
||
103 | */ |
||
104 | protected function setValidationFailedFlashes(Application $app, $optimisticLocking, $mode) { |
||
110 | |||
111 | /** |
||
112 | * Validates and saves the new or updated entity and returns the appropriate HTTP |
||
113 | * response. |
||
114 | * |
||
115 | * @param Application $app |
||
116 | * the current application |
||
117 | * @param AbstractData $crudData |
||
118 | * the data instance of the entity |
||
119 | * @param Entity $instance |
||
120 | * the entity |
||
121 | * @param string $entity |
||
122 | * the name of the entity |
||
123 | * @param boolean $edit |
||
124 | * whether to edit (true) or to create (false) the entity |
||
125 | * |
||
126 | * @return Response |
||
127 | * the HTTP response of this modification |
||
128 | */ |
||
129 | protected function modifyEntity(Application $app, AbstractData $crudData, Entity $instance, $entity, $edit) { |
||
161 | |||
162 | /** |
||
163 | * Gets the parameters for the redirection after deleting an entity. |
||
164 | * |
||
165 | * @param Request $request |
||
166 | * the current request |
||
167 | * @param string $entity |
||
168 | * the entity name |
||
169 | * @param string $redirectPage |
||
170 | * reference, where the page to redirect to will be stored |
||
171 | * |
||
172 | * @return array<string,string> |
||
173 | * the parameters of the redirection, entity and id |
||
174 | */ |
||
175 | protected function getAfterDeleteRedirectParameters(Request $request, $entity, &$redirectPage) { |
||
189 | |||
190 | /** |
||
191 | * Builds up the parameters of the list page filters. |
||
192 | * |
||
193 | * @param Request $request |
||
194 | * the current application |
||
195 | * @param EntityDefinition $definition |
||
196 | * the current entity definition |
||
197 | * @param array &$filter |
||
198 | * will hold a map of fields to request parameters for the filters |
||
199 | * @param boolean $filterActive |
||
200 | * reference, will be true if at least one filter is active |
||
201 | * @param array $filterToUse |
||
202 | * reference, will hold a map of fields to integers (0 or 1) which boolean filters are active |
||
203 | * @param array $filterOperators |
||
204 | * reference, will hold a map of fields to operators for AbstractData::listEntries() |
||
205 | */ |
||
206 | protected function buildUpListFilter(Request $request, EntityDefinition $definition, &$filter, &$filterActive, &$filterToUse, &$filterOperators) { |
||
207 | foreach ($definition->getFilter() as $filterField) { |
||
208 | $type = $definition->getType($filterField); |
||
209 | $filter[$filterField] = $request->get('crudFilter'.$filterField); |
||
210 | if ($filter[$filterField]) { |
||
211 | $filterActive = true; |
||
212 | if ($type === 'boolean') { |
||
213 | $filterToUse[$filterField] = $filter[$filterField] == 'true' ? 1 : 0; |
||
214 | $filterOperators[$filterField] = '='; |
||
215 | } else if ($type === 'many') { |
||
216 | $filter[$filterField] = array_map(function($value) { |
||
217 | return ['id' => $value]; |
||
218 | }, $filter[$filterField]); |
||
219 | $filterToUse[$filterField] = $filter[$filterField]; |
||
220 | } else { |
||
221 | $filterToUse[$filterField] = '%'.$filter[$filterField].'%'; |
||
222 | $filterOperators[$filterField] = 'LIKE'; |
||
223 | } |
||
224 | } |
||
225 | } |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Setups the templates. |
||
230 | * |
||
231 | * @param Application $app |
||
232 | * the Application instance of the Silex application |
||
233 | */ |
||
234 | protected function setupTemplates(Application $app) { |
||
243 | |||
244 | /** |
||
245 | * Setups the routes. |
||
246 | * |
||
247 | * @param Application $app |
||
248 | * the Application instance of the Silex application |
||
249 | * |
||
250 | * @return mixed |
||
251 | * the created controller factory |
||
252 | */ |
||
253 | protected function setupRoutes(Application $app) { |
||
254 | |||
255 | $self = $this; |
||
256 | $entityCheck = function(Request $request, Application $app) use ($self) { |
||
257 | if (!$app['crud']->getData($request->get('entity'))) { |
||
258 | return $self->getNotFoundPage($app, $app['translator']->trans('crudlex.entityNotFound')); |
||
259 | } |
||
260 | }; |
||
261 | |||
262 | $class = get_class($this); |
||
263 | $factory = $app['controllers_factory']; |
||
264 | $factory->get('/resource/static', $class.'::staticFile')->bind('static'); |
||
265 | $factory->match('/{entity}/create', $class.'::create')->bind('crudCreate')->before($entityCheck); |
||
266 | $factory->match('/{entity}', $class.'::showList')->bind('crudList')->before($entityCheck); |
||
267 | $factory->match('/{entity}/{id}', $class.'::show')->bind('crudShow')->before($entityCheck); |
||
268 | $factory->match('/{entity}/{id}/edit', $class.'::edit')->bind('crudEdit')->before($entityCheck); |
||
269 | $factory->post('/{entity}/{id}/delete', $class.'::delete')->bind('crudDelete')->before($entityCheck); |
||
270 | $factory->match('/{entity}/{id}/{field}/file', $class.'::renderFile')->bind('crudRenderFile')->before($entityCheck); |
||
271 | $factory->post('/{entity}/{id}/{field}/delete', $class.'::deleteFile')->bind('crudDeleteFile')->before($entityCheck); |
||
272 | $factory->get('/setting/locale/{locale}', $class.'::setLocale')->bind('crudSetLocale'); |
||
273 | |||
274 | return $factory; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Setups i18n. |
||
279 | * |
||
280 | * @param Application $app |
||
281 | * the Application instance of the Silex application |
||
282 | */ |
||
283 | protected function setupI18n(Application $app) { |
||
284 | $app->before(function(Request $request, Application $app) { |
||
285 | if ($app['crud']->isManagingI18n()) { |
||
286 | $locale = $app['session']->get('locale', 'en'); |
||
287 | $app['translator']->setLocale($locale); |
||
288 | } |
||
289 | $locale = $app['translator']->getLocale(); |
||
290 | $app['crud']->setLocale($locale); |
||
291 | }); |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * Implements ControllerProviderInterface::connect() connecting this |
||
296 | * controller. |
||
297 | * |
||
298 | * @param Application $app |
||
299 | * the Application instance of the Silex application |
||
300 | * |
||
301 | * @return \SilexController\Collection |
||
302 | * this method is expected to return the used ControllerCollection instance |
||
303 | */ |
||
304 | public function connect(Application $app) { |
||
310 | |||
311 | /** |
||
312 | * The controller for the "create" action. |
||
313 | * |
||
314 | * @param Application $app |
||
315 | * the Silex application |
||
316 | * @param string $entity |
||
317 | * the current entity |
||
318 | * |
||
319 | * @return Response |
||
320 | * the HTTP response of this action |
||
321 | */ |
||
322 | public function create(Application $app, $entity) { |
||
327 | |||
328 | /** |
||
329 | * The controller for the "show list" action. |
||
330 | * |
||
331 | * @param Request $request |
||
332 | * the current request |
||
333 | * @param Application $app |
||
334 | * the Silex application |
||
335 | * @param string $entity |
||
336 | * the current entity |
||
337 | * |
||
338 | * @return Response |
||
339 | * the HTTP response of this action or 404 on invalid input |
||
340 | */ |
||
341 | public function showList(Request $request, Application $app, $entity) { |
||
386 | |||
387 | /** |
||
388 | * The controller for the "show" action. |
||
389 | * |
||
390 | * @param Application $app |
||
391 | * the Silex application |
||
392 | * @param string $entity |
||
393 | * the current entity |
||
394 | * @param string $id |
||
395 | * the instance id to show |
||
396 | * |
||
397 | * @return Response |
||
398 | * the HTTP response of this action or 404 on invalid input |
||
399 | */ |
||
400 | public function show(Application $app, $entity, $id) { |
||
435 | |||
436 | /** |
||
437 | * The controller for the "edit" action. |
||
438 | * |
||
439 | * @param Application $app |
||
440 | * the Silex application |
||
441 | * @param string $entity |
||
442 | * the current entity |
||
443 | * @param string $id |
||
444 | * the instance id to edit |
||
445 | * |
||
446 | * @return Response |
||
447 | * the HTTP response of this action or 404 on invalid input |
||
448 | */ |
||
449 | public function edit(Application $app, $entity, $id) { |
||
458 | |||
459 | /** |
||
460 | * The controller for the "delete" action. |
||
461 | * |
||
462 | * @param Application $app |
||
463 | * the Silex application |
||
464 | * @param string $entity |
||
465 | * the current entity |
||
466 | * @param string $id |
||
467 | * the instance id to delete |
||
468 | * |
||
469 | * @return Response |
||
470 | * redirects to the entity list page or 404 on invalid input |
||
471 | */ |
||
472 | public function delete(Application $app, $entity, $id) { |
||
500 | |||
501 | /** |
||
502 | * The controller for the "render file" action. |
||
503 | * |
||
504 | * @param Application $app |
||
505 | * the Silex application |
||
506 | * @param string $entity |
||
507 | * the current entity |
||
508 | * @param string $id |
||
509 | * the instance id |
||
510 | * @param string $field |
||
511 | * the field of the file to render of the instance |
||
512 | * |
||
513 | * @return Response |
||
514 | * the rendered file |
||
515 | */ |
||
516 | public function renderFile(Application $app, $entity, $id, $field) { |
||
525 | |||
526 | /** |
||
527 | * The controller for the "delete file" action. |
||
528 | * |
||
529 | * @param Application $app |
||
530 | * the Silex application |
||
531 | * @param string $entity |
||
532 | * the current entity |
||
533 | * @param string $id |
||
534 | * the instance id |
||
535 | * @param string $field |
||
536 | * the field of the file to delete of the instance |
||
537 | * |
||
538 | * @return Response |
||
539 | * redirects to the instance details page or 404 on invalid input |
||
540 | */ |
||
541 | public function deleteFile(Application $app, $entity, $id, $field) { |
||
556 | |||
557 | /** |
||
558 | * The controller for serving static files. |
||
559 | * |
||
560 | * @param Request $request |
||
561 | * the current request |
||
562 | * @param Application $app |
||
563 | * the Silex application |
||
564 | * |
||
565 | * @return Response |
||
566 | * redirects to the instance details page or 404 on invalid input |
||
567 | */ |
||
568 | public function staticFile(Request $request, Application $app) { |
||
589 | |||
590 | /** |
||
591 | * The controller for setting the locale. |
||
592 | * |
||
593 | * @param Request $request |
||
594 | * the current request |
||
595 | * @param Application $app |
||
596 | * the Silex application |
||
597 | * @param string $locale |
||
598 | * the new locale |
||
599 | * |
||
600 | * @return Response |
||
601 | * redirects to the instance details page or 404 on invalid input |
||
602 | */ |
||
603 | public function setLocale(Request $request, Application $app, $locale) { |
||
615 | } |
||
616 |
$redirect
can contain request data and is used in output context(s) leading to a potential security vulnerability.8 paths for user data to reach this point
$this->parameters['HTTP_AUTHORIZATION']
seems to return tainted data, and$authorizationHeader
is assigned in ServerBag.php on line 62$this->parameters['HTTP_AUTHORIZATION']
seems to return tainted data, and$authorizationHeader
is assignedin vendor/ServerBag.php on line 62
in vendor/ServerBag.php on line 77
in vendor/ParameterBag.php on line 88
$result
is assignedin vendor/Request.php on line 719
$redirect
is assignedin src/CRUDlex/ControllerProvider.php on line 612
$_POST,
and$_POST
is passed to Request::createRequestFromFactory() in Request.php on line 281$_POST,
and$_POST
is passed to Request::createRequestFromFactory()in vendor/Request.php on line 281
$request
is passed to Request::__construct()in vendor/Request.php on line 1929
$request
is passed to Request::initialize()in vendor/Request.php on line 222
$request
is passed to ParameterBag::__construct()in vendor/Request.php on line 240
in vendor/ParameterBag.php on line 35
in vendor/ParameterBag.php on line 88
$result
is assignedin vendor/Request.php on line 719
$redirect
is assignedin src/CRUDlex/ControllerProvider.php on line 612
$_SERVER,
and$server
is assigned in Request.php on line 271$_SERVER,
and$server
is assignedin vendor/Request.php on line 271
$server
is passed to Request::createRequestFromFactory()in vendor/Request.php on line 281
$server
is passed to Request::__construct()in vendor/Request.php on line 1929
$server
is passed to Request::initialize()in vendor/Request.php on line 222
$server
is passed to ParameterBag::__construct()in vendor/Request.php on line 245
in vendor/ParameterBag.php on line 35
in vendor/ParameterBag.php on line 88
$result
is assignedin vendor/Request.php on line 719
$redirect
is assignedin src/CRUDlex/ControllerProvider.php on line 612
HTTP_CONTENT_LENGTH
from$_SERVER,
and$server
is assigned in Request.php on line 274HTTP_CONTENT_LENGTH
from$_SERVER,
and$server
is assignedin vendor/Request.php on line 274
$server
is passed to Request::createRequestFromFactory()in vendor/Request.php on line 281
$server
is passed to Request::__construct()in vendor/Request.php on line 1929
$server
is passed to Request::initialize()in vendor/Request.php on line 222
$server
is passed to ParameterBag::__construct()in vendor/Request.php on line 245
in vendor/ParameterBag.php on line 35
in vendor/ParameterBag.php on line 88
$result
is assignedin vendor/Request.php on line 719
$redirect
is assignedin src/CRUDlex/ControllerProvider.php on line 612
HTTP_CONTENT_TYPE
from$_SERVER,
and$server
is assigned in Request.php on line 277HTTP_CONTENT_TYPE
from$_SERVER,
and$server
is assignedin vendor/Request.php on line 277
$server
is passed to Request::createRequestFromFactory()in vendor/Request.php on line 281
$server
is passed to Request::__construct()in vendor/Request.php on line 1929
$server
is passed to Request::initialize()in vendor/Request.php on line 222
$server
is passed to ParameterBag::__construct()in vendor/Request.php on line 245
in vendor/ParameterBag.php on line 35
in vendor/ParameterBag.php on line 88
$result
is assignedin vendor/Request.php on line 719
$redirect
is assignedin src/CRUDlex/ControllerProvider.php on line 612
$server['HTTP_HOST']
seems to return tainted data, and$server
is assigned in Request.php on line 347$server['HTTP_HOST']
seems to return tainted data, and$server
is assignedin vendor/Request.php on line 347
$server
is assignedin vendor/Request.php on line 395
$server
is assignedin vendor/Request.php on line 396
$server
is passed to Request::createRequestFromFactory()in vendor/Request.php on line 398
$server
is passed to Request::__construct()in vendor/Request.php on line 1929
$server
is passed to Request::initialize()in vendor/Request.php on line 222
$server
is passed to ParameterBag::__construct()in vendor/Request.php on line 245
in vendor/ParameterBag.php on line 35
in vendor/ParameterBag.php on line 88
$result
is assignedin vendor/Request.php on line 719
$redirect
is assignedin src/CRUDlex/ControllerProvider.php on line 612
$this->parameters['PHP_AUTH_USER']
seems to return tainted data, and$headers
is assigned in ServerBag.php on line 43$this->parameters['PHP_AUTH_USER']
seems to return tainted data, and$headers
is assignedin vendor/ServerBag.php on line 43
$headers
is assignedin vendor/ServerBag.php on line 44
$this->server->getHeaders()
is passed to HeaderBag::__construct()in vendor/Request.php on line 246
$values
is assignedin vendor/HeaderBag.php on line 31
$values
is passed to HeaderBag::set()in vendor/HeaderBag.php on line 32
(array) $values
is passed through array_values(), and$values
is assignedin vendor/HeaderBag.php on line 142
in vendor/HeaderBag.php on line 145
in vendor/HeaderBag.php on line 125
$requestUri
is assignedin vendor/Request.php on line 1699
$requestUri
is passed to ParameterBag::set()in vendor/Request.php on line 1730
in vendor/ParameterBag.php on line 99
in vendor/ParameterBag.php on line 88
$result
is assignedin vendor/Request.php on line 719
$redirect
is assignedin src/CRUDlex/ControllerProvider.php on line 612
$this->parameters['PHP_AUTH_PW']
seems to return tainted data, and$headers
is assigned in ServerBag.php on line 44$this->parameters['PHP_AUTH_PW']
seems to return tainted data, and$headers
is assignedin vendor/ServerBag.php on line 44
$this->server->getHeaders()
is passed to HeaderBag::__construct()in vendor/Request.php on line 246
$values
is assignedin vendor/HeaderBag.php on line 31
$values
is passed to HeaderBag::set()in vendor/HeaderBag.php on line 32
(array) $values
is passed through array_values(), and$values
is assignedin vendor/HeaderBag.php on line 142
in vendor/HeaderBag.php on line 145
in vendor/HeaderBag.php on line 125
$requestUri
is assignedin vendor/Request.php on line 1699
$requestUri
is passed to ParameterBag::set()in vendor/Request.php on line 1730
in vendor/ParameterBag.php on line 99
in vendor/ParameterBag.php on line 88
$result
is assignedin vendor/Request.php on line 719
$redirect
is assignedin src/CRUDlex/ControllerProvider.php on line 612
Used in output context
in vendor/src/Silex/Application.php on line 376
in vendor/RedirectResponse.php on line 39
in vendor/RedirectResponse.php on line 82
in vendor/Response.php on line 406
in vendor/Response.php on line 365
Preventing Cross-Site-Scripting Attacks
Cross-Site-Scripting allows an attacker to inject malicious code into your website - in particular Javascript code, and have that code executed with the privileges of a visiting user. This can be used to obtain data, or perform actions on behalf of that visiting user.
In order to prevent this, make sure to escape all user-provided data:
General Strategies to prevent injection
In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:
For numeric data, we recommend to explicitly cast the data: