public function pay(){
$id = $this->unlock_url(input('id'));
$video = db('videos')->where(['id'=>$id])->find();
ini_set('memory_limit', '1024M');
set_time_limit(600);
if(strpos($video['path'],"video.com"))
$play_url = $video['path'];
else
$play_url = 'http://'.$_SERVER['HTTP_HOST'].str_replace("\\",'/',$video['path']);
$this->outPutStream($play_url);
}
public function outPutStream($videoUrl) {
echo $videoUrl;
if(!$videoUrl){
header('HTTP/1.1 500 Internal Server Error');
echo "Error: Video cannot be played !";
exit();
}
$header_array = get_headers($videoUrl, true);
$sizeTemp = $header_array['Content-Length'];
if (is_array($sizeTemp)) {
$size = $sizeTemp[count($sizeTemp) - 1];
} else {
$size = $sizeTemp;
}
$start = 0;
$end = $size - 1;
$length = $size;
$buffer = 1024 * 1024 * 10;
$ranges_arr = array();
if (isset($_SERVER['HTTP_RANGE'])) {
if (!preg_match('/^bytes=\d*-\d*(,\d*-\d*)*$/i', $_SERVER['HTTP_RANGE'])) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
}
$ranges = explode(',', substr($_SERVER['HTTP_RANGE'], 6));
foreach ($ranges as $range) {
$parts = explode('-', $range);
$ranges_arr[] = array($parts[0], $parts[1]);
}
$ranges = $ranges_arr[0];
$start = (int)$ranges[0];
if ($ranges[1] != '') {
$end = (int)$ranges[1];
}
$length = min($end - $start + 1, $buffer);
$end = $start + $length - 1;
}else{
$start=0;
$end=1;
$length=2;
}
$header = array("Range:bytes={$start}-{$end}");
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, $videoUrl);
curl_setopt($ch2, CURLOPT_TIMEOUT, 60);
curl_setopt($ch2, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch2, CURLOPT_BUFFERSIZE, $buffer);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch2, CURLOPT_HEADER, false);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch2, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch2, CURLOPT_NOBODY, false);
curl_setopt($ch2, CURLOPT_REFERER, $videoUrl);
curl_setopt($ch2, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36 Edg/85.0.564.44");
$content = curl_exec($ch2);
curl_close($ch2);
header('HTTP/1.1 206 PARTIAL CONTENT');
header("Accept-Ranges: bytes");
header("Connection: keep-alive");
header("Content-Type: video/mp4");
header("Access-Control-Allow-Origin: *");
if($end!=1){
$end=$size-1;
}
header("Content-Range: bytes {$start}-{$end}/{$size}");
header("Content-Length: ".strlen($content));
ob_clean();
echo $content;
die;
unset($content);
}
function PutMovie($file) {
ini_set('memory_limit','512M');
header("Content-type: video/mp4");
header("Accept-Ranges: bytes");
ob_start();
$size = filesize($file);
if(isset($_SERVER['HTTP_RANGE'])){
header("HTTP/1.1 206 Partial Content");
list($name, $range) = explode("=", $_SERVER['HTTP_RANGE']);
list($begin, $end) =explode("-", $range);
if($end == 0) $end = $size - 1;
}else {
$begin = 0; $end = $size - 1;
}
header("Content-Length: " . ($end - $begin + 1));
header("Content-Disposition: filename=".basename($file));
header("Content-Range: bytes ".$begin."-".$end."/".$size);
try {
$fp = fopen($file, 'r');
} catch (\Exception $e) {
echo $e->getTraceAsString();exit;
}
fseek($fp, $begin);
$contents = '';
while(!feof($fp)) {
$p = min(1024, $end - $begin + 1);
$contents .= fread($fp, $p);
}
ob_end_clean();
ob_clean();
fclose($fp);
exit($contents);
}
function lock_url($txt,$key='str'){
$txt = $txt.$key;
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
$nh = rand(0,64);
$ch = $chars[$nh];
$mdKey = md5($key.$ch);
$mdKey = substr($mdKey,$nh%8, $nh%8+7);
$txt = base64_encode($txt);
$tmp = '';
$i=0;$j=0;$k = 0;
for ($i=0; $i<strlen($txt); $i++) {
$k = $k == strlen($mdKey) ? 0 : $k;
$j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
$tmp .= $chars[$j];
}
return urlencode(base64_encode($ch.$tmp));
}
function unlock_url($txt,$key='str'){
$txt = base64_decode(urldecode($txt));
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
$ch = $txt[0];
$nh = strpos($chars,$ch);
$mdKey = md5($key.$ch);
$mdKey = substr($mdKey,$nh%8, $nh%8+7);
$txt = substr($txt,1);
$tmp = '';
$i=0;$j=0; $k = 0;
for ($i=0; $i<strlen($txt); $i++) {
$k = $k == strlen($mdKey) ? 0 : $k;
$j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);
while ($j<0) $j+=64;
$tmp .= $chars[$j];
}
return trim(base64_decode($tmp),$key);
}