'-', '/' => '_', '=' => '')); } // base64解密(处理URL) function base64url_decode($data) { return base64_decode(strtr($data, '-_', '+/')); } // 根据流量值自动转换单位输出 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; } public static function flowToGB($traffic) { $gb = 1048576 * 1024; return $traffic / $gb; } // 加密方式 public function methodList() { return SsConfig::where('type', 1)->get(); } // 协议 public function protocolList() { return SsConfig::where('type', 2)->get(); } // 混淆 public function obfsList() { return SsConfig::where('type', 3)->get(); } // 系统配置 public function systemConfig() { $config = Config::get(); $data = []; foreach ($config as $vo) { $data[$vo->name] = $vo->value; } return $data; } // 账号等级对应名称 public function userLevelConfig() { return [ 1 => '倔强青铜', 2 => '秩序白银', 3 => '荣耀黄金', 4 => '尊贵铂金', 5 => '永恒钻石', 6 => '至尊黑曜', 7 => '最强王者' ]; } // 获取一个随机端口 public function getRandPort() { $port = mt_rand(10000, 40000); $deny_port = [17185, 28281]; $exists_port = User::query()->pluck('port')->toArray(); if (in_array($port, $exists_port) || in_array($port, $deny_port)) { $port = $this->getRandPort(); } return $port; } // 类似Linux中的tail命令 public function tail($file, $n, $base = 5) { $fp = fopen($file, "r+"); assert($n > 0); $pos = $n + 1; $lines = array(); while (count($lines) <= $n) { try { fseek($fp, -$pos, SEEK_END); } catch (Exception $e) { fseek(0); break; } $pos *= $base; while (!feof($fp)) { array_unshift($lines, fgets($fp)); } } return array_slice($lines, 0, $n); } /** * 文件大小转换 * * @param int $bytes * @param int $precision * * @return string */ public function formatBytes($bytes, $precision = 2) { $units = array('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]; } // 浏览器类型 public function browsers($tag) { $data = [ 'MicroMessenger' => '微信', 'iPhone' => 'iPhone', 'Linux' => '安卓' ]; } /** * 写入邮件发送日志 * @param int $user_id 用户ID * @param string $title 投递类型(投递标题) * @param string $content 投递内容(简要概述) * @param int $status 投递状态 * @param string $error 投递失败时记录的异常信息 */ public function sendEmailLog($user_id, $title, $content, $status = 1, $error = '') { $emailLogObj = new EmailLog(); $emailLogObj->user_id = $user_id; $emailLogObj->title = $title; $emailLogObj->content = $content; $emailLogObj->status = $status; $emailLogObj->error = $error; $emailLogObj->created_at = date('Y-m-d H:i:s'); $emailLogObj->save(); } }