Completed
Push — master ( ad91dd...df4455 )
by Thierry
01:29
created

Validator::validateUploadedFile()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 2
dl 0
loc 29
rs 9.1448
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Validator.php - Jaxon input data validator
5
 *
6
 * Validate requests data before the are passed into the library.
7
 *
8
 * @package jaxon-core
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2016 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\Utils\Validation;
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
class Validator
25
{
26
    /**
27
     * The translator
28
     *
29
     * @var \Jaxon\Utils\Translation\Translator
30
     */
31
    protected $xTranslator;
32
33
    /**
34
     * The config manager
35
     *
36
     * @var \Jaxon\Utils\Config\Config
37
     */
38
    protected $xConfig;
39
40
    /**
41
     * The last error message
42
     *
43
     * @var string
44
     */
45
    protected $sErrorMessage;
46
47
    public function __construct($xTranslator, $xConfig)
48
    {
49
        // Set the translator
50
        $this->xTranslator = $xTranslator;
51
        // Set the config manager
52
        $this->xConfig = $xConfig;
53
    }
54
55
    /**
56
     * Get the last error message
57
     *
58
     * @return string          The last error message
59
     */
60
    public function getErrorMessage()
61
    {
62
        return $this->sErrorMessage;
63
    }
64
65
    /**
66
     * Validate a function name
67
     *
68
     * @param string        $sName            The function name
69
     *
70
     * @return bool            True if the function name is valid, and false if not
71
     */
72
    public function validateFunction($sName)
73
    {
74
        $this->sErrorMessage = '';
75
        return preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $sName);
76
    }
77
78
    /**
79
     * Validate an event name
80
     *
81
     * @param string        $sName            The event name
82
     *
83
     * @return bool            True if the event name is valid, and false if not
84
     */
85
    public function validateEvent($sName)
86
    {
87
        $this->sErrorMessage = '';
88
        return preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $sName);
89
    }
90
91
    /**
92
     * Validate a class name
93
     *
94
     * @param string        $sName            The class name
95
     *
96
     * @return bool            True if the class name is valid, and false if not
97
     */
98
    public function validateClass($sName)
99
    {
100
        $this->sErrorMessage = '';
101
        return preg_match('/^([a-zA-Z][a-zA-Z0-9_]*)(\.[a-zA-Z][a-zA-Z0-9_]*)*$/', $sName);
102
    }
103
104
    /**
105
     * Validate a method name
106
     *
107
     * @param string        $sName            The function name
108
     *
109
     * @return bool            True if the method name is valid, and false if not
110
     */
111
    public function validateMethod($sName)
112
    {
113
        $this->sErrorMessage = '';
114
        // return preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $sName);
115
        return preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $sName);
116
    }
117
118
    /**
119
     * Validate a property of an uploaded file
120
     *
121
     * @param string        $sName          The uploaded file variable name
122
     * @param string        $sValue         The value of the property
123
     * @param string        $sProperty      The property name in config options
124
     * @param string        $sField         The field name in file data
125
     *
126
     * @return bool            True if the property valid, and false if not
127
     */
128
    private function validateFileProperty($sName, $sValue, $sProperty, $sField)
129
    {
130
        $xDefault = $this->xConfig->getOption('upload.default.' . $sProperty);
131
        $aAllowed = $this->xConfig->getOption('upload.files.' . $sName . '.' . $sProperty, $xDefault);
132
        if(is_array($aAllowed) && !in_array($sValue, $aAllowed))
133
        {
134
            $this->sErrorMessage = $this->xTranslator->trans('errors.upload.' . $sField, $aUploadedFile);
0 ignored issues
show
Bug introduced by
The variable $aUploadedFile does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
135
            return false;
136
        }
137
        return true;
138
    }
139
140
    /**
141
     * Validate the size of an uploaded file
142
     *
143
     * @param string        $sName          The uploaded file variable name
144
     * @param integer       $iFileSize      The uploaded file size
145
     * @param string        $sProperty      The property name in config options
146
     *
147
     * @return bool            True if the property valid, and false if not
148
     */
149
    private function validateFileSize($sName, $iFileSize, $sProperty)
150
    {
151
        $xDefault = $this->xConfig->getOption('upload.default.' . $sProperty, 0);
152
        $iSize = $this->xConfig->getOption('upload.files.' . $sName . '.' . $sProperty, $xDefault);
153
        if($iSize > 0 && (
154
            ($sProperty == 'max-size' && $iFileSize > $iSize) ||
155
            ($sProperty == 'min-size' && $iFileSize < $iSize)))
156
        {
157
            $this->sErrorMessage = $this->xTranslator->trans('errors.upload.' . $sProperty, $aUploadedFile);
0 ignored issues
show
Bug introduced by
The variable $aUploadedFile does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
158
            return false;
159
        }
160
        return true;
161
    }
162
163
    /**
164
     * Validate an uploaded file
165
     *
166
     * @param string        $sName            The uploaded file variable name
167
     * @param array         $aUploadedFile    The file data received in the $_FILES array
168
     *
169
     * @return bool            True if the file data are valid, and false if not
170
     */
171
    public function validateUploadedFile($sName, array $aUploadedFile)
172
    {
173
        $this->sErrorMessage = '';
174
        // Verify the file extension
175
        if(!$this->validateFileProperty($sName, $aUploadedFile['type'], 'types', 'type'))
176
        {
177
            return false;
178
        }
179
180
        // Verify the file extension
181
        if(!$this->validateFileProperty($sName, $aUploadedFile['extension'], 'extensions', 'extension'))
182
        {
183
            return false;
184
        }
185
186
        // Verify the max size
187
        if(!$this->validateFileSize($sName, $aUploadedFile['size'], 'max-size'))
188
        {
189
            return false;
190
        }
191
192
        // Verify the min size
193
        if(!$this->validateFileSize($sName, $aUploadedFile['size'], 'min-size'))
194
        {
195
            return false;
196
        }
197
198
        return true;
199
    }
200
}
201