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

Add file_size (mb) validator #272

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
3f20f02
Create de.php
thowen Oct 31, 2017
50969aa
Merge pull request #217 from thowen/patch-1
filisko Nov 1, 2017
a1b7361
Merge pull request #213 from absherzad/master
filisko Nov 1, 2017
51c51fc
FIX #218
filisko Nov 1, 2017
06ba42e
Update es.php
SaphiLC Nov 24, 2017
577dbac
Merge pull request #220 from SaphiLC/patch-1
filisko Nov 24, 2017
a98fed7
Fixed : Credit Card Validation not working if all alphabets are enter…
cjrupak Dec 27, 2017
c1d85a3
Fixed typos
cjrupak Dec 27, 2017
af4be9c
fix "coquilles" in french translations.
davidmars Dec 28, 2017
2294829
french grammatical fix.
davidmars Dec 28, 2017
3bae372
Merge pull request #227 from davidmars/patch-1
filisko Jan 1, 2018
d94412b
Merge pull request #226 from contriveitup/fixes
filisko Jan 1, 2018
e4b12ce
added period to valid characters in phone number
Jan 22, 2018
1f65915
Update gump.class.php
innovaweb-dev Feb 15, 2018
4cbbae0
Create nl.php
innovaweb-dev Feb 15, 2018
0564c9b
Add Twitter Username Validation
scottcase Feb 25, 2018
0501bfe
Add && function_exists('idn_to_ascii')
scottcase Feb 27, 2018
7dec32d
Merge pull request #238 from scottcase/master
filisko Feb 28, 2018
ae496aa
Merge pull request #236 from rifton007/master
filisko Feb 28, 2018
4be80a5
Merge pull request #1 from Wixel/master
scottcase Feb 28, 2018
6b56713
Create tr.php
boralp Apr 6, 2018
7b1b7a3
adding indonesian language
Jun 2, 2018
269be44
adding array_column for php that not have the function
Jun 2, 2018
ecd4111
adding array_column for php that not have the function
Jun 2, 2018
eaf00e7
Merge pull request #247 from zhanza/master
filisko Jun 2, 2018
08a6313
Revert "adding indonesian language"
filisko Jun 2, 2018
01bc6c3
Merge pull request #248 from Wixel/revert-247-master
filisko Jun 2, 2018
8376f24
Revert "adding array_column for php that not have the function"
Jun 2, 2018
657a515
Revert "adding array_column for php that not have the function"
Jun 2, 2018
37595e0
Update .travis.yml
filisko Jun 2, 2018
daab248
Merge pull request #249 from rikylesmana/master
filisko Jun 2, 2018
b32c922
Create id.php
filisko Jun 2, 2018
f172a45
Allow '|' and ',' by optionally using custom separators for rules and…
ChrisFromLafare Aug 6, 2018
15f431b
Merge pull request #261 from ChrisFromLafare/feature/allow-pipe
sn Nov 16, 2018
a6c4fce
Updated .gitignore
sn Nov 16, 2018
a7dfc7a
Merge pull request #243 from boralp/master
sn Nov 16, 2018
6f7451e
Merge pull request #232 from mjarrison/master
sn Nov 16, 2018
0655979
Merge pull request #2 from Wixel/master
scottcase Dec 6, 2018
98ec02d
add file_size validation
scottcase Dec 6, 2018
11047e9
Fix slug filter returning empty value
Sep 12, 2019
26de1a5
Merge pull request #285 from pixeline/patch-1
sn Sep 16, 2019
4299e5a
Merge pull request #3 from Wixel/master
scottcase Nov 21, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
.DS_Store
.DS_Store
.idea
nbproject
nb-configuration.xml
Session.vim
*.tmproj
*.tmproject
tmtags
Thumbs.db
Desktop.ini

