getid3.php 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598
  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. // | getid3.php |
  18. // | Main getID3() file. |
  19. // | dependencies: modules. |
  20. // +----------------------------------------------------------------------+
  21. //
  22. // $Id: getid3.php,v 1.26 2006/12/25 23:44:23 ah Exp $
  23. class getid3
  24. {
  25. //// Settings Section - do NOT modify this file - change setting after newing getid3!
  26. // Encoding
  27. public $encoding = 'ISO-8859-1'; // CASE SENSITIVE! - i.e. (must be supported by iconv() - see http://www.gnu.org/software/libiconv/). Examples: ISO-8859-1 UTF-8 UTF-16 UTF-16BE.
  28. public $encoding_id3v1 = 'ISO-8859-1'; // Override SPECIFICATION encoding for broken ID3v1 tags caused by bad tag programs. Examples: 'EUC-CN' for "Chinese MP3s" and 'CP1251' for "Cyrillic".
  29. public $encoding_id3v2 = 'ISO-8859-1'; // Override ISO-8859-1 encoding for broken ID3v2 tags caused by BRAINDEAD tag programs that writes system codepage as 'ISO-8859-1' instead of UTF-8.
  30. // Tags - disable for speed
  31. public $option_tag_id3v1 = false; // Read and process ID3v1 tags.
  32. public $option_tag_id3v2 = false; // Read and process ID3v2 tags.
  33. public $option_tag_lyrics3 = false; // Read and process Lyrics3 tags.
  34. public $option_tag_apetag = false; // Read and process APE tags.
  35. // Misc calucations - disable for speed
  36. public $option_analyze = true; // Analyze file - disable if you only need to detect file format.
  37. public $option_accurate_results = true; // Disable to greatly speed up parsing of some file formats at the cost of accuracy.
  38. public $option_tags_process = true; // Copy tags to root key 'tags' and 'comments' and encode to $this->encoding.
  39. public $option_tags_images = false; // Scan tags for binary image data - ID3v2 and vorbiscomments only.
  40. public $option_extra_info = true; // Calculate/return additional info such as bitrate, channelmode etc.
  41. public $option_max_2gb_check = false; // Check whether file is larger than 2 Gb and thus not supported by PHP.
  42. // Misc data hashes - slow - require hash module
  43. public $option_md5_data = false; // Get MD5 sum of data part - slow.
  44. public $option_md5_data_source = false; // Use MD5 of source file if available - only FLAC, MAC, OptimFROG and Wavpack4.
  45. public $option_sha1_data = false; // Get SHA1 sum of data part - slow.
  46. // Public variables
  47. public $filename; // Filename of file being analysed.
  48. public $fp; // Filepointer to file being analysed.
  49. public $info; // Result array.
  50. // Protected variables
  51. protected $include_path; // getid3 include path.
  52. protected $warnings = array ();
  53. protected $iconv_present;
  54. // Class constants
  55. const VERSION = '2.0.0b4';
  56. const FREAD_BUFFER_SIZE = 16384; // Read buffer size in bytes.
  57. const ICONV_TEST_STRING = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~�������������������������������� �����������������������������������������������������������������������������������������������';
  58. // Constructor - check PHP enviroment and load library.
  59. public function __construct() {
  60. // Static varibles - no need to recalc every time we new getid3.
  61. static $include_path;
  62. static $iconv_present;
  63. static $initialized;
  64. if ($initialized) {
  65. // Import static variables
  66. $this->include_path = $include_path;
  67. $this->iconv_present = $iconv_present;
  68. // Run init checks only on first instance.
  69. return;
  70. }
  71. // Get include_path
  72. $this->include_path = $include_path = dirname(__FILE__) . '/';
  73. // Check for presence of iconv() and make sure it works (simpel test only).
  74. if (function_exists('iconv') && @iconv('UTF-16LE', 'ISO-8859-1', @iconv('ISO-8859-1', 'UTF-16LE', getid3::ICONV_TEST_STRING)) == getid3::ICONV_TEST_STRING) {
  75. $this->iconv_present = $iconv_present = true;
  76. }
  77. // iconv() not present - load replacement module.
  78. else {
  79. $this->include_module('lib.iconv_replacement');
  80. $this->iconv_present = $iconv_present = false;
  81. }
  82. // Require magic_quotes_runtime off
  83. if (get_magic_quotes_runtime()) {
  84. throw new getid3_exception('magic_quotes_runtime must be disabled before running getID3(). Surround getid3 block by set_magic_quotes_runtime(0) and set_magic_quotes_runtime(1).');
  85. }
  86. // Check memory limit.
  87. $memory_limit = ini_get('memory_limit');
  88. if (eregi('([0-9]+)M', $memory_limit, $matches)) {
  89. // could be stored as "16M" rather than 16777216 for example
  90. $memory_limit = $matches[1] * 1048576;
  91. }
  92. if ($memory_limit <= 0) {
  93. // Should not happen.
  94. } elseif ($memory_limit <= 4194304) {
  95. $this->warning('[SERIOUS] PHP has less than 4 Mb available memory and will very likely run out. Increase memory_limit in php.ini.');
  96. } elseif ($memory_limit <= 12582912) {
  97. $this->warning('PHP has less than 12 Mb available memory and might run out if all modules are loaded. Increase memory_limit in php.ini if needed.');
  98. }
  99. // Check safe_mode off
  100. if ((bool)ini_get('safe_mode')) {
  101. $this->warning('Safe mode is on, shorten support disabled, md5data/sha1data for ogg vorbis disabled, ogg vorbis/flac tag writing disabled.');
  102. }
  103. $initialized = true;
  104. }
  105. // Analyze file by name
  106. public function Analyze($filename) {
  107. // Init and save values
  108. $this->filename = $filename;
  109. $this->warnings = array ();
  110. // Init result array and set parameters
  111. $this->info = array ();
  112. $this->info['GETID3_VERSION'] = getid3::VERSION;
  113. // Remote files not supported
  114. if (preg_match('/^(ht|f)tp:\/\//', $filename)) {
  115. throw new getid3_exception('Remote files are not supported - please copy the file locally first.');
  116. }
  117. // Open local file
  118. if (!$this->fp = @fopen($filename, 'rb')) {
  119. throw new getid3_exception('Could not open file "'.$filename.'"');
  120. }
  121. // Set filesize related parameters
  122. $this->info['filesize'] = filesize($filename);
  123. $this->info['avdataoffset'] = 0;
  124. $this->info['avdataend'] = $this->info['filesize'];
  125. // Option_max_2gb_check
  126. if ($this->option_max_2gb_check) {
  127. // PHP doesn't support integers larger than 31-bit (~2GB)
  128. // filesize() simply returns (filesize % (pow(2, 32)), no matter the actual filesize
  129. // ftell() returns 0 if seeking to the end is beyond the range of unsigned integer
  130. fseek($this->fp, 0, SEEK_END);
  131. if ((($this->info['filesize'] != 0) && (ftell($this->fp) == 0)) ||
  132. ($this->info['filesize'] < 0) ||
  133. (ftell($this->fp) < 0)) {
  134. unset($this->info['filesize']);
  135. fclose($this->fp);
  136. throw new getid3_exception('File is most likely larger than 2GB and is not supported by PHP.');
  137. }
  138. }
  139. // ID3v2 detection (NOT parsing) done to make fileformat easier.
  140. if (!$this->option_tag_id3v2) {
  141. fseek($this->fp, 0, SEEK_SET);
  142. $header = fread($this->fp, 10);
  143. if (substr($header, 0, 3) == 'ID3' && strlen($header) == 10) {
  144. $this->info['id3v2']['header'] = true;
  145. $this->info['id3v2']['majorversion'] = ord($header{3});
  146. $this->info['id3v2']['minorversion'] = ord($header{4});
  147. $this->info['avdataoffset'] += getid3_lib::BigEndian2Int(substr($header, 6, 4), 1) + 10; // length of ID3v2 tag in 10-byte header doesn't include 10-byte header length
  148. }
  149. }
  150. // Handle tags
  151. foreach (array ("id3v2", "id3v1", "apetag", "lyrics3") as $tag_name) {
  152. $option_tag = 'option_tag_' . $tag_name;
  153. if ($this->$option_tag) {
  154. $this->include_module('tag.'.$tag_name);
  155. try {
  156. $tag_class = 'getid3_' . $tag_name;
  157. $tag = new $tag_class($this);
  158. $tag->Analyze();
  159. }
  160. catch (getid3_exception $e) {
  161. throw $e;
  162. }
  163. }
  164. }
  165. //// Determine file format by magic bytes in file header.
  166. // Read 32 kb file data
  167. fseek($this->fp, $this->info['avdataoffset'], SEEK_SET);
  168. $filedata = fread($this->fp, 32774);
  169. // Get huge FileFormatArray
  170. $file_format_array = getid3::GetFileFormatArray();
  171. // Identify file format - loop through $format_info and detect with reg expr
  172. foreach ($file_format_array as $name => $info) {
  173. if (preg_match('/'.$info['pattern'].'/s', $filedata)) { // The /s switch on preg_match() forces preg_match() NOT to treat newline (0x0A) characters as special chars but do a binary match
  174. // Format detected but not supported
  175. if (!@$info['module'] || !@$info['group']) {
  176. fclose($this->fp);
  177. $this->info['fileformat'] = $name;
  178. $this->info['mime_type'] = $info['mime_type'];
  179. $this->warning('Format only detected. Parsing not available yet.');
  180. $this->info['warning'] = $this->warnings;
  181. return $this->info;
  182. }
  183. $determined_format = $info; // copy $info deleted by foreach()
  184. continue;
  185. }
  186. }
  187. // Unable to determine file format
  188. if (!@$determined_format) {
  189. // Too many mp3 encoders on the market put gabage in front of mpeg files
  190. // use assume format on these if format detection failed
  191. if (preg_match('/\.mp[123a]$/i', $filename)) {
  192. $determined_format = $file_format_array['mp3'];
  193. }
  194. else {
  195. fclose($this->fp);
  196. throw new getid3_exception('Unable to determine file format');
  197. }
  198. }
  199. // Free memory
  200. unset($file_format_array);
  201. // Check for illegal ID3 tags
  202. if (@$determined_format['fail_id3'] && (@$this->info['id3v1'] || @$this->info['id3v2'])) {
  203. if ($determined_format['fail_id3'] === 'ERROR') {
  204. fclose($this->fp);
  205. throw new getid3_exception('ID3 tags not allowed on this file type.');
  206. }
  207. elseif ($determined_format['fail_id3'] === 'WARNING') {
  208. @$this->info['id3v1'] and $this->warning('ID3v1 tags not allowed on this file type.');
  209. @$this->info['id3v2'] and $this->warning('ID3v2 tags not allowed on this file type.');
  210. }
  211. }
  212. // Check for illegal APE tags
  213. if (@$determined_format['fail_ape'] && @$this->info['tags']['ape']) {
  214. if ($determined_format['fail_ape'] === 'ERROR') {
  215. fclose($this->fp);
  216. throw new getid3_exception('APE tags not allowed on this file type.');
  217. } elseif ($determined_format['fail_ape'] === 'WARNING') {
  218. $this->warning('APE tags not allowed on this file type.');
  219. }
  220. }
  221. // Set mime type
  222. $this->info['mime_type'] = $determined_format['mime_type'];
  223. // Calc module file name
  224. $determined_format['include'] = 'module.'.$determined_format['group'].'.'.$determined_format['module'].'.php';
  225. // Supported format signature pattern detected, but module deleted.
  226. if (!file_exists($this->include_path.$determined_format['include'])) {
  227. fclose($this->fp);
  228. throw new getid3_exception('Format not supported, module, '.$determined_format['include'].', was removed.');
  229. }
  230. // Include module
  231. $this->include_module($determined_format['group'].'.'.$determined_format['module']);
  232. // Instantiate module class and analyze
  233. $class_name = 'getid3_'.$determined_format['module'];
  234. if (!class_exists($class_name)) {
  235. throw new getid3_exception('Format not supported, module, '.$determined_format['include'].', is corrupt.');
  236. }
  237. $class = new $class_name($this);
  238. try {
  239. $this->option_analyze and $class->Analyze();
  240. }
  241. catch (getid3_exception $e) {
  242. throw $e;
  243. }
  244. catch (Exception $e) {
  245. throw new getid3_exception('Corrupt file.');
  246. }
  247. // Close file
  248. fclose($this->fp);
  249. // Optional - Process all tags - copy to 'tags' and convert charsets
  250. if ($this->option_tags_process) {
  251. $this->HandleAllTags();
  252. }
  253. //// Optional - perform more calculations
  254. if ($this->option_extra_info) {
  255. // Set channelmode on audio
  256. if (@$this->info['audio']['channels'] == '1') {
  257. $this->info['audio']['channelmode'] = 'mono';
  258. } elseif (@$this->info['audio']['channels'] == '2') {
  259. $this->info['audio']['channelmode'] = 'stereo';
  260. }
  261. // Calculate combined bitrate - audio + video
  262. $combined_bitrate = 0;
  263. $combined_bitrate += (isset($this->info['audio']['bitrate']) ? $this->info['audio']['bitrate'] : 0);
  264. $combined_bitrate += (isset($this->info['video']['bitrate']) ? $this->info['video']['bitrate'] : 0);
  265. if (($combined_bitrate > 0) && empty($this->info['bitrate'])) {
  266. $this->info['bitrate'] = $combined_bitrate;
  267. }
  268. if (!isset($this->info['playtime_seconds']) && !empty($this->info['bitrate'])) {
  269. $this->info['playtime_seconds'] = (($this->info['avdataend'] - $this->info['avdataoffset']) * 8) / $this->info['bitrate'];
  270. }
  271. // Set playtime string
  272. if (!empty($this->info['playtime_seconds']) && empty($this->info['playtime_string'])) {
  273. $this->info['playtime_string'] = floor(round($this->info['playtime_seconds']) / 60) . ':' . str_pad(floor(round($this->info['playtime_seconds']) % 60), 2, 0, STR_PAD_LEFT);;
  274. }
  275. // CalculateCompressionRatioVideo() {
  276. if (@$this->info['video'] && @$this->info['video']['resolution_x'] && @$this->info['video']['resolution_y'] && @$this->info['video']['bits_per_sample']) {
  277. // From static image formats
  278. if (in_array($this->info['video']['dataformat'], array ('bmp', 'gif', 'jpeg', 'jpg', 'png', 'tiff'))) {
  279. $frame_rate = 1;
  280. $bitrate_compressed = $this->info['filesize'] * 8;
  281. }
  282. // From video formats
  283. else {
  284. $frame_rate = @$this->info['video']['frame_rate'];
  285. $bitrate_compressed = @$this->info['video']['bitrate'];
  286. }
  287. if ($frame_rate && $bitrate_compressed) {
  288. $this->info['video']['compression_ratio'] = $bitrate_compressed / ($this->info['video']['resolution_x'] * $this->info['video']['resolution_y'] * $this->info['video']['bits_per_sample'] * $frame_rate);
  289. }
  290. }
  291. // CalculateCompressionRatioAudio() {
  292. if (@$this->info['audio']['bitrate'] && @$this->info['audio']['channels'] && @$this->info['audio']['sample_rate']) {
  293. $this->info['audio']['compression_ratio'] = $this->info['audio']['bitrate'] / ($this->info['audio']['channels'] * $this->info['audio']['sample_rate'] * (@$this->info['audio']['bits_per_sample'] ? $this->info['audio']['bits_per_sample'] : 16));
  294. }
  295. if (@$this->info['audio']['streams']) {
  296. foreach ($this->info['audio']['streams'] as $stream_number => $stream_data) {
  297. if (@$stream_data['bitrate'] && @$stream_data['channels'] && @$stream_data['sample_rate']) {
  298. $this->info['audio']['streams'][$stream_number]['compression_ratio'] = $stream_data['bitrate'] / ($stream_data['channels'] * $stream_data['sample_rate'] * (@$stream_data['bits_per_sample'] ? $stream_data['bits_per_sample'] : 16));
  299. }
  300. }
  301. }
  302. // CalculateReplayGain() {
  303. if (@$this->info['replay_gain']) {
  304. if (!@$this->info['replay_gain']['reference_volume']) {
  305. $this->info['replay_gain']['reference_volume'] = 89;
  306. }
  307. if (isset($this->info['replay_gain']['track']['adjustment'])) {
  308. $this->info['replay_gain']['track']['volume'] = $this->info['replay_gain']['reference_volume'] - $this->info['replay_gain']['track']['adjustment'];
  309. }
  310. if (isset($this->info['replay_gain']['album']['adjustment'])) {
  311. $this->info['replay_gain']['album']['volume'] = $this->info['replay_gain']['reference_volume'] - $this->info['replay_gain']['album']['adjustment'];
  312. }
  313. if (isset($this->info['replay_gain']['track']['peak'])) {
  314. $this->info['replay_gain']['track']['max_noclip_gain'] = 0 - 20 * log10($this->info['replay_gain']['track']['peak']);
  315. }
  316. if (isset($this->info['replay_gain']['album']['peak'])) {
  317. $this->info['replay_gain']['album']['max_noclip_gain'] = 0 - 20 * log10($this->info['replay_gain']['album']['peak']);
  318. }
  319. }
  320. // ProcessAudioStreams() {
  321. if (@!$this->info['audio']['streams'] && (@$this->info['audio']['bitrate'] || @$this->info['audio']['channels'] || @$this->info['audio']['sample_rate'])) {
  322. foreach ($this->info['audio'] as $key => $value) {
  323. if ($key != 'streams') {
  324. $this->info['audio']['streams'][0][$key] = $value;
  325. }
  326. }
  327. }
  328. }
  329. // Get the md5/sha1sum of the audio/video portion of the file - without ID3/APE/Lyrics3/etc header/footer tags.
  330. if ($this->option_md5_data || $this->option_sha1_data) {
  331. // Load data-hash library if needed
  332. $this->include_module('lib.data_hash');
  333. if ($this->option_sha1_data) {
  334. new getid3_lib_data_hash($this, 'sha1');
  335. }
  336. if ($this->option_md5_data) {
  337. // no md5_data_source or option disabled -- md5_data_source supported by FLAC, MAC, OptimFROG, Wavpack4
  338. if (!$this->option_md5_data_source || !@$this->info['md5_data_source']) {
  339. new getid3_lib_data_hash($this, 'md5');
  340. }
  341. // copy md5_data_source to md5_data if option set to true
  342. elseif ($this->option_md5_data_source && @$this->info['md5_data_source']) {
  343. $this->info['md5_data'] = $this->info['md5_data_source'];
  344. }
  345. }
  346. }
  347. // Set warnings
  348. if ($this->warnings) {
  349. $this->info['warning'] = $this->warnings;
  350. }
  351. // Return result
  352. return $this->info;
  353. }
  354. // Return array of warnings
  355. public function warnings() {
  356. return $this->warnings;
  357. }
  358. // Add warning(s) to $this->warnings[]
  359. public function warning($message) {
  360. if (is_array($message)) {
  361. $this->warnings = array_merge($this->warnings, $message);
  362. }
  363. else {
  364. $this->warnings[] = $message;
  365. }
  366. }
  367. // Clear all warnings when cloning
  368. public function __clone() {
  369. $this->warnings = array ();
  370. // Copy info array, otherwise it will be a reference.
  371. $temp = $this->info;
  372. unset($this->info);
  373. $this->info = $temp;
  374. }
  375. // Convert string between charsets -- iconv() wrapper
  376. public function iconv($in_charset, $out_charset, $string, $drop01 = false) {
  377. if ($drop01 && ($string === "\x00" || $string === "\x01")) {
  378. return '';
  379. }
  380. if (!$this->iconv_present) {
  381. return getid3_iconv_replacement::iconv($in_charset, $out_charset, $string);
  382. }
  383. // iconv() present
  384. if ($result = @iconv($in_charset, $out_charset.'//TRANSLIT', $string)) {
  385. if ($out_charset == 'ISO-8859-1') {
  386. return rtrim($result, "\x00");
  387. }
  388. return $result;
  389. }
  390. $this->warning('iconv() was unable to convert the string: "' . $string . '" from ' . $in_charset . ' to ' . $out_charset);
  391. return $string;
  392. }
  393. public function include_module($name) {
  394. if (!file_exists($this->include_path.'module.'.$name.'.php')) {
  395. throw new getid3_exception('Required module.'.$name.'.php is missing.');
  396. }
  397. include_once($this->include_path.'module.'.$name.'.php');
  398. }
  399. public function include_module_optional($name) {
  400. if (!file_exists($this->include_path.'module.'.$name.'.php')) {
  401. return;
  402. }
  403. include_once($this->include_path.'module.'.$name.'.php');
  404. return true;
  405. }
  406. // Return array containing information about all supported formats
  407. public static function GetFileFormatArray() {
  408. static $format_info = array (
  409. // Audio formats
  410. // AC-3 - audio - Dolby AC-3 / Dolby Digital
  411. 'ac3' => array (
  412. 'pattern' => '^\x0B\x77',
  413. 'group' => 'audio',
  414. 'module' => 'ac3',
  415. 'mime_type' => 'audio/ac3',
  416. ),
  417. // AAC - audio - Advanced Audio Coding (AAC) - ADIF format
  418. 'adif' => array (
  419. 'pattern' => '^ADIF',
  420. 'group' => 'audio',
  421. 'module' => 'aac_adif',
  422. 'mime_type' => 'application/octet-stream',
  423. 'fail_ape' => 'WARNING',
  424. ),
  425. // AAC - audio - Advanced Audio Coding (AAC) - ADTS format (very similar to MP3)
  426. 'adts' => array (
  427. 'pattern' => '^\xFF[\xF0-\xF1\xF8-\xF9]',
  428. 'group' => 'audio',
  429. 'module' => 'aac_adts',
  430. 'mime_type' => 'application/octet-stream',
  431. 'fail_ape' => 'WARNING',
  432. ),
  433. // AU - audio - NeXT/Sun AUdio (AU)
  434. 'au' => array (
  435. 'pattern' => '^\.snd',
  436. 'group' => 'audio',
  437. 'module' => 'au',
  438. 'mime_type' => 'audio/basic',
  439. ),
  440. // AVR - audio - Audio Visual Research
  441. 'avr' => array (
  442. 'pattern' => '^2BIT',
  443. 'group' => 'audio',
  444. 'module' => 'avr',
  445. 'mime_type' => 'application/octet-stream',
  446. ),
  447. // BONK - audio - Bonk v0.9+
  448. 'bonk' => array (
  449. 'pattern' => '^\x00(BONK|INFO|META| ID3)',
  450. 'group' => 'audio',
  451. 'module' => 'bonk',
  452. 'mime_type' => 'audio/xmms-bonk',
  453. ),
  454. // DTS - audio - Dolby Theatre System
  455. 'dts' => array(
  456. 'pattern' => '^\x7F\xFE\x80\x01',
  457. 'group' => 'audio',
  458. 'module' => 'dts',
  459. 'mime_type' => 'audio/dts',
  460. ),
  461. // FLAC - audio - Free Lossless Audio Codec
  462. 'flac' => array (
  463. 'pattern' => '^fLaC',
  464. 'group' => 'audio',
  465. 'module' => 'xiph',
  466. 'mime_type' => 'audio/x-flac',
  467. ),
  468. // LA - audio - Lossless Audio (LA)
  469. 'la' => array (
  470. 'pattern' => '^LA0[2-4]',
  471. 'group' => 'audio',
  472. 'module' => 'la',
  473. 'mime_type' => 'application/octet-stream',
  474. ),
  475. // LPAC - audio - Lossless Predictive Audio Compression (LPAC)
  476. 'lpac' => array (
  477. 'pattern' => '^LPAC',
  478. 'group' => 'audio',
  479. 'module' => 'lpac',
  480. 'mime_type' => 'application/octet-stream',
  481. ),
  482. // MIDI - audio - MIDI (Musical Instrument Digital Interface)
  483. 'midi' => array (
  484. 'pattern' => '^MThd',
  485. 'group' => 'audio',
  486. 'module' => 'midi',
  487. 'mime_type' => 'audio/midi',
  488. ),
  489. // MAC - audio - Monkey's Audio Compressor
  490. 'mac' => array (
  491. 'pattern' => '^MAC ',
  492. 'group' => 'audio',
  493. 'module' => 'monkey',
  494. 'mime_type' => 'application/octet-stream',
  495. ),
  496. // MOD - audio - MODule (assorted sub-formats)
  497. 'mod' => array (
  498. 'pattern' => '^.{1080}(M.K.|[5-9]CHN|[1-3][0-9]CH)',
  499. 'mime_type' => 'audio/mod',
  500. ),
  501. // MOD - audio - MODule (Impulse Tracker)
  502. 'it' => array (
  503. 'pattern' => '^IMPM',
  504. 'mime_type' => 'audio/it',
  505. ),
  506. // MOD - audio - MODule (eXtended Module, various sub-formats)
  507. 'xm' => array (
  508. 'pattern' => '^Extended Module',
  509. 'mime_type' => 'audio/xm',
  510. ),
  511. // MOD - audio - MODule (ScreamTracker)
  512. 's3m' => array (
  513. 'pattern' => '^.{44}SCRM',
  514. 'mime_type' => 'audio/s3m',
  515. ),
  516. // MPC - audio - Musepack / MPEGplus SV7+
  517. 'mpc' => array (
  518. 'pattern' => '^(MP\+)',
  519. 'group' => 'audio',
  520. 'module' => 'mpc',
  521. 'mime_type' => 'audio/x-musepack',
  522. ),
  523. // MPC - audio - Musepack / MPEGplus SV4-6
  524. 'mpc_old' => array (
  525. 'pattern' => '^([\x00\x01\x10\x11\x40\x41\x50\x51\x80\x81\x90\x91\xC0\xC1\xD0\xD1][\x20-37][\x00\x20\x40\x60\x80\xA0\xC0\xE0])',
  526. 'group' => 'audio',
  527. 'module' => 'mpc_old',
  528. 'mime_type' => 'application/octet-stream',
  529. ),
  530. // MP3 - audio - MPEG-audio Layer 3 (very similar to AAC-ADTS)
  531. 'mp3' => array (
  532. 'pattern' => '^\xFF[\xE2-\xE7\xF2-\xF7\xFA-\xFF][\x00-\xEB]',
  533. 'group' => 'audio',
  534. 'module' => 'mp3',
  535. 'mime_type' => 'audio/mpeg',
  536. ),
  537. // OFR - audio - OptimFROG
  538. 'ofr' => array (
  539. 'pattern' => '^(\*RIFF|OFR)',
  540. 'group' => 'audio',
  541. 'module' => 'optimfrog',
  542. 'mime_type' => 'application/octet-stream',
  543. ),
  544. // RKAU - audio - RKive AUdio compressor
  545. 'rkau' => array (
  546. 'pattern' => '^RKA',
  547. 'group' => 'audio',
  548. 'module' => 'rkau',
  549. 'mime_type' => 'application/octet-stream',
  550. ),
  551. // SHN - audio - Shorten
  552. 'shn' => array (
  553. 'pattern' => '^ajkg',
  554. 'group' => 'audio',
  555. 'module' => 'shorten',
  556. 'mime_type' => 'audio/xmms-shn',
  557. 'fail_id3' => 'ERROR',
  558. 'fail_ape' => 'ERROR',
  559. ),
  560. // TTA - audio - TTA Lossless Audio Compressor (http://tta.corecodec.org)
  561. 'tta' => array (
  562. 'pattern' => '^TTA', // could also be '^TTA(\x01|\x02|\x03|2|1)'
  563. 'group' => 'audio',
  564. 'module' => 'tta',
  565. 'mime_type' => 'application/octet-stream',
  566. ),
  567. // VOC - audio - Creative Voice (VOC)
  568. 'voc' => array (
  569. 'pattern' => '^Creative Voice File',
  570. 'group' => 'audio',
  571. 'module' => 'voc',
  572. 'mime_type' => 'audio/voc',
  573. ),
  574. // VQF - audio - transform-domain weighted interleave Vector Quantization Format (VQF)
  575. 'vqf' => array (
  576. 'pattern' => '^TWIN',
  577. 'group' => 'audio',
  578. 'module' => 'vqf',
  579. 'mime_type' => 'application/octet-stream',
  580. ),
  581. // WV - audio - WavPack (v4.0+)
  582. 'vw' => array(
  583. 'pattern' => '^wvpk',
  584. 'group' => 'audio',
  585. 'module' => 'wavpack',
  586. 'mime_type' => 'application/octet-stream',
  587. ),
  588. // Audio-Video formats
  589. // ASF - audio/video - Advanced Streaming Format, Windows Media Video, Windows Media Audio
  590. 'asf' => array (
  591. 'pattern' => '^\x30\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C',
  592. 'group' => 'audio-video',
  593. 'module' => 'asf',
  594. 'mime_type' => 'video/x-ms-asf',
  595. ),
  596. // BINK - audio/video - Bink / Smacker
  597. 'bink' => array(
  598. 'pattern' => '^(BIK|SMK)',
  599. 'mime_type' => 'application/octet-stream',
  600. ),
  601. // FLV - audio/video - FLash Video
  602. 'flv' => array(
  603. 'pattern' => '^FLV\x01',
  604. 'group' => 'audio-video',
  605. 'module' => 'flv',
  606. 'mime_type' => 'video/x-flv',
  607. ),
  608. // MKAV - audio/video - Mastroka
  609. 'matroska' => array (
  610. 'pattern' => '^\x1A\x45\xDF\xA3',
  611. 'mime_type' => 'application/octet-stream',
  612. ),
  613. // MPEG - audio/video - MPEG (Moving Pictures Experts Group)
  614. 'mpeg' => array (
  615. 'pattern' => '^\x00\x00\x01(\xBA|\xB3)',
  616. 'group' => 'audio-video',
  617. 'module' => 'mpeg',
  618. 'mime_type' => 'video/mpeg',
  619. ),
  620. // NSV - audio/video - Nullsoft Streaming Video (NSV)
  621. 'nsv' => array (
  622. 'pattern' => '^NSV[sf]',
  623. 'group' => 'audio-video',
  624. 'module' => 'nsv',
  625. 'mime_type' => 'application/octet-stream',
  626. ),
  627. // Ogg - audio/video - Ogg (Ogg Vorbis, OggFLAC, Speex, Ogg Theora(*), Ogg Tarkin(*))
  628. 'ogg' => array (
  629. 'pattern' => '^OggS',
  630. 'group' => 'audio',
  631. 'module' => 'xiph',
  632. 'mime_type' => 'application/ogg',
  633. 'fail_id3' => 'WARNING',
  634. 'fail_ape' => 'WARNING',
  635. ),
  636. // QT - audio/video - Quicktime
  637. 'quicktime' => array (
  638. 'pattern' => '^.{4}(cmov|free|ftyp|mdat|moov|pnot|skip|wide)',
  639. 'group' => 'audio-video',
  640. 'module' => 'quicktime',
  641. 'mime_type' => 'video/quicktime',
  642. ),
  643. // RIFF - audio/video - Resource Interchange File Format (RIFF) / WAV / AVI / CD-audio / SDSS = renamed variant used by SmartSound QuickTracks (www.smartsound.com) / FORM = Audio Interchange File Format (AIFF)
  644. 'riff' => array (
  645. 'pattern' => '^(RIFF|SDSS|FORM)',
  646. 'group' => 'audio-video',
  647. 'module' => 'riff',
  648. 'mime_type' => 'audio/x-wave',
  649. 'fail_ape' => 'WARNING',
  650. ),
  651. // Real - audio/video - RealAudio, RealVideo
  652. 'real' => array (
  653. 'pattern' => '^(\.RMF|.ra)',
  654. 'group' => 'audio-video',
  655. 'module' => 'real',
  656. 'mime_type' => 'audio/x-realaudio',
  657. ),
  658. // SWF - audio/video - ShockWave Flash
  659. 'swf' => array (
  660. 'pattern' => '^(F|C)WS',
  661. 'group' => 'audio-video',
  662. 'module' => 'swf',
  663. 'mime_type' => 'application/x-shockwave-flash',
  664. ),
  665. // Still-Image formats
  666. // BMP - still image - Bitmap (Windows, OS/2; uncompressed, RLE8, RLE4)
  667. 'bmp' => array (
  668. 'pattern' => '^BM',
  669. 'group' => 'graphic',
  670. 'module' => 'bmp',
  671. 'mime_type' => 'image/bmp',
  672. 'fail_id3' => 'ERROR',
  673. 'fail_ape' => 'ERROR',
  674. ),
  675. // GIF - still image - Graphics Interchange Format
  676. 'gif' => array (
  677. 'pattern' => '^GIF',
  678. 'group' => 'graphic',
  679. 'module' => 'gif',
  680. 'mime_type' => 'image/gif',
  681. 'fail_id3' => 'ERROR',
  682. 'fail_ape' => 'ERROR',
  683. ),
  684. // JPEG - still image - Joint Photographic Experts Group (JPEG)
  685. 'jpeg' => array (
  686. 'pattern' => '^\xFF\xD8\xFF',
  687. 'group' => 'graphic',
  688. 'module' => 'jpeg',
  689. 'mime_type' => 'image/jpeg',
  690. 'fail_id3' => 'ERROR',
  691. 'fail_ape' => 'ERROR',
  692. ),
  693. // PCD - still image - Kodak Photo CD
  694. 'pcd' => array (
  695. 'pattern' => '^.{2048}PCD_IPI\x00',
  696. 'group' => 'graphic',
  697. 'module' => 'pcd',
  698. 'mime_type' => 'image/x-photo-cd',
  699. 'fail_id3' => 'ERROR',
  700. 'fail_ape' => 'ERROR',
  701. ),
  702. // PNG - still image - Portable Network Graphics (PNG)
  703. 'png' => array (
  704. 'pattern' => '^\x89\x50\x4E\x47\x0D\x0A\x1A\x0A',
  705. 'group' => 'graphic',
  706. 'module' => 'png',
  707. 'mime_type' => 'image/png',
  708. 'fail_id3' => 'ERROR',
  709. 'fail_ape' => 'ERROR',
  710. ),
  711. // SVG - still image - Scalable Vector Graphics (SVG)
  712. 'svg' => array(
  713. 'pattern' => '<!DOCTYPE svg PUBLIC ',
  714. 'mime_type' => 'image/svg+xml',
  715. 'fail_id3' => 'ERROR',
  716. 'fail_ape' => 'ERROR',
  717. ),
  718. // TIFF - still image - Tagged Information File Format (TIFF)
  719. 'tiff' => array (
  720. 'pattern' => '^(II\x2A\x00|MM\x00\x2A)',
  721. 'group' => 'graphic',
  722. 'module' => 'tiff',
  723. 'mime_type' => 'image/tiff',
  724. 'fail_id3' => 'ERROR',
  725. 'fail_ape' => 'ERROR',
  726. ),
  727. // Data formats
  728. 'exe' => array(
  729. 'pattern' => '^MZ',
  730. 'mime_type' => 'application/octet-stream',
  731. 'fail_id3' => 'ERROR',
  732. 'fail_ape' => 'ERROR',
  733. ),
  734. // ISO - data - International Standards Organization (ISO) CD-ROM Image
  735. 'iso' => array (
  736. 'pattern' => '^.{32769}CD001',
  737. 'group' => 'misc',
  738. 'module' => 'iso',
  739. 'mime_type' => 'application/octet-stream',
  740. 'fail_id3' => 'ERROR',
  741. 'fail_ape' => 'ERROR',
  742. ),
  743. // RAR - data - RAR compressed data
  744. 'rar' => array(
  745. 'pattern' => '^Rar\!',
  746. 'mime_type' => 'application/octet-stream',
  747. 'fail_id3' => 'ERROR',
  748. 'fail_ape' => 'ERROR',
  749. ),
  750. // SZIP - audio - SZIP compressed data
  751. 'szip' => array (
  752. 'pattern' => '^SZ\x0A\x04',
  753. 'group' => 'archive',
  754. 'module' => 'szip',
  755. 'mime_type' => 'application/octet-stream',
  756. 'fail_id3' => 'ERROR',
  757. 'fail_ape' => 'ERROR',
  758. ),
  759. // TAR - data - TAR compressed data
  760. 'tar' => array(
  761. 'pattern' => '^.{100}[0-9\x20]{7}\x00[0-9\x20]{7}\x00[0-9\x20]{7}\x00[0-9\x20\x00]{12}[0-9\x20\x00]{12}',
  762. 'group' => 'archive',
  763. 'module' => 'tar',
  764. 'mime_type' => 'application/x-tar',
  765. 'fail_id3' => 'ERROR',
  766. 'fail_ape' => 'ERROR',
  767. ),
  768. // GZIP - data - GZIP compressed data
  769. 'gz' => array(
  770. 'pattern' => '^\x1F\x8B\x08',
  771. 'group' => 'archive',
  772. 'module' => 'gzip',
  773. 'mime_type' => 'application/x-gzip',
  774. 'fail_id3' => 'ERROR',
  775. 'fail_ape' => 'ERROR',
  776. ),
  777. // ZIP - data - ZIP compressed data
  778. 'zip' => array (
  779. 'pattern' => '^PK\x03\x04',
  780. 'group' => 'archive',
  781. 'module' => 'zip',
  782. 'mime_type' => 'application/zip',
  783. 'fail_id3' => 'ERROR',
  784. 'fail_ape' => 'ERROR',
  785. ),
  786. // PAR2 - data - Parity Volume Set Specification 2.0
  787. 'par2' => array (
  788. 'pattern' => '^PAR2\x00PKT',
  789. 'mime_type' => 'application/octet-stream',
  790. 'fail_id3' => 'ERROR',
  791. 'fail_ape' => 'ERROR',
  792. ),
  793. // PDF - data - Portable Document Format
  794. 'pdf' => array(
  795. 'pattern' => '^\x25PDF',
  796. 'mime_type' => 'application/pdf',
  797. 'fail_id3' => 'ERROR',
  798. 'fail_ape' => 'ERROR',
  799. ),
  800. // DOC - data - Microsoft Word
  801. 'msoffice' => array(
  802. 'pattern' => '^\xD0\xCF\x11\xE0', // D0CF11E == DOCFILE == Microsoft Office Document
  803. 'mime_type' => 'application/octet-stream',
  804. 'fail_id3' => 'ERROR',
  805. 'fail_ape' => 'ERROR',
  806. ),
  807. );
  808. return $format_info;
  809. }
  810. // Recursive over array - converts array to $encoding charset from $this->encoding
  811. function CharConvert(&$array, $encoding) {
  812. // Identical encoding - end here
  813. if ($encoding == $this->encoding) {
  814. return;
  815. }
  816. // Loop thru array
  817. foreach ($array as $key => $value) {
  818. // Go recursive
  819. if (is_array($value)) {
  820. $this->CharConvert($array[$key], $encoding);
  821. }
  822. // Convert string
  823. elseif (is_string($value)) {
  824. $array[$key] = $this->iconv($encoding, $this->encoding, $value);
  825. }
  826. }
  827. }
  828. // Convert and copy tags
  829. protected function HandleAllTags() {
  830. // Key name => array (tag name, character encoding)
  831. static $tags = array (
  832. 'asf' => array ('asf', 'UTF-16LE'),
  833. 'midi' => array ('midi', 'ISO-8859-1'),
  834. 'nsv' => array ('nsv', 'ISO-8859-1'),
  835. 'ogg' => array ('vorbiscomment', 'UTF-8'),
  836. 'png' => array ('png', 'UTF-8'),
  837. 'tiff' => array ('tiff', 'ISO-8859-1'),
  838. 'quicktime' => array ('quicktime', 'ISO-8859-1'),
  839. 'real' => array ('real', 'ISO-8859-1'),
  840. 'vqf' => array ('vqf', 'ISO-8859-1'),
  841. 'zip' => array ('zip', 'ISO-8859-1'),
  842. 'riff' => array ('riff', 'ISO-8859-1'),
  843. 'lyrics3' => array ('lyrics3', 'ISO-8859-1'),
  844. 'id3v1' => array ('id3v1', ''), // change below - cannot assign variable to static array
  845. 'id3v2' => array ('id3v2', 'UTF-8'), // module converts all frames to UTF-8
  846. 'ape' => array ('ape', 'UTF-8')
  847. );
  848. $tags['id3v1'][1] = $this->encoding_id3v1;
  849. // Loop thru tags array
  850. foreach ($tags as $comment_name => $tag_name_encoding_array) {
  851. list($tag_name, $encoding) = $tag_name_encoding_array;
  852. // Fill in default encoding type if not already present
  853. @$this->info[$comment_name] and $this->info[$comment_name]['encoding'] = $encoding;
  854. // Copy comments if key name set
  855. if (@$this->info[$comment_name]['comments']) {
  856. foreach ($this->info[$comment_name]['comments'] as $tag_key => $value_array) {
  857. foreach ($value_array as $key => $value) {
  858. if (strlen(trim($value)) > 0) {
  859. $this->info['tags'][$tag_name][trim($tag_key)][] = $value; // do not trim!! Unicode characters will get mangled if trailing nulls are removed!
  860. }
  861. }
  862. }
  863. if (!@$this->info['tags'][$tag_name]) {
  864. // comments are set but contain nothing but empty strings, so skip
  865. continue;
  866. }
  867. $this->CharConvert($this->info['tags'][$tag_name], $encoding);
  868. }
  869. }
  870. // Merge comments from ['tags'] into common ['comments']
  871. if (@$this->info['tags']) {
  872. foreach ($this->info['tags'] as $tag_type => $tag_array) {
  873. foreach ($tag_array as $tag_name => $tagdata) {
  874. foreach ($tagdata as $key => $value) {
  875. if (!empty($value)) {
  876. if (empty($this->info['comments'][$tag_name])) {
  877. // fall through and append value
  878. }
  879. elseif ($tag_type == 'id3v1') {
  880. $new_value_length = strlen(trim($value));
  881. foreach ($this->info['comments'][$tag_name] as $existing_key => $existing_value) {
  882. $old_value_length = strlen(trim($existing_value));
  883. if (($new_value_length <= $old_value_length) && (substr($existing_value, 0, $new_value_length) == trim($value))) {
  884. // new value is identical but shorter-than (or equal-length to) one already in comments - skip
  885. break 2;
  886. }
  887. }
  888. }
  889. else {
  890. $new_value_length = strlen(trim($value));
  891. foreach ($this->info['comments'][$tag_name] as $existing_key => $existing_value) {
  892. $old_value_length = strlen(trim($existing_value));
  893. if (($new_value_length > $old_value_length) && (substr(trim($value), 0, strlen($existing_value)) == $existing_value)) {
  894. $this->info['comments'][$tag_name][$existing_key] = trim($value);
  895. break 2;
  896. }
  897. }
  898. }
  899. if (empty($this->info['comments'][$tag_name]) || !in_array(trim($value), $this->info['comments'][$tag_name])) {
  900. $this->info['comments'][$tag_name][] = trim($value);
  901. }
  902. }
  903. }
  904. }
  905. }
  906. }
  907. return true;
  908. }
  909. }
  910. abstract class getid3_handler
  911. {
  912. protected $getid3; // pointer
  913. protected $data_string_flag = false; // analyzing filepointer or string
  914. protected $data_string; // string to analyze
  915. protected $data_string_position = 0; // seek position in string
  916. public function __construct(getID3 $getid3) {
  917. $this->getid3 = $getid3;
  918. }
  919. // Analyze from file pointer
  920. abstract public function Analyze();
  921. // Analyze from string instead
  922. public function AnalyzeString(&$string) {
  923. // Enter string mode
  924. $this->data_string_flag = true;
  925. $this->data_string = $string;
  926. // Save info
  927. $saved_avdataoffset = $this->getid3->info['avdataoffset'];
  928. $saved_avdataend = $this->getid3->info['avdataend'];
  929. $saved_filesize = $this->getid3->info['filesize'];
  930. // Reset some info
  931. $this->getid3->info['avdataoffset'] = 0;
  932. $this->getid3->info['avdataend'] = $this->getid3->info['filesize'] = strlen($string);
  933. // Analyze
  934. $this->Analyze();
  935. // Restore some info
  936. $this->getid3->info['avdataoffset'] = $saved_avdataoffset;
  937. $this->getid3->info['avdataend'] = $saved_avdataend;
  938. $this->getid3->info['filesize'] = $saved_filesize;
  939. // Exit string mode
  940. $this->data_string_flag = false;
  941. }
  942. protected function ftell() {
  943. if ($this->data_string_flag) {
  944. return $this->data_string_position;
  945. }
  946. return ftell($this->getid3->fp);
  947. }
  948. protected function fread($bytes) {
  949. if ($this->data_string_flag) {
  950. $this->data_string_position += $bytes;
  951. return substr($this->data_string, $this->data_string_position - $bytes, $bytes);
  952. }
  953. return fread($this->getid3->fp, $bytes);
  954. }
  955. protected function fseek($bytes, $whence = SEEK_SET) {
  956. if ($this->data_string_flag) {
  957. switch ($whence) {
  958. case SEEK_SET:
  959. $this->data_string_position = $bytes;
  960. return;
  961. case SEEK_CUR:
  962. $this->data_string_position += $bytes;
  963. return;
  964. case SEEK_END:
  965. $this->data_string_position = strlen($this->data_string) + $bytes;
  966. return;
  967. }
  968. }
  969. return fseek($this->getid3->fp, $bytes, $whence);
  970. }
  971. }
  972. abstract class getid3_handler_write
  973. {
  974. protected $filename;
  975. protected $user_abort;
  976. private $fp_lock;
  977. private $owner;
  978. private $group;
  979. private $perms;
  980. public function __construct($filename) {
  981. if (!file_exists($filename)) {
  982. throw new getid3_exception('File does not exist: "' . $filename . '"');
  983. }
  984. if (!is_writeable($filename)) {
  985. throw new getid3_exception('File is not writeable: "' . $filename . '"');
  986. }
  987. if (!is_writeable(dirname($filename))) {
  988. throw new getid3_exception('Directory is not writeable: ' . dirname($filename) . ' (need to write lock file).');
  989. }
  990. $this->user_abort = ignore_user_abort(true);
  991. $this->fp_lock = fopen($filename . '.getid3.lock', 'w');
  992. flock($this->fp_lock, LOCK_EX);
  993. $this->filename = $filename;
  994. }
  995. public function __destruct() {
  996. flock($this->fp_lock, LOCK_UN);
  997. fclose($this->fp_lock);
  998. unlink($this->filename . '.getid3.lock');
  999. ignore_user_abort($this->user_abort);
  1000. }
  1001. protected function save_permissions() {
  1002. $this->owner = fileowner($this->filename);
  1003. $this->group = filegroup($this->filename);
  1004. $this->perms = fileperms($this->filename);
  1005. }
  1006. protected function restore_permissions() {
  1007. @chown($this->filename, $this->owner);
  1008. @chgrp($this->filename, $this->group);
  1009. @chmod($this->filename, $this->perms);
  1010. }
  1011. abstract public function read();
  1012. abstract public function write();
  1013. abstract public function remove();
  1014. }
  1015. class getid3_exception extends Exception
  1016. {
  1017. public $message;
  1018. }
  1019. class getid3_lib
  1020. {
  1021. // Convert Little Endian byte string to int - max 32 bits
  1022. public static function LittleEndian2Int($byte_word, $signed = false) {
  1023. return getid3_lib::BigEndian2Int(strrev($byte_word), $signed);
  1024. }
  1025. // Convert number to Little Endian byte string
  1026. public static function LittleEndian2String($number, $minbytes=1, $synchsafe=false) {
  1027. $intstring = '';
  1028. while ($number > 0) {
  1029. if ($synchsafe) {
  1030. $intstring = $intstring.chr($number & 127);
  1031. $number >>= 7;
  1032. } else {
  1033. $intstring = $intstring.chr($number & 255);
  1034. $number >>= 8;
  1035. }
  1036. }
  1037. return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
  1038. }
  1039. // Convert Big Endian byte string to int - max 32 bits
  1040. public static function BigEndian2Int($byte_word, $signed = false) {
  1041. $int_value = 0;
  1042. $byte_wordlen = strlen($byte_word);
  1043. for ($i = 0; $i < $byte_wordlen; $i++) {
  1044. $int_value += ord($byte_word{$i}) * pow(256, ($byte_wordlen - 1 - $i));
  1045. }
  1046. if ($signed) {
  1047. $sign_mask_bit = 0x80 << (8 * ($byte_wordlen - 1));
  1048. if ($int_value & $sign_mask_bit) {
  1049. $int_value = 0 - ($int_value & ($sign_mask_bit - 1));
  1050. }
  1051. }
  1052. return $int_value;
  1053. }
  1054. // Convert Big Endian byte sybc safe string to int - max 32 bits
  1055. public static function BigEndianSyncSafe2Int($byte_word) {
  1056. $int_value = 0;
  1057. $byte_wordlen = strlen($byte_word);
  1058. // disregard MSB, effectively 7-bit bytes
  1059. for ($i = 0; $i < $byte_wordlen; $i++) {
  1060. $int_value = $int_value | (ord($byte_word{$i}) & 0x7F) << (($byte_wordlen - 1 - $i) * 7);
  1061. }
  1062. return $int_value;
  1063. }
  1064. // Convert Big Endian byte string to bit string
  1065. public static function BigEndian2Bin($byte_word) {
  1066. $bin_value = '';
  1067. $byte_wordlen = strlen($byte_word);
  1068. for ($i = 0; $i < $byte_wordlen; $i++) {
  1069. $bin_value .= str_pad(decbin(ord($byte_word{$i})), 8, '0', STR_PAD_LEFT);
  1070. }
  1071. return $bin_value;
  1072. }
  1073. public static function BigEndian2Float($byte_word) {
  1074. // ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic
  1075. // http://www.psc.edu/general/software/packages/ieee/ieee.html
  1076. // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html
  1077. $bit_word = getid3_lib::BigEndian2Bin($byte_word);
  1078. if (!$bit_word) {
  1079. return 0;
  1080. }
  1081. $sign_bit = $bit_word{0};
  1082. switch (strlen($byte_word) * 8) {
  1083. case 32:
  1084. $exponent_bits = 8;
  1085. $fraction_bits = 23;
  1086. break;
  1087. case 64:
  1088. $exponent_bits = 11;
  1089. $fraction_bits = 52;
  1090. break;
  1091. case 80:
  1092. // 80-bit Apple SANE format
  1093. // http://www.mactech.com/articles/mactech/Vol.06/06.01/SANENormalized/
  1094. $exponent_string = substr($bit_word, 1, 15);
  1095. $is_normalized = intval($bit_word{16});
  1096. $fraction_string = substr($bit_word, 17, 63);
  1097. $exponent = pow(2, getid3_lib::Bin2Dec($exponent_string) - 16383);
  1098. $fraction = $is_normalized + getid3_lib::DecimalBinary2Float($fraction_string);
  1099. $float_value = $exponent * $fraction;
  1100. if ($sign_bit == '1') {
  1101. $float_value *= -1;
  1102. }
  1103. return $float_value;
  1104. break;
  1105. default:
  1106. return false;
  1107. break;
  1108. }
  1109. $exponent_string = substr($bit_word, 1, $exponent_bits);
  1110. $fraction_string = substr($bit_word, $exponent_bits + 1, $fraction_bits);
  1111. $exponent = bindec($exponent_string);
  1112. $fraction = bindec($fraction_string);
  1113. if (($exponent == (pow(2, $exponent_bits) - 1)) && ($fraction != 0)) {
  1114. // Not a Number
  1115. $float_value = false;
  1116. } elseif (($exponent == (pow(2, $exponent_bits) - 1)) && ($fraction == 0)) {
  1117. if ($sign_bit == '1') {
  1118. $float_value = '-infinity';
  1119. } else {
  1120. $float_value = '+infinity';
  1121. }
  1122. } elseif (($exponent == 0) && ($fraction == 0)) {
  1123. if ($sign_bit == '1') {
  1124. $float_value = -0;
  1125. } else {
  1126. $float_value = 0;
  1127. }
  1128. $float_value = ($sign_bit ? 0 : -0);
  1129. } elseif (($exponent == 0) && ($fraction != 0)) {
  1130. // These are 'unnormalized' values
  1131. $float_value = pow(2, (-1 * (pow(2, $exponent_bits - 1) - 2))) * getid3_lib::DecimalBinary2Float($fraction_string);
  1132. if ($sign_bit == '1') {
  1133. $float_value *= -1;
  1134. }
  1135. } elseif ($exponent != 0) {
  1136. $float_value = pow(2, ($exponent - (pow(2, $exponent_bits - 1) - 1))) * (1 + getid3_lib::DecimalBinary2Float($fraction_string));
  1137. if ($sign_bit == '1') {
  1138. $float_value *= -1;
  1139. }
  1140. }
  1141. return (float) $float_value;
  1142. }
  1143. public static function LittleEndian2Float($byte_word) {
  1144. return getid3_lib::BigEndian2Float(strrev($byte_word));
  1145. }
  1146. public static function DecimalBinary2Float($binary_numerator) {
  1147. $numerator = bindec($binary_numerator);
  1148. $denominator = bindec('1'.str_repeat('0', strlen($binary_numerator)));
  1149. return ($numerator / $denominator);
  1150. }
  1151. public static function PrintHexBytes($string, $hex=true, $spaces=true, $html_safe=true) {
  1152. $return_string = '';
  1153. for ($i = 0; $i < strlen($string); $i++) {
  1154. if ($hex) {
  1155. $return_string .= str_pad(dechex(ord($string{$i})), 2, '0', STR_PAD_LEFT);
  1156. } else {
  1157. $return_string .= ' '.(ereg("[\x20-\x7E]", $string{$i}) ? $string{$i} : '�');
  1158. }
  1159. if ($spaces) {
  1160. $return_string .= ' ';
  1161. }
  1162. }
  1163. if ($html_safe) {
  1164. $return_string = htmlentities($return_string);
  1165. }
  1166. return $return_string;
  1167. }
  1168. // Process header data string - read several values with algorithm and add to target
  1169. // algorithm is one one the getid3_lib::Something2Something() function names
  1170. // parts_array is index => length - $target[index] = algorithm(substring(data))
  1171. // - OR just substring(data) if length is negative!
  1172. // indexes == 'IGNORE**' are ignored
  1173. public static function ReadSequence($algorithm, &$target, &$data, $offset, $parts_array) {
  1174. // Loop thru $parts_array
  1175. foreach ($parts_array as $target_string => $length) {
  1176. // Add to target
  1177. if (!strstr($target_string, 'IGNORE')) {
  1178. // substr(....length)
  1179. if ($length < 0) {
  1180. $target[$target_string] = substr($data, $offset, -$length);
  1181. }
  1182. // algorithm(substr(...length))
  1183. else {
  1184. $target[$target_string] = getid3_lib::$algorithm(substr($data, $offset, $length));
  1185. }
  1186. }
  1187. // Move pointer
  1188. $offset += abs($length);
  1189. }
  1190. }
  1191. }
  1192. class getid3_lib_replaygain
  1193. {
  1194. public static function NameLookup($name_code) {
  1195. static $lookup = array (
  1196. 0 => 'not set',
  1197. 1 => 'Track Gain Adjustment',
  1198. 2 => 'Album Gain Adjustment'
  1199. );
  1200. return @$lookup[$name_code];
  1201. }
  1202. public static function OriginatorLookup($originator_code) {
  1203. static $lookup = array (
  1204. 0 => 'unspecified',
  1205. 1 => 'pre-set by artist/producer/mastering engineer',
  1206. 2 => 'set by user',
  1207. 3 => 'determined automatically'
  1208. );
  1209. return @$lookup[$originator_code];
  1210. }
  1211. public static function AdjustmentLookup($raw_adjustment, $sign_bit) {
  1212. return (float)$raw_adjustment / 10 * ($sign_bit == 1 ? -1 : 1);
  1213. }
  1214. public static function GainString($name_code, $originator_code, $replaygain) {
  1215. $sign_bit = $replaygain < 0 ? 1 : 0;
  1216. $stored_replaygain = intval(round($replaygain * 10));
  1217. $gain_string = str_pad(decbin($name_code), 3, '0', STR_PAD_LEFT);
  1218. $gain_string .= str_pad(decbin($originator_code), 3, '0', STR_PAD_LEFT);
  1219. $gain_string .= $sign_bit;
  1220. $gain_string .= str_pad(decbin($stored_replaygain), 9, '0', STR_PAD_LEFT);
  1221. return $gain_string;
  1222. }
  1223. }
  1224. ?>