Error checking addCoronaUser, some modularization
This commit is contained in:
@@ -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;
|
||||
}
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user