Skip to content

Commit e713035

Browse files
committed
Improve check for nested uploaded file fields
1 parent 5e0fb54 commit e713035

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release Notes for Snaptcha
22

3+
## 5.1.2 - 2025-07-01
4+
5+
- Improved the check for nested uploaded file fields.
6+
37
## 5.1.1 - 2025-06-23
48

59
- Improved the check for uploaded files.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "putyourlightson/craft-snaptcha",
33
"description": "Automatically validates forms and prevents spam bots from submitting to your site.",
4-
"version": "5.1.1",
4+
"version": "5.1.2",
55
"type": "craft-plugin",
66
"homepage": "https://putyourlightson.com/plugins/snaptcha",
77
"license": "proprietary",

src/services/SnaptchaService.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ public function hasFileUpload(): bool
100100
foreach ($_FILES as $file) {
101101
$error = $file['error'] ?? null;
102102
if (is_array($error)) {
103-
if (in_array(UPLOAD_ERR_OK, $error, true)) {
103+
if ($this->hasUploadedFileRecursive($error)) {
104104
return true;
105105
}
106-
} elseif ($error === UPLOAD_ERR_OK) {
106+
} elseif ($error !== UPLOAD_ERR_NO_FILE) {
107107
return true;
108108
}
109109
}
@@ -366,6 +366,24 @@ private function getNormalizedArray(array|string $values): array
366366
return $values;
367367
}
368368

369+
/**
370+
* Returns whether the request has a file upload by recursively checking.
371+
*/
372+
private function hasUploadedFileRecursive(array $errors): bool
373+
{
374+
foreach ($errors as $error) {
375+
if (is_array($error)) {
376+
if ($this->hasUploadedFileRecursive($error)) {
377+
return true;
378+
}
379+
} elseif ($error !== UPLOAD_ERR_NO_FILE) {
380+
return true;
381+
}
382+
}
383+
384+
return false;
385+
}
386+
369387
/**
370388
* Flattens a multi-dimensional array of values to a flat array that can
371389
* be used to output hidden fields, preserving the keys.

0 commit comments

Comments
 (0)