<?php
# 4ekiss (ICQ: 8311156);

$textl = 'Прослушка найденного трека';
define('MP3', true);
include('sys/config.php');
include('sys/fnc.php');

# Удаление и подсчет размера файлов
$dir = opendir('data'); $size = 0;

while($file = readdir ($dir)){
if($file != "." && $file != ".." && $file != ".htaccess"  && $file != "search.dat"){
if($time>(filemtime("data/$file")+$set['time'])) unlink("data/$file");
else $size+=filesize('data/'.$file);
}
}

closedir ($dir);

if($size>$set['size']*1048576) die('Sorry, service unavailable! Please try again later.');

if(is_numeric($_GET['song']) && is_numeric($_GET['id'])){

$api = md5('76575883api_id=1886174audios='.$_GET['id'].'_'.$_GET['song'].'method=audio.getByIdtest_mode=1v=2.0'.$set['key']);
$xml = Send_Post('http://api.vkontakte.ru/api.php', 'api_id=1886174&audios='.$_GET['id'].'_'.$_GET['song'].'&method=audio.getById&test_mode=1&v=2.0&sig='.$api, 'http://vkontakte.ru/app1886174');
$info = simplexml_load_string($xml);

if(empty($info->audio->artist)) die('File not found!');

$name = $info->audio->artist.'-'.$info->audio->title;
$name = strtr($name, array_flip($translate));
$name = preg_replace('/[^a-z0-9_\-\.]/i', '', str_replace(' ', '_', str_replace("%20", "_", strtolower($name))));
if(empty($name)) $name = $_GET['song'];

$extdir = dirname($_SERVER['REQUEST_URI'])=='/' ? dirname($_SERVER['REQUEST_URI']) : dirname($_SERVER['REQUEST_URI']).'/';
$path = 'data/'.($set['domaine']==1 ? $_SERVER['HTTP_HOST'].'_' : '').$name.'.mp3';







if(!file_exists($path)){

copy($info->audio->url, $path);

if($set['write']==1 && (!empty($set['title']) || !empty($set['artist']))){

include('sys/getid3/getid3.php');

$getID3 = new getID3;
$getID3->encoding = 'cp1251';
$getid = $getID3->analyze($path);

include('sys/getid3/write.php');

$tagwriter = new getid3_writetags;
$tagwriter->filename = $path;
$tagwriter->tagformats = array('id3v2.3');
$tagwriter->tag_encoding = 'cp1251';

$tagexist = true; $TagData = array();

if(!empty($getid['tags']['id3v2'])){

if($set['remove']==0) $TagData = $getid['tags']['id3v2'];
else {
$TagData['title'][0] = isset($getid['tags']['id3v2']['title'][0]) ? $getid['tags']['id3v2']['title'][0] : '';
$TagData['artist'][0] = isset($getid['tags']['id3v2']['artist'][0]) ? $getid['tags']['id3v2']['artist'][0] : '';
$TagData['album'][0] = isset($getid['tags']['id3v2']['album'][0]) ? $getid['tags']['id3v2']['album'][0] : '';
}
}
elseif(!empty($getid['tags']['id3v1'])){
if($set['remove']==0) $TagData = $getid['tags']['id3v1'];
else {
$TagData['title'][0] = isset($getid['tags']['id3v1']['title'][0]) ? $getid['tags']['id3v1']['title'][0] : '';
$TagData['artist'][0] = isset($getid['tags']['id3v1']['artist'][0]) ? $getid['tags']['id3v1']['artist'][0] : '';
$TagData['album'][0] = isset($getid['tags']['id3v1']['album'][0]) ? $getid['tags']['id3v1']['album'][0] : '';
}
} else $tagexist = false;

if($tagexist && $set['plus']==1){
if(!empty($set['title'])) $TagData['title'][0] = $TagData['title'][0].encoding($set['title'], 'Windows-1251');
if(!empty($set['artist'])) $TagData['artist'][0] = $TagData['artist'][0].' '.encoding($set['artist'], 'Windows-1251');
if(!empty($set['album'])) $TagData['album'][0] = $TagData['album'][0].' '.encoding($set['album'], 'Windows-1251');
} else {
if(!empty($set['title'])) $TagData['title'][0] = encoding($set['title'], 'Windows-1251');
if(!empty($set['artist'])) $TagData['artist'][0] = encoding($set['artist'], 'Windows-1251');
if(!empty($set['album'])) $TagData['album'][0] = encoding($set['album'], 'Windows-1251');
}

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

}

file_put_contents($path.'.dat', '');
if(isset($_GET['copy'])) exit;

} else {
touch($path); touch($path.'.dat');
}

include('design/head.php');

if(!isset($getid)){
include('sys/getid3/getid3.php');

$getID3 = new getID3;
$getID3->encoding = 'cp1251';
$getid = $getID3->analyze($path);
}

if(!isset($getid['error'][0])){
$tagexist = true;

if(!empty($getid['tags']['id3v2'])) $TagData = $getid['tags']['id3v2'];
elseif(!empty($getid['tags']['id3v1'])) $TagData = $getid['tags']['id3v1'];
else $tagexist = false;

# Вывод;

echo '<div class="phdr">Прослушка найденного трека</div>
<div class="menu"><b>Длительность:</b> '.date('i:s', $getid['playtime_seconds']).'
<br/><b>Частота:</b> '.ceil($getid['audio']['sample_rate']).' Hz
<br/><b>Битрейт:</b> '.ceil($getid['audio']['bitrate']/1000).' Kbps
<br/><b>Каналы:</b> '.$getid['audio']['channels'].' ('.$getid['audio']['channelmode'].')<br/>';

if($tagexist){
if(isset($TagData['artist'][0])) echo '<b>Исполнитель:</b> '.check(encoding($TagData['artist'][0], 'UTF-8')).'<br/>';
if(isset($TagData['title'][0])) echo '<b>Название:</b> '.check(encoding($TagData['title'][0], 'UTF-8')).'<br/>';
if(isset($TagData['album'][0])) echo '<b>Альбом:</b> '.check(encoding($TagData['album'][0], 'UTF-8')).'<br/>';
}
}

echo '<div class="list2"><a href="http://'.$_SERVER['SERVER_NAME'].$extdir.$path.'"><b>Скачать</b></a> ['.formatsize(filesize($path)).']<br />
        <div class="sub">Прослушка:<br />
        <object id="audioplayer1" data="swf/player.swf" type="application/x-shockwave-flash" width="240" height="16">
        <param value="swf/player.swf" name="audio"/><param value="loop=no&amp;autostart=no&amp;soundFile='.$path.'" name="FlashVars"/>
        <param value="high" name="quality"/><param value="false" name="menu"/>
        <param value="transparent" name="wmode"/></object>
        <div/></div></div></div>
        <div class="rmenu"><input type="text" value="http://'.$_SERVER['SERVER_NAME'].$extdir.$path.'" /></div>
        </div>';

include('design/foot.php');

} else die('Error Request!');

?>