UploadController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: liuyifan
  5. * Date: 2019/3/6
  6. * Time: 14:01
  7. */
  8. namespace app\platform\modules\system\controllers;
  9. use app\platform\controllers\BaseController;
  10. use app\platform\modules\system\models\SystemSetting;
  11. use app\platform\modules\application\models\CoreAttach;
  12. use app\platform\modules\application\models\WechatAttachment;
  13. use app\common\services\Utils;
  14. use app\platform\modules\application\models\AppUser;
  15. use Ixudra\Curl\Facades\Curl;
  16. class UploadController extends BaseController
  17. {
  18. protected $global;
  19. protected $uniacid;
  20. protected $remote;
  21. protected $common;
  22. public function __construct()
  23. {
  24. $this->global = SystemSetting::settingLoad('global', 'system_global');
  25. $this->remote = SystemSetting::settingLoad('remote', 'system_remote');
  26. $this->uniacid = \YunShop::app()->uniacid ? : 0 ;
  27. $this->common = $this->common();
  28. }
  29. public function upload()
  30. {
  31. if (!$_FILES['file']['name']) {
  32. return $this->errorJson('上传失败, 请选择要上传的文件!');
  33. }
  34. if ($_FILES['file']['error'] != 0) {
  35. return $this->errorJson('上传失败, 请重试.');
  36. }
  37. $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
  38. $ext = strtolower($ext);
  39. $originname = $_FILES['file']['name'];
  40. $filename = file_random_name(base_path() . '/static/upload' . $this->common['folder'], $ext);
  41. $file = $this->file_upload($_FILES['file'], $this->common['type'], $this->common['folder'] . $filename, true);
  42. if (is_error($file)) {
  43. return $this->errorJson($file['message']);
  44. }
  45. $pathname = $file['path'];
  46. $fullname = base_path() . '/static/upload/' . $pathname;
  47. return $this->saveData($this->common['type'], $fullname, $originname, $ext, $filename, $this->common['module_upload_dir'], $pathname, $this->common['option']);
  48. }
  49. public function file_upload($file, $type = 'image', $name = '', $compress = false)
  50. {
  51. $harmtype = array('asp', 'php', 'jsp', 'js', 'css', 'php3', 'php4', 'php5', 'ashx', 'aspx', 'exe', 'cgi');
  52. if (!$file) {
  53. return error(-1, '没有上传内容');
  54. }
  55. if (!in_array($type, array('image', 'thumb', 'voice', 'video', 'audio'))) {
  56. return error(-2, '未知的上传类型');
  57. }
  58. $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
  59. $ext = strtolower($ext);
  60. switch ($type) {
  61. case 'image':
  62. $allowExt = $this->global['image_extentions'];
  63. $limit = $this->global['image_limit'];
  64. break;
  65. case 'thumb':
  66. case 'voice':
  67. case 'audio':
  68. $allowExt = $this->global['audio_extentions'];
  69. $limit = $this->global['audio_limit'];
  70. break;
  71. case 'video':
  72. $allowExt = $this->global['audio_extentions'];
  73. $limit = $this->global['audio_limit'];
  74. break;
  75. }
  76. $setting = $this->global[$type.'_extentions'];
  77. if ($setting) {
  78. $allowExt = array_merge($setting, $allowExt);
  79. }
  80. if (!in_array(strtolower($ext), $allowExt) || in_array(strtolower($ext), $harmtype)) {
  81. return error(-3, '不允许上传此类文件');
  82. }
  83. if ($limit && $limit * 1024 < filesize($file['tmp_name'])) {
  84. return error(-4, "上传的文件超过大小限制,请上传小于 {$limit}k 的文件");
  85. }
  86. $result = array();
  87. if (!$name || $name == 'auto') {
  88. $path = "/{$type}s/{$this->uniacid}" . '/'.date('Y/m/');
  89. Utils::mkdirs(base_path() . '/static/upload' . $path);
  90. $filename = file_random_name(base_path() . '/' . $path, $ext);
  91. $result['path'] = $path . $filename;
  92. } else {
  93. Utils::mkdirs(dirname(base_path() . '/static/upload/' . $name));
  94. if (!strexists($name, $ext)) {
  95. $name .= '.' . $ext;
  96. }
  97. $result['path'] = $name;
  98. }
  99. $save_path = base_path() . '/static/upload/' . $result['path'];
  100. if (!file_move($file['tmp_name'], $save_path)) {
  101. return error(-1, '保存上传文件失败');
  102. }
  103. if ($type == 'image' && $compress) {
  104. file_image_quality($save_path, $save_path, $ext, $this->global);
  105. }
  106. $result['success'] = true;
  107. return $result;
  108. }
  109. public function saveData($type, $fullname, $originname, $ext, $filename, $module_upload_dir, $pathname, $option)
  110. {
  111. if ($type == 'image') {
  112. $thumb = !$this->global['thumb'] ? 0 : 1;
  113. $width = intval($this->global['thumb_width']);
  114. if (isset($option['thumb'])) {
  115. $thumb = !$option['thumb'] ? 0 : 1;
  116. }
  117. if (isset($option['width']) && $option['width']) {
  118. $width = intval($option['width']);
  119. }
  120. if ($thumb == 1 && $width > 0) {
  121. $thumbnail = file_image_thumb($fullname, '', $width, $this->global);
  122. if ($thumbnail == 1) {
  123. return $this->errorJson('创建目录失败');
  124. } elseif ($thumbnail == 2) {
  125. return $this->errorJson('目录无法写入');
  126. }
  127. @unlink($fullname);
  128. if (is_error($thumbnail)) {
  129. return $this->successJson($thumbnail['message']);
  130. } else {
  131. $filename = pathinfo($thumbnail, PATHINFO_BASENAME);
  132. $pathname = $thumbnail;
  133. $fullname = base_path() . '/static/upload' . $pathname;
  134. }
  135. }
  136. }
  137. $info = array(
  138. 'name' => $originname,
  139. 'ext' => $ext,
  140. 'filename' => $pathname,
  141. 'attachment' => $pathname,
  142. 'url' => yz_tomedia($pathname),
  143. 'is_image' => $type == 'image' ? 1 : 0,
  144. 'filesize' => filesize($fullname),
  145. 'group_id' => intval(request()->group_id)
  146. );
  147. if ($type == 'image') {
  148. $size = getimagesize($fullname);
  149. $info['width'] = $size[0];
  150. $info['height'] = $size[1];
  151. } else {
  152. $size = filesize($fullname);
  153. $info['size'] = sizecount($size);
  154. }
  155. if ($this->remote['type']) {
  156. $remotestatus = file_remote_upload($pathname, true, $this->remote);
  157. if (is_error($remotestatus)) {
  158. file_delete($pathname);
  159. return $this->errorJson('远程附件上传失败,请检查配置并重新上传'.$remotestatus['message']);
  160. } else {
  161. file_delete($pathname);
  162. $info['url'] = yz_tomedia($pathname, false, $this->remote['type']);
  163. }
  164. }
  165. // dd($type, yz_tomedia($pathname), $ext);
  166. //内容审核
  167. if(app('plugins')->isEnabled('upload-verification')) {
  168. if ($type == 'image') {
  169. //图片审核
  170. if (in_array(strtolower($ext), ['png','jpg','jpeg','bmp','gif','webp','tiff'])) {
  171. $uploadReuslt = do_upload_verificaton(yz_tomedia($pathname), 'img');
  172. if (0 === $uploadReuslt[0]['status']) {
  173. return $this->errorJson($uploadReuslt[0]['msg']);
  174. }
  175. }
  176. } else {
  177. //视频审核
  178. $uploadReuslt = do_upload_verificaton(yz_tomedia($pathname), 'video');
  179. if (0 === $uploadReuslt[0]['status']) {
  180. return $this->errorJson($uploadReuslt[0]['msg']);
  181. }
  182. }
  183. }
  184. $core_attach = CoreAttach::create([
  185. 'uniacid' => $this->uniacid,
  186. 'uid' => \Auth::guard('admin')->user()->uid,
  187. 'filename' => safe_gpc_html(htmlspecialchars_decode($originname, ENT_QUOTES)),
  188. 'attachment' => $pathname ? : '',
  189. 'type' => $type == 'image' ? 1 : ($type == 'audio'||$type == 'voice' ? 2 : 3),
  190. 'module_upload_dir' => $module_upload_dir,
  191. 'group_id' => intval(request()->group_id),
  192. 'upload_type' => $this->remote['type']
  193. ]);
  194. \Log::info('----------上传附件----------', json_encode($info));
  195. if ($core_attach) {
  196. $info['state'] = 'SUCCESS';
  197. $info['state'] = 'SUCCESS';
  198. response()->json($info, 200, ['charset' => 'utf-8'])->send();
  199. exit;
  200. } else {
  201. return $this->errorJson('失败');
  202. }
  203. }
  204. public function image()
  205. {
  206. $year = request()->year;
  207. $month = intval(request()->month);
  208. $page = max(1, intval(request()->page));
  209. $groupid = intval(request()->groupid);
  210. $page_size = 24;
  211. $is_local_image = $this->common['islocal'] == 'local' ? true : false;
  212. if ($page<=1) {
  213. $page = 0;
  214. $offset = ($page)*$page_size;
  215. } else {
  216. $offset = ($page-1)*$page_size;
  217. }
  218. if(!$is_local_image) {
  219. $core_attach = new WechatAttachment;
  220. } else {
  221. $core_attach = new CoreAttach;
  222. }
  223. $core_attach = $core_attach->where('uniacid', $this->uniacid)->where('module_upload_dir', $this->common['module_upload_dir']);
  224. if (!$this->uniacid) {
  225. $core_attach = $core_attach->where('uid', \Auth::guard('admin')->user()->uid);
  226. }
  227. if ($groupid > 0) {
  228. $core_attach = $core_attach->where('group_id', $groupid);
  229. }
  230. if ($groupid == 0) {
  231. $core_attach = $core_attach->where('group_id', -1);
  232. }
  233. if ($year || $month) {
  234. $start_time = $month ? strtotime("{$year}-{$month}-01") : strtotime("{$year}-1-01");
  235. $end_time = $month ? strtotime('+1 month', $start_time) : strtotime('+12 month', $start_time);
  236. $core_attach = $core_attach->where('created_at', '>=', $start_time)->where('created_at', '<=', $end_time);
  237. }
  238. if ($this->common['islocal']) {
  239. $core_attach = $core_attach->where('type', 1);
  240. } else {
  241. $core_attach = $core_attach->where('type', 'image');
  242. }
  243. $core_attach = $core_attach->orderby('created_at', 'desc');
  244. $count = $core_attach->count();
  245. $core_attach = $core_attach->offset($offset)->limit($page_size)->get();
  246. foreach ($core_attach as &$meterial) {
  247. if ($this->common['islocal']) {
  248. $meterial['url'] = yz_tomedia($meterial['attachment']);
  249. unset($meterial['uid']);
  250. } else {
  251. $meterial['attach'] = yz_tomedia($meterial['attachment'], true);
  252. $meterial['url'] = $meterial['attach'];
  253. }
  254. }
  255. $pager = pagination($count, $page, $page_size,'',$context = array('before' => 5, 'after' => 4, 'isajax' => '1'));
  256. $result = array('items' => $core_attach, 'pager' => $pager);
  257. $array = [
  258. 'message' => [
  259. 'erron' => 0,
  260. 'message' => $result
  261. ],
  262. 'redirect' => '',
  263. 'type' => 'ajax'
  264. ];
  265. return \GuzzleHttp\json_encode($array);
  266. }
  267. public function fetch()
  268. {
  269. $url = trim(request()->url);
  270. $size = intval($_FILES['file']['size']);
  271. $resp = ihttp_get($url);
  272. if (!$resp) {
  273. return $this->errorJson('提取文件失败');
  274. }
  275. if ($this->common['type'] == 'image') {
  276. switch ($resp['headers']['Content-Type']) {
  277. case 'application/x-jpg':
  278. case 'image/jpeg':
  279. $ext = 'jpg';
  280. break;
  281. case 'image/png':
  282. $ext = 'png';
  283. break;
  284. case 'image/gif':
  285. $ext = 'gif';
  286. break;
  287. default:
  288. return $this->errorJson('提取资源失败, 资源文件类型错误.');
  289. break;
  290. }
  291. } else {
  292. return $this->errorJson('提取资源失败, 仅支持图片提取.');
  293. }
  294. if (intval($resp['headers']['Content-Length']) > $this->global[$this->common['type'].'_limit'] * 1024) {
  295. return $this->errorJson('上传的媒体文件过大(' . sizecount($size) . ' > ' . sizecount($this->global[$this->common['type'].'_limit'] * 1024));
  296. }
  297. $originname = pathinfo($url, PATHINFO_BASENAME);
  298. $filename = file_random_name(base_path() . '/static/upload/' . $this->common['folder'], $ext);
  299. $pathname = $this->common['folder'] . $filename;
  300. $fullname = base_path() . '/static/upload/' . $pathname;
  301. if (file_put_contents($fullname, $resp['content']) == false) {
  302. return $this->errorJson('提取失败');
  303. }
  304. return $this->saveData($this->common['type'], $fullname, $originname, $ext, $filename, $this->common['module_upload_dir'], $pathname, $this->common['option']);
  305. }
  306. public function errorJson($message = '失败', $error = 1, $data = '')
  307. {
  308. return response()->json([
  309. 'result' => 0,
  310. 'msg' => $message,
  311. 'data' => $data,
  312. 'error' => $error,
  313. 'message' => $message
  314. ], 200, ['charset' => 'utf-8']);
  315. }
  316. public function common()
  317. {
  318. $dest_dir = request()->dest_dir;
  319. $type = in_array(request()->upload_type, array('image','audio','video')) ? request()->upload_type : 'image';
  320. $option = array_elements(array('uploadtype', 'global', 'dest_dir'), $_POST);
  321. $option['width'] = intval($option['width']);
  322. $option['global'] = request()->global;
  323. $islocal = request()->local == 'local';
  324. if (preg_match('/^[a-zA-Z0-9_\/]{0,50}$/', $dest_dir, $out)) {
  325. $dest_dir = trim($dest_dir, '/');
  326. $pieces = explode('/', $dest_dir);
  327. if(count($pieces) > 3){
  328. $dest_dir = '';
  329. }
  330. } else {
  331. $dest_dir = '';
  332. }
  333. $module_upload_dir = '';
  334. if($dest_dir != '') {
  335. $module_upload_dir = sha1($dest_dir);
  336. }
  337. if ($option['global']) {
  338. $folder = "{$type}s/global/";
  339. if ($dest_dir) {
  340. $folder .= '' . $dest_dir . '/';
  341. }
  342. } else {
  343. $folder = "{$type}s/{$this->uniacid}";
  344. if (!$dest_dir) {
  345. $folder .= '/' . date('Y/m/');
  346. } else {
  347. $folder .= '/' . $dest_dir . '/';
  348. }
  349. }
  350. return [
  351. 'dest_dir' => $dest_dir,
  352. 'module_upload_dir' => $module_upload_dir,
  353. 'type' => $type,
  354. 'options' => $option,
  355. 'folder' => $folder,
  356. 'islocal' => $islocal
  357. ];
  358. }
  359. public function delete()
  360. {
  361. $uid = \Auth::guard('admin')->user()->uid;
  362. $is_founder = $uid == '1' ? 1 : 0;
  363. $role = AppUser::where('uid', $uid)->first()['role'];
  364. if (!$is_founder && $role != 'manager' && $role != 'owner') {
  365. return $this->errorJson('您没有权限删除文件');
  366. }
  367. $id = request()->id;
  368. if (!is_array($id)) {
  369. $id = array(intval($id));
  370. }
  371. $id = safe_gpc_array($id);
  372. $core_attach = CoreAttach::where('id', $id);
  373. if (!$this->uniacid) {
  374. $core_attach = $core_attach->where('uid', $uid);
  375. } else {
  376. $core_attach = $core_attach->where('uniacid', $this->uniacid);
  377. }
  378. $core_attach = $core_attach->first();
  379. if ($core_attach['upload_type']) {
  380. $status = file_remote_delete($core_attach['attachment'], $core_attach['upload_type'], $this->remote);
  381. } else {
  382. $status = file_delete($core_attach['attachment']);
  383. }
  384. if (is_error($status)) {
  385. return $this->errorJson($status['message']);
  386. }
  387. $core_attach->delete();
  388. if ($core_attach->trashed()) {
  389. return $this->successJson('删除成功');
  390. } else {
  391. return $this->errorJson('删除数据表数据失败');
  392. }
  393. }
  394. public function video()
  395. {
  396. $server = $this->common['islocal'] ? 'local' : 'perm';
  397. $page_index = max(1, request()->page);
  398. $page_size = 5;
  399. if ($page_index<=1) {
  400. $page_index = 0;
  401. $offset = ($page_index)*$page_size;
  402. } else {
  403. $offset = ($page_index-1)*$page_size;
  404. }
  405. $material_news_list = material_list('video', $server, array('page_index' => $page_index, 'page_size' => $page_size), $this->uniacid, $offset);
  406. $material_list = $material_news_list['material_list'];
  407. $pager = $material_news_list['page'];
  408. foreach ($material_list as &$item) {
  409. $item['createtime'] = $item['created_at']->timestamp;
  410. $item['url'] = yz_tomedia($item['attachment']);
  411. unset($item['uid']);
  412. }
  413. $result = array('items' => $material_list, 'pager' => $pager);
  414. $array = [
  415. 'message' => [
  416. 'erron' => 0,
  417. 'message' => $result
  418. ],
  419. 'redirect' => '',
  420. 'type' => 'ajax'
  421. ];
  422. return \GuzzleHttp\json_encode($array);
  423. }
  424. }