SystemFileServices.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\exceptions\AuthException;
  17. use crmeb\services\CacheService;
  18. use crmeb\services\FileService as FileClass;
  19. use crmeb\services\FormBuilder as Form;
  20. use crmeb\utils\JwtAuth;
  21. use Firebase\JWT\ExpiredException;
  22. use think\facade\Log;
  23. use think\facade\Route as Url;
  24. /**
  25. * 文件校验
  26. * Class SystemFileServices
  27. * @package app\services\system\log
  28. */
  29. class SystemFileServices extends BaseServices
  30. {
  31. /**
  32. * 构造方法
  33. * SystemFileServices constructor.
  34. * @param SystemFileDao $dao
  35. */
  36. public function __construct(SystemFileDao $dao)
  37. {
  38. $this->dao = $dao;
  39. }
  40. /**
  41. * @param array $admin
  42. * @param string $password
  43. * @param string $type
  44. * @return array
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\DbException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. *
  49. * @date 2022/09/07
  50. * @author yyw
  51. */
  52. public function Login(string $password, string $type)
  53. {
  54. if (config('filesystem.password') !== $password) {
  55. throw new AdminException(400140);
  56. }
  57. $md5Password = md5($password);
  58. /** @var JwtAuth $jwtAuth */
  59. $jwtAuth = app()->make(JwtAuth::class);
  60. $tokenInfo = $jwtAuth->createToken($md5Password, $type, ['pwd' => $md5Password]);
  61. CacheService::set(md5($tokenInfo['token']), $tokenInfo['token'], 3600);
  62. return [
  63. 'token' => md5($tokenInfo['token']),
  64. 'expires_time' => $tokenInfo['params']['exp'],
  65. ];
  66. }
  67. /**
  68. * 获取Admin授权信息
  69. * @param string $token
  70. * @return bool
  71. * @throws \Psr\SimpleCache\InvalidArgumentException
  72. */
  73. public function parseToken(string $token): bool
  74. {
  75. /** @var CacheService $cacheService */
  76. $cacheService = app()->make(CacheService::class);
  77. if (!$token || $token === 'undefined') {
  78. throw new AuthException(110008);
  79. }
  80. /** @var JwtAuth $jwtAuth */
  81. $jwtAuth = app()->make(JwtAuth::class);
  82. //设置解析token
  83. [$id, $type, $pwd] = $jwtAuth->parseToken($token);
  84. //检测token是否过期
  85. $md5Token = md5($token);
  86. if (!$cacheService->has($md5Token) || !($cacheService->get($md5Token))) {
  87. throw new AuthException(110008);
  88. }
  89. //验证token
  90. try {
  91. $jwtAuth->verifyToken();
  92. } catch (\Throwable $e) {
  93. if (!request()->isCli()) {
  94. $cacheService->delete($md5Token);
  95. }
  96. throw new AuthException(110008);
  97. }
  98. if ($id !== md5(config('filesystem.password'))) {
  99. throw new AuthException(110008);
  100. }
  101. if ($pwd !== md5(config('filesystem.password'))) {
  102. throw new AuthException(110008);
  103. }
  104. return true;
  105. }
  106. /**
  107. * 获取文件校验列表
  108. * @return array
  109. * @throws \think\db\exception\DataNotFoundException
  110. * @throws \think\db\exception\DbException
  111. * @throws \think\db\exception\ModelNotFoundException
  112. */
  113. public function getFileList()
  114. {
  115. $rootPath = app()->getRootPath();
  116. $key = 'system_file_app_crmeb_public';
  117. $arr = CacheService::get(md5($key));
  118. if (!$arr) {
  119. $app = $this->getDir($rootPath . 'app');
  120. $extend = $this->getDir($rootPath . 'crmeb');
  121. $arr = array_merge($app, $extend);
  122. CacheService::set(md5($key), $arr, 3600 * 24);
  123. }
  124. $fileAll = [];//本地文件
  125. $cha = [];//不同的文件
  126. $len = strlen($rootPath);
  127. $file = $this->dao->getAll();//数据库中的文件
  128. if (empty($file)) {
  129. foreach ($arr as $k => $v) {
  130. $update_time = stat($v);
  131. $fileAll[$k]['cthash'] = md5_file($v);
  132. $fileAll[$k]['filename'] = substr($v, $len);
  133. $fileAll[$k]['atime'] = $update_time['atime'];
  134. $fileAll[$k]['mtime'] = $update_time['mtime'];
  135. $fileAll[$k]['ctime'] = $update_time['ctime'];
  136. }
  137. $data_num = array_chunk($fileAll, 100);
  138. $res = true;
  139. $res = $this->transaction(function () use ($data_num, $res) {
  140. foreach ($data_num as $k => $v) {
  141. $res = $res && $this->dao->saveAll($v);
  142. }
  143. return $res;
  144. });
  145. if ($res) {
  146. $cha = [];//不同的文件
  147. } else {
  148. $cha = $fileAll;
  149. }
  150. } else {
  151. $file = array_combine(array_column($file, 'filename'), $file);
  152. foreach ($arr as $ko => $vo) {
  153. $update_time = stat($vo);
  154. $cthash = md5_file($vo);
  155. $cha[] = [
  156. 'filename' => str_replace($rootPath, '', $vo),
  157. 'cthash' => $cthash,
  158. 'atime' => date('Y-m-d H:i:s', $update_time['atime']),
  159. 'mtime' => date('Y-m-d H:i:s', $update_time['mtime']),
  160. 'ctime' => date('Y-m-d H:i:s', $update_time['ctime']),
  161. 'type' => '新增的',
  162. ];
  163. if (isset($file[$vo]) && $file[$vo] != $cthash) {
  164. $cha[] = [
  165. 'type' => '已修改',
  166. ];
  167. unset($file[$vo]);
  168. }
  169. }
  170. foreach ($file as $k => $v) {
  171. $cha[] = [
  172. 'filename' => $v['filename'],
  173. 'cthash' => $v['cthash'],
  174. 'atime' => date('Y-m-d H:i:s', $v['atime']),
  175. 'mtime' => date('Y-m-d H:i:s', $v['mtime']),
  176. 'ctime' => date('Y-m-d H:i:s', $v['ctime']),
  177. 'type' => '已删除',
  178. ];
  179. }
  180. }
  181. $ctime = array_column($cha, 'ctime');
  182. array_multisort($ctime, SORT_DESC, $cha);
  183. return $cha;
  184. }
  185. /**
  186. * 获取文件夹中的文件 包括子文件
  187. * @param $dir
  188. * @return array
  189. */
  190. public function getDir($dir)
  191. {
  192. $data = [];
  193. $this->searchDir($dir, $data);
  194. return $data;
  195. }
  196. /**
  197. * 获取文件夹中的文件 包括子文件 不能直接用 直接使用 $this->getDir()方法 P156
  198. * @param $path
  199. * @param $data
  200. */
  201. public function searchDir($path, &$data)
  202. {
  203. if (is_dir($path) && !strpos($path, 'uploads')) {
  204. $files = scandir($path);
  205. foreach ($files as $file) {
  206. if ($file != '.' && $file != '..') {
  207. $this->searchDir($path . '/' . $file, $data);
  208. }
  209. }
  210. }
  211. if (is_file($path)) {
  212. $data[] = $path;
  213. }
  214. }
  215. //打开目录
  216. public function opendir($dir, $fileDir, $superior)
  217. {
  218. $markList = app()->make(SystemFileInfoServices::class)->getColumn([], 'mark', 'full_path');
  219. $fileAll = array('dir' => [], 'file' => []);
  220. //根目录
  221. $rootDir = $this->formatPath(app()->getRootPath());
  222. //防止查看站点以外的目录
  223. if (strpos($dir, $rootDir) === false || $dir == '') {
  224. $dir = $rootDir;
  225. }
  226. //判断是否是返回上级
  227. if ($superior) {
  228. if (strpos(dirname($dir), $rootDir) !== false) {
  229. $dir = dirname($dir);
  230. } else {
  231. $dir = $rootDir;
  232. }
  233. } else {
  234. $dir = $dir . '/' . $fileDir;
  235. }
  236. //获取目录列表
  237. $list = array_diff(scandir($dir), array('.', '..'));
  238. foreach ($list as $key => $v) {
  239. if (is_dir($dir . DS . $v)) {
  240. $fileAll['dir'][] = FileClass::listInfo($dir . DS . $v);
  241. }
  242. if (is_file($dir . DS . $v)) {
  243. $fileAll['file'][] = FileClass::listInfo($dir . DS . $v);
  244. }
  245. }
  246. //兼容windows
  247. $uname = php_uname('s');
  248. if (strstr($uname, 'Windows') !== false) {
  249. $dir = ltrim($dir, '\\');
  250. $rootDir = str_replace('\\', '\\\\', $rootDir);
  251. }
  252. $list = array_merge($fileAll['dir'], $fileAll['file']);
  253. $navList = [];
  254. foreach ($list as $key => $value) {
  255. $list[$key]['real_path'] = str_replace($rootDir, '', $value['pathname']);
  256. $list[$key]['mtime'] = date('Y-m-d H:i:s', $value['mtime']);
  257. $navList[$key]['title'] = $value['filename'];
  258. if ($value['isDir']) $navList[$key]['loading'] = false;
  259. $navList[$key]['children'] = [];
  260. $navList[$key]['path'] = $value['path'];
  261. $navList[$key]['isDir'] = $value['isDir'];
  262. $navList[$key]['isLeaf'] = !$value['isDir'];
  263. $navList[$key]['pathname'] = $value['pathname'];
  264. $navList[$key]['contextmenu'] = true;
  265. $list[$key]['mark'] = $markList[str_replace(root_path(), '/', $value['pathname'])] ?? '';
  266. $count = app()->make(SystemFileInfoServices::class)->count(['full_path' => $list[$key]['real_path']]);
  267. if (!$count) app()->make(SystemFileInfoServices::class)->save([
  268. 'name' => $value['filename'],
  269. 'path' => str_replace('/' . $value['filename'], '', $list[$key]['real_path']),
  270. 'full_path' => $list[$key]['real_path'],
  271. 'type' => $value['type'],
  272. 'create_time' => date('Y-m-d H:i:s', $value['ctime']),
  273. 'update_time' => date('Y-m-d H:i:s', time()),
  274. ]);
  275. }
  276. $routeList = [['key' => '根目录', 'route' => '']];
  277. $pathArray = explode('/', str_replace($rootDir, '', $dir));
  278. $str = '';
  279. foreach ($pathArray as $item) {
  280. if ($item) {
  281. $str = $str . '/' . $item;
  282. $routeList[] = ['key' => $item, 'route' => $rootDir . $str];
  283. }
  284. }
  285. return compact('dir', 'list', 'navList', 'routeList');
  286. }
  287. //读取文件
  288. public function openfile($filepath)
  289. {
  290. //根目录
  291. $rootDir = $this->formatPath(app()->getRootPath());
  292. //防止查看站点以外的文件
  293. if (strpos($filepath, $rootDir) === false || $filepath == '') {
  294. throw new AdminException('无法打开站点以外的文件');
  295. }
  296. $filepath = $this->formatPath($filepath);
  297. $content = FileClass::readFile($filepath);//防止页面内嵌textarea标签
  298. $ext = FileClass::getExt($filepath);
  299. $encoding = mb_detect_encoding($content, mb_detect_order());
  300. //前端组件支持的语言类型
  301. //['plaintext', 'json', 'abap', 'apex', 'azcli', 'bat', 'cameligo', 'clojure', 'coffeescript', 'c', 'cpp', 'csharp', 'csp', 'css', 'dart', 'dockerfile', 'fsharp', 'go', 'graphql', 'handlebars', 'hcl', 'html', 'ini', 'java', 'javascript', 'julia', 'kotlin', 'less', 'lexon', 'lua', 'markdown', 'mips', 'msdax', 'mysql', 'objective-c', 'pascal', 'pascaligo', 'perl', 'pgsql', 'php', 'postiats', 'powerquery', 'powershell', 'pug', 'python', 'r', 'razor', 'redis', 'redshift', 'restructuredtext', 'ruby', 'rust', 'sb', 'scala', 'scheme', 'scss', 'shell', 'sol', 'aes', 'sql', 'st', 'swift', 'systemverilog', 'verilog', 'tcl', 'twig', 'typescript', 'vb', 'xml', 'yaml']
  302. $extarray = [
  303. 'js' => 'javascript'
  304. , 'htm' => 'html'
  305. , 'shtml' => 'html'
  306. , 'html' => 'html'
  307. , 'xml' => 'xml'
  308. , 'php' => 'php'
  309. , 'sql' => 'mysql'
  310. , 'css' => 'css'
  311. , 'txt' => 'plaintext'
  312. , 'vue' => 'html'
  313. , 'json' => 'json'
  314. , 'lock' => 'json'
  315. , 'md' => 'markdown'
  316. , 'bat' => 'bat'
  317. , 'ini' => 'ini'
  318. ];
  319. $mode = empty($extarray[$ext]) ? 'php' : $extarray[$ext];
  320. return compact('content', 'mode', 'filepath', 'encoding');
  321. }
  322. //保存文件
  323. public function savefile($filepath, $comment)
  324. {
  325. $filepath = $this->formatPath($filepath);
  326. if (!FileClass::isWritable($filepath)) {
  327. throw new AdminException('请检查目录权限,需要给全部文件777WWW权限');
  328. }
  329. return FileClass::writeFile($filepath, $comment);
  330. }
  331. // 文件重命名
  332. public function rename($newname, $oldname)
  333. {
  334. if (($newname != $oldname) && is_writable($oldname)) {
  335. return rename($oldname, $newname);
  336. }
  337. return true;
  338. }
  339. /**
  340. * 删除文件或文件夹
  341. * @param string $path
  342. * @return bool
  343. *
  344. * @date 2022/09/20
  345. * @author yyw
  346. */
  347. public function delFolder(string $path)
  348. {
  349. $path = $this->formatPath($path);
  350. if (is_file($path)) {
  351. return unlink($path);
  352. }
  353. $dir = opendir($path);
  354. while ($fileName = readdir($dir)) {
  355. $file = $path . '/' . $fileName;
  356. if ($fileName != '.' && $fileName != '..') {
  357. if (is_dir($file)) {
  358. self::delFolder($file);
  359. } else {
  360. unlink($file);
  361. }
  362. }
  363. }
  364. closedir($dir);
  365. return rmdir($path);
  366. }
  367. /**
  368. * 新建文件夹
  369. * @param string $path
  370. * @param string $name
  371. * @param int $permissions
  372. * @return bool
  373. *
  374. * @date 2022/09/20
  375. * @author yyw
  376. */
  377. public function createFolder(string $path, string $name, int $permissions = 0755)
  378. {
  379. $path = $this->formatPath($path, $name);
  380. /** @var FileClass $fileClass */
  381. $fileClass = app()->make(FileClass::class);
  382. return $fileClass->createDir($path, $permissions);
  383. }
  384. /**
  385. * 新建文件
  386. * @param string $path
  387. * @param string $name
  388. * @return bool
  389. *
  390. * @date 2022/09/20
  391. * @author yyw
  392. */
  393. public function createFile(string $path, string $name)
  394. {
  395. $path = $this->formatPath($path, $name);
  396. /** @var FileClass $fileClass */
  397. $fileClass = app()->make(FileClass::class);
  398. return $fileClass->createFile($path);
  399. }
  400. public function copyFolder($surDir, $toDir)
  401. {
  402. return FileClass::copyDir($surDir, $toDir);
  403. }
  404. /**
  405. * 格式化路径
  406. * @param string $path
  407. * @param string $name
  408. * @return string
  409. *
  410. * @date 2022/09/20
  411. * @author yyw
  412. */
  413. public function formatPath(string $path = '', string $name = ''): string
  414. {
  415. if ($path) {
  416. $path = rtrim($path, DS);
  417. if ($name) $path = $path . DS . $name;
  418. $uname = php_uname('s');
  419. if (strstr($uname, 'Windows') !== false)
  420. $path = ltrim(str_replace('\\', '\\\\', $path), '.');
  421. }
  422. return $path;
  423. }
  424. /**
  425. * 文件备注表单
  426. * @param $path
  427. * @param $fileToken
  428. * @return array
  429. * @throws \FormBuilder\Exception\FormBuilderException
  430. * @author 吴汐
  431. * @email 442384644@qq.com
  432. * @date 2023/04/10
  433. */
  434. public function markForm($path, $fileToken)
  435. {
  436. $full_path = str_replace(root_path(), '/', $path);
  437. $mark = app()->make(SystemFileInfoServices::class)->value(['full_path' => str_replace(root_path(), '/', $path)], 'mark');
  438. $f = [];
  439. $f[] = Form::hidden('full_path', $full_path);
  440. $f[] = Form::input('mark', '文件备注', $mark);
  441. return create_form('文件备注', $f, Url::buildUrl('/system/file/mark/save?fileToken=' . $fileToken . '&type=mark'), 'POST');
  442. }
  443. /**
  444. * 保存文件备注
  445. * @param $full_path
  446. * @param $mark
  447. * @author 吴汐
  448. * @email 442384644@qq.com
  449. * @date 2023/04/10
  450. */
  451. public function fileMarkSave($full_path, $mark)
  452. {
  453. $res = app()->make(SystemFileInfoServices::class)->update(['full_path' => $full_path], ['mark' => $mark]);
  454. if (!$res) {
  455. throw new AdminException(100006);
  456. }
  457. }
  458. }