|
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\Votifier\Client\Server; |
|
14
|
|
|
|
|
15
|
|
|
use D3strukt0r\Votifier\Client\Exception\NotVotifierException; |
|
16
|
|
|
use D3strukt0r\Votifier\Client\Exception\Socket\NoConnectionException; |
|
17
|
|
|
use D3strukt0r\Votifier\Client\Exception\Socket\PackageNotReceivedException; |
|
18
|
|
|
use D3strukt0r\Votifier\Client\Exception\Socket\PackageNotSentException; |
|
19
|
|
|
use D3strukt0r\Votifier\Client\Vote\VoteInterface; |
|
20
|
|
|
use DateTime; |
|
21
|
|
|
use InvalidArgumentException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The Class to access a server which uses the classic "Votifier" plugin. |
|
25
|
|
|
*/ |
|
26
|
|
|
class Votifier extends GenericServer |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
* |
|
31
|
|
|
* @throws InvalidArgumentException If one required parameter wasn't set |
|
32
|
|
|
* @throws NoConnectionException If connection couldn't be established |
|
33
|
|
|
* @throws PackageNotReceivedException If there was an error receiving the package |
|
34
|
|
|
* @throws PackageNotSentException If there was an error sending the package |
|
35
|
|
|
* @throws NotVotifierException If the server we are connected to is not a valid Votifier server |
|
36
|
|
|
*/ |
|
37
|
13 |
|
public function sendVote(VoteInterface ...$votes): void |
|
38
|
|
|
{ |
|
39
|
|
|
// Check if all variables have been set, to create a connection |
|
40
|
13 |
|
$this->checkRequiredVariablesForSocket(); |
|
41
|
|
|
|
|
42
|
11 |
|
foreach ($votes as $vote) { |
|
43
|
|
|
// Connect to the server |
|
44
|
11 |
|
$socket = $this->getSocket(); |
|
45
|
11 |
|
$socket->open($this->getHost(), $this->getPort()); |
|
46
|
|
|
|
|
47
|
|
|
// Check whether the connection really belongs to a Votifier plugin |
|
48
|
10 |
|
if (!$this->verifyConnection($socket->read(64))) { |
|
49
|
1 |
|
throw new NotVotifierException(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// Update the timestamp of the vote being sent |
|
53
|
9 |
|
$vote->setTimestamp(new DateTime()); |
|
54
|
|
|
|
|
55
|
|
|
// Check if all variables have been set, to create a package |
|
56
|
9 |
|
$this->checkRequiredVariablesForPackage($vote); |
|
57
|
|
|
|
|
58
|
|
|
// Send the vote |
|
59
|
2 |
|
$socket->write($this->preparePackage($vote)); |
|
60
|
|
|
|
|
61
|
|
|
// Make sure to close the connection after package was sent |
|
62
|
1 |
|
$socket->__destruct(); |
|
63
|
|
|
} |
|
64
|
1 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Check that both host and port have been set. |
|
68
|
|
|
* |
|
69
|
|
|
* @throws InvalidArgumentException If one required parameter wasn't set |
|
70
|
|
|
*/ |
|
71
|
13 |
|
protected function checkRequiredVariablesForSocket(): void |
|
72
|
|
|
{ |
|
73
|
13 |
|
if (!isset($this->host, $this->port)) { |
|
74
|
|
|
// $countError = 0; |
|
75
|
2 |
|
$errorMessage = ''; |
|
76
|
|
|
|
|
77
|
2 |
|
if (null === $this->host) { |
|
78
|
2 |
|
$errorMessage .= 'The host variable wasn\'t set with "->setHost(...)".'; |
|
79
|
|
|
// ++$countError; |
|
80
|
|
|
} |
|
81
|
|
|
// Not needed, as port has a default value |
|
82
|
|
|
// if (null === $this->port) { |
|
83
|
|
|
// $errorMessage .= $countError > 0 ? ' ' : ''; |
|
84
|
|
|
// $errorMessage .= 'The port variable wasn\'t set with "->setPort(...)".'; |
|
85
|
|
|
// } |
|
86
|
|
|
|
|
87
|
2 |
|
throw new InvalidArgumentException($errorMessage); |
|
88
|
|
|
} |
|
89
|
11 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Check that service name, username, address, timestamp and public key have been set. |
|
93
|
|
|
* |
|
94
|
|
|
* @param VoteInterface $vote The vote to check |
|
95
|
|
|
* |
|
96
|
|
|
* @throws InvalidArgumentException If one required parameter wasn't set |
|
97
|
|
|
*/ |
|
98
|
9 |
|
protected function checkRequiredVariablesForPackage(VoteInterface $vote) |
|
99
|
|
|
{ |
|
100
|
|
|
if ( |
|
101
|
9 |
|
null === $vote->getServiceName() |
|
102
|
4 |
|
|| null === $vote->getUsername() |
|
103
|
3 |
|
|| null === $vote->getAddress() |
|
104
|
2 |
|
|| null === $vote->getTimestamp() |
|
105
|
9 |
|
|| !isset($this->publicKey) |
|
106
|
|
|
) { |
|
107
|
7 |
|
$countError = 0; |
|
108
|
7 |
|
$errorMessage = ''; |
|
109
|
|
|
|
|
110
|
7 |
|
if (null === $vote->getServiceName()) { |
|
111
|
5 |
|
$errorMessage .= 'The host variable wasn\'t set with "->setServiceName(...)".'; |
|
112
|
5 |
|
++$countError; |
|
113
|
|
|
} |
|
114
|
7 |
|
if (null === $vote->getUsername()) { |
|
115
|
5 |
|
$errorMessage .= $countError > 0 ? ' ' : ''; |
|
116
|
5 |
|
$errorMessage .= 'The host variable wasn\'t set with "->setUsername(...)".'; |
|
117
|
5 |
|
++$countError; |
|
118
|
|
|
} |
|
119
|
7 |
|
if (null === $vote->getAddress()) { |
|
120
|
6 |
|
$errorMessage .= $countError > 0 ? ' ' : ''; |
|
121
|
6 |
|
$errorMessage .= 'The host variable wasn\'t set with "->setAddress(...)".'; |
|
122
|
6 |
|
++$countError; |
|
123
|
|
|
} |
|
124
|
7 |
|
if (null === $vote->getTimestamp()) { |
|
125
|
6 |
|
$errorMessage .= $countError > 0 ? ' ' : ''; |
|
126
|
6 |
|
$errorMessage .= 'The host variable wasn\'t set with "->setTimestamp(...)".'; |
|
127
|
|
|
} |
|
128
|
7 |
|
if (!isset($this->publicKey)) { |
|
129
|
6 |
|
$errorMessage .= $countError > 0 ? ' ' : ''; |
|
130
|
6 |
|
$errorMessage .= 'The public key variable wasn\'t set with "->setPublicKey(...)".'; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
7 |
|
throw new InvalidArgumentException($errorMessage); |
|
134
|
|
|
} |
|
135
|
2 |
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Verifies that the connection is correct. Read more: |
|
139
|
|
|
* https://github.com/vexsoftware/votifier/wiki/Protocol-Documentation. |
|
140
|
|
|
* |
|
141
|
|
|
* @param string|null $header The header that the plugin usually sends |
|
142
|
|
|
* |
|
143
|
|
|
* @return bool returns true if connections is available, otherwise false |
|
144
|
|
|
*/ |
|
145
|
10 |
|
protected function verifyConnection(?string $header): bool |
|
146
|
|
|
{ |
|
147
|
|
|
if ( |
|
148
|
10 |
|
null === $header |
|
149
|
10 |
|
|| false === mb_strpos($header, 'VOTIFIER') |
|
150
|
|
|
) { |
|
151
|
1 |
|
return false; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
9 |
|
return true; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Create encrypted package for default Votifier. Read more: |
|
159
|
|
|
* https://github.com/vexsoftware/votifier/wiki/Protocol-Documentation. |
|
160
|
|
|
* |
|
161
|
|
|
* @param VoteInterface $vote The vote package with all the information |
|
162
|
|
|
* |
|
163
|
|
|
* @return string returns the string to be sent to the server |
|
164
|
|
|
*/ |
|
165
|
2 |
|
protected function preparePackage(VoteInterface $vote): string |
|
166
|
|
|
{ |
|
167
|
|
|
// Details of the vote |
|
168
|
|
|
$votePackage = 'VOTE' . "\n" . |
|
169
|
2 |
|
$vote->getServiceName() . "\n" . |
|
170
|
2 |
|
$vote->getUsername() . "\n" . |
|
171
|
2 |
|
$vote->getAddress() . "\n" . |
|
172
|
2 |
|
$vote->getTimestamp() . "\n"; |
|
173
|
|
|
|
|
174
|
|
|
// Encrypt the string |
|
175
|
2 |
|
openssl_public_encrypt($votePackage, $encryptedVotePackage, $this->getPublicKey(), OPENSSL_SSLV23_PADDING); |
|
176
|
|
|
|
|
177
|
2 |
|
return $encryptedVotePackage; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|