<?php
include_once H.'sys/classes/MP3_Id.class.php';

$id3 = new MP3_Id();
$result = $id3->read($dir_loads . '/' . $file);
$result = $id3->study();
if (access('loads_file_edit')) {
    if (isset($_GET['act']) && $_GET['act'] == 'edit_tag_ok') {
        $id3->setTag('name', (function_exists('iconv') ? iconv('utf-8', 'windows-1251', $_POST['name']) : $_POST['name']));
        $id3->setTag('artists', (function_exists('iconv') ? iconv('utf-8', 'windows-1251', $_POST['artists']) : $_POST['artists']));
        $id3->setTag('album', (function_exists('iconv') ? iconv('utf-8', 'windows-1251', $_POST['album']) : $_POST['album']));
        $id3->setTag('year', (function_exists('iconv') ? iconv('utf-8', 'windows-1251', $_POST['year']) : $_POST['year']));
        $id3->setTag('comment', (function_exists('iconv') ? iconv('utf-8', 'windows-1251', $_POST['comment']) : $_POST['comment']));
        $id3->setTag('genre', (function_exists('iconv') ? iconv('utf-8', 'windows-1251', $_POST['genre']) : $_POST['genre']));
        $id3->setTag('track', (function_exists('iconv') ? iconv('utf-8', 'windows-1251', $_POST['track']) : $_POST['track']));
        $result = $id3->write();
        if (PEAR::isError($result)) {
            echo "Ошибка при записи тегов";
        } else
            msg('Теги успешно записаны');
    }
}
include 'inc/files/file_scr.php';
include 'inc/files/data_ff.php';
$name_id = (function_exists('iconv') ? iconv('windows-1251', 'utf-8', $id3->getTag('name')) : $id3->getTag('name'));
$artist = (function_exists('iconv') ? iconv('windows-1251', 'utf-8', $id3->getTag('artists')) : $id3->getTag('artists'));
$album = (function_exists('iconv') ? iconv('windows-1251', 'utf-8', $id3->getTag('album')) : $id3->getTag('album'));
$year = (function_exists('iconv') ? iconv('windows-1251', 'utf-8', $id3->getTag('year')) : $id3->getTag('year'));
$comment = (function_exists('iconv') ? iconv('windows-1251', 'utf-8', $id3->getTag('comment')) : $id3->getTag('comment'));
$genre = (function_exists('iconv') ? iconv('windows-1251', 'utf-8', $id3->getTag('genre')) : $id3->getTag('genre'));
$track = (function_exists('iconv') ? iconv('windows-1251', 'utf-8', $id3->getTag('track')) : $id3->getTag('track'));
if (class_exists('ffmpeg_movie')) {
    $media = new ffmpeg_movie($dir_loads . '/' . $file);
    if (intval($media->getDuration()) > 3599)
        $length = intval($media->getDuration() / 3600) . ":" . date('s', fmod($media->getDuration() / 60, 60)) . ":" . date('s', fmod($media->getDuration(), 3600));
    elseif (intval($media->getDuration()) > 59)
        $length = intval($media->getDuration() / 60) . ":" . date('s', fmod($media->getDuration(), 60));
    else
        $length = intval($media->getDuration()) . " сек";
    $bitrate = ceil(($media->getBitRate()) / 1024) . ' KBPS';
}else {
    $length = $id3->getTag('length');
    $bitrate = $id3->getTag('bitrate') . ' KBPS';
}
echo "<div class='list-group-item-null'>";
if ($name_id != null && $name_id != 0) {
    echo "Название: " . toOutput($name_id) . "<br />\n";
}
if ($artist != null && $artist != 0)
    echo "Исполнитель: " . toOutput($artist) . "<br />\n";
if ($album != null && $album != 0)
    echo "Альбом: " . toOutput($album) . "<br />\n";
if ($year != null && $year != 0)
    echo "Год: " . toOutput($year) . "<br />\n";
if ($comment != null && $comment != 0)
    echo "Комментарий: " . toOutput($comment) . "<br />\n";
if ($genre != null && $genre != 0)
    echo "Жанр: " . toOutput($genre) . "<br />\n";
if ($track != null && $track != 0)
    echo "№ трека: " . toOutput($track) . "<br />\n";
if ($length != null)
    echo "Продолжительность: $length<br />\n";
if ($bitrate != null)
    echo "Битрейт: $bitrate<br />\n";

