SystemFileServices.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\SystemFileDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. use crmeb\services\CacheService;
  16. use crmeb\services\FileService as FileClass;
  17. /**
  18. * 文件校验
  19. * Class SystemFileServices
  20. * @package app\services\system\log
  21. */
  22. class SystemFileServices extends BaseServices
  23. {
  24. /**
  25. * 构造方法
  26. * SystemFileServices constructor.
  27. * @param SystemFileDao $dao
  28. */
  29. public function __construct(SystemFileDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * 获取文件校验列表
  35. * @return array
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function getFileList()
  41. {
  42. $rootPath = app()->getRootPath();
  43. $key = 'system_file_app_crmeb_public';
  44. $arr = CacheService::get(md5($key));
  45. if (!$arr) {
  46. $app = $this->getDir($rootPath . 'app');
  47. $extend = $this->getDir($rootPath . 'crmeb');
  48. $arr = array_merge($app, $extend);
  49. CacheService::set(md5($key), $arr, 3600 * 24);
  50. }
  51. $fileAll = [];//本地文件
  52. $cha = [];//不同的文件
  53. $len = strlen($rootPath);
  54. $file = $this->dao->getAll();//数据库中的文件
  55. if (empty($file)) {
  56. foreach ($arr as $k => $v) {
  57. $update_time = stat($v);
  58. $fileAll[$k]['cthash'] = md5_file($v);
  59. $fileAll[$k]['filename'] = substr($v, $len);
  60. $fileAll[$k]['atime'] = $update_time['atime'];
  61. $fileAll[$k]['mtime'] = $update_time['mtime'];
  62. $fileAll[$k]['ctime'] = $update_time['ctime'];
  63. }
  64. $data_num = array_chunk($fileAll, 100);
  65. $res = true;
  66. $res = $this->transaction(function () use ($data_num, $res) {
  67. foreach ($data_num as $k => $v) {
  68. $res = $res && $this->dao->saveAll($v);
  69. }
  70. return $res;
  71. });
  72. if ($res) {
  73. $cha = [];//不同的文件
  74. } else {
  75. $cha = $fileAll;
  76. }
  77. } else {
  78. $file = array_combine(array_column($file, 'filename'), $file);
  79. foreach ($arr as $ko => $vo) {
  80. $update_time = stat($vo);
  81. $cthash = md5_file($vo);
  82. $cha[] = [
  83. 'filename' => $vo,
  84. 'cthash' => $cthash,
  85. 'atime' => date('Y-m-d H:i:s', $update_time['atime']),
  86. 'mtime' => date('Y-m-d H:i:s', $update_time['mtime']),
  87. 'ctime' => date('Y-m-d H:i:s', $update_time['ctime']),
  88. 'type' => '新增的',
  89. ];
  90. if (isset($file[$vo]) && $file[$vo] != $cthash) {
  91. $cha[] = [
  92. 'type' => '已修改',
  93. ];
  94. unset($file[$vo]);
  95. }
  96. }
  97. foreach ($file as $k => $v) {
  98. $cha[] = [
  99. 'filename' => $v['filename'],
  100. 'cthash' => $v['cthash'],
  101. 'atime' => date('Y-m-d H:i:s', $v['atime']),
  102. 'mtime' => date('Y-m-d H:i:s', $v['mtime']),
  103. 'ctime' => date('Y-m-d H:i:s', $v['ctime']),
  104. 'type' => '已删除',
  105. ];
  106. }
  107. }
  108. $ctime = array_column($cha, 'ctime');
  109. array_multisort($ctime, SORT_DESC, $cha);
  110. return $cha;
  111. }
  112. /**
  113. * 获取文件夹中的文件 包括子文件
  114. * @param $dir
  115. * @return array
  116. */
  117. public function getDir($dir)
  118. {
  119. $data = [];
  120. $this->searchDir($dir, $data);
  121. return $data;
  122. }
  123. /**
  124. * 获取文件夹中的文件 包括子文件 不能直接用 直接使用 $this->getDir()方法 P156
  125. * @param $path
  126. * @param $data
  127. */
  128. public function searchDir($path, &$data)
  129. {
  130. if (is_dir($path) && !strpos($path, 'uploads')) {
  131. $files = scandir($path);
  132. foreach ($files as $file) {
  133. if ($file != '.' && $file != '..') {
  134. $this->searchDir($path . '/' . $file, $data);
  135. }
  136. }
  137. }
  138. if (is_file($path)) {
  139. $data[] = $path;
  140. }
  141. }
  142. //打开目录
  143. public function opendir()
  144. {
  145. $fileAll = array('dir' => [], 'file' => []);
  146. //根目录
  147. $rootdir = app()->getRootPath();
  148. // return $rootdir;
  149. //当前目录
  150. $request_dir = app('request')->param('dir');
  151. //防止查看站点以外的目录
  152. if (strpos($request_dir, $rootdir) === false) {
  153. $request_dir = $rootdir;
  154. }
  155. //判断是否是返回上级
  156. if (app('request')->param('superior') && !empty($request_dir)) {
  157. if (strpos(dirname($request_dir), $rootdir) !== false) {
  158. $dir = dirname($request_dir);
  159. } else {
  160. $dir = $rootdir;
  161. }
  162. } else {
  163. $dir = !empty($request_dir) ? $request_dir : $rootdir;
  164. $dir = rtrim($dir, DS) . DS . app('request')->param('filedir');
  165. }
  166. $list = scandir($dir);
  167. foreach ($list as $key => $v) {
  168. if ($v != '.' && $v != '..') {
  169. if (is_dir($dir . DS . $v)) {
  170. $fileAll['dir'][] = FileClass::listInfo($dir . DS . $v);
  171. }
  172. if (is_file($dir . DS . $v)) {
  173. $fileAll['file'][] = FileClass::listInfo($dir . DS . $v);
  174. }
  175. }
  176. }
  177. //兼容windows
  178. $uname = php_uname('s');
  179. if (strstr($uname, 'Windows') !== false) {
  180. $dir = ltrim($dir, '\\');
  181. $rootdir = str_replace('\\', '\\\\', $rootdir);
  182. }
  183. $list = array_merge($fileAll['dir'], $fileAll['file']);
  184. foreach ($list as $key => $value) {
  185. $list[$key]['real_path'] = str_replace($rootdir, '', $value['pathname']);
  186. $list[$key]['mtime'] = date('Y-m-d H:i:s', $value['mtime']);
  187. }
  188. return compact('dir', 'list');
  189. }
  190. //读取文件
  191. public function openfile($filepath)
  192. {
  193. $content = FileClass::readFile($filepath);//防止页面内嵌textarea标签
  194. $ext = FileClass::getExt($filepath);
  195. $extarray = [
  196. 'js' => 'text/javascript'
  197. , 'php' => 'text/x-php'
  198. , 'html' => 'text/html'
  199. , 'sql' => 'text/x-mysql'
  200. , 'css' => 'text/x-scss'];
  201. $mode = empty($extarray[$ext]) ? '' : $extarray[$ext];
  202. return compact('content', 'mode', 'filepath');
  203. }
  204. //保存文件
  205. public function savefile($filepath,$comment)
  206. {
  207. //兼容windows
  208. $uname = php_uname('s');
  209. if (strstr($uname, 'Windows') !== false)
  210. $filepath = ltrim(str_replace('/', DS, $filepath), '.');
  211. if (!FileClass::isWritable($filepath)) {
  212. throw new AdminException('没有权限');
  213. }
  214. return FileClass::writeFile($filepath, $comment);
  215. }
  216. }