chore: 添加静态资源文件和版本配置文件
添加大量图片、字体、音频等静态资源文件,包括用户界面图片、模板文件和安装界面素材 添加系统版本配置文件(.version)和常量定义文件(.constant) 更新供应商依赖文件,包括Symfony VarDumper组件和相关许可证文件 添加验证码配置文件(captcha.php)和.gitignore规则
This commit is contained in:
576
crmeb/services/upload/extend/cos/Client.php
Normal file
576
crmeb/services/upload/extend/cos/Client.php
Normal file
@@ -0,0 +1,576 @@
|
||||
<?php
|
||||
/**
|
||||
* +----------------------------------------------------------------------
|
||||
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
* +----------------------------------------------------------------------
|
||||
* | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
|
||||
* +----------------------------------------------------------------------
|
||||
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
* +----------------------------------------------------------------------
|
||||
* | Author: CRMEB Team <admin@crmeb.com>
|
||||
* +----------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
namespace crmeb\services\upload\extend\cos;
|
||||
|
||||
use crmeb\exceptions\UploadException;
|
||||
use crmeb\services\wechat\util\XML;
|
||||
|
||||
/**
|
||||
* Class Client
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/9/29
|
||||
* @package crmeb\services\upload\extend\cos
|
||||
*/
|
||||
class Client
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $accessKey;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $secretKey;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $appid;
|
||||
|
||||
/**
|
||||
* @var mixed|string
|
||||
*/
|
||||
protected $bucket;
|
||||
|
||||
/**
|
||||
* @var mixed|string
|
||||
*/
|
||||
protected $region;
|
||||
|
||||
/**
|
||||
* @var mixed|string
|
||||
*/
|
||||
protected $uploadUrl;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $action = '';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $response = ['content' => null, 'code' => 200, 'header' => []];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $request = ['header' => [], 'body' => [], 'host' => ''];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $cosacl = 'public-read';
|
||||
|
||||
/**
|
||||
* Client constructor.
|
||||
* @param array $config
|
||||
*/
|
||||
public function __construct(array $config)
|
||||
{
|
||||
$this->accessKey = $config['accessKey'] ?? '';
|
||||
$this->secretKey = $config['secretKey'] ?? '';
|
||||
$this->appid = $config['appid'] ?? '';
|
||||
$this->bucket = $config['bucket'] ?? '';
|
||||
$this->region = $config['region'] ?? 'ap-chengdu';
|
||||
$this->uploadUrl = $config['uploadUrl'] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实际请求
|
||||
* @return array
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/10/17
|
||||
*/
|
||||
public function getResponse()
|
||||
{
|
||||
$response = $this->response;
|
||||
|
||||
$this->response = ['content' => null, 'http_code' => 200, 'header' => []];
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/10/17
|
||||
*/
|
||||
public function getRequest()
|
||||
{
|
||||
$request = $this->request;
|
||||
|
||||
$this->request = ['header' => [], 'body' => [], 'host' => ''];
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼接请求地址
|
||||
* @return string
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/9/29
|
||||
*/
|
||||
protected function makeUpUrl()
|
||||
{
|
||||
return $this->bucket . '.cos.' . $this->region . '.myqcloud.com';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/9/29
|
||||
*/
|
||||
protected function ssl()
|
||||
{
|
||||
return strstr($this->uploadUrl, 'https://') !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查参数
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/9/29
|
||||
*/
|
||||
protected function checkOptions()
|
||||
{
|
||||
if (!$this->bucket) {
|
||||
throw new UploadException('请传入桶名');
|
||||
}
|
||||
if (!$this->region) {
|
||||
throw new UploadException('请传入所属地域');
|
||||
}
|
||||
if (!$this->accessKey) {
|
||||
throw new UploadException('请传入SecretId');
|
||||
}
|
||||
if (!$this->secretKey) {
|
||||
throw new UploadException('请传入SecretKey');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* @param string $key
|
||||
* @param $body
|
||||
* @return string[]
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/9/29
|
||||
*/
|
||||
public function putObject(string $key, $body)
|
||||
{
|
||||
|
||||
$this->checkOptions();
|
||||
|
||||
$url = $this->makeUpUrl();
|
||||
|
||||
$header = [
|
||||
'Content-Type' => 'image/jpeg',
|
||||
'x-cos-acl' => $this->cosacl,
|
||||
'Content-MD5' => base64_encode(md5($body, true)),
|
||||
'Host' => $url
|
||||
];
|
||||
|
||||
$imageUrl = ($this->ssl() ? 'https://' : 'http://') . $url . '/' . $key;
|
||||
|
||||
$res = $this->request($imageUrl, 'PUT', ['body' => $body], $header);
|
||||
|
||||
if ($res && !empty($res['Message'])) {
|
||||
throw new UploadException($res['Message']);
|
||||
}
|
||||
|
||||
return [
|
||||
'name' => $key,
|
||||
'path' => $imageUrl
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param string $bucket
|
||||
* @param string $key
|
||||
* @return array|false
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/10/19
|
||||
*/
|
||||
public function deleteObject(string $bucket, string $key)
|
||||
{
|
||||
$url = $this->getRequestHost($bucket);
|
||||
|
||||
$header = [
|
||||
'Host' => $url
|
||||
];
|
||||
|
||||
$res = $this->request('https://' . $url . '/' . $key, 'delete', [], $header);
|
||||
|
||||
if ($res && !empty($res['Message'])) {
|
||||
throw new UploadException($res['Message']);
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取桶列表
|
||||
* @return array|false|\SimpleXMLElement|string
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/10/19
|
||||
*/
|
||||
public function listBuckets()
|
||||
{
|
||||
$url = 'service.cos.myqcloud.com';
|
||||
|
||||
$header = [
|
||||
'Host' => $url
|
||||
];
|
||||
|
||||
$res = $this->request('https://' . $url . '/', 'get', [], $header);
|
||||
|
||||
if ($res && !empty($res['Message'])) {
|
||||
throw new UploadException($res['Message']);
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测桶,不存在返回true
|
||||
* @param string $bucket
|
||||
* @param string $region
|
||||
* @return bool
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/10/17
|
||||
*/
|
||||
public function headBucket(string $bucket, string $region = '')
|
||||
{
|
||||
$url = $this->getRequestHost($bucket, $region);
|
||||
|
||||
$header = [
|
||||
'Host' => $url
|
||||
];
|
||||
|
||||
$this->request('https://' . $url, 'head', [], $header);
|
||||
|
||||
$response = $this->getResponse();
|
||||
|
||||
return $response['code'] == 404;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建桶
|
||||
* @param string $bucket
|
||||
* @param string $region
|
||||
* @param string $acl
|
||||
* @return array|false|\SimpleXMLElement|string
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/10/17
|
||||
*/
|
||||
public function createBucket(string $bucket, string $region = '', string $acl = 'public-read')
|
||||
{
|
||||
return $this->noBodyRequest('put', $bucket, $region, $acl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 组合成xml
|
||||
* @param array $data
|
||||
* @param string $root
|
||||
* @param string $itemKey
|
||||
* @return string
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/10/17
|
||||
*/
|
||||
protected function xmlBuild(array $xmlAttr, string $root = 'xml', string $itemKey = 'item')
|
||||
{
|
||||
$xml = '<' . $root . '>';
|
||||
$xml .= '<' . $itemKey . '>';
|
||||
|
||||
foreach ($xmlAttr as $kk => $vv) {
|
||||
if (is_array($vv)) {
|
||||
foreach ($vv as $v) {
|
||||
$xml .= '<' . $kk . '>' . $v . '</' . $kk . '>';
|
||||
}
|
||||
} else {
|
||||
$xml .= '<' . $kk . '>' . $vv . '</' . $kk . '>';
|
||||
}
|
||||
}
|
||||
$xml .= '</' . $itemKey . '>';
|
||||
$xml .= '</' . $root . '>';
|
||||
|
||||
return $xml;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置跨域
|
||||
* @param string $bucket
|
||||
* @param string $region
|
||||
* @param array $data
|
||||
* @return string
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/10/17
|
||||
*/
|
||||
public function putBucketCors(string $bucket, array $data, string $region = '')
|
||||
{
|
||||
$url = $this->getRequestHost($bucket, $region);
|
||||
|
||||
$xml = $this->xmlBuild($data, 'CORSConfiguration', 'CORSRule');
|
||||
|
||||
$header = [
|
||||
'Host' => $url,
|
||||
'Content-Type' => 'application/xml',
|
||||
'Content-Length' => strlen($xml),
|
||||
'Content-MD5' => base64_encode(md5($xml, true))
|
||||
];
|
||||
|
||||
$res = $this->request('https://' . $url . '/?cors', 'put', ['xml' => $xml], $header);
|
||||
|
||||
if ($res && !empty($res['Message'])) {
|
||||
throw new UploadException($res['Message']);
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
* @param string $name
|
||||
* @param string $region
|
||||
* @return array|false|\SimpleXMLElement|string
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/10/17
|
||||
*/
|
||||
public function deleteBucket(string $name, string $region = '')
|
||||
{
|
||||
return $this->noBodyRequest('delete', $name, $region);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取桶下的
|
||||
* @param string $name
|
||||
* @param string $region
|
||||
* @return array|false|\SimpleXMLElement|string
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/10/17
|
||||
*/
|
||||
public function getBucketDomain(string $name, string $region = '')
|
||||
{
|
||||
$this->action = 'domain';
|
||||
return $this->noBodyRequest('get', $name, $region);
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定域名
|
||||
* @param string $bucket
|
||||
* @param string $region
|
||||
* @param array $data
|
||||
* @return array|false|\SimpleXMLElement|string
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/10/19
|
||||
*/
|
||||
public function putBucketDomain(string $bucket, string $region, array $data)
|
||||
{
|
||||
$url = $this->getRequestHost($bucket, $region);
|
||||
|
||||
$xml = $this->xmlBuild($data, 'DomainConfiguration', 'DomainRule');
|
||||
|
||||
$header = [
|
||||
'Host' => $url,
|
||||
'Content-Type' => 'application/xml',
|
||||
'Content-Length' => strlen($xml),
|
||||
'Content-MD5' => base64_encode(md5($xml, true))
|
||||
];
|
||||
|
||||
$res = $this->request('https://' . $url . '/?domain', 'put', ['xml' => $xml], $header);
|
||||
|
||||
if ($res && !empty($res['Message'])) {
|
||||
throw new UploadException($res['Message']);
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $bucket
|
||||
* @param string $region
|
||||
* @return string
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/10/17
|
||||
*/
|
||||
protected function getRequestHost(string $bucket, string $region = '')
|
||||
{
|
||||
if (!$this->accessKey) {
|
||||
throw new UploadException('请传入SecretId');
|
||||
}
|
||||
if (!$this->secretKey) {
|
||||
throw new UploadException('请传入SecretKey');
|
||||
}
|
||||
|
||||
if (strstr($bucket, '-') === false) {
|
||||
$bucket = $bucket . '-' . $this->appid;
|
||||
}
|
||||
|
||||
return $bucket . '.cos.' . ($region ?: $this->region) . '.myqcloud.com';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
* @param string $bucket
|
||||
* @param string $region
|
||||
* @param string|null $acl
|
||||
* @return array|false|\SimpleXMLElement|string
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/10/17
|
||||
*/
|
||||
public function noBodyRequest(string $method, string $bucket, string $region = '', string $acl = null, bool $isExc = true)
|
||||
{
|
||||
|
||||
$url = $this->getRequestHost($bucket, $region);
|
||||
|
||||
$header = [
|
||||
'Host' => $url
|
||||
];
|
||||
|
||||
if ($acl) {
|
||||
$header['x-cos-acl'] = $acl;
|
||||
}
|
||||
|
||||
if (in_array($method, ['put', 'post'])) {
|
||||
$header['Content-Length'] = 0;
|
||||
}
|
||||
|
||||
$res = $this->request('https://' . $url . '/' . ($this->action ? '?' . $this->action : ''), $method, [], $header);
|
||||
$this->action = '';
|
||||
|
||||
if ($isExc) {
|
||||
if ($res && !empty($res['Message'])) {
|
||||
throw new UploadException($res['Message']);
|
||||
}
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发起请求
|
||||
* @param string $url
|
||||
* @param string $method
|
||||
* @param array $data
|
||||
* @param array $header
|
||||
* @param int $timeout
|
||||
* @return array|false|\SimpleXMLElement|string
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/9/29
|
||||
*/
|
||||
public function request(string $url, string $method, array $data, array $header = [], int $timeout = 5)
|
||||
{
|
||||
|
||||
$this->request['body'] = $data;
|
||||
$this->request['host'] = $url;
|
||||
|
||||
|
||||
$urlAttr = parse_url($url);
|
||||
$curl = curl_init($url);
|
||||
$method = strtoupper($method);
|
||||
//请求方式
|
||||
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
|
||||
|
||||
//超时时间
|
||||
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
|
||||
//设置header头
|
||||
|
||||
$header = array_merge($header, $this->getSign($url, $method, $urlAttr['path'] ?? '', [], $header));
|
||||
|
||||
$this->request['header'] = $header;
|
||||
|
||||
$clientHeader = [];
|
||||
foreach ($header as $key => $item) {
|
||||
$clientHeader[] = $key . ':' . $item;
|
||||
}
|
||||
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, $clientHeader);
|
||||
|
||||
|
||||
curl_setopt($curl, CURLOPT_FAILONERROR, false);
|
||||
//返回抓取数据
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||
//输出header头信息
|
||||
curl_setopt($curl, CURLOPT_HEADER, true);
|
||||
//TRUE 时追踪句柄的请求字符串,从 PHP 5.1.3 开始可用。这个很关键,就是允许你查看请求header
|
||||
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
|
||||
//https请求
|
||||
if (1 == strpos("$" . $url, "https://")) {
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
||||
}
|
||||
|
||||
//post请求
|
||||
if ($method == 'PUT' && !empty($data['body'])) {
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
||||
// 注意这里的'file'是上传地址指定的key名
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $data['body']);
|
||||
}
|
||||
|
||||
if (!empty($data['xml'])) {
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $data['xml']);
|
||||
}
|
||||
|
||||
[$content, $status] = [curl_exec($curl), curl_getinfo($curl), curl_close($curl)];
|
||||
|
||||
$content = trim(substr($content, $status['header_size']));
|
||||
|
||||
$this->response['content'] = $content;
|
||||
$this->response['code'] = $status['http_code'];
|
||||
$this->response['header'] = $status;
|
||||
|
||||
$res = XML::parse($content);
|
||||
if ($res) {
|
||||
return $res;
|
||||
}
|
||||
return (intval($status["http_code"]) === 200) ? $content : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取签名
|
||||
* @param string $method
|
||||
* @param string $urlPath
|
||||
* @param array $query
|
||||
* @param array $headers
|
||||
* @return array
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/9/27
|
||||
*/
|
||||
public function getSign(string $url, string $method, string $urlPath, array $query = [], array $headers = [])
|
||||
{
|
||||
return (new Signature($this->accessKey, $this->secretKey, ['signHost' => $url]))->signRequest($method, $urlPath, $query, $headers);
|
||||
}
|
||||
}
|
||||
77
crmeb/services/upload/extend/cos/Scope.php
Normal file
77
crmeb/services/upload/extend/cos/Scope.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
/**
|
||||
* +----------------------------------------------------------------------
|
||||
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
* +----------------------------------------------------------------------
|
||||
* | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
|
||||
* +----------------------------------------------------------------------
|
||||
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
* +----------------------------------------------------------------------
|
||||
* | Author: CRMEB Team <admin@crmeb.com>
|
||||
* +----------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
namespace crmeb\services\upload\extend\cos;
|
||||
|
||||
|
||||
class Scope
|
||||
{
|
||||
protected $action;
|
||||
protected $bucket;
|
||||
protected $region;
|
||||
protected $resourcePrefix;
|
||||
protected $effect = 'allow';
|
||||
|
||||
public function __construct($action, $bucket, $region, $resourcePrefix)
|
||||
{
|
||||
$this->action = $action;
|
||||
$this->bucket = $bucket;
|
||||
$this->region = $region;
|
||||
$this->resourcePrefix = $resourcePrefix;
|
||||
}
|
||||
|
||||
public function set_effect($isAllow)
|
||||
{
|
||||
if ($isAllow) {
|
||||
$this->effect = 'allow';
|
||||
} else {
|
||||
$this->effect = 'deny';
|
||||
}
|
||||
}
|
||||
|
||||
public function get_action()
|
||||
{
|
||||
if ($this->action == null) {
|
||||
throw new \Exception("action == null");
|
||||
}
|
||||
return $this->action;
|
||||
}
|
||||
|
||||
public function get_resource()
|
||||
{
|
||||
if ($this->bucket == null) {
|
||||
throw new \Exception("bucket == null");
|
||||
}
|
||||
if ($this->region == null) {
|
||||
throw new \Exception("region == null");
|
||||
}
|
||||
if ($this->resourcePrefix == null) {
|
||||
throw new \Exception("resourcePrefix == null");
|
||||
}
|
||||
$index = strripos($this->bucket, '-');
|
||||
if ($index < 0) {
|
||||
throw new Exception("bucket is invalid: " . $this->bucket);
|
||||
}
|
||||
$appid = substr($this->bucket, $index + 1);
|
||||
if (!(strpos($this->resourcePrefix, '/') === 0)) {
|
||||
$this->resourcePrefix = '/' . $this->resourcePrefix;
|
||||
}
|
||||
return 'qcs::cos:' . $this->region . ':uid/' . $appid . ':' . $this->bucket . $this->resourcePrefix;
|
||||
}
|
||||
|
||||
public function get_effect()
|
||||
{
|
||||
return $this->effect;
|
||||
}
|
||||
|
||||
}
|
||||
213
crmeb/services/upload/extend/cos/Signature.php
Normal file
213
crmeb/services/upload/extend/cos/Signature.php
Normal file
@@ -0,0 +1,213 @@
|
||||
<?php
|
||||
/**
|
||||
* +----------------------------------------------------------------------
|
||||
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
* +----------------------------------------------------------------------
|
||||
* | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
|
||||
* +----------------------------------------------------------------------
|
||||
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
* +----------------------------------------------------------------------
|
||||
* | Author: CRMEB Team <admin@crmeb.com>
|
||||
* +----------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
namespace crmeb\services\upload\extend\cos;
|
||||
|
||||
/**
|
||||
* Class 生成签名
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/9/26
|
||||
* @package crmeb\services\upload\extend\cos
|
||||
*/
|
||||
class Signature
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $accessKey;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $secretKey;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* Signature constructor.
|
||||
* @param string $accessKey
|
||||
* @param string $secretKey
|
||||
* @param array $options
|
||||
* @param string $token
|
||||
*/
|
||||
public function __construct(string $accessKey, string $secretKey, array $options = [], string $token = '')
|
||||
{
|
||||
$this->accessKey = $accessKey;
|
||||
$this->secretKey = $secretKey;
|
||||
$this->options = $options;
|
||||
$this->token = $token;
|
||||
$this->signHeader = [
|
||||
'cache-control',
|
||||
'content-disposition',
|
||||
'content-encoding',
|
||||
'content-length',
|
||||
'content-md5',
|
||||
'content-type',
|
||||
'expect',
|
||||
'expires',
|
||||
'host',
|
||||
'if-match',
|
||||
'if-modified-since',
|
||||
'if-none-match',
|
||||
'if-unmodified-since',
|
||||
'origin',
|
||||
'range',
|
||||
'response-cache-control',
|
||||
'response-content-disposition',
|
||||
'response-content-encoding',
|
||||
'response-content-language',
|
||||
'response-content-type',
|
||||
'response-expires',
|
||||
'transfer-encoding',
|
||||
'versionid',
|
||||
];
|
||||
date_default_timezone_set('Asia/Shanghai');
|
||||
}
|
||||
|
||||
public function needCheckHeader($header)
|
||||
{
|
||||
if ($this->startWith($header, 'x-cos-')) {
|
||||
return true;
|
||||
}
|
||||
if (in_array($header, $this->signHeader)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/9/29
|
||||
* @param $haystack
|
||||
* @param $needle
|
||||
* @return bool
|
||||
*/
|
||||
protected function startWith($haystack, $needle)
|
||||
{
|
||||
$length = strlen($needle);
|
||||
if ($length == 0) {
|
||||
return true;
|
||||
}
|
||||
return (substr($haystack, 0, $length) === $needle);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
* @param string $urlPath
|
||||
* @param array $querys
|
||||
* @param array $headers
|
||||
* @return string[]
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/9/26
|
||||
*/
|
||||
public function signRequest(string $method, string $urlPath, array $querys = [], array $headers = [])
|
||||
{
|
||||
$authorization = $this->createAuthorization($method, $urlPath, $querys, $headers);
|
||||
return ['Authorization' => $authorization];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
* @param string $urlPath
|
||||
* @param array $querys
|
||||
* @param array $headers
|
||||
* @param string $expires
|
||||
* @return string
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/9/26
|
||||
*/
|
||||
public function createAuthorization(string $method, string $urlPath, array $querys = [], array $headers = [], $expires = '+30 minutes')
|
||||
{
|
||||
if (is_null($expires) || !strtotime($expires)) {
|
||||
$expires = '+30 minutes';
|
||||
}
|
||||
$signTime = ( string )(time() - 60) . ';' . ( string )(strtotime($expires));
|
||||
$urlParamListArray = [];
|
||||
foreach ($querys as $query) {
|
||||
if (!empty($query)) {
|
||||
$tmpquery = explode('=', $query);
|
||||
//为了保证CI的key中有=号的情况也能正常通过,ci在这层之前已经encode了,这里需要拆开重新encode,防止上方explode拆错
|
||||
$key = strtolower(rawurlencode(urldecode($tmpquery[0])));
|
||||
if (count($tmpquery) >= 2) {
|
||||
$value = $tmpquery[1];
|
||||
} else {
|
||||
$value = "";
|
||||
}
|
||||
//host开关
|
||||
if (!$this->options['signHost'] && $key == 'host') {
|
||||
continue;
|
||||
}
|
||||
$urlParamListArray[$key] = $key . '=' . $value;
|
||||
}
|
||||
}
|
||||
ksort($urlParamListArray);
|
||||
$urlParamList = join(';', array_keys($urlParamListArray));
|
||||
$httpParameters = join('&', array_values($urlParamListArray));
|
||||
|
||||
$headerListArray = [];
|
||||
foreach ($headers as $key => $value) {
|
||||
$key = strtolower(urlencode($key));
|
||||
$value = rawurlencode($value);
|
||||
if (!$this->options['signHost'] && $key == 'host') {
|
||||
continue;
|
||||
}
|
||||
if ($this->needCheckHeader($key)) {
|
||||
$headerListArray[$key] = $key . '=' . $value;
|
||||
}
|
||||
}
|
||||
ksort($headerListArray);
|
||||
$headerList = join(';', array_keys($headerListArray));
|
||||
$httpHeaders = join('&', array_values($headerListArray));
|
||||
$httpString = strtolower($method) . "\n" . urldecode($urlPath) . "\n" . $httpParameters .
|
||||
"\n" . $httpHeaders . "\n";
|
||||
|
||||
$sha1edHttpString = sha1($httpString);
|
||||
$stringToSign = "sha1\n$signTime\n$sha1edHttpString\n";
|
||||
$signKey = hash_hmac('sha1', $signTime, trim($this->secretKey));
|
||||
$signature = hash_hmac('sha1', $stringToSign, $signKey);
|
||||
$authorization = 'q-sign-algorithm=sha1&q-ak=' . trim($this->accessKey) .
|
||||
"&q-sign-time=$signTime&q-key-time=$signTime&q-header-list=$headerList&q-url-param-list=$urlParamList&" .
|
||||
"q-signature=$signature";
|
||||
return $authorization;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param string $method
|
||||
* @param string $urlPath
|
||||
* @param array $querys
|
||||
* @param array $headers
|
||||
* @param string $expires
|
||||
* @return string[]
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/9/26
|
||||
*/
|
||||
public function createPresignedUrl(string $url, string $method, string $urlPath, array $querys = [], array $headers = [], string $expires = '+30 minutes')
|
||||
{
|
||||
$authorization = $this->createAuthorization($method, $urlPath, $querys, $headers, $expires);
|
||||
$uri = $url;
|
||||
$query = 'sign=' . urlencode($authorization) . '&' . implode('&', $querys);
|
||||
if ($this->token != null) {
|
||||
$query = $query . '&x-cos-security-token=' . $this->token;
|
||||
}
|
||||
return [$uri, $query];
|
||||
}
|
||||
}
|
||||
321
crmeb/services/upload/extend/cos/Sts.php
Normal file
321
crmeb/services/upload/extend/cos/Sts.php
Normal file
@@ -0,0 +1,321 @@
|
||||
<?php
|
||||
/**
|
||||
* +----------------------------------------------------------------------
|
||||
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
* +----------------------------------------------------------------------
|
||||
* | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
|
||||
* +----------------------------------------------------------------------
|
||||
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
* +----------------------------------------------------------------------
|
||||
* | Author: CRMEB Team <admin@crmeb.com>
|
||||
* +----------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
namespace crmeb\services\upload\extend\cos;
|
||||
|
||||
|
||||
class Sts
|
||||
{
|
||||
// 临时密钥计算样例
|
||||
function _hex2bin($data)
|
||||
{
|
||||
$len = strlen($data);
|
||||
return pack("H" . $len, $data);
|
||||
}
|
||||
|
||||
// obj 转 query string
|
||||
function json2str($obj, $notEncode = false)
|
||||
{
|
||||
ksort($obj);
|
||||
$arr = array();
|
||||
if (!is_array($obj)) {
|
||||
throw new \Exception('$obj must be an array, the actual value is:' . json_encode($obj));
|
||||
}
|
||||
foreach ($obj as $key => $val) {
|
||||
array_push($arr, $key . '=' . ($notEncode ? $val : rawurlencode($val)));
|
||||
}
|
||||
return join('&', $arr);
|
||||
}
|
||||
|
||||
// 计算临时密钥用的签名
|
||||
function getSignature($opt, $key, $method, $config)
|
||||
{
|
||||
$host = "sts.tencentcloudapi.com";
|
||||
|
||||
if (array_key_exists('domain', $config)) {
|
||||
$host = $config['domain'];
|
||||
}
|
||||
|
||||
if (array_key_exists('endpoint', $config)) {
|
||||
$host = "sts." . $config['endpoint'];
|
||||
}
|
||||
|
||||
$formatString = $method . $host . '/?' . $this->json2str($opt, 1);
|
||||
$sign = hash_hmac('sha1', $formatString, $key);
|
||||
$sign = base64_encode($this->_hex2bin($sign));
|
||||
return $sign;
|
||||
}
|
||||
|
||||
// v2接口的key首字母小写,v3改成大写,此处做了向下兼容
|
||||
function backwardCompat($result)
|
||||
{
|
||||
if (!is_array($result)) {
|
||||
throw new \Exception('$result must be an array, the actual value is:' . json_encode($result));
|
||||
}
|
||||
$compat = array();
|
||||
foreach ($result as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
$compat[lcfirst($key)] = $this->backwardCompat($value);
|
||||
} elseif ($key == 'Token') {
|
||||
$compat['sessionToken'] = $value;
|
||||
} else {
|
||||
$compat[lcfirst($key)] = $value;
|
||||
}
|
||||
}
|
||||
return $compat;
|
||||
}
|
||||
|
||||
// 获取临时密钥
|
||||
function getTempKeys($config)
|
||||
{
|
||||
$result = null;
|
||||
try {
|
||||
if (array_key_exists('policy', $config)) {
|
||||
$policy = $config['policy'];
|
||||
} else {
|
||||
if (array_key_exists('bucket', $config)) {
|
||||
$ShortBucketName = substr($config['bucket'], 0, strripos($config['bucket'], '-'));
|
||||
$AppId = substr($config['bucket'], 1 + strripos($config['bucket'], '-'));
|
||||
} else {
|
||||
throw new \Exception("bucket== null");
|
||||
}
|
||||
if (array_key_exists('allowPrefix', $config)) {
|
||||
if (!(strpos($config['allowPrefix'], '/') === 0)) {
|
||||
$config['allowPrefix'] = '/' . $config['allowPrefix'];
|
||||
}
|
||||
} else {
|
||||
throw new \Exception("allowPrefix == null");
|
||||
}
|
||||
if (!array_key_exists('region', $config)) {
|
||||
throw new \Exception("region == null");
|
||||
}
|
||||
$policy = array(
|
||||
'version' => '2.0',
|
||||
'statement' => array(
|
||||
array(
|
||||
'action' => $config['allowActions'],
|
||||
'effect' => 'allow',
|
||||
'resource' => array(
|
||||
'qcs::cos:' . $config['region'] . ':uid/' . $AppId . ':' . $config['bucket'] . $config['allowPrefix']
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
$policyStr = str_replace('\\/', '/', json_encode($policy));
|
||||
$Action = 'GetFederationToken';
|
||||
$Nonce = rand(10000, 20000);
|
||||
$Timestamp = time();
|
||||
$Method = 'POST';
|
||||
if (array_key_exists('durationSeconds', $config)) {
|
||||
if (!(is_integer($config['durationSeconds']))) {
|
||||
throw new \Exception("durationSeconds must be a int type");
|
||||
}
|
||||
}
|
||||
$params = array(
|
||||
'SecretId' => $config['secretId'],
|
||||
'Timestamp' => $Timestamp,
|
||||
'Nonce' => $Nonce,
|
||||
'Action' => $Action,
|
||||
'DurationSeconds' => $config['durationSeconds'],
|
||||
'Version' => '2018-08-13',
|
||||
'Name' => 'cos',
|
||||
'Region' => $config['region'],
|
||||
'Policy' => urlencode($policyStr)
|
||||
);
|
||||
$params['Signature'] = $this->getSignature($params, $config['secretKey'], $Method, $config);
|
||||
$url = 'https://sts.tencentcloudapi.com/';
|
||||
|
||||
if (array_key_exists('url', $config)) {
|
||||
$url = $config['url'];
|
||||
}
|
||||
|
||||
if (!array_key_exists('url', $config) && array_key_exists('domain', $config)) {
|
||||
$url = 'https://sts.' . $config['domain'];
|
||||
}
|
||||
|
||||
if (array_key_exists('endpoint', $config)) {
|
||||
$url = 'https://sts.' . $config['endpoint'];
|
||||
}
|
||||
|
||||
$ch = curl_init($url);
|
||||
if (array_key_exists('proxy', $config)) {
|
||||
$config['proxy'] && curl_setopt($ch, CURLOPT_PROXY, $config['proxy']);
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_HEADER, 0);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->json2str($params));
|
||||
$result = curl_exec($ch);
|
||||
if (curl_errno($ch)) $result = curl_error($ch);
|
||||
curl_close($ch);
|
||||
$result = json_decode($result, 1);
|
||||
if (isset($result['Response'])) {
|
||||
$result = $result['Response'];
|
||||
if (isset($result['Error'])) {
|
||||
throw new \Exception("get cam failed");
|
||||
}
|
||||
$result['startTime'] = $result['ExpiredTime'] - $config['durationSeconds'];
|
||||
}
|
||||
$result = $this->backwardCompat($result);
|
||||
return $result;
|
||||
} catch (\Exception $e) {
|
||||
if ($result == null) {
|
||||
$result = "error: " . $e->getMessage();
|
||||
} else {
|
||||
$result = json_encode($result);
|
||||
}
|
||||
throw new \Exception($result);
|
||||
}
|
||||
}
|
||||
|
||||
//申请角色授权
|
||||
function getRoleCredential($config)
|
||||
{
|
||||
$result = null;
|
||||
try {
|
||||
if (array_key_exists('policy', $config)) {
|
||||
$policy = $config['policy'];
|
||||
} else {
|
||||
if (array_key_exists('bucket', $config)) {
|
||||
$ShortBucketName = substr($config['bucket'], 0, strripos($config['bucket'], '-'));
|
||||
$AppId = substr($config['bucket'], 1 + strripos($config['bucket'], '-'));
|
||||
} else {
|
||||
throw new \Exception("bucket== null");
|
||||
}
|
||||
if (array_key_exists('allowPrefix', $config)) {
|
||||
if (!(strpos($config['allowPrefix'], '/') === 0)) {
|
||||
$config['allowPrefix'] = '/' . $config['allowPrefix'];
|
||||
}
|
||||
} else {
|
||||
throw new \Exception("allowPrefix == null");
|
||||
}
|
||||
if (!array_key_exists('region', $config)) {
|
||||
throw new \Exception("region == null");
|
||||
}
|
||||
$policy = array(
|
||||
'version' => '2.0',
|
||||
'statement' => array(
|
||||
array(
|
||||
'action' => $config['allowActions'],
|
||||
'effect' => 'allow',
|
||||
'resource' => array(
|
||||
'qcs::cos:' . $config['region'] . ':uid/' . $AppId . ':' . $config['bucket'] . $config['allowPrefix']
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
if (array_key_exists('roleArn', $config)) {
|
||||
$RoleArn = $config['roleArn'];
|
||||
} else {
|
||||
throw new \Exception("roleArn == null");
|
||||
}
|
||||
$policyStr = str_replace('\\/', '/', json_encode($policy));
|
||||
$Action = 'AssumeRole';
|
||||
$Nonce = rand(10000, 20000);
|
||||
$Timestamp = time();
|
||||
$Method = 'POST';
|
||||
$ExternalId = "";
|
||||
if (array_key_exists('externalId', $config)) {
|
||||
$ExternalId = $config['externalId'];
|
||||
}
|
||||
if (array_key_exists('durationSeconds', $config)) {
|
||||
if (!(is_integer($config['durationSeconds']))) {
|
||||
throw new \Exception("durationSeconds must be a int type");
|
||||
}
|
||||
}
|
||||
$params = array(
|
||||
'SecretId' => $config['secretId'],
|
||||
'Timestamp' => $Timestamp,
|
||||
'RoleArn' => $RoleArn,
|
||||
'Action' => $Action,
|
||||
'Nonce' => $Nonce,
|
||||
'DurationSeconds' => $config['durationSeconds'],
|
||||
'Version' => '2018-08-13',
|
||||
'RoleSessionName' => 'cos',
|
||||
'Region' => $config['region'],
|
||||
'ExternalId' => $ExternalId,
|
||||
'Policy' => urlencode($policyStr)
|
||||
);
|
||||
$params['Signature'] = $this->getSignature($params, $config['secretKey'], $Method, $config);
|
||||
$url = 'https://sts.internal.tencentcloudapi.com/';
|
||||
|
||||
if (array_key_exists('endpoint', $config)) {
|
||||
$url = 'https://sts.' . $config['endpoint'];
|
||||
}
|
||||
$ch = curl_init($url);
|
||||
if (array_key_exists('proxy', $config)) {
|
||||
$config['proxy'] && curl_setopt($ch, CURLOPT_PROXY, $config['proxy']);
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_HEADER, 0);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->json2str($params));
|
||||
$result = curl_exec($ch);
|
||||
if (curl_errno($ch)) $result = curl_error($ch);
|
||||
curl_close($ch);
|
||||
$result = json_decode($result, 1);
|
||||
if (isset($result['Response'])) {
|
||||
$result = $result['Response'];
|
||||
if (isset($result['Error'])) {
|
||||
throw new \Exception("get cam failed");
|
||||
}
|
||||
$result['startTime'] = $result['ExpiredTime'] - $config['durationSeconds'];
|
||||
}
|
||||
$result = $this->backwardCompat($result);
|
||||
return $result;
|
||||
} catch (\Exception $e) {
|
||||
if ($result == null) {
|
||||
$result = "error: " . $e->getMessage();
|
||||
} else {
|
||||
$result = json_encode($result);
|
||||
}
|
||||
throw new \Exception($result);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// get policy
|
||||
function getPolicy($scopes)
|
||||
{
|
||||
if (!is_array($scopes)) {
|
||||
return null;
|
||||
}
|
||||
$statements = [];
|
||||
|
||||
for ($i = 0, $counts = count($scopes); $i < $counts; $i++) {
|
||||
$actions = [];
|
||||
$resources = [];
|
||||
$actions[] = $scopes[$i]->get_action();
|
||||
$resources[] = $scopes[$i]->get_resource();
|
||||
|
||||
$statement = [
|
||||
'action' => $actions,
|
||||
'effect' => $scopes[$i]->get_effect(),
|
||||
'resource' => $resources
|
||||
];
|
||||
$statements[] = $statement;
|
||||
}
|
||||
|
||||
$policy = [
|
||||
'version' => '2.0',
|
||||
'statement' => $statements
|
||||
];
|
||||
return $policy;
|
||||
}
|
||||
}
|
||||
444
crmeb/services/upload/extend/jdoss/Client.php
Normal file
444
crmeb/services/upload/extend/jdoss/Client.php
Normal file
@@ -0,0 +1,444 @@
|
||||
<?php
|
||||
/**
|
||||
* +----------------------------------------------------------------------
|
||||
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
* +----------------------------------------------------------------------
|
||||
* | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
|
||||
* +----------------------------------------------------------------------
|
||||
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
* +----------------------------------------------------------------------
|
||||
* | Author: CRMEB Team <admin@crmeb.com>
|
||||
* +----------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
namespace crmeb\services\upload\extend\jdoss;
|
||||
|
||||
use crmeb\exceptions\UploadException;
|
||||
use crmeb\services\upload\BaseClient;
|
||||
use GuzzleHttp\Psr7\Utils;
|
||||
|
||||
/**
|
||||
* 京东云上传
|
||||
* Class Client
|
||||
* @package crmeb\services\upload\extend\jdoss
|
||||
*/
|
||||
class Client extends BaseClient
|
||||
{
|
||||
|
||||
const ALGORITHM_REQUEST = 'AWS4-HMAC-SHA256';
|
||||
|
||||
const BLACKLIST_HEADERS = [
|
||||
'cache-control' => true,
|
||||
'content-type' => true,
|
||||
'content-length' => true,
|
||||
'expect' => true,
|
||||
'max-forwards' => true,
|
||||
'pragma' => true,
|
||||
'range' => true,
|
||||
'te' => true,
|
||||
'if-match' => true,
|
||||
'if-none-match' => true,
|
||||
'if-modified-since' => true,
|
||||
'if-unmodified-since' => true,
|
||||
'if-range' => true,
|
||||
'accept' => true,
|
||||
'authorization' => true,
|
||||
'proxy-authorization' => true,
|
||||
'from' => true,
|
||||
'referer' => true,
|
||||
'user-agent' => true,
|
||||
'x-amzn-trace-id' => true,
|
||||
'aws-sdk-invocation-id' => true,
|
||||
'aws-sdk-retry' => true,
|
||||
];
|
||||
/**
|
||||
* AK
|
||||
* @var
|
||||
*/
|
||||
protected $accessKeyId;
|
||||
|
||||
/**
|
||||
* SK
|
||||
* @var
|
||||
*/
|
||||
protected $secretKey;
|
||||
|
||||
/**
|
||||
* 桶名
|
||||
* @var string
|
||||
*/
|
||||
protected $bucketName;
|
||||
|
||||
/**
|
||||
* 地区
|
||||
* @var string
|
||||
*/
|
||||
protected $region;
|
||||
|
||||
/**
|
||||
* @var mixed|string
|
||||
*/
|
||||
protected $uploadUrl;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $baseUrl = 's3.<REGION>.jdcloud-oss.com';
|
||||
|
||||
//默认地域
|
||||
const DEFAULT_REGION = 'cn-north-1';
|
||||
|
||||
/**
|
||||
* Client constructor.
|
||||
* @param array $config
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
$this->accessKeyId = $config['accessKey'] ?? '';
|
||||
$this->secretKey = $config['secretKey'] ?? '';
|
||||
$this->bucketName = $config['bucket'] ?? '';
|
||||
$this->region = $config['region'] ?? self::DEFAULT_REGION;
|
||||
$this->uploadUrl = $config['uploadUrl'] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传
|
||||
* @param string $bucket
|
||||
* @param string $region
|
||||
* @param string $key
|
||||
* @param array $data
|
||||
* @return array|\crmeb\services\upload\extend\cos\SimpleXMLElement
|
||||
*/
|
||||
public function putObject(string $bucket, string $region, string $key, array $data)
|
||||
{
|
||||
$url = $this->getRequestUrl($bucket, $region);
|
||||
|
||||
$header = [
|
||||
'Host' => $url,
|
||||
];
|
||||
if (isset($data['body'])) {
|
||||
$header['Content-Length'] = strlen($data['body']);
|
||||
}
|
||||
|
||||
return $this->request('https://' . $url . '/'. $key, 'PUT', $data, $header);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param string $bucket
|
||||
* @param string $region
|
||||
* @param string $key
|
||||
* @return array|\crmeb\services\upload\extend\cos\SimpleXMLElement
|
||||
*/
|
||||
public function deleteObject(string $bucket, string $region, string $key)
|
||||
{
|
||||
$url = $this->getRequestUrl($bucket, $region);
|
||||
|
||||
$header = [
|
||||
'Host' => $url,
|
||||
];
|
||||
|
||||
return $this->request('https://' . $url . '/'. $key, 'DELETE', [], $header);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取桶列表
|
||||
* @return array|bool|\crmeb\services\upload\SimpleXMLElement|mixed
|
||||
*/
|
||||
public function listBuckets()
|
||||
{
|
||||
$url = $this->getRequestUrl();
|
||||
$header = [
|
||||
'Host' => $url,
|
||||
];
|
||||
|
||||
$res = $this->request('https://' . $url . '/', 'GET', [], $header);
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测桶,不存在返回true
|
||||
* @param string $bucket
|
||||
* @param string $region
|
||||
* @return array|bool|\crmeb\services\upload\SimpleXMLElement|mixed
|
||||
*/
|
||||
public function headBucket(string $bucket, string $region = '')
|
||||
{
|
||||
$url = $this->getRequestUrl($bucket, $region);
|
||||
|
||||
$header = [
|
||||
'Host' => $url
|
||||
];
|
||||
|
||||
return $this->request('https://' . $url, 'head', [], $header);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建桶
|
||||
* @param $name
|
||||
* @param $region
|
||||
* @param $acl
|
||||
* @return array|\crmeb\services\upload\extend\cos\SimpleXMLElement
|
||||
*/
|
||||
public function createBucket($name, $region, $acl)
|
||||
{
|
||||
$url = $this->getRequestUrl($name, $region);
|
||||
$header = [
|
||||
'Host' => $url,
|
||||
'x-amz-acl' => $acl
|
||||
];
|
||||
$res = $this->request('https://' . $url . '/', 'PUT', [], $header);
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除桶
|
||||
* @param string $bucket
|
||||
* @param string $region
|
||||
* @return array|\crmeb\services\upload\extend\cos\SimpleXMLElement
|
||||
*/
|
||||
public function deleteBucket(string $bucket, string $region = '')
|
||||
{
|
||||
$url = $this->getRequestUrl($bucket, $region);
|
||||
|
||||
$header = [
|
||||
'Host' => $url
|
||||
];
|
||||
|
||||
return $this->request('https://' . $url . '/', 'DELETE', [], $header);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置桶跨域规则
|
||||
* @param string $bucket
|
||||
* @param string $region
|
||||
* @param $data
|
||||
* @return array|\crmeb\services\upload\extend\cos\SimpleXMLElement
|
||||
*/
|
||||
public function putBucketCors(string $bucket, string $region = '', $data = [])
|
||||
{
|
||||
$url = $this->getRequestUrl($bucket, $region);
|
||||
|
||||
$header = [
|
||||
'Host' => $url,
|
||||
'content-type' => 'application/xml'
|
||||
];
|
||||
|
||||
return $this->request('https://' . $url . '/?cors', 'PUT', $data, $header);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取host
|
||||
* @param string $bucket
|
||||
* @param string $region
|
||||
* @return string
|
||||
*/
|
||||
public function getRequestUrl(string $bucket = '', string $region = self::DEFAULT_REGION)
|
||||
{
|
||||
if (!$this->accessKeyId) {
|
||||
throw new UploadException('请传入SecretId');
|
||||
}
|
||||
if (!$this->secretKey) {
|
||||
throw new UploadException('请传入SecretKey');
|
||||
}
|
||||
|
||||
return ($bucket ? $bucket . '.' : '') . 's3.' . $region . '.jdcloud-oss.com';
|
||||
}
|
||||
|
||||
/**
|
||||
* 发起请求
|
||||
* @param string $url
|
||||
* @param string $method
|
||||
* @param array $data
|
||||
* @param array $clientHeader
|
||||
* @param int $timeout
|
||||
* @return array|bool|\crmeb\services\upload\SimpleXMLElement|mixed
|
||||
*/
|
||||
protected function request(string $url, string $method, array $data = [], array $clientHeader = [], int $timeout = 10)
|
||||
{
|
||||
if (!isset($clientHeader['Content-Length'])) {
|
||||
$clientHeader['Content-Length'] = 0;
|
||||
}
|
||||
$clientHeader['x-amz-date'] = gmdate('Ymd\THis\Z');
|
||||
$clientHeader['x-amz-content-sha256'] = hash('sha256', $data['body'] ?? '', false);
|
||||
|
||||
$authorization = $this->generateAwsSignatureV4($data['region'] ?? self::DEFAULT_REGION, $url, $method, $clientHeader, $data);
|
||||
$clientHeader['Authorization'] = $authorization;
|
||||
|
||||
return $this->requestClient($url, $method, $data, $clientHeader, $timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成签名
|
||||
* @param string $region
|
||||
* @param string $url
|
||||
* @param string $httpMethod
|
||||
* @param array $header
|
||||
* @param array $data
|
||||
* @param string $service
|
||||
* @return string
|
||||
*/
|
||||
protected function generateAwsSignatureV4(string $region, string $url, string $httpMethod, array $header, array $data = [], string $service = 's3')
|
||||
{
|
||||
$algorithm = self::ALGORITHM_REQUEST;
|
||||
$t = new \DateTime('UTC');
|
||||
$amzDate = $t->format('Ymd\THis\Z');
|
||||
$dateStamp = $t->format('Ymd');
|
||||
[$canonicalRequest, $signedHeaders] = $this->createCanonicalRequest($url, $httpMethod, $header, $data);
|
||||
|
||||
$credentialScope = $dateStamp . '/' . $region . '/' . $service . '/aws4_request';
|
||||
$stringToSign = $algorithm . "\n" . $amzDate . "\n" . $credentialScope . "\n" . hash('sha256', $canonicalRequest);
|
||||
$signingKey = hash_hmac('sha256', 'aws4_request',
|
||||
hash_hmac('sha256', $service,
|
||||
hash_hmac('sha256', $region,
|
||||
hash_hmac('sha256', $dateStamp, 'AWS4' . $this->secretKey, true),
|
||||
true),
|
||||
true),
|
||||
true);
|
||||
$signature = hash_hmac('sha256', $stringToSign, $signingKey);
|
||||
|
||||
return $algorithm . ' Credential=' . $this->accessKeyId . '/' . $credentialScope . ', SignedHeaders=' . $signedHeaders . ', Signature=' . $signature;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param string $httpMethod
|
||||
* @param array $header
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
public function createCanonicalRequest(string $url, string $httpMethod, array $header, array $data = [])
|
||||
{
|
||||
$canonicalQueryString = '';
|
||||
$payload = '';
|
||||
if (!empty($data['query'])) {
|
||||
$query = $payload = $data['query'];
|
||||
ksort($query);
|
||||
$queryAttr = [];
|
||||
foreach ($query as $key => $item) {
|
||||
$queryAttr[urlencode($key)] = urlencode($item);
|
||||
}
|
||||
if ($queryAttr) {
|
||||
$canonicalQueryString = implode('&', $queryAttr);
|
||||
}
|
||||
$payload = json_encode($payload);
|
||||
} elseif (!empty($data['body'])) {
|
||||
$payload = $data['body'];
|
||||
} elseif (!empty($data['json'])) {
|
||||
$payload = json_encode($data['json']);
|
||||
}
|
||||
$canonicalHeaders = '';
|
||||
$signedHeaders = '';
|
||||
if ($header) {
|
||||
$canonicalHeadersAtrr = $signedHeadersAttr = [];
|
||||
ksort($header);
|
||||
foreach ($header as $key => $item) {
|
||||
$key = strtolower($key);
|
||||
if (isset(self::BLACKLIST_HEADERS[$key])) {
|
||||
continue;
|
||||
}
|
||||
$canonicalHeadersAtrr[] = $key . ':' . preg_replace('/\s+/', ' ', trim($item));
|
||||
$signedHeadersAttr[] = strtolower($key);
|
||||
}
|
||||
ksort($canonicalHeadersAtrr);
|
||||
ksort($signedHeadersAttr);
|
||||
if ($canonicalHeadersAtrr) {
|
||||
$canonicalHeaders = implode("\n", $canonicalHeadersAtrr);
|
||||
$signedHeaders = implode(';', $signedHeadersAttr);
|
||||
}
|
||||
}
|
||||
$canonicalUri = $this->createCanonicalizedPath($url);
|
||||
$bodyDigest = $this->buildBodyDigest($header, (string)Utils::streamFor($payload));
|
||||
$canonicalRequest = $httpMethod . "\n" . $canonicalUri . "\n" . $canonicalQueryString . "\n" . $canonicalHeaders . "\n" . "\n" . $signedHeaders . "\n" . $bodyDigest;
|
||||
return [$canonicalRequest, $signedHeaders];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $url
|
||||
* @return string
|
||||
*/
|
||||
public function createCanonicalizedPath(string $url)
|
||||
{
|
||||
$canonicalUri = '/';
|
||||
|
||||
$urlAttr = pathinfo($url);
|
||||
if (isset($urlAttr['dirname']) && $urlAttr['dirname'] !== 'https:') {
|
||||
$urlParse = parse_url($urlAttr['dirname'] ?? '');
|
||||
if (isset($urlParse['path'])) {
|
||||
$canonicalUri .= substr($urlParse['path'], 1) . '/';
|
||||
}
|
||||
if (isset($urlAttr['basename'])) {
|
||||
$canonicalUri .= $urlAttr['basename'];
|
||||
}
|
||||
//|| strlen($canonicalUri) - 1 !== $pos
|
||||
if (!($pos = strripos($canonicalUri, '/')) ) {
|
||||
$canonicalUri .= '/';
|
||||
}
|
||||
}
|
||||
return $canonicalUri;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理dody hash
|
||||
* @param array $header
|
||||
* @param string $body
|
||||
* @return string
|
||||
*/
|
||||
protected function buildBodyDigest(array $header, string $body = ''): string
|
||||
{
|
||||
if (isset($header['x-amz-content-sha256'])) {
|
||||
$hash = $header['x-amz-content-sha256'];
|
||||
} else {
|
||||
$hash = hash('sha256', $body);
|
||||
}
|
||||
|
||||
return $hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理bogy
|
||||
* @param string $body
|
||||
* @return string
|
||||
*/
|
||||
public function dechunk(string $body): string
|
||||
{
|
||||
$h = fopen('php://temp', 'w+');
|
||||
stream_filter_append($h, 'dechunk', \STREAM_FILTER_WRITE);
|
||||
fwrite($h, $body);
|
||||
$body = stream_get_contents($h, -1, 0);
|
||||
rewind($h);
|
||||
ftruncate($h, 0);
|
||||
|
||||
return $body;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取区域
|
||||
* @return \string[][]
|
||||
*/
|
||||
public function getRegion()
|
||||
{
|
||||
return [
|
||||
[
|
||||
'value' => 'cn-north-1',
|
||||
'label' => '华北-北京',
|
||||
],
|
||||
[
|
||||
'value' => 'cn-south-1',
|
||||
'label' => '华南-广州',
|
||||
],
|
||||
[
|
||||
'value' => 'cn-east-2',
|
||||
'label' => '华东-上海',
|
||||
],
|
||||
[
|
||||
'value' => 'cn-east-1',
|
||||
'label' => '华东-宿迁',
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
717
crmeb/services/upload/extend/obs/Client.php
Normal file
717
crmeb/services/upload/extend/obs/Client.php
Normal file
@@ -0,0 +1,717 @@
|
||||
<?php
|
||||
/**
|
||||
* +----------------------------------------------------------------------
|
||||
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
* +----------------------------------------------------------------------
|
||||
* | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
|
||||
* +----------------------------------------------------------------------
|
||||
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
* +----------------------------------------------------------------------
|
||||
* | Author: CRMEB Team <admin@crmeb.com>
|
||||
* +----------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
namespace crmeb\services\upload\extend\obs;
|
||||
|
||||
|
||||
use crmeb\exceptions\UploadException;
|
||||
use crmeb\services\upload\BaseClient;
|
||||
|
||||
/**
|
||||
* 华为云上传
|
||||
* Class Client
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2023/5/18
|
||||
* @package crmeb\services\upload\extend\obs
|
||||
*/
|
||||
class Client extends BaseClient
|
||||
{
|
||||
const HEADER_PREFIX = 'x-obs-';
|
||||
|
||||
const INTEREST_HEADER_KEY_LIST = ['content-type', 'content-md5', 'date'];
|
||||
|
||||
const ALTERNATIVE_DATE_HEADER = 'x-obs-date';
|
||||
|
||||
const ALLOWED_RESOURCE_PARAMTER_NAMES = [
|
||||
'acl',
|
||||
'policy',
|
||||
'torrent',
|
||||
'logging',
|
||||
'location',
|
||||
'storageinfo',
|
||||
'quota',
|
||||
'storagepolicy',
|
||||
'requestpayment',
|
||||
'versions',
|
||||
'versioning',
|
||||
'versionid',
|
||||
'uploads',
|
||||
'uploadid',
|
||||
'partnumber',
|
||||
'website',
|
||||
'notification',
|
||||
'lifecycle',
|
||||
'deletebucket',
|
||||
'delete',
|
||||
'cors',
|
||||
'restore',
|
||||
'tagging',
|
||||
'response-content-type',
|
||||
'response-content-language',
|
||||
'response-expires',
|
||||
'response-cache-control',
|
||||
'response-content-disposition',
|
||||
'response-content-encoding',
|
||||
'x-image-process',
|
||||
|
||||
'backtosource',
|
||||
'storageclass',
|
||||
'replication',
|
||||
'append',
|
||||
'position',
|
||||
'x-oss-process'
|
||||
];
|
||||
|
||||
//桶acl
|
||||
const OBS_ACL = [
|
||||
[
|
||||
'value' => 'public-read',
|
||||
'label' => '公共读(推荐)',
|
||||
],
|
||||
[
|
||||
'value' => 'public-read-write',
|
||||
'label' => '公共读写',
|
||||
],
|
||||
];
|
||||
//默认acl
|
||||
const DEFAULT_OBS_ACL = 'public-read';
|
||||
|
||||
protected $isCname = false;
|
||||
|
||||
protected $pathStyle;
|
||||
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
protected $accessKeyId;
|
||||
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
protected $secretKey;
|
||||
|
||||
/**
|
||||
* 桶名
|
||||
* @var string
|
||||
*/
|
||||
protected $bucketName;
|
||||
|
||||
/**
|
||||
* 地区
|
||||
* @var string
|
||||
*/
|
||||
protected $region;
|
||||
|
||||
/**
|
||||
* @var mixed|string
|
||||
*/
|
||||
protected $uploadUrl;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $baseUrl = 'obs.cn-north-1.myhuaweicloud.com';
|
||||
|
||||
protected $type = 'hw';
|
||||
|
||||
/**
|
||||
* Client constructor.
|
||||
* @param array $config
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
$this->accessKeyId = $config['accessKey'] ?? '';
|
||||
$this->secretKey = $config['secretKey'] ?? '';
|
||||
$this->bucketName = $config['bucket'] ?? '';
|
||||
$this->region = $config['region'] ?? 'ap-chengdu';
|
||||
$this->uploadUrl = $config['uploadUrl'] ?? '';
|
||||
$this->type = $config['type'] ?? 'hw';
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2023/5/18
|
||||
*/
|
||||
protected function checkOptions()
|
||||
{
|
||||
if (!$this->bucketName) {
|
||||
throw new UploadException('请传入桶名');
|
||||
}
|
||||
if (!$this->region) {
|
||||
throw new UploadException('请传入所属地域');
|
||||
}
|
||||
if (!$this->accessKeyId) {
|
||||
throw new UploadException('请传入SecretId');
|
||||
}
|
||||
if (!$this->secretKey) {
|
||||
throw new UploadException('请传入SecretKey');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传图片
|
||||
* @param string $key
|
||||
* @param $body
|
||||
* @return mixed
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2023/5/18
|
||||
*/
|
||||
public function putObject(string $key, $body, string $contentType = 'image/jpeg')
|
||||
{
|
||||
$header = [
|
||||
'Host' => $this->getRequestUrl($this->bucketName, $this->region),
|
||||
'Content-Type' => $contentType,
|
||||
'Content-Length' => strlen($body),
|
||||
];
|
||||
|
||||
$res = $this->checkOptions()->request('https://' . $header['Host'] . '/' . $key, 'PUT', [
|
||||
'bucket' => $this->bucketName,
|
||||
'body' => $body
|
||||
], $header);
|
||||
|
||||
return $this->response($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除上传对象
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2023/5/18
|
||||
*/
|
||||
public function deleteObject(string $key)
|
||||
{
|
||||
$header = [
|
||||
'Host' => $this->getRequestUrl($this->bucketName, $this->region),
|
||||
];
|
||||
|
||||
$res = $this->request('https://' . $header['Host'] . '/' . $key, 'DELETE', [
|
||||
'bucket' => $this->bucketName
|
||||
], $header);
|
||||
|
||||
return $this->response($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取桶
|
||||
* @return false|string
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2023/5/16
|
||||
*/
|
||||
public function listBuckets()
|
||||
{
|
||||
$header = [
|
||||
'Host' => $this->getRequestUrl('', $this->region),
|
||||
];
|
||||
$res = $this->request('https://' . $header['Host'] . '/', 'GET', [], []);
|
||||
return $this->response($res);
|
||||
}
|
||||
|
||||
public function headBucket(string $bucket, string $region)
|
||||
{
|
||||
$header = [
|
||||
'Host' => $this->getRequestUrl($bucket, $region),
|
||||
];
|
||||
$res = $this->request('https://' . $header['Host'] . '/', 'HEAD', [], []);
|
||||
return $this->response($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置桶的策略
|
||||
* @param string $bucket
|
||||
* @param string $region
|
||||
* @param array $data
|
||||
* @return mixed
|
||||
*
|
||||
* @date 2023/06/08
|
||||
* @author yyw
|
||||
*/
|
||||
public function putPolicy(string $bucket, string $region, array $data)
|
||||
{
|
||||
$header = [
|
||||
'Host' => $this->getRequestUrl($bucket, $region),
|
||||
"Content-Type" => "application/json"
|
||||
];
|
||||
$res = $this->request('https://' . $header['Host'] . '/?policy', 'PUT', [
|
||||
'bucket' => $bucket,
|
||||
'json' => $data
|
||||
], $header);
|
||||
|
||||
return $this->response($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建桶
|
||||
* @param string $bucket
|
||||
* @param string $region
|
||||
* @param string $acl
|
||||
* @return mixed
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2023/5/18
|
||||
*/
|
||||
public function createBucket(string $bucket, string $region, string $acl = self::DEFAULT_OBS_ACL)
|
||||
{
|
||||
$header = [
|
||||
'x-obs-acl' => $acl,
|
||||
'Host' => $this->getRequestUrl($bucket, $region),
|
||||
"Content-Type" => "application/xml"
|
||||
];
|
||||
$xml = "<CreateBucketConfiguration><Location>{$region}</Location></CreateBucketConfiguration>";
|
||||
$res = $this->request('https://' . $header['Host'] . '/', 'PUT', [
|
||||
'bucket' => $bucket,
|
||||
'body' => $xml
|
||||
], $header);
|
||||
|
||||
return $this->response($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除桶
|
||||
* @param string $bucket
|
||||
* @param string $region
|
||||
* @return mixed
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2023/5/18
|
||||
*/
|
||||
public function deleteBucket(string $bucket, string $region)
|
||||
{
|
||||
$header = [
|
||||
'Host' => $this->getRequestUrl($bucket, $region),
|
||||
];
|
||||
$res = $this->request('https://' . $header['Host'] . '/', 'DELETE', [
|
||||
'bucket' => $bucket
|
||||
], $header);
|
||||
|
||||
return $this->response($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取桶的自定义域名
|
||||
* @param string $bucket
|
||||
* @param string $region
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2023/5/18
|
||||
*/
|
||||
public function getBucketDomain(string $bucket, string $region)
|
||||
{
|
||||
$header = [
|
||||
'Host' => $this->getRequestUrl($bucket, $region),
|
||||
];
|
||||
$res = $this->request('https://' . $header['Host'] . '/?customdomain', 'GET', [
|
||||
'bucket' => $bucket
|
||||
], $header);
|
||||
|
||||
return $this->response($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置桶的自定义域名
|
||||
* @param string $bucket
|
||||
* @param string $region
|
||||
* @param array $data
|
||||
* @return mixed
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2023/5/18
|
||||
*/
|
||||
public function putBucketDomain(string $bucket, string $region, array $data = [])
|
||||
{
|
||||
$header = [
|
||||
'Host' => $this->getRequestUrl($bucket, $region),
|
||||
];
|
||||
$res = $this->request('https://' . $header['Host'] . '/?customdomain=' . $data['domainname'], 'PUT', [
|
||||
'bucket' => $bucket
|
||||
], $header);
|
||||
|
||||
return $this->response($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置跨域
|
||||
* @return bool
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2023/5/18
|
||||
*/
|
||||
public function putBucketCors(string $bucket, string $region, array $data = [])
|
||||
{
|
||||
$xml = $this->xmlBuild($data, 'CORSConfiguration', 'CORSRule');
|
||||
$header = [
|
||||
'Host' => $this->getRequestUrl($bucket, $region),
|
||||
'Content-Type' => 'application/xml',
|
||||
'Content-Length' => strlen($xml),
|
||||
'Content-MD5' => base64_encode(md5($xml, true))
|
||||
];
|
||||
$res = $this->request('https://' . $header['Host'] . '/?cors', 'PUT', [
|
||||
'bucket' => $bucket,
|
||||
'body' => $xml
|
||||
], $header);
|
||||
|
||||
return $this->response($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除跨域
|
||||
* @param string $bucket
|
||||
* @param string $region
|
||||
* @return mixed
|
||||
*
|
||||
* @date 2023/06/08
|
||||
* @author yyw
|
||||
*/
|
||||
public function deleteBucketCors(string $bucket, string $region)
|
||||
{
|
||||
$header = [
|
||||
'Host' => $this->getRequestUrl($bucket, $region),
|
||||
];
|
||||
$res = $this->request('https://' . $header['Host'] . '/?cors', 'DELETE', [
|
||||
'bucket' => $bucket,
|
||||
], $header);
|
||||
|
||||
return $this->response($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $res
|
||||
* @return mixed
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2023/5/18
|
||||
*/
|
||||
protected function response($res)
|
||||
{
|
||||
if (!empty($res['Code']) && !empty($res['Message'])) {
|
||||
throw new UploadException($res['Message']);
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求域名
|
||||
* @param string $bucket
|
||||
* @param string $region
|
||||
* @return string
|
||||
*
|
||||
* @date 2023/06/08
|
||||
* @author yyw
|
||||
*/
|
||||
protected function getRequestUrl(string $bucket = '', string $region = '')
|
||||
{
|
||||
if ($this->type == 'hw') {
|
||||
$url = '.myhuaweicloud.com'; // 华为
|
||||
} else {
|
||||
$url = '.ctyun.cn'; // 天翼
|
||||
}
|
||||
if ($bucket) {
|
||||
return $bucket . '.obs.' . $region . $url;
|
||||
} else {
|
||||
return 'obs.' . $region . $url;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 地域名称
|
||||
* @return \string[][]
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2023/5/17
|
||||
*/
|
||||
public function getRegion()
|
||||
{
|
||||
return [
|
||||
[
|
||||
'value' => 'cn-north-1',
|
||||
'label' => '华北-北京一',
|
||||
],
|
||||
// [
|
||||
// 'value' => 'cn-north-4',
|
||||
// 'label' => '华北-北京四',
|
||||
// ],
|
||||
[
|
||||
'value' => 'cn-north-9',
|
||||
'label' => '华北-乌兰察布一',
|
||||
],
|
||||
[
|
||||
'value' => 'cn-east-2',
|
||||
'label' => '华东-上海二',
|
||||
],
|
||||
[
|
||||
'value' => 'cn-east-3',
|
||||
'label' => '华东-上海一',
|
||||
],
|
||||
[
|
||||
'value' => 'cn-south-1',
|
||||
'label' => '华南-广州',
|
||||
],
|
||||
[
|
||||
'value' => 'ap-southeast-1',
|
||||
'label' => '中国-香港',
|
||||
],
|
||||
[
|
||||
'value' => 'cn-south-4',
|
||||
'label' => '华南-广州-友好用户环境',
|
||||
],
|
||||
[
|
||||
'value' => 'cn-southwest-2',
|
||||
'label' => '西南-贵阳一',
|
||||
],
|
||||
[
|
||||
'value' => 'la-north-2',
|
||||
'label' => '拉美-墨西哥城二',
|
||||
],
|
||||
[
|
||||
'value' => 'na-mexico-1',
|
||||
'label' => '拉美-墨西哥城一',
|
||||
],
|
||||
[
|
||||
'value' => 'sa-brazil-1',
|
||||
'label' => '拉美-圣保罗一',
|
||||
],
|
||||
[
|
||||
'value' => 'la-south-2',
|
||||
'label' => '拉美-圣地亚哥',
|
||||
],
|
||||
[
|
||||
'value' => 'tr-west-1',
|
||||
'label' => '土耳其-伊斯坦布尔',
|
||||
],
|
||||
[
|
||||
'value' => 'ap-southeast-2',
|
||||
'label' => '亚太-曼谷',
|
||||
],
|
||||
[
|
||||
'value' => 'ap-southeast-3',
|
||||
'label' => '亚太-新加坡',
|
||||
],
|
||||
[
|
||||
'value' => 'af-south-1',
|
||||
'label' => '非洲-约翰内斯堡',
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置桶名
|
||||
* @param string $bucketName
|
||||
* @return $this
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2023/5/16
|
||||
*/
|
||||
public function setBucketName(string $bucketName)
|
||||
{
|
||||
$this->bucketName = $bucketName;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取签名
|
||||
* @param array $result
|
||||
* @return array
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2023/5/17
|
||||
*/
|
||||
protected function getSign(array $result)
|
||||
{
|
||||
$result['headers']['Date'] = gmdate('D, d M Y H:i:s \G\M\T');
|
||||
$canonicalstring = $this->makeCanonicalstring($result['method'], $result['headers'], $result['pathArgs'], $result['dnsParam'], $result['uriParam']);
|
||||
|
||||
$result['cannonicalRequest'] = $canonicalstring;
|
||||
|
||||
$signature = base64_encode(hash_hmac('sha1', $canonicalstring, $this->secretKey, true));
|
||||
|
||||
$authorization = 'OBS ' . $this->accessKeyId . ':' . $signature;
|
||||
|
||||
$result['headers']['Authorization'] = $authorization;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理签名数据
|
||||
* @param $method
|
||||
* @param $headers
|
||||
* @param $pathArgs
|
||||
* @param $bucketName
|
||||
* @param $objectKey
|
||||
* @param null $expires
|
||||
* @return string
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2023/5/17
|
||||
*/
|
||||
public function makeCanonicalstring($method, $headers, $pathArgs, $bucketName, $objectKey, $expires = null)
|
||||
{
|
||||
$buffer = [];
|
||||
$buffer[] = $method;
|
||||
$buffer[] = "\n";
|
||||
$interestHeaders = [];
|
||||
|
||||
foreach ($headers as $key => $value) {
|
||||
$key = strtolower($key);
|
||||
if (in_array($key, self::INTEREST_HEADER_KEY_LIST) || strpos($key, self::HEADER_PREFIX) === 0) {
|
||||
$interestHeaders[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists(self::ALTERNATIVE_DATE_HEADER, $interestHeaders)) {
|
||||
$interestHeaders['date'] = '';
|
||||
}
|
||||
|
||||
if ($expires !== null) {
|
||||
$interestHeaders['date'] = strval($expires);
|
||||
}
|
||||
|
||||
if (!array_key_exists('content-type', $interestHeaders)) {
|
||||
$interestHeaders['content-type'] = '';
|
||||
}
|
||||
|
||||
if (!array_key_exists('content-md5', $interestHeaders)) {
|
||||
$interestHeaders['content-md5'] = '';
|
||||
}
|
||||
|
||||
ksort($interestHeaders);
|
||||
|
||||
foreach ($interestHeaders as $key => $value) {
|
||||
if (strpos($key, self::HEADER_PREFIX) === 0) {
|
||||
$buffer[] = $key . ':' . $value;
|
||||
} else {
|
||||
$buffer[] = $value;
|
||||
}
|
||||
$buffer[] = "\n";
|
||||
}
|
||||
|
||||
$uri = '';
|
||||
|
||||
$bucketName = $this->isCname ? $headers['Host'] : $bucketName;
|
||||
|
||||
if ($bucketName) {
|
||||
$uri .= '/';
|
||||
$uri .= $bucketName;
|
||||
if (!$this->pathStyle) {
|
||||
$uri .= '/';
|
||||
}
|
||||
}
|
||||
|
||||
if ($objectKey) {
|
||||
if (!($pos = strripos($uri, '/')) || strlen($uri) - 1 !== $pos) {
|
||||
$uri .= '/';
|
||||
}
|
||||
$uri .= $objectKey;
|
||||
}
|
||||
|
||||
$buffer[] = $uri === '' ? '/' : $uri;
|
||||
|
||||
|
||||
if (!empty($pathArgs)) {
|
||||
ksort($pathArgs);
|
||||
$_pathArgs = [];
|
||||
foreach ($pathArgs as $key => $value) {
|
||||
if (in_array(strtolower($key), self::ALLOWED_RESOURCE_PARAMTER_NAMES) || strpos($key, self::HEADER_PREFIX) === 0) {
|
||||
$_pathArgs[] = $value === null || $value === '' ? $key : $key . '=' . urldecode($value);
|
||||
}
|
||||
}
|
||||
if (!empty($_pathArgs)) {
|
||||
$buffer[] = '?';
|
||||
$buffer[] = implode('&', $_pathArgs);
|
||||
}
|
||||
}
|
||||
|
||||
return implode('', $buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发起请求
|
||||
* @param string $url
|
||||
* @param string $method
|
||||
* @param array $data
|
||||
* @param array $clientHeader
|
||||
* @param int $timeout
|
||||
* @return false|string
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2023/5/16
|
||||
*/
|
||||
public function request(string $url, string $method, array $data = [], array $clientHeader = [], int $timeout = 10)
|
||||
{
|
||||
$method = strtoupper($method);
|
||||
$urlAttr = pathinfo($url);
|
||||
$urlParse = parse_url($urlAttr['dirname'] ?? '');
|
||||
|
||||
$uriParam = '';
|
||||
if ($urlAttr['dirname'] !== 'https:') {
|
||||
if (isset($urlParse['path'])) {
|
||||
$uriParam .= substr($urlParse['path'], 1) . '/';
|
||||
}
|
||||
if (isset($urlAttr['basename'])) {
|
||||
$uriParam .= $urlAttr['basename'];
|
||||
}
|
||||
}
|
||||
|
||||
$result = $this->getSign([
|
||||
'method' => $method,
|
||||
'headers' => $clientHeader,
|
||||
'pathArgs' => '',
|
||||
'dnsParam' => $data['bucket'] ?? '',
|
||||
'uriParam' => $uriParam,
|
||||
]);
|
||||
|
||||
return $this->requestClient($url, $method, $data, $result['headers'], $timeout);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 组合成xml
|
||||
* @param array $data
|
||||
* @param string $root
|
||||
* @param string $itemKey
|
||||
* @return string
|
||||
* @author 等风来
|
||||
* @email 136327134@qq.com
|
||||
* @date 2022/10/17
|
||||
*/
|
||||
protected function xmlBuild(array $xmlAttr, string $root = 'xml', string $itemKey = 'item')
|
||||
{
|
||||
$xml = '<' . $root . '>';
|
||||
$xml .= '<' . $itemKey . '>';
|
||||
|
||||
foreach ($xmlAttr as $kk => $vv) {
|
||||
if (is_array($vv)) {
|
||||
foreach ($vv as $v) {
|
||||
$xml .= '<' . $kk . '>' . $v . '</' . $kk . '>';
|
||||
}
|
||||
} else {
|
||||
$xml .= '<' . $kk . '>' . $vv . '</' . $kk . '>';
|
||||
}
|
||||
}
|
||||
$xml .= '</' . $itemKey . '>';
|
||||
$xml .= '</' . $root . '>';
|
||||
|
||||
return $xml;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user