Plugs.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | framework
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2018 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://framework.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/framework
  12. // +----------------------------------------------------------------------
  13. namespace app\admin\controller\api;
  14. use library\Controller;
  15. use library\File;
  16. /**
  17. * 后台插件管理
  18. * Class Plugs
  19. * @package app\admin\controller\api
  20. */
  21. class Plugs extends Controller
  22. {
  23. /**
  24. * Plugs constructor.
  25. * @throws \think\Exception
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. if (!\app\admin\service\AuthService::isLogin()) {
  31. $this->error('访问授权失败,请重新登录授权再试!');
  32. }
  33. }
  34. /**
  35. * 文件状态检查
  36. * @throws \think\Exception
  37. * @throws \think\exception\PDOException
  38. */
  39. public function upstate()
  40. {
  41. $ext = strtolower(pathinfo($this->request->post('filename', ''), PATHINFO_EXTENSION));
  42. $name = File::name($this->request->post('md5'), $ext, '', 'strtolower');
  43. // 检查文件是否已上传
  44. $this->safe = $this->getUploadSafe();
  45. if (is_string($siteUrl = File::url($name))) {
  46. $this->success('检测到该文件已经存在,无需再次上传!', [
  47. 'site_url' => $this->safe ? $name : $siteUrl,
  48. ]);
  49. }
  50. // 文件驱动
  51. $file = File::instance($this->getUploadType());
  52. // 生成上传授权参数
  53. $param = [
  54. 'file_url' => $name, 'uptype' => $this->uptype, 'token' => md5($name . session_id()),
  55. 'site_url' => $file->base($name), 'server' => $file->upload(), 'safe' => $this->safe,
  56. ];
  57. if (strtolower($this->uptype) === 'qiniu') {
  58. $auth = new \Qiniu\Auth(sysconf('storage_qiniu_access_key'), sysconf('storage_qiniu_secret_key'));
  59. $param['token'] = $auth->uploadToken(sysconf('storage_qiniu_bucket'), $name, 3600, [
  60. 'returnBody' => json_encode(['code' => 1, 'data' => ['site_url' => $file->base($name)]], JSON_UNESCAPED_UNICODE),
  61. ]);
  62. } elseif (strtolower($this->uptype) === 'oss') {
  63. $param['OSSAccessKeyId'] = sysconf('storage_oss_keyid');
  64. $param['policy'] = base64_encode(json_encode(['conditions' => [['content-length-range', 0, 1048576000]], 'expiration' => gmdate("Y-m-d\TH:i:s\Z", time() + 3600)]));
  65. $param['signature'] = base64_encode(hash_hmac('sha1', $param['policy'], sysconf('storage_oss_secret'), true));
  66. }
  67. $this->error('未检测到文件,需要上传完整的文件!', $param);
  68. }
  69. /**
  70. * 文件上传
  71. * @return mixed
  72. * @throws \think\Exception
  73. * @throws \think\exception\PDOException
  74. */
  75. public function upfile()
  76. {
  77. $this->safe = $this->getUploadSafe();
  78. $this->uptype = $this->getUploadType();
  79. $this->mode = $this->request->get('mode', 'one');
  80. $this->field = $this->request->get('field', 'file');
  81. $this->types = $this->request->get('type', 'jpg,png');
  82. $this->mimes = File::mine($this->types);
  83. $this->fetch();
  84. }
  85. /**
  86. * WebUpload 文件上传
  87. * @throws \think\Exception
  88. * @throws \think\exception\PDOException
  89. */
  90. public function upload()
  91. {
  92. // 文件接收
  93. if (!($file = $this->getUploadFile()) || empty($file)) {
  94. $this->error('文件上传异常,文件可能过大或未上传!');
  95. }
  96. if (!$file->checkExt(strtolower(sysconf('storage_local_exts')))) {
  97. $this->error('文件上传类型受限,请在后台配置!');
  98. }
  99. if ($file->checkExt('php')) {
  100. $this->error('可执行文件禁止上传到本地服务器!');
  101. }
  102. // 唯一名称
  103. $ext = strtolower(pathinfo($file->getInfo('name'), PATHINFO_EXTENSION));
  104. $name = File::name($this->request->post('md5'), $ext, '', 'strtolower');
  105. // Token 验证
  106. if ($this->request->post('token') !== md5($name . session_id())) {
  107. $this->error('文件上传验证失败,请刷新页面重新上传!');
  108. }
  109. $this->safe = $this->getUploadSafe();
  110. $pathinfo = pathinfo(File::instance('local')->path($name, $this->safe));
  111. if ($file->move($pathinfo['dirname'], $pathinfo['basename'], true)) {
  112. if (is_array($info = File::instance('local')->info($name, $this->safe)) && isset($info['url'])) {
  113. $this->success('文件上传成功!', ['site_url' => $this->safe ? $name : $info['url']]);
  114. }
  115. }
  116. $this->error('文件上传失败,请稍候再试!');
  117. }
  118. /**
  119. * Plupload 插件上传文件
  120. * @return \think\response\Json
  121. * @throws \think\Exception
  122. * @throws \think\exception\PDOException
  123. */
  124. public function plupload()
  125. {
  126. if (!($file = $this->getUploadFile()) || empty($file)) {
  127. return json(['uploaded' => false, 'error' => ['message' => '文件上传异常,文件可能过大或未上传']]);
  128. }
  129. if (!$file->checkExt(strtolower(sysconf('storage_local_exts')))) {
  130. return json(['uploaded' => false, 'error' => ['message' => '文件上传类型受限,请在后台配置']]);
  131. }
  132. if ($file->checkExt('php')) {
  133. return json(['uploaded' => false, 'error' => ['message' => '可执行文件禁止上传到本地服务器']]);
  134. }
  135. $this->safe = $this->getUploadSafe();
  136. $this->uptype = $this->getUploadType();
  137. $this->ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
  138. $name = File::name($file->getPathname(), $this->ext, '', 'md5_file');
  139. $info = File::instance($this->uptype)->save($name, file_get_contents($file->getRealPath()), $this->safe);
  140. if (is_array($info) && isset($info['url'])) {
  141. return json(['uploaded' => true, 'filename' => $name, 'url' => $this->safe ? $name : $info['url']]);
  142. }
  143. return json(['uploaded' => false, 'error' => ['message' => '文件处理失败,请稍候再试!']]);
  144. }
  145. /**
  146. * 获取文件上传方式
  147. * @return string
  148. * @throws \think\Exception
  149. * @throws \think\exception\PDOException
  150. */
  151. private function getUploadType()
  152. {
  153. $this->uptype = input('uptype');
  154. if (!in_array($this->uptype, ['local', 'oss', 'qiniu'])) {
  155. $this->uptype = sysconf('storage_type');
  156. }
  157. return $this->uptype;
  158. }
  159. /**
  160. * 获取上传安全模式
  161. * @return boolean
  162. */
  163. private function getUploadSafe()
  164. {
  165. return $this->safe = boolval(input('safe'));
  166. }
  167. /**
  168. * 获取本地文件对象
  169. * @return \think\File
  170. */
  171. private function getUploadFile()
  172. {
  173. try {
  174. return $this->request->file('file');
  175. } catch (\Exception $e) {
  176. $this->error(lang($e->getMessage()));
  177. }
  178. }
  179. /**
  180. * 系统选择器图标
  181. * @return mixed
  182. */
  183. public function icon()
  184. {
  185. $this->title = '图标选择器';
  186. $this->field = input('field', 'icon');
  187. $this->fetch();
  188. }
  189. }