Passed
Push — master ( f79a4b...8eafb5 )
by Thierry
02:11
created

Validator::validateTempFileName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * Validator.php - Jaxon request data validator
5
 *
6
 * Validate requests data before the are passed into the library.
7
 *
8
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2022 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
14
15
namespace Jaxon\Request;
16
17
/*
18
 * See the following links to get explanations about the regexp.
19
 * http://php.net/manual/en/language.oop5.basic.php
20
 * http://stackoverflow.com/questions/3195614/validate-class-method-names-with-regex
21
 * http://www.w3schools.com/charsets/ref_html_utf8.asp
22
 * http://www.w3schools.com/charsets/ref_utf_latin1_supplement.asp
23
 */
24
25
use Jaxon\Utils\Config\Config;
26
use Jaxon\Utils\Translation\Translator;
27
28
use function preg_match;
29
use function is_array;
30
use function in_array;
31
32
class Validator
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Validator
Loading history...
33
{
34
    /**
35
     * The translator
36
     *
37
     * @var Translator
38
     */
39
    protected $xTranslator;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
40
41
    /**
42
     * The config manager
43
     *
44
     * @var Config
45
     */
46
    protected $xConfig;
47
48
    /**
49
     * The last error message
50
     *
51
     * @var string
52
     */
53
    protected $sErrorMessage;
54
55
    public function __construct(Translator $xTranslator, Config $xConfig)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
56
    {
57
        // Set the translator
58
        $this->xTranslator = $xTranslator;
59
        // Set the config manager
60
        $this->xConfig = $xConfig;
61
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
62
63
    /**
64
     * Get the last error message
65
     *
66
     * @return string
67
     */
68
    public function getErrorMessage(): string
69
    {
70
        return $this->sErrorMessage;
71
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
72
73
    /**
74
     * Validate a function name
75
     *
76
     * @param string $sName    The function name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
77
     *
78
     * @return bool
79
     */
80
    public function validateFunction(string $sName): bool
81
    {
82
        $this->sErrorMessage = '';
83
        return (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $sName) > 0);
84
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
85
86
    /**
87
     * Validate a class name
88
     *
89
     * @param string $sName    The class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
90
     *
91
     * @return bool
92
     */
93
    public function validateClass(string $sName): bool
94
    {
95
        $this->sErrorMessage = '';
96
        return (preg_match('/^([a-zA-Z][a-zA-Z0-9_]*)(\.[a-zA-Z][a-zA-Z0-9_]*)*$/', $sName) > 0);
97
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
98
99
    /**
100
     * Validate a method name
101
     *
102
     * @param string $sName    The function name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
103
     *
104
     * @return bool
105
     */
106
    public function validateMethod(string $sName): bool
107
    {
108
        $this->sErrorMessage = '';
109
        // return (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $sName) > 0);
110
        return (preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $sName) > 0);
111
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
112
113
    /**
114
     * Validate a temp file name
115
     *
116
     * @param string $sName    The temp file name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
117
     *
118
     * @return bool
119
     */
120
    public function validateTempFileName(string $sName): bool
121
    {
122
        $this->sErrorMessage = '';
123
        return (preg_match('/^[a-zA-Z0-9_\x7f-\xff]*$/', $sName) > 0);
124
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
125
126
    /**
127
     * Validate a property of an uploaded file
128
     *
129
     * @param string $sName    The uploaded file variable name
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
130
     * @param string $sValue    The value of the property
131
     * @param string $sProperty    The property name in config options
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
132
     * @param string $sField    The field name in file data
133
     *
134
     * @return bool
135
     */
136
    private function validateFileProperty(string $sName, string $sValue, string $sProperty, string $sField): bool
137
    {
138
        $xDefault = $this->xConfig->getOption('upload.default.' . $sProperty);
139
        $aAllowed = $this->xConfig->getOption('upload.files.' . $sName . '.' . $sProperty, $xDefault);
140
        if(is_array($aAllowed) && !in_array($sValue, $aAllowed))
141
        {
142
            $this->sErrorMessage = $this->xTranslator->trans('errors.upload.' . $sField, [$sField => $sValue]);
143
            return false;
144
        }
145
        return true;
146
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
147
148
    /**
149
     * Validate the size of an uploaded file
150
     *
151
     * @param string $sName    The uploaded file variable name
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
152
     * @param int $nFileSize    The uploaded file size
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
153
     * @param string $sProperty    The property name in config options
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
154
     *
155
     * @return bool
156
     */
157
    private function validateFileSize(string $sName, int $nFileSize, string $sProperty): bool
158
    {
159
        $xDefault = $this->xConfig->getOption('upload.default.' . $sProperty, 0);
160
        $nSize = $this->xConfig->getOption('upload.files.' . $sName . '.' . $sProperty, $xDefault);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
161
        if($nSize > 0 && (
162
            ($sProperty == 'max-size' && $nFileSize > $nSize) ||
163
            ($sProperty == 'min-size' && $nFileSize < $nSize)))
164
        {
165
            $this->sErrorMessage = $this->xTranslator->trans('errors.upload.' . $sProperty, ['size' => $nFileSize]);
166
            return false;
167
        }
168
        return true;
169
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
170
171
    /**
172
     * Validate an uploaded file
173
     *
174
     * @param string $sName    The uploaded file variable name
0 ignored issues
show
Coding Style introduced by
Expected 9 spaces after parameter name; 4 found
Loading history...
175
     * @param array $aUploadedFile    The file data received in the $_FILES array
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
176
     *
177
     * @return bool
178
     */
179
    public function validateUploadedFile(string $sName, array $aUploadedFile): bool
180
    {
181
        $this->sErrorMessage = '';
182
        // Verify the file extension
183
        if(!$this->validateFileProperty($sName, $aUploadedFile['type'], 'types', 'type'))
184
        {
185
            return false;
186
        }
187
188
        // Verify the file extension
189
        if(!$this->validateFileProperty($sName, $aUploadedFile['extension'], 'extensions', 'extension'))
190
        {
191
            return false;
192
        }
193
194
        // Verify the max size
195
        if(!$this->validateFileSize($sName, $aUploadedFile['size'], 'max-size'))
196
        {
197
            return false;
198
        }
199
200
        // Verify the min size
201
        if(!$this->validateFileSize($sName, $aUploadedFile['size'], 'min-size'))
202
        {
203
            return false;
204
        }
205
206
        return true;
207
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
208
}
209