2020-01-08 02:14:38 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 controllers\publics;
|
|
|
|
|
2020-03-04 04:18:26 +01:00
|
|
|
use Monolog\Handler\StreamHandler;
|
|
|
|
use Monolog\Logger;
|
|
|
|
|
2020-03-04 05:10:45 +01:00
|
|
|
/**
|
|
|
|
* Controller of callback pages, like sms status update notification.
|
|
|
|
*/
|
2020-01-08 02:14:38 +01:00
|
|
|
class Callback extends \descartes\Controller
|
|
|
|
{
|
2020-03-04 04:18:26 +01:00
|
|
|
private $logger;
|
2020-03-04 01:40:47 +01:00
|
|
|
private $user;
|
2020-01-08 02:14:38 +01:00
|
|
|
private $internal_user;
|
|
|
|
private $internal_sended;
|
|
|
|
private $internal_adapter;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
|
|
|
|
|
|
|
|
$this->internal_user = new \controllers\internals\User($bdd);
|
|
|
|
$this->internal_sended = new \controllers\internals\Sended($bdd);
|
|
|
|
$this->internal_adapter = new \controllers\internals\Adapter();
|
2020-03-04 01:40:47 +01:00
|
|
|
|
2020-03-04 04:18:26 +01:00
|
|
|
//Logger
|
|
|
|
$this->logger = new Logger('Callback ' . uniqid());
|
|
|
|
$this->logger->pushHandler(new StreamHandler(PWD_LOGS . '/callback.log', Logger::DEBUG));
|
|
|
|
|
|
|
|
//If invalid api key, quit with error
|
2020-03-04 01:40:47 +01:00
|
|
|
$this->user = false;
|
|
|
|
$api_key = $_GET['api_key'] ?? false;
|
|
|
|
if ($api_key)
|
|
|
|
{
|
|
|
|
$this->user = $this->internal_user->get_by_api_key($api_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->user)
|
|
|
|
{
|
|
|
|
http_response_code(401);
|
|
|
|
echo json_encode(['error' => 'Invalid API key. You must provide a valid GET or POST api_key param.']);
|
2020-03-04 05:10:45 +01:00
|
|
|
$this->logger->error('Callback call failed with invalid api key : ' . $api_key);
|
2020-03-04 01:40:47 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
2020-03-04 05:10:45 +01:00
|
|
|
|
2020-03-04 04:18:26 +01:00
|
|
|
$this->logger->info('Callback call succed for user id : ' . $this->user['id']);
|
2020-01-08 02:14:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Function call on a sended sms status change notification reception.
|
2020-03-04 05:10:45 +01:00
|
|
|
* We return nothing, and we let the adapter do his things.
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2020-01-08 02:14:38 +01:00
|
|
|
* @param string $adapter_name : Name of the adapter to use
|
2020-03-04 05:10:45 +01:00
|
|
|
*
|
2020-03-04 01:40:47 +01:00
|
|
|
* @return bool : true on success, false on error
|
2020-01-08 02:14:38 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function update_sended_status(string $adapter_name)
|
2020-01-08 02:14:38 +01:00
|
|
|
{
|
2020-03-04 04:18:26 +01:00
|
|
|
$this->logger->info('Callback status call with adapter name : ' . $adapter_name);
|
|
|
|
|
2020-01-08 02:14:38 +01:00
|
|
|
//Search for an adapter
|
|
|
|
$find_adapter = false;
|
|
|
|
$adapters = $this->internal_adapter->list_adapters();
|
|
|
|
foreach ($adapters as $adapter)
|
|
|
|
{
|
|
|
|
if (mb_strtolower($adapter['meta_name']) === mb_strtolower($adapter_name))
|
|
|
|
{
|
|
|
|
$find_adapter = $adapter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (false === $find_adapter)
|
|
|
|
{
|
2020-03-04 04:18:26 +01:00
|
|
|
$this->logger->error('Callback status use non existing adapter : ' . $adapter_name);
|
2020-03-04 05:10:45 +01:00
|
|
|
|
2020-01-08 02:14:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Instanciate adapter, check if status change is supported and if so call status change callback
|
|
|
|
$adapter_classname = $find_adapter['meta_classname'];
|
|
|
|
if (!$find_adapter['meta_support_status_change'])
|
|
|
|
{
|
2020-03-04 04:18:26 +01:00
|
|
|
$this->logger->error('Callback status use adapter ' . $adapter_name . ' which does not support status change.');
|
2020-03-04 05:10:45 +01:00
|
|
|
|
2020-01-08 02:14:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$callback_return = $adapter_classname::status_change_callback();
|
|
|
|
if (!$callback_return)
|
|
|
|
{
|
2020-03-04 04:18:26 +01:00
|
|
|
$this->logger->error('Callback status with adapter ' . $adapter_name . ' failed on adapter compute.');
|
2020-03-04 05:10:45 +01:00
|
|
|
|
2020-01-08 02:14:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-05 23:07:07 +01:00
|
|
|
$sended = $this->internal_sended->get_by_uid_and_adapter_for_user($this->user['id'], $callback_return['uid'], $adapter_classname);
|
2020-01-08 02:14:38 +01:00
|
|
|
if (!$sended)
|
|
|
|
{
|
2020-03-04 04:18:26 +01:00
|
|
|
$this->logger->error('Callback status try update inexisting message with uid = ' . $callback_return['uid'] . '.');
|
2020-03-04 05:10:45 +01:00
|
|
|
|
2020-01-08 02:14:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-04 05:10:45 +01:00
|
|
|
|
2020-03-04 04:18:26 +01:00
|
|
|
$this->logger->info('Callback status update message with uid ' . $callback_return['uid'] . '.');
|
2020-03-05 23:07:07 +01:00
|
|
|
$this->internal_sended->update_status_for_user($this->user['id'], $sended['id'], $callback_return['status']);
|
2020-01-08 02:14:38 +01:00
|
|
|
|
2020-03-04 01:40:47 +01:00
|
|
|
return true;
|
2020-01-08 02:14:38 +01:00
|
|
|
}
|
|
|
|
}
|