GenisysPro  for Minecraft PE/Windows 10 v1.1.x
Feature-rich server software for Minecraft PE and Windows 10 Edition
Utils Class Reference

Static Public Member Functions

static getCallableIdentifier (callable $variable)
 
static getMachineUniqueId ($extra="")
 
static getIP ($force=false)
 
static getOS ($recalculate=false)
 
static getRealMemoryUsage ()
 
static getMemoryUsage ($advanced=false)
 
static getThreadCount ()
 
static getCoreCount ($recalculate=false)
 
static hexdump ($bin)
 
static printable ($str)
 
static getURL ($page, $timeout=10, array $extraHeaders=[])
 
static postURL ($page, $args, $timeout=10, array $extraHeaders=[])
 
static javaStringHash ($string)
 

Static Public Attributes

static $online = true
 
static $ip = false
 
static $os
 

Detailed Description

Big collection of functions

Member Function Documentation

◆ getCallableIdentifier()

static getCallableIdentifier ( callable  $variable)
static

Generates an unique identifier to a callable

Parameters
callable$variable
Returns
string
46  {
47  if(is_array($variable)){
48  return sha1(strtolower(spl_object_hash($variable[0])) . "::" . strtolower($variable[1]));
49  }else{
50  return sha1(strtolower($variable));
51  }
52  }

◆ getCoreCount()

static getCoreCount (   $recalculate = false)
static
Parameters
bool$recalculate
Returns
int
283  {
284  static $processors = 0;
285 
286  if($processors > 0 and !$recalculate){
287  return $processors;
288  }else{
289  $processors = 0;
290  }
291 
292  switch(Utils::getOS()){
293  case "linux":
294  case "android":
295  if(file_exists("/proc/cpuinfo")){
296  foreach(file("/proc/cpuinfo") as $l){
297  if(preg_match('/^processor[ \t]*:[ \t]*[0-9]+$/m', $l) > 0){
298  ++$processors;
299  }
300  }
301  }else{
302  if(preg_match("/^([0-9]+)\\-([0-9]+)$/", trim(@file_get_contents("/sys/devices/system/cpu/present")), $matches) > 0){
303  $processors = (int) ($matches[2] - $matches[1]);
304  }
305  }
306  break;
307  case "bsd":
308  case "mac":
309  $processors = (int) `sysctl -n hw.ncpu`;
310  $processors = (int) `sysctl -n hw.ncpu`;
311  break;
312  case "win":
313  $processors = (int) getenv("NUMBER_OF_PROCESSORS");
314  break;
315  }
316  return $processors;
317  }

◆ getIP()

static getIP (   $force = false)
static

Gets the External IP using an external service, it is cached

Parameters
bool$forcedefault false, force IP check even when cached
Returns
string
130  {
131  if(Utils::$online === false){
132  return false;
133  }elseif(Utils::$ip !== false and $force !== true){
134  return Utils::$ip;
135  }
136  $ip = trim(strip_tags(Utils::getURL("https://api.ipify.org")));
137  if($ip){
138  Utils::$ip = $ip;
139  }else{
140  $ip = Utils::getURL("http://www.checkip.org/");
141  if(preg_match('#">([0-9a-fA-F\:\.]*)</span>#', $ip, $matches) > 0){
142  Utils::$ip = $matches[1];
143  }else{
144  $ip = Utils::getURL("http://checkmyip.org/");
145  if(preg_match('#Your IP address is ([0-9a-fA-F\:\.]*)#', $ip, $matches) > 0){
146  Utils::$ip = $matches[1];
147  }else{
148  $ip = trim(Utils::getURL("http://ifconfig.me/ip"));
149  if($ip != ""){
150  Utils::$ip = $ip;
151  }else{
152  return false;
153  }
154  }
155  }
156  }
157 
158  return Utils::$ip;
159 
160  }

◆ getMachineUniqueId()

static getMachineUniqueId (   $extra = "")
static

Gets this machine / server instance unique ID Returns a hash, the first 32 characters (or 16 if raw) will be an identifier that won't change frequently. The rest of the hash will change depending on other factors.

