TeamSpeak 3 PHP Framework  1.1.12
libraries/TeamSpeak3/Viewer/Html.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /**
00004  * @file
00005  * TeamSpeak 3 PHP Framework
00006  *
00007  * $Id: Text.php 2/18/2012 12:42:46 scp@orilla $
00008  *
00009  * This program is free software: you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation, either version 3 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00021  *
00022  * @package   TeamSpeak3
00023  * @version   1.1.12
00024  * @author    Sven 'ScP' Paulsen
00025  * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved.
00026  */
00027 
00028 /**
00029  * @class TeamSpeak3_Viewer_Html
00030  * @brief Renders nodes used in HTML-based TeamSpeak 3 viewers.
00031  */
00032 class TeamSpeak3_Viewer_Html implements TeamSpeak3_Viewer_Interface
00033 {
00034   /**
00035    * A pre-defined pattern used to display a node in a TeamSpeak 3 viewer.
00036    *
00037    * @var string
00038    */
00039   protected $pattern = "<table id='%0' class='%1'><tr class='%2'><td class='%3'>%4</td><td class='%5' title='%6'>%7 %8</td><td class='%9'>%10%11</td></tr></table>\n";
00040 
00041   /**
00042    * The TeamSpeak3_Node_Abstract object which is currently processed.
00043    *
00044    * @var TeamSpeak3_Node_Abstract
00045    */
00046   protected $currObj = null;
00047 
00048   /**
00049    * An array filled with siblingsfor the  TeamSpeak3_Node_Abstract object which is currently
00050    * processed.
00051    *
00052    * @var array
00053    */
00054   protected $currSib = null;
00055 
00056   /**
00057    * An internal counter indicating the number of fetched TeamSpeak3_Node_Abstract objects.
00058    *
00059    * @var integer
00060    */
00061   protected $currNum = 0;
00062 
00063   /**
00064    * The relative URI path where the images used by the viewer can be found.
00065    *
00066    * @var string
00067    */
00068   protected $iconpath = null;
00069 
00070   /**
00071    * The relative URI path where the country flag icons used by the viewer can be found.
00072    *
00073    * @var string
00074    */
00075   protected $flagpath = null;
00076 
00077   /**
00078    * The relative path of the file transter client script on the server.
00079    *
00080    * @var string
00081    */
00082   protected $ftclient = null;
00083 
00084   /**
00085    * Stores an array of local icon IDs.
00086    *
00087    * @var array
00088    */
00089   protected $cachedIcons = array(100, 200, 300, 400, 500, 600);
00090 
00091   /**
00092    * Stores an array of remote icon IDs.
00093    *
00094    * @var array
00095    */
00096   protected $remoteIcons = array();
00097 
00098   /**
00099    * The TeamSpeak3_Viewer_Html constructor.
00100    *
00101    * @param  string $iconpath
00102    * @param  string $flagpath
00103    * @param  string $ftclient
00104    * @param  string $pattern
00105    * @return void
00106    */
00107   public function __construct($iconpath = "images/viewer/", $flagpath = null, $ftclient = null, $pattern = null)
00108   {
00109     $this->iconpath = $iconpath;
00110     $this->flagpath = $flagpath;
00111     $this->ftclient = $ftclient;
00112 
00113     if($pattern)
00114     {
00115       $this->pattern = $pattern;
00116     }
00117   }
00118 
00119   /**
00120    * Returns the code needed to display a node in a TeamSpeak 3 viewer.
00121    *
00122    * @param  TeamSpeak3_Node_Abstract $node
00123    * @param  array $siblings
00124    * @return string
00125    */
00126   public function fetchObject(TeamSpeak3_Node_Abstract $node, array $siblings = array())
00127   {
00128     $this->currObj = $node;
00129     $this->currSib = $siblings;
00130 
00131     $args = array(
00132       $this->getContainerIdent(),
00133       $this->getContainerClass(),
00134       $this->getRowClass(),
00135       $this->getPrefixClass(),
00136       $this->getPrefix(),
00137       $this->getCorpusClass(),
00138       $this->getCorpusTitle(),
00139       $this->getCorpusIcon(),
00140       $this->getCorpusName(),
00141       $this->getSuffixClass(),
00142       $this->getSuffixIcon(),
00143       $this->getSuffixFlag(),
00144     );
00145 
00146     return TeamSpeak3_Helper_String::factory($this->pattern)->arg($args);
00147   }
00148 
00149   /**
00150    * Returns a unique identifier for the current node which can be used as a HTML id
00151    * property.
00152    *
00153    * @return string
00154    */
00155   protected function getContainerIdent()
00156   {
00157     return $this->currObj->getUniqueId();
00158   }
00159 
00160   /**
00161    * Returns a dynamic string for the current container element which can be used as
00162    * a HTML class property.
00163    *
00164    * @return string
00165    */
00166   protected function getContainerClass()
00167   {
00168     return "ts3_viewer " . $this->currObj->getClass(null);
00169   }
00170 
00171   /**
00172    * Returns a dynamic string for the current row element which can be used as a HTML
00173    * class property.
00174    *
00175    * @return string
00176    */
00177   protected function getRowClass()
00178   {
00179     return ++$this->currNum%2 ? "row1" : "row2";
00180   }
00181 
00182   /**
00183    * Returns a string for the current prefix element which can be used as a HTML class
00184    * property.
00185    *
00186    * @return string
00187    */
00188   protected function getPrefixClass()
00189   {
00190     return "prefix " . $this->currObj->getClass(null);
00191   }
00192 
00193   /**
00194    * Returns the HTML img tags to display the prefix of the current node.
00195    *
00196    * @return string
00197    */
00198   protected function getPrefix()
00199   {
00200     $prefix = "";
00201 
00202     if(count($this->currSib))
00203     {
00204       $last = array_pop($this->currSib);
00205 
00206       foreach($this->currSib as $sibling)
00207       {
00208         $prefix .=  ($sibling) ? $this->getImage("tree_line.gif") : $this->getImage("tree_blank.png");
00209       }
00210 
00211       $prefix .= ($last) ? $this->getImage("tree_end.gif") : $this->getImage("tree_mid.gif");
00212     }
00213 
00214     return $prefix;
00215   }
00216 
00217   /**
00218    * Returns a string for the current corpus element which can be used as a HTML class
00219    * property. If the current node is a channel spacer the class string will contain
00220    * additional class names to allow further customization of the content via CSS.
00221    *
00222    * @return string
00223    */
00224   protected function getCorpusClass()
00225   {
00226     $extras = "";
00227 
00228     if($this->currObj instanceof TeamSpeak3_Node_Channel && $this->currObj->isSpacer())
00229     {
00230       switch($this->currObj->spacerGetType())
00231       {
00232         case (string) TeamSpeak3::SPACER_SOLIDLINE:
00233           $extras .= " solidline";
00234           break;
00235 
00236         case (string) TeamSpeak3::SPACER_DASHLINE:
00237           $extras .= " dashline";
00238           break;
00239 
00240         case (string) TeamSpeak3::SPACER_DASHDOTLINE:
00241           $extras .= " dashdotline";
00242           break;
00243 
00244         case (string) TeamSpeak3::SPACER_DASHDOTDOTLINE:
00245           $extras .= " dashdotdotline";
00246           break;
00247 
00248         case (string) TeamSpeak3::SPACER_DOTLINE:
00249           $extras .= " dotline";
00250           break;
00251       }
00252 
00253       switch($this->currObj->spacerGetAlign())
00254       {
00255         case TeamSpeak3::SPACER_ALIGN_CENTER:
00256           $extras .= " center";
00257           break;
00258 
00259         case TeamSpeak3::SPACER_ALIGN_RIGHT:
00260           $extras .= " right";
00261           break;
00262 
00263         case TeamSpeak3::SPACER_ALIGN_LEFT:
00264           $extras .= " left";
00265           break;
00266       }
00267     }
00268 
00269     return "corpus " . $this->currObj->getClass(null) . $extras;
00270   }
00271 
00272   /**
00273    * Returns the HTML img tags which can be used to display the various icons for a
00274    * TeamSpeak_Node_Abstract object.
00275    *
00276    * @return string
00277    */
00278   protected function getCorpusTitle()
00279   {
00280     if($this->currObj instanceof TeamSpeak3_Node_Server)
00281     {
00282       return "ID: " . $this->currObj->getId() . " | Clients: " . $this->currObj->clientCount() . "/" . $this->currObj["virtualserver_maxclients"] . " | Uptime: " . TeamSpeak3_Helper_Convert::seconds($this->currObj["virtualserver_uptime"]);
00283     }
00284     elseif($this->currObj instanceof TeamSpeak3_Node_Channel && !$this->currObj->isSpacer())
00285     {
00286       return "ID: " . $this->currObj->getId() . " | Codec: " . TeamSpeak3_Helper_Convert::codec($this->currObj["channel_codec"]) . " | Quality: " . $this->currObj["channel_codec_quality"];
00287     }
00288     elseif($this->currObj instanceof TeamSpeak3_Node_Client)
00289     {
00290       return "ID: " . $this->currObj->getId() . " | Version: " . TeamSpeak3_Helper_Convert::version($this->currObj["client_version"]) . " | Platform: " . $this->currObj["client_platform"];
00291     }
00292     elseif($this->currObj instanceof TeamSpeak3_Node_Servergroup || $this->currObj instanceof TeamSpeak3_Node_Channelgroup)
00293     {
00294       return "ID: " . $this->currObj->getId() . " | Type: " . TeamSpeak3_Helper_Convert::groupType($this->currObj["type"]) . " (" . ($this->currObj["savedb"] ? "Permanent" : "Temporary") . ")";
00295     }
00296   }
00297 
00298   /**
00299    * Returns a HTML img tag which can be used to display the status icon for a
00300    * TeamSpeak_Node_Abstract object.
00301    *
00302    * @return string
00303    */
00304   protected function getCorpusIcon()
00305   {
00306     if($this->currObj instanceof TeamSpeak3_Node_Channel && $this->currObj->isSpacer()) return;
00307 
00308     return $this->getImage($this->currObj->getIcon() . ".png");
00309   }
00310 
00311   /**
00312    * Returns a string for the current corpus element which contains the display name
00313    * for the current TeamSpeak_Node_Abstract object.
00314    *
00315    * @return string
00316    */
00317   protected function getCorpusName()
00318   {
00319     if($this->currObj instanceof TeamSpeak3_Node_Channel && $this->currObj->isSpacer())
00320     {
00321       if($this->currObj->spacerGetType() != TeamSpeak3::SPACER_CUSTOM) return;
00322 
00323       $string = $this->currObj["channel_name"]->section("]", 1, 99);
00324 
00325       if($this->currObj->spacerGetAlign() == TeamSpeak3::SPACER_ALIGN_REPEAT)
00326       {
00327         $string->resize(30, $string);
00328       }
00329 
00330       return htmlspecialchars($string);
00331     }
00332 
00333     if($this->currObj instanceof TeamSpeak3_Node_Client)
00334     {
00335       $before = array();
00336       $behind = array();
00337 
00338       foreach($this->currObj->memberOf() as $group)
00339       {
00340         if($group->getProperty("namemode") == TeamSpeak3::GROUP_NAMEMODE_BEFORE)
00341         {
00342           $before[] = "[" . htmlspecialchars($group["name"]) . "]";
00343         }
00344         elseif($group->getProperty("namemode") == TeamSpeak3::GROUP_NAMEMODE_BEHIND)
00345         {
00346           $behind[] = "[" . htmlspecialchars($group["name"]) . "]";
00347         }
00348       }
00349 
00350       return implode("", $before) . " " . htmlspecialchars($this->currObj) . " " . implode("", $behind);
00351     }
00352 
00353     return htmlspecialchars($this->currObj);
00354   }
00355 
00356   /**
00357    * Returns a string for the current suffix element which can be used as a HTML
00358    * class property.
00359    *
00360    * @return string
00361    */
00362   protected function getSuffixClass()
00363   {
00364     return "suffix " . $this->currObj->getClass(null);
00365   }
00366 
00367   /**
00368    * Returns the HTML img tags which can be used to display the various icons for a
00369    * TeamSpeak_Node_Abstract object.
00370    *
00371    * @return string
00372    */
00373   protected function getSuffixIcon()
00374   {
00375     if($this->currObj instanceof TeamSpeak3_Node_Server)
00376     {
00377       return $this->getSuffixIconServer();
00378     }
00379     elseif($this->currObj instanceof TeamSpeak3_Node_Channel)
00380     {
00381       return $this->getSuffixIconChannel();
00382     }
00383     elseif($this->currObj instanceof TeamSpeak3_Node_Client)
00384     {
00385       return $this->getSuffixIconClient();
00386     }
00387   }
00388 
00389   /**
00390    * Returns the HTML img tags which can be used to display the various icons for a
00391    * TeamSpeak_Node_Server object.
00392    *
00393    * @return string
00394    */
00395   protected function getSuffixIconServer()
00396   {
00397     $html = "";
00398 
00399     if($this->currObj["virtualserver_icon_id"])
00400     {
00401       if(!$this->currObj->iconIsLocal("virtualserver_icon_id") && $this->ftclient)
00402       {
00403         if(!isset($this->cacheIcon[$this->currObj["virtualserver_icon_id"]]))
00404         {
00405           $download = $this->currObj->transferInitDownload(rand(0x0000, 0xFFFF), 0, $this->currObj->iconGetName("virtualserver_icon_id"));
00406 
00407           if($this->ftclient == "data:image")
00408           {
00409             $download = TeamSpeak3::factory("filetransfer://" . $download["host"] . ":" . $download["port"])->download($download["ftkey"], $download["size"]);
00410           }
00411 
00412           $this->cacheIcon[$this->currObj["virtualserver_icon_id"]] = $download;
00413         }
00414         else
00415         {
00416           $download = $this->cacheIcon[$this->currObj["virtualserver_icon_id"]];
00417         }
00418 
00419         if($this->ftclient == "data:image")
00420         {
00421           $html .= $this->getImage("data:" . TeamSpeak3_Helper_Convert::imageMimeType($download) . ";base64," . base64_encode($download), "Server Icon", null, FALSE);
00422         }
00423         else
00424         {
00425           $html .= $this->getImage($this->ftclient . "?ftdata=" . base64_encode(serialize($download)), "Server Icon", null, FALSE);
00426         }
00427       }
00428       elseif(in_array($this->currObj["virtualserver_icon_id"], $this->cachedIcons))
00429       {
00430         $html .= $this->getImage("group_icon_" . $this->currObj["virtualserver_icon_id"] . ".png", "Server Icon");
00431       }
00432     }
00433 
00434     return $html;
00435   }
00436 
00437   /**
00438    * Returns the HTML img tags which can be used to display the various icons for a
00439    * TeamSpeak_Node_Channel object.
00440    *
00441    * @return string
00442    */
00443   protected function getSuffixIconChannel()
00444   {
00445     if($this->currObj instanceof TeamSpeak3_Node_Channel && $this->currObj->isSpacer()) return;
00446 
00447     $html = "";
00448 
00449     if($this->currObj["channel_flag_default"])
00450     {
00451       $html .= $this->getImage("channel_flag_default.png", "Default Channel");
00452     }
00453 
00454     if($this->currObj["channel_flag_password"])
00455     {
00456       $html .= $this->getImage("channel_flag_password.png", "Password-protected");
00457     }
00458 
00459     if($this->currObj["channel_codec"] == 3)
00460     {
00461       $html .= $this->getImage("channel_flag_music.png", "Music Codec");
00462     }
00463 
00464     if($this->currObj["channel_needed_talk_power"])
00465     {
00466       $html .= $this->getImage("channel_flag_moderated.png", "Moderated");
00467     }
00468 
00469     if($this->currObj["channel_icon_id"])
00470     {
00471       if(!$this->currObj->iconIsLocal("channel_icon_id") && $this->ftclient)
00472       {
00473         if(!isset($this->cacheIcon[$this->currObj["channel_icon_id"]]))
00474         {
00475           $download = $this->currObj->getParent()->transferInitDownload(rand(0x0000, 0xFFFF), 0, $this->currObj->iconGetName("channel_icon_id"));
00476 
00477           if($this->ftclient == "data:image")
00478           {
00479             $download = TeamSpeak3::factory("filetransfer://" . $download["host"] . ":" . $download["port"])->download($download["ftkey"], $download["size"]);
00480           }
00481 
00482           $this->cacheIcon[$this->currObj["channel_icon_id"]] = $download;
00483         }
00484         else
00485         {
00486           $download = $this->cacheIcon[$this->currObj["channel_icon_id"]];
00487         }
00488 
00489         if($this->ftclient == "data:image")
00490         {
00491           $html .= $this->getImage("data:" . TeamSpeak3_Helper_Convert::imageMimeType($download) . ";base64," . base64_encode($download), "Channel Icon", null, FALSE);
00492         }
00493         else
00494         {
00495           $html .= $this->getImage($this->ftclient . "?ftdata=" . base64_encode(serialize($download)), "Channel Icon", null, FALSE);
00496         }
00497       }
00498       elseif(in_array($this->currObj["channel_icon_id"], $this->cachedIcons))
00499       {
00500         $html .= $this->getImage("group_icon_" . $this->currObj["channel_icon_id"] . ".png", "Channel Icon");
00501       }
00502     }
00503 
00504     return $html;
00505   }
00506 
00507   /**
00508    * Returns the HTML img tags which can be used to display the various icons for a
00509    * TeamSpeak_Node_Client object.
00510    *
00511    * @return string
00512    */
00513   protected function getSuffixIconClient()
00514   {
00515     $html = "";
00516 
00517     if($this->currObj["client_is_priority_speaker"])
00518     {
00519       $html .= $this->getImage("client_priority.png", "Priority Speaker");
00520     }
00521 
00522     if($this->currObj["client_is_channel_commander"])
00523     {
00524       $html .= $this->getImage("client_cc.png", "Channel Commander");
00525     }
00526 
00527     if($this->currObj["client_is_talker"])
00528     {
00529       $html .= $this->getImage("client_talker.png", "Talk Power granted");
00530     }
00531 
00532     foreach($this->currObj->memberOf() as $group)
00533     {
00534       if(!$group["iconid"]) continue;
00535 
00536       $type = ($group instanceof TeamSpeak3_Node_Servergroup) ? "Server Group" : "Channel Group";
00537 
00538       if(!$group->iconIsLocal("iconid") && $this->ftclient)
00539       {
00540         if(!isset($this->cacheIcon[$group["iconid"]]))
00541         {
00542           $download = $group->getParent()->transferInitDownload(rand(0x0000, 0xFFFF), 0, $group->iconGetName("iconid"));
00543 
00544           if($this->ftclient == "data:image")
00545           {
00546             $download = TeamSpeak3::factory("filetransfer://" . $download["host"] . ":" . $download["port"])->download($download["ftkey"], $download["size"]);
00547           }
00548 
00549           $this->cacheIcon[$group["iconid"]] = $download;
00550         }
00551         else
00552         {
00553           $download = $this->cacheIcon[$group["iconid"]];
00554         }
00555 
00556         if($this->ftclient == "data:image")
00557         {
00558           $html .= $this->getImage("data:" . TeamSpeak3_Helper_Convert::imageMimeType($download) . ";base64," . base64_encode($download), $group . " [" . $type . "]", null, FALSE);
00559         }
00560         else
00561         {
00562           $html .= $this->getImage($this->ftclient . "?ftdata=" . base64_encode(serialize($download)), $group . " [" . $type . "]", null, FALSE);
00563         }
00564       }
00565       elseif(in_array($group["iconid"], $this->cachedIcons))
00566       {
00567         $html .= $this->getImage("group_icon_" . $group["iconid"] . ".png", $group . " [" . $type . "]");
00568       }
00569     }
00570 
00571     if($this->currObj["client_icon_id"])
00572     {
00573       if(!$this->currObj->iconIsLocal("client_icon_id") && $this->ftclient)
00574       {
00575         if(!isset($this->cacheIcon[$this->currObj["client_icon_id"]]))
00576         {
00577           $download = $this->currObj->getParent()->transferInitDownload(rand(0x0000, 0xFFFF), 0, $this->currObj->iconGetName("client_icon_id"));
00578 
00579           if($this->ftclient == "data:image")
00580           {
00581             $download = TeamSpeak3::factory("filetransfer://" . $download["host"] . ":" . $download["port"])->download($download["ftkey"], $download["size"]);
00582           }
00583 
00584           $this->cacheIcon[$this->currObj["client_icon_id"]] = $download;
00585         }
00586         else
00587         {
00588           $download = $this->cacheIcon[$this->currObj["client_icon_id"]];
00589         }
00590 
00591         if($this->ftclient == "data:image")
00592         {
00593           $html .= $this->getImage("data:" . TeamSpeak3_Helper_Convert::imageMimeType($download) . ";base64," . base64_encode($download), "Client Icon", null, FALSE);
00594         }
00595         else
00596         {
00597           $html .= $this->getImage($this->ftclient . "?ftdata=" . base64_encode(serialize($download)), "Client Icon", null, FALSE);
00598         }
00599       }
00600       elseif(in_array($this->currObj["client_icon_id"], $this->cachedIcons))
00601       {
00602         $html .= $this->getImage("group_icon_" . $this->currObj["client_icon_id"] . ".png", "Client Icon");
00603       }
00604     }
00605 
00606     return $html;
00607   }
00608 
00609   /**
00610    * Returns a HTML img tag which can be used to display the country flag for a
00611    * TeamSpeak_Node_Client object.
00612    *
00613    * @return string
00614    */
00615   protected function getSuffixFlag()
00616   {
00617     if(!$this->currObj instanceof TeamSpeak3_Node_Client) return;
00618 
00619     if($this->flagpath && $this->currObj["client_country"])
00620     {
00621       return $this->getImage($this->currObj["client_country"]->toLower() . ".png", $this->currObj["client_country"], null, FALSE, TRUE);
00622     }
00623   }
00624 
00625   /**
00626    * Returns the code to display a custom HTML img tag.
00627    *
00628    * @param  string  $name
00629    * @param  string  $text
00630    * @param  string  $class
00631    * @param  boolean $iconpath
00632    * @param  boolean $flagpath
00633    * @return string
00634    */
00635   protected function getImage($name, $text = "", $class = null, $iconpath = TRUE, $flagpath = FALSE)
00636   {
00637     $src = "";
00638 
00639     if($iconpath)
00640     {
00641       $src = $this->iconpath;
00642     }
00643 
00644     if($flagpath)
00645     {
00646       $src = $this->flagpath;
00647     }
00648 
00649     return "<img src='" . $src . $name . "' title='" . $text . "' alt='' align='top' />";
00650   }
00651 }
 All Classes Files Functions Variables