connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->set_charset("utf8mb4");
// 检查登录
function checkLogin() {
if (empty($_SESSION['employee_id'])) {
echo "";
exit;
}
}
function checkAdmin() {
if ((empty($_SESSION['em_permission_role_id'])||($_SESSION['em_permission_role_id']!=1))) {
die("No permission , Please contact the administrator");
exit;
}
}
function checkPermissionDie(...$permission_role_ids) {
// 检查会话中是否设置权限ID
if (empty($_SESSION['em_permission_role_id'])) {
die("No permission , Please contact the administrator");
}
// 如果是超级管理员(ID=1),直接返回true
if ($_SESSION['em_permission_role_id'] == 1) {
// return true;
}
// 检查当前角色ID是否在允许的角色ID中
if(!in_array($_SESSION['em_permission_role_id'], $permission_role_ids))
{
die("No permission , Please contact the administrator");
}
}
function checkPermission(...$permission_role_ids) {
// 检查会话中是否设置权限ID
if (empty($_SESSION['em_permission_role_id'])) {
return false;
}
// 如果是超级管理员(ID=1),直接返回true
if ($_SESSION['em_permission_role_id'] == 1) {
return true;
}
// 检查当前角色ID是否在允许的角色ID中
return in_array($_SESSION['em_permission_role_id'], $permission_role_ids);
}
// 检查管理员或组长或组员
function checkAdminOrEmployee () {
if ((empty($_SESSION['em_permission_role_id'])||($_SESSION['em_permission_role_id']!=1)&&($_SESSION['em_permission_role_id']!=2)&&($_SESSION['em_permission_role_id']!=3))) {
return false;
}
else
{
return true;
}
}
// 检查是否管理员
function checkIfAdmin() {
if ((empty($_SESSION['em_permission_role_id'])||($_SESSION['em_permission_role_id']!=1))) {
return false;
}
else
{
return true;
}
}
// 获取IP
function getIp() {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'];
if (strlen($ip) > 15) {
$ip = "UnKnow";
}
return htmlspecialcharsFix($ip);
}
// 记录操作日志
function logAction($action) {
global $conn;
// 从SESSION获取当前用户名
$loginName = $_SESSION['employee_name'] ?? 'Unknown User';
// 获取当前IP
$loginIp = getIp();
// 当前时间
$loginTime = date('Y-m-d H:i:s');
// 记录到日志表
$stmt = "INSERT INTO logrecord (loginName, loginIp, loginTime, loginAct) VALUES (
'" . mysqli_real_escape_string($conn, $loginName) . "',
'" . mysqli_real_escape_string($conn, $loginIp) . "',
'$loginTime',
'" . mysqli_real_escape_string($conn, $action) . "')";
$conn->query($stmt);
}
// 移除HTML
function removeHTML($t0) {
if (empty($t0)) {
return "";
}
$t0 = preg_replace("//is", "", $t0);
$t0 = preg_replace("//is", "", $t0);
$t0 = str_replace(["<", ">", " "], ["<", ">", ""], $t0);
$t0 = preg_replace("/<.+?>/", "", $t0);
return str_replace(["\r\n", "\t", "\r", "\n"], "", $t0);
}
// Text转HTML
function txt2HTML($t0) {
if (empty($t0) || is_array($t0)) {
return "";
}
return str_replace(
["&", "\"", "<", ">", " "],
["&", """, "<", ">", " "],
$t0
);
}
// HTML转Text
function html2Txt($t0) {
if (empty($t0) || is_array($t0)) {
return "";
}
return str_replace(
[""", "<", ">", " ", "&"],
["\"", "<", ">", " ", "&"],
$t0
);
}
// HTML编码
function htmlEncode($t0) {
if (empty($t0) || is_array($t0)) {
return "";
}
$replacements = [
chr(38) => "&", chr(9) => " ", chr(11) => "",
chr(10) => "
", chr(13) => "
", chr(32) => " ",
chr(34) => """, chr(37) => "%", chr(39) => "'",
chr(40) => "(", chr(41) => ")", chr(60) => "<",
chr(62) => ">", chr(91) => "[", chr(93) => "]",
chr(94) => "^", chr(95) => "_", chr(123) => "{",
chr(124) => "|", chr(125) => "}"
];
return strtr($t0, $replacements);
}
// HTML解码
function htmlUnCode($t0) {
if (empty($t0) || is_array($t0)) {
return "";
}
$replacements = [
" " => chr(9), "" => chr(11), "
" => chr(10),
"
" => chr(13), " " => chr(32), """ => chr(34),
"%" => chr(37), "'" => chr(39), "(" => chr(40),
")" => chr(41), "<" => chr(60), ">" => chr(62),
"[" => chr(91), "]" => chr(93), "^" => chr(94),
"_" => chr(95), "{" => chr(123), "|" => chr(124),
"}" => chr(125), "&" => chr(38)
];
return strtr($t0, $replacements);
}
// 文本编码
function textEncode($t0) {
if (empty($t0) || is_array($t0)) {
return "";
}
$t0 = trim($t0);
$remove = [chr(8), chr(9), chr(11), chr(12), chr(10), chr(13)];
$t0 = str_replace($remove, "", $t0);
$replacements = [
chr(38) => "&", chr(47) => "/", chr(32) => " ",
chr(34) => """, chr(37) => "%", chr(39) => "'",
chr(40) => "(", chr(41) => ")", "(" => "(",
")" => ")", chr(60) => "<", chr(62) => ">",
chr(91) => "[", chr(93) => "]", chr(94) => "^",
chr(95) => "_", chr(123) => "{", chr(124) => "|",
chr(125) => "}"
];
return strtr($t0, $replacements);
}
// 数字格式化
function numFormat($t0) {
if (empty($t0) || is_array($t0)) {
return "";
}
$t0 = trim($t0);
$remove = ["-", "+", " ", "&", " ", chr(34), "*", "%", "'", "(", ")", "<", ">",
"[", "]", "^", "_", "{", "\\", "/", "|", "}", "(", ")"];
return str_replace($remove, "", $t0);
}
// 文本解码
function textUncode($t0) {
if (empty($t0) || is_array($t0)) {
return "";
}
$replacements = [
" " => chr(32), """ => chr(34), "%" => chr(37),
"'" => chr(39), "(" => chr(40), ")" => chr(41),
"<" => chr(60), ">" => chr(62), "[" => chr(91),
"]" => chr(93), "^" => chr(94), "_" => chr(95),
"{" => chr(123), "|" => chr(124), "}" => chr(125),
"/" => chr(47), "&" => chr(38)
];
return strtr($t0, $replacements);
}
// HTML解码1
function htmlUnCode1($t0) {
if (empty($t0) || is_array($t0)) {
return "";
}
$replacements = [
" " => chr(9), "" => chr(11), "
" => "
",
"
" => "
", "
" => "
", " " => " ",
"&" => chr(38)
];
return strtr($t0, $replacements);
}
// 格式化时间
function formatTime($ttime, $tparam) {
if (!strtotime($ttime)) {
return "";
}
$date = new DateTime($ttime);
$tsrt = $tparam;
$replacements = [
"yyyy" => $date->format("Y"),
"yy" => $date->format("y"),
"mm" => $date->format("m"),
"dd" => $date->format("d"),
"hh" => $date->format("H"),
"ff" => $date->format("i"),
"ss" => $date->format("s"),
"m" => $date->format("n"),
"d" => $date->format("j"),
"h" => $date->format("G"),
"f" => $date->format("i"),
"s" => $date->format("s")
];
return strtr($tsrt, $replacements);
}
// 英文月份
function enMonth($m) {
$months = [
"1" => "Jan", "2" => "Feb", "3" => "Mar", "4" => "Apr",
"5" => "May", "6" => "Jun", "7" => "Jul", "8" => "Aug",
"9" => "Sep", "10" => "Oct", "11" => "Nov", "12" => "Dec"
];
return $months[$m] ?? "Dec";
}
// 字符串截取
function strLeft($str, $strLen) {
if (empty($str)) {
return "";
}
$length = 0;
$result = "";
for ($i = 0; $i < mb_strlen($str); $i++) {
$char = mb_substr($str, $i, 1);
$length += (mb_ord($char) > 255) ? 2 : 1;
if ($length > $strLen) {
return $result . "..";
}
$result .= $char;
}
return $result;
}
// 验证邮箱
function isValidEmail($email) {
$names = explode("@", $email);
if (count($names) !== 2) {
return false;
}
foreach ($names as $name) {
if (empty($name)) {
return false;
}
if (preg_match("/[^a-z0-9_.-]/", strtolower($name))) {
return false;
}
if (str_starts_with($name, ".") || str_ends_with($name, ".")) {
return false;
}
}
$domainParts = explode(".", $names[1]);
if (count($domainParts) < 2) {
return false;
}
$tldLength = strlen(end($domainParts));
if ($tldLength !== 2 && $tldLength !== 3) {
return false;
}
if (str_contains($email, "..")) {
return false;
}
return true;
}
// 站点链接替换
function sitelink_replace($t0, $t1, $t2, $t3) {
if (empty($t0)) {
return "";
}
$t4 = $t0;
$pattern = "/(\]+\>.+?\<\/a\>)|(\
]+\>)|(\.+?\<\/h[1-6]+\>)/i";
preg_match_all($pattern, $t4, $matches);
$myarray = [];
if (count($matches[0]) > 0) {
foreach ($matches[0] as $i => $match) {
$myarray[$i] = $match;
$t4 = str_replace($match, "[$i]", $t4, $t3);
}
}
if (empty($myarray)) {
return str_replace($t1, $t2, $t0, $t3);
}
$t4 = str_replace($t1, $t2, $t4, $t3);
foreach ($myarray as $i => $value) {
$t4 = str_replace("[$i]", $value, $t4, $t3);
}
return $t4;
}
if(!function_exists('htmlspecialcharsFix')) {
//处理特殊字符
function htmlspecialcharsFix($input_str)
{
return $input_str;
}
}
if(!function_exists('htmlspecialcharsAjaxFix')) {
//处理特殊字符
function htmlspecialcharsAjaxFix($input_str)
{
return textUncode($input_str);
}
}
if(!function_exists('textDecode')) {
function textDecode($str) {
return textUncode($str);
}
}
if(!function_exists('htmlDecode')) {
function htmlDecode($str) {
return htmlspecialchars_decode($str, ENT_QUOTES);
}
}