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

/*
$mc = mysql_connect('localhost', 'root', '') or die('Невозможно подключиться к MySQL');
mysql_set_charset('utf8',$mc);
mysql_select_db('muz', $mc) or die('Указанная таблица не найдена');
*/
/*
function curl($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl,CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 5.1; U; ru) Presto/2.2.15 Version/10.10');
curl_setopt($curl, CURLOPT_REFERER, "http://google.ru/");
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
$result = curl_exec($curl);
curl_close($curl);

return $result;
}

*/
/*
$r = curl('http://zaycev.net/pages/70510/7051052.shtml');
preg_match('#data-url="/musicset/play/(.*)"#sU', $r, $url); 
preg_match('#<span class="breadcrumbs__item" itemprop="title">(.*)<#sU', $r, $title); 
preg_match('#<span itemprop="name">(.*)</span>#sU', $r, $artist); 

$req = mysql_query("SELECT * FROM `pars` ");

while($f = mysql_fetch_array($req)){
	$r = curl($f['url']);
	preg_match('#data-url="/musicset/play/(.*)"#sU', $r, $url); 
	preg_match('#<span class="breadcrumbs__item" itemprop="title">(.*)<#sU', $r, $title); 
	preg_match('#<span itemprop="name">(.*)</span>#sU', $r, $artist); 
	
	mysql_query("UPDATE `pars` SET `url` = '".$url[1]."', `title` = '".$title[1]."', `artist` = '".$artist[1]."' WHERE `id` = '".$f['id']."'");
}
*/

/*
for($i=1;$i<172;$i++){
	$r = curl('https://mp3up.me/pesni-shansona/'.($i > 1 ? 'page/'.$i.'/' : null));

	$kol_url = preg_match_all('#<audio src="(.*)"#sU', $r, $arr_href); 
	preg_match_all('#<img src="https://mp3up.me/images/download_icon.png" alt="(.*)"#sU', $r, $arr_name); 
	//print_r($arr_name);
	for($q=0;$q<$kol_url;$q++){
		
		//preg_match('#<a href="(.*)">(.*)</a>#sU', $arr_name[1][$q], $title); 
		//preg_match('#<div style="font-weight:bold;"><a href="(.*)">(.*)</a></div>#sU', $arr_name[1][$q], $artist); 
	
		//$w = curl($url[1][0]);

		//preg_match('#<meta property="og:audio" content="(.*)" />#sU', $w, $url);
		//preg_match('#<meta property="og:title" content="(.*)" />#sU', $w, $name);
	
		$name_s = explode(' - ', str_replace('Скачать ', '', $arr_name[1][$q]));
		
		$name_s[0] = str_replace('&amp;', '&',$name_s[0]);
		$name_s[1] = str_replace('&amp;', '&',$name_s[1]);
		$name_s[0] = str_replace('&#039;', '\'',$name_s[0]);
		$name_s[1] = str_replace('&#039;', '\'',$name_s[1]);
		$title = str_replace('&#39;', '\'',$name_s[0]);
		$artist = str_replace('&#39;', '\'',$name_s[1]);
		
		//echo $title.' - '.$artist.'<br />';
		
		if(!empty($title) and !empty($artist)){
			$req = mysql_query("SELECT `id` FROM `files` WHERE `title` = '".mysql_real_escape_string(trim($title))."' and `artist` = '".mysql_real_escape_string(trim($artist))."'");
			$req_pars = mysql_query("SELECT `id` FROM `pars` WHERE `title` = '".mysql_real_escape_string(trim($title))."' and `artist` = '".mysql_real_escape_string(trim($artist))."'");
			if(mysql_num_rows($req) == 0 and mysql_num_rows($req_pars) == 0){
				mysql_query("INSERT INTO `pars` SET 
				`url` = '".$arr_href[1][$q]."',
				`genre` = '',
				`host` = 'mp3up.me',
				`artist` = '".mysql_real_escape_string(trim($artist))."',
				`title` = '".mysql_real_escape_string(trim($title))."'");
			}
		}
	}
}
*/

