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
|
|
|
|
{
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Phone number using the adapter.
|
2019-12-20 18:31:19 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
private $number;
|
2019-12-20 18:31:19 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Datas used to configure interaction with the implemented service. (e.g : Api credentials, ports numbers, etc.).
|
2019-12-20 18:31:19 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
private $datas;
|
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
|
|
|
*/
|
2020-01-17 18:47:08 +01:00
|
|
|
private $test_file_read = PWD_DATAS . '/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
|
|
|
*/
|
2020-01-17 18:47:08 +01:00
|
|
|
private $test_file_write = PWD_DATAS . '/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.
|
|
|
|
*
|
|
|
|
* @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
|
2020-01-09 22:23:58 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function __construct(string $number, string $datas)
|
|
|
|
{
|
|
|
|
$this->number = $number;
|
|
|
|
$this->datas = $datas;
|
|
|
|
}
|
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-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-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-12-20 18:31:19 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public static function meta_datas_help(): string
|
|
|
|
{
|
|
|
|
return 'No datas.';
|
|
|
|
}
|
2020-01-07 17:55:16 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* List of entries we want in datas for the adapter.
|
|
|
|
*
|
|
|
|
* @return array : Eachline line is a field as an array with keys : name, title, description, required
|
2020-01-07 17:55:16 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public static function meta_datas_fields(): array
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
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-01-17 18:19:25 +01:00
|
|
|
return true;
|
2019-12-20 18:31:19 +01:00
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-12-20 18:31:19 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Method called to send a SMS to a number.
|
|
|
|
*
|
2019-12-20 18:31:19 +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-12-20 18:31:19 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function send(string $destination, string $text, bool $flash = false)
|
2019-12-20 18:31:19 +01:00
|
|
|
{
|
2020-01-07 17:55:16 +01:00
|
|
|
$uid = uniqid();
|
|
|
|
|
|
|
|
$at = (new \DateTime())->format('Y-m-d H:i:s');
|
2020-01-17 18:47:08 +01:00
|
|
|
file_put_contents($this->test_file_write, json_encode(['uid' => $uid, 'at' => $at, 'destination' => $destination, 'text' => $text, 'flash' => $flash]) . "\n", FILE_APPEND);
|
2020-01-07 17:55:16 +01:00
|
|
|
|
2020-01-06 23:35:55 +01:00
|
|
|
return uniqid();
|
2019-12-20 18:31:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Method called to read SMSs of the number.
|
|
|
|
*
|
2019-12-20 18:31:19 +01:00
|
|
|
* @return array : Array of the sms reads
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function read(): array
|
2019-12-20 18:31:19 +01:00
|
|
|
{
|
2020-01-07 17:55:16 +01:00
|
|
|
$file_contents = file_get_contents($this->test_file_read);
|
|
|
|
|
|
|
|
//Empty file to avoid dual read
|
|
|
|
file_put_contents($this->test_file_read, '');
|
|
|
|
|
|
|
|
$smss = explode("\n", $file_contents);
|
|
|
|
|
|
|
|
$return = [];
|
|
|
|
|
|
|
|
foreach ($smss as $key => $sms)
|
|
|
|
{
|
|
|
|
$decode_sms = json_decode($sms, true);
|
2020-01-17 18:19:25 +01:00
|
|
|
if (null === $decode_sms)
|
2020-01-07 17:55:16 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$return[] = $decode_sms;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
2019-12-20 18:31:19 +01:00
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-09 22:23:58 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2020-01-17 18:19:25 +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
|
|
|
{
|
|
|
|
$uid = $_GET['uid'] ?? false;
|
|
|
|
$status = $_GET['status'] ?? false;
|
|
|
|
|
|
|
|
if (!$uid || !$status)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$return = [
|
|
|
|
'uid' => $uid,
|
|
|
|
'status' => 'unknown',
|
|
|
|
];
|
|
|
|
|
|
|
|
switch ($status)
|
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
case 'delivered':
|
2020-01-08 02:14:38 +01:00
|
|
|
$return['status'] = 'delivered';
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-08 02:14:38 +01:00
|
|
|
break;
|
2020-01-17 18:19:25 +01:00
|
|
|
case 'failed':
|
2020-01-08 02:14:38 +01:00
|
|
|
$return['status'] = 'failed';
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
break;
|
|
|
|
default:
|
2020-01-08 02:14:38 +01:00
|
|
|
$return['status'] = 'unknown';
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-08 02:14:38 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
2019-12-20 18:31:19 +01:00
|
|
|
}
|