Issues (12)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Provider/AbstractProvider.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Provider;
4
5
use League\Flysystem\FilesystemInterface;
6
use League\Glide\Filesystem\FilesystemException;
7
use MediaMonks\SonataMediaBundle\Client\HttpClientInterface;
8
use MediaMonks\SonataMediaBundle\ErrorHandlerTrait;
9
use MediaMonks\SonataMediaBundle\Model\AbstractMedia;
10
use MediaMonks\SonataMediaBundle\Form\Type\MediaFocalPointType;
11
use Sonata\AdminBundle\Form\FormMapper;
12
use Sonata\CoreBundle\Validator\ErrorElement;
13
use Symfony\Component\Form\Extension\Core\Type\FileType;
14
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
15
use Symfony\Component\Form\Extension\Core\Type\TextType;
16
use Symfony\Component\HttpFoundation\File\UploadedFile;
17
use Symfony\Component\HttpKernel\Config\FileLocator;
18
use Symfony\Component\Translation\TranslatorInterface;
19
use Symfony\Component\Validator\Constraints as Constraint;
20
21
abstract class AbstractProvider implements ProviderInterface
22
{
23
    use ErrorHandlerTrait;
24
25
    const SUPPORT_EMBED = 'embed';
26
    const SUPPORT_IMAGE = 'image';
27
    const SUPPORT_DOWNLOAD = 'download';
28
29
    const TYPE_AUDIO = 'audio';
30
    const TYPE_IMAGE = 'image';
31
    const TYPE_FILE = 'file';
32
    const TYPE_VIDEO = 'video';
33
34
    /**
35
     * @var FilesystemInterface
36
     */
37
    protected $filesystem;
38
39
    /**
40
     * @var TranslatorInterface
41
     */
42
    private $translator;
43
44
    /**
45
     * @var array
46
     */
47
    private $imageConstraintOptions = [];
48
49
    /**
50
     * @var HttpClientInterface
51
     */
52
    private $httpClient;
53
54
    /**
55
     * @var FileLocator
56
     */
57
    private $fileLocator;
58
59
    /**
60
     * @var AbstractMedia
61
     */
62
    private $media;
63
64
    /**
65
     * @param FilesystemInterface $filesystem
66
     */
67 14
    public function setFilesystem(FilesystemInterface $filesystem)
68
    {
69 14
        $this->filesystem = $filesystem;
70 14
    }
71
72
    /**
73
     * @param TranslatorInterface $translator
74
     */
75 13
    public function setTranslator(TranslatorInterface $translator)
76
    {
77 13
        $this->translator = $translator;
78 13
    }
79
80
    /**
81
     * @return TranslatorInterface
82
     */
83 1
    public function getTranslator(): TranslatorInterface
84
    {
85 1
        return $this->translator;
86
    }
87
88
    /**
89
     * @param array $options
90
     */
91 13
    public function setImageConstraintOptions(array $options)
92
    {
93 13
        $this->imageConstraintOptions = $options;
94 13
    }
95
96
    /**
97
     * @param HttpClientInterface $httpClient
98
     */
99 15
    public function setHttpClient(HttpClientInterface $httpClient)
100
    {
101 15
        $this->httpClient = $httpClient;
102 15
    }
103
104
    /**
105
     * @return FileLocator
106
     */
107 2
    public function getFileLocator(): FileLocator
108
    {
109 2
        return $this->fileLocator;
110
    }
111
112
    /**
113
     * @param FileLocator $fileLocator
114
     */
115 13
    public function setFileLocator(FileLocator $fileLocator)
116
    {
117 13
        $this->fileLocator = $fileLocator;
118 13
    }
119
120
    /**
121
     * @return HttpClientInterface
122
     */
123 6
    public function getHttpClient(): HttpClientInterface
124
    {
125 6
        return $this->httpClient;
126
    }
127
128
    /**
129
     * @return FilesystemInterface
130
     */
131 8
    public function getFilesystem(): FilesystemInterface
132
    {
133 8
        return $this->filesystem;
134
    }
135
136
    /**
137
     * @param AbstractMedia $media
138
     * @return AbstractProvider
139
     */
140 11
    public function setMedia($media)
141
    {
142 11
        $this->media = $media;
143
144 11
        return $this;
145
    }
146
147
    /**
148
     * @param AbstractMedia $media
149
     * @param $providerReferenceUpdated
150
     */
151 7
    public function update(AbstractMedia $media, $providerReferenceUpdated)
152
    {
153 7
        $this->updateImage($media);
154 7
    }
155
156
    /**
157
     * @param FormMapper $formMapper
158
     */
159 11
    public function buildCreateForm(FormMapper $formMapper)
160
    {
161
        $formMapper
162 11
            ->add('provider', HiddenType::class);
163
164 11
        $this->buildProviderCreateForm($formMapper);
165 11
    }
166
167
    /**
168
     * @param FormMapper $formMapper
169
     */
170 7
    public function buildEditForm(FormMapper $formMapper)
171
    {
172
        $formMapper
173 7
            ->tab('general')
174 7
            ->add('provider', HiddenType::class);
175
176 7
        $this->buildProviderEditFormBefore($formMapper);
177
178 7
        $formMapper->add(
179 7
            'imageContent',
180 7
            FileType::class,
181
            [
182 7
                'required'    => false,
183
                'constraints' => [
184 7
                    new Constraint\File(),
185
                ],
186 7
                'label'       => 'form.replacement_image',
187
            ]
188
        )
189 7
            ->add('title', TextType::class, ['label' => 'form.title'])
190 7
            ->add(
191 7
                'description',
192 7
                TextType::class,
193 7
                ['label' => 'form.description', 'required' => false]
194
            )
195 7
            ->add(
196 7
                'authorName',
197 7
                TextType::class,
198 7
                ['label' => 'form.authorName', 'required' => false]
199
            )
200 7
            ->add(
201 7
                'copyright',
202 7
                TextType::class,
203 7
                ['label' => 'form.copyright', 'required' => false]
204
            )
205 7
            ->end()
206 7
            ->end()
207 7
            ->tab('image')
208 7
            ->add('focalPoint', MediaFocalPointType::class, [
209 7
                'media' => $this->media
210
            ])
211 7
            ->end()
212 7
            ->end()
213
        ;
214
215 7
        $this->buildProviderEditFormAfter($formMapper);
216 7
    }
217
218
    /**
219
     * @param FormMapper $formMapper
220
     * @codeCoverageIgnore
221
     */
222
    public function buildProviderEditFormBefore(FormMapper $formMapper)
223
    {
224
    }
225
226
    /**
227
     * @param FormMapper $formMapper
228
     * @codeCoverageIgnore
229
     */
230
    public function buildProviderEditFormAfter(FormMapper $formMapper)
231
    {
232
    }
233
234
    /**
235
     * @param AbstractMedia $media
236
     * @param bool $useAsImage
237
     * @return void|string
238
     */
239 4
    protected function handleFileUpload(AbstractMedia $media, $useAsImage = false)
240
    {
241
        /**
242
         * @var UploadedFile $file
243
         */
244 4
        $file = $media->getBinaryContent();
245
246 4
        if (empty($file)) {
247
            return;
248
        }
249
250 4
        $filename = $this->getFilenameByFile($file);
251 4
        $this->writeToFilesystem($file, $filename);
252
253 4
        $media->setProviderMetadata(
254 4
            array_merge(
255 4
                $media->getProviderMetaData(),
256 4
                $this->getFileMetaData($file)
257
            )
258
        );
259
260 4
        if (empty($media->getImage()) && $useAsImage) {
261 2
            $media->setImage($filename);
262 2
            $media->setImageMetaData($media->getProviderMetaData());
263
        }
264
265 4
        if (empty($media->getTitle())) {
266 4
            $media->setTitle(
267 4
                str_replace(
268 4
                    '_',
269 4
                    ' ',
270 4
                    pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)
271
                )
272
            );
273
        }
274
275 4
        return $filename;
276
    }