echo "</div>";
include 'inc/files/opis_ff.php';
include 'inc/files/prosm.php';
include 'inc/files/rating.php';
if (isset($_GET['act']) && $_GET['act'] == 'crop') {
    echo "<form method='post' class='list-group-item-null' action='?" . url("d=$l&amp;f=$file") . "&amp;act=cropok'>\n";
    echo 'Способ нарезки:<br />
<select name="way" class="form-control">
<option value="size">По размеру</option>
<option value="time">По времени</option>
</select><br />';
    echo "KB или СЕК:<br />\n";
    echo "от <input type=\"text\" name=\"s\" size='3' value='0' /> до <input type=\"text\" name=\"p\" size='3' value='" . intval(filesize($dir_loads . '/' . $file) / 1024) . "' /><br />\n";
    echo "<input type='submit' class='btn' value='Нарезать' />\n";
    echo "</form>\n";
} elseif (isset($_GET['act']) && $_GET['act'] == 'cropok') {
    $s = $_POST['s'];
    $p = $_POST['p'];
    $fp = fopen($dir_loads . '/' . $file, "rb");
    if ($_POST['way'] == "size") {

        $s = $s * 1024;
        $p = $p * 1024;
        if ($s > $size || $s < 0) {
            $s = 0;
        }
        if ($p > $size || $p < $s) {
            $p = $size;
        }
    } else {
        $byterate = $id3->getTag('bitrate') / 8;
        $secbit = $size / 1024 / $byterate;
        if ($s > $secbit || $s < 0) {
            $s = 0;
        }
        if ($p > $secbit || $p < $s) {
            $p = $secbit;
        }
        $s = $s * $byterate * 1024;
        $p = $p * $byterate * 1024;
    }
    $p = $p - $s;
    fseek($fp, $s);
    $filefp = fread($fp, $p);
    fclose($fp);
    $tempfilename = rand(00000001, 9999999) . '_' . $file;
    $tempfile = H . 'sys/loads/mp3crop/' . $tempfilename;
    $fp = fopen($tempfile, "xb");
    fwrite($fp, $filefp);
    if (function_exists('iconv')) {
        echo "Прослушывание:<br />\n";
    }
    if (IS_WEB) {
        echo '<object type="application/x-shockwave-flash" data="/sys/ump3player_500x70.swf" height="35" width="235"><param name="wmode" VALUE="transparent" /><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="movie" value="/sys/ump3player_500x70.swf" /><param name="FlashVars" value="way=/loads/mp3crop/' . url2("$tempfilename") . '&amp;swf=/sys/ump3player_500x70.swf&amp;w=235&amp;h=35&amp;autoplay=0&amp;q=&amp;skin=white&amp;volume=40&amp;comment=' . $file . '" /></object><br />';
    }
    echo "<a href=\"/loads/mp3crop/$tempfilename\"><b>Скачать нарезку</b></a><br />\n";
    echo "<a href=\"?" . url("d=$l&amp;scr=1&amp;f=$file") . "\">Отмена</a><br />\n";
}
echo "<div class=\"list-group-item-null\">";
echo "<a href=\"?" . url("d=$l&amp;f=$file") . "&amp;act=crop\">Нарезать</a><br />\n";
if (access('loads_file_edit')) {
    if (isset($_GET['act']) && $_GET['act'] == 'edit_tag') {
        echo "<form method='post' action='?" . url("d=$l&amp;f=$file") . "&amp;act=edit_tag_ok'>\n";
        echo "Название:<br />\n";
        echo "<input type='text' class='form-control' name='name' value='" . htmlentities($name_id == 0 ? null : $name_id, ENT_QUOTES, 'UTF-8') . "' />\n";
        echo "Исполнитель:<br />\n";
        echo "<input type='text' class='form-control' name='artists' value='" . htmlentities($artist == 0 ? null : $artist, ENT_QUOTES, 'UTF-8') . "' />\n";
        echo "Альбом:<br />\n";
        echo "<input type='text' class='form-control' name='album' value='" . htmlentities($album == 0 ? null : $album, ENT_QUOTES, 'UTF-8') . "' />\n";
        echo "Год:<br />\n";
        echo "<input type='text' class='form-control' name='year' value='" . htmlentities($year == 0 ? null : $year, ENT_QUOTES, 'UTF-8') . "' />\n";
        echo "Комментарий:<br />\n";
        echo "<textarea class='form-control' name='comment'>" . htmlentities($comment == 0 ? null : $comment, ENT_QUOTES, 'UTF-8') . "</textarea>\n";
        echo "Жанр:<br />\n";
        echo "<input type='text' class='form-control' name='genre' value='" . htmlentities($genre == 0 ? null : $genre, ENT_QUOTES, 'UTF-8') . "' />\n";
        echo "№ трека:<br />\n";
        echo "<input type='text' class='form-control' name='track' value='" . htmlentities($track == 0 ? null : $track, ENT_QUOTES, 'UTF-8') . "' /><br />\n";
        $doc->Button('btn btn-success btn-sm', null, 'save', 'Сохранить');
        $doc->Link('btn btn-secondary btn-sm', "?" . url("d=$l&amp;f=$file") . "", null, 'Отмена');
        echo "</form>\n";
    } else {
        echo "<a href=\"?" . url("d=$l&amp;f=$file") . "&amp;act=edit_tag\">Редактировать теги</a><br />\n";
    }
}
echo "</div>";
include 'inc/add_file_loads.php';
include 'inc/files/komm.php';
?>