PageRenderTime 723ms CodeModel.GetById 161ms app.highlight 344ms RepoModel.GetById 203ms app.codeStats 1ms

/modules/music/class/getid3/getid3.putid3.php

http://nukeviet-music.googlecode.com/
PHP | 1647 lines | 1334 code | 30 blank | 283 comment | 522 complexity | 0070fdbfb91a3b05a06c6bfe9bb99d27 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1<?php
  2////////////////////////////////////////////////////////////
  3/// getID3() by James Heinrich <getid3@silisoftware.com>  //
  4//       available at http://www.silisoftware.com        ///
  5////////////////////////////////////////////////////////////
  6//                                                        //
  7// putid3.php - part of getID3()                          //
  8// See getid3.readme.txt for more details                 //
  9//                                                        //
 10////////////////////////////////////////////////////////////
 11
 12function GenerateID3v2TagFlags($majorversion=4, $Unsynchronisation=FALSE, $Compression=FALSE, $ExtendedHeader=FALSE, $Experimental=FALSE, $Footer=FALSE) {
 13	if ($majorversion == 4) {
 14		// %abcd0000
 15		$flag .= Bool2IntString($Unsynchronisation); // a - Unsynchronisation
 16		$flag .= Bool2IntString($ExtendedHeader);    // b - Extended header
 17		$flag .= Bool2IntString($Experimental);      // c - Experimental indicator
 18		$flag .= Bool2IntString($Footer);            // d - Footer present
 19		$flag .= '0000';
 20	} else if ($majorversion == 3) {
 21		// %abc00000
 22		$flag .= Bool2IntString($Unsynchronisation); // a - Unsynchronisation
 23		$flag .= Bool2IntString($ExtendedHeader);    // b - Extended header
 24		$flag .= Bool2IntString($Experimental);      // c - Experimental indicator
 25		$flag .= '00000';
 26	} else if ($majorversion == 2) {
 27		// %ab000000
 28		$flag .= Bool2IntString($Unsynchronisation); // a - Unsynchronisation
 29		$flag .= Bool2IntString($Compression);       // b - Compression
 30		$flag .= '000000';
 31	} else {
 32		return FALSE;
 33	}
 34	return chr(bindec($flag));
 35}
 36
 37
 38function GenerateID3v2FrameFlags($majorversion=4, $TagAlter=FALSE, $FileAlter=FALSE, $ReadOnly=FALSE, $Compression=FALSE, $Encryption=FALSE, $GroupingIdentity=FALSE, $Unsynchronisation=FALSE, $DataLengthIndicator=FALSE) {
 39	if ($majorversion == 4) {
 40		// %0abc0000 %0h00kmnp
 41
 42		$flag1 .= '0';
 43		$flag1 .= Bool2IntString($TagAlter);  // a - Tag alter preservation (TRUE == discard)
 44		$flag1 .= Bool2IntString($FileAlter); // b - File alter preservation (TRUE == discard)
 45		$flag1 .= Bool2IntString($ReadOnly);  // c - Read only (TRUE == read only)
 46		$flag1 .= '0000';
 47
 48		$flag2 .= '0';
 49		$flag2 .= Bool2IntString($GroupingIdentity);    // h - Grouping identity (TRUE == contains group information)
 50		$flag2 .= '00';
 51		$flag2 .= Bool2IntString($Compression);         // k - Compression (TRUE == compressed)
 52		$flag2 .= Bool2IntString($Encryption);          // m - Encryption (TRUE == encrypted)
 53		$flag2 .= Bool2IntString($Unsynchronisation);   // n - Unsynchronisation (TRUE == unsynchronised)
 54		$flag2 .= Bool2IntString($DataLengthIndicator); // p - Data length indicator (TRUE == data length indicator added)
 55
 56	} else if ($majorversion == 3) {
 57		// %abc00000 %ijk00000
 58
 59		$flag1 .= Bool2IntString($TagAlter);  // a - Tag alter preservation (TRUE == discard)
 60		$flag1 .= Bool2IntString($FileAlter); // b - File alter preservation (TRUE == discard)
 61		$flag1 .= Bool2IntString($ReadOnly);  // c - Read only (TRUE == read only)
 62		$flag1 .= '00000';
 63
 64		$flag2 .= Bool2IntString($Compression);      // i - Compression (TRUE == compressed)
 65		$flag2 .= Bool2IntString($Encryption);       // j - Encryption (TRUE == encrypted)
 66		$flag2 .= Bool2IntString($GroupingIdentity); // k - Grouping identity (TRUE == contains group information)
 67		$flag2 .= '00000';
 68
 69	} else {
 70		return FALSE;
 71	}
 72	return chr(bindec($flag1)).chr(bindec($flag2));
 73}
 74
 75function GenerateID3v2FrameData($frame_name, $frame_data, $majorversion=4, $showerrors=FALSE) {
 76	if (!IsValidID3v2FrameName($frame_name, $majorversion)) {
 77		return FALSE;
 78	}
 79	if ($majorversion == 2) {
 80		ksort($frame_data);
 81		reset($frame_data);
 82		switch ($frame_name) {
 83			case 'TXX':
 84				$error .= $frame_name.' not yet supported in putid3.php<BR>';
 85				break;
 86			case 'WXX':
 87				$error .= $frame_name.' not yet supported in putid3.php<BR>';
 88				break;
 89			case 'IPL':
 90				$error .= $frame_name.' not yet supported in putid3.php<BR>';
 91				break;
 92			case 'MCI':
 93				$error .= $frame_name.' not yet supported in putid3.php<BR>';
 94				break;
 95			case 'ETC':
 96				$error .= $frame_name.' not yet supported in putid3.php<BR>';
 97				break;
 98			case 'MLL':
 99				$error .= $frame_name.' not yet supported in putid3.php<BR>';
100				break;
101			case 'STC':
102				$error .= $frame_name.' not yet supported in putid3.php<BR>';
103				break;
104			case 'ULT':
105				$error .= $frame_name.' not yet supported in putid3.php<BR>';
106				break;
107			case 'SLT':
108				$error .= $frame_name.' not yet supported in putid3.php<BR>';
109				break;
110			case 'COM':
111				$error .= $frame_name.' not yet supported in putid3.php<BR>';
112				break;
113			case 'RVA':
114				$error .= $frame_name.' not yet supported in putid3.php<BR>';
115				break;
116			case 'EQU':
117				$error .= $frame_name.' not yet supported in putid3.php<BR>';
118				break;
119			case 'REV':
120				$error .= $frame_name.' not yet supported in putid3.php<BR>';
121				break;
122			case 'PIC':
123				$error .= $frame_name.' not yet supported in putid3.php<BR>';
124				break;
125			case 'GEO':
126				$error .= $frame_name.' not yet supported in putid3.php<BR>';
127				break;
128			case 'CNT':
129				$error .= $frame_name.' not yet supported in putid3.php<BR>';
130				break;
131			case 'POP':
132				$error .= $frame_name.' not yet supported in putid3.php<BR>';
133				break;
134			case 'BUF':
135				$error .= $frame_name.' not yet supported in putid3.php<BR>';
136				break;
137			case 'CRM':
138				$error .= $frame_name.' not yet supported in putid3.php<BR>';
139				break;
140			case 'CRA':
141				$error .= $frame_name.' not yet supported in putid3.php<BR>';
142				break;
143			case 'LNK':
144				$error .= $frame_name.' not yet supported in putid3.php<BR>';
145				break;
146			default:
147				if ($frame_name{0} == 'T') {
148					// T??
149					$error .= $frame_name.' not yet supported in putid3.php<BR>';
150				} else if ($frame_name{0} == 'W') {
151					// W??
152					$error .= $frame_name.' not yet supported in putid3.php<BR>';
153				} else {
154					$error .= $frame_name.' not yet supported in putid3.php<BR>';
155					return FALSE;
156				}
157		}
158	} else { // $majorversion > 2
159		switch ($frame_name) {
160			case 'UFID':
161				// 4.1   UFID Unique file identifier
162				// Owner identifier        <text string> $00
163				// Identifier              <up to 64 bytes binary data>
164				if (strlen($frame_data['data']) > 64) {
165					$error .= 'Identifier not allowed to be longer than 64 bytes in '.$frame_name.' (supplied data was '.strlen($frame_data['data']).' bytes long)<BR>';
166				} else {
167					$framedata .= str_replace(chr(0), '', $frame_data['ownerid']).chr(0);
168					$framedata .= substr($frame_data['data'], 0, 64); // max 64 bytes - truncate anything longer
169				}
170				break;
171			case 'TXXX':
172				// 4.2.2 TXXX User defined text information frame
173				// Text encoding     $xx
174				// Description       <text string according to encoding> $00 (00)
175				// Value             <text string according to encoding>
176				if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
177					$error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].') for ID3v2.'.$majorversion.'<BR>';
178				} else {
179					$framedata .= chr($frame_data['encodingid']);
180					$framedata .= $frame_data['description'].TextEncodingLookup('terminator', $frame_data['encodingid']);
181					$framedata .= $frame_data['data'];
182				}
183				break;
184			case 'WXXX':
185				// 4.3.2 WXXX User defined URL link frame
186				// Text encoding     $xx
187				// Description       <text string according to encoding> $00 (00)
188				// URL               <text string>
189				if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
190					$error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].') for ID3v2.'.$majorversion.'<BR>';
191				} else if (!IsValidURL($frame_data['url'], FALSE, FALSE)) {
192					$error .= 'Invalid URL in '.$frame_name.' ('.$frame_data['url'].')<BR>';
193				} else {
194					$framedata .= chr($frame_data['encodingid']);
195					$framedata .= $frame_data['description'].TextEncodingLookup('terminator', $frame_data['encodingid']);
196					$framedata .= $frame_data['url'];
197				}
198				break;
199			case 'IPLS':
200				// 4.4  IPLS Involved people list (ID3v2.3 only)
201				// Text encoding     $xx
202				// People list strings    <textstrings>
203				if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
204					$error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].') for ID3v2.'.$majorversion.'<BR>';
205				} else {
206					$framedata .= chr($frame_data['encodingid']);
207					$framedata .= $frame_data['data'];
208				}
209				break;
210			case 'MCDI':
211				// 4.4   MCDI Music CD identifier
212				// CD TOC                <binary data>
213				$framedata .= $frame_data['data'];
214				break;
215			case 'ETCO':
216				// 4.5   ETCO Event timing codes
217				// Time stamp format    $xx
218				//   Where time stamp format is:
219				// $01  (32-bit value) MPEG frames from beginning of file
220				// $02  (32-bit value) milliseconds from beginning of file
221				//   Followed by a list of key events in the following format:
222				// Type of event   $xx
223				// Time stamp      $xx (xx ...)
224				//   The 'Time stamp' is set to zero if directly at the beginning of the sound
225				//   or after the previous event. All events MUST be sorted in chronological order.
226				if (($frame_data['timestampformat'] > 2) || ($frame_data['timestampformat'] < 1)) {
227					$error .= 'Invalid Time Stamp Format byte in '.$frame_name.' ('.$frame_data['timestampformat'].')<BR>';
228				} else {
229					$framedata .= chr($frame_data['timestampformat']);
230					foreach ($frame_data as $key => $val) {
231						if (!IsValidETCOevent($val['typeid'], $majorversion)) {
232							$error .= 'Invalid Event Type byte in '.$frame_name.' ('.$val['typeid'].')<BR>';
233						} else if (($key != 'timestampformat') && ($key != 'flags')) {
234							if (($val['timestamp'] > 0) && ($previousETCOtimestamp >= $val['timestamp'])) {
235								//   The 'Time stamp' is set to zero if directly at the beginning of the sound
236								//   or after the previous event. All events MUST be sorted in chronological order.
237								$error .= 'Out-of-order timestamp in '.$frame_name.' ('.$val['timestamp'].') for Event Type ('.$val['typeid'].')<BR>';
238							} else {
239								$framedata .= chr($val['typeid']);
240								$framedata .= BigEndian2String($val['timestamp'], 4, FALSE);
241							}
242						}
243					}
244				}
245				break;
246			case 'MLLT':
247				// 4.6   MLLT MPEG location lookup table
248				// MPEG frames between reference  $xx xx
249				// Bytes between reference        $xx xx xx
250				// Milliseconds between reference $xx xx xx
251				// Bits for bytes deviation       $xx
252				// Bits for milliseconds dev.     $xx
253				//   Then for every reference the following data is included;
254				// Deviation in bytes         %xxx....
255				// Deviation in milliseconds  %xxx....
256				if (($frame_data['framesbetweenreferences'] > 0) && ($frame_data['framesbetweenreferences'] <= 65535)) {
257					$framedata .= BigEndian2String($frame_data['framesbetweenreferences'], 2, FALSE);
258				} else {
259					$error .= 'Invalid MPEG Frames Between References in '.$frame_name.' ('.$frame_data['framesbetweenreferences'].')<BR>';
260				}
261				if (($frame_data['bytesbetweenreferences'] > 0) && ($frame_data['bytesbetweenreferences'] <= 16777215)) {
262					$framedata .= BigEndian2String($frame_data['bytesbetweenreferences'], 3, FALSE);
263				} else {
264					$error .= 'Invalid bytes Between References in '.$frame_name.' ('.$frame_data['bytesbetweenreferences'].')<BR>';
265				}
266				if (($frame_data['msbetweenreferences'] > 0) && ($frame_data['msbetweenreferences'] <= 16777215)) {
267					$framedata .= BigEndian2String($frame_data['msbetweenreferences'], 3, FALSE);
268				} else {
269					$error .= 'Invalid Milliseconds Between References in '.$frame_name.' ('.$frame_data['msbetweenreferences'].')<BR>';
270				}
271				if (!IsWithinBitRange($frame_data['bitsforbytesdeviation'], 8, FALSE)) {
272					if (($frame_data['bitsforbytesdeviation'] % 4) == 0) {
273						$framedata .= chr($frame_data['bitsforbytesdeviation']);
274					} else {
275						$error .= 'Bits For Bytes Deviation in '.$frame_name.' ('.$frame_data['bitsforbytesdeviation'].') must be a multiple of 4.<BR>';
276					}
277				} else {
278					$error .= 'Invalid Bits For Bytes Deviation in '.$frame_name.' ('.$frame_data['bitsforbytesdeviation'].')<BR>';
279				}
280				if (!IsWithinBitRange($frame_data['bitsformsdeviation'], 8, FALSE)) {
281					if (($frame_data['bitsformsdeviation'] % 4) == 0) {
282						$framedata .= chr($frame_data['bitsformsdeviation']);
283					} else {
284						$error .= 'Bits For Milliseconds Deviation in '.$frame_name.' ('.$frame_data['bitsforbytesdeviation'].') must be a multiple of 4.<BR>';
285					}
286				} else {
287					$error .= 'Invalid Bits For Milliseconds Deviation in '.$frame_name.' ('.$frame_data['bitsformsdeviation'].')<BR>';
288				}
289				foreach ($frame_data as $key => $val) {
290					if (($key != 'framesbetweenreferences') && ($key != 'bytesbetweenreferences') && ($key != 'msbetweenreferences') && ($key != 'bitsforbytesdeviation') && ($key != 'bitsformsdeviation') && ($key != 'flags')) {
291						$unwrittenbitstream .= str_pad(Dec2Bin($val['bytedeviation']), $frame_data['bitsforbytesdeviation'], '0', STR_PAD_LEFT);
292						$unwrittenbitstream .= str_pad(Dec2Bin($val['msdeviation']),   $frame_data['bitsformsdeviation'],    '0', STR_PAD_LEFT);
293					}
294				}
295				for ($i=0;$i<strlen($unwrittenbitstream);$i+=8) {
296					$highnibble = bindec(substr($unwrittenbitstream, $i, 4)) << 4;
297					$lownibble  = bindec(substr($unwrittenbitstream, $i + 4, 4));
298					$framedata .= chr($highnibble & $lownibble);
299				}
300				break;
301			case 'SYTC':
302				// 4.7   SYTC Synchronised tempo codes
303				// Time stamp format   $xx
304				// Tempo data          <binary data>
305				//   Where time stamp format is:
306				// $01  (32-bit value) MPEG frames from beginning of file
307				// $02  (32-bit value) milliseconds from beginning of file
308				if (($frame_data['timestampformat'] > 2) || ($frame_data['timestampformat'] < 1)) {
309					$error .= 'Invalid Time Stamp Format byte in '.$frame_name.' ('.$frame_data['timestampformat'].')<BR>';
310				} else {
311					$framedata .= chr($frame_data['timestampformat']);
312					foreach ($frame_data as $key => $val) {
313						if (!IsValidETCOevent($val['typeid'], $majorversion)) {
314							$error .= 'Invalid Event Type byte in '.$frame_name.' ('.$val['typeid'].')<BR>';
315						} else if (($key != 'timestampformat') && ($key != 'flags')) {
316							if (($val['tempo'] < 0) || ($val['tempo'] > 510)) {
317								$error .= 'Invalid Tempo (max = 510) in '.$frame_name.' ('.$val['tempo'].') at timestamp ('.$val['timestamp'].')<BR>';
318							} else {
319								if ($val['tempo'] > 255) {
320									$framedata .= chr(255);
321									$val['tempo'] -= 255;
322								}
323								$framedata .= chr($val['tempo']);
324								$framedata .= BigEndian2String($val['timestamp'], 4, FALSE);
325							}
326						}
327					}
328				}
329				break;
330			case 'USLT':
331				// 4.8   USLT Unsynchronised lyric/text transcription
332				// Text encoding        $xx
333				// Language             $xx xx xx
334				// Content descriptor   <text string according to encoding> $00 (00)
335				// Lyrics/text          <full text string according to encoding>
336				if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
337					$error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].') for ID3v2.'.$majorversion.'<BR>';
338				} else if (LanguageLookup($frame_data['language'], TRUE) == '') {
339					$error .= 'Invalid Language in '.$frame_name.' ('.$frame_data['language'].')<BR>';
340				} else {
341					$framedata .= chr($frame_data['encodingid']);
342					$framedata .= strtolower($frame_data['language']);
343					$framedata .= $frame_data['description'].TextEncodingLookup('terminator', $frame_data['encodingid']);
344					$framedata .= $frame_data['data'];
345				}
346				break;
347			case 'SYLT':
348				// 4.9   SYLT Synchronised lyric/text
349				// Text encoding        $xx
350				// Language             $xx xx xx
351				// Time stamp format    $xx
352				//   $01  (32-bit value) MPEG frames from beginning of file
353				//   $02  (32-bit value) milliseconds from beginning of file
354				// Content type         $xx
355				// Content descriptor   <text string according to encoding> $00 (00)
356				//   Terminated text to be synced (typically a syllable)
357				//   Sync identifier (terminator to above string)   $00 (00)
358				//   Time stamp                                     $xx (xx ...)
359				if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
360					$error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].') for ID3v2.'.$majorversion.'<BR>';
361				} else if (LanguageLookup($frame_data['language'], TRUE) == '') {
362					$error .= 'Invalid Language in '.$frame_name.' ('.$frame_data['language'].')<BR>';
363				} else if (($frame_data['timestampformat'] > 2) || ($frame_data['timestampformat'] < 1)) {
364					$error .= 'Invalid Time Stamp Format byte in '.$frame_name.' ('.$frame_data['timestampformat'].')<BR>';
365				} else if (!IsValidSYLTtype($frame_data['contenttypeid'], $majorversion)) {
366					$error .= 'Invalid Content Type byte in '.$frame_name.' ('.$frame_data['contenttypeid'].')<BR>';
367				} else if (!is_array($frame_data['data'])) {
368					$error .= 'Invalid Lyric/Timestamp data in '.$frame_name.' (must be an array)<BR>';
369				} else {
370					$framedata .= chr($frame_data['encodingid']);
371					$framedata .= strtolower($frame_data['language']);
372					$framedata .= chr($frame_data['timestampformat']);
373					$framedata .= chr($frame_data['contenttypeid']);
374					$framedata .= $frame_data['description'].TextEncodingLookup('terminator', $frame_data['encodingid']);
375					ksort($frame_data['data']);
376					foreach ($frame_data['data'] as $key => $val) {
377						$framedata .= $val['data'].TextEncodingLookup('terminator', $frame_data['encodingid']);
378						$framedata .= BigEndian2String($val['timestamp'], 4, FALSE);
379					}
380				}
381				break;
382			case 'COMM':
383				// 4.10  COMM Comments
384				// Text encoding          $xx
385				// Language               $xx xx xx
386				// Short content descrip. <text string according to encoding> $00 (00)
387				// The actual text        <full text string according to encoding>
388				if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
389					$error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].') for ID3v2.'.$majorversion.'<BR>';
390				} else if (LanguageLookup($frame_data['language'], TRUE) == '') {
391					$error .= 'Invalid Language in '.$frame_name.' ('.$frame_data['language'].')<BR>';
392				} else {
393					$framedata .= chr($frame_data['encodingid']);
394					$framedata .= strtolower($frame_data['language']);
395					$framedata .= $frame_data['description'].TextEncodingLookup('terminator', $frame_data['encodingid']);
396					$framedata .= $frame_data['data'];
397				}
398				break;
399			case 'RVA2':
400				// 4.11  RVA2 Relative volume adjustment (2) (ID3v2.4+ only)
401				// Identification          <text string> $00
402				//   The 'identification' string is used to identify the situation and/or
403				//   device where this adjustment should apply. The following is then
404				//   repeated for every channel:
405				// Type of channel         $xx
406				// Volume adjustment       $xx xx
407				// Bits representing peak  $xx
408				// Peak volume             $xx (xx ...)
409				$framedata .= str_replace(chr(0), '', $frame_data['description']).chr(0);
410				foreach ($frame_data as $key => $val) {
411					if ($key != 'description') {
412						$framedata .= chr($val['channeltypeid']);
413						$framedata .= substr(str_pad(dechex($val['volumeadjust']), 8, '0', STR_PAD_LEFT), 4, 4); // signed 16-bit
414						if (!IsWithinBitRange($frame_data['bitspeakvolume'], 8, FALSE)) {
415							$framedata .= chr($val['bitspeakvolume']);
416							if ($val['bitspeakvolume'] > 0) {
417								$framedata .= BigEndian2String($val['peakvolume'], ceil($val['bitspeakvolume'] / 8), FALSE);
418							}
419						} else {
420							$error .= 'Invalid Bits Representing Peak Volume in '.$frame_name.' ('.$val['bitspeakvolume'].') (range = 0 to 255)<BR>';
421						}
422					}
423				}
424				break;
425			case 'RVAD':
426				// 4.12  RVAD Relative volume adjustment (ID3v2.3 only)
427				// Increment/decrement     %00fedcba
428				// Bits used for volume descr.        $xx
429				// Relative volume change, right      $xx xx (xx ...) // a
430				// Relative volume change, left       $xx xx (xx ...) // b
431				// Peak volume right                  $xx xx (xx ...)
432				// Peak volume left                   $xx xx (xx ...)
433				// Relative volume change, right back $xx xx (xx ...) // c
434				// Relative volume change, left back  $xx xx (xx ...) // d
435				// Peak volume right back             $xx xx (xx ...)
436				// Peak volume left back              $xx xx (xx ...)
437				// Relative volume change, center     $xx xx (xx ...) // e
438				// Peak volume center                 $xx xx (xx ...)
439				// Relative volume change, bass       $xx xx (xx ...) // f
440				// Peak volume bass                   $xx xx (xx ...)
441				if (!IsWithinBitRange($frame_data['bitsvolume'], 8, FALSE)) {
442					$error .= 'Invalid Bits For Volume Description byte in '.$frame_name.' ('.$frame_data['bitsvolume'].') (range = 1 to 255)<BR>';
443				} else {
444					$incdecflag .= '00';
445					$incdecflag .= Bool2IntString($frame_data['incdec']['right']);     // a - Relative volume change, right
446					$incdecflag .= Bool2IntString($frame_data['incdec']['left']);      // b - Relative volume change, left
447					$incdecflag .= Bool2IntString($frame_data['incdec']['rightrear']); // c - Relative volume change, right back
448					$incdecflag .= Bool2IntString($frame_data['incdec']['leftrear']);  // d - Relative volume change, left back
449					$incdecflag .= Bool2IntString($frame_data['incdec']['center']);    // e - Relative volume change, center
450					$incdecflag .= Bool2IntString($frame_data['incdec']['bass']);      // f - Relative volume change, bass
451					$framedata .= chr(bindec($incdecflag));
452					$framedata .= chr($frame_data['bitsvolume']);
453					$framedata .= BigEndian2String($frame_data['volumechange']['right'], ceil($frame_data['bitsvolume'] / 8), FALSE);
454					$framedata .= BigEndian2String($frame_data['volumechange']['left'],  ceil($frame_data['bitsvolume'] / 8), FALSE);
455					$framedata .= BigEndian2String($frame_data['peakvolume']['right'], ceil($frame_data['bitsvolume'] / 8), FALSE);
456					$framedata .= BigEndian2String($frame_data['peakvolume']['left'],  ceil($frame_data['bitsvolume'] / 8), FALSE);
457					if ($frame_data['volumechange']['rightrear'] || $frame_data['volumechange']['leftrear'] ||
458						$frame_data['peakvolume']['rightrear'] || $frame_data['peakvolume']['leftrear'] ||
459						$frame_data['volumechange']['center'] || $frame_data['peakvolume']['center'] ||
460						$frame_data['volumechange']['bass'] || $frame_data['peakvolume']['bass']) {
461							$framedata .= BigEndian2String($frame_data['volumechange']['rightrear'], ceil($frame_data['bitsvolume']/8), FALSE);
462							$framedata .= BigEndian2String($frame_data['volumechange']['leftrear'],  ceil($frame_data['bitsvolume']/8), FALSE);
463							$framedata .= BigEndian2String($frame_data['peakvolume']['rightrear'], ceil($frame_data['bitsvolume']/8), FALSE);
464							$framedata .= BigEndian2String($frame_data['peakvolume']['leftrear'],  ceil($frame_data['bitsvolume']/8), FALSE);
465					}
466					if ($frame_data['volumechange']['center'] || $frame_data['peakvolume']['center'] ||
467						$frame_data['volumechange']['bass'] || $frame_data['peakvolume']['bass']) {
468							$framedata .= BigEndian2String($frame_data['volumechange']['center'], ceil($frame_data['bitsvolume']/8), FALSE);
469							$framedata .= BigEndian2String($frame_data['peakvolume']['center'], ceil($frame_data['bitsvolume']/8), FALSE);
470					}
471					if ($frame_data['volumechange']['bass'] || $frame_data['peakvolume']['bass']) {
472							$framedata .= BigEndian2String($frame_data['volumechange']['bass'], ceil($frame_data['bitsvolume']/8), FALSE);
473							$framedata .= BigEndian2String($frame_data['peakvolume']['bass'], ceil($frame_data['bitsvolume']/8), FALSE);
474					}
475				}
476				break;
477			case 'EQU2':
478				// 4.12  EQU2 Equalisation (2) (ID3v2.4+ only)
479				// Interpolation method  $xx
480				//   $00  Band
481				//   $01  Linear
482				// Identification        <text string> $00
483				//   The following is then repeated for every adjustment point
484				// Frequency          $xx xx
485				// Volume adjustment  $xx xx
486				if (($frame_data['interpolationmethod'] < 0) || ($frame_data['interpolationmethod'] > 1)) {
487					$error .= 'Invalid Interpolation Method byte in '.$frame_name.' ('.$frame_data['interpolationmethod'].') (valid = 0 or 1)<BR>';
488				} else {
489					$framedata .= chr($frame_data['interpolationmethod']);
490					$framedata .= str_replace(chr(0), '', $frame_data['description']).chr(0);
491					foreach ($frame_data['data'] as $key => $val) {
492						$framedata .= BigEndian2String(round($key * 2), 2, FALSE);
493						$framedata .= substr(str_pad(dechex($val), 4, '0', STR_PAD_LEFT), 4, 4); // signed 16-bit
494					}
495				}
496				break;
497			case 'EQUA':
498				// 4.12  EQUA Equalisation (ID3v2.3 only)
499				// Adjustment bits    $xx
500				//   This is followed by 2 bytes + ('adjustment bits' rounded up to the
501				//   nearest byte) for every equalisation band in the following format,
502				//   giving a frequency range of 0 - 32767Hz:
503				// Increment/decrement   %x (MSB of the Frequency)
504				// Frequency             (lower 15 bits)
505				// Adjustment            $xx (xx ...)
506				if (!IsWithinBitRange($frame_data['bitsvolume'], 8, FALSE)) {
507					$error .= 'Invalid Adjustment Bits byte in '.$frame_name.' ('.$frame_data['bitsvolume'].') (range = 1 to 255)<BR>';
508				} else {
509					$framedata .= chr($frame_data['adjustmentbits']);
510					foreach ($frame_data as $key => $val) {
511						if ($key != 'bitsvolume') {
512							if (($key > 32767) || ($key < 0)) {
513								$error .= 'Invalid Frequency in '.$frame_name.' ('.$key.') (range = 0 to 32767)<BR>';
514							} else {
515								if ($val >= 0) {
516									// put MSB of frequency to 1 if increment, 0 if decrement
517									$key |= 0x8000;
518								}
519								$framedata .= BigEndian2String($key, 2, FALSE);
520								$framedata .= BigEndian2String($val, ceil($frame_data['adjustmentbits'] / 8), FALSE);
521							}
522						}
523					}
524				}
525				break;
526			case 'RVRB':
527				// 4.13  RVRB Reverb
528				// Reverb left (ms)                 $xx xx
529				// Reverb right (ms)                $xx xx
530				// Reverb bounces, left             $xx
531				// Reverb bounces, right            $xx
532				// Reverb feedback, left to left    $xx
533				// Reverb feedback, left to right   $xx
534				// Reverb feedback, right to right  $xx
535				// Reverb feedback, right to left   $xx
536				// Premix left to right             $xx
537				// Premix right to left             $xx
538				if (!IsWithinBitRange($frame_data['left'], 16, FALSE)) {
539					$error .= 'Invalid Reverb Left in '.$frame_name.' ('.$frame_data['left'].') (range = 0 to 65535)<BR>';
540				} else if (!IsWithinBitRange($frame_data['right'], 16, FALSE)) {
541					$error .= 'Invalid Reverb Left in '.$frame_name.' ('.$frame_data['right'].') (range = 0 to 65535)<BR>';
542				} else if (!IsWithinBitRange($frame_data['bouncesL'], 8, FALSE)) {
543					$error .= 'Invalid Reverb Bounces, Left in '.$frame_name.' ('.$frame_data['bouncesL'].') (range = 0 to 255)<BR>';
544				} else if (!IsWithinBitRange($frame_data['bouncesR'], 8, FALSE)) {
545					$error .= 'Invalid Reverb Bounces, Right in '.$frame_name.' ('.$frame_data['bouncesR'].') (range = 0 to 255)<BR>';
546				} else if (!IsWithinBitRange($frame_data['feedbackLL'], 8, FALSE)) {
547					$error .= 'Invalid Reverb Feedback, Left-To-Left in '.$frame_name.' ('.$frame_data['feedbackLL'].') (range = 0 to 255)<BR>';
548				} else if (!IsWithinBitRange($frame_data['feedbackLR'], 8, FALSE)) {
549					$error .= 'Invalid Reverb Feedback, Left-To-Right in '.$frame_name.' ('.$frame_data['feedbackLR'].') (range = 0 to 255)<BR>';
550				} else if (!IsWithinBitRange($frame_data['feedbackRR'], 8, FALSE)) {
551					$error .= 'Invalid Reverb Feedback, Right-To-Right in '.$frame_name.' ('.$frame_data['feedbackRR'].') (range = 0 to 255)<BR>';
552				} else if (!IsWithinBitRange($frame_data['feedbackRL'], 8, FALSE)) {
553					$error .= 'Invalid Reverb Feedback, Right-To-Left in '.$frame_name.' ('.$frame_data['feedbackRL'].') (range = 0 to 255)<BR>';
554				} else if (!IsWithinBitRange($frame_data['premixLR'], 8, FALSE)) {
555					$error .= 'Invalid Premix, Left-To-Right in '.$frame_name.' ('.$frame_data['premixLR'].') (range = 0 to 255)<BR>';
556				} else if (!IsWithinBitRange($frame_data['premixRL'], 8, FALSE)) {
557					$error .= 'Invalid Premix, Right-To-Left in '.$frame_name.' ('.$frame_data['premixRL'].') (range = 0 to 255)<BR>';
558				} else {
559					$framedata .= BigEndian2String($frame_data['left'], 2, FALSE);
560					$framedata .= BigEndian2String($frame_data['right'], 2, FALSE);
561					$framedata .= chr($frame_data['bouncesL']);
562					$framedata .= chr($frame_data['bouncesR']);
563					$framedata .= chr($frame_data['feedbackLL']);
564					$framedata .= chr($frame_data['feedbackLR']);
565					$framedata .= chr($frame_data['feedbackRR']);
566					$framedata .= chr($frame_data['feedbackRL']);
567					$framedata .= chr($frame_data['premixLR']);
568					$framedata .= chr($frame_data['premixRL']);
569				}
570				break;
571			case 'APIC':
572				// 4.14  APIC Attached picture
573				// Text encoding      $xx
574				// MIME type          <text string> $00
575				// Picture type       $xx
576				// Description        <text string according to encoding> $00 (00)
577				// Picture data       <binary data>
578				if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
579					$error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].') for ID3v2.'.$majorversion.'<BR>';
580				} else if (!IsValidAPICpicturetype($frame_data['picturetypeid'], $majorversion)) {
581					$error .= 'Invalid Picture Type byte in '.$frame_name.' ('.$frame_data['picturetypeid'].') for ID3v2.'.$majorversion.'<BR>';
582				} else if (($majorversion >= 3) && (!IsValidAPICimageformat($frame_data['mime'], $majorversion))) {
583					$error .= 'Invalid MIME Type in '.$frame_name.' ('.$frame_data['mime'].') for ID3v2.'.$majorversion.'<BR>';
584				} else if (($frame_data['mime'] == '-->') && (!IsValidURL($frame_data['data'], FALSE, FALSE))) {
585					$error .= 'Invalid URL in '.$frame_name.' ('.$frame_data['data'].')<BR>';
586				} else {
587					$framedata .= chr($frame_data['encodingid']);
588					$framedata .= str_replace(chr(0), '', $frame_data['mime']).chr(0);
589					$framedata .= chr($frame_data['picturetypeid']);
590					$framedata .= $frame_data['description'].TextEncodingLookup('terminator', $frame_data['encodingid']);
591					$framedata .= $frame_data['data'];
592				}
593				break;
594			case 'GEOB':
595				// 4.15  GEOB General encapsulated object
596				// Text encoding          $xx
597				// MIME type              <text string> $00
598				// Filename               <text string according to encoding> $00 (00)
599				// Content description    <text string according to encoding> $00 (00)
600				// Encapsulated object    <binary data>
601				if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
602					$error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].') for ID3v2.'.$majorversion.'<BR>';
603				} else if (!IsValidMIMEstring($frame_data['mime'])) {
604					$error .= 'Invalid MIME Type in '.$frame_name.' ('.$frame_data['mime'].')<BR>';
605				} else if (!$frame_data['description']) {
606					$error .= 'Missing Description in '.$frame_name.'<BR>';
607				} else {
608					$framedata .= chr($frame_data['encodingid']);
609					$framedata .= str_replace(chr(0), '', $frame_data['mime']).chr(0);
610					$framedata .= $frame_data['filename'].TextEncodingLookup('terminator', $frame_data['encodingid']);
611					$framedata .= $frame_data['description'].TextEncodingLookup('terminator', $frame_data['encodingid']);
612					$framedata .= $frame_data['data'];
613				}
614				break;
615			case 'PCNT':
616				// 4.16  PCNT Play counter
617				//   When the counter reaches all one's, one byte is inserted in
618				//   front of the counter thus making the counter eight bits bigger
619				// Counter        $xx xx xx xx (xx ...)
620				$framedata .= BigEndian2String($frame_data['data'], 4, FALSE);
621				break;
622			case 'POPM':
623				// 4.17  POPM Popularimeter
624				//   When the counter reaches all one's, one byte is inserted in
625				//   front of the counter thus making the counter eight bits bigger
626				// Email to user   <text string> $00
627				// Rating          $xx
628				// Counter         $xx xx xx xx (xx ...)
629				if (!IsWithinBitRange($frame_data['rating'], 8, FALSE)) {
630					$error .= 'Invalid Rating byte in '.$frame_name.' ('.$frame_data['rating'].') (range = 0 to 255)<BR>';
631				} else if (!IsValidEmail($frame_data['email'])) {
632					$error .= 'Invalid Email in '.$frame_name.' ('.$frame_data['email'].')<BR>';
633				} else {
634					$framedata .= str_replace(chr(0), '', $frame_data['email']).chr(0);
635					$framedata .= chr($frame_data['rating']);
636					$framedata .= BigEndian2String($frame_data['data'], 4, FALSE);
637				}
638				break;
639			case 'RBUF':
640				// 4.18  RBUF Recommended buffer size
641				// Buffer size               $xx xx xx
642				// Embedded info flag        %0000000x
643				// Offset to next tag        $xx xx xx xx
644				if (!IsWithinBitRange($frame_data['buffersize'], 24, FALSE)) {
645					$error .= 'Invalid Buffer Size in '.$frame_name.'<BR>';
646				} else if (!IsWithinBitRange($frame_data['nexttagoffset'], 32, FALSE)) {
647					$error .= 'Invalid Offset To Next Tag in '.$frame_name.'<BR>';
648				} else {
649					$framedata .= BigEndian2String($frame_data['buffersize'], 3, FALSE);
650					$flag .= '0000000';
651					$flag .= Bool2IntString($frame_data['flags']['embededinfo']);
652					$framedata .= chr(bindec($flag));
653					$framedata .= BigEndian2String($frame_data['nexttagoffset'], 4, FALSE);
654				}
655				break;
656			case 'AENC':
657				// 4.19  AENC Audio encryption
658				// Owner identifier   <text string> $00
659				// Preview start      $xx xx
660				// Preview length     $xx xx
661				// Encryption info    <binary data>
662				if (!IsWithinBitRange($frame_data['previewstart'], 16, FALSE)) {
663					$error .= 'Invalid Preview Start in '.$frame_name.' ('.$frame_data['previewstart'].')<BR>';
664				} else if (!IsWithinBitRange($frame_data['previewlength'], 16, FALSE)) {
665					$error .= 'Invalid Preview Length in '.$frame_name.' ('.$frame_data['previewlength'].')<BR>';
666				} else {
667					$framedata .= str_replace(chr(0), '', $frame_data['ownerid']).chr(0);
668					$framedata .= BigEndian2String($frame_data['previewstart'], 2, FALSE);
669					$framedata .= BigEndian2String($frame_data['previewlength'], 2, FALSE);
670					$framedata .= $frame_data['encryptioninfo'];
671				}
672				break;
673			case 'LINK':
674				// 4.20  LINK Linked information
675				// Frame identifier               $xx xx xx xx
676				// URL                            <text string> $00
677				// ID and additional data         <text string(s)>
678				if (!IsValidID3v2FrameName($frame_data['frameid'], $majorversion)) {
679					$error .= 'Invalid Frame Identifier in '.$frame_name.' ('.$frame_data['frameid'].')<BR>';
680				} else if (!IsValidURL($frame_data['url'], TRUE, FALSE)) {
681					$error .= 'Invalid URL in '.$frame_name.' ('.$frame_data['url'].')<BR>';
682				} else if ((($frame_data['frameid'] == 'AENC') || ($frame_data['frameid'] == 'APIC') || ($frame_data['frameid'] == 'GEOB') || ($frame_data['frameid'] == 'TXXX')) && ($frame_data['additionaldata'] == '')) {
683					$error .= 'Content Descriptor must be specified as additional data for Frame Identifier of '.$frame_data['frameid'].' in '.$frame_name.'<BR>';
684				} else if (($frame_data['frameid'] == 'USER') && (LanguageLookup($frame_data['additionaldata'], TRUE) == '')) {
685					$error .= 'Language must be specified as additional data for Frame Identifier of '.$frame_data['frameid'].' in '.$frame_name.'<BR>';
686				} else if (($frame_data['frameid'] == 'PRIV') && ($frame_data['additionaldata'] == '')) {
687					$error .= 'Owner Identifier must be specified as additional data for Frame Identifier of '.$frame_data['frameid'].' in '.$frame_name.'<BR>';
688				} else if ((($frame_data['frameid'] == 'COMM') || ($frame_data['frameid'] == 'SYLT') || ($frame_data['frameid'] == 'USLT')) && ((LanguageLookup(substr($frame_data['additionaldata'], 0, 3), TRUE) == '') || (substr($frame_data['additionaldata'], 3) == ''))) {
689					$error .= 'Language followed by Content Descriptor must be specified as additional data for Frame Identifier of '.$frame_data['frameid'].' in '.$frame_name.'<BR>';
690				} else {
691					$framedata .= $frame_data['frameid'];
692					$framedata .= str_replace(chr(0), '', $frame_data['url']).chr(0);
693					switch ($frame_data['frameid']) {
694						case 'COMM':
695						case 'SYLT':
696						case 'USLT':
697						case 'PRIV':
698						case 'USER':
699						case 'AENC':
700						case 'APIC':
701						case 'GEOB':
702						case 'TXXX':
703							$framedata .= $frame_data['additionaldata'];
704							break;
705						case 'ASPI':
706						case 'ETCO':
707						case 'EQU2':
708						case 'MCID':
709						case 'MLLT':
710						case 'OWNE':
711						case 'RVA2':
712						case 'RVRB':
713						case 'SYTC':
714						case 'IPLS':
715						case 'RVAD':
716						case 'EQUA':
717							// no additional data required
718							break;
719						case 'RBUF':
720							if ($majorversion == 3) {
721								// no additional data required
722							} else {
723								$error .= $frame_data['frameid'].' is not a valid Frame Identifier in '.$frame_name.' (in ID3v2.'.$majorversion.')<BR>';
724							}
725
726						default:
727							if ((substr($frame_data['frameid'], 0, 1) == 'T') || (substr($frame_data['frameid'], 0, 1) == 'W')) {
728								// no additional data required
729							} else {
730								$error .= $frame_data['frameid'].' is not a valid Frame Identifier in '.$frame_name.' (in ID3v2.'.$majorversion.')<BR>';
731							}
732							break;
733					}
734				}
735				break;
736			case 'POSS':
737				// 4.21  POSS Position synchronisation frame (ID3v2.3+ only)
738				// Time stamp format         $xx
739				// Position                  $xx (xx ...)
740				if (($frame_data['timestampformat'] < 1) || ($frame_data['timestampformat'] > 2)) {
741					$error .= 'Invalid Time Stamp Format in '.$frame_name.' ('.$frame_data['timestampformat'].') (valid = 1 or 2)<BR>';
742				} else if (!IsWithinBitRange($frame_data['position'], 32, FALSE)) {
743					$error .= 'Invalid Position in '.$frame_name.' ('.$frame_data['position'].') (range = 0 to 4294967295)<BR>';
744				} else {
745					$framedata .= chr($frame_data['timestampformat']);
746					$framedata .= BigEndian2String($frame_data['position'], 4, FALSE);
747				}
748				break;
749			case 'USER':
750				// 4.22  USER Terms of use (ID3v2.3+ only)
751				// Text encoding        $xx
752				// Language             $xx xx xx
753				// The actual text      <text string according to encoding>
754				if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
755					$error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].')<BR>';
756				} else if (LanguageLookup($frame_data['language'], TRUE) == '') {
757					$error .= 'Invalid Language in '.$frame_name.' ('.$frame_data['language'].')<BR>';
758				} else {
759					$framedata .= chr($frame_data['encodingid']);
760					$framedata .= strtolower($frame_data['language']);
761					$framedata .= $frame_data['data'];
762				}
763				break;
764			case 'OWNE':
765				// 4.23  OWNE Ownership frame (ID3v2.3+ only)
766				// Text encoding     $xx
767				// Price paid        <text string> $00
768				// Date of purch.    <text string>
769				// Seller            <text string according to encoding>
770				if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
771					$error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].')<BR>';
772				} else if (!IsANumber($frame_data['pricepaid']['value'], FALSE)) {
773					$error .= 'Invalid Price Paid in '.$frame_name.' ('.$frame_data['pricepaid']['value'].')<BR>';
774				} else if (!IsValidDateStampString($frame_data['purchasedate'])) {
775					$error .= 'Invalid Date Of Purchase in '.$frame_name.' ('.$frame_data['purchasedate'].') (format = YYYYMMDD)<BR>';
776				} else {
777					$framedata .= chr($frame_data['encodingid']);
778					$framedata .= str_replace(chr(0), '', $frame_data['pricepaid']['value']).chr(0);
779					$framedata .= $frame_data['purchasedate'];
780					$framedata .= $frame_data['seller'];
781				}
782				break;
783			case 'COMR':
784				// 4.24  COMR Commercial frame (ID3v2.3+ only)
785				// Text encoding      $xx
786				// Price string       <text string> $00
787				// Valid until        <text string>
788				// Contact URL        <text string> $00
789				// Received as        $xx
790				// Name of seller     <text string according to encoding> $00 (00)
791				// Description        <text string according to encoding> $00 (00)
792				// Picture MIME type  <string> $00
793				// Seller logo        <binary data>
794				if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
795					$error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].')<BR>';
796				} else if (!IsValidDateStampString($frame_data['pricevaliduntil'])) {
797					$error .= 'Invalid Valid Until date in '.$frame_name.' ('.$frame_data['pricevaliduntil'].') (format = YYYYMMDD)<BR>';
798				} else if (!IsValidURL($frame_data['contacturl'], FALSE, TRUE)) {
799					$error .= 'Invalid Contact URL in '.$frame_name.' ('.$frame_data['contacturl'].') (allowed schemes: http, https, ftp, mailto)<BR>';
800				} else if (!IsValidCOMRreceivedas($frame_data['receivedasid'], $majorversion)) {
801					$error .= 'Invalid Received As byte in '.$frame_name.' ('.$frame_data['contacturl'].') (range = 0 to 8)<BR>';
802				} else if (!IsValidMIMEstring($frame_data['mime'])) {
803					$error .= 'Invalid MIME Type in '.$frame_name.' ('.$frame_data['mime'].')<BR>';
804				} else {
805					$framedata .= chr($frame_data['encodingid']);
806					unset($pricestring);
807					foreach ($frame_data['price'] as $key => $val) {
808						if (IsValidPriceString($key.$val['value'])) {
809							$pricestrings[] = $key.$val['value'];
810						} else {
811							$error .= 'Invalid Price String in '.$frame_name.' ('.$key.$val['value'].')<BR>';
812						}
813					}
814					$framedata .= implode('/', $pricestrings);
815					$framedata .= $frame_data['pricevaliduntil'];
816					$framedata .= str_replace(chr(0), '', $frame_data['contacturl']).chr(0);
817					$framedata .= chr($frame_data['receivedasid']);
818					$framedata .= $frame_data['sellername'].TextEncodingLookup('terminator', $frame_data['encodingid']);
819					$framedata .= $frame_data['description'].TextEncodingLookup('terminator', $frame_data['encodingid']);
820					$framedata .= $frame_data['mime'].chr(0);
821					$framedata .= $frame_data['logo'];
822				}
823				break;
824			case 'ENCR':
825				// 4.25  ENCR Encryption method registration (ID3v2.3+ only)
826				// Owner identifier    <text string> $00
827				// Method symbol       $xx
828				// Encryption data     <binary data>
829				if (!IsWithinBitRange($frame_data['methodsymbol'], 8, FALSE)) {
830					$error .= 'Invalid Group Symbol in '.$frame_name.' ('.$frame_data['methodsymbol'].') (range = 0 to 255)<BR>';
831				} else {
832					$framedata .= str_replace(chr(0), '', $frame_data['ownerid']).chr(0);
833					$framedata .= ord($frame_data['methodsymbol']);
834					$framedata .= $frame_data['data'];
835				}
836				break;
837			case 'GRID':
838				// 4.26  GRID Group identification registration (ID3v2.3+ only)
839				// Owner identifier      <text string> $00
840				// Group symbol          $xx
841				// Group dependent data  <binary data>
842				if (!IsWithinBitRange($frame_data['groupsymbol'], 8, FALSE)) {
843					$error .= 'Invalid Group Symbol in '.$frame_name.' ('.$frame_data['groupsymbol'].') (range = 0 to 255)<BR>';
844				} else {
845					$framedata .= str_replace(chr(0), '', $frame_data['ownerid']).chr(0);
846					$framedata .= ord($frame_data['groupsymbol']);
847					$framedata .= $frame_data['data'];
848				}
849				break;
850			case 'PRIV':
851				// 4.27  PRIV Private frame (ID3v2.3+ only)
852				// Owner identifier      <text string> $00
853				// The private data      <binary data>
854				$framedata .= str_replace(chr(0), '', $frame_data['ownerid']).chr(0);
855				$framedata .= $frame_data['data'];
856				break;
857			case 'SIGN':
858				// 4.28  SIGN Signature frame (ID3v2.4+ only)
859				// Group symbol      $xx
860				// Signature         <binary data>
861				if (!IsWithinBitRange($frame_data['groupsymbol'], 8, FALSE)) {
862					$error .= 'Invalid Group Symbol in '.$frame_name.' ('.$frame_data['groupsymbol'].') (range = 0 to 255)<BR>';
863				} else {
864					$framedata .= ord($frame_data['groupsymbol']);
865					$framedata .= $frame_data['data'];
866				}
867				break;
868			case 'SEEK':
869				// 4.29  SEEK Seek frame (ID3v2.4+ only)
870				// Minimum offset to next tag       $xx xx xx xx
871				if (!IsWithinBitRange($frame_data['data'], 32, FALSE)) {
872					$error .= 'Invalid Minimum Offset in '.$frame_name.' ('.$frame_data['data'].') (range = 0 to 4294967295)<BR>';
873				} else {
874					$framedata .= BigEndian2String($frame_data['data'], 4, FALSE);
875				}
876				break;
877			case 'ASPI':
878				// 4.30  ASPI Audio seek point index (ID3v2.4+ only)
879				// Indexed data start (S)         $xx xx xx xx
880				// Indexed data length (L)        $xx xx xx xx
881				// Number of index points (N)     $xx xx
882				// Bits per index point (b)       $xx
883				//   Then for every index point the following data is included:
884				// Fraction at index (Fi)          $xx (xx)
885				if (!IsWithinBitRange($frame_data['datastart'], 32, FALSE)) {
886					$error .= 'Invalid Indexed Data Start in '.$frame_name.' ('.$frame_data['datastart'].') (range = 0 to 4294967295)<BR>';
887				} else if (!IsWithinBitRange($frame_data['datalength'], 32, FALSE)) {
888					$error .= 'Invalid Indexed Data Length in '.$frame_name.' ('.$frame_data['datalength'].') (range = 0 to 4294967295)<BR>';
889				} else if (!IsWithinBitRange($frame_data['indexpoints'], 16, FALSE)) {
890					$error .= 'Invalid Number Of Index Points in '.$frame_name.' ('.$frame_data['indexpoints'].') (range = 0 to 65535)<BR>';
891				} else if (!IsWithinBitRange($frame_data['bitsperpoint'], 8, FALSE)) {
892					$error .= 'Invalid Bits Per Index Point in '.$frame_name.' ('.$frame_data['bitsperpoint'].') (range = 0 to 255)<BR>';
893				} else if ($frame_data['indexpoints'] != count($frame_data['indexes'])) {
894					$error .= 'Number Of Index Points does not match actual supplied data in '.$frame_name.'<BR>';
895				} else {
896					$framedata .= BigEndian2String($frame_data['datastart'], 4, FALSE);
897					$framedata .= BigEndian2String($frame_data['datalength'], 4, FALSE);
898					$framedata .= BigEndian2String($frame_data['indexpoints'], 2, FALSE);
899					$framedata .= BigEndian2String($frame_data['bitsperpoint'], 1, FALSE);
900					foreach ($frame_data['indexes'] as $key => $val) {
901						$framedata .= BigEndian2String($val, ceil($frame_data['bitsperpoint'] / 8), FALSE);
902					}
903				}
904				break;
905			case 'RGAD':
906				//   RGAD Replay Gain Adjustment
907				//   http://privatewww.essex.ac.uk/~djmrob/replaygain/
908				// Peak Amplitude                     $xx $xx $xx $xx
909				// Radio Replay Gain Adjustment        %aaabbbcd %dddddddd
910				// Audiophile Replay Gain Adjustment   %aaabbbcd %dddddddd
911				//   a - name code
912				//   b - originator code
913				//   c - sign bit
914				//   d - replay gain adjustment
915
916				if (($frame_data['radio_adjustment'] > 51) || ($frame_data['radio_adjustment'] < -51)) {
917					$error .= 'Invalid Radio Adjustment in '.$frame_name.' ('.$frame_data['radio_adjustment'].') (range = -51.0 to +51.0)<BR>';
918				} else if (($frame_data['audiophile_adjustment'] > 51) || ($frame_data['audiophile_adjustment'] < -51)) {
919					$error .= 'Invalid Audiophile Adjustment in '.$frame_name.' ('.$frame_data['audiophile_adjustment'].') (range = -51.0 to +51.0)<BR>';
920				} else if (!IsValidRGADname($frame_data['raw']['radio_name'], $majorversion)) {
921					$error .= 'Invalid Radio Name Code in '.$frame_name.' ('.$frame_data['raw']['radio_name'].') (range = 0 to 2)<BR>';
922				} else if (!IsValidRGADname($frame_data['raw']['audiophile_name'], $majorversion)) {
923					$error .= 'Invalid Audiophile Name Code in '.$frame_name.' ('.$frame_data['raw']['audiophile_name'].') (range = 0 to 2)<BR>';
924				} else if (!IsValidRGADoriginator($frame_data['raw']['radio_originator'], $majorversion)) {
925					$error .= 'Invalid Radio Originator Code in '.$frame_name.' ('.$frame_data['raw']['radio_originator'].') (range = 0 to 3)<BR>';
926				} else if (!IsValidRGADoriginator($frame_data['raw']['audiophile_originator'], $majorversion)) {
927					$error .= 'Invalid Audiophile Originator Code in '.$frame_name.' ('.$frame_data['raw']['audiophile_originator'].') (range = 0 to 3)<BR>';
928				} else {
929					$framedata .= Float2String($frame_data['peakamplitude'], 32);
930					$framedata .= RGADgainString($frame_data['raw']['radio_name'], $frame_data['raw']['radio_originator'], $frame_data['radio_adjustment']);
931					$framedata .= RGADgainString($frame_data['raw']['audiophile_name'], $frame_data['raw']['audiophile_originator'], $frame_data['audiophile_adjustment']);
932				}
933				break;
934			default:
935				if ($frame_name{0} == 'T') {
936					// 4.2. T???  Text information frames
937					// Text encoding                $xx
938					// Information                  <text string(s) according to encoding>
939					if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
940						$error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].') for ID3v2.'.$majorversion.'<BR>';
941					} else {
942						$framedata .= chr($frame_data['encodingid']);
943						$framedata .= $frame_data['data'];
944					}
945				} else if ($frame_name{0} == 'W') {
946					// 4.3. W???  URL link frames
947					// URL              <text string>
948					if (!IsValidURL($frame_data['url'], FALSE, FALSE)) {
949						$error .= 'Invalid URL in '.$frame_name.' ('.$frame_data['url'].')<BR>';
950					} else {
951						$framedata .= $frame_data['url'];
952					}
953				} else {
954					$error .= $frame_name.' not yet supported in putid3.php<BR>';
955				}
956				break;
957		}
958	}
959	if ($error) {
960		if ($showerrors) {
961			echo $error;
962		}
963		return FALSE;
964	} else {
965		return $framedata;
966	}
967}
968
969function ID3v2FrameIsAllowed($frame_name, $frame_data, $majorversion, $showerrors=FALSE) {
970	static $PreviousFrames = array();
971	
972	if ($frame_name === NULL) {
973		// if the writing functions are called multiple times, the static array needs to be
974		// cleared - this can be done by calling ID3v2FrameIsAllowed(NULL, '', '')
975		$PreviousFrames = array();
976		return TRUE;
977	}
978
979	if ($majorversion == 4) {
980		switch ($frame_name) {
981			case

Large files files are truncated, but you can click here to view the full file