SystemFileServices.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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\exceptions\AuthException;
  17. use crmeb\services\CacheService;
  18. use crmeb\services\FileService as FileClass;
  19. use crmeb\utils\JwtAuth;
  20. use Firebase\JWT\ExpiredException;
  21. use think\facade\Log;
  22. /**
  23. * 文件校验
  24. * Class SystemFileServices
  25. * @package app\services\system\log
  26. */
  27. class SystemFileServices extends BaseServices
  28. {
  29. /**
  30. * 构造方法
  31. * SystemFileServices constructor.
  32. * @param SystemFileDao $dao
  33. */
  34. public function __construct(SystemFileDao $dao)
  35. {
  36. $this->dao = $dao;
  37. }
  38. /**
  39. * @param array $admin
  40. * @param string $password
  41. * @param string $type
  42. * @return array
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\DbException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. *
  47. * @date 2022/09/07
  48. * @author yyw
  49. */
  50. public function Login(string $password, string $type)
  51. {
  52. if (config('filesystem.password') !== $password) {
  53. throw new AdminException(400140);
  54. }
  55. $md5Password = md5($password);
  56. /** @var JwtAuth $jwtAuth */
  57. $jwtAuth = app()->make(JwtAuth::class);
  58. $tokenInfo = $jwtAuth->createToken($md5Password, $type, ['pwd' => $md5Password]);
  59. CacheService::set(md5($tokenInfo['token']), $tokenInfo['token'], 3600);
  60. return [
  61. 'token' => md5($tokenInfo['token']),
  62. 'expires_time' => $tokenInfo['params']['exp'],
  63. ];
  64. }
  65. /**
  66. * 获取Admin授权信息
  67. * @param string $token
  68. * @return bool
  69. * @throws \Psr\SimpleCache\InvalidArgumentException
  70. */
  71. public function parseToken(string $token): bool
  72. {
  73. /** @var CacheService $cacheService */
  74. $cacheService = app()->make(CacheService::class);
  75. if (!$token || $token === 'undefined') {
  76. throw new AuthException(110008);
  77. }
  78. /** @var JwtAuth $jwtAuth */
  79. $jwtAuth = app()->make(JwtAuth::class);
  80. //设置解析token
  81. [$id, $type, $pwd] = $jwtAuth->parseToken($token);
  82. //检测token是否过期
  83. $md5Token = md5($token);
  84. if (!$cacheService->hasToken($md5Token) || !($cacheToken = $cacheService->getTokenBucket($md5Token))) {
  85. throw new AuthException(110008);
  86. }
  87. //是否超出有效次数
  88. if (isset($cacheToken['invalidNum']) && $cacheToken['invalidNum'] >= 3) {
  89. if (!request()->isCli()) {
  90. $cacheService->clearToken($md5Token);
  91. }
  92. throw new AuthException(110008);
  93. }
  94. //验证token
  95. try {
  96. $jwtAuth->verifyToken();
  97. $cacheService->setTokenBucket($md5Token, $cacheToken, $cacheToken['exp']);
  98. } catch (ExpiredException $e) {
  99. $cacheToken['invalidNum'] = isset($cacheToken['invalidNum']) ? $cacheToken['invalidNum']++ : 1;
  100. $cacheService->setTokenBucket($md5Token, $cacheToken, $cacheToken['exp']);
  101. } catch (\Throwable $e) {
  102. if (!request()->isCli()) {
  103. $cacheService->clearToken($md5Token);
  104. }
  105. throw new AuthException(110008);
  106. }
  107. if ($id !== md5(config('filesystem.password'))) {
  108. throw new AuthException(110008);
  109. }
  110. if ($pwd !== md5(config('filesystem.password'))) {
  111. throw new AuthException(110008);
  112. }
  113. return true;
  114. }
  115. /**
  116. * 获取文件校验列表
  117. * @return array
  118. * @throws \think\db\exception\DataNotFoundException
  119. * @throws \think\db\exception\DbException
  120. * @throws \think\db\exception\ModelNotFoundException
  121. */
  122. public function getFileList()
  123. {
  124. $rootPath = app()->getRootPath();
  125. $key = 'system_file_app_crmeb_public';
  126. $arr = CacheService::get(md5($key));
  127. if (!$arr) {
  128. $app = $this->getDir($rootPath . 'app');
  129. $extend = $this->getDir($rootPath . 'crmeb');
  130. $arr = array_merge($app, $extend);
  131. CacheService::set(md5($key), $arr, 3600 * 24);
  132. }
  133. $fileAll = [];//本地文件
  134. $cha = [];//不同的文件
  135. $len = strlen($rootPath);
  136. $file = $this->dao->getAll();//数据库中的文件
  137. if (empty($file)) {
  138. foreach ($arr as $k => $v) {
  139. $update_time = stat($v);
  140. $fileAll[$k]['cthash'] = md5_file($v);
  141. $fileAll[$k]['filename'] = substr($v, $len);
  142. $fileAll[$k]['atime'] = $update_time['atime'];
  143. $fileAll[$k]['mtime'] = $update_time['mtime'];
  144. $fileAll[$k]['ctime'] = $update_time['ctime'];
  145. }
  146. $data_num = array_chunk($fileAll, 100);
  147. $res = true;
  148. $res = $this->transaction(function () use ($data_num, $res) {
  149. foreach ($data_num as $k => $v) {
  150. $res = $res && $this->dao->saveAll($v);
  151. }
  152. return $res;
  153. });
  154. if ($res) {
  155. $cha = [];//不同的文件
  156. } else {
  157. $cha = $fileAll;
  158. }
  159. } else {
  160. $file = array_combine(array_column($file, 'filename'), $file);
  161. foreach ($arr as $ko => $vo) {
  162. $update_time = stat($vo);
  163. $cthash = md5_file($vo);
  164. $cha[] = [
  165. 'filename' => $vo,
  166. 'cthash' => $cthash,
  167. 'atime' => date('Y-m-d H:i:s', $update_time['atime']),
  168. 'mtime' => date('Y-m-d H:i:s', $update_time['mtime']),
  169. 'ctime' => date('Y-m-d H:i:s', $update_time['ctime']),
  170. 'type' => '新增的',
  171. ];
  172. if (isset($file[$vo]) && $file[$vo] != $cthash) {
  173. $cha[] = [
  174. 'type' => '已修改',
  175. ];
  176. unset($file[$vo]);
  177. }
  178. }
  179. foreach ($file as $k => $v) {
  180. $cha[] = [
  181. 'filename' => $v['filename'],
  182. 'cthash' => $v['cthash'],
  183. 'atime' => date('Y-m-d H:i:s', $v['atime']),
  184. 'mtime' => date('Y-m-d H:i:s', $v['mtime']),
  185. 'ctime' => date('Y-m-d H:i:s', $v['ctime']),
  186. 'type' => '已删除',
  187. ];
  188. }
  189. }
  190. $ctime = array_column($cha, 'ctime');
  191. array_multisort($ctime, SORT_DESC, $cha);
  192. return $cha;
  193. }
  194. /**
  195. * 获取文件夹中的文件 包括子文件
  196. * @param $dir
  197. * @return array
  198. */
  199. public function getDir($dir)
  200. {
  201. $data = [];
  202. $this->searchDir($dir, $data);
  203. return $data;
  204. }
  205. /**
  206. * 获取文件夹中的文件 包括子文件 不能直接用 直接使用 $this->getDir()方法 P156
  207. * @param $path
  208. * @param $data
  209. */
  210. public function searchDir($path, &$data)
  211. {
  212. if (is_dir($path) && !strpos($path, 'uploads')) {
  213. $files = scandir($path);
  214. foreach ($files as $file) {
  215. if ($file != '.' && $file != '..') {
  216. $this->searchDir($path . '/' . $file, $data);
  217. }
  218. }
  219. }
  220. if (is_file($path)) {
  221. $data[] = $path;
  222. }
  223. }
  224. //打开目录
  225. public function opendir()
  226. {
  227. $fileAll = array('dir' => [], 'file' => []);
  228. //根目录
  229. $rootdir = $this->formatPath(app()->getRootPath());
  230. // return $rootdir;
  231. //当前目录
  232. $request_dir = app('request')->param('dir');
  233. //防止查看站点以外的目录
  234. if (strpos($request_dir, $rootdir) === false) {
  235. $request_dir = $rootdir;
  236. }
  237. //判断是否是返回上级
  238. if (app('request')->param('superior') && !empty($request_dir)) {
  239. if (strpos(dirname($request_dir), $rootdir) !== false) {
  240. $dir = dirname($request_dir);
  241. } else {
  242. $dir = $rootdir;
  243. }
  244. } else {
  245. $dir = !empty($request_dir) ? $request_dir : $rootdir;
  246. $dir = rtrim($dir, DS) . DS . app('request')->param('filedir');
  247. }
  248. $list = scandir($dir);
  249. foreach ($list as $key => $v) {
  250. if ($v != '.' && $v != '..') {
  251. if (is_dir($dir . DS . $v)) {
  252. $fileAll['dir'][] = FileClass::listInfo($dir . DS . $v);
  253. }
  254. if (is_file($dir . DS . $v)) {
  255. $fileAll['file'][] = FileClass::listInfo($dir . DS . $v);
  256. }
  257. }
  258. }
  259. //兼容windows
  260. $uname = php_uname('s');
  261. if (strstr($uname, 'Windows') !== false) {
  262. $dir = ltrim($dir, '\\');
  263. $rootdir = str_replace('\\', '\\\\', $rootdir);
  264. }
  265. $list = array_merge($fileAll['dir'], $fileAll['file']);
  266. $navList = [];
  267. foreach ($list as $key => $value) {
  268. $list[$key]['real_path'] = str_replace($rootdir, '', $value['pathname']);
  269. $list[$key]['mtime'] = date('Y-m-d H:i:s', $value['mtime']);
  270. $navList[$key]['title'] = $value['filename'];
  271. if ($value['isDir']) $navList[$key]['loading'] = false;
  272. $navList[$key]['children'] = [];
  273. $navList[$key]['path'] = $value['path'];
  274. $navList[$key]['isDir'] = $value['isDir'];
  275. $navList[$key]['pathname'] = $value['pathname'];
  276. $navList[$key]['contextmenu'] = true;
  277. }
  278. return compact('dir', 'list', 'navList');
  279. }
  280. //读取文件
  281. public function openfile($filepath)
  282. {
  283. $filepath = $this->formatPath($filepath);
  284. $content = FileClass::readFile($filepath);//防止页面内嵌textarea标签
  285. $ext = FileClass::getExt($filepath);
  286. $encoding = mb_detect_encoding($content, mb_detect_order());
  287. //前端组件支持的语言类型
  288. //['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']
  289. $extarray = [
  290. 'js' => 'javascript'
  291. , 'htm' => 'html'
  292. , 'shtml' => 'html'
  293. , 'html' => 'html'
  294. , 'xml' => 'xml'
  295. , 'php' => 'php'
  296. , 'sql' => 'mysql'
  297. , 'css' => 'css'
  298. , 'txt' => 'plaintext'
  299. , 'vue' => 'html'
  300. , 'json' => 'json'
  301. , 'lock' => 'json'
  302. , 'md' => 'markdown'
  303. , 'bat' => 'bat'
  304. , 'ini' => 'ini'
  305. ];
  306. $mode = empty($extarray[$ext]) ? 'php' : $extarray[$ext];
  307. return compact('content', 'mode', 'filepath', 'encoding');
  308. }
  309. //保存文件
  310. public function savefile($filepath, $comment)
  311. {
  312. $filepath = $this->formatPath($filepath);
  313. if (!FileClass::isWritable($filepath)) {
  314. throw new AdminException(400611);
  315. }
  316. return FileClass::writeFile($filepath, $comment);
  317. }
  318. // 文件重命名
  319. public function rename($newname, $oldname)
  320. {
  321. if (($newname != $oldname) && is_writable($oldname)) {
  322. return rename($oldname, $newname);
  323. }
  324. return true;
  325. }
  326. /**
  327. * 删除文件或文件夹
  328. * @param string $path
  329. * @return bool
  330. *
  331. * @date 2022/09/20
  332. * @author yyw
  333. */
  334. public function delFolder(string $path)
  335. {
  336. $path = $this->formatPath($path);
  337. if (is_file($path)) {
  338. return unlink($path);
  339. }
  340. $dir = opendir($path);
  341. while ($fileName = readdir($dir)) {
  342. $file = $path . '/' . $fileName;
  343. if ($fileName != '.' && $fileName != '..') {
  344. if (is_dir($file)) {
  345. self::delFolder($file);
  346. } else {
  347. unlink($file);
  348. }
  349. }
  350. }
  351. closedir($dir);
  352. return rmdir($path);
  353. }
  354. /**
  355. * 新建文件夹
  356. * @param string $path
  357. * @param string $name
  358. * @param int $permissions
  359. * @return bool
  360. *
  361. * @date 2022/09/20
  362. * @author yyw
  363. */
  364. public function createFolder(string $path, string $name, int $permissions = 0755)
  365. {
  366. $path = $this->formatPath($path, $name);
  367. /** @var FileClass $fileClass */
  368. $fileClass = app()->make(FileClass::class);
  369. return $fileClass->createDir($path, $permissions);
  370. }
  371. /**
  372. * 新建文件
  373. * @param string $path
  374. * @param string $name
  375. * @return bool
  376. *
  377. * @date 2022/09/20
  378. * @author yyw
  379. */
  380. public function createFile(string $path, string $name)
  381. {
  382. $path = $this->formatPath($path, $name);
  383. /** @var FileClass $fileClass */
  384. $fileClass = app()->make(FileClass::class);
  385. return $fileClass->createFile($path);
  386. }
  387. public function copyFolder($surDir, $toDir)
  388. {
  389. return FileClass::copyDir($surDir, $toDir);
  390. }
  391. /**
  392. * 格式化路径
  393. * @param string $path
  394. * @param string $name
  395. * @return string
  396. *
  397. * @date 2022/09/20
  398. * @author yyw
  399. */
  400. public function formatPath(string $path = '', string $name = ''): string
  401. {
  402. if ($path) {
  403. $path = rtrim($path, DS);
  404. if ($name) $path = $path . DS . $name;
  405. $uname = php_uname('s');
  406. // $search = '/';
  407. if (strstr($uname, 'Windows') !== false)
  408. $path = ltrim(str_replace('\\', '\\\\', $path), '.');
  409. }
  410. return $path;
  411. }
  412. }