write.id3v1.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.id3v1.php //
  12. // module for writing ID3v1 tags //
  13. // dependencies: module.tag.id3v1.php //
  14. // ///
  15. /////////////////////////////////////////////////////////////////
  16. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.id3v1.php', __FILE__, true);
  17. class getid3_write_id3v1
  18. {
  19. public $filename;
  20. public $filesize;
  21. public $tag_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 WriteID3v1() {
  28. // File MUST be writeable - CHMOD(646) at least
  29. if (!empty($this->filename) && is_readable($this->filename) && is_writable($this->filename) && is_file($this->filename)) {
  30. $this->setRealFileSize();
  31. if (($this->filesize <= 0) || !getid3_lib::intValueSupported($this->filesize)) {
  32. $this->errors[] = 'Unable to WriteID3v1('.$this->filename.') because filesize ('.$this->filesize.') is larger than '.round(PHP_INT_MAX / 1073741824).'GB';
  33. return false;
  34. }
  35. if ($fp_source = fopen($this->filename, 'r+b')) {
  36. fseek($fp_source, -128, SEEK_END);
  37. if (fread($fp_source, 3) == 'TAG') {
  38. fseek($fp_source, -128, SEEK_END); // overwrite existing ID3v1 tag
  39. } else {
  40. fseek($fp_source, 0, SEEK_END); // append new ID3v1 tag
  41. }
  42. $this->tag_data['track'] = (isset($this->tag_data['track']) ? $this->tag_data['track'] : (isset($this->tag_data['track_number']) ? $this->tag_data['track_number'] : (isset($this->tag_data['tracknumber']) ? $this->tag_data['tracknumber'] : '')));
  43. $new_id3v1_tag_data = getid3_id3v1::GenerateID3v1Tag(
  44. (isset($this->tag_data['title'] ) ? $this->tag_data['title'] : ''),
  45. (isset($this->tag_data['artist'] ) ? $this->tag_data['artist'] : ''),
  46. (isset($this->tag_data['album'] ) ? $this->tag_data['album'] : ''),
  47. (isset($this->tag_data['year'] ) ? $this->tag_data['year'] : ''),
  48. (isset($this->tag_data['genreid']) ? $this->tag_data['genreid'] : ''),
  49. (isset($this->tag_data['comment']) ? $this->tag_data['comment'] : ''),
  50. (isset($this->tag_data['track'] ) ? $this->tag_data['track'] : ''));
  51. fwrite($fp_source, $new_id3v1_tag_data, 128);
  52. fclose($fp_source);
  53. return true;
  54. } else {
  55. $this->errors[] = 'Could not fopen('.$this->filename.', "r+b")';
  56. return false;
  57. }
  58. }
  59. $this->errors[] = 'File is not writeable: '.$this->filename;
  60. return false;
  61. }
  62. public function FixID3v1Padding() {
  63. // ID3v1 data is supposed to be padded with NULL characters, but some taggers incorrectly use spaces
  64. // This function rewrites the ID3v1 tag with correct padding
  65. // Initialize getID3 engine
  66. $getID3 = new getID3;
  67. $getID3->option_tag_id3v2 = false;
  68. $getID3->option_tag_apetag = false;
  69. $getID3->option_tags_html = false;
  70. $getID3->option_extra_info = false;
  71. $getID3->option_tag_id3v1 = true;
  72. $ThisFileInfo = $getID3->analyze($this->filename);
  73. if (isset($ThisFileInfo['tags']['id3v1'])) {
  74. foreach ($ThisFileInfo['tags']['id3v1'] as $key => $value) {
  75. $id3v1data[$key] = implode(',', $value);
  76. }
  77. $this->tag_data = $id3v1data;
  78. return $this->WriteID3v1();
  79. }
  80. return false;
  81. }
  82. public function RemoveID3v1() {
  83. // File MUST be writeable - CHMOD(646) at least
  84. if (!empty($this->filename) && is_readable($this->filename) && is_writable($this->filename) && is_file($this->filename)) {
  85. $this->setRealFileSize();
  86. if (($this->filesize <= 0) || !getid3_lib::intValueSupported($this->filesize)) {
  87. $this->errors[] = 'Unable to RemoveID3v1('.$this->filename.') because filesize ('.$this->filesize.') is larger than '.round(PHP_INT_MAX / 1073741824).'GB';
  88. return false;
  89. }
  90. if ($fp_source = fopen($this->filename, 'r+b')) {
  91. fseek($fp_source, -128, SEEK_END);
  92. if (fread($fp_source, 3) == 'TAG') {
  93. ftruncate($fp_source, $this->filesize - 128);
  94. } else {
  95. // no ID3v1 tag to begin with - do nothing
  96. }
  97. fclose($fp_source);
  98. return true;
  99. } else {
  100. $this->errors[] = 'Could not fopen('.$this->filename.', "r+b")';
  101. }
  102. } else {
  103. $this->errors[] = $this->filename.' is not writeable';
  104. }
  105. return false;
  106. }
  107. public function setRealFileSize() {
  108. if (PHP_INT_MAX > 2147483647) {
  109. $this->filesize = filesize($this->filename);
  110. return true;
  111. }
  112. // 32-bit PHP will not return correct values for filesize() if file is >=2GB
  113. // but getID3->analyze() has workarounds to get actual filesize
  114. $getID3 = new getID3;
  115. $getID3->option_tag_id3v1 = false;
  116. $getID3->option_tag_id3v2 = false;
  117. $getID3->option_tag_apetag = false;
  118. $getID3->option_tags_html = false;
  119. $getID3->option_extra_info = false;
  120. $ThisFileInfo = $getID3->analyze($this->filename);
  121. $this->filesize = $ThisFileInfo['filesize'];
  122. return true;
  123. }
  124. }