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/Generator/ImageGenerator.php (1 issue)

Severity

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\Generator;
4
5
use League\Glide\Filesystem\FilesystemException;
6
use League\Glide\Server;
7
use MediaMonks\SonataMediaBundle\ErrorHandlerTrait;
8
use MediaMonks\SonataMediaBundle\ParameterBag\ImageParameterBag;
9
use MediaMonks\SonataMediaBundle\Model\MediaInterface;
10
11
class ImageGenerator
12
{
13
    use ErrorHandlerTrait;
14
15
    /**
16
     * @var Server
17
     */
18
    private $server;
19
20
    /**
21
     * @var FilenameGeneratorInterface
22
     */
23
    private $filenameGenerator;
24
25
    /**
26
     * @var array
27
     */
28
    private $defaultImageParameters;
29
30
    /**
31
     * @var string
32
     */
33
    private $tmpPath;
34
35
    /**
36
     * @var string
37
     */
38
    private $tmpPrefix;
39
40
    /**
41
     * @var string
42
     */
43
    private $fallbackImage;
44
45
    /**
46
     * @param Server $server
47
     * @param FilenameGeneratorInterface $filenameGenerator
48
     * @param array $defaultImageParameters
49
     * @param null $fallbackImage
50
     * @param null $tmpPath
51
     * @param null $tmpPrefix
52
     */
53 8
    public function __construct(
54
        Server $server,
55
        FilenameGeneratorInterface $filenameGenerator,
56
        $defaultImageParameters = [],
57
        $fallbackImage = null,
58
        $tmpPath = null,
59
        $tmpPrefix = null
60
    ) {
61 8
        $this->server = $server;
62 8
        $this->filenameGenerator = $filenameGenerator;
63 8
        $this->defaultImageParameters = $defaultImageParameters;
64 8
        $this->fallbackImage = $fallbackImage;
65 8
        $this->tmpPath = $tmpPath;
66 8
        $this->tmpPrefix = $tmpPrefix;
67 8
    }
68
69
    /**
70
     * @param MediaInterface $media
71
     * @param ImageParameterBag $parameterBag
72
     * @return string
73
     * @throws FilesystemException
74
     */
75 7
    public function generate(MediaInterface $media, ImageParameterBag $parameterBag): string
76
    {
77 7
        $parameterBag->setDefaults($this->defaultImageParameters);
78 7
        if (!$parameterBag->hasExtra('fit')) {
79 7
            $parameterBag->addExtra('fit', 'crop-'.$media->getFocalPoint());
80
        }
81 7
        if (!$parameterBag->hasExtra('fm') && isset($media->getProviderMetaData()['originalExtension'])) {
82 4
            $parameterBag->addExtra('fm', $media->getProviderMetaData()['originalExtension']);
83
        }
84
85 7
        $filename = $this->filenameGenerator->generate($media, $parameterBag);
86
87 7
        if (!$this->server->getCache()->has($filename)) {
88 6
            $this->generateImage($media, $parameterBag, $filename);
89
        }
90
91 5
        return $filename;
92
    }
93
94
    /**
95
     * @param MediaInterface $media
96
     * @param ImageParameterBag $parameterBag
97
     * @param $filename
98
     *
99
     * @throws FilesystemException
100
     * @throws \Exception
101
     */
102 6
    protected function generateImage(MediaInterface $media, ImageParameterBag $parameterBag, string $filename)
103
    {
104 6
        $tmp = $this->getTemporaryFile();
105 6
        $imageData = $this->getImageData($media);
106
107 6
        $this->disableErrorHandler();
108 6
        if (file_put_contents($tmp, $imageData) === false) {
109
            $this->restoreErrorHandler();
110
            throw new FilesystemException('Unable to write temporary file');
111
        }
112 6
        $this->restoreErrorHandler();
113
114
        try {
115 6
            $this->server->getCache()->put($filename, $this->doGenerateImage($media, $tmp, $parameterBag));
116 2
        } catch (\Exception $e) {
117 2
            throw new FilesystemException('Could not generate image', 0, $e);
118 4
        } finally {
119 6
            if (file_exists($tmp)) {
120 6
                if (!@unlink($tmp)) {
121 6
                    throw new FilesystemException('Unable to clean up temporary file');
122
                }
123
            }
124
        }
125 4
    }
126
127
    /**
128
     * @param MediaInterface $media
129
     * @return string
130
     * @throws FilesystemException
131
     */
132 6
    protected function getImageData(MediaInterface $media): string
133
    {
134 6
        if ($this->server->getSource()->has($media->getImage())) {
135 6
            return $this->server->getSource()->read($media->getImage());
136
        }
137
138
        if (!is_null($this->fallbackImage)) {
139
            return file_get_contents($this->fallbackImage);
140
        }
141
142
        throw new FilesystemException('File not found');
143
    }
144
145
    /**
146
     * @param MediaInterface $media
147
     * @param string $tmp
148
     * @param ImageParameterBag $parameterBag
149
     * @return string
150
     */
151 6
    protected function doGenerateImage(MediaInterface $media, $tmp, ImageParameterBag $parameterBag): string
0 ignored issues
show
The parameter $media is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
152
    {
153 6
        $parameters = $parameterBag->getExtra();
154 6
        $parameters['w'] = $parameterBag->getWidth();
155 6
        $parameters['h'] = $parameterBag->getHeight();
156
157 6
        return $this->server->getApi()->run($tmp, $parameters);
158
    }
159
160
    /**
161
     * @return string
162
     */
163 6
    protected function getTemporaryFile(): string
164
    {
165 6
        if (empty($this->tmpPath)) {
166 5
            $this->tmpPath = sys_get_temp_dir();
167
        }
168 6
        if (empty($this->tmpPrefix)) {
169 6
            $this->tmpPrefix = 'media';
170
        }
171
172 6
        $this->disableErrorHandler();
173 6
        $tempFile = tempnam($this->tmpPath, $this->tmpPrefix);
174 6
        $this->restoreErrorHandler();
175
176 6
        return $tempFile;
177
    }
178
179
    /**
180
     * @return Server
181
     */
182 1
    public function getServer(): Server
183
    {
184 1
        return $this->server;
185
    }
186
187
    /**
188
     * @return FilenameGeneratorInterface
189
     */
190 1
    public function getFilenameGenerator(): FilenameGeneratorInterface
191
    {
192 1
        return $this->filenameGenerator;
193
    }
194
195
    /**
196
     * @return array
197
     */
198 1
    public function getDefaultImageParameters(): array
199
    {
200 1
        return $this->defaultImageParameters;
201
    }
202
203
    /**
204
     * @return string
205
     */
206 1
    public function getTmpPath(): string
207
    {
208 1
        return $this->tmpPath;
209
    }
210
211
    /**
212
     * @return string
213
     */
214 1
    public function getTmpPrefix(): string
215
    {
216 1
        return $this->tmpPrefix;
217
    }
218
219
    /**
220
     * @return string
221
     */
222 1
    public function getFallbackImage(): string
223
    {
224 1
        return $this->fallbackImage;
225
    }
226
}
227