<?php
/**
 * Project:        M.VuiZ.Net
 * Author:         nampt.me <admin@vuiz.net>
 * Copyright:      © 2025 vuiz.net All rights reserved.
 */
error_reporting(0);
require 'sdk.php';
$return = array();
$msg = ''; $download = ''; 
if($_POST){
	$link = isset($_POST['link']) ? htmlspecialchars(rawurldecode($_POST['link'])) : '';
	$temp = explode(".",$link);
	$idbaihat = trim($temp[3]);
	if ($link && $idbaihat != "" && preg_match('#https?://#is',$link)){

		$url = $link;
		if (preg_match('#/bai-hat/#is',$url)) {
				$nct = new NCT;
				$data = $nct -> getSongDetail($idbaihat);
				$linkplay = $data['SongStreamLink'];
				$link128 = $data['SongDownload128'];
				$link320 = $data['SongDownload320'];
				$linklossless = $data['SongDownloadLosLess'];
				$thumbnail = $data['SongThumbnail'];
				$tenbaihat = $data['SongName'];
				$casy = $data['SongSinger'];
				if($tenbaihat != ""){
					$msg.= '
		<link rel="stylesheet" type="text/css" href="/template/audioplayerengine/initaudioplayer-1.css">
		<div class="menu">
			<div style="margin:12px auto;">
				<div id="amazingaudioplayer-1" style="display:block;position:relative;width:100%;max-width:300px;height:300px;margin:0px auto 0px;">
					<ul class="amazingaudioplayer-audios" style="display:none;">
						<li data-artist="'.$casy.'" data-title="'.$tenbaihat.'" data-album="" data-info="" data-image="'.$thumbnail.'" data-duration="0">
							<div class="amazingaudioplayer-source" data-src="'.$linkplay.'" data-type="audio/mpeg" />
						</li>
					</ul>
				</div>
			</div>
			<div style="text-align: center;">';

					$msg.= empty($link128)?'':' <a rel="noreferrer noopener" target="_blank" href="'.$link128.'"><button type="button" class="btn btn-success"><i class="fa fa-cloud-download"></i> 128 Kbps</button></a> ';
					$msg.= empty($link320)?'':' <a rel="noreferrer noopener" target="_blank" href="'.$link320.'"><button type="button" class="btn btn-success"><i class="fa fa-cloud-download"></i> 320 Kbps</button></a> ';
					$msg.= empty($linklossless)?'':' <a rel="noreferrer noopener" target="_blank" href="'.$linklossless.'"><button type="button" class="btn btn-success"><i class="fa fa-cloud-download"></i> Lossless</button></a> ';
					$msg.= '
			</div>
		</div>

	<script src="/template/audioplayerengine/amazingaudioplayer.js"></script>
	<script src="/template/audioplayerengine/initaudioplayer-1.js"></script>';
					$return['success'] = $msg;
				}else{
					$return['error'] = 'Có lỗi xảy ra, vui lòng thử lại sau!';
				}
		}


		elseif (preg_match('#/playlist/#is',$url)) {

				$nct = new NCT;
				$data = $nct -> getPlaylistDetail($idbaihat);
				$PlaylistName = $data['PlaylistName'];
				$PlaylistThumbnail = $data['PlaylistThumbnail'];
				$PlaylistSongDetail = $data['PlaylistSongDetail'];
				if($PlaylistName && $PlaylistThumbnail && $PlaylistSongDetail){
					$msg.= '
		<link rel="stylesheet" type="text/css" href="/template/audioplayerengine/initaudioplayer-1.css">
		<div class="menu" id="playlist">
			<div style="margin:12px auto;">
				<div id="amazingaudioplayer-1" style="display:block;position:relative;width:100%;max-width:300px;height:300px;margin:0px auto 0px;">
					<ul class="amazingaudioplayer-audios" style="display:none;">';
					$download.= '
		<table class="table table-striped">
			<thead>
			  <tr>
				<th style="width:45%">Bài hát</th>
				<th style="width:15%;text-align:center" id="128"><label>128 Kbps <input type="checkbox" checked=""></label></th>
				<th style="width:15%;text-align:center" id="320"><label>320 Kbps <input type="checkbox" checked=""></label></th>
				<th style="width:15%;text-align:center" id="lossless"><label>Lossless <input type="checkbox" checked=""></label></th>
			  </tr>
			</thead>
			
			<script>
				$("#128").click(function(event) {
					if (event.target.nodeName == "LABEL") return;
					$(".128").fadeToggle();
					if($(".128").attr("href")) {
						$(".128").each(function() {
							var $t = $(this);
							$t.attr({href2 : $t.attr("href"),}).removeAttr("href");
						});
					} else  {
						$(".128").each(function() {
							var $t = $(this);
							$t.attr({href : $t.attr("href2"),}).removeAttr("href2");
						});
					}
				}); 
				$("#320").click(function(event) {
					if (event.target.nodeName == "LABEL") return;
					$(".320").fadeToggle();
					if($(".320").attr("href")) {
						$(".320").each(function() {
							var $t = $(this);
							$t.attr({href2 : $t.attr("href"),}).removeAttr("href");
						});
					} else  {
						$(".320").each(function() {
							var $t = $(this);
							$t.attr({href : $t.attr("href2"),}).removeAttr("href2");
						});
					}
				}); 
				$("#lossless").click(function(event) {
					if (event.target.nodeName == "LABEL") return;
					$(".lossless").fadeToggle();
					if($(".lossless").attr("href")) {
						$(".lossless").each(function() {
							var $t = $(this);
							$t.attr({href2 : $t.attr("href"),}).removeAttr("href");
						});
					} else  {
						$(".lossless").each(function() {
							var $t = $(this);
							$t.attr({href : $t.attr("href2"),}).removeAttr("href2");
						});
					}
				}); 
			</script>
			
			<tbody>';
					foreach ($PlaylistSongDetail as $key => $playlist) {
						$linkplay = $playlist[7];
						$link128 = $playlist[11];
						$link320 = $playlist[12];
						$linklossless = $playlist[19];
						$thumbnail = $playlist[8];
						$tenbaihat = $playlist[2];
						$casy = $playlist[3];
						$msg.= '<li data-artist="'.$casy.'" data-title="'.$tenbaihat.'" data-album="" data-info="" data-image="'.$thumbnail.'" data-duration="0">
							<div class="amazingaudioplayer-source" data-src="'.$linkplay.'" data-type="audio/mpeg" /></li>';
						$download.= '
			  <tr>
				<td>'.$key.'. <b>'.$tenbaihat.'</b> - '.$casy.'</td>
				<td style="text-align:center">'.(empty($link128)?'Không có':'<a class="128" rel="noreferrer nofollow" target="_blank" href="'.$link128.'" download="true"><i class="fa fa-download"></i>  Tải nhạc</a>').'</td>
				<td style="text-align:center">'.(empty($link320)?'Không có': '<a class="320" rel="noreferrer nofollow" target="_blank" href="'.$link320.'" download="true"><i class="fa fa-download"></i>  Tải nhạc</a>').'</td>
				<td style="text-align:center">'.(empty($linklossless)?'Không có': '<a class="lossless" rel="noreferrer nofollow" target="_blank" href="'.$linklossless.'" download="true"><i class="fa fa-download"></i>  Tải nhạc</a>').'</td>
			  </tr>';
					}
					$download.= '
			</tbody>
		</table>';
					$msg.= '	
					</ul>
				</div>
			</div>
			'.$download.'
		</div>
	<style type="text/css">
		#playlist table{
			border-collapse:collapse;
			width:100%;
		}
		#playlist th, #playlist td{
			text-align:left;
			padding:10px;
		}
		#playlist table tr:nth-child(odd){
			background-color:#eee;
		}
		#playlist table tr:nth-child(even){
			background-color:white;
		}
		#playlist thead tr:nth-child(1){
			background-color:skyblue;
		}
	</style>
	<script src="/template/audioplayerengine/amazingaudioplayer.js"></script>
	<script src="/template/audioplayerengine/initaudioplayer-1.js"></script>';
					$return['success'] = $msg;
				}else{
					$return['error'] = 'Có lỗi xảy ra, vui lòng thử lại sau!';
				}

		}
