123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- <?php
-
-
- 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');
-
- parent::initialize();
- require_once ROOT_PATH.'weapp/Qiniuyun/vendor/Qiniu/autoload.php';
- }
-
-
- 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';
- }
- }
-
-
- 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']);
-
-
- $find = array('+', '/');
- $replace = array('-', '_');
- $bucket = str_replace($find, $replace, base64_encode($Qiniuyun['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']) {
-
- return ['code'=>1, 'msg'=>'配置成功'];
- }
- if ('200' == $result['statusCode']) {
- return ['code'=>1, 'msg'=>'配置成功'];
- }else{
- $msg = $this->codeList($result['statusCode']);
- return ['code'=>0, 'msg'=>$msg];
- }
- }
-
-
- 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;
- }
- }
-
-
- public function doBucketExist($Qiniuyun){
- $result = $this->listBucket($Qiniuyun);
- if ('false' != $result) {
- if(in_array($Qiniuyun['bucket'], $result)){
- return '存储空间名已经存在,请检查!';
- }
- }
- return false;
- }
-
-
-
- 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 = $auth->uploadToken($bucket);
-
- $filePath = realpath('.'.$images);
-
- $key = ltrim($images, '/');
-
- $uploadMgr = new \Qiniu\Storage\UploadManager;
-
- list($ret, $err) = $uploadMgr->putFile($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){
- 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,
- ];
- }
-
-
-
- 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;
- }
-
-
-
- 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'));
- $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可能配置有误!';
- }
- }
|