断点续传与音乐文件下载解密


<?php
/** php下载类,支持断点续传
*
* Func:
* download: 下载文件
* setSpeed: 设置下载速度
* getRange: 获取header中Range
*/


class Audio
{
public $file=0;
public $size=0;
public $new_file=0;

public function play()
{
$this->buffer();
}

/**
*
* The music download data while playing(音乐边下载数据边播放)
* @param $data
*/
public function buffer()
{
$url = 'https://mp3.baidu.com/1/top/music/40.m4a';
$music_id = '40';
$hostname = 'mp3.baidu.com';
$test_type = 'm4a';
$http_url = 'https://mp3.baidu.com/1/top/music/40.m4a';

//The header file(头部文件)
header("Content-Type: ". $data['suffix_type'] ."");
header('Content-Disposition: filename='.$test_type.'');
header('Cache-Control: no-cache');
header('Pragma: no-cache');
header('Content-Transfer-Encoding: chunked');
//Content-Length
$headers = get_headers($http_url, 1);
foreach($headers as $key => $val) {
switch($key) {
case 'Content-Length':
header("Content-Length: {$val}");
break;
default:
break;
}
}

$port = '80';
$fp=fsockopen($hostname,$port,$errno,$errstr);
// set_socket_blocking($fp,false);
stream_set_blocking($fp, false);

if(!$fp){
echo "$errno : $errstr<br/>";
}else {
$request_header="GET {$url} HTTP/1.1\r\n";
$request_header.="Host: $hostname\r\n";
$request_header .= "Connection: Close\r\n\r\n";

fputs($fp,$request_header);
$inheader = 1;

//Download the cycle data(循环下载数据)
while (!feof($fp)) {
$line = fgets($fp, 1000);

//To determine whether the header file(去除头文件)
if ($inheader && ($line == "\n" || $line == "\r\n")) {
$inheader = 0;
}

//Data contents decryption(数据内容解密)
if ($inheader == 0) {
//Decryption method(解密方式)
$check_dir = $this->decode($line, 1000, $music_id);
echo $check_dir;
}

}
fclose($fp);
}

}

/**
*
* A portion of the data decryption part(数据一部分一部分的解密)
* @param $content
* @param $new_content_size
* @param $music_id
*/
public function decode($content, $new_content_size, $music_id)
{
$randomData = array(122,11,246,167,217,85,180,205,77,124,152,198,246,152,170,206,196,62,169,91,42,139,103,80,155,31,22,228,196,13,66,52,131,214,228,143,73,103,3,25,84,156,170,213,239,131,185,239,222,28,112,191,220,210,177,139,75,247,89,69,122,163,120,89,24,251,144,164,89,68,71,242,233,134,171,101,231,103,159,192,96,197,31,100,66,93,106,154,59,152,117,15,10,86,200,213,132,165,162,146,221,164,161,239,199,170,62,213,155,23,5,174,118,161,118,163,47,214,211,79,145,59,21,139,214,237,78,22,208,85,150,157,53,127,250,98,47,139,241,99,211,245,189,60,189,205,170,32,132,227,19,41,252,192,234,175,101,60,64,246,0,50,82,4,245,93,131,22,167,74,236,198,164,10,157,26,247,36,233,46,233,190,142,213,184,135,165,194,136,3,251,173,212,246,52,112,85,87,134,170,220,237,214,246,25,224,74,247,118,47,165,108,159,186,215,127,240,76,89,100,49,243,57,165,134,251,63,239,252,75,27,3,49,231,94,148,212,187,142,144,215,114,52,51,225,69,201,132,12,124,211,129,131,49,26,218,27,230,204,242,159,191,63,85,102,103,244,138,43,152,159,114,124,151,53,75,95,208,48,246,97,150,90,169,142,247,86,43,169,124,70,213,245);
$en_content = '';
$content_size = strlen($content);
$cc = intval(intval($this->file) + intval($content_size));

if ($this->size == 0 && $cc == 2){ $this->size++; return ;}

for ($i = $this->file; $i < $cc; $i++) {
$asc_content = ord($content[$i - $this->new_file]) ^ ((($music_id * 7) + $i + $randomData[$i % 293]) & 0X00FF);
$en_content .= chr($asc_content);
$this->file++;
}
$this->new_file += strlen($en_content);

return $en_content;
}
}

class FileDownload{ // class start

private $_speed = 512; // 下载速度


/** 下载
* @param String $file 要下载的文件路径
* @param String $name 文件名称,为空则与下载的文件名称一样
* @param boolean $reload 是否开启断点续传
*/
public function download($file, $name='', $reload=false){
if(file_exists($file)){
if($name==''){
$name = basename($file);
}

$fp = fopen($file, 'rb');
$file_size = filesize($file);
$ranges = $this->getRange($file_size);

header('cache-control:public');
header('content-type:application/octet-stream');
header('content-disposition:attachment; filename='.$name);

if($reload && $ranges!=null){ // 使用续传
header('HTTP/1.1 206 Partial Content');
header('Accept-Ranges:bytes');

// 剩余长度
header(sprintf('content-length:%u',$ranges['end']-$ranges['start']));

// range信息
header(sprintf('content-range:bytes %s-%s/%s', $ranges['start'], $ranges['end'], $file_size));

// fp指针跳到断点位置
fseek($fp, sprintf('%u', $ranges['start']));
}else{
header('HTTP/1.1 200 OK');
header('content-length:'.$file_size);
}

while(!feof($fp)){
echo fread($fp, round($this->_speed*1024,0));
ob_flush();
sleep(1); // 用于测试,减慢下载速度
}

($fp!=null) && fclose($fp);

}else{
return '';
}
}


/** 设置下载速度
* @param int $speed
*/
public function setSpeed($speed){
if(is_numeric($speed) && $speed>16 && $speed<4096){
$this->_speed = $speed;
}
}


/** 获取header range信息
* @param int $file_size 文件大小
* @return Array
*/
private function getRange($file_size){
if(isset($_SERVER['HTTP_RANGE']) && !empty($_SERVER['HTTP_RANGE'])){
//file_put_contents('./log.log', 'xxxxccccc');
$range = $_SERVER['HTTP_RANGE'];
$range = preg_replace('/[\s|,].*/', '', $range);
$range = explode('-', substr($range, 6));
if(count($range)<2){
$range[1] = $file_size;
}
$range = array_combine(array('start','end'), $range);
if(empty($range['start'])){
$range['start'] = 0;
}
if(empty($range['end'])){
$range['end'] = $file_size;
}
return $range;
}
return null;
}

} // class end


$Audio = new Audio();
$Audio->play();
exit;


$file = 'https://mp3.baidu.com/1/top/music/40.m4a';
$name = time().'.zip';
$obj = new FileDownload();
$flag = $obj->download($file, $name, true);
//$flag = $obj->download($file, $name, true); // 断点续传

if(!$flag){
echo 'file not exists';
}

  

智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告