@@ -14,40 +14,40 @@ public function __construct($accessKey, $secretKey)
1414 $ this ->secretKey = $ secretKey ;
1515 }
1616
17- public function token ($ data )
17+ public function sign ($ data )
1818 {
1919 $ hmac = hash_hmac ('sha1 ' , $ data , $ this ->secretKey , true );
2020 return $ this ->accessKey . ': ' . \Qiniu \base64_urlSafeEncode ($ hmac );
2121 }
2222
23- public function tokenWithData ($ data )
23+ public function signWithData ($ data )
2424 {
2525 $ data = \Qiniu \base64_urlSafeEncode ($ data );
26- return $ this ->token ($ data ) . ': ' . $ data ;
26+ return $ this ->sign ($ data ) . ': ' . $ data ;
2727 }
2828
29- public function tokenOfRequest ($ urlString , $ body , $ contentType = null )
29+ public function signRequest ($ urlString , $ body , $ contentType = null )
3030 {
3131 $ url = parse_url ($ urlString );
3232 $ data = '' ;
33- if (isset ( $ url [ 'path ' ] )) {
33+ if (array_key_exists ( 'path ' , $ url )) {
3434 $ data = $ url ['path ' ];
3535 }
36- if (isset ( $ url [ 'query ' ] )) {
36+ if (array_key_exists ( 'query ' , $ url )) {
3737 $ data .= '? ' . $ url ['query ' ];
3838 }
3939 $ data .= "\n" ;
4040
41- if ($ body != null &&
42- ( $ contentType == 'application/x-www-form-urlencoded ' ) || $ contentType == 'application/json ' ) {
41+ if ($ body !== null &&
42+ in_array (( string ) $ contentType, array ( 'application/x-www-form-urlencoded ' , 'application/json ' ), true ) ) {
4343 $ data .= $ body ;
4444 }
45- return $ this ->token ($ data );
45+ return $ this ->sign ($ data );
4646 }
4747
4848 public function verifyCallback ($ contentType , $ originAuthorization , $ url , $ body )
4949 {
50- $ authorization = 'QBox ' . $ this ->tokenOfRequest ($ url , $ body , $ contentType );
50+ $ authorization = 'QBox ' . $ this ->signRequest ($ url , $ body , $ contentType );
5151 return $ originAuthorization === $ authorization ;
5252 }
5353
@@ -63,7 +63,7 @@ public function privateDownloadUrl($baseUrl, $expires = 3600)
6363 }
6464 $ baseUrl .= $ deadline ;
6565
66- $ token = $ this ->token ($ baseUrl );
66+ $ token = $ this ->sign ($ baseUrl );
6767 return "$ baseUrl&token= $ token " ;
6868 }
6969
@@ -76,15 +76,21 @@ public function uploadToken(
7676 ) {
7777 $ deadline = time () + $ expires ;
7878 $ scope = $ bucket ;
79- if ($ key != null ) {
79+ if ($ key !== null ) {
8080 $ scope .= ': ' . $ key ;
8181 }
82- $ args = array ('scope ' => $ scope , 'deadline ' => $ deadline );
83- self ::copyPolicy ($ args , $ policy , $ strictPolicy );
82+ $ args = array ();
83+ $ args = self ::copyPolicy ($ args , $ policy , $ strictPolicy );
84+ $ args ['scope ' ] = $ scope ;
85+ $ args ['deadline ' ] = $ deadline ;
8486 $ b = json_encode ($ args );
85- return $ this ->tokenWithData ($ b );
87+ return $ this ->signWithData ($ b );
8688 }
8789
90+ /**
91+ *上传策略,参数规格详见
92+ *http://developer.qiniu.com/docs/v6/api/reference/security/put-policy.html
93+ */
8894 private static $ policyFields = array (
8995 'callbackUrl ' ,
9096 'callbackBody ' ,
@@ -112,24 +118,25 @@ public function uploadToken(
112118 'asyncOps ' ,
113119 );
114120
115- private static function copyPolicy ($ policy , $ originPolicy , $ strictPolicy )
121+ private static function copyPolicy (& $ policy , $ originPolicy , $ strictPolicy )
116122 {
117- if ($ originPolicy == null ) {
118- return ;
123+ if ($ originPolicy === null ) {
124+ return array () ;
119125 }
120126 foreach ($ originPolicy as $ key => $ value ) {
121- if (in_array ($ key , self ::$ deprecatedPolicyFields )) {
127+ if (in_array (( string ) $ key , self ::$ deprecatedPolicyFields, true )) {
122128 throw new \InvalidArgumentException ("{$ key } has deprecated " );
123129 }
124- if (!$ strictPolicy || in_array ($ key , self ::$ policyFields )) {
130+ if (!$ strictPolicy || in_array (( string ) $ key , self ::$ policyFields, true )) {
125131 $ policy [$ key ] = $ value ;
126132 }
127133 }
134+ return $ policy ;
128135 }
129136
130137 public function authorization ($ url , $ body = null , $ contentType = null )
131138 {
132- $ authorization = 'QBox ' . $ this ->tokenOfRequest ($ url , $ body , $ contentType );
139+ $ authorization = 'QBox ' . $ this ->signRequest ($ url , $ body , $ contentType );
133140 return array ('Authorization ' => $ authorization );
134141 }
135142}
0 commit comments