UploadDomain.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace laytp\library;
  3. use app\model\Files;
  4. use app\service\ConfServiceFacade;
  5. use laytp\traits\Error;
  6. use think\facade\Config;
  7. use think\facade\Env;
  8. /**
  9. * 上传文件域名前缀处理类
  10. */
  11. class UploadDomain
  12. {
  13. use Error;
  14. /**
  15. * 检测上传的文件
  16. * @param $fileName
  17. * @param $fileSize
  18. * @param $fileExt
  19. * @param $fileMime
  20. * @return bool
  21. */
  22. public function check($fileName, $fileSize, $fileExt, $fileMime)
  23. {
  24. $allowSize = request()->param('size', ConfServiceFacade::get('system.upload.size'));
  25. if (!$this->checkSize($fileSize, $allowSize)) {
  26. return false;
  27. }
  28. $allowExt = request()->param('mime', ConfServiceFacade::get('system.upload.mime'));
  29. if (!$this->checkExt($fileName, $fileExt, $allowExt)) {
  30. return false;
  31. }
  32. // if (!$this->checkMime($fileMime)) {
  33. // return false;
  34. // }
  35. return true;
  36. }
  37. /**
  38. * 检测上传文件大小
  39. * @param $fileSize
  40. * @param string $allowSize
  41. * @return bool
  42. */
  43. public function checkSize($fileSize, $allowSize = '')
  44. {
  45. $allowSize = str_replace(' ', '', $allowSize);
  46. $allowUploadSizeConf = ConfServiceFacade::get('system.upload.size');
  47. // 没有配置,也没有传入上传文件大小限制参数,即程序上不限制上传文件大小
  48. if (!$allowUploadSizeConf && !$allowSize) {
  49. return true;
  50. }
  51. $maxSize = $allowSize ? $allowSize : $allowUploadSizeConf;
  52. preg_match('/(\d+)(\w+)/', $maxSize, $matches);
  53. $type = strtolower($matches[2]);
  54. $typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3];
  55. $maxSize = (int)$maxSize * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0);
  56. if ($fileSize > $maxSize) {
  57. $this->setError('上传失败,文件大小超过 ' . $allowSize);
  58. return false;
  59. }
  60. return true;
  61. }
  62. /**
  63. * 检测上传文件后缀
  64. * @param $fileName
  65. * @param $fileExt
  66. * @param string $allowExt
  67. * @return bool
  68. */
  69. public function checkExt($fileName, $fileExt, $allowExt = '')
  70. {
  71. $allowExtArr = explode(',', strtolower($allowExt));
  72. //禁止上传PHP和HTML文件
  73. if (in_array($fileExt, ['php', 'html', 'htm'])) {
  74. $this->setError('上传失败,禁止上传php和html文件');
  75. return false;
  76. }
  77. //验证文件后缀
  78. $allowExtConf = ConfServiceFacade::get('system.upload.mime');
  79. // 没有配置,也没有传入上传文件大小限制参数,即程序上不限制上传文件大小
  80. if (!$allowExtConf && !$allowExt) {
  81. $this->setError('上传失败,允许上传的文件后缀为空,请到系统配置 - 上传配置进行设置');
  82. return false;
  83. }
  84. if ($allowExt !== '*' && (!in_array($fileExt, $allowExtArr))) {
  85. $this->setError('上传失败,允许上传的文件后缀为' . $allowExt . ',实际上传文件[ ' . $fileName . ' ]的后缀为' . $fileExt);
  86. return false;
  87. }
  88. return true;
  89. }
  90. /**
  91. * 检测上传文件类型
  92. * @param $fileMime
  93. * @return bool
  94. */
  95. public function checkMime($fileMime)
  96. {
  97. //禁止上传PHP和HTML文件
  98. if (in_array($fileMime, ['text/x-php', 'text/html'])) {
  99. $this->setError('上传失败,禁止上传php和html文件');
  100. return false;
  101. }
  102. return true;
  103. }
  104. /**
  105. * 编辑器内容,添加上传文件的域名前缀,一般提供给数据模型层getFiledAttr方法调用
  106. * @param $string
  107. * @param $uploadType
  108. * @return string|string[]|null
  109. */
  110. static public function addUploadDomain($string, $uploadType = 'local')
  111. {
  112. $defaultType = ConfServiceFacade::get('system.upload.defaultType', 'local');
  113. if($uploadType == 'default') $uploadType = $defaultType;
  114. $uploadDomain = self::getUploadDomain($uploadType, 'via');
  115. //ueditor编辑器正则替换所有的图片、视频、音频
  116. /* $string = preg_replace("/(<img .*?src=\")^(?!http)(.*?)(\".*?>+)/is", "\${1}{$uploadDomain}\${2}\${3}", $string);*/
  117. preg_match_all("/(<img .*?src=\")(.*?)(\".*?>+)/is", $string, $matches);
  118. if ($matches && isset($matches['2'])) {
  119. foreach ($matches['2'] as $item) {
  120. if (substr($item, 0, 4) != 'http') {
  121. $string = str_replace($item, $uploadDomain . $item, $string);
  122. }
  123. }
  124. }
  125. $string = preg_replace("/(<video .*?src=\")(.*?)(\".*?>+)/is", "\${1}{$uploadDomain}\${2}\${3}", $string);
  126. $string = preg_replace("/(<source .*?src=\")(.*?)(\".*?>+)/is", "\${1}{$uploadDomain}\${2}\${3}", $string);
  127. //meditor编辑器正则替换所有的图片
  128. $string = preg_replace("/(\!\[\]\()(.*?)(\))/is", "\${1}{$uploadDomain}\${2}\${3}", $string);
  129. return $string;
  130. }
  131. /**
  132. * 编辑器内容,删除上传文件的域名前缀,一般提供给控制器添加和编辑方法调用
  133. * @param $string
  134. * @param $uploadType
  135. * @return string|string[]|null
  136. */
  137. static public function delUploadDomain($string, $uploadType = 'local')
  138. {
  139. $defaultType = ConfServiceFacade::get('system.upload.defaultType', 'local');
  140. if($uploadType == 'default') $uploadType = $defaultType;
  141. $uploadDomain = addcslashes(self::getUploadDomain($uploadType, 'via'), '/');
  142. //ueditor编辑器正则替换所有的图片、视频、音频
  143. $string = preg_replace("/(<img .*?src=\"){$uploadDomain}(.*?)(\".*?>+)/is", "\${1}\${2}\${3}", $string);
  144. $string = preg_replace("/(<video .*?src=\"){$uploadDomain}(.*?)(\".*?>+)/is", "\${1}\${2}\${3}", $string);
  145. $string = preg_replace("/(<source .*?src=\"){$uploadDomain}(.*?)(\".*?>+)/is", "\${1}\${2}\${3}", $string);
  146. //meditor编辑器正则替换所有的图片
  147. $string = preg_replace("/(\!\[\]\(){$uploadDomain}(.*?)(\))/is", "\${1}\${2}\${3}", $string);
  148. return $string;
  149. }
  150. /**
  151. * 获取文件上传后访问的域名
  152. * @param string $uploadType 上传方式
  153. * @param string $viaServer 上传是否经过服务器
  154. * @return mixed|string
  155. */
  156. static public function getUploadDomain($uploadType = 'local', $viaServer = 'via')
  157. {
  158. $uploadDomain = '';
  159. if ($uploadType === 'local') {
  160. $uploadDomain = Env::get('domain.static', request()->domain() . STATIC_PATH);
  161. } else if ($uploadType === 'qiniu-kodo') {
  162. $uploadDomain = ConfServiceFacade::get('plugin.qiniu_kodo.domain');
  163. } else if ($uploadType === 'ali-oss') {
  164. if ($viaServer === 'via') {
  165. $uploadDomain = ConfServiceFacade::get('plugin.ali_oss.domain');
  166. } else {
  167. $uploadDomain = ConfServiceFacade::get('plugin.ali_oss_sts.domain');
  168. }
  169. }
  170. return $uploadDomain;
  171. }
  172. // 设置上传方式为本地时的path,提供给当前类的singleAddUploadDomain方法调用
  173. static public function setLocalPath($data)
  174. {
  175. $uploadDomain = self::getUploadDomain($data['upload_type']);
  176. $uploadType = $data['upload_type'];
  177. $value = $data['path'];
  178. if($uploadType === 'local'){
  179. if($uploadDomain){
  180. $value = '/storage/' . $data['path'];
  181. }else{
  182. $value = '/static' . $value;
  183. }
  184. }
  185. return $value;
  186. }
  187. /**
  188. * 上传控件,单个,添加域名前缀,提供给Files模型层getPathAttr方法调用
  189. * @param $data
  190. * @return string
  191. */
  192. static public function singleAddUploadDomain($data)
  193. {
  194. $uploadDomain = self::getUploadDomain($data['upload_type'], $data['via_server']);
  195. $value = self::setLocalPath($data);
  196. return $uploadDomain . '/' . ltrim($value, '/');
  197. }
  198. /**
  199. * 上传控件,单个,保存path时,删除掉域名前缀,此方法仅FileController的add方法调用
  200. * @param $data
  201. * @return mixed
  202. */
  203. static public function singleDelUploadDomain($data)
  204. {
  205. $uploadDomain = self::getUploadDomain($data['upload_type'], $data['via_server']);
  206. return str_replace($uploadDomain, '', $data['path']);
  207. }
  208. /**
  209. * 上传控件,多个,整理成前端需要的数据,以[, ]组合的字符串
  210. * @param $fileIds
  211. * @return array|boolean
  212. */
  213. static public function multi($fileIds)
  214. {
  215. try {
  216. $files = Files::where('id', 'in', $fileIds)->select()->toArray();
  217. $idArr = [];
  218. $pathArr = [];
  219. $filenameArr = [];
  220. foreach ($files as $v) {
  221. $idArr[] = $v['id'];
  222. $pathArr[] = $v['path'];
  223. $filenameArr[] = $v['name'];
  224. }
  225. return $pathArr;
  226. } catch (\Exception $e) {
  227. return false;
  228. }
  229. }
  230. /**
  231. * 上传控件,多个,整理成前端需要的数据,以[, ]组合的字符串
  232. * @param $fileIds
  233. * @return array|boolean
  234. */
  235. static public function multiJoin($fileIds)
  236. {
  237. try {
  238. $files = Files::where('id', 'in', $fileIds)->select()->toArray();
  239. $idArr = [];
  240. $pathArr = [];
  241. $filenameArr = [];
  242. foreach ($files as $v) {
  243. $idArr[] = $v['id'];
  244. $pathArr[] = $v['path'];
  245. $filenameArr[] = $v['name'];
  246. }
  247. return [
  248. 'id' => $fileIds,
  249. 'path' => join(', ', $pathArr),
  250. 'filename' => join(', ', $filenameArr)
  251. ];
  252. } catch (\Exception $e) {
  253. return false;
  254. }
  255. }
  256. /**
  257. * 获取默认的用户头像
  258. * @return string
  259. */
  260. public static function getDefaultAvatar()
  261. {
  262. $path = '/admin/images/avatar.jpg';
  263. $uploadDomain = request()->domain() . STATIC_PATH;
  264. return $uploadDomain . $path;
  265. }
  266. }