SystemFileServices.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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 app\services\system\admin\SystemAdminServices;
  15. use crmeb\exceptions\AdminException;
  16. use crmeb\services\CacheService;
  17. use crmeb\services\FileService as FileClass;
  18. /**
  19. * 文件校验
  20. * Class SystemFileServices
  21. * @package app\services\system\log
  22. */
  23. class SystemFileServices extends BaseServices
  24. {
  25. /**
  26. * 构造方法
  27. * SystemFileServices constructor.
  28. * @param SystemFileDao $dao
  29. */
  30. public function __construct(SystemFileDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * @param array $admin
  36. * @param string $password
  37. * @param string $type
  38. * @return array
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. *
  43. * @date 2022/09/07
  44. * @author yyw
  45. */
  46. public function Login($account, string $password, string $type)
  47. {
  48. /** @var SystemAdminServices $adminServer */
  49. $adminServer = app()->make(SystemAdminServices::class);
  50. $adminInfo = $adminServer->verifyFileLogin($account, $password);
  51. $tokenInfo = $this->createToken($adminInfo->id, $type,$adminInfo->pwd);
  52. return [
  53. 'token' => $tokenInfo['token'],
  54. 'expires_time' => $tokenInfo['params']['exp'],
  55. ];
  56. }
  57. /**
  58. * 获取文件校验列表
  59. * @return array
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. */
  64. public function getFileList()
  65. {
  66. $rootPath = app()->getRootPath();
  67. $key = 'system_file_app_crmeb_public';
  68. $arr = CacheService::get(md5($key));
  69. if (!$arr) {
  70. $app = $this->getDir($rootPath . 'app');
  71. $extend = $this->getDir($rootPath . 'crmeb');
  72. $arr = array_merge($app, $extend);
  73. CacheService::set(md5($key), $arr, 3600 * 24);
  74. }
  75. $fileAll = [];//本地文件
  76. $cha = [];//不同的文件
  77. $len = strlen($rootPath);
  78. $file = $this->dao->getAll();//数据库中的文件
  79. if (empty($file)) {
  80. foreach ($arr as $k => $v) {
  81. $update_time = stat($v);
  82. $fileAll[$k]['cthash'] = md5_file($v);
  83. $fileAll[$k]['filename'] = substr($v, $len);
  84. $fileAll[$k]['atime'] = $update_time['atime'];
  85. $fileAll[$k]['mtime'] = $update_time['mtime'];
  86. $fileAll[$k]['ctime'] = $update_time['ctime'];
  87. }
  88. $data_num = array_chunk($fileAll, 100);
  89. $res = true;
  90. $res = $this->transaction(function () use ($data_num, $res) {
  91. foreach ($data_num as $k => $v) {
  92. $res = $res && $this->dao->saveAll($v);
  93. }
  94. return $res;
  95. });
  96. if ($res) {
  97. $cha = [];//不同的文件
  98. } else {
  99. $cha = $fileAll;
  100. }
  101. } else {
  102. $file = array_combine(array_column($file, 'filename'), $file);
  103. foreach ($arr as $ko => $vo) {
  104. $update_time = stat($vo);
  105. $cthash = md5_file($vo);
  106. $cha[] = [
  107. 'filename' => $vo,
  108. 'cthash' => $cthash,
  109. 'atime' => date('Y-m-d H:i:s', $update_time['atime']),
  110. 'mtime' => date('Y-m-d H:i:s', $update_time['mtime']),
  111. 'ctime' => date('Y-m-d H:i:s', $update_time['ctime']),
  112. 'type' => '新增的',
  113. ];
  114. if (isset($file[$vo]) && $file[$vo] != $cthash) {
  115. $cha[] = [
  116. 'type' => '已修改',
  117. ];
  118. unset($file[$vo]);
  119. }
  120. }
  121. foreach ($file as $k => $v) {
  122. $cha[] = [
  123. 'filename' => $v['filename'],
  124. 'cthash' => $v['cthash'],
  125. 'atime' => date('Y-m-d H:i:s', $v['atime']),
  126. 'mtime' => date('Y-m-d H:i:s', $v['mtime']),
  127. 'ctime' => date('Y-m-d H:i:s', $v['ctime']),
  128. 'type' => '已删除',
  129. ];
  130. }
  131. }
  132. $ctime = array_column($cha, 'ctime');
  133. array_multisort($ctime, SORT_DESC, $cha);
  134. return $cha;
  135. }
  136. /**
  137. * 获取文件夹中的文件 包括子文件
  138. * @param $dir
  139. * @return array
  140. */
  141. public function getDir($dir)
  142. {
  143. $data = [];
  144. $this->searchDir($dir, $data);
  145. return $data;
  146. }
  147. /**
  148. * 获取文件夹中的文件 包括子文件 不能直接用 直接使用 $this->getDir()方法 P156
  149. * @param $path
  150. * @param $data
  151. */
  152. public function searchDir($path, &$data)
  153. {
  154. if (is_dir($path) && !strpos($path, 'uploads')) {
  155. $files = scandir($path);
  156. foreach ($files as $file) {
  157. if ($file != '.' && $file != '..') {
  158. $this->searchDir($path . '/' . $file, $data);
  159. }
  160. }
  161. }
  162. if (is_file($path)) {
  163. $data[] = $path;
  164. }
  165. }
  166. //打开目录
  167. public function opendir()
  168. {
  169. $fileAll = array('dir' => [], 'file' => []);
  170. //根目录
  171. $rootdir = app()->getRootPath();
  172. // return $rootdir;
  173. //当前目录
  174. $request_dir = app('request')->param('dir');
  175. //防止查看站点以外的目录
  176. if (strpos($request_dir, $rootdir) === false) {
  177. $request_dir = $rootdir;
  178. }
  179. //判断是否是返回上级
  180. if (app('request')->param('superior') && !empty($request_dir)) {
  181. if (strpos(dirname($request_dir), $rootdir) !== false) {
  182. $dir = dirname($request_dir);
  183. } else {
  184. $dir = $rootdir;
  185. }
  186. } else {
  187. $dir = !empty($request_dir) ? $request_dir : $rootdir;
  188. $dir = rtrim($dir, DS) . DS . app('request')->param('filedir');
  189. }
  190. $list = scandir($dir);
  191. foreach ($list as $key => $v) {
  192. if ($v != '.' && $v != '..') {
  193. if (is_dir($dir . DS . $v)) {
  194. $fileAll['dir'][] = FileClass::listInfo($dir . DS . $v);
  195. }
  196. if (is_file($dir . DS . $v)) {
  197. $fileAll['file'][] = FileClass::listInfo($dir . DS . $v);
  198. }
  199. }
  200. }
  201. //兼容windows
  202. $uname = php_uname('s');
  203. if (strstr($uname, 'Windows') !== false) {
  204. $dir = ltrim($dir, '\\');
  205. $rootdir = str_replace('\\', '\\\\', $rootdir);
  206. }
  207. $list = array_merge($fileAll['dir'], $fileAll['file']);
  208. $navList = [];
  209. foreach ($list as $key => $value) {
  210. $list[$key]['real_path'] = str_replace($rootdir, '', $value['pathname']);
  211. $list[$key]['mtime'] = date('Y-m-d H:i:s', $value['mtime']);
  212. $navList[$key]['title'] = $value['filename'];
  213. if($value['isDir']) $navList[$key]['loading'] = false;
  214. $navList[$key]['children'] = [];
  215. $navList[$key]['path'] = $value['path'];
  216. $navList[$key]['isDir'] = $value['isDir'];
  217. $navList[$key]['pathname'] = $value['pathname'];
  218. }
  219. return compact('dir', 'list','navList');
  220. }
  221. //读取文件
  222. public function openfile($filepath)
  223. {
  224. $content = FileClass::readFile($filepath);//防止页面内嵌textarea标签
  225. $ext = FileClass::getExt($filepath);
  226. $extarray = [
  227. 'js' => 'text/javascript'
  228. , 'htm' => 'text/html'
  229. , 'shtml' => 'text/html'
  230. , 'xml' => 'text/xml'
  231. , 'php' => 'text/x-php'
  232. , 'html' => 'text/html'
  233. , 'sql' => 'text/x-mysql'
  234. , 'css' => 'text/x-scss'
  235. , 'txt'=>'text/plain'
  236. ];
  237. $mode = empty($extarray[$ext]) ? 'text/plain' : $extarray[$ext];
  238. return compact('content', 'mode', 'filepath');
  239. }
  240. //保存文件
  241. public function savefile($filepath,$comment)
  242. {
  243. //兼容windows
  244. $uname = php_uname('s');
  245. if (strstr($uname, 'Windows') !== false)
  246. $filepath = ltrim(str_replace('/', DS, $filepath), '.');
  247. if (!FileClass::isWritable($filepath)) {
  248. throw new AdminException(400611);
  249. }
  250. return FileClass::writeFile($filepath, $comment);
  251. }
  252. public function delFolder(string $path)
  253. {
  254. if (!file_exists($path)) {
  255. return false;
  256. }
  257. if(is_file($path))
  258. {
  259. return unlink($path);
  260. }
  261. $dir = opendir($path);
  262. while ($fileName = readdir($dir)) {
  263. $file = $path . '/' . $fileName;
  264. if ($fileName != '.' && $fileName != '..') {
  265. if (is_dir($file)) {
  266. self::delDir($file);
  267. } else {
  268. unlink($file);
  269. }
  270. }
  271. }
  272. closedir($dir);
  273. return rmdir($path);
  274. }
  275. public function createFolder(string $path, int $permissions = 0755)
  276. {
  277. /** @var FileClass $fileClass */
  278. $fileClass = app()->make(FileClass::class);
  279. return $fileClass->createDir($path,$permissions);
  280. }
  281. public function createFile(string $path)
  282. {
  283. /** @var FileClass $fileClass */
  284. $fileClass = app()->make(FileClass::class);
  285. return $fileClass->createFile($path);
  286. }
  287. public function copyFolder($surDir,$toDir)
  288. {
  289. return FileClass::copyDir($surDir,$toDir);
  290. }
  291. }