conn.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. // 移除HTML
  72. function removeHTML($t0) {
  73. if (empty($t0)) {
  74. return "";
  75. }
  76. $t0 = preg_replace("/<script.+?\/script>/is", "", $t0);
  77. $t0 = preg_replace("/<iframe.+?\/iframe>/is", "", $t0);
  78. $t0 = str_replace(["&lt;", "&gt;", "&nbsp;"], ["<", ">", ""], $t0);
  79. $t0 = preg_replace("/<.+?>/", "", $t0);
  80. return str_replace(["\r\n", "\t", "\r", "\n"], "", $t0);
  81. }
  82. // Text转HTML
  83. function txt2HTML($t0) {
  84. if (empty($t0) || is_array($t0)) {
  85. return "";
  86. }
  87. return str_replace(
  88. ["&", "\"", "<", ">", " "],
  89. ["&amp;", "&quot;", "&lt;", "&gt;", "&nbsp;"],
  90. $t0
  91. );
  92. }
  93. // HTML转Text
  94. function html2Txt($t0) {
  95. if (empty($t0) || is_array($t0)) {
  96. return "";
  97. }
  98. return str_replace(
  99. ["&quot;", "&lt;", "&gt;", "&nbsp;", "&amp;"],
  100. ["\"", "<", ">", " ", "&"],
  101. $t0
  102. );
  103. }
  104. // HTML编码
  105. function htmlEncode($t0) {
  106. if (empty($t0) || is_array($t0)) {
  107. return "";
  108. }
  109. $replacements = [
  110. chr(38) => "&#38;", chr(9) => "&#9;", chr(11) => "&#11;",
  111. chr(10) => "&#10;", chr(13) => "&#13;", chr(32) => "&#32;",
  112. chr(34) => "&#34;", chr(37) => "&#37;", chr(39) => "&#39;",
  113. chr(40) => "&#40;", chr(41) => "&#41;", chr(60) => "&#60;",
  114. chr(62) => "&#62;", chr(91) => "&#91;", chr(93) => "&#93;",
  115. chr(94) => "&#94;", chr(95) => "&#95;", chr(123) => "&#123;",
  116. chr(124) => "&#124;", chr(125) => "&#125;"
  117. ];
  118. return strtr($t0, $replacements);
  119. }
  120. // HTML解码
  121. function htmlUnCode($t0) {
  122. if (empty($t0) || is_array($t0)) {
  123. return "";
  124. }
  125. $replacements = [
  126. "&#9;" => chr(9), "&#11;" => chr(11), "&#10;" => chr(10),
  127. "&#13;" => chr(13), "&#32;" => chr(32), "&#34;" => chr(34),
  128. "&#37;" => chr(37), "&#39;" => chr(39), "&#40;" => chr(40),
  129. "&#41;" => chr(41), "&#60;" => chr(60), "&#62;" => chr(62),
  130. "&#91;" => chr(91), "&#93;" => chr(93), "&#94;" => chr(94),
  131. "&#95;" => chr(95), "&#123;" => chr(123), "&#124;" => chr(124),
  132. "&#125;" => chr(125), "&#38;" => chr(38)
  133. ];
  134. return strtr($t0, $replacements);
  135. }
  136. // 文本编码
  137. function textEncode($t0) {
  138. if (empty($t0) || is_array($t0)) {
  139. return "";
  140. }
  141. $t0 = trim($t0);
  142. $remove = [chr(8), chr(9), chr(11), chr(12), chr(10), chr(13)];
  143. $t0 = str_replace($remove, "", $t0);
  144. $replacements = [
  145. chr(38) => "&#38;", chr(47) => "&#47;", chr(32) => "&#32;",
  146. chr(34) => "&#34;", chr(37) => "&#37;", chr(39) => "&#39;",
  147. chr(40) => "&#40;", chr(41) => "&#41;", "(" => "&#40;",
  148. ")" => "&#41;", chr(60) => "&#60;", chr(62) => "&#62;",
  149. chr(91) => "&#91;", chr(93) => "&#93;", chr(94) => "&#94;",
  150. chr(95) => "&#95;", chr(123) => "&#123;", chr(124) => "&#124;",
  151. chr(125) => "&#125;"
  152. ];
  153. return strtr($t0, $replacements);
  154. }
  155. // 数字格式化
  156. function numFormat($t0) {
  157. if (empty($t0) || is_array($t0)) {
  158. return "";
  159. }
  160. $t0 = trim($t0);
  161. $remove = ["-", "+", "&#32;", "&", " ", chr(34), "*", "%", "'", "(", ")", "<", ">",
  162. "[", "]", "^", "_", "{", "\\", "/", "|", "}", "(", ")"];
  163. return str_replace($remove, "", $t0);
  164. }
  165. // 文本解码
  166. function textUncode($t0) {
  167. if (empty($t0) || is_array($t0)) {
  168. return "";
  169. }
  170. $replacements = [
  171. "&#32;" => chr(32), "&#34;" => chr(34), "&#37;" => chr(37),
  172. "&#39;" => chr(39), "&#40;" => chr(40), "&#41;" => chr(41),
  173. "&#60;" => chr(60), "&#62;" => chr(62), "&#91;" => chr(91),
  174. "&#93;" => chr(93), "&#94;" => chr(94), "&#95;" => chr(95),
  175. "&#123;" => chr(123), "&#124;" => chr(124), "&#125;" => chr(125),
  176. "&#47;" => chr(47), "&#38;" => chr(38)
  177. ];
  178. return strtr($t0, $replacements);
  179. }
  180. // HTML解码1
  181. function htmlUnCode1($t0) {
  182. if (empty($t0) || is_array($t0)) {
  183. return "";
  184. }
  185. $replacements = [
  186. "&#9;" => chr(9), "&#11;" => chr(11), "&#13;&#10;" => "<br />",
  187. "&#10;" => "<br />", "&#13;" => "<br />", "&#32;" => "&nbsp;",
  188. "&#38;" => chr(38)
  189. ];
  190. return strtr($t0, $replacements);
  191. }
  192. // 格式化时间
  193. function formatTime($ttime, $tparam) {
  194. if (!strtotime($ttime)) {
  195. return "";
  196. }
  197. $date = new DateTime($ttime);
  198. $tsrt = $tparam;
  199. $replacements = [
  200. "yyyy" => $date->format("Y"),
  201. "yy" => $date->format("y"),
  202. "mm" => $date->format("m"),
  203. "dd" => $date->format("d"),
  204. "hh" => $date->format("H"),
  205. "ff" => $date->format("i"),
  206. "ss" => $date->format("s"),
  207. "m" => $date->format("n"),
  208. "d" => $date->format("j"),
  209. "h" => $date->format("G"),
  210. "f" => $date->format("i"),
  211. "s" => $date->format("s")
  212. ];
  213. return strtr($tsrt, $replacements);
  214. }
  215. // 英文月份
  216. function enMonth($m) {
  217. $months = [
  218. "1" => "Jan", "2" => "Feb", "3" => "Mar", "4" => "Apr",
  219. "5" => "May", "6" => "Jun", "7" => "Jul", "8" => "Aug",
  220. "9" => "Sep", "10" => "Oct", "11" => "Nov", "12" => "Dec"
  221. ];
  222. return $months[$m] ?? "Dec";
  223. }
  224. // 字符串截取
  225. function strLeft($str, $strLen) {
  226. if (empty($str)) {
  227. return "";
  228. }
  229. $length = 0;
  230. $result = "";
  231. for ($i = 0; $i < mb_strlen($str); $i++) {
  232. $char = mb_substr($str, $i, 1);
  233. $length += (mb_ord($char) > 255) ? 2 : 1;
  234. if ($length > $strLen) {
  235. return $result . "..";
  236. }
  237. $result .= $char;
  238. }
  239. return $result;
  240. }
  241. // 验证邮箱
  242. function isValidEmail($email) {
  243. $names = explode("@", $email);
  244. if (count($names) !== 2) {
  245. return false;
  246. }
  247. foreach ($names as $name) {
  248. if (empty($name)) {
  249. return false;
  250. }
  251. if (preg_match("/[^a-z0-9_.-]/", strtolower($name))) {
  252. return false;
  253. }
  254. if (str_starts_with($name, ".") || str_ends_with($name, ".")) {
  255. return false;
  256. }
  257. }
  258. $domainParts = explode(".", $names[1]);
  259. if (count($domainParts) < 2) {
  260. return false;
  261. }
  262. $tldLength = strlen(end($domainParts));
  263. if ($tldLength !== 2 && $tldLength !== 3) {
  264. return false;
  265. }
  266. if (str_contains($email, "..")) {
  267. return false;
  268. }
  269. return true;
  270. }
  271. // 站点链接替换
  272. function sitelink_replace($t0, $t1, $t2, $t3) {
  273. if (empty($t0)) {
  274. return "";
  275. }
  276. $t4 = $t0;
  277. $pattern = "/(\<a[^<>]+\>.+?\<\/a\>)|(\<img[^<>]+\>)|(\<h[1-6]+[\s]*\>.+?\<\/h[1-6]+\>)/i";
  278. preg_match_all($pattern, $t4, $matches);
  279. $myarray = [];
  280. if (count($matches[0]) > 0) {
  281. foreach ($matches[0] as $i => $match) {
  282. $myarray[$i] = $match;
  283. $t4 = str_replace($match, "[$i]", $t4, $t3);
  284. }
  285. }
  286. if (empty($myarray)) {
  287. return str_replace($t1, $t2, $t0, $t3);
  288. }
  289. $t4 = str_replace($t1, $t2, $t4, $t3);
  290. foreach ($myarray as $i => $value) {
  291. $t4 = str_replace("[$i]", $value, $t4, $t3);
  292. }
  293. return $t4;
  294. }
  295. //处理特殊字符
  296. function htmlspecialcharsFix($input_str)
  297. {
  298. return $input_str;
  299. }
  300. //处理特殊字符
  301. function htmlspecialcharsAjaxFix($input_str)
  302. {
  303. return textUncode($input_str);
  304. }
  305. if(!function_exists('textDecode')) {
  306. function textDecode($str) {
  307. return htmlspecialchars_decode($str, ENT_QUOTES);
  308. }
  309. }
  310. if(!function_exists('htmlDecode')) {
  311. function htmlDecode($str) {
  312. return htmlspecialchars_decode($str, ENT_QUOTES);
  313. }
  314. }