mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
2.x uploadedFile validation (backported from #4524)
This commit is contained in:
parent
8c404ad6a7
commit
af8c992655
2 changed files with 1088 additions and 935 deletions
|
@ -2454,6 +2454,12 @@ class ValidationTest extends CakeTestCase {
|
|||
$this->assertFalse(Validation::uploadError(2));
|
||||
$this->assertFalse(Validation::uploadError(array('error' => 2)));
|
||||
$this->assertFalse(Validation::uploadError(array('error' => '2')));
|
||||
|
||||
$this->assertFalse(Validation::uploadError(UPLOAD_ERR_NO_FILE));
|
||||
$this->assertFalse(Validation::uploadError(UPLOAD_ERR_FORM_SIZE, true));
|
||||
$this->assertFalse(Validation::uploadError(UPLOAD_ERR_INI_SIZE, true));
|
||||
$this->assertFalse(Validation::uploadError(UPLOAD_ERR_NO_TMP_DIR, true));
|
||||
$this->assertTrue(Validation::uploadError(UPLOAD_ERR_NO_FILE, true));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2474,4 +2480,97 @@ class ValidationTest extends CakeTestCase {
|
|||
$this->assertFalse(Validation::fileSize(array('tmp_name' => $image), '>', '1KB'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test uploaded file validation.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testUploadedFileErrorCode() {
|
||||
$this->assertFalse(Validation::uploadedFile('derp'));
|
||||
$invalid = array(
|
||||
'name' => 'testing'
|
||||
);
|
||||
$this->assertFalse(Validation::uploadedFile($invalid));
|
||||
$file = array(
|
||||
'name' => 'cake.power.gif',
|
||||
'tmp_name' => CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot/img/cake.power.gif',
|
||||
'error' => UPLOAD_ERR_OK,
|
||||
'type' => 'image/gif',
|
||||
'size' => 201
|
||||
);
|
||||
$this->assertTrue(Validation::uploadedFile($file));
|
||||
$file['error'] = UPLOAD_ERR_NO_FILE;
|
||||
$this->assertFalse(Validation::uploadedFile($file), 'Error upload should fail.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test uploaded file validation.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testUploadedFileMimeType() {
|
||||
$file = array(
|
||||
'name' => 'cake.power.gif',
|
||||
'tmp_name' => CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot/img/cake.power.gif',
|
||||
'error' => UPLOAD_ERR_OK,
|
||||
'type' => 'text/plain',
|
||||
'size' => 201
|
||||
);
|
||||
$options = array(
|
||||
'types' => array('text/plain')
|
||||
);
|
||||
$this->assertFalse(Validation::uploadedFile($file, $options), 'Incorrect mimetype.');
|
||||
$options = array(
|
||||
'types' => array('image/gif', 'image/png')
|
||||
);
|
||||
$this->assertTrue(Validation::uploadedFile($file, $options));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test uploaded file validation.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testUploadedFileSize() {
|
||||
$file = array(
|
||||
'name' => 'cake.power.gif',
|
||||
'tmp_name' => CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot/img/cake.power.gif',
|
||||
'error' => UPLOAD_ERR_OK,
|
||||
'type' => 'text/plain',
|
||||
'size' => 201
|
||||
);
|
||||
$options = array(
|
||||
'minSize' => 500
|
||||
);
|
||||
$this->assertFalse(Validation::uploadedFile($file, $options), 'Too small');
|
||||
$options = array(
|
||||
'maxSize' => 100
|
||||
);
|
||||
$this->assertFalse(Validation::uploadedFile($file, $options), 'Too big');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test uploaded file validation.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testUploadedFileNoFile() {
|
||||
$file = array(
|
||||
'name' => '',
|
||||
'tmp_name' => CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot/img/cake.power.gif',
|
||||
'error' => UPLOAD_ERR_NO_FILE,
|
||||
'type' => '',
|
||||
'size' => 0
|
||||
);
|
||||
$options = array(
|
||||
'optional' => true,
|
||||
'minSize' => 500,
|
||||
'types' => array('image/gif', 'image/png')
|
||||
);
|
||||
$this->assertTrue(Validation::uploadedFile($file, $options), 'No file should be ok.');
|
||||
$options = array(
|
||||
'optional' => false
|
||||
);
|
||||
$this->assertFalse(Validation::uploadedFile($file, $options), 'File is required.');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -446,13 +446,11 @@ class Validation {
|
|||
|
||||
if ($places === null) {
|
||||
$regex = "/^{$sign}(?:{$lnum}|{$dnum}){$exp}$/";
|
||||
|
||||
} elseif ($places === true) {
|
||||
if (is_float($check) && floor($check) === $check) {
|
||||
$check = sprintf("%.1f", $check);
|
||||
}
|
||||
$regex = "/^{$sign}{$dnum}{$exp}$/";
|
||||
|
||||
} elseif (is_numeric($places)) {
|
||||
$places = '[0-9]{' . $places . '}';
|
||||
$dnum = "(?:[0-9]*[\.]{$places}|{$lnum}[\.]{$places})";
|
||||
|
@ -1022,17 +1020,73 @@ class Validation {
|
|||
* Checking for upload errors
|
||||
*
|
||||
* @param string|array $check Value to check.
|
||||
* @param bool $allowNoFile Set to true to allow UPLOAD_ERR_NO_FILE as a pass.
|
||||
* @return bool
|
||||
* @see http://www.php.net/manual/en/features.file-upload.errors.php
|
||||
*/
|
||||
public static function uploadError($check) {
|
||||
public static function uploadError($check, $allowNoFile = false) {
|
||||
if (is_array($check) && isset($check['error'])) {
|
||||
$check = $check['error'];
|
||||
}
|
||||
|
||||
if ($allowNoFile) {
|
||||
return in_array((int) $check, array(UPLOAD_ERR_OK, UPLOAD_ERR_NO_FILE), true);
|
||||
}
|
||||
return (int) $check === UPLOAD_ERR_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate an uploaded file.
|
||||
*
|
||||
* Helps join `uploadError`, `fileSize` and `mimeType` into
|
||||
* one higher level validation method.
|
||||
*
|
||||
* ### Options
|
||||
*
|
||||
* - `types` - A list of valid mime types. If empty all types
|
||||
* will be accepted. The `type` will not be looked at, instead
|
||||
* the file type will be checked with ext/finfo.
|
||||
* - `minSize` - The minimum file size. Defaults to not checking.
|
||||
* - `maxSize` - The maximum file size. Defaults to not checking.
|
||||
* - `optional` - Whether or not this file is optional. Defaults to false.
|
||||
* If true a missing file will pass the validator regardless of other constraints.
|
||||
*
|
||||
* @param array $file The uploaded file data from PHP.
|
||||
* @param array $options An array of options for the validation.
|
||||
* @return bool
|
||||
*/
|
||||
public static function uploadedFile($file, $options = array()) {
|
||||
$options += array(
|
||||
'minSize' => null,
|
||||
'maxSize' => null,
|
||||
'types' => null,
|
||||
'optional' => false,
|
||||
);
|
||||
if (!is_array($file)) {
|
||||
return false;
|
||||
}
|
||||
$keys = array('name', 'tmp_name', 'error', 'type', 'size');
|
||||
if (array_keys($file) != $keys) {
|
||||
return false;
|
||||
}
|
||||
if (!static::uploadError($file, $options['optional'])) {
|
||||
return false;
|
||||
}
|
||||
if ($options['optional'] && (int) $file['error'] === UPLOAD_ERR_NO_FILE) {
|
||||
return true;
|
||||
}
|
||||
if (isset($options['minSize']) && !static::fileSize($file, '>=', $options['minSize'])) {
|
||||
return false;
|
||||
}
|
||||
if (isset($options['maxSize']) && !static::fileSize($file, '<=', $options['maxSize'])) {
|
||||
return false;
|
||||
}
|
||||
if (isset($options['types']) && !static::mimeType($file, $options['types'])) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lazily populate the IP address patterns used for validations
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue