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