Take phonenumbers with + and then a language code (partial)
This commit is contained in:
1 parent
f04f53be03
commit
56d6fc176d
2 files changed
+15
-2
No files matched your search
@@ -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));
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user