diff options
author | Martin Keßler <martin@moegger.de> | 2019-03-20 23:21:36 +0300 |
---|---|---|
committer | Martin Keßler <martin@moegger.de> | 2019-03-21 14:24:44 +0300 |
commit | 066247dd0ee1bd59145c121215f0051b0bd7b0b4 (patch) | |
tree | 7b4a6929a628e2c32e3361c4ce080e2e0f747eb3 /lib/Service | |
parent | 89e65085d8d7e5941ec253381c2fdbc111e848ac (diff) |
add provider to send SMS via Huawei E3531 3G stick
Diffstat (limited to 'lib/Service')
-rw-r--r-- | lib/Service/Gateway/SMS/Provider/HuaweiE3531.php | 91 | ||||
-rw-r--r-- | lib/Service/Gateway/SMS/Provider/HuaweiE3531Config.php | 63 | ||||
-rw-r--r-- | lib/Service/Gateway/SMS/Provider/ProviderFactory.php | 2 |
3 files changed, 156 insertions, 0 deletions
diff --git a/lib/Service/Gateway/SMS/Provider/HuaweiE3531.php b/lib/Service/Gateway/SMS/Provider/HuaweiE3531.php new file mode 100644 index 0000000..214a931 --- /dev/null +++ b/lib/Service/Gateway/SMS/Provider/HuaweiE3531.php @@ -0,0 +1,91 @@ +<?php + +declare(strict_types=1); + +/** + * @author Martin Keßler <martin@moegger.de> + * + * Nextcloud - Two-factor Gateway + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\TwoFactorGateway\Service\Gateway\SMS\Provider; + +use Exception; +use OCA\TwoFactorGateway\Exception\SmsTransmissionException; +use OCP\Http\Client\IClient; +use OCP\Http\Client\IClientService; +use OCP\IConfig; + +class HuaweiE3531 implements IProvider { + + const PROVIDER_ID = 'huawei_e3531'; + + /** @var IClient */ + private $client; + + /** @var HuaweiE3531Config */ + private $config; + + public function __construct(IClientService $clientService, + HuaweiE3531Config $config) { + $this->client = $clientService->newClient(); + $this->config = $config; + } + + /** + * @param string $identifier + * @param string $message + * + * @throws SmsTransmissionException + */ + public function send(string $identifier, string $message) { + $config = $this->getConfig(); + $url = $config->getUrl(); + + try { + $sessionTokenResponse = $this->client->get("$url/webserver/SesTokInfo"); + $sessionTokenXml = simplexml_load_string($sessionTokenResponse->getBody()); + + $date = date('Y-m-d H:i:s'); + $messageEscaped = htmlspecialchars($message, ENT_XML1); + + $sendResponse = $this->client->post("$url/sms/send-sms", [ + 'body' => "<request><Index>-1</Index><Phones><Phone>$identifier</Phone></Phones><Sca/><Content>$messageEscaped</Content><Length>-1</Length><Reserved>1</Reserved><Date>$date</Date></request>", + 'headers' => [ + 'Cookie' => $sessionTokenXml->SesInfo, + 'X-Requested-With' => 'XMLHttpRequest', + '__RequestVerificationToken' => $sessionTokenXml->TokInfo, + 'Content-Type' => 'text/xml', + ], + ]); + $sendXml = simplexml_load_string($sendResponse->getBody()); + } catch (Exception $ex) { + throw new SmsTransmissionException(); + } + + if ((string) $sendXml !== "OK") { + throw new SmsTransmissionException(); + } + } + + /** + * @return HuaweiE3531Config + */ + public function getConfig(): IProviderConfig { + return $this->config; + } + +} diff --git a/lib/Service/Gateway/SMS/Provider/HuaweiE3531Config.php b/lib/Service/Gateway/SMS/Provider/HuaweiE3531Config.php new file mode 100644 index 0000000..e9d0b48 --- /dev/null +++ b/lib/Service/Gateway/SMS/Provider/HuaweiE3531Config.php @@ -0,0 +1,63 @@ +<?php + +declare(strict_types=1); + +/** + * @author Martin Keßler <martin@moegger.de> + * + * Nextcloud - Two-factor Gateway + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\TwoFactorGateway\Service\Gateway\SMS\Provider; + +use function array_intersect; +use OCA\TwoFactorGateway\AppInfo\Application; +use OCA\TwoFactorGateway\Exception\ConfigurationException; +use OCP\IConfig; + +class HuaweiE3531Config implements IProviderConfig { + + /** @var IConfig */ + private $config; + + public function __construct(IConfig $config) { + $this->config = $config; + } + + private function getOrFail(string $key): string { + $val = $this->config->getAppValue(Application::APP_NAME, $key, null); + if (is_null($val)) { + throw new ConfigurationException(); + } + return $val; + } + + public function getUrl(): string { + return $this->getOrFail('huawei_e3531_api'); + } + + public function setUrl(string $url) { + $this->config->setAppValue(Application::APP_NAME, 'huawei_e3531_api', $url); + } + + public function isComplete(): bool { + $set = $this->config->getAppKeys(Application::APP_NAME); + $expected = [ + 'huawei_e3531_api', + ]; + return count(array_intersect($set, $expected)) === count($expected); + } +} diff --git a/lib/Service/Gateway/SMS/Provider/ProviderFactory.php b/lib/Service/Gateway/SMS/Provider/ProviderFactory.php index bcb39d3..db11057 100644 --- a/lib/Service/Gateway/SMS/Provider/ProviderFactory.php +++ b/lib/Service/Gateway/SMS/Provider/ProviderFactory.php @@ -49,6 +49,8 @@ class ProviderFactory { return $this->container->query(EcallSMS::class); case VoipMs::PROVIDER_ID: return $this->container->query(VoipMs::class); + case HuaweiE3531::PROVIDER_ID: + return $this->container->query(HuaweiE3531::class); default: throw new InvalidSmsProviderException("Provider <$id> does not exist"); } |