common.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. // 应用公共文件
  3. function validateEmail($email): bool
  4. {
  5. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  6. return false;
  7. } else {
  8. return true;
  9. }
  10. }
  11. function uuid(): string
  12. {
  13. $chars = md5(uniqid(mt_rand(), true));
  14. return substr($chars, 0, 8) . '-'
  15. . substr($chars, 8, 4) . '-'
  16. . substr($chars, 12, 4) . '-'
  17. . substr($chars, 16, 4) . '-'
  18. . substr($chars, 20, 12);
  19. }
  20. function renderToken($t = 'tab'): string
  21. {
  22. $s = uuid() . strval(time()) . $t;
  23. return md5($s);
  24. }
  25. function joinPath($path1, $path2)
  26. {
  27. return preg_replace("#//#", "/", $path1 . $path2);
  28. }
  29. function getRealIp(): string
  30. {
  31. $ip1 = request()->header('x-forwarded-for', false);
  32. if ($ip1) {
  33. $arr = explode(",", $ip1);
  34. if(count($arr)>0){
  35. return trim($arr[0]);
  36. }
  37. }
  38. return request()->ip();
  39. }
  40. function plugins_path($path=''): string
  41. {
  42. if (mb_strlen($path) > 0) {
  43. if (strpos($path, "/") == 0) {
  44. return $_ENV['plugins_dir_name'] . $path;
  45. }
  46. return $_ENV['plugins_dir_name'] . '/' . $path;
  47. }
  48. return $_ENV['plugins_dir_name'] . "/";
  49. }
  50. function is_demo_mode($is_exit = false)
  51. {
  52. if (env('demo_mode')) {
  53. if ($is_exit) {
  54. json(["msg" => "演示模式,部分功能受限,禁止更新或删除!", "code" => 0])->send();
  55. exit();
  56. }
  57. return true;
  58. }
  59. return false;
  60. }