PageRenderTime 64ms CodeModel.GetById 28ms RepoModel.GetById 0ms 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
  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. function GenerateID3v2TagFlags($majorversion=4, $Unsynchronisation=FALSE, $Compression=FALSE, $ExtendedHeader=FALSE, $Experimental=FALSE, $Footer=FALSE) {
  12. if ($majorversion == 4) {
  13. // %abcd0000
  14. $flag .= Bool2IntString($Unsynchronisation); // a - Unsynchronisation
  15. $flag .= Bool2IntString($ExtendedHeader); // b - Extended header
  16. $flag .= Bool2IntString($Experimental); // c - Experimental indicator
  17. $flag .= Bool2IntString($Footer); // d - Footer present
  18. $flag .= '0000';
  19. } else if ($majorversion == 3) {
  20. // %abc00000
  21. $flag .= Bool2IntString($Unsynchronisation); // a - Unsynchronisation
  22. $flag .= Bool2IntString($ExtendedHeader); // b - Extended header
  23. $flag .= Bool2IntString($Experimental); // c - Experimental indicator
  24. $flag .= '00000';
  25. } else if ($majorversion == 2) {
  26. // %ab000000
  27. $flag .= Bool2IntString($Unsynchronisation); // a - Unsynchronisation
  28. $flag .= Bool2IntString($Compression); // b - Compression
  29. $flag .= '000000';
  30. } else {
  31. return FALSE;
  32. }
  33. return chr(bindec($flag));
  34. }
  35. function GenerateID3v2FrameFlags($majorversion=4, $TagAlter=FALSE, $FileAlter=FALSE, $ReadOnly=FALSE, $Compression=FALSE, $Encryption=FALSE, $GroupingIdentity=FALSE, $Unsynchronisation=FALSE, $DataLengthIndicator=FALSE) {
  36. if ($majorversion == 4) {
  37. // %0abc0000 %0h00kmnp
  38. $flag1 .= '0';
  39. $flag1 .= Bool2IntString($TagAlter); // a - Tag alter preservation (TRUE == discard)
  40. $flag1 .= Bool2IntString($FileAlter); // b - File alter preservation (TRUE == discard)
  41. $flag1 .= Bool2IntString($ReadOnly); // c - Read only (TRUE == read only)
  42. $flag1 .= '0000';
  43. $flag2 .= '0';
  44. $flag2 .= Bool2IntString($GroupingIdentity); // h - Grouping identity (TRUE == contains group information)
  45. $flag2 .= '00';
  46. $flag2 .= Bool2IntString($Compression); // k - Compression (TRUE == compressed)
  47. $flag2 .= Bool2IntString($Encryption); // m - Encryption (TRUE == encrypted)
  48. $flag2 .= Bool2IntString($Unsynchronisation); // n - Unsynchronisation (TRUE == unsynchronised)
  49. $flag2 .= Bool2IntString($DataLengthIndicator); // p - Data length indicator (TRUE == data length indicator added)
  50. } else if ($majorversion == 3) {
  51. // %abc00000 %ijk00000
  52. $flag1 .= Bool2IntString($TagAlter); // a - Tag alter preservation (TRUE == discard)
  53. $flag1 .= Bool2IntString($FileAlter); // b - File alter preservation (TRUE == discard)
  54. $flag1 .= Bool2IntString($ReadOnly); // c - Read only (TRUE == read only)
  55. $flag1 .= '00000';
  56. $flag2 .= Bool2IntString($Compression); // i - Compression (TRUE == compressed)
  57. $flag2 .= Bool2IntString($Encryption); // j - Encryption (TRUE == encrypted)
  58. $flag2 .= Bool2IntString($GroupingIdentity); // k - Grouping identity (TRUE == contains group information)
  59. $flag2 .= '00000';
  60. } else {
  61. return FALSE;
  62. }
  63. return chr(bindec($flag1)).chr(bindec($flag2));
  64. }
  65. function GenerateID3v2FrameData($frame_name, $frame_data, $majorversion=4, $showerrors=FALSE) {
  66. if (!IsValidID3v2FrameName($frame_name, $majorversion)) {
  67. return FALSE;
  68. }
  69. if ($majorversion == 2) {
  70. ksort($frame_data);
  71. reset($frame_data);
  72. switch ($frame_name) {
  73. case 'TXX':
  74. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  75. break;
  76. case 'WXX':
  77. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  78. break;
  79. case 'IPL':
  80. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  81. break;
  82. case 'MCI':
  83. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  84. break;
  85. case 'ETC':
  86. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  87. break;
  88. case 'MLL':
  89. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  90. break;
  91. case 'STC':
  92. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  93. break;
  94. case 'ULT':
  95. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  96. break;
  97. case 'SLT':
  98. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  99. break;
  100. case 'COM':
  101. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  102. break;
  103. case 'RVA':
  104. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  105. break;
  106. case 'EQU':
  107. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  108. break;
  109. case 'REV':
  110. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  111. break;
  112. case 'PIC':
  113. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  114. break;
  115. case 'GEO':
  116. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  117. break;
  118. case 'CNT':
  119. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  120. break;
  121. case 'POP':
  122. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  123. break;
  124. case 'BUF':
  125. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  126. break;
  127. case 'CRM':
  128. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  129. break;
  130. case 'CRA':
  131. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  132. break;
  133. case 'LNK':
  134. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  135. break;
  136. default:
  137. if ($frame_name{0} == 'T') {
  138. // T??
  139. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  140. } else if ($frame_name{0} == 'W') {
  141. // W??
  142. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  143. } else {
  144. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  145. return FALSE;
  146. }
  147. }
  148. } else { // $majorversion > 2
  149. switch ($frame_name) {
  150. case 'UFID':
  151. // 4.1 UFID Unique file identifier
  152. // Owner identifier <text string> $00
  153. // Identifier <up to 64 bytes binary data>
  154. if (strlen($frame_data['data']) > 64) {
  155. $error .= 'Identifier not allowed to be longer than 64 bytes in '.$frame_name.' (supplied data was '.strlen($frame_data['data']).' bytes long)<BR>';
  156. } else {
  157. $framedata .= str_replace(chr(0), '', $frame_data['ownerid']).chr(0);
  158. $framedata .= substr($frame_data['data'], 0, 64); // max 64 bytes - truncate anything longer
  159. }
  160. break;
  161. case 'TXXX':
  162. // 4.2.2 TXXX User defined text information frame
  163. // Text encoding $xx
  164. // Description <text string according to encoding> $00 (00)
  165. // Value <text string according to encoding>
  166. if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
  167. $error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].') for ID3v2.'.$majorversion.'<BR>';
  168. } else {
  169. $framedata .= chr($frame_data['encodingid']);
  170. $framedata .= $frame_data['description'].TextEncodingLookup('terminator', $frame_data['encodingid']);
  171. $framedata .= $frame_data['data'];
  172. }
  173. break;
  174. case 'WXXX':
  175. // 4.3.2 WXXX User defined URL link frame
  176. // Text encoding $xx
  177. // Description <text string according to encoding> $00 (00)
  178. // URL <text string>
  179. if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
  180. $error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].') for ID3v2.'.$majorversion.'<BR>';
  181. } else if (!IsValidURL($frame_data['url'], FALSE, FALSE)) {
  182. $error .= 'Invalid URL in '.$frame_name.' ('.$frame_data['url'].')<BR>';
  183. } else {
  184. $framedata .= chr($frame_data['encodingid']);
  185. $framedata .= $frame_data['description'].TextEncodingLookup('terminator', $frame_data['encodingid']);
  186. $framedata .= $frame_data['url'];
  187. }
  188. break;
  189. case 'IPLS':
  190. // 4.4 IPLS Involved people list (ID3v2.3 only)
  191. // Text encoding $xx
  192. // People list strings <textstrings>
  193. if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
  194. $error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].') for ID3v2.'.$majorversion.'<BR>';
  195. } else {
  196. $framedata .= chr($frame_data['encodingid']);
  197. $framedata .= $frame_data['data'];
  198. }
  199. break;
  200. case 'MCDI':
  201. // 4.4 MCDI Music CD identifier
  202. // CD TOC <binary data>
  203. $framedata .= $frame_data['data'];
  204. break;
  205. case 'ETCO':
  206. // 4.5 ETCO Event timing codes
  207. // Time stamp format $xx
  208. // Where time stamp format is:
  209. // $01 (32-bit value) MPEG frames from beginning of file
  210. // $02 (32-bit value) milliseconds from beginning of file
  211. // Followed by a list of key events in the following format:
  212. // Type of event $xx
  213. // Time stamp $xx (xx ...)
  214. // The 'Time stamp' is set to zero if directly at the beginning of the sound
  215. // or after the previous event. All events MUST be sorted in chronological order.
  216. if (($frame_data['timestampformat'] > 2) || ($frame_data['timestampformat'] < 1)) {
  217. $error .= 'Invalid Time Stamp Format byte in '.$frame_name.' ('.$frame_data['timestampformat'].')<BR>';
  218. } else {
  219. $framedata .= chr($frame_data['timestampformat']);
  220. foreach ($frame_data as $key => $val) {
  221. if (!IsValidETCOevent($val['typeid'], $majorversion)) {
  222. $error .= 'Invalid Event Type byte in '.$frame_name.' ('.$val['typeid'].')<BR>';
  223. } else if (($key != 'timestampformat') && ($key != 'flags')) {
  224. if (($val['timestamp'] > 0) && ($previousETCOtimestamp >= $val['timestamp'])) {
  225. // The 'Time stamp' is set to zero if directly at the beginning of the sound
  226. // or after the previous event. All events MUST be sorted in chronological order.
  227. $error .= 'Out-of-order timestamp in '.$frame_name.' ('.$val['timestamp'].') for Event Type ('.$val['typeid'].')<BR>';
  228. } else {
  229. $framedata .= chr($val['typeid']);
  230. $framedata .= BigEndian2String($val['timestamp'], 4, FALSE);
  231. }
  232. }
  233. }
  234. }
  235. break;
  236. case 'MLLT':
  237. // 4.6 MLLT MPEG location lookup table
  238. // MPEG frames between reference $xx xx
  239. // Bytes between reference $xx xx xx
  240. // Milliseconds between reference $xx xx xx
  241. // Bits for bytes deviation $xx
  242. // Bits for milliseconds dev. $xx
  243. // Then for every reference the following data is included;
  244. // Deviation in bytes %xxx....
  245. // Deviation in milliseconds %xxx....
  246. if (($frame_data['framesbetweenreferences'] > 0) && ($frame_data['framesbetweenreferences'] <= 65535)) {
  247. $framedata .= BigEndian2String($frame_data['framesbetweenreferences'], 2, FALSE);
  248. } else {
  249. $error .= 'Invalid MPEG Frames Between References in '.$frame_name.' ('.$frame_data['framesbetweenreferences'].')<BR>';
  250. }
  251. if (($frame_data['bytesbetweenreferences'] > 0) && ($frame_data['bytesbetweenreferences'] <= 16777215)) {
  252. $framedata .= BigEndian2String($frame_data['bytesbetweenreferences'], 3, FALSE);
  253. } else {
  254. $error .= 'Invalid bytes Between References in '.$frame_name.' ('.$frame_data['bytesbetweenreferences'].')<BR>';
  255. }
  256. if (($frame_data['msbetweenreferences'] > 0) && ($frame_data['msbetweenreferences'] <= 16777215)) {
  257. $framedata .= BigEndian2String($frame_data['msbetweenreferences'], 3, FALSE);
  258. } else {
  259. $error .= 'Invalid Milliseconds Between References in '.$frame_name.' ('.$frame_data['msbetweenreferences'].')<BR>';
  260. }
  261. if (!IsWithinBitRange($frame_data['bitsforbytesdeviation'], 8, FALSE)) {
  262. if (($frame_data['bitsforbytesdeviation'] % 4) == 0) {
  263. $framedata .= chr($frame_data['bitsforbytesdeviation']);
  264. } else {
  265. $error .= 'Bits For Bytes Deviation in '.$frame_name.' ('.$frame_data['bitsforbytesdeviation'].') must be a multiple of 4.<BR>';
  266. }
  267. } else {
  268. $error .= 'Invalid Bits For Bytes Deviation in '.$frame_name.' ('.$frame_data['bitsforbytesdeviation'].')<BR>';
  269. }
  270. if (!IsWithinBitRange($frame_data['bitsformsdeviation'], 8, FALSE)) {
  271. if (($frame_data['bitsformsdeviation'] % 4) == 0) {
  272. $framedata .= chr($frame_data['bitsformsdeviation']);
  273. } else {
  274. $error .= 'Bits For Milliseconds Deviation in '.$frame_name.' ('.$frame_data['bitsforbytesdeviation'].') must be a multiple of 4.<BR>';
  275. }
  276. } else {
  277. $error .= 'Invalid Bits For Milliseconds Deviation in '.$frame_name.' ('.$frame_data['bitsformsdeviation'].')<BR>';
  278. }
  279. foreach ($frame_data as $key => $val) {
  280. if (($key != 'framesbetweenreferences') && ($key != 'bytesbetweenreferences') && ($key != 'msbetweenreferences') && ($key != 'bitsforbytesdeviation') && ($key != 'bitsformsdeviation') && ($key != 'flags')) {
  281. $unwrittenbitstream .= str_pad(Dec2Bin($val['bytedeviation']), $frame_data['bitsforbytesdeviation'], '0', STR_PAD_LEFT);
  282. $unwrittenbitstream .= str_pad(Dec2Bin($val['msdeviation']), $frame_data['bitsformsdeviation'], '0', STR_PAD_LEFT);
  283. }
  284. }
  285. for ($i=0;$i<strlen($unwrittenbitstream);$i+=8) {
  286. $highnibble = bindec(substr($unwrittenbitstream, $i, 4)) << 4;
  287. $lownibble = bindec(substr($unwrittenbitstream, $i + 4, 4));
  288. $framedata .= chr($highnibble & $lownibble);
  289. }
  290. break;
  291. case 'SYTC':
  292. // 4.7 SYTC Synchronised tempo codes
  293. // Time stamp format $xx
  294. // Tempo data <binary data>
  295. // Where time stamp format is:
  296. // $01 (32-bit value) MPEG frames from beginning of file
  297. // $02 (32-bit value) milliseconds from beginning of file
  298. if (($frame_data['timestampformat'] > 2) || ($frame_data['timestampformat'] < 1)) {
  299. $error .= 'Invalid Time Stamp Format byte in '.$frame_name.' ('.$frame_data['timestampformat'].')<BR>';
  300. } else {
  301. $framedata .= chr($frame_data['timestampformat']);
  302. foreach ($frame_data as $key => $val) {
  303. if (!IsValidETCOevent($val['typeid'], $majorversion)) {
  304. $error .= 'Invalid Event Type byte in '.$frame_name.' ('.$val['typeid'].')<BR>';
  305. } else if (($key != 'timestampformat') && ($key != 'flags')) {
  306. if (($val['tempo'] < 0) || ($val['tempo'] > 510)) {
  307. $error .= 'Invalid Tempo (max = 510) in '.$frame_name.' ('.$val['tempo'].') at timestamp ('.$val['timestamp'].')<BR>';
  308. } else {
  309. if ($val['tempo'] > 255) {
  310. $framedata .= chr(255);
  311. $val['tempo'] -= 255;
  312. }
  313. $framedata .= chr($val['tempo']);
  314. $framedata .= BigEndian2String($val['timestamp'], 4, FALSE);
  315. }
  316. }
  317. }
  318. }
  319. break;
  320. case 'USLT':
  321. // 4.8 USLT Unsynchronised lyric/text transcription
  322. // Text encoding $xx
  323. // Language $xx xx xx
  324. // Content descriptor <text string according to encoding> $00 (00)
  325. // Lyrics/text <full text string according to encoding>
  326. if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
  327. $error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].') for ID3v2.'.$majorversion.'<BR>';
  328. } else if (LanguageLookup($frame_data['language'], TRUE) == '') {
  329. $error .= 'Invalid Language in '.$frame_name.' ('.$frame_data['language'].')<BR>';
  330. } else {
  331. $framedata .= chr($frame_data['encodingid']);
  332. $framedata .= strtolower($frame_data['language']);
  333. $framedata .= $frame_data['description'].TextEncodingLookup('terminator', $frame_data['encodingid']);
  334. $framedata .= $frame_data['data'];
  335. }
  336. break;
  337. case 'SYLT':
  338. // 4.9 SYLT Synchronised lyric/text
  339. // Text encoding $xx
  340. // Language $xx xx xx
  341. // Time stamp format $xx
  342. // $01 (32-bit value) MPEG frames from beginning of file
  343. // $02 (32-bit value) milliseconds from beginning of file
  344. // Content type $xx
  345. // Content descriptor <text string according to encoding> $00 (00)
  346. // Terminated text to be synced (typically a syllable)
  347. // Sync identifier (terminator to above string) $00 (00)
  348. // Time stamp $xx (xx ...)
  349. if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
  350. $error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].') for ID3v2.'.$majorversion.'<BR>';
  351. } else if (LanguageLookup($frame_data['language'], TRUE) == '') {
  352. $error .= 'Invalid Language in '.$frame_name.' ('.$frame_data['language'].')<BR>';
  353. } else if (($frame_data['timestampformat'] > 2) || ($frame_data['timestampformat'] < 1)) {
  354. $error .= 'Invalid Time Stamp Format byte in '.$frame_name.' ('.$frame_data['timestampformat'].')<BR>';
  355. } else if (!IsValidSYLTtype($frame_data['contenttypeid'], $majorversion)) {
  356. $error .= 'Invalid Content Type byte in '.$frame_name.' ('.$frame_data['contenttypeid'].')<BR>';
  357. } else if (!is_array($frame_data['data'])) {
  358. $error .= 'Invalid Lyric/Timestamp data in '.$frame_name.' (must be an array)<BR>';
  359. } else {
  360. $framedata .= chr($frame_data['encodingid']);
  361. $framedata .= strtolower($frame_data['language']);
  362. $framedata .= chr($frame_data['timestampformat']);
  363. $framedata .= chr($frame_data['contenttypeid']);
  364. $framedata .= $frame_data['description'].TextEncodingLookup('terminator', $frame_data['encodingid']);
  365. ksort($frame_data['data']);
  366. foreach ($frame_data['data'] as $key => $val) {
  367. $framedata .= $val['data'].TextEncodingLookup('terminator', $frame_data['encodingid']);
  368. $framedata .= BigEndian2String($val['timestamp'], 4, FALSE);
  369. }
  370. }
  371. break;
  372. case 'COMM':
  373. // 4.10 COMM Comments
  374. // Text encoding $xx
  375. // Language $xx xx xx
  376. // Short content descrip. <text string according to encoding> $00 (00)
  377. // The actual text <full text string according to encoding>
  378. if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
  379. $error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].') for ID3v2.'.$majorversion.'<BR>';
  380. } else if (LanguageLookup($frame_data['language'], TRUE) == '') {
  381. $error .= 'Invalid Language in '.$frame_name.' ('.$frame_data['language'].')<BR>';
  382. } else {
  383. $framedata .= chr($frame_data['encodingid']);
  384. $framedata .= strtolower($frame_data['language']);
  385. $framedata .= $frame_data['description'].TextEncodingLookup('terminator', $frame_data['encodingid']);
  386. $framedata .= $frame_data['data'];
  387. }
  388. break;
  389. case 'RVA2':
  390. // 4.11 RVA2 Relative volume adjustment (2) (ID3v2.4+ only)
  391. // Identification <text string> $00
  392. // The 'identification' string is used to identify the situation and/or
  393. // device where this adjustment should apply. The following is then
  394. // repeated for every channel:
  395. // Type of channel $xx
  396. // Volume adjustment $xx xx
  397. // Bits representing peak $xx
  398. // Peak volume $xx (xx ...)
  399. $framedata .= str_replace(chr(0), '', $frame_data['description']).chr(0);
  400. foreach ($frame_data as $key => $val) {
  401. if ($key != 'description') {
  402. $framedata .= chr($val['channeltypeid']);
  403. $framedata .= substr(str_pad(dechex($val['volumeadjust']), 8, '0', STR_PAD_LEFT), 4, 4); // signed 16-bit
  404. if (!IsWithinBitRange($frame_data['bitspeakvolume'], 8, FALSE)) {
  405. $framedata .= chr($val['bitspeakvolume']);
  406. if ($val['bitspeakvolume'] > 0) {
  407. $framedata .= BigEndian2String($val['peakvolume'], ceil($val['bitspeakvolume'] / 8), FALSE);
  408. }
  409. } else {
  410. $error .= 'Invalid Bits Representing Peak Volume in '.$frame_name.' ('.$val['bitspeakvolume'].') (range = 0 to 255)<BR>';
  411. }
  412. }
  413. }
  414. break;
  415. case 'RVAD':
  416. // 4.12 RVAD Relative volume adjustment (ID3v2.3 only)
  417. // Increment/decrement %00fedcba
  418. // Bits used for volume descr. $xx
  419. // Relative volume change, right $xx xx (xx ...) // a
  420. // Relative volume change, left $xx xx (xx ...) // b
  421. // Peak volume right $xx xx (xx ...)
  422. // Peak volume left $xx xx (xx ...)
  423. // Relative volume change, right back $xx xx (xx ...) // c
  424. // Relative volume change, left back $xx xx (xx ...) // d
  425. // Peak volume right back $xx xx (xx ...)
  426. // Peak volume left back $xx xx (xx ...)
  427. // Relative volume change, center $xx xx (xx ...) // e
  428. // Peak volume center $xx xx (xx ...)
  429. // Relative volume change, bass $xx xx (xx ...) // f
  430. // Peak volume bass $xx xx (xx ...)
  431. if (!IsWithinBitRange($frame_data['bitsvolume'], 8, FALSE)) {
  432. $error .= 'Invalid Bits For Volume Description byte in '.$frame_name.' ('.$frame_data['bitsvolume'].') (range = 1 to 255)<BR>';
  433. } else {
  434. $incdecflag .= '00';
  435. $incdecflag .= Bool2IntString($frame_data['incdec']['right']); // a - Relative volume change, right
  436. $incdecflag .= Bool2IntString($frame_data['incdec']['left']); // b - Relative volume change, left
  437. $incdecflag .= Bool2IntString($frame_data['incdec']['rightrear']); // c - Relative volume change, right back
  438. $incdecflag .= Bool2IntString($frame_data['incdec']['leftrear']); // d - Relative volume change, left back
  439. $incdecflag .= Bool2IntString($frame_data['incdec']['center']); // e - Relative volume change, center
  440. $incdecflag .= Bool2IntString($frame_data['incdec']['bass']); // f - Relative volume change, bass
  441. $framedata .= chr(bindec($incdecflag));
  442. $framedata .= chr($frame_data['bitsvolume']);
  443. $framedata .= BigEndian2String($frame_data['volumechange']['right'], ceil($frame_data['bitsvolume'] / 8), FALSE);
  444. $framedata .= BigEndian2String($frame_data['volumechange']['left'], ceil($frame_data['bitsvolume'] / 8), FALSE);
  445. $framedata .= BigEndian2String($frame_data['peakvolume']['right'], ceil($frame_data['bitsvolume'] / 8), FALSE);
  446. $framedata .= BigEndian2String($frame_data['peakvolume']['left'], ceil($frame_data['bitsvolume'] / 8), FALSE);
  447. if ($frame_data['volumechange']['rightrear'] || $frame_data['volumechange']['leftrear'] ||
  448. $frame_data['peakvolume']['rightrear'] || $frame_data['peakvolume']['leftrear'] ||
  449. $frame_data['volumechange']['center'] || $frame_data['peakvolume']['center'] ||
  450. $frame_data['volumechange']['bass'] || $frame_data['peakvolume']['bass']) {
  451. $framedata .= BigEndian2String($frame_data['volumechange']['rightrear'], ceil($frame_data['bitsvolume']/8), FALSE);
  452. $framedata .= BigEndian2String($frame_data['volumechange']['leftrear'], ceil($frame_data['bitsvolume']/8), FALSE);
  453. $framedata .= BigEndian2String($frame_data['peakvolume']['rightrear'], ceil($frame_data['bitsvolume']/8), FALSE);
  454. $framedata .= BigEndian2String($frame_data['peakvolume']['leftrear'], ceil($frame_data['bitsvolume']/8), FALSE);
  455. }
  456. if ($frame_data['volumechange']['center'] || $frame_data['peakvolume']['center'] ||
  457. $frame_data['volumechange']['bass'] || $frame_data['peakvolume']['bass']) {
  458. $framedata .= BigEndian2String($frame_data['volumechange']['center'], ceil($frame_data['bitsvolume']/8), FALSE);
  459. $framedata .= BigEndian2String($frame_data['peakvolume']['center'], ceil($frame_data['bitsvolume']/8), FALSE);
  460. }
  461. if ($frame_data['volumechange']['bass'] || $frame_data['peakvolume']['bass']) {
  462. $framedata .= BigEndian2String($frame_data['volumechange']['bass'], ceil($frame_data['bitsvolume']/8), FALSE);
  463. $framedata .= BigEndian2String($frame_data['peakvolume']['bass'], ceil($frame_data['bitsvolume']/8), FALSE);
  464. }
  465. }
  466. break;
  467. case 'EQU2':
  468. // 4.12 EQU2 Equalisation (2) (ID3v2.4+ only)
  469. // Interpolation method $xx
  470. // $00 Band
  471. // $01 Linear
  472. // Identification <text string> $00
  473. // The following is then repeated for every adjustment point
  474. // Frequency $xx xx
  475. // Volume adjustment $xx xx
  476. if (($frame_data['interpolationmethod'] < 0) || ($frame_data['interpolationmethod'] > 1)) {
  477. $error .= 'Invalid Interpolation Method byte in '.$frame_name.' ('.$frame_data['interpolationmethod'].') (valid = 0 or 1)<BR>';
  478. } else {
  479. $framedata .= chr($frame_data['interpolationmethod']);
  480. $framedata .= str_replace(chr(0), '', $frame_data['description']).chr(0);
  481. foreach ($frame_data['data'] as $key => $val) {
  482. $framedata .= BigEndian2String(round($key * 2), 2, FALSE);
  483. $framedata .= substr(str_pad(dechex($val), 4, '0', STR_PAD_LEFT), 4, 4); // signed 16-bit
  484. }
  485. }
  486. break;
  487. case 'EQUA':
  488. // 4.12 EQUA Equalisation (ID3v2.3 only)
  489. // Adjustment bits $xx
  490. // This is followed by 2 bytes + ('adjustment bits' rounded up to the
  491. // nearest byte) for every equalisation band in the following format,
  492. // giving a frequency range of 0 - 32767Hz:
  493. // Increment/decrement %x (MSB of the Frequency)
  494. // Frequency (lower 15 bits)
  495. // Adjustment $xx (xx ...)
  496. if (!IsWithinBitRange($frame_data['bitsvolume'], 8, FALSE)) {
  497. $error .= 'Invalid Adjustment Bits byte in '.$frame_name.' ('.$frame_data['bitsvolume'].') (range = 1 to 255)<BR>';
  498. } else {
  499. $framedata .= chr($frame_data['adjustmentbits']);
  500. foreach ($frame_data as $key => $val) {
  501. if ($key != 'bitsvolume') {
  502. if (($key > 32767) || ($key < 0)) {
  503. $error .= 'Invalid Frequency in '.$frame_name.' ('.$key.') (range = 0 to 32767)<BR>';
  504. } else {
  505. if ($val >= 0) {
  506. // put MSB of frequency to 1 if increment, 0 if decrement
  507. $key |= 0x8000;
  508. }
  509. $framedata .= BigEndian2String($key, 2, FALSE);
  510. $framedata .= BigEndian2String($val, ceil($frame_data['adjustmentbits'] / 8), FALSE);
  511. }
  512. }
  513. }
  514. }
  515. break;
  516. case 'RVRB':
  517. // 4.13 RVRB Reverb
  518. // Reverb left (ms) $xx xx
  519. // Reverb right (ms) $xx xx
  520. // Reverb bounces, left $xx
  521. // Reverb bounces, right $xx
  522. // Reverb feedback, left to left $xx
  523. // Reverb feedback, left to right $xx
  524. // Reverb feedback, right to right $xx
  525. // Reverb feedback, right to left $xx
  526. // Premix left to right $xx
  527. // Premix right to left $xx
  528. if (!IsWithinBitRange($frame_data['left'], 16, FALSE)) {
  529. $error .= 'Invalid Reverb Left in '.$frame_name.' ('.$frame_data['left'].') (range = 0 to 65535)<BR>';
  530. } else if (!IsWithinBitRange($frame_data['right'], 16, FALSE)) {
  531. $error .= 'Invalid Reverb Left in '.$frame_name.' ('.$frame_data['right'].') (range = 0 to 65535)<BR>';
  532. } else if (!IsWithinBitRange($frame_data['bouncesL'], 8, FALSE)) {
  533. $error .= 'Invalid Reverb Bounces, Left in '.$frame_name.' ('.$frame_data['bouncesL'].') (range = 0 to 255)<BR>';
  534. } else if (!IsWithinBitRange($frame_data['bouncesR'], 8, FALSE)) {
  535. $error .= 'Invalid Reverb Bounces, Right in '.$frame_name.' ('.$frame_data['bouncesR'].') (range = 0 to 255)<BR>';
  536. } else if (!IsWithinBitRange($frame_data['feedbackLL'], 8, FALSE)) {
  537. $error .= 'Invalid Reverb Feedback, Left-To-Left in '.$frame_name.' ('.$frame_data['feedbackLL'].') (range = 0 to 255)<BR>';
  538. } else if (!IsWithinBitRange($frame_data['feedbackLR'], 8, FALSE)) {
  539. $error .= 'Invalid Reverb Feedback, Left-To-Right in '.$frame_name.' ('.$frame_data['feedbackLR'].') (range = 0 to 255)<BR>';
  540. } else if (!IsWithinBitRange($frame_data['feedbackRR'], 8, FALSE)) {
  541. $error .= 'Invalid Reverb Feedback, Right-To-Right in '.$frame_name.' ('.$frame_data['feedbackRR'].') (range = 0 to 255)<BR>';
  542. } else if (!IsWithinBitRange($frame_data['feedbackRL'], 8, FALSE)) {
  543. $error .= 'Invalid Reverb Feedback, Right-To-Left in '.$frame_name.' ('.$frame_data['feedbackRL'].') (range = 0 to 255)<BR>';
  544. } else if (!IsWithinBitRange($frame_data['premixLR'], 8, FALSE)) {
  545. $error .= 'Invalid Premix, Left-To-Right in '.$frame_name.' ('.$frame_data['premixLR'].') (range = 0 to 255)<BR>';
  546. } else if (!IsWithinBitRange($frame_data['premixRL'], 8, FALSE)) {
  547. $error .= 'Invalid Premix, Right-To-Left in '.$frame_name.' ('.$frame_data['premixRL'].') (range = 0 to 255)<BR>';
  548. } else {
  549. $framedata .= BigEndian2String($frame_data['left'], 2, FALSE);
  550. $framedata .= BigEndian2String($frame_data['right'], 2, FALSE);
  551. $framedata .= chr($frame_data['bouncesL']);
  552. $framedata .= chr($frame_data['bouncesR']);
  553. $framedata .= chr($frame_data['feedbackLL']);
  554. $framedata .= chr($frame_data['feedbackLR']);
  555. $framedata .= chr($frame_data['feedbackRR']);
  556. $framedata .= chr($frame_data['feedbackRL']);
  557. $framedata .= chr($frame_data['premixLR']);
  558. $framedata .= chr($frame_data['premixRL']);
  559. }
  560. break;
  561. case 'APIC':
  562. // 4.14 APIC Attached picture
  563. // Text encoding $xx
  564. // MIME type <text string> $00
  565. // Picture type $xx
  566. // Description <text string according to encoding> $00 (00)
  567. // Picture data <binary data>
  568. if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
  569. $error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].') for ID3v2.'.$majorversion.'<BR>';
  570. } else if (!IsValidAPICpicturetype($frame_data['picturetypeid'], $majorversion)) {
  571. $error .= 'Invalid Picture Type byte in '.$frame_name.' ('.$frame_data['picturetypeid'].') for ID3v2.'.$majorversion.'<BR>';
  572. } else if (($majorversion >= 3) && (!IsValidAPICimageformat($frame_data['mime'], $majorversion))) {
  573. $error .= 'Invalid MIME Type in '.$frame_name.' ('.$frame_data['mime'].') for ID3v2.'.$majorversion.'<BR>';
  574. } else if (($frame_data['mime'] == '-->') && (!IsValidURL($frame_data['data'], FALSE, FALSE))) {
  575. $error .= 'Invalid URL in '.$frame_name.' ('.$frame_data['data'].')<BR>';
  576. } else {
  577. $framedata .= chr($frame_data['encodingid']);
  578. $framedata .= str_replace(chr(0), '', $frame_data['mime']).chr(0);
  579. $framedata .= chr($frame_data['picturetypeid']);
  580. $framedata .= $frame_data['description'].TextEncodingLookup('terminator', $frame_data['encodingid']);
  581. $framedata .= $frame_data['data'];
  582. }
  583. break;
  584. case 'GEOB':
  585. // 4.15 GEOB General encapsulated object
  586. // Text encoding $xx
  587. // MIME type <text string> $00
  588. // Filename <text string according to encoding> $00 (00)
  589. // Content description <text string according to encoding> $00 (00)
  590. // Encapsulated object <binary data>
  591. if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
  592. $error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].') for ID3v2.'.$majorversion.'<BR>';
  593. } else if (!IsValidMIMEstring($frame_data['mime'])) {
  594. $error .= 'Invalid MIME Type in '.$frame_name.' ('.$frame_data['mime'].')<BR>';
  595. } else if (!$frame_data['description']) {
  596. $error .= 'Missing Description in '.$frame_name.'<BR>';
  597. } else {
  598. $framedata .= chr($frame_data['encodingid']);
  599. $framedata .= str_replace(chr(0), '', $frame_data['mime']).chr(0);
  600. $framedata .= $frame_data['filename'].TextEncodingLookup('terminator', $frame_data['encodingid']);
  601. $framedata .= $frame_data['description'].TextEncodingLookup('terminator', $frame_data['encodingid']);
  602. $framedata .= $frame_data['data'];
  603. }
  604. break;
  605. case 'PCNT':
  606. // 4.16 PCNT Play counter
  607. // When the counter reaches all one's, one byte is inserted in
  608. // front of the counter thus making the counter eight bits bigger
  609. // Counter $xx xx xx xx (xx ...)
  610. $framedata .= BigEndian2String($frame_data['data'], 4, FALSE);
  611. break;
  612. case 'POPM':
  613. // 4.17 POPM Popularimeter
  614. // When the counter reaches all one's, one byte is inserted in
  615. // front of the counter thus making the counter eight bits bigger
  616. // Email to user <text string> $00
  617. // Rating $xx
  618. // Counter $xx xx xx xx (xx ...)
  619. if (!IsWithinBitRange($frame_data['rating'], 8, FALSE)) {
  620. $error .= 'Invalid Rating byte in '.$frame_name.' ('.$frame_data['rating'].') (range = 0 to 255)<BR>';
  621. } else if (!IsValidEmail($frame_data['email'])) {
  622. $error .= 'Invalid Email in '.$frame_name.' ('.$frame_data['email'].')<BR>';
  623. } else {
  624. $framedata .= str_replace(chr(0), '', $frame_data['email']).chr(0);
  625. $framedata .= chr($frame_data['rating']);
  626. $framedata .= BigEndian2String($frame_data['data'], 4, FALSE);
  627. }
  628. break;
  629. case 'RBUF':
  630. // 4.18 RBUF Recommended buffer size
  631. // Buffer size $xx xx xx
  632. // Embedded info flag %0000000x
  633. // Offset to next tag $xx xx xx xx
  634. if (!IsWithinBitRange($frame_data['buffersize'], 24, FALSE)) {
  635. $error .= 'Invalid Buffer Size in '.$frame_name.'<BR>';
  636. } else if (!IsWithinBitRange($frame_data['nexttagoffset'], 32, FALSE)) {
  637. $error .= 'Invalid Offset To Next Tag in '.$frame_name.'<BR>';
  638. } else {
  639. $framedata .= BigEndian2String($frame_data['buffersize'], 3, FALSE);
  640. $flag .= '0000000';
  641. $flag .= Bool2IntString($frame_data['flags']['embededinfo']);
  642. $framedata .= chr(bindec($flag));
  643. $framedata .= BigEndian2String($frame_data['nexttagoffset'], 4, FALSE);
  644. }
  645. break;
  646. case 'AENC':
  647. // 4.19 AENC Audio encryption
  648. // Owner identifier <text string> $00
  649. // Preview start $xx xx
  650. // Preview length $xx xx
  651. // Encryption info <binary data>
  652. if (!IsWithinBitRange($frame_data['previewstart'], 16, FALSE)) {
  653. $error .= 'Invalid Preview Start in '.$frame_name.' ('.$frame_data['previewstart'].')<BR>';
  654. } else if (!IsWithinBitRange($frame_data['previewlength'], 16, FALSE)) {
  655. $error .= 'Invalid Preview Length in '.$frame_name.' ('.$frame_data['previewlength'].')<BR>';
  656. } else {
  657. $framedata .= str_replace(chr(0), '', $frame_data['ownerid']).chr(0);
  658. $framedata .= BigEndian2String($frame_data['previewstart'], 2, FALSE);
  659. $framedata .= BigEndian2String($frame_data['previewlength'], 2, FALSE);
  660. $framedata .= $frame_data['encryptioninfo'];
  661. }
  662. break;
  663. case 'LINK':
  664. // 4.20 LINK Linked information
  665. // Frame identifier $xx xx xx xx
  666. // URL <text string> $00
  667. // ID and additional data <text string(s)>
  668. if (!IsValidID3v2FrameName($frame_data['frameid'], $majorversion)) {
  669. $error .= 'Invalid Frame Identifier in '.$frame_name.' ('.$frame_data['frameid'].')<BR>';
  670. } else if (!IsValidURL($frame_data['url'], TRUE, FALSE)) {
  671. $error .= 'Invalid URL in '.$frame_name.' ('.$frame_data['url'].')<BR>';
  672. } else if ((($frame_data['frameid'] == 'AENC') || ($frame_data['frameid'] == 'APIC') || ($frame_data['frameid'] == 'GEOB') || ($frame_data['frameid'] == 'TXXX')) && ($frame_data['additionaldata'] == '')) {
  673. $error .= 'Content Descriptor must be specified as additional data for Frame Identifier of '.$frame_data['frameid'].' in '.$frame_name.'<BR>';
  674. } else if (($frame_data['frameid'] == 'USER') && (LanguageLookup($frame_data['additionaldata'], TRUE) == '')) {
  675. $error .= 'Language must be specified as additional data for Frame Identifier of '.$frame_data['frameid'].' in '.$frame_name.'<BR>';
  676. } else if (($frame_data['frameid'] == 'PRIV') && ($frame_data['additionaldata'] == '')) {
  677. $error .= 'Owner Identifier must be specified as additional data for Frame Identifier of '.$frame_data['frameid'].' in '.$frame_name.'<BR>';
  678. } 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) == ''))) {
  679. $error .= 'Language followed by Content Descriptor must be specified as additional data for Frame Identifier of '.$frame_data['frameid'].' in '.$frame_name.'<BR>';
  680. } else {
  681. $framedata .= $frame_data['frameid'];
  682. $framedata .= str_replace(chr(0), '', $frame_data['url']).chr(0);
  683. switch ($frame_data['frameid']) {
  684. case 'COMM':
  685. case 'SYLT':
  686. case 'USLT':
  687. case 'PRIV':
  688. case 'USER':
  689. case 'AENC':
  690. case 'APIC':
  691. case 'GEOB':
  692. case 'TXXX':
  693. $framedata .= $frame_data['additionaldata'];
  694. break;
  695. case 'ASPI':
  696. case 'ETCO':
  697. case 'EQU2':
  698. case 'MCID':
  699. case 'MLLT':
  700. case 'OWNE':
  701. case 'RVA2':
  702. case 'RVRB':
  703. case 'SYTC':
  704. case 'IPLS':
  705. case 'RVAD':
  706. case 'EQUA':
  707. // no additional data required
  708. break;
  709. case 'RBUF':
  710. if ($majorversion == 3) {
  711. // no additional data required
  712. } else {
  713. $error .= $frame_data['frameid'].' is not a valid Frame Identifier in '.$frame_name.' (in ID3v2.'.$majorversion.')<BR>';
  714. }
  715. default:
  716. if ((substr($frame_data['frameid'], 0, 1) == 'T') || (substr($frame_data['frameid'], 0, 1) == 'W')) {
  717. // no additional data required
  718. } else {
  719. $error .= $frame_data['frameid'].' is not a valid Frame Identifier in '.$frame_name.' (in ID3v2.'.$majorversion.')<BR>';
  720. }
  721. break;
  722. }
  723. }
  724. break;
  725. case 'POSS':
  726. // 4.21 POSS Position synchronisation frame (ID3v2.3+ only)
  727. // Time stamp format $xx
  728. // Position $xx (xx ...)
  729. if (($frame_data['timestampformat'] < 1) || ($frame_data['timestampformat'] > 2)) {
  730. $error .= 'Invalid Time Stamp Format in '.$frame_name.' ('.$frame_data['timestampformat'].') (valid = 1 or 2)<BR>';
  731. } else if (!IsWithinBitRange($frame_data['position'], 32, FALSE)) {
  732. $error .= 'Invalid Position in '.$frame_name.' ('.$frame_data['position'].') (range = 0 to 4294967295)<BR>';
  733. } else {
  734. $framedata .= chr($frame_data['timestampformat']);
  735. $framedata .= BigEndian2String($frame_data['position'], 4, FALSE);
  736. }
  737. break;
  738. case 'USER':
  739. // 4.22 USER Terms of use (ID3v2.3+ only)
  740. // Text encoding $xx
  741. // Language $xx xx xx
  742. // The actual text <text string according to encoding>
  743. if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
  744. $error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].')<BR>';
  745. } else if (LanguageLookup($frame_data['language'], TRUE) == '') {
  746. $error .= 'Invalid Language in '.$frame_name.' ('.$frame_data['language'].')<BR>';
  747. } else {
  748. $framedata .= chr($frame_data['encodingid']);
  749. $framedata .= strtolower($frame_data['language']);
  750. $framedata .= $frame_data['data'];
  751. }
  752. break;
  753. case 'OWNE':
  754. // 4.23 OWNE Ownership frame (ID3v2.3+ only)
  755. // Text encoding $xx
  756. // Price paid <text string> $00
  757. // Date of purch. <text string>
  758. // Seller <text string according to encoding>
  759. if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
  760. $error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].')<BR>';
  761. } else if (!IsANumber($frame_data['pricepaid']['value'], FALSE)) {
  762. $error .= 'Invalid Price Paid in '.$frame_name.' ('.$frame_data['pricepaid']['value'].')<BR>';
  763. } else if (!IsValidDateStampString($frame_data['purchasedate'])) {
  764. $error .= 'Invalid Date Of Purchase in '.$frame_name.' ('.$frame_data['purchasedate'].') (format = YYYYMMDD)<BR>';
  765. } else {
  766. $framedata .= chr($frame_data['encodingid']);
  767. $framedata .= str_replace(chr(0), '', $frame_data['pricepaid']['value']).chr(0);
  768. $framedata .= $frame_data['purchasedate'];
  769. $framedata .= $frame_data['seller'];
  770. }
  771. break;
  772. case 'COMR':
  773. // 4.24 COMR Commercial frame (ID3v2.3+ only)
  774. // Text encoding $xx
  775. // Price string <text string> $00
  776. // Valid until <text string>
  777. // Contact URL <text string> $00
  778. // Received as $xx
  779. // Name of seller <text string according to encoding> $00 (00)
  780. // Description <text string according to encoding> $00 (00)
  781. // Picture MIME type <string> $00
  782. // Seller logo <binary data>
  783. if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
  784. $error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].')<BR>';
  785. } else if (!IsValidDateStampString($frame_data['pricevaliduntil'])) {
  786. $error .= 'Invalid Valid Until date in '.$frame_name.' ('.$frame_data['pricevaliduntil'].') (format = YYYYMMDD)<BR>';
  787. } else if (!IsValidURL($frame_data['contacturl'], FALSE, TRUE)) {
  788. $error .= 'Invalid Contact URL in '.$frame_name.' ('.$frame_data['contacturl'].') (allowed schemes: http, https, ftp, mailto)<BR>';
  789. } else if (!IsValidCOMRreceivedas($frame_data['receivedasid'], $majorversion)) {
  790. $error .= 'Invalid Received As byte in '.$frame_name.' ('.$frame_data['contacturl'].') (range = 0 to 8)<BR>';
  791. } else if (!IsValidMIMEstring($frame_data['mime'])) {
  792. $error .= 'Invalid MIME Type in '.$frame_name.' ('.$frame_data['mime'].')<BR>';
  793. } else {
  794. $framedata .= chr($frame_data['encodingid']);
  795. unset($pricestring);
  796. foreach ($frame_data['price'] as $key => $val) {
  797. if (IsValidPriceString($key.$val['value'])) {
  798. $pricestrings[] = $key.$val['value'];
  799. } else {
  800. $error .= 'Invalid Price String in '.$frame_name.' ('.$key.$val['value'].')<BR>';
  801. }
  802. }
  803. $framedata .= implode('/', $pricestrings);
  804. $framedata .= $frame_data['pricevaliduntil'];
  805. $framedata .= str_replace(chr(0), '', $frame_data['contacturl']).chr(0);
  806. $framedata .= chr($frame_data['receivedasid']);
  807. $framedata .= $frame_data['sellername'].TextEncodingLookup('terminator', $frame_data['encodingid']);
  808. $framedata .= $frame_data['description'].TextEncodingLookup('terminator', $frame_data['encodingid']);
  809. $framedata .= $frame_data['mime'].chr(0);
  810. $framedata .= $frame_data['logo'];
  811. }
  812. break;
  813. case 'ENCR':
  814. // 4.25 ENCR Encryption method registration (ID3v2.3+ only)
  815. // Owner identifier <text string> $00
  816. // Method symbol $xx
  817. // Encryption data <binary data>
  818. if (!IsWithinBitRange($frame_data['methodsymbol'], 8, FALSE)) {
  819. $error .= 'Invalid Group Symbol in '.$frame_name.' ('.$frame_data['methodsymbol'].') (range = 0 to 255)<BR>';
  820. } else {
  821. $framedata .= str_replace(chr(0), '', $frame_data['ownerid']).chr(0);
  822. $framedata .= ord($frame_data['methodsymbol']);
  823. $framedata .= $frame_data['data'];
  824. }
  825. break;
  826. case 'GRID':
  827. // 4.26 GRID Group identification registration (ID3v2.3+ only)
  828. // Owner identifier <text string> $00
  829. // Group symbol $xx
  830. // Group dependent data <binary data>
  831. if (!IsWithinBitRange($frame_data['groupsymbol'], 8, FALSE)) {
  832. $error .= 'Invalid Group Symbol in '.$frame_name.' ('.$frame_data['groupsymbol'].') (range = 0 to 255)<BR>';
  833. } else {
  834. $framedata .= str_replace(chr(0), '', $frame_data['ownerid']).chr(0);
  835. $framedata .= ord($frame_data['groupsymbol']);
  836. $framedata .= $frame_data['data'];
  837. }
  838. break;
  839. case 'PRIV':
  840. // 4.27 PRIV Private frame (ID3v2.3+ only)
  841. // Owner identifier <text string> $00
  842. // The private data <binary data>
  843. $framedata .= str_replace(chr(0), '', $frame_data['ownerid']).chr(0);
  844. $framedata .= $frame_data['data'];
  845. break;
  846. case 'SIGN':
  847. // 4.28 SIGN Signature frame (ID3v2.4+ only)
  848. // Group symbol $xx
  849. // Signature <binary data>
  850. if (!IsWithinBitRange($frame_data['groupsymbol'], 8, FALSE)) {
  851. $error .= 'Invalid Group Symbol in '.$frame_name.' ('.$frame_data['groupsymbol'].') (range = 0 to 255)<BR>';
  852. } else {
  853. $framedata .= ord($frame_data['groupsymbol']);
  854. $framedata .= $frame_data['data'];
  855. }
  856. break;
  857. case 'SEEK':
  858. // 4.29 SEEK Seek frame (ID3v2.4+ only)
  859. // Minimum offset to next tag $xx xx xx xx
  860. if (!IsWithinBitRange($frame_data['data'], 32, FALSE)) {
  861. $error .= 'Invalid Minimum Offset in '.$frame_name.' ('.$frame_data['data'].') (range = 0 to 4294967295)<BR>';
  862. } else {
  863. $framedata .= BigEndian2String($frame_data['data'], 4, FALSE);
  864. }
  865. break;
  866. case 'ASPI':
  867. // 4.30 ASPI Audio seek point index (ID3v2.4+ only)
  868. // Indexed data start (S) $xx xx xx xx
  869. // Indexed data length (L) $xx xx xx xx
  870. // Number of index points (N) $xx xx
  871. // Bits per index point (b) $xx
  872. // Then for every index point the following data is included:
  873. // Fraction at index (Fi) $xx (xx)
  874. if (!IsWithinBitRange($frame_data['datastart'], 32, FALSE)) {
  875. $error .= 'Invalid Indexed Data Start in '.$frame_name.' ('.$frame_data['datastart'].') (range = 0 to 4294967295)<BR>';
  876. } else if (!IsWithinBitRange($frame_data['datalength'], 32, FALSE)) {
  877. $error .= 'Invalid Indexed Data Length in '.$frame_name.' ('.$frame_data['datalength'].') (range = 0 to 4294967295)<BR>';
  878. } else if (!IsWithinBitRange($frame_data['indexpoints'], 16, FALSE)) {
  879. $error .= 'Invalid Number Of Index Points in '.$frame_name.' ('.$frame_data['indexpoints'].') (range = 0 to 65535)<BR>';
  880. } else if (!IsWithinBitRange($frame_data['bitsperpoint'], 8, FALSE)) {
  881. $error .= 'Invalid Bits Per Index Point in '.$frame_name.' ('.$frame_data['bitsperpoint'].') (range = 0 to 255)<BR>';
  882. } else if ($frame_data['indexpoints'] != count($frame_data['indexes'])) {
  883. $error .= 'Number Of Index Points does not match actual supplied data in '.$frame_name.'<BR>';
  884. } else {
  885. $framedata .= BigEndian2String($frame_data['datastart'], 4, FALSE);
  886. $framedata .= BigEndian2String($frame_data['datalength'], 4, FALSE);
  887. $framedata .= BigEndian2String($frame_data['indexpoints'], 2, FALSE);
  888. $framedata .= BigEndian2String($frame_data['bitsperpoint'], 1, FALSE);
  889. foreach ($frame_data['indexes'] as $key => $val) {
  890. $framedata .= BigEndian2String($val, ceil($frame_data['bitsperpoint'] / 8), FALSE);
  891. }
  892. }
  893. break;
  894. case 'RGAD':
  895. // RGAD Replay Gain Adjustment
  896. // http://privatewww.essex.ac.uk/~djmrob/replaygain/
  897. // Peak Amplitude $xx $xx $xx $xx
  898. // Radio Replay Gain Adjustment %aaabbbcd %dddddddd
  899. // Audiophile Replay Gain Adjustment %aaabbbcd %dddddddd
  900. // a - name code
  901. // b - originator code
  902. // c - sign bit
  903. // d - replay gain adjustment
  904. if (($frame_data['radio_adjustment'] > 51) || ($frame_data['radio_adjustment'] < -51)) {
  905. $error .= 'Invalid Radio Adjustment in '.$frame_name.' ('.$frame_data['radio_adjustment'].') (range = -51.0 to +51.0)<BR>';
  906. } else if (($frame_data['audiophile_adjustment'] > 51) || ($frame_data['audiophile_adjustment'] < -51)) {
  907. $error .= 'Invalid Audiophile Adjustment in '.$frame_name.' ('.$frame_data['audiophile_adjustment'].') (range = -51.0 to +51.0)<BR>';
  908. } else if (!IsValidRGADname($frame_data['raw']['radio_name'], $majorversion)) {
  909. $error .= 'Invalid Radio Name Code in '.$frame_name.' ('.$frame_data['raw']['radio_name'].') (range = 0 to 2)<BR>';
  910. } else if (!IsValidRGADname($frame_data['raw']['audiophile_name'], $majorversion)) {
  911. $error .= 'Invalid Audiophile Name Code in '.$frame_name.' ('.$frame_data['raw']['audiophile_name'].') (range = 0 to 2)<BR>';
  912. } else if (!IsValidRGADoriginator($frame_data['raw']['radio_originator'], $majorversion)) {
  913. $error .= 'Invalid Radio Originator Code in '.$frame_name.' ('.$frame_data['raw']['radio_originator'].') (range = 0 to 3)<BR>';
  914. } else if (!IsValidRGADoriginator($frame_data['raw']['audiophile_originator'], $majorversion)) {
  915. $error .= 'Invalid Audiophile Originator Code in '.$frame_name.' ('.$frame_data['raw']['audiophile_originator'].') (range = 0 to 3)<BR>';
  916. } else {
  917. $framedata .= Float2String($frame_data['peakamplitude'], 32);
  918. $framedata .= RGADgainString($frame_data['raw']['radio_name'], $frame_data['raw']['radio_originator'], $frame_data['radio_adjustment']);
  919. $framedata .= RGADgainString($frame_data['raw']['audiophile_name'], $frame_data['raw']['audiophile_originator'], $frame_data['audiophile_adjustment']);
  920. }
  921. break;
  922. default:
  923. if ($frame_name{0} == 'T') {
  924. // 4.2. T??? Text information frames
  925. // Text encoding $xx
  926. // Information <text string(s) according to encoding>
  927. if (!IsValidTextEncoding($frame_data['encodingid'], $majorversion)) {
  928. $error .= 'Invalid Text Encoding in '.$frame_name.' ('.$frame_data['encodingid'].') for ID3v2.'.$majorversion.'<BR>';
  929. } else {
  930. $framedata .= chr($frame_data['encodingid']);
  931. $framedata .= $frame_data['data'];
  932. }
  933. } else if ($frame_name{0} == 'W') {
  934. // 4.3. W??? URL link frames
  935. // URL <text string>
  936. if (!IsValidURL($frame_data['url'], FALSE, FALSE)) {
  937. $error .= 'Invalid URL in '.$frame_name.' ('.$frame_data['url'].')<BR>';
  938. } else {
  939. $framedata .= $frame_data['url'];
  940. }
  941. } else {
  942. $error .= $frame_name.' not yet supported in putid3.php<BR>';
  943. }
  944. break;
  945. }
  946. }
  947. if ($error) {
  948. if ($showerrors) {
  949. echo $error;
  950. }
  951. return FALSE;
  952. } else {
  953. return $framedata;
  954. }
  955. }
  956. function ID3v2FrameIsAllowed($frame_name, $frame_data, $majorversion, $showerrors=FALSE) {
  957. static $PreviousFrames = array();
  958. if ($frame_name === NULL) {
  959. // if the writing functions are called multiple times, the static array needs to be
  960. // cleared - this can be done by calling ID3v2FrameIsAllowed(NULL, '', '')
  961. $PreviousFrames = array();
  962. return TRUE;
  963. }
  964. if ($majorversion == 4) {
  965. switch ($frame_name) {
  966. case 'UFID':
  967. case 'AENC':
  968. case 'ENCR':
  969. case 'GRID':
  970. if (in_array($frame_name.$frame_data['ownerid'], $PreviousFrames)) {
  971. $error .= 'Only one '.$frame_name.' tag allowed with the same OwnerID ('.$frame_data['ownerid'].')<BR>';
  972. } else {
  973. $PreviousFrames[] = $frame_name.$frame_data['ownerid'];
  974. }
  975. break;
  976. case 'TXXX':
  977. case 'WXXX':
  978. case 'RVA2':
  979. case 'EQU2':
  980. case 'APIC':
  981. case 'GEOB':
  982. if (in_array($frame_name.$frame_data['description'], $PreviousFrames)) {
  983. $error .= 'Only one '.$frame_name.' tag allowed with the same Description ('.$frame_data['description'].')<BR>';
  984. } else {
  985. $PreviousFrames[] = $frame_name.$frame_data['description'];
  986. }
  987. break;
  988. case 'USER':
  989. if (in_array($frame_name.$frame_data['language'], $PreviousFrames)) {
  990. $error .= 'Only one '.$frame_name.' tag allowed with the same Language ('.$frame_data['language'].')<BR>';
  991. } else {
  992. $PreviousFrames[] = $frame_name.$frame_data['language'];
  993. }
  994. break;
  995. case 'USLT':
  996. case 'SYLT':
  997. case 'COMM':
  998. if (in_array($frame_name.$frame_data['language'].$frame_data['description'], $PreviousFrames)) {
  999. $error .= 'Only one '.$frame_name.' tag allowed with the same Language + Description ('.$frame_data['language'].' + '.$frame_data['description'].')<BR>';
  1000. } else {
  1001. $PreviousFrames[] = $frame_name.$frame_data['language'].$frame_data['description'];
  1002. }
  1003. break;
  1004. case 'POPM':
  1005. if (in_array($frame_name.$frame_data['email'], $PreviousFrames)) {
  1006. $error .= 'Only one '.$frame_name.' tag allowed with the same Email ('.$frame_data['email'].')<BR>';
  1007. } else {
  1008. $PreviousFrames[] = $frame_name.$frame_data['email'];
  1009. }
  1010. break;
  1011. case 'IPLS':
  1012. case 'MCDI':
  1013. case 'ETCO':
  1014. case 'MLLT':
  1015. case 'SYTC':
  1016. case 'RVRB':
  1017. case 'PCNT':
  1018. case 'RBUF':
  1019. case 'POSS':
  1020. case 'OWNE':
  1021. case 'SEEK':
  1022. case 'ASPI':
  1023. case 'RGAD':
  1024. if (in_array($frame_name, $PreviousFrames)) {
  1025. $error .= 'Only one '.$frame_name.' tag allowed<BR>';
  1026. } else {
  1027. $PreviousFrames[] = $frame_name;
  1028. }
  1029. break;
  1030. case 'LINK':
  1031. // this isn't implemented quite right (yet) - it should check the target frame data for compliance
  1032. // but right now it just allows one linked frame of each type, to be safe.
  1033. if (in_array($frame_name.$frame_data['frameid'], $PreviousFrames)) {
  1034. $error .= 'Only one '.$frame_name.' tag allowed with the same FrameID ('.$frame_data['frameid'].')<BR>';
  1035. } else if (in_array($frame_data['frameid'], $PreviousFrames)) {
  1036. // no links to singleton tags
  1037. $error .= 'Cannot specify a '.$frame_name.' tag to a singleton tag that already exists ('.$frame_data['frameid'].')<BR>';
  1038. } else {
  1039. $PreviousFrames[] = $frame_name.$frame_data['frameid']; // only one linked tag of this type
  1040. $PreviousFrames[] = $frame_data['frameid']; // no non-linked singleton tags of this type
  1041. }
  1042. break;
  1043. case 'COMR':
  1044. // There may be more than one 'commercial frame' in a tag, but no two may be identical
  1045. // Checking isn't implemented at all (yet) - just assumes that it's OK.
  1046. break;
  1047. case 'PRIV':
  1048. case 'SIGN':
  1049. if (in_array($frame_name.$frame_data['ownerid'].$frame_data['data'], $PreviousFrames)) {
  1050. $error .= 'Only one '.$frame_name.' tag allowed with the same OwnerID + Data ('.$frame_data['ownerid'].' + '.$frame_data['data'].')<BR>';
  1051. } else {
  1052. $PreviousFrames[] = $frame_name.$frame_data['ownerid'].$frame_data['data'];
  1053. }
  1054. break;
  1055. default:
  1056. if (($frame_name{0} != 'T') && ($frame_name{0} != 'W')) {
  1057. $error .= 'Frame not allowed in ID3v2.'.$majorversion.': '.$frame_name.'<BR>';
  1058. }
  1059. break;
  1060. }
  1061. } else if ($majorversion == 3) {
  1062. switch ($frame_name) {
  1063. case 'UFID':
  1064. case 'AENC':
  1065. case 'ENCR':
  1066. case 'GRID':
  1067. if (in_array($frame_name.$frame_data['ownerid'], $PreviousFrames)) {
  1068. $error .= 'Only one '.$frame_name.' tag allowed with the same OwnerID ('.$frame_data['ownerid'].')<BR>';
  1069. } else {
  1070. $PreviousFrames[] = $frame_name.$frame_data['ownerid'];
  1071. }
  1072. break;
  1073. case 'TXXX':
  1074. case 'WXXX':
  1075. case 'APIC':
  1076. case 'GEOB':
  1077. if (in_array($frame_name.$frame_data['description'], $PreviousFrames)) {
  1078. $error .= 'Only one '.$frame_name.' tag allowed with the same Description ('.$frame_data['description'].')<BR>';
  1079. } else {
  1080. $PreviousFrames[] = $frame_name.$frame_data['description'];
  1081. }
  1082. break;
  1083. case 'USER':
  1084. if (in_array($frame_name.$frame_data['language'], $PreviousFrames)) {
  1085. $error .= 'Only one '.$frame_name.' tag allowed with the same Language ('.$frame_data['language'].')<BR>';
  1086. } else {
  1087. $PreviousFrames[] = $frame_name.$frame_data['language'];
  1088. }
  1089. break;
  1090. case 'USLT':
  1091. case 'SYLT':
  1092. case 'COMM':
  1093. if (in_array($frame_name.$frame_data['language'].$frame_data['description'], $PreviousFrames)) {
  1094. $error .= 'Only one '.$frame_name.' tag allowed with the same Language + Description ('.$frame_data['language'].' + '.$frame_data['description'].')<BR>';
  1095. } else {
  1096. $PreviousFrames[] = $frame_name.$frame_data['language'].$frame_data['description'];
  1097. }
  1098. break;
  1099. case 'POPM':
  1100. if (in_array($frame_name.$frame_data['email'], $PreviousFrames)) {
  1101. $error .= 'Only one '.$frame_name.' tag allowed with the same Email ('.$frame_data['email'].')<BR>';
  1102. } else {
  1103. $PreviousFrames[] = $frame_name.$frame_data['email'];
  1104. }
  1105. break;
  1106. case 'IPLS':
  1107. case 'MCDI':
  1108. case 'ETCO':
  1109. case 'MLLT':
  1110. case 'SYTC':
  1111. case 'RVAD':
  1112. case 'EQUA':
  1113. case 'RVRB':
  1114. case 'PCNT':
  1115. case 'RBUF':
  1116. case 'POSS':
  1117. case 'OWNE':
  1118. case 'RGAD':
  1119. if (in_array($frame_name, $PreviousFrames)) {
  1120. $error .= 'Only one '.$frame_name.' tag allowed<BR>';
  1121. } else {
  1122. $PreviousFrames[] = $frame_name;
  1123. }
  1124. break;
  1125. case 'LINK':
  1126. // this isn't implemented quite right (yet) - it should check the target frame data for compliance
  1127. // but right now it just allows one linked frame of each type, to be safe.
  1128. if (in_array($frame_name.$frame_data['frameid'], $PreviousFrames)) {
  1129. $error .= 'Only one '.$frame_name.' tag allowed with the same FrameID ('.$frame_data['frameid'].')<BR>';
  1130. } else if (in_array($frame_data['frameid'], $PreviousFrames)) {
  1131. // no links to singleton tags
  1132. $error .= 'Cannot specify a '.$frame_name.' tag to a singleton tag that already exists ('.$frame_data['frameid'].')<BR>';
  1133. } else {
  1134. $PreviousFrames[] = $frame_name.$frame_data['frameid']; // only one linked tag of this type
  1135. $PreviousFrames[] = $frame_data['frameid']; // no non-linked singleton tags of this type
  1136. }
  1137. break;
  1138. case 'COMR':
  1139. // There may be more than one 'commercial frame' in a tag, but no two may be identical
  1140. // Checking isn't implemented at all (yet) - just assumes that it's OK.
  1141. break;
  1142. case 'PRIV':
  1143. if (in_array($frame_name.$frame_data['ownerid'].$frame_data['data'], $PreviousFrames)) {
  1144. $error .= 'Only one '.$frame_name.' tag allowed with the same OwnerID + Data ('.$frame_data['ownerid'].' + '.$frame_data['data'].')<BR>';
  1145. } else {
  1146. $PreviousFrames[] = $frame_name.$frame_data['ownerid'].$frame_data['data'];
  1147. }
  1148. break;
  1149. default:
  1150. if (($frame_name{0} != 'T') && ($frame_name{0} != 'W')) {
  1151. $error .= 'Frame not allowed in ID3v2.'.$majorversion.': '.$frame_name.'<BR>';
  1152. }
  1153. break;
  1154. }
  1155. } else if ($majorversion == 2) {
  1156. switch ($frame_name) {
  1157. case 'UFI':
  1158. case 'CRM':
  1159. case 'CRA':
  1160. if (in_array($frame_name.$frame_data['ownerid'], $PreviousFrames)) {
  1161. $error .= 'Only one '.$frame_name.' tag allowed with the same OwnerID ('.$frame_data['ownerid'].')<BR>';
  1162. } else {
  1163. $PreviousFrames[] = $frame_name.$frame_data['ownerid'];
  1164. }
  1165. break;
  1166. case 'TXX':
  1167. case 'WXX':
  1168. case 'PIC':
  1169. case 'GEO':
  1170. if (in_array($frame_name.$frame_data['description'], $PreviousFrames)) {
  1171. $error .= 'Only one '.$frame_name.' tag allowed with the same Description ('.$frame_data['description'].')<BR>';
  1172. } else {
  1173. $PreviousFrames[] = $frame_name.$frame_data['description'];
  1174. }
  1175. break;
  1176. case 'ULT':
  1177. case 'SLT':
  1178. case 'COM':
  1179. if (in_array($frame_name.$frame_data['language'].$frame_data['description'], $PreviousFrames)) {
  1180. $error .= 'Only one '.$frame_name.' tag allowed with the same Language + Description ('.$frame_data['language'].' + '.$frame_data['description'].')<BR>';
  1181. } else {
  1182. $PreviousFrames[] = $frame_name.$frame_data['language'].$frame_data['description'];
  1183. }
  1184. break;
  1185. case 'POP':
  1186. if (in_array($frame_name.$frame_data['email'], $PreviousFrames)) {
  1187. $error .= 'Only one '.$frame_name.' tag allowed with the same Email ('.$frame_data['email'].')<BR>';
  1188. } else {
  1189. $PreviousFrames[] = $frame_name.$frame_data['email'];
  1190. }
  1191. break;
  1192. case 'IPL':
  1193. case 'MCI':
  1194. case 'ETC':
  1195. case 'MLL':
  1196. case 'STC':
  1197. case 'RVA':
  1198. case 'EQU':
  1199. case 'REV':
  1200. case 'CNT':
  1201. case 'BUF':
  1202. if (in_array($frame_name, $PreviousFrames)) {
  1203. $error .= 'Only one '.$frame_name.' tag allowed<BR>';
  1204. } else {
  1205. $PreviousFrames[] = $frame_name;
  1206. }
  1207. break;
  1208. case 'LNK':
  1209. // this isn't implemented quite right (yet) - it should check the target frame data for compliance
  1210. // but right now it just allows one linked frame of each type, to be safe.
  1211. if (in_array($frame_name.$frame_data['frameid'], $PreviousFrames)) {
  1212. $error .= 'Only one '.$frame_name.' tag allowed with the same FrameID ('.$frame_data['frameid'].')<BR>';
  1213. } else if (in_array($frame_data['frameid'], $PreviousFrames)) {
  1214. // no links to singleton tags
  1215. $error .= 'Cannot specify a '.$frame_name.' tag to a singleton tag that already exists ('.$frame_data['frameid'].')<BR>';
  1216. } else {
  1217. $PreviousFrames[] = $frame_name.$frame_data['frameid']; // only one linked tag of this type
  1218. $PreviousFrames[] = $frame_data['frameid']; // no non-linked singleton tags of this type
  1219. }
  1220. break;
  1221. default:
  1222. if (($frame_name{0} != 'T') && ($frame_name{0} != 'W')) {
  1223. $error .= 'Frame not allowed in ID3v2.'.$majorversion.': '.$frame_name.'<BR>';
  1224. }
  1225. break;
  1226. }
  1227. }
  1228. if ($error) {
  1229. if ($showerrors) {
  1230. echo $error;
  1231. }
  1232. return FALSE;
  1233. } else {
  1234. return TRUE;
  1235. }
  1236. }
  1237. function GenerateID3v2Tag($data, $majorversion=4, $minorversion=0, $paddedlength=0, $extendedheader='', $footer=FALSE, $showerrors=TRUE, $noerrorsonly=TRUE) {
  1238. ID3v2FrameIsAllowed(NULL, '', ''); // clear static array in case this isn't the first call to GenerateID3v2Tag()
  1239. if (is_array($data)) {
  1240. if (is_array($extendedheader)) {
  1241. // not supported yet
  1242. }
  1243. foreach ($data as $frame_name => $frame_rawinputdata) {
  1244. if (!is_array($frame_rawinputdata[0])) {
  1245. // force everything to be arrayed so only one processing loop
  1246. $frame_rawinputdata = array($frame_rawinputdata);
  1247. }
  1248. foreach ($frame_rawinputdata as $irrelevantindex => $frame_inputdata) {
  1249. if (IsValidID3v2FrameName($frame_name, $majorversion)) {
  1250. unset($frame_length);
  1251. unset($frame_flags);
  1252. $frame_data = FALSE;
  1253. if (ID3v2FrameIsAllowed($frame_name, $frame_inputdata, $majorversion, $showerrors)) {
  1254. if ($frame_data = GenerateID3v2FrameData($frame_name, $frame_inputdata, $majorversion, $showerrors)) {
  1255. if ($majorversion >= 4) {
  1256. // frame-level unsynchronization
  1257. $FrameUnsynchronisation = FALSE;
  1258. $unsynchdata = Unsynchronise($frame_data);
  1259. if (strlen($unsynchdata) != strlen($frame_data)) {
  1260. // unsynchronization needed
  1261. $FrameUnsynchronisation = TRUE;
  1262. $frame_data = $unsynchdata;
  1263. if (isset($TagUnsynchronisation) && $TagUnsynchronisation === FALSE) {
  1264. // only set to true if ALL frames are unsynchronised
  1265. } else {
  1266. $TagUnsynchronisation = TRUE;
  1267. }
  1268. } else {
  1269. if (isset($TagUnsynchronisation)) {
  1270. $TagUnsynchronisation = FALSE;
  1271. }
  1272. }
  1273. unset($unsynchdata);
  1274. $frame_length = BigEndian2String(strlen($frame_data), 4, TRUE);
  1275. } else {
  1276. $frame_length = BigEndian2String(strlen($frame_data), 4, FALSE);
  1277. }
  1278. $frame_flags = GenerateID3v2FrameFlags($majorversion, ID3v2FrameFlagsLookupTagAlter($frame_name, $majorversion), ID3v2FrameFlagsLookupFileAlter($frame_name, $majorversion), FALSE, FALSE, FALSE, FALSE, $FrameUnsynchronisation, FALSE);
  1279. }
  1280. } else {
  1281. if ($showerrors) {
  1282. echo 'Frame "'.$frame_name.'" is NOT allowed<BR>';
  1283. }
  1284. }
  1285. if ($frame_data === FALSE) {
  1286. if ($showerrors) {
  1287. echo 'GenerateID3v2FrameData() failed for "'.$frame_name.'"<BR>';
  1288. echo 'Error generated in getID3() v'.GETID3VERSION.'<BR>';
  1289. }
  1290. if ($noerrorsonly) {
  1291. return FALSE;
  1292. } else {
  1293. unset($frame_name);
  1294. }
  1295. }
  1296. } else {
  1297. // ignore any invalid frame names, including 'title', 'header', etc
  1298. unset($frame_name);
  1299. unset($frame_length);
  1300. unset($frame_flags);
  1301. unset($frame_data);
  1302. }
  1303. $tagstring .= $frame_name.$frame_length.$frame_flags.$frame_data;
  1304. }
  1305. }
  1306. if ($footer) {
  1307. if ($showerrors) {
  1308. echo 'Footer not supported (yet)<BR>';
  1309. }
  1310. return FALSE;
  1311. }
  1312. //echo number_format(strlen($tagstring));
  1313. if ($majorversion <= 3) {
  1314. // tag-level unsynchronization
  1315. $unsynchdata = Unsynchronise($tagstring);
  1316. if (strlen($unsynchdata) != strlen($tagstring)) {
  1317. // unsynchronization needed
  1318. $TagUnsynchronisation = TRUE;
  1319. $tagstring = $unsynchdata;
  1320. }
  1321. }
  1322. //echo ' - '.number_format(strlen($tagstring)).'<BR>';
  1323. if (!$footer && ($paddedlength > (strlen($tagstring) + ID3v2HeaderLength($id3info['majorversion'])))) {
  1324. // pad up to $paddedlength bytes if unpadded tag is shorter than $paddedlength
  1325. // "Furthermore it MUST NOT have any padding when a tag footer is added to the tag."
  1326. $tagstring .= @str_repeat(chr(0), $paddedlength - strlen($tagstring));
  1327. }
  1328. if (substr($tagstring, strlen($tagstring) - 1, 1) == chr(255)) {
  1329. // special unsynchronization case:
  1330. // if last byte == $FF then appended a $00
  1331. $TagUnsynchronisation = TRUE;
  1332. $tagstring .= chr(0);
  1333. }
  1334. $tagheader = 'ID3';
  1335. $tagheader .= chr($majorversion);
  1336. $tagheader .= chr($minorversion);
  1337. $tagheader .= GenerateID3v2TagFlags($majorversion, $TagUnsynchronisation, FALSE, (bool) $extendedheader, FALSE, $footer);
  1338. $tagheader .= BigEndian2String(strlen($tagstring), 4, TRUE);
  1339. return $tagheader.$tagstring;
  1340. } else {
  1341. return FALSE;
  1342. }
  1343. }
  1344. function WriteID3v2($filename, $data, $majorversion=4, $minorversion=0, $overwrite=FALSE, $paddedlength=0, $showerrors=FALSE) {
  1345. // File MUST be writeable - CHMOD(646) at least. It's best if the
  1346. // directory is also writeable, because that method is both faster and less susceptible to errors.
  1347. if (is_writeable($filename) || (!file_exists($filename) && is_writeable(dirname($filename)))) {
  1348. $OldMP3fileInfo = GetAllMP3info($filename);
  1349. if ($overwrite) {
  1350. // ignore previous data
  1351. } else {
  1352. // merge with existing data
  1353. $data = array_join_merge($OldMP3fileInfo, $data);
  1354. $paddedlength = max($OldMP3fileInfo['id3']['id3v2']['headerlength'], $paddedlength);
  1355. }
  1356. if ($NewID3v2Tag = GenerateID3v2Tag($data['id3']['id3v2'], $majorversion, $minorversion, $paddedlength, '', FALSE, $showerrors, TRUE)) {
  1357. if ((!file_exists($filename) && is_writeable(dirname($filename))) || (is_writeable($filename) && ($OldMP3fileInfo['id3']['id3v2']['headerlength'] == strlen($NewID3v2Tag)))) {
  1358. // best and fastest method - insert-overwrite existing tag (padded to length of old tag if neccesary)
  1359. if (file_exists($filename)) {
  1360. if ($fp = @fopen($filename, 'r+b')) {
  1361. rewind($fp);
  1362. fwrite($fp, $NewID3v2Tag, strlen($NewID3v2Tag));
  1363. fclose($fp);
  1364. } else {
  1365. $error .= 'Could not open '.$filename.' mode "r+b"<BR>';
  1366. }
  1367. } else {
  1368. if ($fp = @fopen($filename, 'wb')) {
  1369. rewind($fp);
  1370. fwrite($fp, $NewID3v2Tag, strlen($NewID3v2Tag));
  1371. fclose($fp);
  1372. } else {
  1373. $error .= 'Could not open '.$filename.' mode "wb"<BR>';
  1374. }
  1375. }
  1376. } else {
  1377. // new tag is longer than old tag - must rewrite entire file
  1378. if (is_writeable(dirname($filename))) {
  1379. // preferred alternate method - only one copying operation, minimal chance of corrupting
  1380. // original file if script is interrupted, but required directory to be writeable
  1381. if ($OldMP3fileInfo['audiobytes'] > 0) {
  1382. if ($fp_source = @fopen($filename, 'rb')) {
  1383. rewind($fp_source);
  1384. if ($OldMP3fileInfo['audiodataoffset'] !== FALSE) {
  1385. fseek($fp_source, $OldMP3fileInfo['audiodataoffset'], SEEK_SET);
  1386. }
  1387. if ($fp_temp = @fopen($filename.'getid3tmp', 'wb')) {
  1388. fwrite($fp_temp, $NewID3v2Tag, strlen($NewID3v2Tag));
  1389. // while (($buffer = fread($fp_source, FREAD_BUFFER_SIZE)) !== FALSE) {
  1390. while ($buffer = fread($fp_source, FREAD_BUFFER_SIZE)) {
  1391. fwrite($fp_temp, $buffer, strlen($buffer));
  1392. }
  1393. fclose($fp_temp);
  1394. } else {
  1395. $error .= 'Could not open '.$filename.'getid3tmp mode "wb"<BR>';
  1396. }
  1397. fclose($fp_source);
  1398. } else { // no previous audiodata
  1399. if ($fp_temp = @fopen($filename.'getid3tmp', 'wb')) {
  1400. fwrite($fp_temp, $NewID3v2Tag, strlen($NewID3v2Tag));
  1401. fclose($fp_temp);
  1402. }
  1403. }
  1404. } else {
  1405. $error .= 'Could not open '.$filename.' mode "rb"<BR>';
  1406. }
  1407. if (file_exists($filename)) {
  1408. unlink($filename);
  1409. }
  1410. rename($filename.'getid3tmp', $filename);
  1411. } else {
  1412. // less desirable alternate method - double-copies the file, overwrites original file
  1413. // and could corrupt source file if the script is interrupted or an error occurs.
  1414. if ($fp_source = @fopen($filename, 'rb')) {
  1415. rewind($fp_source);
  1416. if ($OldMP3fileInfo['audiodataoffset'] !== FALSE) {
  1417. fseek($fp_source, $OldMP3fileInfo['audiodataoffset'], SEEK_SET);
  1418. }
  1419. if ($fp_temp = tmpfile()) {
  1420. fwrite($fp_temp, $NewID3v2Tag, strlen($NewID3v2Tag));
  1421. // while (($buffer = fread($fp_source, FREAD_BUFFER_SIZE)) !== FALSE) {
  1422. while ($buffer = fread($fp_source, FREAD_BUFFER_SIZE)) {
  1423. fwrite($fp_temp, $buffer, strlen($buffer));
  1424. }
  1425. fclose($fp_source);
  1426. if ($fp_source = @fopen($filename, 'wb')) {
  1427. rewind($fp_temp);
  1428. // while (($buffer = fread($fp_temp, FREAD_BUFFER_SIZE)) !== FALSE) {
  1429. while ($buffer = fread($fp_temp, FREAD_BUFFER_SIZE)) {
  1430. fwrite($fp_source, $buffer, strlen($buffer));
  1431. }
  1432. fseek($fp_temp, -128, SEEK_END);
  1433. fclose($fp_source);
  1434. } else {
  1435. $error .= 'Could not open '.$filename.' mode "wb"<BR>';
  1436. }
  1437. fclose($fp_temp);
  1438. } else {
  1439. $error .= 'Could not create tmpfile()<BR>';
  1440. }
  1441. } else {
  1442. $error .= 'Could not open '.$filename.' mode "rb"<BR>';
  1443. }
  1444. }
  1445. }
  1446. } else {
  1447. $error .= 'GenerateID3v2Tag() failed<BR>';
  1448. }
  1449. if ($error) {
  1450. if ($showerrors) {
  1451. echo $error;
  1452. }
  1453. return FALSE;
  1454. } else {
  1455. return TRUE;
  1456. }
  1457. } else {
  1458. return FALSE;
  1459. }
  1460. }
  1461. function RemoveID3v2($filename, $showerrors=FALSE) {
  1462. // File MUST be writeable - CHMOD(646) at least. It's best if the
  1463. // directory is also writeable, because that method is both faster and less susceptible to errors.
  1464. if (is_writeable(dirname($filename))) {
  1465. // preferred method - only one copying operation, minimal chance of corrupting
  1466. // original file if script is interrupted, but required directory to be writeable
  1467. if ($fp_source = @fopen($filename, 'rb')) {
  1468. $OldMP3fileInfo = GetAllMP3info($filename);
  1469. rewind($fp_source);
  1470. if ($OldMP3fileInfo['audiodataoffset'] !== FALSE) {
  1471. fseek($fp_source, $OldMP3fileInfo['audiodataoffset'], SEEK_SET);
  1472. }
  1473. if ($fp_temp = @fopen($filename.'getid3tmp', 'w+b')) {
  1474. // while (($buffer = fread($fp_source, FREAD_BUFFER_SIZE)) !== FALSE) {
  1475. while ($buffer = fread($fp_source, FREAD_BUFFER_SIZE)) {
  1476. fwrite($fp_temp, $buffer, strlen($buffer));
  1477. }
  1478. fclose($fp_temp);
  1479. } else {
  1480. $error .= 'Could not open '.$filename.'getid3tmp mode "w+b"<BR>';
  1481. }
  1482. fclose($fp_source);
  1483. } else {
  1484. $error .= 'Could not open '.$filename.' mode "rb"<BR>';
  1485. }
  1486. if (file_exists($filename)) {
  1487. unlink($filename);
  1488. }
  1489. rename($filename.'getid3tmp', $filename);
  1490. } else if (is_writable($filename)) {
  1491. // less desirable alternate method - double-copies the file, overwrites original file
  1492. // and could corrupt source file if the script is interrupted or an error occurs.
  1493. if ($fp_source = @fopen($filename, 'rb')) {
  1494. $OldMP3fileInfo = GetAllMP3info($filename);
  1495. rewind($fp_source);
  1496. if ($OldMP3fileInfo['audiodataoffset'] !== FALSE) {
  1497. fseek($fp_source, $OldMP3fileInfo['audiodataoffset'], SEEK_SET);
  1498. }
  1499. if ($fp_temp = tmpfile()) {
  1500. // while (($buffer = fread($fp_source, FREAD_BUFFER_SIZE)) !== FALSE) {
  1501. while ($buffer = fread($fp_source, FREAD_BUFFER_SIZE)) {
  1502. fwrite($fp_temp, $buffer, strlen($buffer));
  1503. }
  1504. fclose($fp_source);
  1505. if ($fp_source = @fopen($filename, 'wb')) {
  1506. rewind($fp_temp);
  1507. // while (($buffer = fread($fp_temp, FREAD_BUFFER_SIZE)) !== FALSE) {
  1508. while ($buffer = fread($fp_temp, FREAD_BUFFER_SIZE)) {
  1509. fwrite($fp_source, $buffer, strlen($buffer));
  1510. }
  1511. fseek($fp_temp, -128, SEEK_END);
  1512. fclose($fp_source);
  1513. } else {
  1514. $error .= 'Could not open '.$filename.' mode "wb"<BR>';
  1515. }
  1516. fclose($fp_temp);
  1517. } else {
  1518. $error .= 'Could not create tmpfile()<BR>';
  1519. }
  1520. } else {
  1521. $error .= 'Could not open '.$filename.' mode "rb"<BR>';
  1522. }
  1523. } else {
  1524. $error .= 'Directory and file both not writeable<BR>';
  1525. }
  1526. if ($error) {
  1527. if ($showerrors) {
  1528. echo $error;
  1529. }
  1530. return FALSE;
  1531. } else {
  1532. return TRUE;
  1533. }
  1534. }
  1535. function GenerateID3v1Tag($title, $artist, $album, $year, $genre, $comment, $track) {
  1536. $ID3v1Tag .= 'TAG';
  1537. $ID3v1Tag .= str_pad(substr($title, 0, 30), 30, chr(0), STR_PAD_RIGHT);
  1538. $ID3v1Tag .= str_pad(substr($artist, 0, 30), 30, chr(0), STR_PAD_RIGHT);
  1539. $ID3v1Tag .= str_pad(substr($album, 0, 30), 30, chr(0), STR_PAD_RIGHT);
  1540. $ID3v1Tag .= str_pad(substr($year, 0, 4), 4, ' ', STR_PAD_LEFT);
  1541. if (isset($track) && ($track > 0) && ($track <= 255)) {
  1542. $ID3v1Tag .= str_pad(substr($comment, 0, 28), 28, chr(0), STR_PAD_RIGHT);
  1543. $ID3v1Tag .= chr(0);
  1544. if (gettype($track) == 'string') {
  1545. $track = (int) $track;
  1546. }
  1547. $ID3v1Tag .= chr($track);
  1548. } else {
  1549. $ID3v1Tag .= str_pad(substr($comment, 0, 30), 30, chr(0), STR_PAD_RIGHT);
  1550. }
  1551. if (($genre < 0) || ($genre > 147)) {
  1552. $genre = 255; // 'unknown' genre
  1553. }
  1554. if (gettype($genre) == 'string') {
  1555. $genrenumber = (int) $genre;
  1556. $ID3v1Tag .= chr($genrenumber);
  1557. } else if (gettype($genre) == 'integer') {
  1558. $ID3v1Tag .= chr($genre);
  1559. } else {
  1560. $ID3v1Tag .= chr(255); // 'unknown' genre
  1561. }
  1562. return $ID3v1Tag;
  1563. }
  1564. function WriteID3v1($filename, $title='', $artist='', $album='', $year='', $comment='', $genre=255, $track='', $showerrors=FALSE) {
  1565. // File MUST be writeable - CHMOD(646) at least
  1566. if (is_writeable($filename)) {
  1567. if ($fp_source = @fopen($filename, 'r+b')) {
  1568. fseek($fp_source, -128, SEEK_END);
  1569. if (fread($fp_source, 3) == 'TAG') {
  1570. fseek($fp_source, -128, SEEK_END); // overwrite existing ID3v1 tag
  1571. } else {
  1572. fseek($fp_source, 0, SEEK_END); // append new ID3v1 tag
  1573. }
  1574. fwrite($fp_source, GenerateID3v1Tag($title, $artist, $album, $year, $genre, $comment, $track), 128);
  1575. fclose($fp_source);
  1576. } else {
  1577. $error .= 'Could not open '.$filename.' mode "r+b"<BR>';
  1578. }
  1579. if ($error) {
  1580. if ($showerrors) {
  1581. echo $error;
  1582. }
  1583. return FALSE;
  1584. } else {
  1585. return TRUE;
  1586. }
  1587. } else {
  1588. return FALSE;
  1589. }
  1590. }
  1591. function RemoveID3v1($filename, $showerrors=FALSE) {
  1592. // File MUST be writeable - CHMOD(646) at least
  1593. if (is_writeable($filename)) {
  1594. if ($fp_source = @fopen($filename, 'r+b')) {
  1595. fseek($fp_source, -128, SEEK_END);
  1596. if (fread($fp_source, 3) == 'TAG') {
  1597. ftruncate($fp_source, filesize($filename) - 128);
  1598. } else {
  1599. // no ID3v1 tag to begin with - do nothing
  1600. }
  1601. fclose($fp_source);
  1602. } else {
  1603. $error .= 'Could not open '.$filename.' mode "r+b"<BR>';
  1604. }
  1605. if ($error) {
  1606. if ($showerrors) {
  1607. echo $error;
  1608. }
  1609. return FALSE;
  1610. } else {
  1611. return TRUE;
  1612. }
  1613. } else {
  1614. return FALSE;
  1615. }
  1616. }
  1617. ?>