123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- <?php
- // 设置编码
- session_start();
- header('Content-Type: text/html; charset=utf-8');
- header('Cache-Control: no-cache');
- date_default_timezone_set('Asia/Shanghai');
- // 数据库连接
- $conn = new mysqli("127.0.0.1", "crm", "Qweasdzxc", "crm_new");
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $conn->set_charset("utf8mb4");
- // 检查登录
- function checkLogin() {
- if (empty($_SESSION['employee_id'])) {
- echo "<script>top.location.href='index.php'</script>";
- 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("/<script.+?\/script>/is", "", $t0);
- $t0 = preg_replace("/<iframe.+?\/iframe>/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), " " => "<br />",
- " " => "<br />", " " => "<br />", " " => " ",
- "&" => 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[^<>]+\>.+?\<\/a\>)|(\<img[^<>]+\>)|(\<h[1-6]+[\s]*\>.+?\<\/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);
- }
- }
|