TeamSpeak 3 PHP Framework  1.1.23
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 10/11/2013 11:35:21 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.23
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";
85  return "Speex Wideband";
87  return "Speex Ultra-Wideband";
88  if($codec == TeamSpeak3::CODEC_CELT_MONO)
89  return "CELT Mono";
90  if($codec == TeamSpeak3::CODEC_OPUS_VOICE)
91  return "Opus Voice";
92  if($codec == TeamSpeak3::CODEC_OPUS_MUSIC)
93  return "Opus Music";
94 
95  return "Unknown";
96  }
97 
98  /**
99  * Converts a given group type ID to a human readable name.
100  *
101  * @param integer $type
102  * @return string
103  */
104  public static function groupType($type)
105  {
107  return "Template";
109  return "Regular";
111  return "ServerQuery";
112 
113  return "Unknown";
114  }
115 
116  /**
117  * Converts a given permission type ID to a human readable name.
118  *
119  * @param integer $type
120  * @return string
121  */
122  public static function permissionType($type)
123  {
125  return "Server Group";
126  if($type == TeamSpeak3::PERM_TYPE_CLIENT)
127  return "Client";
128  if($type == TeamSpeak3::PERM_TYPE_CHANNEL)
129  return "Channel";
131  return "Channel Group";
133  return "Channel Client";
134 
135  return "Unknown";
136  }
137 
138  /**
139  * Converts a given permission category value to a human readable name.
140  *
141  * @param integer $pcat
142  * @return string
143  */
144  public static function permissionCategory($pcat)
145  {
146  if($pcat == TeamSpeak3::PERM_CAT_GLOBAL)
147  return "Global";
149  return "Global / Information";
151  return "Global / Virtual Server Management";
153  return "Global / Administration";
155  return "Global / Settings";
156  if($pcat == TeamSpeak3::PERM_CAT_SERVER)
157  return "Virtual Server";
159  return "Virtual Server / Information";
161  return "Virtual Server / Administration";
163  return "Virtual Server / Settings";
164  if($pcat == TeamSpeak3::PERM_CAT_CHANNEL)
165  return "Channel";
167  return "Channel / Information";
169  return "Channel / Create";
171  return "Channel / Modify";
173  return "Channel / Delete";
175  return "Channel / Access";
176  if($pcat == TeamSpeak3::PERM_CAT_GROUP)
177  return "Group";
179  return "Group / Information";
181  return "Group / Create";
183  return "Group / Modify";
185  return "Group / Delete";
186  if($pcat == TeamSpeak3::PERM_CAT_CLIENT)
187  return "Client";
189  return "Client / Information";
191  return "Client / Admin";
193  return "Client / Basics";
195  return "Client / Modify";
197  return "File Transfer";
199  return "Grant";
200 
201  return "Unknown";
202  }
203 
204  /**
205  * Converts a given log level ID to a human readable name and vice versa.
206  *
207  * @param mixed $level
208  * @return string
209  */
210  public static function logLevel($level)
211  {
212  if(is_numeric($level))
213  {
214  if($level == TeamSpeak3::LOGLEVEL_CRITICAL)
215  return "CRITICAL";
216  if($level == TeamSpeak3::LOGLEVEL_ERROR)
217  return "ERROR";
218  if($level == TeamSpeak3::LOGLEVEL_DEBUG)
219  return "DEBUG";
220  if($level == TeamSpeak3::LOGLEVEL_WARNING)
221  return "WARNING";
222  if($level == TeamSpeak3::LOGLEVEL_INFO)
223  return "INFO";
224 
225  return "DEVELOP";
226  }
227  else
228  {
229  if(strtoupper($level) == "CRITICAL")
231  if(strtoupper($level) == "ERROR")
233  if(strtoupper($level) == "DEBUG")
235  if(strtoupper($level) == "WARNING")
237  if(strtoupper($level) == "INFO")
239 
241  }
242  }
243 
244  /**
245  * Converts a specified log entry string into an array containing the data.
246  *
247  * @param string $entry
248  * @return array
249  */
250  public static function logEntry($entry)
251  {
252  $parts = explode("|", $entry, 5);
253  $array = array();
254 
255  if(count($parts) != 5)
256  {
257  $array["timestamp"] = 0;
258  $array["level"] = TeamSpeak3::LOGLEVEL_ERROR;
259  $array["channel"] = "ParamParser";
260  $array["server_id"] = "";
261  $array["msg"] = TeamSpeak3_Helper_String::factory("convert error (" . trim($entry) . ")");
262  $array["msg_plain"] = $entry;
263  $array["malformed"] = TRUE;
264  }
265  else
266  {
267  $array["timestamp"] = strtotime(trim($parts[0]));
268  $array["level"] = self::logLevel(trim($parts[1]));
269  $array["channel"] = trim($parts[2]);
270  $array["server_id"] = trim($parts[3]);
271  $array["msg"] = TeamSpeak3_Helper_String::factory(trim($parts[4]));
272  $array["msg_plain"] = $entry;
273  $array["malformed"] = FALSE;
274  }
275 
276  return $array;
277  }
278 
279  /**
280  * Converts a given string to a ServerQuery password hash.
281  *
282  * @param string $plain
283  * @return string
284  */
285  public static function password($plain)
286  {
287  return base64_encode(sha1($plain, TRUE));
288  }
289 
290  /**
291  * Returns a client-like formatted version of the TeamSpeak 3 version string.
292  *
293  * @param string $version
294  * @param string $format
295  * @return string
296  */
297  public static function version($version, $format = "Y-m-d h:i:s")
298  {
299  if(!$version instanceof TeamSpeak3_Helper_String)
300  {
301  $version = new TeamSpeak3_Helper_String($version);
302  }
303 
304  $buildno = $version->section("[", 1)->filterDigits()->toInt();
305 
306  return ($buildno <= 15001) ? $version : $version->section("[")->append("(" . date($format, $buildno) . ")");
307  }
308 
309  /**
310  * Returns a client-like short-formatted version of the TeamSpeak 3 version string.
311  *
312  * @param string $version
313  * @return string
314  */
315  public static function versionShort($version)
316  {
317  if(!$version instanceof TeamSpeak3_Helper_String)
318  {
319  $version = new TeamSpeak3_Helper_String($version);
320  }
321 
322  return $version->section(" ", 0);
323  }
324 
325  /**
326  * Tries to detect the type of an image by a given string and returns it.
327  *
328  * @param string $binary
329  * @return string
330  */
331  public static function imageMimeType($binary)
332  {
333  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))
334  {
335  return "application/octet-stream";
336  }
337 
338  $type = array(
339  1 => "image/jpeg",
340  2 => "image/gif",
341  3 => "image/png",
342  4 => "image/x-windows-bmp",
343  5 => "image/tiff",
344  6 => "image/x-ilbm",
345  );
346 
347  return $type[count($matches)-1];
348  }
349 }