/*
/////Получить обложку
$req = mysql_query("SELECT * FROM `files`");

while($f = mysql_fetch_array($req)){
	$f['artist'] = str_replace('and', '&', $f['artist']);
	$f_and = explode('&', $f['artist']);
	
	
	$r = simplexml_load_file('http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist='.$f['artist'].'&api_key=5c1f8c0599632c230b797aa45403d377');
	$img_small = $r->artist->image[0]; 
	$img_medium = $r->artist->image[1]; 
	$img_large = $r->artist->image[2]; 
	$img_extralarge = $r->artist->image[3]; 
	
	if(!empty($img_small)){
		copy($img_small, 'style/artist/'.md5($f['artist']).'_small.png');
	}
	if(!empty($img_medium)){
		copy($img_medium, 'style/artist/'.md5($f['artist']).'_medium.png');
	}
	if(!empty($img_large)){
		copy($img_large, 'style/artist/'.md5($f['artist']).'_large.png');
	}
	if(!empty($img_extralarge)){
		copy($img_extralarge, 'style/artist/'.md5($f['artist']).'_extralarge.png');
	}


}
*/
/*
$req = mysql_query("SELECT * FROM `files` WHERE `canonical_artist` != 0");
//echo mysql_num_rows($req);
while($f = mysql_fetch_array($req)){
	echo $f['artist'].' -- '.$f['id'].'<br />';
	mysql_query("UPDATE `files` SET `canonical_artist` = ".$f['id']." WHERE `artist` = '".mysql_real_escape_string($f['artist'])."'") or die(mysql_error());
}*/

/*
$getID3 = new getID3;
$ThisFileInfo = $getID3->analyze('mp3/2.mp3');
print_r($ThisFileInfo);

*/

/*
$tagwriter = new getid3_writetags;
$tagwriter->filename = 'mp3/1.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'][] = 'df';
$TagData['artist'][] = '';
$TagData['album'][] = '';
$TagData['genre'][] = '';
$TagData['comment'][] = '';
$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();
*/


/*
$log = 'mp3store33';
$pass = 'ad730da';


$disk = new yandex_disk($log, $pass);
$ls = $disk->ls('/mp3/');

for($i=0;$i<count($ls);$i++){
	$ls[$i] = str_replace('/mp3/', '', $ls[$i]);
	if(!empty($ls[$i])){
		file_put_contents($log.'.txt', $ls[$i]."\n", FILE_APPEND);
	}
}




$kol = 0;
$file = file($log.'.txt');
for($i=0;$i<count($file);$i++){
	$req = mysql_query("SELECT `id` FROM `pars` WHERE `filename` = '".mysql_real_escape_string(trim($file[$i]))."' and `store` != '".$log."'");
	if(mysql_num_rows($req) > 0){
		$url = get_url('/mp3/'.trim($file[$i]), $log, $pass);
		
		mysql_query("UPDATE `pars` SET `ya_url` = '".$url."', `store` = '".$log."', `host` = 'yandex' WHERE `filename` = '".mysql_real_escape_string(trim($file[$i]))."'");
		
		$kol++;
	}
}
echo $kol;
*/


