I created a function that filters data and I want to know if it's fully secure to filter user input data.
function user_input_data($data,$options) {
$output = null;
$error_msg = "";
$check_length = false;
$options_fetch = array(
@$length_max = $options["length_max"],
@$length_min = $options["length_min"],
$data_type = $options["data_type"],
);
if($length_min && $length_max) {
$check_length = true;
}
if($data_type == "only_letters") {
if (preg_match('/^[\p{Arabic}a-zA-Z]+\h?[\p{Arabic}a-zA-Z]*$/u', $data)) {
$output = $data;
}else{
$output = false;
}
}
if($data_type == "only_letters_numbers") {
if (preg_match('/^[\p{Arabic}a-zA-Z0-9]+\h?[\p{Arabic}a-zA-Z0-9]*$/u', $data)) {
$output = $data;
}else{
$output = false;
}
}
if($data_type == "string") {
$output = htmlspecialchars($data,ENT_QUOTES,"UTF-8",true);
}
if($data_type == "text") {
$output = strip_tags($data);
}
if($data_type == "integer") {
if(filter_var($data,FILTER_VALIDATE_INT)) {
$output = $data;
}
}
if($data_type == "float") {
if(filter_var($data,FILTER_VALIDATE_FLOAT)) {
$output = $data;
}
}
if($data_type == "boolean") {
if(filter_var($data,FILTER_VALIDATE_BOOLEAN)) {
$output = $data;
}
}
if($data_type == "email") {
if(filter_var($data,FILTER_VALIDATE_EMAIL)) {
$output = $data;
}
}
if($data_type == "url") {
if(filter_var($data,FILTER_VALIDATE_URL)) {
$output = $data;
}
}
if($data_type == "ip") {
if(filter_var($data,FILTER_VALIDATE_IP)) {
$output = $data;
}
}
if($data_type == "array") {
if(is_array($data)) {
$data = array_map("htmlspecialchars",$data);
$output = $data;
}
}
if($data_type == "file_directory") {
if(file_exists($data)) {
$output = basename($data);
}
}
if($output) {
if($check_length) {
if( ( ( mb_strlen($output) >= $length_min ) && ( mb_strlen($output) <= $length_max ) ) === false) {
$output = false;
}
}
return $output;
}
}
// call function
$password = user_input_data(
$_POST["password"],
array(
"length_min" => 8, "length_max" => 24 ,"data_type" => "string"
)
);
$var = user_input_data($POST['var'],array("data_type" => "only_letters"))so if user input data contain numbers or characters it return false \$\endgroup\$