<?php
/**
 * Project:        M.VuiZ.Net
 * Author:         nampt.me <admin@vuiz.net>
 * Copyright:      © 2025 vuiz.net All rights reserved.
 */


if (isset($_POST['link'])) {
	$link = htmlentities(trim($_POST['link']));
	$data = array();
	if (preg_match('#https?://#is',$link)) {
		//$data[] = ['site' => 'Sl.ink', 'link' => slink($link)];
		$data[] = ['site' => 'Tinyurl.Com', 'link' => tinyurl($link)];
		$data[] = ['site' => 'Bit.ly', 'link' => bitly($link)];
		$data[] = ['site' => 'Shink.In', 'link' => shink($link)];
		$data[] = ['site' => 'Shorte.St', 'link' => shorte($link)];
		//$data[] = ['site' => 'Ouo.IO', 'link' => ouo($link)];
	} else {
		$data['error'] = 'Thí chủ có link không?';
	}
	echo json_encode($data);
}

function slink($link)
{
	$url = 'https://service.sl.ink/api/shorten';
	$postlink = '{"url":"'.$link.'"}';
	$sl = Curl_Post($url, $postlink);
	$sl = json_decode($sl,true);
	return isset($sl['data']) ? $sl['data'] : 'Error';
}

function tinyurl($link) {
	$api = 'http://tinyurl.com/api-create.php?url='.$link;
	$url = auto($api);
	return htmlentities(trim($url));
}

function bitly($link) {
	$api = 'https://api-ssl.bitly.com/v3/shorten?access_token=&longUrl='.$link.'&format=txt';
	$url = auto($api);
	return htmlentities(trim($url));
}

function shink($link) {
	$api = 'https://shon.xyz/api/0/id//auth_token/?s='.$link;
	$json = json_decode(auto($api), true);
	if ($json['error'] == 0)
		return 'https://shon.xyz/'.$json['hash'];
	else 
		return 'Error';
}

function shorte($link) {
	$curl = 'https://api.shorte.st/s//'.$link;
	$data = json_decode(auto($curl),true);
	if ($data['status'] == 'ok') {
		$return = $data['shortenedUrl'];
	} else {
		$return = 'Có lỗi xảy ra'; 
	}
	return trim($return);
}


function ouo($link) {
	$api = 'http://ouo.io/api/?s='.$link;
	$url = auto($api);
	if (preg_match('#https?://ouo.io#is',$url))
		return htmlentities(trim($url));
	else 
		return 'Error';
}
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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36');
	curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_ENCODING , 'gzip, deflate');
	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 Curl_Post($url, $post)
{
	$curl = curl_init();
	curl_setopt_array($curl, array(
		CURLOPT_URL => $url,
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_ENCODING => '',
		CURLOPT_MAXREDIRS => 10,
		CURLOPT_TIMEOUT => 30,
		CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		CURLOPT_CUSTOMREQUEST => 'POST',
		CURLOPT_POSTFIELDS => $post,
		CURLOPT_HTTPHEADER => array(
			'Accept: application/json',
			'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',
			'Content-Type: application/json;charset=UTF-8',
			'Origin: https://service.sl.ink',
			'Referer: https://service.sl.ink/',
			'Accept-Language: vi-VN'
		),
	));
	$return = curl_exec($curl);
	curl_close($curl);
	return $return;
}
