write.id3v1.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.id3v1.php //
  11. // module for writing ID3v1 tags //
  12. // dependencies: module.tag.id3v1.php //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.id3v1.php', __FILE__, true);
  16. class getid3_write_id3v1
  17. {
  18. var $filename;
  19. var $tag_data;
  20. var $warnings = array(); // any non-critical errors will be stored here
  21. var $errors = array(); // any critical errors will be stored here
  22. function getid3_write_id3v1() {
  23. return true;
  24. }
  25. function WriteID3v1() {
  26. if ((filesize($this->filename) >= (pow(2, 31) - 128)) || (filesize($this->filename) < 0)) {
  27. $this->errors[] = 'Unable to write ID3v1 because file is larger than 2GB';
  28. return false;
  29. }
  30. // File MUST be writeable - CHMOD(646) at least
  31. if (is_writeable($this->filename)) {
  32. if ($fp_source = @fopen($this->filename, 'r+b')) {
  33. fseek($fp_source, -128, SEEK_END);
  34. if (fread($fp_source, 3) == 'TAG') {
  35. fseek($fp_source, -128, SEEK_END); // overwrite existing ID3v1 tag
  36. } else {
  37. fseek($fp_source, 0, SEEK_END); // append new ID3v1 tag
  38. }
  39. $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'] : '')));
  40. $new_id3v1_tag_data = getid3_id3v1::GenerateID3v1Tag(
  41. @$this->tag_data['title'],
  42. @$this->tag_data['artist'],
  43. @$this->tag_data['album'],
  44. @$this->tag_data['year'],
  45. @$this->tag_data['genreid'],
  46. @$this->tag_data['comment'],
  47. @$this->tag_data['track']);
  48. fwrite($fp_source, $new_id3v1_tag_data, 128);
  49. fclose($fp_source);
  50. return true;
  51. } else {
  52. $this->errors[] = 'Could not open '.$this->filename.' mode "r+b"';
  53. return false;
  54. }
  55. }
  56. $this->errors[] = 'File is not writeable: '.$this->filename;
  57. return false;
  58. }
  59. function FixID3v1Padding() {
  60. // ID3v1 data is supposed to be padded with NULL characters, but some taggers incorrectly use spaces
  61. // This function rewrites the ID3v1 tag with correct padding
  62. // Initialize getID3 engine
  63. $getID3 = new getID3;
  64. $ThisFileInfo = $getID3->analyze($this->filename);
  65. if ($ThisFileInfo['filesize'] >= (pow(2, 31) - 128)) {
  66. // cannot write tags on files > 2GB
  67. return false;
  68. }
  69. if (isset($ThisFileInfo['tags']['id3v1'])) {
  70. foreach ($ThisFileInfo['tags']['id3v1'] as $key => $value) {
  71. $id3v1data[$key] = implode(',', $value);
  72. }
  73. $this->tag_data = $id3v1data;
  74. return $this->WriteID3v1();
  75. }
  76. return false;
  77. }
  78. function RemoveID3v1() {
  79. if ($ThisFileInfo['filesize'] >= pow(2, 31)) {
  80. $this->errors[] = 'Unable to write ID3v1 because file is larger than 2GB';
  81. return false;
  82. }
  83. // File MUST be writeable - CHMOD(646) at least
  84. if (is_writeable($this->filename)) {
  85. if ($fp_source = @fopen($this->filename, 'r+b')) {
  86. fseek($fp_source, -128, SEEK_END);
  87. if (fread($fp_source, 3) == 'TAG') {
  88. ftruncate($fp_source, filesize($this->filename) - 128);
  89. } else {
  90. // no ID3v1 tag to begin with - do nothing
  91. }
  92. fclose($fp_source);
  93. return true;
  94. } else {
  95. $this->errors[] = 'Could not open '.$this->filename.' mode "r+b"';
  96. }
  97. } else {
  98. $this->errors[] = $this->filename.' is not writeable';
  99. }
  100. return false;
  101. }
  102. }
  103. ?>