<?php
namespace laytp\library;
use app\model\Files;
use app\service\ConfServiceFacade;
use laytp\traits\Error;
use think\facade\Config;
use think\facade\Env;
class UploadDomain
{
use Error;
public function check($fileName, $fileSize, $fileExt, $fileMime)
{
$allowSize = request()->param('size', ConfServiceFacade::get('system.upload.size'));
if (!$this->checkSize($fileSize, $allowSize)) {
return false;
}
$allowExt = request()->param('mime', ConfServiceFacade::get('system.upload.mime'));
if (!$this->checkExt($fileName, $fileExt, $allowExt)) {
return false;
}
if (!$this->checkMime($fileMime)) {
return false;
}
return true;
}
public function checkSize($fileSize, $allowSize = '')
{
$allowSize = str_replace(' ', '', $allowSize);
$allowUploadSizeConf = ConfServiceFacade::get('system.upload.size');
if (!$allowUploadSizeConf && !$allowSize) {
return true;
}
$maxSize = $allowSize ? $allowSize : $allowUploadSizeConf;
preg_match('/(\d+)(\w+)/', $maxSize, $matches);
$type = strtolower($matches[2]);
$typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3];
$maxSize = (int)$maxSize * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0);
if ($fileSize > $maxSize) {
$this->setError('上传失败,文件大小超过 ' . $allowSize);
return false;
}
return true;
}
public function checkExt($fileName, $fileExt, $allowExt = '')
{
$allowExtArr = explode(',', strtolower($allowExt));
if (in_array($fileExt, ['php', 'html', 'htm'])) {
$this->setError('上传失败,禁止上传php和html文件');
return false;
}
$allowExtConf = ConfServiceFacade::get('system.upload.mime');
if (!$allowExtConf && !$allowExt) {
$this->setError('上传失败,允许上传的文件后缀为空,请到系统配置 - 上传配置进行设置');
return false;
}
if ($allowExt !== '*' && (!in_array($fileExt, $allowExtArr))) {
$this->setError('上传失败,允许上传的文件后缀为' . $allowExt . ',实际上传文件[ ' . $fileName . ' ]的后缀为' . $fileExt);
return false;
}
return true;
}
public function checkMime($fileMime)
{
if (in_array($fileMime, ['text/x-php', 'text/html'])) {
$this->setError('上传失败,禁止上传php和html文件');
return false;
}
return true;
}
static public function addUploadDomain($string, $uploadType = 'local')
{
$uploadDomain = self::getUploadDomain($uploadType, 'via');
/* $string = preg_replace("/(<img .*?src=\")^(?!http)(.*?)(\".*?>+)/is", "\${1}{$uploadDomain}\${2}\${3}", $string);*/
preg_match_all("/(<img .*?src=\")(.*?)(\".*?>+)/is", $string, $matches);
if ($matches && isset($matches['2'])) {
foreach ($matches['2'] as $item) {
if (substr($item, 0, 4) != 'http') {
$string = str_replace($item, $uploadDomain . $item, $string);
}
}
}
$string = preg_replace("/(<video .*?src=\")(.*?)(\".*?>+)/is", "\${1}{$uploadDomain}\${2}\${3}", $string);
$string = preg_replace("/(<source .*?src=\")(.*?)(\".*?>+)/is", "\${1}{$uploadDomain}\${2}\${3}", $string);
$string = preg_replace("/(\!\[\]\()(.*?)(\))/is", "\${1}{$uploadDomain}\${2}\${3}", $string);
return $string;
}
static public function delUploadDomain($string, $uploadType = 'local')
{
$uploadDomain = addcslashes(self::getUploadDomain($uploadType, 'via'), '/');
$string = preg_replace("/(<img .*?src=\"){$uploadDomain}(.*?)(\".*?>+)/is", "\${1}\${2}\${3}", $string);
$string = preg_replace("/(<video .*?src=\"){$uploadDomain}(.*?)(\".*?>+)/is", "\${1}\${2}\${3}", $string);
$string = preg_replace("/(<source .*?src=\"){$uploadDomain}(.*?)(\".*?>+)/is", "\${1}\${2}\${3}", $string);
$string = preg_replace("/(\!\[\]\(){$uploadDomain}(.*?)(\))/is", "\${1}\${2}\${3}", $string);
return $string;
}
static public function getUploadDomain($uploadType = 'local', $viaServer = 'via')
{
$uploadDomain = '';
if ($uploadType === 'local') {
$uploadDomain = Env::get('domain.static', request()->domain() . '/static');
} else if ($uploadType === 'qiniu-kodo') {
$uploadDomain = Env::get('KODO.domain');
} else if ($uploadType === 'ali-oss') {
if ($viaServer === 'via') {
$uploadDomain = Env::get('OSS.domain');
} else {
$uploadDomain = Env::get('STS.domain');
}
}
return $uploadDomain;
}
static public function setLocalPath($data)
{
$uploadDomain = self::getUploadDomain($data['upload_type']);
$uploadType = $data['upload_type'];
$value = $data['path'];
if($uploadType === 'local'){
if($uploadDomain){
$value = '/storage/' . $data['path'];
}else{
$value = '/static' . $value;
}
}
return $value;
}
static public function singleAddUploadDomain($data)
{
$uploadDomain = self::getUploadDomain($data['upload_type'], $data['via_server']);
$value = self::setLocalPath($data);
return $uploadDomain . '/' . ltrim($value, '/');
}
static public function singleDelUploadDomain($data)
{
$uploadDomain = self::getUploadDomain($data['upload_type'], $data['via_server']);
return str_replace($uploadDomain, '', $data['path']);
}
static public function multiJoin($fileIds)
{
try {
$files = Files::where('id', 'in', $fileIds)->select()->toArray();
$idArr = [];
$pathArr = [];
$filenameArr = [];
foreach ($files as $v) {
$idArr[] = $v['id'];
$pathArr[] = $v['path'];
$filenameArr[] = $v['name'];
}
return [
'id' => $fileIds,
'path' => join(', ', $pathArr),
'filename' => join(', ', $filenameArr)
];
} catch (\Exception $e) {
return false;
}
}
public static function getDefaultAvatar()
{
$path = '/admin/images/avatar.jpg';
$uploadDomain = Env::get('domain.static', '/static');
return $uploadDomain . $path;
}
}