SystemFileMd5Services.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system\log;
  12. use app\dao\system\log\SystemFileMd5Dao;
  13. use app\services\BaseServices;
  14. use think\facade\Config;
  15. use think\facade\Db;
  16. class SystemFileMd5Services extends BaseServices
  17. {
  18. public function __construct(SystemFileMd5Dao $dao)
  19. {
  20. $this->dao = $dao;
  21. }
  22. public function saveMd5List(array $list)
  23. {
  24. if (empty($list)) return true;
  25. return (bool)$this->transaction(function () use ($list) {
  26. return $this->dao->saveAll($list);
  27. });
  28. }
  29. public function clearMd5List()
  30. {
  31. $prefix = Config::get('database.connections.' . Config::get('database.default') . '.prefix');
  32. Db::execute('TRUNCATE TABLE `' . $prefix . 'system_file_md5`');
  33. }
  34. public function checkFile()
  35. {
  36. $rootPath = app()->getRootPath();
  37. $files = array_merge(
  38. $this->getDir($rootPath . 'app'),
  39. $this->getDir($rootPath . 'crmeb')
  40. );
  41. // 只找 .php 文件
  42. $files = array_filter($files, function ($path) {
  43. return pathinfo($path, PATHINFO_EXTENSION) === 'php';
  44. });
  45. $len = strlen($rootPath);
  46. // 当前列表
  47. $list = $this->dao->getColumn([], 'md5', 'filename');
  48. $currentList = [];
  49. foreach ($files as $path) {
  50. $currentList[substr($path, $len)] = md5_file($path);
  51. }
  52. // 忽略app/adminapi/controller/UpgradeController.php文件的改动
  53. unset($currentList['app/adminapi/controller/UpgradeController.php']);
  54. unset($list['app/adminapi/controller/UpgradeController.php']);
  55. $diffFiles = [];
  56. $newFiles = array_keys(array_diff_key($currentList, $list));
  57. $delFiles = array_keys(array_diff_key($list, $currentList));
  58. $changedFiles = array_keys(array_diff_assoc($currentList, $list));
  59. return array_values(array_unique(array_merge($newFiles, $delFiles, $changedFiles)));
  60. }
  61. public function getDir($dir)
  62. {
  63. $data = [];
  64. $this->searchDir($dir, $data);
  65. return $data;
  66. }
  67. public function searchDir($path, &$data)
  68. {
  69. if (is_dir($path) && !strpos($path, 'uploads')) {
  70. $files = scandir($path);
  71. foreach ($files as $file) {
  72. if ($file != '.' && $file != '..') {
  73. $this->searchDir($path . '/' . $file, $data);
  74. }
  75. }
  76. }
  77. if (is_file($path)) {
  78. $data[] = $path;
  79. }
  80. }
  81. }