1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Votifier PHP Client |
5
|
|
|
* |
6
|
|
|
* @package VotifierClient |
7
|
|
|
* @author Manuele Vaccari <[email protected]> |
8
|
|
|
* @copyright Copyright (c) 2017-2020 Manuele Vaccari <[email protected]> |
9
|
|
|
* @license https://github.com/D3strukt0r/votifier-client-php/blob/master/LICENSE.txt GNU General Public License v3.0 |
10
|
|
|
* @link https://github.com/D3strukt0r/votifier-client-php |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace D3strukt0r\VotifierClient\ServerType; |
14
|
|
|
|
15
|
|
|
use function count; |
16
|
|
|
use D3strukt0r\VotifierClient\Messages; |
17
|
|
|
use D3strukt0r\VotifierClient\ServerConnection; |
18
|
|
|
use D3strukt0r\VotifierClient\VoteType\VoteInterface; |
19
|
|
|
use Exception; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The Class to access a server which uses the plugin "NuVotifier". |
23
|
|
|
*/ |
24
|
|
|
class NuVotifier extends ClassicVotifier |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var bool use version 2 of the protocol |
28
|
|
|
*/ |
29
|
|
|
private $protocolV2; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string|null The token from the config.yml. |
33
|
|
|
*/ |
34
|
|
|
private $token; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Creates the NuVotifier object. |
38
|
|
|
* |
39
|
|
|
* @param string $host (Required) The domain or ip to connect to Votifier |
40
|
|
|
* @param int|null $port (Required) The port which votifier uses on the server |
41
|
|
|
* @param string|null $publicKey (Required) The key which is generated by the plugin. Only needed if using v1! |
42
|
|
|
* @param bool $protocolV2 (Optional) Use version 2 of the protocol (Recommended) |
43
|
|
|
* @param string|null $token (Optional) To use version 2 protocol the token is needed from the config.yml. |
44
|
|
|
* |
45
|
|
|
* @throws Exception |
46
|
|
|
*/ |
47
|
4 |
|
public function __construct( |
48
|
|
|
string $host, |
49
|
|
|
?int $port, |
50
|
|
|
?string $publicKey, |
51
|
|
|
bool $protocolV2 = false, |
52
|
|
|
?string $token = null |
53
|
|
|
) { |
54
|
4 |
|
if (null === $publicKey) { |
55
|
4 |
|
if ($protocolV2) { |
56
|
4 |
|
$publicKey = 'empty'; |
57
|
|
|
} else { |
58
|
|
|
throw new Exception('When not using v2 for the protocol, the public key is required!'); |
59
|
|
|
} |
60
|
|
|
} |
61
|
4 |
|
parent::__construct($host, $port, $publicKey); |
62
|
|
|
|
63
|
4 |
|
$this->protocolV2 = $protocolV2; |
64
|
4 |
|
$this->token = $token; |
65
|
4 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Checks whether the connection uses the version 2 protocol. |
69
|
|
|
* |
70
|
|
|
* @return bool returns true, if using the new version of NuVotifier or false otherwise |
71
|
|
|
*/ |
72
|
1 |
|
public function isProtocolV2(): bool |
73
|
|
|
{ |
74
|
1 |
|
return $this->protocolV2; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
1 |
|
public function verifyConnection(?string $header): bool |
81
|
|
|
{ |
82
|
1 |
|
$header_parts = explode(' ', $header); |
83
|
1 |
|
if (null === $header || false === mb_strpos($header, 'VOTIFIER') || 3 !== count($header_parts)) { |
84
|
1 |
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Prepares the vote package to be sent as version 2 protocol package. |
92
|
|
|
* |
93
|
|
|
* @param VoteInterface $vote (Required) The vote package with information |
94
|
|
|
* @param string $challenge (Required) The challenge sent by the server |
95
|
|
|
* |
96
|
|
|
* @return string returns the string to be sent to the server |
97
|
|
|
*/ |
98
|
1 |
|
public function preparePackageV2(VoteInterface $vote, string $challenge): string |
99
|
|
|
{ |
100
|
1 |
|
$payloadJson = json_encode( |
101
|
|
|
[ |
102
|
1 |
|
'username' => $vote->getUsername(), |
103
|
1 |
|
'serviceName' => $vote->getServiceName(), |
104
|
1 |
|
'timestamp' => $vote->getTimestamp(), |
105
|
1 |
|
'address' => $vote->getAddress(), |
106
|
1 |
|
'challenge' => $challenge, |
107
|
|
|
] |
108
|
|
|
); |
109
|
1 |
|
$signature = base64_encode(hash_hmac('sha256', $payloadJson, $this->token, true)); |
110
|
1 |
|
$messageJson = json_encode(['signature' => $signature, 'payload' => $payloadJson]); |
111
|
|
|
|
112
|
1 |
|
return pack('nn', 0x733a, mb_strlen($messageJson)).$messageJson; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
public function send(ServerConnection $connection, VoteInterface $vote): void |
119
|
|
|
{ |
120
|
|
|
if (!$this->isProtocolV2()) { |
121
|
|
|
parent::send($connection, $vote); |
122
|
|
|
|
123
|
|
|
return; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if (!$this->verifyConnection($header = $connection->receive(64))) { |
127
|
|
|
throw new Exception(Messages::get(Messages::NOT_VOTIFIER)); |
128
|
|
|
} |
129
|
|
|
$header_parts = explode(' ', $header); |
130
|
|
|
$challenge = mb_substr($header_parts[2], 0, -1); |
131
|
|
|
$payload = $this->preparePackageV2($vote, $challenge); |
132
|
|
|
|
133
|
|
|
if (false === $connection->send($payload)) { |
134
|
|
|
throw new Exception(Messages::get(Messages::NOT_SENT_PACKAGE)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if (!$response = $connection->receive(256)) { |
138
|
|
|
throw new Exception(Messages::get(Messages::NOT_RECEIVED_PACKAGE)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$result = json_decode($response); |
142
|
|
|
if ('ok' !== $result->status) { |
143
|
|
|
throw new Exception(Messages::get(Messages::NUVOTIFIER_SERVER_ERROR, null, $result->cause, $result->error)); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|