diff --git a/api/api_functions.php b/api/api_functions.php new file mode 100644 index 0000000..c6e334e --- /dev/null +++ b/api/api_functions.php @@ -0,0 +1,61 @@ + 2, + 'portability' => DB_PORTABILITY_ALL, + ); + } + + $conn = DB::connect(getDSN(), $options); + + if(DB :: isError($conn)) { + echo $conn->toString(); + } else { + if($fetch_mode != NULL) { + $conn->setFetchMode($fetch_mode); + } + } + + return $conn; +} + + +function clean_phonenumber($pn) { + $pn = preg_replace('/[^0-9\+]/', '', $pn); // remove everything except valid chars + $pn = preg_replace('/^00/','+', $pn); // replace starting 00 with + + // norwegian phone numbers + if( strlen($pn) === 8 && ($pn[0] === "4" || $pn[0] === "9") ) { + $pn = "+47".$pn; + } + // ...without + + if( strlen($pn) === 10 && $pn[0] === "4" && $pn[1] === "7" && ($pn[2] === "4" || $pn[2] === "9") ) { + $pn = "+".$pn; + } + return $pn; +} + + +// E.164 +function valid_phonenumber($phone) { + return preg_match('/^\+?\d{8,15}$/i', $phone); +} \ No newline at end of file diff --git a/api/groups.php b/api/groups.php index e407cbf..3ab152f 100644 --- a/api/groups.php +++ b/api/groups.php @@ -1,32 +1,12 @@ 2, - 'portability' => DB_PORTABILITY_ALL, -); - -$conn = DB::connect(getDSN(), $options); - -if(DB :: isError($conn)) { - echo $conn->toString(); -} -$conn->setFetchMode(DB_FETCHMODE_ASSOC); +$conn = get_db_connection(DB_FETCHMODE_ASSOC); // Search query $sql = "SELECT g.name as group_name, g.id as group_id, g.posix_group, g.mailinglist, g.admin as is_admin_group, diff --git a/api/phonenumber.php b/api/phonenumber.php new file mode 100644 index 0000000..77d015a --- /dev/null +++ b/api/phonenumber.php @@ -0,0 +1,61 @@ + "Missing param apikey.")); + die(); +} +/* Valid API KEY (defined in credentials.php) ? */ +if( $_GET['apikey'] !== USER_API_KEY && $_GET['apikey'] !== USER_API_KEY_KASSA ) { + set_response_code(400); + echo json_encode(array('error' => "Invalid apikey: '".$_GET['apikey']."'.")); + die(); +} + +/* Validate params */ +if(!isset($_GET['q'])) { + set_response_code(400); + echo json_encode(array('error' => "Missing param q")); + die(); +} + +$phonenumber = $_GET['q']; +$phonenumber = clean_phonenumber($phonenumber); +if(!valid_phonenumber($phonenumber)) { + set_response_code(400); + echo json_encode(array('error' => "Invalid phone number: '".$phonenumber."'")); + die(); +} +$phonenumber = $conn->quoteSmart($phonenumber); + +// Search query +$sql = " + SELECT u.id FROM din_userphonenumber AS p + LEFT JOIN din_user AS u ON p.user_id=u.id + WHERE p.number=$phonenumber"; + +$res = $conn->getAll($sql); + +if( DB::isError($res) ) { + set_response_code(500); + echo json_encode(array('error' => $res->message)); + die(); +} +$user_ids = array(); +if(count($res) == 0) { + echo json_encode(array('results' => [])); + die(); +} +$user_ids = $res[0]; +echo json_encode(array('results' => $user_ids)); + +?> \ No newline at end of file diff --git a/api/user.php b/api/user.php index 252212a..8debecd 100644 --- a/api/user.php +++ b/api/user.php @@ -1,36 +1,12 @@ 2, - 'portability' => DB_PORTABILITY_ALL, -); - -$conn = DB::connect(getDSN(), $options); - -if(DB :: isError($conn)) { - echo $conn->toString(); -} -$conn->setFetchMode(DB_FETCHMODE_ORDERED); +$conn = get_db_connection(DB_FETCHMODE_ORDERED); if( !isset($_GET['apikey']) ) { set_response_code(400); @@ -38,11 +14,13 @@ if( !isset($_GET['apikey']) ) { die(); } /* Valid API KEY (defined in credentials.php) ? */ -if( $_GET['apikey'] !== USER_API_KEY ) { +if( $_GET['apikey'] !== USER_API_KEY && $_GET['apikey'] !== USER_API_KEY_KASSA ) { set_response_code(400); - echo json_encode(array('error' => "Invalid apikey.".$_GET['apikey'])); + echo json_encode(array('error' => "Invalid apikey: '".$_GET['apikey']."'.")); die(); } +/* Note: Only gives kassa.neuf.no API-rights to search all members */ +$limit_to_active = $_GET['apikey'] !== USER_API_KEY_KASSA; /* Validate params */ if(!isset($_GET['q'])) { @@ -104,17 +82,18 @@ if( isset($_GET['exact']) ) { } $valid_membership_query = $filter_valid_membership ? " AND (u.expires >= NOW() OR u.expires IS NULL)" : ""; +$limit_active_query = $limit_to_active ? "AND ug.group_id=2" : ""; // Note: Conditional filter on active group // Default $sql = "SELECT DISTINCT u.id FROM din_user u LEFT JOIN din_usergrouprelationship as ug ON u.id=ug.user_id WHERE ($user_search_query) - AND ug.group_id=2 /* Note: Always filter on active group */ + $limit_active_query $valid_membership_query"; // Override query with groups -if(count($groups) > 0) { +if(count($groups) > 0 && $limit_to_active) { $sql = "SELECT DISTINCT u.id FROM din_user u LEFT JOIN din_usergrouprelationship as ug ON u.id=ug.user_id @@ -123,6 +102,13 @@ if(count($groups) > 0) { AND ug.group_id IN(".implode($groups, ",").") AND ug2.group_id=2 /* Note: Always filter on active group, here with a double join */ $valid_membership_query"; +} elseif(count($groups) > 0 && !$limit_to_active) { + $sql = "SELECT DISTINCT u.id + FROM din_user u + LEFT JOIN din_usergrouprelationship as ug ON u.id=ug.user_id + WHERE ($user_search_query) + AND ug.group_id IN(".implode($groups, ",").") + $valid_membership_query"; } $res = $conn->getAll($sql); @@ -149,7 +135,7 @@ $conn->setFetchMode(DB_FETCHMODE_ASSOC); /* Get data */ $sql_is_member = "u.expires > NOW() OR u.expires IS NULL AS is_member"; $sql_groups = "GROUP_CONCAT(DISTINCT g.id,';',g.name SEPARATOR ',') AS groups"; -$data_sql = "SELECT u.id,u.username,u.firstname,u.lastname,u.email,up.number,u.expires,$sql_groups,$sql_is_member +$data_sql = "SELECT u.id,u.username,u.firstname,u.lastname,u.email,up.number,u.expires,u.cardno,$sql_groups,$sql_is_member FROM din_user as u LEFT JOIN din_userphonenumber as up ON up.user_id=u.id LEFT JOIN din_usergrouprelationship AS ug ON u.id=ug.user_id @@ -208,4 +194,4 @@ if($out === false) { die(); } echo $out; -?> +?> \ No newline at end of file diff --git a/api/username.php b/api/username.php index b043435..3e81764 100644 --- a/api/username.php +++ b/api/username.php @@ -1,32 +1,12 @@ 2, - 'portability' => DB_PORTABILITY_ALL, -); - -$conn = DB::connect(getDSN(), $options); - -if(DB :: isError($conn)) { - echo $conn->toString(); -} -$conn->setFetchMode(DB_FETCHMODE_ORDERED); +$conn = get_db_connection(DB_FETCHMODE_ORDERED); /* Validate params */ if(!isset($_GET['q'])) { diff --git a/api/zipcode.php b/api/zipcode.php index 84e3f63..5b9f24a 100644 --- a/api/zipcode.php +++ b/api/zipcode.php @@ -1,36 +1,12 @@ 2, - 'portability' => DB_PORTABILITY_ALL, -); - -$conn = DB::connect(getDSN(), $options); - -if(DB :: isError($conn)) { - echo $conn->toString(); -} -$conn->setFetchMode(DB_FETCHMODE_ORDERED); +$conn = get_db_connection(DB_FETCHMODE_ORDERED); +error_reporting(0); /* Validate params */ if(!isset($_GET['q'])) { diff --git a/inside/credentials-sample.php b/inside/credentials-sample.php index 87fcb5c..9cfe72f 100644 --- a/inside/credentials-sample.php +++ b/inside/credentials-sample.php @@ -27,4 +27,6 @@ function getDSN($host = "default") { define("USER_API_KEY", ""); +define("USER_API_KEY_KASSA", ""); + ?>