Issues (74)

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/ResponsiveImages/ResponsiveImageGenerator.php (1 issue)

Labels
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 Spatie\MediaLibrary\ResponsiveImages;
4
5
use Illuminate\Support\Str;
6
use Spatie\MediaLibrary\Conversion\Conversion;
7
use Spatie\MediaLibrary\Events\ResponsiveImagesGenerated;
8
use Spatie\MediaLibrary\Filesystem\Filesystem;
9
use Spatie\MediaLibrary\Helpers\File;
10
use Spatie\MediaLibrary\Helpers\ImageFactory;
11
use Spatie\MediaLibrary\Helpers\TemporaryDirectory;
12
use Spatie\MediaLibrary\Models\Media;
13
use Spatie\MediaLibrary\ResponsiveImages\Exceptions\InvalidTinyJpg;
14
use Spatie\MediaLibrary\ResponsiveImages\TinyPlaceholderGenerator\TinyPlaceholderGenerator;
15
use Spatie\MediaLibrary\ResponsiveImages\WidthCalculator\WidthCalculator;
16
use Spatie\TemporaryDirectory\TemporaryDirectory as BaseTemporaryDirectory;
17
18
class ResponsiveImageGenerator
19
{
20
    /** \Spatie\MediaLibrary\Filesystem\Filesystem */
21
    protected $filesystem;
22
23
    /** \Spatie\MediaLibrary\ResponsiveImages\WidthCalculator\WidthCalculator */
24
    protected $widthCalculator;
25
26
    /** \Spatie\MediaLibrary\ResponsiveImages\TinyPlaceHolderGenerator\TinyPlaceHolderGenerator */
27
    protected $tinyPlaceholderGenerator;
28
29
    public function __construct(
30
        Filesystem $filesystem,
31
        WidthCalculator $widthCalculator,
32
        TinyPlaceholderGenerator $tinyPlaceholderGenerator
33
    ) {
34
        $this->filesystem = $filesystem;
35
36
        $this->widthCalculator = $widthCalculator;
37
38
        $this->tinyPlaceholderGenerator = $tinyPlaceholderGenerator;
39
    }
40
41
    public function generateResponsiveImages(Media $media)
42
    {
43
        $temporaryDirectory = TemporaryDirectory::create();
44
45
        $baseImage = app(Filesystem::class)->copyFromMediaLibrary(
46
            $media,
47
            $temporaryDirectory->path(Str::random(16).'.'.$media->extension)
48
        );
49
50
        $media = $this->cleanResponsiveImages($media);
51
52
        foreach ($this->widthCalculator->calculateWidthsFromFile($baseImage) as $width) {
53
            $this->generateResponsiveImage($media, $baseImage, 'medialibrary_original', $width, $temporaryDirectory);
54
        }
55
56
        event(new ResponsiveImagesGenerated($media));
57
58
        $this->generateTinyJpg($media, $baseImage, 'medialibrary_original', $temporaryDirectory);
59
60
        $temporaryDirectory->delete();
61
    }
62
63
    public function generateResponsiveImagesForConversion(Media $media, Conversion $conversion, string $baseImage)
64
    {
65
        $temporaryDirectory = TemporaryDirectory::create();
66
67
        $media = $this->cleanResponsiveImages($media, $conversion->getName());
68
69
        foreach ($this->widthCalculator->calculateWidthsFromFile($baseImage) as $width) {
70
            $this->generateResponsiveImage($media, $baseImage, $conversion->getName(), $width, $temporaryDirectory);
71
        }
72
73
        $this->generateTinyJpg($media, $baseImage, $conversion->getName(), $temporaryDirectory);
74
75
        $temporaryDirectory->delete();
76
    }
77
78
    public function generateResponsiveImage(
79
        Media $media,
80
        string $baseImage,
81
        string $conversionName,
82
        int $targetWidth,
83
        BaseTemporaryDirectory $temporaryDirectory
84
    ) {
85
        $responsiveImagePath = $this->appendToFileName($media->file_name, "___{$conversionName}_{$targetWidth}");
86
87
        $tempDestination = $temporaryDirectory->path($responsiveImagePath);
88
89
        ImageFactory::load($baseImage)
0 ignored issues
show
The method optimize() does not exist on Spatie\Image\Image. Did you maybe mean shouldOptimize()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
90
            ->optimize()
91
            ->width($targetWidth)
92
            ->save($tempDestination);
93
94
        $responsiveImageHeight = ImageFactory::load($tempDestination)->getHeight();
95
96
        $finalImageFileName = $this->appendToFileName($responsiveImagePath, "_{$responsiveImageHeight}");
97
98
        $finalResponsiveImagePath = $temporaryDirectory->path($finalImageFileName);
99
100
        rename($tempDestination, $finalResponsiveImagePath);
101
102
        $this->filesystem->copyToMediaLibrary($finalResponsiveImagePath, $media, 'responsiveImages');
103
104
        ResponsiveImage::register($media, $finalImageFileName, $conversionName);
105
    }
106
107
    public function generateTinyJpg(Media $media, string $originalImagePath, string $conversionName, BaseTemporaryDirectory $temporaryDirectory)
108
    {
109
        $tempDestination = $temporaryDirectory->path('tiny.jpg');
110
111
        $this->tinyPlaceholderGenerator->generateTinyPlaceholder($originalImagePath, $tempDestination);
112
113
        $this->guardAgainstInvalidTinyPlaceHolder($tempDestination);
114
115
        $tinyImageDataBase64 = base64_encode(file_get_contents($tempDestination));
116
117
        $tinyImageBase64 = 'data:image/jpeg;base64,'.$tinyImageDataBase64;
118
119
        $originalImage = ImageFactory::load($originalImagePath);
120
121
        $originalImageWidth = $originalImage->getWidth();
122
123
        $originalImageHeight = $originalImage->getHeight();
124
125
        $svg = view('medialibrary::placeholderSvg', compact(
126
            'originalImageWidth',
127
            'originalImageHeight',
128
            'tinyImageBase64'
129
        ));
130
131
        $base64Svg = 'data:image/svg+xml;base64,'.base64_encode($svg);
132
133
        ResponsiveImage::registerTinySvg($media, $base64Svg, $conversionName);
134
    }
135
136
    protected function appendToFileName(string $filePath, string $suffix): string
137
    {
138
        $baseName = pathinfo($filePath, PATHINFO_FILENAME);
139
140
        $extension = pathinfo($filePath, PATHINFO_EXTENSION);
141
142
        return $baseName.$suffix.'.'.$extension;
143
    }
144
145
    protected function guardAgainstInvalidTinyPlaceHolder(string $tinyPlaceholderPath)
146
    {
147
        if (! file_exists($tinyPlaceholderPath)) {
148
            throw InvalidTinyJpg::doesNotExist($tinyPlaceholderPath);
149
        }
150
151
        $mimeType = File::getMimetype($tinyPlaceholderPath);
152
153
        if ($mimeType !== 'image/jpeg') {
154
            throw InvalidTinyJpg::hasWrongMimeType($tinyPlaceholderPath);
155
        }
156
    }
157
158
    protected function cleanResponsiveImages(Media $media, string $conversionName = 'medialibrary_original'): Media
159
    {
160
        $responsiveImages = $media->responsive_images;
161
        $responsiveImages[$conversionName]['urls'] = [];
162
        $media->responsive_images = $responsiveImages;
163
164
        $this->filesystem->removeResponsiveImages($media, $conversionName);
165
166
        return $media;
167
    }
168
}
169