Issues (49)

Security Analysis    12 potential vulnerabilities

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting (10)
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 (2)
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.

CAsciiArt.php (2 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
 * Create an ASCII version of an image.
4
 *
5
 */
6
class CAsciiArt
7
{
8
    /**
9
     * Character set to use.
10
     */
11
    private $characterSet = array(
12
        'one' => "#0XT|:,.' ",
13
        'two' => "@%#*+=-:. ",
14
        'three' => "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. "
15
    );
16
17
18
19
    /**
20
     * Current character set.
21
     */
22
    private $characters = null;
23
24
25
26
    /**
27
     * Length of current character set.
28
     */
29
    private $charCount = null;
30
31
32
33
    /**
34
     * Scale of the area to swap to a character.
35
     */
36
    private $scale = null;
37
38
39
40
    /**
41
     * Strategy to calculate luminance.
42
     */
43
    private $luminanceStrategy = null;
44
45
46
47
    /**
48
     * Constructor which sets default options.
49
     */
50
    public function __construct()
51
    {
52
        $this->setOptions();
53
    }
54
55
56
57
    /**
58
     * Add a custom character set.
59
     *
60
     * @param string $key   for the character set.
61
     * @param string $value for the character set.
62
     *
63
     * @return $this
64
     */
65
    public function addCharacterSet($key, $value)
66
    {
67
        $this->characterSet[$key] = $value;
68
        return $this;
69
    }
70
71
72
73
    /**
74
     * Set options for processing, defaults are available.
75
     *
76
     * @param array $options to use as default settings.
77
     *
78
     * @return $this
79
     */
80
    public function setOptions($options = array())
81
    {
82
        $default = array(
83
            "characterSet" => 'two',
84
            "scale" => 14,
85
            "luminanceStrategy" => 3,
86
            "customCharacterSet" => null,
87
        );
88
        $default = array_merge($default, $options);
89
        
90
        if (!is_null($default['customCharacterSet'])) {
91
            $this->addCharacterSet('custom', $default['customCharacterSet']);
92
            $default['characterSet'] = 'custom';
93
        }
94
        
95
        $this->scale = $default['scale'];
96
        $this->characters = $this->characterSet[$default['characterSet']];
97
        $this->charCount = strlen($this->characters);
98
        $this->luminanceStrategy = $default['luminanceStrategy'];
99
        
100
        return $this;
101
    }
102
103
104
105
    /**
106
     * Create an Ascii image from an image file.
107
     *
108
     * @param string $filename of the image to use.
109
     *
110
     * @return string $ascii with the ASCII image.
111
     */
112
    public function createFromFile($filename)
113
    {
114
        $img = imagecreatefromstring(file_get_contents($filename));
115
        list($width, $height) = getimagesize($filename);
116
117
        $ascii = null;
118
        $incY = $this->scale;
119
        $incX = $this->scale / 2;
120
        
121
        for ($y = 0; $y < $height - 1; $y += $incY) {
122
            for ($x = 0; $x < $width - 1; $x += $incX) {
123
                $toX = min($x + $this->scale / 2, $width - 1);
124
                $toY = min($y + $this->scale, $height - 1);
125
                $luminance = $this->luminanceAreaAverage($img, $x, $y, $toX, $toY);
0 ignored issues
show
$img is of type resource, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
126
                $ascii .= $this->luminance2character($luminance);
127
            }
128
            $ascii .= PHP_EOL;
129
        }
130
131
        return $ascii;
132
    }
133
134
135
136
    /**
137
     * Get the luminance from a region of an image using average color value.
138
     *
139
     * @param string  $img the image.
140
     * @param integer $x1  the area to get pixels from.
141
     * @param integer $y1  the area to get pixels from.
142
     * @param integer $x2  the area to get pixels from.
143
     * @param integer $y2  the area to get pixels from.
144
     *
145
     * @return integer $luminance with a value between 0 and 100.
146
     */
147
    public function luminanceAreaAverage($img, $x1, $y1, $x2, $y2)
148
    {
149
        $numPixels = ($x2 - $x1 + 1) * ($y2 - $y1 + 1);
150
        $luminance = 0;
151
        
152
        for ($x = $x1; $x <= $x2; $x++) {
153
            for ($y = $y1; $y <= $y2; $y++) {
154
                $rgb   = imagecolorat($img, $x, $y);
155
                $red   = (($rgb >> 16) & 0xFF);
156
                $green = (($rgb >> 8) & 0xFF);
157
                $blue  = ($rgb & 0xFF);
158
                $luminance += $this->getLuminance($red, $green, $blue);
159
            }
160
        }
161
        
162
        return $luminance / $numPixels;
163
    }
164
165
166
167
    /**
168
     * Calculate luminance value with different strategies.
169
     *
170
     * @param integer $red   The color red.
171
     * @param integer $green The color green.
172
     * @param integer $blue  The color blue.
173
     *
174
     * @return float $luminance with a value between 0 and 1.
175
     */
176
    public function getLuminance($red, $green, $blue)
177
    {
178
        switch ($this->luminanceStrategy) {
179
            case 1:
180
                $luminance = ($red * 0.2126 + $green * 0.7152 + $blue * 0.0722) / 255;
181
                break;
182
            case 2:
183
                $luminance = ($red * 0.299 + $green * 0.587 + $blue * 0.114) / 255;
184
                break;
185
            case 3:
186
                $luminance = sqrt(0.299 * pow($red, 2) + 0.587 * pow($green, 2) + 0.114 * pow($blue, 2)) / 255;
187
                break;
188
            case 0:
189
            default:
190
                $luminance = ($red + $green + $blue) / (255 * 3);
191
        }
192
193
        return $luminance;
194
    }
195
196
197
198
    /**
199
     * Translate the luminance value to a character.
200
     *
201
     * @param string $position a value between 0-100 representing the
0 ignored issues
show
There is no parameter named $position. 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...
202
     *                         luminance.
203
     *
204
     * @return string with the ascii character.
205
     */
206
    public function luminance2character($luminance)
207
    {
208
        $position = (int) round($luminance * ($this->charCount - 1));
209
        $char = $this->characters[$position];
210
        return $char;
211
    }
212
}
213