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/FileManipulator.php (3 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 Spatie\MediaLibrary;
4
5
use Illuminate\Contracts\Bus\Dispatcher;
6
use Illuminate\Support\Facades\File;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Spatie\MediaLibrary\File.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use Illuminate\Support\Str;
8
use Spatie\MediaLibrary\Conversion\Conversion;
9
use Spatie\MediaLibrary\Conversion\ConversionCollection;
10
use Spatie\MediaLibrary\Events\ConversionHasBeenCompleted;
11
use Spatie\MediaLibrary\Events\ConversionWillStart;
12
use Spatie\MediaLibrary\Filesystem\Filesystem;
13
use Spatie\MediaLibrary\Helpers\File as MediaLibraryFileHelper;
14
use Spatie\MediaLibrary\Helpers\ImageFactory;
15
use Spatie\MediaLibrary\Helpers\TemporaryDirectory;
16
use Spatie\MediaLibrary\ImageGenerators\ImageGenerator;
17
use Spatie\MediaLibrary\Jobs\PerformConversions;
18
use Spatie\MediaLibrary\Models\Media;
19
use Spatie\MediaLibrary\ResponsiveImages\ResponsiveImageGenerator;
20
use Storage;
21
22
class FileManipulator
23
{
24
    /**
25
     * Create all derived files for the given media.
26
     *
27
     * @param \Spatie\MediaLibrary\Models\Media $media
28
     * @param array $only
29
     * @param bool $onlyMissing
30
     */
31
    public function createDerivedFiles(Media $media, array $only = [], bool $onlyMissing = false)
32
    {
33
        $profileCollection = ConversionCollection::createForMedia($media);
34
35
        if (! empty($only)) {
36
            $profileCollection = $profileCollection->filter(function ($collection) use ($only) {
37
                return in_array($collection->getName(), $only);
38
            });
39
        }
40
41
        $this->performConversions(
42
            $profileCollection->getNonQueuedConversions($media->collection_name),
43
            $media,
44
            $onlyMissing
45
        );
46
47
        $queuedConversions = $profileCollection->getQueuedConversions($media->collection_name);
48
49
        if ($queuedConversions->isNotEmpty()) {
50
            $this->dispatchQueuedConversions($media, $queuedConversions, $onlyMissing);
51
        }
52
    }
53
54
    /**
55
     * Perform the given conversions for the given media.
56
     *
57
     * @param \Spatie\MediaLibrary\Conversion\ConversionCollection $conversions
58
     * @param \Spatie\MediaLibrary\Models\Media $media
59
     * @param bool $onlyMissing
60
     */
61
    public function performConversions(ConversionCollection $conversions, Media $media, bool $onlyMissing = false)
62
    {
63
        if ($conversions->isEmpty()) {
64
            return;
65
        }
66
67
        $imageGenerator = $this->determineImageGenerator($media);
68
69
        if (! $imageGenerator) {
70
            return;
71
        }
72
73
        $temporaryDirectory = TemporaryDirectory::create();
74
75
        $copiedOriginalFile = app(Filesystem::class)->copyFromMediaLibrary(
76
            $media,
77
            $temporaryDirectory->path(Str::random(16).'.'.$media->extension)
78
        );
79
80
        $conversions
81
            ->reject(function (Conversion $conversion) use ($onlyMissing, $media) {
82
                $relativePath = $media->getPath($conversion->getName());
83
84
                $rootPath = config('filesystems.disks.'.$media->disk.'.root');
85
86
                if ($rootPath) {
87
                    $relativePath = str_replace($rootPath, '', $relativePath);
88
                }
89
90
                return $onlyMissing && Storage::disk($media->disk)->exists($relativePath);
91
            })
92
            ->each(function (Conversion $conversion) use ($media, $imageGenerator, $copiedOriginalFile) {
93
                event(new ConversionWillStart($media, $conversion, $copiedOriginalFile));
94
95
                $copiedOriginalFile = $imageGenerator->convert($copiedOriginalFile, $conversion);
0 ignored issues
show
Consider using a different name than the imported variable $copiedOriginalFile, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
96
97
                $manipulationResult = $this->performManipulations($media, $conversion, $copiedOriginalFile);
98
99
                $newFileName = $conversion->getConversionFile($media->file_name);
100
101
                $renamedFile = MediaLibraryFileHelper::renameInDirectory($manipulationResult, $newFileName);
102
103
                if ($conversion->shouldGenerateResponsiveImages()) {
104
                    app(ResponsiveImageGenerator::class)->generateResponsiveImagesForConversion(
105
                        $media,
106
                        $conversion,
107
                        $renamedFile
108
                    );
109
                }
110
111
                app(Filesystem::class)->copyToMediaLibrary($renamedFile, $media, 'conversions');
112
113
                $media->markAsConversionGenerated($conversion->getName(), true);
114
115
                event(new ConversionHasBeenCompleted($media, $conversion));
116
            });
117
118
        $temporaryDirectory->delete();
119
    }
120
121
    public function performManipulations(Media $media, Conversion $conversion, string $imageFile): string
122
    {
123
        if ($conversion->getManipulations()->isEmpty()) {
124
            return $imageFile;
125
        }
126
127
        $conversionTempFile = pathinfo($imageFile, PATHINFO_DIRNAME).'/'.Str::random(16)
128
            .$conversion->getName()
129
            .'.'
130
            .$media->extension;
131
132
        File::copy($imageFile, $conversionTempFile);
133
134
        $supportedFormats = ['jpg', 'pjpg', 'png', 'gif'];
135
        if ($conversion->shouldKeepOriginalImageFormat() && in_array($media->extension, $supportedFormats)) {
136
            $conversion->format($media->extension);
0 ignored issues
show
The method format() does not exist on Spatie\MediaLibrary\Conversion\Conversion. Did you maybe mean keepOriginalImageFormat()?

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...
137
        }
138
139
        ImageFactory::load($conversionTempFile)
140
            ->manipulate($conversion->getManipulations())
141
            ->save();
142
143
        return $conversionTempFile;
144
    }
145
146
    protected function dispatchQueuedConversions(Media $media, ConversionCollection $queuedConversions, bool $onlyMissing = false)
147
    {
148
        $performConversionsJobClass = config('medialibrary.jobs.perform_conversions', PerformConversions::class);
149
150
        $job = new $performConversionsJobClass($queuedConversions, $media, $onlyMissing);
151
152
        if ($customQueue = config('medialibrary.queue_name')) {
153
            $job->onQueue($customQueue);
154
        }
155
156
        app(Dispatcher::class)->dispatch($job);
157
    }
158
159
    /**
160
     * @param \Spatie\MediaLibrary\Models\Media $media
161
     *
162
     * @return \Spatie\MediaLibrary\ImageGenerators\ImageGenerator|null
163
     */
164
    public function determineImageGenerator(Media $media)
165
    {
166
        return $media->getImageGenerators()
167
            ->map(function (string $imageGeneratorClassName) {
168
                return app($imageGeneratorClassName);
169
            })
170
            ->first(function (ImageGenerator $imageGenerator) use ($media) {
171
                return $imageGenerator->canConvert($media);
172
            });
173
    }
174
}
175