.htaccess
5 months ago
Client.php
5 months ago
WellKnown.php
5 months ago
index.html
5 months ago
web.config
5 months ago
Client.php
127 lines
| 1 | <?php |
| 2 | /* |
| 3 | * |
| 4 | * JetBackup @ package |
| 5 | * Created By Idan Ben-Ezra |
| 6 | * |
| 7 | * Copyrights @ JetApps |
| 8 | * https://www.jetapps.com |
| 9 | * |
| 10 | **/ |
| 11 | namespace JetBackup\SocketAPI\Client; |
| 12 | |
| 13 | use JetBackup\Entities\Util; |
| 14 | use JetBackup\SocketAPI\Exception\ClientException; |
| 15 | use JetBackup\SocketAPI\Exception\MessageException; |
| 16 | use JetBackup\SocketAPI\Exception\SocketException; |
| 17 | use JetBackup\SocketAPI\Exception\WellKnownException; |
| 18 | use JetBackup\SocketAPI\Message\Message; |
| 19 | use JetBackup\SocketAPI\Message\MessageReader; |
| 20 | use JetBackup\SocketAPI\Message\MessageWriter; |
| 21 | use JetBackup\SocketAPI\Protocol\ProtocolListener; |
| 22 | use JetBackup\SocketAPI\Socket\Socket; |
| 23 | |
| 24 | class Client implements ProtocolListener { |
| 25 | |
| 26 | const SOCKET_FILE = '/usr/local/jetapps/tmp/jetbackup5/api.sock'; |
| 27 | |
| 28 | private $_details; |
| 29 | /** @var Socket */ |
| 30 | private $_socket; |
| 31 | /** @var MessageReader */ |
| 32 | private $_reader; |
| 33 | /** @var MessageWriter */ |
| 34 | private $_writer; |
| 35 | |
| 36 | private $_status; |
| 37 | private $_message; |
| 38 | private $_operation; |
| 39 | |
| 40 | /** |
| 41 | * @throws ClientException |
| 42 | */ |
| 43 | public function __construct() { |
| 44 | |
| 45 | $this->_details = Util::getpwuid(Util::geteuid()); |
| 46 | if(!$this->_details) throw new ClientException("Cannot get user details for socket (posix uid/gid)"); |
| 47 | $this->_openSocketConnection(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @return void |
| 52 | * @throws ClientException |
| 53 | */ |
| 54 | private function _openSocketConnection() { |
| 55 | |
| 56 | try { |
| 57 | $this->_socket = new Socket(); |
| 58 | $this->_socket->setFile(self::SOCKET_FILE); |
| 59 | $this->_socket->connect(); |
| 60 | |
| 61 | $this->_reader = $this->_socket->getReader(); |
| 62 | $this->_writer = $this->_socket->getWriter(); |
| 63 | |
| 64 | $this->_reader->addListener($this); |
| 65 | } catch(SocketException $e) { |
| 66 | throw new ClientException($e->getMessage(), 1); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param string $msg |
| 72 | * |
| 73 | * @return string |
| 74 | * @throws ClientException |
| 75 | */ |
| 76 | public function send($msg='') { |
| 77 | |
| 78 | $this->_status = null; |
| 79 | $this->_message = null; |
| 80 | $this->_operation = true; |
| 81 | |
| 82 | try { |
| 83 | $password = WellKnown::getPassword(); |
| 84 | } catch(WellKnownException $e) { |
| 85 | throw new ClientException($e->getMessage()); |
| 86 | } |
| 87 | |
| 88 | while(true) { |
| 89 | try { |
| 90 | if(!$this->_writer->write(base64_encode("{$this->_details['name']}|$password|$msg"))) |
| 91 | throw new MessageException("Failed to write to socket"); |
| 92 | |
| 93 | while($this->_operation) { |
| 94 | $length = $this->_reader->read(); |
| 95 | $this->_reader->checkMessage(); |
| 96 | if($this->_operation && $length <= 0) throw new MessageException("Connection to socket closed"); |
| 97 | } |
| 98 | |
| 99 | break; |
| 100 | } catch(MessageException $e) { |
| 101 | $this->_socket->close(); |
| 102 | $this->_openSocketConnection(); |
| 103 | throw new ClientException($e->getMessage(), 1); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if($this->_status > 0) throw new ClientException($this->_message, $this->_status); |
| 108 | return $this->_message; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @param string $message |
| 113 | * |
| 114 | * @return void |
| 115 | */ |
| 116 | public function onMessageReady($message) { |
| 117 | $this->_status = Message::unpackInt($message); |
| 118 | $this->_message = substr($message, 4); |
| 119 | $this->_operation = false; |
| 120 | } |
| 121 | |
| 122 | public function close() { |
| 123 | $this->_socket->close(); |
| 124 | unset($this->_socket); |
| 125 | } |
| 126 | } |
| 127 |