From 32fcb8b0e80c5ab13f2ac131bf3e731ead7dea02 Mon Sep 17 00:00:00 2001 From: scheibling Date: Mon, 21 Jun 2021 22:49:40 +0200 Subject: [PATCH] Added Tele2 API (#1130) * Added Tele2 API * Fixed mistake, added to readme, added number formatting Co-authored-by: larsec Co-authored-by: Lars Scheibling Co-authored-by: Tim Zandbergen --- README.rst | 1 + src/includes/functions.inc.php | 3 + src/psm/Txtmsg/Tele2.php | 143 +++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 src/psm/Txtmsg/Tele2.php diff --git a/README.rst b/README.rst index 4f96b85f..ad00a393 100644 --- a/README.rst +++ b/README.rst @@ -67,6 +67,7 @@ The following SMS gateways are currently available: * PromoSMS - * Infobip - * LabsMobile - +* Tele2 Messaging - Please note: for these gateways you will need an account with sufficient credits. diff --git a/src/includes/functions.inc.php b/src/includes/functions.inc.php index bff7dc6b..85e11b3c 100644 --- a/src/includes/functions.inc.php +++ b/src/includes/functions.inc.php @@ -810,6 +810,9 @@ namespace { case 'labsmobile': $sms = new \psm\Txtmsg\LabsMobile(); break; + case 'tele2': + $sms = new \psm\Txtmsg\Tele2(); + break; } // copy login information from the config file diff --git a/src/psm/Txtmsg/Tele2.php b/src/psm/Txtmsg/Tele2.php new file mode 100644 index 00000000..44f5aca0 --- /dev/null +++ b/src/psm/Txtmsg/Tele2.php @@ -0,0 +1,143 @@ +. + * + * @package phpservermon + * @author Victor Macko + * @copyright Copyright (c) 2008-2017 Pepijn Over + * @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3 + * @version Release: @package_version@ + * @link http://www.phpservermonitor.org/ + * @since phpservermon 3.6.0 + **/ + +namespace psm\Txtmsg; + +class Tele2 extends Core +{ + /** + * Formats the number to e.g. 45701234567 instead of +45701234567/00451234567 + * Error if the number begins with a single 0, indicates no country code has been provided. + * Will still attempt to send to this and other numbers, but return an error message. + * Also remove spaces, braces and other special characters + */ + private function formatNumber( $number ) : string + { + $number = str_replace(['-', ' ', '(', ')'], '', $number); + + if (substr($number, 0, 1) === '+') { + return substr($number, 1); + } + elseif (substr($number, 0, 2) === '00') { + return substr($number, 2); + } + elseif (substr($number, 0, 1) === '0') { + return null; + } + else return $number; + + } + + + /** + * Send sms using the Tele2 Messaging API based on Infobip + * The username can be blank, password is the API key + * + * + * @var string $message + * @var string $this->baseurl + * @var string $this->password + * @var array $this->recipients + * @var array $this->originator + * @var string $recipients + * + * @var resource $curl + * @var string $err + * @var mixed $result + * + * @var int $success + * @var string $error + * + * @return bool|string + */ + + + + public function sendSMS($message) + { + $success = 1; + $error = ''; + + /** + * Creates a curl object, loops through participants to add them to the same message and makes a single API call to send to all + */ + $ch = curl_init("https://api.tele2messaging.com/sms/2/text/advanced"); + + + + $recipients = []; + + foreach ($this->recipients as $recipient) { + $format = $this->formatNumber($recipient); + if (!$format) { + $error = "ERROR: Incorrect format, needs to include country code (e.g. 45123456789 instead of 0123456789/450123456789/+45123456789/0045123456789)"; + } + $recipients[] = [ + 'to' => $format ?? $recipient + ]; + } + + $postfields = [ + 'messages' => [ + [ + 'from' => $this->originator, + 'destinations' => $recipients, + 'text' => "$message" + ] + ] + ]; + + curl_setopt_array($ch, [ + CURLOPT_POST => 1, + CURLOPT_RETURNTRANSFER => false, + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_POSTFIELDS => json_encode($postfields), + CURLOPT_HTTPHEADER => [ + 'AUTHORIZATION: App '.$this->password, + 'Content-Type: application/json', + 'Accept: application/json' + ], + ]); + + $result = curl_exec($ch); + $returncode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); + + if ($returncode !== 200 || $error !== '') { + $success = 0; + $error .= $result; + } + + return ($success === 1 ? 1 : $error); + } +} +