/* GetlinkVideo */
		elseif (preg_match('#/video/#is',$url)) {

			$nct = new NCT;
			$data = $nct -> getVideoDetail($idbaihat);
			if($data['VideoName'] && $data['VideoStreamURL']){
				$download.= empty($data['Video360'])?'':'<a rel="noreferrer noopener" target="_blank" href="'.$data['Video360']['VideoDownloadLink'].'"><button type="button" class="btn btn-success"><i class="fa fa-cloud-download"></i> Download '.$data['Video360']['VideoQuality'].'p</button></a> ';
				$download.= empty($data['Video480'])?'':'<a rel="noreferrer noopener" target="_blank" href="'.$data['Video480']['VideoDownloadLink'].'"><button type="button" class="btn btn-success"><i class="fa fa-cloud-download"></i> Download '.$data['Video480']['VideoQuality'].'p</button></a> ';
				$download.= empty($data['Video720'])?'':'<a rel="noreferrer noopener" target="_blank" href="'.$data['Video720']['VideoDownloadLink'].'"><button type="button" class="btn btn-success"><i class="fa fa-cloud-download"></i> Download '.$data['Video720']['VideoQuality'].'p</button></a> ';
				$download.= empty($data['Video1080'])?'':'<a rel="noreferrer noopener" target="_blank" href="'.$data['Video1080']['VideoDownloadLink'].'"><button type="button" class="btn btn-success"><i class="fa fa-cloud-download"></i> Download '.$data['Video1080']['VideoQuality'].'p</button></a> ';
				$return['success'] = '
	<div class="menu">
		<div style="padding-bottom:15px;font-size: 16px;text-align:center;" ><b>'.$data['VideoName'].'</b> - '.$data['VideoArtis'].'</div>
		<div id="video" style="width: 100%;"><div id="myvideo"></div></div>
		<script>
				$("head").append(\'<meta name="referrer" content="no-referrer" />\');
				var playerInstance = jwplayer("myvideo");
				jwDefaults.key = "9dOyFG96QFb9AWbR+FhhislXHfV1gIhrkaxLYfLydfiYyC0s";
				playerInstance.setup({
					id: "player",
					title: "'.$data['VideoName'].'",
					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($data['VideoStreamURL']).',
					image: "'.$data['VideoThumbnail'].'"
				});
				playerInstance.on("error",function() {
					playerInstance.load("https://cdn.jwplayer.com/v2/media/7RtXk3vl");
					playerInstance.setControls(false);
					playerInstance.play();
				});
		</script>
    	<div style="text-align: center; padding-top:15px;">
			'.$download.'
		</div>
	</div>';
			}else{
				$return['error'] = 'Có lỗi xảy ra, vui lòng thử lại sau!';
			}
		}
		else {
			$return['error'] = 'Định dạng đường dẫn không được hỗ trợ!';
		}
	}else{
		$return['error'] = 'Lỗi cmnr: Không tìm thấy ID bài hát! Định dạng link không hợp lệ!';
	}

}elseif($_GET){
	require '../simple_html_dom.php';
	$html = file_get_html('https://www.nhaccuatui.com/');
	$link = $html->find('a[id=aUrlTop20]',0)->href;
	$return['success'] = $link;

}

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

