<?php

/*
Скрипт загруз центра для JohnCMS
Автор: Максим (simba)
ICQ: 61590077
Сайт: http://symbos.su
R866920725287
Z117468354234
*/

/*
Редактор тегов by Koenig
*/

define('_IN_JOHNCMS', 1);
$headmod = 'Tags editor';
$textl = 'Teglarni o\'zgartirish';
require_once '../incfiles/core.php';
require_once '../incfiles/head.php';
require_once 'functions.php';

if ($rights < 7) {
die('Access denyed');
}

spl_autoload_register('dnlautoload');
function dnlautoload($class) {
$file = 'lib' . DIRECTORY_SEPARATOR . 'getid3' . DIRECTORY_SEPARATOR . strtolower($class) . '.php';
if (file_exists($file)) {
require_once($file);
} else {
die('Klass - ' . $class . ' topilmadi!');
}
}

$TaggingFormat = 'UTF-8';

#require_once('../getid3/getid3.php');

$getID3 = new getID3;
$getID3->setOption(array('encoding'=>$TaggingFormat));
getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'write.php', __FILE__, true);

$Filename = isset($_GET['file']) ? $_GET['file'] : die('Error');

if (isset($_POST['WriteTags'])) {

$TagFormatsToWrite = (isset($_POST['TagFormatsToWrite']) ? $_POST['TagFormatsToWrite'] : array());
if (!empty($TagFormatsToWrite)) {
$tagwriter = new getid3_writetags;
$tagwriter->filename       = $Filename;
$tagwriter->tagformats     = $TagFormatsToWrite;
$tagwriter->overwrite_tags = true;
$tagwriter->tag_encoding   = $TaggingFormat;
if (!empty($_POST['remove_other_tags'])) {
$tagwriter->remove_other_tags = true;
}

$commonkeysarray = array('Title', 'Artist', 'Album', 'Year', 'Comment');
foreach ($commonkeysarray as $key) {
if (!empty($_POST[$key])) {
$TagData[strtolower($key)][] = $_POST[$key];
}
}
if (!empty($_POST['Genre'])) {
$TagData['genre'][] = $_POST['Genre'];
}
if (!empty($_POST['GenreOther'])) {
$TagData['genre'][] = $_POST['GenreOther'];
}
if (!empty($_POST['Track'])) {
$TagData['track'][] = $_POST['Track'].(!empty($_POST['TracksTotal']) ? '/'.$_POST['TracksTotal'] : '');
}

if (!empty($_FILES['userfile']['tmp_name'])) {
if (in_array('id3v2.4', $tagwriter->tagformats) || in_array('id3v2.3', $tagwriter->tagformats) || in_array('id3v2.2', $tagwriter->tagformats)) {
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
ob_start();
if ($fd = fopen($_FILES['userfile']['tmp_name'], 'rb')) {
ob_end_clean();
$APICdata = fread($fd, filesize($_FILES['userfile']['tmp_name']));
fclose ($fd);

list($APIC_width, $APIC_height, $APIC_imageTypeID) = GetImageSize($_FILES['userfile']['tmp_name']);
$imagetypes = array(1=>'gif', 2=>'jpeg', 3=>'png');
if (isset($imagetypes[$APIC_imageTypeID])) {

$TagData['attached_picture'][0]['data']          = $APICdata;
$TagData['attached_picture'][0]['picturetypeid'] = $_POST['APICpictureType'];
$TagData['attached_picture'][0]['description']   = $_FILES['userfile']['name'];
$TagData['attached_picture'][0]['mime']          = 'image/'.$imagetypes[$APIC_imageTypeID];

} else {
echo '<b>invalid image format (only GIF, JPEG, PNG)</b><br>';
}
} else {
$errormessage = ob_get_contents();
ob_end_clean();
echo '<b>cannot open '.$_FILES['userfile']['tmp_name'].'</b><br>';
}
} else {
echo '<b>!is_uploaded_file('.$_FILES['userfile']['tmp_name'].')</b><br>';
}
} else {
echo '<b>Ogohlantirish:</b> Can only embed images for ID3v2<br>';
}
}

$tagwriter->tag_data = $TagData;
if ($tagwriter->WriteTags()) {
echo '<div class="menu"><center><font color="red"><b>Teglar muvaffaqiyatli saqlanadi!</font></b></center></div>';
if (!empty($tagwriter->warnings)) {
echo functions::display_error(implode('<br><br>', $tagwriter->warnings));
}
} else {
echo functions::display_error(implode('<br><br>', $tagwriter->errors));
}

} else {

echo '<div>Format mavjud emas!</div>';

}
echo '<hr />';

}