Parameters
string$extraoptional, additional data to identify the machine
Returns
UUID
64  {
65  if(self::$serverUniqueId !== null and $extra === ""){
66  return self::$serverUniqueId;
67  }
68 
69  $machine = php_uname("a");
70  $machine .= file_exists("/proc/cpuinfo") ? implode(preg_grep("/(model name|Processor|Serial)/", file("/proc/cpuinfo"))) : "";
71  $machine .= sys_get_temp_dir();
72  $machine .= $extra;
73  $os = Utils::getOS();
74  if($os === "win"){
75  @exec("ipconfig /ALL", $mac);
76  $mac = implode("\n", $mac);
77  if(preg_match_all("#Physical Address[. ]{1,}: ([0-9A-F\\-]{17})#", $mac, $matches)){
78  foreach($matches[1] as $i => $v){
79  if($v == "00-00-00-00-00-00"){
80  unset($matches[1][$i]);
81  }
82  }
83  $machine .= implode(" ", $matches[1]); //Mac Addresses
84  }
85  }elseif($os === "linux"){
86  if(file_exists("/etc/machine-id")){
87  $machine .= file_get_contents("/etc/machine-id");
88  }else{
89  @exec("ifconfig", $mac);
90  $mac = implode("\n", $mac);
91  if(preg_match_all("#HWaddr[ \t]{1,}([0-9a-f:]{17})#", $mac, $matches)){
92  foreach($matches[1] as $i => $v){
93  if($v == "00:00:00:00:00:00"){
94  unset($matches[1][$i]);
95  }
96  }
97  $machine .= implode(" ", $matches[1]); //Mac Addresses
98  }
99  }
100  }elseif($os === "android"){
101  $machine .= @file_get_contents("/system/build.prop");
102  }elseif($os === "mac"){
103  $machine .= `system_profiler SPHardwareDataType | grep UUID`;
104  }
105  $data = $machine . PHP_MAXPATHLEN;
106  $data .= PHP_INT_MAX;
107  $data .= PHP_INT_SIZE;
108  $data .= get_current_user();
109  foreach(get_loaded_extensions() as $ext){
110  $data .= $ext . ":" . phpversion($ext);
111  }
112 
113  $uuid = UUID::fromData($machine, $data);
114 
115  if($extra === ""){
116  self::$serverUniqueId = $uuid;
117  }
118 
119  return $uuid;
120  }

◆ getMemoryUsage()

static getMemoryUsage (   $advanced = false)
static
Parameters
bool$advanced
Returns
array|int|null
232  {
233  $reserved = memory_get_usage();
234  $VmSize = null;
235  $VmRSS = null;
236  if(Utils::getOS() === "linux" or Utils::getOS() === "android"){
237  $status = file_get_contents("/proc/self/status");
238  if(preg_match("/VmRSS:[ \t]+([0-9]+) kB/", $status, $matches) > 0){
239  $VmRSS = $matches[1] * 1024;
240  }
241 
242  if(preg_match("/VmSize:[ \t]+([0-9]+) kB/", $status, $matches) > 0){
243  $VmSize = $matches[1] * 1024;
244  }
245  }
246 
247  //TODO: more OS
248 
249  if($VmRSS === null){
250  $VmRSS = memory_get_usage();
251  }
252 
253  if(!$advanced){
254  return $VmRSS;
255  }
256 
257  if($VmSize === null){
258  $VmSize = memory_get_usage(true);
259  }
260 
261  return [$reserved, $VmRSS, $VmSize];
262  }

◆ getOS()

static getOS (   $recalculate = false)
static

Returns the current Operating System Windows => win MacOS => mac iOS => ios Android => android Linux => Linux BSD => bsd Other => other

Parameters
bool$recalculate
Returns
string
176  {
177  if(self::$os === null or $recalculate){
178  $uname = php_uname("s");
179  if(stripos($uname, "Darwin") !== false){
180  if(strpos(php_uname("m"), "iP") === 0){
181  self::$os = "ios";
182  }else{
183  self::$os = "mac";
184  }
185  }elseif(stripos($uname, "Win") !== false or $uname === "Msys"){
186  self::$os = "win";
187  }elseif(stripos($uname, "Linux") !== false){
188  if(@file_exists("/system/build.prop")){
189  self::$os = "android";
190  }else{
191  self::$os = "linux";
192  }
193  }elseif(stripos($uname, "BSD") !== false or $uname === "DragonFly"){
194  self::$os = "bsd";
195  }else{
196  self::$os = "other";
197  }
198  }
199 
200  return self::$os;
201  }

◆ getRealMemoryUsage()

