No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Attachments.php 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 附件上传处理类
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller\general;
  15. use app\common\controller\Adminbase;
  16. use app\common\model\Attachment as AttachmentModel;
  17. use think\facade\Hook;
  18. class Attachments extends Adminbase
  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('public_url') . 'uploads/';
  27. }
  28. /**
  29. * 查看
  30. */
  31. public function index()
  32. {
  33. if ($this->request->isAjax()) {
  34. $mimetypeQuery = [];
  35. $allGet = $this->request->request();
  36. $filterArr = isset($allGet['filter']) ? (array) json_decode($allGet['filter'], true) : [];
  37. if (isset($filterArr['mime']) && preg_match("/(\/|\,|\*)/", $filterArr['mime'])) {
  38. $mimetype = $filterArr['mime'];
  39. $filterArr = array_diff_key($filterArr, ['mime' => '']);
  40. $mimetypeQuery = function ($query) use ($mimetype) {
  41. $mimetypeArr = array_filter(explode(',', $mimetype));
  42. foreach ($mimetypeArr as $index => $item) {
  43. $query->whereOr('mime', 'like', '%' . str_replace("/*", "/", $item) . '%');
  44. }
  45. };
  46. }
  47. $allGet['filter'] = json_encode($filterArr);
  48. $this->request->withGet($allGet);
  49. [$page, $limit, $where, $sort, $order] = $this->buildTableParames();
  50. $count = $this->modelClass
  51. ->where($where)
  52. ->where($mimetypeQuery)
  53. ->order($sort, $order)
  54. ->count();
  55. $data = $this->modelClass
  56. ->where($where)
  57. ->where($mimetypeQuery)
  58. ->order($sort, $order)
  59. ->page($page, $limit)
  60. ->select();
  61. $result = ["code" => 0, 'count' => $count, 'data' => $data];
  62. return json($result);
  63. }
  64. return $this->fetch();
  65. }
  66. //附件选择
  67. public function select()
  68. {
  69. if ($this->request->isAjax()) {
  70. return $this->index();
  71. }
  72. $mimetype = $this->request->get('mimetype/s', '');
  73. $mimetype = substr($mimetype, -1) === '/' ? $mimetype . '*' : $mimetype;
  74. $this->assign('mimetype', $mimetype);
  75. return $this->fetch();
  76. }
  77. public function cropper()
  78. {
  79. return $this->fetch();
  80. }
  81. //附件删除
  82. public function del()
  83. {
  84. if (false === $this->request->isPost()) {
  85. $this->error('未知参数');
  86. }
  87. $ids = $this->request->param('id/a', null);
  88. if (empty($ids)) {
  89. $this->error('请选择需要删除的附件!');
  90. }
  91. if (!is_array($ids)) {
  92. $ids = [0 => $ids];
  93. }
  94. $isAdministrator = $this->auth->isAdministrator();
  95. Hook::add('upload_delete', function ($params) {
  96. if ($params['driver'] == 'local') {
  97. $attachmentFile = ROOT_PATH . '/public' . $params['path'];
  98. if (is_file($attachmentFile)) {
  99. @unlink($attachmentFile);
  100. }
  101. }
  102. });
  103. $attachmentlist = AttachmentModel::where('id', 'in', $ids)->select();
  104. foreach ($attachmentlist as $attachment) {
  105. Hook::listen("upload_delete", $attachment);
  106. $attachment->delete();
  107. }
  108. $this->success('文件删除成功~');
  109. }
  110. /**
  111. * html代码远程图片本地化
  112. * @param string $content html代码
  113. * @param string $type 文件类型
  114. */
  115. public function getUrlFile()
  116. {
  117. $content = $this->request->post('content');
  118. $type = $this->request->post('type');
  119. $urls = [];
  120. $urls = \util\GetImgSrc::srcList($content);
  121. $urls = array_filter(array_map(function ($val) {
  122. //http开头验证
  123. if (strpos($val, "http") === 0) {
  124. return $val;
  125. }
  126. }, $urls));
  127. $file_info = [
  128. 'admin_id' => $this->auth->id,
  129. 'thumb' => '',
  130. ];
  131. foreach ($urls as $vo) {
  132. $vo = trim(urldecode($vo));
  133. $host = parse_url($vo, PHP_URL_HOST);
  134. if ($host != $_SERVER['HTTP_HOST']) {
  135. //当前域名下的文件不下载
  136. $fileExt = strrchr($vo, '.'); //$fileExt = '.jpg';非正常后缀图片可以强制设置图片后缀进行抓取下载
  137. if (!in_array($fileExt, ['.jpg', '.gif', '.png', '.bmp', '.jpeg', '.tiff'])) {
  138. exit($content);
  139. }
  140. //图片是否合法
  141. $imgInfo = getimagesize($vo);
  142. if (!$imgInfo || !isset($imgInfo[0]) || !isset($imgInfo[1])) {
  143. exit($content);
  144. }
  145. $filename = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'temp' . DS . md5($vo) . $fileExt;
  146. if (http_down($vo, $filename) !== false) {
  147. $file_info['md5'] = hash_file('md5', $filename);
  148. if ($file_exists = AttachmentModel::get(['md5' => $file_info['md5']])) {
  149. unlink($filename);
  150. $localpath = $file_exists['path'];
  151. } else {
  152. $file_info['sha1'] = hash_file('sha1', $filename);
  153. $file_info['size'] = filesize($filename);
  154. $file_info['mime'] = mime_content_type($filename);
  155. $fpath = $type . DS . date('Ymd');
  156. $savePath = ROOT_PATH . 'public' . DS . 'uploads' . DS . $fpath;
  157. if (!is_dir($savePath)) {
  158. mkdir($savePath, 0755, true);
  159. }
  160. $fname = DS . md5(microtime(true)) . $fileExt;
  161. $file_info['name'] = substr(htmlspecialchars(strip_tags($vo)), 0, 100);
  162. $file_info['path'] = $this->uploadUrl . str_replace(DS, '/', $fpath . $fname);
  163. $file_info['ext'] = ltrim($fileExt, ".");
  164. if (rename($filename, $savePath . $fname)) {
  165. AttachmentModel::create($file_info);
  166. $localpath = $file_info['path'];
  167. }
  168. }
  169. $content = str_replace($vo, $localpath, $content);
  170. }
  171. }
  172. }
  173. exit($content);
  174. }
  175. }