mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-06-06 14:46:27 +02:00
Fix php style
This commit is contained in:
parent
461bd9c98d
commit
b8bd067dc7
59 changed files with 2307 additions and 1868 deletions
|
@ -1,6 +1,17 @@
|
|||
<?php
|
||||
namespace adapters;
|
||||
use \Ovh\Api;
|
||||
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Interface for phones adapters
|
||||
|
@ -12,32 +23,85 @@
|
|||
class OvhSmsAdapter implements AdapterInterface
|
||||
{
|
||||
/**
|
||||
* Classname of the adapter
|
||||
* Phone number using the adapter.
|
||||
*/
|
||||
public static function meta_classname() : string { return __CLASS__; }
|
||||
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.
|
||||
*/
|
||||
public static function meta_classname(): string
|
||||
{
|
||||
return __CLASS__;
|
||||
}
|
||||
|
||||
/**
|
||||
* Name of the adapter.
|
||||
* It should probably be the name of the service it adapt (e.g : Gammu SMSD, OVH SMS, SIM800L, etc.)
|
||||
* 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 'OVH SMS'; }
|
||||
public static function meta_name(): string
|
||||
{
|
||||
return 'OVH SMS';
|
||||
}
|
||||
|
||||
/**
|
||||
* Description of the adapter.
|
||||
* A short description of the service the adapter implements.
|
||||
*/
|
||||
public static function meta_description() : string { return 'Solution de SMS proposé par le groupe OVH, https://www.ovhtelecom.fr/sms/.'; }
|
||||
|
||||
public static function meta_description(): string
|
||||
{
|
||||
return 'Solution de SMS proposé par le groupe OVH, https://www.ovhtelecom.fr/sms/.';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
* 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).
|
||||
*/
|
||||
public static function meta_datas_help() : string { return 'Clefs API OVH, https://api.ovh.com/createToken/index.cgi.'; }
|
||||
|
||||
public static function meta_datas_help(): string
|
||||
{
|
||||
return 'Clefs API OVH, https://api.ovh.com/createToken/index.cgi.';
|
||||
}
|
||||
|
||||
/**
|
||||
* List of entries we want in datas for the adapter
|
||||
* 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
|
||||
public static function meta_datas_fields(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
|
@ -74,70 +138,37 @@
|
|||
}
|
||||
|
||||
/**
|
||||
* Does the implemented service support flash smss
|
||||
* 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; }
|
||||
|
||||
|
||||
/**
|
||||
* 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)
|
||||
public static function meta_support_flash(): bool
|
||||
{
|
||||
$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']
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Method called to send a SMS to a number
|
||||
* Does the implemented service support status change.
|
||||
*/
|
||||
public static function meta_support_status_change(): 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
|
||||
* @param string $text : Text of the SMS to send
|
||||
* @param bool $flash : Is the SMS a Flash SMS
|
||||
*
|
||||
* @return mixed Uid of the sended message if send, False else
|
||||
*/
|
||||
public function send (string $destination, string $text, bool $flash = false)
|
||||
public function send(string $destination, string $text, bool $flash = false)
|
||||
{
|
||||
try
|
||||
try
|
||||
{
|
||||
$success = true;
|
||||
|
||||
$endpoint = '/sms/' . $this->datas['service_name'] . '/virtualNumbers/' . $this->formatted_number . '/jobs';
|
||||
$endpoint = '/sms/'.$this->datas['service_name'].'/virtualNumbers/'.$this->formatted_number.'/jobs';
|
||||
$params = [
|
||||
'message' => $text,
|
||||
'receivers' => [$destination],
|
||||
|
@ -145,14 +176,15 @@
|
|||
|
||||
$response = $this->api->post($endpoint, $params);
|
||||
|
||||
$nb_invalid_receivers = count(($response['invalidReceivers'] ?? []));
|
||||
$nb_invalid_receivers = \count(($response['invalidReceivers'] ?? []));
|
||||
if ($nb_invalid_receivers > 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$uids = $response['ids'] ?? [];
|
||||
return ($uids[0] ?? false);
|
||||
|
||||
return $uids[0] ?? false;
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
|
@ -160,21 +192,21 @@
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Method called to read SMSs of the number
|
||||
* Method called to read SMSs of the number.
|
||||
*
|
||||
* @return array : Array of the sms reads
|
||||
*/
|
||||
public function read () : array
|
||||
public function read(): array
|
||||
{
|
||||
try
|
||||
try
|
||||
{
|
||||
$success = true;
|
||||
|
||||
$endpoint = '/sms/' . $this->datas['service_name'] . '/virtualNumbers/' . $this->formatted_number . '/incoming';
|
||||
$endpoint = '/sms/'.$this->datas['service_name'].'/virtualNumbers/'.$this->formatted_number.'/incoming';
|
||||
$uids = $this->api->get($endpoint);
|
||||
|
||||
if (!is_array($uids) || !$uids)
|
||||
if (!\is_array($uids) || !$uids)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
@ -182,7 +214,7 @@
|
|||
$received_smss = [];
|
||||
foreach ($uids as $uid)
|
||||
{
|
||||
$endpoint = '/sms/' . $this->datas['service_name'] . '/virtualNumbers/' . $this->formatted_number . '/incoming/' . $uid;
|
||||
$endpoint = '/sms/'.$this->datas['service_name'].'/virtualNumbers/'.$this->formatted_number.'/incoming/'.$uid;
|
||||
$sms_details = $this->api->get($endpoint);
|
||||
|
||||
if (!isset($sms_details['creationDatetime'], $sms_details['message'], $sms_details['sender']))
|
||||
|
@ -198,7 +230,7 @@
|
|||
];
|
||||
|
||||
//Remove the sms to prevent double reading as ovh do not offer a filter for unread messages only
|
||||
$endpoint = '/sms/' . $this->datas['service_name'] . '/virtualNumbers/' . $this->formatted_number . '/incoming/' . $uid;
|
||||
$endpoint = '/sms/'.$this->datas['service_name'].'/virtualNumbers/'.$this->formatted_number.'/incoming/'.$uid;
|
||||
$this->api->delete($endpoint);
|
||||
}
|
||||
|
||||
|
@ -210,29 +242,28 @@
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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 boolean : False on error, true else
|
||||
* 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
|
||||
public function test(): bool
|
||||
{
|
||||
try
|
||||
try
|
||||
{
|
||||
$success = true;
|
||||
|
||||
//Check service name
|
||||
$endpoint = '/sms/' . $this->datas['service_name'];
|
||||
$response = $this->api->get($endpoint);
|
||||
$success = $success && (bool) $response;
|
||||
|
||||
//Check virtualnumber
|
||||
$endpoint = '/sms/virtualNumbers/' . $this->formatted_number;
|
||||
$endpoint = '/sms/'.$this->datas['service_name'];
|
||||
$response = $this->api->get($endpoint);
|
||||
$success = $success && (bool) $response;
|
||||
|
||||
return $success;
|
||||
//Check virtualnumber
|
||||
$endpoint = '/sms/virtualNumbers/'.$this->formatted_number;
|
||||
$response = $this->api->get($endpoint);
|
||||
|
||||
return $success && (bool) $response;
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
|
@ -240,17 +271,17 @@
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Method called on reception of a status update notification for a SMS
|
||||
* 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 ('unknown', 'delivered', 'failed')]
|
||||
*/
|
||||
public static function status_change_callback ()
|
||||
public static function status_change_callback()
|
||||
{
|
||||
$uid = $_GET['id'] ?? false;
|
||||
$dlr = $_GET['dlr'] ?? false;
|
||||
|
||||
if ($uid === false || $dlr === false)
|
||||
if (false === $uid || false === $dlr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -259,15 +290,16 @@
|
|||
{
|
||||
case 1:
|
||||
$status = 'delivered';
|
||||
break;
|
||||
|
||||
break;
|
||||
case 2:
|
||||
case 16:
|
||||
$status = 'failed';
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
$status = 'unknown';
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue