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


$link = isset($_POST['link']) ? htmlspecialchars(rawurldecode($_POST['link'])) : '';

if ($link && preg_match('#https?://#is',$link) ){
	$download = get_tiktok($link);
	if (isset($download) && isset($download['link'])) {
		$return['success'] = $download;
	}else{
		$return['error'] = 'There was an issue with the TikTok link you submitted. Please try again.';
	}
}
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 get_tiktok($url) {

	$video = [];
	$json_video_download = json_decode(url_get_contents('http://185.227.111.142/api/video/getAwemeWithUrl?url='.$url), true);

	$get_media = media_info($url);
	if (isset($json_video_download['status']) && $json_video_download['status'] === 0 ) {
		$video["link"] = $json_video_download['video']['download_url'];
		if (isset($get_media)) {
			$video = $get_media;
		} else {
			$video['title'] = $json_video_download['video']['description'];
			$video['thumbnail'] = 'https://i.imgur.com/MWfLcn4.png';
		}
		$video["link"] = $json_video_download['video']['download_url'];

		return $video;
	} else {
		return $get_media;
	}
}

function media_info($videoUrl)
{
	$videoUrl = getRedirectUrl($videoUrl);
	preg_match('/#\/@\w{2,32}\/video\/\d{2,32}/', $videoUrl, $matches);
	if (count($matches) === 1) {
		$videoUrl = 'https://www.tiktok.com' . ltrim($matches[0], '#');
	}
	preg_match('/\/video\/([0-9]+)/', $videoUrl, $matches);
	if (count($matches) >= 2) {
		$shareUrl = 'https://www.tiktok.com/node/share/video/@' . $matches[1] . '/' . $matches[1];
		$shareData = url_get_contents($shareUrl);
		$shareData = json_decode($shareData, true);
		if (!isset($shareData['itemInfo']['itemStruct']['video'])) {
			$webPage = url_get_contents($videoUrl);
			preg_match("/<script id=\"__NEXT_DATA__\" (?:.*?)>(.*?)<\/script>/", $webPage, $matches);
			if (isset($matches[1]) != '') {
				$shareData = json_decode($matches[1], true)['props']['pageProps'];
		}
			
		$video["title"] = $shareData['itemInfo']['itemStruct']['desc'];
		if (empty($video["title"])) {
			$video["title"] = $shareData['metaParams']['title'];
		}
		$video["thumbnail"] = $shareData['itemInfo']['itemStruct']['video']['cover'];

		//Get Video URL
		$video["link"] = $shareData['itemInfo']['itemStruct']['video']['downloadAddr'];

		if (isset($shareData['itemInfo']['itemStruct']['music']['playUrl']) != '') {
			$video["audio"] = $shareData['itemInfo']['itemStruct']['music']['playUrl'];
		}
		return $video;

		}

	}
	return false;
}

function checkHost($url)
{
	$host = str_replace('www.', '', parse_url($url, PHP_URL_HOST));
	return $host == 'tiktok.com';
}

function url_get_contents($url) {
	$ch = @curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	$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";
	$head[] = "Sec-Fetch-Dest: video";
	$head[] = "Sec-Fetch-Mode: no-cors";
	$head[] = "Sec-Fetch-Site: cross-site";
	$head[] = "Host: ".parse_url($url,PHP_URL_HOST);
	$head[] = "Referer: https://www.tiktok.com/";

	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);
	curl_setopt($ch, CURLOPT_COOKIEFILE, 'tiktok-cookie.txt');
	curl_setopt($ch, CURLOPT_COOKIEJAR, 'tiktok-cookie.txt');
	$nd = curl_exec($ch);
	curl_close($ch);
	return $nd;
}

function getRedirectUrl($url, $maxRedirs = 3)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HEADER, true);
	curl_setopt($ch, CURLOPT_NOBODY, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($ch, CURLOPT_MAXREDIRS, $maxRedirs);
	curl_setopt($ch, CURLOPT_TIMEOUT, 15);
	$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";
	$head[] = "Sec-Fetch-Dest: video";
	$head[] = "Sec-Fetch-Mode: no-cors";
	$head[] = "Sec-Fetch-Site: cross-site";
	$head[] = "Host: ".parse_url($url,PHP_URL_HOST);
	$head[] = "Referer: https://www.tiktok.com/";
	curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
	curl_setopt($ch, CURLOPT_COOKIEFILE, 'tiktok-cookie.txt');
	curl_setopt($ch, CURLOPT_COOKIEJAR, 'tiktok-cookie.txt');
	curl_exec($ch);
	$longUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
	curl_close($ch);
	parse_str(parse_url($longUrl, PHP_URL_QUERY), $query);
	if (!empty($query['next'])) {
		return $query['next'];
	} else {
		return $longUrl;
	}
}
?>