common.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. //程序版本号,请勿修改
  3. const app_version = '2.4.3';
  4. //程序内部更新版本代码,请勿修改
  5. const app_version_code = 243;
  6. // 应用公共文件
  7. function validateEmail($email): bool
  8. {
  9. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  10. return false;
  11. } else {
  12. return true;
  13. }
  14. }
  15. function uuid(): string
  16. {
  17. $chars = md5(uniqid(mt_rand(), true));
  18. return substr($chars, 0, 8) . '-'
  19. . substr($chars, 8, 4) . '-'
  20. . substr($chars, 12, 4) . '-'
  21. . substr($chars, 16, 4) . '-'
  22. . substr($chars, 20, 12);
  23. }
  24. function renderToken($t = 'tab'): string
  25. {
  26. $s = uuid() . strval(time()) . $t;
  27. return md5($s);
  28. }
  29. function joinPath($path1, $path2='')
  30. {
  31. return preg_replace("#/+/#", "/", $path1 . $path2);
  32. }
  33. function getRealIp(): string
  34. {
  35. $ip1 = request()->header('x-forwarded-for', false);
  36. if ($ip1) {
  37. $arr = explode(",", $ip1);
  38. if (count($arr) > 0) {
  39. return trim($arr[0]);
  40. }
  41. }
  42. return request()->ip();
  43. }
  44. function plugins_path($path = ''): string
  45. {
  46. if (mb_strlen($path) > 0) {
  47. if (strpos($path, "/") == 0) {
  48. return $_ENV['plugins_dir_name'] . $path;
  49. }
  50. return $_ENV['plugins_dir_name'] . '/' . $path;
  51. }
  52. return $_ENV['plugins_dir_name'] . "/";
  53. }
  54. function is_demo_mode($is_exit = false)
  55. {
  56. if (env('demo_mode')) {
  57. if ($is_exit) {
  58. json(["msg" => "演示模式,部分功能受限,禁止更新或删除!", "code" => 0])->send();
  59. exit();
  60. }
  61. return true;
  62. }
  63. return false;
  64. }
  65. function modifyImageUrls($htmlContent, $newBaseUrl): string
  66. {
  67. try {
  68. $dom = new DOMDocument();
  69. $htmlContent = mb_convert_encoding($htmlContent, 'HTML-ENTITIES', 'UTF-8');
  70. libxml_use_internal_errors(true);
  71. $wrappedContent = '<div>' . $htmlContent . '</div>';
  72. $dom->loadHTML($wrappedContent, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
  73. $images = $dom->getElementsByTagName('img');
  74. foreach ($images as $img) {
  75. $oldSrc = $img->getAttribute('src');
  76. if (!preg_match('/^http/', $oldSrc)) {
  77. $newSrc = $newBaseUrl . $oldSrc;
  78. $img->setAttribute('src', $newSrc);
  79. }
  80. }
  81. // 返回修改后的 HTML,去掉根节点
  82. $newHtmlContent = '';
  83. foreach ($dom->documentElement->childNodes as $child) {
  84. $newHtmlContent .= $dom->saveHTML($child);
  85. }
  86. return $newHtmlContent;
  87. } catch (Exception $e) {
  88. return $htmlContent;
  89. }
  90. }
  91. function removeImagesUrls($htmlContent, $newBaseUrl)
  92. {
  93. try {
  94. $dom = new DOMDocument();
  95. $htmlContent = mb_convert_encoding($htmlContent, 'HTML-ENTITIES', 'UTF-8');
  96. libxml_use_internal_errors(true);
  97. $wrappedContent = '<div>' . $htmlContent . '</div>';
  98. $dom->loadHTML($wrappedContent, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
  99. $domain = $newBaseUrl;
  100. $images = $dom->getElementsByTagName('img');
  101. foreach ($images as $img) {
  102. $oldSrc = $img->getAttribute('src');
  103. $newSrc = str_replace($domain, '', $oldSrc);
  104. $img->setAttribute('src', $newSrc);
  105. }
  106. // 返回修改后的 HTML,去掉根节点
  107. $newHtmlContent = '';
  108. foreach ($dom->documentElement->childNodes as $child) {
  109. $newHtmlContent .= $dom->saveHTML($child);
  110. }
  111. return $newHtmlContent;
  112. } catch (Exception $e) {
  113. return $htmlContent;
  114. }
  115. }