2019-11-12 05:18:32 +01:00
< ? php
2020-01-17 18:19:25 +01:00
/*
* 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 ;
use Ovh\Api ;
2019-11-12 05:18:32 +01:00
/**
2020-06-23 21:06:13 +02:00
* OVH SMS service with a virtual number adapter .
2019-11-12 05:18:32 +01:00
*/
2020-03-31 02:45:54 +02:00
class OvhSmsVirtualNumberAdapter implements AdapterInterface
2019-11-12 05:18:32 +01:00
{
2020-01-17 18:19:25 +01:00
/**
2021-01-17 03:16:57 +01:00
* Data used to configure interaction with the implemented service . ( e . g : Api credentials , ports numbers , etc . ) .
2020-01-17 18:19:25 +01:00
*/
2021-01-17 03:16:57 +01:00
private $data ;
2020-01-17 18:19:25 +01:00
/**
* OVH Api instance .
*/
private $api ;
2020-06-22 18:27:56 +02:00
/**
2020-06-23 21:06:13 +02:00
* Number used .
2020-06-22 18:27:56 +02:00
*/
private $number ;
2020-01-17 18:19:25 +01:00
/**
* Number formated to be compatible with http query according to the ovh way .
*/
private $formatted_number ;
/**
* Adapter constructor , called when instanciated by RaspiSMS .
*
2021-01-17 03:16:57 +01:00
* @ param json string $data : JSON string of the data to configure interaction with the implemented service
2020-01-17 18:19:25 +01:00
*/
2021-01-17 03:16:57 +01:00
public function __construct ( string $data )
2020-01-17 18:19:25 +01:00
{
2021-01-17 03:16:57 +01:00
$this -> data = json_decode ( $data , true );
2020-01-17 18:19:25 +01:00
$this -> api = new Api (
2021-01-17 03:16:57 +01:00
$this -> data [ 'app_key' ],
$this -> data [ 'app_secret' ],
2020-03-04 01:40:47 +01:00
'ovh-eu' ,
2021-01-17 03:16:57 +01:00
$this -> data [ 'consumer_key' ]
2020-01-17 18:19:25 +01:00
);
2020-06-23 21:06:13 +02:00
2021-01-17 03:16:57 +01:00
$this -> number = $this -> data [ 'number' ];
2020-03-31 01:28:32 +02:00
$this -> formatted_number = str_replace ( '+' , '00' , $this -> number );
2020-01-17 18:19:25 +01:00
}
/**
* Classname of the adapter .
2019-11-12 05:18:32 +01:00
*/
2020-01-17 18:19:25 +01:00
public static function meta_classname () : string
{
return __CLASS__ ;
}
2020-06-23 21:06:13 +02:00
2020-04-02 01:55:55 +02:00
/**
* Uniq name of the adapter
2020-06-23 21:06:13 +02:00
* It should be the classname of the adapter un snakecase .
2020-04-02 01:55:55 +02:00
*/
2020-06-23 21:06:13 +02:00
public static function meta_uid () : string
2020-04-02 01:55:55 +02:00
{
return 'ovh_sms_virtual_number_adapter' ;
}
2021-02-23 00:31:54 +01:00
2021-01-26 19:27:30 +01:00
/**
* Should this adapter be hidden in user interface for phone creation and
2021-02-23 00:31:54 +01:00
* available to creation through API only .
2021-01-26 19:27:30 +01:00
*/
public static function meta_hidden () : bool
{
return false ;
}
2019-11-12 05:18:32 +01:00
2022-03-28 01:54:38 +02:00
/**
* Should this adapter data be hidden after creation
* this help to prevent API credentials to other service leak if an attacker gain access to RaspiSMS through user credentials .
*/
public static function meta_hide_data () : bool
{
return false ;
}
2019-11-12 05:18:32 +01:00
/**
* Name of the adapter .
2020-01-17 18:19:25 +01:00
* It should probably be the name of the service it adapt ( e . g : Gammu SMSD , OVH SMS , SIM800L , etc . ) .
2019-11-12 05:18:32 +01:00
*/
2020-01-17 18:19:25 +01:00
public static function meta_name () : string
{
2020-03-31 02:45:54 +02:00
return 'OVH SMS Numéro virtuel' ;
2020-01-17 18:19:25 +01:00
}
2019-11-12 05:18:32 +01:00
/**
* Description of the adapter .
* A short description of the service the adapter implements .
*/
2020-01-17 18:19:25 +01:00
public static function meta_description () : string
{
2020-03-30 01:52:53 +02:00
$generate_credentials_url = 'https://eu.api.ovh.com/createToken/index.cgi?GET=/sms&GET=/sms/*&POST=/sms/*&PUT=/sms/*&DELETE=/sms/*&' ;
2020-03-29 20:55:47 +02:00
return '
2020-07-04 21:35:39 +02:00
Solution de SMS proposé par le groupe < a target = " _blank " href = " https://www.ovhtelecom.fr/sms/ " > OVH </ a >. Pour générer les clefs API OVH , < a target = " _blank " href = " ' . $generate_credentials_url . ' " > cliquez ici .</ a >< br />
2021-02-04 16:44:13 +01:00
Pour plus d\ ' information sur l\ ' utilisation de ce téléphone , reportez - vous à < a href = " https://documentation.raspisms.fr/users/adapters/ovh_virtual_number.html " target = " _blank " > la documentation sur le téléphone " OVH Numéro Virtuel " .</ a >
2020-03-29 20:55:47 +02:00
' ;
2020-01-17 18:19:25 +01:00
}
2020-01-09 22:23:58 +01:00
/**
2021-01-17 03:16:57 +01:00
* List of entries we want in data for the adapter .
2020-01-17 18:19:25 +01:00
*
2020-01-09 22:23:58 +01:00
* @ return array : Every line is a field as an array with keys : name , title , description , required
*/
2021-01-17 03:16:57 +01:00
public static function meta_data_fields () : array
2020-01-09 22:23:58 +01:00
{
return [
2020-03-29 20:20:07 +02:00
[
'name' => 'service_name' ,
'title' => 'Service Name' ,
'description' => 'Service Name de votre service SMS chez OVH. Il s\'agit du nom associé à votre service SMS dans la console OVH, probablement quelque chose comme "sms-xxxxx-1" ou "xxxx" est votre identifiant client OVH.' ,
'required' => true ,
],
2020-03-31 01:28:32 +02:00
[
'name' => 'number' ,
'title' => 'Numéro' ,
2020-03-31 02:45:54 +02:00
'description' => 'Numéro de téléphone virtuel chez OVH.' ,
2020-03-31 01:28:32 +02:00
'required' => true ,
2022-07-12 13:16:22 +02:00
'type' => 'phone_number' ,
2020-03-31 01:28:32 +02:00
],
2020-01-09 22:23:58 +01:00
[
'name' => 'app_key' ,
'title' => 'Application Key' ,
'description' => 'Paramètre "Application Key" obtenu lors de la génération de la clef API OVH.' ,
'required' => true ,
],
[
'name' => 'app_secret' ,
'title' => 'Application Secret' ,
'description' => 'Paramètre "Application Secret" obtenu lors de la génération de la clef API OVH.' ,
'required' => true ,
],
[
'name' => 'consumer_key' ,
'title' => 'Consumer Key' ,
'description' => 'Paramètre "Consumer Key" obtenu lors de la génération de la clef API OVH.' ,
'required' => true ,
],
];
}
2019-11-12 05:18:32 +01:00
2020-03-31 02:22:40 +02:00
/**
* Does the implemented service support reading smss .
*/
public static function meta_support_read () : bool
{
return true ;
}
2023-02-18 16:39:07 +01:00
/**
* Does the implemented service support updating phone status .
*/
public static function meta_support_phone_status () : bool
{
return false ;
}
2019-11-12 05:18:32 +01:00
/**
2020-01-17 18:19:25 +01:00
* Does the implemented service support flash smss .
2020-01-09 22:23:58 +01:00
*/
2020-01-17 18:19:25 +01:00
public static function meta_support_flash () : bool
{
return false ;
}
2020-01-09 22:23:58 +01:00
/**
2020-01-17 18:19:25 +01:00
* Does the implemented service support status change .
2019-11-12 05:18:32 +01:00
*/
2020-01-17 18:19:25 +01:00
public static function meta_support_status_change () : bool
2019-11-12 05:18:32 +01:00
{
2020-01-17 18:19:25 +01:00
return true ;
2019-11-12 05:18:32 +01:00
}
2020-01-17 18:19:25 +01:00
2020-04-03 02:15:55 +02:00
/**
* Does the implemented service support reception callback .
*/
public static function meta_support_reception () : bool
{
return false ;
}
2021-06-17 00:51:33 +02:00
2021-03-19 02:45:12 +01:00
/**
2021-06-17 00:51:33 +02:00
* Does the implemented service support mms reception .
2021-03-19 02:45:12 +01:00
*/
public static function meta_support_mms_reception () : bool
{
return false ;
}
2020-04-03 02:15:55 +02:00
2019-11-12 05:18:32 +01:00
/**
2021-06-17 00:51:33 +02:00
* Does the implemented service support mms sending .
2019-11-12 05:18:32 +01:00
*/
2021-03-19 02:45:12 +01:00
public static function meta_support_mms_sending () : bool
{
return false ;
}
2021-06-17 00:51:33 +02:00
2021-03-23 17:39:13 +01:00
public static function meta_support_inbound_call_callback () : bool
{
return false ;
}
2021-06-17 00:51:33 +02:00
2021-03-23 17:39:13 +01:00
public static function meta_support_end_call_callback () : bool
{
return false ;
}
2021-03-19 02:45:12 +01:00
2021-06-17 00:51:33 +02:00
public function send ( string $destination , string $text , bool $flash = false , bool $mms = false , array $medias = []) : array
2019-11-12 05:18:32 +01:00
{
2020-04-02 01:55:55 +02:00
$response = [
'error' => false ,
'error_message' => null ,
'uid' => null ,
];
2020-01-17 18:19:25 +01:00
try
2020-01-09 22:23:58 +01:00
{
$success = true ;
2021-01-17 03:16:57 +01:00
$endpoint = '/sms/' . $this -> data [ 'service_name' ] . '/virtualNumbers/' . $this -> formatted_number . '/jobs' ;
2020-01-09 22:23:58 +01:00
$params = [
'message' => $text ,
'receivers' => [ $destination ],
];
$response = $this -> api -> post ( $endpoint , $params );
2020-01-17 18:19:25 +01:00
$nb_invalid_receivers = \count (( $response [ 'invalidReceivers' ] ? ? []));
2020-01-09 22:23:58 +01:00
if ( $nb_invalid_receivers > 0 )
{
2020-04-02 01:55:55 +02:00
$response [ 'error' ] = true ;
$response [ 'error_message' ] = 'Invalid receiver' ;
2020-06-23 21:06:13 +02:00
2020-04-02 01:55:55 +02:00
return $response ;
2020-01-09 22:23:58 +01:00
}
2020-04-02 01:55:55 +02:00
$uid = $response [ 'ids' ][ 0 ] ? ? false ;
if ( ! $uid )
{
$response [ 'error' ] = true ;
$response [ 'error_message' ] = 'Cannot retrieve uid' ;
2020-06-23 21:06:13 +02:00
2020-04-02 01:55:55 +02:00
return $response ;
}
2020-01-17 18:19:25 +01:00
2020-04-02 01:55:55 +02:00
$response [ 'uid' ] = $uid ;
2020-06-23 21:06:13 +02:00
2020-04-02 01:55:55 +02:00
return $response ;
2020-01-09 22:23:58 +01:00
}
2020-04-02 01:55:55 +02:00
catch ( \Throwable $t )
2020-01-09 22:23:58 +01:00
{
2020-04-02 01:55:55 +02:00
$response [ 'error' ] = true ;
$response [ 'error_message' ] = $t -> getMessage ();
2020-06-23 21:06:13 +02:00
2020-04-02 01:55:55 +02:00
return $response ;
2020-01-09 22:23:58 +01:00
}
2019-11-12 05:18:32 +01:00
}
2020-01-17 18:19:25 +01:00
public function read () : array
2019-11-12 05:18:32 +01:00
{
2020-04-02 01:55:55 +02:00
$response = [
'error' => false ,
'error_message' => null ,
'smss' => [],
];
2020-01-17 18:19:25 +01:00
try
2020-01-09 22:23:58 +01:00
{
2021-01-17 03:16:57 +01:00
$endpoint = '/sms/' . $this -> data [ 'service_name' ] . '/virtualNumbers/' . $this -> formatted_number . '/incoming' ;
2020-01-09 22:23:58 +01:00
$uids = $this -> api -> get ( $endpoint );
2020-06-23 21:06:13 +02:00
if ( ! \is_array ( $uids ) || ! $uids )
2020-01-09 22:23:58 +01:00
{
2020-04-02 01:55:55 +02:00
return $response ;
2020-01-09 22:23:58 +01:00
}
foreach ( $uids as $uid )
{
2021-01-17 03:16:57 +01:00
$endpoint = '/sms/' . $this -> data [ 'service_name' ] . '/virtualNumbers/' . $this -> formatted_number . '/incoming/' . $uid ;
2020-01-09 22:23:58 +01:00
$sms_details = $this -> api -> get ( $endpoint );
if ( ! isset ( $sms_details [ 'creationDatetime' ], $sms_details [ 'message' ], $sms_details [ 'sender' ]))
{
continue ;
}
2020-04-02 01:55:55 +02:00
$response [ 'smss' ][] = [
2020-01-09 22:23:58 +01:00
'at' => ( new \DateTime ( $sms_details [ 'creationDatetime' ])) -> format ( 'Y-m-d H:i:s' ),
'text' => $sms_details [ 'message' ],
'origin' => $sms_details [ 'sender' ],
];
//Remove the sms to prevent double reading as ovh do not offer a filter for unread messages only
2021-01-17 03:16:57 +01:00
$endpoint = '/sms/' . $this -> data [ 'service_name' ] . '/virtualNumbers/' . $this -> formatted_number . '/incoming/' . $uid ;
2020-01-09 22:23:58 +01:00
$this -> api -> delete ( $endpoint );
}
2020-04-02 01:55:55 +02:00
return $response ;
2020-01-09 22:23:58 +01:00
}
2020-04-02 01:55:55 +02:00
catch ( \Throwable $t )
2020-01-09 22:23:58 +01:00
{
2020-04-02 01:55:55 +02:00
$response [ 'error' ] = true ;
$response [ 'error_message' ] = $t -> getMessage ();
2020-06-23 21:06:13 +02:00
2020-04-02 01:55:55 +02:00
return $response ;
2020-01-09 22:23:58 +01:00
}
}
2023-02-18 16:39:07 +01:00
/**
* Method called to verify phone status
*
* @ return string : Return one phone status among 'available' , 'unavailable' , 'no_credit'
*/
public function check_phone_status () : string
{
return \models\Phone :: STATUS_AVAILABLE ;
}
2020-01-17 18:19:25 +01:00
public function test () : bool
2020-01-09 22:23:58 +01:00
{
2020-01-17 18:19:25 +01:00
try
2020-01-09 22:23:58 +01:00
{
2020-04-02 01:55:55 +02:00
return true ;
2020-01-09 22:23:58 +01:00
$success = true ;
//Check service name
2021-01-17 03:16:57 +01:00
$endpoint = '/sms/' . $this -> data [ 'service_name' ];
2020-01-09 22:23:58 +01:00
$response = $this -> api -> get ( $endpoint );
$success = $success && ( bool ) $response ;
2020-01-17 18:19:25 +01:00
2020-01-09 22:23:58 +01:00
//Check virtualnumber
2020-01-17 18:47:08 +01:00
$endpoint = '/sms/virtualNumbers/' . $this -> formatted_number ;
2020-01-09 22:23:58 +01:00
$response = $this -> api -> get ( $endpoint );
2020-01-17 18:19:25 +01:00
return $success && ( bool ) $response ;
2020-01-09 22:23:58 +01:00
}
2020-04-02 01:55:55 +02:00
catch ( \Throwable $t )
2020-01-09 22:23:58 +01:00
{
return false ;
}
2019-11-12 05:18:32 +01:00
}
2020-01-08 02:14:38 +01:00
2020-01-17 18:19:25 +01:00
public static function status_change_callback ()
2020-01-08 02:14:38 +01:00
{
2020-01-09 22:23:58 +01:00
$uid = $_GET [ 'id' ] ? ? false ;
$dlr = $_GET [ 'dlr' ] ? ? false ;
2020-01-17 18:19:25 +01:00
if ( false === $uid || false === $dlr )
2020-01-09 22:23:58 +01:00
{
return false ;
}
switch ( $dlr )
{
case 1 :
2020-04-02 01:55:55 +02:00
$status = \models\Sended :: STATUS_DELIVERED ;
2020-01-09 22:23:58 +01:00
2020-01-17 18:19:25 +01:00
break ;
2021-01-14 03:32:17 +01:00
2020-01-09 22:23:58 +01:00
case 2 :
case 16 :
2020-04-02 01:55:55 +02:00
$status = \models\Sended :: STATUS_FAILED ;
2020-01-17 18:19:25 +01:00
2020-01-09 22:23:58 +01:00
break ;
2021-01-14 03:32:17 +01:00
2020-01-09 22:23:58 +01:00
default :
2020-04-02 01:55:55 +02:00
$status = \models\Sended :: STATUS_UNKNOWN ;
2020-01-17 18:19:25 +01:00
2020-01-09 22:23:58 +01:00
break ;
}
return [ 'uid' => $uid , 'status' => $status ];
2020-01-08 02:14:38 +01:00
}
2020-06-23 21:06:13 +02:00
public static function reception_callback () : array
2020-04-03 02:15:55 +02:00
{
return [];
}
2021-06-17 00:51:33 +02:00
2023-09-22 18:27:17 +02:00
public static function inbound_call_callback () : array
2021-03-23 17:39:13 +01:00
{
return [];
}
2021-06-17 00:51:33 +02:00
2023-09-22 18:27:17 +02:00
public static function end_call_callback () : array
2021-03-23 17:39:13 +01:00
{
return [];
}
2019-11-12 05:18:32 +01:00
}