PageRenderTime 64ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/getid3/write.id3v2.php

https://bitbucket.org/Dianoga/playlist-generator
PHP | 2050 lines | 1578 code | 160 blank | 312 comment | 454 complexity | d762cd4196a38acca81e2353a1e9d8dd MD5 | raw file
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at http://getid3.sourceforge.net //
  5. // or http://www.getid3.org //
  6. /////////////////////////////////////////////////////////////////
  7. // See readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. /// //
  10. // write.id3v2.php //
  11. // module for writing ID3v2 tags //
  12. // dependencies: module.tag.id3v2.php //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.id3v2.php', __FILE__, true);
  16. class getid3_write_id3v2
  17. {
  18. var $filename;
  19. var $tag_data;
  20. var $fread_buffer_size = 32768; // read buffer size in bytes
  21. var $paddedlength = 4096; // minimum length of ID3v2 tag in bytes
  22. var $majorversion = 3; // ID3v2 major version (2, 3 (recommended), 4)
  23. var $minorversion = 0; // ID3v2 minor version - always 0
  24. var $merge_existing_data = false; // if true, merge new data with existing tags; if false, delete old tag data and only write new tags
  25. var $id3v2_default_encodingid = 0; // default text encoding (ISO-8859-1) if not explicitly passed
  26. var $id3v2_use_unsynchronisation = false; // the specs say it should be TRUE, but most other ID3v2-aware programs are broken if unsynchronization is used, so by default don't use it.
  27. var $warnings = array(); // any non-critical errors will be stored here
  28. var $errors = array(); // any critical errors will be stored here
  29. function getid3_write_id3v2() {
  30. return true;
  31. }
  32. function WriteID3v2() {
  33. // File MUST be writeable - CHMOD(646) at least. It's best if the
  34. // directory is also writeable, because that method is both faster and less susceptible to errors.
  35. if (!empty($this->filename) && (is_writeable($this->filename) || (!file_exists($this->filename) && is_writeable(dirname($this->filename))))) {
  36. // Initialize getID3 engine
  37. $getID3 = new getID3;
  38. $OldThisFileInfo = $getID3->analyze($this->filename);
  39. if (!getid3_lib::intValueSupported($OldThisFileInfo['filesize'])) {
  40. $this->errors[] = 'Unable to write ID3v2 because file is larger than '.round(PHP_INT_MAX / 1073741824).'GB';
  41. fclose($fp_source);
  42. return false;
  43. }
  44. if ($this->merge_existing_data) {
  45. // merge with existing data
  46. if (!empty($OldThisFileInfo['id3v2'])) {
  47. $this->tag_data = $this->array_join_merge($OldThisFileInfo['id3v2'], $this->tag_data);
  48. }
  49. }
  50. $this->paddedlength = (isset($OldThisFileInfo['id3v2']['headerlength']) ? max($OldThisFileInfo['id3v2']['headerlength'], $this->paddedlength) : $this->paddedlength);
  51. if ($NewID3v2Tag = $this->GenerateID3v2Tag()) {
  52. if (file_exists($this->filename) && is_writeable($this->filename) && isset($OldThisFileInfo['id3v2']['headerlength']) && ($OldThisFileInfo['id3v2']['headerlength'] == strlen($NewID3v2Tag))) {
  53. // best and fastest method - insert-overwrite existing tag (padded to length of old tag if neccesary)
  54. if (file_exists($this->filename)) {
  55. if (is_readable($this->filename) && is_writable($this->filename) && is_file($this->filename) && ($fp = fopen($this->filename, 'r+b'))) {
  56. rewind($fp);
  57. fwrite($fp, $NewID3v2Tag, strlen($NewID3v2Tag));
  58. fclose($fp);
  59. } else {
  60. $this->errors[] = 'Could not fopen("'.$this->filename.'", "r+b")';
  61. }
  62. } else {
  63. if (is_writable($this->filename) && is_file($this->filename) && ($fp = fopen($this->filename, 'wb'))) {
  64. rewind($fp);
  65. fwrite($fp, $NewID3v2Tag, strlen($NewID3v2Tag));
  66. fclose($fp);
  67. } else {
  68. $this->errors[] = 'Could not fopen("'.$this->filename.'", "wb")';
  69. }
  70. }
  71. } else {
  72. if ($tempfilename = tempnam(GETID3_TEMP_DIR, 'getID3')) {
  73. if (is_readable($this->filename) && is_file($this->filename) && ($fp_source = fopen($this->filename, 'rb'))) {
  74. if (is_writable($tempfilename) && is_file($tempfilename) && ($fp_temp = fopen($tempfilename, 'wb'))) {
  75. fwrite($fp_temp, $NewID3v2Tag, strlen($NewID3v2Tag));
  76. rewind($fp_source);
  77. if (!empty($OldThisFileInfo['avdataoffset'])) {
  78. fseek($fp_source, $OldThisFileInfo['avdataoffset'], SEEK_SET);
  79. }
  80. while ($buffer = fread($fp_source, $this->fread_buffer_size)) {
  81. fwrite($fp_temp, $buffer, strlen($buffer));
  82. }
  83. fclose($fp_temp);
  84. fclose($fp_source);
  85. copy($tempfilename, $this->filename);
  86. unlink($tempfilename);
  87. return true;
  88. } else {
  89. $this->errors[] = 'Could not fopen("'.$tempfilename.'", "wb")';
  90. }
  91. fclose($fp_source);
  92. } else {
  93. $this->errors[] = 'Could not fopen("'.$this->filename.'", "rb")';
  94. }
  95. }
  96. return false;
  97. }
  98. } else {
  99. $this->errors[] = '$this->GenerateID3v2Tag() failed';
  100. }
  101. if (!empty($this->errors)) {
  102. return false;
  103. }
  104. return true;
  105. } else {
  106. $this->errors[] = 'WriteID3v2() failed: !is_writeable('.$this->filename.')';
  107. }
  108. return false;
  109. }
  110. function RemoveID3v2() {
  111. // File MUST be writeable - CHMOD(646) at least. It's best if the
  112. // directory is also writeable, because that method is both faster and less susceptible to errors.
  113. if (is_writeable(dirname($this->filename))) {
  114. // preferred method - only one copying operation, minimal chance of corrupting
  115. // original file if script is interrupted, but required directory to be writeable
  116. if (is_readable($this->filename) && is_file($this->filename) && ($fp_source = fopen($this->filename, 'rb'))) {
  117. // Initialize getID3 engine
  118. $getID3 = new getID3;
  119. $OldThisFileInfo = $getID3->analyze($this->filename);
  120. if (!getid3_lib::intValueSupported($OldThisFileInfo['filesize'])) {
  121. $this->errors[] = 'Unable to remove ID3v2 because file is larger than '.round(PHP_INT_MAX / 1073741824).'GB';
  122. fclose($fp_source);
  123. return false;
  124. }
  125. rewind($fp_source);
  126. if ($OldThisFileInfo['avdataoffset'] !== false) {
  127. fseek($fp_source, $OldThisFileInfo['avdataoffset'], SEEK_SET);
  128. }
  129. if (is_writable($this->filename) && is_file($this->filename) && ($fp_temp = fopen($this->filename.'getid3tmp', 'w+b'))) {
  130. while ($buffer = fread($fp_source, $this->fread_buffer_size)) {
  131. fwrite($fp_temp, $buffer, strlen($buffer));
  132. }
  133. fclose($fp_temp);
  134. } else {
  135. $this->errors[] = 'Could not fopen("'.$this->filename.'getid3tmp", "w+b")';
  136. }
  137. fclose($fp_source);
  138. } else {
  139. $this->errors[] = 'Could not fopen("'.$this->filename.'", "rb")';
  140. }
  141. if (file_exists($this->filename)) {
  142. unlink($this->filename);
  143. }
  144. rename($this->filename.'getid3tmp', $this->filename);
  145. } elseif (is_writable($this->filename)) {
  146. // less desirable alternate method - double-copies the file, overwrites original file
  147. // and could corrupt source file if the script is interrupted or an error occurs.
  148. if (is_readable($this->filename) && is_file($this->filename) && ($fp_source = fopen($this->filename, 'rb'))) {
  149. // Initialize getID3 engine
  150. $getID3 = new getID3;
  151. $OldThisFileInfo = $getID3->analyze($this->filename);
  152. if (!getid3_lib::intValueSupported($OldThisFileInfo['filesize'])) {
  153. $this->errors[] = 'Unable to remove ID3v2 because file is larger than '.round(PHP_INT_MAX / 1073741824).'GB';
  154. fclose($fp_source);
  155. return false;
  156. }
  157. rewind($fp_source);
  158. if ($OldThisFileInfo['avdataoffset'] !== false) {
  159. fseek($fp_source, $OldThisFileInfo['avdataoffset'], SEEK_SET);
  160. }
  161. if ($fp_temp = tmpfile()) {
  162. while ($buffer = fread($fp_source, $this->fread_buffer_size)) {
  163. fwrite($fp_temp, $buffer, strlen($buffer));
  164. }
  165. fclose($fp_source);
  166. if (is_writable($this->filename) && is_file($this->filename) && ($fp_source = fopen($this->filename, 'wb'))) {
  167. rewind($fp_temp);
  168. while ($buffer = fread($fp_temp, $this->fread_buffer_size)) {
  169. fwrite($fp_source, $buffer, strlen($buffer));
  170. }
  171. fseek($fp_temp, -128, SEEK_END);
  172. fclose($fp_source);
  173. } else {
  174. $this->errors[] = 'Could not fopen("'.$this->filename.'", "wb")';
  175. }
  176. fclose($fp_temp);
  177. } else {
  178. $this->errors[] = 'Could not create tmpfile()';
  179. }
  180. } else {
  181. $this->errors[] = 'Could not fopen("'.$this->filename.'", "rb")';
  182. }
  183. } else {
  184. $this->errors[] = 'Directory and file both not writeable';
  185. }
  186. if (!empty($this->errors)) {
  187. return false;
  188. }
  189. return true;
  190. }
  191. function GenerateID3v2TagFlags($flags) {
  192. switch ($this->majorversion) {
  193. case 4:
  194. // %abcd0000
  195. $flag = (!empty($flags['unsynchronisation']) ? '1' : '0'); // a - Unsynchronisation
  196. $flag .= (!empty($flags['extendedheader'] ) ? '1' : '0'); // b - Extended header
  197. $flag .= (!empty($flags['experimental'] ) ? '1' : '0'); // c - Experimental indicator
  198. $flag .= (!empty($flags['footer'] ) ? '1' : '0'); // d - Footer present
  199. $flag .= '0000';
  200. break;
  201. case 3:
  202. // %abc00000
  203. $flag = (!empty($flags['unsynchronisation']) ? '1' : '0'); // a - Unsynchronisation
  204. $flag .= (!empty($flags['extendedheader'] ) ? '1' : '0'); // b - Extended header
  205. $flag .= (!empty($flags['experimental'] ) ? '1' : '0'); // c - Experimental indicator
  206. $flag .= '00000';
  207. break;
  208. case 2:
  209. // %ab000000
  210. $flag = (!empty($flags['unsynchronisation']) ? '1' : '0'); // a - Unsynchronisation
  211. $flag .= (!empty($flags['compression'] ) ? '1' : '0'); // b - Compression
  212. $flag .= '000000';
  213. break;
  214. default:
  215. return false;
  216. break;
  217. }
  218. return chr(bindec($flag));
  219. }
  220. function GenerateID3v2FrameFlags($TagAlter=false, $FileAlter=false, $ReadOnly=false, $Compression=false, $Encryption=false, $GroupingIdentity=false, $Unsynchronisation=false, $DataLengthIndicator=false) {
  221. switch ($this->majorversion) {
  222. case 4:
  223. // %0abc0000 %0h00kmnp
  224. $flag1 = '0';
  225. $flag1 .= $TagAlter ? '1' : '0'; // a - Tag alter preservation (true == discard)
  226. $flag1 .= $FileAlter ? '1' : '0'; // b - File alter preservation (true == discard)
  227. $flag1 .= $ReadOnly ? '1' : '0'; // c - Read only (true == read only)
  228. $flag1 .= '0000';
  229. $flag2 = '0';
  230. $flag2 .= $GroupingIdentity ? '1' : '0'; // h - Grouping identity (true == contains group information)
  231. $flag2 .= '00';
  232. $flag2 .= $Compression ? '1' : '0'; // k - Compression (true == compressed)
  233. $flag2 .= $Encryption ? '1' : '0'; // m - Encryption (true == encrypted)
  234. $flag2 .= $Unsynchronisation ? '1' : '0'; // n - Unsynchronisation (true == unsynchronised)
  235. $flag2 .= $DataLengthIndicator ? '1' : '0'; // p - Data length indicator (true == data length indicator added)
  236. break;
  237. case 3:
  238. // %abc00000 %ijk00000
  239. $flag1 = $TagAlter ? '1' : '0'; // a - Tag alter preservation (true == discard)
  240. $flag1 .= $FileAlter ? '1' : '0'; // b - File alter preservation (true == discard)
  241. $flag1 .= $ReadOnly ? '1' : '0'; // c - Read only (true == read only)
  242. $flag1 .= '00000';
  243. $flag2 = $Compression ? '1' : '0'; // i - Compression (true == compressed)
  244. $flag2 .= $Encryption ? '1' : '0'; // j - Encryption (true == encrypted)
  245. $flag2 .= $GroupingIdentity ? '1' : '0'; // k - Grouping identity (true == contains group information)
  246. $flag2 .= '00000';
  247. break;
  248. default:
  249. return false;
  250. break;
  251. }
  252. return chr(bindec($flag1)).chr(bindec($flag2));
  253. }
  254. function GenerateID3v2FrameData($frame_name, $source_data_array) {
  255. if (!getid3_id3v2::IsValidID3v2FrameName($frame_name, $this->majorversion)) {
  256. return false;
  257. }
  258. $framedata = '';
  259. if (($this->majorversion < 3) || ($this->majorversion > 4)) {
  260. $this->errors[] = 'Only ID3v2.3 and ID3v2.4 are supported in GenerateID3v2FrameData()';
  261. } else { // $this->majorversion 3 or 4
  262. switch ($frame_name) {
  263. case 'UFID':
  264. // 4.1 UFID Unique file identifier
  265. // Owner identifier <text string> $00
  266. // Identifier <up to 64 bytes binary data>
  267. if (strlen($source_data_array['data']) > 64) {
  268. $this->errors[] = 'Identifier not allowed to be longer than 64 bytes in '.$frame_name.' (supplied data was '.strlen($source_data_array['data']).' bytes long)';
  269. } else {
  270. $framedata .= str_replace("\x00", '', $source_data_array['ownerid'])."\x00";
  271. $framedata .= substr($source_data_array['data'], 0, 64); // max 64 bytes - truncate anything longer
  272. }
  273. break;
  274. case 'TXXX':
  275. // 4.2.2 TXXX User defined text information frame
  276. // Text encoding $xx
  277. // Description <text string according to encoding> $00 (00)
  278. // Value <text string according to encoding>
  279. $source_data_array['encodingid'] = (isset($source_data_array['encodingid']) ? $source_data_array['encodingid'] : $this->id3v2_default_encodingid);
  280. if (!$this->ID3v2IsValidTextEncoding($source_data_array['encodingid'], $this->majorversion)) {
  281. $this->errors[] = 'Invalid Text Encoding in '.$frame_name.' ('.$source_data_array['encodingid'].') for ID3v2.'.$this->majorversion;
  282. } else {
  283. $framedata .= chr($source_data_array['encodingid']);
  284. $framedata .= $source_data_array['description'].getid3_id3v2::TextEncodingTerminatorLookup($source_data_array['encodingid']);
  285. $framedata .= $source_data_array['data'];
  286. }
  287. break;
  288. case 'WXXX':
  289. // 4.3.2 WXXX User defined URL link frame
  290. // Text encoding $xx
  291. // Description <text string according to encoding> $00 (00)
  292. // URL <text string>
  293. $source_data_array['encodingid'] = (isset($source_data_array['encodingid']) ? $source_data_array['encodingid'] : $this->id3v2_default_encodingid);
  294. if (!$this->ID3v2IsValidTextEncoding($source_data_array['encodingid'], $this->majorversion)) {
  295. $this->errors[] = 'Invalid Text Encoding in '.$frame_name.' ('.$source_data_array['encodingid'].') for ID3v2.'.$this->majorversion;
  296. } elseif (!isset($source_data_array['data']) || !$this->IsValidURL($source_data_array['data'], false, false)) {
  297. //$this->errors[] = 'Invalid URL in '.$frame_name.' ('.$source_data_array['data'].')';
  298. // probably should be an error, need to rewrite IsValidURL() to handle other encodings
  299. $this->warnings[] = 'Invalid URL in '.$frame_name.' ('.$source_data_array['data'].')';
  300. } else {
  301. $framedata .= chr($source_data_array['encodingid']);
  302. $framedata .= $source_data_array['description'].getid3_id3v2::TextEncodingTerminatorLookup($source_data_array['encodingid']);
  303. $framedata .= $source_data_array['data'];
  304. }
  305. break;
  306. case 'IPLS':
  307. // 4.4 IPLS Involved people list (ID3v2.3 only)
  308. // Text encoding $xx
  309. // People list strings <textstrings>
  310. $source_data_array['encodingid'] = (isset($source_data_array['encodingid']) ? $source_data_array['encodingid'] : $this->id3v2_default_encodingid);
  311. if (!$this->ID3v2IsValidTextEncoding($source_data_array['encodingid'], $this->majorversion)) {
  312. $this->errors[] = 'Invalid Text Encoding in '.$frame_name.' ('.$source_data_array['encodingid'].') for ID3v2.'.$this->majorversion;
  313. } else {
  314. $framedata .= chr($source_data_array['encodingid']);
  315. $framedata .= $source_data_array['data'];
  316. }
  317. break;
  318. case 'MCDI':
  319. // 4.4 MCDI Music CD identifier
  320. // CD TOC <binary data>
  321. $framedata .= $source_data_array['data'];
  322. break;
  323. case 'ETCO':
  324. // 4.5 ETCO Event timing codes
  325. // Time stamp format $xx
  326. // Where time stamp format is:
  327. // $01 (32-bit value) MPEG frames from beginning of file
  328. // $02 (32-bit value) milliseconds from beginning of file
  329. // Followed by a list of key events in the following format:
  330. // Type of event $xx
  331. // Time stamp $xx (xx ...)
  332. // The 'Time stamp' is set to zero if directly at the beginning of the sound
  333. // or after the previous event. All events MUST be sorted in chronological order.
  334. if (($source_data_array['timestampformat'] > 2) || ($source_data_array['timestampformat'] < 1)) {
  335. $this->errors[] = 'Invalid Time Stamp Format byte in '.$frame_name.' ('.$source_data_array['timestampformat'].')';
  336. } else {
  337. $framedata .= chr($source_data_array['timestampformat']);
  338. foreach ($source_data_array as $key => $val) {
  339. if (!$this->ID3v2IsValidETCOevent($val['typeid'])) {
  340. $this->errors[] = 'Invalid Event Type byte in '.$frame_name.' ('.$val['typeid'].')';
  341. } elseif (($key != 'timestampformat') && ($key != 'flags')) {
  342. if (($val['timestamp'] > 0) && ($previousETCOtimestamp >= $val['timestamp'])) {
  343. // The 'Time stamp' is set to zero if directly at the beginning of the sound
  344. // or after the previous event. All events MUST be sorted in chronological order.
  345. $this->errors[] = 'Out-of-order timestamp in '.$frame_name.' ('.$val['timestamp'].') for Event Type ('.$val['typeid'].')';
  346. } else {
  347. $framedata .= chr($val['typeid']);
  348. $framedata .= getid3_lib::BigEndian2String($val['timestamp'], 4, false);
  349. }
  350. }
  351. }
  352. }
  353. break;
  354. case 'MLLT':
  355. // 4.6 MLLT MPEG location lookup table
  356. // MPEG frames between reference $xx xx
  357. // Bytes between reference $xx xx xx
  358. // Milliseconds between reference $xx xx xx
  359. // Bits for bytes deviation $xx
  360. // Bits for milliseconds dev. $xx
  361. // Then for every reference the following data is included;
  362. // Deviation in bytes %xxx....
  363. // Deviation in milliseconds %xxx....
  364. if (($source_data_array['framesbetweenreferences'] > 0) && ($source_data_array['framesbetweenreferences'] <= 65535)) {
  365. $framedata .= getid3_lib::BigEndian2String($source_data_array['framesbetweenreferences'], 2, false);
  366. } else {
  367. $this->errors[] = 'Invalid MPEG Frames Between References in '.$frame_name.' ('.$source_data_array['framesbetweenreferences'].')';
  368. }
  369. if (($source_data_array['bytesbetweenreferences'] > 0) && ($source_data_array['bytesbetweenreferences'] <= 16777215)) {
  370. $framedata .= getid3_lib::BigEndian2String($source_data_array['bytesbetweenreferences'], 3, false);
  371. } else {
  372. $this->errors[] = 'Invalid bytes Between References in '.$frame_name.' ('.$source_data_array['bytesbetweenreferences'].')';
  373. }
  374. if (($source_data_array['msbetweenreferences'] > 0) && ($source_data_array['msbetweenreferences'] <= 16777215)) {
  375. $framedata .= getid3_lib::BigEndian2String($source_data_array['msbetweenreferences'], 3, false);
  376. } else {
  377. $this->errors[] = 'Invalid Milliseconds Between References in '.$frame_name.' ('.$source_data_array['msbetweenreferences'].')';
  378. }
  379. if (!$this->IsWithinBitRange($source_data_array['bitsforbytesdeviation'], 8, false)) {
  380. if (($source_data_array['bitsforbytesdeviation'] % 4) == 0) {
  381. $framedata .= chr($source_data_array['bitsforbytesdeviation']);
  382. } else {
  383. $this->errors[] = 'Bits For Bytes Deviation in '.$frame_name.' ('.$source_data_array['bitsforbytesdeviation'].') must be a multiple of 4.';
  384. }
  385. } else {
  386. $this->errors[] = 'Invalid Bits For Bytes Deviation in '.$frame_name.' ('.$source_data_array['bitsforbytesdeviation'].')';
  387. }
  388. if (!$this->IsWithinBitRange($source_data_array['bitsformsdeviation'], 8, false)) {
  389. if (($source_data_array['bitsformsdeviation'] % 4) == 0) {
  390. $framedata .= chr($source_data_array['bitsformsdeviation']);
  391. } else {
  392. $this->errors[] = 'Bits For Milliseconds Deviation in '.$frame_name.' ('.$source_data_array['bitsforbytesdeviation'].') must be a multiple of 4.';
  393. }
  394. } else {
  395. $this->errors[] = 'Invalid Bits For Milliseconds Deviation in '.$frame_name.' ('.$source_data_array['bitsformsdeviation'].')';
  396. }
  397. foreach ($source_data_array as $key => $val) {
  398. if (($key != 'framesbetweenreferences') && ($key != 'bytesbetweenreferences') && ($key != 'msbetweenreferences') && ($key != 'bitsforbytesdeviation') && ($key != 'bitsformsdeviation') && ($key != 'flags')) {
  399. $unwrittenbitstream .= str_pad(getid3_lib::Dec2Bin($val['bytedeviation']), $source_data_array['bitsforbytesdeviation'], '0', STR_PAD_LEFT);
  400. $unwrittenbitstream .= str_pad(getid3_lib::Dec2Bin($val['msdeviation']), $source_data_array['bitsformsdeviation'], '0', STR_PAD_LEFT);
  401. }
  402. }
  403. for ($i = 0; $i < strlen($unwrittenbitstream); $i += 8) {
  404. $highnibble = bindec(substr($unwrittenbitstream, $i, 4)) << 4;
  405. $lownibble = bindec(substr($unwrittenbitstream, $i + 4, 4));
  406. $framedata .= chr($highnibble & $lownibble);
  407. }
  408. break;
  409. case 'SYTC':
  410. // 4.7 SYTC Synchronised tempo codes
  411. // Time stamp format $xx
  412. // Tempo data <binary data>
  413. // Where time stamp format is:
  414. // $01 (32-bit value) MPEG frames from beginning of file
  415. // $02 (32-bit value) milliseconds from beginning of file
  416. if (($source_data_array['timestampformat'] > 2) || ($source_data_array['timestampformat'] < 1)) {
  417. $this->errors[] = 'Invalid Time Stamp Format byte in '.$frame_name.' ('.$source_data_array['timestampformat'].')';
  418. } else {
  419. $framedata .= chr($source_data_array['timestampformat']);
  420. foreach ($source_data_array as $key => $val) {
  421. if (!$this->ID3v2IsValidETCOevent($val['typeid'])) {
  422. $this->errors[] = 'Invalid Event Type byte in '.$frame_name.' ('.$val['typeid'].')';
  423. } elseif (($key != 'timestampformat') && ($key != 'flags')) {
  424. if (($val['tempo'] < 0) || ($val['tempo'] > 510)) {
  425. $this->errors[] = 'Invalid Tempo (max = 510) in '.$frame_name.' ('.$val['tempo'].') at timestamp ('.$val['timestamp'].')';
  426. } else {
  427. if ($val['tempo'] > 255) {
  428. $framedata .= chr(255);
  429. $val['tempo'] -= 255;
  430. }
  431. $framedata .= chr($val['tempo']);
  432. $framedata .= getid3_lib::BigEndian2String($val['timestamp'], 4, false);
  433. }
  434. }
  435. }
  436. }
  437. break;
  438. case 'USLT':
  439. // 4.8 USLT Unsynchronised lyric/text transcription
  440. // Text encoding $xx
  441. // Language $xx xx xx
  442. // Content descriptor <text string according to encoding> $00 (00)
  443. // Lyrics/text <full text string according to encoding>
  444. $source_data_array['encodingid'] = (isset($source_data_array['encodingid']) ? $source_data_array['encodingid'] : $this->id3v2_default_encodingid);
  445. if (!$this->ID3v2IsValidTextEncoding($source_data_array['encodingid'])) {
  446. $this->errors[] = 'Invalid Text Encoding in '.$frame_name.' ('.$source_data_array['encodingid'].') for ID3v2.'.$this->majorversion;
  447. } elseif (getid3_id3v2::LanguageLookup($source_data_array['language'], true) == '') {
  448. $this->errors[] = 'Invalid Language in '.$frame_name.' ('.$source_data_array['language'].')';
  449. } else {
  450. $framedata .= chr($source_data_array['encodingid']);
  451. $framedata .= strtolower($source_data_array['language']);
  452. $framedata .= $source_data_array['description'].getid3_id3v2::TextEncodingTerminatorLookup($source_data_array['encodingid']);
  453. $framedata .= $source_data_array['data'];
  454. }
  455. break;
  456. case 'SYLT':
  457. // 4.9 SYLT Synchronised lyric/text
  458. // Text encoding $xx
  459. // Language $xx xx xx
  460. // Time stamp format $xx
  461. // $01 (32-bit value) MPEG frames from beginning of file
  462. // $02 (32-bit value) milliseconds from beginning of file
  463. // Content type $xx
  464. // Content descriptor <text string according to encoding> $00 (00)
  465. // Terminated text to be synced (typically a syllable)
  466. // Sync identifier (terminator to above string) $00 (00)
  467. // Time stamp $xx (xx ...)
  468. $source_data_array['encodingid'] = (isset($source_data_array['encodingid']) ? $source_data_array['encodingid'] : $this->id3v2_default_encodingid);
  469. if (!$this->ID3v2IsValidTextEncoding($source_data_array['encodingid'])) {
  470. $this->errors[] = 'Invalid Text Encoding in '.$frame_name.' ('.$source_data_array['encodingid'].') for ID3v2.'.$this->majorversion;
  471. } elseif (getid3_id3v2::LanguageLookup($source_data_array['language'], true) == '') {
  472. $this->errors[] = 'Invalid Language in '.$frame_name.' ('.$source_data_array['language'].')';
  473. } elseif (($source_data_array['timestampformat'] > 2) || ($source_data_array['timestampformat'] < 1)) {
  474. $this->errors[] = 'Invalid Time Stamp Format byte in '.$frame_name.' ('.$source_data_array['timestampformat'].')';
  475. } elseif (!$this->ID3v2IsValidSYLTtype($source_data_array['contenttypeid'])) {
  476. $this->errors[] = 'Invalid Content Type byte in '.$frame_name.' ('.$source_data_array['contenttypeid'].')';
  477. } elseif (!is_array($source_data_array['data'])) {
  478. $this->errors[] = 'Invalid Lyric/Timestamp data in '.$frame_name.' (must be an array)';
  479. } else {
  480. $framedata .= chr($source_data_array['encodingid']);
  481. $framedata .= strtolower($source_data_array['language']);
  482. $framedata .= chr($source_data_array['timestampformat']);
  483. $framedata .= chr($source_data_array['contenttypeid']);
  484. $framedata .= $source_data_array['description'].getid3_id3v2::TextEncodingTerminatorLookup($source_data_array['encodingid']);
  485. ksort($source_data_array['data']);
  486. foreach ($source_data_array['data'] as $key => $val) {
  487. $framedata .= $val['data'].getid3_id3v2::TextEncodingTerminatorLookup($source_data_array['encodingid']);
  488. $framedata .= getid3_lib::BigEndian2String($val['timestamp'], 4, false);
  489. }
  490. }
  491. break;
  492. case 'COMM':
  493. // 4.10 COMM Comments
  494. // Text encoding $xx
  495. // Language $xx xx xx
  496. // Short content descrip. <text string according to encoding> $00 (00)
  497. // The actual text <full text string according to encoding>
  498. $source_data_array['encodingid'] = (isset($source_data_array['encodingid']) ? $source_data_array['encodingid'] : $this->id3v2_default_encodingid);
  499. if (!$this->ID3v2IsValidTextEncoding($source_data_array['encodingid'])) {
  500. $this->errors[] = 'Invalid Text Encoding in '.$frame_name.' ('.$source_data_array['encodingid'].') for ID3v2.'.$this->majorversion;
  501. } elseif (getid3_id3v2::LanguageLookup($source_data_array['language'], true) == '') {
  502. $this->errors[] = 'Invalid Language in '.$frame_name.' ('.$source_data_array['language'].')';
  503. } else {
  504. $framedata .= chr($source_data_array['encodingid']);
  505. $framedata .= strtolower($source_data_array['language']);
  506. $framedata .= $source_data_array['description'].getid3_id3v2::TextEncodingTerminatorLookup($source_data_array['encodingid']);
  507. $framedata .= $source_data_array['data'];
  508. }
  509. break;
  510. case 'RVA2':
  511. // 4.11 RVA2 Relative volume adjustment (2) (ID3v2.4+ only)
  512. // Identification <text string> $00
  513. // The 'identification' string is used to identify the situation and/or
  514. // device where this adjustment should apply. The following is then
  515. // repeated for every channel:
  516. // Type of channel $xx
  517. // Volume adjustment $xx xx
  518. // Bits representing peak $xx
  519. // Peak volume $xx (xx ...)
  520. $framedata .= str_replace("\x00", '', $source_data_array['description'])."\x00";
  521. foreach ($source_data_array as $key => $val) {
  522. if ($key != 'description') {
  523. $framedata .= chr($val['channeltypeid']);
  524. $framedata .= getid3_lib::BigEndian2String($val['volumeadjust'], 2, false, true); // signed 16-bit
  525. if (!$this->IsWithinBitRange($source_data_array['bitspeakvolume'], 8, false)) {
  526. $framedata .= chr($val['bitspeakvolume']);
  527. if ($val['bitspeakvolume'] > 0) {
  528. $framedata .= getid3_lib::BigEndian2String($val['peakvolume'], ceil($val['bitspeakvolume'] / 8), false, false);
  529. }
  530. } else {
  531. $this->errors[] = 'Invalid Bits Representing Peak Volume in '.$frame_name.' ('.$val['bitspeakvolume'].') (range = 0 to 255)';
  532. }
  533. }
  534. }
  535. break;
  536. case 'RVAD':
  537. // 4.12 RVAD Relative volume adjustment (ID3v2.3 only)
  538. // Increment/decrement %00fedcba
  539. // Bits used for volume descr. $xx
  540. // Relative volume change, right $xx xx (xx ...) // a
  541. // Relative volume change, left $xx xx (xx ...) // b
  542. // Peak volume right $xx xx (xx ...)
  543. // Peak volume left $xx xx (xx ...)
  544. // Relative volume change, right back $xx xx (xx ...) // c
  545. // Relative volume change, left back $xx xx (xx ...) // d
  546. // Peak volume right back $xx xx (xx ...)
  547. // Peak volume left back $xx xx (xx ...)
  548. // Relative volume change, center $xx xx (xx ...) // e
  549. // Peak volume center $xx xx (xx ...)
  550. // Relative volume change, bass $xx xx (xx ...) // f
  551. // Peak volume bass $xx xx (xx ...)
  552. if (!$this->IsWithinBitRange($source_data_array['bitsvolume'], 8, false)) {
  553. $this->errors[] = 'Invalid Bits For Volume Description byte in '.$frame_name.' ('.$source_data_array['bitsvolume'].') (range = 1 to 255)';
  554. } else {
  555. $incdecflag .= '00';
  556. $incdecflag .= $source_data_array['incdec']['right'] ? '1' : '0'; // a - Relative volume change, right
  557. $incdecflag .= $source_data_array['incdec']['left'] ? '1' : '0'; // b - Relative volume change, left
  558. $incdecflag .= $source_data_array['incdec']['rightrear'] ? '1' : '0'; // c - Relative volume change, right back
  559. $incdecflag .= $source_data_array['incdec']['leftrear'] ? '1' : '0'; // d - Relative volume change, left back
  560. $incdecflag .= $source_data_array['incdec']['center'] ? '1' : '0'; // e - Relative volume change, center
  561. $incdecflag .= $source_data_array['incdec']['bass'] ? '1' : '0'; // f - Relative volume change, bass
  562. $framedata .= chr(bindec($incdecflag));
  563. $framedata .= chr($source_data_array['bitsvolume']);
  564. $framedata .= getid3_lib::BigEndian2String($source_data_array['volumechange']['right'], ceil($source_data_array['bitsvolume'] / 8), false);
  565. $framedata .= getid3_lib::BigEndian2String($source_data_array['volumechange']['left'], ceil($source_data_array['bitsvolume'] / 8), false);
  566. $framedata .= getid3_lib::BigEndian2String($source_data_array['peakvolume']['right'], ceil($source_data_array['bitsvolume'] / 8), false);
  567. $framedata .= getid3_lib::BigEndian2String($source_data_array['peakvolume']['left'], ceil($source_data_array['bitsvolume'] / 8), false);
  568. if ($source_data_array['volumechange']['rightrear'] || $source_data_array['volumechange']['leftrear'] ||
  569. $source_data_array['peakvolume']['rightrear'] || $source_data_array['peakvolume']['leftrear'] ||
  570. $source_data_array['volumechange']['center'] || $source_data_array['peakvolume']['center'] ||
  571. $source_data_array['volumechange']['bass'] || $source_data_array['peakvolume']['bass']) {
  572. $framedata .= getid3_lib::BigEndian2String($source_data_array['volumechange']['rightrear'], ceil($source_data_array['bitsvolume']/8), false);
  573. $framedata .= getid3_lib::BigEndian2String($source_data_array['volumechange']['leftrear'], ceil($source_data_array['bitsvolume']/8), false);
  574. $framedata .= getid3_lib::BigEndian2String($source_data_array['peakvolume']['rightrear'], ceil($source_data_array['bitsvolume']/8), false);
  575. $framedata .= getid3_lib::BigEndian2String($source_data_array['peakvolume']['leftrear'], ceil($source_data_array['bitsvolume']/8), false);
  576. }
  577. if ($source_data_array['volumechange']['center'] || $source_data_array['peakvolume']['center'] ||
  578. $source_data_array['volumechange']['bass'] || $source_data_array['peakvolume']['bass']) {
  579. $framedata .= getid3_lib::BigEndian2String($source_data_array['volumechange']['center'], ceil($source_data_array['bitsvolume']/8), false);
  580. $framedata .= getid3_lib::BigEndian2String($source_data_array['peakvolume']['center'], ceil($source_data_array['bitsvolume']/8), false);
  581. }
  582. if ($source_data_array['volumechange']['bass'] || $source_data_array['peakvolume']['bass']) {
  583. $framedata .= getid3_lib::BigEndian2String($source_data_array['volumechange']['bass'], ceil($source_data_array['bitsvolume']/8), false);
  584. $framedata .= getid3_lib::BigEndian2String($source_data_array['peakvolume']['bass'], ceil($source_data_array['bitsvolume']/8), false);
  585. }
  586. }
  587. break;
  588. case 'EQU2':
  589. // 4.12 EQU2 Equalisation (2) (ID3v2.4+ only)
  590. // Interpolation method $xx
  591. // $00 Band
  592. // $01 Linear
  593. // Identification <text string> $00
  594. // The following is then repeated for every adjustment point
  595. // Frequency $xx xx
  596. // Volume adjustment $xx xx
  597. if (($source_data_array['interpolationmethod'] < 0) || ($source_data_array['interpolationmethod'] > 1)) {
  598. $this->errors[] = 'Invalid Interpolation Method byte in '.$frame_name.' ('.$source_data_array['interpolationmethod'].') (valid = 0 or 1)';
  599. } else {
  600. $framedata .= chr($source_data_array['interpolationmethod']);
  601. $framedata .= str_replace("\x00", '', $source_data_array['description'])."\x00";
  602. foreach ($source_data_array['data'] as $key => $val) {
  603. $framedata .= getid3_lib::BigEndian2String(intval(round($key * 2)), 2, false);
  604. $framedata .= getid3_lib::BigEndian2String($val, 2, false, true); // signed 16-bit
  605. }
  606. }
  607. break;
  608. case 'EQUA':
  609. // 4.12 EQUA Equalisation (ID3v2.3 only)
  610. // Adjustment bits $xx
  611. // This is followed by 2 bytes + ('adjustment bits' rounded up to the
  612. // nearest byte) for every equalisation band in the following format,
  613. // giving a frequency range of 0 - 32767Hz:
  614. // Increment/decrement %x (MSB of the Frequency)
  615. // Frequency (lower 15 bits)
  616. // Adjustment $xx (xx ...)
  617. if (!$this->IsWithinBitRange($source_data_array['bitsvolume'], 8, false)) {
  618. $this->errors[] = 'Invalid Adjustment Bits byte in '.$frame_name.' ('.$source_data_array['bitsvolume'].') (range = 1 to 255)';
  619. } else {
  620. $framedata .= chr($source_data_array['adjustmentbits']);
  621. foreach ($source_data_array as $key => $val) {
  622. if ($key != 'bitsvolume') {
  623. if (($key > 32767) || ($key < 0)) {
  624. $this->errors[] = 'Invalid Frequency in '.$frame_name.' ('.$key.') (range = 0 to 32767)';
  625. } else {
  626. if ($val >= 0) {
  627. // put MSB of frequency to 1 if increment, 0 if decrement
  628. $key |= 0x8000;
  629. }
  630. $framedata .= getid3_lib::BigEndian2String($key, 2, false);
  631. $framedata .= getid3_lib::BigEndian2String($val, ceil($source_data_array['adjustmentbits'] / 8), false);
  632. }
  633. }
  634. }
  635. }
  636. break;
  637. case 'RVRB':
  638. // 4.13 RVRB Reverb
  639. // Reverb left (ms) $xx xx
  640. // Reverb right (ms) $xx xx
  641. // Reverb bounces, left $xx
  642. // Reverb bounces, right $xx
  643. // Reverb feedback, left to left $xx
  644. // Reverb feedback, left to right $xx
  645. // Reverb feedback, right to right $xx
  646. // Reverb feedback, right to left $xx
  647. // Premix left to right $xx
  648. // Premix right to left $xx
  649. if (!$this->IsWithinBitRange($source_data_array['left'], 16, false)) {
  650. $this->errors[] = 'Invalid Reverb Left in '.$frame_name.' ('.$source_data_array['left'].') (range = 0 to 65535)';
  651. } elseif (!$this->IsWithinBitRange($source_data_array['right'], 16, false)) {
  652. $this->errors[] = 'Invalid Reverb Left in '.$frame_name.' ('.$source_data_array['right'].') (range = 0 to 65535)';
  653. } elseif (!$this->IsWithinBitRange($source_data_array['bouncesL'], 8, false)) {
  654. $this->errors[] = 'Invalid Reverb Bounces, Left in '.$frame_name.' ('.$source_data_array['bouncesL'].') (range = 0 to 255)';
  655. } elseif (!$this->IsWithinBitRange($source_data_array['bouncesR'], 8, false)) {
  656. $this->errors[] = 'Invalid Reverb Bounces, Right in '.$frame_name.' ('.$source_data_array['bouncesR'].') (range = 0 to 255)';
  657. } elseif (!$this->IsWithinBitRange($source_data_array['feedbackLL'], 8, false)) {
  658. $this->errors[] = 'Invalid Reverb Feedback, Left-To-Left in '.$frame_name.' ('.$source_data_array['feedbackLL'].') (range = 0 to 255)';
  659. } elseif (!$this->IsWithinBitRange($source_data_array['feedbackLR'], 8, false)) {
  660. $this->errors[] = 'Invalid Reverb Feedback, Left-To-Right in '.$frame_name.' ('.$source_data_array['feedbackLR'].') (range = 0 to 255)';
  661. } elseif (!$this->IsWithinBitRange($source_data_array['feedbackRR'], 8, false)) {
  662. $this->errors[] = 'Invalid Reverb Feedback, Right-To-Right in '.$frame_name.' ('.$source_data_array['feedbackRR'].') (range = 0 to 255)';
  663. } elseif (!$this->IsWithinBitRange($source_data_array['feedbackRL'], 8, false)) {
  664. $this->errors[] = 'Invalid Reverb Feedback, Right-To-Left in '.$frame_name.' ('.$source_data_array['feedbackRL'].') (range = 0 to 255)';
  665. } elseif (!$this->IsWithinBitRange($source_data_array['premixLR'], 8, false)) {
  666. $this->errors[] = 'Invalid Premix, Left-To-Right in '.$frame_name.' ('.$source_data_array['premixLR'].') (range = 0 to 255)';
  667. } elseif (!$this->IsWithinBitRange($source_data_array['premixRL'], 8, false)) {
  668. $this->errors[] = 'Invalid Premix, Right-To-Left in '.$frame_name.' ('.$source_data_array['premixRL'].') (range = 0 to 255)';
  669. } else {
  670. $framedata .= getid3_lib::BigEndian2String($source_data_array['left'], 2, false);
  671. $framedata .= getid3_lib::BigEndian2String($source_data_array['right'], 2, false);
  672. $framedata .= chr($source_data_array['bouncesL']);
  673. $framedata .= chr($source_data_array['bouncesR']);
  674. $framedata .= chr($source_data_array['feedbackLL']);
  675. $framedata .= chr($source_data_array['feedbackLR']);
  676. $framedata .= chr($source_data_array['feedbackRR']);
  677. $framedata .= chr($source_data_array['feedbackRL']);
  678. $framedata .= chr($source_data_array['premixLR']);
  679. $framedata .= chr($source_data_array['premixRL']);
  680. }
  681. break;
  682. case 'APIC':
  683. // 4.14 APIC Attached picture
  684. // Text encoding $xx
  685. // MIME type <text string> $00
  686. // Picture type $xx
  687. // Description <text string according to encoding> $00 (00)
  688. // Picture data <binary data>
  689. $source_data_array['encodingid'] = (isset($source_data_array['encodingid']) ? $source_data_array['encodingid'] : $this->id3v2_default_encodingid);
  690. if (!$this->ID3v2IsValidTextEncoding($source_data_array['encodingid'])) {
  691. $this->errors[] = 'Invalid Text Encoding in '.$frame_name.' ('.$source_data_array['encodingid'].') for ID3v2.'.$this->majorversion;
  692. } elseif (!$this->ID3v2IsValidAPICpicturetype($source_data_array['picturetypeid'])) {
  693. $this->errors[] = 'Invalid Picture Type byte in '.$frame_name.' ('.$source_data_array['picturetypeid'].') for ID3v2.'.$this->majorversion;
  694. } elseif (($this->majorversion >= 3) && (!$this->ID3v2IsValidAPICimageformat($source_data_array['mime']))) {
  695. $this->errors[] = 'Invalid MIME Type in '.$frame_name.' ('.$source_data_array['mime'].') for ID3v2.'.$this->majorversion;
  696. } elseif (($source_data_array['mime'] == '-->') && (!$this->IsValidURL($source_data_array['data'], false, false))) {
  697. //$this->errors[] = 'Invalid URL in '.$frame_name.' ('.$source_data_array['data'].')';
  698. // probably should be an error, need to rewrite IsValidURL() to handle other encodings
  699. $this->warnings[] = 'Invalid URL in '.$frame_name.' ('.$source_data_array['data'].')';
  700. } else {
  701. $framedata .= chr($source_data_array['encodingid']);
  702. $framedata .= str_replace("\x00", '', $source_data_array['mime'])."\x00";
  703. $framedata .= chr($source_data_array['picturetypeid']);
  704. $framedata .= (!empty($source_data_array['description']) ? $source_data_array['description'] : '').getid3_id3v2::TextEncodingTerminatorLookup($source_data_array['encodingid']);
  705. $framedata .= $source_data_array['data'];
  706. }
  707. break;
  708. case 'GEOB':
  709. // 4.15 GEOB General encapsulated object
  710. // Text encoding $xx
  711. // MIME type <text string> $00
  712. // Filename <text string according to encoding> $00 (00)
  713. // Content description <text string according to encoding> $00 (00)
  714. // Encapsulated object <binary data>
  715. $source_data_array['encodingid'] = (isset($source_data_array['encodingid']) ? $source_data_array['encodingid'] : $this->id3v2_default_encodingid);
  716. if (!$this->ID3v2IsValidTextEncoding($source_data_array['encodingid'])) {
  717. $this->errors[] = 'Invalid Text Encoding in '.$frame_name.' ('.$source_data_array['encodingid'].') for ID3v2.'.$this->majorversion;
  718. } elseif (!$this->IsValidMIMEstring($source_data_array['mime'])) {
  719. $this->errors[] = 'Invalid MIME Type in '.$frame_name.' ('.$source_data_array['mime'].')';
  720. } elseif (!$source_data_array['description']) {
  721. $this->errors[] = 'Missing Description in '.$frame_name;
  722. } else {
  723. $framedata .= chr($source_data_array['encodingid']);
  724. $framedata .= str_replace("\x00", '', $source_data_array['mime'])."\x00";
  725. $framedata .= $source_data_array['filename'].getid3_id3v2::TextEncodingTerminatorLookup($source_data_array['encodingid']);
  726. $framedata .= $source_data_array['description'].getid3_id3v2::TextEncodingTerminatorLookup($source_data_array['encodingid']);
  727. $framedata .= $source_data_array['data'];
  728. }
  729. break;
  730. case 'PCNT':
  731. // 4.16 PCNT Play counter
  732. // When the counter reaches all one's, one byte is inserted in
  733. // front of the counter thus making the counter eight bits bigger
  734. // Counter $xx xx xx xx (xx ...)
  735. $framedata .= getid3_lib::BigEndian2String($source_data_array['data'], 4, false);
  736. break;
  737. case 'POPM':
  738. // 4.17 POPM Popularimeter
  739. // When the counter reaches all one's, one byte is inserted in
  740. // front of the counter thus making the counter eight bits bigger
  741. // Email to user <text string> $00
  742. // Rating $xx
  743. // Counter $xx xx xx xx (xx ...)
  744. if (!$this->IsWithinBitRange($source_data_array['rating'], 8, false)) {
  745. $this->errors[] = 'Invalid Rating byte in '.$frame_name.' ('.$source_data_array['rating'].') (range = 0 to 255)';
  746. } elseif (!IsValidEmail($source_data_array['email'])) {
  747. $this->errors[] = 'Invalid Email in '.$frame_name.' ('.$source_data_array['email'].')';
  748. } else {
  749. $framedata .= str_replace("\x00", '', $source_data_array['email'])."\x00";
  750. $framedata .= chr($source_data_array['rating']);
  751. $framedata .= getid3_lib::BigEndian2String($source_data_array['data'], 4, false);
  752. }
  753. break;
  754. case 'RBUF':
  755. // 4.18 RBUF Recommended buffer size
  756. // Buffer size $xx xx xx
  757. // Embedded info flag %0000000x
  758. // Offset to next tag $xx xx xx xx
  759. if (!$this->IsWithinBitRange($source_data_array['buffersize'], 24, false)) {
  760. $this->errors[] = 'Invalid Buffer Size in '.$frame_name;
  761. } elseif (!$this->IsWithinBitRange($source_data_array['nexttagoffset'], 32, false)) {
  762. $this->errors[] = 'Invalid Offset To Next Tag in '.$frame_name;
  763. } else {
  764. $framedata .= getid3_lib::BigEndian2String($source_data_array['buffersize'], 3, false);
  765. $flag .= '0000000';
  766. $flag .= $source_data_array['flags']['embededinfo'] ? '1' : '0';
  767. $framedata .= chr(bindec($flag));
  768. $framedata .= getid3_lib::BigEndian2String($source_data_array['nexttagoffset'], 4, false);
  769. }
  770. break;
  771. case 'AENC':
  772. // 4.19 AENC Audio encryption
  773. // Owner identifier <text string> $00
  774. // Preview start $xx xx
  775. // Preview length $xx xx
  776. // Encryption info <binary data>
  777. if (!$this->IsWithinBitRange($source_data_array['previewstart'], 16, false)) {
  778. $this->errors[] = 'Invalid Preview Start in '.$frame_name.' ('.$source_data_array['previewstart'].')';
  779. } elseif (!$this->IsWithinBitRange($source_data_array['previewlength'], 16, false)) {
  780. $this->errors[] = 'Invalid Preview Length in '.$frame_name.' ('.$source_data_array['previewlength'].')';
  781. } else {
  782. $framedata .= str_replace("\x00", '', $source_data_array['ownerid'])."\x00";
  783. $framedata .= getid3_lib::BigEndian2String($source_data_array['previewstart'], 2, false);
  784. $framedata .= getid3_lib::BigEndian2String($source_data_array['previewlength'], 2, false);
  785. $framedata .= $source_data_array['encryptioninfo'];
  786. }
  787. break;
  788. case 'LINK':
  789. // 4.20 LINK Linked information
  790. // Frame identifier $xx xx xx xx
  791. // URL <text string> $00
  792. // ID and additional data <text string(s)>
  793. if (!getid3_id3v2::IsValidID3v2FrameName($source_data_array['frameid'], $this->majorversion)) {
  794. $this->errors[] = 'Invalid Frame Identifier in '.$frame_name.' ('.$source_data_array['frameid'].')';
  795. } elseif (!$this->IsValidURL($source_data_array['data'], true, false)) {
  796. //$this->errors[] = 'Invalid URL in '.$frame_name.' ('.$source_data_array['data'].')';
  797. // probably should be an error, need to rewrite IsValidURL() to handle other encodings
  798. $this->warnings[] = 'Invalid URL in '.$frame_name.' ('.$source_data_array['data'].')';
  799. } elseif ((($source_data_array['frameid'] == 'AENC') || ($source_data_array['frameid'] == 'APIC') || ($source_data_array['frameid'] == 'GEOB') || ($source_data_array['frameid'] == 'TXXX')) && ($source_data_array['additionaldata'] == '')) {
  800. $this->errors[] = 'Content Descriptor must be specified as additional data for Frame Identifier of '.$source_data_array['frameid'].' in '.$frame_name;
  801. } elseif (($source_data_array['frameid'] == 'USER') && (getid3_id3v2::LanguageLookup($source_data_array['additionaldata'], true) == '')) {
  802. $this->errors[] = 'Language must be specified as additional data for Frame Identifier of '.$source_data_array['frameid'].' in '.$frame_name;
  803. } elseif (($source_data_array['frameid'] == 'PRIV') && ($source_data_array['additionaldata'] == '')) {
  804. $this->errors[] = 'Owner Identifier must be specified as additional data for Frame Identifier of '.$source_data_array['frameid'].' in '.$frame_name;
  805. } elseif ((($source_data_array['frameid'] == 'COMM') || ($source_data_array['frameid'] == 'SYLT') || ($source_data_array['frameid'] == 'USLT')) && ((getid3_id3v2::LanguageLookup(substr($source_data_array['additionaldata'], 0, 3), true) == '') || (substr($source_data_array['additionaldata'], 3) == ''))) {
  806. $this->errors[] = 'Language followed by Content Descriptor must be specified as additional data for Frame Identifier of '.$source_data_array['frameid'].' in '.$frame_name;
  807. } else {
  808. $framedata .= $source_data_array['frameid'];
  809. $framedata .= str_replace("\x00", '', $source_data_array['data'])."\x00";
  810. switch ($source_data_array['frameid']) {
  811. case 'COMM':
  812. case 'SYLT':
  813. case 'USLT':
  814. case 'PRIV':
  815. case 'USER':
  816. case 'AENC':
  817. case 'APIC':
  818. case 'GEOB':
  819. case 'TXXX':
  820. $framedata .= $source_data_array['additionaldata'];
  821. break;
  822. case 'ASPI':
  823. case 'ETCO':
  824. case 'EQU2':
  825. case 'MCID':
  826. case 'MLLT':
  827. case 'OWNE':
  828. case 'RVA2':
  829. case 'RVRB':
  830. case 'SYTC':
  831. case 'IPLS':
  832. case 'RVAD':
  833. case 'EQUA':
  834. // no additional data required
  835. break;
  836. case 'RBUF':
  837. if ($this->majorversion == 3) {
  838. // no additional data required
  839. } else {
  840. $this->errors[] = $source_data_array['frameid'].' is not a valid Frame Identifier in '.$frame_name.' (in ID3v2.'.$this->majorversion.')';
  841. }
  842. default:
  843. if ((substr($source_data_array['frameid'], 0, 1) == 'T') || (substr($source_data_array['frameid'], 0, 1) == 'W')) {
  844. // no additional data required
  845. } else {
  846. $this->errors[] = $source_data_array['frameid'].' is not a valid Frame Identifier in '.$frame_name.' (in ID3v2.'.$this->majorversion.')';
  847. }
  848. break;
  849. }
  850. }
  851. break;
  852. case 'POSS':
  853. // 4.21 POSS Position synchronisation frame (ID3v2.3+ only)
  854. // Time stamp format $xx
  855. // Position $xx (xx ...)
  856. if (($source_data_array['timestampformat'] < 1) || ($source_data_array['timestampformat'] > 2)) {
  857. $this->errors[] = 'Invalid Time Stamp Format in '.$frame_name.' ('.$source_data_array['timestampformat'].') (valid = 1 or 2)';
  858. } elseif (!$this->IsWithinBitRange($source_data_array['position'], 32, false)) {
  859. $this->errors[] = 'Invalid Position in '.$frame_name.' ('.$source_data_array['position'].') (range = 0 to 4294967295)';
  860. } else {
  861. $framedata .= chr($source_data_array['timestampformat']);
  862. $framedata .= getid3_lib::BigEndian2String($source_data_array['position'], 4, false);
  863. }
  864. break;
  865. case 'USER':
  866. // 4.22 USER Terms of use (ID3v2.3+ only)
  867. // Text encoding $xx
  868. // Language $xx xx xx
  869. // The actual text <text string according to encoding>
  870. $source_data_array['encodingid'] = (isset($source_data_array['encodingid']) ? $source_data_array['encodingid'] : $this->id3v2_default_encodingid);
  871. if (!$this->ID3v2IsValidTextEncoding($source_data_array['encodingid'])) {
  872. $this->errors[] = 'Invalid Text Encoding in '.$frame_name.' ('.$source_data_array['encodingid'].')';
  873. } elseif (getid3_id3v2::LanguageLookup($source_data_array['language'], true) == '') {
  874. $this->errors[] = 'Invalid Language in '.$frame_name.' ('.$source_data_array['language'].')';
  875. } else {
  876. $framedata .= chr($source_data_array['encodingid']);
  877. $framedata .= strtolower($source_data_array['language']);
  878. $framedata .= $source_data_array['data'];
  879. }
  880. break;
  881. case 'OWNE':
  882. // 4.23 OWNE Ownership frame (ID3v2.3+ only)
  883. // Text encoding $xx
  884. // Price paid <text string> $00
  885. // Date of purch. <text string>
  886. // Seller <text string according to encoding>
  887. $source_data_array['encodingid'] = (isset($source_data_array['encodingid']) ? $source_data_array['encodingid'] : $this->id3v2_default_encodingid);
  888. if (!$this->ID3v2IsValidTextEncoding($source_data_array['encodingid'])) {
  889. $this->errors[] = 'Invalid Text Encoding in '.$frame_name.' ('.$source_data_array['encodingid'].')';
  890. } elseif (!$this->IsANumber($source_data_array['pricepaid']['value'], false)) {
  891. $this->errors[] = 'Invalid Price Paid in '.$frame_name.' ('.$source_data_array['pricepaid']['value'].')';
  892. } elseif (!$this->IsValidDateStampString($source_data_array['purchasedate'])) {
  893. $this->errors[] = 'Invalid Date Of Purchase in '.$frame_name.' ('.$source_data_array['purchasedate'].') (format = YYYYMMDD)';
  894. } else {
  895. $framedata .= chr($source_data_array['encodingid']);
  896. $framedata .= str_replace("\x00", '', $source_data_array['pricepaid']['value'])."\x00";
  897. $framedata .= $source_data_array['purchasedate'];
  898. $framedata .= $source_data_array['seller'];
  899. }
  900. break;
  901. case 'COMR':
  902. // 4.24 COMR Commercial frame (ID3v2.3+ only)
  903. // Text encoding $xx
  904. // Price string <text string> $00
  905. // Valid until <text string>
  906. // Contact URL <text string> $00
  907. // Received as $xx
  908. // Name of seller <text string according to encoding> $00 (00)
  909. // Description <text string according to encoding> $00 (00)
  910. // Picture MIME type <string> $00
  911. // Seller logo <binary data>
  912. $source_data_array['encodingid'] = (isset($source_data_array['encodingid']) ? $source_data_array['encodingid'] : $this->id3v2_default_encodingid);
  913. if (!$this->ID3v2IsValidTextEncoding($source_data_array['encodingid'])) {
  914. $this->errors[] = 'Invalid Text Encoding in '.$frame_name.' ('.$source_data_array['encodingid'].')';
  915. } elseif (!$this->IsValidDateStampString($source_data_array['pricevaliduntil'])) {
  916. $this->errors[] = 'Invalid Valid Until date in '.$frame_name.' ('.$source_data_array['pricevaliduntil'].') (format = YYYYMMDD)';
  917. } elseif (!$this->IsValidURL($source_data_array['contacturl'], false, true)) {
  918. $this->errors[] = 'Invalid Contact URL in '.$frame_name.' ('.$source_data_array['contacturl'].') (allowed schemes: http, https, ftp, mailto)';
  919. } elseif (!$this->ID3v2IsValidCOMRreceivedAs($source_data_array['receivedasid'])) {
  920. $this->errors[] = 'Invalid Received As byte in '.$frame_name.' ('.$source_data_array['contacturl'].') (range = 0 to 8)';
  921. } elseif (!$this->IsValidMIMEstring($source_data_array['mime'])) {
  922. $this->errors[] = 'Invalid MIME Type in '.$frame_name.' ('.$source_data_array['mime'].')';
  923. } else {
  924. $framedata .= chr($source_data_array['encodingid']);
  925. unset($pricestring);
  926. foreach ($source_data_array['price'] as $key => $val) {
  927. if ($this->ID3v2IsValidPriceString($key.$val['value'])) {
  928. $pricestrings[] = $key.$val['value'];
  929. } else {
  930. $this->errors[] = 'Invalid Price String in '.$frame_name.' ('.$key.$val['value'].')';
  931. }
  932. }
  933. $framedata .= implode('/', $pricestrings);
  934. $framedata .= $source_data_array['pricevaliduntil'];
  935. $framedata .= str_replace("\x00", '', $source_data_array['contacturl'])."\x00";
  936. $framedata .= chr($source_data_array['receivedasid']);
  937. $framedata .= $source_data_array['sellername'].getid3_id3v2::TextEncodingTerminatorLookup($source_data_array['encodingid']);
  938. $framedata .= $source_data_array['description'].getid3_id3v2::TextEncodingTerminatorLookup($source_data_array['encodingid']);
  939. $framedata .= $source_data_array['mime']."\x00";
  940. $framedata .= $source_data_array['logo'];
  941. }
  942. break;
  943. case 'ENCR':
  944. // 4.25 ENCR Encryption method registration (ID3v2.3+ only)
  945. // Owner identifier <text string> $00
  946. // Method symbol $xx
  947. // Encryption data <binary data>
  948. if (!$this->IsWithinBitRange($source_data_array['methodsymbol'], 8, false)) {
  949. $this->errors[] = 'Invalid Group Symbol in '.$frame_name.' ('.$source_data_array['methodsymbol'].') (range = 0 to 255)';
  950. } else {
  951. $framedata .= str_replace("\x00", '', $source_data_array['ownerid'])."\x00";
  952. $framedata .= ord($source_data_array['methodsymbol']);
  953. $framedata .= $source_data_array['data'];
  954. }
  955. break;
  956. case 'GRID':
  957. // 4.26 GRID Group identification registration (ID3v2.3+ only)
  958. // Owner identifier <text string> $00
  959. // Group symbol $xx
  960. // Group dependent data <binary data>
  961. if (!$this->IsWithinBitRange($source_data_array['groupsymbol'], 8, false)) {
  962. $this->errors[] = 'Invalid Group Symbol in '.$frame_name.' ('.$source_data_array['groupsymbol'].') (range = 0 to 255)';
  963. } else {
  964. $framedata .= str_replace("\x00", '', $source_data_array['ownerid'])."\x00";
  965. $framedata .= ord($source_data_array['groupsymbol']);
  966. $framedata .= $source_data_array['data'];
  967. }
  968. break;
  969. case 'PRIV':
  970. // 4.27 PRIV Private frame (ID3v2.3+ only)
  971. // Owner identifier <text string> $00
  972. // The private data <binary data>
  973. $framedata .= str_replace("\x00", '', $source_data_array['ownerid'])."\x00";
  974. $framedata .= $source_data_array['data'];
  975. break;
  976. case 'SIGN':
  977. // 4.28 SIGN Signature frame (ID3v2.4+ only)
  978. // Group symbol $xx
  979. // Signature <binary data>
  980. if (!$this->IsWithinBitRange($source_data_array['groupsymbol'], 8, false)) {
  981. $this->errors[] = 'Invalid Group Symbol in '.$frame_name.' ('.$source_data_array['groupsymbol'].') (range = 0 to 255)';
  982. } else {
  983. $framedata .= ord($source_data_array['groupsymbol']);
  984. $framedata .= $source_data_array['data'];
  985. }
  986. break;
  987. case 'SEEK':
  988. // 4.29 SEEK Seek frame (ID3v2.4+ only)
  989. // Minimum offset to next tag $xx xx xx xx
  990. if (!$this->IsWithinBitRange($source_data_array['data'], 32, false)) {
  991. $this->errors[] = 'Invalid Minimum Offset in '.$frame_name.' ('.$source_data_array['data'].') (range = 0 to 4294967295)';
  992. } else {
  993. $framedata .= getid3_lib::BigEndian2String($source_data_array['data'], 4, false);
  994. }
  995. break;
  996. case 'ASPI':
  997. // 4.30 ASPI Audio seek point index (ID3v2.4+ only)
  998. // Indexed data start (S) $xx xx xx xx
  999. // Indexed data length (L) $xx xx xx xx
  1000. // Number of index points (N) $xx xx
  1001. // Bits per index point (b) $xx
  1002. // Then for every index point the following data is included:
  1003. // Fraction at index (Fi) $xx (xx)
  1004. if (!$this->IsWithinBitRange($source_data_array['datastart'], 32, false)) {
  1005. $this->errors[] = 'Invalid Indexed Data Start in '.$frame_name.' ('.$source_data_array['datastart'].') (range = 0 to 4294967295)';
  1006. } elseif (!$this->IsWithinBitRange($source_data_array['datalength'], 32, false)) {
  1007. $this->errors[] = 'Invalid Indexed Data Length in '.$frame_name.' ('.$source_data_array['datalength'].') (range = 0 to 4294967295)';
  1008. } elseif (!$this->IsWithinBitRange($source_data_array['indexpoints'], 16, false)) {
  1009. $this->errors[] = 'Invalid Number Of Index Points in '.$frame_name.' ('.$source_data_array['indexpoints'].') (range = 0 to 65535)';
  1010. } elseif (!$this->IsWithinBitRange($source_data_array['bitsperpoint'], 8, false)) {
  1011. $this->errors[] = 'Invalid Bits Per Index Point in '.$frame_name.' ('.$source_data_array['bitsperpoint'].') (range = 0 to 255)';
  1012. } elseif ($source_data_array['indexpoints'] != count($source_data_array['indexes'])) {
  1013. $this->errors[] = 'Number Of Index Points does not match actual supplied data in '.$frame_name;
  1014. } else {
  1015. $framedata .= getid3_lib::BigEndian2String($source_data_array['datastart'], 4, false);
  1016. $framedata .= getid3_lib::BigEndian2String($source_data_array['datalength'], 4, false);
  1017. $framedata .= getid3_lib::BigEndian2String($source_data_array['indexpoints'], 2, false);
  1018. $framedata .= getid3_lib::BigEndian2String($source_data_array['bitsperpoint'], 1, false);
  1019. foreach ($source_data_array['indexes'] as $key => $val) {
  1020. $framedata .= getid3_lib::BigEndian2String($val, ceil($source_data_array['bitsperpoint'] / 8), false);
  1021. }
  1022. }
  1023. break;
  1024. case 'RGAD':
  1025. // RGAD Replay Gain Adjustment
  1026. // http://privatewww.essex.ac.uk/~djmrob/replaygain/
  1027. // Peak Amplitude $xx $xx $xx $xx
  1028. // Radio Replay Gain Adjustment %aaabbbcd %dddddddd
  1029. // Audiophile Replay Gain Adjustment %aaabbbcd %dddddddd
  1030. // a - name code
  1031. // b - originator code
  1032. // c - sign bit
  1033. // d - replay gain adjustment
  1034. if (($source_data_array['track_adjustment'] > 51) || ($source_data_array['track_adjustment'] < -51)) {
  1035. $this->errors[] = 'Invalid Track Adjustment in '.$frame_name.' ('.$source_data_array['track_adjustment'].') (range = -51.0 to +51.0)';
  1036. } elseif (($source_data_array['album_adjustment'] > 51) || ($source_data_array['album_adjustment'] < -51)) {
  1037. $this->errors[] = 'Invalid Album Adjustment in '.$frame_name.' ('.$source_data_array['album_adjustment'].') (range = -51.0 to +51.0)';
  1038. } elseif (!$this->ID3v2IsValidRGADname($source_data_array['raw']['track_name'])) {
  1039. $this->errors[] = 'Invalid Track Name Code in '.$frame_name.' ('.$source_data_array['raw']['track_name'].') (range = 0 to 2)';
  1040. } elseif (!$this->ID3v2IsValidRGADname($source_data_array['raw']['album_name'])) {
  1041. $this->errors[] = 'Invalid Album Name Code in '.$frame_name.' ('.$source_data_array['raw']['album_name'].') (range = 0 to 2)';
  1042. } elseif (!$this->ID3v2IsValidRGADoriginator($source_data_array['raw']['track_originator'])) {
  1043. $this->errors[] = 'Invalid Track Originator Code in '.$frame_name.' ('.$source_data_array['raw']['track_originator'].') (range = 0 to 3)';
  1044. } elseif (!$this->ID3v2IsValidRGADoriginator($source_data_array['raw']['album_originator'])) {
  1045. $this->errors[] = 'Invalid Album Originator Code in '.$frame_name.' ('.$source_data_array['raw']['album_originator'].') (range = 0 to 3)';
  1046. } else {
  1047. $framedata .= getid3_lib::Float2String($source_data_array['peakamplitude'], 32);
  1048. $framedata .= getid3_lib::RGADgainString($source_data_array['raw']['track_name'], $source_data_array['raw']['track_originator'], $source_data_array['track_adjustment']);
  1049. $framedata .= getid3_lib::RGADgainString($source_data_array['raw']['album_name'], $source_data_array['raw']['album_originator'], $source_data_array['album_adjustment']);
  1050. }
  1051. break;
  1052. default:
  1053. if ((($this->majorversion == 2) && (strlen($frame_name) != 3)) || (($this->majorversion > 2) && (strlen($frame_name) != 4))) {
  1054. $this->errors[] = 'Invalid frame name "'.$frame_name.'" for ID3v2.'.$this->majorversion;
  1055. } elseif ($frame_name{0} == 'T') {
  1056. // 4.2. T??? Text information frames
  1057. // Text encoding $xx
  1058. // Information <text string(s) according to encoding>
  1059. $source_data_array['encodingid'] = (isset($source_data_array['encodingid']) ? $source_data_array['encodingid'] : $this->id3v2_default_encodingid);
  1060. if (!$this->ID3v2IsValidTextEncoding($source_data_array['encodingid'])) {
  1061. $this->errors[] = 'Invalid Text Encoding in '.$frame_name.' ('.$source_data_array['encodingid'].') for ID3v2.'.$this->majorversion;
  1062. } else {
  1063. $framedata .= chr($source_data_array['encodingid']);
  1064. $framedata .= $source_data_array['data'];
  1065. }
  1066. } elseif ($frame_name{0} == 'W') {
  1067. // 4.3. W??? URL link frames
  1068. // URL <text string>
  1069. if (!$this->IsValidURL($source_data_array['data'], false, false)) {
  1070. //$this->errors[] = 'Invalid URL in '.$frame_name.' ('.$source_data_array['data'].')';
  1071. // probably should be an error, need to rewrite IsValidURL() to handle other encodings
  1072. $this->warnings[] = 'Invalid URL in '.$frame_name.' ('.$source_data_array['data'].')';
  1073. } else {
  1074. $framedata .= $source_data_array['data'];
  1075. }
  1076. } else {
  1077. $this->errors[] = $frame_name.' not yet supported in $this->GenerateID3v2FrameData()';
  1078. }
  1079. break;
  1080. }
  1081. }
  1082. if (!empty($this->errors)) {
  1083. return false;
  1084. }
  1085. return $framedata;
  1086. }
  1087. function ID3v2FrameIsAllowed($frame_name, $source_data_array) {
  1088. static $PreviousFrames = array();
  1089. if ($frame_name === null) {
  1090. // if the writing functions are called multiple times, the static array needs to be
  1091. // cleared - this can be done by calling $this->ID3v2FrameIsAllowed(null, '')
  1092. $PreviousFrames = array();
  1093. return true;
  1094. }
  1095. if ($this->majorversion == 4) {
  1096. switch ($frame_name) {
  1097. case 'UFID':
  1098. case 'AENC':
  1099. case 'ENCR':
  1100. case 'GRID':
  1101. if (!isset($source_data_array['ownerid'])) {
  1102. $this->errors[] = '[ownerid] not specified for '.$frame_name;
  1103. } elseif (in_array($frame_name.$source_data_array['ownerid'], $PreviousFrames)) {
  1104. $this->errors[] = 'Only one '.$frame_name.' tag allowed with the same OwnerID ('.$source_data_array['ownerid'].')';
  1105. } else {
  1106. $PreviousFrames[] = $frame_name.$source_data_array['ownerid'];
  1107. }
  1108. break;
  1109. case 'TXXX':
  1110. case 'WXXX':
  1111. case 'RVA2':
  1112. case 'EQU2':
  1113. case 'APIC':
  1114. case 'GEOB':
  1115. if (!isset($source_data_array['description'])) {
  1116. $this->errors[] = '[description] not specified for '.$frame_name;
  1117. } elseif (in_array($frame_name.$source_data_array['description'], $PreviousFrames)) {
  1118. $this->errors[] = 'Only one '.$frame_name.' tag allowed with the same Description ('.$source_data_array['description'].')';
  1119. } else {
  1120. $PreviousFrames[] = $frame_name.$source_data_array['description'];
  1121. }
  1122. break;
  1123. case 'USER':
  1124. if (!isset($source_data_array['language'])) {
  1125. $this->errors[] = '[language] not specified for '.$frame_name;
  1126. } elseif (in_array($frame_name.$source_data_array['language'], $PreviousFrames)) {
  1127. $this->errors[] = 'Only one '.$frame_name.' tag allowed with the same Language ('.$source_data_array['language'].')';
  1128. } else {
  1129. $PreviousFrames[] = $frame_name.$source_data_array['language'];
  1130. }
  1131. break;
  1132. case 'USLT':
  1133. case 'SYLT':
  1134. case 'COMM':
  1135. if (!isset($source_data_array['language'])) {
  1136. $this->errors[] = '[language] not specified for '.$frame_name;
  1137. } elseif (!isset($source_data_array['description'])) {
  1138. $this->errors[] = '[description] not specified for '.$frame_name;
  1139. } elseif (in_array($frame_name.$source_data_array['language'].$source_data_array['description'], $PreviousFrames)) {
  1140. $this->errors[] = 'Only one '.$frame_name.' tag allowed with the same Language + Description ('.$source_data_array['language'].' + '.$source_data_array['description'].')';
  1141. } else {
  1142. $PreviousFrames[] = $frame_name.$source_data_array['language'].$source_data_array['description'];
  1143. }
  1144. break;
  1145. case 'POPM':
  1146. if (!isset($source_data_array['email'])) {
  1147. $this->errors[] = '[email] not specified for '.$frame_name;
  1148. } elseif (in_array($frame_name.$source_data_array['email'], $PreviousFrames)) {
  1149. $this->errors[] = 'Only one '.$frame_name.' tag allowed with the same Email ('.$source_data_array['email'].')';
  1150. } else {
  1151. $PreviousFrames[] = $frame_name.$source_data_array['email'];
  1152. }
  1153. break;
  1154. case 'IPLS':
  1155. case 'MCDI':
  1156. case 'ETCO':
  1157. case 'MLLT':
  1158. case 'SYTC':
  1159. case 'RVRB':
  1160. case 'PCNT':
  1161. case 'RBUF':
  1162. case 'POSS':
  1163. case 'OWNE':
  1164. case 'SEEK':
  1165. case 'ASPI':
  1166. case 'RGAD':
  1167. if (in_array($frame_name, $PreviousFrames)) {
  1168. $this->errors[] = 'Only one '.$frame_name.' tag allowed';
  1169. } else {
  1170. $PreviousFrames[] = $frame_name;
  1171. }
  1172. break;
  1173. case 'LINK':
  1174. // this isn't implemented quite right (yet) - it should check the target frame data for compliance
  1175. // but right now it just allows one linked frame of each type, to be safe.
  1176. if (!isset($source_data_array['frameid'])) {
  1177. $this->errors[] = '[frameid] not specified for '.$frame_name;
  1178. } elseif (in_array($frame_name.$source_data_array['frameid'], $PreviousFrames)) {
  1179. $this->errors[] = 'Only one '.$frame_name.' tag allowed with the same FrameID ('.$source_data_array['frameid'].')';
  1180. } elseif (in_array($source_data_array['frameid'], $PreviousFrames)) {
  1181. // no links to singleton tags
  1182. $this->errors[] = 'Cannot specify a '.$frame_name.' tag to a singleton tag that already exists ('.$source_data_array['frameid'].')';
  1183. } else {
  1184. $PreviousFrames[] = $frame_name.$source_data_array['frameid']; // only one linked tag of this type
  1185. $PreviousFrames[] = $source_data_array['frameid']; // no non-linked singleton tags of this type
  1186. }
  1187. break;
  1188. case 'COMR':
  1189. // There may be more than one 'commercial frame' in a tag, but no two may be identical
  1190. // Checking isn't implemented at all (yet) - just assumes that it's OK.
  1191. break;
  1192. case 'PRIV':
  1193. case 'SIGN':
  1194. if (!isset($source_data_array['ownerid'])) {
  1195. $this->errors[] = '[ownerid] not specified for '.$frame_name;
  1196. } elseif (!isset($source_data_array['data'])) {
  1197. $this->errors[] = '[data] not specified for '.$frame_name;
  1198. } elseif (in_array($frame_name.$source_data_array['ownerid'].$source_data_array['data'], $PreviousFrames)) {
  1199. $this->errors[] = 'Only one '.$frame_name.' tag allowed with the same OwnerID + Data ('.$source_data_array['ownerid'].' + '.$source_data_array['data'].')';
  1200. } else {
  1201. $PreviousFrames[] = $frame_name.$source_data_array['ownerid'].$source_data_array['data'];
  1202. }
  1203. break;
  1204. default:
  1205. if (($frame_name{0} != 'T') && ($frame_name{0} != 'W')) {
  1206. $this->errors[] = 'Frame not allowed in ID3v2.'.$this->majorversion.': '.$frame_name;
  1207. }
  1208. break;
  1209. }
  1210. } elseif ($this->majorversion == 3) {
  1211. switch ($frame_name) {
  1212. case 'UFID':
  1213. case 'AENC':
  1214. case 'ENCR':
  1215. case 'GRID':
  1216. if (!isset($source_data_array['ownerid'])) {
  1217. $this->errors[] = '[ownerid] not specified for '.$frame_name;
  1218. } elseif (in_array($frame_name.$source_data_array['ownerid'], $PreviousFrames)) {
  1219. $this->errors[] = 'Only one '.$frame_name.' tag allowed with the same OwnerID ('.$source_data_array['ownerid'].')';
  1220. } else {
  1221. $PreviousFrames[] = $frame_name.$source_data_array['ownerid'];
  1222. }
  1223. break;
  1224. case 'TXXX':
  1225. case 'WXXX':
  1226. case 'APIC':
  1227. case 'GEOB':
  1228. if (!isset($source_data_array['description'])) {
  1229. $this->errors[] = '[description] not specified for '.$frame_name;
  1230. } elseif (in_array($frame_name.$source_data_array['description'], $PreviousFrames)) {
  1231. $this->errors[] = 'Only one '.$frame_name.' tag allowed with the same Description ('.$source_data_array['description'].')';
  1232. } else {
  1233. $PreviousFrames[] = $frame_name.$source_data_array['description'];
  1234. }
  1235. break;
  1236. case 'USER':
  1237. if (!isset($source_data_array['language'])) {
  1238. $this->errors[] = '[language] not specified for '.$frame_name;
  1239. } elseif (in_array($frame_name.$source_data_array['language'], $PreviousFrames)) {
  1240. $this->errors[] = 'Only one '.$frame_name.' tag allowed with the same Language ('.$source_data_array['language'].')';
  1241. } else {
  1242. $PreviousFrames[] = $frame_name.$source_data_array['language'];
  1243. }
  1244. break;
  1245. case 'USLT':
  1246. case 'SYLT':
  1247. case 'COMM':
  1248. if (!isset($source_data_array['language'])) {
  1249. $this->errors[] = '[language] not specified for '.$frame_name;
  1250. } elseif (!isset($source_data_array['description'])) {
  1251. $this->errors[] = '[description] not specified for '.$frame_name;
  1252. } elseif (in_array($frame_name.$source_data_array['language'].$source_data_array['description'], $PreviousFrames)) {
  1253. $this->errors[] = 'Only one '.$frame_name.' tag allowed with the same Language + Description ('.$source_data_array['language'].' + '.$source_data_array['description'].')';
  1254. } else {
  1255. $PreviousFrames[] = $frame_name.$source_data_array['language'].$source_data_array['description'];
  1256. }
  1257. break;
  1258. case 'POPM':
  1259. if (!isset($source_data_array['email'])) {
  1260. $this->errors[] = '[email] not specified for '.$frame_name;
  1261. } elseif (in_array($frame_name.$source_data_array['email'], $PreviousFrames)) {
  1262. $this->errors[] = 'Only one '.$frame_name.' tag allowed with the same Email ('.$source_data_array['email'].')';
  1263. } else {
  1264. $PreviousFrames[] = $frame_name.$source_data_array['email'];
  1265. }
  1266. break;
  1267. case 'IPLS':
  1268. case 'MCDI':
  1269. case 'ETCO':
  1270. case 'MLLT':
  1271. case 'SYTC':
  1272. case 'RVAD':
  1273. case 'EQUA':
  1274. case 'RVRB':
  1275. case 'PCNT':
  1276. case 'RBUF':
  1277. case 'POSS':
  1278. case 'OWNE':
  1279. case 'RGAD':
  1280. if (in_array($frame_name, $PreviousFrames)) {
  1281. $this->errors[] = 'Only one '.$frame_name.' tag allowed';
  1282. } else {
  1283. $PreviousFrames[] = $frame_name;
  1284. }
  1285. break;
  1286. case 'LINK':
  1287. // this isn't implemented quite right (yet) - it should check the target frame data for compliance
  1288. // but right now it just allows one linked frame of each type, to be safe.
  1289. if (!isset($source_data_array['frameid'])) {
  1290. $this->errors[] = '[frameid] not specified for '.$frame_name;
  1291. } elseif (in_array($frame_name.$source_data_array['frameid'], $PreviousFrames)) {
  1292. $this->errors[] = 'Only one '.$frame_name.' tag allowed with the same FrameID ('.$source_data_array['frameid'].')';
  1293. } elseif (in_array($source_data_array['frameid'], $PreviousFrames)) {
  1294. // no links to singleton tags
  1295. $this->errors[] = 'Cannot specify a '.$frame_name.' tag to a singleton tag that already exists ('.$source_data_array['frameid'].')';
  1296. } else {
  1297. $PreviousFrames[] = $frame_name.$source_data_array['frameid']; // only one linked tag of this type
  1298. $PreviousFrames[] = $source_data_array['frameid']; // no non-linked singleton tags of this type
  1299. }
  1300. break;
  1301. case 'COMR':
  1302. // There may be more than one 'commercial frame' in a tag, but no two may be identical
  1303. // Checking isn't implemented at all (yet) - just assumes that it's OK.
  1304. break;
  1305. case 'PRIV':
  1306. if (!isset($source_data_array['ownerid'])) {
  1307. $this->errors[] = '[ownerid] not specified for '.$frame_name;
  1308. } elseif (!isset($source_data_array['data'])) {
  1309. $this->errors[] = '[data] not specified for '.$frame_name;
  1310. } elseif (in_array($frame_name.$source_data_array['ownerid'].$source_data_array['data'], $PreviousFrames)) {
  1311. $this->errors[] = 'Only one '.$frame_name.' tag allowed with the same OwnerID + Data ('.$source_data_array['ownerid'].' + '.$source_data_array['data'].')';
  1312. } else {
  1313. $PreviousFrames[] = $frame_name.$source_data_array['ownerid'].$source_data_array['data'];
  1314. }
  1315. break;
  1316. default:
  1317. if (($frame_name{0} != 'T') && ($frame_name{0} != 'W')) {
  1318. $this->errors[] = 'Frame not allowed in ID3v2.'.$this->majorversion.': '.$frame_name;
  1319. }
  1320. break;
  1321. }
  1322. } elseif ($this->majorversion == 2) {
  1323. switch ($frame_name) {
  1324. case 'UFI':
  1325. case 'CRM':
  1326. case 'CRA':
  1327. if (!isset($source_data_array['ownerid'])) {
  1328. $this->errors[] = '[ownerid] not specified for '.$frame_name;
  1329. } elseif (in_array($frame_name.$source_data_array['ownerid'], $PreviousFrames)) {
  1330. $this->errors[] = 'Only one '.$frame_name.' tag allowed with the same OwnerID ('.$source_data_array['ownerid'].')';
  1331. } else {
  1332. $PreviousFrames[] = $frame_name.$source_data_array['ownerid'];
  1333. }
  1334. break;
  1335. case 'TXX':
  1336. case 'WXX':
  1337. case 'PIC':
  1338. case 'GEO':
  1339. if (!isset($source_data_array['description'])) {
  1340. $this->errors[] = '[description] not specified for '.$frame_name;
  1341. } elseif (in_array($frame_name.$source_data_array['description'], $PreviousFrames)) {
  1342. $this->errors[] = 'Only one '.$frame_name.' tag allowed with the same Description ('.$source_data_array['description'].')';
  1343. } else {
  1344. $PreviousFrames[] = $frame_name.$source_data_array['description'];
  1345. }
  1346. break;
  1347. case 'ULT':
  1348. case 'SLT':
  1349. case 'COM':
  1350. if (!isset($source_data_array['language'])) {
  1351. $this->errors[] = '[language] not specified for '.$frame_name;
  1352. } elseif (!isset($source_data_array['description'])) {
  1353. $this->errors[] = '[description] not specified for '.$frame_name;
  1354. } elseif (in_array($frame_name.$source_data_array['language'].$source_data_array['description'], $PreviousFrames)) {
  1355. $this->errors[] = 'Only one '.$frame_name.' tag allowed with the same Language + Description ('.$source_data_array['language'].' + '.$source_data_array['description'].')';
  1356. } else {
  1357. $PreviousFrames[] = $frame_name.$source_data_array['language'].$source_data_array['description'];
  1358. }
  1359. break;
  1360. case 'POP':
  1361. if (!isset($source_data_array['email'])) {
  1362. $this->errors[] = '[email] not specified for '.$frame_name;
  1363. } elseif (in_array($frame_name.$source_data_array['email'], $PreviousFrames)) {
  1364. $this->errors[] = 'Only one '.$frame_name.' tag allowed with the same Email ('.$source_data_array['email'].')';
  1365. } else {
  1366. $PreviousFrames[] = $frame_name.$source_data_array['email'];
  1367. }
  1368. break;
  1369. case 'IPL':
  1370. case 'MCI':
  1371. case 'ETC':
  1372. case 'MLL':
  1373. case 'STC':
  1374. case 'RVA':
  1375. case 'EQU':
  1376. case 'REV':
  1377. case 'CNT':
  1378. case 'BUF':
  1379. if (in_array($frame_name, $PreviousFrames)) {
  1380. $this->errors[] = 'Only one '.$frame_name.' tag allowed';
  1381. } else {
  1382. $PreviousFrames[] = $frame_name;
  1383. }
  1384. break;
  1385. case 'LNK':
  1386. // this isn't implemented quite right (yet) - it should check the target frame data for compliance
  1387. // but right now it just allows one linked frame of each type, to be safe.
  1388. if (!isset($source_data_array['frameid'])) {
  1389. $this->errors[] = '[frameid] not specified for '.$frame_name;
  1390. } elseif (in_array($frame_name.$source_data_array['frameid'], $PreviousFrames)) {
  1391. $this->errors[] = 'Only one '.$frame_name.' tag allowed with the same FrameID ('.$source_data_array['frameid'].')';
  1392. } elseif (in_array($source_data_array['frameid'], $PreviousFrames)) {
  1393. // no links to singleton tags
  1394. $this->errors[] = 'Cannot specify a '.$frame_name.' tag to a singleton tag that already exists ('.$source_data_array['frameid'].')';
  1395. } else {
  1396. $PreviousFrames[] = $frame_name.$source_data_array['frameid']; // only one linked tag of this type
  1397. $PreviousFrames[] = $source_data_array['frameid']; // no non-linked singleton tags of this type
  1398. }
  1399. break;
  1400. default:
  1401. if (($frame_name{0} != 'T') && ($frame_name{0} != 'W')) {
  1402. $this->errors[] = 'Frame not allowed in ID3v2.'.$this->majorversion.': '.$frame_name;
  1403. }
  1404. break;
  1405. }
  1406. }
  1407. if (!empty($this->errors)) {
  1408. return false;
  1409. }
  1410. return true;
  1411. }
  1412. function GenerateID3v2Tag($noerrorsonly=true) {
  1413. $this->ID3v2FrameIsAllowed(null, ''); // clear static array in case this isn't the first call to $this->GenerateID3v2Tag()
  1414. $tagstring = '';
  1415. if (is_array($this->tag_data)) {
  1416. foreach ($this->tag_data as $frame_name => $frame_rawinputdata) {
  1417. foreach ($frame_rawinputdata as $irrelevantindex => $source_data_array) {
  1418. if (getid3_id3v2::IsValidID3v2FrameName($frame_name, $this->majorversion)) {
  1419. unset($frame_length);
  1420. unset($frame_flags);
  1421. $frame_data = false;
  1422. if ($this->ID3v2FrameIsAllowed($frame_name, $source_data_array)) {
  1423. if ($frame_data = $this->GenerateID3v2FrameData($frame_name, $source_data_array)) {
  1424. $FrameUnsynchronisation = false;
  1425. if ($this->majorversion >= 4) {
  1426. // frame-level unsynchronisation
  1427. $unsynchdata = $frame_data;
  1428. if ($this->id3v2_use_unsynchronisation) {
  1429. $unsynchdata = $this->Unsynchronise($frame_data);
  1430. }
  1431. if (strlen($unsynchdata) != strlen($frame_data)) {
  1432. // unsynchronisation needed
  1433. $FrameUnsynchronisation = true;
  1434. $frame_data = $unsynchdata;
  1435. if (isset($TagUnsynchronisation) && $TagUnsynchronisation === false) {
  1436. // only set to true if ALL frames are unsynchronised
  1437. } else {
  1438. $TagUnsynchronisation = true;
  1439. }
  1440. } else {
  1441. if (isset($TagUnsynchronisation)) {
  1442. $TagUnsynchronisation = false;
  1443. }
  1444. }
  1445. unset($unsynchdata);
  1446. $frame_length = getid3_lib::BigEndian2String(strlen($frame_data), 4, true);
  1447. } else {
  1448. $frame_length = getid3_lib::BigEndian2String(strlen($frame_data), 4, false);
  1449. }
  1450. $frame_flags = $this->GenerateID3v2FrameFlags($this->ID3v2FrameFlagsLookupTagAlter($frame_name), $this->ID3v2FrameFlagsLookupFileAlter($frame_name), false, false, false, false, $FrameUnsynchronisation, false);
  1451. }
  1452. } else {
  1453. $this->errors[] = 'Frame "'.$frame_name.'" is NOT allowed';
  1454. }
  1455. if ($frame_data === false) {
  1456. $this->errors[] = '$this->GenerateID3v2FrameData() failed for "'.$frame_name.'"';
  1457. if ($noerrorsonly) {
  1458. return false;
  1459. } else {
  1460. unset($frame_name);
  1461. }
  1462. }
  1463. } else {
  1464. // ignore any invalid frame names, including 'title', 'header', etc
  1465. $this->warnings[] = 'Ignoring invalid ID3v2 frame type: "'.$frame_name.'"';
  1466. unset($frame_name);
  1467. unset($frame_length);
  1468. unset($frame_flags);
  1469. unset($frame_data);
  1470. }
  1471. if (isset($frame_name) && isset($frame_length) && isset($frame_flags) && isset($frame_data)) {
  1472. $tagstring .= $frame_name.$frame_length.$frame_flags.$frame_data;
  1473. }
  1474. }
  1475. }
  1476. if (!isset($TagUnsynchronisation)) {
  1477. $TagUnsynchronisation = false;
  1478. }
  1479. if (($this->majorversion <= 3) && $this->id3v2_use_unsynchronisation) {
  1480. // tag-level unsynchronisation
  1481. $unsynchdata = $this->Unsynchronise($tagstring);
  1482. if (strlen($unsynchdata) != strlen($tagstring)) {
  1483. // unsynchronisation needed
  1484. $TagUnsynchronisation = true;
  1485. $tagstring = $unsynchdata;
  1486. }
  1487. }
  1488. while ($this->paddedlength < (strlen($tagstring) + getid3_id3v2::ID3v2HeaderLength($this->majorversion))) {
  1489. $this->paddedlength += 1024;
  1490. }
  1491. $footer = false; // ID3v2 footers not yet supported in getID3()
  1492. if (!$footer && ($this->paddedlength > (strlen($tagstring) + getid3_id3v2::ID3v2HeaderLength($this->majorversion)))) {
  1493. // pad up to $paddedlength bytes if unpadded tag is shorter than $paddedlength
  1494. // "Furthermore it MUST NOT have any padding when a tag footer is added to the tag."
  1495. if (($this->paddedlength - strlen($tagstring) - getid3_id3v2::ID3v2HeaderLength($this->majorversion)) > 0) {
  1496. $tagstring .= str_repeat("\x00", $this->paddedlength - strlen($tagstring) - getid3_id3v2::ID3v2HeaderLength($this->majorversion));
  1497. }
  1498. }
  1499. if ($this->id3v2_use_unsynchronisation && (substr($tagstring, strlen($tagstring) - 1, 1) == "\xFF")) {
  1500. // special unsynchronisation case:
  1501. // if last byte == $FF then appended a $00
  1502. $TagUnsynchronisation = true;
  1503. $tagstring .= "\x00";
  1504. }
  1505. $tagheader = 'ID3';
  1506. $tagheader .= chr($this->majorversion);
  1507. $tagheader .= chr($this->minorversion);
  1508. $tagheader .= $this->GenerateID3v2TagFlags(array('unsynchronisation'=>$TagUnsynchronisation));
  1509. $tagheader .= getid3_lib::BigEndian2String(strlen($tagstring), 4, true);
  1510. return $tagheader.$tagstring;
  1511. }
  1512. $this->errors[] = 'tag_data is not an array in GenerateID3v2Tag()';
  1513. return false;
  1514. }
  1515. function ID3v2IsValidPriceString($pricestring) {
  1516. if (getid3_id3v2::LanguageLookup(substr($pricestring, 0, 3), true) == '') {
  1517. return false;
  1518. } elseif (!$this->IsANumber(substr($pricestring, 3), true)) {
  1519. return false;
  1520. }
  1521. return true;
  1522. }
  1523. function ID3v2FrameFlagsLookupTagAlter($framename) {
  1524. // unfinished
  1525. switch ($framename) {
  1526. case 'RGAD':
  1527. $allow = true;
  1528. default:
  1529. $allow = false;
  1530. break;
  1531. }
  1532. return $allow;
  1533. }
  1534. function ID3v2FrameFlagsLookupFileAlter($framename) {
  1535. // unfinished
  1536. switch ($framename) {
  1537. case 'RGAD':
  1538. return false;
  1539. break;
  1540. default:
  1541. return false;
  1542. break;
  1543. }
  1544. }
  1545. function ID3v2IsValidETCOevent($eventid) {
  1546. if (($eventid < 0) || ($eventid > 0xFF)) {
  1547. // outside range of 1 byte
  1548. return false;
  1549. } elseif (($eventid >= 0xF0) && ($eventid <= 0xFC)) {
  1550. // reserved for future use
  1551. return false;
  1552. } elseif (($eventid >= 0x17) && ($eventid <= 0xDF)) {
  1553. // reserved for future use
  1554. return false;
  1555. } elseif (($eventid >= 0x0E) && ($eventid <= 0x16) && ($this->majorversion == 2)) {
  1556. // not defined in ID3v2.2
  1557. return false;
  1558. } elseif (($eventid >= 0x15) && ($eventid <= 0x16) && ($this->majorversion == 3)) {
  1559. // not defined in ID3v2.3
  1560. return false;
  1561. }
  1562. return true;
  1563. }
  1564. function ID3v2IsValidSYLTtype($contenttype) {
  1565. if (($contenttype >= 0) && ($contenttype <= 8) && ($this->majorversion == 4)) {
  1566. return true;
  1567. } elseif (($contenttype >= 0) && ($contenttype <= 6) && ($this->majorversion == 3)) {
  1568. return true;
  1569. }
  1570. return false;
  1571. }
  1572. function ID3v2IsValidRVA2channeltype($channeltype) {
  1573. if (($channeltype >= 0) && ($channeltype <= 8) && ($this->majorversion == 4)) {
  1574. return true;
  1575. }
  1576. return false;
  1577. }
  1578. function ID3v2IsValidAPICpicturetype($picturetype) {
  1579. if (($picturetype >= 0) && ($picturetype <= 0x14) && ($this->majorversion >= 2) && ($this->majorversion <= 4)) {
  1580. return true;
  1581. }
  1582. return false;
  1583. }
  1584. function ID3v2IsValidAPICimageformat($imageformat) {
  1585. if ($imageformat == '-->') {
  1586. return true;
  1587. } elseif ($this->majorversion == 2) {
  1588. if ((strlen($imageformat) == 3) && ($imageformat == strtoupper($imageformat))) {
  1589. return true;
  1590. }
  1591. } elseif (($this->majorversion == 3) || ($this->majorversion == 4)) {
  1592. if ($this->IsValidMIMEstring($imageformat)) {
  1593. return true;
  1594. }
  1595. }
  1596. return false;
  1597. }
  1598. function ID3v2IsValidCOMRreceivedAs($receivedas) {
  1599. if (($this->majorversion >= 3) && ($receivedas >= 0) && ($receivedas <= 8)) {
  1600. return true;
  1601. }
  1602. return false;
  1603. }
  1604. function ID3v2IsValidRGADname($RGADname) {
  1605. if (($RGADname >= 0) && ($RGADname <= 2)) {
  1606. return true;
  1607. }
  1608. return false;
  1609. }
  1610. function ID3v2IsValidRGADoriginator($RGADoriginator) {
  1611. if (($RGADoriginator >= 0) && ($RGADoriginator <= 3)) {
  1612. return true;
  1613. }
  1614. return false;
  1615. }
  1616. function ID3v2IsValidTextEncoding($textencodingbyte) {
  1617. static $ID3v2IsValidTextEncoding_cache = array(
  1618. 2 => array(true, true),
  1619. 3 => array(true, true),
  1620. 4 => array(true, true, true, true));
  1621. return isset($ID3v2IsValidTextEncoding_cache[$this->majorversion][$textencodingbyte]);
  1622. }
  1623. function Unsynchronise($data) {
  1624. // Whenever a false synchronisation is found within the tag, one zeroed
  1625. // byte is inserted after the first false synchronisation byte. The
  1626. // format of a correct sync that should be altered by ID3 encoders is as
  1627. // follows:
  1628. // %11111111 111xxxxx
  1629. // And should be replaced with:
  1630. // %11111111 00000000 111xxxxx
  1631. // This has the side effect that all $FF 00 combinations have to be
  1632. // altered, so they won't be affected by the decoding process. Therefore
  1633. // all the $FF 00 combinations have to be replaced with the $FF 00 00
  1634. // combination during the unsynchronisation.
  1635. $data = str_replace("\xFF\x00", "\xFF\x00\x00", $data);
  1636. $unsyncheddata = '';
  1637. $datalength = strlen($data);
  1638. for ($i = 0; $i < $datalength; $i++) {
  1639. $thischar = $data{$i};
  1640. $unsyncheddata .= $thischar;
  1641. if ($thischar == "\xFF") {
  1642. $nextchar = ord($data{$i + 1});
  1643. if (($nextchar & 0xE0) == 0xE0) {
  1644. // previous byte = 11111111, this byte = 111?????
  1645. $unsyncheddata .= "\x00";
  1646. }
  1647. }
  1648. }
  1649. return $unsyncheddata;
  1650. }
  1651. function is_hash($var) {
  1652. // written by dev-nullŘchristophe*vg
  1653. // taken from http://www.php.net/manual/en/function.array-merge-recursive.php
  1654. if (is_array($var)) {
  1655. $keys = array_keys($var);
  1656. $all_num = true;
  1657. for ($i = 0; $i < count($keys); $i++) {
  1658. if (is_string($keys[$i])) {
  1659. return true;
  1660. }
  1661. }
  1662. }
  1663. return false;
  1664. }
  1665. function array_join_merge($arr1, $arr2) {
  1666. // written by dev-nullŘchristophe*vg
  1667. // taken from http://www.php.net/manual/en/function.array-merge-recursive.php
  1668. if (is_array($arr1) && is_array($arr2)) {
  1669. // the same -> merge
  1670. $new_array = array();
  1671. if ($this->is_hash($arr1) && $this->is_hash($arr2)) {
  1672. // hashes -> merge based on keys
  1673. $keys = array_merge(array_keys($arr1), array_keys($arr2));
  1674. foreach ($keys as $key) {
  1675. $new_array[$key] = $this->array_join_merge((isset($arr1[$key]) ? $arr1[$key] : ''), (isset($arr2[$key]) ? $arr2[$key] : ''));
  1676. }
  1677. } else {
  1678. // two real arrays -> merge
  1679. $new_array = array_reverse(array_unique(array_reverse(array_merge($arr1, $arr2))));
  1680. }
  1681. return $new_array;
  1682. } else {
  1683. // not the same ... take new one if defined, else the old one stays
  1684. return $arr2 ? $arr2 : $arr1;
  1685. }
  1686. }
  1687. function IsValidMIMEstring($mimestring) {
  1688. if ((strlen($mimestring) >= 3) && (strpos($mimestring, '/') > 0) && (strpos($mimestring, '/') < (strlen($mimestring) - 1))) {
  1689. return true;
  1690. }
  1691. return false;
  1692. }
  1693. function IsWithinBitRange($number, $maxbits, $signed=false) {
  1694. if ($signed) {
  1695. if (($number > (0 - pow(2, $maxbits - 1))) && ($number <= pow(2, $maxbits - 1))) {
  1696. return true;
  1697. }
  1698. } else {
  1699. if (($number >= 0) && ($number <= pow(2, $maxbits))) {
  1700. return true;
  1701. }
  1702. }
  1703. return false;
  1704. }
  1705. function safe_parse_url($url) {
  1706. $parts = @parse_url($url);
  1707. $parts['scheme'] = (isset($parts['scheme']) ? $parts['scheme'] : '');
  1708. $parts['host'] = (isset($parts['host']) ? $parts['host'] : '');
  1709. $parts['user'] = (isset($parts['user']) ? $parts['user'] : '');
  1710. $parts['pass'] = (isset($parts['pass']) ? $parts['pass'] : '');
  1711. $parts['path'] = (isset($parts['path']) ? $parts['path'] : '');
  1712. $parts['query'] = (isset($parts['query']) ? $parts['query'] : '');
  1713. return $parts;
  1714. }
  1715. function IsValidURL($url, $allowUserPass=false) {
  1716. if ($url == '') {
  1717. return false;
  1718. }
  1719. if ($allowUserPass !== true) {
  1720. if (strstr($url, '@')) {
  1721. // in the format http://user:pass@example.com or http://user@example.com
  1722. // but could easily be somebody incorrectly entering an email address in place of a URL
  1723. return false;
  1724. }
  1725. }
  1726. if ($parts = $this->safe_parse_url($url)) {
  1727. if (($parts['scheme'] != 'http') && ($parts['scheme'] != 'https') && ($parts['scheme'] != 'ftp') && ($parts['scheme'] != 'gopher')) {
  1728. return false;
  1729. } elseif (!preg_match('#^[[:alnum:]]([-.]?[0-9a-z])*\\.[a-z]{2,3}$#i', $parts['host'], $regs) && !preg_match('#^[0-9]{1,3}(\\.[0-9]{1,3}){3}$#', $parts['host'])) {
  1730. return false;
  1731. } elseif (!preg_match('#^([[:alnum:]-]|[\\_])*$#i', $parts['user'], $regs)) {
  1732. return false;
  1733. } elseif (!preg_match('#^([[:alnum:]-]|[\\_])*$#i', $parts['pass'], $regs)) {
  1734. return false;
  1735. } elseif (!preg_match('#^[[:alnum:]/_\\.@~-]*$#i', $parts['path'], $regs)) {
  1736. return false;
  1737. } elseif (!empty($parts['query']) && !preg_match('#^[[:alnum:]?&=+:;_()%\\#/,\\.-]*$#i', $parts['query'], $regs)) {
  1738. return false;
  1739. } else {
  1740. return true;
  1741. }
  1742. }
  1743. return false;
  1744. }
  1745. static function ID3v2ShortFrameNameLookup($majorversion, $long_description) {
  1746. $long_description = str_replace(' ', '_', strtolower(trim($long_description)));
  1747. static $ID3v2ShortFrameNameLookup = array();
  1748. if (empty($ID3v2ShortFrameNameLookup)) {
  1749. // The following are unique to ID3v2.2
  1750. $ID3v2ShortFrameNameLookup[2]['comment'] = 'COM';
  1751. $ID3v2ShortFrameNameLookup[2]['album'] = 'TAL';
  1752. $ID3v2ShortFrameNameLookup[2]['beats_per_minute'] = 'TBP';
  1753. $ID3v2ShortFrameNameLookup[2]['composer'] = 'TCM';
  1754. $ID3v2ShortFrameNameLookup[2]['genre'] = 'TCO';
  1755. $ID3v2ShortFrameNameLookup[2]['itunescompilation'] = 'TCP';
  1756. $ID3v2ShortFrameNameLookup[2]['copyright'] = 'TCR';
  1757. $ID3v2ShortFrameNameLookup[2]['encoded_by'] = 'TEN';
  1758. $ID3v2ShortFrameNameLookup[2]['language'] = 'TLA';
  1759. $ID3v2ShortFrameNameLookup[2]['length'] = 'TLE';
  1760. $ID3v2ShortFrameNameLookup[2]['original_artist'] = 'TOA';
  1761. $ID3v2ShortFrameNameLookup[2]['original_filename'] = 'TOF';
  1762. $ID3v2ShortFrameNameLookup[2]['original_lyricist'] = 'TOL';
  1763. $ID3v2ShortFrameNameLookup[2]['original_album_title'] = 'TOT';
  1764. $ID3v2ShortFrameNameLookup[2]['artist'] = 'TP1';
  1765. $ID3v2ShortFrameNameLookup[2]['band'] = 'TP2';
  1766. $ID3v2ShortFrameNameLookup[2]['conductor'] = 'TP3';
  1767. $ID3v2ShortFrameNameLookup[2]['remixer'] = 'TP4';
  1768. $ID3v2ShortFrameNameLookup[2]['publisher'] = 'TPB';
  1769. $ID3v2ShortFrameNameLookup[2]['isrc'] = 'TRC';
  1770. $ID3v2ShortFrameNameLookup[2]['tracknumber'] = 'TRK';
  1771. $ID3v2ShortFrameNameLookup[2]['size'] = 'TSI';
  1772. $ID3v2ShortFrameNameLookup[2]['encoder_settings'] = 'TSS';
  1773. $ID3v2ShortFrameNameLookup[2]['description'] = 'TT1';
  1774. $ID3v2ShortFrameNameLookup[2]['title'] = 'TT2';
  1775. $ID3v2ShortFrameNameLookup[2]['subtitle'] = 'TT3';
  1776. $ID3v2ShortFrameNameLookup[2]['lyricist'] = 'TXT';
  1777. $ID3v2ShortFrameNameLookup[2]['user_text'] = 'TXX';
  1778. $ID3v2ShortFrameNameLookup[2]['year'] = 'TYE';
  1779. $ID3v2ShortFrameNameLookup[2]['unique_file_identifier'] = 'UFI';
  1780. $ID3v2ShortFrameNameLookup[2]['unsynchronised_lyrics'] = 'ULT';
  1781. $ID3v2ShortFrameNameLookup[2]['url_file'] = 'WAF';
  1782. $ID3v2ShortFrameNameLookup[2]['url_artist'] = 'WAR';
  1783. $ID3v2ShortFrameNameLookup[2]['url_source'] = 'WAS';
  1784. $ID3v2ShortFrameNameLookup[2]['copyright_information'] = 'WCP';
  1785. $ID3v2ShortFrameNameLookup[2]['url_publisher'] = 'WPB';
  1786. $ID3v2ShortFrameNameLookup[2]['url_user'] = 'WXX';
  1787. // The following are common to ID3v2.3 and ID3v2.4
  1788. $ID3v2ShortFrameNameLookup[3]['audio_encryption'] = 'AENC';
  1789. $ID3v2ShortFrameNameLookup[3]['attached_picture'] = 'APIC';
  1790. $ID3v2ShortFrameNameLookup[3]['comment'] = 'COMM';
  1791. $ID3v2ShortFrameNameLookup[3]['commercial'] = 'COMR';
  1792. $ID3v2ShortFrameNameLookup[3]['encryption_method_registration'] = 'ENCR';
  1793. $ID3v2ShortFrameNameLookup[3]['event_timing_codes'] = 'ETCO';
  1794. $ID3v2ShortFrameNameLookup[3]['general_encapsulated_object'] = 'GEOB';
  1795. $ID3v2ShortFrameNameLookup[3]['group_identification_registration'] = 'GRID';
  1796. $ID3v2ShortFrameNameLookup[3]['linked_information'] = 'LINK';
  1797. $ID3v2ShortFrameNameLookup[3]['music_cd_identifier'] = 'MCDI';
  1798. $ID3v2ShortFrameNameLookup[3]['mpeg_location_lookup_table'] = 'MLLT';
  1799. $ID3v2ShortFrameNameLookup[3]['ownership'] = 'OWNE';
  1800. $ID3v2ShortFrameNameLookup[3]['play_counter'] = 'PCNT';
  1801. $ID3v2ShortFrameNameLookup[3]['popularimeter'] = 'POPM';
  1802. $ID3v2ShortFrameNameLookup[3]['position_synchronisation'] = 'POSS';
  1803. $ID3v2ShortFrameNameLookup[3]['private'] = 'PRIV';
  1804. $ID3v2ShortFrameNameLookup[3]['recommended_buffer_size'] = 'RBUF';
  1805. $ID3v2ShortFrameNameLookup[3]['reverb'] = 'RVRB';
  1806. $ID3v2ShortFrameNameLookup[3]['synchronised_lyrics'] = 'SYLT';
  1807. $ID3v2ShortFrameNameLookup[3]['synchronised_tempo_codes'] = 'SYTC';
  1808. $ID3v2ShortFrameNameLookup[3]['album'] = 'TALB';
  1809. $ID3v2ShortFrameNameLookup[3]['beats_per_minute'] = 'TBPM';
  1810. $ID3v2ShortFrameNameLookup[3]['itunescompilation'] = 'TCMP';
  1811. $ID3v2ShortFrameNameLookup[3]['composer'] = 'TCOM';
  1812. $ID3v2ShortFrameNameLookup[3]['genre'] = 'TCON';
  1813. $ID3v2ShortFrameNameLookup[3]['copyright'] = 'TCOP';
  1814. $ID3v2ShortFrameNameLookup[3]['playlist_delay'] = 'TDLY';
  1815. $ID3v2ShortFrameNameLookup[3]['encoded_by'] = 'TENC';
  1816. $ID3v2ShortFrameNameLookup[3]['lyricist'] = 'TEXT';
  1817. $ID3v2ShortFrameNameLookup[3]['file_type'] = 'TFLT';
  1818. $ID3v2ShortFrameNameLookup[3]['content_group_description'] = 'TIT1';
  1819. $ID3v2ShortFrameNameLookup[3]['title'] = 'TIT2';
  1820. $ID3v2ShortFrameNameLookup[3]['subtitle'] = 'TIT3';
  1821. $ID3v2ShortFrameNameLookup[3]['initial_key'] = 'TKEY';
  1822. $ID3v2ShortFrameNameLookup[3]['language'] = 'TLAN';
  1823. $ID3v2ShortFrameNameLookup[3]['length'] = 'TLEN';
  1824. $ID3v2ShortFrameNameLookup[3]['media_type'] = 'TMED';
  1825. $ID3v2ShortFrameNameLookup[3]['original_album_title'] = 'TOAL';
  1826. $ID3v2ShortFrameNameLookup[3]['original_filename'] = 'TOFN';
  1827. $ID3v2ShortFrameNameLookup[3]['original_lyricist'] = 'TOLY';
  1828. $ID3v2ShortFrameNameLookup[3]['original_artist'] = 'TOPE';
  1829. $ID3v2ShortFrameNameLookup[3]['file_owner'] = 'TOWN';
  1830. $ID3v2ShortFrameNameLookup[3]['artist'] = 'TPE1';
  1831. $ID3v2ShortFrameNameLookup[3]['band'] = 'TPE2';
  1832. $ID3v2ShortFrameNameLookup[3]['conductor'] = 'TPE3';
  1833. $ID3v2ShortFrameNameLookup[3]['remixer'] = 'TPE4';
  1834. $ID3v2ShortFrameNameLookup[3]['part_of_a_set'] = 'TPOS';
  1835. $ID3v2ShortFrameNameLookup[3]['publisher'] = 'TPUB';
  1836. $ID3v2ShortFrameNameLookup[3]['tracknumber'] = 'TRCK';
  1837. $ID3v2ShortFrameNameLookup[3]['internet_radio_station_name'] = 'TRSN';
  1838. $ID3v2ShortFrameNameLookup[3]['internet_radio_station_owner'] = 'TRSO';
  1839. $ID3v2ShortFrameNameLookup[3]['isrc'] = 'TSRC';
  1840. $ID3v2ShortFrameNameLookup[3]['encoder_settings'] = 'TSSE';
  1841. $ID3v2ShortFrameNameLookup[3]['user_text'] = 'TXXX';
  1842. $ID3v2ShortFrameNameLookup[3]['unique_file_identifier'] = 'UFID';
  1843. $ID3v2ShortFrameNameLookup[3]['terms_of_use'] = 'USER';
  1844. $ID3v2ShortFrameNameLookup[3]['unsynchronised_lyrics'] = 'USLT';
  1845. $ID3v2ShortFrameNameLookup[3]['commercial'] = 'WCOM';
  1846. $ID3v2ShortFrameNameLookup[3]['copyright_information'] = 'WCOP';
  1847. $ID3v2ShortFrameNameLookup[3]['url_file'] = 'WOAF';
  1848. $ID3v2ShortFrameNameLookup[3]['url_artist'] = 'WOAR';
  1849. $ID3v2ShortFrameNameLookup[3]['url_source'] = 'WOAS';
  1850. $ID3v2ShortFrameNameLookup[3]['url_station'] = 'WORS';
  1851. $ID3v2ShortFrameNameLookup[3]['payment'] = 'WPAY';
  1852. $ID3v2ShortFrameNameLookup[3]['url_publisher'] = 'WPUB';
  1853. $ID3v2ShortFrameNameLookup[3]['url_user'] = 'WXXX';
  1854. // The above are common to ID3v2.3 and ID3v2.4
  1855. // so copy them to ID3v2.4 before adding specifics for 2.3 and 2.4
  1856. $ID3v2ShortFrameNameLookup[4] = $ID3v2ShortFrameNameLookup[3];
  1857. // The following are unique to ID3v2.3
  1858. $ID3v2ShortFrameNameLookup[3]['equalisation'] = 'EQUA';
  1859. $ID3v2ShortFrameNameLookup[3]['involved_people_list'] = 'IPLS';
  1860. $ID3v2ShortFrameNameLookup[3]['relative_volume_adjustment'] = 'RVAD';
  1861. $ID3v2ShortFrameNameLookup[3]['date'] = 'TDAT';
  1862. $ID3v2ShortFrameNameLookup[3]['time'] = 'TIME';
  1863. $ID3v2ShortFrameNameLookup[3]['original_release_year'] = 'TORY';
  1864. $ID3v2ShortFrameNameLookup[3]['recording_dates'] = 'TRDA';
  1865. $ID3v2ShortFrameNameLookup[3]['size'] = 'TSIZ';
  1866. $ID3v2ShortFrameNameLookup[3]['year'] = 'TYER';
  1867. // The following are unique to ID3v2.4
  1868. $ID3v2ShortFrameNameLookup[4]['audio_seek_point_index'] = 'ASPI';
  1869. $ID3v2ShortFrameNameLookup[4]['equalisation'] = 'EQU2';
  1870. $ID3v2ShortFrameNameLookup[4]['relative_volume_adjustment'] = 'RVA2';
  1871. $ID3v2ShortFrameNameLookup[4]['seek'] = 'SEEK';
  1872. $ID3v2ShortFrameNameLookup[4]['signature'] = 'SIGN';
  1873. $ID3v2ShortFrameNameLookup[4]['encoding_time'] = 'TDEN';
  1874. $ID3v2ShortFrameNameLookup[4]['original_release_time'] = 'TDOR';
  1875. $ID3v2ShortFrameNameLookup[4]['recording_time'] = 'TDRC';
  1876. $ID3v2ShortFrameNameLookup[4]['release_time'] = 'TDRL';
  1877. $ID3v2ShortFrameNameLookup[4]['tagging_time'] = 'TDTG';
  1878. $ID3v2ShortFrameNameLookup[4]['involved_people_list'] = 'TIPL';
  1879. $ID3v2ShortFrameNameLookup[4]['musician_credits_list'] = 'TMCL';
  1880. $ID3v2ShortFrameNameLookup[4]['mood'] = 'TMOO';
  1881. $ID3v2ShortFrameNameLookup[4]['produced_notice'] = 'TPRO';
  1882. $ID3v2ShortFrameNameLookup[4]['album_sort_order'] = 'TSOA';
  1883. $ID3v2ShortFrameNameLookup[4]['performer_sort_order'] = 'TSOP';
  1884. $ID3v2ShortFrameNameLookup[4]['title_sort_order'] = 'TSOT';
  1885. $ID3v2ShortFrameNameLookup[4]['set_subtitle'] = 'TSST';
  1886. }
  1887. return (isset($ID3v2ShortFrameNameLookup[$majorversion][strtolower($long_description)]) ? $ID3v2ShortFrameNameLookup[$majorversion][strtolower($long_description)] : '');
  1888. }
  1889. }
  1890. ?>