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 D3strukt0r\VotifierClient\Exception\NotVotifierException; |
16
|
|
|
use D3strukt0r\VotifierClient\Exception\NuVotifierChallengeInvalidException; |
17
|
|
|
use D3strukt0r\VotifierClient\Exception\NuVotifierException; |
18
|
|
|
use D3strukt0r\VotifierClient\Exception\NuVotifierSignatureInvalidException; |
19
|
|
|
use D3strukt0r\VotifierClient\Exception\NuVotifierUnknownServiceException; |
20
|
|
|
use D3strukt0r\VotifierClient\Exception\NuVotifierUsernameTooLongException; |
21
|
|
|
use D3strukt0r\VotifierClient\Exception\PackageNotReceivedException; |
22
|
|
|
use D3strukt0r\VotifierClient\Exception\PackageNotSentException; |
23
|
|
|
use D3strukt0r\VotifierClient\ServerConnection; |
24
|
|
|
use D3strukt0r\VotifierClient\VoteType\VoteInterface; |
25
|
|
|
|
26
|
|
|
use function count; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The Class to access a server which uses the plugin "NuVotifier". |
30
|
|
|
*/ |
31
|
|
|
class NuVotifier extends ClassicVotifier |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var bool use version 2 of the protocol |
35
|
|
|
*/ |
36
|
|
|
private $protocolV2 = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string|null The token from the config.yml. |
40
|
|
|
*/ |
41
|
|
|
private $token; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Checks whether the connection uses the version 2 protocol. |
45
|
|
|
* |
46
|
|
|
* @return bool returns true, if using the new version of NuVotifier or false otherwise |
47
|
|
|
*/ |
48
|
|
|
public function isProtocolV2(): bool |
49
|
|
|
{ |
50
|
|
|
return $this->protocolV2; |
51
|
4 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Sets whether to use version 2 of the protocol. |
55
|
|
|
* |
56
|
|
|
* @param bool $isProtocolV2 Whether to use version 2 of the protocol |
57
|
|
|
* |
58
|
4 |
|
* @return $this returns the class itself, for doing multiple things at once |
59
|
4 |
|
*/ |
60
|
4 |
|
public function setProtocolV2(bool $isProtocolV2): self |
61
|
|
|
{ |
62
|
|
|
$this->protocolV2 = $isProtocolV2; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
4 |
|
} |
66
|
|
|
|
67
|
4 |
|
/** |
68
|
4 |
|
* Gets the token from the config.yml. |
69
|
4 |
|
* |
70
|
|
|
* @return string|null returns The token from the config.yml. |
71
|
|
|
*/ |
72
|
|
|
public function getToken(): ?string |
73
|
|
|
{ |
74
|
|
|
return $this->token; |
75
|
|
|
} |
76
|
1 |
|
|
77
|
|
|
/** |
78
|
1 |
|
* Sets the token from the config.yml. |
79
|
|
|
* |
80
|
|
|
* @param string|null $token The token from the config.yml. |
81
|
|
|
* |
82
|
|
|
* @return $this returns the class itself, for doing multiple things at once |
83
|
|
|
*/ |
84
|
1 |
|
public function setToken(?string $token): self |
85
|
|
|
{ |
86
|
1 |
|
$this->token = $token; |
87
|
1 |
|
|
88
|
1 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
* |
94
|
|
|
* @throws NotVotifierException |
95
|
|
|
* @throws PackageNotSentException |
96
|
|
|
* @throws PackageNotReceivedException |
97
|
|
|
* @throws NuVotifierServerErrorException |
98
|
|
|
*/ |
99
|
|
|
public function send(ServerConnection $connection, VoteInterface $vote): void |
100
|
|
|
{ |
101
|
|
|
if (!$this->isProtocolV2()) { |
102
|
1 |
|
parent::send($connection, $vote); |
103
|
|
|
|
104
|
1 |
|
return; |
105
|
|
|
} |
106
|
1 |
|
|
107
|
1 |
|
if (!$this->verifyConnection($header = $connection->receive(64))) { |
108
|
1 |
|
throw new NotVotifierException(); |
109
|
1 |
|
} |
110
|
1 |
|
$header_parts = explode(' ', $header); |
111
|
|
|
$challenge = mb_substr($header_parts[2], 0, -1); |
112
|
|
|
|
113
|
1 |
|
if (false === $connection->send($this->preparePackageV2($vote, $challenge))) { |
114
|
1 |
|
throw new PackageNotSentException(); |
115
|
|
|
} |
116
|
1 |
|
|
117
|
|
|
if (!$response = $connection->receive(256)) { |
118
|
|
|
throw new PackageNotReceivedException(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/* |
122
|
|
|
* https://github.com/NuVotifier/NuVotifier/blob/master/common/src/main/java/com/vexsoftware/votifier/net/protocol/VotifierProtocol2Decoder.java |
123
|
|
|
* Examples: |
124
|
|
|
* {"status":"ok"} |
125
|
|
|
* {"status":"error","cause":"CorruptedFrameException","error":"Challenge is not valid"} |
126
|
|
|
* {"status":"error","cause":"CorruptedFrameException","error":"Unknown service 'xxx'"} |
127
|
|
|
* {"status":"error","cause":"CorruptedFrameException","error":"Signature is not valid (invalid token?)"} |
128
|
|
|
* {"status":"error","cause":"CorruptedFrameException","error":"Username too long"} (over 16 characters) |
129
|
|
|
*/ |
130
|
|
|
$result = json_decode($response); |
131
|
|
|
if ('ok' !== $result->status) { |
132
|
|
|
if ('Challenge is not valid' === $result->error) { |
133
|
|
|
throw new NuVotifierChallengeInvalidException(); |
134
|
|
|
} elseif (preg_match('/Unknown service \'(.*)\'/', $result->error, $matches)) { |
135
|
|
|
throw new NuVotifierUnknownServiceException(); |
136
|
|
|
} elseif ('Signature is not valid (invalid token?)' === $result->error) { |
137
|
|
|
throw new NuVotifierSignatureInvalidException(); |
138
|
|
|
} elseif ('Username too long' === $result->error) { |
139
|
|
|
throw new NuVotifierUsernameTooLongException(); |
140
|
|
|
} |
141
|
|
|
throw new NuVotifierException('Unknown NuVotifier Exception'); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Verifies that the connection is correct. |
147
|
|
|
* |
148
|
|
|
* @param string|null $header (Required) The header that the plugin usually sends |
149
|
|
|
* |
150
|
|
|
* @return bool returns true if connections is available, otherwise false |
151
|
|
|
*/ |
152
|
|
|
private function verifyConnection(?string $header): bool |
153
|
|
|
{ |
154
|
|
|
$header_parts = explode(' ', $header); |
155
|
|
|
if (null === $header || false === mb_strpos($header, 'VOTIFIER') || 3 !== count($header_parts)) { |
156
|
|
|
return false; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return true; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Prepares the vote package to be sent as version 2 protocol package. |
164
|
|
|
* |
165
|
|
|
* @param VoteInterface $vote (Required) The vote package with information |
166
|
|
|
* @param string $challenge (Required) The challenge sent by the server |
167
|
|
|
* |
168
|
|
|
* @return string returns the string to be sent to the server |
169
|
|
|
*/ |
170
|
|
|
private function preparePackageV2(VoteInterface $vote, string $challenge): string |
171
|
|
|
{ |
172
|
|
|
$payloadJson = json_encode( |
173
|
|
|
[ |
174
|
|
|
'username' => $vote->getUsername(), |
175
|
|
|
'serviceName' => $vote->getServiceName(), |
176
|
|
|
'timestamp' => $vote->getTimestamp(), |
177
|
|
|
'address' => $vote->getAddress(), |
178
|
|
|
'challenge' => $challenge, |
179
|
|
|
] |
180
|
|
|
); |
181
|
|
|
$signature = base64_encode(hash_hmac('sha256', $payloadJson, $this->token, true)); |
182
|
|
|
$messageJson = json_encode(['signature' => $signature, 'payload' => $payloadJson]); |
183
|
|
|
|
184
|
|
|
return pack('nn', 0x733a, mb_strlen($messageJson)) . $messageJson; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|