write.apetag.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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.apetag.php //
  12. // module for writing APE tags //
  13. // dependencies: module.tag.apetag.php //
  14. // ///
  15. /////////////////////////////////////////////////////////////////
  16. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.apetag.php', __FILE__, true);
  17. class getid3_write_apetag
  18. {
  19. public $filename;
  20. public $tag_data;
  21. public $always_preserve_replaygain = true; // ReplayGain / MP3gain tags will be copied from old tag even if not passed in data
  22. public $warnings = array(); // any non-critical errors will be stored here
  23. public $errors = array(); // any critical errors will be stored here
  24. public function __construct() {
  25. return true;
  26. }
  27. public function WriteAPEtag() {
  28. // NOTE: All data passed to this function must be UTF-8 format
  29. $getID3 = new getID3;
  30. $ThisFileInfo = $getID3->analyze($this->filename);
  31. if (isset($ThisFileInfo['ape']['tag_offset_start']) && isset($ThisFileInfo['lyrics3']['tag_offset_end'])) {
  32. if ($ThisFileInfo['ape']['tag_offset_start'] >= $ThisFileInfo['lyrics3']['tag_offset_end']) {
  33. // Current APE tag between Lyrics3 and ID3v1/EOF
  34. // This break Lyrics3 functionality
  35. if (!$this->DeleteAPEtag()) {
  36. return false;
  37. }
  38. $ThisFileInfo = $getID3->analyze($this->filename);
  39. }
  40. }
  41. if ($this->always_preserve_replaygain) {
  42. $ReplayGainTagsToPreserve = array('mp3gain_minmax', 'mp3gain_album_minmax', 'mp3gain_undo', 'replaygain_track_peak', 'replaygain_track_gain', 'replaygain_album_peak', 'replaygain_album_gain');
  43. foreach ($ReplayGainTagsToPreserve as $rg_key) {
  44. if (isset($ThisFileInfo['ape']['items'][strtolower($rg_key)]['data'][0]) && !isset($this->tag_data[strtoupper($rg_key)][0])) {
  45. $this->tag_data[strtoupper($rg_key)][0] = $ThisFileInfo['ape']['items'][strtolower($rg_key)]['data'][0];
  46. }
  47. }
  48. }
  49. if ($APEtag = $this->GenerateAPEtag()) {
  50. if (is_writable($this->filename) && is_file($this->filename) && ($fp = fopen($this->filename, 'a+b'))) {
  51. $oldignoreuserabort = ignore_user_abort(true);
  52. flock($fp, LOCK_EX);
  53. $PostAPEdataOffset = $ThisFileInfo['avdataend'];
  54. if (isset($ThisFileInfo['ape']['tag_offset_end'])) {
  55. $PostAPEdataOffset = max($PostAPEdataOffset, $ThisFileInfo['ape']['tag_offset_end']);
  56. }
  57. if (isset($ThisFileInfo['lyrics3']['tag_offset_start'])) {
  58. $PostAPEdataOffset = max($PostAPEdataOffset, $ThisFileInfo['lyrics3']['tag_offset_start']);
  59. }
  60. fseek($fp, $PostAPEdataOffset);
  61. $PostAPEdata = '';
  62. if ($ThisFileInfo['filesize'] > $PostAPEdataOffset) {
  63. $PostAPEdata = fread($fp, $ThisFileInfo['filesize'] - $PostAPEdataOffset);
  64. }
  65. fseek($fp, $PostAPEdataOffset);
  66. if (isset($ThisFileInfo['ape']['tag_offset_start'])) {
  67. fseek($fp, $ThisFileInfo['ape']['tag_offset_start']);
  68. }
  69. ftruncate($fp, ftell($fp));
  70. fwrite($fp, $APEtag, strlen($APEtag));
  71. if (!empty($PostAPEdata)) {
  72. fwrite($fp, $PostAPEdata, strlen($PostAPEdata));
  73. }
  74. flock($fp, LOCK_UN);
  75. fclose($fp);
  76. ignore_user_abort($oldignoreuserabort);
  77. return true;
  78. }
  79. }
  80. return false;
  81. }
  82. public function DeleteAPEtag() {
  83. $getID3 = new getID3;
  84. $ThisFileInfo = $getID3->analyze($this->filename);
  85. if (isset($ThisFileInfo['ape']['tag_offset_start']) && isset($ThisFileInfo['ape']['tag_offset_end'])) {
  86. if (is_writable($this->filename) && is_file($this->filename) && ($fp = fopen($this->filename, 'a+b'))) {
  87. flock($fp, LOCK_EX);
  88. $oldignoreuserabort = ignore_user_abort(true);
  89. fseek($fp, $ThisFileInfo['ape']['tag_offset_end']);
  90. $DataAfterAPE = '';
  91. if ($ThisFileInfo['filesize'] > $ThisFileInfo['ape']['tag_offset_end']) {
  92. $DataAfterAPE = fread($fp, $ThisFileInfo['filesize'] - $ThisFileInfo['ape']['tag_offset_end']);
  93. }
  94. ftruncate($fp, $ThisFileInfo['ape']['tag_offset_start']);
  95. fseek($fp, $ThisFileInfo['ape']['tag_offset_start']);
  96. if (!empty($DataAfterAPE)) {
  97. fwrite($fp, $DataAfterAPE, strlen($DataAfterAPE));
  98. }
  99. flock($fp, LOCK_UN);
  100. fclose($fp);
  101. ignore_user_abort($oldignoreuserabort);
  102. return true;
  103. }
  104. return false;
  105. }
  106. return true;
  107. }
  108. public function GenerateAPEtag() {
  109. // NOTE: All data passed to this function must be UTF-8 format
  110. $items = array();
  111. if (!is_array($this->tag_data)) {
  112. return false;
  113. }
  114. foreach ($this->tag_data as $key => $arrayofvalues) {
  115. if (!is_array($arrayofvalues)) {
  116. return false;
  117. }
  118. $valuestring = '';
  119. foreach ($arrayofvalues as $value) {
  120. $valuestring .= str_replace("\x00", '', $value)."\x00";
  121. }
  122. $valuestring = rtrim($valuestring, "\x00");
  123. // Length of the assigned value in bytes
  124. $tagitem = getid3_lib::LittleEndian2String(strlen($valuestring), 4);
  125. //$tagitem .= $this->GenerateAPEtagFlags(true, true, false, 0, false);
  126. $tagitem .= "\x00\x00\x00\x00";
  127. $tagitem .= $this->CleanAPEtagItemKey($key)."\x00";
  128. $tagitem .= $valuestring;
  129. $items[] = $tagitem;
  130. }
  131. return $this->GenerateAPEtagHeaderFooter($items, true).implode('', $items).$this->GenerateAPEtagHeaderFooter($items, false);
  132. }
  133. public function GenerateAPEtagHeaderFooter(&$items, $isheader=false) {
  134. $tagdatalength = 0;
  135. foreach ($items as $itemdata) {
  136. $tagdatalength += strlen($itemdata);
  137. }
  138. $APEheader = 'APETAGEX';
  139. $APEheader .= getid3_lib::LittleEndian2String(2000, 4);
  140. $APEheader .= getid3_lib::LittleEndian2String(32 + $tagdatalength, 4);
  141. $APEheader .= getid3_lib::LittleEndian2String(count($items), 4);
  142. $APEheader .= $this->GenerateAPEtagFlags(true, true, $isheader, 0, false);
  143. $APEheader .= str_repeat("\x00", 8);
  144. return $APEheader;
  145. }
  146. public function GenerateAPEtagFlags($header=true, $footer=true, $isheader=false, $encodingid=0, $readonly=false) {
  147. $APEtagFlags = array_fill(0, 4, 0);
  148. if ($header) {
  149. $APEtagFlags[0] |= 0x80; // Tag contains a header
  150. }
  151. if (!$footer) {
  152. $APEtagFlags[0] |= 0x40; // Tag contains no footer
  153. }
  154. if ($isheader) {
  155. $APEtagFlags[0] |= 0x20; // This is the header, not the footer
  156. }
  157. // 0: Item contains text information coded in UTF-8
  158. // 1: Item contains binary information °)
  159. // 2: Item is a locator of external stored information °°)
  160. // 3: reserved
  161. $APEtagFlags[3] |= ($encodingid << 1);
  162. if ($readonly) {
  163. $APEtagFlags[3] |= 0x01; // Tag or Item is Read Only
  164. }
  165. return chr($APEtagFlags[3]).chr($APEtagFlags[2]).chr($APEtagFlags[1]).chr($APEtagFlags[0]);
  166. }
  167. public function CleanAPEtagItemKey($itemkey) {
  168. $itemkey = preg_replace("#[^\x20-\x7E]#i", '', $itemkey);
  169. // http://www.personal.uni-jena.de/~pfk/mpp/sv8/apekey.html
  170. switch (strtoupper($itemkey)) {
  171. case 'EAN/UPC':
  172. case 'ISBN':
  173. case 'LC':
  174. case 'ISRC':
  175. $itemkey = strtoupper($itemkey);
  176. break;
  177. default:
  178. $itemkey = ucwords($itemkey);
  179. break;
  180. }
  181. return $itemkey;
  182. }
  183. }