|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ushahidi\DataSource\Sislog; |
| 4 | + |
| 5 | +/** |
| 6 | + * Sislog Data Provider |
| 7 | + * |
| 8 | + * @author Ushahidi Team <team@ushahidi.com> |
| 9 | + * @package DataSource\Sislog |
| 10 | + * @copyright 2024 Ushahidi |
| 11 | + * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License Version 3 (GPLv3) |
| 12 | + */ |
| 13 | + |
| 14 | +use Illuminate\Routing\Router; |
| 15 | +use Illuminate\Support\Facades\Log; |
| 16 | +use Ushahidi\DataSource\Contracts\MessageType; |
| 17 | +use Ushahidi\DataSource\Contracts\MessageStatus; |
| 18 | +use Ushahidi\DataSource\Contracts\CallbackDataSource; |
| 19 | +use Ushahidi\DataSource\Contracts\OutgoingDataSource; |
| 20 | +use Ushahidi\DataSource\Concerns\MapsInboundFields; |
| 21 | + |
| 22 | +class Sislog implements CallbackDataSource, OutgoingDataSource |
| 23 | +{ |
| 24 | + use MapsInboundFields; |
| 25 | + |
| 26 | + protected $config; |
| 27 | + |
| 28 | + /** |
| 29 | + * Client to talk to the sislog API |
| 30 | + * |
| 31 | + * @var \Vonage\Message\Client |
| 32 | + */ |
| 33 | + private $client; |
| 34 | + |
| 35 | + protected $defaultServer = ''; |
| 36 | + |
| 37 | + /** |
| 38 | + * Constructor function for DataSource |
| 39 | + */ |
| 40 | + public function __construct(array $config, \GuzzleHttp\Client $client = null) |
| 41 | + { |
| 42 | + $this->config = $config; |
| 43 | + $this->client = $client; |
| 44 | + } |
| 45 | + |
| 46 | + public function getName() |
| 47 | + { |
| 48 | + return 'Sislog'; |
| 49 | + } |
| 50 | + |
| 51 | + public function getId() |
| 52 | + { |
| 53 | + return strtolower($this->getName()); |
| 54 | + } |
| 55 | + |
| 56 | + public function getServices() |
| 57 | + { |
| 58 | + return [MessageType::SMS]; |
| 59 | + } |
| 60 | + |
| 61 | + public function getOptions() |
| 62 | + { |
| 63 | + return [ |
| 64 | + 'server_url' => [ |
| 65 | + 'label' => 'FrontlineSMS Server URL', |
| 66 | + 'input' => 'text', |
| 67 | + 'description' => 'The URL where the Sislog server is installed, i.e. https://server.url.com/', |
| 68 | + 'rules' => ['required'] |
| 69 | + ], |
| 70 | + 'from' => [ |
| 71 | + 'label' => 'From', |
| 72 | + 'input' => 'text', |
| 73 | + 'description' => 'The from number', |
| 74 | + 'rules' => ['required'] |
| 75 | + ], |
| 76 | + 'api_username' => [ |
| 77 | + 'label' => 'Username', |
| 78 | + 'input' => 'text', |
| 79 | + 'description' => 'The API username', |
| 80 | + 'rules' => ['required'] |
| 81 | + ], |
| 82 | + 'api_password' => [ |
| 83 | + 'label' => 'Password', |
| 84 | + 'input' => 'text', |
| 85 | + 'description' => 'The API password', |
| 86 | + 'rules' => ['required'] |
| 87 | + ], |
| 88 | + // 'api_secret' => [ |
| 89 | + // 'label' => 'API secret', |
| 90 | + // 'input' => 'text', |
| 91 | + // 'description' => 'The API secret', |
| 92 | + // 'rules' => ['required'] |
| 93 | + // ] |
| 94 | + ]; |
| 95 | + } |
| 96 | + |
| 97 | + public function getInboundFields() |
| 98 | + { |
| 99 | + return [ |
| 100 | + 'Message' => 'text' |
| 101 | + ]; |
| 102 | + } |
| 103 | + |
| 104 | + public function isUserConfigurable() |
| 105 | + { |
| 106 | + return true; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @return mixed |
| 111 | + */ |
| 112 | + public function send($to, $message, $title = "", $contact_type = null) |
| 113 | + { |
| 114 | + // Obtain server url, ensure it ends with '/' |
| 115 | + $serverUrl = $this->config['server_url'] ?? $this->defaultServer; |
| 116 | + if (substr($serverUrl, -1) != '/') { |
| 117 | + $serverUrl = $serverUrl . '/'; |
| 118 | + } |
| 119 | + // Check we have the required config |
| 120 | + if (!isset($this->config['api_username']) || !isset($this->config['api_password'])) { |
| 121 | + Log::warning('Could not send message with Sislog, incomplete config'); |
| 122 | + return [MessageStatus::FAILED, false]; |
| 123 | + } |
| 124 | + |
| 125 | + $auth = 'Authorization: Basic ' . base64_encode("$this->config['api_username']:$this->config['api_password']"); |
| 126 | + |
| 127 | + |
| 128 | + // Prepare data to send to frontline cloud |
| 129 | + $data = [ |
| 130 | + "From" => "", |
| 131 | + "To" => $to, |
| 132 | + "Content" => $message |
| 133 | + // "ClientReference" => $message |
| 134 | + ]; |
| 135 | + |
| 136 | + // Make a POST request to send the data to frontline cloud |
| 137 | + |
| 138 | + try { |
| 139 | + $response = $this->client->request('POST', $serverUrl . $this->apiUrl, [ |
| 140 | + 'headers' => [ |
| 141 | + 'Accept' => 'application/json', |
| 142 | + 'Content-Type' => 'application/json', |
| 143 | + 'Authorization' => $auth |
| 144 | + ], |
| 145 | + 'json' => $data |
| 146 | + ]); |
| 147 | + // Successfully executed the request |
| 148 | + |
| 149 | + if ($response->getStatusCode() === 200) { |
| 150 | + return [MessageStatus::SENT, false]; |
| 151 | + } |
| 152 | + |
| 153 | + // Log warning to log file. |
| 154 | + $status = $response->getStatusCode(); |
| 155 | + Log::warning( |
| 156 | + 'Could not make a successful POST request', |
| 157 | + ['message' => $response->messages[$status], 'status' => $status] |
| 158 | + ); |
| 159 | + } catch (\GuzzleHttp\Exception\ClientException | \GuzzleHttp\Exception\RequestException $e) { |
| 160 | + // Log warning to log file. |
| 161 | + Log::warning( |
| 162 | + 'Could not make a successful POST request', |
| 163 | + ['message' => $e->getMessage()] |
| 164 | + ); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + public static function registerRoutes(Router $router) |
| 169 | + { |
| 170 | + $router->post('sms/sislog', 'Ushahidi\DataSource\Sislog\SislogController@handleRequest'); |
| 171 | + } |
| 172 | + |
| 173 | + public function verifySecret($secret) |
| 174 | + { |
| 175 | + if (isset($this->config['secret']) and $secret === $this->config['secret']) { |
| 176 | + return true; |
| 177 | + } |
| 178 | + |
| 179 | + return false; |
| 180 | + } |
| 181 | +} |
0 commit comments