getFileName()) . "#L" . $func->getStartLine(); } /** * Returns a readable identifier for the class of the given object. Sanitizes class names for anonymous classes. * * @param object $obj * * @return string * @throws \ReflectionException */ public static function getNiceClassName(object $obj) : string{ $reflect = new \ReflectionClass($obj); if($reflect->isAnonymous()){ return "anonymous@" . self::cleanPath($reflect->getFileName()) . "#L" . $reflect->getStartLine(); } return $reflect->getName(); } /** * 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. * * @param string $extra optional, additional data to identify the machine * * @return UUID */ public static function getMachineUniqueId(string $extra = "") : UUID{ if(self::$serverUniqueId !== null and $extra === ""){ return self::$serverUniqueId; } $machine = php_uname("a"); $machine .= file_exists("/proc/cpuinfo") ? implode(preg_grep("/(model name|Processor|Serial)/", file("/proc/cpuinfo"))) : ""; $machine .= sys_get_temp_dir(); $machine .= $extra; $os = Utils::getOS(); if($os === "win"){ @exec("ipconfig /ALL", $mac); $mac = implode("\n", $mac); if(preg_match_all("#Physical Address[. ]{1,}: ([0-9A-F\\-]{17})#", $mac, $matches)){ foreach($matches[1] as $i => $v){ if($v == "00-00-00-00-00-00"){ unset($matches[1][$i]); } } $machine .= implode(" ", $matches[1]); //Mac Addresses } }elseif($os === "linux"){ if(file_exists("/etc/machine-id")){ $machine .= file_get_contents("/etc/machine-id"); }else{ @exec("ifconfig 2>/dev/null", $mac); $mac = implode("\n", $mac); if(preg_match_all("#HWaddr[ \t]{1,}([0-9a-f:]{17})#", $mac, $matches)){ foreach($matches[1] as $i => $v){ if($v == "00:00:00:00:00:00"){ unset($matches[1][$i]); } } $machine .= implode(" ", $matches[1]); //Mac Addresses } } }elseif($os === "android"){ $machine .= @file_get_contents("/system/build.prop"); }elseif($os === "mac"){ $machine .= `system_profiler SPHardwareDataType | grep UUID`; } $data = $machine . PHP_MAXPATHLEN; $data .= PHP_INT_MAX; $data .= PHP_INT_SIZE; $data .= get_current_user(); foreach(get_loaded_extensions() as $ext){ $data .= $ext . ":" . phpversion($ext); } $uuid = UUID::fromData($machine, $data); if($extra === ""){ self::$serverUniqueId = $uuid; } return $uuid; } /** * @deprecated * @see Internet::getIP() * * @param bool $force default false, force IP check even when cached * * @return string|bool */ public static function getIP(bool $force = false){ return Internet::getIP($force); } /** * Returns the current Operating System * Windows => win * MacOS => mac * iOS => ios * Android => android * Linux => Linux * BSD => bsd * Other => other * * @param bool $recalculate * * @return string */ public static function getOS(bool $recalculate = false) : string{ if(self::$os === null or $recalculate){ $uname = php_uname("s"); if(stripos($uname, "Darwin") !== false){ if(strpos(php_uname("m"), "iP") === 0){ self::$os = "ios"; }else{ self::$os = "mac"; } }elseif(stripos($uname, "Win") !== false or $uname === "Msys"){ self::$os = "win"; }elseif(stripos($uname, "Linux") !== false){ if(@file_exists("/system/build.prop")){ self::$os = "android"; }else{ self::$os = "linux"; } }elseif(stripos($uname, "BSD") !== false or $uname === "DragonFly"){ self::$os = "bsd"; }else{ self::$os = "other"; } } return self::$os; } /** * @return int[] */ public static function getRealMemoryUsage() : array{ $stack = 0; $heap = 0; if(Utils::getOS() === "linux" or Utils::getOS() === "android"){ $mappings = file("/proc/self/maps"); foreach($mappings as $line){ if(preg_match("#([a-z0-9]+)\\-([a-z0-9]+) [rwxp\\-]{4} [a-z0-9]+ [^\\[]*\\[([a-zA-z0-9]+)\\]#", trim($line), $matches) > 0){ if(strpos($matches[3], "heap") === 0){ $heap += hexdec($matches[2]) - hexdec($matches[1]); }elseif(strpos($matches[3], "stack") === 0){ $stack += hexdec($matches[2]) - hexdec($matches[1]); } } } } return [$heap, $stack]; } /** * @param bool $advanced * * @return int[]|int */ public static function getMemoryUsage(bool $advanced = false){ $reserved = memory_get_usage(); $VmSize = null; $VmRSS = null; if(Utils::getOS() === "linux" or Utils::getOS() === "android"){ $status = file_get_contents("/proc/self/status"); if(preg_match("/VmRSS:[ \t]+([0-9]+) kB/", $status, $matches) > 0){ $VmRSS = $matches[1] * 1024; } if(preg_match("/VmSize:[ \t]+([0-9]+) kB/", $status, $matches) > 0){ $VmSize = $matches[1] * 1024; } } //TODO: more OS if($VmRSS === null){ $VmRSS = memory_get_usage(); } if(!$advanced){ return $VmRSS; } if($VmSize === null){ $VmSize = memory_get_usage(true); } return [$reserved, $VmRSS, $VmSize]; } public static function getThreadCount() : int{ if(Utils::getOS() === "linux" or Utils::getOS() === "android"){ if(preg_match("/Threads:[ \t]+([0-9]+)/", file_get_contents("/proc/self/status"), $matches) > 0){ return (int) $matches[1]; } } //TODO: more OS return count(ThreadManager::getInstance()->getAll()) + 3; //RakLib + MainLogger + Main Thread } /** * @param bool $recalculate * * @return int */ public static function getCoreCount(bool $recalculate = false) : int{ static $processors = 0; if($processors > 0 and !$recalculate){ return $processors; }else{ $processors = 0; } switch(Utils::getOS()){ case "linux": case "android": if(file_exists("/proc/cpuinfo")){ foreach(file("/proc/cpuinfo") as $l){ if(preg_match('/^processor[ \t]*:[ \t]*[0-9]+$/m', $l) > 0){ ++$processors; } } }else{ if(preg_match("/^([0-9]+)\\-([0-9]+)$/", trim(@file_get_contents("/sys/devices/system/cpu/present")), $matches) > 0){ $processors = (int) ($matches[2] - $matches[1]); } } break; case "bsd": case "mac": $processors = (int) `sysctl -n hw.ncpu`; break; case "win": $processors = (int) getenv("NUMBER_OF_PROCESSORS"); break; } return $processors; } /** * Returns a prettified hexdump * * @param string $bin * * @return string */ public static function hexdump(string $bin) : string{ $output = ""; $bin = str_split($bin, 16); foreach($bin as $counter => $line){ $hex = chunk_split(chunk_split(str_pad(bin2hex($line), 32, " ", STR_PAD_RIGHT), 2, " "), 24, " "); $ascii = preg_replace('#([^\x20-\x7E])#', ".", $line); $output .= str_pad(dechex($counter << 4), 4, "0", STR_PAD_LEFT) . " " . $hex . " " . $ascii . PHP_EOL; } return $output; } /** * Returns a string that can be printed, replaces non-printable characters * * @param mixed $str * * @return string */ public static function printable($str) : string{ if(!is_string($str)){ return gettype($str); } return preg_replace('#([^\x20-\x7E])#', '.', $str); } /* public static function angle3D($pos1, $pos2){ $X = $pos1["x"] - $pos2["x"]; $Z = $pos1["z"] - $pos2["z"]; $dXZ = sqrt(pow($X, 2) + pow($Z, 2)); $Y = $pos1["y"] - $pos2["y"]; $hAngle = rad2deg(atan2($Z, $X) - M_PI_2); $vAngle = rad2deg(-atan2($Y, $dXZ)); return array("yaw" => $hAngle, "pitch" => $vAngle); }*/ /** * @deprecated * @see Internet::getURL() * * @param string $page * @param int $timeout default 10 * @param array $extraHeaders * @param string &$err Will be set to the output of curl_error(). Use this to retrieve errors that occured during the operation. * @param array[] &$headers * @param int &$httpCode * * @return bool|mixed false if an error occurred, mixed data if successful. */ public static function getURL(string $page, int $timeout = 10, array $extraHeaders = [], &$err = null, &$headers = null, &$httpCode = null){ return Internet::getURL($page, $timeout, $extraHeaders, $err, $headers, $httpCode); } /** * @deprecated * @see Internet::postURL() * * @param string $page * @param array|string $args * @param int $timeout * @param array $extraHeaders * @param string &$err Will be set to the output of curl_error(). Use this to retrieve errors that occured during the operation. * @param array[] &$headers * @param int &$httpCode * * @return bool|mixed false if an error occurred, mixed data if successful. */ public static function postURL(string $page, $args, int $timeout = 10, array $extraHeaders = [], &$err = null, &$headers = null, &$httpCode = null){ return Internet::postURL($page, $args, $timeout, $extraHeaders, $err, $headers, $httpCode); } /** * @deprecated * @see Internet::simpleCurl() * * @param string $page * @param float|int $timeout The maximum connect timeout and timeout in seconds, correct to ms. * @param string[] $extraHeaders extra headers to send as a plain string array * @param array $extraOpts extra CURLOPT_* to set as an [opt => value] map * @param callable|null $onSuccess function to be called if there is no error. Accepts a resource argument as the cURL handle. * * @return array a plain array of three [result body : string, headers : array[], HTTP response code : int]. Headers are grouped by requests with strtolower(header name) as keys and header value as values * * @throws \RuntimeException if a cURL error occurs */ public static function simpleCurl(string $page, $timeout = 10, array $extraHeaders = [], array $extraOpts = [], callable $onSuccess = null){ return Internet::simpleCurl($page, $timeout, $extraHeaders, $extraOpts, $onSuccess); } public static function javaStringHash(string $string) : int{ $hash = 0; for($i = 0, $len = strlen($string); $i < $len; $i++){ $ord = ord($string{$i}); if($ord & 0x80){ $ord -= 0x100; } $hash = 31 * $hash + $ord; while($hash > 0x7FFFFFFF){ $hash -= 0x100000000; } while($hash < -0x80000000){ $hash += 0x100000000; } $hash &= 0xFFFFFFFF; } return $hash; } /** * @param string $command Command to execute * @param string|null &$stdout Reference parameter to write stdout to * @param string|null &$stderr Reference parameter to write stderr to * * @return int process exit code */ public static function execute(string $command, string &$stdout = null, string &$stderr = null) : int{ $process = proc_open($command, [ ["pipe", "r"], ["pipe", "w"], ["pipe", "w"] ], $pipes); if($process === false){ $stderr = "Failed to open process"; $stdout = ""; return -1; } $stdout = stream_get_contents($pipes[1]); $stderr = stream_get_contents($pipes[2]); foreach($pipes as $p){ fclose($p); } return proc_close($process); } public static function decodeJWT(string $token) : array{ list($headB64, $payloadB64, $sigB64) = explode(".", $token); return json_decode(base64_decode(strtr($payloadB64, '-_', '+/'), true), true); } public static function kill($pid) : void{ if(MainLogger::isRegisteredStatic()){ MainLogger::getLogger()->syncFlushBuffer(); } switch(Utils::getOS()){ case "win": exec("taskkill.exe /F /PID " . ((int) $pid) . " > NUL"); break; case "mac": case "linux": default: if(function_exists("posix_kill")){ posix_kill($pid, 9); //SIGKILL }else{ exec("kill -9 " . ((int) $pid) . " > /dev/null 2>&1"); } } } /** * @param object $value * @param bool $includeCurrent * * @return int */ public static function getReferenceCount($value, bool $includeCurrent = true){ ob_start(); debug_zval_dump($value); $ret = explode("\n", ob_get_contents()); ob_end_clean(); if(count($ret) >= 1 and preg_match('/^.* refcount\\(([0-9]+)\\)\\{$/', trim($ret[0]), $m) > 0){ return ((int) $m[1]) - ($includeCurrent ? 3 : 4); //$value + zval call + extra call } return -1; } /** * @param int $start * @param array|null $trace * * @return array */ public static function getTrace($start = 0, $trace = null){ if($trace === null){ if(function_exists("xdebug_get_function_stack")){ $trace = array_reverse(xdebug_get_function_stack()); }else{ $e = new \Exception(); $trace = $e->getTrace(); } } $messages = []; $j = 0; for($i = (int) $start; isset($trace[$i]); ++$i, ++$j){ $params = ""; if(isset($trace[$i]["args"]) or isset($trace[$i]["params"])){ if(isset($trace[$i]["args"])){ $args = $trace[$i]["args"]; }else{ $args = $trace[$i]["params"]; } $params = implode(", ", array_map(function($value){ return (is_object($value) ? get_class($value) . " object" : gettype($value) . " " . (is_array($value) ? "Array()" : Utils::printable(@strval($value)))); }, $args)); } $messages[] = "#$j " . (isset($trace[$i]["file"]) ? self::cleanPath($trace[$i]["file"]) : "") . "(" . (isset($trace[$i]["line"]) ? $trace[$i]["line"] : "") . "): " . (isset($trace[$i]["class"]) ? $trace[$i]["class"] . (($trace[$i]["type"] === "dynamic" or $trace[$i]["type"] === "->") ? "->" : "::") : "") . $trace[$i]["function"] . "(" . Utils::printable($params) . ")"; } return $messages; } public static function cleanPath($path){ return str_replace(["\\", ".php", "phar://", str_replace(["\\", "phar://"], ["/", ""], \pocketmine\PATH), str_replace(["\\", "phar://"], ["/", ""], \pocketmine\PLUGIN_PATH)], ["/", "", "", "", ""], $path); } /** * Extracts one-line tags from the doc-comment * * @param string $docComment * * @return string[] an array of tagName => tag value. If the tag has no value, an empty string is used as the value. */ public static function parseDocComment(string $docComment) : array{ preg_match_all('/(*ANYCRLF)^[\t ]*\* @([a-zA-Z]+)(?:[\t ]+(.+))?[\t ]*$/m', $docComment, $matches); return array_combine($matches[1], $matches[2]); } /** * @param int $severity * @param string $message * @param string $file * @param int $line * * @return bool * @throws \ErrorException */ public static function errorExceptionHandler(int $severity, string $message, string $file, int $line) : bool{ if(error_reporting() & $severity){ throw new \ErrorException($message, 0, $severity, $file, $line); } return true; //stfu operator } }