TeamSpeak 3 PHP Framework  1.1.16
Copyright © Planet TeamSpeak. All rights reserved.
 All Classes Namespaces Files Functions Variables Pages
Html.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * @file
5  * TeamSpeak 3 PHP Framework
6  *
7  * $Id: Text.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_Viewer_Html
30  * @brief Renders nodes used in HTML-based TeamSpeak 3 viewers.
31  */
33 {
34  /**
35  * A pre-defined pattern used to display a node in a TeamSpeak 3 viewer.
36  *
37  * @var string
38  */
39  protected $pattern = "<table id='%0' class='%1' summary='%2'><tr class='%3'><td class='%4'>%5</td><td class='%6' title='%7'>%8 %9</td><td class='%10'>%11%12</td></tr></table>\n";
40 
41  /**
42  * The TeamSpeak3_Node_Abstract object which is currently processed.
43  *
44  * @var TeamSpeak3_Node_Abstract
45  */
46  protected $currObj = null;
47 
48  /**
49  * An array filled with siblingsfor the TeamSpeak3_Node_Abstract object which is currently
50  * processed.
51  *
52  * @var array
53  */
54  protected $currSib = null;
55 
56  /**
57  * An internal counter indicating the number of fetched TeamSpeak3_Node_Abstract objects.
58  *
59  * @var integer
60  */
61  protected $currNum = 0;
62 
63  /**
64  * The relative URI path where the images used by the viewer can be found.
65  *
66  * @var string
67  */
68  protected $iconpath = null;
69 
70  /**
71  * The relative URI path where the country flag icons used by the viewer can be found.
72  *
73  * @var string
74  */
75  protected $flagpath = null;
76 
77  /**
78  * The relative path of the file transter client script on the server.
79  *
80  * @var string
81  */
82  protected $ftclient = null;
83 
84  /**
85  * Stores an array of local icon IDs.
86  *
87  * @var array
88  */
89  protected $cachedIcons = array(100, 200, 300, 400, 500, 600);
90 
91  /**
92  * Stores an array of remote icon IDs.
93  *
94  * @var array
95  */
96  protected $remoteIcons = array();
97 
98  /**
99  * The TeamSpeak3_Viewer_Html constructor.
100  *
101  * @param string $iconpath
102  * @param string $flagpath
103  * @param string $ftclient
104  * @param string $pattern
105  * @return void
106  */
107  public function __construct($iconpath = "images/viewer/", $flagpath = null, $ftclient = null, $pattern = null)
108  {
109  $this->iconpath = $iconpath;
110  $this->flagpath = $flagpath;
111  $this->ftclient = $ftclient;
112 
113  if($pattern)
114  {
115  $this->pattern = $pattern;
116  }
117  }
118 
119  /**
120  * Returns the code needed to display a node in a TeamSpeak 3 viewer.
121  *
122  * @param TeamSpeak3_Node_Abstract $node
123  * @param array $siblings
124  * @return string
125  */
126  public function fetchObject(TeamSpeak3_Node_Abstract $node, array $siblings = array())
127  {
128  $this->currObj = $node;
129  $this->currSib = $siblings;
130 
131  $args = array(
132  $this->getContainerIdent(),
133  $this->getContainerClass(),
134  $this->getContainerSummary(),
135  $this->getRowClass(),
136  $this->getPrefixClass(),
137  $this->getPrefix(),
138  $this->getCorpusClass(),
139  $this->getCorpusTitle(),
140  $this->getCorpusIcon(),
141  $this->getCorpusName(),
142  $this->getSuffixClass(),
143  $this->getSuffixIcon(),
144  $this->getSuffixFlag(),
145  );
146 
147  return TeamSpeak3_Helper_String::factory($this->pattern)->arg($args);
148  }
149 
150  /**
151  * Returns a unique identifier for the current node which can be used as a HTML id
152  * property.
153  *
154  * @return string
155  */
156  protected function getContainerIdent()
157  {
158  return $this->currObj->getUniqueId();
159  }
160 
161  /**
162  * Returns a dynamic string for the current container element which can be used as
163  * a HTML class property.
164  *
165  * @return string
166  */
167  protected function getContainerClass()
168  {
169  return "ts3_viewer " . $this->currObj->getClass(null);
170  }
171 
172  /**
173  * Returns the ID of the current node which will be used as a summary element for
174  * the container element.
175  *
176  * @return integer
177  */
178  protected function getContainerSummary()
179  {
180  return $this->currObj->getId();
181  }
182 
183  /**
184  * Returns a dynamic string for the current row element which can be used as a HTML
185  * class property.
186  *
187  * @return string
188  */
189  protected function getRowClass()
190  {
191  return ++$this->currNum%2 ? "row1" : "row2";
192  }
193 
194  /**
195  * Returns a string for the current prefix element which can be used as a HTML class
196  * property.
197  *
198  * @return string
199  */
200  protected function getPrefixClass()
201  {
202  return "prefix " . $this->currObj->getClass(null);
203  }
204 
205  /**
206  * Returns the HTML img tags to display the prefix of the current node.
207  *
208  * @return string
209  */
210  protected function getPrefix()
211  {
212  $prefix = "";
213 
214  if(count($this->currSib))
215  {
216  $last = array_pop($this->currSib);
217 
218  foreach($this->currSib as $sibling)
219  {
220  $prefix .= ($sibling) ? $this->getImage("tree_line.gif") : $this->getImage("tree_blank.png");
221  }
222 
223  $prefix .= ($last) ? $this->getImage("tree_end.gif") : $this->getImage("tree_mid.gif");
224  }
225 
226  return $prefix;
227  }
228 
229  /**
230  * Returns a string for the current corpus element which can be used as a HTML class
231  * property. If the current node is a channel spacer the class string will contain
232  * additional class names to allow further customization of the content via CSS.
233  *
234  * @return string
235  */
236  protected function getCorpusClass()
237  {
238  $extras = "";
239 
240  if($this->currObj instanceof TeamSpeak3_Node_Channel && $this->currObj->isSpacer())
241  {
242  switch($this->currObj->spacerGetType())
243  {
244  case (string) TeamSpeak3::SPACER_SOLIDLINE:
245  $extras .= " solidline";
246  break;
247 
248  case (string) TeamSpeak3::SPACER_DASHLINE:
249  $extras .= " dashline";
250  break;
251 
252  case (string) TeamSpeak3::SPACER_DASHDOTLINE:
253  $extras .= " dashdotline";
254  break;
255 
256  case (string) TeamSpeak3::SPACER_DASHDOTDOTLINE:
257  $extras .= " dashdotdotline";
258  break;
259 
260  case (string) TeamSpeak3::SPACER_DOTLINE:
261  $extras .= " dotline";
262  break;
263  }
264 
265  switch($this->currObj->spacerGetAlign())
266  {
268  $extras .= " center";
269  break;
270 
272  $extras .= " right";
273  break;
274 
276  $extras .= " left";
277  break;
278  }
279  }
280 
281  return "corpus " . $this->currObj->getClass(null) . $extras;
282  }
283 
284  /**
285  * Returns the HTML img tags which can be used to display the various icons for a
286  * TeamSpeak_Node_Abstract object.
287  *
288  * @return string
289  */
290  protected function getCorpusTitle()
291  {
292  if($this->currObj instanceof TeamSpeak3_Node_Server)
293  {
294  return "ID: " . $this->currObj->getId() . " | Clients: " . $this->currObj->clientCount() . "/" . $this->currObj["virtualserver_maxclients"] . " | Uptime: " . TeamSpeak3_Helper_Convert::seconds($this->currObj["virtualserver_uptime"]);
295  }
296  elseif($this->currObj instanceof TeamSpeak3_Node_Channel && !$this->currObj->isSpacer())
297  {
298  return "ID: " . $this->currObj->getId() . " | Codec: " . TeamSpeak3_Helper_Convert::codec($this->currObj["channel_codec"]) . " | Quality: " . $this->currObj["channel_codec_quality"];
299  }
300  elseif($this->currObj instanceof TeamSpeak3_Node_Client)
301  {
302  return "ID: " . $this->currObj->getId() . " | Version: " . TeamSpeak3_Helper_Convert::version($this->currObj["client_version"]) . " | Platform: " . $this->currObj["client_platform"];
303  }
304  elseif($this->currObj instanceof TeamSpeak3_Node_Servergroup || $this->currObj instanceof TeamSpeak3_Node_Channelgroup)
305  {
306  return "ID: " . $this->currObj->getId() . " | Type: " . TeamSpeak3_Helper_Convert::groupType($this->currObj["type"]) . " (" . ($this->currObj["savedb"] ? "Permanent" : "Temporary") . ")";
307  }
308  }
309 
310  /**
311  * Returns a HTML img tag which can be used to display the status icon for a
312  * TeamSpeak_Node_Abstract object.
313  *
314  * @return string
315  */
316  protected function getCorpusIcon()
317  {
318  if($this->currObj instanceof TeamSpeak3_Node_Channel && $this->currObj->isSpacer()) return;
319 
320  return $this->getImage($this->currObj->getIcon() . ".png");
321  }
322 
323  /**
324  * Returns a string for the current corpus element which contains the display name
325  * for the current TeamSpeak_Node_Abstract object.
326  *
327  * @return string
328  */
329  protected function getCorpusName()
330  {
331  if($this->currObj instanceof TeamSpeak3_Node_Channel && $this->currObj->isSpacer())
332  {
333  if($this->currObj->spacerGetType() != TeamSpeak3::SPACER_CUSTOM) return;
334 
335  $string = $this->currObj["channel_name"]->section("]", 1, 99);
336 
337  if($this->currObj->spacerGetAlign() == TeamSpeak3::SPACER_ALIGN_REPEAT)
338  {
339  $string->resize(30, $string);
340  }
341 
342  return htmlspecialchars($string);
343  }
344 
345  if($this->currObj instanceof TeamSpeak3_Node_Client)
346  {
347  $before = array();
348  $behind = array();
349 
350  foreach($this->currObj->memberOf() as $group)
351  {
352  if($group->getProperty("namemode") == TeamSpeak3::GROUP_NAMEMODE_BEFORE)
353  {
354  $before[] = "[" . htmlspecialchars($group["name"]) . "]";
355  }
356  elseif($group->getProperty("namemode") == TeamSpeak3::GROUP_NAMEMODE_BEHIND)
357  {
358  $behind[] = "[" . htmlspecialchars($group["name"]) . "]";
359  }
360  }
361 
362  return implode("", $before) . " " . htmlspecialchars($this->currObj) . " " . implode("", $behind);
363  }
364 
365  return htmlspecialchars($this->currObj);
366  }
367 
368  /**
369  * Returns a string for the current suffix element which can be used as a HTML
370  * class property.
371  *
372  * @return string
373  */
374  protected function getSuffixClass()
375  {
376  return "suffix " . $this->currObj->getClass(null);
377  }
378 
379  /**
380  * Returns the HTML img tags which can be used to display the various icons for a
381  * TeamSpeak_Node_Abstract object.
382  *
383  * @return string
384  */
385  protected function getSuffixIcon()
386  {
387  if($this->currObj instanceof TeamSpeak3_Node_Server)
388  {
389  return $this->getSuffixIconServer();
390  }
391  elseif($this->currObj instanceof TeamSpeak3_Node_Channel)
392  {
393  return $this->getSuffixIconChannel();
394  }
395  elseif($this->currObj instanceof TeamSpeak3_Node_Client)
396  {
397  return $this->getSuffixIconClient();
398  }
399  }
400 
401  /**
402  * Returns the HTML img tags which can be used to display the various icons for a
403  * TeamSpeak_Node_Server object.
404  *
405  * @return string
406  */
407  protected function getSuffixIconServer()
408  {
409  $html = "";
410 
411  if($this->currObj["virtualserver_icon_id"])
412  {
413  if(!$this->currObj->iconIsLocal("virtualserver_icon_id") && $this->ftclient)
414  {
415  if(!isset($this->cacheIcon[$this->currObj["virtualserver_icon_id"]]))
416  {
417  $download = $this->currObj->transferInitDownload(rand(0x0000, 0xFFFF), 0, $this->currObj->iconGetName("virtualserver_icon_id"));
418 
419  if($this->ftclient == "data:image")
420  {
421  $download = TeamSpeak3::factory("filetransfer://" . $download["host"] . ":" . $download["port"])->download($download["ftkey"], $download["size"]);
422  }
423 
424  $this->cacheIcon[$this->currObj["virtualserver_icon_id"]] = $download;
425  }
426  else
427  {
428  $download = $this->cacheIcon[$this->currObj["virtualserver_icon_id"]];
429  }
430 
431  if($this->ftclient == "data:image")
432  {
433  $html .= $this->getImage("data:" . TeamSpeak3_Helper_Convert::imageMimeType($download) . ";base64," . base64_encode($download), "Server Icon", null, FALSE);
434  }
435  else
436  {
437  $html .= $this->getImage($this->ftclient . "?ftdata=" . base64_encode(serialize($download)), "Server Icon", null, FALSE);
438  }
439  }
440  elseif(in_array($this->currObj["virtualserver_icon_id"], $this->cachedIcons))
441  {
442  $html .= $this->getImage("group_icon_" . $this->currObj["virtualserver_icon_id"] . ".png", "Server Icon");
443  }
444  }
445 
446  return $html;
447  }
448 
449  /**
450  * Returns the HTML img tags which can be used to display the various icons for a
451  * TeamSpeak_Node_Channel object.
452  *
453  * @return string
454  */
455  protected function getSuffixIconChannel()
456  {
457  if($this->currObj instanceof TeamSpeak3_Node_Channel && $this->currObj->isSpacer()) return;
458 
459  $html = "";
460 
461  if($this->currObj["channel_flag_default"])
462  {
463  $html .= $this->getImage("channel_flag_default.png", "Default Channel");
464  }
465 
466  if($this->currObj["channel_flag_password"])
467  {
468  $html .= $this->getImage("channel_flag_password.png", "Password-protected");
469  }
470 
471  if($this->currObj["channel_codec"] == 3)
472  {
473  $html .= $this->getImage("channel_flag_music.png", "Music Codec");
474  }
475 
476  if($this->currObj["channel_needed_talk_power"])
477  {
478  $html .= $this->getImage("channel_flag_moderated.png", "Moderated");
479  }
480 
481  if($this->currObj["channel_icon_id"])
482  {
483  if(!$this->currObj->iconIsLocal("channel_icon_id") && $this->ftclient)
484  {
485  if(!isset($this->cacheIcon[$this->currObj["channel_icon_id"]]))
486  {
487  $download = $this->currObj->getParent()->transferInitDownload(rand(0x0000, 0xFFFF), 0, $this->currObj->iconGetName("channel_icon_id"));
488 
489  if($this->ftclient == "data:image")
490  {
491  $download = TeamSpeak3::factory("filetransfer://" . $download["host"] . ":" . $download["port"])->download($download["ftkey"], $download["size"]);
492  }
493 
494  $this->cacheIcon[$this->currObj["channel_icon_id"]] = $download;
495  }
496  else
497  {
498  $download = $this->cacheIcon[$this->currObj["channel_icon_id"]];
499  }
500 
501  if($this->ftclient == "data:image")
502  {
503  $html .= $this->getImage("data:" . TeamSpeak3_Helper_Convert::imageMimeType($download) . ";base64," . base64_encode($download), "Channel Icon", null, FALSE);
504  }
505  else
506  {
507  $html .= $this->getImage($this->ftclient . "?ftdata=" . base64_encode(serialize($download)), "Channel Icon", null, FALSE);
508  }
509  }
510  elseif(in_array($this->currObj["channel_icon_id"], $this->cachedIcons))
511  {
512  $html .= $this->getImage("group_icon_" . $this->currObj["channel_icon_id"] . ".png", "Channel Icon");
513  }
514  }
515 
516  return $html;
517  }
518 
519  /**
520  * Returns the HTML img tags which can be used to display the various icons for a
521  * TeamSpeak_Node_Client object.
522  *
523  * @return string
524  */
525  protected function getSuffixIconClient()
526  {
527  $html = "";
528 
529  if($this->currObj["client_is_priority_speaker"])
530  {
531  $html .= $this->getImage("client_priority.png", "Priority Speaker");
532  }
533 
534  if($this->currObj["client_is_channel_commander"])
535  {
536  $html .= $this->getImage("client_cc.png", "Channel Commander");
537  }
538 
539  if($this->currObj["client_is_talker"])
540  {
541  $html .= $this->getImage("client_talker.png", "Talk Power granted");
542  }
543  elseif($cntp = $this->currObj->getParent()->channelGetById($this->currObj["cid"])->channel_needed_talk_power)
544  {
545  if($cntp > $this->currObj["client_talk_power"])
546  {
547  $html .= $this->getImage("client_mic_muted.png", "Insufficient Talk Power");
548  }
549  }
550 
551  foreach($this->currObj->memberOf() as $group)
552  {
553  if(!$group["iconid"]) continue;
554 
555  $type = ($group instanceof TeamSpeak3_Node_Servergroup) ? "Server Group" : "Channel Group";
556 
557  if(!$group->iconIsLocal("iconid") && $this->ftclient)
558  {
559  if(!isset($this->cacheIcon[$group["iconid"]]))
560  {
561  $download = $group->getParent()->transferInitDownload(rand(0x0000, 0xFFFF), 0, $group->iconGetName("iconid"));
562 
563  if($this->ftclient == "data:image")
564  {
565  $download = TeamSpeak3::factory("filetransfer://" . $download["host"] . ":" . $download["port"])->download($download["ftkey"], $download["size"]);
566  }
567 
568  $this->cacheIcon[$group["iconid"]] = $download;
569  }
570  else
571  {
572  $download = $this->cacheIcon[$group["iconid"]];
573  }
574 
575  if($this->ftclient == "data:image")
576  {
577  $html .= $this->getImage("data:" . TeamSpeak3_Helper_Convert::imageMimeType($download) . ";base64," . base64_encode($download), $group . " [" . $type . "]", null, FALSE);
578  }
579  else
580  {
581  $html .= $this->getImage($this->ftclient . "?ftdata=" . base64_encode(serialize($download)), $group . " [" . $type . "]", null, FALSE);
582  }
583  }
584  elseif(in_array($group["iconid"], $this->cachedIcons))
585  {
586  $html .= $this->getImage("group_icon_" . $group["iconid"] . ".png", $group . " [" . $type . "]");
587  }
588  }
589 
590  if($this->currObj["client_icon_id"])
591  {
592  if(!$this->currObj->iconIsLocal("client_icon_id") && $this->ftclient)
593  {
594  if(!isset($this->cacheIcon[$this->currObj["client_icon_id"]]))
595  {
596  $download = $this->currObj->getParent()->transferInitDownload(rand(0x0000, 0xFFFF), 0, $this->currObj->iconGetName("client_icon_id"));
597 
598  if($this->ftclient == "data:image")
599  {
600  $download = TeamSpeak3::factory("filetransfer://" . $download["host"] . ":" . $download["port"])->download($download["ftkey"], $download["size"]);
601  }
602 
603  $this->cacheIcon[$this->currObj["client_icon_id"]] = $download;
604  }
605  else
606  {
607  $download = $this->cacheIcon[$this->currObj["client_icon_id"]];
608  }
609 
610  if($this->ftclient == "data:image")
611  {
612  $html .= $this->getImage("data:" . TeamSpeak3_Helper_Convert::imageMimeType($download) . ";base64," . base64_encode($download), "Client Icon", null, FALSE);
613  }
614  else
615  {
616  $html .= $this->getImage($this->ftclient . "?ftdata=" . base64_encode(serialize($download)), "Client Icon", null, FALSE);
617  }
618  }
619  elseif(in_array($this->currObj["client_icon_id"], $this->cachedIcons))
620  {
621  $html .= $this->getImage("group_icon_" . $this->currObj["client_icon_id"] . ".png", "Client Icon");
622  }
623  }
624 
625  return $html;
626  }
627 
628  /**
629  * Returns a HTML img tag which can be used to display the country flag for a
630  * TeamSpeak_Node_Client object.
631  *
632  * @return string
633  */
634  protected function getSuffixFlag()
635  {
636  if(!$this->currObj instanceof TeamSpeak3_Node_Client) return;
637 
638  if($this->flagpath && $this->currObj["client_country"])
639  {
640  return $this->getImage($this->currObj["client_country"]->toLower() . ".png", $this->currObj["client_country"], null, FALSE, TRUE);
641  }
642  }
643 
644  /**
645  * Returns the code to display a custom HTML img tag.
646  *
647  * @param string $name
648  * @param string $text
649  * @param string $class
650  * @param boolean $iconpath
651  * @param boolean $flagpath
652  * @return string
653  */
654  protected function getImage($name, $text = "", $class = null, $iconpath = TRUE, $flagpath = FALSE)
655  {
656  $src = "";
657 
658  if($iconpath)
659  {
660  $src = $this->iconpath;
661  }
662 
663  if($flagpath)
664  {
665  $src = $this->flagpath;
666  }
667 
668  return "<img src='" . $src . $name . "' title='" . $text . "' alt='' align='top' />";
669  }
670 }