PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / trunk
JetBackup – Backup, Restore & Migrate vtrunk
3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / src / JetBackup / 3rdparty / SocketAPI / Client / Client.php
backup / src / JetBackup / 3rdparty / SocketAPI / Client Last commit date
.htaccess 1 year ago Client.php 9 months ago WellKnown.php 9 months ago index.html 1 year ago web.config 1 year 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