Issues (27)

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.

modules/admin/helpers/FileRulesTextHelper.php (1 issue)

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 app\modules\admin\helpers;
4
5
use Yii;
6
use yii\helpers\ArrayHelper;
7
8
/**
9
 * Converting file validation rules to text
10
 */
11
class FileRulesTextHelper
12
{
13
    /**
14
     * @var array Validation rules
15
     */
16
    private $rules;
17
    /**
18
     * @var array Validation rules of image
19
     */
20
    private $imageSize;
21
    /**
22
     * @var string Delimiter for rules
23
     */
24
    private $delimiter = '<br>';
25
26
    /**
27
     * @param array $rules Validation rules
28
     * @param string $delimiter Delimiter for rules. Default `<br>` 
29
     */
30
    public function __construct(array $rules, $delimiter = null)
31
    {
32
        $this->rules = $rules;
33
        $this->imageSize = ArrayHelper::getValue($rules, 'imageSize', []);
0 ignored issues
show
Documentation Bug introduced by
It seems like \yii\helpers\ArrayHelper..., 'imageSize', array()) of type * is incompatible with the declared type array of property $imageSize.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
34
35
        if ($delimiter !== null) {
36
            $this->delimiter = $delimiter;
37
        }
38
    }
39
40
    /**
41
     * Get a description of the validation rules in as text
42
     *
43
     * @return string
44
     */
45
    public function __toString()
46
    {
47
        $text = [];
48
        
49
        $text[] = $this->imageSizeDescription();
50
        $text[] = $this->extensionDescription();
51
        $text[] = $this->maxSizeDescription();
52
        $text[] = $this->maxFilesDescription();
53
54
        return implode($this->delimiter, array_filter($text));
55
    }
56
57
    /**
58
     * Get description for rule the `maximum size`
59
     * 
60
     * @return string|null
61
     */
62
    public function maxSizeDescription(): ?string
63
    {
64
        $rules = ArrayHelper::getValue($this->rules, 'maxSize');
65
        if ($rules === null) {
66
            return null;
67
        }
68
69
        return Yii::t('app', 'Max. file size') . ': ' . Yii::$app->formatter->asShortSize($rules) . ' ';
70
    }
71
72
    /**
73
     * Get description for rule the `maximum count of files`
74
     * 
75
     * @return string|null
76
     */
77
    public function maxFilesDescription(): ?string
78
    {
79
        $rules = ArrayHelper::getValue($this->rules, 'maxFiles');
80
        if ($rules === null) {
81
            return null;
82
        }
83
84
        return Yii::t('app', 'Max. file number') . ': ' . $rules . ' ';
85
    }
86
87
    /**
88
     * Get description for rule the `allowed extensions`
89
     * 
90
     * @return string|null
91
     */
92
    public function extensionDescription(): ?string
93
    {
94
        $rules = ArrayHelper::getValue($this->rules, 'extensions');
95
        if ($rules === null) {
96
            return null;
97
        }
98
99
        return Yii::t('app', 'File types') . ': ' . strtoupper(implode(', ', $rules)) . ' ';
100
    }
101
102
    /**
103
     * Get description for rule the `image sizes`
104
     * 
105
     * @return string|null
106
     */
107
    public function imageSizeDescription(): ?string
108
    {
109
        $text = [];
110
        switch ($this->imageSize) {
111
            case $this->isImageStrictSize():
112
                $text[] = $this->imageStrictSizeDescription();
113
                break;
114
            case $this->isImageMinAndMaxSize():
115
                $text[] = $this->imageMinSizeDescription();
116
                $text[] = $this->imageMaxSizeDescription();
117
                break;
118
            case $this->isImageMinSize():
119
                $text[] = $this->imageMinSizeDescription();
120
                $text = array_merge($text, $this->imageRules(['minWidth', 'minHeight']));
121
                break;
122
            case $this->isImageMaxSize():
123
                $text[] = $this->imageMaxSizeDescription();
124
                $text = array_merge($text, $this->imageRules(['maxWidth', 'maxHeight']));
125
                break;
126
            default:
127
                $text = $this->imageRules();
128
                break;
129
        }
130
131
        if (count($text)) {
132
            return implode($this->delimiter, array_filter($text));
133
        }
134
135
        return null;
136
    }
137
138
    /**
139
     * Whether the image has strict size
140
     * 
141
     * @return bool 
142
     */
143
    public function isImageStrictSize(): bool
144
    {
145
        $maxWidth  = ArrayHelper::getValue($this->imageSize, 'maxWidth');
146
        $minWidth  = ArrayHelper::getValue($this->imageSize, 'minWidth');
147
        $maxHeight = ArrayHelper::getValue($this->imageSize, 'maxHeight');
148
        $minHeight = ArrayHelper::getValue($this->imageSize, 'minHeight');
149
150
        return count($this->imageSize) == 4 && ($maxWidth == $minWidth && $maxHeight == $minHeight);
151
    }
152
153
    /**
154
     * Get description for image with strict size
155
     * 
156
     * @return string
157
     */
158
    public function imageStrictSizeDescription(): string
159
    {
160
        return
161
            Yii::t('app', 'Image size') . ': ' . 
162
            ArrayHelper::getValue($this->imageSize, 'maxWidth') . 'x' . 
163
            ArrayHelper::getValue($this->imageSize, 'maxHeight') . 'px';
164
    }
165
166
    /**
167
     * Whether the image has max and min size
168
     * 
169
     * @return bool 
170
     */
171
    public function isImageMinAndMaxSize(): bool
172
    {
173
        return count($this->imageSize) == 4;
174
    }
175
176
    /**
177
     * Whether the image has min size
178
     * 
179
     * @return bool 
180
     */
181
    public function isImageMinSize(): bool
182
    {
183
        $minWidth  = ArrayHelper::getValue($this->imageSize, 'minWidth');
184
        $minHeight = ArrayHelper::getValue($this->imageSize, 'minHeight');
185
186
        return (count($this->imageSize) == 2 || count($this->imageSize) == 3) && $minWidth && $minHeight;
187
    }
188
189
    /**
190
     * Get description for image with min size
191
     * 
192
     * @return string
193
     */
194
    public function imageMinSizeDescription(): string
195
    {
196
        return
197
            Yii::t('app', 'Min. size of image') . ': ' . 
198
            ArrayHelper::getValue($this->imageSize, 'minWidth') . 'x' . 
199
            ArrayHelper::getValue($this->imageSize, 'minHeight') . 'px';
200
    }
201
202
    /**
203
     * Whether the image has max size
204
     * 
205
     * @return bool 
206
     */
207
    public function isImageMaxSize(): bool
208
    {
209
        $maxWidth  = ArrayHelper::getValue($this->imageSize, 'maxWidth');
210
        $maxHeight = ArrayHelper::getValue($this->imageSize, 'maxHeight');
211
212
        return (count($this->imageSize) == 2 || count($this->imageSize) == 3) && $maxWidth && $maxHeight;
213
    }
214
215
    /**
216
     * Get description for image with max size
217
     * 
218
     * @return string
219
     */
220
    public function imageMaxSizeDescription(): string
221
    {
222
        return
223
            Yii::t('app', 'Max. size of image') . ': ' . 
224
            ArrayHelper::getValue($this->imageSize, 'maxWidth') . 'x' . 
225
            ArrayHelper::getValue($this->imageSize, 'maxHeight') . 'px';
226
    }
227
228
    private function imageRules(array $exclude = []): array
229
    {
230
        $rules = $this->imageSize;
231
        foreach ($exclude as $item) {
232
            unset($rules[$item]);
233
        }
234
235
        $text = [];
236
        foreach ($rules as $rule => $value) {
237
            switch ($rule) {
238
                case 'minWidth':
239
                    $text[] = Yii::t('app', 'Min. width') . ' ' . $value . 'px';
240
                    break;
241
                case 'minHeight':
242
                    $text[] = Yii::t('app', 'Min. height') . ' ' . $value . 'px';
243
                    break;
244
                case 'maxWidth':
245
                    $text[] = Yii::t('app', 'Max. width') . ' ' . $value . 'px';
246
                    break;
247
                case 'maxHeight':
248
                    $text[] = Yii::t('app', 'Max. height') . ' ' . $value . 'px';
249
                    break;
250
            }
251
        }
252
253
        return $text;
254
    }
255
}
256