write.metaflac.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.metaflac.php //
  12. // module for writing metaflac tags //
  13. // dependencies: /helperapps/metaflac.exe //
  14. // ///
  15. /////////////////////////////////////////////////////////////////
  16. class getid3_write_metaflac
  17. {
  18. public $filename;
  19. public $tag_data;
  20. public $warnings = array(); // any non-critical errors will be stored here
  21. public $errors = array(); // any critical errors will be stored here
  22. public function __construct() {
  23. return true;
  24. }
  25. public function WriteMetaFLAC() {
  26. if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
  27. $this->errors[] = 'PHP running in Safe Mode (backtick operator not available) - cannot call metaflac, tags not written';
  28. return false;
  29. }
  30. // Create file with new comments
  31. $tempcommentsfilename = tempnam(GETID3_TEMP_DIR, 'getID3');
  32. if (is_writable($tempcommentsfilename) && is_file($tempcommentsfilename) && ($fpcomments = fopen($tempcommentsfilename, 'wb'))) {
  33. foreach ($this->tag_data as $key => $value) {
  34. foreach ($value as $commentdata) {
  35. fwrite($fpcomments, $this->CleanmetaflacName($key).'='.$commentdata."\n");
  36. }
  37. }
  38. fclose($fpcomments);
  39. } else {
  40. $this->errors[] = 'failed to open temporary tags file, tags not written - fopen("'.$tempcommentsfilename.'", "wb")';
  41. return false;
  42. }
  43. $oldignoreuserabort = ignore_user_abort(true);
  44. if (GETID3_OS_ISWINDOWS) {
  45. if (file_exists(GETID3_HELPERAPPSDIR.'metaflac.exe')) {
  46. //$commandline = '"'.GETID3_HELPERAPPSDIR.'metaflac.exe" --no-utf8-convert --remove-all-tags --import-tags-from="'.$tempcommentsfilename.'" "'.str_replace('/', '\\', $this->filename).'"';
  47. // metaflac works fine if you copy-paste the above commandline into a command prompt,
  48. // but refuses to work with `backtick` if there are "doublequotes" present around BOTH
  49. // the metaflac pathname and the target filename. For whatever reason...??
  50. // The solution is simply ensure that the metaflac pathname has no spaces,
  51. // and therefore does not need to be quoted
  52. // On top of that, if error messages are not always captured properly under Windows
  53. // To at least see if there was a problem, compare file modification timestamps before and after writing
  54. clearstatcache();
  55. $timestampbeforewriting = filemtime($this->filename);
  56. $commandline = GETID3_HELPERAPPSDIR.'metaflac.exe --no-utf8-convert --remove-all-tags --import-tags-from='.escapeshellarg($tempcommentsfilename).' '.escapeshellarg($this->filename).' 2>&1';
  57. $metaflacError = `$commandline`;
  58. if (empty($metaflacError)) {
  59. clearstatcache();
  60. if ($timestampbeforewriting == filemtime($this->filename)) {
  61. $metaflacError = 'File modification timestamp has not changed - it looks like the tags were not written';
  62. }
  63. }
  64. } else {
  65. $metaflacError = 'metaflac.exe not found in '.GETID3_HELPERAPPSDIR;
  66. }
  67. } else {
  68. // It's simpler on *nix
  69. $commandline = 'metaflac --no-utf8-convert --remove-all-tags --import-tags-from='.escapeshellarg($tempcommentsfilename).' '.escapeshellarg($this->filename).' 2>&1';
  70. $metaflacError = `$commandline`;
  71. }
  72. // Remove temporary comments file
  73. unlink($tempcommentsfilename);
  74. ignore_user_abort($oldignoreuserabort);
  75. if (!empty($metaflacError)) {
  76. $this->errors[] = 'System call to metaflac failed with this message returned: '."\n\n".$metaflacError;
  77. return false;
  78. }
  79. return true;
  80. }
  81. public function DeleteMetaFLAC() {
  82. if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
  83. $this->errors[] = 'PHP running in Safe Mode (backtick operator not available) - cannot call metaflac, tags not deleted';
  84. return false;
  85. }
  86. $oldignoreuserabort = ignore_user_abort(true);
  87. if (GETID3_OS_ISWINDOWS) {
  88. if (file_exists(GETID3_HELPERAPPSDIR.'metaflac.exe')) {
  89. // To at least see if there was a problem, compare file modification timestamps before and after writing
  90. clearstatcache();
  91. $timestampbeforewriting = filemtime($this->filename);
  92. $commandline = GETID3_HELPERAPPSDIR.'metaflac.exe --remove-all-tags "'.$this->filename.'" 2>&1';
  93. $metaflacError = `$commandline`;
  94. if (empty($metaflacError)) {
  95. clearstatcache();
  96. if ($timestampbeforewriting == filemtime($this->filename)) {
  97. $metaflacError = 'File modification timestamp has not changed - it looks like the tags were not deleted';
  98. }
  99. }
  100. } else {
  101. $metaflacError = 'metaflac.exe not found in '.GETID3_HELPERAPPSDIR;
  102. }
  103. } else {
  104. // It's simpler on *nix
  105. $commandline = 'metaflac --remove-all-tags "'.$this->filename.'" 2>&1';
  106. $metaflacError = `$commandline`;
  107. }
  108. ignore_user_abort($oldignoreuserabort);
  109. if (!empty($metaflacError)) {
  110. $this->errors[] = 'System call to metaflac failed with this message returned: '."\n\n".$metaflacError;
  111. return false;
  112. }
  113. return true;
  114. }
  115. public function CleanmetaflacName($originalcommentname) {
  116. // A case-insensitive field name that may consist of ASCII 0x20 through 0x7D, 0x3D ('=') excluded.
  117. // ASCII 0x41 through 0x5A inclusive (A-Z) is to be considered equivalent to ASCII 0x61 through
  118. // 0x7A inclusive (a-z).
  119. // replace invalid chars with a space, return uppercase text
  120. // Thanks Chris Bolt <chris-getid3Øbolt*cx> for improving this function
  121. // note: *reg_replace() replaces nulls with empty string (not space)
  122. return strtoupper(preg_replace('#[^ -<>-}]#', ' ', str_replace("\x00", ' ', $originalcommentname)));
  123. }
  124. }