2019-12-20 18:31:19 +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;
|
2019-12-20 18:31:19 +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 TestAdapter 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.).
|
2019-12-20 18:31:19 +01:00
|
|
|
*/
|
2021-01-17 03:16:57 +01:00
|
|
|
private $data;
|
2019-12-20 18:31:19 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Path for the file to read sms as a json from.
|
2019-12-20 18:31:19 +01:00
|
|
|
*/
|
2021-01-17 03:16:57 +01:00
|
|
|
private $test_file_read = PWD_DATA . '/test_read_sms.json';
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-12-20 18:31:19 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Path for the file to write sms as a json in.
|
2019-12-20 18:31:19 +01:00
|
|
|
*/
|
2021-01-17 03:16:57 +01:00
|
|
|
private $test_file_write = PWD_DATA . '/test_write_sms.json';
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-09 22:23:58 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* 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-09 22:23:58 +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 = $data;
|
2020-01-17 18:19:25 +01:00
|
|
|
}
|
2019-12-20 18:31:19 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Classname of the adapter.
|
2019-12-20 18:31:19 +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 'test_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-01-17 18:19:25 +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;
|
|
|
|
}
|
|
|
|
|
2020-01-08 02:14:38 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Name of the adapter.
|
|
|
|
* It should probably be the name of the service it adapt (e.g : Gammu SMSD, OVH SMS, SIM800L, etc.).
|
2020-01-08 02:14:38 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public static function meta_name(): string
|
|
|
|
{
|
|
|
|
return 'Test';
|
|
|
|
}
|
2019-12-20 18:31:19 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Description of the adapter.
|
|
|
|
* A short description of the service the adapter implements.
|
2019-12-20 18:31:19 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public static function meta_description(): string
|
|
|
|
{
|
|
|
|
return 'A test adaptater that do not actually send or receive any message.';
|
|
|
|
}
|
2019-12-20 18:31:19 +01:00
|
|
|
|
2020-01-07 17:55:16 +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
|
|
|
*
|
|
|
|
* @return array : Eachline line is a field as an array with keys : name, title, description, required
|
2020-01-07 17:55:16 +01:00
|
|
|
*/
|
2021-01-17 03:16:57 +01:00
|
|
|
public static function meta_data_fields(): array
|
2020-01-17 18:19:25 +01:00
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
2020-06-23 21:06:13 +02: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;
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-07 17:55:16 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Does the implemented service support flash smss.
|
2020-01-07 17:55:16 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public static function meta_support_flash(): bool
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2020-01-07 17:55:16 +01:00
|
|
|
|
2019-12-20 18:31:19 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Does the implemented service support status change.
|
2019-12-20 18:31:19 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public static function meta_support_status_change(): bool
|
2019-12-20 18:31:19 +01:00
|
|
|
{
|
2020-04-03 02:15:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does the implemented service support reception callback.
|
|
|
|
*/
|
|
|
|
public static function meta_support_reception(): bool
|
|
|
|
{
|
|
|
|
return false;
|
2019-12-20 18:31:19 +01:00
|
|
|
}
|
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 true;
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-12-20 18:31:19 +01:00
|
|
|
/**
|
2021-06-17 00:51:33 +02:00
|
|
|
* Does the implemented service support mms sending.
|
2019-12-20 18:31:19 +01:00
|
|
|
*/
|
2021-03-19 02:45:12 +01:00
|
|
|
public static function meta_support_mms_sending(): bool
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
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
|
|
|
|
{
|
2021-03-24 00:22:17 +01:00
|
|
|
return true;
|
2021-03-23 17:39:13 +01:00
|
|
|
}
|
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
|
|
|
|
{
|
2021-03-24 00:22:17 +01:00
|
|
|
return true;
|
2021-03-23 17:39:13 +01:00
|
|
|
}
|
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-12-20 18:31:19 +01:00
|
|
|
{
|
2020-04-02 01:55:55 +02:00
|
|
|
$response = [
|
|
|
|
'error' => false,
|
|
|
|
'error_message' => null,
|
|
|
|
'uid' => null,
|
|
|
|
];
|
|
|
|
|
2020-01-07 17:55:16 +01:00
|
|
|
$uid = uniqid();
|
|
|
|
|
|
|
|
$at = (new \DateTime())->format('Y-m-d H:i:s');
|
2021-03-19 02:45:12 +01:00
|
|
|
$success = file_put_contents($this->test_file_write, json_encode(['uid' => $uid, 'at' => $at, 'destination' => $destination, 'text' => $text, 'flash' => $flash, 'mms' => $mms, 'medias' => $medias]) . "\n", FILE_APPEND);
|
2020-06-23 21:06:13 +02:00
|
|
|
if (false === $success)
|
2020-04-02 01:55:55 +02:00
|
|
|
{
|
|
|
|
$response['error'] = true;
|
|
|
|
$response['error_message'] = 'Cannot write in file : ' . $this->test_file_write;
|
2020-06-23 21:06:13 +02:00
|
|
|
|
2020-04-02 01:55:55 +02:00
|
|
|
return $response;
|
|
|
|
}
|
2020-01-07 17:55:16 +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;
|
2019-12-20 18:31:19 +01:00
|
|
|
}
|
|
|
|
|
2021-03-26 23:32:29 +01:00
|
|
|
/**
|
|
|
|
* Read from a files to simulate sms reception.
|
2021-03-27 01:15:09 +01:00
|
|
|
* In the file we expect a series of lines, each line beeing a SMS as a json string of format :
|
2021-03-26 23:32:29 +01:00
|
|
|
* {
|
|
|
|
* "at" : "2021-03-26 11:21:48",
|
|
|
|
* "medias" : [
|
|
|
|
* "https://unsplash.com/photos/q4DJVtxES0w/download?force=true&w=640",
|
|
|
|
* "/tmp/somelocalfile.jpg"
|
|
|
|
* ],
|
|
|
|
* "mms" : true,
|
|
|
|
* "origin" : "+33612345678",
|
|
|
|
* "text" : "SMS Text"
|
2021-06-17 00:51:33 +02:00
|
|
|
* }.
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function read(): array
|
2019-12-20 18:31:19 +01:00
|
|
|
{
|
2020-04-02 01:55:55 +02:00
|
|
|
$response = [
|
|
|
|
'error' => false,
|
|
|
|
'error_message' => null,
|
|
|
|
'smss' => [],
|
|
|
|
];
|
2020-01-07 17:55:16 +01:00
|
|
|
|
2020-06-23 21:06:13 +02:00
|
|
|
try
|
2020-04-02 01:55:55 +02:00
|
|
|
{
|
|
|
|
$file_contents = file_get_contents($this->test_file_read);
|
2020-06-23 21:06:13 +02:00
|
|
|
if (false === $file_contents)
|
2020-04-02 01:55:55 +02:00
|
|
|
{
|
|
|
|
$response['error'] = true;
|
|
|
|
$response['error_message'] = 'Cannot read file : ' . $this->test_file_read;
|
2020-06-23 21:06:13 +02:00
|
|
|
|
2020-04-02 01:55:55 +02:00
|
|
|
return $response;
|
|
|
|
}
|
2020-01-07 17:55:16 +01:00
|
|
|
|
2020-04-02 01:55:55 +02:00
|
|
|
//Empty file to avoid dual read
|
|
|
|
$success = file_put_contents($this->test_file_read, '');
|
2020-06-23 21:06:13 +02:00
|
|
|
if (false === $success)
|
2020-04-02 01:55:55 +02:00
|
|
|
{
|
|
|
|
$response['error'] = true;
|
|
|
|
$response['error_message'] = 'Cannot write in file : ' . $this->test_file_read;
|
2020-06-23 21:06:13 +02:00
|
|
|
|
2020-04-02 01:55:55 +02:00
|
|
|
return $response;
|
|
|
|
}
|
2020-01-07 17:55:16 +01:00
|
|
|
|
2020-04-02 01:55:55 +02:00
|
|
|
$smss = explode("\n", $file_contents);
|
2020-01-07 17:55:16 +01:00
|
|
|
|
2020-04-02 01:55:55 +02:00
|
|
|
foreach ($smss as $key => $sms)
|
2020-01-07 17:55:16 +01:00
|
|
|
{
|
2020-04-02 01:55:55 +02:00
|
|
|
$decode_sms = json_decode($sms, true);
|
|
|
|
if (null === $decode_sms)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-03-26 23:32:29 +01:00
|
|
|
$clean_sms = [
|
|
|
|
'at' => $decode_sms['at'],
|
|
|
|
'text' => $decode_sms['text'],
|
|
|
|
'origin' => $decode_sms['origin'],
|
|
|
|
'mms' => $decode_sms['mms'],
|
|
|
|
'medias' => [],
|
|
|
|
];
|
|
|
|
|
|
|
|
//In medias we want a media URI or URL
|
|
|
|
foreach ($decode_sms['medias'] ?? [] as $media)
|
|
|
|
{
|
|
|
|
$tempfile = tempnam('/tmp', 'raspisms-media-');
|
|
|
|
if (!$tempfile)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$copy = copy($media, $tempfile);
|
|
|
|
if (!$copy)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$clean_sms['medias'][] = [
|
|
|
|
'filepath' => $tempfile,
|
|
|
|
'extension' => pathinfo($media, PATHINFO_EXTENSION) ?: null,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$response['smss'][] = $clean_sms;
|
2020-01-07 17:55:16 +01:00
|
|
|
}
|
|
|
|
|
2020-04-02 01:55:55 +02:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
catch (\Throwable $t)
|
|
|
|
{
|
|
|
|
$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-07 17:55:16 +01:00
|
|
|
}
|
2019-12-20 18:31:19 +01:00
|
|
|
}
|
2020-01-17 18:19:25 +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
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
|
|
|
public static function status_change_callback()
|
2020-01-08 02:14:38 +01:00
|
|
|
{
|
|
|
|
$uid = $_GET['uid'] ?? false;
|
|
|
|
$status = $_GET['status'] ?? false;
|
|
|
|
|
|
|
|
if (!$uid || !$status)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$return = [
|
|
|
|
'uid' => $uid,
|
2020-04-02 01:55:55 +02:00
|
|
|
'status' => \models\Sended::STATUS_UNKNOWN,
|
2020-01-08 02:14:38 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
switch ($status)
|
|
|
|
{
|
2020-04-02 01:55:55 +02:00
|
|
|
case \models\Sended::STATUS_DELIVERED:
|
|
|
|
$return['status'] = \models\Sended::STATUS_DELIVERED;
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-08 02:14:38 +01:00
|
|
|
break;
|
2021-01-14 03:32:17 +01:00
|
|
|
|
2020-04-02 01:55:55 +02:00
|
|
|
case \models\Sended::STATUS_FAILED:
|
|
|
|
$return['status'] = \models\Sended::STATUS_FAILED;
|
2020-01-08 02:14:38 +01:00
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
break;
|
2021-01-14 03:32:17 +01:00
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
default:
|
2020-04-02 01:55:55 +02:00
|
|
|
$return['status'] = \models\Sended::STATUS_UNKNOWN;
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-08 02:14:38 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
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
|
|
|
|
2021-03-23 17:39:13 +01:00
|
|
|
public function inbound_call_callback(): array
|
|
|
|
{
|
|
|
|
$response = [
|
|
|
|
'error' => false,
|
|
|
|
'error_message' => null,
|
|
|
|
'call' => [],
|
|
|
|
];
|
|
|
|
|
|
|
|
$uid = $_POST['uid'] ?? false;
|
|
|
|
$start = $_POST['start'] ?? false;
|
|
|
|
$end = $_POST['end'] ?? null;
|
|
|
|
$origin = $_POST['origin'] ?? false;
|
|
|
|
|
|
|
|
if (!$uid || !$start || !$origin)
|
|
|
|
{
|
|
|
|
$response['error'] = true;
|
|
|
|
$response['error_message'] = 'Missing required argument.';
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response['call']['uid'] = $uid;
|
|
|
|
$response['call']['start'] = $start;
|
|
|
|
$response['call']['end'] = $end;
|
|
|
|
$response['call']['origin'] = $origin;
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2021-06-17 00:51:33 +02:00
|
|
|
|
2021-03-23 17:39:13 +01:00
|
|
|
public function end_call_callback(): array
|
|
|
|
{
|
|
|
|
$response = [
|
|
|
|
'error' => false,
|
|
|
|
'error_message' => null,
|
|
|
|
'call' => [],
|
|
|
|
];
|
|
|
|
|
|
|
|
$uid = $_POST['uid'] ?? false;
|
|
|
|
$end = $_POST['end'] ?? null;
|
|
|
|
|
|
|
|
if (!$uid || !$end)
|
|
|
|
{
|
|
|
|
$response['error'] = true;
|
|
|
|
$response['error_message'] = 'Missing required argument.';
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response['call']['uid'] = $uid;
|
|
|
|
$response['call']['end'] = $end;
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2019-12-20 18:31:19 +01:00
|
|
|
}
|