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