Bcs.class.php 7.9 KB

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