/wp-content/plugins/podpress/getid3/write.id3v1.php

https://github.com/AJenbo/ubuntudanmark.dk · PHP · 147 lines · 111 code · 15 blank · 21 comment · 21 complexity · e6aba10662f49cd43613fbc0061c85af MD5 · raw file

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