SystemFile.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\system;
  12. use app\admin\model\system\SystemFile as SystemFileModel;
  13. use app\admin\controller\AuthController;
  14. use service\FormBuilder as Form;
  15. /**
  16. * 文件校验控制器
  17. * Class SystemFile
  18. * @package app\admin\controller\system
  19. *
  20. */
  21. class SystemFile extends AuthController
  22. {
  23. public function index()
  24. {
  25. $app = $this->getDir('./application');
  26. $extend = $this->getDir('./extend');
  27. $public = $this->getDir('./public');
  28. $arr = array();
  29. $arr = array_merge($app, $extend);
  30. $arr = array_merge($arr, $public);
  31. $fileAll = array();//本地文件
  32. $cha = array();//不同的文件
  33. foreach ($arr as $k => $v) {
  34. $fp = fopen($v, 'r');
  35. if (filesize($v)) $ct = fread($fp, filesize($v));
  36. else $ct = null;
  37. fclose($fp);
  38. $cthash = md5($ct);
  39. $update_time = stat($v);
  40. $fileAll[$k]['cthash'] = $cthash;
  41. $fileAll[$k]['filename'] = $v;
  42. $fileAll[$k]['atime'] = $update_time['atime'];
  43. $fileAll[$k]['mtime'] = $update_time['mtime'];
  44. $fileAll[$k]['ctime'] = $update_time['ctime'];
  45. }
  46. $file = SystemFileModel::all(function ($query) {
  47. $query->order('atime', 'desc');
  48. })->toArray();//数据库中的文件
  49. if (empty($file)) {
  50. $data_num = array_chunk($fileAll, 10);
  51. SystemFileModel::beginTrans();
  52. $res = true;
  53. foreach ($data_num as $k => $v) {
  54. $res = $res && SystemFileModel::insertAll($v);
  55. }
  56. SystemFileModel::checkTrans($res);
  57. if ($res) {
  58. $cha = array();//不同的文件
  59. } else {
  60. $cha = $fileAll;
  61. }
  62. } else {
  63. $cha = array();//差异文件
  64. foreach ($file as $k => $v) {
  65. foreach ($fileAll as $ko => $vo) {
  66. if ($v['filename'] == $vo['filename']) {
  67. if ($v['cthash'] != $vo['cthash']) {
  68. $cha[$k]['filename'] = $v['filename'];
  69. $cha[$k]['cthash'] = $v['cthash'];
  70. $cha[$k]['atime'] = $v['atime'];
  71. $cha[$k]['mtime'] = $v['mtime'];
  72. $cha[$k]['ctime'] = $v['ctime'];
  73. $cha[$k]['type'] = '已修改';
  74. }
  75. unset($fileAll[$ko]);
  76. unset($file[$k]);
  77. }
  78. }
  79. }
  80. foreach ($file as $k => $v) {
  81. $cha[$k]['filename'] = $v['filename'];
  82. $cha[$k]['cthash'] = $v['cthash'];
  83. $cha[$k]['atime'] = $v['atime'];
  84. $cha[$k]['mtime'] = $v['mtime'];
  85. $cha[$k]['ctime'] = $v['ctime'];
  86. $cha[$k]['type'] = '已删除';
  87. }
  88. foreach ($fileAll as $k => $v) {
  89. $cha[$k]['filename'] = $v['filename'];
  90. $cha[$k]['cthash'] = $v['cthash'];
  91. $cha[$k]['atime'] = $v['atime'];
  92. $cha[$k]['mtime'] = $v['mtime'];
  93. $cha[$k]['ctime'] = $v['ctime'];
  94. $cha[$k]['type'] = '新增的';
  95. }
  96. }
  97. $this->assign('cha', $cha);
  98. return $this->fetch();
  99. }
  100. public function filelist()
  101. {
  102. $app = $this->getDir('./application');
  103. print_r($app);
  104. $extend = $this->getDir('./extend');
  105. $public = $this->getDir('./public');
  106. $arr = array();
  107. $arr = array_merge($app, $extend);
  108. $arr = array_merge($arr, $public);
  109. $fileAll = array();//本地文件
  110. foreach ($arr as $k => $v) {
  111. $fp = fopen($v, 'r');
  112. if (filesize($v)) $ct = fread($fp, filesize($v));
  113. else $ct = null;
  114. fclose($fp);
  115. $cthash = md5($ct);
  116. $update_time = stat($v);
  117. $fileAll[$k]['cthash'] = $cthash;
  118. $fileAll[$k]['filename'] = $v;
  119. $fileAll[$k]['atime'] = $update_time['atime'];
  120. $fileAll[$k]['mtime'] = $update_time['mtime'];
  121. $fileAll[$k]['ctime'] = $update_time['ctime'];
  122. }
  123. dump($fileAll);
  124. }
  125. /**
  126. * 获取文件夹中的文件 不包括子文件
  127. * @param $dir
  128. * @return array
  129. */
  130. public function getNextDir()
  131. {
  132. $dir = './';
  133. $list = scandir($dir);
  134. $dirlist = array();
  135. $filelist = array();
  136. foreach ($list as $key => $v) {
  137. if ($v != '.' && $v != '..') {
  138. if (is_dir($dir . '/' . $v)) {
  139. $dirlist[$key]['name'] = $v;
  140. $dirlist[$key]['type'] = 'dir';
  141. }
  142. if (is_file($dir . '/' . $v)) {
  143. $filelist[$key]['name'] = $v;
  144. $filelist[$key]['type'] = 'file';
  145. }
  146. }
  147. }
  148. $filesarr = array_merge($dirlist, $filelist);
  149. print_r($filesarr);
  150. }
  151. /**
  152. * 获取文件夹中的文件 包括子文件 不能直接用 直接使用 $this->getDir()方法 P156
  153. * @param $path
  154. * @param $data
  155. */
  156. public function searchDir($path, &$data)
  157. {
  158. if (is_dir($path) && !strpos($path, 'uploads')) {
  159. $dp = dir($path);
  160. while ($file = $dp->read()) {
  161. if ($file != '.' && $file != '..') {
  162. $this->searchDir($path . '/' . $file, $data);
  163. }
  164. }
  165. $dp->close();
  166. }
  167. if (is_file($path)) {
  168. $data[] = $path;
  169. }
  170. }
  171. /**
  172. * 获取文件夹中的文件 包括子文件
  173. * @param $dir
  174. * @return array
  175. */
  176. public function getDir($dir)
  177. {
  178. $data = array();
  179. $this->searchDir($dir, $data);
  180. return $data;
  181. }
  182. //测试
  183. public function ceshi()
  184. {
  185. //创建form
  186. $form = Form::create('/save.php', [
  187. Form::input('goods_name', '商品名称')
  188. , Form::input('goods_name1', 'password')->type('password')
  189. , Form::input('goods_name2', 'textarea')->type('textarea')
  190. , Form::input('goods_name3', 'email')->type('email')
  191. , Form::input('goods_name4', 'date')->type('date')
  192. , Form::city('address', 'cityArea',
  193. '陕西省', '西安市'
  194. )
  195. , Form::dateRange('limit_time', 'dateRange',
  196. strtotime('- 10 day'),
  197. time()
  198. )
  199. , Form::dateTime('add_time', 'dateTime')
  200. , Form::color('color', 'color', '#ff0000')
  201. , Form::checkbox('checkbox', 'checkbox', [1])->options([['value' => 1, 'label' => '白色'], ['value' => 2, 'label' => '红色'], ['value' => 31, 'label' => '黑色']])
  202. , Form::date('riqi', 'date', '2018-03-1')
  203. , Form::dateTimeRange('dateTimeRange', '区间时间段')
  204. , Form::year('year', 'year')
  205. , Form::month('month', 'month')
  206. , Form::frame('frame', 'frame', '/admin/system.system_attachment/index.html?fodder=frame')
  207. , Form::frameInputs('frameInputs', 'frameInputs', '/admin/system.system_attachment/index.html?fodder=frameInputs')
  208. , Form::frameFiles('month1', 'frameFiles', '/admin/system.system_attachment/index.html?fodder=month1')
  209. , Form::frameImages('fodder1', 'frameImages', '/admin/system.system_attachment/index.html?fodder=fodder1')->maxLength(3)->width('800px')->height('400px')
  210. , Form::frameImages('fodder11', 'frameImages', '/admin/system.system_attachment/index.html?fodder=fodder11')->icon('images')
  211. , Form::frameInputOne('month3', 'frameInputOne', '/admin/system.system_attachment/index.html?fodder=month3')->icon('ionic')
  212. , Form::frameFileOne('month4', 'frameFileOne', '/admin/system.system_attachment/index.html?fodder=month4')
  213. , Form::frameImageOne('month5', 'frameImageOne', '/admin/system.system_attachment/index.html?fodder=month5')->icon('image')
  214. , Form::hidden('month6', 'hidden')
  215. , Form::number('month7', 'number')
  216. // ,Form::input input输入框,其他type: text类型Form::text,password类型Form::password,textarea类型Form::textarea,url类型Form::url,email类型Form::email,date类型Form::idate
  217. , Form::radio('month8', 'radio')->options([['value' => 1, 'label' => '白色'], ['value' => 2, 'label' => '红色'], ['value' => 31, 'label' => '黑色']])
  218. , Form::rate('month9', 'rate')
  219. , Form::select('month10', 'select')->options([['value' => 1, 'label' => '白色'], ['value' => 2, 'label' => '红色'], ['value' => 31, 'label' => '黑色']])
  220. , Form::selectMultiple('month11', 'selectMultiple')
  221. , Form::selectOne('month12', 'selectOne')
  222. , Form::slider('month13', 'slider', 2)
  223. , Form::sliderRange('month23', 'sliderRange', 2, 13)
  224. , Form::switches('month14', '区间时间段')
  225. , Form::timePicker('month15', '区间时间段')
  226. , Form::time('month16', '区间时间段')
  227. , Form::timeRange('month17', '区间时间段')
  228. // ,Form::upload('month','区间时间段')
  229. // ,Form::uploadImages('month','区间时间段')
  230. // ,Form::uploadFiles('month','区间时间段')
  231. // ,Form::uploadImageOne('month','区间时间段')
  232. // ,Form::uploadFileOne('month','区间时间段')
  233. ]);
  234. $html = $form->setMethod('get')->setTitle('编辑商品')->view();
  235. echo $html;
  236. }
  237. }