mirror of
https://github.com/cedar2025/Xboard.git
synced 2025-01-22 18:48:14 -05:00
154 lines
4.9 KiB
PHP
154 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Utils;
|
|
|
|
class Helper
|
|
{
|
|
public static function uuidToBase64($uuid, $length)
|
|
{
|
|
return base64_encode(substr($uuid, 0, $length));
|
|
}
|
|
|
|
public static function getServerKey($timestamp, $length)
|
|
{
|
|
return base64_encode(substr(md5($timestamp), 0, $length));
|
|
}
|
|
|
|
public static function guid($format = false)
|
|
{
|
|
if (function_exists('com_create_guid') === true) {
|
|
return md5(trim(com_create_guid(), '{}'));
|
|
}
|
|
$data = openssl_random_pseudo_bytes(16);
|
|
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
|
|
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
|
|
if ($format) {
|
|
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
|
|
}
|
|
return md5(vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)) . '-' . time());
|
|
}
|
|
|
|
public static function generateOrderNo(): string
|
|
{
|
|
$randomChar = mt_rand(10000, 99999);
|
|
return date('YmdHms') . substr(microtime(), 2, 6) . $randomChar;
|
|
}
|
|
|
|
public static function exchange($from, $to)
|
|
{
|
|
$result = file_get_contents('https://api.exchangerate.host/latest?symbols=' . $to . '&base=' . $from);
|
|
$result = json_decode($result, true);
|
|
return $result['rates'][$to];
|
|
}
|
|
|
|
public static function randomChar($len, $special = false)
|
|
{
|
|
$chars = array(
|
|
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
|
|
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
|
|
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
|
|
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
|
|
"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
|
|
"3", "4", "5", "6", "7", "8", "9"
|
|
);
|
|
|
|
if ($special) {
|
|
$chars = array_merge($chars, array(
|
|
"!", "@", "#", "$", "?", "|", "{", "/", ":", ";",
|
|
"%", "^", "&", "*", "(", ")", "-", "_", "[", "]",
|
|
"}", "<", ">", "~", "+", "=", ",", "."
|
|
));
|
|
}
|
|
|
|
$charsLen = count($chars) - 1;
|
|
shuffle($chars);
|
|
$str = '';
|
|
for ($i = 0; $i < $len; $i++) {
|
|
$str .= $chars[mt_rand(0, $charsLen)];
|
|
}
|
|
return $str;
|
|
}
|
|
|
|
public static function multiPasswordVerify($algo, $salt, $password, $hash)
|
|
{
|
|
switch($algo) {
|
|
case 'md5': return md5($password) === $hash;
|
|
case 'sha256': return hash('sha256', $password) === $hash;
|
|
case 'md5salt': return md5($password . $salt) === $hash;
|
|
default: return password_verify($password, $hash);
|
|
}
|
|
}
|
|
|
|
public static function emailSuffixVerify($email, $suffixs)
|
|
{
|
|
$suffix = preg_split('/@/', $email)[1];
|
|
if (!$suffix) return false;
|
|
if (!is_array($suffixs)) {
|
|
$suffixs = preg_split('/,/', $suffixs);
|
|
}
|
|
if (!in_array($suffix, $suffixs)) return false;
|
|
return true;
|
|
}
|
|
|
|
public static function trafficConvert(int $byte)
|
|
{
|
|
$kb = 1024;
|
|
$mb = 1048576;
|
|
$gb = 1073741824;
|
|
if ($byte > $gb) {
|
|
return round($byte / $gb, 2) . ' GB';
|
|
} else if ($byte > $mb) {
|
|
return round($byte / $mb, 2) . ' MB';
|
|
} else if ($byte > $kb) {
|
|
return round($byte / $kb, 2) . ' KB';
|
|
} else if ($byte < 0) {
|
|
return 0;
|
|
} else {
|
|
return round($byte, 2) . ' B';
|
|
}
|
|
}
|
|
|
|
public static function getSubscribeUrl($path)
|
|
{
|
|
$subscribeUrls = explode(',', admin_setting('subscribe_url'));
|
|
$subscribeUrl = $subscribeUrls[array_rand($subscribeUrls)];
|
|
$subscribeUrl = self::replaceRandomNumber($subscribeUrl);
|
|
if ($subscribeUrl) return $subscribeUrl . $path;
|
|
return url($path);
|
|
}
|
|
|
|
public static function randomPort($range) {
|
|
$portRange = explode('-', $range);
|
|
return rand($portRange[0], $portRange[1]);
|
|
}
|
|
|
|
public static function base64EncodeUrlSafe($data)
|
|
{
|
|
$encoded = base64_encode($data);
|
|
return str_replace(['+', '/', '='], ['-', '_', ''], $encoded);
|
|
}
|
|
|
|
/**
|
|
* 替换字符串中的 [num1-num2] 格式为介于 num1 和 num2 之间的随机数字
|
|
*
|
|
* @param string $input 用户输入的字符串
|
|
* @return string 替换后的字符串
|
|
*/
|
|
public static function replaceRandomNumber($input) {
|
|
// 匹配 [1-4999] 格式的正则表达式
|
|
$pattern = '/\[(\d+)-(\d+)\]/';
|
|
|
|
// 使用 preg_replace_callback 替换匹配到的内容
|
|
$result = preg_replace_callback($pattern, function ($matches) {
|
|
// 提取最小和最大值
|
|
$min = intval($matches[1]);
|
|
$max = intval($matches[2]);
|
|
// 生成随机数
|
|
$randomNumber = rand($min, $max);
|
|
return $randomNumber;
|
|
}, $input);
|
|
|
|
return $result;
|
|
}
|
|
}
|