| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 | 
							- <?php
 
- /**
 
-  * 又拍云上传驱动
 
-  *
 
-  *
 
-  *
 
-  *
 
-  */
 
- class UCloud_Engine_Upyun{
 
-     /**
 
-      * 上传文件根目录
 
-      * @var string
 
-      */
 
-     private $rootPath;
 
-     /**
 
-      * 上传错误信息
 
-      * @var string
 
-      */
 
-     private $error = '';
 
-     private $config = array(
 
-         'host'     => '', //又拍云服务器
 
-         'username' => '', //又拍云用户
 
-         'password' => '', //又拍云密码
 
-         'bucket'   => '', //空间名称
 
-         'timeout'  => 90, //超时时间
 
-     );
 
-     /**
 
-      * 构造函数,用于设置上传根路径
 
-      * @param string $root   根目录
 
-      * @param array  $config FTP配置
 
-      */
 
- 	public function __construct($root, $config){
 
-         /* 默认FTP配置 */
 
-         $this->config = array_merge($this->config, $config);
 
-         $this->config['host'] = $this->config['api'];
 
-         $this->config['username'] = $this->config['accessKey'];
 
-         $this->config['password'] = md5($this->config['secretKey']);
 
-         $this->config['host'] = $this->config['host'];
 
-         $this->config['timeout'] = $this->config['timeout'];
 
-         /* 设置根目录 */
 
-         $this->rootPath = trim($root, './') . '/';
 
- 	}
 
-     /**
 
-      * 检测上传根目录(又拍云上传时支持自动创建目录,直接返回)
 
-      * @return boolean true-检测通过,false-检测失败
 
-      */
 
-     public function checkRootPath(){
 
-         return true;
 
-     }
 
-     /**
 
-      * 检测上传目录(又拍云上传时支持自动创建目录,直接返回)
 
-      * @param  string $savepath 上传目录
 
-      * @return boolean          检测结果,true-通过,false-失败
 
-      */
 
- 	public function checkSavePath($savepath){
 
- 		return true;
 
-     }
 
-     /**
 
-      * 创建文件夹 (又拍云上传时支持自动创建目录,直接返回)
 
-      * @param  string $savepath 目录名称
 
-      * @return boolean          true-创建成功,false-创建失败
 
-      */
 
-     public function mkdir($savepath){
 
-     	return true;
 
-     }
 
-     /**
 
-      * 保存指定文件
 
-      * @param  array   $file    保存的文件信息
 
-      * @param  boolean $replace 同名文件是否覆盖
 
-      * @return boolean          保存状态,true-成功,false-失败
 
-      */
 
-     public function save($file, $replace = true, $opt = array()) {
 
-         $header['Content-Type'] = $file['type'];
 
-         $header['Content-MD5'] = md5_file($file['tmp_name']);
 
-         //是否开启缩略图
 
-         if($opt['ifresize'] == 1) {
 
-             //不传将自动设置为限制宽度,高度自适应
 
-             $header['x-gmkerl-type'] = $opt['type']? $opt['type']: 'fix_width'; 
 
-             
 
-             //宽度高度
 
-             $header['x-gmkerl-value'] = $opt['value']? $opt['value']: '150';
 
-             //图片压缩质量
 
-             $header['x-gmkerl-quality'] = $opt['quality']? $opt['quality']: '95';
 
-             //图片锐化,默认开启锐化
 
-             $header['x-gmkerl-unsharp'] = $opt['unsharp']? $opt['unsharp']: 'true';
 
-         }
 
-         $header['Mkdir'] = 'true';
 
-         //打开文件
 
-         $resource = fopen($file['tmp_name'], 'rb');
 
-         $save = $this->rootPath . $file['savepath'] ."/". $file['savename'];
 
-         $data = $this->request($save, 'PUT', $header, $resource);
 
-         //$file['url']= $this->config['domain']."/".$save; //返回路径
 
-         return false === $data ? false : true;
 
-     }
 
-     /**
 
-      * 获取最后一次上传错误信息
 
-      * @return string 错误信息
 
-      */
 
-     public function getError(){
 
-         return $this->error;
 
-     }
 
-     /**
 
-      * 请求又拍云服务器
 
-      * @param  string   $path    请求的PATH
 
-      * @param  string   $method  请求方法
 
-      * @param  array    $headers 请求header
 
-      * @param  resource $body    上传文件资源
 
-      * @return boolean
 
-      */
 
-     private function request($path, $method, $headers = null, $body = null){
 
-     	$uri = "/{$this->config['bucket']}{$path}";
 
-         $ch = curl_init("http://{$this->config['host']}{$uri}");
 
-         $_headers = array('Expect:');
 
-         if (!is_null($headers) && is_array($headers)){
 
-             foreach($headers as $k => $v) {
 
-                 array_push($_headers, "{$k}: {$v}");
 
-             }
 
-         }
 
-         $length = 0;
 
- 		$date   = gmdate('D, d M Y H:i:s \G\M\T');
 
-         if (!is_null($body)) {
 
-             if(is_resource($body)){
 
-                 fseek($body, 0, SEEK_END);
 
-                 $length = ftell($body);
 
-                 fseek($body, 0);
 
-                 array_push($_headers, "Content-Length: {$length}");
 
-                 curl_setopt($ch, CURLOPT_INFILE, $body);
 
-                 curl_setopt($ch, CURLOPT_INFILESIZE, $length);
 
-             } else {
 
-                 $length = @strlen($body);
 
-                 array_push($_headers, "Content-Length: {$length}");
 
-                 curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
 
-             }
 
-         } else {
 
-             array_push($_headers, "Content-Length: {$length}");
 
-         }
 
-         array_push($_headers, "Authorization: {$this->sign($method, $uri, $date, $length)}");
 
-         //array_push($_headers, 'Authorization: ' . $this->sign($method, $uri, $date, $length));
 
-         array_push($_headers, "Date: {$date}");
 
-         curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers);
 
-         curl_setopt($ch, CURLOPT_TIMEOUT, $this->config['timeout']);
 
-         curl_setopt($ch, CURLOPT_HEADER, 1);
 
-         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
-         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
 
- 		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
 
-         if ($method == 'PUT' || $method == 'POST') {
 
- 			curl_setopt($ch, CURLOPT_POST, 1);
 
-         } else {
 
- 			curl_setopt($ch, CURLOPT_POST, 0);
 
-         }
 
-         if ($method == 'HEAD') {
 
-             curl_setopt($ch, CURLOPT_NOBODY, true);
 
-         }
 
-         $response = curl_exec($ch);
 
-         $status   = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 
-         curl_close($ch);
 
-         list($header, $body) = explode("\r\n\r\n", $response, 2);
 
-         if ($status == 200) {
 
-             if ($method == 'GET') {
 
-                 return $body;
 
-             } else {
 
-                 $data = $this->response($header);
 
-                 return count($data) > 0 ? $data : true;
 
-             }
 
-         } else {
 
-             $this->error($header);
 
-             return false;
 
-         }
 
-     }
 
