123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- function isActionAllow($method, $pathname, $query, $headers)
- {
- $allow = true;
-
-
-
-
-
-
-
-
- if ($pathname === '/' && !($method === 'get' && isset($query['uploads']))) {
- $allow = false;
- }
-
- if ($pathname !== '/' && isset($query['acl'])) {
- $allow = false;
- }
-
- if ($method === 'delete' && $pathname !== '/') {
-
- }
- if ($method === 'put' && $pathname !== '/') {
-
- }
- if ($method === 'get' && $pathname !== '/') {
-
- }
- return $allow;
- }
- function getAuthorization($method, $pathname, $query, $headers)
- {
-
- $SecretId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
- $SecretKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
-
- !$query && ($query = array());
- !$headers && ($headers = array());
- $method = strtolower($method ? $method : 'get');
- $pathname = $pathname ? $pathname : '/';
- substr($pathname, 0, 1) != '/' && ($pathname = '/' . $pathname);
-
- if (!isActionAllow($method, $pathname, $query, $headers)) {
- return 'action deny';
- }
-
- function getObjectKeys($obj)
- {
- $list = array_keys($obj);
- sort($list);
- return $list;
- }
- function obj2str($obj)
- {
- $list = array();
- $keyList = getObjectKeys($obj);
- $len = count($keyList);
- for ($i = 0; $i < $len; $i++) {
- $key = $keyList[$i];
- $val = isset($obj[$key]) ? $obj[$key] : '';
- $key = strtolower($key);
- $list[] = rawurlencode($key) . '=' . rawurlencode($val);
- }
- return implode('&', $list);
- }
-
- $now = time() - 1;
- $expired = $now + 600;
-
- $qSignAlgorithm = 'sha1';
- $qAk = $SecretId;
- $qSignTime = $now . ';' . $expired;
- $qKeyTime = $now . ';' . $expired;
- $qHeaderList = strtolower(implode(';', getObjectKeys($headers)));
- $qUrlParamList = strtolower(implode(';', getObjectKeys($query)));
-
-
- $signKey = hash_hmac("sha1", $qKeyTime, $SecretKey);
-
- $formatString = implode("\n", array(strtolower($method), $pathname, obj2str($query), obj2str($headers), ''));
-
- $stringToSign = implode("\n", array('sha1', $qSignTime, sha1($formatString), ''));
-
- $qSignature = hash_hmac('sha1', $stringToSign, $signKey);
-
- $authorization = implode('&', array(
- 'q-sign-algorithm=' . $qSignAlgorithm,
- 'q-ak=' . $qAk,
- 'q-sign-time=' . $qSignTime,
- 'q-key-time=' . $qKeyTime,
- 'q-header-list=' . $qHeaderList,
- 'q-url-param-list=' . $qUrlParamList,
- 'q-signature=' . $qSignature
- ));
- return $authorization;
- }
- $inputBody = file_get_contents("php://input");
- if ($_SERVER['REQUEST_METHOD'] === 'POST' && $inputBody){
- $params = json_decode($inputBody, 1);
- $pathname = isset($params['pathname']) ? $params['pathname'] : '/';
- $method = isset($params['method']) ? $params['method'] : 'get';
- $query = isset($params['query']) ? $params['query'] : array();
- $headers = isset($params['headers']) ? $params['headers'] : array();
- } else {
- $pathname = isset($_GET['pathname']) ? $_GET['pathname'] : '/';
- $method = isset($_GET['method']) ? $_GET['method'] : 'get';
- $query = isset($_GET['query']) && $_GET['query'] ? json_decode($_GET['query'], 1) : array();
- $headers = isset($_GET['headers']) && $_GET['headers'] ? json_decode($_GET['headers'], 1) : array();
- }
- header('Content-Type: text/plain');
- header('Allow-Control-Allow-Origin: http://127.0.0.1');
- header('Allow-Control-Allow-Headers: origin,accept,content-type');
- $sign = getAuthorization($method, $pathname, $query, $headers);
- echo '{"sign":"' . $sign .'"}';
|