we need the post file for the ajax call to send the user the token. This is using twilio SMS API, you will need your own account to use it and don’t forget to add it to your composer packages…
you can create this without going through phpmaker. the code below is created via phpmaker, so don’t forget to give the users permissions to run the script. Also, note that the some functions have been removed as they are not needed for the ajax post or will interfere with it. Pay attention to the “remmed out” phpmaker code
remove/rem out:
// Page_Rendering();
<?php // include_once $RELATIVE_PATH . "header.php"; ?>
<?php // if (Config("DEBUG")) echo GetDebugMessage(); ?>
<?php // include_once $RELATIVE_PATH . "footer.php"; ?>
<?php //$_sendSMSMessage->terminate(); ?>
in the custom code section:
<?php
use \Twilio\Rest\Client as twiSMS;
$authenticator = new \PHPGangsta_GoogleAuthenticator();
$sid = _getPreferences("TWILIO_SMS_SID");
$token = _getPreferences("TWILIO_SMS_TOKEN");
$Mobile_No = _getUserInfo("CellularPhone", CurrentUserID());
$secret = _getPreferences("2FA_SECRET");
$oneCode = $authenticator->getCode($secret); // get the verification token without using authentication app
$msg = "Please enter " .$oneCode. " withing 1 minute to confirm your session.";
if($Mobile_No == '') {
WriteAuditTrail("log", StdCurrentDateTime(), ScriptName(), CurrentUserID(), 'SMS', CurrentUserIP(), $Mobile_No, 'Invalid Mobile Number', '', 'SMS Send');
echo "No celluar number found on file. Unable to send verification token" ;
exit();
}
$msg = RemoveHtml($msg);
try {
$client = new twiSMS($sid, $token); // Twilio\Rest\Client($sid, $token);
WriteAuditTrail("log", StdCurrentDateTime(), ScriptName(), CurrentUserID(), 'SMS', 'client->create', $client, $sid, $token, 'Completed');
}
catch(Exception $e) {
WriteAuditTrail("log", StdCurrentDateTime(), ScriptName(), CurrentUserID(), 'SMS', 'client->create', $e, $sid, $token, 'Failed');
echo "$e";
exit();
}
try {
$message = $client->messages->create(
$Mobile_No, // Text this number
array(
'from' => _getPreferences("TWILIO_SMS_NUMBER"), // From a valid Twilio number
'body' => $msg
)
);
WriteAuditTrail("log", StdCurrentDateTime(), ScriptName(), CurrentUserID(), 'SMS', CurrentUserIP(), $Mobile_No, $msg, $message, 'Completed');
}
catch(Exception $e) {
WriteAuditTrail("log", StdCurrentDateTime(), ScriptName(), CurrentUserID(), 'SMS', 'messages->create', $e, $Mobile_No, $msg, 'Failed');
echo "Failed To Send Token
". "$e";
exit();
}
echo "Token Sent.";
?>
Notes:
remove any calls to WriteAuditTrail if you are not interested in tracking the progress.
_getPreferences(), _getUserInfo() are custom functions we use, replace with your static data or method of retrieving the data that is needed.