Passed
Pull Request — master (#640)
by Vitor
03:36
created

HuaweiE3531   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 30
dl 0
loc 47
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A send() 0 31 4
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * SPDX-FileCopyrightText: 2024 Martin Keßler <[email protected]>
7
 * SPDX-License-Identifier: AGPL-3.0-or-later
8
 */
9
10
namespace OCA\TwoFactorGateway\Provider\Channel\SMS\Provider\Drivers;
11
12
use Exception;
13
use OCA\TwoFactorGateway\Exception\MessageTransmissionException;
14
use OCA\TwoFactorGateway\Provider\Channel\SMS\Provider\AProvider;
15
use OCP\Http\Client\IClient;
16
use OCP\Http\Client\IClientService;
17
18
/**
19
 * @method string getApi()
20
 * @method static setApi(string $api)
21
 */
22
class HuaweiE3531 extends AProvider {
23
	public const SCHEMA = [
24
		'id' => 'huawei_e3531',
25
		'name' => 'Huawei E3531',
26
		'fields' => [
27
			['field' => 'api', 'prompt' => 'Please enter the base URL of the Huawei E3531 stick: ', 'default' => 'http://192.168.8.1/api'],
28
		],
29
	];
30
	private IClient $client;
31
32
	public function __construct(
33
		IClientService $clientService,
34
	) {
35
		$this->client = $clientService->newClient();
36
	}
37
38
	#[\Override]
39
	public function send(string $identifier, string $message) {
40
		$url = $this->getApi();
41
42
		try {
43
			$sessionTokenResponse = $this->client->get("$url/webserver/SesTokInfo");
44
			$sessionTokenXml = simplexml_load_string((string)$sessionTokenResponse->getBody());
45
			if ($sessionTokenXml === false) {
46
				throw new Exception();
47
			}
48
49
			$date = date('Y-m-d H:i:s');
50
			$messageEscaped = htmlspecialchars($message, ENT_XML1);
51
52
			$sendResponse = $this->client->post("$url/sms/send-sms", [
53
				'body' => "<request><Index>-1</Index><Phones><Phone>$identifier</Phone></Phones><Sca/><Content>$messageEscaped</Content><Length>-1</Length><Reserved>1</Reserved><Date>$date</Date></request>",
54
				'headers' => [
55
					'Cookie' => (string)$sessionTokenXml->SesInfo,
56
					'X-Requested-With' => 'XMLHttpRequest',
57
					'__RequestVerificationToken' => (string)$sessionTokenXml->TokInfo,
58
					'Content-Type' => 'text/xml',
59
				],
60
			]);
61
			$body = (string)$sendResponse->getBody();
62
			$sendXml = simplexml_load_string($body);
63
		} catch (Exception $ex) {
64
			throw new MessageTransmissionException();
65
		}
66
67
		if ((string)$sendXml !== 'OK') {
68
			throw new MessageTransmissionException();
69
		}
70
	}
71
}
72