<?php
require_once KS3_API_PATH.DIRECTORY_SEPARATOR."encryption".DIRECTORY_SEPARATOR."EncryptionUtil.php";class AESCBCStreamWriteCallBack{
private $iv;
private $cek;
private $contentLength;
private $expectedRange;
private $adjustedRange;
private $currentIndex;
private $buffer private $firstWrite = TRUE;
public function __set($property_name, $value){
$this->$property_name=$value;
}
public function __get($property_name){
if(isset($this->$property_name))
{
return($this->$property_name);
}else
{
return(NULL);
}
}
public function streaming_write_callback($curl_handle,$data,$write_stream){
$data = $this->buffer.$data;
$length = strlen($data);
$written_total = 0-strlen($this->buffer);
$blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC);
if($length<$blocksize)
$this->buffer = $data;
else{
if($this->expectedRange["end"] < $this->expectedRange["start"]){
return $written_total+strlen($data);
}
$this->buffer = substr($data,$length - $length%$blocksize);
$data = substr($data,0,$length - $length%$blocksize);
$ivoffset = 0;
if($this->firstWrite){
$this->firstWrite = FALSE;
if(!$this->isBegin()){
$this->iv = substr($data,0,$blocksize);
$data = substr($data,$blocksize);
$ivoffset = $blocksize;
}
if(isset($this->adjustedRange))
$this->currentIndex = $ivoffset+$this->adjustedRange["start"];
else
$this->currentIndex = $ivoffset;
}
$written_total+=$ivoffset;
if(strlen($data) == 0){
$decoded = "";
return $written_total;
}else{
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,'');
mcrypt_generic_init($td,$this->cek,$this->iv);
$decoded = mdecrypt_generic($td,$data);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
}
$this->iv = substr($data,strlen($data)-$blocksize);
$needRemovePad = FALSE;
$pad = NULL;
if($this->currentIndex+strlen($decoded) >=$this->contentLength){
$needRemovePad = TRUE;
$pad = ord(substr($decoded,strlen($decoded)-1,1));
if($pad<=0||$pad>$blocksize)
{
$needRemovePad = FALSE;
}
}
$startOffset = 0;
$endOffset = 0;
if(isset($this->expectedRange)){
$trueEnd = $expectedEnd = $this->expectedRange["end"];
if($this->currentIndex+strlen($decoded)>$expectedEnd){
$preLength = strlen($decoded);
$decoded = substr($decoded, 0,$expectedEnd-$this->currentIndex+1);
$endOffset = $preLength-strlen($decoded);
}else{
$trueEnd = $this->currentIndex+strlen($decoded)-1;
}
$expectedStart = $this->expectedRange["start"];
if($this->currentIndex<$expectedStart){
$decoded = substr($decoded,$expectedStart - $this->currentIndex);
$startOffset = $expectedStart - $this->currentIndex;
}
$this->expectedRange["start"] = $trueEnd+1;
}
$padOffset = 0;
if($needRemovePad&&$endOffset > $pad){
$needRemovePad = FALSE;
}
$actualWriteCount = 0;
if($needRemovePad){
$padOffset = $pad-$endOffset;
$actualWriteCount = strlen($decoded)-$padOffset;
if($actualWriteCount <= 0 $decoded = "";
else
$decoded = substr($decoded,0,strlen($decoded)-$padOffset);
}
$count = fwrite($write_stream, $decoded);
if($count == 0)
$count = $actualWriteCount;
$count += $padOffset;
$count += $startOffset;
$count += $endOffset;
$this->currentIndex += $count;
$written_total+=$count;
}
$written_total+=strlen($this->buffer);
return $written_total;
}
private function isBegin(){
$beginIndex = 0;
if(isset($this->adjustedRange["start"]))
$beginIndex = $this->adjustedRange["start"];
if($beginIndex == 0)
return TRUE;
else
return FALSE;
}
}class AESCBCStreamReadCallBack{
private $iv;
private $cek;
private $contentLength;
private $buffer;
private $hasread = 0;
private $mutipartUpload =FALSE;
private $isLastPart = FALSE;
public function __set($property_name, $value){
$this->$property_name=$value;
}
public function __get($property_name){
if(isset($this->$property_name))
{
return($this->$property_name);
}else
{
return(NULL);
}
}
public function streaming_read_callback($curl_handle,$file_handle,$length,$read_stream,$seek_position){
if ($this->hasread >= $this->contentLength)
{
return '';
}
if ($this->hasread == 0 && $seek_position>0 && $seek_position !== ftell($read_stream))
{
if (fseek($read_stream, $seek_position) !== 0)
{
throw new RequestCore_Exception('The stream does not support seeking and is either not at the requested position or the position is unknown.');
}
}
$blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB);
$needRead = min($this->contentLength - $this->hasread,$length);
$read = fread($read_stream,$needRead);
$this->hasread += strlen($read);
$isLast = FALSE;
if($this->hasread >= $this->contentLength){
$isLast = TRUE;
}
$data = $this->buffer.$read;
$dataLength = strlen($data);
if(!$isLast){
$this->buffer = substr($data,$dataLength-$dataLength%$blocksize);
$data = substr($data, 0,$dataLength-$dataLength%$blocksize);
}else{
if($this->mutipartUpload){
if($this->isLastPart){
$this->buffer = NULL;
$data = EncryptionUtil::PKCS5Padding($data,$blocksize);
}else{
}
}else{
$this->buffer = NULL;
$data = EncryptionUtil::PKCS5Padding($data,$blocksize);
}
}
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,'');
mcrypt_generic_init($td,$this->cek,$this->iv);
$encrypted = mcrypt_generic($td,$data);
mcrypt_generic_deinit($td);
$this->iv = substr($encrypted, strlen($encrypted)-$blocksize);
return $encrypted;
}
}
?>