2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@ language: php
php:
- "7.0"
- "5.6"
- "5.5"
- "5.4"
- hhvm
script: php tests.php
122 changes: 106 additions & 16 deletions gump.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,20 @@ public function filter_rules(array $rules = array())
*
* @param array $data
* @param bool $check_fields
* @param string $rules_delimiter
* @param string $parameters_delimiters
*
* @return array
*
* @throws Exception
*/
public function run(array $data, $check_fields = false)
public function run(array $data, $check_fields = false, $rules_delimiter='|', $parameters_delimiters=',')
{
$data = $this->filter($data, $this->filter_rules());
$data = $this->filter($data, $this->filter_rules(), $rules_delimiter, $parameters_delimiters);

$validated = $this->validate(
$data, $this->validation_rules()
$data, $this->validation_rules(),
$rules_delimiter, $parameters_delimiters
);

if ($check_fields === true) {
Expand Down Expand Up @@ -368,21 +371,24 @@ public function errors()

/**
* Perform data validation against the provided ruleset.
* If any rule's parameter contains either '|' or ',', the corresponding default separator can be changed
*
* @param mixed $input
* @param array $ruleset
* @param string $rules_delimiter
* @param string $parameters_delimiter
*
* @return mixed
*
* @throws Exception
*/
public function validate(array $input, array $ruleset)
public function validate(array $input, array $ruleset, $rules_delimiter='|', $parameters_delimiter=',')
{
$this->errors = array();

foreach ($ruleset as $field => $rules) {

$rules = explode('|', $rules);
$rules = explode($rules_delimiter, $rules);

$look_for = array('required_file', 'required');

Expand All @@ -407,8 +413,8 @@ public function validate(array $input, array $ruleset)
$param = null;

// Check if we have rule parameters
if (strstr($rule, ',') !== false) {
$rule = explode(',', $rule);
if (strstr($rule, $parameters_delimiter) !== false) {
$rule = explode($parameters_delimiter, $rule);
$method = 'validate_'.$rule[0];
$param = $rule[1];
$rule = $rule[0];
Expand Down Expand Up @@ -651,30 +657,33 @@ public function get_errors_array($convert_to_string = null)

/**
* Filter the input data according to the specified filter set.
* If any filter's parameter contains either '|' or ',', the corresponding default separator can be changed
*
* @param mixed $input
* @param array $filterset
* @param string $filters_delimeter
* @param string $parameters_delimiter
*
* @throws Exception
*
* @return mixed
*
* @throws Exception
*/
public function filter(array $input, array $filterset)
public function filter(array $input, array $filterset, $filters_delimeter='|', $parameters_delimiter=',')
{
foreach ($filterset as $field => $filters) {
if (!array_key_exists($field, $input)) {
continue;
}

$filters = explode('|', $filters);
$filters = explode($filters_delimeter, $filters);

foreach ($filters as $filter) {
$params = null;

if (strstr($filter, ',') !== false) {
$filter = explode(',', $filter);
if (strstr($filter, $parameters_delimiter) !== false) {
$filter = explode($parameters_delimiter, $filter);

$params = array_slice($filter, 1, count($filter) - 1);

Expand Down Expand Up @@ -942,7 +951,7 @@ protected function filter_upper_case($value, $params = null)
protected function filter_slug($value, $params = null)
{
$delimiter = '-';
$slug = strtolower(trim(preg_replace('/[\s-]+/', $delimiter, preg_replace('/[^A-Za-z0-9-]+/', $delimiter, preg_replace('/[&]/', 'and', preg_replace('/[\']/', '', iconv('UTF-8', 'ASCII//TRANSLIT', $str))))), $delimiter));
$slug = strtolower(trim(preg_replace('/[\s-]+/', $delimiter, preg_replace('/[^A-Za-z0-9-]+/', $delimiter, preg_replace('/[&]/', 'and', preg_replace('/[\']/', '', iconv('UTF-8', 'ASCII//TRANSLIT', $value))))), $delimiter));
return $slug;
}

Expand Down Expand Up @@ -1513,7 +1522,7 @@ protected function validate_url_exists($field, $input, $param = null)
$url = $url['host'];
}

if (function_exists('checkdnsrr')) {
if (function_exists('checkdnsrr') && function_exists('idn_to_ascii')) {
if (checkdnsrr(idn_to_ascii($url), 'A') === false) {
return array(
'field' => $field,
Expand Down Expand Up @@ -1646,6 +1655,23 @@ protected function validate_valid_cc($field, $input, $param = null)
$number_length = strlen($number);
}


/**
* Bail out if $number_length is 0.
* This can be the case if a user has entered only alphabets
*
* @since 1.5
*/
if( $number_length == 0 ) {
return array(
'field' => $field,
'value' => $input[$field],
'rule' => __FUNCTION__,
'param' => $param,
);
}


$parity = $number_length % 2;

$total = 0;
Expand Down Expand Up @@ -1693,7 +1719,7 @@ protected function validate_valid_name($field, $input, $param = null)
return;
}

if (!preg_match("/^([a-zÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖßÙÚÛÜÝàáâãäåçèéêëìíîïñðòóôõöùúûüýÿ '-])+$/i", $input[$field]) !== false) {
if (!preg_match("/^([a-z \p{L} '-])+$/i", $input[$field]) !== false) {
return array(
'field' => $field,
'value' => $input[$field],
Expand Down Expand Up @@ -1999,7 +2025,7 @@ protected function validate_extension($field, $input, $param = null)
$path_info = pathinfo($input[$field]['name']);
$extension = isset($path_info['extension']) ? $path_info['extension'] : false;

if ($extension && in_array($extension, $allowed_extensions)) {
if ($extension && in_array(strtolower($extension), $allowed_extensions)) {
return;
}

Expand All @@ -2011,6 +2037,42 @@ protected function validate_extension($field, $input, $param = null)
);
}
}

/**
* Check the uploaded file for filesize
* checks only the ext should add mime type check.
*
* Usage: '<index>' => 'file_size,500kb
*
* @param string $field
* @param array $input
*
* @return mixed
*/
protected function validate_file_size($field, $input, $param = null)
{
if (!isset($input[$field])) {
return;
}

if (is_array($input[$field]) && $input[$field]['error'] !== 4) {
$max_filesize = str_replace('kb', '', trim(strtolower($param))) * 1024;

//get the image filesize
$imagesize = $input[$field]['size'];

if ( ($imagesize > 0) && ($imagesize < $max_filesize) ) {
return;
}

return array(
'field' => $field,
'value' => $input[$field],
'rule' => __FUNCTION__,
'param' => $param,
);
}
}

/**
* Determine if the provided field value equals current field value.
Expand Down Expand Up @@ -2099,6 +2161,7 @@ private function trimScalar($value)
* Examples:
*
* 555-555-5555: valid
* 555.555.5555: valid
* 5555425555: valid
* 555 555 5555: valid
* 1(519) 555-4444: valid
Expand All @@ -2112,7 +2175,7 @@ protected function validate_phone_number($field, $input, $param = null)
return;
}

$regex = '/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i';
$regex = '/^(\d[\s-\.]?)?[\(\[\s-\.]{0,2}?\d{3}[\)\]\s-\.]{0,2}?\d{3}[\s-\.]?\d{4}$/i';
if (!preg_match($regex, $input[$field])) {
return array(
'field' => $field,
Expand Down Expand Up @@ -2387,4 +2450,31 @@ protected function validate_valid_pashtu_text($field, $input, $param = null)
}
}

/**
* Determine if the provided value is a valid twitter handle.
*
* @access protected
* @param string $field
* @param array $input
* @return mixed
*/
protected function validate_valid_twitter($field, $input, $param = NULL)
{
if(!isset($input[$field]) || empty($input[$field]))
{
return;
}
$json_twitter = file_get_contents("http://twitter.com/users/username_available?username=".$input[$field]);

$twitter_response = json_decode($json_twitter);
if($twitter_response->reason != "taken"){
return array(
'field' => $field,
'value' => $input[$field],
'rule' => __FUNCTION__,
'param' => $param
);
}
}

}
46 changes: 46 additions & 0 deletions lang/de.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

return array(
'validate_required' => 'Das Feld "{field}" ist erforderlich.',
'validate_valid_email' => 'Das Feld "{field}" muss eine g&uuml;ltige E-Mail-Adresse sein.',
'validate_max_len' => 'Das Feld "{field}" darf maximal {param} Zeichen enthalten.',
'validate_min_len' => 'Das Feld "{field}" muss mindestens {param} Zeichen enthalten.',
'validate_exact_len' => 'Das Feld "{field}" muss genau {param} Zeichen enthalten.',
'validate_alpha' => 'Das Feld "{field}" darf nur Buchstaben enthalten.',
'validate_alpha_numeric' => 'Das Feld "{field}" darf nur Buchstaben und Ziffern enthalten.',
'validate_alpha_numeric_space' => 'Das Feld "{field}" darf nur Buchstaben, Ziffern und Leerzeichen enthalten.',
'validate_alpha_dash' => 'Das Feld "{field}" darf nur Buchstaben und Bindestriche enthalten.',
'validate_alpha_space' => 'Das Feld "{field}" darf nur Buchstaben und Leerzeichen enthalten.',
'validate_numeric' => 'Das Feld "{field}" muss eine Nummer sein.',
'validate_integer' => 'Das Feld "{field}" muss eine Nummer ohne Nachkommastellen (ganze Zahl) sein.',
'validate_boolean' => 'Das Feld "{field}" muss entweder wahr oder falsch sein.',
'validate_float' => 'Das Feld "{field}" muss eine Nummer mit einem Dezimalpunkt (Gleitpunktzahl) sein.',
'validate_valid_url' => 'Das Feld "{field}" muss eine URL sein.',
'validate_url_exists' => 'Die im Feld "{field}" angegebene URL existiert nicht.',
'validate_valid_ip' => 'Das Feld "{field}" muss eine g&uuml;ltige IP-Adresse sein.',
'validate_valid_ipv4' => 'Das Feld "{field}" muss eine g&uuml;ltige IPv4-Adresse enthalten.',
'validate_valid_ipv6' => 'Das Feld "{field}" muss eine g&uuml;ltige IPv6-Adresse enthalten.',
'validate_guidv4' => 'Das Feld "{field}" muss eine g&uuml;ltige GUID enthalten.',
'validate_valid_cc' => 'Das Feld "{field}" ist keine g&uuml;ltige Kreditkartennummer.',
'validate_valid_name' => 'Das Feld "{field}" muss ein voller Name sein.',
'validate_contains' => 'Das Feld "{field}" kann nur eines der folgenden sein: {param}',
'validate_contains_list' => 'Das Feld "{field}" ist keine g&uuml;ltige Wahl.',
'validate_doesnt_contain_list' => 'Das Feld "{field}" enth&auml;lt einen nicht akzeptierten Wert.',
'validate_street_address' => 'Das Feld "{field}" muss eine g&uuml;ltige Stra&szlig;enangabe sein.',
'validate_date' => 'Das Feld "{field}" muss ein g&uuml;ltiges Datum sein.',
'validate_min_numeric' => 'Das Feld "{field}" muss ein numerischer Wert gr&ouml;&szlig;ergleich {param} sein.',
'validate_max_numeric' => 'Das Feld "{field}" muss ein numerischer Wert kleinergleich {param} sein.',
'validate_min_age' => 'Das Feld "{field}" muss ein Alter gr&ouml;&szlig;ergleich {param} haben.',
'validate_invalid' => 'Das Feld "{field}" ist ung&uuml;ltig.',
'validate_starts' => 'Das Feld "{field}" muss mit {param} beginnen.',
'validate_extension' => 'Das Feld "{field}" kann nur eine der folgenden Erweiterungen haben: {param}',
'validate_required_file' => 'Das Feld "{field}" ist erforderlich.',
'validate_equalsfield' => 'Das Feld "{field}" ist nicht gleich dem Feld "{param}".',
'validate_iban' => 'Das Feld "{field}" muss eine g&uuml;ltige IBAN enthalten.',
'validate_phone_number' => 'Das Feld "{field}" muss eine g&uuml;ltige Telefonnummer sein.',
'validate_regex' => 'Das Feld "{field}" muss einen Wert im g&uuml;ltigem Format enthalten.',
'validate_valid_json_string' => 'Das Feld "{field}" muss eine g&uuml;ltige JSON-Format-Zeichenfolge enthalten.',
'validate_valid_array_size_greater' => 'Das Feld "{field}" muss ein Array mit einer Gr&ouml;&szlig;e gr&ouml;&szlig;ergleich {param} sein.',
'validate_valid_array_size_lesser' => 'Das Feld "{field}" muss ein Array mit einer Gr&ouml;&szlig;e kleinergleich {param} sein.',
'validate_valid_array_size_equal' => 'Das Feld "{field}" muss ein Array mit einer Gr&ouml;&szlig;e gleich {param} sein.',
);
2 changes: 2 additions & 0 deletions lang/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
'validate_invalid' => 'The {field} field is invalid',
'validate_starts' => 'The {field} field needs to start with {param}',
'validate_extension' => 'The {field} field can only have one of the following extensions: {param}',
'validate_file_size' => 'The {field} must be smaller than: {param}',
'validate_required_file' => 'The {field} field is required',
'validate_equalsfield' => 'The {field} field does not equal {param} field',
'validate_iban' => 'The {field} field needs to contain a valid IBAN',
Expand All @@ -48,4 +49,5 @@
'validate_valid_persian_digit' => 'The {field} should be a valid digit in Persian/Dari or Arabic format',
'validate_valid_persian_text' => 'The {field} should be a valid text in Persian/Dari or Arabic format',
'validate_valid_pashtu_text' => 'The {field} should be a valid text in Pashtu format',
'validate_valid_twitter' => 'The {field} is not a valid twitter handle',
);
8 changes: 4 additions & 4 deletions lang/es.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
return array(
'validate_required' => 'El campo {field} es requerido',
'validate_valid_email' => 'El campo {field} debe ser una dirección de correo electrónico válida',
'validate_max_len' => 'El campo {field} no puede tener más de {param} carácteres de longitud',
'validate_min_len' => 'El campo {field} debe tener al menos {param} carácteres de longitud',
'validate_exact_len' => 'El campo {field} debe tener {param} carácteres de longitud',
'validate_max_len' => 'El campo {field} no puede tener más de {param} caracteres de longitud',
'validate_min_len' => 'El campo {field} debe tener al menos {param} caracteres de longitud',
'validate_exact_len' => 'El campo {field} debe tener {param} caracteres de longitud',
'validate_alpha' => 'El campo {field} sólo puede contener letras',
'validate_alpha_numeric' => 'El campo {field} sólo puede contener letras y números',
'validate_alpha_numeric_space' => 'El campo {field} solo puede contener letras, números y espacios',
'validate_alpha_dash' => 'El campo {field} sólo puede contener letras y guiones',
'validate_alpha_space' => 'El campo {field} sólo puede contener letras y espacios',
'validate_numeric' => 'El campo {field} sólo puede contener carácteres numéricos',
'validate_numeric' => 'El campo {field} sólo puede contener caracteres numéricos',
'validate_integer' => 'El campo {field} sólo puede contener un valor numérico',
'validate_boolean' => 'El campo {field} debe ser verdadero o falso',
'validate_float' => 'El campo {field} sólo puede contener un valor flotante',
Expand Down
4 changes: 2 additions & 2 deletions lang/fr.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
'validate_date' => 'Le champ {field} doit &#234;tre une date valide',
'validate_min_numeric' => 'Le champ {field} doit &#234;tre une valeur num&#233;rique &#233;gale ou sup&#233;rieur à {param}',
'validate_max_numeric' => 'Le champ {field} doit &#234;tre une valeur num&#233;rique &#233;gale ou inf&#233;rieur à {param}',
'validate_min_age' => 'Le champ {field} doit &#234;tre un age &#233;gale ou sup&#233;rieur à {param}',
'validate_min_age' => 'Le champ {field} doit &#234;tre un &#226;ge &#233;gal ou sup&#233;rieur à {param}',
'validate_invalid' => 'Le champ {field} est invalide',
'validate_starts' => 'Le champ {field} doit commencer par {param}',
'validate_extension' => 'Le champ {field} doit avoir les extensions suivantes {param}',
'validate_required_file' => 'Le champ {field} est obligatoire',
'validate_equalsfield' => 'Le champ {field} n&#44;est pas &#233;gale au champ {param}',
'validate_iban' => 'Le champ {field} doit contenir un IBAN valide',
'validate_phone_number' => 'Le champ {field} doit contenir num&#233;ro de t&#233;l&#233;phone valide',
'validate_phone_number' => 'Le champ {field} doit contenir un num&#233;ro de t&#233;l&#233;phone valide',
'validate_regex' => 'Le champ {field} doit contenir une valeur valide',
'validate_valid_json_string' => 'Le champ {field} doit avoir un format JSON',
);
Loading