Issues (1)

Security Analysis    no request data  

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/Providers/FakerImageGenerationProvider.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 Swatty007\FakerImageGenerator\Providers;
4
5
use Faker\Generator;
6
use Faker\Provider\Base;
7
8
class FakerImageGenerationProvider extends Base
9
{
10
    public string $directory;
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
11
    public bool $fullPath;
12
13
    public int $width;
14
    public int $height;
15
    public string $format;
16
17
    public $backgroundColor;
18
19
    public $text;
20
    public $textColor;
21
    public $fontFace;
22
23
    public function __construct(
24
        Generator $generator,
25
        $directory = null,
26
        $width = 640,
27
        $height = 480,
28
        $format = 'png',
29
        $fullPath = true,
30
        $text = true,
31
        $backgroundColor = null,
32
        $textColor = null,
33
        $fontFace = null
34
    ) {
35
        // Allow our users to define a configuration for our entire instance.
36
        // Or fallback to our default config values
37
        $this->directory = $directory ?? config('faker-image-generator.directory');
38
        $this->width = $width ?? config('faker-image-generator.width');
39
        $this->height = $height ?? config('faker-image-generator.height');
40
        $this->format = $format ?? config('faker-image-generator.format');
41
        $this->fullPath = $fullPath ?? config('faker-image-generator.full_path');
42
        $this->text = $text ?? config('faker-image-generator.text');
43
        $this->backgroundColor = $backgroundColor ?? config('faker-image-generator.background_color');
44
        $this->textColor = $textColor ?? config('faker-image-generator.text_color');
45
        $this->fontFace = $fontFace ?? config('faker-image-generator.font_face');
46
47
        parent::__construct($generator);
48
    }
49
50
    /**
51
     * Generate a new image to disk and return its location
52
     *
53
     * Requires gd (default in most PHP setup).
54
     *
55
     * @param null $directory
56
     * @param null $width
57
     * @param null $height
58
     * @param null $format
59
     * @param null $fullPath
60
     * @param null $text
61
     * @param null $backgroundColor
62
     * @param null $textColor
63
     * @param null $fontFace
64
     * @return false|\RuntimeException|string
65
     * @example '/path/to/dir/13b73edae8443990be1aa8f1a483bc27.jpg'
66
     */
67
    public function imageGenerator(
68
        $directory = null,
69
        $width = null,
70
        $height = null,
71
        $format = null,
72
        $fullPath = null,
73
        $text = null,
74
        $backgroundColor = null,
75
        $textColor = null,
76
        $fontFace = null
77
    ) {
78
        // Allow our users to overwrite our settings per item, if they desire so
79
        $directory = $directory ?? $this->directory;
80
        $width = $width ?? $this->width;
81
        $height = $height ?? $this->height;
82
        $format = $format ?? $this->format;
83
        $fullPath = $fullPath ?? $this->fullPath;
84
        $text = $text ?? $this->text;
85
        $backgroundColor = $backgroundColor ?? $this->backgroundColor;
86
        $textColor = $textColor ?? $this->textColor;
87
        $fontFace = $fontFace ?? $this->fontFace;
88
89
        // Validate directory path
90
        if (! is_dir($directory) || ! is_writable($directory)) {
91
            throw new \InvalidArgumentException(sprintf('Cannot write to directory "%s"', $directory));
92
        }
93
94
        // Generate a random filename. Use the server address so that a file
95
        // generated at the same time on a different server won't have a collision.
96
        $name = md5(uniqid(empty($_SERVER['SERVER_ADDR']) ? '' : $_SERVER['SERVER_ADDR'], true));
97
        $filename = $name . '.' . $format;
98
        $filepath = $directory . DIRECTORY_SEPARATOR . $filename;
99
100
        // Create our Image, if our driver exists
101
        if (function_exists('imagecreate')) {
102
            $image = imagecreate($width, $height);
103
104
            if ($backgroundColor) {
105
                if (substr($backgroundColor, 0, 1) == '#') {
106
                    $rgb = str_split(substr($backgroundColor, 1), 2);
107
                } else {
108
                    $rgb = str_split($backgroundColor, 2);
109
                }
110
                imagecolorallocate($image, hexdec($rgb[0]), hexdec($rgb[1]), hexdec($rgb[2]));
111
            } else {
112
                imagecolorallocate($image, 0, 0, 0);
113
            }
114
115
            if ($text === true) {
116
                $text = $width . 'x' . $height;
117
            }
118
119
            if (! is_null($text)) {
120
                if ($textColor) {
121
                    if (substr($textColor, 0, 1) == '#') {
122
                        $rgb = str_split(substr($textColor, 1), 2);
123
                    } else {
124
                        $rgb = str_split($textColor, 2);
125
                    }
126
                    $text_color = imagecolorallocate($image, hexdec($rgb[0]), hexdec($rgb[1]), hexdec($rgb[2]));
127
                } else {
128
                    $text_color = imagecolorallocate($image, 255, 255, 255);
129
                }
130
131
                $fontSize = 200;
132
                $textBoundingBox = imagettfbbox($fontSize, 0, $fontFace, $text);
133
                // decrease the default fonts size until it fits nicely within the image - Code adapted from https://github.com/img-src/placeholder
134
                while (((($width - ($textBoundingBox[2] - $textBoundingBox[0])) < 10) || (($height - ($textBoundingBox[1] - $textBoundingBox[7])) < 10)) && ($fontSize > 1)) {
135
                    $fontSize --;
136
                    $textBoundingBox = imagettfbbox($fontSize, 0, $fontFace, $text);
137
                }
138
                imagettftext($image, $fontSize, 0, ($width / 2) - (($textBoundingBox[2] - $textBoundingBox[0]) / 2), ($height / 2) + (($textBoundingBox[1] - $textBoundingBox[7]) / 2), $text_color, $fontFace, $text);
139
            }
140
141
            switch (strtolower($format)) {
142
                case 'jpg':
143
                case 'jpeg':
144
                default:
145
                    $success = imagejpeg($image, $filepath);
146
                    break;
147
                case 'png':
148
                    $success = imagepng($image, $filepath);
149
            }
150
151
            $success = imagedestroy($image);
152
        } else {
153
            // @codeCoverageIgnoreStart
154
            return new \RuntimeException('GD is not available on this PHP installation. Impossible to generate image.');
155
            // @codeCoverageIgnoreEnd
156
        }
157
158
        if (! $success) {
159
            // @codeCoverageIgnoreStart
160
            // could not save the file - fail silently.
161
            return false;
162
            // @codeCoverageIgnoreEnd
163
        }
164
165
        return $fullPath ? $filepath : $filename;
166
    }
167
}
168