echo '<div class="phdr"><center>Teglarni o\'zgartirishga hush kelibsiz! Fanat_FCB aka!!!</center></div>';
if (!empty($Filename)) {
echo '<div><form action="?file=' . $Filename . '" method="post" enctype="multipart/form-data">';
#echo '<table border="3" cellspacing="0" cellpadding="4">';
if (file_exists($Filename)) {
// Initialize getID3 engine
$getID3 = new getID3;
$OldThisFileInfo = $getID3->analyze($Filename);
getid3_lib::CopyTagsToComments($OldThisFileInfo);

switch ($OldThisFileInfo['fileformat']) {
case 'mp3':
$ValidTagTypes = array('id3v1', 'id3v2.3');
break;
default:
$ValidTagTypes = array();
break;
}
echo '<div class="menu"><b>Nomi:</b><br/><input type="text" size="40" name="Title"  value="' . str_replace('_', ' ', $namee[0]) . ' [Yoqimli.Uz]">';
echo '<br/><b>Ijrochi:</b><br/><input type="text" size="40" name="Artist" value="">';
echo '<br/><b>Albom:</b><br/><input type="text" size="40" name="Album"  value="[YOQIMLI.UZ]">';
echo '<br/><b>Yil</b><br/><input type="text" size="4"  name="Year"   value="'.htmlentities((!empty($OldThisFileInfo['comments']['year'])   ? implode(', ', $OldThisFileInfo['comments']['year']  ) : ''), ENT_QUOTES).'"></div><hr/>';

$TracksTotal = '';
$TrackNumber = '';
if (!empty($OldThisFileInfo['comments']['track_number']) && is_array($OldThisFileInfo['comments']['track_number'])) {
$RawTrackNumberArray = $OldThisFileInfo['comments']['track_number'];
} elseif (!empty($OldThisFileInfo['comments']['track']) && is_array($OldThisFileInfo['comments']['track'])) {
$RawTrackNumberArray = $OldThisFileInfo['comments']['track'];
} else {
$RawTrackNumberArray = array();
}
foreach ($RawTrackNumberArray as $key => $value) {
if (strlen($value) > strlen($TrackNumber)) {
// ID3v1 may store track as "3" but ID3v2/APE would store as "03/16"
$TrackNumber = $value;
}
}
if (strstr($TrackNumber, '/')) {
list($TrackNumber, $TracksTotal) = explode('/', $TrackNumber);
}
echo '<div class="menu"><b>Yillar</b><br/><input type="text" size="2" name="Track" value="'.htmlentities($TrackNumber, ENT_QUOTES).'"> из <input type="text" size="2" name="TracksTotal" value="'.htmlentities($TracksTotal, ENT_QUOTES).'">';

$ArrayOfGenresTemp = getid3_id3v1::ArrayOfGenres();   // get the array of genres
foreach ($ArrayOfGenresTemp as $key => $value) {      // change keys to match displayed value
$ArrayOfGenres[$value] = $value;
}
unset($ArrayOfGenresTemp);                            // remove temporary array
unset($ArrayOfGenres['Cover']);                       // take off these special cases
unset($ArrayOfGenres['Remix']);
unset($ArrayOfGenres['Unknown']);
$ArrayOfGenres['']      = '- Unknown -';              // Add special cases back in with renamed key/value
$ArrayOfGenres['Cover'] = '-Cover-';
$ArrayOfGenres['Remix'] = '-Remix-';
asort($ArrayOfGenres);                                // sort into alphabetical order
echo '<div><b>Qaysi yo`nalishda</b></div><div><select name="Genre">';
$AllGenresArray = (!empty($OldThisFileInfo['comments']['genre']) ? $OldThisFileInfo['comments']['genre'] : array());
foreach ($ArrayOfGenres as $key => $value) {
echo '<option value="'.htmlentities($key, ENT_QUOTES).'"';
if (in_array($key, $AllGenresArray)) {
echo ' selected="selected"';
unset($AllGenresArray[array_search($key, $AllGenresArray)]);
sort($AllGenresArray);
}
echo '>'.htmlentities($value).'</option>';
}
echo '</select></div><div><input type="text" name="GenreOther" size="10" value="'.htmlentities((!empty($AllGenresArray[0]) ? $AllGenresArray[0] : ''), ENT_QUOTES).'"></div>';

echo '<div><b>Kirish teglar</b></div><div>';
foreach ($ValidTagTypes as $ValidTagType) {
echo '<div><input type="checkbox" name="TagFormatsToWrite[]" value="'.$ValidTagType.'"';
if (count($ValidTagTypes) == 1) {
echo ' checked="checked"';
} else {
switch ($ValidTagType) {
case 'id3v2.2':
case 'id3v2.3':
case 'id3v2.4':
if (isset($OldThisFileInfo['tags']['id3v2'])) {
echo ' checked="checked"';
}
break;

default:
if (isset($OldThisFileInfo['tags'][$ValidTagType])) {
echo ' checked="checked"';
}
break;
}
}
echo '>'.$ValidTagType.'</div>';
}
if (count($ValidTagTypes) > 1) {
echo '<hr /><div><input type="checkbox" name="remove_other_tags" value="1">Belgilanmagan versiya teglarini o`chirish</div>';
}

echo '<div><b>Muhokamasi:</b></div><div><textarea cols="30" rows="3" name="Comment">Eng yangi orginal mp3lar bizda Yoqimli.Uz da!</textarea></div>';
echo '<div><b>Rasm manzilini ko`chirib oling:</b></div><div><input type="text" size="40" name="Album"  value="http://yoqimli.uz/download/cover.jpg"></div></div>';
echo '<div><b>Rasm qo`yish:</b></div><div><b>(ID3v2 only)</b></div><div><input type="file" name="userfile" accept="image/jpeg, image/gif, image/png"></div>';
echo '<div><select name="APICpictureType">';
$APICtypes = getid3_id3v2::APICPictureTypeLookup('', true);
foreach ($APICtypes as $key => $value) {
echo '<option value="'.htmlentities($key, ENT_QUOTES).'">'.htmlentities($value).'</option>';
}
echo '</select></div><br /><div><img id="FileImage" src="data:' . $OldThisFileInfo['comments']['picture'][0]['image_mime'] . ' ;charset=utf-8;base64,' . chunk_split(base64_encode($OldThisFileInfo['comments']['picture'][0]['data'])) . '"></div><br />';
echo '<div><input type="submit" name="WriteTags" value="Saqlash"></div>';

} else {

echo '<div><b>Xatolik</b></div><div>'.htmlentities($Filename).' Fayl mavjud emas</div>';

}
echo '</form></div>';

}

require_once '../incfiles/end.php';
?>
