demo.write.php 11 KB

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