PageRenderTime 78ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 1ms

/lib/modules/metadata/getid3/write.id3v2.php

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