write.vorbiscomment.php 4.8 KB

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