write.metaflac.php 5.9 KB

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