conn.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. // 设置编码
  3. session_start();
  4. header('Content-Type: text/html; charset=utf-8');
  5. header('Cache-Control: no-cache');
  6. date_default_timezone_set('Asia/Shanghai');
  7. // 数据库连接
  8. $conn = new mysqli("127.0.0.1", "crm", "Qweasdzxc", "crm_new");
  9. if ($conn->connect_error) {
  10. die("Connection failed: " . $conn->connect_error);
  11. }
  12. $conn->set_charset("utf8mb4");
  13. // 检查登录
  14. function checkLogin() {
  15. if (empty($_SESSION['employee_id'])) {
  16. echo "<script>top.location.href='index.php'</script>";
  17. exit;
  18. }
  19. }
  20. function checkAdmin() {
  21. if ((empty($_SESSION['em_permission_role_id'])||($_SESSION['em_permission_role_id']!=1))) {
  22. die("No permission , Please contact the administrator");
  23. exit;
  24. }
  25. }
  26. function checkPermissionDie(...$permission_role_ids) {
  27. // 检查会话中是否设置权限ID
  28. if (empty($_SESSION['em_permission_role_id'])) {
  29. die("No permission , Please contact the administrator");
  30. }
  31. // 如果是超级管理员(ID=1),直接返回true
  32. if ($_SESSION['em_permission_role_id'] == 1) {
  33. // return true;
  34. }
  35. // 检查当前角色ID是否在允许的角色ID中
  36. if(!in_array($_SESSION['em_permission_role_id'], $permission_role_ids))
  37. {
  38. die("No permission , Please contact the administrator");
  39. }
  40. }
  41. function checkPermission(...$permission_role_ids) {
  42. // 检查会话中是否设置权限ID
  43. if (empty($_SESSION['em_permission_role_id'])) {
  44. return false;
  45. }
  46. // 如果是超级管理员(ID=1),直接返回true
  47. if ($_SESSION['em_permission_role_id'] == 1) {
  48. return true;
  49. }
  50. // 检查当前角色ID是否在允许的角色ID中
  51. return in_array($_SESSION['em_permission_role_id'], $permission_role_ids);
  52. }
  53. // 检查管理员或组长或组员
  54. function checkAdminOrEmployee () {
  55. 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))) {
  56. return false;
  57. }
  58. else
  59. {
  60. return true;
  61. }
  62. }
  63. // 获取IP
  64. function getIp() {
  65. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'];
  66. if (strlen($ip) > 15) {
  67. $ip = "UnKnow";
  68. }
  69. return htmlspecialcharsFix($ip);
  70. }
  71. // 记录操作日志
  72. function logAction($action) {
  73. global $conn;
  74. // 从SESSION获取当前用户名
  75. $loginName = $_SESSION['employee_name'] ?? 'Unknown User';
  76. // 获取当前IP
  77. $loginIp = getIp();
  78. // 当前时间
  79. $loginTime = date('Y-m-d H:i:s');
  80. // 记录到日志表
  81. $stmt = "INSERT INTO logrecord (loginName, loginIp, loginTime, loginAct) VALUES (
  82. '" . mysqli_real_escape_string($conn, $loginName) . "',
  83. '" . mysqli_real_escape_string($conn, $loginIp) . "',
  84. '$loginTime',
  85. '" . mysqli_real_escape_string($conn, $action) . "')";
  86. $conn->query($stmt);
  87. }
  88. // 移除HTML
  89. function removeHTML($t0) {
  90. if (empty($t0)) {
  91. return "";
  92. }
  93. $t0 = preg_replace("/<script.+?\/script>/is", "", $t0);
  94. $t0 = preg_replace("/<iframe.+?\/iframe>/is", "", $t0);
  95. $t0 = str_replace(["&lt;", "&gt;", "&nbsp;"], ["<", ">", ""], $t0);
  96. $t0 = preg_replace("/<.+?>/", "", $t0);
  97. return str_replace(["\r\n", "\t", "\r", "\n"], "", $t0);
  98. }
  99. // Text转HTML
  100. function txt2HTML($t0) {
  101. if (empty($t0) || is_array($t0)) {
  102. return "";
  103. }
  104. return str_replace(
  105. ["&", "\"", "<", ">", " "],
  106. ["&amp;", "&quot;", "&lt;", "&gt;", "&nbsp;"],
  107. $t0
  108. );
  109. }
  110. // HTML转Text
  111. function html2Txt($t0) {
  112. if (empty($t0) || is_array($t0)) {
  113. return "";
  114. }
  115. return str_replace(
  116. ["&quot;", "&lt;", "&gt;", "&nbsp;", "&amp;"],
  117. ["\"", "<", ">", " ", "&"],
  118. $t0
  119. );
  120. }
  121. // HTML编码
  122. function htmlEncode($t0) {
  123. if (empty($t0) || is_array($t0)) {
  124. return "";
  125. }
  126. $replacements = [
  127. chr(38) => "&#38;", chr(9) => "&#9;", chr(11) => "&#11;",
  128. chr(10) => "&#10;", chr(13) => "&#13;", chr(32) => "&#32;",
  129. chr(34) => "&#34;", chr(37) => "&#37;", chr(39) => "&#39;",
  130. chr(40) => "&#40;", chr(41) => "&#41;", chr(60) => "&#60;",
  131. chr(62) => "&#62;", chr(91) => "&#91;", chr(93) => "&#93;",
  132. chr(94) => "&#94;", chr(95) => "&#95;", chr(123) => "&#123;",
  133. chr(124) => "&#124;", chr(125) => "&#125;"
  134. ];
  135. return strtr($t0, $replacements);
  136. }
  137. // HTML解码
  138. function htmlUnCode($t0) {
  139. if (empty($t0) || is_array($t0)) {
  140. return "";
  141. }
  142. $replacements = [
  143. "&#9;" => chr(9), "&#11;" => chr(11), "&#10;" => chr(10),
  144. "&#13;" => chr(13), "&#32;" => chr(32), "&#34;" => chr(34),
  145. "&#37;" => chr(37), "&#39;" => chr(39), "&#40;" => chr(40),
  146. "&#41;" => chr(41), "&#60;" => chr(60), "&#62;" => chr(62),
  147. "&#91;" => chr(91), "&#93;" => chr(93), "&#94;" => chr(94),
  148. "&#95;" => chr(95), "&#123;" => chr(123), "&#124;" => chr(124),
  149. "&#125;" => chr(125), "&#38;" => chr(38)
  150. ];
  151. return strtr($t0, $replacements);
  152. }
  153. // 文本编码
  154. function textEncode($t0) {
  155. if (empty($t0) || is_array($t0)) {
  156. return "";
  157. }
  158. $t0 = trim($t0);
  159. $remove = [chr(8), chr(9), chr(11), chr(12), chr(10), chr(13)];
  160. $t0 = str_replace($remove, "", $t0);
  161. $replacements = [
  162. chr(38) => "&#38;", chr(47) => "&#47;", chr(32) => "&#32;",
  163. chr(34) => "&#34;", chr(37) => "&#37;", chr(39) => "&#39;",
  164. chr(40) => "&#40;", chr(41) => "&#41;", "(" => "&#40;",
  165. ")" => "&#41;", chr(60) => "&#60;", chr(62) => "&#62;",
  166. chr(91) => "&#91;", chr(93) => "&#93;", chr(94) => "&#94;",
  167. chr(95) => "&#95;", chr(123) => "&#123;", chr(124) => "&#124;",
  168. chr(125) => "&#125;"
  169. ];
  170. return strtr($t0, $replacements);
  171. }
  172. // 数字格式化
  173. function numFormat($t0) {
  174. if (empty($t0) || is_array($t0)) {
  175. return "";
  176. }
  177. $t0 = trim($t0);
  178. $remove = ["-", "+", "&#32;", "&", " ", chr(34), "*", "%", "'", "(", ")", "<", ">",
  179. "[", "]", "^", "_", "{", "\\", "/", "|", "}", "(", ")"];
  180. return str_replace($remove, "", $t0);
  181. }
  182. // 文本解码
  183. function textUncode($t0) {
  184. if (empty($t0) || is_array($t0)) {
  185. return "";
  186. }
  187. $replacements = [
  188. "&#32;" => chr(32), "&#34;" => chr(34), "&#37;" => chr(37),
  189. "&#39;" => chr(39), "&#40;" => chr(40), "&#41;" => chr(41),
  190. "&#60;" => chr(60), "&#62;" => chr(62), "&#91;" => chr(91),
  191. "&#93;" => chr(93), "&#94;" => chr(94), "&#95;" => chr(95),
  192. "&#123;" => chr(123), "&#124;" => chr(124), "&#125;" => chr(125),
  193. "&#47;" => chr(47), "&#38;" => chr(38)
  194. ];
  195. return strtr($t0, $replacements);
  196. }
  197. // HTML解码1
  198. function htmlUnCode1($t0) {
  199. if (empty($t0) || is_array($t0)) {
  200. return "";
  201. }
  202. $replacements = [
  203. "&#9;" => chr(9), "&#11;" => chr(11), "&#13;&#10;" => "<br />",
  204. "&#10;" => "<br />", "&#13;" => "<br />", "&#32;" => "&nbsp;",
  205. "&#38;" => chr(38)
  206. ];
  207. return strtr($t0, $replacements);
  208. }
  209. // 格式化时间
  210. function formatTime($ttime, $tparam) {
  211. if (!strtotime($ttime)) {
  212. return "";
  213. }
  214. $date = new DateTime($ttime);
  215. $tsrt = $tparam;
  216. $replacements = [
  217. "yyyy" => $date->format("Y"),
  218. "yy" => $date->format("y"),
  219. "mm" => $date->format("m"),
  220. "dd" => $date->format("d"),
  221. "hh" => $date->format("H"),
  222. "ff" => $date->format("i"),
  223. "ss" => $date->format("s"),
  224. "m" => $date->format("n"),
  225. "d" => $date->format("j"),
  226. "h" => $date->format("G"),
  227. "f" => $date->format("i"),
  228. "s" => $date->format("s")
  229. ];
  230. return strtr($tsrt, $replacements);
  231. }
  232. // 英文月份
  233. function enMonth($m) {
  234. $months = [
  235. "1" => "Jan", "2" => "Feb", "3" => "Mar", "4" => "Apr",
  236. "5" => "May", "6" => "Jun", "7" => "Jul", "8" => "Aug",
  237. "9" => "Sep", "10" => "Oct", "11" => "Nov", "12" => "Dec"
  238. ];
  239. return $months[$m] ?? "Dec";
  240. }
  241. // 字符串截取
  242. function strLeft($str, $strLen) {
  243. if (empty($str)) {
  244. return "";
  245. }
  246. $length = 0;
  247. $result = "";
  248. for ($i = 0; $i < mb_strlen($str); $i++) {
  249. $char = mb_substr($str, $i, 1);
  250. $length += (mb_ord($char) > 255) ? 2 : 1;
  251. if ($length > $strLen) {
  252. return $result . "..";
  253. }
  254. $result .= $char;
  255. }
  256. return $result;
  257. }
  258. // 验证邮箱
  259. function isValidEmail($email) {
  260. $names = explode("@", $email);
  261. if (count($names) !== 2) {
  262. return false;
  263. }
  264. foreach ($names as $name) {
  265. if (empty($name)) {
  266. return false;
  267. }
  268. if (preg_match("/[^a-z0-9_.-]/", strtolower($name))) {
  269. return false;
  270. }
  271. if (str_starts_with($name, ".") || str_ends_with($name, ".")) {
  272. return false;
  273. }
  274. }
  275. $domainParts = explode(".", $names[1]);
  276. if (count($domainParts) < 2) {
  277. return false;
  278. }
  279. $tldLength = strlen(end($domainParts));
  280. if ($tldLength !== 2 && $tldLength !== 3) {
  281. return false;
  282. }
  283. if (str_contains($email, "..")) {
  284. return false;
  285. }
  286. return true;
  287. }
  288. // 站点链接替换
  289. function sitelink_replace($t0, $t1, $t2, $t3) {
  290. if (empty($t0)) {
  291. return "";
  292. }
  293. $t4 = $t0;
  294. $pattern = "/(\<a[^<>]+\>.+?\<\/a\>)|(\<img[^<>]+\>)|(\<h[1-6]+[\s]*\>.+?\<\/h[1-6]+\>)/i";
  295. preg_match_all($pattern, $t4, $matches);
  296. $myarray = [];
  297. if (count($matches[0]) > 0) {
  298. foreach ($matches[0] as $i => $match) {
  299. $myarray[$i] = $match;
  300. $t4 = str_replace($match, "[$i]", $t4, $t3);
  301. }
  302. }
  303. if (empty($myarray)) {
  304. return str_replace($t1, $t2, $t0, $t3);
  305. }
  306. $t4 = str_replace($t1, $t2, $t4, $t3);
  307. foreach ($myarray as $i => $value) {
  308. $t4 = str_replace("[$i]", $value, $t4, $t3);
  309. }
  310. return $t4;
  311. }
  312. if(!function_exists('htmlspecialcharsFix')) {
  313. //处理特殊字符
  314. function htmlspecialcharsFix($input_str)
  315. {
  316. return $input_str;
  317. }
  318. }
  319. if(!function_exists('htmlspecialcharsAjaxFix')) {
  320. //处理特殊字符
  321. function htmlspecialcharsAjaxFix($input_str)
  322. {
  323. return textUncode($input_str);
  324. }
  325. }
  326. if(!function_exists('textDecode')) {
  327. function textDecode($str) {
  328. return textUncode($str);
  329. }
  330. }
  331. if(!function_exists('htmlDecode')) {
  332. function htmlDecode($str) {
  333. return htmlspecialchars_decode($str, ENT_QUOTES);
  334. }
  335. }