Skip to content

Commit

Permalink
refactoring of smsapi functions (remove global variables) (#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Coutadeur committed Apr 18, 2024
1 parent 97a548f commit 4f61e49
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 156 deletions.
7 changes: 5 additions & 2 deletions htdocs/sendsms.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#
#==============================================================================

include_once(dirname(__FILE__) . "/../lib/smsapi.php");

# This page is called to send random generated password to user by SMS

#==============================================================================
Expand Down Expand Up @@ -237,10 +239,11 @@
$result = "smsnotsent";
error_log('No API library found, set $sms_api_lib in configuration.');
} else {
include_once("../".$sms_api_lib);
$sms_message = str_replace('{smsresetmessage}', $messages['smsresetmessage'], $sms_message);
$sms_message = str_replace('{smstoken}', $smstoken, $sms_message);
if (send_sms_by_api($sms, $sms_message)) {
$definedVariables = get_defined_vars(); // get all variables, including configuration
$smsInstance = createSMSInstance($sms_api_lib, $definedVariables);
if ($smsInstance->send_sms_by_api($sms, $sms_message)) {
$token = encrypt(session_id(), $keyphrase);
$result = "smssent";
if ( !empty($reset_request_log) ) {
Expand Down
39 changes: 26 additions & 13 deletions lib/smsapi-example.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,33 @@
#
#==============================================================================

/* @function boolean send_sms_by_api(string $mobile, string $message)
* Send SMS trough an API
* @param mobile mobile number
* @param message text to send
* @return 1 if message sent, 0 if not
*/
function send_sms_by_api($mobile, $message) {
class smsExample
{

# PHP code
# ...
public $example_param1;
public $example_param2;

# Or call to external script
# $command = escapeshellcmd(/path/to/script).' '.escapeshellarg($mobile).' '.escapeshellarg($message);
# exec($command);
public function __construct($example_param1, $example_param2)
{
$this->example_param1 = $example_param1;
$this->example_param2 = $example_param2;
}

return 1;
/* @function boolean send_sms_by_api(string $mobile, string $message)
* Send SMS trough an API
* @param mobile mobile number
* @param message text to send
* @return 1 if message sent, 0 if not
*/
function send_sms_by_api($mobile, $message) {

# PHP code
# ...

# Or call to external script
# $command = escapeshellcmd(/path/to/script).' '.escapeshellarg($mobile).' '.escapeshellarg($message);
# exec($command);

return 1;
}
}
62 changes: 38 additions & 24 deletions lib/smsapi-signal-cli.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,49 @@
* $signal_cli = '<path to signal-cli>';
*/

/* @function boolean send_sms_by_api(string $mobile, string $message)
* Send SMS trough an API
* @param mobile mobile number
* @param message text to send
* @return 1 if message sent, 0 if not
*/
function send_sms_by_api($mobile, $message) {
global $signal_user, $signal_config, $signal_cli;
if (!$signal_user || !$signal_config || !$signal_cli) {
error_log('Trying to access signal without credentials. Set signal_user, signal_config and signal_cli in your config.inc.local.php');
return 0;
class smsSignal
{

public $signal_user;
public $signal_config;
public $signal_cli;

public function __construct($signal_user, $signal_config, $signal_cli)
{
$this->signal_user = $signal_user;
$this->signal_config = $signal_config;
$this->signal_cli = $signal_cli;
}

$command = escapeshellcmd($signal_cli).' -u '.escapeshellarg($signal_user).' --config '.escapeshellarg($signal_config).' send -m '.escapeshellarg($message).' '.escapeshellarg($mobile);
/* @function boolean send_sms_by_api(string $mobile, string $message)
* Send SMS trough an API
* @param mobile mobile number
* @param message text to send
* @return 1 if message sent, 0 if not
*/
function send_sms_by_api($mobile, $message) {
if (!$this->signal_user || !$this->signal_config || !$this->signal_cli) {
error_log('Trying to access signal without credentials. Set signal_user, signal_config and signal_cli in your config.inc.local.php');
return 0;
}

$v = '';
$o = '';
exec($command." 2>&1", $o, $v);
$command = escapeshellcmd($this->signal_cli).' -u '.escapeshellarg($this->signal_user).' --config '.escapeshellarg($this->signal_config).' send -m '.escapeshellarg($message).' '.escapeshellarg($mobile);

if ($v !== 0) {
error_log('Error sending message: ');
$o_size = count($o);
for ($x = 0; $x < $o_size; $x++) {
error_log(' ' . $o[$x]);
}
$v = '';
$o = '';
exec($command." 2>&1", $o, $v);

return 0;
if ($v !== 0) {
error_log('Error sending message: ');
$o_size = count($o);
for ($x = 0; $x < $o_size; $x++) {
error_log(' ' . $o[$x]);
}

return 0;

}
return 1;
}
return 1;
}

}
136 changes: 76 additions & 60 deletions lib/smsapi-twilio.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,70 +32,86 @@
* $twilio_lookup_first = true / false
*/

/* @function boolean send_sms_by_api(string $mobile, string $message)
* Send SMS trough an API
* @param mobile mobile number
* @param message text to send
* @return 1 if message sent, 0 if not
*/
function send_sms_by_api($mobile, $message) {
global $twilio_sid, $twilio_auth_token, $twilio_outgoing_number, $twilio_lookup_first;
if (!$twilio_sid || !$twilio_auth_token) {
error_log('Trying to access twilio without credentials. Set twilio_sid and twilio_auth_token in your config.inc.local.php with values from https://www.twilio.com/console');
return 0;
}
class smsTwilio
{

public $twilio_sid;
public $twilio_auth_token;
public $twilio_outgoing_number;
public $twilio_lookup_first;

if (!$twilio_outgoing_number) {
error_log('No outgoing twilio number, set twilio_outgoing_number in config.inc.local.php with values from https://www.twilio.com/console/phone-numbers/search');
return 0;
public function __construct($twilio_sid, $twilio_auth_token, $twilio_outgoing_number, $twilio_lookup_first)
{
$this->twilio_sid = $twilio_sid;
$this->twilio_auth_token = $twilio_auth_token;
$this->twilio_outgoing_number = $twilio_outgoing_number;
$this->twilio_lookup_first = $twilio_lookup_first;
}

if ($twilio_lookup_first) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://lookups.twilio.com/v1/PhoneNumbers/' . rawurlencode($mobile));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $twilio_sid . ":" . $twilio_auth_token);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close($ch);
if ($http_code != "200") {
error_log("Error code $http_code from twilio: $response");
return 0;
}
$json = json_decode($response, true);
if (@$json['code'] || @$json['message']) {
error_log("Error from twilio: $response");
return 0;
}

$mobile = $json['phone_number'];
}
/* @function boolean send_sms_by_api(string $mobile, string $message)
* Send SMS trough an API
* @param mobile mobile number
* @param message text to send
* @return 1 if message sent, 0 if not
*/
function send_sms_by_api($mobile, $message) {
if (!$this->twilio_sid || !$this->twilio_auth_token) {
error_log('Trying to access twilio without credentials. Set twilio_sid and twilio_auth_token in your config.inc.local.php with values from https://www.twilio.com/console');
return 0;
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.twilio.com/2010-04-01/Accounts/' . rawurlencode($twilio_sid) . '/Messages.json');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $twilio_sid . ":" . $twilio_auth_token);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
'Body' => $message,
'From' => $twilio_outgoing_number,
'To' => $mobile
)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close($ch);
if ($http_code != "200" && $http_code != "201") {
error_log("Error code $http_code from twilio: $response");
return 0;
}
$json = json_decode($response, true);
if (@$json['code'] || @$json['message']) {
error_log("Error from twilio: $response");
return 0;
}
if (!$this->twilio_outgoing_number) {
error_log('No outgoing twilio number, set twilio_outgoing_number in config.inc.local.php with values from https://www.twilio.com/console/phone-numbers/search');
return 0;
}

return 1;
}
if ($this->twilio_lookup_first) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://lookups.twilio.com/v1/PhoneNumbers/' . rawurlencode($mobile));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $this->twilio_sid . ":" . $this->twilio_auth_token);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close($ch);
if ($http_code != "200") {
error_log("Error code $http_code from twilio: $response");
return 0;
}
$json = json_decode($response, true);
if (@$json['code'] || @$json['message']) {
error_log("Error from twilio: $response");
return 0;
}

$mobile = $json['phone_number'];
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.twilio.com/2010-04-01/Accounts/' . rawurlencode($this->twilio_sid) . '/Messages.json');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $this->twilio_sid . ":" . $this->twilio_auth_token);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
'Body' => $message,
'From' => $this->twilio_outgoing_number,
'To' => $mobile
)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close($ch);
if ($http_code != "200" && $http_code != "201") {
error_log("Error code $http_code from twilio: $response");
return 0;
}
$json = json_decode($response, true);
if (@$json['code'] || @$json['message']) {
error_log("Error from twilio: $response");
return 0;
}

return 1;
}
}
38 changes: 38 additions & 0 deletions lib/smsapi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

function createSMSInstance($sms_api_lib, $sms_config)
{
if(preg_match('/ovh/', $sms_api_lib))
{
include_once(dirname(__FILE__) . "/smsovh/smsapi-ovh.inc.php");

return new smsOVH($sms_config['ovh_appkey'],
$sms_config['ovh_appsecret'],
$sms_config['ovh_consumerkey'],
$sms_config['ovh_smssender']);
}
elseif(preg_match('/signal/', $sms_api_lib))
{
include_once(dirname(__FILE__) . "/smsapi-signal-cli.inc.php");

return new smsSignal($sms_config['signal_user'],
$sms_config['signal_config'],
$sms_config['signal_cli']);
}
elseif(preg_match('/twilio/', $sms_api_lib))
{
include_once(dirname(__FILE__) . "/smsapi-twilio.inc.php");

return new smsTwilio($sms_config['twilio_sid'],
$sms_config['twilio_auth_token'],
$sms_config['twilio_outgoing_number'],
$sms_config['twilio_lookup_first']);
}
else
{
include_once($sms_api_lib);
//TODO
}
}

?>
Loading

0 comments on commit 4f61e49

Please sign in to comment.