Client.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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_Client
  25. * @brief Class describing a TeamSpeak 3 client and all it's parameters.
  26. */
  27. class TeamSpeak3_Node_Client extends TeamSpeak3_Node_Abstract
  28. {
  29. /**
  30. * The TeamSpeak3_Node_Client constructor.
  31. *
  32. * @param TeamSpeak3_Node_Server $server
  33. * @param array $info
  34. * @param string $index
  35. * @throws TeamSpeak3_Adapter_ServerQuery_Exception
  36. * @return TeamSpeak3_Node_Client
  37. */
  38. public function __construct(TeamSpeak3_Node_Server $server, array $info, $index = "clid")
  39. {
  40. $this->parent = $server;
  41. $this->nodeInfo = $info;
  42. if(!array_key_exists($index, $this->nodeInfo))
  43. {
  44. throw new TeamSpeak3_Adapter_ServerQuery_Exception("invalid clientID", 0x200);
  45. }
  46. $this->nodeId = $this->nodeInfo[$index];
  47. }
  48. /**
  49. * Changes the clients properties using given properties.
  50. *
  51. * @param array $properties
  52. * @return void
  53. */
  54. public function modify(array $properties)
  55. {
  56. $properties["clid"] = $this->getId();
  57. $this->execute("clientedit", $properties);
  58. $this->resetNodeInfo();
  59. }
  60. /**
  61. * Changes the clients properties using given properties.
  62. *
  63. * @param array $properties
  64. * @return void
  65. */
  66. public function modifyDb(array $properties)
  67. {
  68. $this->getParent()->clientModifyDb($this["client_database_id"], $properties);
  69. }
  70. /**
  71. * Deletes the clients properties from the database.
  72. *
  73. * @return void
  74. */
  75. public function deleteDb()
  76. {
  77. $this->getParent()->clientDeleteDb($this["client_database_id"]);
  78. }
  79. /**
  80. * Returns a list of properties from the database for the client.
  81. *
  82. * @return array
  83. */
  84. public function infoDb()
  85. {
  86. return $this->getParent()->clientInfoDb($this["client_database_id"]);
  87. }
  88. /**
  89. * Sends a text message to the client.
  90. *
  91. * @param string $msg
  92. * @return void
  93. */
  94. public function message($msg)
  95. {
  96. $this->execute("sendtextmessage", array("msg" => $msg, "target" => $this->getId(), "targetmode" => TeamSpeak3::TEXTMSG_CLIENT));
  97. }
  98. /**
  99. * Moves the client to another channel.
  100. *
  101. * @param integer $cid
  102. * @param string $cpw
  103. * @return void
  104. */
  105. public function move($cid, $cpw = null)
  106. {
  107. $this->getParent()->clientMove($this->getId(), $cid, $cpw);
  108. }
  109. /**
  110. * Kicks the client from his currently joined channel or from the server.
  111. *
  112. * @param integer $reasonid
  113. * @param string $reasonmsg
  114. * @return void
  115. */
  116. public function kick($reasonid = TeamSpeak3::KICK_CHANNEL, $reasonmsg = null)
  117. {
  118. $this->getParent()->clientKick($this->getId(), $reasonid, $reasonmsg);
  119. }
  120. /**
  121. * Sends a poke message to the client.
  122. *
  123. * @param string $msg
  124. * @return void
  125. */
  126. public function poke($msg)
  127. {
  128. $this->getParent()->clientPoke($this->getId(), $msg);
  129. }
  130. /**
  131. * Bans the client from the server. Please note that this will create two separate
  132. * ban rules for the targeted clients IP address and his unique identifier.
  133. *
  134. * @param integer $timeseconds
  135. * @param string $reason
  136. * @return array
  137. */
  138. public function ban($timeseconds = null, $reason = null)
  139. {
  140. return $this->getParent()->clientBan($this->getId(), $timeseconds, $reason);
  141. }
  142. /**
  143. * Returns a list of custom properties for the client.
  144. *
  145. * @return array
  146. */
  147. public function customInfo()
  148. {
  149. return $this->getParent()->customInfo($this["client_database_id"]);
  150. }
  151. /**
  152. * Creates or updates a custom property for the client.
  153. *
  154. * @param string $ident
  155. * @param string $value
  156. * @return void
  157. */
  158. public function customSet($ident, $value)
  159. {
  160. $this->getParent()->customSet($this["client_database_id"], $ident, $value);
  161. }
  162. /**
  163. * Removes a custom property from the client.
  164. *
  165. * @param string $ident
  166. * @return void
  167. */
  168. public function customDelete($ident)
  169. {
  170. $this->getParent()->customDelete($this["client_database_id"], $ident);
  171. }
  172. /**
  173. * Returns an array containing the permission overview of the client.
  174. *
  175. * @param integer $cid
  176. * @return array
  177. */
  178. public function permOverview($cid)
  179. {
  180. return $this->execute("permoverview", array("cldbid" => $this["client_database_id"], "cid" => $cid, "permid" => 0))->toArray();
  181. }
  182. /**
  183. * Returns a list of permissions defined for the client.
  184. *
  185. * @param boolean $permsid
  186. * @return array
  187. */
  188. public function permList($permsid = FALSE)
  189. {
  190. return $this->getParent()->clientPermList($this["client_database_id"], $permsid);
  191. }
  192. /**
  193. * Adds a set of specified permissions to the client. Multiple permissions can be added by providing
  194. * the three parameters of each permission.
  195. *
  196. * @param integer $permid
  197. * @param integer $permvalue
  198. * @param integer $permskip
  199. * @return void
  200. */
  201. public function permAssign($permid, $permvalue, $permskip = FALSE)
  202. {
  203. $this->getParent()->clientPermAssign($this["client_database_id"], $permid, $permvalue, $permskip);
  204. }
  205. /**
  206. * Alias for permAssign().
  207. *
  208. * @deprecated
  209. */
  210. public function permAssignByName($permname, $permvalue, $permskip = FALSE)
  211. {
  212. $this->permAssign($permname, $permvalue, $permskip);
  213. }
  214. /**
  215. * Removes a set of specified permissions from a client. Multiple permissions can be removed at once.
  216. *
  217. * @param integer $permid
  218. * @return void
  219. */
  220. public function permRemove($permid)
  221. {
  222. $this->getParent()->clientPermRemove($this["client_database_id"], $permid);
  223. }
  224. /**
  225. * Alias for permRemove().
  226. *
  227. * @deprecated
  228. */
  229. public function permRemoveByName($permname)
  230. {
  231. $this->permRemove($permname);
  232. }
  233. /**
  234. * Sets the channel group of a client to the ID specified.
  235. *
  236. * @param integer $cid
  237. * @param integer $cgid
  238. * @return void
  239. */
  240. public function setChannelGroup($cid, $cgid)
  241. {
  242. $this->getParent()->clientSetChannelGroup($this["client_database_id"], $cid, $cgid);
  243. }
  244. /**
  245. * Adds the client to the server group specified with $sgid.
  246. *
  247. * @param integer $sgid
  248. * @return void
  249. */
  250. public function addServerGroup($sgid)
  251. {
  252. $this->getParent()->serverGroupClientAdd($sgid, $this["client_database_id"]);
  253. }
  254. /**
  255. * Removes the client from the server group specified with $sgid.
  256. *
  257. * @param integer $sgid
  258. * @return void
  259. */
  260. public function remServerGroup($sgid)
  261. {
  262. $this->getParent()->serverGroupClientDel($sgid, $this["client_database_id"]);
  263. }
  264. /**
  265. * Returns the possible name of the clients avatar.
  266. *
  267. * @return TeamSpeak3_Helper_String
  268. */
  269. public function avatarGetName()
  270. {
  271. return new TeamSpeak3_Helper_String("/avatar_" . $this["client_base64HashClientUID"]);
  272. }
  273. /**
  274. * Downloads and returns the clients avatar file content.
  275. *
  276. * @return TeamSpeak3_Helper_String
  277. */
  278. public function avatarDownload()
  279. {
  280. if($this["client_flag_avatar"] == NULL) return;
  281. $download = $this->getParent()->transferInitDownload(rand(0x0000, 0xFFFF), 0, $this->avatarGetName());
  282. $transfer = TeamSpeak3::factory("filetransfer://" . (strstr($download["host"], ":") !== FALSE ? "[" . $download["host"] . "]" : $download["host"]) . ":" . $download["port"]);
  283. return $transfer->download($download["ftkey"], $download["size"]);
  284. }
  285. /**
  286. * Returns a list of client connections using the same identity as this client.
  287. *
  288. * @return array
  289. */
  290. public function getClones()
  291. {
  292. return $this->execute("clientgetids", array("cluid" => $this["client_unique_identifier"]))->toAssocArray("clid");
  293. }
  294. /**
  295. * Returns TRUE if the client is using Overwolf.
  296. *
  297. * @return boolean
  298. */
  299. public function hasOverwolf()
  300. {
  301. return strstr($this["client_badges"], "overwolf=1") !== FALSE;
  302. }
  303. /**
  304. * Returns a list of equipped badges for this client.
  305. *
  306. * @return array
  307. */
  308. public function getBadges()
  309. {
  310. $badges = array();
  311. foreach(explode(":", $this["client_badges"]) as $set)
  312. {
  313. if(substr($set, 0, 7) == "badges=")
  314. {
  315. $badges[] = array_map("trim", explode(",", substr($set, 7)));
  316. }
  317. }
  318. return $badges;
  319. }
  320. /**
  321. * Returns the revision/build number from the clients version string.
  322. *
  323. * @return integer
  324. */
  325. public function getRev()
  326. {
  327. return $this["client_type"] ? null : $this["client_version"]->section("[", 1)->filterDigits();
  328. }
  329. /**
  330. * Returns all server and channel groups the client is currently residing in.
  331. *
  332. * @return array
  333. */
  334. public function memberOf()
  335. {
  336. $cgroups = array($this->getParent()->channelGroupGetById($this["client_channel_group_id"]));
  337. $sgroups = array();
  338. foreach(explode(",", $this["client_servergroups"]) as $sgid)
  339. {
  340. $sgroups[] = $this->getParent()->serverGroupGetById($sgid);
  341. }
  342. uasort($sgroups, array(__CLASS__, "sortGroupList"));
  343. return array_merge($cgroups, $sgroups);
  344. }
  345. /**
  346. * Downloads and returns the clients icon file content.
  347. *
  348. * @return TeamSpeak3_Helper_String
  349. */
  350. public function iconDownload()
  351. {
  352. if($this->iconIsLocal("client_icon_id") || $this["client_icon_id"] == 0) return;
  353. $download = $this->getParent()->transferInitDownload(rand(0x0000, 0xFFFF), 0, $this->iconGetName("client_icon_id"));
  354. $transfer = TeamSpeak3::factory("filetransfer://" . (strstr($download["host"], ":") !== FALSE ? "[" . $download["host"] . "]" : $download["host"]) . ":" . $download["port"]);
  355. return $transfer->download($download["ftkey"], $download["size"]);
  356. }
  357. /**
  358. * Sends a plugin command to the client.
  359. *
  360. * @param string $plugin
  361. * @param string $data
  362. * @return void
  363. */
  364. public function sendPluginCmd($plugin, $data)
  365. {
  366. $this->execute("plugincmd", array("name" => $plugin, "data" => $data, "targetmode" => TeamSpeak3::PLUGINCMD_CLIENT, "target" => $this->getId()));
  367. }
  368. /**
  369. * @ignore
  370. */
  371. protected function fetchNodeInfo()
  372. {
  373. if($this->offsetExists("client_type") && $this["client_type"] == 1) return;
  374. $this->nodeInfo = array_merge($this->nodeInfo, $this->execute("clientinfo", array("clid" => $this->getId()))->toList());
  375. }
  376. /**
  377. * Returns a unique identifier for the node which can be used as a HTML property.
  378. *
  379. * @return string
  380. */
  381. public function getUniqueId()
  382. {
  383. return $this->getParent()->getUniqueId() . "_cl" . $this->getId();
  384. }
  385. /**
  386. * Returns the name of a possible icon to display the node object.
  387. *
  388. * @return string
  389. */
  390. public function getIcon()
  391. {
  392. if($this["client_type"])
  393. {
  394. return "client_query";
  395. }
  396. elseif($this["client_away"])
  397. {
  398. return "client_away";
  399. }
  400. elseif(!$this["client_output_hardware"])
  401. {
  402. return "client_snd_disabled";
  403. }
  404. elseif($this["client_output_muted"])
  405. {
  406. return "client_snd_muted";
  407. }
  408. elseif(!$this["client_input_hardware"])
  409. {
  410. return "client_mic_disabled";
  411. }
  412. elseif($this["client_input_muted"])
  413. {
  414. return "client_mic_muted";
  415. }
  416. elseif($this["client_is_channel_commander"])
  417. {
  418. return $this["client_flag_talking"] ? "client_cc_talk" : "client_cc_idle";
  419. }
  420. else
  421. {
  422. return $this["client_flag_talking"] ? "client_talk" : "client_idle";
  423. }
  424. }
  425. /**
  426. * Returns a symbol representing the node.
  427. *
  428. * @return string
  429. */
  430. public function getSymbol()
  431. {
  432. return "@";
  433. }
  434. /**
  435. * Returns a string representation of this node.
  436. *
  437. * @return string
  438. */
  439. public function __toString()
  440. {
  441. return (string) $this["client_nickname"];
  442. }
  443. }