Adds phonenumber lookup to API, refactoring
This commit is contained in:
1 parent
d7979a3691
commit
ac7d136948
7 files changed
+149
-103
No files matched your search
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
set_include_path("../includes/");
|
||||
require_once("../includes/DB.php");
|
||||
require_once("../inside/credentials.php");
|
||||
|
||||
|
||||
/* Set HTTP response code */
|
||||
function set_response_code($code) {
|
||||
if(!is_int($code)) {
|
||||
return false;
|
||||
}
|
||||
header('X-Ignore-This: something', true, $code);
|
||||
}
|
||||
|
||||
|
||||
function is_valid_utf8($text) {
|
||||
return mb_check_encoding($text, 'utf-8');
|
||||
}
|
||||
|
||||
|
||||
function get_db_connection($fetch_mode=NULL, $options=NULL) {
|
||||
if($options == NULL) {
|
||||
$options = array(
|
||||
'debug' => 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);
|
||||
}
|
||||
+2
-22
@@ -1,32 +1,12 @@
|
||||
<?php
|
||||
set_include_path("../includes/");
|
||||
require_once("../inside/credentials.php");
|
||||
require_once("api_functions.php");
|
||||
require_once("../includes/DB.php");
|
||||
|
||||
/* Functions */
|
||||
|
||||
/* Set HTTP response code */
|
||||
function set_response_code($code) {
|
||||
if(!is_int($code)) {
|
||||
return false;
|
||||
}
|
||||
header('X-Ignore-This: something', true, $code);
|
||||
}
|
||||
|
||||
header('Access-Control-Allow-Origin: *', true); // Come fetch!
|
||||
|
||||
/* Connect to database */
|
||||
$options = array(
|
||||
'debug' => 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,
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
require_once("api_functions.php");
|
||||
require_once("../includes/DB.php");
|
||||
|
||||
|
||||
header('Access-Control-Allow-Origin: *', true); // Come fetch!
|
||||
|
||||
/* Connect to database */
|
||||
$conn = get_db_connection(DB_FETCHMODE_ORDERED);
|
||||
|
||||
|
||||
if( !isset($_GET['apikey']) ) {
|
||||
set_response_code(400);
|
||||
echo json_encode(array('error' => "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));
|
||||
|
||||
?>
|
||||
+18
-32
@@ -1,36 +1,12 @@
|
||||
<?php
|
||||
set_include_path("../includes/");
|
||||
require_once("../inside/credentials.php");
|
||||
require_once("api_functions.php");
|
||||
require_once("../includes/DB.php");
|
||||
|
||||
/* Functions */
|
||||
|
||||
/* Set HTTP response code */
|
||||
function set_response_code($code) {
|
||||
if(!is_int($code)) {
|
||||
return false;
|
||||
}
|
||||
header('X-Ignore-This: something', true, $code);
|
||||
}
|
||||
|
||||
function is_valid_utf8($text) {
|
||||
return mb_check_encoding($text, 'utf-8');
|
||||
}
|
||||
|
||||
header('Access-Control-Allow-Origin: *', true); // Come fetch!
|
||||
|
||||
/* Connect to database */
|
||||
$options = array(
|
||||
'debug' => 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;
|
||||
?>
|
||||
?>
|
||||
+2
-22
@@ -1,32 +1,12 @@
|
||||
<?php
|
||||
set_include_path("../includes/");
|
||||
require_once("../inside/credentials.php");
|
||||
require_once("api_functions.php");
|
||||
require_once("../includes/DB.php");
|
||||
|
||||
/* Functions */
|
||||
|
||||
/* Set HTTP response code */
|
||||
function set_response_code($code) {
|
||||
if(!is_int($code)) {
|
||||
return false;
|
||||
}
|
||||
header('X-Ignore-This: something', true, $code);
|
||||
}
|
||||
|
||||
header('Access-Control-Allow-Origin: *', true); // Come fetch!
|
||||
|
||||
/* Connect to database */
|
||||
$options = array(
|
||||
'debug' => 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'])) {
|
||||
|
||||
+3
-27
@@ -1,36 +1,12 @@
|
||||
<?php
|
||||
set_include_path("../includes/");
|
||||
require_once("../inside/credentials.php");
|
||||
require_once("api_functions.php");
|
||||
require_once("../includes/DB.php");
|
||||
|
||||
/* Functions */
|
||||
|
||||
/* Set HTTP response code */
|
||||
function set_response_code($code) {
|
||||
if(!is_int($code)) {
|
||||
return false;
|
||||
}
|
||||
header('X-Ignore-This: something', true, $code);
|
||||
}
|
||||
|
||||
function is_valid_utf8($text) {
|
||||
return mb_check_encoding($text, 'utf-8');
|
||||
}
|
||||
|
||||
header('Access-Control-Allow-Origin: *', true); // Come fetch!
|
||||
|
||||
/* Connect to database */
|
||||
$options = array(
|
||||
'debug' => 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'])) {
|
||||
|
||||
@@ -27,4 +27,6 @@ function getDSN($host = "default") {
|
||||
|
||||
define("USER_API_KEY", "");
|
||||
|
||||
define("USER_API_KEY_KASSA", "");
|
||||
|
||||
?>
|
||||
Reference in new issue
Block a user