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\Messages; |
16
|
|
|
use D3strukt0r\VotifierClient\ServerConnection; |
17
|
|
|
use D3strukt0r\VotifierClient\VoteType\VoteInterface; |
18
|
|
|
use Exception; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The Class to access a server which uses the classic "Votifier" plugin. |
22
|
|
|
*/ |
23
|
|
|
class ClassicVotifier implements ServerTypeInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string the domain or ip to connect to Votifier |
27
|
|
|
*/ |
28
|
|
|
private $host; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int the port which votifier uses on the server |
32
|
|
|
*/ |
33
|
|
|
private $port = 8192; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string The public.key which is generated by the plugin. |
37
|
|
|
*/ |
38
|
|
|
private $publicKey; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Creates the ClassicVotifier object. |
42
|
|
|
* |
43
|
|
|
* @param string $host (Required) The domain or ip to connect to Votifier |
44
|
|
|
* @param int|null $port (Required) The port which votifier uses on the server |
45
|
|
|
* @param string $publicKey (Required) The public.key which is generated by the plugin |
46
|
|
|
*/ |
47
|
3 |
|
public function __construct(string $host, ?int $port, string $publicKey) |
48
|
|
|
{ |
49
|
3 |
|
$this->host = $host; |
50
|
|
|
|
51
|
3 |
|
if (null !== $port) { |
52
|
3 |
|
$this->port = $port; |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
$this->publicKey = $publicKey; |
56
|
3 |
|
$this->publicKey = wordwrap($this->publicKey, 65, "\n", true); |
57
|
3 |
|
$this->publicKey = <<<EOF |
58
|
3 |
|
-----BEGIN PUBLIC KEY----- |
59
|
3 |
|
{$this->publicKey} |
60
|
|
|
-----END PUBLIC KEY----- |
61
|
|
|
EOF; |
62
|
3 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
1 |
|
public function getHost(): string |
68
|
|
|
{ |
69
|
1 |
|
return $this->host; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
1 |
|
public function getPort(): int |
76
|
|
|
{ |
77
|
1 |
|
return $this->port; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
1 |
|
public function getPublicKey(): string |
84
|
|
|
{ |
85
|
1 |
|
return $this->publicKey; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
1 |
|
public function verifyConnection(?string $header): bool |
92
|
|
|
{ |
93
|
1 |
|
if (null === $header || false === mb_strpos($header, 'VOTIFIER')) { |
94
|
1 |
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Create encrypted package for default Votifier. |
102
|
|
|
* |
103
|
|
|
* @param voteInterface $vote (Required) The vote package with all the information |
104
|
|
|
* |
105
|
|
|
* @return string returns the string to be sent to the server |
106
|
|
|
*/ |
107
|
|
|
public function preparePackage(VoteInterface $vote): string |
108
|
|
|
{ |
109
|
|
|
// Details of the vote |
110
|
|
|
$votePackage = 'VOTE'."\n". |
111
|
|
|
$vote->getServiceName()."\n". |
112
|
|
|
$vote->getUsername()."\n". |
113
|
|
|
$vote->getAddress()."\n". |
114
|
|
|
$vote->getTimestamp()."\n"; |
115
|
|
|
|
116
|
|
|
// Encrypt the string |
117
|
|
|
openssl_public_encrypt($votePackage, $encryptedVotePackage, $this->getPublicKey()); |
118
|
|
|
|
119
|
|
|
return $encryptedVotePackage; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
public function send(ServerConnection $connection, VoteInterface $vote): void |
126
|
|
|
{ |
127
|
|
|
if (!$this->verifyConnection($connection->receive(64))) { |
128
|
|
|
throw new Exception(Messages::get(Messages::NOT_VOTIFIER)); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (!$connection->send($this->preparePackage($vote))) { |
132
|
|
|
throw new Exception(Messages::get(Messages::NOT_SENT_PACKAGE)); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|