| Conditions | 32 |
| Paths | 19520 |
| Total Lines | 160 |
| Code Lines | 122 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 116 | public function getObjectSecurely($args=array()){ |
||
| 117 | $meta = $this->ks3client->getObjectMeta($args); |
||
| 118 | if(isset($meta["UserMeta"]["x-kss-meta-x-kss-key"])&&isset($meta["UserMeta"]["x-kss-meta-x-kss-iv"])){ |
||
| 119 | $encryptedInMeta = TRUE; |
||
| 120 | }else{ |
||
| 121 | $encryptedInMeta = FALSE; |
||
| 122 | } |
||
| 123 | $encrypted = TRUE; |
||
| 124 | $encryptionInfo = array(); |
||
| 125 | if($encryptedInMeta){ |
||
| 126 | $encryptionInfo["iv"] = base64_decode($meta["UserMeta"]["x-kss-meta-x-kss-iv"]); |
||
| 127 | $matdesc =$meta["UserMeta"]["x-kss-meta-x-kss-matdesc"]; |
||
| 128 | $encryptionInfo["matdesc"] = $matdesc; |
||
| 129 | $cekEncrypted = base64_decode($meta["UserMeta"]["x-kss-meta-x-kss-key"]); |
||
| 130 | $encryptionInfo["cek"] = $cek = EncryptionUtil::decodeCek($this->encryptionMaterials,$cekEncrypted); |
||
| 131 | }else{ |
||
| 132 | if($this->ks3client->objectExists(array( |
||
| 133 | "Bucket"=>$args["Bucket"], |
||
| 134 | "Key"=>$args["Key"].EncryptionUtil::$INSTRUCTION_SUFFIX) |
||
| 135 | ) |
||
| 136 | ){ |
||
| 137 | $insKey = $args["Key"].EncryptionUtil::$INSTRUCTION_SUFFIX; |
||
| 138 | $getIns = array( |
||
| 139 | "Bucket"=>$args["Bucket"], |
||
| 140 | "Key"=>$insKey, |
||
| 141 | ); |
||
| 142 | $s3Object = $this->ks3client->getObject($getIns); |
||
| 143 | if(!EncryptionUtil::isInstructionFile($s3Object)) |
||
| 144 | throw new Ks3ClientException($insKey." is not an InstructionFile"); |
||
| 145 | |||
| 146 | $content = $s3Object["Content"]; |
||
| 147 | $content = json_decode($content,TRUE); |
||
| 148 | $encryptionInfo["iv"] = base64_decode($content["x-kss-iv"]); |
||
| 149 | $matdesc =$content["x-kss-matdesc"]; |
||
| 150 | $encryptionInfo["matdesc"] = $matdesc; |
||
| 151 | $cekEncrypted = base64_decode($content["x-kss-key"]); |
||
| 152 | $encryptionInfo["cek"] = $cek = EncryptionUtil::decodeCek($this->encryptionMaterials,$cekEncrypted); |
||
| 153 | }else{ |
||
| 154 | $encrypted =FALSE; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | //是否为下载到文件中 |
||
| 158 | $isWriteToFile=FALSE; |
||
| 159 | if($encrypted) |
||
| 160 | { |
||
| 161 | $iv = $encryptionInfo["iv"]; |
||
| 162 | $cek = $encryptionInfo["cek"]; |
||
| 163 | |||
| 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 | |||
| 169 | if(isset($args["Range"])){ |
||
| 170 | $range = $args["Range"]; |
||
| 171 | if(!is_array($range)){ |
||
| 172 | if(preg_match('/^bytes=[0-9]*-[0-9]*$/', $range)){ |
||
| 173 | $ranges = explode("-",substr($range,strlen("bytes="))); |
||
| 174 | $a = $ranges[0]; |
||
| 175 | $b = $ranges[1]; |
||
| 176 | if($a > $b){ |
||
| 177 | throw new Ks3ClientException("Invalid range ".$range); |
||
| 178 | } |
||
| 179 | $range = array("start"=>$a,"end"=>$b); |
||
| 180 | }else{ |
||
| 181 | throw new Ks3ClientException("Invalid range ".$range); |
||
| 182 | } |
||
| 183 | }else{ |
||
| 184 | if(!isset($range["start"])||!isset($range["end"])){ |
||
| 185 | throw new Ks3ClientException("Invalid range ".serialize($range)); |
||
| 186 | } |
||
| 187 | if($range["start"] > $range["end"]){ |
||
| 188 | throw new Ks3ClientException("Invalid range ".serialize($range)); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | $isWriteToFile = isset($args["WriteTo"]); |
||
| 194 | $contentLength = $meta["ObjectMeta"]["Content-Length"]; |
||
| 195 | if($isWriteToFile){ |
||
| 196 | $writeCallBack = new AESCBCStreamWriteCallBack(); |
||
| 197 | $writeCallBack->iv=$iv; |
||
| 198 | $writeCallBack->cek=$cek; |
||
| 199 | $writeCallBack->contentLength = $contentLength; |
||
| 200 | if(isset($range)){ |
||
| 201 | $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC); |
||
| 202 | $adjustedRange = EncryptionUtil::getAdjustedRange($range,$blocksize); |
||
| 203 | $writeCallBack->expectedRange = $range; |
||
| 204 | $writeCallBack->adjustedRange = $adjustedRange; |
||
| 205 | $args["Range"]=$adjustedRange; |
||
| 206 | } |
||
| 207 | $args["writeCallBack"] = $writeCallBack; |
||
| 208 | return $this->ks3client->getObject($args); |
||
| 209 | }else{ |
||
| 210 | $offset = 0; |
||
| 211 | $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC); |
||
| 212 | if(isset($range)){ |
||
| 213 | $adjustedRange = EncryptionUtil::getAdjustedRange($range,$blocksize); |
||
| 214 | $args["Range"]=$adjustedRange; |
||
| 215 | } |
||
| 216 | $s3Object = $this->ks3client->getObject($args); |
||
| 217 | $content = $s3Object["Content"]; |
||
| 218 | |||
| 219 | if(isset($range)){ |
||
| 220 | if($adjustedRange["start"] > 0){ |
||
| 221 | $iv = substr($content,0,$blocksize); |
||
| 222 | $content = substr($content,$blocksize); |
||
| 223 | $offset = $blocksize+$adjustedRange["start"]; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | if(!empty($content)){ |
||
| 227 | $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,''); |
||
| 228 | mcrypt_generic_init($td,$cek,$iv); |
||
| 229 | $decoded = mdecrypt_generic($td,$content); |
||
| 230 | mcrypt_generic_deinit($td); |
||
| 231 | mcrypt_module_close($td); |
||
| 232 | }else{ |
||
| 233 | $decoded = ""; |
||
| 234 | } |
||
| 235 | |||
| 236 | //判断是否需要删除最后填充的字符,以及获取填充的字符 |
||
| 237 | $needRemovePad = FALSE; |
||
| 238 | $pad = NULL; |
||
| 239 | if($offset+strlen($decoded) >=$contentLength){ |
||
| 240 | $needRemovePad = TRUE; |
||
| 241 | $pad = ord(substr($decoded,strlen($decoded)-1,1)); |
||
| 242 | if($pad<=0||$pad>$blocksize) |
||
| 243 | { |
||
| 244 | //invalid pad |
||
| 245 | $needRemovePad = FALSE; |
||
| 246 | } |
||
| 247 | } |
||
| 248 | $endOffset = 0; |
||
| 249 | if(isset($range)){ |
||
| 250 | if($offset+strlen($decoded)>$range["end"]){ |
||
| 251 | $preLength = strlen($decoded); |
||
| 252 | $decoded = substr($decoded, 0,$range["end"]-$offset+1); |
||
| 253 | $endOffset = $preLength-strlen($decoded); |
||
| 254 | } |
||
| 255 | if($offset<$range["start"]){ |
||
| 256 | $decoded = substr($decoded,$range["start"] - $offset); |
||
| 257 | } |
||
| 258 | } |
||
| 259 | //再次根据截取的长度判断是否需要删除最后填充的字符 |
||
| 260 | if($needRemovePad&&$endOffset > $pad){ |
||
| 261 | $needRemovePad = FALSE; |
||
| 262 | } |
||
| 263 | if($needRemovePad){ |
||
| 264 | $padOffset = $pad-$endOffset; |
||
| 265 | $actualWriteCount = strlen($decoded)-$padOffset; |
||
| 266 | if($actualWriteCount <= 0)//负数的情况就是用户期望的range里全是填充的 |
||
| 267 | $decoded = ""; |
||
| 268 | else |
||
| 269 | $decoded = substr($decoded,0,strlen($decoded)-$padOffset); |
||
| 270 | } |
||
| 271 | $s3Object["Content"] = $decoded; |
||
| 272 | return $s3Object; |
||
| 273 | } |
||
| 274 | }else{ |
||
| 275 | return $this->ks3client->getObject($args); |
||
| 276 | } |
||
| 358 | ?> |
||