From bc24589dd09a1fc2f4a26b1694d8dec4786cae83 Mon Sep 17 00:00:00 2001 From: marko Date: Wed, 20 Oct 2021 10:24:08 +0200 Subject: [PATCH] Error checking addCoronaUser, some modularization --- homepage/mams/index.php | 96 ++++++++-------------- homepage/mams/lib/api.php | 52 +++++++++--- homepage/mams/lib/mams/materializeInit.php | 11 +++ homepage/mams/lib/mams/sidenav.php | 6 ++ 4 files changed, 91 insertions(+), 74 deletions(-) create mode 100644 homepage/mams/lib/mams/materializeInit.php create mode 100644 homepage/mams/lib/mams/sidenav.php diff --git a/homepage/mams/index.php b/homepage/mams/index.php index bc45acd..63bfa03 100644 --- a/homepage/mams/index.php +++ b/homepage/mams/index.php @@ -1,74 +1,48 @@
- - - - - - - - - - - - Corona-Anwesenheitsliste der Judoka des Chemnitzer WSV -
- - - ☰ mams + + + + + + + + + Corona-Anwesenheitsliste der Judoka des Chemnitzer WSV + + + +

Anwesenheiten hinzufügen

@@ -97,7 +71,7 @@ $lastTrainingDay = max($dateLastWendsday, $dateLastFriday); - +
diff --git a/homepage/mams/lib/api.php b/homepage/mams/lib/api.php index 04910fa..6cd3bdf 100644 --- a/homepage/mams/lib/api.php +++ b/homepage/mams/lib/api.php @@ -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; +} ?> diff --git a/homepage/mams/lib/mams/materializeInit.php b/homepage/mams/lib/mams/materializeInit.php new file mode 100644 index 0000000..62f5524 --- /dev/null +++ b/homepage/mams/lib/mams/materializeInit.php @@ -0,0 +1,11 @@ + diff --git a/homepage/mams/lib/mams/sidenav.php b/homepage/mams/lib/mams/sidenav.php new file mode 100644 index 0000000..6ca1209 --- /dev/null +++ b/homepage/mams/lib/mams/sidenav.php @@ -0,0 +1,6 @@ + +☰ mams