Validator   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
c 0
b 0
f 0
dl 0
loc 122
rs 10
wmc 16

5 Methods

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