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