TeamSpeak 3 PHP Framework  1.1.16
Copyright © Planet TeamSpeak. All rights reserved.
 All Classes Namespaces Files Functions Variables Pages
Convert.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * @file
5  * TeamSpeak 3 PHP Framework
6  *
7  * $Id: Convert.php 8/31/2012 11:06:09 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.16
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  */
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  {
83  return "Speex Narrowband (8 kHz)";
85  return "Speex Wideband (16 kHz)";
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  {
103  return "Template";
105  return "Regular";
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  {
121  return "Server Group";
122  if($type == TeamSpeak3::PERM_TYPE_CLIENT)
123  return "Client";
124  if($type == TeamSpeak3::PERM_TYPE_CHANNEL)
125  return "Channel";
127  return "Channel Group";
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";
145  return "Global / Information";
147  return "Global / Virtual Server Management";
149  return "Global / Administration";
151  return "Global / Settings";
152  if($pcat == TeamSpeak3::PERM_CAT_SERVER)
153  return "Virtual Server";
155  return "Virtual Server / Information";
157  return "Virtual Server / Administration";
159  return "Virtual Server / Settings";
160  if($pcat == TeamSpeak3::PERM_CAT_CHANNEL)
161  return "Channel";
163  return "Channel / Information";
165  return "Channel / Create";
167  return "Channel / Modify";
169  return "Channel / Delete";
171  return "Channel / Access";
172  if($pcat == TeamSpeak3::PERM_CAT_GROUP)
173  return "Group";
175  return "Group / Information";
177  return "Group / Create";
179  return "Group / Modify";
181  return "Group / Delete";
182  if($pcat == TeamSpeak3::PERM_CAT_CLIENT)
183  return "Client";
185  return "Client / Information";
187  return "Client / Admin";
189  return "Client / Basics";
191  return "Client / Modify";
193  return "File Transfer";
195  return "Grant";
196 
197  return "Unknown";
198  }
199 
200  /**
201  * Converts a given log level ID to a human readable name and vice versa.
202  *
203  * @param mixed $level
204  * @return string
205  */
206  public static function logLevel($level)
207  {
208  if(is_numeric($level))
209  {
210  if($level == TeamSpeak3::LOGLEVEL_CRITICAL)
211  return "CRITICAL";
212  if($level == TeamSpeak3::LOGLEVEL_ERROR)
213  return "ERROR";
214  if($level == TeamSpeak3::LOGLEVEL_DEBUG)
215  return "DEBUG";
216  if($level == TeamSpeak3::LOGLEVEL_WARNING)
217  return "WARNING";
218  if($level == TeamSpeak3::LOGLEVEL_INFO)
219  return "INFO";
220 
221  return "DEVELOP";
222  }
223  else
224  {
225  if(strtoupper($level) == "CRITICAL")
227  if(strtoupper($level) == "ERROR")
229  if(strtoupper($level) == "DEBUG")
231  if(strtoupper($level) == "WARNING")
233  if(strtoupper($level) == "INFO")
235 
237  }
238  }
239 
240  /**
241  * Converts a specified log entry string into an array containing the data.
242  *
243  * @param string $entry
244  * @return array
245  */
246  public static function logEntry($entry)
247  {
248  $parts = explode("|", $entry, 5);
249  $array = array();
250 
251  if(count($parts) != 5)
252  {
253  $array["timestamp"] = 0;
254  $array["level"] = TeamSpeak3::LOGLEVEL_ERROR;
255  $array["channel"] = "ParamParser";
256  $array["server_id"] = "";
257  $array["msg"] = TeamSpeak3_Helper_String::factory("convert error (" . trim($entry) . ")");
258  $array["msg_plain"] = $entry;
259  $array["malformed"] = TRUE;
260  }
261  else
262  {
263  $array["timestamp"] = strtotime(trim($parts[0]));
264  $array["level"] = self::logLevel(trim($parts[1]));
265  $array["channel"] = trim($parts[2]);
266  $array["server_id"] = trim($parts[3]);
267  $array["msg"] = TeamSpeak3_Helper_String::factory(trim($parts[4]));
268  $array["msg_plain"] = $entry;
269  $array["malformed"] = FALSE;
270  }
271 
272  return $array;
273  }
274 
275  /**
276  * Converts a given string to a ServerQuery password hash.
277  *
278  * @param string $plain
279  * @return string
280  */
281  public static function password($plain)
282  {
283  return base64_encode(sha1($plain, TRUE));
284  }
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 
300  $buildno = $version->section("[", 1)->filterDigits()->toInt();
301 
302  return ($buildno <= 15001) ? $version : $version->section("[")->append("(" . date($format, $buildno) . ")");
303  }
304 
305  /**
306  * Tries to detect the type of an image by a given string and returns it.
307  *
308  * @param string $binary
309  * @return string
310  */
311  public static function imageMimeType($binary)
312  {
313  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))
314  {
315  return "application/octet-stream";
316  }
317 
318  $type = array(
319  1 => "image/jpeg",
320  2 => "image/gif",
321  3 => "image/png",
322  4 => "image/x-windows-bmp",
323  5 => "image/tiff",
324  6 => "image/x-ilbm",
325  );
326 
327  return $type[count($matches)-1];
328  }
329 }