From 56d6fc176d2f1707c4c0ec1ade725ccc1d84749a Mon Sep 17 00:00:00 2001 From: Nikolai Kristiansen Date: Thu, 22 May 2014 22:44:02 +0200 Subject: [PATCH] Take phonenumbers with + and then a language code (partial) --- snapporder/api/query.php | 4 +++- snapporder/lib/functions.php | 13 ++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/snapporder/api/query.php b/snapporder/api/query.php index 532261b..b96616f 100644 --- a/snapporder/api/query.php +++ b/snapporder/api/query.php @@ -4,7 +4,7 @@ * * Request: * - * $ curl /snapporder/api/query.php?phone=48105885 + * $ curl /snapporder/api/query.php?phone=%2B4748105885 # %2B is a + * * Response: * @@ -39,6 +39,8 @@ if(!isset($_GET['phone'])) { die(); } $phone = $_GET['phone']; +$phone = clean_phonenumber($phone); +// todo you are here if( !valid_phonenumber($phone) ) { set_response_code(400); echo json_encode(array('error' => 'Not a phone number', 'query' => $_GET)); diff --git a/snapporder/lib/functions.php b/snapporder/lib/functions.php index fa49aa8..c05a6c2 100644 --- a/snapporder/lib/functions.php +++ b/snapporder/lib/functions.php @@ -68,8 +68,19 @@ function get_user($user_id) { return $user; } +function clean_phonenumber($pn) { + var_dump($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[1] === "9") ) { + $pn = "+47".$pn; + } + return $pn; +} +// E.164 function valid_phonenumber($phone) { - return is_numeric($phone) && strlen($phone) === 8; + return preg_match('/^\+?\d{8,15}$/i', $phone); } function valid_email($email) { return preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email);