@@ -104,10 +104,12 @@ discard block |
||
104 | 104 | ); |
105 | 105 | $client->putObjectByContent($args); |
106 | 106 | |
107 | - if($client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy"))) |
|
108 | - $client->deleteObject(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy")); |
|
109 | - if($client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy.instruction"))) |
|
110 | - $client->deleteObject(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy.instruction")); |
|
107 | + if($client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy"))) { |
|
108 | + $client->deleteObject(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy")); |
|
109 | + } |
|
110 | + if($client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy.instruction"))) { |
|
111 | + $client->deleteObject(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy.instruction")); |
|
112 | + } |
|
111 | 113 | |
112 | 114 | $copyReq = array( |
113 | 115 | "Bucket"=>$bucket, |
@@ -119,18 +121,22 @@ discard block |
||
119 | 121 | ); |
120 | 122 | $client->copyObject($copyReq); |
121 | 123 | |
122 | - if(!$client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy"))) |
|
123 | - throw new Exception("not found ".$keyprefix."EOFileCopy"); |
|
124 | - if(!$client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy.instruction"))) |
|
125 | - throw new Exception("not found ".$keyprefix."EOFileCopy.instruction"); |
|
124 | + if(!$client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy"))) { |
|
125 | + throw new Exception("not found ".$keyprefix."EOFileCopy"); |
|
126 | + } |
|
127 | + if(!$client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy.instruction"))) { |
|
128 | + throw new Exception("not found ".$keyprefix."EOFileCopy.instruction"); |
|
129 | + } |
|
126 | 130 | |
127 | 131 | $client->deleteObject(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy")); |
128 | 132 | |
129 | - if($client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy"))) |
|
130 | - throw new Exception("found ".$keyprefix."EOFileCopy"); |
|
131 | - if($client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy.instruction"))) |
|
132 | - throw new Exception("found ".$keyprefix."EOFileCopy.instruction"); |
|
133 | -} |
|
133 | + if($client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy"))) { |
|
134 | + throw new Exception("found ".$keyprefix."EOFileCopy"); |
|
135 | + } |
|
136 | + if($client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy.instruction"))) { |
|
137 | + throw new Exception("found ".$keyprefix."EOFileCopy.instruction"); |
|
138 | + } |
|
139 | + } |
|
134 | 140 | function rangeGetAndCheckMd5($client,$bucket,$key,$file,$expectedMd5){ |
135 | 141 | $args = array("Bucket"=>$bucket,"Key"=>$key); |
136 | 142 | $meta = $client->getObjectMeta($args); |
@@ -164,7 +170,8 @@ discard block |
||
164 | 170 | @unlink($value); |
165 | 171 | } |
166 | 172 | $md5 = base64_encode(md5_file($file)); |
167 | - if($md5 != $expectedMd5) |
|
168 | - throw new Exception("file md5 check error expected ".$expectedMd5." ,actual ".$md5, 1); |
|
173 | + if($md5 != $expectedMd5) { |
|
174 | + throw new Exception("file md5 check error expected ".$expectedMd5." ,actual ".$md5, 1); |
|
175 | + } |
|
169 | 176 | @unlink($file); |
170 | 177 | } |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | public static function getKeyEncryptionAlgm($encryptionMaterials){ |
40 | 40 | if(is_array($encryptionMaterials)){ |
41 | 41 | return "RSA"; |
42 | - }else{ |
|
42 | + } else{ |
|
43 | 43 | return "AES"; |
44 | 44 | } |
45 | 45 | } |
@@ -47,8 +47,9 @@ discard block |
||
47 | 47 | $a = $range["start"]; |
48 | 48 | $b = $range["end"]; |
49 | 49 | $a = $a - ($a%$blocksize)-$blocksize; |
50 | - if($a < 0) |
|
51 | - $a = 0; |
|
50 | + if($a < 0) { |
|
51 | + $a = 0; |
|
52 | + } |
|
52 | 53 | |
53 | 54 | $b = $b+$blocksize-$b%$blocksize+$blocksize; |
54 | 55 | |
@@ -59,13 +60,15 @@ discard block |
||
59 | 60 | if($encrypKeyAlg === "AES"){ |
60 | 61 | $secretKey = $encryptionMaterials; |
61 | 62 | $encryptedSek = EncryptionUtil::encode_AES_ECB($cek,$secretKey); |
62 | - if(empty($encryptedSek)) |
|
63 | - throw new Ks3ClientException("can not encode cek useing AES"); |
|
64 | - }else if($encrypKeyAlg === "RSA"){ |
|
63 | + if(empty($encryptedSek)) { |
|
64 | + throw new Ks3ClientException("can not encode cek useing AES"); |
|
65 | + } |
|
66 | + } else if($encrypKeyAlg === "RSA"){ |
|
65 | 67 | $encryptedSek = ""; |
66 | 68 | openssl_public_encrypt($cek,$encryptedSek, $encryptionMaterials[0]); |
67 | - if(empty($encryptedSek)) |
|
68 | - throw new Ks3ClientException("can not encode cek useing RSA"); |
|
69 | + if(empty($encryptedSek)) { |
|
70 | + throw new Ks3ClientException("can not encode cek useing RSA"); |
|
71 | + } |
|
69 | 72 | } |
70 | 73 | return $encryptedSek; |
71 | 74 | } |
@@ -74,13 +77,15 @@ discard block |
||
74 | 77 | if($encrypKeyAlg === "AES"){ |
75 | 78 | $secretKey = $encryptionMaterials; |
76 | 79 | $cek = EncryptionUtil::decode_AES_ECB($cekEncrypted,$secretKey); |
77 | - if(empty($cek)) |
|
78 | - throw new Ks3ClientException("can not decode cek useing AES,secret key maybe not correct"); |
|
79 | - }else if($encrypKeyAlg === "RSA"){ |
|
80 | + if(empty($cek)) { |
|
81 | + throw new Ks3ClientException("can not decode cek useing AES,secret key maybe not correct"); |
|
82 | + } |
|
83 | + } else if($encrypKeyAlg === "RSA"){ |
|
80 | 84 | $cek = ""; |
81 | 85 | openssl_private_decrypt($cekEncrypted,$cek, $encryptionMaterials[1]); |
82 | - if(empty($cek)) |
|
83 | - throw new Ks3ClientException("can not decode cek useing RSA,public/private key pair maybe not correct"); |
|
86 | + if(empty($cek)) { |
|
87 | + throw new Ks3ClientException("can not decode cek useing RSA,public/private key pair maybe not correct"); |
|
88 | + } |
|
84 | 89 | } |
85 | 90 | return $cek; |
86 | 91 | } |
@@ -97,39 +102,42 @@ discard block |
||
97 | 102 | return $data; |
98 | 103 | } |
99 | 104 | public static function updateContentMD5Header($req){ |
100 | - if(!is_array($req)) |
|
101 | - return $req; |
|
105 | + if(!is_array($req)) { |
|
106 | + return $req; |
|
107 | + } |
|
102 | 108 | if(isset($req["ObjectMeta"])){ |
103 | 109 | $meta = $req["ObjectMeta"]; |
104 | - }else{ |
|
110 | + } else{ |
|
105 | 111 | return $req; |
106 | 112 | } |
107 | 113 | if(is_array($meta) && isset($meta["Content-MD5"])){ |
108 | 114 | $md5 = $meta["Content-MD5"]; |
109 | - }else{ |
|
115 | + } else{ |
|
110 | 116 | return $req; |
111 | 117 | } |
112 | 118 | if(empty($md5)){ |
113 | 119 | return $req; |
114 | - }else{ |
|
120 | + } else{ |
|
115 | 121 | $req["ObjectMeta"]["Content-MD5"] = NULL; |
116 | 122 | $req["UserMeta"]["x-kss-meta-x-kss-unencrypted-content-md5"] = $md5; |
117 | 123 | } |
118 | 124 | return $req; |
119 | 125 | } |
120 | 126 | public static function metaTextLength($req){ |
121 | - if(!is_array($req)) |
|
122 | - return -1; |
|
127 | + if(!is_array($req)) { |
|
128 | + return -1; |
|
129 | + } |
|
123 | 130 | if(isset($req["ObjectMeta"])){ |
124 | 131 | $meta = $req["ObjectMeta"]; |
125 | - }else{ |
|
132 | + } else{ |
|
126 | 133 | return -1; |
127 | 134 | } |
128 | 135 | if(is_array($meta) && isset($meta["Content-Length"])){ |
129 | 136 | $length = $meta["Content-Length"]; |
130 | 137 | return $length; |
131 | - }else |
|
132 | - return -1; |
|
138 | + } else { |
|
139 | + return -1; |
|
140 | + } |
|
133 | 141 | } |
134 | 142 | public static function plainTextLength($args){ |
135 | 143 | if(isset($args["Content"])){ |
@@ -146,11 +154,12 @@ discard block |
||
146 | 154 | if(Utils::chk_chinese($content)&&!Utils::check_char($content)){ |
147 | 155 | $content = iconv('utf-8','gbk',$content); |
148 | 156 | } |
149 | - if(!file_exists($content)) |
|
150 | - throw new Ks3ClientException("the specified file does not exist "); |
|
157 | + if(!file_exists($content)) { |
|
158 | + throw new Ks3ClientException("the specified file does not exist "); |
|
159 | + } |
|
151 | 160 | $length = Utils::getFileSize($content); |
152 | 161 | $content = fopen($content,"r"); |
153 | - }else{ |
|
162 | + } else{ |
|
154 | 163 | $stats = fstat($content); |
155 | 164 | if ($stats && $stats["size"] >= 0){ |
156 | 165 | $length = $stats["size"]; |
@@ -160,10 +169,11 @@ discard block |
||
160 | 169 | //优先取用户设置seek_position,没有的话取ftell |
161 | 170 | if(isset($args["Content"]["seek_position"])&&$args["Content"]["seek_position"]>0){ |
162 | 171 | $seek_position = $args["Content"]["seek_position"]; |
163 | - }else if(!$isFile){ |
|
172 | + } else if(!$isFile){ |
|
164 | 173 | $seek_position = ftell($content); |
165 | - if($seek_position<0) |
|
166 | - $seek_position = 0; |
|
174 | + if($seek_position<0) { |
|
175 | + $seek_position = 0; |
|
176 | + } |
|
167 | 177 | fseek($content,0); |
168 | 178 | } |
169 | 179 | |
@@ -173,21 +183,23 @@ discard block |
||
173 | 183 | } |
174 | 184 | if($lengthInMeta > 0){ |
175 | 185 | $length = $lengthInMeta; |
176 | - }else if($resourceLength > 0){ |
|
186 | + } else if($resourceLength > 0){ |
|
177 | 187 | //根据seek_position计算实际长度 |
178 | 188 | $length = $resourceLength - $seek_position; |
179 | 189 | } |
180 | - if($length <= 0) |
|
181 | - throw new Ks3ClientException("calculate content length failed,unexpected contetn length ".$length); |
|
190 | + if($length <= 0) { |
|
191 | + throw new Ks3ClientException("calculate content length failed,unexpected contetn length ".$length); |
|
192 | + } |
|
182 | 193 | return $length; |
183 | - }else{ |
|
194 | + } else{ |
|
184 | 195 | $content = $args["Content"]; |
185 | 196 | $lengthInMeta = EncryptionUtil::metaTextLength($args); |
186 | 197 | $length = strlen($content); |
187 | - if($length<$lengthInMeta||$lengthInMeta <= 0) |
|
188 | - return $length; |
|
189 | - else |
|
190 | - return $lengthInMeta; |
|
198 | + if($length<$lengthInMeta||$lengthInMeta <= 0) { |
|
199 | + return $length; |
|
200 | + } else { |
|
201 | + return $lengthInMeta; |
|
202 | + } |
|
191 | 203 | } |
192 | 204 | } |
193 | 205 | return -1; |
@@ -195,10 +207,12 @@ discard block |
||
195 | 207 | public static function initMultipartUploadContext($initResult,$iv,$cek,$encryptedCek,$matdesc="{}"){ |
196 | 208 | $cacheDir = KS3_API_PATH.DIRECTORY_SEPARATOR."cache".DIRECTORY_SEPARATOR; |
197 | 209 | $encryptionDir = KS3_API_PATH.DIRECTORY_SEPARATOR."cache".DIRECTORY_SEPARATOR."encryption".DIRECTORY_SEPARATOR; |
198 | - if(!is_dir($cacheDir)) |
|
199 | - mkdir($cacheDir); |
|
200 | - if(!is_dir($encryptionDir)) |
|
201 | - mkdir($encryptionDir); |
|
210 | + if(!is_dir($cacheDir)) { |
|
211 | + mkdir($cacheDir); |
|
212 | + } |
|
213 | + if(!is_dir($encryptionDir)) { |
|
214 | + mkdir($encryptionDir); |
|
215 | + } |
|
202 | 216 | if(is_array($matdesc)){ |
203 | 217 | $matdesc = json_encode($matdesc); |
204 | 218 | } |
@@ -225,8 +239,9 @@ discard block |
||
225 | 239 | } |
226 | 240 | public static function getMultipartUploadContext($UploadId){ |
227 | 241 | $encryptionDir = KS3_API_PATH.DIRECTORY_SEPARATOR."cache".DIRECTORY_SEPARATOR."encryption".DIRECTORY_SEPARATOR; |
228 | - if(!EncryptionUtil::multipartUploadContextExists($UploadId)) |
|
229 | - throw new Ks3ClientException("can not found multipart upload context in cache dir"); |
|
242 | + if(!EncryptionUtil::multipartUploadContextExists($UploadId)) { |
|
243 | + throw new Ks3ClientException("can not found multipart upload context in cache dir"); |
|
244 | + } |
|
230 | 245 | $jsonString = file_get_contents($encryptionDir.$UploadId); |
231 | 246 | $arry = json_decode($jsonString,TRUE); |
232 | 247 | return $arry; |
@@ -241,10 +256,11 @@ discard block |
||
241 | 256 | } |
242 | 257 | public static function openfile($path,$mode){ |
243 | 258 | $file = fopen($path, $mode); |
244 | - if($file) |
|
245 | - return $file; |
|
246 | - else |
|
247 | - throw new Ks3ClientException("open file ".$path." error"); |
|
259 | + if($file) { |
|
260 | + return $file; |
|
261 | + } else { |
|
262 | + throw new Ks3ClientException("open file ".$path." error"); |
|
263 | + } |
|
248 | 264 | } |
249 | 265 | //matdesc为字符串或array数据类型。 |
250 | 266 | public static function createInstructionFile($bucket,$key,$cek,$iv,$matdesc="{}"){ |
@@ -271,8 +287,9 @@ discard block |
||
271 | 287 | } |
272 | 288 | public static function isInstructionFile($s3Object){ |
273 | 289 | $meta = $s3Object["Meta"]; |
274 | - if(isset($meta["UserMeta"]["x-kss-meta-x-kss-crypto-instr-file"])) |
|
275 | - return TRUE; |
|
290 | + if(isset($meta["UserMeta"]["x-kss-meta-x-kss-crypto-instr-file"])) { |
|
291 | + return TRUE; |
|
292 | + } |
|
276 | 293 | return FALSE; |
277 | 294 | } |
278 | 295 | } |
@@ -22,17 +22,19 @@ discard block |
||
22 | 22 | $sek = EncryptionUtil::genereateOnceUsedKey(); |
23 | 23 | $encryptedSek = EncryptionUtil::encodeCek($this->encryptionMaterials,$sek); |
24 | 24 | $content = $args["Content"]; |
25 | - if(empty($content)) |
|
26 | - throw new Ks3ClientException("please specifie Content in request args"); |
|
25 | + if(empty($content)) { |
|
26 | + throw new Ks3ClientException("please specifie Content in request args"); |
|
27 | + } |
|
27 | 28 | $metaContentLength = EncryptionUtil::metaTextLength($args); |
28 | 29 | $plainTextLength = strlen($content); |
29 | 30 | if($metaContentLength > 0 && $metaContentLength < $plainTextLength){ |
30 | 31 | $plainTextLength = $metaContentLength; |
31 | 32 | } |
32 | - if($plainTextLength > 0) |
|
33 | - $args["UserMeta"]["x-kss-meta-x-kss-unencrypted-content-length"] = $plainTextLength; |
|
34 | - else |
|
35 | - throw new Ks3ClientException("unexpected content length ".$plainTextLength); |
|
33 | + if($plainTextLength > 0) { |
|
34 | + $args["UserMeta"]["x-kss-meta-x-kss-unencrypted-content-length"] = $plainTextLength; |
|
35 | + } else { |
|
36 | + throw new Ks3ClientException("unexpected content length ".$plainTextLength); |
|
37 | + } |
|
36 | 38 | |
37 | 39 | $content = substr($content, 0,$plainTextLength); |
38 | 40 | |
@@ -73,8 +75,9 @@ discard block |
||
73 | 75 | $encryptedSek = EncryptionUtil::encodeCek($this->encryptionMaterials,$sek); |
74 | 76 | if(!isset($args["Content"])||!is_array($args["Content"]) |
75 | 77 | ||!isset($args["Content"]["content"]) |
76 | - ||empty($args["Content"]["content"])) |
|
77 | - throw new Ks3ClientException("please specifie file content in request args"); |
|
78 | + ||empty($args["Content"]["content"])) { |
|
79 | + throw new Ks3ClientException("please specifie file content in request args"); |
|
80 | + } |
|
78 | 81 | $content = $args["Content"]; |
79 | 82 | $plainTextLength = EncryptionUtil::plainTextLength($args); |
80 | 83 | if($plainTextLength <= 0){ |
@@ -117,7 +120,7 @@ discard block |
||
117 | 120 | $meta = $this->ks3client->getObjectMeta($args); |
118 | 121 | if(isset($meta["UserMeta"]["x-kss-meta-x-kss-key"])&&isset($meta["UserMeta"]["x-kss-meta-x-kss-iv"])){ |
119 | 122 | $encryptedInMeta = TRUE; |
120 | - }else{ |
|
123 | + } else{ |
|
121 | 124 | $encryptedInMeta = FALSE; |
122 | 125 | } |
123 | 126 | $encrypted = TRUE; |
@@ -128,7 +131,7 @@ discard block |
||
128 | 131 | $encryptionInfo["matdesc"] = $matdesc; |
129 | 132 | $cekEncrypted = base64_decode($meta["UserMeta"]["x-kss-meta-x-kss-key"]); |
130 | 133 | $encryptionInfo["cek"] = $cek = EncryptionUtil::decodeCek($this->encryptionMaterials,$cekEncrypted); |
131 | - }else{ |
|
134 | + } else{ |
|
132 | 135 | if($this->ks3client->objectExists(array( |
133 | 136 | "Bucket"=>$args["Bucket"], |
134 | 137 | "Key"=>$args["Key"].EncryptionUtil::$INSTRUCTION_SUFFIX) |
@@ -140,8 +143,9 @@ discard block |
||
140 | 143 | "Key"=>$insKey, |
141 | 144 | ); |
142 | 145 | $s3Object = $this->ks3client->getObject($getIns); |
143 | - if(!EncryptionUtil::isInstructionFile($s3Object)) |
|
144 | - throw new Ks3ClientException($insKey." is not an InstructionFile"); |
|
146 | + if(!EncryptionUtil::isInstructionFile($s3Object)) { |
|
147 | + throw new Ks3ClientException($insKey." is not an InstructionFile"); |
|
148 | + } |
|
145 | 149 | |
146 | 150 | $content = $s3Object["Content"]; |
147 | 151 | $content = json_decode($content,TRUE); |
@@ -150,7 +154,7 @@ discard block |
||
150 | 154 | $encryptionInfo["matdesc"] = $matdesc; |
151 | 155 | $cekEncrypted = base64_decode($content["x-kss-key"]); |
152 | 156 | $encryptionInfo["cek"] = $cek = EncryptionUtil::decodeCek($this->encryptionMaterials,$cekEncrypted); |
153 | - }else{ |
|
157 | + } else{ |
|
154 | 158 | $encrypted =FALSE; |
155 | 159 | } |
156 | 160 | } |
@@ -161,10 +165,12 @@ discard block |
||
161 | 165 | $iv = $encryptionInfo["iv"]; |
162 | 166 | $cek = $encryptionInfo["cek"]; |
163 | 167 | |
164 | - if(empty($iv)) |
|
165 | - throw new Ks3ClientException("can not find iv in UserMeta or InstructionFile"); |
|
166 | - if(empty($cek)) |
|
167 | - throw new Ks3ClientException("can not find cek in UserMeta or InstructionFile"); |
|
168 | + if(empty($iv)) { |
|
169 | + throw new Ks3ClientException("can not find iv in UserMeta or InstructionFile"); |
|
170 | + } |
|
171 | + if(empty($cek)) { |
|
172 | + throw new Ks3ClientException("can not find cek in UserMeta or InstructionFile"); |
|
173 | + } |
|
168 | 174 | |
169 | 175 | if(isset($args["Range"])){ |
170 | 176 | $range = $args["Range"]; |
@@ -177,10 +183,10 @@ discard block |
||
177 | 183 | throw new Ks3ClientException("Invalid range ".$range); |
178 | 184 | } |
179 | 185 | $range = array("start"=>$a,"end"=>$b); |
180 | - }else{ |
|
186 | + } else{ |
|
181 | 187 | throw new Ks3ClientException("Invalid range ".$range); |
182 | 188 | } |
183 | - }else{ |
|
189 | + } else{ |
|
184 | 190 | if(!isset($range["start"])||!isset($range["end"])){ |
185 | 191 | throw new Ks3ClientException("Invalid range ".serialize($range)); |
186 | 192 | } |
@@ -206,7 +212,7 @@ discard block |
||
206 | 212 | } |
207 | 213 | $args["writeCallBack"] = $writeCallBack; |
208 | 214 | return $this->ks3client->getObject($args); |
209 | - }else{ |
|
215 | + } else{ |
|
210 | 216 | $offset = 0; |
211 | 217 | $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC); |
212 | 218 | if(isset($range)){ |
@@ -229,7 +235,7 @@ discard block |
||
229 | 235 | $decoded = mdecrypt_generic($td,$content); |
230 | 236 | mcrypt_generic_deinit($td); |
231 | 237 | mcrypt_module_close($td); |
232 | - }else{ |
|
238 | + } else{ |
|
233 | 239 | $decoded = ""; |
234 | 240 | } |
235 | 241 | |
@@ -263,15 +269,17 @@ discard block |
||
263 | 269 | if($needRemovePad){ |
264 | 270 | $padOffset = $pad-$endOffset; |
265 | 271 | $actualWriteCount = strlen($decoded)-$padOffset; |
266 | - if($actualWriteCount <= 0)//负数的情况就是用户期望的range里全是填充的 |
|
272 | + if($actualWriteCount <= 0) { |
|
273 | + //负数的情况就是用户期望的range里全是填充的 |
|
267 | 274 | $decoded = ""; |
268 | - else |
|
269 | - $decoded = substr($decoded,0,strlen($decoded)-$padOffset); |
|
275 | + } else { |
|
276 | + $decoded = substr($decoded,0,strlen($decoded)-$padOffset); |
|
277 | + } |
|
270 | 278 | } |
271 | 279 | $s3Object["Content"] = $decoded; |
272 | 280 | return $s3Object; |
273 | 281 | } |
274 | - }else{ |
|
282 | + } else{ |
|
275 | 283 | return $this->ks3client->getObject($args); |
276 | 284 | } |
277 | 285 | } |
@@ -298,8 +306,9 @@ discard block |
||
298 | 306 | $uploadId = $args["Options"]["uploadId"]; |
299 | 307 | $isLastPart = FALSE; |
300 | 308 | $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC); |
301 | - if(isset($args["LastPart"])) |
|
302 | - $isLastPart = $args["LastPart"]; |
|
309 | + if(isset($args["LastPart"])) { |
|
310 | + $isLastPart = $args["LastPart"]; |
|
311 | + } |
|
303 | 312 | $exists = EncryptionUtil::multipartUploadContextExists($uploadId); |
304 | 313 | if(!$exists){ |
305 | 314 | throw new Ks3ClientException("no such upload in cache/encryption/"); |
@@ -313,9 +322,10 @@ discard block |
||
313 | 322 | throw new Ks3ClientException("get content length failed ,unexpected content length ".$plainTextLength); |
314 | 323 | } |
315 | 324 | if(!$isLastPart){ |
316 | - if($plainTextLength % $blocksize != 0) |
|
317 | - throw new Ks3ClientException("Invalid part size,part size (".$plainTextLength.") must be multiples of the block size ".$blocksize); |
|
318 | - }else{ |
|
325 | + if($plainTextLength % $blocksize != 0) { |
|
326 | + throw new Ks3ClientException("Invalid part size,part size (".$plainTextLength.") must be multiples of the block size ".$blocksize); |
|
327 | + } |
|
328 | + } else{ |
|
319 | 329 | $args["ObjectMeta"]["Content-Length"] = $plainTextLength + ($blocksize - $plainTextLength%$blocksize); |
320 | 330 | } |
321 | 331 | $readCallBack = new AESCBCStreamReadCallBack(); |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | if(isset($this->$property_name)) |
21 | 21 | { |
22 | 22 | return($this->$property_name); |
23 | - }else |
|
23 | + } else |
|
24 | 24 | { |
25 | 25 | return(NULL); |
26 | 26 | } |
@@ -33,9 +33,9 @@ discard block |
||
33 | 33 | //不能把上次的没读完的长度算在这次里,应该算在上次 |
34 | 34 | $written_total = 0-strlen($this->buffer); |
35 | 35 | $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC); |
36 | - if($length<$blocksize) |
|
37 | - $this->buffer = $data; |
|
38 | - else{ |
|
36 | + if($length<$blocksize) { |
|
37 | + $this->buffer = $data; |
|
38 | + } else{ |
|
39 | 39 | //如果期望的范围之后还有数据,则认为数据已经接收完毕。不做任何处理 |
40 | 40 | if($this->expectedRange["end"] < $this->expectedRange["start"]){ |
41 | 41 | return $written_total+strlen($data); |
@@ -53,16 +53,17 @@ discard block |
||
53 | 53 | $ivoffset = $blocksize; |
54 | 54 | } |
55 | 55 | //初始化当前位置 |
56 | - if(isset($this->adjustedRange)) |
|
57 | - $this->currentIndex = $ivoffset+$this->adjustedRange["start"]; |
|
58 | - else |
|
59 | - $this->currentIndex = $ivoffset; |
|
56 | + if(isset($this->adjustedRange)) { |
|
57 | + $this->currentIndex = $ivoffset+$this->adjustedRange["start"]; |
|
58 | + } else { |
|
59 | + $this->currentIndex = $ivoffset; |
|
60 | + } |
|
60 | 61 | } |
61 | 62 | $written_total+=$ivoffset; |
62 | 63 | if(strlen($data) == 0){ |
63 | 64 | $decoded = ""; |
64 | 65 | return $written_total; |
65 | - }else{ |
|
66 | + } else{ |
|
66 | 67 | $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,''); |
67 | 68 | mcrypt_generic_init($td,$this->cek,$this->iv); |
68 | 69 | $decoded = mdecrypt_generic($td,$data); |
@@ -95,7 +96,7 @@ discard block |
||
95 | 96 | $preLength = strlen($decoded); |
96 | 97 | $decoded = substr($decoded, 0,$expectedEnd-$this->currentIndex+1); |
97 | 98 | $endOffset = $preLength-strlen($decoded); |
98 | - }else{ |
|
99 | + } else{ |
|
99 | 100 | //因为range是开始结束都计算的,range=1-2。currentIndex=1,长度是2,end=currentIndex+2-1 |
100 | 101 | $trueEnd = $this->currentIndex+strlen($decoded)-1; |
101 | 102 | } |
@@ -117,14 +118,17 @@ discard block |
||
117 | 118 | if($needRemovePad){ |
118 | 119 | $padOffset = $pad-$endOffset; |
119 | 120 | $actualWriteCount = strlen($decoded)-$padOffset; |
120 | - if($actualWriteCount <= 0)//负数的情况就是用户期望的range里全是填充的 |
|
121 | + if($actualWriteCount <= 0) { |
|
122 | + //负数的情况就是用户期望的range里全是填充的 |
|
121 | 123 | $decoded = ""; |
122 | - else |
|
123 | - $decoded = substr($decoded,0,strlen($decoded)-$padOffset); |
|
124 | + } else { |
|
125 | + $decoded = substr($decoded,0,strlen($decoded)-$padOffset); |
|
126 | + } |
|
124 | 127 | } |
125 | 128 | $count = fwrite($write_stream, $decoded); |
126 | - if($count == 0) |
|
127 | - $count = $actualWriteCount; |
|
129 | + if($count == 0) { |
|
130 | + $count = $actualWriteCount; |
|
131 | + } |
|
128 | 132 | $count += $padOffset; |
129 | 133 | $count += $startOffset; |
130 | 134 | $count += $endOffset; |
@@ -138,12 +142,14 @@ discard block |
||
138 | 142 | //是的话则使用初始化IV |
139 | 143 | private function isBegin(){ |
140 | 144 | $beginIndex = 0; |
141 | - if(isset($this->adjustedRange["start"])) |
|
142 | - $beginIndex = $this->adjustedRange["start"]; |
|
143 | - if($beginIndex == 0) |
|
144 | - return TRUE; |
|
145 | - else |
|
146 | - return FALSE; |
|
145 | + if(isset($this->adjustedRange["start"])) { |
|
146 | + $beginIndex = $this->adjustedRange["start"]; |
|
147 | + } |
|
148 | + if($beginIndex == 0) { |
|
149 | + return TRUE; |
|
150 | + } else { |
|
151 | + return FALSE; |
|
152 | + } |
|
147 | 153 | } |
148 | 154 | } |
149 | 155 | //上传 |
@@ -162,7 +168,7 @@ discard block |
||
162 | 168 | if(isset($this->$property_name)) |
163 | 169 | { |
164 | 170 | return($this->$property_name); |
165 | - }else |
|
171 | + } else |
|
166 | 172 | { |
167 | 173 | return(NULL); |
168 | 174 | } |
@@ -199,16 +205,16 @@ discard block |
||
199 | 205 | if(!$isLast){ |
200 | 206 | $this->buffer = substr($data,$dataLength-$dataLength%$blocksize); |
201 | 207 | $data = substr($data, 0,$dataLength-$dataLength%$blocksize); |
202 | - }else{ |
|
208 | + } else{ |
|
203 | 209 | //分块上传除最后一块外肯定是blocksize大小的倍数,所以不需要填充。 |
204 | 210 | if($this->mutipartUpload){ |
205 | 211 | if($this->isLastPart){ |
206 | 212 | $this->buffer = NULL; |
207 | 213 | $data = EncryptionUtil::PKCS5Padding($data,$blocksize); |
208 | - }else{ |
|
214 | + } else{ |
|
209 | 215 | //donothing |
210 | 216 | } |
211 | - }else{ |
|
217 | + } else{ |
|
212 | 218 | $this->buffer = NULL; |
213 | 219 | $data = EncryptionUtil::PKCS5Padding($data,$blocksize); |
214 | 220 | } |
@@ -4,26 +4,33 @@ discard block |
||
4 | 4 | date_default_timezone_set('Asia/Shanghai'); |
5 | 5 | |
6 | 6 | //检测API路径 |
7 | -if(!defined("KS3_API_PATH")) |
|
8 | -define("KS3_API_PATH", dirname(__FILE__)); |
|
7 | +if(!defined("KS3_API_PATH")) { |
|
8 | + define("KS3_API_PATH", dirname(__FILE__)); |
|
9 | +} |
|
9 | 10 | //是否使用VHOST |
10 | -if(!defined("KS3_API_VHOST")) |
|
11 | -define("KS3_API_VHOST",TRUE); |
|
11 | +if(!defined("KS3_API_VHOST")) { |
|
12 | + define("KS3_API_VHOST",TRUE); |
|
13 | +} |
|
12 | 14 | //是否开启日志(写入日志文件) |
13 | -if(!defined("KS3_API_LOG")) |
|
14 | -define("KS3_API_LOG",TRUE); |
|
15 | +if(!defined("KS3_API_LOG")) { |
|
16 | + define("KS3_API_LOG",TRUE); |
|
17 | +} |
|
15 | 18 | //是否显示日志(直接输出日志) |
16 | -if(!defined("KS3_API_DISPLAY_LOG")) |
|
17 | -define("KS3_API_DISPLAY_LOG", TRUE); |
|
19 | +if(!defined("KS3_API_DISPLAY_LOG")) { |
|
20 | + define("KS3_API_DISPLAY_LOG", TRUE); |
|
21 | +} |
|
18 | 22 | //定义日志目录(默认是该项目log下) |
19 | -if(!defined("KS3_API_LOG_PATH")) |
|
20 | -define("KS3_API_LOG_PATH",""); |
|
23 | +if(!defined("KS3_API_LOG_PATH")) { |
|
24 | + define("KS3_API_LOG_PATH",""); |
|
25 | +} |
|
21 | 26 | //是否使用HTTPS |
22 | -if(!defined("KS3_API_USE_HTTPS")) |
|
23 | -define("KS3_API_USE_HTTPS",FALSE); |
|
27 | +if(!defined("KS3_API_USE_HTTPS")) { |
|
28 | + define("KS3_API_USE_HTTPS",FALSE); |
|
29 | +} |
|
24 | 30 | //是否开启curl debug模式 |
25 | -if(!defined("KS3_API_DEBUG_MODE")) |
|
26 | -define("KS3_API_DEBUG_MODE",FALSE); |
|
31 | +if(!defined("KS3_API_DEBUG_MODE")) { |
|
32 | + define("KS3_API_DEBUG_MODE",FALSE); |
|
33 | +} |
|
27 | 34 | define("KS3_API_Author","[email protected]"); |
28 | 35 | define("KS3_API_Version","1.2"); |
29 | 36 | |
@@ -49,10 +56,10 @@ discard block |
||
49 | 56 | throw new Ks3ClientException("please install mbstring extension"); |
50 | 57 | |
51 | 58 | } |
52 | - }else{ |
|
59 | + } else{ |
|
53 | 60 | throw new Ks3ClientException("please install extensions"); |
54 | 61 | } |
55 | -}else{ |
|
62 | +} else{ |
|
56 | 63 | throw new Ks3ClientException(); |
57 | 64 | } |
58 | 65 | |
@@ -116,14 +123,15 @@ discard block |
||
116 | 123 | $ex = NULL; |
117 | 124 | try{ |
118 | 125 | $result = $this->invoke($method,$args,$holder); |
119 | - }catch(Exception $e){ |
|
126 | + } catch(Exception $e){ |
|
120 | 127 | $holder->msg.=$e."\r\n"; |
121 | 128 | $ex = $e; |
122 | 129 | } |
123 | 130 | $holder->msg .= "------------------Logging End-------------------------\r\n"; |
124 | 131 | $this->log->info($holder->msg); |
125 | - if($ex !=NULL) |
|
126 | - throw $ex; |
|
132 | + if($ex !=NULL) { |
|
133 | + throw $ex; |
|
134 | + } |
|
127 | 135 | return $result; |
128 | 136 | } |
129 | 137 | private function invoke($method,$args=array(),$holder,$location=NULL){ |
@@ -145,7 +153,7 @@ discard block |
||
145 | 153 | if($api["needBucket"]){ |
146 | 154 | throw new Ks3ClientException($method." this api need bucket"); |
147 | 155 | } |
148 | - }else{ |
|
156 | + } else{ |
|
149 | 157 | $request->bucket = $args["Bucket"]; |
150 | 158 | } |
151 | 159 | $position = "Key"; |
@@ -157,7 +165,7 @@ discard block |
||
157 | 165 | if($api["needObject"]){ |
158 | 166 | throw new Ks3ClientException($method." this api need ".$position); |
159 | 167 | } |
160 | - }else{ |
|
168 | + } else{ |
|
161 | 169 | $key = $args[$position]; |
162 | 170 | $preEncoding = mb_detect_encoding($key, array("ASCII","UTF-8","GB2312","GBK","BIG5")); |
163 | 171 | $holder->msg.="key encoding ".$preEncoding."\r\n"; |
@@ -170,16 +178,17 @@ discard block |
||
170 | 178 | if($method == "Method"){ |
171 | 179 | if(empty($args["Method"])){ |
172 | 180 | $request->method="GET"; |
173 | - }else{ |
|
181 | + } else{ |
|
174 | 182 | $request->method=$args["Method"]; |
175 | 183 | } |
176 | - }else{ |
|
184 | + } else{ |
|
177 | 185 | $request->method=$api["method"]; |
178 | 186 | } |
179 | - if(KS3_API_USE_HTTPS) |
|
180 | - $request->scheme="https://"; |
|
181 | - else |
|
182 | - $request->scheme="http://"; |
|
187 | + if(KS3_API_USE_HTTPS) { |
|
188 | + $request->scheme="https://"; |
|
189 | + } else { |
|
190 | + $request->scheme="http://"; |
|
191 | + } |
|
183 | 192 | $request->endpoint=$this->endpoint; |
184 | 193 | //add subresource |
185 | 194 | if(!empty($api["subResource"])){ |
@@ -200,7 +209,7 @@ discard block |
||
200 | 209 | foreach ($index as $key1 => $value1) { |
201 | 210 | if(!isset($curIndexArg[$value1])&&$value1 !== "*"){ |
202 | 211 | $add = FALSE; |
203 | - }else{ |
|
212 | + } else{ |
|
204 | 213 | $curkey = $value1; |
205 | 214 | //星号表示所有,按照暂时的业务,默认星号后面就没了 |
206 | 215 | if($curkey == "*"){ |
@@ -212,7 +221,7 @@ discard block |
||
212 | 221 | $add = FALSE; |
213 | 222 | $required = FALSE; |
214 | 223 | break; |
215 | - }else{ |
|
224 | + } else{ |
|
216 | 225 | $curIndexArg = $curIndexArg[$value1]; |
217 | 226 | } |
218 | 227 | } |
@@ -221,8 +230,9 @@ discard block |
||
221 | 230 | $request->addQueryParams($curkey,$curIndexArg); |
222 | 231 | continue; |
223 | 232 | } |
224 | - if($required) |
|
225 | - throw new Ks3ClientException($method." param ".$value." is required"); |
|
233 | + if($required) { |
|
234 | + throw new Ks3ClientException($method." param ".$value." is required"); |
|
235 | + } |
|
226 | 236 | } |
227 | 237 | } |
228 | 238 | if(isset($api["body"])){ |
@@ -230,7 +240,7 @@ discard block |
||
230 | 240 | $builderName = $api["body"]["builder"]; |
231 | 241 | $builder = new $builderName(); |
232 | 242 | $request->body = $builder->build($args); |
233 | - }else if(isset($api["body"]["position"])){ |
|
243 | + } else if(isset($api["body"]["position"])){ |
|
234 | 244 | $position = $api["body"]["position"]; |
235 | 245 | $index = explode("->",$position); |
236 | 246 | $curIndexArg = $args; |
@@ -239,7 +249,7 @@ discard block |
||
239 | 249 | foreach ($index as $key1 => $value1) { |
240 | 250 | if(!isset($curIndexArg[$value1])){ |
241 | 251 | $add = FALSE; |
242 | - }else{ |
|
252 | + } else{ |
|
243 | 253 | $curIndexArg = $curIndexArg[$value1]; |
244 | 254 | $curkey = $value1; |
245 | 255 | } |
@@ -267,11 +277,13 @@ discard block |
||
267 | 277 | |
268 | 278 | if($signer===NULL||!($signer instanceof QueryAuthSigner)){ |
269 | 279 | $url = $request->toUrl($this->endpoint); |
270 | - if($location!=NULL) |
|
271 | - $url = $location; |
|
280 | + if($location!=NULL) { |
|
281 | + $url = $location; |
|
282 | + } |
|
272 | 283 | $httpRequest = new RequestCore($url); |
273 | - if(KS3_API_DEBUG_MODE===TRUE) |
|
274 | - $httpRequest->debug_mode=TRUE; |
|
284 | + if(KS3_API_DEBUG_MODE===TRUE) { |
|
285 | + $httpRequest->debug_mode=TRUE; |
|
286 | + } |
|
275 | 287 | $httpRequest->set_method($request->method); |
276 | 288 | foreach ($request->headers as $key => $value) { |
277 | 289 | $httpRequest->add_header($key,$value); |
@@ -330,7 +342,7 @@ discard block |
||
330 | 342 | $data = $handler->handle($data); |
331 | 343 | } |
332 | 344 | return $data; |
333 | - }else{ |
|
345 | + } else{ |
|
334 | 346 | $url = $request->toUrl($this->endpoint); |
335 | 347 | $holder->msg.=$url."\r\n"; |
336 | 348 | return $url; |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | array_push($keys, $object["Key"]); |
35 | 35 | } |
36 | 36 | $this->client->deleteObjects(array("Bucket"=>$this->bucket,"DeleteKeys"=>$keys)); |
37 | - }else{ |
|
37 | + } else{ |
|
38 | 38 | $this->client->createBucket(array("Bucket"=>$this->bucket)); |
39 | 39 | } |
40 | 40 | } |
@@ -42,11 +42,13 @@ discard block |
||
42 | 42 | $buckets = $this->client->listBuckets(); |
43 | 43 | $found = FALSE; |
44 | 44 | foreach ($buckets as $bucket) { |
45 | - if($bucket["Name"] == $this->bucket) |
|
46 | - $found = TRUE; |
|
45 | + if($bucket["Name"] == $this->bucket) { |
|
46 | + $found = TRUE; |
|
47 | + } |
|
48 | + } |
|
49 | + if(!$found) { |
|
50 | + throw new Exception("list buckets expected found ".$this->bucket.",but not found"); |
|
47 | 51 | } |
48 | - if(!$found) |
|
49 | - throw new Exception("list buckets expected found ".$this->bucket.",but not found"); |
|
50 | 52 | |
51 | 53 | } |
52 | 54 | public function testDeleteBucket(){ |
@@ -54,7 +56,7 @@ discard block |
||
54 | 56 | $ex = NULL; |
55 | 57 | try{ |
56 | 58 | $this->client->deleteBucket(array("Bucket"=>$this->bucket)); |
57 | - }catch(Exception $e){ |
|
59 | + } catch(Exception $e){ |
|
58 | 60 | $ex = $e; |
59 | 61 | } |
60 | 62 | if($ex == NULL||!($ex->errorCode === "BucketNotEmpty")){ |
@@ -90,7 +92,7 @@ discard block |
||
90 | 92 | $ex = NULL; |
91 | 93 | try{ |
92 | 94 | $this->client->createBucket(array("Bucket"=>$this->bucket)); |
93 | - }catch(Exception $e){ |
|
95 | + } catch(Exception $e){ |
|
94 | 96 | $ex = $e; |
95 | 97 | } |
96 | 98 | if($ex == NULL||!($ex->errorCode === "BucketAlreadyExists")){ |
@@ -256,7 +258,7 @@ discard block |
||
256 | 258 | $ex = NULL; |
257 | 259 | try{ |
258 | 260 | $this->client->listParts(array("Bucket"=>$this->bucket,"Key"=>$this->key,"Options"=>array("uploadId"=>$uid))); |
259 | - }catch(Exception $e){ |
|
261 | + } catch(Exception $e){ |
|
260 | 262 | $ex = $e; |
261 | 263 | } |
262 | 264 | if($ex == NULL||!($ex->errorCode === "NoSuchUpload")){ |
@@ -33,8 +33,9 @@ |
||
33 | 33 | } |
34 | 34 | $md5 = md5_file($file); |
35 | 35 | //@unlink($file); |
36 | - if($md5 != $expectedMd5) |
|
37 | - throw new Exception("file md5 check error expected ".$expectedMd5." ,actual ".$md5, 1); |
|
36 | + if($md5 != $expectedMd5) { |
|
37 | + throw new Exception("file md5 check error expected ".$expectedMd5." ,actual ".$md5, 1); |
|
38 | + } |
|
38 | 39 | foreach ($filelist as $key => $value) { |
39 | 40 | @unlink($value); |
40 | 41 | } |
@@ -9,12 +9,13 @@ discard block |
||
9 | 9 | function run($torun = NULL){ |
10 | 10 | $r = new ReflectionClass($this); |
11 | 11 | foreach($r->getMethods() as $key=>$methodObj){ |
12 | - if($methodObj->isPrivate()) |
|
13 | - $methods[$key]['type'] = 'private'; |
|
14 | - elseif($methodObj->isProtected()) |
|
15 | - $methods[$key]['type'] = 'protected'; |
|
16 | - else |
|
17 | - $methods[$key]['type'] = 'public'; |
|
12 | + if($methodObj->isPrivate()) { |
|
13 | + $methods[$key]['type'] = 'private'; |
|
14 | + } elseif($methodObj->isProtected()) { |
|
15 | + $methods[$key]['type'] = 'protected'; |
|
16 | + } else { |
|
17 | + $methods[$key]['type'] = 'public'; |
|
18 | + } |
|
18 | 19 | $methods[$key]['name'] = $methodObj->name; |
19 | 20 | $methods[$key]['class'] = $methodObj->class; |
20 | 21 | } |
@@ -33,19 +34,21 @@ discard block |
||
33 | 34 | foreach ($methods as $method) { |
34 | 35 | if($method["class"] != "PUnit"&&substr($method["name"],0,4) == "test"){ |
35 | 36 | if($torun !== NULL){ |
36 | - if(!in_array($method["name"],$torun)) |
|
37 | - continue; |
|
37 | + if(!in_array($method["name"],$torun)) { |
|
38 | + continue; |
|
39 | + } |
|
38 | 40 | } |
39 | 41 | try{ |
40 | 42 | if($method["type"] == "public"){ |
41 | 43 | $log = new Logger(); |
42 | 44 | $log->info("Run unit --->".$method["name"]); |
43 | - if($before!=NULL) |
|
44 | - $this->$before["name"](); |
|
45 | + if($before!=NULL) { |
|
46 | + $this->$before["name"](); |
|
47 | + } |
|
45 | 48 | $this->$method["name"](); |
46 | 49 | array_push($success,$method["name"]); |
47 | 50 | } |
48 | - }catch(Exception $e){ |
|
51 | + } catch(Exception $e){ |
|
49 | 52 | $error[$method["name"]]="".$e; |
50 | 53 | } |
51 | 54 | } |