2020-09-02 23:32:17 +02:00
|
|
|
<?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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 BenchmarkAdapter implements AdapterInterface
|
|
|
|
{
|
|
|
|
/**
|
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-09-02 23:32:17 +02:00
|
|
|
*/
|
2021-01-17 03:16:57 +01:00
|
|
|
private $data;
|
2020-09-02 23:32:17 +02:00
|
|
|
|
|
|
|
/**
|
2020-09-02 23:45:01 +02:00
|
|
|
* API URL.
|
2020-09-02 23:32:17 +02:00
|
|
|
*/
|
|
|
|
private $api_url = 'https://jsonplaceholder.typicode.com/posts';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-09-02 23:32:17 +02:00
|
|
|
*/
|
2021-01-17 03:16:57 +01:00
|
|
|
public function __construct(string $data)
|
2020-09-02 23:32:17 +02:00
|
|
|
{
|
2021-01-17 03:16:57 +01:00
|
|
|
$this->data = $data;
|
2020-09-02 23:32:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Classname of the adapter.
|
|
|
|
*/
|
|
|
|
public static function meta_classname(): string
|
|
|
|
{
|
|
|
|
return __CLASS__;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uniq name of the adapter
|
|
|
|
* It should be the classname of the adapter un snakecase.
|
|
|
|
*/
|
|
|
|
public static function meta_uid(): string
|
|
|
|
{
|
|
|
|
return 'benchmark_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;
|
|
|
|
}
|
2020-09-02 23:32:17 +02: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;
|
|
|
|
}
|
|
|
|
|
2020-09-02 23:32:17 +02:00
|
|
|
/**
|
|
|
|
* 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 'Benchmark';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Description of the adapter.
|
|
|
|
* A short description of the service the adapter implements.
|
|
|
|
*/
|
|
|
|
public static function meta_description(): string
|
|
|
|
{
|
|
|
|
return 'A benchmark adaptater that use https://jsonplaceholder.typicode.com to test speed of SMS sending.';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-17 03:16:57 +01:00
|
|
|
* List of entries we want in data for the adapter.
|
2020-09-02 23:32:17 +02:00
|
|
|
*
|
|
|
|
* @return array : Eachline 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-09-02 23:32:17 +02:00
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does the implemented service support status change.
|
|
|
|
*/
|
|
|
|
public static function meta_support_status_change(): bool
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-09-02 23:32:17 +02:00
|
|
|
|
|
|
|
/**
|
2021-06-17 00:51:33 +02:00
|
|
|
* Does the implemented service support mms sending.
|
2020-09-02 23:32:17 +02: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
|
2020-09-02 23:32:17 +02:00
|
|
|
{
|
|
|
|
$response = [
|
|
|
|
'error' => false,
|
|
|
|
'error_message' => null,
|
|
|
|
'uid' => null,
|
|
|
|
];
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-01-17 03:16:57 +01:00
|
|
|
$data = [
|
2020-09-02 23:32:17 +02:00
|
|
|
'sms_text' => $text,
|
|
|
|
'sms_destination' => $destination,
|
|
|
|
'sms_flash' => $flash,
|
|
|
|
];
|
|
|
|
|
|
|
|
$endpoint = $this->api_url;
|
|
|
|
|
|
|
|
$curl = curl_init();
|
|
|
|
curl_setopt($curl, CURLOPT_URL, $endpoint);
|
2021-11-05 23:26:09 +01:00
|
|
|
curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
|
2020-09-02 23:32:17 +02:00
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
|
|
|
|
curl_setopt($curl, CURLOPT_POST, true);
|
2021-01-17 03:16:57 +01:00
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
|
2020-09-02 23:32:17 +02:00
|
|
|
$curl_response = curl_exec($curl);
|
|
|
|
curl_close($curl);
|
|
|
|
|
|
|
|
if (false === $curl_response)
|
|
|
|
{
|
|
|
|
$response['error'] = true;
|
|
|
|
$response['error_message'] = 'HTTP query failed.';
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
var_dump($curl_response);
|
|
|
|
|
|
|
|
$response_decode = json_decode($curl_response, true);
|
|
|
|
if (null === $response_decode)
|
|
|
|
{
|
|
|
|
$response['error'] = true;
|
|
|
|
$response['error_message'] = 'Invalid JSON for response.';
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response['uid'] = uniqid();
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
catch (\Throwable $t)
|
|
|
|
{
|
|
|
|
$response['error'] = true;
|
|
|
|
$response['error_message'] = $t->getMessage();
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function read(): array
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function status_change_callback()
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function reception_callback(): array
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test(): bool
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2021-06-17 00:51:33 +02:00
|
|
|
|
2021-03-23 17:39:13 +01:00
|
|
|
public function inbound_call_callback(): array
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
2021-06-17 00:51:33 +02:00
|
|
|
|
2021-03-23 17:39:13 +01:00
|
|
|
public function end_call_callback(): array
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
2020-09-02 23:32:17 +02:00
|
|
|
}
|