|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Dedipanel project |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2010-2015 Dedipanel <http://www.dedicated-panel.net> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace DP\VoipServer\TeamspeakServerBundle\ServerQuery; |
|
13
|
|
|
|
|
14
|
|
|
use DP\Core\CoreBundle\Exception\MaxSlotsLimitReachedException; |
|
15
|
|
|
use DP\Core\CoreBundle\Exception\PortAlreadyInUseException; |
|
16
|
|
|
use DP\Core\CoreBundle\Socket\Exception\SocketException; |
|
17
|
|
|
use DP\VoipServer\TeamspeakServerBundle\Entity\TeamspeakServerInstance; |
|
18
|
|
|
use DP\Core\CoreBundle\Exception\MaxServerException; |
|
19
|
|
|
use DP\VoipServer\VoipServerBundle\Exception\OfflineServerException; |
|
20
|
|
|
use DP\Core\CoreBundle\Exception\IPBannedException; |
|
21
|
|
|
|
|
22
|
|
|
class QueryGateway |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var TeamSpeak3_Adapter_ServerQuery */ |
|
25
|
|
|
private $query; |
|
26
|
|
|
/** @var string $user */ |
|
27
|
|
|
private $user; |
|
28
|
|
|
/** @var string $pass */ |
|
29
|
|
|
private $pass; |
|
30
|
|
|
/** @var boolean $online */ |
|
31
|
|
|
private $online; |
|
32
|
|
|
/** @var boolean $connected */ |
|
33
|
|
|
private $connected = false; |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param integer $port |
|
38
|
|
|
* @param string $user |
|
39
|
|
|
* @param string $pass |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct($host, $port, $user, $pass, $timeout = 1) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->user = $user; |
|
44
|
|
|
$this->pass = $pass; |
|
45
|
|
|
|
|
46
|
|
|
try { |
|
47
|
|
|
$uri = 'serverquery://' . $host . ':' . $port; |
|
48
|
|
|
$uri .= '?timeout=' . $timeout . '&use_offline_as_virtual=1#no_query_clients'; |
|
49
|
|
|
$this->query = \TeamSpeak3::factory($uri); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
$this->online = true; |
|
52
|
|
|
} catch (\TeamSpeak3_Transport_Exception $e) { |
|
53
|
|
|
$this->online = false; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function login() |
|
58
|
|
|
{ |
|
59
|
|
|
if (!$this->online) { |
|
60
|
|
|
throw new OfflineServerException("You need to start the server before log in."); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
try { |
|
64
|
|
|
$this->query->login($this->user, $this->pass); |
|
65
|
|
|
|
|
66
|
|
|
$this->connected = true; |
|
67
|
|
|
} |
|
68
|
|
|
catch (\TeamSpeak3_Adapter_ServerQuery_Exception $e) { |
|
69
|
|
|
$matches = []; |
|
70
|
|
|
$this->connected = false; |
|
71
|
|
|
|
|
72
|
|
|
if (preg_match('#^connection failed, you are banned \(you may retry in ([\d]*) seconds\)$#', $e->getMessage(), $matches) === 1) { |
|
73
|
|
|
|
|
74
|
|
|
throw new IPBannedException('Banned from the server for ' . $matches[1] . ' seconds.', $matches[1]); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $this->connected; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function createInstance(TeamspeakServerInstance $instance) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->needConnected(); |
|
84
|
|
|
|
|
85
|
|
|
$params = $this->getInstanceParams($instance); |
|
86
|
|
|
|
|
87
|
|
|
try { |
|
88
|
|
|
$details = $this->query->serverCreate($params); |
|
89
|
|
|
} |
|
90
|
|
|
catch (\TeamSpeak3_Adapter_ServerQuery_Exception $e) { |
|
91
|
|
|
$message = $e->getMessage(); |
|
92
|
|
|
|
|
93
|
|
|
if ('virtualserver limit reached' === $message) { |
|
94
|
|
|
throw new MaxServerException; |
|
95
|
|
|
} |
|
96
|
|
|
elseif ('unable to bind network port' === $message) { |
|
97
|
|
|
throw new PortAlreadyInUseException; |
|
98
|
|
|
} |
|
99
|
|
|
elseif ('max slot limit reached' === $message) { |
|
100
|
|
|
throw new MaxSlotsLimitReachedException; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $details; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function deleteInstance($sid) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->needConnected(); |
|
112
|
|
|
|
|
113
|
|
|
/** @var \TeamSpeak3_Node_Server $instance */ |
|
114
|
|
|
$instance = $this->getInstance($sid); |
|
115
|
|
|
|
|
116
|
|
|
if ($instance->isOnline()) { |
|
117
|
|
|
$instance->stop(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
// Need to unselect the current virtual server |
|
121
|
|
|
// before deleting it |
|
122
|
|
|
$this->query->serverDeselect(); |
|
123
|
|
|
$instance->delete(); |
|
124
|
|
|
|
|
125
|
|
|
return true; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function getInstanceList() |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->query->serverList(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function startInstance($sid) |
|
134
|
|
|
{ |
|
135
|
|
|
$this->needConnected(); |
|
136
|
|
|
|
|
137
|
|
|
return $this->query->serverStart($sid); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function stopInstance($sid) |
|
141
|
|
|
{ |
|
142
|
|
|
$this->needConnected(); |
|
143
|
|
|
|
|
144
|
|
|
return $this->query->serverStop($sid); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param integer $sid |
|
149
|
|
|
*/ |
|
150
|
|
|
public function restartInstance($sid) |
|
151
|
|
|
{ |
|
152
|
|
|
$this->needConnected(); |
|
153
|
|
|
|
|
154
|
|
|
$this->stopInstance($sid); |
|
155
|
|
|
|
|
156
|
|
|
return $this->startInstance($sid); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function isInstanceOnline($sid) |
|
160
|
|
|
{ |
|
161
|
|
|
$this->needConnected(); |
|
162
|
|
|
|
|
163
|
|
|
try { |
|
164
|
|
|
return $this->getInstance($sid)->isOnline(); |
|
165
|
|
|
} catch (\TeamSpeak3_Adapter_ServerQuery_Exception $e) {} |
|
166
|
|
|
|
|
167
|
|
|
return false; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function isInstanceOffline($sid) |
|
171
|
|
|
{ |
|
172
|
|
|
try { |
|
173
|
|
|
return $this->getInstance($sid)->isOffline(); |
|
174
|
|
|
} catch (\TeamSpeak3_Adapter_ServerQuery_Exception $e) {} |
|
175
|
|
|
|
|
176
|
|
|
return true; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function getInstance($sid) |
|
180
|
|
|
{ |
|
181
|
|
|
$this->needConnected(); |
|
182
|
|
|
|
|
183
|
|
|
return $this->query->serverGetById($sid); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function updateInstanceConfig(TeamspeakServerInstance $instance) |
|
187
|
|
|
{ |
|
188
|
|
|
$sid = $instance->getInstanceId(); |
|
189
|
|
|
$params = $this->getInstanceParams($instance); |
|
190
|
|
|
|
|
191
|
|
|
try { |
|
192
|
|
|
if ($this->getInstance($sid)->modify($params)) { |
|
193
|
|
|
return $this->restartInstance($sid); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
catch (\TeamSpeak3_Adapter_ServerQuery_Exception $e) { |
|
197
|
|
|
$message = $e->getMessage(); |
|
198
|
|
|
|
|
199
|
|
|
if ('max slot limit reached' === $message) { |
|
200
|
|
|
throw new MaxSlotsLimitReachedException; |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
return false; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function getInstanceParams(TeamspeakServerInstance $instance) |
|
208
|
|
|
{ |
|
209
|
|
|
$params = [ |
|
210
|
|
|
'virtualserver_name' => $instance->getFullName(), |
|
211
|
|
|
'virtualserver_maxclients' => $instance->getMaxClients(), |
|
212
|
|
|
'virtualserver_autostart' => intval($instance->isAutostart()), |
|
213
|
|
|
'virtualserver_port' => $instance->getPort(), |
|
214
|
|
|
'virtualserver_password' => $instance->getPassword(), |
|
215
|
|
|
'virtualserver_welcomemessage' => $instance->getBanner(), |
|
216
|
|
|
]; |
|
217
|
|
|
|
|
218
|
|
|
return array_merge($params, $this->getHostButtonParams($instance)); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
public function isOnline() |
|
222
|
|
|
{ |
|
223
|
|
|
return $this->online; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
public function isConnected() |
|
227
|
|
|
{ |
|
228
|
|
|
return $this->connected; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
private function needConnected() |
|
232
|
|
|
{ |
|
233
|
|
|
if (!$this->connected && !$this->login()) { |
|
234
|
|
|
throw new OfflineServerException("You need to start the server."); |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
private function getHostButtonParams(TeamspeakServerInstance $instance) |
|
|
|
|
|
|
239
|
|
|
{ |
|
240
|
|
|
return [ |
|
241
|
|
|
'virtualserver_hostbutton_tooltip' => 'DediPanel', |
|
242
|
|
|
'virtualserver_hostbutton_url' => 'http://www.dedicated-panel.net', |
|
243
|
|
|
'virtualserver_hostbutton_gfx_url' => 'http://www.dedicated-panel.net/assets/img/icone/logo-ts.png', |
|
244
|
|
|
]; |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..