<?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)){
	$source = auto($link);
	preg_match('/soundcloud\:\/\/sounds\:([0-9]+)"/', $source, $dl);
	$dl = $dl[1];
	$keyapi = 'client_id=LBCcHmRB8XSStWL6wKH2HPACspQlXg2P';
	$api = 'https://api-v2.soundcloud.com/tracks?ids='.$dl.'&'.$keyapi;
	$data = json_decode(auto($api),true);

	if(count($data) != 0){
		$ten = $data[0]['title'];
		$thumb = $data[0]['artwork_url'];
		$streams = $data[0]['media']['transcodings'];
		$artist = $data[0]['user']['username'];

		foreach ($streams as $stream) {
			if ($stream["format"]["protocol"] == "progressive") {
				$down = json_decode(auto($stream["url"] . "?" . $keyapi), true)["url"];
			}
		}

		$return['success'] = '
		<link rel="stylesheet" type="text/css" href="/template/audioplayerengine/initaudioplayer-1.css">
		<script>
			function downloadResource(url, filename) {
				if (!filename) filename = url.split("\\\\").pop().split("/").pop();
				fetch(url, {
					headers: new Headers({
						"Origin": location.origin
					}),
					mode: "cors"
				})
				.then(response => response.blob())
				.then(blob => {
					let blobUrl = window.URL.createObjectURL(blob);
					forceDownload(blobUrl, filename);
				})
				.catch(e => console.error(e));
			}
			function forceDownload(blob, filename) {
				var a = document.createElement("a");
				a.download = filename;
				a.href = blob;
				a.click();
			}
		</script>
		<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="'.$artist.'" data-title="'.$ten.'" data-album="" data-info="" data-image="'.$thumb.'" data-duration="0">
							<div class="amazingaudioplayer-source" data-src="'.$down.'" data-type="audio/mpeg" />
						</li>
					</ul>
				</div>
			</div>
			<div style="text-align: center;">
				<a rel="noreferrer noopener" target="_blank" href="'.$down.'"><button type="button" class="btn btn-success">Link Play</button></a>
				<a onclick="downloadResource(\''.$down.'\',\''.$ten.'.mp3\')"><button type="button" class="btn btn-success"><i class="fa fa-cloud-download"></i> Download 128 Kbps</button> </a>
			</div>
		</div>
		<script src="/template/audioplayerengine/amazingaudioplayer.js"></script>
		<script src="/template/audioplayerengine/initaudioplayer-1.js"></script>
		';
	} else { $return['error'] = 'Có lỗi xảy ra, vui lòng thử lại sau!'; }
}else { $return['error'] = 'Đường dẫn 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 auto($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";
	curl_setopt($ch, CURLOPT_USERAGENT, '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_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;
}

?>