static getRealMemoryUsage ( )
static
Returns
array
207  {
208  $stack = 0;
209  $heap = 0;
210 
211  if(Utils::getOS() === "linux" or Utils::getOS() === "android"){
212  $mappings = file("/proc/self/maps");
213  foreach($mappings as $line){
214  if(preg_match("#([a-z0-9]+)\\-([a-z0-9]+) [rwxp\\-]{4} [a-z0-9]+ [^\\[]*\\[([a-zA-z0-9]+)\\]#", trim($line), $matches) > 0){
215  if(strpos($matches[3], "heap") === 0){
216  $heap += hexdec($matches[2]) - hexdec($matches[1]);
217  }elseif(strpos($matches[3], "stack") === 0){
218  $stack += hexdec($matches[2]) - hexdec($matches[1]);
219  }
220  }
221  }
222  }
223 
224  return [$heap, $stack];
225  }

◆ getThreadCount()

static getThreadCount ( )
static
Returns
int
267  {
268  if(Utils::getOS() === "linux" or Utils::getOS() === "android"){
269  if(preg_match("/Threads:[ \t]+([0-9]+)/", file_get_contents("/proc/self/status"), $matches) > 0){
270  return (int) $matches[1];
271  }
272  }
273  //TODO: more OS
274 
275  return count(ThreadManager::getInstance()->getAll()) + 3; //RakLib + MainLogger + Main Thread
276  }

◆ getURL()

static getURL (   $page,
  $timeout = 10,
array  $extraHeaders = [] 
)
static

GETs an URL using cURL

Parameters
$page
int$timeoutdefault 10
array$extraHeaders
Returns
bool|mixed
375  {
376  if(Utils::$online === false){
377  return false;
378  }
379 
380  $ch = curl_init($page);
381  curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge(["User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0 PocketMine-MP"], $extraHeaders));
382  curl_setopt($ch, CURLOPT_AUTOREFERER, true);
383  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
384  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
385  curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
386  curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
387  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
388  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
389  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
390  curl_setopt($ch, CURLOPT_TIMEOUT, (int) $timeout);
391  $ret = curl_exec($ch);
392  curl_close($ch);
393 
394  return $ret;
395  }

◆ hexdump()

static hexdump (   $bin)
static

Returns a prettified hexdump

Parameters
string$bin
Returns
string
326  {
327  $output = "";
328  $bin = str_split($bin, 16);
329  foreach($bin as $counter => $line){
330  $hex = chunk_split(chunk_split(str_pad(bin2hex($line), 32, " ", STR_PAD_RIGHT), 2, " "), 24, " ");
331  $ascii = preg_replace('#([^\x20-\x7E])#', ".", $line);
332  $output .= str_pad(dechex($counter << 4), 4, "0", STR_PAD_LEFT) . " " . $hex . " " . $ascii . PHP_EOL;
333  }
334 
335  return $output;
336  }

◆ javaStringHash()

static javaStringHash (   $string)
static
Parameters
$string
Returns
int
436  {
437  $hash = 0;
438  for($i = 0; $i < strlen($string); $i++){
439  $ord = ord($string{$i});
440  if($ord & 0x80){
441  $ord -= 0x100;
442  }
443  $hash = 31 * $hash + $ord;
444  while($hash > 0x7FFFFFFF){
445  $hash -= 0x100000000;
446  }
447  while($hash < -0x80000000){
448  $hash += 0x100000000;
449  }
450  $hash &= 0xFFFFFFFF;
451  }
452  return $hash;
453  }

◆ postURL()

static postURL (   $page,
  $args,
  $timeout = 10,
array  $extraHeaders = [] 
)
static

POSTs data to an URL

Parameters
$page
array | string$args
int$timeout
array$extraHeaders
Returns
bool|mixed
407  {
408  if(Utils::$online === false){
409  return false;
410  }
411 
412  $ch = curl_init($page);
413  curl_setopt($ch, CURLOPT_POST, 1);
414  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
415  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
416  curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
417  curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
418  curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
419  curl_setopt($ch, CURLOPT_AUTOREFERER, true);
420  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
421  curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge(["User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0 PocketMine-MP"], $extraHeaders));
422  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
423  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
424  curl_setopt($ch, CURLOPT_TIMEOUT, (int) $timeout);
425  $ret = curl_exec($ch);
426  curl_close($ch);
427 
428  return $ret;
429  }

◆ printable()

static printable (   $str)
static

Returns a string that can be printed, replaces non-printable characters

Parameters
$str
Returns
string
346  {
347  if(!is_string($str)){
348  return gettype($str);
349  }
350 
351  return preg_replace('#([^\x20-\x7E])#', '.', $str);
352  }

Field Documentation

◆ $ip

$ip = false
static

◆ $online

$online = true
static

◆ $os

$os
static

The documentation for this class was generated from the following file: