Issues (116)

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/NwLaravel/FileStorage/ImagineImagick.php (10 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 NwLaravel\FileStorage;
4
5
use Imagick;
6
use ImagickPixel;
7
8
class ImagineImagick implements Imagine
9
{
10
    /**
11
     * @var Imagick
12
     */
13
    protected $image;
14
15
    /**
16
     * Construct
17
     *
18
     * @param string|blob $data
19
     */
20
    public function __construct($data)
21
    {
22
        if ($this->isBinary($data)) {
23
            $pathTmp = tempnam(sys_get_temp_dir(), 'img');
24
            file_put_contents($pathTmp, $data);
25
            $this->image = new Imagick($pathTmp);
26
            @unlink($pathTmp);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
27
        } else {
28
            $this->image = new Imagick($data);
29
        }
30
    }
31
32
    /**
33
     * Determines if current source data is binary data
34
     *
35
     * @return boolean
36
     */
37
    protected function isBinary($data)
38
    {
39
        if (is_string($data)) {
40
            $mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $data);
41
            return (substr($mime, 0, 4) != 'text' && $mime != 'application/x-empty');
42
        }
43
44
        return false;
45
    }
46
47
    /**
48
     * Execute in Canvas
49
     *
50
     * @param \Closure $callback
51
     *
52
     * @return Imagine
53
     */
54
    protected function execute($callback)
55
    {
56
        $format = strtolower($this->image->getImageFormat());
57
58
        if ($format == 'gif') {
59
          $this->image = $this->image->coalesceImages();
60
          do {
61
              $callback($this->image);
62
          } while ($this->image->nextImage());
63
64
          $this->image = $this->image->deconstructImages();
65
        } else {
66
          $callback($this->image);
67
        }
68
69
        return $this;
70
    }
71
72
    /**
73
     * Filesize
74
     *
75
     * @return int
76
     */
77
    public function filesize()
78
    {
79
        return $this->image->getImageLength();
80
    }
81
82
    /**
83
     * Define Resize
84
     *
85
     * @param int     $maxWidth
86
     * @param int     $maxHeight
87
     * @param boolean $force
88
     *
89
     * @return Imagine
90
     */
91
    public function resize($maxWidth, $maxHeight, $force = false)
92
    {
93
        return $this->execute(function ($image) use ($maxWidth, $maxHeight, $force) {
94
            $width = $maxWidth;
95
            $height = $maxHeight;
96
            $imageWidth = $image->getImageWidth();
97
            $imageHeight = $image->getImageHeight();
98
99
            if(($maxWidth && $imageWidth > $maxWidth) || !$maxHeight) {
100
                $height = floor(($imageHeight/$imageWidth)*$maxWidth);
101
                if (!$height) {
102
                    $height = $imageHeight;
103
                }
104
105
            } else if(($maxHeight && $imageHeight > $maxHeight) || !$maxWidth) {
106
                $width = floor(($imageWidth/$imageHeight)*$maxHeight);
107
                if (!$width) {
108
                    $width = $imageWidth;
109
                }
110
            }
111
112
            $image->scaleImage($width, $height, !$force);
113
        });
114
    }
115
116
    /**
117
     * Opacity
118
     *
119
     * @return Imagine
120
     */
121
    public function opacity($opacity)
122
    {
123
        $opacity = intval($opacity);
124
125
        if ($opacity > 0 && $opacity < 100) {
126
            $this->execute(function ($image) use ($opacity) {
127
                $image->setImageOpacity($opacity/100);
128
            });
129
        }
130
131
        return $this;
132
    }
133
134
    /**
135
     * Watermark
136
     *
137
     * @param string  $watermark
138
     * @param string  $position
139
     * @param integer $opacity
140
     *
141
     * @return Imagine
142
     */
143
    public function watermark($watermark, $position = 'center', $opacity = null)
144
    {
145
        if ($this->isImage($watermark)) {
146
            $watermark = new Imagick($watermark);
147
        }
148
149
        if ($watermark instanceof Imagick) {
150
            $opacity = intval($opacity);
151
            if ($opacity > 0 && $opacity < 100) {
152
                $watermark->setImageOpacity($opacity/100);
153
            }
154
155
            $self = $this;
156
157
            $this->execute(function ($image) use ($watermark, $position, $self) {
158
                $self->watermarkCanvas($image, $watermark, $position);
159
            });
160
        }
161
162
        return $this;
163
    }
164
165
    protected function watermarkCanvas(Imagick $image, Imagick $watermark, $position = 'center')
166
    {
167
        // how big are the images?
168
        $iWidth = $image->getImageWidth();
169
        $iHeight = $image->getImageHeight();
170
        $wWidth = $watermark->getImageWidth();
171
        $wHeight = $watermark->getImageHeight();
172
173
        if ($iHeight < $wHeight || $iWidth < $wWidth) {
174
          // resize the watermark
175
          $watermark->scaleImage($iWidth, $iHeight, true);
176
177
          // get new size
178
          $wWidth = $watermark->getImageWidth();
179
          $wHeight = $watermark->getImageHeight();
180
        }
181
182
        $xOffset = 0;
0 ignored issues
show
$xOffset is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
183
        $yOffset = 0;
0 ignored issues
show
$yOffset is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
184
185
        switch ($position) {
186
          case 'center':
187
          default:
188
            $x = ($iWidth - $wWidth) / 2;
189
            $y = ($iHeight - $wHeight) / 2;
190
            break;
191
          case 'topLeft':
0 ignored issues
show
case 'topLeft': $x =... = $yOffset; break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
192
            $x = $xOffset;
193
            $y = $yOffset;
194
            break;
195
          case 'top':
196
          case 'topCenter':
197
            $x = ($iWidth - $wWidth) / 2;
198
            $y = $yOffset;
199
            break;
200
          case 'topRight':
201
            $x = $iWidth - $wWidth - $xOffset;
202
            $y = $yOffset;
203
            break;
204
          case 'right':
205
          case 'rightCenter':
206
            $x = $iWidth - $wWidth - $xOffset;
207
            $y = ($iHeight - $wHeight) / 2;
208
            break;
209
          case 'bottomRight':
210
            $x = $iWidth - $wWidth - $xOffset;
211
            $y = $iHeight - $wHeight - $yOffset;
212
            break;
213
          case 'bottom':
214
          case 'bottomCenter':
215
            $x = ($iWidth - $wWidth) / 2;
216
            $y = $iHeight - $wHeight - $yOffset;
217
            break;
218
          case 'bottomLeft':
219
            $x = $xOffset;
220
            $y = $iHeight - $wHeight - $yOffset;
221
            break;
222
          case 'left':
223
          case 'leftCenter':
224
            $x = $xOffset;
225
            $y = ($iHeight - $wHeight) / 2;
226
            break;
227
        }
228
229
        $image->compositeImage($watermark, Imagick::COMPOSITE_OVER, $x, $y);
230
    }
231
232
    /**
233
     * Is Image
234
     *
235
     * @param string $path
236
     *
237
     * @return boolean
238
     */
239
    protected function isImage($path)
240
    {
241
        return (bool) ($path && is_file($path) && strpos(mime_content_type($path), 'image/')===0);
242
    }
243
244
    /**
245
     * Crop
246
     *
247
     * @param integer $width
248
     * @param integer $height
249
     * @param integer $x
250
     * @param integer $y
251
     *
252
     * @return binary
253
     */
254
    public function crop($width, $height, $x, $y)
255
    {
256
        return $this->execute(function ($image) use ($width, $height, $x, $y) {
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->execute(fu..., $height, $x, $y); }); (NwLaravel\FileStorage\Imagine) is incompatible with the return type declared by the interface NwLaravel\FileStorage\Imagine::crop of type NwLaravel\FileStorage\binary.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
257
            $image->cropImage($width, $height, $x, $y);
258
        });
259
    }
260
261
    /**
262
     * Rotate Image
263
     *
264
     * @param integer $angle
265
     *
266
     * @return binary
267
     */
268
    public function rotate($angle)
269
    {
270
        $angle = intval($angle);
271
272
        if ($angle > -360 && $angle < 360) {
273
            $this->execute(function ($image) use ($angle) {
274
                $image->rotateImage('#ffffff', $angle);
275
            });
276
        }
277
278
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (NwLaravel\FileStorage\ImagineImagick) is incompatible with the return type declared by the interface NwLaravel\FileStorage\Imagine::rotate of type NwLaravel\FileStorage\binary.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
279
    }
280
281
    /**
282
     * Strip Profiles
283
     *
284
     * @param string $except
0 ignored issues
show
There is no parameter named $except. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
285
     *
286
     * @return this
287
     */
288
    public function stripProfiles()
289
    {
290
        $profiles = $this->image->getImageProfiles('icc', true);
291
292
        $this->image->stripImage();
293
294
        if(!empty($profiles))
295
            $this->image->profileImage('icc', $profiles['icc']);
296
297
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (NwLaravel\FileStorage\ImagineImagick) is incompatible with the return type declared by the interface NwLaravel\FileStorage\Imagine::stripProfiles of type NwLaravel\FileStorage\this.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
298
    }
299
300
    /**
301
     * Encode
302
     *
303
     * @param string  $format
304
     * @param integer $quality
305
     *
306
     * @return binary
307
     */
308
    public function encode($format = null, $quality = null)
309
    {
310
        return $this->image->getImagesBlob();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->image->getImagesBlob(); (string) is incompatible with the return type declared by the interface NwLaravel\FileStorage\Imagine::encode of type NwLaravel\FileStorage\binary.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
311
    }
312
313
    /**
314
     * Save
315
     *
316
     * @param string  $path
317
     * @param integer $quality
318
     *
319
     * @return binary
320
     */
321
    public function save($path, $quality = null)
322
    {
323
        $quality = intval($quality);
324
        if ($quality > 0 && $quality <= 100) {
325
            $this->image->setImageCompressionQuality($quality);
326
        }
327
        $this->image->writeImage($path);
328
329
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (NwLaravel\FileStorage\ImagineImagick) is incompatible with the return type declared by the interface NwLaravel\FileStorage\Imagine::save of type NwLaravel\FileStorage\binary.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
330
    }
331
}
332