• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 8677009716

13 Apr 2024 11:45PM UTC coverage: 84.44% (-2.2%) from 86.607%
8677009716

push

github

web-flow
Merge pull request #8776 from kenjis/fix-findQualifiedNameFromPath-Cannot-declare-class-v3

fix: Cannot declare class CodeIgniter\Config\Services, because the name is already in use

0 of 3 new or added lines in 1 file covered. (0.0%)

478 existing lines in 72 files now uncovered.

20318 of 24062 relevant lines covered (84.44%)

188.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.7
/system/Validation/FileRules.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Validation;
15

16
use CodeIgniter\HTTP\CLIRequest;
17
use CodeIgniter\HTTP\IncomingRequest;
18
use CodeIgniter\HTTP\RequestInterface;
19
use Config\Mimes;
20
use InvalidArgumentException;
21

22
/**
23
 * File validation rules
24
 *
25
 * @see \CodeIgniter\Validation\FileRulesTest
26
 */
27
class FileRules
28
{
29
    /**
30
     * Request instance. So we can get access to the files.
31
     *
32
     * @var IncomingRequest
33
     */
34
    protected $request;
35

36
    /**
37
     * Constructor.
38
     */
39
    public function __construct(?RequestInterface $request = null)
40
    {
41
        if ($request === null) {
1,714✔
42
            $request = service('request');
1,714✔
43
        }
44

45
        assert($request instanceof IncomingRequest || $request instanceof CLIRequest);
46

47
        $this->request = $request;
1,714✔
48
    }
49

50
    /**
51
     * Verifies that $name is the name of a valid uploaded file.
52
     */
53
    public function uploaded(?string $blank, string $name): bool
54
    {
55
        if (! ($files = $this->request->getFileMultiple($name))) {
8✔
56
            $files = [$this->request->getFile($name)];
4✔
57
        }
58

59
        foreach ($files as $file) {
8✔
60
            if ($file === null) {
8✔
61
                return false;
2✔
62
            }
63

64
            if (ENVIRONMENT === 'testing') {
6✔
65
                if ($file->getError() !== 0) {
6✔
66
                    return false;
2✔
67
                }
68
            } else {
69
                // Note: cannot unit test this; no way to over-ride ENVIRONMENT?
70
                // @codeCoverageIgnoreStart
UNCOV
71
                if (! $file->isValid()) {
×
UNCOV
72
                    return false;
×
73
                }
74
                // @codeCoverageIgnoreEnd
75
            }
76
        }
77

78
        return true;
4✔
79
    }
80

81
    /**
82
     * Verifies if the file's size in Kilobytes is no larger than the parameter.
83
     */
84
    public function max_size(?string $blank, string $params): bool
85
    {
86
        // Grab the file name off the top of the $params
87
        // after we split it.
88
        $paramArray = explode(',', $params);
14✔
89
        if (count($paramArray) !== 2) {
14✔
90
            throw new InvalidArgumentException('Invalid max_size parameter: "' . $params . '"');
2✔
91
        }
92
        $name = array_shift($paramArray);
12✔
93

94
        if (! ($files = $this->request->getFileMultiple($name))) {
12✔
95
            $files = [$this->request->getFile($name)];
12✔
96
        }
97

98
        foreach ($files as $file) {
12✔
99
            if ($file === null) {
12✔
100
                return false;
2✔
101
            }
102

103
            if ($file->getError() === UPLOAD_ERR_NO_FILE) {
10✔
104
                return true;
×
105
            }
106

107
            if ($file->getError() === UPLOAD_ERR_INI_SIZE) {
10✔
108
                return false;
2✔
109
            }
110

111
            if ($file->getSize() / 1024 > $paramArray[0]) {
8✔
112
                return false;
4✔
113
            }
114
        }
115

116
        return true;
4✔
117
    }
118

119
    /**
120
     * Uses the mime config file to determine if a file is considered an "image",
121
     * which for our purposes basically means that it's a raster image or svg.
122
     */
123
    public function is_image(?string $blank, string $params): bool
124
    {
125
        // Grab the file name off the top of the $params
126
        // after we split it.
127
        $params = explode(',', $params);
6✔
128
        $name   = array_shift($params);
6✔
129

130
        if (! ($files = $this->request->getFileMultiple($name))) {
6✔
131
            $files = [$this->request->getFile($name)];
6✔
132
        }
133

134
        foreach ($files as $file) {
6✔
135
            if ($file === null) {
6✔
136
                return false;
2✔
137
            }
138

139
            if ($file->getError() === UPLOAD_ERR_NO_FILE) {
4✔
140
                return true;
×
141
            }
142

143
            // We know that our mimes list always has the first mime
144
            // start with `image` even when then are multiple accepted types.
145
            $type = Mimes::guessTypeFromExtension($file->getExtension()) ?? '';
4✔
146

147
            if (mb_strpos($type, 'image') !== 0) {
4✔
148
                return false;
2✔
149
            }
150
        }
151

152
        return true;
2✔
153
    }
154

155
    /**
156
     * Checks to see if an uploaded file's mime type matches one in the parameter.
157
     */
158
    public function mime_in(?string $blank, string $params): bool
159
    {
160
        // Grab the file name off the top of the $params
161
        // after we split it.
162
        $params = explode(',', $params);
6✔
163
        $name   = array_shift($params);
6✔
164

165
        if (! ($files = $this->request->getFileMultiple($name))) {
6✔
166
            $files = [$this->request->getFile($name)];
6✔
167
        }
168

169
        foreach ($files as $file) {
6✔
170
            if ($file === null) {
6✔
171
                return false;
2✔
172
            }
173

174
            if ($file->getError() === UPLOAD_ERR_NO_FILE) {
4✔
175
                return true;
×
176
            }
177

178
            if (! in_array($file->getMimeType(), $params, true)) {
4✔
179
                return false;
2✔
180
            }
181
        }
182

183
        return true;
2✔
184
    }
185

186
    /**
187
     * Checks to see if an uploaded file's extension matches one in the parameter.
188
     */
189
    public function ext_in(?string $blank, string $params): bool
190
    {
191
        // Grab the file name off the top of the $params
192
        // after we split it.
193
        $params = explode(',', $params);
6✔
194
        $name   = array_shift($params);
6✔
195

196
        if (! ($files = $this->request->getFileMultiple($name))) {
6✔
197
            $files = [$this->request->getFile($name)];
6✔
198
        }
199

200
        foreach ($files as $file) {
6✔
201
            if ($file === null) {
6✔
202
                return false;
2✔
203
            }
204

205
            if ($file->getError() === UPLOAD_ERR_NO_FILE) {
4✔
206
                return true;
×
207
            }
208

209
            if (! in_array($file->guessExtension(), $params, true)) {
4✔
210
                return false;
2✔
211
            }
212
        }
213

214
        return true;
2✔
215
    }
216

217
    /**
218
     * Checks an uploaded file to verify that the dimensions are within
219
     * a specified allowable dimension.
220
     */
221
    public function max_dims(?string $blank, string $params): bool
222
    {
223
        // Grab the file name off the top of the $params
224
        // after we split it.
225
        $params = explode(',', $params);
6✔
226
        $name   = array_shift($params);
6✔
227

228
        if (! ($files = $this->request->getFileMultiple($name))) {
6✔
229
            $files = [$this->request->getFile($name)];
6✔
230
        }
231

232
        foreach ($files as $file) {
6✔
233
            if ($file === null) {
6✔
234
                return false;
2✔
235
            }
236

237
            if ($file->getError() === UPLOAD_ERR_NO_FILE) {
4✔
238
                return true;
×
239
            }
240

241
            // Get Parameter sizes
242
            $allowedWidth  = $params[0] ?? 0;
4✔
243
            $allowedHeight = $params[1] ?? 0;
4✔
244

245
            // Get uploaded image size
246
            $info = getimagesize($file->getTempName());
4✔
247

248
            if ($info === false) {
4✔
249
                // Cannot get the image size.
250
                return false;
×
251
            }
252

253
            $fileWidth  = $info[0];
4✔
254
            $fileHeight = $info[1];
4✔
255

256
            if ($fileWidth > $allowedWidth || $fileHeight > $allowedHeight) {
4✔
257
                return false;
2✔
258
            }
259
        }
260

261
        return true;
2✔
262
    }
263
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc