PageRenderTime 37ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/sys/plugins/id3/getid3/write.id3v2.php

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