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
/**
* Interface for phones adapters
* Phone ' s adapters allow RaspiSMS to use a platform to communicate with a phone number .
* Its an adapter between internal and external code , as an API , command line software , physical modem , etc .
*
* All Phone Adapters must implement this interface
*/
class OvhSmsAdapter implements AdapterInterface
{
/**
2020-01-17 18:19:25 +01:00
* Phone number using the adapter .
*/
private $number ;
/**
* Datas used to configure interaction with the implemented service . ( e . g : Api credentials , ports numbers , etc . ) .
*/
private $datas ;
/**
* OVH Api instance .
*/
private $api ;
/**
* Number formated to be compatible with http query according to the ovh way .
*/
private $formatted_number ;
/**
* 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 $number , string $datas )
{
$this -> number = $number ;
$this -> formatted_number = str_replace ( '+' , '00' , $number );
$this -> datas = json_decode ( $datas , true );
$this -> api = new Api (
$this -> datas [ 'app_key' ],
$this -> datas [ 'app_secret' ],
$this -> datas [ 'endpoint' ],
$this -> datas [ 'consumer_key' ]
);
}
/**
* 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__ ;
}
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
{
return 'OVH SMS' ;
}
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
{
return 'Solution de SMS proposé par le groupe OVH, https://www.ovhtelecom.fr/sms/.' ;
}
2019-11-12 05:18:32 +01:00
/**
2020-01-17 18:19:25 +01:00
* Description of the datas expected by the adapter to help the user . ( e . g : A list of expecteds Api credentials fields , with name and value ) .
2019-11-12 05:18:32 +01:00
*/
2020-01-17 18:19:25 +01:00
public static function meta_datas_help () : string
{
return 'Clefs API OVH, https://api.ovh.com/createToken/index.cgi.' ;
}
2020-01-09 22:23:58 +01:00
/**
2020-01-17 18:19:25 +01:00
* List of entries we want in datas for the adapter .
*
2020-01-09 22:23:58 +01:00
* @ return array : Every line is a field as an array with keys : name , title , description , required
*/
2020-01-17 18:19:25 +01:00
public static function meta_datas_fields () : array
2020-01-09 22:23:58 +01:00
{
return [
[
'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 ,
],
[
'name' => 'endpoint' ,
'title' => 'Endpoint' ,
'description' => 'Endpoint de l\'API OVH, voir https://github.com/ovh/php-ovh/#supported-apis.' ,
'required' => true ,
],
[
'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 ,
],
];
}
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
2019-11-12 05:18:32 +01:00
/**
2020-01-17 18:19:25 +01:00
* Method called to send a SMS to a number .
*
2019-11-12 05:18:32 +01:00
* @ param string $destination : Phone number to send the sms to
2020-01-17 18:19:25 +01:00
* @ param string $text : Text of the SMS to send
* @ param bool $flash : Is the SMS a Flash SMS
*
2020-01-06 23:35:55 +01:00
* @ return mixed Uid of the sended message if send , False else
2019-11-12 05:18:32 +01:00
*/
2020-01-17 18:19:25 +01:00
public function send ( string $destination , string $text , bool $flash = false )
2019-11-12 05:18:32 +01:00
{
2020-01-17 18:19:25 +01:00
try
2020-01-09 22:23:58 +01:00
{
$success = true ;
2020-01-17 18:47:08 +01:00
$endpoint = '/sms/' . $this -> datas [ '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 )
{
return false ;
}
$uids = $response [ 'ids' ] ? ? [];
2020-01-17 18:19:25 +01:00
return $uids [ 0 ] ? ? false ;
2020-01-09 22:23:58 +01:00
}
catch ( \Exception $e )
{
return false ;
}
2019-11-12 05:18:32 +01:00
}
/**
2020-01-17 18:19:25 +01:00
* Method called to read SMSs of the number .
*
2019-11-12 05:18:32 +01:00
* @ return array : Array of the sms reads
*/
2020-01-17 18:19:25 +01:00
public function read () : array
2019-11-12 05:18:32 +01:00
{
2020-01-17 18:19:25 +01:00
try
2020-01-09 22:23:58 +01:00
{
$success = true ;
2020-01-17 18:47:08 +01:00
$endpoint = '/sms/' . $this -> datas [ 'service_name' ] . '/virtualNumbers/' . $this -> formatted_number . '/incoming' ;
2020-01-09 22:23:58 +01:00
$uids = $this -> api -> get ( $endpoint );
2020-01-17 18:19:25 +01:00
if ( ! \is_array ( $uids ) || ! $uids )
2020-01-09 22:23:58 +01:00
{
return [];
}
$received_smss = [];
foreach ( $uids as $uid )
{
2020-01-17 18:47:08 +01:00
$endpoint = '/sms/' . $this -> datas [ '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 ;
}
$received_smss [] = [
'at' => ( new \DateTime ( $sms_details [ 'creationDatetime' ])) -> format ( 'Y-m-d H:i:s' ),
'text' => $sms_details [ 'message' ],
'origin' => $sms_details [ 'sender' ],
'destination' => $this -> number ,
];
//Remove the sms to prevent double reading as ovh do not offer a filter for unread messages only
2020-01-17 18:47:08 +01:00
$endpoint = '/sms/' . $this -> datas [ 'service_name' ] . '/virtualNumbers/' . $this -> formatted_number . '/incoming/' . $uid ;
2020-01-09 22:23:58 +01:00
$this -> api -> delete ( $endpoint );
}
return $received_smss ;
}
catch ( \Exception $e )
{
return [];
}
}
/**
* Method called to verify if the adapter is working correctly
2020-01-17 18:19:25 +01:00
* should be use for exemple to verify that credentials and number are both valid .
*
* @ return bool : False on error , true else
2020-01-09 22:23:58 +01:00
*/
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
{
$success = true ;
//Check service name
2020-01-17 18:47:08 +01:00
$endpoint = '/sms/' . $this -> datas [ '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
}
catch ( \Exception $e )
{
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
* Method called on reception of a status update notification for a SMS .
*
2020-01-08 02:14:38 +01:00
* @ return mixed : False on error , else array [ 'uid' => uid of the sms , 'status' => New status of the sms ( 'unknown' , 'delivered' , 'failed' )]
*/
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 :
$status = 'delivered' ;
2020-01-17 18:19:25 +01:00
break ;
2020-01-09 22:23:58 +01:00
case 2 :
case 16 :
$status = 'failed' ;
2020-01-17 18:19:25 +01:00
2020-01-09 22:23:58 +01:00
break ;
default :
$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
}
2019-11-12 05:18:32 +01:00
}