Refine the cleaning of mobile number. #658

This commit is contained in:
Korina Cordero 2022-04-12 09:53:01 +00:00
parent 466d6c124f
commit a6add363bd

View file

@ -36,56 +36,61 @@ class RisingTideGateway
// at this point, assume that mobile is numeric and valid mobile number // at this point, assume that mobile is numeric and valid mobile number
$clean_num = $this->cleanPhoneNumber($mobile_num); $clean_num = $this->cleanPhoneNumber($mobile_num);
$headers = [ if ($clean_num != '')
'Content-Type: application/vnd.net.wyrls.Document-v3+json' {
]; $headers = [
'Content-Type: application/vnd.net.wyrls.Document-v3+json'
];
$sms = new SMSMessage(); $sms = new SMSMessage();
$sms->setFrom($this->shortcode) $sms->setFrom($this->shortcode)
->setFromAlias($mask) ->setFromAlias($mask)
->setTo($clean_num) ->setTo($clean_num)
->setMessage($message) ->setMessage($message)
->setStatus('sent'); ->setStatus('sent');
$this->em->persist($sms); $this->em->persist($sms);
$this->em->flush(); $this->em->flush();
$date = $sms->getDateCreate(); $date = $sms->getDateCreate();
// $date = new DateTime(); // $date = new DateTime();
$date_string = $date->format('Y-m-d') . 'T' . $date->format('H:m:s'); $date_string = $date->format('Y-m-d') . 'T' . $date->format('H:m:s');
$data = [ $data = [
'id' => $sms->getID(), 'id' => $sms->getID(),
'from' => $this->shortcode, 'from' => $this->shortcode,
'from_alias' => $mask, 'from_alias' => $mask,
'to' => $clean_num, 'to' => $clean_num,
'content_type' => 'text/plain', 'content_type' => 'text/plain',
'body' => $message, 'body' => $message,
'date' => $date_string, 'date' => $date_string,
'usagetype' => $this->usage_type, 'usagetype' => $this->usage_type,
'delivery_receipt_url' => $this->dr_url, 'delivery_receipt_url' => $this->dr_url,
]; ];
error_log(print_r($data, true)); 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 = curl_init();
curl_setopt($curl, CURLOPT_URL, self::SERVER_URL); curl_setopt($curl, CURLOPT_URL, self::SERVER_URL);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_VERBOSE, true); curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json); curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $userpwd); curl_setopt($curl, CURLOPT_USERPWD, $userpwd);
$result = curl_exec($curl); $result = curl_exec($curl);
error_log('error_no - ' . curl_errno($curl)); error_log('error_no - ' . curl_errno($curl));
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
error_log($http_code); error_log($http_code);
curl_close($curl); curl_close($curl);
error_log($result); error_log($result);
}
else
error_log('Invalid mobile number provided. Cannot send SMS message.');
} }
public function validatePhoneNumber($mobile) public function validatePhoneNumber($mobile)
@ -114,29 +119,38 @@ class RisingTideGateway
protected function cleanPhoneNumber($mobile) 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 // does it fit our 09XXXXXXXXX pattern?
if (preg_match('/^09[0-9]{9}$/', $clean_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 // remove first '0'
$stripped_num = substr($num, 1); $clean_number = substr($clean_number, 1);
$clean_num = '63' . $stripped_num;
return $clean_num; // 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 // prepend 63
$clean_num = '63' . $num; $clean_number = '63' . $clean_number;
return $clean_num;
error_log("CONVERT TO $clean_number");
return $clean_number;
} }
return $num; return "";
} }
} }