123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- <?php
- /**
- * 易优CMS
- * ============================================================================
- * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
- * 网站地址: http://www.eyoucms.com
- * ----------------------------------------------------------------------------
- * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
- * ============================================================================
- * Author: 陈风任 <491085389@qq.com>
- * Date: 2019-04-23
- */
-
- namespace weapp\Qiniuyun\model;
-
- use think\Model;
- use think\Db;
-
- //引入七牛云的相关文件
- use Qiniu\Auth as Auth;
- use Qiniu\Storage\BucketManager;
- use Qiniu\Storage\UploadManager;
- use Qiniu\Http\Client;
- use Qiniu\Http\Error;
- /**
- * 模型
- */
- class QiniuyunModel extends Model
- {
- //初始化
- protected function initialize()
- {
- $this->weapp_db = Db::name('weapp');
- // 需要调用`Model`的`initialize`方法
- parent::initialize();
- require_once ROOT_PATH.'weapp/Qiniuyun/vendor/Qiniu/autoload.php';
- }
-
- // 获取bucket列表
- public function listBucket($Qiniuyun=null){
- // 判断传入的七牛云配置信息并实例化七牛云操作类
- if (!empty($Qiniuyun) && is_array($Qiniuyun)) {
- $auth = new Auth($Qiniuyun['access_key'], $Qiniuyun['secret_key']);
- }else{
- $data = $this->weapp_db->where('code','Qiniuyun')->field('data')->find();
- $Qiniuyun = json_decode($data['data'], true);
- $auth = new Auth($Qiniuyun['access_key'], $Qiniuyun['secret_key']);
- }
-
- // 查询空间
- $url = "http://rs.qbox.me/buckets";
- $headers = $auth->authorization($url);
- $ret = Client::get($url, $headers);
-
- // 结果返回
- $result = json_decode(json_encode($ret),true);
- $body = json_decode($result['body'],true);
- if ('200' == $result['statusCode']) {
- if (!empty($body)) {
- return $body;
- }else{
- return false;
- }
- }else{
- return 'false';
- }
- }
-
- // 生成bucket
- public function createBucket($Qiniuyun,$region="z0"){
- if (!is_array($Qiniuyun)) {
- return ['code'=>0, 'msg'=>'AccessKey或SecretKey可能配置有误,请检查!'];
- }
-
- // 七牛云配置信息并实例化七牛云操作类
- $auth = new Auth($Qiniuyun['access_key'], $Qiniuyun['secret_key']);
-
- // bucket加密
- $find = array('+', '/');
- $replace = array('-', '_');
- $bucket = str_replace($find, $replace, base64_encode($Qiniuyun['bucket']));
-
- // 生成bucket空间
- $url = "http://rs.qiniu.com/mkbucketv2/$bucket/region/$region";
- $headers = $auth->authorization($url);
- $ret = Client::post($url,"", $headers);
-
- // 结果返回
- $result = json_decode(json_encode($ret),true);
- if ('614' == $result['statusCode']) {
- // 这个状态表示七牛云上已有相同的存储空间名,生成失败,但是返回TRUE,添加成功时,会获取七牛云相关存储空间回来
- return ['code'=>1, 'msg'=>'配置成功'];
- }
- if ('200' == $result['statusCode']) {
- return ['code'=>1, 'msg'=>'配置成功'];
- }else{
- $msg = $this->codeList($result['statusCode']);
- return ['code'=>0, 'msg'=>$msg];
- }
- }
-
- // 获取bucket空间域名
- public function listBucketDomain($Qiniuyun=null){
- // 判断传入的七牛云配置信息并实例化七牛云操作类
- if (!empty($Qiniuyun) && is_array($Qiniuyun)) {
- $auth = new Auth($Qiniuyun['access_key'], $Qiniuyun['secret_key']);
- $bucket = $Qiniuyun['bucket'];
- }else{
- $data = $this->weapp_db->where('code','Qiniuyun')->field('data')->find();
- $Qiniuyun = json_decode($data['data'], true);
- $auth = new Auth($Qiniuyun['access_key'], $Qiniuyun['secret_key']);
- $bucket = $Qiniuyun['bucket'];
- }
-
- // 查询空间
- $url = "http://api.qiniu.com/v6/domain/list?tbl=$bucket";
- $headers = $auth->authorization($url);
- $ret = Client::get($url, $headers);
-
- // 结果返回
- $result = json_decode(json_encode($ret),true);
- $body = json_decode($result['body'],true);
- if ('200' == $result['statusCode']) {
- if (!empty($body)) {
- return $body;
- }else{
- return false;
- }
- }else{
- return false;
- }
- }
-
- // 判断bucket是否已存在
- public function doBucketExist($Qiniuyun){
- $result = $this->listBucket($Qiniuyun);
- if ('false' != $result) {
- if(in_array($Qiniuyun['bucket'], $result)){
- return '存储空间名已经存在,请检查!';
- }
- }
- return false;
- }
-
- /**
- * 参数说明:
- * $config 配置信息
- * $images 本地图片地址
- * $fileName 文件路径名
- * $is_tcp 是否需要加上域名协议
- * 返回说明:
- * return false 没有配置齐全
- * return true 同步成功
- */
- public function Synchronize($config = [], $images = null,$is_tcp = true)
- {
- static $qnyConfig = null;
- // 若没有传入配信信息则读取数据库
- if (!empty($config)) {
- $qnyConfig = $config;
- } else if (null == $qnyConfig) {
- $data = Db::name('weapp')->where('code','Qiniuyun')->field('data')->find();
- $qnyConfig = json_decode($data['data'], true);
- }
-
- /*支持子目录*/
- $images = preg_replace('#^(/[/\w]+)?(/uploads/)#i', '$2', $images);
-
- // 配置为空则返回原图片路径
- if (empty($qnyConfig) || empty($qnyConfig['domain'])) {
- $result = [
- 'url' => ROOT_DIR . $images,
- 'state' => 'SUCCESS',
- ];
- return $result;
- }
-
- //引入七牛云的相关文件
- weapp_vendor('Qiniu.src.Qiniu.Auth', 'Qiniuyun');
- weapp_vendor('Qiniu.src.Qiniu.Storage.UploadManager', 'Qiniuyun');
- require_once ROOT_PATH.'weapp/Qiniuyun/vendor/Qiniu/autoload.php';
-
- // 配置信息
- $accessKey = $qnyConfig['access_key'];
- $secretKey = $qnyConfig['secret_key'];
- $bucket = $qnyConfig['bucket'];
- $domain = $qnyConfig['domain'];
- // 构建鉴权对象
- $auth = new \Qiniu\Auth($accessKey, $secretKey);
- // 生成上传 Token
- $token = $auth->uploadToken($bucket);
- // 要上传文件的本地路径
- $filePath = realpath('.'.$images);
- // 上传到七牛后保存的文件名
- $key = ltrim($images, '/');
- // 初始化 UploadManager 对象并进行文件的上传。
- $uploadMgr = new \Qiniu\Storage\UploadManager;
- // 调用 UploadManager 的 putFile 方法进行文件的上传。
- list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
- // list($ret, $err) = $uploadMgr->put($token, $key, $filePath);
- if (empty($err) || $err === null) {
- $tcp = '//';
- if (!empty($is_tcp)) {
- $web_is_https = tpCache('global.web_is_https');
- $tcp_key = !empty($qnyConfig['tcp']) ? $qnyConfig['tcp'] : '';
- if ($web_is_https && $tcp_key == 1){ //cms为https而图片存储路径为http,图片必须带协议头
- switch ($tcp_key) {
- case '2':
- $tcp = 'https://';
- break;
-
- case '3':
- $tcp = '//';
- break;
-
- case '1':
- default:
- $tcp = 'http://';
- break;
- }
- }
-
- }
- // 是否开启删除本地图片
- if (!empty($qnyConfig['local_save'])) {
- $this->del_local('/'.ltrim($images, '/'));
- }
- $images = $tcp.$domain.'/'.ltrim($images, '/');
- }
-
- return [
- 'state' => 'SUCCESS',
- 'url' => $images,
- ];
- }
-
- /**
- * 对象存储与本地存储平滑切换展示
- * @param array $qnyConfig [description]
- * @param array $parseData [description]
- * @param string $images [description]
- * @return [type] [description]
- */
- public function handle_subdir_pic($qnyConfig = [], $parseData = [], $images = '')
- {
- if ($qnyConfig['domain'] == $parseData['host']) {
- $tcp = !empty($qnyConfig['tcp']) ? $qnyConfig['tcp'] : '';
- switch ($tcp) {
- case '2':
- $tcp = 'https://';
- break;
-
- case '3':
- $tcp = '//';
- break;
-
- case '1':
- default:
- $tcp = 'http://';
- break;
- }
- $images = $tcp.$qnyConfig['domain'].$parseData['path'];
- }else{
- // 若切换了存储空间或访问域名,与数据库中存储的图片路径域名不一致时,访问本地路径,保证图片正常
- $images = ROOT_DIR.$parseData['path'];
- }
-
- return $images;
- }
-
- /**
- * 未开启同步本地功能,并删除本地图片
- * @return [type] [description]
- */
- public function del_local($filename = '')
- {
- $arr = explode('?', $filename);
- $filename = current($arr);
- $arr = explode('/', $filename);
- if (!empty($arr)) {
- $total = count($arr);
- $filename = $arr[$total - 1];
- $filename = str_replace('\\', '', $filename);
- unset($arr[$total - 1]);
- $filepath = implode('/', $arr);
- if (!preg_match('/^([\w\-\_\/]+)$/i', $filepath)) {
- return false;
- }
- $filename = $filepath.'/'.$filename;
- $filename= trim($filename,'.');
- $filename = preg_replace('#^(/[/\w]+)?(/public/upload/|/uploads/|/public/static/admin/logo/)#i', '$2', $filename);
- if(eyPreventShell($filename) && !empty($filename) && @file_exists('.'.$filename)){
- $filename_new = trim($filename,'/');
- $filetype = preg_replace('/^(.*)\.(\w+)$/i', '$2', $filename);
- $phpfile = strtolower(strstr($filename,'.php')); //排除PHP文件
- $size = getimagesize($filename_new);
- $fileInfo = explode('/',$size['mime']);
- if((file_exists($filename_new) && $fileInfo[0] != 'image') || $phpfile || !in_array($filetype, explode(',', config('global.image_ext')))){
- return false;
- }
-
- if( @unlink('.'.$filename) ){
- return true;
- }else{
- return false;
- }
- }
- }
-
- return true;
- }
-
- public function codeList($code = 200)
- {
- $codeList = [
- 298 => '部分操作执行成功',
- 400 => '请求报文格式错误',
- 401 => '认证授权失败',
- 403 => '权限不足,拒绝访问',
- 404 => '空间或镜像源资源不存在!',
- 405 => '请求方式错误',
- 406 => '上传的数据 CRC32 校验错误',
- 413 => '请求资源大小大于指定的最大值',
- 419 => '用户账号被冻结',
- 478 => '镜像源服务器出现异常',
- 502 => '错误网关',
- 503 => '服务端不可用',
- 504 => '服务端操作超时',
- 573 => '单个资源访问频率过高',
- 579 => '上传成功但是七牛服务器异常',
- 599 => '服务端操作失败',
- 608 => '资源内容被修改',
- 612 => '指定资源不存在或已被删除',
- 614 => '目标资源已存在',
- 630 => '已创建的空间数量达到上限,无法创建新空间',
- 631 => '指定空间不存在',
- 640 => '调用列举资源(list)接口时,指定非法的marker参数',
- 701 => '在断点续上传过程中,后续上传接收地址不正确或ctx信息已过期',
- ];
-
- return !empty($codeList[$code]) ? $codeList[$code] : 'AccessKey或SecretKey可能配置有误!';
- }
- }
|