PageRenderTime 73ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/zina/extras/getid3/write.id3v2.php

https://bitbucket.org/helmespc/zina2
PHP | 1892 lines | 1441 code | 146 blank | 305 comment | 423 complexity | 462e2b8b21f7d12f437fcc8ad965e2fe MD5 | raw file
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at http://getid3.sourceforge.net //
  5. // or http://www.getid3.org //
  6. /////////////////////////////////////////////////////////////////
  7. // See readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. /// //
  10. // write.id3v2.php //
  11. // module for writing ID3v2 tags //
  12. // dependencies: module.tag.id3v2.php //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.id3v2.php', __FILE__, true);
  16. class getid3_write_id3v2
  17. {
  18. var $filename;
  19. var $tag_data;
  20. var $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 = true; // 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. var $skip_errors = false; // if errors in frame, skip and write tag anyway
  29. function getid3_write_id3v2() {
  30. return true;
  31. }
  32. function WriteID3v2() {
  33. // File MUST be writeable - CHMOD(646) at least. It's best if the
  34. // directory is also writeable, because that method is both faster and less susceptible to errors.
  35. if (is_writeable($this->filename) || (!file_exists($this->filename) && is_writeable(dirname($this->filename)))) {
  36. // Initialize getID3 engine
  37. $getID3 = new getID3;
  38. $OldThisFileInfo = $getID3->analyze($this->filename);
  39. if ($OldThisFileInfo['filesize'] >= pow(2, 31)) {
  40. $this->errors[] = 'Unable to write ID3v2 because file is larger than 2GB';
  41. fclose($fp_source);
  42. return false;
  43. }
  44. if ($this->merge_existing_data) {
  45. // merge with existing data
  46. if (!empty($OldThisFileInfo['id3v2'])) {
  47. $this->tag_data = $this->array_join_merge($OldThisFileInfo['id3v2'], $this->tag_data);
  48. }
  49. }
  50. $this->paddedlength = max(@$OldThisFileInfo['id3v2']['headerlength'], $this->paddedlength);
  51. if ($NewID3v2Tag = $this->GenerateID3v2Tag()) {
  52. if (file_exists($this->filename) && is_writeable($this->filename) && isset($OldThisFileInfo['id3v2']['headerlength']) && ($OldThisFileInfo['id3v2']['headerlength'] == strlen($NewID3v2Tag))) {
  53. // best and fastest method - insert-overwrite existing tag (padded to length of old tag if neccesary)
  54. if (file_exists($this->filename)) {
  55. ob_start();
  56. if ($fp = fopen($this->filename, 'r+b')) {
  57. rewind($fp);
  58. fwrite($fp, $NewID3v2Tag, strlen($NewID3v2Tag));
  59. fclose($fp);
  60. } else {
  61. $this->errors[] = 'Could not open '.$this->filename.' mode "r+b" - '.strip_tags(ob_get_contents());
  62. }
  63. ob_end_clean();
  64. } else {
  65. ob_start();
  66. if ($fp = fopen($this->filename, 'wb')) {
  67. rewind($fp);
  68. fwrite($fp, $NewID3v2Tag, strlen($NewID3v2Tag));
  69. fclose($fp);
  70. } else {
  71. $this->errors[] = 'Could not open '.$this->filename.' mode "wb" - '.strip_tags(ob_get_contents());
  72. }
  73. ob_end_clean();
  74. }
  75. } else {
  76. if ($tempfilename = tempnam('*', 'getID3')) {
  77. ob_start();
  78. if ($fp_source = fopen($this->filename, 'rb')) {
  79. if ($fp_temp = fopen($tempfilename, 'wb')) {
  80. fwrite($fp_temp, $NewID3v2Tag, strlen($NewID3v2Tag));
  81. rewind($fp_source);
  82. if (!empty($OldThisFileInfo['avdataoffset'])) {
  83. fseek($fp_source, $OldThisFileInfo['avdataoffset'], SEEK_SET);
  84. }
  85. while ($buffer = fread($fp_source, GETID3_FREAD_BUFFER_SIZE)) {
  86. fwrite($fp_temp, $buffer, strlen($buffer));
  87. }
  88. fclose($fp_temp);
  89. fclose($fp_source);
  90. copy($tempfilename, $this->filename);
  91. unlink($tempfilename);
  92. ob_end_clean();
  93. return true;
  94. } else {
  95. $this->errors[] = 'Could not open '.$tempfilename.' mode "wb" - '.strip_tags(ob_get_contents());
  96. }
  97. fclose($fp_source);
  98. } else {
  99. $this->errors[] = 'Could not open '.$this->filename.' mode "rb" - '.strip_tags(ob_get_contents());
  100. }
  101. ob_end_clean();
  102. }
  103. return false;
  104. }
  105. } else {
  106. $this->errors[] = '$this->GenerateID3v2Tag() failed';
  107. }
  108. if (!empty($this->errors)) {
  109. return false;
  110. }
  111. return true;
  112. } else {
  113. $this->errors[] = '!is_writeable('.$this->filename.')';
  114. }
  115. return false;
  116. }
  117. function RemoveID3v2() {
  118. // File MUST be writeable - CHMOD(646) at least. It's best if the
  119. // directory is also writeable, because that method is both faster and less susceptible to errors.
  120. if (is_writeable(dirname($this->filename))) {
  121. // preferred method - only one copying operation, minimal chance of corrupting
  122. // original file if script is interrupted, but required directory to be writeable
  123. if ($fp_source = @fopen($this->filename, 'rb')) {
  124. // Initialize getID3 engine
  125. $getID3 = new getID3;
  126. $OldThisFileInfo = $getID3->analyze($this->filename);
  127. if ($OldThisFileInfo['filesize'] >= pow(2, 31)) {
  128. $this->errors[] = 'Unable to remove ID3v2 because file is larger than 2GB';
  129. fclose($fp_source);
  130. return false;
  131. }
  132. rewind($fp_source);
  133. if ($OldThisFileInfo['avdataoffset'] !== false) {
  134. fseek($fp_source, $OldThisFileInfo['avdataoffset'], SEEK_SET);
  135. }
  136. if ($fp_temp = @fopen($this->filename.'getid3tmp', 'w+b')) {
  137. while ($buffer = fread($fp_source, GETID3_FREAD_BUFFER_SIZE)) {
  138. fwrite($fp_temp, $buffer, strlen($buffer));
  139. }
  140. fclose($fp_temp);
  141. } else {
  142. $this->errors[] = 'Could not open '.$this->filename.'getid3tmp mode "w+b"';
  143. }
  144. fclose($fp_source);
  145. } else {
  146. $this->errors[] = 'Could not open '.$this->filename.' mode "rb"';
  147. }
  148. if (file_exists($this->filename)) {
  149. unlink($this->filename);
  150. }
  151. rename($this->filename.'getid3tmp', $this->filename);
  152. } elseif (is_writable($this->filename)) {
  153. // less desirable alternate method - double-copies the file, overwrites original file
  154. // and could corrupt source file if the script is interrupted or an error occurs.
  155. if ($fp_source = @fopen($this->filename, 'rb')) {
  156. // Initialize getID3 engine
  157. $getID3 = new getID3;
  158. $OldThisFileInfo = $getID3->analyze($this->filename);
  159. if ($OldThisFileInfo['filesize'] >= pow(2, 31)) {
  160. $this->errors[] = 'Unable to remove ID3v2 because file is larger than 2GB';
  161. fclose($fp_source);
  162. return false;
  163. }
  164. rewind($fp_source);
  165. if ($OldThisFileInfo['avdataoffset'] !== false) {
  166. fseek($fp_source, $OldThisFileInfo['avdataoffset'], SEEK_SET);
  167. }
  168. if ($fp_temp = tmpfile()) {
  169. while ($buffer = fread($fp_source, GETID3_FREAD_BUFFER_SIZE)) {
  170. fwrite($fp_temp, $buffer, strlen($buffer));
  171. }
  172. fclose($fp_source);
  173. if ($fp_source = @fopen($this->filename, 'wb')) {
  174. rewind($fp_temp);
  175. while ($buffer = fread($fp_temp, GETID3_FREAD_BUFFER_SIZE)) {
  176. fwrite($fp_source, $buffer, strlen($buffer));
  177. }
  178. fseek($fp_temp, -128, SEEK_END);
  179. fclose($fp_source);
  180. } else {
  181. $this->errors[] = 'Could not open '.$this->filename.' mode "wb"';
  182. }
  183. fclose($fp_temp);
  184. } else {
  185. $this->errors[] = 'Could not create tmpfile()';
  186. }
  187. } else {
  188. $this->errors[] = 'Could not open '.$this->filename.' mode "rb"';
  189. }
  190. } else {
  191. $this->errors[] = 'Directory and file both not writeable';
  192. }
  193. if (!empty($this->errors)) {
  194. return false;
  195. }
  196. return true;
  197. }
  198. function GenerateID3v2TagFlags($flags) {
  199. switch ($this->majorversion) {
  200. case 4:
  201. // %abcd0000
  202. $flag = (@$flags['unsynchronisation'] ? '1' : '0'); // a - Unsynchronisation
  203. $flag .= (@$flags['extendedheader'] ? '1' : '0'); // b - Extended header
  204. $flag .= (@$flags['experimental'] ? '1' : '0'); // c - Experimental indicator
  205. $flag .= (@$flags['footer'] ? '1' : '0'); // d - Footer present
  206. $flag .= '0000';
  207. break;
  208. case 3:
  209. // %abc00000
  210. $flag = (@$flags['unsynchronisation'] ? '1' : '0'); // a - Unsynchronisation
  211. $flag .= (@$flags['extendedheader'] ? '1' : '0'); // b - Extended header
  212. $flag .= (@$flags['experimental'] ? '1' : '0'); // c - Experimental indicator
  213. $flag .= '00000';
  214. break;
  215. case 2:
  216. // %ab000000
  217. $flag = (@$flags['unsynchronisation'] ? '1' : '0'); // a - Unsynchronisation
  218. $flag .= (@$flags['compression'] ? '1' : '0'); // b - Compression
  219. $flag .= '000000';
  220. break;
  221. default:
  222. return false;
  223. break;
  224. }
  225. return chr(bindec($flag));
  226. }
  227. function GenerateID3v2FrameFlags($TagAlter=false, $FileAlter=false, $ReadOnly=false, $Compression=false, $Encryption=false, $GroupingIdentity=false, $Unsynchronisation=false, $DataLengthIndicator=false) {
  228. switch ($this->majorversion) {
  229. case 4:
  230. // %0abc0000 %0h00kmnp
  231. $flag1 = '0';
  232. $flag1 .= $TagAlter ? '1' : '0'; // a - Tag alter preservation (true == discard)
  233. $flag1 .= $FileAlter ? '1' : '0'; // b - File alter preservation (true == discard)
  234. $flag1 .= $ReadOnly ? '1' : '0'; // c - Read only (true == read only)
  235. $flag1 .= '0000';
  236. $flag2 = '0';
  237. $flag2 .= $GroupingIdentity ? '1' : '0'; // h - Grouping identity (true == contains group information)
  238. $flag2 .= '00';
  239. $flag2 .= $Compression ? '1' : '0'; // k - Compression (true == compressed)
  240. $flag2 .= $Encryption ? '1' : '0'; // m - Encryption (true == encrypted)
  241. $flag2 .= $Unsynchronisation ? '1' : '0'; // n - Unsynchronisation (true == unsynchronised)
  242. $flag2 .= $DataLengthIndicator ? '1' : '0'; // p - Data length indicator (true == data length indicator added)
  243. break;
  244. case 3:
  245. // %abc00000 %ijk00000
  246. $flag1 = $TagAlter ? '1' : '0'; // a - Tag alter preservation (true == discard)
  247. $flag1 .= $FileAlter ? '1' : '0'; // b - File alter preservation (true == discard)
  248. $flag1 .= $ReadOnly ? '1' : '0'; // c - Read only (true == read only)
  249. $flag1 .= '00000';
  250. $flag2 = $Compression ? '1' : '0'; // i - Compression (true == compressed)
  251. $flag2 .= $Encryption ? '1' : '0'; // j - Encryption (true == encrypted)
  252. $flag2 .= $GroupingIdentity ? '1' : '0'; // k - Grouping identity (true == contains group information)
  253. $flag2 .= '00000';
  254. break;
  255. default:
  256. return false;
  257. break;
  258. }
  259. return chr(bindec($flag1)).chr(bindec($flag2));
  260. }
  261. function GenerateID3v2FrameData($frame_name, $source_data_array) {
  262. if (!getid3_id3v2::IsValidID3v2FrameName($frame_name, $this->majorversion)) {
  263. return false;
  264. }
  265. $framedata = '';
  266. if (($this->majorversion < 3) || ($this->majorversion > 4)) {
  267. $this->errors[] = 'Only ID3v2.3 and ID3v2.4 are supported in GenerateID3v2FrameData()';
  268. } else { // $this->majorversion 3 or 4
  269. switch ($frame_name) {
  270. case 'UFID':
  271. // 4.1 UFID Unique file identifier
  272. // Owner identifier <text string> $00
  273. // Identifier <up to 64 bytes binary data>
  274. if (strlen($source_data_array['data']) > 64) {
  275. $this->errors[] = 'Identifier not allowed to be longer than 64 bytes in '.$frame_name.' (supplied data was '.strlen($source_data_array['data']).' bytes long)';
  276. } else {
  277. $framedata .= str_replace("\x00", '', $source_data_array['ownerid'])."\x00";
  278. $framedata .= substr($source_data_array['data'], 0, 64); // max 64 bytes - truncate anything longer
  279. }
  280. break;
  281. case 'TXXX':
  282. // 4.2.2 TXXX User defined text information frame
  283. // Text encoding $xx
  284. // Description <text string according to encoding> $00 (00)
  285. // Value <text string according to encoding>
  286. $source_data_array['encodingid'] = (isset($source_data_array['encodingid']) ? $source_data_array['encodingid'] : $this->id3v2_default_encodingid);
  287. if (!$this->ID3v2IsValidTextEncoding($source_data_array['encodingid'], $this->majorversion)) {
  288. $this->errors[] = 'Invalid Text Encoding in '.$frame_name.' ('.$source_data_array['encodingid'].') for ID3v2.'.$this->majorversion;
  289. } else {
  290. $framedata .= chr($source_data_array['encodingid']);
  291. $framedata .= $source_data_array['description'].getid3_id3v2::TextEncodingTerminatorLookup($source_data_array['encodingid']);
  292. $framedata .= $source_data_array['data'];
  293. }
  294. break;
  295. case 'WXXX':
  296. // 4.3.2 WXXX User defined URL link frame
  297. // Text encoding $xx
  298. // Description <text string according to encoding> $00 (00)
  299. // URL <text string>
  300. $source_data_array['encodingid'] = (isset($source_data_array['encodingid']) ? $source_data_array['encodingid'] : $this->id3v2_default_encodingid);
  301. if (!$this->ID3v2IsValidTextEncoding($source_data_array['encodingid'], $this->majorversion)) {
  302. $this->errors[] = 'Invalid Text Encoding in '.$frame_name.' ('.$source_data_array['encodingid'].') for ID3v2.'.$this->majorversion;
  303. } elseif (!isset($source_data_array['data']) || !$this->IsValidURL($source_data_array['data'], false, false)) {
  304. // need to rewrite IsValidURL() to handle other encodings
  305. $this->errors[] = 'Invalid URL in '.$frame_name.(isset($source_data_array['data']) ? ' ('.$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. if (!getid3_id3v2::FrameNameShortLookup($frame_name, $this->majorversion)) {
  1422. #$this->warnings[] = 'Ignoring invalid ID3v2 frame type: "'.$frame_name.'"';
  1423. continue;
  1424. }
  1425. foreach ($frame_rawinputdata as $source_data_array) {
  1426. unset($frame_length);
  1427. unset($frame_flags);
  1428. $frame_data = false;
  1429. if ($this->ID3v2FrameIsAllowed($frame_name, $source_data_array)) {
  1430. if ($frame_data = $this->GenerateID3v2FrameData($frame_name, $source_data_array)) {
  1431. $FrameUnsynchronisation = false;
  1432. if ($this->majorversion >= 4) {
  1433. // frame-level unsynchronisation
  1434. $unsynchdata = $frame_data;
  1435. if ($this->id3v2_use_unsynchronisation) {
  1436. $unsynchdata = $this->Unsynchronise($frame_data);
  1437. }
  1438. if (strlen($unsynchdata) != strlen($frame_data)) {
  1439. // unsynchronisation needed
  1440. $FrameUnsynchronisation = true;
  1441. $frame_data = $unsynchdata;
  1442. if (isset($TagUnsynchronisation) && $TagUnsynchronisation === false) {
  1443. // only set to true if ALL frames are unsynchronised
  1444. } else {
  1445. $TagUnsynchronisation = true;
  1446. }
  1447. } else {
  1448. if (isset($TagUnsynchronisation)) {
  1449. $TagUnsynchronisation = false;
  1450. }
  1451. }
  1452. unset($unsynchdata);
  1453. $frame_length = getid3_lib::BigEndian2String(strlen($frame_data), 4, true);
  1454. } else {
  1455. $frame_length = getid3_lib::BigEndian2String(strlen($frame_data), 4, false);
  1456. }
  1457. $frame_flags = $this->GenerateID3v2FrameFlags($this->ID3v2FrameFlagsLookupTagAlter($frame_name), $this->ID3v2FrameFlagsLookupFileAlter($frame_name), false, false, false, false, $FrameUnsynchronisation, false);
  1458. $tagstring .= $frame_name.$frame_length.$frame_flags.$frame_data;
  1459. } else {
  1460. $this->errors[] = '$this->GenerateID3v2FrameData() failed for "'.$frame_name.'"';
  1461. }
  1462. } else {
  1463. $this->errors[] = 'Frame "'.$frame_name.'" is NOT allowed';
  1464. }
  1465. if (!empty($this->errors)) {
  1466. if ($this->skip_errors) {
  1467. $this->warnings = array_merge($this->warnings, $this->errors);
  1468. $this->errors = array();
  1469. break;
  1470. } else {
  1471. return false;
  1472. }
  1473. }
  1474. } # data loop
  1475. } # frame loop
  1476. if (!isset($TagUnsynchronisation)) $TagUnsynchronisation = false;
  1477. if (($this->majorversion <= 3) && $this->id3v2_use_unsynchronisation) {
  1478. // tag-level unsynchronisation
  1479. $unsynchdata = $this->Unsynchronise($tagstring);
  1480. if (strlen($unsynchdata) != strlen($tagstring)) {
  1481. // unsynchronisation needed
  1482. $TagUnsynchronisation = true;
  1483. $tagstring = $unsynchdata;
  1484. }
  1485. }
  1486. while ($this->paddedlength < (strlen($tagstring) + getid3_id3v2::ID3v2HeaderLength($this->majorversion))) {
  1487. $this->paddedlength += 1024;
  1488. }
  1489. $footer = false; // ID3v2 footers not yet supported in getID3()
  1490. if (!$footer && ($this->paddedlength > (strlen($tagstring) + getid3_id3v2::ID3v2HeaderLength($this->majorversion)))) {
  1491. // pad up to $paddedlength bytes if unpadded tag is shorter than $paddedlength
  1492. // "Furthermore it MUST NOT have any padding when a tag footer is added to the tag."
  1493. $tagstring .= @str_repeat("\x00", $this->paddedlength - strlen($tagstring) - getid3_id3v2::ID3v2HeaderLength($this->majorversion));
  1494. }
  1495. if ($this->id3v2_use_unsynchronisation && (substr($tagstring, strlen($tagstring) - 1, 1) == "\xFF")) {
  1496. // special unsynchronisation case:
  1497. // if last byte == $FF then appended a $00
  1498. $TagUnsynchronisation = true;
  1499. $tagstring .= "\x00";
  1500. }
  1501. $tagheader = 'ID3';
  1502. $tagheader .= chr($this->majorversion);
  1503. $tagheader .= chr($this->minorversion);
  1504. $tagheader .= $this->GenerateID3v2TagFlags(array('unsynchronisation'=>$TagUnsynchronisation));
  1505. $tagheader .= getid3_lib::BigEndian2String(strlen($tagstring), 4, true);
  1506. return $tagheader.$tagstring;
  1507. }
  1508. $this->errors[] = 'tag_data is not an array in GenerateID3v2Tag()';
  1509. return false;
  1510. }
  1511. function ID3v2IsValidPriceString($pricestring) {
  1512. if (getid3_id3v2::LanguageLookup(substr($pricestring, 0, 3), true) == '') {
  1513. return false;
  1514. } elseif (!$this->IsANumber(substr($pricestring, 3), true)) {
  1515. return false;
  1516. }
  1517. return true;
  1518. }
  1519. function ID3v2FrameFlagsLookupTagAlter($framename) {
  1520. // unfinished
  1521. switch ($framename) {
  1522. case 'RGAD':
  1523. $allow = true;
  1524. default:
  1525. $allow = false;
  1526. break;
  1527. }
  1528. return $allow;
  1529. }
  1530. function ID3v2FrameFlagsLookupFileAlter($framename) {
  1531. // unfinished
  1532. switch ($framename) {
  1533. case 'RGAD':
  1534. return false;
  1535. break;
  1536. default:
  1537. return false;
  1538. break;
  1539. }
  1540. }
  1541. function ID3v2IsValidETCOevent($eventid) {
  1542. if (($eventid < 0) || ($eventid > 0xFF)) {
  1543. // outside range of 1 byte
  1544. return false;
  1545. } elseif (($eventid >= 0xF0) && ($eventid <= 0xFC)) {
  1546. // reserved for future use
  1547. return false;
  1548. } elseif (($eventid >= 0x17) && ($eventid <= 0xDF)) {
  1549. // reserved for future use
  1550. return false;
  1551. } elseif (($eventid >= 0x0E) && ($eventid <= 0x16) && ($this->majorversion == 2)) {
  1552. // not defined in ID3v2.2
  1553. return false;
  1554. } elseif (($eventid >= 0x15) && ($eventid <= 0x16) && ($this->majorversion == 3)) {
  1555. // not defined in ID3v2.3
  1556. return false;
  1557. }
  1558. return true;
  1559. }
  1560. function ID3v2IsValidSYLTtype($contenttype) {
  1561. if (($contenttype >= 0) && ($contenttype <= 8) && ($this->majorversion == 4)) {
  1562. return true;
  1563. } elseif (($contenttype >= 0) && ($contenttype <= 6) && ($this->majorversion == 3)) {
  1564. return true;
  1565. }
  1566. return false;
  1567. }
  1568. function ID3v2IsValidRVA2channeltype($channeltype) {
  1569. if (($channeltype >= 0) && ($channeltype <= 8) && ($this->majorversion == 4)) {
  1570. return true;
  1571. }
  1572. return false;
  1573. }
  1574. function ID3v2IsValidAPICpicturetype($picturetype) {
  1575. if (($picturetype >= 0) && ($picturetype <= 0x14) && ($this->majorversion >= 2) && ($this->majorversion <= 4)) {
  1576. return true;
  1577. }
  1578. return false;
  1579. }
  1580. function ID3v2IsValidAPICimageformat($imageformat) {
  1581. if ($imageformat == '-->') {
  1582. return true;
  1583. } elseif ($this->majorversion == 2) {
  1584. if ((strlen($imageformat) == 3) && ($imageformat == strtoupper($imageformat))) {
  1585. return true;
  1586. }
  1587. } elseif (($this->majorversion == 3) || ($this->majorversion == 4)) {
  1588. if ($this->IsValidMIMEstring($imageformat)) {
  1589. return true;
  1590. }
  1591. }
  1592. return false;
  1593. }
  1594. function ID3v2IsValidCOMRreceivedAs($receivedas) {
  1595. if (($this->majorversion >= 3) && ($receivedas >= 0) && ($receivedas <= 8)) {
  1596. return true;
  1597. }
  1598. return false;
  1599. }
  1600. function ID3v2IsValidRGADname($RGADname) {
  1601. if (($RGADname >= 0) && ($RGADname <= 2)) {
  1602. return true;
  1603. }
  1604. return false;
  1605. }
  1606. function ID3v2IsValidRGADoriginator($RGADoriginator) {
  1607. if (($RGADoriginator >= 0) && ($RGADoriginator <= 3)) {
  1608. return true;
  1609. }
  1610. return false;
  1611. }
  1612. function ID3v2IsValidTextEncoding($textencodingbyte) {
  1613. static $ID3v2IsValidTextEncoding_cache = array(
  1614. 2 => array(true, true),
  1615. 3 => array(true, true),
  1616. 4 => array(true, true, true, true));
  1617. return isset($ID3v2IsValidTextEncoding_cache[$this->majorversion][$textencodingbyte]);
  1618. }
  1619. function Unsynchronise($data) {
  1620. // Whenever a false synchronisation is found within the tag, one zeroed
  1621. // byte is inserted after the first false synchronisation byte. The
  1622. // format of a correct sync that should be altered by ID3 encoders is as
  1623. // follows:
  1624. // %11111111 111xxxxx
  1625. // And should be replaced with:
  1626. // %11111111 00000000 111xxxxx
  1627. // This has the side effect that all $FF 00 combinations have to be
  1628. // altered, so they won't be affected by the decoding process. Therefore
  1629. // all the $FF 00 combinations have to be replaced with the $FF 00 00
  1630. // combination during the unsynchronisation.
  1631. $data = str_replace("\xFF\x00", "\xFF\x00\x00", $data);
  1632. $unsyncheddata = '';
  1633. $datalength = strlen($data);
  1634. for ($i = 0; $i < $datalength; $i++) {
  1635. $thischar = $data{$i};
  1636. $unsyncheddata .= $thischar;
  1637. if ($thischar == "\xFF") {
  1638. $nextchar = ord($data{$i + 1});
  1639. if (($nextchar & 0xE0) == 0xE0) {
  1640. // previous byte = 11111111, this byte = 111?????
  1641. $unsyncheddata .= "\x00";
  1642. }
  1643. }
  1644. }
  1645. return $unsyncheddata;
  1646. }
  1647. function is_hash($var) {
  1648. // written by dev-nullØchristophe*vg
  1649. // taken from http://www.php.net/manual/en/function.array-merge-recursive.php
  1650. if (is_array($var)) {
  1651. $keys = array_keys($var);
  1652. $all_num = true;
  1653. for ($i = 0; $i < count($keys); $i++) {
  1654. if (is_string($keys[$i])) {
  1655. return true;
  1656. }
  1657. }
  1658. }
  1659. return false;
  1660. }
  1661. function array_join_merge($arr1, $arr2) {
  1662. // written by dev-nullØchristophe*vg
  1663. // taken from http://www.php.net/manual/en/function.array-merge-recursive.php
  1664. if (is_array($arr1) && is_array($arr2)) {
  1665. // the same -> merge
  1666. $new_array = array();
  1667. if ($this->is_hash($arr1) && $this->is_hash($arr2)) {
  1668. // hashes -> merge based on keys
  1669. $keys = array_merge(array_keys($arr1), array_keys($arr2));
  1670. foreach ($keys as $key) {
  1671. $new_array[$key] = $this->array_join_merge(@$arr1[$key], @$arr2[$key]);
  1672. }
  1673. } else {
  1674. // two real arrays -> merge
  1675. $new_array = array_reverse(array_unique(array_reverse(array_merge($arr1, $arr2))));
  1676. }
  1677. return $new_array;
  1678. } else {
  1679. // not the same ... take new one if defined, else the old one stays
  1680. return $arr2 ? $arr2 : $arr1;
  1681. }
  1682. }
  1683. function IsValidMIMEstring($mimestring) {
  1684. if ((strlen($mimestring) >= 3) && (strpos($mimestring, '/') > 0) && (strpos($mimestring, '/') < (strlen($mimestring) - 1))) {
  1685. return true;
  1686. }
  1687. return false;
  1688. }
  1689. function IsWithinBitRange($number, $maxbits, $signed=false) {
  1690. if ($signed) {
  1691. if (($number > (0 - pow(2, $maxbits - 1))) && ($number <= pow(2, $maxbits - 1))) {
  1692. return true;
  1693. }
  1694. } else {
  1695. if (($number >= 0) && ($number <= pow(2, $maxbits))) {
  1696. return true;
  1697. }
  1698. }
  1699. return false;
  1700. }
  1701. function safe_parse_url($url) {
  1702. $parts = @parse_url($url);
  1703. $parts['scheme'] = (isset($parts['scheme']) ? $parts['scheme'] : '');
  1704. $parts['host'] = (isset($parts['host']) ? $parts['host'] : '');
  1705. $parts['user'] = (isset($parts['user']) ? $parts['user'] : '');
  1706. $parts['pass'] = (isset($parts['pass']) ? $parts['pass'] : '');
  1707. $parts['path'] = (isset($parts['path']) ? $parts['path'] : '');
  1708. $parts['query'] = (isset($parts['query']) ? $parts['query'] : '');
  1709. return $parts;
  1710. }
  1711. function IsValidURL($url, $allowUserPass=false) {
  1712. if ($url == '') {
  1713. return false;
  1714. }
  1715. if ($allowUserPass !== true) {
  1716. if (strstr($url, '@')) {
  1717. // in the format http://user:pass@example.com or http://user@example.com
  1718. // but could easily be somebody incorrectly entering an email address in place of a URL
  1719. return false;
  1720. }
  1721. }
  1722. if ($parts = $this->safe_parse_url($url)) {
  1723. if (($parts['scheme'] != 'http') && ($parts['scheme'] != 'https') && ($parts['scheme'] != 'ftp') && ($parts['scheme'] != 'gopher')) {
  1724. return false;
  1725. } elseif (!eregi("^[[:alnum:]]([-.]?[0-9a-z])*\.[a-z]{2,3}$", $parts['host'], $regs) && !IsValidDottedIP($parts['host'])) {
  1726. return false;
  1727. } elseif (!eregi("^([[:alnum:]-]|[\_])*$", $parts['user'], $regs)) {
  1728. return false;
  1729. } elseif (!eregi("^([[:alnum:]-]|[\_])*$", $parts['pass'], $regs)) {
  1730. return false;
  1731. } elseif (!eregi("^[[:alnum:]/_\.@~-]*$", $parts['path'], $regs)) {
  1732. return false;
  1733. } elseif (!eregi("^[[:alnum:]?&=+:;_()%#/,\.-]*$", $parts['query'], $regs)) {
  1734. return false;
  1735. } else {
  1736. return true;
  1737. }
  1738. }
  1739. return false;
  1740. }
  1741. function ID3v2ShortFrameNameLookup($majorversion, $description) {
  1742. $description = str_replace(' ', '_', strtolower(trim($description)));
  1743. return getid3_id3v2::FrameNameShortLookup($description, $majorversion, true);
  1744. }
  1745. }
  1746. ?>