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