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

if($_POST){
	$link = isset($_POST['link']) ? htmlspecialchars(rawurldecode($_POST['link'])) : '';
	$link = check_old_beta($link);
	if ($link && preg_match('#https?://#is',$link)){

		if (preg_match('#/mp3/#is',$link)) {
			$source = Curl_Content($link);
			//preg_match_all('/musicId = \'([0-9]+)\';/i', $source, $id);
			preg_match('/musicId = \'([0-9]+)\';/', $source, $id);
			$id = @$id[1];
			if(isset($id)){
				$data_json = Curl_Content('https://old.chiasenhac.vn/api/listen.php?code=csn22052018&return=json&m='.$id.'&url='.$link);
				$data_json = str_replace('download2', 'stream2', $data_json);
				$api = json_decode( $data_json, true);
				if($api['music_info']['music_id']==$id)
				{
					$data = $api['music_info'];
					$download = '';

					if (!empty($data['file_url']))
						$download .= ' <a rel="noreferrer noopener" target="_blank" href="'.httpssl($data['file_url']).'"><button type="button" class="btn btn-success"><i class="fa fa-cloud-download"></i> MP3 128Kbps</button></a> ';
					if (!empty($data['file_320_url']))
						$download .= ' <a rel="noreferrer noopener" target="_blank" href="'.httpssl($data['file_320_url']).'"><button type="button" class="btn btn-success"><i class="fa fa-cloud-download"></i> MP3 320Kbps</button></a> ';
					if (!empty($data['file_m4a_url']))
						$download .= ' <a rel="noreferrer noopener" target="_blank" href="'.httpssl($data['file_m4a_url']).'"><button type="button" class="btn btn-success"><i class="fa fa-cloud-download"></i> M4A 500Kbps</button></a> ';
					if (!empty($data['file_lossless_url']))
						$download .= ' <a rel="noreferrer noopener" target="_blank" href="'.httpssl($data['file_lossless_url']).'"><button type="button" class="btn btn-success"><i class="fa fa-cloud-download"></i> FLAC Lossless</button></a> ';
					if (!empty($data['file_m4a_url']))
						$download .= ' <a rel="noreferrer noopener" target="_blank" href="'.httpssl($data['file_m4a_url']).'"><button type="button" class="btn btn-success"><i class="fa fa-cloud-download"></i> M4A 32Kbps</button></a> ';
					

					$return['success'] = '
					<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="'.$data['music_artist'].'" data-title="'.$data['music_title'].'" data-album="'.$data['music_album'].'" data-info="" data-image="'.httpssl($data['music_img']).'" data-duration="0">
										<div class="amazingaudioplayer-source" data-src="'.httpssl($data['file_url']).'" data-type="audio/mpeg" />
									</li>
								</ul>
							</div>
						</div>
						<div style="text-align: center;">
							'.$download.'
						</div>
					</div>
					<script src="/template/audioplayerengine/amazingaudioplayer.js"></script>
					<script src="/template/audioplayerengine/initaudioplayer-1.js"></script>';
				} else {
					$return['error'] = $api['errorMsg'];
				}
			} else {
				$return['error'] = 'Địa chỉ bạn nhập không hợp lệ hoặc bài nhạc đã bị xóa!';
			}
		
		}elseif (preg_match('#/chiasenhac.vn/#is',$link)) {
			$source = Curl_Content($link);
			preg_match_all('/addPlaylistTable\((.*?)\)"/i', $source, $playlist);
			$playlist = @$playlist[1];

			if(count($playlist) > 0){
				$listPlayer = ''; $download = '';


				$listPlayer.= '
						<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 ($playlist as $songInfo) {
					$songInfo = json_decode(str_replace('\'', '"', '['.$songInfo.']'), true);
					$data_json = Curl_Content('http://old.chiasenhac.vn/api/listen.php?code=csn22052018&return=json&m='.$songInfo[1].'&url='.$link);
					$data_json = str_replace('download2', 'stream2', $data_json);
					$data = json_decode($data_json,true);
					$song = $data['music_info'];
					$listPlayer.= '
									<li data-artist="'.$song['music_artist'].'" data-title="'.$song['music_title'].'" data-album="" data-info="" data-image="'.httpssl($song['music_img']).'" data-duration="0">
									<div class="amazingaudioplayer-source" data-src="'.httpssl($song['file_url']).'" data-type="audio/mpeg" />';
					$download .= '
							  <tr>
								<td><b>'.$song['music_title'].'</b> - '.$song['music_artist'].'</td>
								<td style="text-align:center">'.(!empty($song['file_url'])? '<a class="128" rel="noreferrer nofollow" target="_blank" href="'.httpssl($song['file_url']).'" download="true"><i class="fa fa-download"></i>  Tải nhạc</a>':'Không có').'</td>
								<td style="text-align:center">'.(!empty($song['file_320_url'])? '<a class="320" rel="noreferrer nofollow" target="_blank" href="'.httpssl($song['file_320_url']).'" download="true"><i class="fa fa-download"></i>  Tải nhạc</a>':'Không có').'</td>
								<td style="text-align:center">'.(!empty($song['file_lossless_url'])? '<a class="lossless" rel="noreferrer nofollow" target="_blank" href="'.httpssl($song['file_lossless_url']).'" download="true"><i class="fa fa-download"></i>  Tải nhạc</a>':'Không có').'</td>
							  </tr>';
				}

				$download.= '
							</tbody>
						</table>';

				$listPlayer.= '
								</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'] =  $listPlayer;
			} else {
				$return['error'] = 'Địa chỉ bạn nhập không hợp lệ hoặc bài nhạc đã bị xóa!';
			}
		}
		else
		{
			$return['error'] = 'Định dạng đường dẫn chưa được hỗ trợ.';
		}
	}
	else
	{
		$return['error'] = 'Link không hợp lệ.';
	}
}

elseif($_GET)
{
	$chart = json_decode(Curl_Content('https://chiasenhac.vn/api/nhac-hot.html'),true);
	$return = $chart['data']['dataBxh']['3']['music'];
}


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

function Curl_Content($dkm) {
	$ch = @curl_init();
	curl_setopt($ch, CURLOPT_URL, $dkm);
	$head[] = "Connection: keep-alive";
	$head[] = "Keep-Alive: 300";
	$head[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
	$head[] = "Accept-Language: en-us,en;q=0.5";
	$ua = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) coc_coc_browser/45.0 Chrome/39.0.2171.95 Safari/537.36';
	curl_setopt($ch, CURLOPT_USERAGENT, $ua);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
	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;
}

function check_old_beta($link)
{
	$link = str_replace('http://','https://',$link);
	$link = preg_replace('/https\:\/\/(.*?)chiasenhac/is', 'https://chiasenhac', $link);
	return $link;
}
function httpssl($link)
{
	$link = str_replace('http://','https://',$link);
	return $link;
}


?>