/demo/scalr_newui/app/src/Lib/Media/Audio/write.id3v1.php

https://github.com/kennethjiang/Wolke · PHP · 156 lines · 92 code · 36 blank · 28 comment · 19 complexity · 8417bd04a65250f77308f0bdb8c7de9b MD5 · raw file

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP version 5 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 2002-2006 James Heinrich, Allan Hansen |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2 of the GPL license, |
  8. // | that is bundled with this package in the file license.txt and is |
  9. // | available through the world-wide-web at the following url: |
  10. // | http://www.gnu.org/copyleft/gpl.html |
  11. // +----------------------------------------------------------------------+
  12. // | getID3() - http://getid3.sourceforge.net or http://www.getid3.org |
  13. // +----------------------------------------------------------------------+
  14. // | Authors: James Heinrich <infoØgetid3*org> |
  15. // | Allan Hansen <ahØartemis*dk> |
  16. // +----------------------------------------------------------------------+
  17. // | write.id3v1.php |
  18. // | writing module for id3v1 tags |
  19. // | dependencies: module.tag.id3v1.php. |
  20. // +----------------------------------------------------------------------+
  21. //
  22. // $Id: write.id3v1.php,v 1.15 2006/11/20 16:09:33 ah Exp $
  23. class getid3_write_id3v1 extends getid3_handler_write
  24. {
  25. public $title;
  26. public $artist;
  27. public $album;
  28. public $year;
  29. public $genre_id;
  30. public $genre;
  31. public $comment;
  32. public $track;
  33. public function read() {
  34. $engine = new getid3;
  35. $engine->filename = $this->filename;
  36. $engine->fp = fopen($this->filename, 'rb');
  37. $engine->include_module('tag.id3v1');
  38. $tag = new getid3_id3v1($engine);
  39. $tag->Analyze();
  40. if (!isset($engine->info['id3v1'])) {
  41. return;
  42. }
  43. $this->title = $engine->info['id3v1']['title'];
  44. $this->artist = $engine->info['id3v1']['artist'];
  45. $this->album = $engine->info['id3v1']['album'];
  46. $this->year = $engine->info['id3v1']['year'];
  47. $this->genre_id = $engine->info['id3v1']['genre_id'];
  48. $this->genre = $engine->info['id3v1']['genre'];
  49. $this->comment = $engine->info['id3v1']['comment'];
  50. $this->track = $engine->info['id3v1']['track'];
  51. return true;
  52. }
  53. public function write() {
  54. if (!$fp = @fopen($this->filename, 'r+b')) {
  55. throw new getid3_exception('Could not open r+b: ' . $this->filename);
  56. }
  57. // seek to end minus 128 bytes
  58. fseek($fp, -128, SEEK_END);
  59. // overwrite existing ID3v1 tag
  60. if (fread($fp, 3) == 'TAG') {
  61. fseek($fp, -128, SEEK_END);
  62. }
  63. // append new ID3v1 tag
  64. else {
  65. fseek($fp, 0, SEEK_END);
  66. }
  67. fwrite($fp, $this->generate_tag(), 128);
  68. fclose($fp);
  69. clearstatcache();
  70. return true;
  71. }
  72. protected function generate_tag() {
  73. $result = 'TAG';
  74. $result .= str_pad(trim(substr($this->title, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
  75. $result .= str_pad(trim(substr($this->artist, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
  76. $result .= str_pad(trim(substr($this->album, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
  77. $result .= str_pad(trim(substr($this->year, 0, 4)), 4, "\x00", STR_PAD_LEFT);
  78. if (!empty($this->track) && ($this->track > 0) && ($this->track <= 255)) {
  79. $result .= str_pad(trim(substr($this->comment, 0, 28)), 28, "\x00", STR_PAD_RIGHT);
  80. $result .= "\x00";
  81. $result .= chr($this->track);
  82. }
  83. else {
  84. $result .= str_pad(trim(substr($comment, 0, 30)), 30, "\x00", STR_PAD_RIGHT);
  85. }
  86. // both genre and genre_id set
  87. if ($this->genre && $this->genre_id) {
  88. if ($this->genre != getid3_id3v1::LookupGenreName($this->genre_id)) {
  89. throw new getid3_exception('Genre and genre_id does not match. Unset one and the other will be determined automatically.');
  90. }
  91. }
  92. // only genre set
  93. elseif ($this->genre) {
  94. $this->genre_id = getid3_id3v1::LookupGenreID($this->genre);
  95. }
  96. // only genre_id set
  97. else {
  98. if ($this->genre_id < 0 || $this->genre_id > 147) {
  99. $this->genre_id = 255; // 'unknown' genre
  100. }
  101. $this->genre = getid3_id3v1::LookupGenreName($this->genre_id);
  102. }
  103. $result .= chr(intval($this->genre_id));
  104. return $result;
  105. }
  106. public function remove() {
  107. if (!$fp = @fopen($this->filename, 'r+b')) {
  108. throw new getid3_exception('Could not open r+b: ' . $filename);
  109. }
  110. fseek($fp, -128, SEEK_END);
  111. if (fread($fp, 3) == 'TAG') {
  112. ftruncate($fp, filesize($this->filename) - 128);
  113. fclose($fp);
  114. clearstatcache();
  115. }
  116. // success when removing non-existant tag
  117. return true;
  118. }
  119. }
  120. ?>