Base.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace app\sys\controller;
  3. use OSS\OssClient;
  4. use think\facade\Request;
  5. use DateTime;
  6. class Base
  7. {
  8. public function ossUpload($path, $exts)
  9. {
  10. $path = str_replace('\\', '/', $path);
  11. $filePath = [];
  12. // 检测要上传的文件格式是否正确
  13. foreach ($_FILES as $key => $file) {
  14. if ($file['size'] == 0) continue;
  15. $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
  16. if (!in_array($ext, $exts)) {
  17. return ['code' => 1, 'msg' => '文件格式不正确'];
  18. }
  19. $filePath[$key] = ['name' => $file['tmp_name'], 'ext' => $ext];
  20. }
  21. $accessKeyId = config('app.ali_oss_access_key_id');
  22. $accessKeySecret = config('app.ali_oss_access_key_secret');
  23. $endpoint = config('app.ali_oss_end_point');
  24. $bucket = config('app.ali_oss_bucket');
  25. $oss = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  26. $path = trim($path, '/');
  27. $data = [];
  28. foreach ($filePath as $key => $f) {
  29. $fileName = date('Ymd') . '/' . date('His') . rand(1000, 9999) . '.' . $f['ext'];
  30. $p = $path . '/' . $fileName;
  31. $oss->uploadFile($bucket, $p, $f['name']);
  32. $data[$key] = $p;
  33. }
  34. return ['code' => 0, 'data' => $data];
  35. }
  36. /*
  37. * 富文本图片
  38. */
  39. public function imgUpload()
  40. {
  41. $bindUrl = config('app.ali_oss_bindurl');
  42. $data = [
  43. 'src' => 'http://' . $bindUrl . '/' . Request::param('file'),
  44. 'title' => ''
  45. ];
  46. return json(['code' => 0, 'msg' => '', 'data' => $data]);
  47. }
  48. public function ossSignature()
  49. {
  50. $cate = input('cate');
  51. $id = config('app.ali_oss_access_key_id'); // 请填写您的AccessKeyId。
  52. $key = config('app.ali_oss_access_key_secret'); // 请填写您的AccessKeySecret。
  53. // $host的格式为 bucketname.endpoint,请替换为您的真实信息。
  54. $host = 'https://'.config('app.ali_oss_bindurl');
  55. // $callbackUrl为上传回调服务器的URL,请将下面的IP和Port配置为您自己的真实URL信息。
  56. $callbackUrl = 'https://' . $_SERVER['HTTP_HOST'] . '/sys/video/osscallback.html';
  57. if ($cate) {
  58. $dir = $cate . '/'; // 用户上传文件时指定的前缀。
  59. } else {
  60. $dir = 'videos/'; // 用户上传文件时指定的前缀。
  61. }
  62. $callback_param = array(
  63. 'callbackUrl' => $callbackUrl,
  64. 'callbackBody' => 'filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}',
  65. 'callbackBodyType' => "application/x-www-form-urlencoded"
  66. );
  67. $callback_string = json_encode($callback_param);
  68. $base64_callback_body = base64_encode($callback_string);
  69. $now = time();
  70. $expire = 30; //设置该policy超时时间是10s. 即这个policy过了这个有效时间,将不能访问。
  71. $end = $now + $expire;
  72. $expiration = $this->gmt_iso8601($end);
  73. //最大文件大小.用户可以自己设置
  74. $condition = array(0 => 'content-length-range', 1 => 0, 2 => 1048576000);
  75. $conditions[] = $condition;
  76. // 表示用户上传的数据,必须是以$dir开始,不然上传会失败,这一步不是必须项,只是为了安全起见,防止用户通过policy上传到别人的目录。
  77. $start = array(0 => 'starts-with', 1 => '$key', 2 => $dir);
  78. $conditions[] = $start;
  79. $arr = array('expiration' => $expiration, 'conditions' => $conditions);
  80. $policy = json_encode($arr);
  81. $base64_policy = base64_encode($policy);
  82. $string_to_sign = $base64_policy;
  83. $signature = base64_encode(hash_hmac('sha1', $string_to_sign, $key, true));
  84. $response = array();
  85. $response['accessid'] = $id;
  86. $response['host'] = $host;
  87. $response['policy'] = $base64_policy;
  88. $response['signature'] = $signature;
  89. $response['expire'] = $end;
  90. $response['callback'] = $base64_callback_body;
  91. $response['dir'] = $dir; // 这个参数是设置用户上传文件时指定的前缀。
  92. return json($response);
  93. }
  94. private function gmt_iso8601($time)
  95. {
  96. $dtStr = date("c", $time);
  97. $mydatetime = new DateTime($dtStr);
  98. $expiration = $mydatetime->format(DateTime::ISO8601);
  99. $pos = strpos($expiration, '+');
  100. $expiration = substr($expiration, 0, $pos);
  101. return $expiration . "Z";
  102. }
  103. public function osscallback()
  104. {
  105. // 1.获取OSS的签名header和公钥url header
  106. $authorizationBase64 = "";
  107. $pubKeyUrlBase64 = "";
  108. /*
  109. * 注意:如果要使用HTTP_AUTHORIZATION头,你需要先在apache或者nginx中设置rewrite,以apache为例,修改
  110. * 配置文件/etc/httpd/conf/httpd.conf(以你的apache安装路径为准),在DirectoryIndex index.php这行下面增加以下两行
  111. RewriteEngine On
  112. RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization},last]
  113. * */
  114. trace($_SERVER, 'debug');
  115. if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
  116. $authorizationBase64 = $_SERVER['HTTP_AUTHORIZATION'];
  117. }
  118. if (isset($_SERVER['HTTP_X_OSS_PUB_KEY_URL'])) {
  119. $pubKeyUrlBase64 = $_SERVER['HTTP_X_OSS_PUB_KEY_URL'];
  120. }
  121. if ($authorizationBase64 == '' || $pubKeyUrlBase64 == '') {
  122. header("http/1.1 403 Forbidden");
  123. exit();
  124. }
  125. // 2.获取OSS的签名
  126. $authorization = base64_decode($authorizationBase64);
  127. // 3.获取公钥
  128. $pubKeyUrl = base64_decode($pubKeyUrlBase64);
  129. $ch = curl_init();
  130. curl_setopt($ch, CURLOPT_URL, $pubKeyUrl);
  131. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  132. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  133. $pubKey = curl_exec($ch);
  134. if ($pubKey == "") {
  135. //header("http/1.1 403 Forbidden");
  136. exit();
  137. }
  138. // 4.获取回调body
  139. $body = file_get_contents('php://input');
  140. // 5.拼接待签名字符串
  141. $authStr = '';
  142. $path = $_SERVER['REQUEST_URI'];
  143. $pos = strpos($path, '?');
  144. if ($pos === false) {
  145. $authStr = urldecode($path) . "\n" . $body;
  146. } else {
  147. $authStr = urldecode(substr($path, 0, $pos)) . substr($path, $pos, strlen($path) - $pos) . "\n" . $body;
  148. }
  149. // 6.验证签名
  150. $ok = openssl_verify($authStr, $authorization, $pubKey, OPENSSL_ALGO_MD5);
  151. if ($ok == 1) {
  152. header("Content-Type: application/json");
  153. $data = array("Status" => "Ok");
  154. return json($data);
  155. } else {
  156. //header("http/1.1 403 Forbidden");
  157. exit();
  158. }
  159. }
  160. }