| Conditions | 18 |
| Paths | 907 |
| Total Lines | 108 |
| Code Lines | 76 |
| 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 |
||
| 29 | public function streaming_write_callback($curl_handle,$data,$write_stream){ |
||
| 30 | $data = $this->buffer.$data; |
||
| 31 | |||
| 32 | $length = strlen($data); |
||
| 33 | //不能把上次的没读完的长度算在这次里,应该算在上次 |
||
| 34 | $written_total = 0-strlen($this->buffer); |
||
| 35 | $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC); |
||
| 36 | if($length<$blocksize) |
||
| 37 | $this->buffer = $data; |
||
| 38 | else{ |
||
| 39 | //如果期望的范围之后还有数据,则认为数据已经接收完毕。不做任何处理 |
||
| 40 | if($this->expectedRange["end"] < $this->expectedRange["start"]){ |
||
| 41 | return $written_total+strlen($data); |
||
| 42 | } |
||
| 43 | $this->buffer = substr($data,$length - $length%$blocksize); |
||
| 44 | $data = substr($data,0,$length - $length%$blocksize); |
||
| 45 | |||
| 46 | $ivoffset = 0; |
||
| 47 | //range get时,如果不是从刚开始,则应该取加密后数据的前16个字节作为之后解密的iv |
||
| 48 | if($this->firstWrite){ |
||
| 49 | $this->firstWrite = FALSE; |
||
| 50 | if(!$this->isBegin()){ |
||
| 51 | $this->iv = substr($data,0,$blocksize); |
||
| 52 | $data = substr($data,$blocksize); |
||
| 53 | $ivoffset = $blocksize; |
||
| 54 | } |
||
| 55 | //初始化当前位置 |
||
| 56 | if(isset($this->adjustedRange)) |
||
| 57 | $this->currentIndex = $ivoffset+$this->adjustedRange["start"]; |
||
| 58 | else |
||
| 59 | $this->currentIndex = $ivoffset; |
||
| 60 | } |
||
| 61 | $written_total+=$ivoffset; |
||
| 62 | if(strlen($data) == 0){ |
||
| 63 | $decoded = ""; |
||
|
|
|||
| 64 | return $written_total; |
||
| 65 | }else{ |
||
| 66 | $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,''); |
||
| 67 | mcrypt_generic_init($td,$this->cek,$this->iv); |
||
| 68 | $decoded = mdecrypt_generic($td,$data); |
||
| 69 | mcrypt_generic_deinit($td); |
||
| 70 | mcrypt_module_close($td); |
||
| 71 | } |
||
| 72 | |||
| 73 | $this->iv = substr($data,strlen($data)-$blocksize); |
||
| 74 | //判断是否需要删除最后填充的字符,以及获取填充的字符 |
||
| 75 | $needRemovePad = FALSE; |
||
| 76 | $pad = NULL; |
||
| 77 | |||
| 78 | if($this->currentIndex+strlen($decoded) >=$this->contentLength){ |
||
| 79 | $needRemovePad = TRUE; |
||
| 80 | $pad = ord(substr($decoded,strlen($decoded)-1,1)); |
||
| 81 | if($pad<=0||$pad>$blocksize) |
||
| 82 | { |
||
| 83 | //invalid pad |
||
| 84 | $needRemovePad = FALSE; |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | //将解密后的数据截取到期望的长度 |
||
| 89 | $startOffset = 0; |
||
| 90 | $endOffset = 0; |
||
| 91 | if(isset($this->expectedRange)){ |
||
| 92 | $trueEnd = $expectedEnd = $this->expectedRange["end"]; |
||
| 93 | |||
| 94 | if($this->currentIndex+strlen($decoded)>$expectedEnd){ |
||
| 95 | $preLength = strlen($decoded); |
||
| 96 | $decoded = substr($decoded, 0,$expectedEnd-$this->currentIndex+1); |
||
| 97 | $endOffset = $preLength-strlen($decoded); |
||
| 98 | }else{ |
||
| 99 | //因为range是开始结束都计算的,range=1-2。currentIndex=1,长度是2,end=currentIndex+2-1 |
||
| 100 | $trueEnd = $this->currentIndex+strlen($decoded)-1; |
||
| 101 | } |
||
| 102 | $expectedStart = $this->expectedRange["start"]; |
||
| 103 | if($this->currentIndex<$expectedStart){ |
||
| 104 | $decoded = substr($decoded,$expectedStart - $this->currentIndex); |
||
| 105 | $startOffset = $expectedStart - $this->currentIndex; |
||
| 106 | } |
||
| 107 | //调整下次期望的开始 |
||
| 108 | $this->expectedRange["start"] = $trueEnd+1; |
||
| 109 | } |
||
| 110 | |||
| 111 | $padOffset = 0; |
||
| 112 | //再次根据截取的长度判断是否需要删除最后填充的字符 |
||
| 113 | if($needRemovePad&&$endOffset > $pad){ |
||
| 114 | $needRemovePad = FALSE; |
||
| 115 | } |
||
| 116 | $actualWriteCount = 0; |
||
| 117 | if($needRemovePad){ |
||
| 118 | $padOffset = $pad-$endOffset; |
||
| 119 | $actualWriteCount = strlen($decoded)-$padOffset; |
||
| 120 | if($actualWriteCount <= 0)//负数的情况就是用户期望的range里全是填充的 |
||
| 121 | $decoded = ""; |
||
| 122 | else |
||
| 123 | $decoded = substr($decoded,0,strlen($decoded)-$padOffset); |
||
| 124 | } |
||
| 125 | $count = fwrite($write_stream, $decoded); |
||
| 126 | if($count == 0) |
||
| 127 | $count = $actualWriteCount; |
||
| 128 | $count += $padOffset; |
||
| 129 | $count += $startOffset; |
||
| 130 | $count += $endOffset; |
||
| 131 | $this->currentIndex += $count; |
||
| 132 | $written_total+=$count; |
||
| 133 | } |
||
| 134 | //否则curl框架会报错 |
||
| 135 | $written_total+=strlen($this->buffer); |
||
| 136 | return $written_total; |
||
| 137 | } |
||
| 227 | ?> |
||