Error checking addCoronaUser, some modularization

This commit is contained in:
marko
2021-10-20 10:24:08 +02:00
parent 6f5e2ee405
commit bc24589dd0
4 changed files with 91 additions and 74 deletions

View File

@@ -10,12 +10,16 @@ function processPostData($db, $post, $redirectLocation = "."){
}
if($post['action'] == "addCoronaUser"){
if(
isValid($post['corona_PLZ'], "plz")
array_keys_exist(
$post,
['name', 'vorname', 'corona_PLZ', 'corona_telephon', 'corona_eMail']
)
&& isValid($post['corona_PLZ'], "plz")
&& isValid($post['corona_telephon'], "phonenumber")
&& isValid($post['corona_eMail'], "email")
// && isValid($post['name'], "name")
// && isValid($post['vorname'], "name")
// && isValid($post['corona_telephon'], "phonenumber")
// && isValid($post['corona_eMail'], "email")
){
){
addCoronaUser(
$db,
$post['name'],
@@ -114,15 +118,37 @@ function isValid($toValidate, $type){
// for now we disable the name validation: what do i know how people can be called!
// $regexName="/^[A-Z][a-zA-Z]*$/";
$regexPlz ="/^[0-9]{5}$/";
if($type == "plz" )
return preg_match($regexPlz, $toValidate) > 0;
if($type == "name")
return preg_match($regexName, $toValidate) > 0;
if($type == "phonenumber" )
return validate_phone_number($toValidate);
if($type == "email")
return filter_var($toValidate, FILTER_VALIDATE_EMAIL);
switch( $type ){
case "plz":
return preg_match($regexPlz, $toValidate) > 0;
case "phonenumber":
return validate_phone_number($toValidate);
case "email":
return filter_var($toValidate, FILTER_VALIDATE_EMAIL);
default:
return false;
}
return false;
}
//! Checks if multiple keys exist in an array
//!
//! @param array $array array to check for key
//! @param array|string $keys keys to check for
//!
//! @return bool true, if *all* keys are set in the array
function array_keys_exist( array $array, $keys ) {
if ( ! is_array( $keys ) ) {
$keys = func_get_args();
array_shift( $keys );
}
$count = 0;
foreach ( $keys as $key ) {
if ( isset( $array[$key] ) || array_key_exists( $key, $array ) ) {
$count++;
}
}
return count( $keys ) === $count;
}
?>