TeamSpeak 3 PHP Framework  1.1.12
libraries/TeamSpeak3/Helper/Convert.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /**
00004  * @file
00005  * TeamSpeak 3 PHP Framework
00006  *
00007  * $Id: Convert.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_Helper_Convert
00030  * @brief Helper class for data conversion.
00031  */
00032 class TeamSpeak3_Helper_Convert
00033 {
00034   /**
00035    * Converts bytes to a human readable value.
00036    *
00037    * @param  integer $bytes
00038    * @return string
00039    */
00040   public static function bytes($bytes)
00041   {
00042     $kbytes = sprintf("%.02f", $bytes/1024);
00043     $mbytes = sprintf("%.02f", $kbytes/1024);
00044     $gbytes = sprintf("%.02f", $mbytes/1024);
00045     $tbytes = sprintf("%.02f", $gbytes/1024);
00046 
00047     if($tbytes >= 1)
00048       return $tbytes . " TB";
00049     if($gbytes >= 1)
00050       return $gbytes . " GB";
00051     if($mbytes >= 1)
00052       return $mbytes . " MB";
00053     if($kbytes >= 1)
00054       return $kbytes . " KB";
00055 
00056     return $bytes . " B";
00057   }
00058 
00059   /**
00060    * Converts seconds/milliseconds to a human readable value.
00061    *
00062    * @param  integer $seconds
00063    * @param  boolean $is_ms
00064    * @param  string  $format
00065    * @return string
00066    */
00067   public static function seconds($seconds, $is_ms = FALSE, $format = "%dD %02d:%02d:%02d")
00068   {
00069     if($is_ms) $seconds = $seconds/1000;
00070 
00071     return sprintf($format, $seconds/60/60/24, ($seconds/60/60)%24, ($seconds/60)%60, $seconds%60);
00072   }
00073 
00074   /**
00075    * Converts a given codec ID to a human readable name.
00076    *
00077    * @param  integer $codec
00078    * @return string
00079    */
00080   public static function codec($codec)
00081   {
00082     if($codec == TeamSpeak3::CODEC_SPEEX_NARROWBAND)
00083       return "Speex Narrowband (8 kHz)";
00084     if($codec == TeamSpeak3::CODEC_SPEEX_WIDEBAND)
00085       return "Speex Wideband (16 kHz)";
00086     if($codec == TeamSpeak3::CODEC_SPEEX_ULTRAWIDEBAND)
00087       return "Speex Ultra-Wideband (32 kHz)";
00088     if($codec == TeamSpeak3::CODEC_CELT_MONO)
00089       return "CELT Mono (48 kHz)";
00090 
00091     return "Unknown";
00092   }
00093 
00094   /**
00095    * Converts a given group type ID to a human readable name.
00096    *
00097    * @param  integer $type
00098    * @return string
00099    */
00100   public static function groupType($type)
00101   {
00102     if($type == TeamSpeak3::GROUP_DBTYPE_TEMPLATE)
00103       return "Template";
00104     if($type == TeamSpeak3::GROUP_DBTYPE_REGULAR)
00105       return "Regular";
00106     if($type == TeamSpeak3::GROUP_DBTYPE_SERVERQUERY)
00107       return "ServerQuery";
00108 
00109     return "Unknown";
00110   }
00111 
00112   /**
00113    * Converts a given permission type ID to a human readable name.
00114    *
00115    * @param  integer $type
00116    * @return string
00117    */
00118   public static function permissionType($type)
00119   {
00120     if($type == TeamSpeak3::PERM_TYPE_SERVERGROUP)
00121       return "Server Group";
00122     if($type == TeamSpeak3::PERM_TYPE_CLIENT)
00123       return "Client";
00124     if($type == TeamSpeak3::PERM_TYPE_CHANNEL)
00125       return "Channel";
00126     if($type == TeamSpeak3::PERM_TYPE_CHANNELGROUP)
00127       return "Channel Group";
00128     if($type == TeamSpeak3::PERM_TYPE_CHANNELCLIENT)
00129       return "Channel Client";
00130 
00131     return "Unknown";
00132   }
00133 
00134   /**
00135    * Converts a given permission category value to a human readable name.
00136    *
00137    * @param  integer $pcat
00138    * @return string
00139    */
00140   public static function permissionCategory($pcat)
00141   {
00142     if($pcat == TeamSpeak3::PERM_CAT_GLOBAL)
00143       return "Global";
00144     if($pcat == TeamSpeak3::PERM_CAT_GLOBAL_INFORMATION)
00145       return "Global / Information";
00146     if($pcat == TeamSpeak3::PERM_CAT_GLOBAL_SERVER_MGMT)
00147       return "Global / Virtual Server Management";
00148     if($pcat == TeamSpeak3::PERM_CAT_GLOBAL_ADM_ACTIONS)
00149       return "Global / Administration";
00150     if($pcat == TeamSpeak3::PERM_CAT_GLOBAL_SETTINGS)
00151       return "Global / Settings";
00152     if($pcat == TeamSpeak3::PERM_CAT_SERVER)
00153       return "Virtual Server";
00154     if($pcat == TeamSpeak3::PERM_CAT_SERVER_INFORMATION)
00155       return "Virtual Server / Information";
00156     if($pcat == TeamSpeak3::PERM_CAT_SERVER_ADM_ACTIONS)
00157       return "Virtual Server / Administration";
00158     if($pcat == TeamSpeak3::PERM_CAT_SERVER_SETTINGS)
00159       return "Virtual Server / Settings";
00160     if($pcat == TeamSpeak3::PERM_CAT_CHANNEL)
00161       return "Channel";
00162     if($pcat == TeamSpeak3::PERM_CAT_CHANNEL_INFORMATION)
00163       return "Channel / Information";
00164     if($pcat == TeamSpeak3::PERM_CAT_CHANNEL_CREATE)
00165       return "Channel / Create";
00166     if($pcat == TeamSpeak3::PERM_CAT_CHANNEL_MODIFY)
00167       return "Channel / Modify";
00168     if($pcat == TeamSpeak3::PERM_CAT_CHANNEL_DELETE)
00169       return "Channel / Delete";
00170     if($pcat == TeamSpeak3::PERM_CAT_CHANNEL_ACCESS)
00171       return "Channel / Access";
00172     if($pcat == TeamSpeak3::PERM_CAT_GROUP)
00173       return "Group";
00174     if($pcat == TeamSpeak3::PERM_CAT_GROUP_INFORMATION)
00175       return "Group / Information";
00176     if($pcat == TeamSpeak3::PERM_CAT_GROUP_CREATE)
00177       return "Group / Create";
00178     if($pcat == TeamSpeak3::PERM_CAT_GROUP_MODIFY)
00179       return "Group / Modify";
00180     if($pcat == TeamSpeak3::PERM_CAT_GROUP_DELETE)
00181       return "Group / Delete";
00182     if($pcat == TeamSpeak3::PERM_CAT_CLIENT)
00183       return "Client";
00184     if($pcat == TeamSpeak3::PERM_CAT_CLIENT_INFORMATION)
00185       return "Client / Information";
00186     if($pcat == TeamSpeak3::PERM_CAT_CLIENT_ADM_ACTIONS)
00187       return "Client / Admin";
00188     if($pcat == TeamSpeak3::PERM_CAT_CLIENT_BASICS)
00189       return "Client / Basics";
00190     if($pcat == TeamSpeak3::PERM_CAT_CLIENT_MODIFY)
00191       return "Client / Modify";
00192     if($pcat == TeamSpeak3::PERM_CAT_FILETRANSFER)
00193       return "File Transfer";
00194     if($pcat == TeamSpeak3::PERM_CAT_NEEDED_MODIFY_POWER)
00195       return "Grant";
00196 
00197     return "Unknown";
00198   }
00199 
00200   /**
00201    * Converts a given log level ID to a human readable name and vice versa.
00202    *
00203    * @param  mixed  $level
00204    * @return string
00205    */
00206   public static function logLevel($level)
00207   {
00208     if(is_numeric($level))
00209     {
00210       if($level == TeamSpeak3::LOGLEVEL_CRITICAL)
00211         return "CRITICAL";
00212       if($level == TeamSpeak3::LOGLEVEL_ERROR)
00213         return "ERROR";
00214       if($level == TeamSpeak3::LOGLEVEL_DEBUG)
00215         return "DEBUG";
00216       if($level == TeamSpeak3::LOGLEVEL_WARNING)
00217         return "WARNING";
00218       if($level == TeamSpeak3::LOGLEVEL_INFO)
00219         return "INFO";
00220 
00221       return "DEVELOP";
00222     }
00223     else
00224     {
00225       if(strtoupper($level) == "CRITICAL")
00226         return TeamSpeak3::LOGLEVEL_CRITICAL;
00227       if(strtoupper($level) == "ERROR")
00228         return TeamSpeak3::LOGLEVEL_ERROR;
00229       if(strtoupper($level) == "DEBUG")
00230         return TeamSpeak3::LOGLEVEL_DEBUG;
00231       if(strtoupper($level) == "WARNING")
00232         return TeamSpeak3::LOGLEVEL_WARNING;
00233       if(strtoupper($level) == "INFO")
00234         return TeamSpeak3::LOGLEVEL_INFO;
00235 
00236       return TeamSpeak3::LOGLEVEL_DEVEL;
00237     }
00238   }
00239 
00240   /**
00241    * Converts a specified log entry string into an array containing the data.
00242    *
00243    * @param  string $entry
00244    * @return array
00245    */
00246   public static function logEntry($entry)
00247   {
00248     $parts = explode("|", $entry, 5);
00249     $array = array();
00250 
00251     if(count($parts) != 5)
00252     {
00253       $array["timestamp"] = 0;
00254       $array["level"]     = TeamSpeak3::LOGLEVEL_ERROR;
00255       $array["channel"]   = "ParamParser";
00256       $array["server_id"] = "";
00257       $array["msg"]       = TeamSpeak3_Helper_String::factory("convert error (" . trim($entry) . ")");
00258       $array["msg_plain"] = $entry;
00259       $array["malformed"] = TRUE;
00260     }
00261     else
00262     {
00263       $array["timestamp"] = strtotime(trim($parts[0]));
00264       $array["level"]     = self::logLevel(trim($parts[1]));
00265       $array["channel"]   = trim($parts[2]);
00266       $array["server_id"] = trim($parts[3]);
00267       $array["msg"]       = TeamSpeak3_Helper_String::factory(trim($parts[4]));
00268       $array["msg_plain"] = $entry;
00269       $array["malformed"] = FALSE;
00270     }
00271 
00272     return $array;
00273   }
00274 
00275   /**
00276    * Converts a given string to a ServerQuery password hash.
00277    *
00278    * @param  string $plain
00279    * @return string
00280    */
00281   public static function password($plain)
00282   {
00283     return base64_encode(sha1($plain, TRUE));
00284   }
00285 
00286   /**
00287    * Returns a client-like formatted version of the TeamSpeak 3 version string.
00288    *
00289    * @param  string $version
00290    * @param  string $format
00291    * @return string
00292    */
00293   public static function version($version, $format = "Y-m-d h:i:s")
00294   {
00295     if(!$version instanceof TeamSpeak3_Helper_String)
00296     {
00297       $version = new TeamSpeak3_Helper_String($version);
00298     }
00299 
00300     $buildno = $version->section("[", 1)->filterDigits()->toInt();
00301 
00302     return ($buildno <= 15001) ? $version : $version->section("[")->append("(" . date($format, $buildno) . ")");
00303   }
00304 
00305   /**
00306    * Tries to detect the type of an image by a given string and returns it.
00307    *
00308    * @param  string $binary
00309    * @return string
00310    */
00311   public static function imageMimeType($binary)
00312   {
00313     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))
00314     {
00315       return "application/octet-stream";
00316     }
00317 
00318     $type = array(
00319       1 => "image/jpeg",
00320       2 => "image/gif",
00321       3 => "image/png",
00322       4 => "image/x-windows-bmp",
00323       5 => "image/tiff",
00324       6 => "image/x-ilbm",
00325     );
00326 
00327     return $type[count($matches)-1];
00328   }
00329 }
 All Classes Files Functions Variables