<?php
/**
 * [UQcms System] Copyright uqcms.com
 * QQ: 2200631718
 * Author: gj.name
 * This is NOT a freeware
*/  class oss_qcloud { private static $timeout = 60; private static $instance; public static function init(){ if (is_null(self::$instance)){ self::$instance = new self(); } return self::$instance; } function __construct($param, $bucket){ $this->appid = $param['appID']; $this->secretId = $param['secretID']; $this->secretKey = $param['secretKey']; $this->bucket = $bucket; $this->deadline = 180; $this->url = 'http://'.$param['area'].'.file.myqcloud.com/files/v2/'.$param['appID'].'/'; } function upload($srcPath, $dstPath){ $size = filesize($srcPath); if($size <= ((1024*1024)*2)){ $url = $this->url . $this->bucket .'/'. $dstPath; $srcPath = realpath($srcPath); $datas = array( 'op' => 'upload', 'sha' => hash_file('sha1', $srcPath), 'biz_attr' => '', 'insertOnly' => '0', 'filecontent' => file_get_contents($srcPath) ); $arr = $this->buildCustomPostFields($datas); $method = 'POST'; $header[] = 'Method:'.$method; $header[] = 'Authorization: '.$this->sign(); $header[] = 'User-Agent:cos-php-sdk-v4.2.3'; $header[] = 'Connection: keep-alive'; $header[] = 'Expect: 100-continue'; array_push($header, 'Content-Type: multipart/form-data; boundary='.$arr[0]); $result = $this->curl_post($url, $arr[1], $header); $result = json_decode($result); if($result->code == '0'){ $data['error'] = '0'; $data['size'] = $size; $data['name'] = $dstPath; $data['url'] = $result->data->source_url; return $data; }else{ $data['error'] = 1; $data['msg'] = '上传错误'; return $data; } }else{ $data['error'] = 1; $data['msg'] = '上传的文件过大'; return $data; } } function del($dstPath){ $url = $this->url .$this->bucket .'/'. $dstPath; $fileId = '/' . $this->appid . '/' . $this->bucket . '/' . $dstPath; $header[] = 'Authorization: '.$this->sign($fileId); $header[] = 'Content-Type: application/json'; $data = json_encode(array('op'=>'delete')); $result = $this->curl_post($url, $data, $header); $result = json_decode($result); if($result->code == '0'){ return true; }else{ return false; } } private function sign($fileId = null){ if($fileId){ $expired = 0; }else{ $expired = time() + 30; } $now = time(); $random = rand(); $plainText = "a=".$this->appid."&k=".$this->secretId."&e=".$expired."&t=".$now."&r=".$random."&f=".$fileId."&b=".$this->bucket; $bin = hash_hmac('SHA1', $plainText, $this->secretKey, true); $bin = $bin.$plainText; $signature = base64_encode($bin); return $signature; } private function curl_post($url, $data, $header = array()){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); $method = 'POST'; curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_TIMEOUT, '30'); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl, CURLOPT_HTTPHEADER, $header); $ret = curl_exec($curl); return $ret; } function buildCustomPostFields($fields) { $body = array(); static $disallow = array("\0", "\"", "\r", "\n"); foreach ($fields as $key => $value) { $key = str_replace($disallow, "_", $key); $body[] = implode("\r\n", array( "Content-Disposition: form-data; name=\"{$key}\"", '', filter_var($value), )); } do{ $boundary = "---------------------" . md5(mt_rand() . microtime()); }while(preg_grep("/{$boundary}/", $body)); foreach ($body as &$part) { $part = "--{$boundary}\r\n{$part}"; } unset($part); $body[] = "--{$boundary}--"; $body[] = ''; return array($boundary, implode("\r\n", $body)); } static function img_size($w, $h){ if($h == '0'){ return '?imageView2/0/w/'.$w; }elseif($w == '10000'){ return '?imageView2/0/h/'.$h; }elseif($w != '0' && $h != '0'){ return '?imageView2/0/w/'.$w.'/h/'.$h; }else{ return ''; } } }