liufumingren 7b18287328 fix: 全项目清理 PHP 7.4 升级遗留(open_basedir 临时目录、implode 反序、null 取下标、嵌套三元)
一、open_basedir 不能再写 /tmp(7.4 池子把白名单收紧,Monolog/EasyWeChat 会抛异常)
- addons/shopro/library/Wechat.php:easywechat 日志改到 runtime/easywechat/
- addons/epay/library/Yansongda/Supports/Logger.php:epay 日志默认路径改到 runtime/epay/
- application/admin/library/traits/Backend.php:CSV 导入临时文件改到 runtime/temp/

二、implode 参数反序(7.4 起 E_DEPRECATED,TP5 会抛异常)
- application/api/controller/Datas.php
- application/index/controller/wms/Pda.php
- 另修正 Wms.php 中 1 处(拆分面单提示)

三、null 取下标(7.4 起 "Trying to access array offset on value of type null")
- 覆盖 71 个文件 135 行:find()/value()/json_decode()/redis get 等结果未判空即取下标处,
  统一 (\['k'] ?? null) 兜底;foreach 目标用 ?? [];in_array/count 等数组参数用 ?? []
- 涉及 api/*、admin/shopro/*、addons/shopro/*、addons/epay/*、index/wms/* 等

四、嵌套三元未加括号(7.4 弃用)
- application/index/controller/wms/Wms.php getPackingprice 2 处,补括号后与旧版本解析顺序一致

验证:74 个文件、148 行改动,逐行括号/引号配平与 HEAD 一致,未改动 vendor/ 与 thinkphp/;
剩余扫描命中均为写目标、unset、或已被 if(!\){error()} 拦截的假阳性。

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-25 09:47:48 +08:00

456 lines
17 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace addons\cos\controller;
use app\common\exception\UploadException;
use app\common\library\Upload;
use app\common\model\Attachment;
use Qcloud\Cos\Client;
use Qcloud\Cos\Signature;
use think\addons\Controller;
use think\Config;
/**
* COS云储存
*
*/
class Index extends Controller
{
protected $cosConfig = [];
public function _initialize()
{
//跨域检测
check_cors_request();
parent::_initialize();
Config::set('default_return_type', 'json');
$config = get_addon_config('cos');
$this->cosConfig = array(
'region' => $config['region'],
'schema' => 'https', //协议头部,默认为http
'credentials' => array(
'secretId' => $config['secretId'],
'secretKey' => $config['secretKey']
)
);
}
public function index()
{
Config::set('default_return_type', 'html');
$this->error("当前插件暂无前台页面");
}
public function paramsApi($md5,$name,$type,$fileSize,$chunk,$partSize,$costoken){
$this->costoken = $costoken;
$this->check();
$config = get_addon_config('cos');
$key = (new Upload())->getSavekey($config['savekey'], $name, $md5);
$key = ltrim($key, "/");
$params = [
'key' => $key,
'md5' => $md5
];
if ($chunk) {
$oss = new Client($this->cosConfig);
$result = $oss->createMultipartUpload(array(
'Bucket' => $config['bucket'],
'Key' => $key,
));
$uploadId = $result['UploadId'];
$sig = new Signature($config['secretId'], $config['secretKey']);
$i = 0;
$size_count = $fileSize;
$values = array();
while ($size_count > 0) {
$size_count -= $partSize;
$values[] = array(
$partSize * $i,
($size_count > 0) ? $partSize : ($size_count + $partSize),
);
$i++;
}
$params['key'] = $key;
$params['uploadId'] = $uploadId;
$params['partsAuthorization'] = [];
$date = gmdate('D, d M Y H:i:s \G\M\T');
foreach ($values as $index => $part) {
$partNumber = $index + 1;
$options = array(
'Bucket' => $config['bucket'],
'Key' => $key,
'UploadId' => $uploadId,
'PartNumber' => $partNumber,
'Body' => ''
);
$command = $oss->getCommand('uploadPart', $options);
$request = $oss->commandToRequestTransformer($command);
$authorization = $sig->createAuthorization($request);
$params['partsAuthorization'][$index] = $authorization;
}
$params['date'] = $date;
} else {
if ($config['uploadmode'] == 'client') {
$expiretime = time() + $config['expire'];
$expiration = gmdate("Y-m-d\TH:i:s.414\Z", $expiretime);
$keytime = (time() - 60) . ';' . $expiretime;
$policy = json_encode([
'expiration' => $expiration,
'conditions' => [
['q-sign-algorithm' => 'sha1'],
['q-ak' => $config['secretId']],
['q-sign-time' => $keytime]
]
]);
$signature = hash_hmac('sha1', sha1($policy), hash_hmac('sha1', $keytime, $config['secretKey']));
$params = [
'key' => $key,
'policy' => base64_encode($policy),
'q-sign-algorithm' => 'sha1',
'q-ak' => $config['secretId'],
'q-key-time' => $keytime,
'q-sign-time' => $keytime,
'q-signature' => $signature
];
}
}
return $params;
}
public function params()
{
$this->check();
$config = get_addon_config('cos');
$name = $this->request->post('name');
$md5 = $this->request->post('md5');
$chunk = $this->request->post('chunk');
$key = (new Upload())->getSavekey($config['savekey'], $name, $md5);
$key = ltrim($key, "/");
$params = [
'key' => $key,
'md5' => $md5
];
if ($chunk) {
$fileSize = $this->request->post('size');
$oss = new Client($this->cosConfig);
$result = $oss->createMultipartUpload(array(
'Bucket' => $config['bucket'],
'Key' => $key,
));
$uploadId = $result['UploadId'];
$sig = new Signature($config['secretId'], $config['secretKey']);
$partSize = $this->request->post("chunksize");
$i = 0;
$size_count = $fileSize;
$values = array();
while ($size_count > 0) {
$size_count -= $partSize;
$values[] = array(
$partSize * $i,
($size_count > 0) ? $partSize : ($size_count + $partSize),
);
$i++;
}
$params['key'] = $key;
$params['uploadId'] = $uploadId;
$params['partsAuthorization'] = [];
$date = gmdate('D, d M Y H:i:s \G\M\T');
foreach ($values as $index => $part) {
$partNumber = $index + 1;
$options = array(
'Bucket' => $config['bucket'],
'Key' => $key,
'UploadId' => $uploadId,
'PartNumber' => $partNumber,
'Body' => ''
);
$command = $oss->getCommand('uploadPart', $options);
$request = $oss->commandToRequestTransformer($command);
$authorization = $sig->createAuthorization($request);
$params['partsAuthorization'][$index] = $authorization;
}
$params['date'] = $date;
} else {
if ($config['uploadmode'] == 'client') {
$expiretime = time() + $config['expire'];
$expiration = gmdate("Y-m-d\TH:i:s.414\Z", $expiretime);
$keytime = (time() - 60) . ';' . $expiretime;
$policy = json_encode([
'expiration' => $expiration,
'conditions' => [
['q-sign-algorithm' => 'sha1'],
['q-ak' => $config['secretId']],
['q-sign-time' => $keytime]
]
]);
$signature = hash_hmac('sha1', sha1($policy), hash_hmac('sha1', $keytime, $config['secretKey']));
$params = [
'key' => $key,
'policy' => base64_encode($policy),
'q-sign-algorithm' => 'sha1',
'q-ak' => $config['secretId'],
'q-key-time' => $keytime,
'q-sign-time' => $keytime,
'q-signature' => $signature
];
}
}
$this->success('', null, $params);
return;
}
/**
* 服务器中转上传文件
* 上传分片
* 合并分片
* @param bool $isApi
*/
public function upload($isApi = false)
{
if ($isApi === true) {
if (!$this->auth->isLogin()) {
$this->error("请登录后再进行操作");
}
} else {
$this->check();
}
$config = get_addon_config('cos');
$oss = new Client($this->cosConfig);
//检测删除文件或附件
$checkDeleteFile = function ($attachment, $upload, $force = false) use ($config) {
//如果设定为不备份则删除文件和记录 或 强制删除
if ((isset($config['serverbackup']) && !$config['serverbackup']) || $force) {
if ($attachment && !empty($attachment['id'])) {
$attachment->delete();
}
if ($upload) {
//文件绝对路径
$filePath = $upload->getFile()->getRealPath() ?: $upload->getFile()->getPathname();
@unlink($filePath);
}
}
};
$chunkid = $this->request->post("chunkid");
if ($chunkid) {
$action = $this->request->post("action");
$chunkindex = $this->request->post("chunkindex/d");
$chunkcount = $this->request->post("chunkcount/d");
$filesize = $this->request->post("filesize");
$filename = $this->request->post("filename");
$method = $this->request->method(true);
$key = $this->request->post("key");
$uploadId = $this->request->post("uploadId");
if ($action == 'merge') {
$attachment = null;
$upload = null;
//合并分片
if ($config['uploadmode'] == 'server') {
//合并分片文件
try {
$upload = new Upload();
$attachment = $upload->merge($chunkid, $chunkcount, $filename);
} catch (UploadException $e) {
$this->error($e->getMessage());
}
}
$etags = $this->request->post("etags/a", []);
if (count($etags) != $chunkcount) {
$checkDeleteFile($attachment, $upload, true);
$this->error("分片数据错误");
}
$listParts = [];
for ($i = 0; $i < $chunkcount; $i++) {
$listParts[] = array("PartNumber" => $i + 1, "ETag" => $etags[$i]);
}
try {
$result = $oss->completeMultipartUpload(array(
'Bucket' => $config['bucket'],
'Key' => $key,
'UploadId' => $uploadId,
'Parts' => $listParts
)
);
} catch (\Exception $e) {
$checkDeleteFile($attachment, $upload, true);
$this->error($e->getMessage());
}
if (!isset($result['Key'])) {
$checkDeleteFile($attachment, $upload, true);
$this->error("上传失败");
} else {
$checkDeleteFile($attachment, $upload);
$this->success("上传成功", '', ['url' => "/" . $key, 'fullurl' => cdnurl("/" . $key, true)]);
}
} else {
//默认普通上传文件
$file = $this->request->file('file');
try {
$upload = new Upload($file);
$file = $upload->chunk($chunkid, $chunkindex, $chunkcount);
} catch (UploadException $e) {
$this->error($e->getMessage());
}
try {
$params = array(
'Bucket' => $config['bucket'],
'Key' => $key,
'UploadId' => $uploadId,
'PartNumber' => $chunkindex + 1,
'Body' => $file->fread($file->getSize())
);
$ret = $oss->uploadPart($params);
$etag = $ret['ETag'];
} catch (\Exception $e) {
$this->error($e->getMessage());
}
$this->success("上传成功", "", [], 3, ['ETag' => $etag]);
}
} else {
$attachment = null;
//默认普通上传文件
$file = $this->request->file('file');
try {
$upload = new Upload($file);
$attachment = $upload->upload();
} catch (UploadException $e) {
$this->error($e->getMessage());
}
//文件绝对路径
$filePath = $upload->getFile()->getRealPath() ?: $upload->getFile()->getPathname();
$url = $attachment->url;
try {
$ret = $oss->upload($config['bucket'], ltrim($attachment->url, "/"), $upload->getFile());
//成功不做任何操作
} catch (\Exception $e) {
$checkDeleteFile($attachment, $upload, true);
$this->error("上传失败");
}
$checkDeleteFile($attachment, $upload);
$this->success("上传成功", '', ['url' => $url, 'fullurl' => cdnurl($url, true)]);
}
return;
}
public function noticeApi($size,$name,$md5,$type,$url,$width,$height,$category,$costoken,$uid){
$this->costoken = $costoken;
$this->check();
$category = array_key_exists($category, config('site.attachmentcategory') ?? []) ? $category : '';
$suffix = strtolower(pathinfo($name, PATHINFO_EXTENSION));
$suffix = $suffix && preg_match("/^[a-zA-Z0-9]+$/", $suffix) ? $suffix : 'file';
$attachment = Attachment::where('url', $url)->where('storage', 'cos')->find();
if (!$attachment) {
$params = array(
'category' => $category,
'admin_id' => 0,
'user_id' => $uid,
'filesize' => $size,
'filename' => $name,
'imagewidth' => $width,
'imageheight' => $height,
'imagetype' => $suffix,
'imageframes' => 0,
'mimetype' => $type,
'url' => $url,
'uploadtime' => time(),
'storage' => 'cos',
'sha1' => $md5,
);
Attachment::create($params, true);
}
$this->success();
return;
}
/**
* 回调
*/
public function notify()
{
$this->check();
$size = $this->request->post('size/d');
$name = $this->request->post('name', '');
$md5 = $this->request->post('md5', '');
$type = $this->request->post('type', '');
$url = $this->request->post('url', '');
$width = $this->request->post('width/d');
$height = $this->request->post('height/d');
$category = $this->request->post('category', '');
$category = array_key_exists($category, config('site.attachmentcategory') ?? []) ? $category : '';
$suffix = strtolower(pathinfo($name, PATHINFO_EXTENSION));
$suffix = $suffix && preg_match("/^[a-zA-Z0-9]+$/", $suffix) ? $suffix : 'file';
$attachment = Attachment::where('url', $url)->where('storage', 'cos')->find();
if (!$attachment) {
$params = array(
'category' => $category,
'admin_id' => (int)session('admin.id'),
'user_id' => (int)cookie('uid'),
'filesize' => $size,
'filename' => $name,
'imagewidth' => $width,
'imageheight' => $height,
'imagetype' => $suffix,
'imageframes' => 0,
'mimetype' => $type,
'url' => $url,
'uploadtime' => time(),
'storage' => 'cos',
'sha1' => $md5,
);
Attachment::create($params, true);
}
$this->success();
return;
}
/**
* 检查签名是否正确或过期
*/
protected function check()
{
$costoken = $this->request->post('costoken', '', 'trim');
$costoken = $costoken?$costoken:$this->costoken;
if (!$costoken) {
$this->error("参数不正确");
}
$config = get_addon_config('cos');
list($appId, $sign, $data) = explode(':', $costoken);
if (!$appId || !$sign || !$data) {
$this->error("参数不正确");
}
if ($appId !== $config['appId']) {
$this->error("参数不正确");
}
if ($sign !== base64_encode(hash_hmac('sha1', base64_decode($data), $config['secretKey'], true))) {
$this->error("签名不正确");
}
$json = json_decode(base64_decode($data), true);
if (($json['deadline'] ?? null)< time()) {
$this->error("请求已经超时");
}
}
}