<?php
set_time_limit(0);
error_reporting(0);
include 'inc/mysqli.php';
include 'inc/yandex.php';
include('getid3/getid3.php');

if($_GET['hash'] != 'jRTUOalkGF258QAnfeo'){
	exit('Доступ ограничен!');
}

$store  = $db->getRow("SELECT * FROM `yandex` WHERE `size` < 10630044057 ORDER BY `id` ASC LIMIT 1");

if(!empty($store['login'])){

	$rand = md5(mt_rand(1000000000,9999999999));

	$file  = $db->getAll("SELECT * FROM `pars` WHERE `status` = 0 ORDER BY RAND() LIMIT 1");

	$check_file = $db->query("SELECT * FROM `files` WHERE `title` = ?s and `artist` = ?s", trim($file['title']), trim($file['artist']));
	
	if($db->numRows($check_file) > 0){
		$db->query("DELETE FROM `pars` WHERE `id` = ?i", $file['id']); exit;
	}
	$f_zaycev = json_decode(file_get_contents('http://zaycev.net/musicset/play/'.$file[0]['url']), true );
	if(copy($f_zaycev['url'], 'mp3/'.$rand.'.mp3')){
		
		if(file_exists('mp3/'.$rand.'.mp3')){
				$getID3 = new getID3;
				$getID3->setOption(array('encoding'=>$TaggingFormat));
				$ThisFileInfo = $getID3->analyze('mp3/'.$rand.'.mp3');

				if($ThisFileInfo['fileformat'] != 'mp3'){
					exit($ThisFileInfo['error'][0]);
				}
				
				if(!empty($ThisFileInfo['tags']['id3v2']['year'][0])){
					$year = $ThisFileInfo['tags']['id3v2']['year'][0];
				}else{
					$year = $ThisFileInfo['tags']['id3v1']['year'][0];
				}
				
				if(!empty($ThisFileInfo['tags']['id3v2']['genre'][0])){
					$genre = $ThisFileInfo['tags']['id3v2']['genre'][0];
				}else{
					$genre = $ThisFileInfo['tags']['id3v1']['genre'][0];
				}
			
			include('getid3/write.php');
			
			$tagwriter = new getid3_writetags;
			$tagwriter->filename = 'mp3/'.$rand.'.mp3';
			$tagwriter->tagformats = array('id3v1', 'id3v2.3');

			$tagwriter->overwrite_tags = true;
			$tagwriter->tag_encoding = 'UTF-8';
			$tagwriter->remove_other_tags = true;
			$cover = file_get_contents('style/img/logo.jpg');

			$TagData['title'][] = $file[0]['title'].' (midi-mp3.org)';
			$TagData['artist'][] = $file[0]['artist'].' (midi-mp3.org)';
			$TagData['album'][] = 'midi-mp3.org';
			$TagData['genre'][] = 'midi-mp3.org';
			$TagData['comment'][] = 'midi-mp3.org';
			$TagData['attached_picture'][0]['data']          = $cover;
			$TagData['attached_picture'][0]['picturetypeid'] = 0;
			$TagData['attached_picture'][0]['description']   = 'logo.jpg';
			$TagData['attached_picture'][0]['mime']          = 'image/jpeg';


			$tagwriter->tag_data = $TagData;
			$tagwriter->WriteTags();

		if(empty($year)) $year = '';

		function str2lang2($str) {
			$str = mb_strtolower($str, 'UTF-8');
			$str = preg_replace('~[^a-zA-Zа-яА-Я]+~u', '', $str);
			return $str;
		}

		if (preg_match("/[а-я]+/iu", str2lang2($file[0]['title'].$file[0]['artist']))) {
			$lang = 'ru';
		}else{
			$lang = 'en';
		}

		$a = mb_strtolower(mb_substr(trim($file[0]['artist']), -mb_strlen(trim($file[0]['artist']), 'UTF-8'), 1, 'UTF-8'), 'UTF-8');
		$t = mb_strtolower(mb_substr(trim($file[0]['title']), -mb_strlen(trim($file[0]['title']), 'UTF-8'), 1, 'UTF-8'), 'UTF-8');
				
		$disk=new yandex_disk($store['login'], $store['password']);
		$disk->put('mp3/'.$rand.'.mp3', '/mp3/'.$rand.'.mp3');
		$url = get_url('/mp3/'.$rand.'.mp3', $store['login'], $store['password']);
		
		$filesize = filesize('mp3/'.$rand.'.mp3');
		 
		if(!empty($url)){
			$db->query("UPDATE `pars` SET 
			`filename` = ?s, `year` = ?s,
			`name_full` = ?s, `playtime_string` = ?s,
			`filesize` = ?s, `bitrate` = ?s, `store` = ?s,
			`host` = ?s, `ya_url` = ?s, `date_add` = ?s, `lang` = ?s,
			`first_letter_a` = ?s, `first_letter_t` = ?s, `status` = 1, `genre` = ?s WHERE `id` = '".$file[0]['id']."'",

			$rand.'.mp3', $year, 
			trim($file[0]['artist']).' - '.trim($file[0]['title']),
			$ThisFileInfo['playtime_string'], $filesize,
			$ThisFileInfo['bitrate'], $store['login'], 'yandex', $url, time(), $lang, $a, $t, $genre);
			
			$db->query("UPDATE `yandex` SET `size` = `size` +?s WHERE `login` = ?s", $filesize, $store['login']);
		}
			$file[0]['artist'] = str_replace('and', '&', $file[0]['artist']);
			
			if(!file_exists('style/artist/'.md5($file[0]['artist']).'_large.png')){
				$r = simplexml_load_file('http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist='.$file[0]['artist'].'&api_key=5c1f8c0599632c230b797aa45403d377');
				$img_small = $r->artist->image[0]; 
				$img_large = $r->artist->image[2];
				
				if(!empty($img_small)){
					copy($img_small, 'style/artist/'.md5($file[0]['artist']).'_small.png');
				}
				if(!empty($img_large)){
					copy($img_large, 'style/artist/'.md5($file[0]['artist']).'_large.png');
				}
			}
			unlink('mp3/'.$rand.'.mp3');
		}
	}
}
?>