PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
Utils.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
25 namespace pocketmine\utils;
26 
30 class Utils{
31  public static $online = true;
32  public static $ip = false;
33 
41  public static function getCallableIdentifier(callable $variable){
42  if(is_array($variable)){
43  return sha1(strtolower(spl_object_hash($variable[0])) . "::" . strtolower($variable[1]));
44  }else{
45  return sha1(strtolower($variable));
46  }
47  }
48 
60  public static function getUniqueID($raw = false, $extra = ""){
61  $machine = php_uname("a");
62  $machine .= file_exists("/proc/cpuinfo") ? implode(preg_grep("/model name/", file("/proc/cpuinfo"))) : "";
63  $machine .= sys_get_temp_dir();
64  $machine .= $extra;
65  $os = Utils::getOS();
66  if($os === "win"){
67  @exec("ipconfig /ALL", $mac);
68  $mac = implode("\n", $mac);
69  if(preg_match_all("#Physical Address[. ]{1,}: ([0-9A-F\\-]{17})#", $mac, $matches)){
70  foreach($matches[1] as $i => $v){
71  if($v == "00-00-00-00-00-00"){
72  unset($matches[1][$i]);
73  }
74  }
75  $machine .= implode(" ", $matches[1]); //Mac Addresses
76  }
77  }elseif($os === "linux"){
78  @exec("ifconfig", $mac);
79  $mac = implode("\n", $mac);
80  if(preg_match_all("#HWaddr[ \t]{1,}([0-9a-f:]{17})#", $mac, $matches)){
81  foreach($matches[1] as $i => $v){
82  if($v == "00:00:00:00:00:00"){
83  unset($matches[1][$i]);
84  }
85  }
86  $machine .= implode(" ", $matches[1]); //Mac Addresses
87  }
88  }
89  $data = $machine . PHP_MAXPATHLEN;
90  $data .= PHP_INT_MAX;
91  $data .= PHP_INT_SIZE;
92  $data .= get_current_user();
93  foreach(get_loaded_extensions() as $ext){
94  $data .= $ext . ":" . phpversion($ext);
95  }
96 
97  return hash("md5", $machine, $raw) . hash("sha512", $data, $raw);
98  }
99 
108  public static function getIP($force = false){
109  if(Utils::$online === false){
110  return false;
111  }elseif(Utils::$ip !== false and $force !== true){
112  return Utils::$ip;
113  }
114  $ip = trim(strip_tags(Utils::getURL("http://checkip.dyndns.org/")));
115  if(preg_match('#Current IP Address\: ([0-9a-fA-F\:\.]*)#', $ip, $matches) > 0){
116  Utils::$ip = $matches[1];
117  }else{
118  $ip = Utils::getURL("http://www.checkip.org/");
119  if(preg_match('#">([0-9a-fA-F\:\.]*)</span>#', $ip, $matches) > 0){
120  Utils::$ip = $matches[1];
121  }else{
122  $ip = Utils::getURL("http://checkmyip.org/");
123  if(preg_match('#Your IP address is ([0-9a-fA-F\:\.]*)#', $ip, $matches) > 0){
124  Utils::$ip = $matches[1];
125  }else{
126  $ip = trim(Utils::getURL("http://ifconfig.me/ip"));
127  if($ip != ""){
128  Utils::$ip = $ip;
129  }else{
130  return false;
131  }
132  }
133  }
134  }
135 
136  return Utils::$ip;
137 
138  }
139 
152  public static function getOS(){
153  $uname = php_uname("s");
154  if(stripos($uname, "Darwin") !== false){
155  if(strpos(php_uname("m"), "iP") === 0){
156  return "ios";
157  }else{
158  return "mac";
159  }
160  }elseif(stripos($uname, "Win") !== false or $uname === "Msys"){
161  return "win";
162  }elseif(stripos($uname, "Linux") !== false){
163  if(@file_exists("/system/build.prop")){
164  return "android";
165  }else{
166  return "linux";
167  }
168  }elseif(stripos($uname, "BSD") !== false or $uname === "DragonFly"){
169  return "bsd";
170  }else{
171  return "other";
172  }
173  }
174 
182  public static function hexdump($bin){
183  $output = "";
184  $bin = str_split($bin, 16);
185  foreach($bin as $counter => $line){
186  $hex = chunk_split(chunk_split(str_pad(bin2hex($line), 32, " ", STR_PAD_RIGHT), 2, " "), 24, " ");
187  $ascii = preg_replace('#([^\x20-\x7E])#', ".", $line);
188  $output .= str_pad(dechex($counter << 4), 4, "0", STR_PAD_LEFT) . " " . $hex . " " . $ascii . PHP_EOL;
189  }
190 
191  return $output;
192  }
193 
194 
202  public static function printable($str){
203  if(!is_string($str)){
204  return gettype($str);
205  }
206 
207  return preg_replace('#([^\x20-\x7E])#', '.', $str);
208  }
209 
223  public static function getRandomBytes($length = 16, $secure = true, $raw = true, $startEntropy = "", &$rounds = 0, &$drop = 0){
224  static $lastRandom = "";
225  $output = "";
226  $length = abs((int) $length);
227  $secureValue = "";
228  $rounds = 0;
229  $drop = 0;
230  while(!isset($output{$length - 1})){
231  //some entropy, but works ^^
232  $weakEntropy = [
233  is_array($startEntropy) ? implode($startEntropy) : $startEntropy,
234  __DIR__,
235  PHP_OS,
236  microtime(),
237  (string) lcg_value(),
238  (string) PHP_MAXPATHLEN,
239  PHP_SAPI,
240  (string) PHP_INT_MAX . "." . PHP_INT_SIZE,
241  serialize($_SERVER),
242  get_current_user(),
243  (string) memory_get_usage() . "." . memory_get_peak_usage(),
244  php_uname(),
245  phpversion(),
246  extension_loaded("gmp") ? gmp_strval(gmp_random(4)) : microtime(),
247  zend_version(),
248  (string) getmypid(),
249  (string) getmyuid(),
250  (string) mt_rand(),
251  (string) getmyinode(),
252  (string) getmygid(),
253  (string) rand(),
254  function_exists("zend_thread_id") ? ((string) zend_thread_id()) : microtime(),
255  function_exists("getrusage") ? implode(getrusage()) : microtime(),
256  function_exists("sys_getloadavg") ? implode(sys_getloadavg()) : microtime(),
257  serialize(get_loaded_extensions()),
258  sys_get_temp_dir(),
259  (string) disk_free_space("."),
260  (string) disk_total_space("."),
261  uniqid(microtime(), true),
262  file_exists("/proc/cpuinfo") ? file_get_contents("/proc/cpuinfo") : microtime(),
263  ];
264 
265  shuffle($weakEntropy);
266  $value = hash("sha512", implode($weakEntropy), true);
267  $lastRandom .= $value;
268  foreach($weakEntropy as $k => $c){ //mixing entropy values with XOR and hash randomness extractor
269  $value ^= hash("sha256", $c . microtime() . $k, true) . hash("sha256", mt_rand() . microtime() . $k . $c, true);
270  $value ^= hash("sha512", ((string) lcg_value()) . $c . microtime() . $k, true);
271  }
272  unset($weakEntropy);
273 
274  if($secure === true){
275  $strongEntropyValues = [
276  is_array($startEntropy) ? hash("sha512", $startEntropy[($rounds + $drop) % count($startEntropy)], true) : hash("sha512", $startEntropy, true), //Get a random index of the startEntropy, or just read it
277  file_exists("/dev/urandom") ? fread(fopen("/dev/urandom", "rb"), 64) : str_repeat("\x00", 64),
278  function_exists("openssl_random_pseudo_bytes") ? openssl_random_pseudo_bytes(64) : str_repeat("\x00", 64),
279  function_exists("mcrypt_create_iv") ? mcrypt_create_iv(64, MCRYPT_DEV_URANDOM) : str_repeat("\x00", 64),
280  $value,
281  ];
282  $strongEntropy = array_pop($strongEntropyValues);
283  foreach($strongEntropyValues as $value){
284  $strongEntropy = $strongEntropy ^ $value;
285  }
286  $value = "";
287  //Von Neumann randomness extractor, increases entropy
288  $bitcnt = 0;
289  for($j = 0; $j < 64; ++$j){
290  $a = ord($strongEntropy{$j});
291  for($i = 0; $i < 8; $i += 2){
292  $b = ($a & (1 << $i)) > 0 ? 1 : 0;
293  if($b != (($a & (1 << ($i + 1))) > 0 ? 1 : 0)){
294  $secureValue |= $b << $bitcnt;
295  if($bitcnt == 7){
296  $value .= chr($secureValue);
297  $secureValue = 0;
298  $bitcnt = 0;
299  }else{
300  ++$bitcnt;
301  }
302  ++$drop;
303  }else{
304  $drop += 2;
305  }
306  }
307  }
308  }
309  $output .= substr($value, 0, min($length - strlen($output), $length));
310  unset($value);
311  ++$rounds;
312  }
313  $lastRandom = hash("sha512", $lastRandom, true);
314 
315  return $raw === false ? bin2hex($output) : $output;
316  }
317 
318  /*
319  public static function angle3D($pos1, $pos2){
320  $X = $pos1["x"] - $pos2["x"];
321  $Z = $pos1["z"] - $pos2["z"];
322  $dXZ = sqrt(pow($X, 2) + pow($Z, 2));
323  $Y = $pos1["y"] - $pos2["y"];
324  $hAngle = rad2deg(atan2($Z, $X) - M_PI_2);
325  $vAngle = rad2deg(-atan2($Y, $dXZ));
326 
327  return array("yaw" => $hAngle, "pitch" => $vAngle);
328  }*/
329 
338  public static function getURL($page, $timeout = 10){
339  if(Utils::$online === false){
340  return false;
341  }
342 
343  $ch = curl_init($page);
344  curl_setopt($ch, CURLOPT_HTTPHEADER, ["User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0 PocketMine-MP"]);
345  curl_setopt($ch, CURLOPT_AUTOREFERER, true);
346  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
347  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
348  curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
349  curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
350  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
351  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
352  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
353  $ret = curl_exec($ch);
354  curl_close($ch);
355 
356  return $ret;
357  }
358 
368  public static function postURL($page, $args, $timeout = 10){
369  if(Utils::$online === false){
370  return false;
371  }
372 
373  $ch = curl_init($page);
374  curl_setopt($ch, CURLOPT_POST, 1);
375  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
376  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
377  curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
378  curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
379  curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
380  curl_setopt($ch, CURLOPT_AUTOREFERER, true);
381  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
382  curl_setopt($ch, CURLOPT_HTTPHEADER, ["User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0 PocketMine-MP"]);
383  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
384  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
385  $ret = curl_exec($ch);
386  curl_close($ch);
387 
388  return $ret;
389  }
390 
391 }
static postURL($page, $args, $timeout=10)
Definition: Utils.php:368
static hexdump($bin)
Definition: Utils.php:182
static getIP($force=false)
Definition: Utils.php:108
static printable($str)
Definition: Utils.php:202
static getCallableIdentifier(callable $variable)
Definition: Utils.php:41
static getRandomBytes($length=16, $secure=true, $raw=true, $startEntropy="", &$rounds=0, &$drop=0)
Definition: Utils.php:223
static getUniqueID($raw=false, $extra="")
Definition: Utils.php:60
static getURL($page, $timeout=10)
Definition: Utils.php:338