Channel.php 14 KB

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