Attachments.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 附件上传处理类
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller\general;
  15. use app\common\controller\Backend;
  16. use app\common\model\Attachment as AttachmentModel;
  17. use think\facade\Event;
  18. class Attachments extends Backend
  19. {
  20. private $uploadUrl = '';
  21. protected $searchFields = 'id,name';
  22. protected function initialize()
  23. {
  24. parent::initialize();
  25. $this->modelClass = new AttachmentModel;
  26. $this->uploadUrl = config('upload.cdnurl') . '/uploads/';
  27. $this->assign("categoryList", AttachmentModel::getCategoryList());
  28. $this->assignconfig("mimetypeList", AttachmentModel::getMimetypeList());
  29. $this->assignconfig("categoryList", AttachmentModel::getCategoryList());
  30. }
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. if ($this->request->isAjax()) {
  37. $mimetypeQuery = [];
  38. $allGet = $this->request->request();
  39. $filterArr = isset($allGet['filter']) ? (array) json_decode($allGet['filter'], true) : [];
  40. if (isset($filterArr['category']) && $filterArr['category'] == 'unclassed') {
  41. $filterArr['category'] = ',unclassed';
  42. }
  43. if (isset($filterArr['mime']) && preg_match("/(\/|\,|\*)/", $filterArr['mime'])) {
  44. $mimetype = $filterArr['mime'];
  45. $filterArr = array_diff_key($filterArr, ['mime' => '']);
  46. $mimetypeQuery = function ($query) use ($mimetype) {
  47. $mimetypeArr = array_filter(explode(',', $mimetype));
  48. foreach ($mimetypeArr as $index => $item) {
  49. $query->whereOr('mime', 'like', '%' . str_replace("/*", "/", $item) . '%');
  50. }
  51. };
  52. }
  53. $allGet['filter'] = json_encode($filterArr);
  54. $this->request->withGet($allGet);
  55. [$page, $limit, $where, $sort, $order] = $this->buildTableParames();
  56. $count = $this->modelClass
  57. ->where($where)
  58. ->where($mimetypeQuery)
  59. ->order($sort, $order)
  60. ->count();
  61. $data = $this->modelClass
  62. ->where($where)
  63. ->where($mimetypeQuery)
  64. ->order($sort, $order)
  65. ->page($page, $limit)
  66. ->select();
  67. $result = ["code" => 0, 'count' => $count, 'data' => $data];
  68. return json($result);
  69. }
  70. return $this->fetch();
  71. }
  72. public function add()
  73. {
  74. if ($this->request->isAjax()) {
  75. $this->error();
  76. }
  77. return $this->fetch();
  78. }
  79. //附件选择
  80. public function select()
  81. {
  82. if ($this->request->isAjax()) {
  83. return $this->index();
  84. }
  85. $mimetype = $this->request->get('mime/s', '');
  86. $mimetype = substr($mimetype, -1) === '/' ? $mimetype . '*' : $mimetype;
  87. $this->assignconfig('mimetype', $mimetype);
  88. return $this->fetch();
  89. }
  90. public function cropper()
  91. {
  92. return $this->fetch();
  93. }
  94. /**
  95. * 归类
  96. */
  97. public function classify()
  98. {
  99. if (!$this->auth->check('general.attachment/edit')) {
  100. \think\Hook::listen('admin_nopermission', $this);
  101. $this->error('你没有权限访问');
  102. }
  103. if (!$this->request->isPost()) {
  104. $this->error('参数不正确');
  105. }
  106. $category = $this->request->post('category', '');
  107. $ids = $this->request->param('id/a', null);
  108. if ($ids && !is_array($ids)) {
  109. $ids = [0 => $ids];
  110. }
  111. $categoryList = AttachmentModel::getCategoryList();
  112. if ($category && !isset($categoryList[$category])) {
  113. $this->error('指定的类别未找到');
  114. }
  115. $category = $category == 'unclassed' ? '' : $category;
  116. AttachmentModel::where('id', 'in', $ids)->update(['category' => $category]);
  117. $this->success();
  118. }
  119. //附件删除
  120. public function del()
  121. {
  122. if (false === $this->request->isPost()) {
  123. $this->error('未知参数');
  124. }
  125. $ids = $this->request->param('id/a', null);
  126. if (empty($ids)) {
  127. $this->error('请选择需要删除的附件!');
  128. }
  129. if (!is_array($ids)) {
  130. $ids = [0 => $ids];
  131. }
  132. $isAdministrator = $this->auth->isAdministrator();
  133. Event::listen('upload_delete', function ($params) {
  134. if ($params['driver'] == 'local') {
  135. $attachmentFile = public_path() . $params['path'];
  136. if (is_file($attachmentFile)) {
  137. @unlink($attachmentFile);
  138. }
  139. }
  140. });
  141. $attachmentlist = AttachmentModel::where('id', 'in', $ids)->select();
  142. foreach ($attachmentlist as $attachment) {
  143. Event::trigger("upload_delete", $attachment);
  144. $attachment->delete();
  145. }
  146. $this->success('文件删除成功~');
  147. }
  148. /**
  149. * html代码远程图片本地化
  150. * @param string $content html代码
  151. * @param string $type 文件类型
  152. */
  153. public function getUrlFile()
  154. {
  155. $content = $this->request->post('content');
  156. $type = $this->request->post('type');
  157. $urls = [];
  158. $urls = \util\GetImgSrc::srcList($content);
  159. $urls = array_filter(array_map(function ($val) {
  160. //http开头验证
  161. if (strpos($val, "http") === 0) {
  162. return $val;
  163. }
  164. }, $urls));
  165. $file_info = [
  166. 'admin_id' => $this->auth->id,
  167. 'thumb' => '',
  168. ];
  169. foreach ($urls as $vo) {
  170. $vo = trim(urldecode($vo));
  171. $host = parse_url($vo, PHP_URL_HOST);
  172. if ($host != $_SERVER['HTTP_HOST']) {
  173. //当前域名下的文件不下载
  174. $fileExt = strrchr($vo, '.'); //$fileExt = '.jpg';非正常后缀图片可以强制设置图片后缀进行抓取下载
  175. if (!in_array($fileExt, ['.jpg', '.gif', '.png', '.bmp', '.jpeg', '.tiff'])) {
  176. $this->success('', '', $content);
  177. }
  178. //图片是否合法
  179. $imgInfo = getimagesize($vo);
  180. if (!$imgInfo || !isset($imgInfo[0]) || !isset($imgInfo[1])) {
  181. $this->success('', '', $content);
  182. }
  183. $filename = public_path() . 'uploads' . DS . 'temp' . DS . md5($vo) . $fileExt;
  184. if (http_down($vo, $filename) !== false) {
  185. $file_info['md5'] = hash_file('md5', $filename);
  186. if ($file_exists = AttachmentModel::where('md5', $file_info['md5'])->find()) {
  187. unlink($filename);
  188. $localpath = $file_exists['path'];
  189. } else {
  190. $file_info['sha1'] = hash_file('sha1', $filename);
  191. $file_info['size'] = filesize($filename);
  192. $file_info['mime'] = mime_content_type($filename);
  193. $fpath = $type . DS . date('Ymd');
  194. $savePath = public_path() . 'uploads' . DS . $fpath;
  195. if (!is_dir($savePath)) {
  196. mkdir($savePath, 0755, true);
  197. }
  198. $fname = DS . md5(microtime(true)) . $fileExt;
  199. $file_info['name'] = substr(htmlspecialchars(strip_tags($vo)), 0, 100);
  200. $file_info['path'] = $this->uploadUrl . str_replace(DS, '/', $fpath . $fname);
  201. $file_info['ext'] = ltrim($fileExt, ".");
  202. if (rename($filename, $savePath . $fname)) {
  203. AttachmentModel::create($file_info);
  204. $localpath = $file_info['path'];
  205. }
  206. }
  207. $content = str_replace($vo, $localpath, $content);
  208. }
  209. }
  210. }
  211. $this->success('', '', $content);
  212. }
  213. }