Files
Fuxsto-V4/Main/Hv4.Function.php
2025-10-18 10:19:37 +08:00

235 lines
6.7 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Content-Type');
session_start();
// 自动将 JSON 请求体转为 $_POST
if ($_SERVER['CONTENT_TYPE'] === 'application/json') {
$input = file_get_contents('php://input');
$data = json_decode($input, true);
if ($data) {
$_POST = array_merge($_POST, $data);
}
}
//祖传适配CDN
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
// 获取 'X-Forwarded-For' 中的第一个 IP 地址
$ipArray = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$_SERVER['REMOTE_ADDR'] = trim($ipArray[0]); // 更新 REMOTE_ADDR
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
// 如果 'X-Forwarded-For' 不存在,则尝试使用 'HTTP_CLIENT_IP'
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CLIENT_IP'];
}
//祖传输出
$Out = "{}";
function 定义输出($key,$value) {
global $Out;
$Out = json_decode($Out, true);
$Out[$key] = $value;
$Out = json_encode($Out, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
function 终止并输出() {
global $Out;
die($Out);
}
//祖传数据库函数
function 数据库运行($sql) {
// 数据库连接参数
$servername = "110.40.52.75";
$username = "fuxstohostv4";
$password = "yxh20080130";
$dbname = "fuxstohostv4";
// 创建数据库连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接是否成功
if ($conn->connect_error) {
return "连接失败: " . $conn->connect_error;
}
// 执行 SQL 查询
$result = $conn->query($sql);
// 查询失败,返回错误信息
if ($result === FALSE) {
$conn->close();
return "查询失败: " . $conn->error;
}
// 如果是 SELECT 查询,自动处理并返回结果
if (stripos($sql, 'SELECT') === 0) {
// 创建一个数组存储所有的查询结果
$data = [];
while ($row = $result->fetch_assoc()) {
$data[] = $row; // 将每一行的数据加入结果数组
}
$conn->close();
return $data; // 返回查询结果
}
// 对于非 SELECT 查询INSERT, UPDATE, DELETE直接返回 true
$conn->close();
return true;
}
// 生成随机字符串的函数
function 生成随机字符串($长度, $字符集, $字符集长度) {
$随机字符串 = '';
for ($i = 0; $i < $长度; $i++) {
$随机字符串 .= $字符集[rand(0, $字符集长度 - 1)];
}
return $随机字符串;
}
function 唯一随机字符串($长度) {
// 定义字符集
$字符集 = 'abcdefghijklmnopqrstuvwxyz0123456789';
$字符集长度 = strlen($字符集);
// 文件路径,用来存储已生成的字符串
$文件路径 = 'generated_strings.txt';
// 检查文件是否存在,如果不存在则创建
if (!file_exists($文件路径)) {
file_put_contents($文件路径, ''); // 创建一个空文件
}
// 生成唯一字符串
do {
$随机字符串 = 生成随机字符串($长度, $字符集, $字符集长度);
$已生成的字符串 = file($文件路径, FILE_IGNORE_NEW_LINES);
} while (in_array($随机字符串, $已生成的字符串));
// 将新生成的字符串保存到文件
file_put_contents($文件路径, $随机字符串 . PHP_EOL, FILE_APPEND);
return $随机字符串;
}
function JSON解析($json) {
return @json_decode($json,true);
}
function 转JSON($json) {
return @json_encode($json, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
/**
* 生成高颜值不重复产品编号
* 特点:排除易混淆字符、智能查重、自动保存记录、美观格式化
*/
function 唯一字符串() {
// 配置参数(可自行调整)
$charSet = [
'letters' => 'ABCDEFGHJKLMNPQRSTUVWXYZ', // 24个大写字母排除I/O
'digits' => '23456789', // 8个数字排除0/1
];
$pattern = 'AAA-111-AAA'; // 定义编号格式3字母 + 3数字 + 3字母
$storageFile = 'product_codes.txt'; // 存储文件
do {
// 根据格式生成各部分
$codeParts = [];
foreach (explode('-', $pattern) as $segment) {
$type = preg_match('/^A+$/', $segment) ? 'letters' : 'digits';
$codeParts[] = generateSegment($charSet[$type], strlen($segment));
}
// 组合完整编码去掉分隔符后确保10位
$fullCode = str_replace('-', '', implode('', $codeParts));
// 检查是否已存在(使用内存加速查询)
$existingCodes = file_exists($storageFile)
? array_flip(file($storageFile, FILE_IGNORE_NEW_LINES))
: [];
} while (isset($existingCodes[$fullCode]));
// 保存新编码并返回格式化版本
file_put_contents($storageFile, $fullCode.PHP_EOL, FILE_APPEND);
return implode('-', $codeParts);
}
/**
* 生成指定长度的字符段
*/
function generateSegment($characters, $length) {
$segment = '';
$maxIndex = strlen($characters) - 1;
for ($i = 0; $i < $length; $i++) {
$segment .= $characters[random_int(0, $maxIndex)];
}
return $segment;
}
function 发送邮件接口($收件人, $主题, $内容, $接口地址 = 'https://hv3.fuxsto.cn/user/OpenEmailApi.php')
{
// 参数有效性验证
if (!filter_var($收件人, FILTER_VALIDATE_EMAIL)) {
return "收件人邮箱格式无效";
}
// 准备请求参数(自动编码处理)
$请求数据 = http_build_query([
'to' => $收件人,
'subject' => $主题,
'msg' => $内容
]);
// 初始化cURL
$ch = curl_init();
// 配置cURL选项
curl_setopt_array($ch, [
CURLOPT_URL => $接口地址,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true, // 验证SSL证书
CURLOPT_TIMEOUT => 15, // 15秒超时
CURLOPT_CONNECTTIMEOUT => 5, // 5秒连接超时
CURLOPT_POSTFIELDS => $请求数据,
CURLOPT_HTTPHEADER => [
'Content-Type: application/x-www-form-urlencoded; charset=UTF-8'
]
]);
// 执行请求
$响应 = curl_exec($ch);
// 错误处理
if (curl_errno($ch)) {
$错误信息 = '网络请求失败: ' . curl_error($ch);
curl_close($ch);
return $错误信息;
}
// 获取HTTP状态码
$状态码 = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// 处理响应
if ($状态码 !== 200) {
return "接口响应异常 (HTTP {$状态码})";
}
// 验证响应内容
return ($响应 === '200') ? 200 : trim($响应);
}