2020-01-12 00:50:29 +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;
|
2020-01-12 00:50:29 +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 GammuAdapter implements AdapterInterface
|
|
|
|
{
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* 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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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->datas = json_decode($datas, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Classname of the adapter.
|
2020-01-12 00:50:29 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public static function meta_classname(): string
|
|
|
|
{
|
|
|
|
return __CLASS__;
|
|
|
|
}
|
2020-01-12 00:50:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the adapter.
|
2020-01-17 18:19:25 +01:00
|
|
|
* It should probably be the name of the service it adapt (e.g : Gammu SMSD, OVH SMS, SIM800L, etc.).
|
2020-01-12 00:50:29 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public static function meta_name(): string
|
|
|
|
{
|
|
|
|
return 'Gammu';
|
|
|
|
}
|
2020-01-12 00:50:29 +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 'Utilisation du logiciel Gammu qui doit être installé sur le serveur et configuré. Voir https://wammu.eu.';
|
|
|
|
}
|
|
|
|
|
2020-01-12 00:50:29 +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).
|
2020-01-12 00:50:29 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public static function meta_datas_help(): string
|
|
|
|
{
|
|
|
|
return 'Fichier de configuration à fournir à Gammu pour utiliser ce modem.';
|
|
|
|
}
|
|
|
|
|
2020-01-12 00:50:29 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* List of entries we want in datas for the adapter.
|
|
|
|
*
|
2020-01-12 00:50:29 +01:00
|
|
|
* @return array : Every 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
|
2020-01-12 00:50:29 +01:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'name' => 'config_file',
|
|
|
|
'title' => 'Fichier de configuration',
|
|
|
|
'description' => 'Chemin vers le fichier de configuration que Gammu devra utilisé pour se connecter au téléphone.',
|
|
|
|
'required' => true,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => 'pin',
|
|
|
|
'title' => 'Code PIN',
|
|
|
|
'description' => 'Code PIN devant être utilisé pour activer la carte SIM (laisser vide pour ne pas utiliser de code PIN).',
|
|
|
|
'required' => false,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Does the implemented service support flash smss.
|
2020-01-12 00:50:29 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public static function meta_support_flash(): bool
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-12 00:50:29 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Does the implemented service support status change.
|
2020-01-12 00:50:29 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public static function meta_support_status_change(): bool
|
2020-01-12 00:50:29 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
return false;
|
2020-01-12 00:50:29 +01:00
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-12 00:50:29 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Method called to send a SMS to a number.
|
|
|
|
*
|
2020-01-12 00:50:29 +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-12 00:50:29 +01:00
|
|
|
* @return mixed : Uid of the sended message if send, False else
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function send(string $destination, string $text, bool $flash = false)
|
2020-01-12 00:50:29 +01:00
|
|
|
{
|
|
|
|
if (!$this->unlock_sim())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-12 00:50:29 +01:00
|
|
|
$command_parts = [
|
|
|
|
'gammu',
|
|
|
|
'--config',
|
|
|
|
escapeshellarg($this->datas['config_file']),
|
|
|
|
'sendsms',
|
|
|
|
'TEXT',
|
|
|
|
escapeshellarg($destination),
|
|
|
|
'-text',
|
|
|
|
escapeshellarg($text),
|
|
|
|
'-validity',
|
|
|
|
'MAX',
|
|
|
|
'-autolen',
|
|
|
|
mb_strlen($text),
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($flash)
|
|
|
|
{
|
|
|
|
$command_parts[] = '-flash';
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $this->exec_command($command_parts);
|
2020-01-17 18:19:25 +01:00
|
|
|
if (0 !== $result['return'])
|
2020-01-12 00:50:29 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-12 22:20:12 +01:00
|
|
|
$find_ok = $this->search_for_string($result['output'], 'ok');
|
2020-01-12 00:50:29 +01:00
|
|
|
if (!$find_ok)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$uid = false;
|
2020-01-12 22:20:12 +01:00
|
|
|
foreach ($result['output'] as $line)
|
2020-01-12 00:50:29 +01:00
|
|
|
{
|
|
|
|
$matches = [];
|
|
|
|
preg_match('#reference=([0-9]+)#u', $line, $matches);
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-12 00:50:29 +01:00
|
|
|
if ($matches[1] ?? false)
|
|
|
|
{
|
|
|
|
$uid = $matches[1];
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-12 00:50:29 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
if (false === $uid)
|
2020-01-12 00:50:29 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Method called to read SMSs of the number.
|
|
|
|
*
|
2020-01-12 00:50:29 +01:00
|
|
|
* @return array : Array of the sms reads
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function read(): array
|
2020-01-12 00:50:29 +01:00
|
|
|
{
|
|
|
|
if (!$this->unlock_sim())
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
2020-01-12 23:57:24 +01:00
|
|
|
|
|
|
|
$command_parts = [
|
2020-01-17 18:19:25 +01:00
|
|
|
PWD.'/bin/gammu_get_unread_sms.py',
|
2020-01-12 23:57:24 +01:00
|
|
|
escapeshellarg($this->datas['config_file']),
|
|
|
|
];
|
|
|
|
|
|
|
|
$return = $this->exec_command($command_parts);
|
2020-01-17 18:19:25 +01:00
|
|
|
if (0 !== $return['return'])
|
2020-01-12 23:57:24 +01:00
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$smss = [];
|
|
|
|
foreach ($return['output'] as $line)
|
|
|
|
{
|
|
|
|
$decode = json_decode($line, true);
|
2020-01-17 18:19:25 +01:00
|
|
|
if (null === $decode)
|
2020-01-12 23:57:24 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$smss[] = [
|
|
|
|
'at' => $decode['at'],
|
|
|
|
'text' => $decode['text'],
|
|
|
|
'origin' => $decode['number'],
|
|
|
|
'destination' => $this->number,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $smss;
|
2020-01-12 00:50:29 +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-12 00:50:29 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function test(): bool
|
2020-01-12 00:50:29 +01:00
|
|
|
{
|
2020-01-12 22:20:12 +01:00
|
|
|
//Always return true as we cannot test because we would be needing a root account
|
2020-01-12 00:50:29 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Method called on reception of a status update notification for a SMS.
|
|
|
|
*
|
2020-01-12 00:50:29 +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-12 00:50:29 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Function to unlock pin.
|
|
|
|
*
|
2020-01-12 00:50:29 +01:00
|
|
|
* @return bool : False on error, true else
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
private function unlock_sim(): bool
|
2020-01-12 00:50:29 +01:00
|
|
|
{
|
|
|
|
if (!$this->datas['pin'])
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$command_parts = [
|
|
|
|
'gammu',
|
|
|
|
'--config',
|
|
|
|
escapeshellarg($this->datas['config_file']),
|
|
|
|
'entersecuritycode',
|
|
|
|
'PIN',
|
|
|
|
escapeshellarg($this->datas['pin']),
|
|
|
|
];
|
|
|
|
|
|
|
|
$result = $this->exec_command($command_parts);
|
|
|
|
|
|
|
|
//Check security status
|
|
|
|
$command_parts = [
|
|
|
|
'gammu',
|
|
|
|
'--config',
|
|
|
|
escapeshellarg($this->datas['config_file']),
|
|
|
|
'getsecuritystatus',
|
|
|
|
];
|
|
|
|
|
|
|
|
$result = $this->exec_command($command_parts);
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
if (0 !== $result['return'])
|
2020-01-12 00:50:29 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->search_for_string($result['output'], 'nothing');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Function to execute a command and transmit it to Gammu.
|
|
|
|
*
|
2020-01-12 00:50:29 +01:00
|
|
|
* @param array $command_parts : Commands parts to be join with a space
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2020-01-12 00:50:29 +01:00
|
|
|
* @return array : ['return' => int:return code of command, 'output' => array:each raw is a line of the output]
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
private function exec_command(array $command_parts): array
|
2020-01-12 00:50:29 +01:00
|
|
|
{
|
2020-01-12 22:20:12 +01:00
|
|
|
//Add redirect of error to stdout
|
|
|
|
$command_parts[] = '2>&1';
|
|
|
|
|
2020-01-12 00:50:29 +01:00
|
|
|
$command = implode(' ', $command_parts);
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-12 00:50:29 +01:00
|
|
|
$output = [];
|
|
|
|
$return_var = null;
|
|
|
|
exec($command, $output, $return_var);
|
|
|
|
|
|
|
|
return ['return' => (int) $return_var, 'output' => $output];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Function to search a string in the output of an executer command.
|
|
|
|
*
|
|
|
|
* @param array $output : Text to search in where each raw is a line
|
2020-01-12 00:50:29 +01:00
|
|
|
* @param string $search : Text to search for
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2020-01-12 00:50:29 +01:00
|
|
|
* @return bool : True if found, false else
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
private function search_for_string(array $output, string $search): bool
|
2020-01-12 00:50:29 +01:00
|
|
|
{
|
|
|
|
$find = false;
|
|
|
|
foreach ($output as $line)
|
|
|
|
{
|
|
|
|
$find = mb_stristr($line, $search);
|
2020-01-17 18:19:25 +01:00
|
|
|
if (false !== $find)
|
2020-01-12 00:50:29 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (bool) $find;
|
|
|
|
}
|
|
|
|
}
|