1<?php 2///////////////////////////////////////////////////////////////// 3/// getID3() by James Heinrich <info@getid3.org> // 4// available at https://github.com/JamesHeinrich/getID3 // 5// or https://www.getid3.org // 6// or http://getid3.sourceforge.net // 7// // 8// /demo/demo.write.php - part of getID3() // 9// sample script for demonstrating writing ID3v1 and ID3v2 // 10// tags for MP3, or Ogg comment tags for Ogg Vorbis // 11// see readme.txt for more details // 12// /// 13///////////////////////////////////////////////////////////////// 14 15 16die('For security reasons, this demo has been disabled. It can be enabled by removing line '.__LINE__.' in demos/'.basename(__FILE__)); 17 18$TaggingFormat = 'UTF-8'; 19 20header('Content-Type: text/html; charset='.$TaggingFormat); 21echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'; 22echo '<html><head><title>getID3() - Sample tag writer</title></head><style type="text/css">BODY,TD,TH { font-family: sans-serif; font-size: 9pt;" }</style><body>'; 23 24require_once('../getid3/getid3.php'); 25// Initialize getID3 engine 26$getID3 = new getID3; 27$getID3->setOption(array('encoding'=>$TaggingFormat)); 28 29getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'write.php', __FILE__, true); 30 31$browsescriptfilename = 'demo.browse.php'; 32 33$Filename = (isset($_REQUEST['Filename']) ? $_REQUEST['Filename'] : ''); 34 35 36 37if (isset($_POST['WriteTags'])) { 38 39 $TagFormatsToWrite = (isset($_POST['TagFormatsToWrite']) ? $_POST['TagFormatsToWrite'] : array()); 40 if (!empty($TagFormatsToWrite)) { 41 echo 'starting to write tag(s)<BR>'; 42 43 $tagwriter = new getid3_writetags; 44 $tagwriter->filename = $Filename; 45 $tagwriter->tagformats = $TagFormatsToWrite; 46 $tagwriter->overwrite_tags = false; 47 $tagwriter->tag_encoding = $TaggingFormat; 48 if (!empty($_POST['remove_other_tags'])) { 49 $tagwriter->remove_other_tags = true; 50 } 51 52 $commonkeysarray = array('Title', 'Artist', 'Album', 'Year', 'Comment'); 53 foreach ($commonkeysarray as $key) { 54 if (!empty($_POST[$key])) { 55 $TagData[strtolower($key)][] = $_POST[$key]; 56 } 57 } 58 if (!empty($_POST['Genre'])) { 59 $TagData['genre'][] = $_POST['Genre']; 60 } 61 if (!empty($_POST['GenreOther'])) { 62 $TagData['genre'][] = $_POST['GenreOther']; 63 } 64 if (!empty($_POST['Track'])) { 65 $TagData['track_number'][] = $_POST['Track'].(!empty($_POST['TracksTotal']) ? '/'.$_POST['TracksTotal'] : ''); 66 } 67 68 if (!empty($_FILES['userfile']['tmp_name'])) { 69 if (in_array('id3v2.4', $tagwriter->tagformats) || in_array('id3v2.3', $tagwriter->tagformats) || in_array('id3v2.2', $tagwriter->tagformats)) { 70 if (is_uploaded_file($_FILES['userfile']['tmp_name'])) { 71 if ($APICdata = file_get_contents($_FILES['userfile']['tmp_name'])) { 72 73 if ($exif_imagetype = exif_imagetype($_FILES['userfile']['tmp_name'])) { 74 75 $TagData['attached_picture'][0]['data'] = $APICdata; 76 $TagData['attached_picture'][0]['picturetypeid'] = $_POST['APICpictureType']; 77 $TagData['attached_picture'][0]['description'] = $_FILES['userfile']['name']; 78 $TagData['attached_picture'][0]['mime'] = image_type_to_mime_type($exif_imagetype); 79 80 } else { 81 echo '<b>invalid image format (only GIF, JPEG, PNG)</b><br>'; 82 } 83 } else { 84 echo '<b>cannot open '.htmlentities($_FILES['userfile']['tmp_name']).'</b><br>'; 85 } 86 } else { 87 echo '<b>!is_uploaded_file('.htmlentities($_FILES['userfile']['tmp_name']).')</b><br>'; 88 } 89 } else { 90 echo '<b>WARNING:</b> Can only embed images for ID3v2<br>'; 91 } 92 } 93 94 $tagwriter->tag_data = $TagData; 95 if ($tagwriter->WriteTags()) { 96 echo 'Successfully wrote tags<BR>'; 97 if (!empty($tagwriter->warnings)) { 98 echo 'There were some warnings:<blockquote style="background-color: #FFCC33; padding: 10px;">'.implode('<br><br>', $tagwriter->warnings).'</div>'; 99 } 100 } else { 101 echo 'Failed to write tags!<div style="background-color: #FF9999; padding: 10px;">'.implode('<br><br>', $tagwriter->errors).'</div>'; 102 } 103 104 } else { 105 106 echo 'WARNING: no tag formats selected for writing - nothing written'; 107 108 } 109 echo '<HR>'; 110 111} 112 113 114echo '<div style="font-size: 1.2em; font-weight: bold;">Sample tag editor/writer</div>'; 115echo '<a href="'.htmlentities($browsescriptfilename.'?listdirectory='.rawurlencode(realpath(dirname($Filename))), ENT_QUOTES).'">Browse current directory</a><br>'; 116if (!empty($Filename)) { 117 echo '<a href="'.htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES).'">Start Over</a><br><br>'; 118 echo '<form action="'.htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES).'" method="post" enctype="multipart/form-data">'; 119 echo '<table border="3" cellspacing="0" cellpadding="4">'; 120 echo '<tr><th align="right">Filename:</th><td><input type="hidden" name="Filename" value="'.htmlentities($Filename, ENT_QUOTES).'"><a href="'.htmlentities($browsescriptfilename.'?filename='.rawurlencode($Filename), ENT_QUOTES).'" target="_blank">'.$Filename.'</a></td></tr>'; 121 if (file_exists($Filename)) { 122 123 // Initialize getID3 engine 124 $getID3 = new getID3; 125 $OldThisFileInfo = $getID3->analyze($Filename); 126 getid3_lib::CopyTagsToComments($OldThisFileInfo); 127 128 switch ($OldThisFileInfo['fileformat']) { 129 case 'mp3': 130 case 'mp2': 131 case 'mp1': 132 $ValidTagTypes = array('id3v1', 'id3v2.3', 'ape'); 133 break; 134 135 case 'mpc': 136 $ValidTagTypes = array('ape'); 137 break; 138 139 case 'ogg': 140 if (!empty($OldThisFileInfo['audio']['dataformat']) && ($OldThisFileInfo['audio']['dataformat'] == 'flac')) { 141 //$ValidTagTypes = array('metaflac'); 142 // metaflac doesn't (yet) work with OggFLAC files 143 $ValidTagTypes = array(); 144 } else { 145 $ValidTagTypes = array('vorbiscomment'); 146 } 147 break; 148 149 case 'flac': 150 $ValidTagTypes = array('metaflac'); 151 break; 152 153 case 'real': 154 $ValidTagTypes = array('real'); 155 break; 156 157 default: 158 $ValidTagTypes = array(); 159 break; 160 } 161 echo '<tr><td align="right"><b>Title</b></td> <td><input type="text" size="40" name="Title" value="'.htmlentities((!empty($OldThisFileInfo['comments']['title']) ? implode(', ', $OldThisFileInfo['comments']['title'] ) : ''), ENT_QUOTES).'"></td></tr>'; 162 echo '<tr><td align="right"><b>Artist</b></td><td><input type="text" size="40" name="Artist" value="'.htmlentities((!empty($OldThisFileInfo['comments']['artist']) ? implode(', ', $OldThisFileInfo['comments']['artist']) : ''), ENT_QUOTES).'"></td></tr>'; 163 echo '<tr><td align="right"><b>Album</b></td> <td><input type="text" size="40" name="Album" value="'.htmlentities((!empty($OldThisFileInfo['comments']['album']) ? implode(', ', $OldThisFileInfo['comments']['album'] ) : ''), ENT_QUOTES).'"></td></tr>'; 164 echo '<tr><td align="right"><b>Year</b></td> <td><input type="text" size="4" name="Year" value="'.htmlentities((!empty($OldThisFileInfo['comments']['year']) ? implode(', ', $OldThisFileInfo['comments']['year'] ) : ''), ENT_QUOTES).'"></td></tr>'; 165 166 $TracksTotal = ''; 167 $TrackNumber = ''; 168 if (!empty($OldThisFileInfo['comments']['track_number']) && is_array($OldThisFileInfo['comments']['track_number'])) { 169 $RawTrackNumberArray = $OldThisFileInfo['comments']['track_number']; 170 } elseif (!empty($OldThisFileInfo['comments']['track_number']) && is_array($OldThisFileInfo['comments']['track_number'])) { 171 $RawTrackNumberArray = $OldThisFileInfo['comments']['track_number']; 172 } else { 173 $RawTrackNumberArray = array(); 174 } 175 foreach ($RawTrackNumberArray as $key => $value) { 176 if (strlen($value) > strlen($TrackNumber)) { 177 // ID3v1 may store track as "3" but ID3v2/APE would store as "03/16" 178 $TrackNumber = $value; 179 } 180 } 181 if (strstr($TrackNumber, '/')) { 182 list($TrackNumber, $TracksTotal) = explode('/', $TrackNumber); 183 } 184 echo '<tr><td align="right"><b>Track</b></td><td><input type="text" size="2" name="Track" value="'.htmlentities($TrackNumber, ENT_QUOTES).'"> of <input type="text" size="2" name="TracksTotal" value="'.htmlentities($TracksTotal, ENT_QUOTES).'"></TD></TR>'; 185 186 $ArrayOfGenresTemp = getid3_id3v1::ArrayOfGenres(); // get the array of genres 187 foreach ($ArrayOfGenresTemp as $key => $value) { // change keys to match displayed value 188 $ArrayOfGenres[$value] = $value; 189 } 190 unset($ArrayOfGenresTemp); // remove temporary array 191 unset($ArrayOfGenres['Cover']); // take off these special cases 192 unset($ArrayOfGenres['Remix']); 193 unset($ArrayOfGenres['Unknown']); 194 $ArrayOfGenres[''] = '- Unknown -'; // Add special cases back in with renamed key/value 195 $ArrayOfGenres['Cover'] = '-Cover-'; 196 $ArrayOfGenres['Remix'] = '-Remix-'; 197 asort($ArrayOfGenres); // sort into alphabetical order 198 echo '<tr><th align="right">Genre</th><td><select name="Genre">'; 199 $AllGenresArray = (!empty($OldThisFileInfo['comments']['genre']) ? $OldThisFileInfo['comments']['genre'] : array()); 200 foreach ($ArrayOfGenres as $key => $value) { 201 echo '<option value="'.htmlentities($key, ENT_QUOTES).'"'; 202 if (in_array($key, $AllGenresArray)) { 203 echo ' selected="selected"'; 204 unset($AllGenresArray[array_search($key, $AllGenresArray)]); 205 sort($AllGenresArray); 206 } 207 echo '>'.htmlentities($value).'</option>'; 208 } 209 echo '</select><input type="text" name="GenreOther" size="10" value="'.htmlentities((!empty($AllGenresArray[0]) ? $AllGenresArray[0] : ''), ENT_QUOTES).'"></td></tr>'; 210 211 echo '<tr><td align="right"><b>Write Tags</b></td><td>'; 212 foreach ($ValidTagTypes as $ValidTagType) { 213 echo '<input type="checkbox" name="TagFormatsToWrite[]" value="'.$ValidTagType.'"'; 214 if (count($ValidTagTypes) == 1) { 215 echo ' checked="checked"'; 216 } else { 217 switch ($ValidTagType) { 218 case 'id3v2.2': 219 case 'id3v2.3': 220 case 'id3v2.4': 221 if (isset($OldThisFileInfo['tags']['id3v2'])) { 222 echo ' checked="checked"'; 223 } 224 break; 225 226 default: 227 if (isset($OldThisFileInfo['tags'][$ValidTagType])) { 228 echo ' checked="checked"'; 229 } 230 break; 231 } 232 } 233 echo '>'.$ValidTagType.'<br>'; 234 } 235 if (count($ValidTagTypes) > 1) { 236 echo '<hr><input type="checkbox" name="remove_other_tags" value="1"> Remove non-selected tag formats when writing new tag<br>'; 237 } 238 echo '</td></tr>'; 239 240 echo '<tr><td align="right"><b>Comment</b></td><td><textarea cols="30" rows="3" name="Comment" wrap="virtual">'.((isset($OldThisFileInfo['comments']['comment']) && is_array($OldThisFileInfo['comments']['comment'])) ? implode("\n", $OldThisFileInfo['comments']['comment']) : '').'</textarea></td></tr>'; 241 242 echo '<tr><td align="right"><b>Picture</b><br>(ID3v2 only)</td><td><input type="file" name="userfile" accept="image/jpeg, image/gif, image/png"><br>'; 243 echo '<select name="APICpictureType">'; 244 $APICtypes = getid3_id3v2::APICPictureTypeLookup('', true); 245 foreach ($APICtypes as $key => $value) { 246 echo '<option value="'.htmlentities($key, ENT_QUOTES).'">'.htmlentities($value).'</option>'; 247 } 248 echo '</select></td></tr>'; 249 echo '<tr><td align="center" colspan="2"><input type="submit" name="WriteTags" value="Save Changes"> '; 250 echo '<input type="reset" value="Reset"></td></tr>'; 251 252 } else { 253 254 echo '<tr><td align="right"><b>Error</b></td><td>'.htmlentities($Filename).' does not exist</td></tr>'; 255 256 } 257 echo '</table>'; 258 echo '</form>'; 259 260} 261 262echo '</body></html>'; 263