Filemanager.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 小虎哥 <1105415366@qq.com>
  11. * Date: 2018-4-3
  12. */
  13. namespace app\admin\controller;
  14. use app\admin\controller\Base;
  15. use think\Controller;
  16. use think\Db;
  17. use app\admin\logic\FilemanagerLogic;
  18. class Filemanager extends Base
  19. {
  20. public $filemanagerLogic;
  21. public $baseDir = '';
  22. public $maxDir = '';
  23. public $globalTpCache = array();
  24. public function _initialize() {
  25. parent::_initialize();
  26. $this->filemanagerLogic = new FilemanagerLogic();
  27. $this->globalTpCache = $this->filemanagerLogic->globalTpCache;
  28. $this->baseDir = $this->filemanagerLogic->baseDir; // 服务器站点根目录绝对路径
  29. $this->maxDir = $this->filemanagerLogic->maxDir; // 默认文件管理的最大级别目录
  30. }
  31. public function index()
  32. {
  33. // 获取到所有GET参数
  34. $param = input('param.', '', null);
  35. $activepath = input('param.activepath', '', null);
  36. $activepath = $this->filemanagerLogic->replace_path($activepath, ':', true);
  37. /*当前目录路径*/
  38. $activepath = !empty($activepath) ? $activepath : $this->maxDir;
  39. $tmp_max_dir = preg_replace("#\/#i", "\/", $this->maxDir);
  40. if (!preg_match("#^".$tmp_max_dir."#i", $activepath)) {
  41. $activepath = $this->maxDir;
  42. }
  43. /*--end*/
  44. $inpath = "";
  45. $activepath = str_replace("..", "", $activepath);
  46. $activepath = preg_replace("#^\/{1,}#", "/", $activepath); // 多个斜杆替换为单个斜杆
  47. if($activepath == "/") $activepath = "";
  48. if(empty($activepath)) {
  49. $inpath = $this->baseDir.$this->maxDir;
  50. } else {
  51. $inpath = $this->baseDir.$activepath;
  52. }
  53. $list = $this->filemanagerLogic->getDirFile($inpath, $activepath);
  54. $assign_data['list'] = $list;
  55. /*文件操作*/
  56. $assign_data['replaceImgOpArr'] = $this->filemanagerLogic->replaceImgOpArr;
  57. $assign_data['editOpArr'] = $this->filemanagerLogic->editOpArr;
  58. $assign_data['renameOpArr'] = $this->filemanagerLogic->renameOpArr;
  59. $assign_data['delOpArr'] = $this->filemanagerLogic->delOpArr;
  60. $assign_data['moveOpArr'] = $this->filemanagerLogic->moveOpArr;
  61. /*--end*/
  62. $assign_data['activepath'] = $activepath;
  63. $this->assign($assign_data);
  64. return $this->fetch();
  65. }
  66. /**
  67. * 替换图片
  68. */
  69. public function replace_img()
  70. {
  71. if (IS_POST) {
  72. $post = input('post.', '', null);
  73. $activepath = !empty($post['activepath']) ? trim($post['activepath']) : '';
  74. if (empty($activepath)) {
  75. $this->error('参数有误');
  76. exit;
  77. }
  78. $file = request()->file('upfile');
  79. if (empty($file)) {
  80. $this->error('请选择上传图片!');
  81. exit;
  82. } else {
  83. $image_type = tpCache('basic.image_type');
  84. $fileExt = !empty($image_type) ? str_replace('|', ',', $image_type) : config('global.image_ext');
  85. $image_upload_limit_size = intval(tpCache('basic.file_size') * 1024 * 1024);
  86. $result = $this->validate(
  87. ['file' => $file],
  88. ['file'=>'image|fileSize:'.$image_upload_limit_size.'|fileExt:'.$fileExt],
  89. ['file.image' => '上传文件必须为图片','file.fileSize' => '上传文件过大','file.fileExt'=>'上传文件后缀名必须为'.$fileExt]
  90. );
  91. if (true !== $result || empty($file)) {
  92. $this->error($result);
  93. exit;
  94. }
  95. }
  96. $res = $this->filemanagerLogic->upload('upfile', $activepath, $post['filename'], 'image');
  97. if ($res['code'] == 1) {
  98. $this->success('操作成功!', url('Filemanager/index', array('activepath'=>$this->filemanagerLogic->replace_path($activepath, ':', false))));
  99. } else {
  100. $this->error($res['msg'], url('Filemanager/index', array('activepath'=>$this->filemanagerLogic->replace_path($activepath, ':', false))));
  101. }
  102. }
  103. $filename = input('param.filename/s', '', null);
  104. $activepath = input('param.activepath/s', '', null);
  105. $activepath = $this->filemanagerLogic->replace_path($activepath, ':', true);
  106. if ($activepath == "") $activepathname = "根目录";
  107. else $activepathname = $activepath;
  108. $info = array(
  109. 'activepath' => $activepath,
  110. 'activepathname' => $activepathname,
  111. 'filename' => $filename,
  112. );
  113. $this->assign('info', $info);
  114. return $this->fetch();
  115. }
  116. /**
  117. * 编辑
  118. */
  119. public function edit()
  120. {
  121. if (IS_POST) {
  122. $post = input('post.', '', null);
  123. $content = input('post.content', '', null);
  124. $filename = !empty($post['filename']) ? trim($post['filename']) : '';
  125. $content = !empty($content) ? $content : '';
  126. $activepath = !empty($post['activepath']) ? trim($post['activepath']) : '';
  127. if (empty($filename) || empty($activepath)) {
  128. $this->error('参数有误');
  129. exit;
  130. }
  131. $r = $this->filemanagerLogic->editFile($filename, $activepath, $content);
  132. if ($r === true) {
  133. $this->success('操作成功!', url('Filemanager/index', array('activepath'=>$this->filemanagerLogic->replace_path($activepath, ':', false))));
  134. exit;
  135. } else {
  136. $this->error($r);
  137. exit;
  138. }
  139. }
  140. $activepath = input('param.activepath/s', '', null);
  141. $activepath = $this->filemanagerLogic->replace_path($activepath, ':', true);
  142. $filename = input('param.filename/s', '', null);
  143. $activepath = str_replace("..", "", $activepath);
  144. $filename = str_replace("..", "", $filename);
  145. $path_parts = pathinfo($filename);
  146. $path_parts['extension'] = strtolower($path_parts['extension']);
  147. /*不允许越过指定最大级目录的文件编辑*/
  148. $tmp_max_dir = preg_replace("#\/#i", "\/", $this->filemanagerLogic->maxDir);
  149. if (!preg_match("#^".$tmp_max_dir."#i", $activepath)) {
  150. $this->error('没有操作权限!');
  151. exit;
  152. }
  153. /*--end*/
  154. /*允许编辑的文件类型*/
  155. if (!in_array($path_parts['extension'], $this->filemanagerLogic->editExt)) {
  156. $this->error('只允许操作文件类型如下:'.implode('|', $this->filemanagerLogic->editExt));
  157. exit;
  158. }
  159. /*--end*/
  160. /*读取文件内容*/
  161. $file = $this->baseDir."$activepath/$filename";
  162. $content = "";
  163. if(is_file($file))
  164. {
  165. $filesize = filesize($file);
  166. if (0 < $filesize) {
  167. $fp = fopen($file, "r");
  168. $content = fread($fp, $filesize);
  169. fclose($fp);
  170. if ('css' != $path_parts['extension']) {
  171. $content = htmlspecialchars($content, ENT_QUOTES);
  172. $content = preg_replace("/(@)?eval(\s*)\(/i", 'intval(', $content);
  173. // $content = preg_replace("/\?\bphp\b/i", "?muma", $content);
  174. }
  175. }
  176. }
  177. /*--end*/
  178. if($path_parts['extension'] == 'js'){
  179. $extension = 'text/javascript';
  180. } else if($path_parts['extension'] == 'css'){
  181. $extension = 'text/css';
  182. } else {
  183. $extension = 'text/html';
  184. }
  185. $info = array(
  186. 'filename' => $filename,
  187. 'activepath'=> $activepath,
  188. 'extension' => $extension,
  189. 'content' => $content,
  190. );
  191. $this->assign('info', $info);
  192. return $this->fetch();
  193. }
  194. /**
  195. * 新建文件
  196. */
  197. public function newfile()
  198. {
  199. if (IS_POST) {
  200. $post = input('post.', '', null);
  201. $content = input('post.content', '', null);
  202. $filename = !empty($post['filename']) ? trim($post['filename']) : '';
  203. $content = !empty($content) ? $content : '';
  204. $activepath = !empty($post['activepath']) ? trim($post['activepath']) : '';
  205. if (empty($filename) || empty($activepath)) {
  206. $this->error('参数有误');
  207. exit;
  208. }
  209. $r = $this->filemanagerLogic->editFile($filename, $activepath, $content);
  210. if ($r === true) {
  211. $this->success('操作成功!', url('Filemanager/index', array('activepath'=>$this->filemanagerLogic->replace_path($activepath, ':', false))));
  212. exit;
  213. } else {
  214. $this->error($r);
  215. exit;
  216. }
  217. }
  218. $activepath = input('param.activepath/s', '', null);
  219. $activepath = $this->filemanagerLogic->replace_path($activepath, ':', true);
  220. $filename = 'newfile.htm';
  221. $content = "";
  222. $info = array(
  223. 'filename' => $filename,
  224. 'activepath'=> $activepath,
  225. 'content' => $content,
  226. 'extension' => 'text/html',
  227. );
  228. $this->assign('info', $info);
  229. return $this->fetch();
  230. }
  231. }