/*
$req = mysql_query("SELECT DISTINCT `artist` FROM `files` WHERE `id` > 38682");

while($file = mysql_fetch_array($req)){
	
	
		$file['artist'] = str_replace('and', '&', $file['artist']);
			
			if(!file_exists('style/artist/'.md5($file['artist']).'_large.png')){
				$r = simplexml_load_file('http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist='.$file['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['artist']).'_small.png');
					
				}
				if(!empty($img_large)){
					copy($img_large, 'style/artist/'.md5($file['artist']).'_large.png');
				}
			}
}
*/
/*
function str2lang2($str) {
			$str = mb_strtolower($str, 'UTF-8');
			$str = preg_replace('~[^a-zA-Zа-яА-Я]+~u', '', $str);
			return $str;
		}

$req = mysql_query("SELECT `id`,`url`,`title`,`artist`,`filename` FROM `pars` WHERE `status` = 1");
$getID3 = new getID3;
$getID3->setOption(array('encoding'=>$TaggingFormat));

while($file = mysql_fetch_array($req)){
	$file['title'] = str_replace('&#039;', '\'',$file['title']);
	$file['artist'] = str_replace('&#039;', '\'',$file['artist']);
	$file['title'] = str_replace('&#39;', '\'',$file['title']);
	$file['artist'] = str_replace('&#39;', '\'',$file['artist']);
	$file['title'] = str_replace('&amp;', '&',$file['title']);
	$file['artist'] = str_replace('&amp;', '&',$file['artist']);
	
	$rand = mt_rand(111111111,999999999);
	$name = 'mp3/'.iconv('UTF-8', 'windows-1251', $file['artist'].' - '.$file['title']).' (midi-mp3.org).mp3';
	if(file_exists($name)){
		rename($name, 'mp3/'.md5($rand).'.mp3');
		
		$ThisFileInfo = $getID3->analyze('mp3/'.md5($rand).'.mp3');
		
		if(!empty($ThisFileInfo['tags']['id3v2']['year'][0])){
			$year = $ThisFileInfo['tags']['id3v2']['year'][0];
		}else{
			$year = $ThisFileInfo['tags']['id3v1']['year'][0];
		}
		
		if (preg_match("/[а-я]+/iu", str2lang2($file['artist'].$file['title']))) {
			$lang = 'ru';
		}else{
			$lang = 'en';
		}
		$a = mb_strtolower(mb_substr(trim($file['artist']), -mb_strlen(trim($file['artist']), 'UTF-8'), 1, 'UTF-8'), 'UTF-8');
		$t = mb_strtolower(mb_substr(trim($file['title']), -mb_strlen(trim($file['title']), 'UTF-8'), 1, 'UTF-8'), 'UTF-8');
		
		mysql_query("UPDATE `pars` SET 
		`filename` = '".md5($rand).".mp3', 
		`status` = 2,
		`name_full` = '".mysql_real_escape_string(trim($file['artist']))." - ".mysql_real_escape_string(trim($file['title']))."',
		`year` = '".abs(intval($year))."', 
		`filesize` = '".filesize('mp3/'.md5($rand).'.mp3')."',
		`bitrate` = '".$ThisFileInfo['bitrate']."',
		`playtime_string` = '".$ThisFileInfo['playtime_string']."',
		`lang` = '".$lang."', `first_letter_a` = '".$a."', `first_letter_t` = '".$t."'
		WHERE `id` = '".$file['id']."'");
	}
}
*/
/*

$filelist = glob("mp3/*.mp3");
$getID3 = new getID3;
//$getID3->setOption(array('encoding'=>$TaggingFormat));
	//$ThisFileInfo = $getID3->analyze('mp3/0a1877455602607f1d6de7b441e705c9.mp3');
	//echo $ThisFileInfo['id3v2']['comments']['title'][0];
	//print_r($ThisFileInfo);
foreach ($filelist as $filename){
	$file2 = explode('mp3/', $filename);

	$getID3->setOption(array('encoding'=>$TaggingFormat));
	$ThisFileInfo = $getID3->analyze('mp3/'.$file2[1]);
	
	if(!empty($ThisFileInfo['tags']['id3v2']['year'][0])){
		$year = $ThisFileInfo['tags']['id3v2']['year'][0];
	}else{
		$year = $ThisFileInfo['tags']['id3v1']['year'][0];
	}
	
	if(empty($year)) $year = '';

	$title = $ThisFileInfo['id3v2']['comments']['title'][0];
	$artist = $ThisFileInfo['id3v2']['comments']['artist'][0];
	$title = str_replace('(midi-mp3.org)', '', $title);
		if (preg_match("/[а-я]+/iu", str2lang2($title.$artist))) {
			$lang = 'ru';
		}else{
			$lang = 'en';
		}
	$a = mb_strtolower(mb_substr(trim($artist), -mb_strlen(trim($artist), 'UTF-8'), 1, 'UTF-8'), 'UTF-8');
	$t = mb_strtolower(mb_substr(trim($title), -mb_strlen(trim($title), 'UTF-8'), 1, 'UTF-8'), 'UTF-8');
		
		$filesize = filesize('mp3/'.$file2[1]);
		
		mysql_query("INSERT INTO `pars` SET 
		`year` = '".abs(intval($year))."', 
		`filename` = '".mysql_real_escape_string($file2[1])."', 
		`title` = '".mysql_real_escape_string(trim($title))."', 
		`artist` = '".mysql_real_escape_string(trim($artist))."', 
		`name_full` = '".mysql_real_escape_string(trim($artist))." - ".mysql_real_escape_string(trim($title))."',
		`bitrate` = '".$ThisFileInfo['bitrate']."',
		`filesize` = '".$filesize."', 
		`playtime_string` = '".$ThisFileInfo['playtime_string']."',
		`lang` = '".$lang."', `first_letter_a` = '".$a."', `first_letter_t` = '".$t."', `status` = 1") or die(mysql_error());

}*/
/*$req = mysql_query("SELECT * FROM `pars` WHERE `status` = 1");
while($file2 = mysql_fetch_array($req)){
	mysql_query("UPDATE `pars` SET `filename` = '".$file2['artist']." - ".$file2['title'].".mp3', `status` = 2 WHERE `id` = '".$file2['id']."'");
}*/
/*
$r = simplexml_load_file('http://zaycev.net/sitemap/520.xml');

for($i=0;$i<count($r);$i++){
	//echo $r->url[$i]->loc.'<br />';
	
	mysql_query("INSERT INTO `pars` SET `url` = '".$r->url[$i]->loc."'");
}*/

$data  = $db->getAll("SELECT * FROM `files`");

foreach($data as $file){

		$db->query("UPDATE `files` SET `date_add` = '".(time()-mt_rand(86400,604800))."' WHERE `id` = '".$file['id']."'");
	
}
?>
