PageRenderTime 39ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/source/Library/Think/Upload/Driver/Bcs.class.php

https://gitlab.com/fangjianwei/weifenxiao
PHP | 237 lines | 141 code | 26 blank | 70 comment | 22 complexity | 8f4cf28248acbd491a05867da92c0e97 MD5 | raw file
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2013 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: Jay <yangweijiester@gmail.com> <http://code-tech.diandian.com>
  10. // +----------------------------------------------------------------------
  11. namespace Think\Upload\Driver;
  12. use Think\Upload\Driver\Bcs\BaiduBcs;
  13. class Bcs{
  14. /**
  15. * 上传文件根目录
  16. * @var string
  17. */
  18. private $rootPath;
  19. const DEFAULT_URL = 'bcs.duapp.com';
  20. /**
  21. * 上传错误信息
  22. * @var string
  23. */
  24. private $error = '';
  25. public $config = array(
  26. 'AccessKey'=> '',
  27. 'SecretKey'=> '', //百度云服务器
  28. 'bucket' => '', //空间名称
  29. 'rename' => false,
  30. 'timeout' => 3600, //超时时间
  31. );
  32. public $bcs = null;
  33. /**
  34. * 构造函数,用于设置上传根路径
  35. * @param string $root 根目录
  36. * @param array $config FTP配置
  37. */
  38. public function __construct($root, $config){
  39. /* 默认FTP配置 */
  40. $this->config = array_merge($this->config, $config);
  41. /* 设置根目录 */
  42. $this->rootPath = str_replace('./', '/', $root);
  43. $bcsClass = dirname(__FILE__). "/Bcs/bcs.class.php";
  44. if(is_file($bcsClass))
  45. require_once($bcsClass);
  46. $this->bcs = new BaiduBCS ( $this->config['AccessKey'], $this->config['SecretKey'], self:: DEFAULT_URL );
  47. }
  48. /**
  49. * 检测上传根目录(百度云上传时支持自动创建目录,直接返回)
  50. * @return boolean true-检测通过,false-检测失败
  51. */
  52. public function checkRootPath(){
  53. return true;
  54. }
  55. /**
  56. * 检测上传目录(百度云上传时支持自动创建目录,直接返回)
  57. * @param string $savepath 上传目录
  58. * @return boolean 检测结果,true-通过,false-失败
  59. */
  60. public function checkSavePath($savepath){
  61. return true;
  62. }
  63. /**
  64. * 创建文件夹 (百度云上传时支持自动创建目录,直接返回)
  65. * @param string $savepath 目录名称
  66. * @return boolean true-创建成功,false-创建失败
  67. */
  68. public function mkdir($savepath){
  69. return true;
  70. }
  71. /**
  72. * 保存指定文件
  73. * @param array $file 保存的文件信息
  74. * @param boolean $replace 同名文件是否覆盖
  75. * @return boolean 保存状态,true-成功,false-失败
  76. */
  77. public function save(&$file,$replace=true) {
  78. $opt = array ();
  79. $opt ['acl'] = BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_WRITE;
  80. $opt ['curlopts'] = array (
  81. CURLOPT_CONNECTTIMEOUT => 10,
  82. CURLOPT_TIMEOUT => 1800
  83. );
  84. $object = "/{$file['savepath']}{$file['savename']}";
  85. $response = $this->bcs->create_object ( $this->config['bucket'], $object, $file['tmp_name'], $opt );
  86. $url = $this->download($object);
  87. $file['url'] = $url;
  88. return $response->isOK() ? true : false;
  89. }
  90. public function download($file){
  91. $file = str_replace('./', '/', $file);
  92. $opt = array();
  93. $opt['time'] = mktime('2049-12-31'); //这是最长有效时间!--
  94. $response = $this->bcs->generate_get_object_url ( $this->config['bucket'], $file, $opt );
  95. return $response;
  96. }
  97. /**
  98. * 获取最后一次上传错误信息
  99. * @return string 错误信息
  100. */
  101. public function getError(){
  102. return $this->error;
  103. }
  104. /**
  105. * 请求百度云服务器
  106. * @param string $path 请求的PATH
  107. * @param string $method 请求方法
  108. * @param array $headers 请求header
  109. * @param resource $body 上传文件资源
  110. * @return boolean
  111. */
  112. private function request($path, $method, $headers = null, $body = null){
  113. $ch = curl_init($path);
  114. $_headers = array('Expect:');
  115. if (!is_null($headers) && is_array($headers)){
  116. foreach($headers as $k => $v) {
  117. array_push($_headers, "{$k}: {$v}");
  118. }
  119. }
  120. $length = 0;
  121. $date = gmdate('D, d M Y H:i:s \G\M\T');
  122. if (!is_null($body)) {
  123. if(is_resource($body)){
  124. fseek($body, 0, SEEK_END);
  125. $length = ftell($body);
  126. fseek($body, 0);
  127. array_push($_headers, "Content-Length: {$length}");
  128. curl_setopt($ch, CURLOPT_INFILE, $body);
  129. curl_setopt($ch, CURLOPT_INFILESIZE, $length);
  130. } else {
  131. $length = @strlen($body);
  132. array_push($_headers, "Content-Length: {$length}");
  133. curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
  134. }
  135. } else {
  136. array_push($_headers, "Content-Length: {$length}");
  137. }
  138. // array_push($_headers, 'Authorization: ' . $this->sign($method, $uri, $date, $length));
  139. array_push($_headers, "Date: {$date}");
  140. curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers);
  141. curl_setopt($ch, CURLOPT_TIMEOUT, $this->config['timeout']);
  142. curl_setopt($ch, CURLOPT_HEADER, 1);
  143. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  144. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
  145. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  146. if ($method == 'PUT' || $method == 'POST') {
  147. curl_setopt($ch, CURLOPT_POST, 1);
  148. } else {
  149. curl_setopt($ch, CURLOPT_POST, 0);
  150. }
  151. if ($method == 'HEAD') {
  152. curl_setopt($ch, CURLOPT_NOBODY, true);
  153. }
  154. $response = curl_exec($ch);
  155. $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  156. curl_close($ch);
  157. list($header, $body) = explode("\r\n\r\n", $response, 2);
  158. if ($status == 200) {
  159. if ($method == 'GET') {
  160. return $body;
  161. } else {
  162. $data = $this->response($header);
  163. return count($data) > 0 ? $data : true;
  164. }
  165. } else {
  166. $this->error($header);
  167. return false;
  168. }
  169. }
  170. /**
  171. * 获取响应数据
  172. * @param string $text 响应头字符串
  173. * @return array 响应数据列表
  174. */
  175. private function response($text){
  176. $items = json_decode($text, true);
  177. return $items;
  178. }
  179. /**
  180. * 生成请求签名
  181. * @return string 请求签名
  182. */
  183. private function sign($method, $Bucket, $object='/', $size=''){
  184. if(!$size)
  185. $size = $this->config['size'];
  186. $param = array(
  187. 'ak'=>$this->config['AccessKey'],
  188. 'sk'=>$this->config['SecretKey'],
  189. 'size'=>$size,
  190. 'bucket'=>$Bucket,
  191. 'host'=>self :: DEFAULT_URL,
  192. 'date'=>time()+$this->config['timeout'],
  193. 'ip'=>'',
  194. 'object'=>$object
  195. );
  196. $response = $this->request($this->apiurl.'?'.http_build_query($param), 'POST');
  197. if($response)
  198. $response = json_decode($response, true);
  199. return $response['content'][$method];
  200. }
  201. /**
  202. * 获取请求错误信息
  203. * @param string $header 请求返回头信息
  204. */
  205. private function error($header) {
  206. list($status, $stash) = explode("\r\n", $header, 2);
  207. list($v, $code, $message) = explode(" ", $status, 3);
  208. $message = is_null($message) ? 'File Not Found' : "[{$status}]:{$message}";
  209. $this->error = $message;
  210. }
  211. }