write.metaflac.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at https://github.com/JamesHeinrich/getID3 //
  5. // or https://www.getid3.org //
  6. // or http://getid3.sourceforge.net //
  7. // see readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. // //
  10. // write.metaflac.php //
  11. // module for writing metaflac tags //
  12. // dependencies: /helperapps/metaflac.exe //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. class getid3_write_metaflac
  16. {
  17. /**
  18. * @var string
  19. */
  20. public $filename;
  21. /**
  22. * @var array
  23. */
  24. public $tag_data;
  25. /**
  26. * Any non-critical errors will be stored here.
  27. *
  28. * @var array
  29. */
  30. public $warnings = array();
  31. /**
  32. * Any critical errors will be stored here.
  33. *
  34. * @var array
  35. */
  36. public $errors = array();
  37. private $pictures = array();
  38. public function __construct() {
  39. }
  40. /**
  41. * @return bool
  42. */
  43. public function WriteMetaFLAC() {
  44. if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
  45. $this->errors[] = 'PHP running in Safe Mode (backtick operator not available) - cannot call metaflac, tags not written';
  46. return false;
  47. }
  48. $tempfilenames = array();
  49. if (!empty($this->tag_data['ATTACHED_PICTURE'])) {
  50. foreach ($this->tag_data['ATTACHED_PICTURE'] as $key => $picturedetails) {
  51. $temppicturefilename = tempnam(GETID3_TEMP_DIR, 'getID3');
  52. $tempfilenames[] = $temppicturefilename;
  53. if (getID3::is_writable($temppicturefilename) && is_file($temppicturefilename) && ($fpcomments = fopen($temppicturefilename, 'wb'))) {
  54. // https://xiph.org/flac/documentation_tools_flac.html#flac_options_picture
  55. // [TYPE]|[MIME-TYPE]|[DESCRIPTION]|[WIDTHxHEIGHTxDEPTH[/COLORS]]|FILE
  56. fwrite($fpcomments, $picturedetails['data']);
  57. fclose($fpcomments);
  58. $picture_typeid = (!empty($picturedetails['picturetypeid']) ? $this->ID3v2toFLACpictureTypes($picturedetails['picturetypeid']) : 3); // default to "3:Cover (front)"
  59. $picture_mimetype = (!empty($picturedetails['mime']) ? $picturedetails['mime'] : ''); // should be auto-detected
  60. $picture_width_height_depth = '';
  61. $this->pictures[] = $picture_typeid.'|'.$picture_mimetype.'|'.preg_replace('#[^\x20-\x7B\x7D-\x7F]#', '', $picturedetails['description']).'|'.$picture_width_height_depth.'|'.$temppicturefilename;
  62. } else {
  63. $this->errors[] = 'failed to open temporary tags file, tags not written - fopen("'.$temppicturefilename.'", "wb")';
  64. return false;
  65. }
  66. }
  67. unset($this->tag_data['ATTACHED_PICTURE']);
  68. }
  69. // Create file with new comments
  70. $tempcommentsfilename = tempnam(GETID3_TEMP_DIR, 'getID3');
  71. $tempfilenames[] = $tempcommentsfilename;
  72. if (getID3::is_writable($tempcommentsfilename) && is_file($tempcommentsfilename) && ($fpcomments = fopen($tempcommentsfilename, 'wb'))) {
  73. foreach ($this->tag_data as $key => $value) {
  74. foreach ($value as $commentdata) {
  75. fwrite($fpcomments, $this->CleanmetaflacName($key).'='.$commentdata."\n");
  76. }
  77. }
  78. fclose($fpcomments);
  79. } else {
  80. $this->errors[] = 'failed to open temporary tags file, tags not written - fopen("'.$tempcommentsfilename.'", "wb")';
  81. return false;
  82. }
  83. $oldignoreuserabort = ignore_user_abort(true);
  84. if (GETID3_OS_ISWINDOWS) {
  85. if (file_exists(GETID3_HELPERAPPSDIR.'metaflac.exe')) {
  86. //$commandline = '"'.GETID3_HELPERAPPSDIR.'metaflac.exe" --no-utf8-convert --remove-all-tags --import-tags-from="'.$tempcommentsfilename.'" "'.str_replace('/', '\\', $this->filename).'"';
  87. // metaflac works fine if you copy-paste the above commandline into a command prompt,
  88. // but refuses to work with `backtick` if there are "doublequotes" present around BOTH
  89. // the metaflac pathname and the target filename. For whatever reason...??
  90. // The solution is simply ensure that the metaflac pathname has no spaces,
  91. // and therefore does not need to be quoted
  92. // On top of that, if error messages are not always captured properly under Windows
  93. // To at least see if there was a problem, compare file modification timestamps before and after writing
  94. clearstatcache();
  95. $timestampbeforewriting = filemtime($this->filename);
  96. $commandline = GETID3_HELPERAPPSDIR.'metaflac.exe --no-utf8-convert --remove-all-tags --import-tags-from='.escapeshellarg($tempcommentsfilename);
  97. foreach ($this->pictures as $picturecommand) {
  98. $commandline .= ' --import-picture-from='.escapeshellarg($picturecommand);
  99. }
  100. $commandline .= ' '.escapeshellarg($this->filename).' 2>&1';
  101. $metaflacError = `$commandline`;
  102. if (empty($metaflacError)) {
  103. clearstatcache();
  104. if ($timestampbeforewriting == filemtime($this->filename)) {
  105. $metaflacError = 'File modification timestamp has not changed - it looks like the tags were not written';
  106. }
  107. }
  108. } else {
  109. $metaflacError = 'metaflac.exe not found in '.GETID3_HELPERAPPSDIR;
  110. }
  111. } else {
  112. // It's simpler on *nix
  113. $commandline = 'metaflac --no-utf8-convert --remove-all-tags --import-tags-from='.escapeshellarg($tempcommentsfilename);
  114. foreach ($this->pictures as $picturecommand) {
  115. $commandline .= ' --import-picture-from='.escapeshellarg($picturecommand);
  116. }
  117. $commandline .= ' '.escapeshellarg($this->filename).' 2>&1';
  118. $metaflacError = `$commandline`;
  119. }
  120. // Remove temporary comments file
  121. foreach ($tempfilenames as $tempfilename) {
  122. unlink($tempfilename);
  123. }
  124. ignore_user_abort($oldignoreuserabort);
  125. if (!empty($metaflacError)) {
  126. $this->errors[] = 'System call to metaflac failed with this message returned: '."\n\n".$metaflacError;
  127. return false;
  128. }
  129. return true;
  130. }
  131. /**
  132. * @return bool
  133. */
  134. public function DeleteMetaFLAC() {
  135. if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
  136. $this->errors[] = 'PHP running in Safe Mode (backtick operator not available) - cannot call metaflac, tags not deleted';
  137. return false;
  138. }
  139. $oldignoreuserabort = ignore_user_abort(true);
  140. if (GETID3_OS_ISWINDOWS) {
  141. if (file_exists(GETID3_HELPERAPPSDIR.'metaflac.exe')) {
  142. // To at least see if there was a problem, compare file modification timestamps before and after writing
  143. clearstatcache();
  144. $timestampbeforewriting = filemtime($this->filename);
  145. $commandline = GETID3_HELPERAPPSDIR.'metaflac.exe --remove-all-tags "'.$this->filename.'" 2>&1';
  146. $metaflacError = `$commandline`;
  147. if (empty($metaflacError)) {
  148. clearstatcache();
  149. if ($timestampbeforewriting == filemtime($this->filename)) {
  150. $metaflacError = 'File modification timestamp has not changed - it looks like the tags were not deleted';
  151. }
  152. }
  153. } else {
  154. $metaflacError = 'metaflac.exe not found in '.GETID3_HELPERAPPSDIR;
  155. }
  156. } else {
  157. // It's simpler on *nix
  158. $commandline = 'metaflac --remove-all-tags "'.$this->filename.'" 2>&1';
  159. $metaflacError = `$commandline`;
  160. }
  161. ignore_user_abort($oldignoreuserabort);
  162. if (!empty($metaflacError)) {
  163. $this->errors[] = 'System call to metaflac failed with this message returned: '."\n\n".$metaflacError;
  164. return false;
  165. }
  166. return true;
  167. }
  168. /**
  169. * @param int $id3v2_picture_typeid
  170. *
  171. * @return int
  172. */
  173. public function ID3v2toFLACpictureTypes($id3v2_picture_typeid) {
  174. // METAFLAC picture type list is identical to ID3v2 picture type list (as least up to 0x14 "Publisher/Studio logotype")
  175. // http://id3.org/id3v2.4.0-frames (section 4.14)
  176. // https://xiph.org/flac/documentation_tools_flac.html#flac_options_picture
  177. //return (isset($ID3v2toFLACpictureTypes[$id3v2_picture_typeid]) ? $ID3v2toFLACpictureTypes[$id3v2_picture_typeid] : 3); // default: "3: Cover (front)"
  178. return (($id3v2_picture_typeid <= 0x14) ? $id3v2_picture_typeid : 3); // default: "3: Cover (front)"
  179. }
  180. /**
  181. * @param string $originalcommentname
  182. *
  183. * @return string
  184. */
  185. public function CleanmetaflacName($originalcommentname) {
  186. // A case-insensitive field name that may consist of ASCII 0x20 through 0x7D, 0x3D ('=') excluded.
  187. // ASCII 0x41 through 0x5A inclusive (A-Z) is to be considered equivalent to ASCII 0x61 through
  188. // 0x7A inclusive (a-z).
  189. // replace invalid chars with a space, return uppercase text
  190. // Thanks Chris Bolt <chris-getid3Øbolt*cx> for improving this function
  191. // note: *reg_replace() replaces nulls with empty string (not space)
  192. return strtoupper(preg_replace('#[^ -<>-}]#', ' ', str_replace("\x00", ' ', $originalcommentname)));
  193. }
  194. }