Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for composite file extension validation #18137

Merged
merged 19 commits into from Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Expand Up @@ -25,6 +25,7 @@ Yii Framework 2 Change Log
- Enh #15202: Add optional param `--silent-exit-on-exception` in `yii\console\Controller` (egorrishe)
- Bug #18110: Add quotes to return value of viewName in MSSQL schema. It is `[someView]` now (darkdef)
- Bug #18134: Instance of `ExpressionInterface` will not be quoted in `Connection:quoteColumnName` (darkdef)
- Bug #18094: Support for composite file extension validation (darkdef)


2.0.35 May 02, 2020
Expand Down
14 changes: 11 additions & 3 deletions framework/assets/yii.validation.js
Expand Up @@ -408,10 +408,18 @@ yii.validation = (function ($) {

function validateFile(file, messages, options) {
if (options.extensions && options.extensions.length > 0) {
var index = file.name.lastIndexOf('.');
var ext = !~index ? '' : file.name.substr(index + 1, file.name.length).toLowerCase();
var found = false;
var filename = file.name.toLowerCase();

if (!~options.extensions.indexOf(ext)) {
for (var index=0; index < options.extensions.length; index++) {
var ext = options.extensions[index].toLowerCase();
samdark marked this conversation as resolved.
Show resolved Hide resolved
if (filename.substr(filename.length - options.extensions[index].length) === ext) {
found = true;
break;
}
}

if (!found) {
messages.push(options.wrongExtension.replace(/\{file\}/g, file.name));
}
}
Expand Down
7 changes: 6 additions & 1 deletion framework/validators/FileValidator.php
Expand Up @@ -11,6 +11,7 @@
use yii\helpers\FileHelper;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\StringHelper;
use yii\web\JsExpression;
use yii\web\UploadedFile;

Expand Down Expand Up @@ -412,7 +413,11 @@ protected function validateExtension($file)
}
}

if (!in_array($extension, $this->extensions, true)) {
if (is_array($this->extensions) and count($this->extensions)) {
foreach ($this->extensions as $ext) {
if (StringHelper::endsWith($file->name, $ext, false))
return true;
}
darkdef marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

Expand Down