conn.php 8.5 KB

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