<?php

/* Begin With Some Note 
	
	* This SDK is not by NCT Crop
	* This SDK is not for COMMERCIAL
	* The SDK by Phuc Phoenix (github@phuchptty)
	* You Can Find Me At
		+ Facebook: https://facebook.com/hoangphuchotboy
		+ Twitter : @phuchptty
		+ GitHub  : phuchptty
	* Share free at GitHub, BitBucket and J2Team Community
	* DO NOT EDIT UNLESS YOU KNOW ABOUT CODING. JUST WAIT FOR A NEW UPDATE

This is END */

class NCT {

	private function getToken(){

		$headers = array();
		$headers[] = 'Content-Type: application/x-www-form-urlencoded';
		$headers[] = 'Host: graph.nhaccuatui.com';
		$headers[] = 'Connection: Keep-Alive';
		$c = curl_init();
		curl_setopt($c, CURLOPT_URL, "https://graph.nhaccuatui.com/v2/commons/token");
		curl_setopt($c, CURLOPT_SSL_VERIFYPEER,false);
		curl_setopt($c, CURLOPT_SSL_VERIFYHOST,false);
		curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($c, CURLOPT_POST, 1);
		curl_setopt($c, CURLOPT_POSTFIELDS, "deviceinfo=%7B%22DeviceID%22%3A%22dd03852ada21ec149103d02f76eb0a04%22%2C%22DeviceName%22%3A%22AppTroLyBeDieu%22%2C%22OsName%22%3A%22WINDOWS%22%2C%22OsVersion%22%3A%228.0%22%2C%22AppName%22%3A%22NCTTablet%22%2C%22AppTroLyBeDieu%22%3A%221.3.0%22%2C%22UserName%22%3A%220%22%2C%22QualityPlay%22%3A%22128%22%2C%22QualityDownload%22%3A%22128%22%2C%22QualityCloud%22%3A%22128%22%2C%22Network%22%3A%22WIFI%22%2C%22Provider%22%3A%22NCTCorp%22%7D&md5=ebd547335f855f3e4f7136f92ccc6955&timestamp=1499177482892");


		$page = curl_exec($c);
		curl_close($c);
		
		$a = json_decode($page,true);
		
		if ($a['code'] == "0"){
			
			$a = $a['data'];
			return $a['accessToken'];
		
		}else{
			return "Get Token Error";
		}

		return $infotoken;
	}

	/* Begin API Music V2 */

	public function getSongDetail($id){
	
		$linklist = 'https://graph.nhaccuatui.com/v2/songs/'.$id.'?access_token='.$this -> getToken();
		$c = curl_init();
		curl_setopt($c, CURLOPT_URL, $linklist);
		curl_setopt($c, CURLOPT_SSL_VERIFYPEER,false);
		curl_setopt($c, CURLOPT_SSL_VERIFYHOST,false);
		curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($c, CURLOPT_RETURNTRANSFER, true);

		$page = curl_exec($c);
		curl_close($c);
		
		$data = json_decode($page,true);
		
		if ($data['code'] != 0){
			return $data['msg'];
		}else{

			$a = $data['data'];

			$array = array(

				"SongID" => $a[1],
				"SongName" => $a[2],
				"SongSinger" => $a[3],
				"SongLike" => $a[4],
				"SongListen" => $a[5],
				"SongLink" => $a[6],
				"SongStreamLink" => $a[7],
				"SongThumbnail" => $a[8],
				"SongDuration" => $a[10],
				"SongDownload128" => $a[11],
				"SongDownload320" => $a[12],
				"SongDownloadLosLess" => $a[19]				

			);
			
			return $array;
		}
	
	}

	function getVideoDetail($id){
	
		$linklist = 'https://graph.nhaccuatui.com/v1/videos/'.$id.'?access_token='.$this -> getToken();
		$c = curl_init();
		curl_setopt($c, CURLOPT_URL, $linklist);
		curl_setopt($c, CURLOPT_SSL_VERIFYPEER,false);
		curl_setopt($c, CURLOPT_SSL_VERIFYHOST,false);
		curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($c, CURLOPT_RETURNTRANSFER, true);

		$page = curl_exec($c);
		curl_close($c);
		
		$data = json_decode($page,true);
		
		if ($data['code'] != 0){
			return $data['msg'];
		}else{

			$a = $data['data'];

			$array360 = array(

				"VideoQuality" => $a[12][0][1],
				"VideoStreamLink" => $a[12][0][2],
				"VideoDownloadLink" => $a[12][0][4],

			);

			$array480 = array(

				"VideoQuality" => $a[12][1][1],
				"VideoStreamLink" => $a[12][1][2],
				"VideoDownloadLink" => $a[12][1][4],

			);

			$array720 = array(

				"VideoQuality" => $a[12][2][1],
				"VideoStreamLink" => $a[12][2][2],
				"VideoDownloadLink" => $a[12][2][4],

			);

			$array1080 = array(

				"VideoQuality" => $a[12][3][1],
				"VideoStreamLink" => $a[12][3][2],
				"VideoDownloadLink" => $a[12][3][4],

			);

			$stream[] = array('file' => $a[12][3][2], 'type' => 'mp4', 'label' => $a[12][3][1].'p');//1080
			$stream[] = array('file' => $a[12][2][2], 'type' => 'mp4', 'label' => $a[12][2][1].'p');//720
			$stream[] = array('file' => $a[12][1][2], 'type' => 'mp4', 'label' => $a[12][1][1].'p');//480
			$stream[] = array('file' => $a[12][0][2], 'type' => 'mp4', 'label' => $a[12][0][1].'p');//360

			$array = array(

				"VideoID" => $a[1],
				"VideoName" => $a[2],
				"VideoThumbnail" => $a[3],
				"VideoArtis" => $a[4],
				"VideoDuration" => $a[5],
				"VideoLike" => $a[6],
				"VideoView" => $a[7],
				"VideoLink" => $a[8],
				"VideoStreamURL" => $stream,
				"Video360" => $array360,
				"Video480" => $array480,
				"Video720" => $array720,
				"Video1080" => $array1080,

			);
			
			return $array;
		}
	
	}

	public function getPlaylistDetail($id){
	
		$linklist = 'https://graph.nhaccuatui.com/v1/playlists/'.$id.'?access_token='.$this -> getToken();
		$c = curl_init();
		curl_setopt($c, CURLOPT_URL, $linklist);
		curl_setopt($c, CURLOPT_SSL_VERIFYPEER,false);
		curl_setopt($c, CURLOPT_SSL_VERIFYHOST,false);
		curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($c, CURLOPT_RETURNTRANSFER, true);

		$page = curl_exec($c);
		curl_close($c);
		
		$data = json_decode($page,true);
		
		if ($data['code'] != 0){
			return $data['msg'];
		
		}else{

			$a = $data['data'];

			$array = array(

				"PlaylistID" => $a[1],
				"PlaylistName" => $a[2],
				"PlaylistThumbnail" => $a[4],
				"PlaylistArtis" => $a[5],
				"PlaylistLink" => $a[6],
				"PlaylistView" => $a[7],
				"PlaylistLink" => $a[8],
				"PlaylistSongDetail" => $a[9],
				"PlaylistDescription" => $a[11],

			);

			return $array;

		}
		
	}
}