You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
143 lines
4.3 KiB
143 lines
4.3 KiB
<?php
|
|
/**
|
|
* 辅助工具类
|
|
*/
|
|
class Helper {
|
|
/**
|
|
* 生成唯一ID
|
|
* @return string 唯一ID
|
|
*/
|
|
public static function generateId() {
|
|
return uniqid('', true);
|
|
}
|
|
|
|
/**
|
|
* 格式化日期时间
|
|
* @param mixed $timestamp 时间戳或日期时间字符串
|
|
* @param string $format 格式化字符串
|
|
* @return string 格式化后的日期时间
|
|
*/
|
|
public static function formatDateTime($timestamp, $format = 'Y-m-d H:i:s') {
|
|
if (is_numeric($timestamp)) {
|
|
return date($format, $timestamp);
|
|
} elseif (is_string($timestamp)) {
|
|
return date($format, strtotime($timestamp));
|
|
}
|
|
return date($format);
|
|
}
|
|
|
|
/**
|
|
* 验证邮箱格式
|
|
* @param string $email 邮箱地址
|
|
* @return bool 是否为有效的邮箱格式
|
|
*/
|
|
public static function validateEmail($email) {
|
|
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
|
|
}
|
|
|
|
/**
|
|
* 验证IP地址格式
|
|
* @param string $ip IP地址
|
|
* @return bool 是否为有效的IP地址格式
|
|
*/
|
|
public static function validateIp($ip) {
|
|
return filter_var($ip, FILTER_VALIDATE_IP) !== false;
|
|
}
|
|
|
|
/**
|
|
* 获取客户端IP地址
|
|
* @return string 客户端IP地址
|
|
*/
|
|
public static function getClientIp() {
|
|
$ip = $_SERVER['REMOTE_ADDR'] ?? '127.0.0.1';
|
|
|
|
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
|
$forwardedIps = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
|
|
$ip = trim($forwardedIps[0]);
|
|
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
|
|
$ip = $_SERVER['HTTP_CLIENT_IP'];
|
|
}
|
|
|
|
return $ip;
|
|
}
|
|
|
|
/**
|
|
* 加密密码
|
|
* @param string $password 原始密码
|
|
* @return string 加密后的密码
|
|
*/
|
|
public static function encryptPassword($password) {
|
|
return password_hash($password, PASSWORD_DEFAULT);
|
|
}
|
|
|
|
/**
|
|
* 验证密码
|
|
* @param string $password 原始密码
|
|
* @param string $hash 加密后的密码
|
|
* @return bool 密码是否匹配
|
|
*/
|
|
public static function verifyPassword($password, $hash) {
|
|
return password_verify($password, $hash);
|
|
}
|
|
|
|
/**
|
|
* 截断字符串
|
|
* @param string $string 原始字符串
|
|
* @param int $length 截断长度
|
|
* @param string $suffix 后缀
|
|
* @return string 截断后的字符串
|
|
*/
|
|
public static function truncate($string, $length = 100, $suffix = '...') {
|
|
if (strlen($string) <= $length) {
|
|
return $string;
|
|
}
|
|
return substr($string, 0, $length - strlen($suffix)) . $suffix;
|
|
}
|
|
|
|
/**
|
|
* 生成随机字符串
|
|
* @param int $length 字符串长度
|
|
* @param string $characters 字符集
|
|
* @return string 随机字符串
|
|
*/
|
|
public static function generateRandomString($length = 16, $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
|
|
$randomString = '';
|
|
$charactersLength = strlen($characters);
|
|
for ($i = 0; $i < $length; $i++) {
|
|
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
|
}
|
|
return $randomString;
|
|
}
|
|
|
|
/**
|
|
* 计算文件大小的人类可读格式
|
|
* @param int $bytes 文件大小(字节)
|
|
* @param int $precision 精度
|
|
* @return string 人类可读的文件大小
|
|
*/
|
|
public static function formatFileSize($bytes, $precision = 2) {
|
|
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
$bytes = max($bytes, 0);
|
|
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
|
|
$pow = min($pow, count($units) - 1);
|
|
$bytes /= pow(1024, $pow);
|
|
return round($bytes, $precision) . ' ' . $units[$pow];
|
|
}
|
|
|
|
/**
|
|
* 检查端口是否可用
|
|
* @param string $host 主机地址
|
|
* @param int $port 端口号
|
|
* @param int $timeout 超时时间(秒)
|
|
* @return bool 端口是否可用
|
|
*/
|
|
public static function isPortAvailable($host, $port, $timeout = 1) {
|
|
$socket = @fsockopen($host, $port, $errno, $errstr, $timeout);
|
|
if ($socket) {
|
|
fclose($socket);
|
|
return false; // 端口已被占用
|
|
}
|
|
return true; // 端口可用
|
|
}
|
|
}
|