<?php
/**
 * Project:        M.VuiZ.Net
 * Author:         nampt.me <admin@vuiz.net>
 * Copyright:      © 2025 vuiz.net All rights reserved.
 */
//error_reporting(0);
$return = array();

$link = isset($_POST['link']) ? htmlspecialchars(rawurldecode($_POST['link'])) : '';
if ($link && preg_match('#https?://#is',$link) && preg_match('#/video/#is',$link)){
	$json = json_decode(auto(getSignURL($link)), true);
	
	if (isset($json['err']) !== 0 && !empty($json['data'])) {
		$data = $json['data'];
		$title = empty($data['full_name']) ? '' : $data['full_name'];
		$thumbnail = empty($data['thumbnail']) ? (empty($data['program']['thumbnail']) ? '' : $data['program']['thumbnail']) : $data['thumbnail'];

		// stream link
		$streamlink = empty($data['hls']['file_url']) ? '' : $data['hls']['file_url'];

		// Download Link
		$download = []; $i = 0;
		if (!empty($data['other_url']['Video3GP'])) {
			$download[$i]['file'] = $data['other_url']['Video3GP'];
			$download[$i]['type'] = 'mp4';
			$download[$i]['label'] = 240;
			$download[$i]['default'] = true;
			$i++;
		}
		if (!empty($data['other_url']['Video360'])) {
			$download[$i]['file'] = $data['other_url']['Video360'];
			$download[$i]['type'] = 'mp4';
			$download[$i]['label'] = 360;
			$i++;
		}
		if (!empty($data['other_url']['Video480'])) {
			$download[$i]['file'] = $data['other_url']['Video480'];
			$download[$i]['type'] = 'mp4';
			$download[$i]['label'] = 480;
			$i++;
		}
		if (!empty($data['other_url']['Video720'])) {
			$download[$i]['file'] = $data['other_url']['Video720'];
			$download[$i]['type'] = 'mp4';
			$download[$i]['label'] = 720;
			$i++;
		}
		if (!empty($data['other_url']['Video1080'])) {
			$download[$i]['file'] = $data['other_url']['Video1080'];
			$download[$i]['type'] = 'mp4';
			$download[$i]['label'] = 1080;
			$i++;
		}



		/* RETURN HTML */


		$html_download = '';
		foreach ($download as $video) {
			$html_download .= ' <a rel="noreferrer noopener" target="_blank" href="'.$video['file'].'"><button type="button" class="btn btn-success"><i class="fa fa-cloud-download"></i> Download '.$video['label'].'p</button></a> ';
		}

		$player = '';
		if (!empty($streamlink)) {
			$player = '
			<script>
				$("head").append(\'<meta name="referrer" content="no-referrer" />\');
				var playerInstance = jwplayer("myvideo");
				jwDefaults.key = "9dOyFG96QFb9AWbR+FhhislXHfV1gIhrkaxLYfLydfiYyC0s";
				playerInstance.setup({
					id: "player",
					controls: true,
					playbackRateControls: true,
					abouttext: "VuiZ.Net",
					aboutlink: "https://m.vuiz.net/",
					logo: {
					    file: "https://m.vuiz.net/template/images/logovideoplayer.png",
					    logoBar: "https://m.vuiz.net/template/images/logovideoplayer.png",
					    link: "https://m.vuiz.net/",
					    position: "control-bar"
					},
					file: "'.$streamlink.'",
					title: "'.$title.'",
					image: "'.$thumbnail.'",
					
				});
				playerInstance.on("error",function() {
					playerInstance.load("https://cdn.jwplayer.com/v2/media/7RtXk3vl");
					playerInstance.setControls(false);
					playerInstance.play();
				});
			</script>
			';
		} else {
			$player = '
			<script>
				$("head").append(\'<meta name="referrer" content="no-referrer" />\');
				var playerInstance = jwplayer("myvideo");
				jwDefaults.key = "9dOyFG96QFb9AWbR+FhhislXHfV1gIhrkaxLYfLydfiYyC0s";
				playerInstance.setup({
					id: "player",
					controls: true,
					playbackRateControls: true,
					abouttext: "VuiZ.Net",
					aboutlink: "https://m.vuiz.net/",
					logo: {
					    file: "https://m.vuiz.net/template/images/logovideoplayer.png",
					    logoBar: "https://m.vuiz.net/template/images/logovideoplayer.png",
					    link: "https://m.vuiz.net/",
					    position: "control-bar"
					},
					sources: '.json_encode($download).',
					title: "'.$title.'",
					image: "'.$thumbnail.'",
					
				});
				playerInstance.on("error",function() {
					playerInstance.load("https://cdn.jwplayer.com/v2/media/7RtXk3vl");
					playerInstance.setControls(false);
					playerInstance.play();
				});
			</script>
			';
		}

		$return['success'] = '
				<div class="menu">
					<div style="padding-bottom:15px;font-size: 16px;text-align:center;" ><b>'.$title.'</b></div>
					<div style="margin: 0 auto;"><div id="myvideo"></div></div>
					<div style="padding-top:15px; text-align: center;">
						'.$html_download.'
					</div>
					'.$player.'
				</div>';

	}
	else
	{
		$return['error'] = 'Lỗi phân giải máy chủ đích. '.isset($json['msg'])?$json['msg']:'Vui lòng thử lại sau!';
	}
}
else{
	$return['error'] = 'Đường dẫn bạn nhập không hợp lệ!';
}


echo json_encode($return, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);





function ZingTV_Url_ID($url) {
	$text = explode('/', $url);
	$id  = $text[count($text)-1];
	$id  = explode('.', $id)[0];
	return $id;
}

function getSignURL($url=''){
	$API_URL = 'https://api.tv.zing.vn/';
	$MEDIA_PATH = '3.1.4/media/info';

	$API_KEY     = '7d3343b073f9fb9ec75e53335111dcc1';
	$PRIVATE_KEY = '7856fd5b3af6b785bd5648207c9e56e8';

	$ZingTvID = ZingTV_Url_ID($url);
	$inputArr = [
		'media_id' => $ZingTvID,
		'api_key' => $API_KEY,
		'os' => 'android',
		'app_version' => '210010015',
		'ctime' => time()*1000
	];

	/* HASH SIG CODE */
	ksort($inputArr); //Sort input
	$hash_join = implode('', array_map(
		function ($v, $k) {
			return $k.$v; 
		}, 
		$inputArr, 
		array_keys($inputArr)
	));

	$hash_data = $hash_join.$PRIVATE_KEY;

	$sig = hash('md5', $hash_data);

	// Add to send data
	$inputArr['api_sig'] = $sig;
	/* END HASH SIG CODE */


	$url = $API_URL . $MEDIA_PATH . '?' . http_build_query($inputArr);

	return $url;
}

function auto($dkm) {
	$ch = @curl_init();
	curl_setopt($ch, CURLOPT_URL, $dkm);
	curl_setopt($ch, CURLOPT_USERAGENT, 'Stetho');
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_TIMEOUT, 60);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
	
	$nd = curl_exec($ch);
	curl_close($ch);
	return $nd;
}
?>