SystemFile.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. namespace app\admin\controller\system;
  3. use app\admin\model\system\SystemFile as SystemFileModel;
  4. use app\admin\controller\AuthController;
  5. use crmeb\services\FormBuilder as Form;
  6. use crmeb\services\FileService as FileClass;
  7. use crmeb\services\JsonService as Json;
  8. /**
  9. * 文件校验控制器
  10. * Class SystemFile
  11. * @package app\admin\controller\system
  12. *
  13. */
  14. class SystemFile extends AuthController
  15. {
  16. //打开目录
  17. public function opendir($filedir = '')
  18. {
  19. $fileAll = array('dir' => [], 'file' => []);
  20. //根目录
  21. $rootdir = app()->getRootPath();
  22. //当前目录
  23. $request_dir = app('request')->param('dir');
  24. //防止查看站点以外的目录
  25. if (strpos($request_dir, $rootdir) === false) {
  26. $request_dir = $rootdir;
  27. }
  28. //判断是否是返回上级
  29. if (app('request')->param('superior') && !empty($request_dir)) {
  30. if (strpos(dirname($request_dir), $rootdir) !== false) {
  31. $dir = dirname($request_dir);
  32. } else {
  33. $dir = $rootdir;
  34. }
  35. } else {
  36. $dir = !empty($request_dir) ? $request_dir : $rootdir;
  37. $dir = rtrim($dir, DS) . DS . app('request')->param('filedir');
  38. }
  39. $list = scandir($dir);
  40. foreach ($list as $key => $v) {
  41. if ($v != '.' && $v != '..') {
  42. if (is_dir($dir . DS . $v)) {
  43. $fileAll['dir'][] = FileClass::list_info($dir . DS . $v);
  44. }
  45. if (is_file($dir . DS . $v)) {
  46. $fileAll['file'][] = FileClass::list_info($dir . DS . $v);
  47. }
  48. }
  49. }
  50. //兼容windows
  51. $uname = php_uname('s');
  52. if (strstr($uname, 'Windows') !== false) $dir = ltrim($dir, '\\');
  53. $this->assign(compact('fileAll', 'dir'));
  54. return $this->fetch();
  55. }
  56. //读取文件
  57. public function openfile($file = '')
  58. {
  59. $file = $this->request->param('file');
  60. if (empty($file)) return Json::fail('出现错误');
  61. $filepath = $file;
  62. $content = FileClass::read_file($filepath);//防止页面内嵌textarea标签
  63. $ext = FileClass::get_ext($filepath);
  64. $extarray = [
  65. 'js' => 'text/javascript'
  66. , 'php' => 'text/x-php'
  67. , 'html' => 'text/html'
  68. , 'sql' => 'text/x-mysql'
  69. , 'css' => 'text/x-scss'];
  70. $mode = empty($extarray[$ext]) ? '' : $extarray[$ext];
  71. $this->assign(compact('content', 'mode', 'filepath'));
  72. return $this->fetch();
  73. }
  74. //保存文件
  75. public function savefile()
  76. {
  77. $comment = $this->request->post('comment');
  78. $filepath = $this->request->post('filepath');
  79. if (!empty($comment) && !empty($filepath)) {
  80. //兼容windows
  81. $uname = php_uname('s');
  82. if (strstr($uname, 'Windows') !== false)
  83. $filepath = ltrim(str_replace('/', DS, $filepath), '.');
  84. if (FileClass::isWritable($filepath)) {
  85. $res = FileClass::write_file($filepath, $comment);
  86. if ($res) {
  87. return Json::successful('保存成功!');
  88. } else {
  89. return Json::fail('保存失败');
  90. }
  91. } else {
  92. return Json::fail('没有权限!');
  93. }
  94. } else {
  95. return Json::fail('出现错误');
  96. }
  97. }
  98. public function index()
  99. {
  100. $rootPath = app()->getRootPath();
  101. $app = $this->getDir($rootPath . 'app');
  102. $extend = $this->getDir($rootPath . 'crmeb');
  103. $arr = [];
  104. $arr = array_merge($app, $extend);
  105. $fileAll = [];//本地文件
  106. $cha = [];//不同的文件
  107. foreach ($arr as $k => $v) {
  108. $fp = fopen($v, 'r');
  109. if (filesize($v)) $ct = fread($fp, filesize($v));
  110. else $ct = null;
  111. fclose($fp);
  112. $cthash = md5($ct);
  113. $update_time = stat($v);
  114. $fileAll[$k]['cthash'] = $cthash;
  115. $fileAll[$k]['filename'] = str_replace($rootPath, '', $v);
  116. $fileAll[$k]['atime'] = $update_time['atime'];
  117. $fileAll[$k]['mtime'] = $update_time['mtime'];
  118. $fileAll[$k]['ctime'] = $update_time['ctime'];
  119. }
  120. $file = SystemFileModel::all(function ($query) {
  121. $query->order('atime', 'desc');
  122. })->toArray();//数据库中的文件
  123. if (empty($file)) {
  124. $data_num = array_chunk($fileAll, 10);
  125. SystemFileModel::beginTrans();
  126. $res = true;
  127. foreach ($data_num as $k => $v) {
  128. $res = $res && SystemFileModel::insertAll($v);
  129. }
  130. SystemFileModel::checkTrans($res);
  131. if ($res) {
  132. $cha = [];//不同的文件
  133. } else {
  134. $cha = $fileAll;
  135. }
  136. } else {
  137. $cha = [];//差异文件
  138. foreach ($file as $k => $v) {
  139. foreach ($fileAll as $ko => $vo) {
  140. if ($v['filename'] == $vo['filename']) {
  141. if ($v['cthash'] != $vo['cthash']) {
  142. $cha[$k]['filename'] = str_replace($rootPath, '', $v['filename']);
  143. $cha[$k]['cthash'] = $v['cthash'];
  144. $cha[$k]['atime'] = $v['atime'];
  145. $cha[$k]['mtime'] = $v['mtime'];
  146. $cha[$k]['ctime'] = $v['ctime'];
  147. $cha[$k]['type'] = '已修改';
  148. }
  149. unset($fileAll[$ko]);
  150. unset($file[$k]);
  151. }
  152. }
  153. }
  154. foreach ($file as $k => $v) {
  155. $cha[$k]['filename'] = str_replace($rootPath, '', $v['filename']);
  156. $cha[$k]['cthash'] = $v['cthash'];
  157. $cha[$k]['atime'] = $v['atime'];
  158. $cha[$k]['mtime'] = $v['mtime'];
  159. $cha[$k]['ctime'] = $v['ctime'];
  160. $cha[$k]['type'] = '已删除';
  161. }
  162. foreach ($fileAll as $k => $v) {
  163. $cha[$k]['filename'] = str_replace($rootPath, '', $v['filename']);
  164. $cha[$k]['cthash'] = $v['cthash'];
  165. $cha[$k]['atime'] = $v['atime'];
  166. $cha[$k]['mtime'] = $v['mtime'];
  167. $cha[$k]['ctime'] = $v['ctime'];
  168. $cha[$k]['type'] = '新增的';
  169. }
  170. }
  171. // dump($file);
  172. // dump($fileAll);
  173. $this->assign('cha', $cha);
  174. return $this->fetch();
  175. }
  176. /**
  177. * 获取文件夹中的文件 不包括子文件
  178. * @param $dir
  179. * @return array
  180. */
  181. public function getNextDir()
  182. {
  183. $dir = './';
  184. $list = scandir($dir);
  185. $dirlist = [];
  186. $filelist = [];
  187. foreach ($list as $key => $v) {
  188. if ($v != '.' && $v != '..') {
  189. if (is_dir($dir . '/' . $v)) {
  190. $dirlist['dir'][$key] = $v;
  191. }
  192. if (is_file($dir . '/' . $v)) {
  193. $filelist['file'][$key] = $v;
  194. }
  195. }
  196. }
  197. $filesarr = array_merge($dirlist, $filelist);
  198. print_r($filesarr);
  199. }
  200. /**
  201. * 获取文件夹中的文件 包括子文件 不能直接用 直接使用 $this->getDir()方法 P156
  202. * @param $path
  203. * @param $data
  204. */
  205. public function searchDir($path, &$data)
  206. {
  207. if (is_dir($path) && !strpos($path, 'uploads')) {
  208. $dp = dir($path);
  209. while ($file = $dp->read()) {
  210. if ($file != '.' && $file != '..') {
  211. $this->searchDir($path . '/' . $file, $data);
  212. }
  213. }
  214. $dp->close();
  215. }
  216. if (is_file($path)) {
  217. $data[] = $path;
  218. }
  219. }
  220. /**
  221. * 获取文件夹中的文件 包括子文件
  222. * @param $dir
  223. * @return array
  224. */
  225. public function getDir($dir)
  226. {
  227. $data = [];
  228. $this->searchDir($dir, $data);
  229. return $data;
  230. }
  231. //测试
  232. public function ceshi()
  233. {
  234. //创建form
  235. $form = Form::create('/save.php', [
  236. Form::input('goods_name', '商品名称')
  237. , Form::input('goods_name1', 'password')->type('password')
  238. , Form::input('goods_name2', 'textarea')->type('textarea')
  239. , Form::input('goods_name3', 'email')->type('email')
  240. , Form::input('goods_name4', 'date')->type('date')
  241. , Form::city('address', 'cityArea',
  242. '陕西省', '西安市'
  243. )
  244. , Form::dateRange('limit_time', 'dateRange',
  245. strtotime('- 10 day'),
  246. time()
  247. )
  248. , Form::dateTime('add_time', 'dateTime')
  249. , Form::color('color', 'color', '#ff0000')
  250. , Form::checkbox('checkbox', 'checkbox', [1])->options([['value' => 1, 'label' => '白色'], ['value' => 2, 'label' => '红色'], ['value' => 31, 'label' => '黑色']])
  251. , Form::date('riqi', 'date', '2018-03-1')
  252. , Form::dateTimeRange('dateTimeRange', '区间时间段')
  253. , Form::year('year', 'year')
  254. , Form::month('month', 'month')
  255. , Form::frame('frame', 'frame', '/admin/system.system_attachment/index.html?fodder=frame')
  256. , Form::frameInputs('frameInputs', 'frameInputs', '/admin/system.system_attachment/index.html?fodder=frameInputs')
  257. , Form::frameFiles('month1', 'frameFiles', '/admin/system.system_attachment/index.html?fodder=month1')
  258. , Form::frameImages('fodder1', 'frameImages', '/admin/system.system_attachment/index.html?fodder=fodder1')->maxLength(3)->width('800px')->height('400px')
  259. , Form::frameImages('fodder11', 'frameImages', '/admin/system.system_attachment/index.html?fodder=fodder11')->icon('images')
  260. , Form::frameInputOne('month3', 'frameInputOne', '/admin/system.system_attachment/index.html?fodder=month3')->icon('ionic')
  261. , Form::frameFileOne('month4', 'frameFileOne', '/admin/system.system_attachment/index.html?fodder=month4')
  262. , Form::frameImageOne('month5', 'frameImageOne', '/admin/system.system_attachment/index.html?fodder=month5')->icon('image')
  263. , Form::hidden('month6', 'hidden')
  264. , Form::number('month7', 'number')
  265. // ,Form::input input输入框,其他type: text类型Form::text,password类型Form::password,textarea类型Form::textarea,url类型Form::url,email类型Form::email,date类型Form::idate
  266. , Form::radio('month8', 'radio')->options([['value' => 1, 'label' => '白色'], ['value' => 2, 'label' => '红色'], ['value' => 31, 'label' => '黑色']])
  267. , Form::rate('month9', 'rate')
  268. , Form::select('month10', 'select')->options([['value' => 1, 'label' => '白色'], ['value' => 2, 'label' => '红色'], ['value' => 31, 'label' => '黑色']])
  269. , Form::selectMultiple('month11', 'selectMultiple')
  270. , Form::selectOne('month12', 'selectOne')
  271. , Form::slider('month13', 'slider', 2)
  272. , Form::sliderRange('month23', 'sliderRange', 2, 13)
  273. , Form::switches('month14', '区间时间段')
  274. , Form::timePicker('month15', '区间时间段')
  275. , Form::time('month16', '区间时间段')
  276. , Form::timeRange('month17', '区间时间段')
  277. // ,Form::upload('month','区间时间段')
  278. // ,Form::uploadImages('month','区间时间段')
  279. // ,Form::uploadFiles('month','区间时间段')
  280. // ,Form::uploadImageOne('month','区间时间段')
  281. // ,Form::uploadFileOne('month','区间时间段')
  282. ]);
  283. $html = $form->setMethod('get')->setTitle('编辑商品')->view();
  284. echo $html;
  285. }
  286. }