PageRenderTime 80ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/app/vendors/adapters/ffmpeg-php/php-reader/src/ID3v1.php

https://github.com/mariuz/firetube
PHP | 343 lines | 128 code | 37 blank | 178 comment | 20 complexity | 9e9cac80917fa981d70e4f69d5417228 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * PHP Reader Library
  4. *
  5. * Copyright (c) 2008 The PHP Reader Project Workgroup. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * - Neither the name of the project workgroup nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * @package php-reader
  32. * @subpackage ID3
  33. * @copyright Copyright (c) 2008 The PHP Reader Project Workgroup
  34. * @license http://code.google.com/p/php-reader/wiki/License New BSD License
  35. * @version $Id: ID3v1.php 64 2008-04-01 10:38:12Z svollbehr $
  36. */
  37. /**#@+ @ignore */
  38. require_once("Reader.php");
  39. require_once("ID3/Exception.php");
  40. /**#@-*/
  41. /**
  42. * This class represents a file containing ID3v1 headers as described in
  43. * {@link http://www.id3.org/id3v2-00 The ID3-Tag Specification Appendix}.
  44. *
  45. * @package php-reader
  46. * @subpackage ID3
  47. * @author Sven Vollbehr <svollbehr@gmail.com>
  48. * @copyright Copyright (c) 2008 The PHP Reader Project Workgroup
  49. * @license http://code.google.com/p/php-reader/wiki/License New BSD License
  50. * @version $Rev: 64 $
  51. */
  52. final class ID3v1
  53. {
  54. /** @var string */
  55. private $_title;
  56. /** @var string */
  57. private $_artist;
  58. /** @var string */
  59. private $_album;
  60. /** @var string */
  61. private $_year;
  62. /** @var string */
  63. private $_comment;
  64. /** @var integer */
  65. private $_track;
  66. /** @var integer */
  67. private $_genre = 255;
  68. /**
  69. * The genre list.
  70. *
  71. * @var Array
  72. */
  73. public static $genres = array
  74. ("Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge",
  75. "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other", "Pop", "R&B",
  76. "Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative", "Ska",
  77. "Death Metal", "Pranks", "Soundtrack", "Euro-Techno", "Ambient",
  78. "Trip-Hop", "Vocal", "Jazz+Funk", "Fusion", "Trance", "Classical",
  79. "Instrumental", "Acid", "House", "Game", "Sound Clip", "Gospel", "Noise",
  80. "AlternRock", "Bass", "Soul", "Punk", "Space", "Meditative",
  81. "Instrumental Pop", "Instrumental Rock", "Ethnic", "Gothic", "Darkwave",
  82. "Techno-Industrial", "Electronic", "Pop-Folk", "Eurodance", "Dream",
  83. "Southern Rock", "Comedy", "Cult", "Gangsta", "Top 40", "Christian Rap",
  84. "Pop/Funk", "Jungle", "Native American", "Cabaret", "New Wave",
  85. "Psychadelic", "Rave", "Showtunes", "Trailer", "Lo-Fi", "Tribal",
  86. "Acid Punk", "Acid Jazz", "Polka", "Retro", "Musical", "Rock & Roll",
  87. "Hard Rock", "Folk", "Folk-Rock", "National Folk", "Swing", "Fast Fusion",
  88. "Bebob", "Latin", "Revival", "Celtic", "Bluegrass", "Avantgarde",
  89. "Gothic Rock", "Progressive Rock", "Psychedelic Rock", "Symphonic Rock",
  90. "Slow Rock", "Big Band", "Chorus", "Easy Listening", "Acoustic", "Humour",
  91. "Speech", "Chanson", "Opera", "Chamber Music", "Sonata", "Symphony",
  92. "Booty Bass", "Primus", "Porn Groove", "Satire", "Slow Jam", "Club",
  93. "Tango", "Samba", "Folklore", "Ballad", "Power Ballad", "Rhythmic Soul",
  94. "Freestyle", "Duet", "Punk Rock", "Drum Solo", "A capella", "Euro-House",
  95. "Dance Hall", 255 => "Unknown");
  96. /** @var Reader */
  97. private $_reader;
  98. /** @var string */
  99. private $_filename;
  100. /**
  101. * Constructs the ID3v1 class with given file. The file is not mandatory
  102. * argument and may be omitted. A new tag can be written to a file also by
  103. * giving the filename to the {@link #write} method of this class.
  104. *
  105. * @param string $filename The path to the file.
  106. */
  107. public function __construct($filename = false)
  108. {
  109. if (($this->_filename = $filename) === false ||
  110. file_exists($filename) === false)
  111. return;
  112. $this->_reader = new Reader($filename);
  113. if ($this->_reader->getSize() < 128)
  114. return;
  115. $this->_reader->setOffset(-128);
  116. if ($this->_reader->read(3) != "TAG") {
  117. $this->_reader = false; // reset reader, see write
  118. return;
  119. }
  120. $this->_title = rtrim($this->_reader->readString8(30), " \0");
  121. $this->_artist = rtrim($this->_reader->readString8(30), " \0");
  122. $this->_album = rtrim($this->_reader->readString8(30), " \0");
  123. $this->_year = $this->_reader->readString8(4);
  124. $this->_comment = rtrim($this->_reader->readString8(28), " \0");
  125. /* ID3v1.1 support for tracks */
  126. $v11_null = $this->_reader->read(1);
  127. $v11_track = $this->_reader->read(1);
  128. if (ord($v11_null) == 0 && ord($v11_track) != 0)
  129. $this->_track = ord($v11_track);
  130. else
  131. $this->_comment = rtrim($this->_comment . $v11_null . $v11_track, " \0");
  132. $this->_genre = $this->_reader->readInt8();
  133. }
  134. /**
  135. * Returns the title field.
  136. *
  137. * @return string
  138. */
  139. public function getTitle() { return $this->_title; }
  140. /**
  141. * Sets a new value for the title field. The field cannot exceed 30
  142. * characters in length.
  143. *
  144. * @param string $title The title.
  145. */
  146. public function setTitle($title) { $this->_title = $title; }
  147. /**
  148. * Returns the artist field.
  149. *
  150. * @return string
  151. */
  152. public function getArtist() { return $this->_artist; }
  153. /**
  154. * Sets a new value for the artist field. The field cannot exceed 30
  155. * characters in length.
  156. *
  157. * @param string $artist The artist.
  158. */
  159. public function setArtist($artist) { $this->_artist = $artist; }
  160. /**
  161. * Returns the album field.
  162. *
  163. * @return string
  164. */
  165. public function getAlbum() { return $this->_album; }
  166. /**
  167. * Sets a new value for the album field. The field cannot exceed 30
  168. * characters in length.
  169. *
  170. * @param string $album The album.
  171. */
  172. public function setAlbum($album) { $this->_album = $album; }
  173. /**
  174. * Returns the year field.
  175. *
  176. * @return string
  177. */
  178. public function getYear() { return $this->_year; }
  179. /**
  180. * Sets a new value for the year field. The field cannot exceed 4
  181. * characters in length.
  182. *
  183. * @param string $year The year.
  184. */
  185. public function setYear($year) { $this->_year = $year; }
  186. /**
  187. * Returns the comment field.
  188. *
  189. * @return string
  190. */
  191. public function getComment() { return $this->_comment; }
  192. /**
  193. * Sets a new value for the comment field. The field cannot exceed 30
  194. * characters in length.
  195. *
  196. * @param string $comment The comment.
  197. */
  198. public function setComment($comment) { $this->_comment = $comment; }
  199. /**
  200. * Returns the track field.
  201. *
  202. * @since ID3v1.1
  203. * @return integer
  204. */
  205. public function getTrack() { return $this->_track; }
  206. /**
  207. * Sets a new value for the track field. By setting this field you enforce the
  208. * 1.1 version to be used.
  209. *
  210. * @since ID3v1.1
  211. * @param integer $track The track number.
  212. */
  213. public function setTrack($track) { $this->_track = $track; }
  214. /**
  215. * Returns the genre.
  216. *
  217. * @return string
  218. */
  219. public function getGenre()
  220. {
  221. if (isset(self::$genres[$this->_genre]))
  222. return self::$genres[$this->_genre];
  223. else
  224. return self::$genres[255]; // unknown
  225. }
  226. /**
  227. * Sets a new value for the genre field. The value may either be a numerical
  228. * code representing one of the genres, or its string variant.
  229. *
  230. * The genre is set to unknown (code 255) in case the string is not found from
  231. * the static {@link $genres} array of this class.
  232. *
  233. * @param integer $genre The genre.
  234. */
  235. public function setGenre($genre)
  236. {
  237. if ((is_numeric($genre) && $genre >= 0 && $genre <= 255) ||
  238. ($genre = array_search($genre, self::$genres)) !== false)
  239. $this->_genre = $genre;
  240. else
  241. $this->_genre = 255; // unknown
  242. }
  243. /**
  244. * Writes the possibly altered ID3v1 tag back to the file where it was read.
  245. * If the class was constructed without a file name, one can be provided here
  246. * as an argument. Regardless, the write operation will override previous
  247. * tag information, if found.
  248. *
  249. * @param string $filename The optional path to the file.
  250. */
  251. public function write($filename = false)
  252. {
  253. if ($filename === false && ($filename = $this->_filename) === false)
  254. throw new ID3_Exception("No file given to write the tag to");
  255. if (($fd = fopen
  256. ($filename, file_exists($filename) ? "r+b" : "wb")) === false)
  257. throw new ID3_Exception("Unable to open file for writing: " . $filename);
  258. fseek($fd, $this->_reader !== false ? -128 : 0, SEEK_END);
  259. fwrite($fd, $this, 128);
  260. $this->_filename = $filename;
  261. }
  262. /**
  263. * Magic function so that $obj->value will work.
  264. *
  265. * @param string $name The field name.
  266. * @return mixed
  267. */
  268. public function __get($name)
  269. {
  270. if (method_exists($this, "get" . ucfirst(strtolower($name))))
  271. return call_user_func(array($this, "get" . ucfirst(strtolower($name))));
  272. else throw new ID3_Exception("Unknown field: " . $name);
  273. }
  274. /**
  275. * Magic function so that assignments with $obj->value will work.
  276. *
  277. * @param string $name The field name.
  278. * @param string $value The field value.
  279. * @return mixed
  280. */
  281. public function __set($name, $value)
  282. {
  283. if (method_exists($this, "set" . ucfirst(strtolower($name))))
  284. call_user_func
  285. (array($this, "set" . ucfirst(strtolower($name))), $value);
  286. else throw new ID3_Exception("Unknown field: " . $name);
  287. }
  288. /**
  289. * Returns the tag raw data.
  290. *
  291. * @return string
  292. */
  293. private function __toString()
  294. {
  295. return "TAG" .
  296. Transform::toString8(substr($this->_title, 0, 30), 30) .
  297. Transform::toString8(substr($this->_artist, 0, 30), 30) .
  298. Transform::toString8(substr($this->_album, 0, 30), 30) .
  299. Transform::toString8(substr($this->_year, 0, 4), 4) .
  300. ($this->_track ?
  301. Transform::toString8(substr($this->_comment, 0, 28), 28) .
  302. "\0" . Transform::toInt8($this->_track) :
  303. Transform::toString8(substr($this->_comment, 0, 30), 30)) .
  304. Transform::toInt8($this->_genre);
  305. }
  306. }