|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the CRUDlex package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Philip Lehmann-Böhm <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace CRUDlex; |
|
13
|
|
|
|
|
14
|
|
|
use League\Flysystem\Util\MimeType; |
|
15
|
|
|
use Silex\Application; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* This is the Controller offering all CRUD pages. |
|
23
|
|
|
* |
|
24
|
|
|
* It offers functions for this routes: |
|
25
|
|
|
* |
|
26
|
|
|
* "/resource/static" serving static resources |
|
27
|
|
|
* |
|
28
|
|
|
* "/{entity}/create" creation page of the entity |
|
29
|
|
|
* |
|
30
|
|
|
* "/{entity}" list page of the entity |
|
31
|
|
|
* |
|
32
|
|
|
* "/{entity}/{id}" details page of a single entity instance |
|
33
|
|
|
* |
|
34
|
|
|
* "/{entity}/{id}/edit" edit page of a single entity instance |
|
35
|
|
|
* |
|
36
|
|
|
* "/{entity}/{id}/delete" POST only deletion route for an entity instance |
|
37
|
|
|
* |
|
38
|
|
|
* "/{entity}/{id}/{field}/file" renders a file field of an entity instance |
|
39
|
|
|
* |
|
40
|
|
|
* "/{entity}/{id}/{field}/delete" POST only deletion of a file field of an entity instance |
|
41
|
|
|
*/ |
|
42
|
|
|
class Controller { |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Postprocesses the entity after modification by handling the uploaded |
|
46
|
|
|
* files and setting the flash. |
|
47
|
|
|
* |
|
48
|
|
|
* @param Application $app |
|
49
|
|
|
* the current application |
|
50
|
|
|
* @param AbstractData $crudData |
|
51
|
|
|
* the data instance of the entity |
|
52
|
|
|
* @param Entity $instance |
|
53
|
|
|
* the entity |
|
54
|
|
|
* @param string $entity |
|
55
|
|
|
* the name of the entity |
|
56
|
|
|
* @param string $mode |
|
57
|
|
|
* whether to 'edit' or to 'create' the entity |
|
58
|
|
|
* |
|
59
|
|
|
* @return null|\Symfony\Component\HttpFoundation\RedirectResponse |
|
60
|
|
|
* the HTTP response of this modification |
|
61
|
|
|
*/ |
|
62
|
4 |
|
protected function modifyFilesAndSetFlashBag(Application $app, AbstractData $crudData, Entity $instance, $entity, $mode) |
|
63
|
|
|
{ |
|
64
|
4 |
|
$id = $instance->get('id'); |
|
65
|
4 |
|
$request = $app['request_stack']->getCurrentRequest(); |
|
66
|
4 |
|
$fileHandler = new FileHandler($app['crud.filesystem'], $crudData->getDefinition()); |
|
67
|
4 |
|
$result = $mode == 'edit' ? $fileHandler->updateFiles($crudData, $request, $instance, $entity) : $fileHandler->createFiles($crudData, $request, $instance, $entity); |
|
68
|
4 |
|
if (!$result) { |
|
69
|
2 |
|
return null; |
|
70
|
|
|
} |
|
71
|
4 |
|
$app['session']->getFlashBag()->add('success', $app['translator']->trans('crudlex.'.$mode.'.success', [ |
|
72
|
4 |
|
'%label%' => $crudData->getDefinition()->getLabel(), |
|
73
|
4 |
|
'%id%' => $id |
|
74
|
|
|
])); |
|
75
|
4 |
|
return $app->redirect($app['url_generator']->generate('crudShow', ['entity' => $entity, 'id' => $id])); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Sets the flashes of a failed entity modification. |
|
80
|
|
|
* |
|
81
|
|
|
* @param Application $app |
|
82
|
|
|
* the current application |
|
83
|
|
|
* @param boolean $optimisticLocking |
|
84
|
|
|
* whether the optimistic locking failed |
|
85
|
|
|
* @param string $mode |
|
86
|
|
|
* the modification mode, either 'create' or 'edit' |
|
87
|
|
|
*/ |
|
88
|
2 |
|
protected function setValidationFailedFlashes(Application $app, $optimisticLocking, $mode) |
|
89
|
|
|
{ |
|
90
|
2 |
|
$app['session']->getFlashBag()->add('danger', $app['translator']->trans('crudlex.'.$mode.'.error')); |
|
91
|
2 |
|
if ($optimisticLocking) { |
|
92
|
1 |
|
$app['session']->getFlashBag()->add('danger', $app['translator']->trans('crudlex.edit.locked')); |
|
93
|
|
|
} |
|
94
|
2 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Validates and saves the new or updated entity and returns the appropriate HTTP |
|
98
|
|
|
* response. |
|
99
|
|
|
* |
|
100
|
|
|
* @param Application $app |
|
101
|
|
|
* the current application |
|
102
|
|
|
* @param AbstractData $crudData |
|
103
|
|
|
* the data instance of the entity |
|
104
|
|
|
* @param Entity $instance |
|
105
|
|
|
* the entity |
|
106
|
|
|
* @param string $entity |
|
107
|
|
|
* the name of the entity |
|
108
|
|
|
* @param boolean $edit |
|
109
|
|
|
* whether to edit (true) or to create (false) the entity |
|
110
|
|
|
* |
|
111
|
|
|
* @return Response |
|
112
|
|
|
* the HTTP response of this modification |
|
113
|
|
|
*/ |
|
114
|
5 |
|
protected function modifyEntity(Application $app, AbstractData $crudData, Entity $instance, $entity, $edit) |
|
115
|
|
|
{ |
|
116
|
5 |
|
$fieldErrors = []; |
|
117
|
5 |
|
$mode = $edit ? 'edit' : 'create'; |
|
118
|
5 |
|
$request = $app['request_stack']->getCurrentRequest(); |
|
119
|
5 |
|
if ($request->getMethod() == 'POST') { |
|
120
|
4 |
|
$instance->populateViaRequest($request); |
|
121
|
4 |
|
$validator = new EntityValidator($instance); |
|
122
|
4 |
|
$validation = $validator->validate($crudData, intval($request->get('version'))); |
|
123
|
|
|
|
|
124
|
4 |
|
$fieldErrors = $validation['errors']; |
|
125
|
4 |
|
if (!$validation['valid']) { |
|
126
|
2 |
|
$optimisticLocking = isset($fieldErrors['version']); |
|
127
|
2 |
|
$this->setValidationFailedFlashes($app, $optimisticLocking, $mode); |
|
128
|
|
|
} else { |
|
129
|
4 |
|
$modified = $edit ? $crudData->update($instance) : $crudData->create($instance); |
|
130
|
4 |
|
$response = $modified ? $this->modifyFilesAndSetFlashBag($app, $crudData, $instance, $entity, $mode) : false; |
|
131
|
4 |
|
if ($response) { |
|
132
|
4 |
|
return $response; |
|
133
|
|
|
} |
|
134
|
2 |
|
$app['session']->getFlashBag()->add('danger', $app['translator']->trans('crudlex.'.$mode.'.failed')); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
3 |
|
return $app['twig']->render($app['crud']->getTemplate('template', 'form', $entity), [ |
|
139
|
3 |
|
'crud' => $app['crud'], |
|
140
|
3 |
|
'crudEntity' => $entity, |
|
141
|
3 |
|
'crudData' => $crudData, |
|
142
|
3 |
|
'entity' => $instance, |
|
143
|
3 |
|
'mode' => $mode, |
|
144
|
3 |
|
'fieldErrors' => $fieldErrors, |
|
145
|
3 |
|
'layout' => $app['crud']->getTemplate('layout', $mode, $entity) |
|
146
|
|
|
]); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Gets the parameters for the redirection after deleting an entity. |
|
151
|
|
|
* |
|
152
|
|
|
* @param Request $request |
|
153
|
|
|
* the current request |
|
154
|
|
|
* @param string $entity |
|
155
|
|
|
* the entity name |
|
156
|
|
|
* @param string $redirectPage |
|
157
|
|
|
* reference, where the page to redirect to will be stored |
|
158
|
|
|
* |
|
159
|
|
|
* @return array<string,string> |
|
160
|
|
|
* the parameters of the redirection, entity and id |
|
161
|
|
|
*/ |
|
162
|
1 |
|
protected function getAfterDeleteRedirectParameters(Request $request, $entity, &$redirectPage) |
|
163
|
|
|
{ |
|
164
|
1 |
|
$redirectPage = 'crudList'; |
|
165
|
1 |
|
$redirectParameters = ['entity' => $entity]; |
|
166
|
1 |
|
$redirectEntity = $request->get('redirectEntity'); |
|
167
|
1 |
|
$redirectId = $request->get('redirectId'); |
|
168
|
1 |
|
if ($redirectEntity && $redirectId) { |
|
169
|
1 |
|
$redirectPage = 'crudShow'; |
|
170
|
|
|
$redirectParameters = [ |
|
171
|
1 |
|
'entity' => $redirectEntity, |
|
172
|
1 |
|
'id' => $redirectId |
|
173
|
|
|
]; |
|
174
|
|
|
} |
|
175
|
1 |
|
return $redirectParameters; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Builds up the parameters of the list page filters. |
|
180
|
|
|
* |
|
181
|
|
|
* @param Request $request |
|
182
|
|
|
* the current application |
|
183
|
|
|
* @param EntityDefinition $definition |
|
184
|
|
|
* the current entity definition |
|
185
|
|
|
* @param array &$filter |
|
186
|
|
|
* will hold a map of fields to request parameters for the filters |
|
187
|
|
|
* @param boolean $filterActive |
|
188
|
|
|
* reference, will be true if at least one filter is active |
|
189
|
|
|
* @param array $filterToUse |
|
190
|
|
|
* reference, will hold a map of fields to integers (0 or 1) which boolean filters are active |
|
191
|
|
|
* @param array $filterOperators |
|
192
|
|
|
* reference, will hold a map of fields to operators for AbstractData::listEntries() |
|
193
|
|
|
*/ |
|
194
|
4 |
|
protected function buildUpListFilter(Request $request, EntityDefinition $definition, &$filter, &$filterActive, &$filterToUse, &$filterOperators) |
|
195
|
|
|
{ |
|
196
|
4 |
|
foreach ($definition->getFilter() as $filterField) { |
|
197
|
4 |
|
$type = $definition->getType($filterField); |
|
198
|
4 |
|
$filter[$filterField] = $request->get('crudFilter'.$filterField); |
|
199
|
4 |
|
if ($filter[$filterField]) { |
|
200
|
1 |
|
$filterActive = true; |
|
201
|
1 |
|
$filterToUse[$filterField] = $filter[$filterField]; |
|
202
|
1 |
|
$filterOperators[$filterField] = '='; |
|
203
|
1 |
|
if ($type === 'boolean') { |
|
204
|
1 |
|
$filterToUse[$filterField] = $filter[$filterField] == 'true' ? 1 : 0; |
|
205
|
1 |
|
} else if ($type === 'reference') { |
|
206
|
1 |
|
$filter[$filterField] = ['id' => $filter[$filterField]]; |
|
207
|
1 |
|
} else if ($type === 'many') { |
|
208
|
1 |
|
$filter[$filterField] = array_map(function($value) { |
|
209
|
1 |
|
return ['id' => $value]; |
|
210
|
1 |
|
}, $filter[$filterField]); |
|
211
|
1 |
|
$filterToUse[$filterField] = $filter[$filterField]; |
|
212
|
1 |
|
} else if (in_array($type, ['text', 'multiline', 'fixed'])){ |
|
213
|
1 |
|
$filterToUse[$filterField] = '%'.$filter[$filterField].'%'; |
|
214
|
4 |
|
$filterOperators[$filterField] = 'LIKE'; |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
4 |
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Generates the not found page. |
|
222
|
|
|
* |
|
223
|
|
|
* @param Application $app |
|
224
|
|
|
* the Silex application |
|
225
|
|
|
* @param string $error |
|
226
|
|
|
* the cause of the not found error |
|
227
|
|
|
* |
|
228
|
|
|
* @return Response |
|
229
|
|
|
* the rendered not found page with the status code 404 |
|
230
|
|
|
*/ |
|
231
|
9 |
|
public function getNotFoundPage(Application $app, $error) |
|
232
|
|
|
{ |
|
233
|
9 |
|
return new Response($app['twig']->render('@crud/notFound.twig', [ |
|
234
|
9 |
|
'crud' => $app['crud'], |
|
235
|
9 |
|
'error' => $error, |
|
236
|
9 |
|
'crudEntity' => '', |
|
237
|
9 |
|
'layout' => $app['crud']->getTemplate('layout', '', '') |
|
238
|
9 |
|
]), 404); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* The controller for the "create" action. |
|
243
|
|
|
* |
|
244
|
|
|
* @param Application $app |
|
245
|
|
|
* the Silex application |
|
246
|
|
|
* @param string $entity |
|
247
|
|
|
* the current entity |
|
248
|
|
|
* |
|
249
|
|
|
* @return Response |
|
250
|
|
|
* the HTTP response of this action |
|
251
|
|
|
*/ |
|
252
|
4 |
|
public function create(Application $app, $entity) |
|
253
|
|
|
{ |
|
254
|
4 |
|
$crudData = $app['crud']->getData($entity); |
|
255
|
4 |
|
$instance = $crudData->createEmpty(); |
|
256
|
4 |
|
$request = $app['request_stack']->getCurrentRequest(); |
|
257
|
4 |
|
$instance->populateViaRequest($request); |
|
258
|
4 |
|
return $this->modifyEntity($app, $crudData, $instance, $entity, false); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* The controller for the "show list" action. |
|
263
|
|
|
* |
|
264
|
|
|
* @param Request $request |
|
265
|
|
|
* the current request |
|
266
|
|
|
* @param Application $app |
|
267
|
|
|
* the Silex application |
|
268
|
|
|
* @param string $entity |
|
269
|
|
|
* the current entity |
|
270
|
|
|
* |
|
271
|
|
|
* @return Response |
|
272
|
|
|
* the HTTP response of this action or 404 on invalid input |
|
273
|
|
|
*/ |
|
274
|
4 |
|
public function showList(Request $request, Application $app, $entity) |
|
275
|
|
|
{ |
|
276
|
4 |
|
$crudData = $app['crud']->getData($entity); |
|
277
|
4 |
|
$definition = $crudData->getDefinition(); |
|
278
|
|
|
|
|
279
|
4 |
|
$filter = []; |
|
280
|
4 |
|
$filterActive = false; |
|
281
|
4 |
|
$filterToUse = []; |
|
282
|
4 |
|
$filterOperators = []; |
|
283
|
4 |
|
$this->buildUpListFilter($request, $definition, $filter, $filterActive, $filterToUse, $filterOperators); |
|
284
|
|
|
|
|
285
|
4 |
|
$pageSize = $definition->getPageSize(); |
|
286
|
4 |
|
$total = $crudData->countBy($definition->getTable(), $filterToUse, $filterOperators, true); |
|
287
|
4 |
|
$page = abs(intval($request->get('crudPage', 0))); |
|
288
|
4 |
|
$maxPage = intval($total / $pageSize); |
|
289
|
4 |
|
if ($total % $pageSize == 0) { |
|
290
|
4 |
|
$maxPage--; |
|
291
|
|
|
} |
|
292
|
4 |
|
if ($page > $maxPage) { |
|
293
|
4 |
|
$page = $maxPage; |
|
294
|
|
|
} |
|
295
|
4 |
|
$skip = $page * $pageSize; |
|
296
|
|
|
|
|
297
|
4 |
|
$sortField = $request->get('crudSortField', $definition->getInitialSortField()); |
|
298
|
4 |
|
$sortAscendingRequest = $request->get('crudSortAscending'); |
|
299
|
4 |
|
$sortAscending = $sortAscendingRequest !== null ? $sortAscendingRequest === 'true' : $definition->isInitialSortAscending(); |
|
300
|
|
|
|
|
301
|
4 |
|
$entities = $crudData->listEntries($filterToUse, $filterOperators, $skip, $pageSize, $sortField, $sortAscending); |
|
302
|
|
|
|
|
303
|
4 |
|
return $app['twig']->render($app['crud']->getTemplate('template', 'list', $entity), [ |
|
304
|
4 |
|
'crud' => $app['crud'], |
|
305
|
4 |
|
'crudEntity' => $entity, |
|
306
|
4 |
|
'crudData' => $crudData, |
|
307
|
4 |
|
'definition' => $definition, |
|
308
|
4 |
|
'entities' => $entities, |
|
309
|
4 |
|
'pageSize' => $pageSize, |
|
310
|
4 |
|
'maxPage' => $maxPage, |
|
311
|
4 |
|
'page' => $page, |
|
312
|
4 |
|
'total' => $total, |
|
313
|
4 |
|
'filter' => $filter, |
|
314
|
4 |
|
'filterActive' => $filterActive, |
|
315
|
4 |
|
'sortField' => $sortField, |
|
316
|
4 |
|
'sortAscending' => $sortAscending, |
|
317
|
4 |
|
'layout' => $app['crud']->getTemplate('layout', 'list', $entity) |
|
318
|
|
|
]); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* The controller for the "show" action. |
|
323
|
|
|
* |
|
324
|
|
|
* @param Application $app |
|
325
|
|
|
* the Silex application |
|
326
|
|
|
* @param string $entity |
|
327
|
|
|
* the current entity |
|
328
|
|
|
* @param string $id |
|
329
|
|
|
* the instance id to show |
|
330
|
|
|
* |
|
331
|
|
|
* @return Response |
|
332
|
|
|
* the HTTP response of this action or 404 on invalid input |
|
333
|
|
|
*/ |
|
334
|
6 |
|
public function show(Application $app, $entity, $id) |
|
335
|
|
|
{ |
|
336
|
6 |
|
$crudData = $app['crud']->getData($entity); |
|
337
|
6 |
|
$instance = $crudData->get($id); |
|
338
|
6 |
|
if (!$instance) { |
|
339
|
1 |
|
return $this->getNotFoundPage($app, $app['translator']->trans('crudlex.instanceNotFound')); |
|
340
|
|
|
} |
|
341
|
6 |
|
$definition = $crudData->getDefinition(); |
|
342
|
|
|
|
|
343
|
6 |
|
$childrenLabelFields = $definition->getChildrenLabelFields(); |
|
344
|
6 |
|
$children = []; |
|
345
|
6 |
|
if (count($childrenLabelFields) > 0) { |
|
346
|
3 |
|
foreach ($definition->getChildren() as $child) { |
|
347
|
3 |
|
$childField = $child[1]; |
|
348
|
3 |
|
$childEntity = $child[2]; |
|
349
|
3 |
|
$childLabelField = array_key_exists($childEntity, $childrenLabelFields) ? $childrenLabelFields[$childEntity] : 'id'; |
|
350
|
3 |
|
$childCrud = $app['crud']->getData($childEntity); |
|
351
|
3 |
|
$children[] = [ |
|
352
|
3 |
|
$childCrud->getDefinition()->getLabel(), |
|
353
|
3 |
|
$childEntity, |
|
354
|
3 |
|
$childLabelField, |
|
355
|
3 |
|
$childCrud->listEntries([$childField => $instance->get('id')]), |
|
356
|
3 |
|
$childField |
|
357
|
|
|
]; |
|
358
|
|
|
} |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
6 |
|
return $app['twig']->render($app['crud']->getTemplate('template', 'show', $entity), [ |
|
362
|
6 |
|
'crud' => $app['crud'], |
|
363
|
6 |
|
'crudEntity' => $entity, |
|
364
|
6 |
|
'entity' => $instance, |
|
365
|
6 |
|
'children' => $children, |
|
366
|
6 |
|
'layout' => $app['crud']->getTemplate('layout', 'show', $entity) |
|
367
|
|
|
]); |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* The controller for the "edit" action. |
|
372
|
|
|
* |
|
373
|
|
|
* @param Application $app |
|
374
|
|
|
* the Silex application |
|
375
|
|
|
* @param string $entity |
|
376
|
|
|
* the current entity |
|
377
|
|
|
* @param string $id |
|
378
|
|
|
* the instance id to edit |
|
379
|
|
|
* |
|
380
|
|
|
* @return Response |
|
381
|
|
|
* the HTTP response of this action or 404 on invalid input |
|
382
|
|
|
*/ |
|
383
|
1 |
|
public function edit(Application $app, $entity, $id) |
|
384
|
|
|
{ |
|
385
|
1 |
|
$crudData = $app['crud']->getData($entity); |
|
386
|
1 |
|
$instance = $crudData->get($id); |
|
387
|
1 |
|
if (!$instance) { |
|
388
|
1 |
|
return $this->getNotFoundPage($app, $app['translator']->trans('crudlex.instanceNotFound')); |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
1 |
|
return $this->modifyEntity($app, $crudData, $instance, $entity, true); |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
/** |
|
395
|
|
|
* The controller for the "delete" action. |
|
396
|
|
|
* |
|
397
|
|
|
* @param Application $app |
|
398
|
|
|
* the Silex application |
|
399
|
|
|
* @param string $entity |
|
400
|
|
|
* the current entity |
|
401
|
|
|
* @param string $id |
|
402
|
|
|
* the instance id to delete |
|
403
|
|
|
* |
|
404
|
|
|
* @return Response |
|
405
|
|
|
* redirects to the entity list page or 404 on invalid input |
|
406
|
|
|
*/ |
|
407
|
1 |
|
public function delete(Application $app, $entity, $id) |
|
408
|
|
|
{ |
|
409
|
1 |
|
$crudData = $app['crud']->getData($entity); |
|
410
|
1 |
|
$instance = $crudData->get($id); |
|
411
|
1 |
|
if (!$instance) { |
|
412
|
1 |
|
return $this->getNotFoundPage($app, $app['translator']->trans('crudlex.instanceNotFound')); |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
1 |
|
$fileHandler = new FileHandler($app['crud.filesystem'], $crudData->getDefinition()); |
|
416
|
1 |
|
$filesDeleted = $fileHandler->deleteFiles($crudData, $instance, $entity); |
|
417
|
1 |
|
$deleted = $filesDeleted ? $crudData->delete($instance) : AbstractData::DELETION_FAILED_EVENT; |
|
418
|
|
|
|
|
419
|
1 |
|
if ($deleted === AbstractData::DELETION_FAILED_EVENT) { |
|
420
|
1 |
|
$app['session']->getFlashBag()->add('danger', $app['translator']->trans('crudlex.delete.failed')); |
|
421
|
1 |
|
return $app->redirect($app['url_generator']->generate('crudShow', ['entity' => $entity, 'id' => $id])); |
|
422
|
1 |
|
} elseif ($deleted === AbstractData::DELETION_FAILED_STILL_REFERENCED) { |
|
423
|
1 |
|
$app['session']->getFlashBag()->add('danger', $app['translator']->trans('crudlex.delete.error', [ |
|
424
|
1 |
|
'%label%' => $crudData->getDefinition()->getLabel() |
|
425
|
|
|
])); |
|
426
|
1 |
|
return $app->redirect($app['url_generator']->generate('crudShow', ['entity' => $entity, 'id' => $id])); |
|
427
|
|
|
} |
|
428
|
|
|
|
|
429
|
1 |
|
$redirectPage = 'crudList'; |
|
430
|
1 |
|
$redirectParameters = $this->getAfterDeleteRedirectParameters($app['request_stack']->getCurrentRequest(), $entity, $redirectPage); |
|
431
|
|
|
|
|
432
|
1 |
|
$app['session']->getFlashBag()->add('success', $app['translator']->trans('crudlex.delete.success', [ |
|
433
|
1 |
|
'%label%' => $crudData->getDefinition()->getLabel() |
|
434
|
|
|
])); |
|
435
|
1 |
|
return $app->redirect($app['url_generator']->generate($redirectPage, $redirectParameters)); |
|
436
|
|
|
} |
|
437
|
|
|
|
|
438
|
|
|
/** |
|
439
|
|
|
* The controller for the "render file" action. |
|
440
|
|
|
* |
|
441
|
|
|
* @param Application $app |
|
442
|
|
|
* the Silex application |
|
443
|
|
|
* @param string $entity |
|
444
|
|
|
* the current entity |
|
445
|
|
|
* @param string $id |
|
446
|
|
|
* the instance id |
|
447
|
|
|
* @param string $field |
|
448
|
|
|
* the field of the file to render of the instance |
|
449
|
|
|
* |
|
450
|
|
|
* @return Response |
|
451
|
|
|
* the rendered file |
|
452
|
|
|
*/ |
|
453
|
1 |
|
public function renderFile(Application $app, $entity, $id, $field) |
|
454
|
|
|
{ |
|
455
|
1 |
|
$crudData = $app['crud']->getData($entity); |
|
456
|
1 |
|
$instance = $crudData->get($id); |
|
457
|
1 |
|
$definition = $crudData->getDefinition(); |
|
458
|
1 |
|
if (!$instance || $definition->getType($field) != 'file' || !$instance->get($field)) { |
|
459
|
1 |
|
return $this->getNotFoundPage($app, $app['translator']->trans('crudlex.instanceNotFound')); |
|
460
|
|
|
} |
|
461
|
1 |
|
$fileHandler = new FileHandler($app['crud.filesystem'], $definition); |
|
462
|
1 |
|
return $fileHandler->renderFile($instance, $entity, $field); |
|
463
|
|
|
} |
|
464
|
|
|
|
|
465
|
|
|
/** |
|
466
|
|
|
* The controller for the "delete file" action. |
|
467
|
|
|
* |
|
468
|
|
|
* @param Application $app |
|
469
|
|
|
* the Silex application |
|
470
|
|
|
* @param string $entity |
|
471
|
|
|
* the current entity |
|
472
|
|
|
* @param string $id |
|
473
|
|
|
* the instance id |
|
474
|
|
|
* @param string $field |
|
475
|
|
|
* the field of the file to delete of the instance |
|
476
|
|
|
* |
|
477
|
|
|
* @return Response |
|
478
|
|
|
* redirects to the instance details page or 404 on invalid input |
|
479
|
|
|
*/ |
|
480
|
1 |
|
public function deleteFile(Application $app, $entity, $id, $field) |
|
481
|
|
|
{ |
|
482
|
1 |
|
$crudData = $app['crud']->getData($entity); |
|
483
|
1 |
|
$instance = $crudData->get($id); |
|
484
|
1 |
|
if (!$instance) { |
|
485
|
1 |
|
return $this->getNotFoundPage($app, $app['translator']->trans('crudlex.instanceNotFound')); |
|
486
|
|
|
} |
|
487
|
1 |
|
$fileHandler = new FileHandler($app['crud.filesystem'], $crudData->getDefinition()); |
|
488
|
1 |
|
if (!$crudData->getDefinition()->getField($field, 'required', false) && $fileHandler->deleteFile($crudData, $instance, $entity, $field)) { |
|
489
|
1 |
|
$instance->set($field, ''); |
|
490
|
1 |
|
$crudData->update($instance); |
|
491
|
1 |
|
$app['session']->getFlashBag()->add('success', $app['translator']->trans('crudlex.file.deleted')); |
|
492
|
|
|
} else { |
|
493
|
1 |
|
$app['session']->getFlashBag()->add('danger', $app['translator']->trans('crudlex.file.notDeleted')); |
|
494
|
|
|
} |
|
495
|
1 |
|
return $app->redirect($app['url_generator']->generate('crudShow', ['entity' => $entity, 'id' => $id])); |
|
496
|
|
|
} |
|
497
|
|
|
|
|
498
|
|
|
/** |
|
499
|
|
|
* The controller for serving static files. |
|
500
|
|
|
* |
|
501
|
|
|
* @param Request $request |
|
502
|
|
|
* the current request |
|
503
|
|
|
* @param Application $app |
|
504
|
|
|
* the Silex application |
|
505
|
|
|
* |
|
506
|
|
|
* @return Response |
|
507
|
|
|
* redirects to the instance details page or 404 on invalid input |
|
508
|
|
|
*/ |
|
509
|
1 |
|
public function staticFile(Request $request, Application $app) |
|
510
|
|
|
{ |
|
511
|
1 |
|
$fileParam = str_replace('..', '', $request->get('file')); |
|
512
|
1 |
|
$file = __DIR__.'/../static/'.$fileParam; |
|
513
|
1 |
|
if (!$fileParam || !file_exists($file)) { |
|
514
|
1 |
|
return $this->getNotFoundPage($app, $app['translator']->trans('crudlex.resourceNotFound')); |
|
515
|
|
|
} |
|
516
|
|
|
|
|
517
|
1 |
|
$mimeType = MimeType::detectByFilename($file); |
|
518
|
1 |
|
$size = filesize($file); |
|
519
|
|
|
|
|
520
|
1 |
|
$streamedFileResponse = new StreamedFileResponse(); |
|
521
|
1 |
|
$response = new StreamedResponse($streamedFileResponse->getStreamedFileFunction($file), 200, [ |
|
522
|
1 |
|
'Content-Type' => $mimeType, |
|
523
|
1 |
|
'Content-Disposition' => 'attachment; filename="'.basename($file).'"', |
|
524
|
1 |
|
'Content-length' => $size |
|
525
|
|
|
]); |
|
526
|
|
|
|
|
527
|
1 |
|
$response->setETag(filemtime($file))->setPublic()->isNotModified($request); |
|
528
|
1 |
|
$response->send(); |
|
529
|
|
|
|
|
530
|
1 |
|
return $response; |
|
531
|
|
|
} |
|
532
|
|
|
|
|
533
|
|
|
/** |
|
534
|
|
|
* The controller for setting the locale. |
|
535
|
|
|
* |
|
536
|
|
|
* @param Request $request |
|
537
|
|
|
* the current request |
|
538
|
|
|
* @param Application $app |
|
539
|
|
|
* the Silex application |
|
540
|
|
|
* @param string $locale |
|
541
|
|
|
* the new locale |
|
542
|
|
|
* |
|
543
|
|
|
* @return Response |
|
544
|
|
|
* redirects to the instance details page or 404 on invalid input |
|
545
|
|
|
*/ |
|
546
|
1 |
|
public function setLocale(Request $request, Application $app, $locale) |
|
547
|
|
|
{ |
|
548
|
|
|
|
|
549
|
1 |
|
if (!in_array($locale, $app['crud']->getLocales())) { |
|
550
|
1 |
|
return $this->getNotFoundPage($app, 'Locale '.$locale.' not found.'); |
|
551
|
|
|
} |
|
552
|
|
|
|
|
553
|
1 |
|
$manageI18n = $app['crud']->isManageI18n(); |
|
554
|
1 |
|
if ($manageI18n) { |
|
555
|
1 |
|
$app['session']->set('locale', $locale); |
|
556
|
|
|
} |
|
557
|
1 |
|
$redirect = $request->get('redirect'); |
|
558
|
1 |
|
return $app->redirect($redirect); |
|
|
|
|
|
|
559
|
|
|
} |
|
560
|
|
|
} |
$redirectcan 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$authorizationHeaderis assigned in ServerBag.php on line 62$this->parameters['HTTP_AUTHORIZATION']seems to return tainted data, and$authorizationHeaderis assignedin vendor/ServerBag.php on line 62
in vendor/ServerBag.php on line 77
in vendor/ParameterBag.php on line 84
$resultis assignedin vendor/Request.php on line 817
$redirectis assignedin src/CRUDlex/Controller.php on line 557
$_POST,and$_POSTis passed to Request::createRequestFromFactory() in Request.php on line 314$_POST,and$_POSTis passed to Request::createRequestFromFactory()in vendor/Request.php on line 314
$requestis passed to Request::__construct()in vendor/Request.php on line 2068
$requestis passed to Request::initialize()in vendor/Request.php on line 255
$requestis passed to ParameterBag::__construct()in vendor/Request.php on line 273
in vendor/ParameterBag.php on line 31
in vendor/ParameterBag.php on line 84
$resultis assignedin vendor/Request.php on line 817
$redirectis assignedin src/CRUDlex/Controller.php on line 557
$_SERVER,and$serveris assigned in Request.php on line 304$_SERVER,and$serveris assignedin vendor/Request.php on line 304
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 314
$serveris passed to Request::__construct()in vendor/Request.php on line 2068
$serveris passed to Request::initialize()in vendor/Request.php on line 255
$serveris passed to ParameterBag::__construct()in vendor/Request.php on line 278
in vendor/ParameterBag.php on line 31
in vendor/ParameterBag.php on line 84
$resultis assignedin vendor/Request.php on line 817
$redirectis assignedin src/CRUDlex/Controller.php on line 557
HTTP_CONTENT_LENGTHfrom$_SERVER,and$serveris assigned in Request.php on line 307HTTP_CONTENT_LENGTHfrom$_SERVER,and$serveris assignedin vendor/Request.php on line 307
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 314
$serveris passed to Request::__construct()in vendor/Request.php on line 2068
$serveris passed to Request::initialize()in vendor/Request.php on line 255
$serveris passed to ParameterBag::__construct()in vendor/Request.php on line 278
in vendor/ParameterBag.php on line 31
in vendor/ParameterBag.php on line 84
$resultis assignedin vendor/Request.php on line 817
$redirectis assignedin src/CRUDlex/Controller.php on line 557
HTTP_CONTENT_TYPEfrom$_SERVER,and$serveris assigned in Request.php on line 310HTTP_CONTENT_TYPEfrom$_SERVER,and$serveris assignedin vendor/Request.php on line 310
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 314
$serveris passed to Request::__construct()in vendor/Request.php on line 2068
$serveris passed to Request::initialize()in vendor/Request.php on line 255
$serveris passed to ParameterBag::__construct()in vendor/Request.php on line 278
in vendor/ParameterBag.php on line 31
in vendor/ParameterBag.php on line 84
$resultis assignedin vendor/Request.php on line 817
$redirectis assignedin src/CRUDlex/Controller.php on line 557
$server['HTTP_HOST']seems to return tainted data, and$serveris assigned in Request.php on line 380$server['HTTP_HOST']seems to return tainted data, and$serveris assignedin vendor/Request.php on line 380
$serveris assignedin vendor/Request.php on line 428
$serveris assignedin vendor/Request.php on line 429
$serveris passed to Request::createRequestFromFactory()in vendor/Request.php on line 431
$serveris passed to Request::__construct()in vendor/Request.php on line 2068
$serveris passed to Request::initialize()in vendor/Request.php on line 255
$serveris passed to ParameterBag::__construct()in vendor/Request.php on line 278
in vendor/ParameterBag.php on line 31
in vendor/ParameterBag.php on line 84
$resultis assignedin vendor/Request.php on line 817
$redirectis assignedin src/CRUDlex/Controller.php on line 557
$this->parameters['PHP_AUTH_USER']seems to return tainted data, and$headersis assigned in ServerBag.php on line 43$this->parameters['PHP_AUTH_USER']seems to return tainted data, and$headersis assignedin vendor/ServerBag.php on line 43
$headersis assignedin vendor/ServerBag.php on line 44
$this->server->getHeaders()is passed to HeaderBag::__construct()in vendor/Request.php on line 279
$valuesis assignedin vendor/HeaderBag.php on line 29
$valuesis passed to HeaderBag::set()in vendor/HeaderBag.php on line 30
$valuesis passed through array_values(), and$valuesis assignedin vendor/HeaderBag.php on line 142
in vendor/HeaderBag.php on line 145
in vendor/HeaderBag.php on line 65
$headersis assignedin vendor/HeaderBag.php on line 113
$requestUriis assignedin vendor/Request.php on line 1831
$requestUriis passed to ParameterBag::set()in vendor/Request.php on line 1862
in vendor/ParameterBag.php on line 95
in vendor/ParameterBag.php on line 84
$resultis assignedin vendor/Request.php on line 817
$redirectis assignedin src/CRUDlex/Controller.php on line 557
$this->parameters['PHP_AUTH_PW']seems to return tainted data, and$headersis assigned in ServerBag.php on line 44$this->parameters['PHP_AUTH_PW']seems to return tainted data, and$headersis assignedin vendor/ServerBag.php on line 44
$this->server->getHeaders()is passed to HeaderBag::__construct()in vendor/Request.php on line 279
$valuesis assignedin vendor/HeaderBag.php on line 29
$valuesis passed to HeaderBag::set()in vendor/HeaderBag.php on line 30
$valuesis passed through array_values(), and$valuesis assignedin vendor/HeaderBag.php on line 142
in vendor/HeaderBag.php on line 145
in vendor/HeaderBag.php on line 65
$headersis assignedin vendor/HeaderBag.php on line 113
$requestUriis assignedin vendor/Request.php on line 1831
$requestUriis passed to ParameterBag::set()in vendor/Request.php on line 1862
in vendor/ParameterBag.php on line 95
in vendor/ParameterBag.php on line 84
$resultis assignedin vendor/Request.php on line 817
$redirectis assignedin src/CRUDlex/Controller.php on line 557
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 92
in vendor/Response.php on line 391
in vendor/Response.php on line 350
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:
if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) { throw new \InvalidArgumentException('This input is not allowed.'); }For numeric data, we recommend to explicitly cast the data: