raspisms/adapters/TestAdapter.php

294 lines
8.7 KiB
PHP
Raw Normal View History

<?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;
/**
* 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
* Datas used to configure interaction with the implemented service. (e.g : Api credentials, ports numbers, etc.).
*/
2020-01-17 18:19:25 +01:00
private $datas;
/**
2020-01-17 18:19:25 +01:00
* Path for the file to read sms as a json from.
*/
private $test_file_read = PWD_DATAS . '/test_read_sms.json';
2020-01-17 18:19:25 +01:00
/**
2020-01-17 18:19:25 +01:00
* Path for the file to write sms as a json in.
*/
private $test_file_write = PWD_DATAS . '/test_write_sms.json';
2020-01-17 18:19:25 +01:00
/**
2020-01-17 18:19:25 +01:00
* Adapter constructor, called when instanciated by RaspiSMS.
*
2020-06-23 21:06:13 +02:00
* @param json string $datas : JSON string of the datas to configure interaction with the implemented service
*/
public function __construct(string $datas)
2020-01-17 18:19:25 +01:00
{
$this->datas = $datas;
}
/**
2020-01-17 18:19:25 +01:00
* Classname of the adapter.
*/
2020-01-17 18:19:25 +01:00
public static function meta_classname(): string
{
return __CLASS__;
}
2020-06-23 21:06:13 +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-06-23 21:06:13 +02:00
public static function meta_uid(): string
{
return 'test_adapter';
}
2020-01-17 18:19:25 +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-17 18:19:25 +01:00
public static function meta_name(): string
{
return 'Test';
}
/**
2020-01-17 18:19:25 +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 'A test adaptater that do not actually send or receive any message.';
}
/**
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-17 18:19:25 +01:00
public static function meta_datas_fields(): array
{
return [];
}
2020-06-23 21:06:13 +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
/**
2020-01-17 18:19:25 +01:00
* Does the implemented service support flash smss.
*/
2020-01-17 18:19:25 +01:00
public static function meta_support_flash(): bool
{
return true;
}
/**
2020-01-17 18:19:25 +01:00
* Does the implemented service support status change.
*/
2020-01-17 18:19:25 +01:00
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;
}
2020-01-17 18:19:25 +01:00
/**
2020-01-17 18:19:25 +01:00
* Method called to send a SMS to a number.
*
* @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
*
* @return array : [
2020-06-23 21:06:13 +02:00
* bool 'error' => false if no error, true else
* ?string 'error_message' => null if no error, else error message
* array 'uid' => Uid of the sms created on success
* ]
*/
2020-01-17 18:19:25 +01:00
public function send(string $destination, string $text, bool $flash = false)
{
$response = [
'error' => false,
'error_message' => null,
'uid' => null,
];
$uid = uniqid();
$at = (new \DateTime())->format('Y-m-d H:i:s');
$success = file_put_contents($this->test_file_write, json_encode(['uid' => $uid, 'at' => $at, 'destination' => $destination, 'text' => $text, 'flash' => $flash]) . "\n", FILE_APPEND);
2020-06-23 21:06:13 +02:00
if (false === $success)
{
$response['error'] = true;
$response['error_message'] = 'Cannot write in file : ' . $this->test_file_write;
2020-06-23 21:06:13 +02:00
return $response;
}
$response['uid'] = $uid;
2020-06-23 21:06:13 +02:00
return $response;
}
/**
2020-01-17 18:19:25 +01:00
* Method called to read SMSs of the number.
*
* @return array : [
2020-06-23 21:06:13 +02:00
* bool 'error' => false if no error, true else
* ?string 'error_message' => null if no error, else error message
* array 'sms' => Array of the sms reads
* ]
*/
2020-01-17 18:19:25 +01:00
public function read(): array
{
$response = [
'error' => false,
'error_message' => null,
'smss' => [],
];
2020-06-23 21:06:13 +02:00
try
{
$file_contents = file_get_contents($this->test_file_read);
2020-06-23 21:06:13 +02:00
if (false === $file_contents)
{
$response['error'] = true;
$response['error_message'] = 'Cannot read file : ' . $this->test_file_read;
2020-06-23 21:06:13 +02:00
return $response;
}
//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)
{
$response['error'] = true;
$response['error_message'] = 'Cannot write in file : ' . $this->test_file_read;
2020-06-23 21:06:13 +02:00
return $response;
}
$smss = explode("\n", $file_contents);
foreach ($smss as $key => $sms)
{
$decode_sms = json_decode($sms, true);
if (null === $decode_sms)
{
continue;
}
$response['smss'][] = $decode_sms;
}
return $response;
}
catch (\Throwable $t)
{
$response['error'] = true;
$response['error_message'] = $t->getMessage();
2020-06-23 21:06:13 +02:00
return $response;
}
}
2020-01-17 18:19:25 +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-17 18:19:25 +01:00
public function test(): bool
{
return true;
}
2020-01-17 18:19:25 +01:00
/**
2020-01-17 18:19:25 +01:00
* Method called on reception of a status update notification for a SMS.
*/
2020-01-17 18:19:25 +01:00
public static function status_change_callback()
{
$uid = $_GET['uid'] ?? false;
$status = $_GET['status'] ?? false;
if (!$uid || !$status)
{
return false;
}
$return = [
'uid' => $uid,
'status' => \models\Sended::STATUS_UNKNOWN,
];
switch ($status)
{
case \models\Sended::STATUS_DELIVERED:
$return['status'] = \models\Sended::STATUS_DELIVERED;
2020-01-17 18:19:25 +01:00
break;
2021-01-14 03:32:17 +01:00
case \models\Sended::STATUS_FAILED:
$return['status'] = \models\Sended::STATUS_FAILED;
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:
$return['status'] = \models\Sended::STATUS_UNKNOWN;
2020-01-17 18:19:25 +01:00
break;
}
return $return;
}
2020-06-23 21:06:13 +02:00
/**
* Method called on reception of a sms notification.
*
* @return array : [
2020-06-23 21:06:13 +02:00
* bool 'error' => false on success, true on error
* ?string 'error_message' => null on success, error message else
* array 'sms' => array [
* string 'at' : Recepetion date format Y-m-d H:i:s,
* string 'text' : SMS body,
* string 'origin' : SMS sender,
* ]
*
* ]
*/
2020-06-23 21:06:13 +02:00
public static function reception_callback(): array
{
return [];
}
}