123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace addons\alioss\library;
- use app\common\library\Upload;
- class Auth
- {
- public function __construct()
- {
- }
- public function params($name, $md5, $callback = true)
- {
- $config = get_addon_config('alioss');
- $callback_param = array(
- 'callbackUrl' => isset($config['notifyurl']) ? $config['notifyurl'] : '',
- 'callbackBody' => 'filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}',
- 'callbackBodyType' => "application/x-www-form-urlencoded"
- );
- $base64_callback_body = base64_encode(json_encode($callback_param));
- $now = time();
- $end = $now + $config['expire'];
- $expiration = $this->gmt_iso8601($end);
- preg_match('/(\d+)(\w+)/', $config['maxsize'], $matches);
- $type = strtolower($matches[2]);
- $typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3];
- $size = (int)$config['maxsize'] * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0);
-
- $condition = array(0 => 'content-length-range', 1 => 0, 2 => $size);
- $conditions[] = $condition;
-
-
-
- $arr = array('expiration' => $expiration, 'conditions' => $conditions);
- $policy = base64_encode(json_encode($arr));
- $signature = base64_encode(hash_hmac('sha1', $policy, $config['accessKeySecret'], true));
- $key = (new Upload())->getSavekey($config['savekey'], $name, $md5);
- $key = ltrim($key, "/");
- $response = array();
- $response['id'] = $config['accessKeyId'];
- $response['key'] = $key;
- $response['policy'] = $policy;
- $response['signature'] = $signature;
- $response['expire'] = $end;
- $response['callback'] = '';
- return $response;
- }
- public function check($signature, $policy)
- {
- $config = get_addon_config('alioss');
- $sign = base64_encode(hash_hmac('sha1', $policy, $config['accessKeySecret'], true));
- return $signature == $sign;
- }
- private function gmt_iso8601($time)
- {
- $dtStr = date("c", $time);
- $mydatetime = new \DateTime($dtStr);
- $expiration = $mydatetime->format(\DateTime::ISO8601);
- $pos = strpos($expiration, '+');
- $expiration = substr($expiration, 0, $pos);
- return $expiration . "Z";
- }
- }
|