From 466d6c124f6fbac876093f06bb4b31cc6a8d1545 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 12 Apr 2022 09:30:00 +0000 Subject: [PATCH 1/5] Prepend country code to mobile numbers for SMS messaging. #658 --- src/Service/RisingTideGateway.php | 36 +++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/Service/RisingTideGateway.php b/src/Service/RisingTideGateway.php index acd7d4d5..bcbc1073 100644 --- a/src/Service/RisingTideGateway.php +++ b/src/Service/RisingTideGateway.php @@ -32,6 +32,10 @@ class RisingTideGateway public function sendSMS($mobile_num, $mask, $message) { + // make sure number is acceptable to RT + // at this point, assume that mobile is numeric and valid mobile number + $clean_num = $this->cleanPhoneNumber($mobile_num); + $headers = [ 'Content-Type: application/vnd.net.wyrls.Document-v3+json' ]; @@ -39,7 +43,7 @@ class RisingTideGateway $sms = new SMSMessage(); $sms->setFrom($this->shortcode) ->setFromAlias($mask) - ->setTo($mobile_num) + ->setTo($clean_num) ->setMessage($message) ->setStatus('sent'); @@ -54,7 +58,7 @@ class RisingTideGateway 'id' => $sms->getID(), 'from' => $this->shortcode, 'from_alias' => $mask, - 'to' => $mobile_num, + 'to' => $clean_num, 'content_type' => 'text/plain', 'body' => $message, 'date' => $date_string, @@ -107,4 +111,32 @@ class RisingTideGateway return true; } + + protected function cleanPhoneNumber($mobile) + { + $num = trim($mobile); + + // at this point, assume that mobile is numeric and valid mobile number + + // check if number begins with 63 and length is 12 for the format 639XXXXXXXXX + if ((strlen($num) == 12) && (substr($num, 0, 2) == '63')) + return $num; + + if ($num[0] == '0') + { + // remove the 0, prepend 63 + $stripped_num = substr($num, 1); + $clean_num = '63' . $stripped_num; + return $clean_num; + } + + if ($num[0] == '9') + { + // prepend 63 + $clean_num = '63' . $num; + return $clean_num; + } + + return $num; + } } From a6add363bd64fccc336a7b14ad9d784b743bf43f Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 12 Apr 2022 09:53:01 +0000 Subject: [PATCH 2/5] Refine the cleaning of mobile number. #658 --- src/Service/RisingTideGateway.php | 134 +++++++++++++++++------------- 1 file changed, 74 insertions(+), 60 deletions(-) diff --git a/src/Service/RisingTideGateway.php b/src/Service/RisingTideGateway.php index bcbc1073..2a01799c 100644 --- a/src/Service/RisingTideGateway.php +++ b/src/Service/RisingTideGateway.php @@ -36,56 +36,61 @@ class RisingTideGateway // at this point, assume that mobile is numeric and valid mobile number $clean_num = $this->cleanPhoneNumber($mobile_num); - $headers = [ - 'Content-Type: application/vnd.net.wyrls.Document-v3+json' - ]; + if ($clean_num != '') + { + $headers = [ + 'Content-Type: application/vnd.net.wyrls.Document-v3+json' + ]; - $sms = new SMSMessage(); - $sms->setFrom($this->shortcode) - ->setFromAlias($mask) - ->setTo($clean_num) - ->setMessage($message) - ->setStatus('sent'); + $sms = new SMSMessage(); + $sms->setFrom($this->shortcode) + ->setFromAlias($mask) + ->setTo($clean_num) + ->setMessage($message) + ->setStatus('sent'); - $this->em->persist($sms); - $this->em->flush(); + $this->em->persist($sms); + $this->em->flush(); - $date = $sms->getDateCreate(); - // $date = new DateTime(); - $date_string = $date->format('Y-m-d') . 'T' . $date->format('H:m:s'); + $date = $sms->getDateCreate(); + // $date = new DateTime(); + $date_string = $date->format('Y-m-d') . 'T' . $date->format('H:m:s'); - $data = [ - 'id' => $sms->getID(), - 'from' => $this->shortcode, - 'from_alias' => $mask, - 'to' => $clean_num, - 'content_type' => 'text/plain', - 'body' => $message, - 'date' => $date_string, - 'usagetype' => $this->usage_type, - 'delivery_receipt_url' => $this->dr_url, - ]; - error_log(print_r($data, true)); + $data = [ + 'id' => $sms->getID(), + 'from' => $this->shortcode, + 'from_alias' => $mask, + 'to' => $clean_num, + 'content_type' => 'text/plain', + 'body' => $message, + 'date' => $date_string, + 'usagetype' => $this->usage_type, + 'delivery_receipt_url' => $this->dr_url, + ]; + error_log(print_r($data, true)); - $data_json = json_encode($data); + $data_json = json_encode($data); - $userpwd = $this->user . ':' . $this->pass; + $userpwd = $this->user . ':' . $this->pass; - $curl = curl_init(); - curl_setopt($curl, CURLOPT_URL, self::SERVER_URL); - curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); - curl_setopt($curl, CURLOPT_VERBOSE, true); - curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json); - curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); - curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); - curl_setopt($curl, CURLOPT_USERPWD, $userpwd); - $result = curl_exec($curl); - error_log('error_no - ' . curl_errno($curl)); - $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); - error_log($http_code); - curl_close($curl); + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, self::SERVER_URL); + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); + curl_setopt($curl, CURLOPT_VERBOSE, true); + curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json); + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); + curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); + curl_setopt($curl, CURLOPT_USERPWD, $userpwd); + $result = curl_exec($curl); + error_log('error_no - ' . curl_errno($curl)); + $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); + error_log($http_code); + curl_close($curl); - error_log($result); + error_log($result); + } + else + error_log('Invalid mobile number provided. Cannot send SMS message.'); } public function validatePhoneNumber($mobile) @@ -114,29 +119,38 @@ class RisingTideGateway protected function cleanPhoneNumber($mobile) { - $num = trim($mobile); + // remove any non digit character from string + $clean_number = preg_replace('~\D~', '', $mobile); + error_log('cleaned ' . $clean_number); - // at this point, assume that mobile is numeric and valid mobile number - - // check if number begins with 63 and length is 12 for the format 639XXXXXXXXX - if ((strlen($num) == 12) && (substr($num, 0, 2) == '63')) - return $num; - - if ($num[0] == '0') + // does it fit our 09XXXXXXXXX pattern? + if (preg_match('/^09[0-9]{9}$/', $clean_number)) { - // remove the 0, prepend 63 - $stripped_num = substr($num, 1); - $clean_num = '63' . $stripped_num; - return $clean_num; - } + // remove first '0' + $clean_number = substr($clean_number, 1); + + // prepend 63 + $clean_number = '63' . $clean_number; - if ($num[0] == '9') + error_log("CONVERTED TO $clean_number"); + return $clean_number; + } + // does it fit our 63XXXXXXXXXX pattern? + else if (preg_match('/^63[0-9]{10}$/', $clean_number)) + { + // leave alone + return $clean_number; + } + // does it fit our 9XXXXXXXXX pattern? + else if (preg_match('/^9[0-9]{9}$/', $clean_number)) { // prepend 63 - $clean_num = '63' . $num; - return $clean_num; + $clean_number = '63' . $clean_number; + + error_log("CONVERT TO $clean_number"); + return $clean_number; } - - return $num; + + return ""; } } From 6fff162c0e68f3a23d56b96fef531caa97bc04f0 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 12 Apr 2022 09:59:35 +0000 Subject: [PATCH 3/5] Improve error message and return value. #658 --- src/Service/RisingTideGateway.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Service/RisingTideGateway.php b/src/Service/RisingTideGateway.php index 2a01799c..0a4ba1aa 100644 --- a/src/Service/RisingTideGateway.php +++ b/src/Service/RisingTideGateway.php @@ -36,7 +36,9 @@ class RisingTideGateway // at this point, assume that mobile is numeric and valid mobile number $clean_num = $this->cleanPhoneNumber($mobile_num); - if ($clean_num != '') + if ($clean_num == false) + error_log('Invalid mobile number provided. Cannot send SMS message to ' . $mobile_num); + else { $headers = [ 'Content-Type: application/vnd.net.wyrls.Document-v3+json' @@ -89,8 +91,6 @@ class RisingTideGateway error_log($result); } - else - error_log('Invalid mobile number provided. Cannot send SMS message.'); } public function validatePhoneNumber($mobile) @@ -151,6 +151,6 @@ class RisingTideGateway return $clean_number; } - return ""; + return false; } } From b712b35a971639f86c339d1cc433e5357c89289e Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 12 Apr 2022 10:01:14 +0000 Subject: [PATCH 4/5] Fix checking for return. #658 --- src/Service/RisingTideGateway.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Service/RisingTideGateway.php b/src/Service/RisingTideGateway.php index 0a4ba1aa..5928e72e 100644 --- a/src/Service/RisingTideGateway.php +++ b/src/Service/RisingTideGateway.php @@ -36,7 +36,7 @@ class RisingTideGateway // at this point, assume that mobile is numeric and valid mobile number $clean_num = $this->cleanPhoneNumber($mobile_num); - if ($clean_num == false) + if ($clean_num === false) error_log('Invalid mobile number provided. Cannot send SMS message to ' . $mobile_num); else { From 9bc1f44ca2323788e40b6b74870d2b60b0a6454b Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 12 Apr 2022 10:08:13 +0000 Subject: [PATCH 5/5] Clean up code. #658 --- src/Service/RisingTideGateway.php | 105 +++++++++++++++--------------- 1 file changed, 53 insertions(+), 52 deletions(-) diff --git a/src/Service/RisingTideGateway.php b/src/Service/RisingTideGateway.php index 5928e72e..0681edd4 100644 --- a/src/Service/RisingTideGateway.php +++ b/src/Service/RisingTideGateway.php @@ -37,60 +37,61 @@ class RisingTideGateway $clean_num = $this->cleanPhoneNumber($mobile_num); if ($clean_num === false) - error_log('Invalid mobile number provided. Cannot send SMS message to ' . $mobile_num); - else { - $headers = [ - 'Content-Type: application/vnd.net.wyrls.Document-v3+json' - ]; - - $sms = new SMSMessage(); - $sms->setFrom($this->shortcode) - ->setFromAlias($mask) - ->setTo($clean_num) - ->setMessage($message) - ->setStatus('sent'); - - $this->em->persist($sms); - $this->em->flush(); - - $date = $sms->getDateCreate(); - // $date = new DateTime(); - $date_string = $date->format('Y-m-d') . 'T' . $date->format('H:m:s'); - - $data = [ - 'id' => $sms->getID(), - 'from' => $this->shortcode, - 'from_alias' => $mask, - 'to' => $clean_num, - 'content_type' => 'text/plain', - 'body' => $message, - 'date' => $date_string, - 'usagetype' => $this->usage_type, - 'delivery_receipt_url' => $this->dr_url, - ]; - error_log(print_r($data, true)); - - $data_json = json_encode($data); - - $userpwd = $this->user . ':' . $this->pass; - - $curl = curl_init(); - curl_setopt($curl, CURLOPT_URL, self::SERVER_URL); - curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); - curl_setopt($curl, CURLOPT_VERBOSE, true); - curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json); - curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); - curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); - curl_setopt($curl, CURLOPT_USERPWD, $userpwd); - $result = curl_exec($curl); - error_log('error_no - ' . curl_errno($curl)); - $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); - error_log($http_code); - curl_close($curl); - - error_log($result); + error_log('Invalid mobile number provided. Cannot send SMS message to ' . $mobile_num); + return; } + + $headers = [ + 'Content-Type: application/vnd.net.wyrls.Document-v3+json' + ]; + + $sms = new SMSMessage(); + $sms->setFrom($this->shortcode) + ->setFromAlias($mask) + ->setTo($clean_num) + ->setMessage($message) + ->setStatus('sent'); + + $this->em->persist($sms); + $this->em->flush(); + + $date = $sms->getDateCreate(); + // $date = new DateTime(); + $date_string = $date->format('Y-m-d') . 'T' . $date->format('H:m:s'); + + $data = [ + 'id' => $sms->getID(), + 'from' => $this->shortcode, + 'from_alias' => $mask, + 'to' => $clean_num, + 'content_type' => 'text/plain', + 'body' => $message, + 'date' => $date_string, + 'usagetype' => $this->usage_type, + 'delivery_receipt_url' => $this->dr_url, + ]; + error_log(print_r($data, true)); + + $data_json = json_encode($data); + + $userpwd = $this->user . ':' . $this->pass; + + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, self::SERVER_URL); + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); + curl_setopt($curl, CURLOPT_VERBOSE, true); + curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json); + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); + curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); + curl_setopt($curl, CURLOPT_USERPWD, $userpwd); + $result = curl_exec($curl); + error_log('error_no - ' . curl_errno($curl)); + $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); + error_log($http_code); + curl_close($curl); + + error_log($result); } public function validatePhoneNumber($mobile)