Advertisement
Guest User

TeamSpeak3_Helper_Convert (Patch)

a guest
Jun 27th, 2011
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.64 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @file
  5.  * TeamSpeak 3 PHP Framework
  6.  *
  7.  * $Id: Convert.php 6/4/2011 3:01:20 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.6-beta
  24.  * @author    Sven 'ScP' Paulsen
  25.  * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved.
  26.  */
  27.  
  28. /**
  29.  * @class TeamSpeak3_Helper_Convert
  30.  * @brief Helper class for data conversion.
  31.  */
  32. class TeamSpeak3_Helper_Convert
  33. {
  34.   /**
  35.    * Converts bytes to a human readable value.
  36.    *
  37.    * @param  integer $bytes
  38.    * @return string
  39.    */
  40.   public static function bytes($bytes)
  41.   {
  42.     $kbytes = sprintf("%.02f", $bytes/1024);
  43.     $mbytes = sprintf("%.02f", $kbytes/1024);
  44.     $gbytes = sprintf("%.02f", $mbytes/1024);
  45.     $tbytes = sprintf("%.02f", $gbytes/1024);
  46.  
  47.     if($tbytes >= 1)
  48.       return $tbytes . " TB";
  49.     if($gbytes >= 1)
  50.       return $gbytes . " GB";
  51.     if($mbytes >= 1)
  52.       return $mbytes . " MB";
  53.     if($kbytes >= 1)
  54.       return $kbytes . " KB";
  55.  
  56.     return $bytes . " B";
  57.   }
  58.  
  59.   /**
  60.    * Converts seconds/milliseconds to a human readable value.
  61.    *
  62.    * @param  integer $seconds
  63.    * @param  boolean $is_ms
  64.    * @param  string  $format
  65.    * @return string
  66.    */
  67.   public static function seconds($seconds, $is_ms = FALSE, $format = "%dD %02d:%02d:%02d")
  68.   {
  69.     if($is_ms) $seconds = $seconds/1000;
  70.  
  71.     return sprintf($format, $seconds/60/60/24, ($seconds/60/60)%24, ($seconds/60)%60, $seconds%60);
  72.   }
  73.  
  74.   /**
  75.    * Converts a given codec ID to a human readable name.
  76.    *
  77.    * @param  integer $codec
  78.    * @return string
  79.    */
  80.   public static function codec($codec)
  81.   {
  82.     if($codec == TeamSpeak3::CODEC_SPEEX_NARROWBAND)
  83.       return "Speex Narrowband (8 kHz)";
  84.     if($codec == TeamSpeak3::CODEC_SPEEX_WIDEBAND)
  85.       return "Speex Wideband (16 kHz)";
  86.     if($codec == TeamSpeak3::CODEC_SPEEX_ULTRAWIDEBAND)
  87.       return "Speex Ultra-Wideband (32 kHz)";
  88.     if($codec == TeamSpeak3::CODEC_CELT_MONO)
  89.       return "CELT Mono (48 kHz)";
  90.  
  91.     return "Unknown";
  92.   }
  93.  
  94.   /**
  95.    * Converts a given group type ID to a human readable name.
  96.    *
  97.    * @param  integer $type
  98.    * @return string
  99.    */
  100.   public static function groupType($type)
  101.   {
  102.     if($type == TeamSpeak3::GROUP_DBTYPE_TEMPLATE)
  103.       return "Template";
  104.     if($type == TeamSpeak3::GROUP_DBTYPE_REGULAR)
  105.       return "Regular";
  106.     if($type == TeamSpeak3::GROUP_DBTYPE_SERVERQUERY)
  107.       return "ServerQuery";
  108.  
  109.     return "Unknown";
  110.   }
  111.  
  112.   /**
  113.    * Converts a given permission type ID to a human readable name.
  114.    *
  115.    * @param  integer $type
  116.    * @return string
  117.    */
  118.   public static function permissionType($type)
  119.   {
  120.     if($type == TeamSpeak3::PERM_TYPE_SERVERGROUP)
  121.       return "Server Group";
  122.     if($type == TeamSpeak3::PERM_TYPE_CLIENT)
  123.       return "Client";
  124.     if($type == TeamSpeak3::PERM_TYPE_CHANNEL)
  125.       return "Channel";
  126.     if($type == TeamSpeak3::PERM_TYPE_CHANNELGROUP)
  127.       return "Channel Group";
  128.     if($type == TeamSpeak3::PERM_TYPE_CHANNELCLIENT)
  129.       return "Channel Client";
  130.  
  131.     return "Unknown";
  132.   }
  133.  
  134.   /**
  135.    * Converts a given permission category value to a human readable name.
  136.    *
  137.    * @param  integer $pcat
  138.    * @return string
  139.    */
  140.   public static function permissionCategory($pcat)
  141.   {
  142.     if($pcat == TeamSpeak3::PERM_CAT_GLOBAL)
  143.       return "Global";
  144.     if($pcat == TeamSpeak3::PERM_CAT_GLOBAL_INFORMATION)
  145.       return "Global / Information";
  146.     if($pcat == TeamSpeak3::PERM_CAT_GLOBAL_SERVER_MGMT)
  147.       return "Global / Virtual Server Management";
  148.     if($pcat == TeamSpeak3::PERM_CAT_GLOBAL_ADM_ACTIONS)
  149.       return "Global / Administration";
  150.     if($pcat == TeamSpeak3::PERM_CAT_GLOBAL_SETTINGS)
  151.       return "Global / Settings";
  152.     if($pcat == TeamSpeak3::PERM_CAT_SERVER)
  153.       return "Virtual Server";
  154.     if($pcat == TeamSpeak3::PERM_CAT_SERVER_INFORMATION)
  155.       return "Virtual Server / Information";
  156.     if($pcat == TeamSpeak3::PERM_CAT_SERVER_ADM_ACTIONS)
  157.       return "Virtual Server / Administration";
  158.     if($pcat == TeamSpeak3::PERM_CAT_SERVER_SETTINGS)
  159.       return "Virtual Server / Settings";
  160.     if($pcat == TeamSpeak3::PERM_CAT_CHANNEL)
  161.       return "Channel";
  162.     if($pcat == TeamSpeak3::PERM_CAT_CHANNEL_INFORMATION)
  163.       return "Channel / Information";
  164.     if($pcat == TeamSpeak3::PERM_CAT_CHANNEL_CREATE)
  165.       return "Channel / Create";
  166.     if($pcat == TeamSpeak3::PERM_CAT_CHANNEL_MODIFY)
  167.       return "Channel / Modify";
  168.     if($pcat == TeamSpeak3::PERM_CAT_CHANNEL_DELETE)
  169.       return "Channel / Delete";
  170.     if($pcat == TeamSpeak3::PERM_CAT_CHANNEL_ACCESS)
  171.       return "Channel / Access";
  172.     if($pcat == TeamSpeak3::PERM_CAT_GROUP)
  173.       return "Group";
  174.     if($pcat == TeamSpeak3::PERM_CAT_GROUP_INFORMATION)
  175.       return "Group / Information";
  176.     if($pcat == TeamSpeak3::PERM_CAT_GROUP_CREATE)
  177.       return "Group / Create";
  178.     if($pcat == TeamSpeak3::PERM_CAT_GROUP_MODIFY)
  179.       return "Group / Modify";
  180.     if($pcat == TeamSpeak3::PERM_CAT_GROUP_DELETE)
  181.       return "Group / Delete";
  182.     if($pcat == TeamSpeak3::PERM_CAT_CLIENT)
  183.       return "Client";
  184.     if($pcat == TeamSpeak3::PERM_CAT_CLIENT_INFORMATION)
  185.       return "Client / Information";
  186.     if($pcat == TeamSpeak3::PERM_CAT_CLIENT_ADM_ACTIONS)
  187.       return "Client / Admin";
  188.     if($pcat == TeamSpeak3::PERM_CAT_CLIENT_BASICS)
  189.       return "Client / Basics";
  190.     if($pcat == TeamSpeak3::PERM_CAT_CLIENT_MODIFY)
  191.       return "Client / Modify";
  192.     if($pcat == TeamSpeak3::PERM_CAT_FILETRANSFER)
  193.       return "File Transfer";
  194.     if($pcat == TeamSpeak3::PERM_CAT_NEEDED_MODIFY_POWER)
  195.       return "Grant";
  196.  
  197.     return "Unknown";
  198.   }
  199.  
  200.   /**
  201.    * Converts a given log level ID to a human readable name.
  202.    *
  203.    * @param  integer $level
  204.    * @return string
  205.    */
  206.   public static function logLevel($level)
  207.   {
  208.     if($level == TeamSpeak3::LOGLEVEL_CRITICAL)
  209.       return "CRITICAL";
  210.     if($level == TeamSpeak3::LOGLEVEL_ERROR)
  211.       return "ERROR";
  212.     if($level == TeamSpeak3::LOGLEVEL_DEBUG)
  213.       return "DEBUG";
  214.     if($level == TeamSpeak3::LOGLEVEL_WARNING)
  215.       return "WARNING";
  216.     if($level == TeamSpeak3::LOGLEVEL_INFO)
  217.       return "INFO";
  218.  
  219.     return "DEVELOP";
  220.   }
  221.  
  222.   /**
  223.    * Converts a given string to a ServerQuery password hash.
  224.    *
  225.    * @param  string $plain
  226.    * @return string
  227.    */
  228.   public static function password($plain)
  229.   {
  230.     return base64_encode(sha1($plain, TRUE));
  231.   }
  232.  
  233.   /**
  234.    * Tries to detect the type of an image by a given string and returns it.
  235.    *
  236.    * @param  string $binary
  237.    * @return string
  238.    */
  239.   public static function imageMimeType($binary)
  240.   {
  241.     if(!preg_match('/\A(?:(\xff\xd8\xff)|(GIF8[79]a)|(\x89PNG\x0d\x0a)|(BM)|(\x49\x49(\x2a\x00|\x00\x4a))|(FORM.{4}ILBM))/', $binary, $matches))
  242.     {
  243.       return "application/octet-stream";
  244.     }
  245.  
  246.     $type = array(
  247.       1 => "image/jpeg",
  248.       2 => "image/gif",
  249.       3 => "image/png",
  250.       4 => "image/x-windows-bmp",
  251.       5 => "image/tiff",
  252.       6 => "image/x-ilbm",
  253.     );
  254.  
  255.     return $type[count($matches)-1];
  256.   }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement