Convert.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. /**
  3. * @file
  4. * TeamSpeak 3 PHP Framework
  5. *
  6. * $Id: Convert.php 06/06/2016 22:27:13 scp@Svens-iMac $
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * @package TeamSpeak3
  22. * @version 1.1.24
  23. * @author Sven 'ScP' Paulsen
  24. * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved.
  25. */
  26. /**
  27. * @class TeamSpeak3_Helper_Convert
  28. * @brief Helper class for data conversion.
  29. */
  30. class TeamSpeak3_Helper_Convert
  31. {
  32. /**
  33. * Converts bytes to a human readable value.
  34. *
  35. * @param integer $bytes
  36. * @return string
  37. */
  38. public static function bytes($bytes)
  39. {
  40. $kbytes = sprintf("%.02f", $bytes/1024);
  41. $mbytes = sprintf("%.02f", $kbytes/1024);
  42. $gbytes = sprintf("%.02f", $mbytes/1024);
  43. $tbytes = sprintf("%.02f", $gbytes/1024);
  44. if($tbytes >= 1)
  45. return $tbytes . " TB";
  46. if($gbytes >= 1)
  47. return $gbytes . " GB";
  48. if($mbytes >= 1)
  49. return $mbytes . " MB";
  50. if($kbytes >= 1)
  51. return $kbytes . " KB";
  52. return $bytes . " B";
  53. }
  54. /**
  55. * Converts seconds/milliseconds to a human readable value.
  56. *
  57. * @param integer $seconds
  58. * @param boolean $is_ms
  59. * @param string $format
  60. * @return string
  61. */
  62. public static function seconds($seconds, $is_ms = FALSE, $format = "%dD %02d:%02d:%02d")
  63. {
  64. if($is_ms) $seconds = $seconds/1000;
  65. return sprintf($format, $seconds/60/60/24, ($seconds/60/60)%24, ($seconds/60)%60, $seconds%60);
  66. }
  67. /**
  68. * Converts a given codec ID to a human readable name.
  69. *
  70. * @param integer $codec
  71. * @return string
  72. */
  73. public static function codec($codec)
  74. {
  75. if($codec == TeamSpeak3::CODEC_SPEEX_NARROWBAND)
  76. return "Speex Narrowband";
  77. if($codec == TeamSpeak3::CODEC_SPEEX_WIDEBAND)
  78. return "Speex Wideband";
  79. if($codec == TeamSpeak3::CODEC_SPEEX_ULTRAWIDEBAND)
  80. return "Speex Ultra-Wideband";
  81. if($codec == TeamSpeak3::CODEC_CELT_MONO)
  82. return "CELT Mono";
  83. if($codec == TeamSpeak3::CODEC_OPUS_VOICE)
  84. return "Opus Voice";
  85. if($codec == TeamSpeak3::CODEC_OPUS_MUSIC)
  86. return "Opus Music";
  87. return "Unknown";
  88. }
  89. /**
  90. * Converts a given group type ID to a human readable name.
  91. *
  92. * @param integer $type
  93. * @return string
  94. */
  95. public static function groupType($type)
  96. {
  97. if($type == TeamSpeak3::GROUP_DBTYPE_TEMPLATE)
  98. return "Template";
  99. if($type == TeamSpeak3::GROUP_DBTYPE_REGULAR)
  100. return "Regular";
  101. if($type == TeamSpeak3::GROUP_DBTYPE_SERVERQUERY)
  102. return "ServerQuery";
  103. return "Unknown";
  104. }
  105. /**
  106. * Converts a given permission type ID to a human readable name.
  107. *
  108. * @param integer $type
  109. * @return string
  110. */
  111. public static function permissionType($type)
  112. {
  113. if($type == TeamSpeak3::PERM_TYPE_SERVERGROUP)
  114. return "Server Group";
  115. if($type == TeamSpeak3::PERM_TYPE_CLIENT)
  116. return "Client";
  117. if($type == TeamSpeak3::PERM_TYPE_CHANNEL)
  118. return "Channel";
  119. if($type == TeamSpeak3::PERM_TYPE_CHANNELGROUP)
  120. return "Channel Group";
  121. if($type == TeamSpeak3::PERM_TYPE_CHANNELCLIENT)
  122. return "Channel Client";
  123. return "Unknown";
  124. }
  125. /**
  126. * Converts a given permission category value to a human readable name.
  127. *
  128. * @param integer $pcat
  129. * @return string
  130. */
  131. public static function permissionCategory($pcat)
  132. {
  133. if($pcat == TeamSpeak3::PERM_CAT_GLOBAL)
  134. return "Global";
  135. if($pcat == TeamSpeak3::PERM_CAT_GLOBAL_INFORMATION)
  136. return "Global / Information";
  137. if($pcat == TeamSpeak3::PERM_CAT_GLOBAL_SERVER_MGMT)
  138. return "Global / Virtual Server Management";
  139. if($pcat == TeamSpeak3::PERM_CAT_GLOBAL_ADM_ACTIONS)
  140. return "Global / Administration";
  141. if($pcat == TeamSpeak3::PERM_CAT_GLOBAL_SETTINGS)
  142. return "Global / Settings";
  143. if($pcat == TeamSpeak3::PERM_CAT_SERVER)
  144. return "Virtual Server";
  145. if($pcat == TeamSpeak3::PERM_CAT_SERVER_INFORMATION)
  146. return "Virtual Server / Information";
  147. if($pcat == TeamSpeak3::PERM_CAT_SERVER_ADM_ACTIONS)
  148. return "Virtual Server / Administration";
  149. if($pcat == TeamSpeak3::PERM_CAT_SERVER_SETTINGS)
  150. return "Virtual Server / Settings";
  151. if($pcat == TeamSpeak3::PERM_CAT_CHANNEL)
  152. return "Channel";
  153. if($pcat == TeamSpeak3::PERM_CAT_CHANNEL_INFORMATION)
  154. return "Channel / Information";
  155. if($pcat == TeamSpeak3::PERM_CAT_CHANNEL_CREATE)
  156. return "Channel / Create";
  157. if($pcat == TeamSpeak3::PERM_CAT_CHANNEL_MODIFY)
  158. return "Channel / Modify";
  159. if($pcat == TeamSpeak3::PERM_CAT_CHANNEL_DELETE)
  160. return "Channel / Delete";
  161. if($pcat == TeamSpeak3::PERM_CAT_CHANNEL_ACCESS)
  162. return "Channel / Access";
  163. if($pcat == TeamSpeak3::PERM_CAT_GROUP)
  164. return "Group";
  165. if($pcat == TeamSpeak3::PERM_CAT_GROUP_INFORMATION)
  166. return "Group / Information";
  167. if($pcat == TeamSpeak3::PERM_CAT_GROUP_CREATE)
  168. return "Group / Create";
  169. if($pcat == TeamSpeak3::PERM_CAT_GROUP_MODIFY)
  170. return "Group / Modify";
  171. if($pcat == TeamSpeak3::PERM_CAT_GROUP_DELETE)
  172. return "Group / Delete";
  173. if($pcat == TeamSpeak3::PERM_CAT_CLIENT)
  174. return "Client";
  175. if($pcat == TeamSpeak3::PERM_CAT_CLIENT_INFORMATION)
  176. return "Client / Information";
  177. if($pcat == TeamSpeak3::PERM_CAT_CLIENT_ADM_ACTIONS)
  178. return "Client / Admin";
  179. if($pcat == TeamSpeak3::PERM_CAT_CLIENT_BASICS)
  180. return "Client / Basics";
  181. if($pcat == TeamSpeak3::PERM_CAT_CLIENT_MODIFY)
  182. return "Client / Modify";
  183. if($pcat == TeamSpeak3::PERM_CAT_FILETRANSFER)
  184. return "File Transfer";
  185. if($pcat == TeamSpeak3::PERM_CAT_NEEDED_MODIFY_POWER)
  186. return "Grant";
  187. return "Unknown";
  188. }
  189. /**
  190. * Converts a given log level ID to a human readable name and vice versa.
  191. *
  192. * @param mixed $level
  193. * @return string
  194. */
  195. public static function logLevel($level)
  196. {
  197. if(is_numeric($level))
  198. {
  199. if($level == TeamSpeak3::LOGLEVEL_CRITICAL)
  200. return "CRITICAL";
  201. if($level == TeamSpeak3::LOGLEVEL_ERROR)
  202. return "ERROR";
  203. if($level == TeamSpeak3::LOGLEVEL_DEBUG)
  204. return "DEBUG";
  205. if($level == TeamSpeak3::LOGLEVEL_WARNING)
  206. return "WARNING";
  207. if($level == TeamSpeak3::LOGLEVEL_INFO)
  208. return "INFO";
  209. return "DEVELOP";
  210. }
  211. else
  212. {
  213. if(strtoupper($level) == "CRITICAL")
  214. return TeamSpeak3::LOGLEVEL_CRITICAL;
  215. if(strtoupper($level) == "ERROR")
  216. return TeamSpeak3::LOGLEVEL_ERROR;
  217. if(strtoupper($level) == "DEBUG")
  218. return TeamSpeak3::LOGLEVEL_DEBUG;
  219. if(strtoupper($level) == "WARNING")
  220. return TeamSpeak3::LOGLEVEL_WARNING;
  221. if(strtoupper($level) == "INFO")
  222. return TeamSpeak3::LOGLEVEL_INFO;
  223. return TeamSpeak3::LOGLEVEL_DEVEL;
  224. }
  225. }
  226. /**
  227. * Converts a specified log entry string into an array containing the data.
  228. *
  229. * @param string $entry
  230. * @return array
  231. */
  232. public static function logEntry($entry)
  233. {
  234. $parts = explode("|", $entry, 5);
  235. $array = array();
  236. if(count($parts) != 5)
  237. {
  238. $array["timestamp"] = 0;
  239. $array["level"] = TeamSpeak3::LOGLEVEL_ERROR;
  240. $array["channel"] = "ParamParser";
  241. $array["server_id"] = "";
  242. $array["msg"] = TeamSpeak3_Helper_String::factory("convert error (" . trim($entry) . ")");
  243. $array["msg_plain"] = $entry;
  244. $array["malformed"] = TRUE;
  245. }
  246. else
  247. {
  248. $array["timestamp"] = strtotime(trim($parts[0]));
  249. $array["level"] = self::logLevel(trim($parts[1]));
  250. $array["channel"] = trim($parts[2]);
  251. $array["server_id"] = trim($parts[3]);
  252. $array["msg"] = TeamSpeak3_Helper_String::factory(trim($parts[4]));
  253. $array["msg_plain"] = $entry;
  254. $array["malformed"] = FALSE;
  255. }
  256. return $array;
  257. }
  258. /**
  259. * Converts a given string to a ServerQuery password hash.
  260. *
  261. * @param string $plain
  262. * @return string
  263. */
  264. public static function password($plain)
  265. {
  266. return base64_encode(sha1($plain, TRUE));
  267. }
  268. /**
  269. * Returns a client-like formatted version of the TeamSpeak 3 version string.
  270. *
  271. * @param string $version
  272. * @param string $format
  273. * @return string
  274. */
  275. public static function version($version, $format = "Y-m-d h:i:s")
  276. {
  277. if(!$version instanceof TeamSpeak3_Helper_String)
  278. {
  279. $version = new TeamSpeak3_Helper_String($version);
  280. }
  281. $buildno = $version->section("[", 1)->filterDigits()->toInt();
  282. return ($buildno <= 15001) ? $version : $version->section("[")->append("(" . date($format, $buildno) . ")");
  283. }
  284. /**
  285. * Returns a client-like short-formatted version of the TeamSpeak 3 version string.
  286. *
  287. * @param string $version
  288. * @return string
  289. */
  290. public static function versionShort($version)
  291. {
  292. if(!$version instanceof TeamSpeak3_Helper_String)
  293. {
  294. $version = new TeamSpeak3_Helper_String($version);
  295. }
  296. return $version->section(" ", 0);
  297. }
  298. /**
  299. * Tries to detect the type of an image by a given string and returns it.
  300. *
  301. * @param string $binary
  302. * @return string
  303. */
  304. public static function imageMimeType($binary)
  305. {
  306. 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))
  307. {
  308. return "application/octet-stream";
  309. }
  310. $type = array(
  311. 1 => "image/jpeg",
  312. 2 => "image/gif",
  313. 3 => "image/png",
  314. 4 => "image/x-windows-bmp",
  315. 5 => "image/tiff",
  316. 6 => "image/x-ilbm",
  317. );
  318. return $type[count($matches)-1];
  319. }
  320. }