raspisms/adapters/OctopushVirtualNumberAdapte...

433 lines
12 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of RaspiSMS.
*
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
*
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/
namespace adapters;
/**
2020-06-23 21:06:13 +02:00
* Octopush SMS service with a shortcode adapter.
*/
class OctopushVirtualNumberAdapter implements AdapterInterface
{
const ERROR_CODE_OK = '000';
const SMS_TYPE_LOWCOST = 'XXX';
const SMS_TYPE_PREMIUM = 'FR';
const SMS_TYPE_INTERNATIONAL = 'WWW';
/**
* Datas used to configure interaction with the implemented service. (e.g : Api credentials, ports numbers, etc.).
*/
private $datas;
/**
2020-06-23 21:06:13 +02:00
* Octopush login.
*/
private $login;
/**
2020-06-23 21:06:13 +02:00
* Octopush api key.
*/
private $api_key;
/**
2020-06-23 21:06:13 +02:00
* Number phone to use.
*/
private $number;
/**
2020-06-23 21:06:13 +02:00
* Number phone to use formated for octopush compatibility.
*/
private $formatted_number;
/**
2020-06-23 21:06:13 +02:00
* Octopush api baseurl.
*/
private $api_url = 'https://www.octopush-dm.com/api';
2020-06-23 21:06:13 +02:00
/**
* Adapter constructor, called when instanciated by RaspiSMS.
*
* @param string $number : Phone number the adapter is used for
* @param json string $datas : JSON string of the datas to configure interaction with the implemented service
*/
public function __construct(string $datas)
{
$this->datas = json_decode($datas, true);
$this->login = $this->datas['login'];
$this->api_key = $this->datas['api_key'];
$this->number = $this->datas['number'];
$this->formatted_number = '+' . mb_substr($this->datas['number'], 2);
}
/**
* Classname of the adapter.
*/
public static function meta_classname(): string
{
return __CLASS__;
}
/**
* Uniq name of the adapter
2020-06-23 21:06:13 +02:00
* It should be the classname of the adapter un snakecase.
*/
2020-06-23 21:06:13 +02:00
public static function meta_uid(): string
{
return 'octopush_virtual_number_adapter';
}
/**
* Name of the adapter.
* It should probably be the name of the service it adapt (e.g : Gammu SMSD, OVH SMS, SIM800L, etc.).
*/
public static function meta_name(): string
{
return 'Octopush Numéro virtuel';
}
/**
* Description of the adapter.
* A short description of the service the adapter implements.
*/
public static function meta_description(): string
{
$credentials_url = 'https://www.octopush-dm.com/api-logins';
2020-06-23 21:06:13 +02:00
return '
2020-07-04 21:35:39 +02:00
Envoi de SMS avec un numéro virtuel en utilisant <a target="_blank" href="https://www.octopush.com/">Octopush</a>. Pour trouver vos clés API Octopush <a target="_blank" href="' . $credentials_url . '">cliquez ici.</a><br/>
Pour plus d\'information sur l\'utilisation de l\'adaptateur, reportez-vous à <a href="https://documentation.raspisms.fr/users/adapters/octopush_virtual_number.html" target="_blank">la documentation sur l\'adaptateur Octopush Numéro Virtuel.</a>
';
}
/**
* List of entries we want in datas for the adapter.
*
* @return array : Every line is a field as an array with keys : name, title, description, required
*/
public static function meta_datas_fields(): array
{
return [
[
'name' => 'login',
'title' => 'Octopush Login',
'description' => 'Login du compte Octopush à employer. Trouvable sur la page des identifiants API Octopush.',
'required' => true,
],
[
'name' => 'api_key',
'title' => 'API Key',
'description' => 'Clef API octopush. Trouvable sur la page des identifiants API Octopush.',
'required' => true,
],
[
'name' => 'number',
'title' => 'Numéro de téléphone virtuel',
'description' => 'Numéro de téléphone virtuel Octopush à utiliser.',
'required' => true,
'number' => true,
],
];
}
/**
* Does the implemented service support reading smss.
*/
public static function meta_support_read(): bool
{
return false;
}
/**
* Does the implemented service support flash smss.
*/
public static function meta_support_flash(): bool
{
return false;
}
/**
* Does the implemented service support status change.
*/
public static function meta_support_status_change(): bool
{
return true;
}
/**
* Does the implemented service support reception callback.
*/
public static function meta_support_reception(): bool
{
return true;
}
/**
* Method called to send a SMS to a number.
*
* @param string $destination : Phone number to send the sms to
* @param string $text : Text of the SMS to send
* @param bool $flash : Is the SMS a Flash SMS
*
* @return array : [
2020-06-23 21:06:13 +02:00
* bool 'error' => false if no error, true else
* ?string 'error_message' => null if no error, else error message
* array 'uid' => Uid of the sms created on success
* ]
*/
public function send(string $destination, string $text, bool $flash = false)
{
$response = [
'error' => false,
'error_message' => null,
'uid' => null,
];
try
{
$datas = [
'user_login' => $this->login,
'api_key' => $this->api_key,
'sms_text' => $text,
'sms_recipients' => str_replace('+', '00', $destination), //Must use 00 instead of + notation
'sms_sender' => $this->formatted_number,
'sms_type' => self::SMS_TYPE_LOWCOST,
];
$endpoint = $this->api_url . '/sms/json';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $endpoint);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $datas);
$response = curl_exec($curl);
curl_close($curl);
2020-06-23 21:06:13 +02:00
if (false === $response)
{
$response['error'] = true;
$response['error_message'] = 'HTTP query failed.';
2020-06-23 21:06:13 +02:00
return $response;
}
$response_decode = json_decode($response, true);
2020-06-23 21:06:13 +02:00
if (null === $response_decode)
{
$response['error'] = true;
$response['error_message'] = 'Invalid JSON for response.';
2020-06-23 21:06:13 +02:00
return $response;
}
2020-06-23 21:06:13 +02:00
if (self::ERROR_CODE_OK !== $response_decode['error_code'])
{
$response['error'] = true;
$response['error_message'] = 'Response indicate error code : ' . $response_decode['error_code'];
2020-06-23 21:06:13 +02:00
return $response;
}
$uid = $response_decode['ticket'] ?? false;
if (!$uid)
{
$response['error'] = true;
$response['error_message'] = 'Cannot extract SMS uid';
2020-06-23 21:06:13 +02:00
return $response;
}
$response['uid'] = $uid;
2020-06-23 21:06:13 +02:00
return $response;
}
catch (\Throwable $t)
{
$response['error'] = true;
$response['error_message'] = $t->getMessage();
2020-06-23 21:06:13 +02:00
return $response;
}
}
/**
* Method called to read SMSs of the number.
*
* @return array : [
2020-06-23 21:06:13 +02:00
* bool 'error' => false if no error, true else
* ?string 'error_message' => null if no error, else error message
* array 'sms' => Array of the sms reads
* ]
*/
public function read(): array
{
return [];
}
/**
* Method called to verify if the adapter is working correctly
* should be use for exemple to verify that credentials and number are both valid.
*
* @return bool : False on error, true else
*/
public function test(): bool
{
try
{
$success = true;
if ($this->datas['sender'] && (mb_strlen($this->datas['sender']) < 3 || mb_strlen($this->datas['sender'] > 11)))
{
return false;
}
2020-06-23 21:06:13 +02:00
$datas = [
'user_login' => $this->login,
'api_key' => $this->api_key,
];
//Check service name
$endpoint = $this->api_url . '/balance/json';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $endpoint);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $datas);
$response = curl_exec($curl);
curl_close($curl);
2020-06-23 21:06:13 +02:00
if (false === $response)
{
return false;
}
$response_decode = json_decode($response, true);
2020-06-23 21:06:13 +02:00
if (null === $response_decode)
{
return false;
}
2020-06-23 21:06:13 +02:00
if (self::ERROR_CODE_OK !== $response_decode['error_code'])
{
return false;
}
return true;
}
catch (\Throwable $t)
{
return false;
}
}
/**
* Method called on reception of a status update notification for a SMS.
*
* @return mixed : False on error, else array ['uid' => uid of the sms, 'status' => New status of the sms (\models\Sended::STATUS_UNKNOWN, \models\Sended::STATUS_DELIVERED, \models\Sended::STATUS_FAILED)]
*/
public static function status_change_callback()
{
header('Connection: close');
header('Content-Encoding: none');
header('Content-Length: 0');
$uid = $_POST['message_id'] ?? false;
$status = $_POST['status'] ?? false;
2020-06-23 21:06:13 +02:00
if (false === $uid || false === $status)
{
return false;
}
switch ($status)
{
case 'DELIVERED':
$status = \models\Sended::STATUS_DELIVERED;
2020-06-23 21:06:13 +02:00
break;
2021-01-14 03:32:17 +01:00
case 'NOT_ALLOWED':
case 'INVALID_DESTINATION_ADDRESS':
case 'OUT_OF_DATE':
case 'EXPIRED':
case 'BLACKLISTED_NUMBER':
$status = \models\Sended::STATUS_FAILED;
2020-06-23 21:06:13 +02:00
break;
2021-01-14 03:32:17 +01:00
2020-06-23 21:06:13 +02:00
default:
$status = \models\Sended::STATUS_UNKNOWN;
2020-06-23 21:06:13 +02:00
break;
}
return ['uid' => $uid, 'status' => $status];
}
/**
* Method called on reception of a sms notification.
*
* @return array : [
2020-06-23 21:06:13 +02:00
* bool 'error' => false on success, true on error
* ?string 'error_message' => null on success, error message else
* array 'sms' => array [
* string 'at' : Recepetion date format Y-m-d H:i:s,
* string 'text' : SMS body,
* string 'origin' : SMS sender,
* ]
*
* ]
*/
2020-06-23 21:06:13 +02:00
public static function reception_callback(): array
{
$response = [
'error' => false,
'error_message' => null,
'uid' => null,
];
header('Connection: close');
header('Content-Encoding: none');
header('Content-Length: 0');
$number = $_POST['number'] ?? false;
$text = $_POST['text'] ?? false;
$at = $_POST['reception_date'] ?? false;
if (!$number || !$text || !$at)
{
$response['error'] = true;
$response['error_message'] = 'One required data of the callback is missing.';
2020-06-23 21:06:13 +02:00
return $response;
}
$origin = \controllers\internals\Tool::parse_phone('+' . mb_substr($number, 2));
if (!$origin)
{
$response['error'] = true;
$response['error_message'] = 'Invalid origin number : ' . mb_substr($number, 2);
2020-06-23 21:06:13 +02:00
return $response;
}
$response['sms'] = [
'at' => $at,
'text' => $text,
'origin' => $origin,
];
2020-06-23 21:06:13 +02:00
return $response;
}
}