SystemFile.php 11 KB

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