You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Utils/Tools.php

192 lines
4.6 KiB
PHTML

<?php
namespace App\Utils;
use App\Services\Config;
use DateTime;
class Tools
{
/**
* 根据流量值自动转换单位输出
*/
public static function flowAutoShow($value = 0)
{
$kb = 1024;
$mb = 1048576;
$gb = 1073741824;
$tb = $gb * 1024;
$pb = $tb * 1024;
if (abs($value) > $pb) {
return round($value / $pb, 2) . "PB";
} elseif (abs($value) > $tb) {
return round($value / $tb, 2) . "TB";
} elseif (abs($value) > $gb) {
return round($value / $gb, 2) . "GB";
} elseif (abs($value) > $mb) {
return round($value / $mb, 2) . "MB";
} elseif (abs($value) > $kb) {
return round($value / $kb, 2) . "KB";
} else {
return round($value, 2) . "B";
}
}
public static function toMB($traffic)
{
$mb = 1048576;
return $traffic * $mb;
}
public static function toGB($traffic)
{
$gb = 1048576 * 1024;
return $traffic * $gb;
}
/**
* @param $traffic
* @return float
*/
public static function flowToGB($traffic)
{
$gb = 1048576 * 1024;
return $traffic / $gb;
}
/**
* @param $traffic
* @return float
*/
public static function flowToMB($traffic)
{
$gb = 1048576;
return $traffic / $gb;
}
//获取随机字符串
public static function genRandomNum($length = 8)
{
$chars = '0123456789';
$char = '';
for ($i = 0; $i < $length; $i++) {
$char .= $chars[random_int(0, strlen($chars) - 1)];
}
return $char;
}
//获取随机字符串
public static function genRandomChar($length = 8)
{
// 密码字符集,可任意添加你需要的字符
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$char = '';
for ($i = 0; $i < $length; $i++) {
$char .= $chars[random_int(0, strlen($chars) - 1)];
}
return $char;
}
public static function genToken()
{
return self::genRandomChar(64);
}
public static function is_ip($a)
{
$b = preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $a);
return $b;
}
// Unix time to Date Time
public static function toDateTime($time)
{
return date('Y-m-d H:i:s', $time);
}
public static function secondsToTime($seconds)
{
$dtF = new DateTime("@0");
$dtT = new DateTime("@$seconds");
return $dtF->diff($dtT)->format('%a 天, %h 小时, %i 分 + %s 秒');
}
public static function genSID()
{
return bin2hex(random_bytes(32));
}
public static function genUUID()
{
// @TODO
return self::genSID();
}
public static function base64_url_encode($input)
{
return strtr(base64_encode($input), array('+' => '-', '/' => '_', '=' => ''));
}
public static function base64_url_decode($input)
{
return base64_decode(strtr($input, '-_', '+/'));
}
public static function getDir($dir)
{
$dirArray[] = null;
if (false != ($handle = opendir($dir))) {
$i = 0;
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && !strpos($file, ".")) {
$dirArray[$i] = $file;
$i++;
}
}
closedir($handle);
}
return $dirArray;
}
public static function is_validate($str)
{
$pattern = "/[^A-Za-z0-9\-_\.]/";
if (preg_match($pattern, $str)) {
return false;
}
return true;
}
public static function get_middle_text($origin_text, $begin_text, $end_text)
{
$begin_pos = strpos($origin_text, $begin_text);
if ($begin_pos == false) {
return null;
}
$end_pos = strpos($origin_text, $end_text, $begin_pos + strlen($begin_text));
if ($end_pos == false) {
return null;
}
return substr($origin_text, $begin_pos + strlen($begin_text), $end_pos - $begin_pos - strlen($begin_text));
}
public static function keyFilter($object, $filter_array)
{
foreach ($object['attributes'] as $key => $value) {
if (!in_array($key, $filter_array)) {
unset($object->$key);
}
}
return $object;
}
public static function getRealIp($rawIp)
{
return str_replace("::ffff:", "", $rawIp);
}
}