Validator   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 135
rs 10
c 0
b 0
f 0
wmc 17

6 Methods

Rating   Name   Duplication   Size   Complexity  
A validateFileProperty() 0 10 3
A validateUploadedFile() 0 28 5
A validateTempFileName() 0 4 1
A validateFileSize() 0 12 6
A __construct() 0 6 1
A getErrorMessage() 0 3 1
1
<?php
2
3
/**
4
 * Validator.php
5
 *
6
 * Validate requests data before the are passed into the library.
7
 *
8
 * @package jaxon-upload
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
 */
14
15
namespace Jaxon\Upload\Manager;
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\App\Config\ConfigManager;
26
use Jaxon\App\I18n\Translator;
27
28
use function in_array;
29
use function is_array;
30
use function preg_match;
31
32
class Validator
33
{
34
    /**
35
     * The config manager
36
     *
37
     * @var ConfigManager
38
     */
39
    protected $xConfigManager;
40
41
    /**
42
     * The translator
43
     *
44
     * @var Translator
45
     */
46
    protected $xTranslator;
47
48
    /**
49
     * The last error message
50
     *
51
     * @var string
52
     */
53
    protected $sErrorMessage;
54
55
    public function __construct(ConfigManager $xConfigManager, Translator $xTranslator)
56
    {
57
        // Set the config manager
58
        $this->xConfigManager = $xConfigManager;
59
        // Set the translator
60
        $this->xTranslator = $xTranslator;
61
    }
62
63
    /**
64
     * Get the last error message
65
     *
66
     * @return string
67
     */
68
    public function getErrorMessage(): string
69
    {
70
        return $this->sErrorMessage;
71
    }
72
73
    /**
74
     * Validate a temp file name
75
     *
76
     * @param string $sVarName    The temp file name
77
     *
78
     * @return bool
79
     */
80
    public function validateTempFileName(string $sVarName): bool
81
    {
82
        $this->sErrorMessage = '';
83
        return (preg_match('/^[a-zA-Z0-9_\x7f-\xff]*$/', $sVarName) > 0);
84
    }
85
86
    /**
87
     * Validate a property of an uploaded file
88
     *
89
     * @param string $sVarName    The uploaded file variable name
90
     * @param string $sValue    The value of the property
91
     * @param string $sProperty    The property name in config options
92
     * @param string $sField    The field name in file data
93
     *
94
     * @return bool
95
     */
96
    private function validateFileProperty(string $sVarName, string $sValue, string $sProperty, string $sField): bool
97
    {
98
        $xDefault = $this->xConfigManager->getOption('upload.default.' . $sProperty);
99
        $aAllowed = $this->xConfigManager->getOption('upload.files.' . $sVarName . '.' . $sProperty, $xDefault);
100
        if(is_array($aAllowed) && !in_array($sValue, $aAllowed))
101
        {
102
            $this->sErrorMessage = $this->xTranslator->trans('errors.upload.' . $sField, [$sField => $sValue]);
103
            return false;
104
        }
105
        return true;
106
    }
107
108
    /**
109
     * Validate the size of an uploaded file
110
     *
111
     * @param string $sVarName    The uploaded file variable name
112
     * @param int $nFileSize    The uploaded file size
113
     * @param string $sProperty    The property name in config options
114
     *
115
     * @return bool
116
     */
117
    private function validateFileSize(string $sVarName, int $nFileSize, string $sProperty): bool
118
    {
119
        $xDefault = $this->xConfigManager->getOption('upload.default.' . $sProperty, 0);
120
        $nSize = $this->xConfigManager->getOption('upload.files.' . $sVarName . '.' . $sProperty, $xDefault);
121
        if($nSize > 0 && (
122
            ($sProperty == 'max-size' && $nFileSize > $nSize) ||
123
            ($sProperty == 'min-size' && $nFileSize < $nSize)))
124
        {
125
            $this->sErrorMessage = $this->xTranslator->trans('errors.upload.' . $sProperty, ['size' => $nFileSize]);
126
            return false;
127
        }
128
        return true;
129
    }
130
131
    /**
132
     * Validate an uploaded file
133
     *
134
     * @param string $sVarName    The uploaded file variable name
135
     * @param File $xFile    The uploaded file
136
     *
137
     * @return bool
138
     */
139
    public function validateUploadedFile(string $sVarName, File $xFile): bool
140
    {
141
        $this->sErrorMessage = '';
142
        // Verify the file extension
143
        if(!$this->validateFileProperty($sVarName, $xFile->type(), 'types', 'type'))
144
        {
145
            return false;
146
        }
147
148
        // Verify the file extension
149
        if(!$this->validateFileProperty($sVarName, $xFile->extension(), 'extensions', 'extension'))
150
        {
151
            return false;
152
        }
153
154
        // Verify the max size
155
        if(!$this->validateFileSize($sVarName, $xFile->size(), 'max-size'))
156
        {
157
            return false;
158
        }
159
160
        // Verify the min size
161
        if(!$this->validateFileSize($sVarName, $xFile->size(), 'min-size'))
162
        {
163
            return false;
164
        }
165
166
        return true;
167
    }
168
}
169