write.lyrics3.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. // see readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. // //
  10. // write.lyrics3.php //
  11. // module for writing Lyrics3 tags //
  12. // dependencies: module.tag.lyrics3.php //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. class getid3_write_lyrics3
  16. {
  17. /**
  18. * @var string
  19. */
  20. public $filename;
  21. /**
  22. * @var array
  23. */
  24. public $tag_data;
  25. //public $lyrics3_version = 2; // 1 or 2
  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 WriteLyrics3() {
  44. $this->errors[] = 'WriteLyrics3() not yet functional - cannot write Lyrics3';
  45. return false;
  46. }
  47. /**
  48. * @return bool
  49. */
  50. public function DeleteLyrics3() {
  51. // Initialize getID3 engine
  52. $getID3 = new getID3;
  53. $ThisFileInfo = $getID3->analyze($this->filename);
  54. if (isset($ThisFileInfo['lyrics3']['tag_offset_start']) && isset($ThisFileInfo['lyrics3']['tag_offset_end'])) {
  55. if (is_readable($this->filename) && getID3::is_writable($this->filename) && is_file($this->filename) && ($fp = fopen($this->filename, 'a+b'))) {
  56. flock($fp, LOCK_EX);
  57. $oldignoreuserabort = ignore_user_abort(true);
  58. fseek($fp, $ThisFileInfo['lyrics3']['tag_offset_end']);
  59. $DataAfterLyrics3 = '';
  60. if ($ThisFileInfo['filesize'] > $ThisFileInfo['lyrics3']['tag_offset_end']) {
  61. $DataAfterLyrics3 = fread($fp, $ThisFileInfo['filesize'] - $ThisFileInfo['lyrics3']['tag_offset_end']);
  62. }
  63. ftruncate($fp, $ThisFileInfo['lyrics3']['tag_offset_start']);
  64. if (!empty($DataAfterLyrics3)) {
  65. fseek($fp, $ThisFileInfo['lyrics3']['tag_offset_start']);
  66. fwrite($fp, $DataAfterLyrics3, strlen($DataAfterLyrics3));
  67. }
  68. flock($fp, LOCK_UN);
  69. fclose($fp);
  70. ignore_user_abort($oldignoreuserabort);
  71. return true;
  72. } else {
  73. $this->errors[] = 'Cannot fopen('.$this->filename.', "a+b")';
  74. return false;
  75. }
  76. }
  77. // no Lyrics3 present
  78. return true;
  79. }
  80. }