raspisms/adapters/GammuAdapter.php

443 lines
13 KiB
PHP
Raw Permalink Normal View History

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
/**
* Data used to configure interaction with the implemented service. (e.g : Api credentials, ports numbers, etc.).
2020-01-17 18:19:25 +01:00
*/
private $data;
2020-01-17 18:19:25 +01:00
/**
* Adapter constructor, called when instanciated by RaspiSMS.
*
* @param json string $data : JSON string of the data to configure interaction with the implemented service
2020-01-17 18:19:25 +01:00
*/
public function __construct(string $data)
2020-01-17 18:19:25 +01:00
{
$this->data = json_decode($data, true);
2020-01-17 18:19:25 +01:00
}
/**
* 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-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 'gammu_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;
}
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-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
{
2020-07-04 21:35:39 +02:00
return 'Utilisation du logiciel Gammu qui doit être installé sur le serveur et configuré. Voir <a target="_blank" href="https://wammu.eu">https://wammu.eu</a>.<br/>
2021-02-04 16:44:13 +01:00
Pour plus d\'information sur l\'utilisation de ce type de téléphone, reportez-vous à <a href="https://documentation.raspisms.fr/users/adapters/gammu.html" target="_blank">la documentation sur le téléphone "Gammu".</a>
2020-07-04 21:35:39 +02:00
';
2020-01-17 18:19:25 +01:00
}
2020-01-12 00:50:29 +01:00
/**
* List of entries we want in data for the adapter.
2020-01-17 18:19:25 +01:00
*
2020-01-12 00:50:29 +01:00
* @return array : Every line is a field as an array with keys : name, title, description, required
*/
public static function meta_data_fields(): array
2020-01-12 00:50:29 +01:00
{
return [
[
'name' => 'config_file',
'title' => 'Fichier de configuration',
2020-07-04 22:55:06 +02:00
'description' => 'Chemin vers le fichier de configuration que Gammu devra utiliser pour se connecter au téléphone .',
2020-01-12 00:50:29 +01:00
'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,
],
];
}
/**
* Does the implemented service support reading smss.
*/
public static function meta_support_read(): bool
{
return true;
}
/**
* Does the implemented service support updating phone status.
*/
public static function meta_support_phone_status(): bool
{
return false;
}
2020-01-12 00:50:29 +01:00
/**
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-06-23 21:06:13 +02:00
/**
* 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-06-17 00:51:33 +02:00
* Does the implemented service support mms reception.
*/
public static function meta_support_mms_reception(): bool
{
return false;
}
2020-01-12 00:50:29 +01:00
/**
2021-06-17 00:51:33 +02:00
* Does the implemented service support mms sending.
2020-01-12 00:50:29 +01:00
*/
public static function meta_support_mms_sending(): bool
{
return false;
}
2021-06-17 00:51:33 +02:00
public static function meta_support_inbound_call_callback(): bool
{
return false;
}
2021-06-17 00:51:33 +02:00
public static function meta_support_end_call_callback(): bool
{
return false;
}
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-01-12 00:50:29 +01:00
{
$response = [
'error' => false,
'error_message' => null,
'uid' => null,
];
2020-01-12 00:50:29 +01:00
if (!$this->unlock_sim())
{
$response['error'] = true;
$response['error_message'] = 'Cannot unlock SIM.';
2020-06-23 21:06:13 +02:00
return $response;
2020-01-12 00:50:29 +01:00
}
2020-01-17 18:19:25 +01:00
2020-01-12 00:50:29 +01:00
$command_parts = [
'LC_ALL=C',
2020-01-12 00:50:29 +01:00
'gammu',
'--config',
escapeshellarg($this->data['config_file']),
2020-01-12 00:50:29 +01:00
'sendsms',
'TEXT',
escapeshellarg($destination),
'-textutf8',
2020-01-12 00:50:29 +01:00
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
{
$response['error'] = true;
$response['error_message'] = 'Gammu command failed.';
2020-06-23 21:06:13 +02:00
return $response;
2020-01-12 00:50:29 +01:00
}
$find_ok = $this->search_for_string($result['output'], 'ok');
2020-01-12 00:50:29 +01:00
if (!$find_ok)
{
$response['error'] = true;
$response['error_message'] = 'Cannot find output OK.';
2020-06-23 21:06:13 +02:00
return $response;
2020-01-12 00:50:29 +01:00
}
$uid = false;
foreach ($result['output'] as $line)
2020-01-12 00:50:29 +01:00
{
$matches = [];
preg_match('#=([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
{
$response['error'] = true;
$response['error_message'] = 'Cannot retrieve sms uid.';
2020-06-23 21:06:13 +02:00
return $response;
2020-01-12 00:50:29 +01:00
}
$response['uid'] = $uid;
2020-06-23 21:06:13 +02:00
return $response;
2020-01-12 00:50:29 +01:00
}
2020-01-17 18:19:25 +01:00
public function read(): array
2020-01-12 00:50:29 +01:00
{
$response = [
'error' => false,
'error_message' => null,
'smss' => [],
];
2020-01-12 00:50:29 +01:00
if (!$this->unlock_sim())
{
$response['error'] = true;
$response['error_message'] = 'Cannot unlock sim.';
2020-06-23 21:06:13 +02:00
return $response;
2020-01-12 00:50:29 +01:00
}
2020-01-12 23:57:24 +01:00
$command_parts = [
PWD . '/bin/gammu_get_unread_sms.py',
escapeshellarg($this->data['config_file']),
'--delete'
2020-01-12 23:57:24 +01:00
];
$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
{
$response['error'] = true;
$response['error_message'] = 'Gammu command return failed.';
2020-06-23 21:06:13 +02:00
return $response;
2020-01-12 23:57:24 +01:00
}
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;
}
$response['smss'][] = [
2020-01-12 23:57:24 +01:00
'at' => $decode['at'],
'text' => $decode['text'],
'origin' => $decode['number'],
];
}
return $response;
2020-01-12 00:50:29 +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-12 00:50:29 +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
public static function status_change_callback()
2020-01-12 00:50:29 +01:00
{
return false;
}
2020-06-23 21:06:13 +02:00
public static function reception_callback(): array
{
return [];
}
2021-06-17 00:51:33 +02:00
public static function inbound_call_callback(): array
{
return [];
}
2021-06-17 00:51:33 +02:00
public static function end_call_callback(): array
{
return [];
}
2020-06-23 21:06:13 +02:00
2020-01-12 00:50:29 +01:00
/**
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->data['pin'])
2020-01-12 00:50:29 +01:00
{
return true;
}
// The command returns 123 on failed execution (even if SIM is already unlocked), and returns 0 if unlock was successful
// We can directly return true if command was succesful
2020-01-12 00:50:29 +01:00
$command_parts = [
'LC_ALL=C',
2020-01-12 00:50:29 +01:00
'gammu',
'--config',
escapeshellarg($this->data['config_file']),
2020-01-12 00:50:29 +01:00
'entersecuritycode',
'PIN',
escapeshellarg($this->data['pin']),
2020-01-12 00:50:29 +01:00
];
$result = $this->exec_command($command_parts);
if (0 === $result['return'])
{
return true;
}
2020-01-12 00:50:29 +01:00
//Check security status
// The command returns 0 regardless of the SIM security state
2020-01-12 00:50:29 +01:00
$command_parts = [
'LC_ALL=C',
2020-01-12 00:50:29 +01:00
'gammu',
'--config',
escapeshellarg($this->data['config_file']),
2020-01-12 00:50:29 +01:00
'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
{
//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;
}
}