277
278
    /**
279
     * @param UploadedFile $file
280
     * @return array
281
     */
282 4
    protected function getFileMetaData(UploadedFile $file): array
283
    {
284
        $fileData = [
285 4
            'originalName'      => $file->getClientOriginalName(),
286 4
            'originalExtension' => $file->getClientOriginalExtension(),
287 4
            'mimeType'          => $file->getClientMimeType(),
288 4
            'size'              => $file->getSize(),
289
        ];
290
291 4
        $this->disableErrorHandler();
292 4
        $imageSize = getimagesize($file->getRealPath());
293 4
        if (is_array($imageSize)) {
294 4
            if (is_int($imageSize[0]) && is_int($imageSize[1])) {
295 4
                $fileData['width']  = $imageSize[0];
296 4
                $fileData['height'] = $imageSize[1];
297
            }
298 4
            if (isset($imageSize['bits'])) {
299 4
                $fileData['bits'] = $imageSize['bits'];
300
            }
301 4
            if (isset($imageSize['channels'])) {
302 2
                $fileData['channels'] = $imageSize['channels'];
303
            }
304
        }
305 4
        $this->restoreErrorHandler();
306
307 4
        return $fileData;
308
    }
309
310
    /**
311
     * @param AbstractMedia $media
312
     */
