SystemFileServices.php 13 KB

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