Convert.php 11 KB

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