313 7
    public function updateImage(AbstractMedia $media)
314
    {
315
        /**
316
         * @var UploadedFile $file
317
         */
318 7
        $file = $media->getImageContent();
319 7
        if (empty($file)) {
320 5
            return;
321
        }
322
323 2
        $filename = $this->getFilenameByFile($file);
324 2
        $this->writeToFilesystem($file, $filename);
325
326 2
        $media->setImage($filename);
327 2
        $media->setImageMetaData($this->getFileMetaData($file));
328 2
    }
329
330
    /**
331
     * @param \Sonata\AdminBundle\Form\FormMapper $formMapper
332
     * @param $name
333
     * @param $label
334
     * @param $required
335
     * @param array $constraints
336
     */
337 7
    protected function doAddFileField(
338
        FormMapper $formMapper,
339
        $name,
340
        $label,
341
        $required,
342
        $constraints = []
343
    ) {
344 7
        if ($required) {
345 7
            $constraints = array_merge(
346
                [
347 7
                    new Constraint\NotBlank(),
348 7
                    new Constraint\NotNull(),
349
                ],
350 7
                $constraints
351
            );
352
        }
353
354
        $formMapper
355 7
            ->add(
356 7
                $name,
357 7
                FileType::class,
358
                [
359 7
                    'multiple'    => false,
360
                    'data_class'  => null,
361 7
                    'constraints' => $constraints,
362 7
                    'label'       => $label,
363 7
                    'required'    => $required,
364
                ]
365
            );
366 7
    }
367
368
    /**
369
     * @param FormMapper $formMapper
370
     * @param string $name
371
     * @param string $label
372
     * @param array $options
373
     */
374 2 View Code Duplication
    public function addImageField(
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
375
        FormMapper $formMapper,
376
        $name,
377
        $label,
378
        $options = []
379
    ) {
380 2
        $this->doAddFileField(
381 2
            $formMapper,
382 2
            $name,
383 2
            $label,
384 2
            false,
385
            [
386 2
                new Constraint\Image(
387 2
                    array_merge($this->imageConstraintOptions, $options)
388
                ),
389
            ]
390
        );
391 2
    }
392
393
    /**
394
     * @param FormMapper $formMapper
395
     * @param $name
396
     * @param $label
397
     * @param array $options
398
     */
399 3 View Code Duplication
    public function addRequiredImageField(
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
400
        FormMapper $formMapper,
401
        $name,
402
        $label,
403
        $options = []
404
    ) {
405 3
        $this->doAddFileField(
406 3
            $formMapper,
407 3
            $name,
408 3
            $label,
409 3
            true,
410
            [
411 3
                new Constraint\Image(
412 3
                    array_merge($this->imageConstraintOptions, $options)
413
                ),
414
            ]
415
        );
416 3
    }
417
418
    /**
419
     * @param UploadedFile $file
420
     * @return string
421
     */
422 4
    protected function getFilenameByFile(UploadedFile $file): string
423
    {
424 4
        return sprintf(
425 4
            '%s_%d.%s',
426 4
            sha1($file->getClientOriginalName()),
427 4
            time(),
428 4
            $file->getClientOriginalExtension()
429
        );
430
    }
431
432
    /**
433
     * @param UploadedFile $file
434
     * @param $filename
435
     * @throws FilesystemException
436
     */
437 5
    protected function writeToFilesystem(UploadedFile $file, string $filename)
438
    {
439 5
        $this->disableErrorHandler();
440 5
        $stream = fopen($file->getRealPath(), 'r+');
441 5
        $written = $this->getFilesystem()->writeStream($filename, $stream);
442 5
        fclose($stream); // this sometime messes up
443 5
        $this->restoreErrorHandler();
444
445 5
        if (!$written) {
446 1
            throw new FilesystemException('Could not write to file system');
447
        }
448 4
    }
449
450
    /**
451
     * @param ErrorElement $errorElement
452
     * @param AbstractMedia $media
453
     */
454 7
    public function validate(ErrorElement $errorElement, AbstractMedia $media)
455
    {
456 7
    }
457
458
    /**
459
     * @return string
460
     */
461 3
    public function getEmbedTemplate(): string
462
    {
463 3
        return sprintf(
464 3
            '@MediaMonksSonataMedia/Provider/%s_embed.html.twig',
465 3
            $this->getName()
466
        );
467
    }
468
}
469