Channel.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. <?php
  2. /**
  3. * @file
  4. * TeamSpeak 3 PHP Framework
  5. *
  6. * $Id: Channel.php 06/06/2016 22:27:13 scp@Svens-iMac $
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * @package TeamSpeak3
  22. * @version 1.1.24
  23. * @author Sven 'ScP' Paulsen
  24. * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved.
  25. */
  26. /**
  27. * @class TeamSpeak3_Node_Channel
  28. * @brief Class describing a TeamSpeak 3 channel and all it's parameters.
  29. */
  30. class TeamSpeak3_Node_Channel extends TeamSpeak3_Node_Abstract
  31. {
  32. /**
  33. * The TeamSpeak3_Node_Channel constructor.
  34. *
  35. * @param TeamSpeak3_Node_Server $server
  36. * @param array $info
  37. * @param string $index
  38. * @throws TeamSpeak3_Adapter_ServerQuery_Exception
  39. * @return TeamSpeak3_Node_Channel
  40. */
  41. public function __construct(TeamSpeak3_Node_Server $server, array $info, $index = "cid")
  42. {
  43. $this->parent = $server;
  44. $this->nodeInfo = $info;
  45. if(!array_key_exists($index, $this->nodeInfo))
  46. {
  47. throw new TeamSpeak3_Adapter_ServerQuery_Exception("invalid channelID", 0x300);
  48. }
  49. $this->nodeId = $this->nodeInfo[$index];
  50. }
  51. /**
  52. * Returns an array filled with TeamSpeak3_Node_Channel objects.
  53. *
  54. * @param array $filter
  55. * @return array
  56. */
  57. public function subChannelList(array $filter = array())
  58. {
  59. $channels = array();
  60. foreach($this->getParent()->channelList() as $channel)
  61. {
  62. if($channel["pid"] == $this->getId())
  63. {
  64. $channels[$channel->getId()] = $channel;
  65. }
  66. }
  67. return $this->filterList($channels, $filter);
  68. }
  69. /**
  70. * Returns the TeamSpeak3_Node_Channel object matching the given ID.
  71. *
  72. * @param integer $cid
  73. * @throws TeamSpeak3_Adapter_ServerQuery_Exception
  74. * @return TeamSpeak3_Node_Channel
  75. */
  76. public function subChannelGetById($cid)
  77. {
  78. if(!array_key_exists((string) $cid, $this->subChannelList()))
  79. {
  80. throw new TeamSpeak3_Adapter_ServerQuery_Exception("invalid channelID", 0x300);
  81. }
  82. return $this->channelList[(string) $cid];
  83. }
  84. /**
  85. * Returns the TeamSpeak3_Node_Channel object matching the given name.
  86. *
  87. * @param integer $name
  88. * @throws TeamSpeak3_Adapter_ServerQuery_Exception
  89. * @return TeamSpeak3_Node_Channel
  90. */
  91. public function subChannelGetByName($name)
  92. {
  93. foreach($this->subChannelList() as $channel)
  94. {
  95. if($channel["channel_name"] == $name) return $channel;
  96. }
  97. throw new TeamSpeak3_Adapter_ServerQuery_Exception("invalid channelID", 0x300);
  98. }
  99. /**
  100. * Returns an array filled with TeamSpeak3_Node_Client objects.
  101. *
  102. * @param array $filter
  103. * @return array
  104. */
  105. public function clientList(array $filter = array())
  106. {
  107. $clients = array();
  108. foreach($this->getParent()->clientList() as $client)
  109. {
  110. if($client["cid"] == $this->getId())
  111. {
  112. $clients[$client->getId()] = $client;
  113. }
  114. }
  115. return $this->filterList($clients, $filter);
  116. }
  117. /**
  118. * Returns the TeamSpeak3_Node_Client object matching the given ID.
  119. *
  120. * @param integer $clid
  121. * @throws TeamSpeak3_Adapter_ServerQuery_Exception
  122. * @return TeamSpeak3_Node_Client
  123. */
  124. public function clientGetById($clid)
  125. {
  126. if(!array_key_exists($clid, $this->clientList()))
  127. {
  128. throw new TeamSpeak3_Adapter_ServerQuery_Exception("invalid clientID", 0x200);
  129. }
  130. return $this->clientList[intval($clid)];
  131. }
  132. /**
  133. * Returns the TeamSpeak3_Node_Client object matching the given name.
  134. *
  135. * @param integer $name
  136. * @throws TeamSpeak3_Adapter_ServerQuery_Exception
  137. * @return TeamSpeak3_Node_Client
  138. */
  139. public function clientGetByName($name)
  140. {
  141. foreach($this->clientList() as $client)
  142. {
  143. if($client["client_nickname"] == $name) return $client;
  144. }
  145. throw new TeamSpeak3_Adapter_ServerQuery_Exception("invalid clientID", 0x200);
  146. }
  147. /**
  148. * Returns a list of permissions defined for a client in the channel.
  149. *
  150. * @param integer $cldbid
  151. * @param boolean $permsid
  152. * @return array
  153. */
  154. public function clientPermList($cldbid, $permsid = FALSE)
  155. {
  156. return $this->getParent()->channelClientPermList($this->getId(), $cldbid, $permsid);
  157. }
  158. /**
  159. * Adds a set of specified permissions to a client in a specific channel. Multiple permissions can be added by
  160. * providing the two parameters of each permission.
  161. *
  162. * @param integer $cldbid
  163. * @param integer $permid
  164. * @param integer $permvalue
  165. * @return void
  166. */
  167. public function clientPermAssign($cldbid, $permid, $permvalue)
  168. {
  169. $this->getParent()->channelClientPermAssign($this->getId(), $cldbid, $permid, $permvalue);
  170. }
  171. /**
  172. * Alias for clientPermAssign().
  173. *
  174. * @deprecated
  175. */
  176. public function clientPermAssignByName($cldbid, $permname, $permvalue)
  177. {
  178. $this->clientPermAssign($cldbid, $permname, $permvalue);
  179. }
  180. /**
  181. * Removes a set of specified permissions from a client in the channel. Multiple permissions can be removed at once.
  182. *
  183. * @param integer $cldbid
  184. * @param integer $permid
  185. * @return void
  186. */
  187. public function clientPermRemove($cldbid, $permid)
  188. {
  189. $this->getParent()->channelClientPermRemove($this->getId(), $cldbid, $permid);
  190. }
  191. /**
  192. * Alias for clientPermRemove().
  193. *
  194. * @deprecated
  195. */
  196. public function clientPermRemoveByName($cldbid, $permname)
  197. {
  198. $this->clientPermRemove($cldbid, $permname);
  199. }
  200. /**
  201. * Returns a list of permissions defined for the channel.
  202. *
  203. * @param boolean $permsid
  204. * @return array
  205. */
  206. public function permList($permsid = FALSE)
  207. {
  208. return $this->getParent()->channelPermList($this->getId(), $permsid);
  209. }
  210. /**
  211. * Adds a set of specified permissions to the channel. Multiple permissions can be added by
  212. * providing the two parameters of each permission.
  213. *
  214. * @param integer $permid
  215. * @param integer $permvalue
  216. * @return void
  217. */
  218. public function permAssign($permid, $permvalue)
  219. {
  220. $this->getParent()->channelPermAssign($this->getId(), $permid, $permvalue);
  221. }
  222. /**
  223. * Alias for permAssign().
  224. *
  225. * @deprecated
  226. */
  227. public function permAssignByName($permname, $permvalue)
  228. {
  229. $this->permAssign($permname, $permvalue);
  230. }
  231. /**
  232. * Removes a set of specified permissions from the channel. Multiple permissions can be removed at once.
  233. *
  234. * @param integer $permid
  235. * @return void
  236. */
  237. public function permRemove($permid)
  238. {
  239. $this->getParent()->channelPermRemove($this->getId(), $permid);
  240. }
  241. /**
  242. * Alias for permRemove().
  243. *
  244. * @deprecated
  245. */
  246. public function permRemoveByName($permname)
  247. {
  248. $this->permRemove($permname);
  249. }
  250. /**
  251. * Returns a list of files and directories stored in the channels file repository.
  252. *
  253. * @param string $cpw
  254. * @param string $path
  255. * @param boolean $recursive
  256. * @return array
  257. */
  258. public function fileList($cpw = "", $path = "/", $recursive = FALSE)
  259. {
  260. return $this->getParent()->channelFileList($this->getId(), $cpw, $path, $recursive);
  261. }
  262. /**
  263. * Returns detailed information about the specified file stored in the channels file repository.
  264. *
  265. * @param string $cpw
  266. * @param string $name
  267. * @return array
  268. */
  269. public function fileInfo($cpw = "", $name = "/")
  270. {
  271. return $this->getParent()->channelFileInfo($this->getId(), $cpw, $name);
  272. }
  273. /**
  274. * Renames a file in the channels file repository. If the two parameters $tcid and $tcpw are specified, the file
  275. * will be moved into another channels file repository.
  276. *
  277. * @param string $cpw
  278. * @param string $oldname
  279. * @param string $newname
  280. * @param integer $tcid
  281. * @param string $tcpw
  282. * @return void
  283. */
  284. public function fileRename($cpw = "", $oldname = "/", $newname = "/", $tcid = null, $tcpw = null)
  285. {
  286. $this->getParent()->channelFileRename($this->getId(), $cpw, $oldname, $newname, $tcid, $tcpw);
  287. }
  288. /**
  289. * Deletes one or more files stored in the channels file repository.
  290. *
  291. * @param string $cpw
  292. * @param string $path
  293. * @return void
  294. */
  295. public function fileDelete($cpw = "", $name = "/")
  296. {
  297. $this->getParent()->channelFileDelete($this->getId(), $cpw, $name);
  298. }
  299. /**
  300. * Creates new directory in a channels file repository.
  301. *
  302. * @param string $cpw
  303. * @param string $dirname
  304. * @return void
  305. */
  306. public function dirCreate($cpw = "", $dirname = "/")
  307. {
  308. $this->getParent()->channelDirCreate($this->getId(), $cpw, $dirname);
  309. }
  310. /**
  311. * Returns the level of the channel.
  312. *
  313. * @return integer
  314. */
  315. public function getLevel()
  316. {
  317. return $this->getParent()->channelGetLevel($this->getId());
  318. }
  319. /**
  320. * Returns the pathway of the channel which can be used as a clients default channel.
  321. *
  322. * @return string
  323. */
  324. public function getPathway()
  325. {
  326. return $this->getParent()->channelGetPathway($this->getId());
  327. }
  328. /**
  329. * Returns the possible spacer type of the channel.
  330. *
  331. * @return integer
  332. */
  333. public function spacerGetType()
  334. {
  335. return $this->getParent()->channelSpacerGetType($this->getId());
  336. }
  337. /**
  338. * Returns the possible spacer alignment of the channel.
  339. *
  340. * @return integer
  341. */
  342. public function spacerGetAlign()
  343. {
  344. return $this->getParent()->channelSpacerGetAlign($this->getId());
  345. }
  346. /**
  347. * Returns TRUE if the channel is a spacer.
  348. *
  349. * @return boolean
  350. */
  351. public function isSpacer()
  352. {
  353. return $this->getParent()->channelIsSpacer($this);
  354. }
  355. /**
  356. * Downloads and returns the channels icon file content.
  357. *
  358. * @return TeamSpeak3_Helper_String
  359. */
  360. public function iconDownload()
  361. {
  362. if($this->iconIsLocal("channel_icon_id") || $this["channel_icon_id"] == 0) return;
  363. $download = $this->getParent()->transferInitDownload(rand(0x0000, 0xFFFF), 0, $this->iconGetName("channel_icon_id"));
  364. $transfer = TeamSpeak3::factory("filetransfer://" . (strstr($download["host"], ":") !== FALSE ? "[" . $download["host"] . "]" : $download["host"]) . ":" . $download["port"]);
  365. return $transfer->download($download["ftkey"], $download["size"]);
  366. }
  367. /**
  368. * Changes the channel configuration using given properties.
  369. *
  370. * @param array $properties
  371. * @return void
  372. */
  373. public function modify(array $properties)
  374. {
  375. $properties["cid"] = $this->getId();
  376. $this->execute("channeledit", $properties);
  377. $this->resetNodeInfo();
  378. }
  379. /**
  380. * Sends a text message to all clients in the channel.
  381. *
  382. * @param string $msg
  383. * @param string $cpw
  384. * @return void
  385. */
  386. public function message($msg, $cpw = null)
  387. {
  388. if($this->getId() != $this->getParent()->whoamiGet("client_channel_id"))
  389. {
  390. $this->getParent()->clientMove($this->getParent()->whoamiGet("client_id"), $this->getId(), $cpw);
  391. }
  392. $this->execute("sendtextmessage", array("msg" => $msg, "target" => $this->getId(), "targetmode" => TeamSpeak3::TEXTMSG_CHANNEL));
  393. }
  394. /**
  395. * Deletes the channel.
  396. *
  397. * @param boolean $force
  398. * @return void
  399. */
  400. public function delete($force = FALSE)
  401. {
  402. $this->getParent()->channelDelete($this->getId(), $force);
  403. }
  404. /**
  405. * Moves the channel to the parent channel specified with $pid.
  406. *
  407. * @param integer $pid
  408. * @param integer $order
  409. * @return void
  410. */
  411. public function move($pid, $order = null)
  412. {
  413. $this->getParent()->channelMove($this->getId(), $pid, $order);
  414. }
  415. /**
  416. * Sends a plugin command to all clients in the channel.
  417. *
  418. * @param string $plugin
  419. * @param string $data
  420. * @param string $cpw
  421. * @param boolean $subscribed
  422. * @return void
  423. */
  424. public function sendPluginCmd($plugin, $data, $cpw = null, $subscribed = FALSE)
  425. {
  426. if($this->getId() != $this->getParent()->whoamiGet("client_channel_id"))
  427. {
  428. $this->getParent()->clientMove($this->getParent()->whoamiGet("client_id"), $this->getId(), $cpw);
  429. }
  430. $this->execute("plugincmd", array("name" => $plugin, "data" => $data, "targetmode" => $subscribed ? TeamSpeak3::PLUGINCMD_CHANNEL_SUBSCRIBED : TeamSpeak3::PLUGINCMD_CHANNEL));
  431. }
  432. /**
  433. * @ignore
  434. */
  435. protected function fetchNodeList()
  436. {
  437. $this->nodeList = array();
  438. if($this->getParent()->getLoadClientlistFirst())
  439. {
  440. foreach($this->clientList() as $client)
  441. {
  442. if($client["cid"] == $this->getId())
  443. {
  444. $this->nodeList[] = $client;
  445. }
  446. }
  447. foreach($this->subChannelList() as $channel)
  448. {
  449. if($channel["pid"] == $this->getId())
  450. {
  451. $this->nodeList[] = $channel;
  452. }
  453. }
  454. }
  455. else
  456. {
  457. foreach($this->subChannelList() as $channel)
  458. {
  459. if($channel["pid"] == $this->getId())
  460. {
  461. $this->nodeList[] = $channel;
  462. }
  463. }
  464. foreach($this->clientList() as $client)
  465. {
  466. if($client["cid"] == $this->getId())
  467. {
  468. $this->nodeList[] = $client;
  469. }
  470. }
  471. }
  472. }
  473. /**
  474. * @ignore
  475. */
  476. protected function fetchNodeInfo()
  477. {
  478. $this->nodeInfo = array_merge($this->nodeInfo, $this->execute("channelinfo", array("cid" => $this->getId()))->toList());
  479. }
  480. /**
  481. * Returns a unique identifier for the node which can be used as a HTML property.
  482. *
  483. * @return string
  484. */
  485. public function getUniqueId()
  486. {
  487. return $this->getParent()->getUniqueId() . "_ch" . $this->getId();
  488. }
  489. /**
  490. * Returns the name of a possible icon to display the node object.
  491. *
  492. * @return string
  493. */
  494. public function getIcon()
  495. {
  496. if($this["channel_maxclients"] != -1 && $this["channel_maxclients"] <= $this["total_clients"])
  497. {
  498. return "channel_full";
  499. }
  500. elseif($this["channel_flag_password"])
  501. {
  502. return "channel_pass";
  503. }
  504. else
  505. {
  506. return "channel_open";
  507. }
  508. }
  509. /**
  510. * Returns a symbol representing the node.
  511. *
  512. * @return string
  513. */
  514. public function getSymbol()
  515. {
  516. return "#";
  517. }
  518. /**
  519. * Returns a string representation of this node.
  520. *
  521. * @return string
  522. */
  523. public function __toString()
  524. {
  525. return (string) $this["channel_name"];
  526. }
  527. }