-     /**
 
-      * 获取响应数据
 
-      * @param  string $text 响应头字符串
 
-      * @return array        响应数据列表
 
-      */
 
-     private function response($text){
 
-         $headers = explode("\r\n", $text);
 
-         $items = array();
 
-         foreach($headers as $header) {
 
-             $header = trim($header);
 
-             if(strpos($header, 'x-upyun') !== False){
 
-                 list($k, $v) = explode(':', $header);
 
-                 $items[trim($k)] = in_array(substr($k,8,5), array('width','heigh','frame')) ? intval($v) : trim($v);
 
-             }
 
-         }
 
-         return $items;
 
-     }
 
-     /**
 
-      * 生成请求签名
 
-      * @param  string  $method 请求方法
 
-      * @param  string  $uri    请求URI
 
-      * @param  string  $date   请求时间
 
-      * @param  integer $length 请求内容大小
 
-      * @return string          请求签名
 
-      */
 
-     private function sign($method, $uri, $date, $length){
 
- 		$sign = "{$method}&{$uri}&{$date}&{$length}&{$this->config['password']}";
 
- 		return 'UpYun ' . $this->config['username'] . ':' . md5($sign);
 
- 	}
 
-     /**
 
-      * 获取请求错误信息
 
-      * @param  string $header 请求返回头信息
 
-      */
 
- 	private function error($header) {
 
-         list($status, $stash) = explode("\r\n", $header, 2);
 
-         list($v, $code, $message) = explode(" ", $status, 3);
 
-         $message = is_null($message) ? 'File Not Found' : "[{$status}]:{$message}";
 
-         $this->error = $message;
 
-     }
 
- }
 
 
  |