<?php
$url128kbps = $_POST['url128kbps'];
$url320kbps = $_POST['url320kbps'];
$image = $_POST['image'];
$src = trim($image);
$artist = $_POST['en_artist'];
$song = $_POST['en_title'];
$mp3_name = $_POST['mp3_name'];
$dir_file = $_POST['dir_file'];

if ( ! file_exists($dir_file))
mkdir($dir_file, 0755, true);

$GenerateTrackLink320 = $_SERVER['DOCUMENT_ROOT'] . "/{$dir_file}/{$mp3_name}.mp3";
$GenerateTrackLink128 = $_SERVER['DOCUMENT_ROOT'] . "/{$dir_file}/{$mp3_name} (128).mp3";
$mysrc = $_SERVER['DOCUMENT_ROOT'] . "/{$dir_file}/{$mp3_name}.jpg";



function MP3_Id_tags($dirfile, $artist, $song, $axahang){
$TextEncoding = 'UTF-8';
$albumPath = $axahang; // path to your image
require_once('getid3/getid3.php');
// Initialize getID3 engine
$getID3 = new getID3;
$getID3->setOption(array('encoding'=>$TextEncoding));
require_once('getid3/write.php');
// Initialize getID3 tag-writing module
$tagwriter = new getid3_writetags;
//$tagwriter->filename = '/path/to/file.mp3';
$tagwriter->filename = $dirfile;
//$tagwriter->tagformats = array('id3v1', 'id3v2.3');
$tagwriter->tagformats = array('id3v2.3');
// set various options (optional)
$tagwriter->overwrite_tags    = true;  // if true will erase existing tag data and write only passed data; if false will merge passed data with existing tag data (experimental)
$tagwriter->remove_other_tags = false; // if true removes other tag formats (e.g. ID3v1, ID3v2, APE, Lyrics3, etc) that may be present in the file and only write the specified tag format(s). If false leaves any unspecified tag formats as-is.
$tagwriter->tag_encoding      = $TextEncoding;
$tagwriter->remove_other_tags = true;
// populate data array
$TagData = array(
	'title'                  => array($song. ' [ ShirazSong.Org ]'),
	'artist'                 => array($artist.' [ ShirazSong.Org ]'),
	'album'                  => array($song. ' (Single)'),
	'year'                   => array( date( 'Y' ) ),
	'genre'                  => array('Persian'),
	'encoded_by'             => array('[ ShirazSong.Org ]'),
	'comment'                => array('[ ShirazSong.Org ]'),
	'publisher'        => array( '[ ShirazSong.Org ]' ),
	'subtitle'        => array( '[ ShirazSong.Org ]' ),
	'track_number'           => array('0'),
	'popularimeter'          => array('email'=>'[ ShirazSong.Org ]', 'rating'=>255, 'data'=>0),
	'unique_file_identifier' => array('ownerid'=>'[ ShirazSong.Org ]', 'data'=>md5(time())),
);
$fd = fopen($albumPath, 'rb');
$APICdata = fread($fd, filesize($albumPath));
fclose($fd);
if($APICdata) {
    $TagData += array(
        'attached_picture' => array(0 => array(
            'data'          => $APICdata,
            'picturetypeid' => 2,
            'description'   => 'cover',
            'mime'          => 'image/jpeg'
        ))
    );
}
$tagwriter->tag_data = $TagData;
if ($tagwriter->WriteTags()) {
    echo 'Successfully wrote tags<br>';
    if (!empty($tagwriter->warnings)) {
        echo 'There were some warnings:<br>'.implode('<br><br>', $tagwriter->warnings);
    }
} else {
    echo 'Failed to write tags!<br>'.implode('<br><br>', $tagwriter->errors);
}
}

function Download($urldl, $saveto) {
	$windl = curl_init($urldl);
	$fpdl = fopen($saveto, 'wb');
	curl_setopt($windl, CURLOPT_FILE, $fpdl);
	curl_setopt($windl, CURLOPT_HEADER, 0);
	curl_exec($windl);
	curl_close($windl);
	fclose($fpdl);
}


Download($src,$mysrc);  
if (!empty($url320kbps)) Download($url320kbps, $GenerateTrackLink320);
if (!empty($url128kbps)) Download($url128kbps, $GenerateTrackLink128);
if (!empty($url320kbps)) MP3_Id_tags($GenerateTrackLink320, $artist, $song,$mysrc);
if (!empty($url128kbps)) MP3_Id_tags($GenerateTrackLink128, $artist, $song,$mysrc);
unlink($mysrc);
?>