Host.php 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. <?php
  2. /**
  3. * @file
  4. * TeamSpeak 3 PHP Framework
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * @package TeamSpeak3
  20. * @author Sven 'ScP' Paulsen
  21. * @copyright Copyright (c) Planet TeamSpeak. All rights reserved.
  22. */
  23. /**
  24. * @class TeamSpeak3_Node_Host
  25. * @brief Class describing a TeamSpeak 3 server instance and all it's parameters.
  26. */
  27. class TeamSpeak3_Node_Host extends TeamSpeak3_Node_Abstract
  28. {
  29. /**
  30. * @ignore
  31. */
  32. protected $whoami = null;
  33. /**
  34. * @ignore
  35. */
  36. protected $version = null;
  37. /**
  38. * @ignore
  39. */
  40. protected $serverList = null;
  41. /**
  42. * @ignore
  43. */
  44. protected $permissionEnds = null;
  45. /**
  46. * @ignore
  47. */
  48. protected $permissionList = null;
  49. /**
  50. * @ignore
  51. */
  52. protected $permissionCats = null;
  53. /**
  54. * @ignore
  55. */
  56. protected $predefined_query_name = null;
  57. /**
  58. * @ignore
  59. */
  60. protected $exclude_query_clients = FALSE;
  61. /**
  62. * @ignore
  63. */
  64. protected $start_offline_virtual = FALSE;
  65. /**
  66. * @ignore
  67. */
  68. protected $sort_clients_channels = FALSE;
  69. /**
  70. * The TeamSpeak3_Node_Host constructor.
  71. *
  72. * @param TeamSpeak3_Adapter_ServerQuery $squery
  73. * @return TeamSpeak3_Node_Host
  74. */
  75. public function __construct(TeamSpeak3_Adapter_ServerQuery $squery)
  76. {
  77. $this->parent = $squery;
  78. }
  79. /**
  80. * Returns the primary ID of the selected virtual server.
  81. *
  82. * @return integer
  83. */
  84. public function serverSelectedId()
  85. {
  86. return $this->whoamiGet("virtualserver_id", 0);
  87. }
  88. /**
  89. * Returns the primary UDP port of the selected virtual server.
  90. *
  91. * @return integer
  92. */
  93. public function serverSelectedPort()
  94. {
  95. return $this->whoamiGet("virtualserver_port", 0);
  96. }
  97. /**
  98. * Returns the servers version information including platform and build number.
  99. *
  100. * @param string $ident
  101. * @return mixed
  102. */
  103. public function version($ident = null)
  104. {
  105. if($this->version === null)
  106. {
  107. $this->version = $this->request("version")->toList();
  108. }
  109. return ($ident && isset($this->version[$ident])) ? $this->version[$ident] : $this->version;
  110. }
  111. /**
  112. * Selects a virtual server by ID to allow further interaction.
  113. *
  114. * @param integer $sid
  115. * @param boolean $virtual
  116. * @return void
  117. */
  118. public function serverSelect($sid, $virtual = null)
  119. {
  120. if($this->whoami !== null && $this->serverSelectedId() == $sid) return;
  121. $virtual = ($virtual !== null) ? $virtual : $this->start_offline_virtual;
  122. $getargs = func_get_args();
  123. $this->execute("use", array("sid" => $sid, $virtual ? "-virtual" : null));
  124. if($sid != 0 && $this->predefined_query_name !== null)
  125. {
  126. $this->execute("clientupdate", array("client_nickname" => (string) $this->predefined_query_name));
  127. }
  128. $this->whoamiReset();
  129. $this->setStorage("_server_use", array(__FUNCTION__, $getargs));
  130. TeamSpeak3_Helper_Signal::getInstance()->emit("notifyServerselected", $this);
  131. }
  132. /**
  133. * Alias for serverSelect().
  134. *
  135. * @param integer $sid
  136. * @param boolean $virtual
  137. * @return void
  138. */
  139. public function serverSelectById($sid, $virtual = null)
  140. {
  141. $this->serverSelect($sid, $virtual);
  142. }
  143. /**
  144. * Selects a virtual server by UDP port to allow further interaction.
  145. *
  146. * @param integer $port
  147. * @param boolean $virtual
  148. * @return void
  149. */
  150. public function serverSelectByPort($port, $virtual = null)
  151. {
  152. if($this->whoami !== null && $this->serverSelectedPort() == $port) return;
  153. $virtual = ($virtual !== null) ? $virtual : $this->start_offline_virtual;
  154. $getargs = func_get_args();
  155. $this->execute("use", array("port" => $port, $virtual ? "-virtual" : null));
  156. if($port != 0 && $this->predefined_query_name !== null)
  157. {
  158. $this->execute("clientupdate", array("client_nickname" => (string) $this->predefined_query_name));
  159. }
  160. $this->whoamiReset();
  161. $this->setStorage("_server_use", array(__FUNCTION__, $getargs));
  162. TeamSpeak3_Helper_Signal::getInstance()->emit("notifyServerselected", $this);
  163. }
  164. /**
  165. * Deselects the active virtual server.
  166. *
  167. * @return void
  168. */
  169. public function serverDeselect()
  170. {
  171. $this->serverSelect(0);
  172. $this->delStorage("_server_use");
  173. }
  174. /**
  175. * Returns the ID of a virtual server matching the given port.
  176. *
  177. * @param integer $port
  178. * @return integer
  179. */
  180. public function serverIdGetByPort($port)
  181. {
  182. $sid = $this->execute("serveridgetbyport", array("virtualserver_port" => $port))->toList();
  183. return $sid["server_id"];
  184. }
  185. /**
  186. * Returns the port of a virtual server matching the given ID.
  187. *
  188. * @param integer $sid
  189. * @return integer
  190. */
  191. public function serverGetPortById($sid)
  192. {
  193. if(!array_key_exists((string) $sid, $this->serverList()))
  194. {
  195. throw new TeamSpeak3_Adapter_ServerQuery_Exception("invalid serverID", 0x400);
  196. }
  197. return $this->serverList[intval((string) $sid)]["virtualserver_port"];
  198. }
  199. /**
  200. * Returns the TeamSpeak3_Node_Server object matching the currently selected ID.
  201. *
  202. * @return TeamSpeak3_Node_Server
  203. */
  204. public function serverGetSelected()
  205. {
  206. return $this->serverGetById($this->serverSelectedId());
  207. }
  208. /**
  209. * Returns the TeamSpeak3_Node_Server object matching the given ID.
  210. *
  211. * @param integer $sid
  212. * @return TeamSpeak3_Node_Server
  213. */
  214. public function serverGetById($sid)
  215. {
  216. $this->serverSelectById($sid);
  217. return new TeamSpeak3_Node_Server($this, array("virtualserver_id" => intval($sid)));
  218. }
  219. /**
  220. * Returns the TeamSpeak3_Node_Server object matching the given port number.
  221. *
  222. * @param integer $port
  223. * @return TeamSpeak3_Node_Server
  224. */
  225. public function serverGetByPort($port)
  226. {
  227. $this->serverSelectByPort($port);
  228. return new TeamSpeak3_Node_Server($this, array("virtualserver_id" => $this->serverSelectedId()));
  229. }
  230. /**
  231. * Returns the first TeamSpeak3_Node_Server object matching the given name.
  232. *
  233. * @param string $name
  234. * @throws TeamSpeak3_Adapter_ServerQuery_Exception
  235. * @return TeamSpeak3_Node_Server
  236. */
  237. public function serverGetByName($name)
  238. {
  239. foreach($this->serverList() as $server)
  240. {
  241. if($server["virtualserver_name"] == $name) return $server;
  242. }
  243. throw new TeamSpeak3_Adapter_ServerQuery_Exception("invalid serverID", 0x400);
  244. }
  245. /**
  246. * Returns the first TeamSpeak3_Node_Server object matching the given unique identifier.
  247. *
  248. * @param string $uid
  249. * @throws TeamSpeak3_Adapter_ServerQuery_Exception
  250. * @return TeamSpeak3_Node_Server
  251. */
  252. public function serverGetByUid($uid)
  253. {
  254. foreach($this->serverList() as $server)
  255. {
  256. if($server["virtualserver_unique_identifier"] == $uid) return $server;
  257. }
  258. throw new TeamSpeak3_Adapter_ServerQuery_Exception("invalid serverID", 0x400);
  259. }
  260. /**
  261. * Creates a new virtual server using given properties and returns an assoc
  262. * array containing the new ID and initial admin token.
  263. *
  264. * @param array $properties
  265. * @return array
  266. */
  267. public function serverCreate(array $properties = array())
  268. {
  269. $this->serverListReset();
  270. $detail = $this->execute("servercreate", $properties)->toList();
  271. $server = new TeamSpeak3_Node_Server($this, array("virtualserver_id" => intval($detail["sid"])));
  272. TeamSpeak3_Helper_Signal::getInstance()->emit("notifyServercreated", $this, $detail["sid"]);
  273. TeamSpeak3_Helper_Signal::getInstance()->emit("notifyTokencreated", $server, $detail["token"]);
  274. return $detail;
  275. }
  276. /**
  277. * Deletes the virtual server specified by ID.
  278. *
  279. * @param integer $sid
  280. * @return void
  281. */
  282. public function serverDelete($sid)
  283. {
  284. $this->serverListReset();
  285. $this->execute("serverdelete", array("sid" => $sid));
  286. TeamSpeak3_Helper_Signal::getInstance()->emit("notifyServerdeleted", $this, $sid);
  287. }
  288. /**
  289. * Starts the virtual server specified by ID.
  290. *
  291. * @param integer $sid
  292. * @return void
  293. */
  294. public function serverStart($sid)
  295. {
  296. if($sid == $this->serverSelectedId())
  297. {
  298. $this->serverDeselect();
  299. }
  300. $this->execute("serverstart", array("sid" => $sid));
  301. $this->serverListReset();
  302. TeamSpeak3_Helper_Signal::getInstance()->emit("notifyServerstarted", $this, $sid);
  303. }
  304. /**
  305. * Stops the virtual server specified by ID.
  306. *
  307. * @param integer $sid
  308. * @param string $msg
  309. * @return void
  310. */
  311. public function serverStop($sid, $msg = null)
  312. {
  313. if($sid == $this->serverSelectedId())
  314. {
  315. $this->serverDeselect();
  316. }
  317. $this->execute("serverstop", array("sid" => $sid, "reasonmsg" => $msg));
  318. $this->serverListReset();
  319. TeamSpeak3_Helper_Signal::getInstance()->emit("notifyServerstopped", $this, $sid);
  320. }
  321. /**
  322. * Stops the entire TeamSpeak 3 Server instance by shutting down the process.
  323. *
  324. * @param string $msg
  325. * @return void
  326. */
  327. public function serverStopProcess($msg = null)
  328. {
  329. TeamSpeak3_Helper_Signal::getInstance()->emit("notifyServershutdown", $this);
  330. $this->execute("serverprocessstop", array("reasonmsg" => $msg));
  331. }
  332. /**
  333. * Returns an array filled with TeamSpeak3_Node_Server objects.
  334. *
  335. * @param array $filter
  336. * @return array
  337. */
  338. public function serverList(array $filter = array())
  339. {
  340. if($this->serverList === null)
  341. {
  342. $servers = $this->request("serverlist -uid")->toAssocArray("virtualserver_id");
  343. $this->serverList = array();
  344. foreach($servers as $sid => $server)
  345. {
  346. $this->serverList[$sid] = new TeamSpeak3_Node_Server($this, $server);
  347. }
  348. $this->resetNodeList();
  349. }
  350. return $this->filterList($this->serverList, $filter);
  351. }
  352. /**
  353. * Resets the list of virtual servers.
  354. *
  355. * @return void
  356. */
  357. public function serverListReset()
  358. {
  359. $this->resetNodeList();
  360. $this->serverList = null;
  361. }
  362. /**
  363. * Returns a list of IP addresses used by the server instance on multi-homed machines.
  364. *
  365. * @return array
  366. */
  367. public function bindingList($subsystem = "voice")
  368. {
  369. return $this->execute("bindinglist", array("subsystem" => $subsystem))->toArray();
  370. }
  371. /**
  372. * Returns a list of permissions available on the server instance.
  373. *
  374. * @return array
  375. */
  376. public function permissionList()
  377. {
  378. if($this->permissionList === null)
  379. {
  380. $this->fetchPermissionList();
  381. }
  382. foreach($this->permissionList as $permname => $permdata)
  383. {
  384. if(isset($permdata["permcatid"]) && $permdata["permgrant"])
  385. {
  386. continue;
  387. }
  388. $this->permissionList[$permname]["permcatid"] = $this->permissionGetCategoryById($permdata["permid"]);
  389. $this->permissionList[$permname]["permgrant"] = $this->permissionGetGrantById($permdata["permid"]);
  390. $grantsid = "i_needed_modify_power_" . substr($permname, 2);
  391. if(!$permdata["permname"]->startsWith("i_needed_modify_power_") && !isset($this->permissionList[$grantsid]))
  392. {
  393. $this->permissionList[$grantsid]["permid"] = $this->permissionList[$permname]["permgrant"];
  394. $this->permissionList[$grantsid]["permname"] = TeamSpeak3_Helper_String::factory($grantsid);
  395. $this->permissionList[$grantsid]["permdesc"] = null;
  396. $this->permissionList[$grantsid]["permcatid"] = 0xFF;
  397. $this->permissionList[$grantsid]["permgrant"] = $this->permissionList[$permname]["permgrant"];
  398. }
  399. }
  400. return $this->permissionList;
  401. }
  402. /**
  403. * Returns a list of permission categories available on the server instance.
  404. *
  405. * @return array
  406. */
  407. public function permissionCats()
  408. {
  409. if($this->permissionCats === null)
  410. {
  411. $this->fetchPermissionCats();
  412. }
  413. return $this->permissionCats;
  414. }
  415. /**
  416. * Returns a list of permission category endings available on the server instance.
  417. *
  418. * @return array
  419. */
  420. public function permissionEnds()
  421. {
  422. if($this->permissionEnds === null)
  423. {
  424. $this->fetchPermissionList();
  425. }
  426. return $this->permissionCats;
  427. }
  428. /**
  429. * Returns an array filled with all permission categories known to the server including
  430. * their ID, name and parent.
  431. *
  432. * @return array
  433. */
  434. public function permissionTree()
  435. {
  436. $permtree = array();
  437. foreach($this->permissionCats() as $key => $val)
  438. {
  439. $permtree[$val]["permcatid"] = $val;
  440. $permtree[$val]["permcathex"] = "0x" . dechex($val);
  441. $permtree[$val]["permcatname"] = TeamSpeak3_Helper_String::factory(TeamSpeak3_Helper_Convert::permissionCategory($val));
  442. $permtree[$val]["permcatparent"] = $permtree[$val]["permcathex"]{3} == 0 ? 0 : hexdec($permtree[$val]["permcathex"]{2} . 0);
  443. $permtree[$val]["permcatchilren"] = 0;
  444. $permtree[$val]["permcatcount"] = 0;
  445. if(isset($permtree[$permtree[$val]["permcatparent"]]))
  446. {
  447. $permtree[$permtree[$val]["permcatparent"]]["permcatchilren"]++;
  448. }
  449. if($permtree[$val]["permcatname"]->contains("/"))
  450. {
  451. $permtree[$val]["permcatname"] = $permtree[$val]["permcatname"]->section("/", 1)->trim();
  452. }
  453. foreach($this->permissionList() as $permission)
  454. {
  455. if($permission["permid"]["permcatid"] == $val)
  456. {
  457. $permtree[$val]["permcatcount"]++;
  458. }
  459. }
  460. }
  461. return $permtree;
  462. }
  463. /**
  464. * Returns the IDs of all clients, channels or groups using the permission with the
  465. * specified ID.
  466. *
  467. * @param integer $permid
  468. * @return array
  469. */
  470. public function permissionFind($permid)
  471. {
  472. if(!is_array($permid))
  473. {
  474. $permident = (is_numeric($permid)) ? "permid" : "permsid";
  475. }
  476. else
  477. {
  478. $permident = (is_numeric(current($permid))) ? "permid" : "permsid";
  479. }
  480. return $this->execute("permfind", array($permident => $permid))->toArray();
  481. }
  482. /**
  483. * Returns the ID of the permission matching the given name.
  484. *
  485. * @param string $name
  486. * @throws TeamSpeak3_Adapter_ServerQuery_Exception
  487. * @return integer
  488. */
  489. public function permissionGetIdByName($name)
  490. {
  491. if(!array_key_exists((string) $name, $this->permissionList()))
  492. {
  493. throw new TeamSpeak3_Adapter_ServerQuery_Exception("invalid permission ID", 0xA02);
  494. }
  495. return $this->permissionList[(string) $name]["permid"];
  496. }
  497. /**
  498. * Returns the name of the permission matching the given ID.
  499. *
  500. * @param integer $permid
  501. * @throws TeamSpeak3_Adapter_ServerQuery_Exception
  502. * @return TeamSpeak3_Helper_String
  503. */
  504. public function permissionGetNameById($permid)
  505. {
  506. foreach($this->permissionList() as $name => $perm)
  507. {
  508. if($perm["permid"] == $permid) return new TeamSpeak3_Helper_String($name);
  509. }
  510. throw new TeamSpeak3_Adapter_ServerQuery_Exception("invalid permission ID", 0xA02);
  511. }
  512. /**
  513. * Returns the internal category of the permission matching the given ID.
  514. *
  515. * All pre-3.0.7 permission IDs are are 2 bytes wide. The first byte identifies the category while
  516. * the second byte is the permission count within that group.
  517. *
  518. * @param integer $permid
  519. * @return integer
  520. */
  521. public function permissionGetCategoryById($permid)
  522. {
  523. if(!is_numeric($permid))
  524. {
  525. $permid = $this->permissionGetIdByName($permid);
  526. }
  527. if($permid < 0x1000)
  528. {
  529. if($this->permissionEnds === null)
  530. {
  531. $this->fetchPermissionList();
  532. }
  533. if($this->permissionCats === null)
  534. {
  535. $this->fetchPermissionCats();
  536. }
  537. $catids = array_values($this->permissionCats());
  538. foreach($this->permissionEnds as $key => $val)
  539. {
  540. if($val >= $permid && isset($catids[$key]))
  541. {
  542. return $catids[$key];
  543. }
  544. }
  545. return 0;
  546. }
  547. else
  548. {
  549. return (int) $permid >> 8;
  550. }
  551. }
  552. /**
  553. * Returns the internal ID of the i_needed_modify_power_* or grant permission.
  554. *
  555. * Every permission has an associated i_needed_modify_power_* permission, for example b_client_ban_create has an
  556. * associated permission called i_needed_modify_power_client_ban_create.
  557. *
  558. * @param integer $permid
  559. * @return integer
  560. */
  561. public function permissionGetGrantById($permid)
  562. {
  563. if(!is_numeric($permid))
  564. {
  565. $permid = $this->permissionGetIdByName($permid);
  566. }
  567. if($permid < 0x1000)
  568. {
  569. return (int) $permid+0x8000;
  570. }
  571. else
  572. {
  573. return (int) bindec(substr(decbin($permid), -8))+0xFF00;
  574. }
  575. }
  576. /**
  577. * Adds a set of specified permissions to all regular server groups on all virtual servers. The target groups will
  578. * be identified by the value of their i_group_auto_update_type permission specified with $sgtype.
  579. *
  580. * @param integer $sgtype
  581. * @param integer $permid
  582. * @param integer $permvalue
  583. * @param integer $permnegated
  584. * @param integer $permskip
  585. * @return void
  586. */
  587. public function serverGroupPermAutoAssign($sgtype, $permid, $permvalue, $permnegated = FALSE, $permskip = FALSE)
  588. {
  589. if(!is_array($permid))
  590. {
  591. $permident = (is_numeric($permid)) ? "permid" : "permsid";
  592. }
  593. else
  594. {
  595. $permident = (is_numeric(current($permid))) ? "permid" : "permsid";
  596. }
  597. $this->execute("servergroupautoaddperm", array("sgtype" => $sgtype, $permident => $permid, "permvalue" => $permvalue, "permnegated" => $permnegated, "permskip" => $permskip));
  598. }
  599. /**
  600. * Removes a set of specified permissions from all regular server groups on all virtual servers. The target groups
  601. * will be identified by the value of their i_group_auto_update_type permission specified with $sgtype.
  602. *
  603. * @param integer $sgtype
  604. * @param integer $permid
  605. * @return void
  606. */
  607. public function serverGroupPermAutoRemove($sgtype, $permid)
  608. {
  609. if(!is_array($permid))
  610. {
  611. $permident = (is_numeric($permid)) ? "permid" : "permsid";
  612. }
  613. else
  614. {
  615. $permident = (is_numeric(current($permid))) ? "permid" : "permsid";
  616. }
  617. $this->execute("servergroupautodelperm", array("sgtype" => $sgtype, $permident => $permid));
  618. }
  619. /**
  620. * Returns an array containing the value of a specified permission for your own client.
  621. *
  622. * @param integer $permid
  623. * @return array
  624. */
  625. public function selfPermCheck($permid)
  626. {
  627. if(!is_array($permid))
  628. {
  629. $permident = (is_numeric($permid)) ? "permid" : "permsid";
  630. }
  631. else
  632. {
  633. $permident = (is_numeric(current($permid))) ? "permid" : "permsid";
  634. }
  635. return $this->execute("permget", array($permident => $permid))->toAssocArray("permsid");
  636. }
  637. /**
  638. * Changes the server instance configuration using given properties.
  639. *
  640. * @param array $properties
  641. * @return void
  642. */
  643. public function modify(array $properties)
  644. {
  645. $this->execute("instanceedit", $properties);
  646. $this->resetNodeInfo();
  647. }
  648. /**
  649. * Sends a text message to all clients on all virtual servers in the TeamSpeak 3 Server instance.
  650. *
  651. * @param string $msg
  652. * @return void
  653. */
  654. public function message($msg)
  655. {
  656. $this->execute("gm", array("msg" => $msg));
  657. }
  658. /**
  659. * Displays a specified number of entries (1-100) from the servers log.
  660. *
  661. * @param integer $lines
  662. * @param integer $begin_pos
  663. * @param boolean $reverse
  664. * @param boolean $instance
  665. * @return array
  666. */
  667. public function logView($lines = 30, $begin_pos = null, $reverse = null, $instance = TRUE)
  668. {
  669. return $this->execute("logview", array("lines" => $lines, "begin_pos" => $begin_pos, "instance" => $instance, "reverse" => $reverse))->toArray();
  670. }
  671. /**
  672. * Writes a custom entry into the server instance log.
  673. *
  674. * @param string $logmsg
  675. * @param integer $loglevel
  676. * @return void
  677. */
  678. public function logAdd($logmsg, $loglevel = TeamSpeak3::LOGLEVEL_INFO)
  679. {
  680. $sid = $this->serverSelectedId();
  681. $this->serverDeselect();
  682. $this->execute("logadd", array("logmsg" => $logmsg, "loglevel" => $loglevel));
  683. $this->serverSelect($sid);
  684. }
  685. /**
  686. * Authenticates with the TeamSpeak 3 Server instance using given ServerQuery login credentials.
  687. *
  688. * @param string $username
  689. * @param string $password
  690. * @return void
  691. */
  692. public function login($username, $password)
  693. {
  694. $this->execute("login", array("client_login_name" => $username, "client_login_password" => $password));
  695. $this->whoamiReset();
  696. $crypt = new TeamSpeak3_Helper_Crypt($username);
  697. $this->setStorage("_login_user", $username);
  698. $this->setStorage("_login_pass", $crypt->encrypt($password));
  699. TeamSpeak3_Helper_Signal::getInstance()->emit("notifyLogin", $this);
  700. }
  701. /**
  702. * Deselects the active virtual server and logs out from the server instance.
  703. *
  704. * @return void
  705. */
  706. public function logout()
  707. {
  708. $this->request("logout");
  709. $this->whoamiReset();
  710. $this->delStorage("_login_user");
  711. $this->delStorage("_login_pass");
  712. TeamSpeak3_Helper_Signal::getInstance()->emit("notifyLogout", $this);
  713. }
  714. /**
  715. * Returns information about your current ServerQuery connection.
  716. *
  717. * @return array
  718. */
  719. public function whoami()
  720. {
  721. if($this->whoami === null)
  722. {
  723. $this->whoami = $this->request("whoami")->toList();
  724. }
  725. return $this->whoami;
  726. }
  727. /**
  728. * Returns a single value from the current ServerQuery connection info.
  729. *
  730. * @param string $ident
  731. * @param mixed $default
  732. * @return mixed
  733. */
  734. public function whoamiGet($ident, $default = null)
  735. {
  736. if(array_key_exists($ident, $this->whoami()))
  737. {
  738. return $this->whoami[$ident];
  739. }
  740. return $default;
  741. }
  742. /**
  743. * Sets a single value in the current ServerQuery connection info.
  744. *
  745. * @param string $ident
  746. * @param mixed $value
  747. * @return mixed
  748. */
  749. public function whoamiSet($ident, $value = null)
  750. {
  751. $this->whoami();
  752. $this->whoami[$ident] = (is_numeric($value)) ? (int) $value : TeamSpeak3_Helper_String::factory($value);
  753. }
  754. /**
  755. * Resets the current ServerQuery connection info.
  756. *
  757. * @return void
  758. */
  759. public function whoamiReset()
  760. {
  761. $this->whoami = null;
  762. }
  763. /**
  764. * Returns the hostname or IPv4 address the adapter is connected to.
  765. *
  766. * @return string
  767. */
  768. public function getAdapterHost()
  769. {
  770. return $this->getParent()->getTransportHost();
  771. }
  772. /**
  773. * Returns the network port the adapter is connected to.
  774. *
  775. * @return string
  776. */
  777. public function getAdapterPort()
  778. {
  779. return $this->getParent()->getTransportPort();
  780. }
  781. /**
  782. * @ignore
  783. */
  784. protected function fetchNodeList()
  785. {
  786. $servers = $this->serverList();
  787. foreach($servers as $server)
  788. {
  789. $this->nodeList[] = $server;
  790. }
  791. }
  792. /**
  793. * @ignore
  794. */
  795. protected function fetchNodeInfo()
  796. {
  797. $info1 = $this->request("hostinfo")->toList();
  798. $info2 = $this->request("instanceinfo")->toList();
  799. $this->nodeInfo = array_merge($this->nodeInfo, $info1, $info2);
  800. }
  801. /**
  802. * @ignore
  803. */
  804. protected function fetchPermissionList()
  805. {
  806. $reply = $this->request("permissionlist -new")->toArray();
  807. $start = 1;
  808. $this->permissionEnds = array();
  809. $this->permissionList = array();
  810. foreach($reply as $line)
  811. {
  812. if(array_key_exists("group_id_end", $line))
  813. {
  814. $this->permissionEnds[] = $line["group_id_end"];
  815. }
  816. else
  817. {
  818. $this->permissionList[$line["permname"]->toString()] = array_merge(array("permid" => $start++), $line);
  819. }
  820. }
  821. }
  822. /**
  823. * @ignore
  824. */
  825. protected function fetchPermissionCats()
  826. {
  827. $permcats = array();
  828. $reflects = new ReflectionClass("TeamSpeak3");
  829. foreach($reflects->getConstants() as $key => $val)
  830. {
  831. if(!TeamSpeak3_Helper_String::factory($key)->startsWith("PERM_CAT") || $val == 0xFF)
  832. {
  833. continue;
  834. }
  835. $permcats[$key] = $val;
  836. }
  837. $this->permissionCats = $permcats;
  838. }
  839. /**
  840. * Sets a pre-defined nickname for ServerQuery clients which will be used automatically
  841. * after selecting a virtual server.
  842. *
  843. * @param string $name
  844. * @return void
  845. */
  846. public function setPredefinedQueryName($name = null)
  847. {
  848. $this->setStorage("_query_nick", $name);
  849. $this->predefined_query_name = $name;
  850. }
  851. /**
  852. * Returns the pre-defined nickname for ServerQuery clients which will be used automatically
  853. * after selecting a virtual server.
  854. *
  855. * @return string
  856. */
  857. public function getPredefinedQueryName()
  858. {
  859. return $this->predefined_query_name;
  860. }
  861. /**
  862. * Sets the option to decide whether ServerQuery clients should be excluded from node
  863. * lists or not.
  864. *
  865. * @param boolean $exclude
  866. * @return void
  867. */
  868. public function setExcludeQueryClients($exclude = FALSE)
  869. {
  870. $this->setStorage("_query_hide", $exclude);
  871. $this->exclude_query_clients = $exclude;
  872. }
  873. /**
  874. * Returns the option to decide whether ServerQuery clients should be excluded from node
  875. * lists or not.
  876. *
  877. * @return boolean
  878. */
  879. public function getExcludeQueryClients()
  880. {
  881. return $this->exclude_query_clients;
  882. }
  883. /**
  884. * Sets the option to decide whether offline servers will be started in virtual mode
  885. * by default or not.
  886. *
  887. * @param boolean $virtual
  888. * @return void
  889. */
  890. public function setUseOfflineAsVirtual($virtual = FALSE)
  891. {
  892. $this->setStorage("_do_virtual", $virtual);
  893. $this->start_offline_virtual = $virtual;
  894. }
  895. /**
  896. * Returns the option to decide whether offline servers will be started in virtual mode
  897. * by default or not.
  898. *
  899. * @return boolean
  900. */
  901. public function getUseOfflineAsVirtual()
  902. {
  903. return $this->start_offline_virtual;
  904. }
  905. /**
  906. * Sets the option to decide whether clients should be sorted before sub-channels to support
  907. * the new TeamSpeak 3 Client display mode or not.
  908. *
  909. * @param boolean $first
  910. * @return void
  911. */
  912. public function setLoadClientlistFirst($first = FALSE)
  913. {
  914. $this->setStorage("_client_top", $first);
  915. $this->sort_clients_channels = $first;
  916. }
  917. /**
  918. * Returns the option to decide whether offline servers will be started in virtual mode
  919. * by default or not.
  920. *
  921. * @return boolean
  922. */
  923. public function getLoadClientlistFirst()
  924. {
  925. return $this->sort_clients_channels;
  926. }
  927. /**
  928. * Returns the underlying TeamSpeak3_Adapter_ServerQuery object.
  929. *
  930. * @return TeamSpeak3_Adapter_ServerQuery
  931. */
  932. public function getAdapter()
  933. {
  934. return $this->getParent();
  935. }
  936. /**
  937. * Returns a unique identifier for the node which can be used as a HTML property.
  938. *
  939. * @return string
  940. */
  941. public function getUniqueId()
  942. {
  943. return "ts3_h";
  944. }
  945. /**
  946. * Returns the name of a possible icon to display the node object.
  947. *
  948. * @return string
  949. */
  950. public function getIcon()
  951. {
  952. return "host";
  953. }
  954. /**
  955. * Returns a symbol representing the node.
  956. *
  957. * @return string
  958. */
  959. public function getSymbol()
  960. {
  961. return "+";
  962. }
  963. /**
  964. * Re-authenticates with the TeamSpeak 3 Server instance using given ServerQuery login
  965. * credentials and re-selects a previously selected virtual server.
  966. *
  967. * @return void
  968. */
  969. public function __wakeup()
  970. {
  971. $username = $this->getStorage("_login_user");
  972. $password = $this->getStorage("_login_pass");
  973. if($username && $password)
  974. {
  975. $crypt = new TeamSpeak3_Helper_Crypt($username);
  976. $this->login($username, $crypt->decrypt($password));
  977. }
  978. $this->predefined_query_name = $this->getStorage("_query_nick");
  979. $this->exclude_query_clients = $this->getStorage("_query_hide", FALSE);
  980. $this->start_offline_virtual = $this->getStorage("_do_virtual", FALSE);
  981. $this->sort_clients_channels = $this->getStorage("_client_top", FALSE);
  982. if($server = $this->getStorage("_server_use"))
  983. {
  984. $func = array_shift($server);
  985. $args = array_shift($server);
  986. try
  987. {
  988. call_user_func_array(array($this, $func), $args);
  989. }
  990. catch(Exception $e)
  991. {
  992. $class = get_class($e);
  993. throw new $class($e->getMessage(), $e->getCode());
  994. }
  995. }
  996. }
  997. /**
  998. * Returns a string representation of this node.
  999. *
  1000. * @return string
  1001. */
  1002. public function __toString()
  1003. {
  1004. return (string) $this->getAdapterHost();
  1005. }
  1006. }