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\Server; |
14
|
|
|
|
15
|
|
|
use D3strukt0r\VotifierClient\Exception\NotVotifierException; |
16
|
|
|
use D3strukt0r\VotifierClient\Exception\Socket\NoConnectionException; |
17
|
|
|
use D3strukt0r\VotifierClient\Exception\Socket\PackageNotReceivedException; |
18
|
|
|
use D3strukt0r\VotifierClient\Exception\Socket\PackageNotSentException; |
19
|
|
|
use D3strukt0r\VotifierClient\Vote\VoteInterface; |
20
|
|
|
use DateTime; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The Class to access a server which uses the classic "Votifier" plugin. |
24
|
|
|
*/ |
25
|
|
|
class Votifier extends GenericServerType |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
* |
30
|
|
|
* @throws NoConnectionException If connection couldn't be established |
31
|
|
|
* @throws NotVotifierException If the server we are connected to is not a valid Votifier server |
32
|
|
|
* @throws PackageNotReceivedException If there was an error receiving the package |
33
|
|
|
* @throws PackageNotSentException If there was an error sending the package |
34
|
|
|
*/ |
35
|
4 |
|
public function sendVote(VoteInterface ...$votes): void |
36
|
|
|
{ |
37
|
4 |
|
foreach ($votes as $vote) { |
38
|
|
|
// Connect to the server |
39
|
4 |
|
$socket = $this->getSocket(); |
40
|
4 |
|
$socket->open($this->getHost(), $this->getPort()); |
41
|
|
|
|
42
|
|
|
// Check whether the connection really belongs to a Votifier plugin |
43
|
3 |
|
if (!$this->verifyConnection($socket->read(64))) { |
44
|
1 |
|
throw new NotVotifierException(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Update the timestamp of the vote being sent |
48
|
2 |
|
$vote->setTimestamp(new DateTime()); |
49
|
|
|
|
50
|
|
|
// Send the vote |
51
|
2 |
|
$socket->write($this->preparePackage($vote)); |
52
|
|
|
|
53
|
|
|
// Make sure to close the connection after package was sent |
54
|
1 |
|
$socket->__destruct(); |
55
|
|
|
} |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Verifies that the connection is correct.. Read more: |
60
|
|
|
* https://github.com/vexsoftware/votifier/wiki/Protocol-Documentation. |
61
|
|
|
* |
62
|
|
|
* @param string|null $header (Required) The header that the plugin usually sends |
63
|
|
|
* |
64
|
|
|
* @return bool returns true if connections is available, otherwise false |
65
|
|
|
*/ |
66
|
3 |
|
protected function verifyConnection(?string $header): bool |
67
|
|
|
{ |
68
|
3 |
|
if (null === $header || false === mb_strpos($header, 'VOTIFIER')) { |
69
|
1 |
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Create encrypted package for default Votifier. Read more: |
77
|
|
|
* https://github.com/vexsoftware/votifier/wiki/Protocol-Documentation. |
78
|
|
|
* |
79
|
|
|
* @param voteInterface $vote The vote package with all the information |
80
|
|
|
* |
81
|
|
|
* @return string returns the string to be sent to the server |
82
|
|
|
*/ |
83
|
2 |
|
protected function preparePackage(VoteInterface $vote): string |
84
|
|
|
{ |
85
|
|
|
// Details of the vote |
86
|
|
|
$votePackage = 'VOTE' . "\n" . |
87
|
2 |
|
$vote->getServiceName() . "\n" . |
88
|
2 |
|
$vote->getUsername() . "\n" . |
89
|
2 |
|
$vote->getAddress() . "\n" . |
90
|
2 |
|
$vote->getTimestamp() . "\n"; |
91
|
|
|
|
92
|
|
|
// Encrypt the string |
93
|
2 |
|
openssl_public_encrypt($votePackage, $encryptedVotePackage, $this->getPublicKey()); |
94
|
|
|
|
95
|
2 |
|
return $encryptedVotePackage; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|