getid3.php 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826
  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. // also https://github.com/JamesHeinrich/getID3 //
  7. /////////////////////////////////////////////////////////////////
  8. // //
  9. // Please see readme.txt for more information //
  10. // ///
  11. /////////////////////////////////////////////////////////////////
  12. // define a constant rather than looking up every time it is needed
  13. if (!defined('GETID3_OS_ISWINDOWS')) {
  14. define('GETID3_OS_ISWINDOWS', (stripos(PHP_OS, 'WIN') === 0));
  15. }
  16. // Get base path of getID3() - ONCE
  17. if (!defined('GETID3_INCLUDEPATH')) {
  18. define('GETID3_INCLUDEPATH', dirname(__FILE__).DIRECTORY_SEPARATOR);
  19. }
  20. // Workaround Bug #39923 (https://bugs.php.net/bug.php?id=39923)
  21. if (!defined('IMG_JPG') && defined('IMAGETYPE_JPEG')) {
  22. define('IMG_JPG', IMAGETYPE_JPEG);
  23. }
  24. // attempt to define temp dir as something flexible but reliable
  25. $temp_dir = ini_get('upload_tmp_dir');
  26. if ($temp_dir && (!is_dir($temp_dir) || !is_readable($temp_dir))) {
  27. $temp_dir = '';
  28. }
  29. if (!$temp_dir && function_exists('sys_get_temp_dir')) { // sys_get_temp_dir added in PHP v5.2.1
  30. // sys_get_temp_dir() may give inaccessible temp dir, e.g. with open_basedir on virtual hosts
  31. $temp_dir = sys_get_temp_dir();
  32. }
  33. $temp_dir = @realpath($temp_dir); // see https://github.com/JamesHeinrich/getID3/pull/10
  34. $open_basedir = ini_get('open_basedir');
  35. if ($open_basedir) {
  36. // e.g. "/var/www/vhosts/getid3.org/httpdocs/:/tmp/"
  37. $temp_dir = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $temp_dir);
  38. $open_basedir = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $open_basedir);
  39. if (substr($temp_dir, -1, 1) != DIRECTORY_SEPARATOR) {
  40. $temp_dir .= DIRECTORY_SEPARATOR;
  41. }
  42. $found_valid_tempdir = false;
  43. $open_basedirs = explode(PATH_SEPARATOR, $open_basedir);
  44. foreach ($open_basedirs as $basedir) {
  45. if (substr($basedir, -1, 1) != DIRECTORY_SEPARATOR) {
  46. $basedir .= DIRECTORY_SEPARATOR;
  47. }
  48. if (preg_match('#^'.preg_quote($basedir).'#', $temp_dir)) {
  49. $found_valid_tempdir = true;
  50. break;
  51. }
  52. }
  53. if (!$found_valid_tempdir) {
  54. $temp_dir = '';
  55. }
  56. unset($open_basedirs, $found_valid_tempdir, $basedir);
  57. }
  58. if (!$temp_dir) {
  59. $temp_dir = '*'; // invalid directory name should force tempnam() to use system default temp dir
  60. }
  61. // $temp_dir = '/something/else/'; // feel free to override temp dir here if it works better for your system
  62. if (!defined('GETID3_TEMP_DIR')) {
  63. define('GETID3_TEMP_DIR', $temp_dir);
  64. }
  65. unset($open_basedir, $temp_dir);
  66. // End: Defines
  67. class getID3
  68. {
  69. // public: Settings
  70. public $encoding = 'UTF-8'; // CASE SENSITIVE! - i.e. (must be supported by iconv()). Examples: ISO-8859-1 UTF-8 UTF-16 UTF-16BE
  71. public $encoding_id3v1 = 'ISO-8859-1'; // Should always be 'ISO-8859-1', but some tags may be written in other encodings such as 'EUC-CN' or 'CP1252'
  72. // public: Optional tag checks - disable for speed.
  73. public $option_tag_id3v1 = true; // Read and process ID3v1 tags
  74. public $option_tag_id3v2 = true; // Read and process ID3v2 tags
  75. public $option_tag_lyrics3 = true; // Read and process Lyrics3 tags
  76. public $option_tag_apetag = true; // Read and process APE tags
  77. public $option_tags_process = true; // Copy tags to root key 'tags' and encode to $this->encoding
  78. public $option_tags_html = true; // Copy tags to root key 'tags_html' properly translated from various encodings to HTML entities
  79. // public: Optional tag/comment calucations
  80. public $option_extra_info = true; // Calculate additional info such as bitrate, channelmode etc
  81. // public: Optional handling of embedded attachments (e.g. images)
  82. public $option_save_attachments = true; // defaults to true (ATTACHMENTS_INLINE) for backward compatibility
  83. // public: Optional calculations
  84. public $option_md5_data = false; // Get MD5 sum of data part - slow
  85. public $option_md5_data_source = false; // Use MD5 of source file if availble - only FLAC and OptimFROG
  86. public $option_sha1_data = false; // Get SHA1 sum of data part - slow
  87. public $option_max_2gb_check = null; // Check whether file is larger than 2GB and thus not supported by 32-bit PHP (null: auto-detect based on PHP_INT_MAX)
  88. // public: Read buffer size in bytes
  89. public $option_fread_buffer_size = 32768;
  90. // Public variables
  91. public $filename; // Filename of file being analysed.
  92. public $fp; // Filepointer to file being analysed.
  93. public $info; // Result array.
  94. public $tempdir = GETID3_TEMP_DIR;
  95. public $memory_limit = 0;
  96. // Protected variables
  97. protected $startup_error = '';
  98. protected $startup_warning = '';
  99. const VERSION = '1.9.11-201601190922';
  100. const FREAD_BUFFER_SIZE = 32768;
  101. const ATTACHMENTS_NONE = false;
  102. const ATTACHMENTS_INLINE = true;
  103. // public: constructor
  104. public function __construct() {
  105. // Check for PHP version
  106. $required_php_version = '5.3.0';
  107. if (version_compare(PHP_VERSION, $required_php_version, '<')) {
  108. $this->startup_error .= 'getID3() requires PHP v'.$required_php_version.' or higher - you are running v'.PHP_VERSION;
  109. return false;
  110. }
  111. // Check memory
  112. $this->memory_limit = ini_get('memory_limit');
  113. if (preg_match('#([0-9]+)M#i', $this->memory_limit, $matches)) {
  114. // could be stored as "16M" rather than 16777216 for example
  115. $this->memory_limit = $matches[1] * 1048576;
  116. } elseif (preg_match('#([0-9]+)G#i', $this->memory_limit, $matches)) { // The 'G' modifier is available since PHP 5.1.0
  117. // could be stored as "2G" rather than 2147483648 for example
  118. $this->memory_limit = $matches[1] * 1073741824;
  119. }
  120. if ($this->memory_limit <= 0) {
  121. // memory limits probably disabled
  122. } elseif ($this->memory_limit <= 4194304) {
  123. $this->startup_error .= 'PHP has less than 4MB available memory and will very likely run out. Increase memory_limit in php.ini';
  124. } elseif ($this->memory_limit <= 12582912) {
  125. $this->startup_warning .= 'PHP has less than 12MB available memory and might run out if all modules are loaded. Increase memory_limit in php.ini';
  126. }
  127. // Check safe_mode off
  128. if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
  129. $this->warning('WARNING: Safe mode is on, shorten support disabled, md5data/sha1data for ogg vorbis disabled, ogg vorbos/flac tag writing disabled.');
  130. }
  131. if (intval(ini_get('mbstring.func_overload')) > 0) {
  132. $this->warning('WARNING: php.ini contains "mbstring.func_overload = '.ini_get('mbstring.func_overload').'", this may break things.');
  133. }
  134. // Check for magic_quotes_runtime
  135. if (function_exists('get_magic_quotes_runtime')) {
  136. if (get_magic_quotes_runtime()) {
  137. return $this->startup_error('magic_quotes_runtime must be disabled before running getID3(). Surround getid3 block by set_magic_quotes_runtime(0) and set_magic_quotes_runtime(1).');
  138. }
  139. }
  140. // Check for magic_quotes_gpc
  141. if (function_exists('magic_quotes_gpc')) {
  142. if (get_magic_quotes_gpc()) {
  143. return $this->startup_error('magic_quotes_gpc must be disabled before running getID3(). Surround getid3 block by set_magic_quotes_gpc(0) and set_magic_quotes_gpc(1).');
  144. }
  145. }
  146. // Load support library
  147. if (!include_once(GETID3_INCLUDEPATH.'getid3.lib.php')) {
  148. $this->startup_error .= 'getid3.lib.php is missing or corrupt';
  149. }
  150. if ($this->option_max_2gb_check === null) {
  151. $this->option_max_2gb_check = (PHP_INT_MAX <= 2147483647);
  152. }
  153. // Needed for Windows only:
  154. // Define locations of helper applications for Shorten, VorbisComment, MetaFLAC
  155. // as well as other helper functions such as head, tail, md5sum, etc
  156. // This path cannot contain spaces, but the below code will attempt to get the
  157. // 8.3-equivalent path automatically
  158. // IMPORTANT: This path must include the trailing slash
  159. if (GETID3_OS_ISWINDOWS && !defined('GETID3_HELPERAPPSDIR')) {
  160. $helperappsdir = GETID3_INCLUDEPATH.'..'.DIRECTORY_SEPARATOR.'helperapps'; // must not have any space in this path
  161. if (!is_dir($helperappsdir)) {
  162. $this->startup_warning .= '"'.$helperappsdir.'" cannot be defined as GETID3_HELPERAPPSDIR because it does not exist';
  163. } elseif (strpos(realpath($helperappsdir), ' ') !== false) {
  164. $DirPieces = explode(DIRECTORY_SEPARATOR, realpath($helperappsdir));
  165. $path_so_far = array();
  166. foreach ($DirPieces as $key => $value) {
  167. if (strpos($value, ' ') !== false) {
  168. if (!empty($path_so_far)) {
  169. $commandline = 'dir /x '.escapeshellarg(implode(DIRECTORY_SEPARATOR, $path_so_far));
  170. $dir_listing = `$commandline`;
  171. $lines = explode("\n", $dir_listing);
  172. foreach ($lines as $line) {
  173. $line = trim($line);
  174. if (preg_match('#^([0-9/]{10}) +([0-9:]{4,5}( [AP]M)?) +(<DIR>|[0-9,]+) +([^ ]{0,11}) +(.+)$#', $line, $matches)) {
  175. list($dummy, $date, $time, $ampm, $filesize, $shortname, $filename) = $matches;
  176. if ((strtoupper($filesize) == '<DIR>') && (strtolower($filename) == strtolower($value))) {
  177. $value = $shortname;
  178. }
  179. }
  180. }
  181. } else {
  182. $this->startup_warning .= 'GETID3_HELPERAPPSDIR must not have any spaces in it - use 8dot3 naming convention if neccesary. You can run "dir /x" from the commandline to see the correct 8.3-style names.';
  183. }
  184. }
  185. $path_so_far[] = $value;
  186. }
  187. $helperappsdir = implode(DIRECTORY_SEPARATOR, $path_so_far);
  188. }
  189. define('GETID3_HELPERAPPSDIR', $helperappsdir.DIRECTORY_SEPARATOR);
  190. }
  191. return true;
  192. }
  193. public function version() {
  194. return self::VERSION;
  195. }
  196. public function fread_buffer_size() {
  197. return $this->option_fread_buffer_size;
  198. }
  199. // public: setOption
  200. public function setOption($optArray) {
  201. if (!is_array($optArray) || empty($optArray)) {
  202. return false;
  203. }
  204. foreach ($optArray as $opt => $val) {
  205. if (isset($this->$opt) === false) {
  206. continue;
  207. }
  208. $this->$opt = $val;
  209. }
  210. return true;
  211. }
  212. public function openfile($filename, $filesize=null) {
  213. try {
  214. if (!empty($this->startup_error)) {
  215. throw new getid3_exception($this->startup_error);
  216. }
  217. if (!empty($this->startup_warning)) {
  218. $this->warning($this->startup_warning);
  219. }
  220. // init result array and set parameters
  221. $this->filename = $filename;
  222. $this->info = array();
  223. $this->info['GETID3_VERSION'] = $this->version();
  224. $this->info['php_memory_limit'] = (($this->memory_limit > 0) ? $this->memory_limit : false);
  225. // remote files not supported
  226. if (preg_match('/^(ht|f)tp:\/\//', $filename)) {
  227. throw new getid3_exception('Remote files are not supported - please copy the file locally first');
  228. }
  229. $filename = str_replace('/', DIRECTORY_SEPARATOR, $filename);
  230. $filename = preg_replace('#(.+)'.preg_quote(DIRECTORY_SEPARATOR).'{2,}#U', '\1'.DIRECTORY_SEPARATOR, $filename);
  231. // open local file
  232. //if (is_readable($filename) && is_file($filename) && ($this->fp = fopen($filename, 'rb'))) { // see http://www.getid3.org/phpBB3/viewtopic.php?t=1720
  233. if ((is_readable($filename) || file_exists($filename)) && is_file($filename) && ($this->fp = fopen($filename, 'rb'))) {
  234. // great
  235. } else {
  236. $errormessagelist = array();
  237. if (!is_readable($filename)) {
  238. $errormessagelist[] = '!is_readable';
  239. }
  240. if (!is_file($filename)) {
  241. $errormessagelist[] = '!is_file';
  242. }
  243. if (!file_exists($filename)) {
  244. $errormessagelist[] = '!file_exists';
  245. }
  246. if (empty($errormessagelist)) {
  247. $errormessagelist[] = 'fopen failed';
  248. }
  249. throw new getid3_exception('Could not open "'.$filename.'" ('.implode('; ', $errormessagelist).')');
  250. }
  251. $this->info['filesize'] = (!is_null($filesize) ? $filesize : filesize($filename));
  252. // set redundant parameters - might be needed in some include file
  253. // filenames / filepaths in getID3 are always expressed with forward slashes (unix-style) for both Windows and other to try and minimize confusion
  254. $filename = str_replace('\\', '/', $filename);
  255. $this->info['filepath'] = str_replace('\\', '/', realpath(dirname($filename)));
  256. $this->info['filename'] = getid3_lib::mb_basename($filename);
  257. $this->info['filenamepath'] = $this->info['filepath'].'/'.$this->info['filename'];
  258. // option_max_2gb_check
  259. if ($this->option_max_2gb_check) {
  260. // PHP (32-bit all, and 64-bit Windows) doesn't support integers larger than 2^31 (~2GB)
  261. // filesize() simply returns (filesize % (pow(2, 32)), no matter the actual filesize
  262. // ftell() returns 0 if seeking to the end is beyond the range of unsigned integer
  263. $fseek = fseek($this->fp, 0, SEEK_END);
  264. if (($fseek < 0) || (($this->info['filesize'] != 0) && (ftell($this->fp) == 0)) ||
  265. ($this->info['filesize'] < 0) ||
  266. (ftell($this->fp) < 0)) {
  267. $real_filesize = getid3_lib::getFileSizeSyscall($this->info['filenamepath']);
  268. if ($real_filesize === false) {
  269. unset($this->info['filesize']);
  270. fclose($this->fp);
  271. throw new getid3_exception('Unable to determine actual filesize. File is most likely larger than '.round(PHP_INT_MAX / 1073741824).'GB and is not supported by PHP.');
  272. } elseif (getid3_lib::intValueSupported($real_filesize)) {
  273. unset($this->info['filesize']);
  274. fclose($this->fp);
  275. throw new getid3_exception('PHP seems to think the file is larger than '.round(PHP_INT_MAX / 1073741824).'GB, but filesystem reports it as '.number_format($real_filesize, 3).'GB, please report to info@getid3.org');
  276. }
  277. $this->info['filesize'] = $real_filesize;
  278. $this->warning('File is larger than '.round(PHP_INT_MAX / 1073741824).'GB (filesystem reports it as '.number_format($real_filesize, 3).'GB) and is not properly supported by PHP.');
  279. }
  280. }
  281. // set more parameters
  282. $this->info['avdataoffset'] = 0;
  283. $this->info['avdataend'] = $this->info['filesize'];
  284. $this->info['fileformat'] = ''; // filled in later
  285. $this->info['audio']['dataformat'] = ''; // filled in later, unset if not used
  286. $this->info['video']['dataformat'] = ''; // filled in later, unset if not used
  287. $this->info['tags'] = array(); // filled in later, unset if not used
  288. $this->info['error'] = array(); // filled in later, unset if not used
  289. $this->info['warning'] = array(); // filled in later, unset if not used
  290. $this->info['comments'] = array(); // filled in later, unset if not used
  291. $this->info['encoding'] = $this->encoding; // required by id3v2 and iso modules - can be unset at the end if desired
  292. return true;
  293. } catch (Exception $e) {
  294. $this->error($e->getMessage());
  295. }
  296. return false;
  297. }
  298. // public: analyze file
  299. public function analyze($filename, $filesize=null, $original_filename='') {
  300. try {
  301. if (!$this->openfile($filename, $filesize)) {
  302. return $this->info;
  303. }
  304. // Handle tags
  305. foreach (array('id3v2'=>'id3v2', 'id3v1'=>'id3v1', 'apetag'=>'ape', 'lyrics3'=>'lyrics3') as $tag_name => $tag_key) {
  306. $option_tag = 'option_tag_'.$tag_name;
  307. if ($this->$option_tag) {
  308. $this->include_module('tag.'.$tag_name);
  309. try {
  310. $tag_class = 'getid3_'.$tag_name;
  311. $tag = new $tag_class($this);
  312. $tag->Analyze();
  313. }
  314. catch (getid3_exception $e) {
  315. throw $e;
  316. }
  317. }
  318. }
  319. if (isset($this->info['id3v2']['tag_offset_start'])) {
  320. $this->info['avdataoffset'] = max($this->info['avdataoffset'], $this->info['id3v2']['tag_offset_end']);
  321. }
  322. foreach (array('id3v1'=>'id3v1', 'apetag'=>'ape', 'lyrics3'=>'lyrics3') as $tag_name => $tag_key) {
  323. if (isset($this->info[$tag_key]['tag_offset_start'])) {
  324. $this->info['avdataend'] = min($this->info['avdataend'], $this->info[$tag_key]['tag_offset_start']);
  325. }
  326. }
  327. // ID3v2 detection (NOT parsing), even if ($this->option_tag_id3v2 == false) done to make fileformat easier
  328. if (!$this->option_tag_id3v2) {
  329. fseek($this->fp, 0);
  330. $header = fread($this->fp, 10);
  331. if ((substr($header, 0, 3) == 'ID3') && (strlen($header) == 10)) {
  332. $this->info['id3v2']['header'] = true;
  333. $this->info['id3v2']['majorversion'] = ord($header{3});
  334. $this->info['id3v2']['minorversion'] = ord($header{4});
  335. $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
  336. }
  337. }
  338. // read 32 kb file data
  339. fseek($this->fp, $this->info['avdataoffset']);
  340. $formattest = fread($this->fp, 32774);
  341. // determine format
  342. $determined_format = $this->GetFileFormat($formattest, ($original_filename ? $original_filename : $filename));
  343. // unable to determine file format
  344. if (!$determined_format) {
  345. fclose($this->fp);
  346. return $this->error('unable to determine file format');
  347. }
  348. // check for illegal ID3 tags
  349. if (isset($determined_format['fail_id3']) && (in_array('id3v1', $this->info['tags']) || in_array('id3v2', $this->info['tags']))) {
  350. if ($determined_format['fail_id3'] === 'ERROR') {
  351. fclose($this->fp);
  352. return $this->error('ID3 tags not allowed on this file type.');
  353. } elseif ($determined_format['fail_id3'] === 'WARNING') {
  354. $this->warning('ID3 tags not allowed on this file type.');
  355. }
  356. }
  357. // check for illegal APE tags
  358. if (isset($determined_format['fail_ape']) && in_array('ape', $this->info['tags'])) {
  359. if ($determined_format['fail_ape'] === 'ERROR') {
  360. fclose($this->fp);
  361. return $this->error('APE tags not allowed on this file type.');
  362. } elseif ($determined_format['fail_ape'] === 'WARNING') {
  363. $this->warning('APE tags not allowed on this file type.');
  364. }
  365. }
  366. // set mime type
  367. $this->info['mime_type'] = $determined_format['mime_type'];
  368. // supported format signature pattern detected, but module deleted
  369. if (!file_exists(GETID3_INCLUDEPATH.$determined_format['include'])) {
  370. fclose($this->fp);
  371. return $this->error('Format not supported, module "'.$determined_format['include'].'" was removed.');
  372. }
  373. // module requires iconv support
  374. // Check encoding/iconv support
  375. if (!empty($determined_format['iconv_req']) && !function_exists('iconv') && !in_array($this->encoding, array('ISO-8859-1', 'UTF-8', 'UTF-16LE', 'UTF-16BE', 'UTF-16'))) {
  376. $errormessage = 'iconv() support is required for this module ('.$determined_format['include'].') for encodings other than ISO-8859-1, UTF-8, UTF-16LE, UTF16-BE, UTF-16. ';
  377. if (GETID3_OS_ISWINDOWS) {
  378. $errormessage .= 'PHP does not have iconv() support. Please enable php_iconv.dll in php.ini, and copy iconv.dll from c:/php/dlls to c:/windows/system32';
  379. } else {
  380. $errormessage .= 'PHP is not compiled with iconv() support. Please recompile with the --with-iconv switch';
  381. }
  382. return $this->error($errormessage);
  383. }
  384. // include module
  385. include_once(GETID3_INCLUDEPATH.$determined_format['include']);
  386. // instantiate module class
  387. $class_name = 'getid3_'.$determined_format['module'];
  388. if (!class_exists($class_name)) {
  389. return $this->error('Format not supported, module "'.$determined_format['include'].'" is corrupt.');
  390. }
  391. $class = new $class_name($this);
  392. $class->Analyze();
  393. unset($class);
  394. // close file
  395. fclose($this->fp);
  396. // process all tags - copy to 'tags' and convert charsets
  397. if ($this->option_tags_process) {
  398. $this->HandleAllTags();
  399. }
  400. // perform more calculations
  401. if ($this->option_extra_info) {
  402. $this->ChannelsBitratePlaytimeCalculations();
  403. $this->CalculateCompressionRatioVideo();
  404. $this->CalculateCompressionRatioAudio();
  405. $this->CalculateReplayGain();
  406. $this->ProcessAudioStreams();
  407. }
  408. // get the MD5 sum of the audio/video portion of the file - without ID3/APE/Lyrics3/etc header/footer tags
  409. if ($this->option_md5_data) {
  410. // do not calc md5_data if md5_data_source is present - set by flac only - future MPC/SV8 too
  411. if (!$this->option_md5_data_source || empty($this->info['md5_data_source'])) {
  412. $this->getHashdata('md5');
  413. }
  414. }
  415. // get the SHA1 sum of the audio/video portion of the file - without ID3/APE/Lyrics3/etc header/footer tags
  416. if ($this->option_sha1_data) {
  417. $this->getHashdata('sha1');
  418. }
  419. // remove undesired keys
  420. $this->CleanUp();
  421. } catch (Exception $e) {
  422. $this->error('Caught exception: '.$e->getMessage());
  423. }
  424. // return info array
  425. return $this->info;
  426. }
  427. // private: error handling
  428. public function error($message) {
  429. $this->CleanUp();
  430. if (!isset($this->info['error'])) {
  431. $this->info['error'] = array();
  432. }
  433. $this->info['error'][] = $message;
  434. return $this->info;
  435. }
  436. // private: warning handling
  437. public function warning($message) {
  438. $this->info['warning'][] = $message;
  439. return true;
  440. }
  441. // private: CleanUp
  442. private function CleanUp() {
  443. // remove possible empty keys
  444. $AVpossibleEmptyKeys = array('dataformat', 'bits_per_sample', 'encoder_options', 'streams', 'bitrate');
  445. foreach ($AVpossibleEmptyKeys as $dummy => $key) {
  446. if (empty($this->info['audio'][$key]) && isset($this->info['audio'][$key])) {
  447. unset($this->info['audio'][$key]);
  448. }
  449. if (empty($this->info['video'][$key]) && isset($this->info['video'][$key])) {
  450. unset($this->info['video'][$key]);
  451. }
  452. }
  453. // remove empty root keys
  454. if (!empty($this->info)) {
  455. foreach ($this->info as $key => $value) {
  456. if (empty($this->info[$key]) && ($this->info[$key] !== 0) && ($this->info[$key] !== '0')) {
  457. unset($this->info[$key]);
  458. }
  459. }
  460. }
  461. // remove meaningless entries from unknown-format files
  462. if (empty($this->info['fileformat'])) {
  463. if (isset($this->info['avdataoffset'])) {
  464. unset($this->info['avdataoffset']);
  465. }
  466. if (isset($this->info['avdataend'])) {
  467. unset($this->info['avdataend']);
  468. }
  469. }
  470. // remove possible duplicated identical entries
  471. if (!empty($this->info['error'])) {
  472. $this->info['error'] = array_values(array_unique($this->info['error']));
  473. }
  474. if (!empty($this->info['warning'])) {
  475. $this->info['warning'] = array_values(array_unique($this->info['warning']));
  476. }
  477. // remove "global variable" type keys
  478. unset($this->info['php_memory_limit']);
  479. return true;
  480. }
  481. // return array containing information about all supported formats
  482. public function GetFileFormatArray() {
  483. static $format_info = array();
  484. if (empty($format_info)) {
  485. $format_info = array(
  486. // Audio formats
  487. // AC-3 - audio - Dolby AC-3 / Dolby Digital
  488. 'ac3' => array(
  489. 'pattern' => '^\x0B\x77',
  490. 'group' => 'audio',
  491. 'module' => 'ac3',
  492. 'mime_type' => 'audio/ac3',
  493. ),
  494. // AAC - audio - Advanced Audio Coding (AAC) - ADIF format
  495. 'adif' => array(
  496. 'pattern' => '^ADIF',
  497. 'group' => 'audio',
  498. 'module' => 'aac',
  499. 'mime_type' => 'application/octet-stream',
  500. 'fail_ape' => 'WARNING',
  501. ),
  502. /*
  503. // AA - audio - Audible Audiobook
  504. 'aa' => array(
  505. 'pattern' => '^.{4}\x57\x90\x75\x36',
  506. 'group' => 'audio',
  507. 'module' => 'aa',
  508. 'mime_type' => 'audio/audible',
  509. ),
  510. */
  511. // AAC - audio - Advanced Audio Coding (AAC) - ADTS format (very similar to MP3)
  512. 'adts' => array(
  513. 'pattern' => '^\xFF[\xF0-\xF1\xF8-\xF9]',
  514. 'group' => 'audio',
  515. 'module' => 'aac',
  516. 'mime_type' => 'application/octet-stream',
  517. 'fail_ape' => 'WARNING',
  518. ),
  519. // AU - audio - NeXT/Sun AUdio (AU)
  520. 'au' => array(
  521. 'pattern' => '^\.snd',
  522. 'group' => 'audio',
  523. 'module' => 'au',
  524. 'mime_type' => 'audio/basic',
  525. ),
  526. // AMR - audio - Adaptive Multi Rate
  527. 'amr' => array(
  528. 'pattern' => '^\x23\x21AMR\x0A', // #!AMR[0A]
  529. 'group' => 'audio',
  530. 'module' => 'amr',
  531. 'mime_type' => 'audio/amr',
  532. ),
  533. // AVR - audio - Audio Visual Research
  534. 'avr' => array(
  535. 'pattern' => '^2BIT',
  536. 'group' => 'audio',
  537. 'module' => 'avr',
  538. 'mime_type' => 'application/octet-stream',
  539. ),
  540. // BONK - audio - Bonk v0.9+
  541. 'bonk' => array(
  542. 'pattern' => '^\x00(BONK|INFO|META| ID3)',
  543. 'group' => 'audio',
  544. 'module' => 'bonk',
  545. 'mime_type' => 'audio/xmms-bonk',
  546. ),
  547. // DSS - audio - Digital Speech Standard
  548. 'dss' => array(
  549. 'pattern' => '^[\x02-\x03]ds[s2]',
  550. 'group' => 'audio',
  551. 'module' => 'dss',
  552. 'mime_type' => 'application/octet-stream',
  553. ),
  554. // DTS - audio - Dolby Theatre System
  555. 'dts' => array(
  556. 'pattern' => '^\x7F\xFE\x80\x01',
  557. 'group' => 'audio',
  558. 'module' => 'dts',
  559. 'mime_type' => 'audio/dts',
  560. ),
  561. // FLAC - audio - Free Lossless Audio Codec
  562. 'flac' => array(
  563. 'pattern' => '^fLaC',
  564. 'group' => 'audio',
  565. 'module' => 'flac',
  566. 'mime_type' => 'audio/x-flac',
  567. ),
  568. // LA - audio - Lossless Audio (LA)
  569. 'la' => array(
  570. 'pattern' => '^LA0[2-4]',
  571. 'group' => 'audio',
  572. 'module' => 'la',
  573. 'mime_type' => 'application/octet-stream',
  574. ),
  575. // LPAC - audio - Lossless Predictive Audio Compression (LPAC)
  576. 'lpac' => array(
  577. 'pattern' => '^LPAC',
  578. 'group' => 'audio',
  579. 'module' => 'lpac',
  580. 'mime_type' => 'application/octet-stream',
  581. ),
  582. // MIDI - audio - MIDI (Musical Instrument Digital Interface)
  583. 'midi' => array(
  584. 'pattern' => '^MThd',
  585. 'group' => 'audio',
  586. 'module' => 'midi',
  587. 'mime_type' => 'audio/midi',
  588. ),
  589. // MAC - audio - Monkey's Audio Compressor
  590. 'mac' => array(
  591. 'pattern' => '^MAC ',
  592. 'group' => 'audio',
  593. 'module' => 'monkey',
  594. 'mime_type' => 'application/octet-stream',
  595. ),
  596. // has been known to produce false matches in random files (e.g. JPEGs), leave out until more precise matching available
  597. // // MOD - audio - MODule (assorted sub-formats)
  598. // 'mod' => array(
  599. // 'pattern' => '^.{1080}(M\\.K\\.|M!K!|FLT4|FLT8|[5-9]CHN|[1-3][0-9]CH)',
  600. // 'group' => 'audio',
  601. // 'module' => 'mod',
  602. // 'option' => 'mod',
  603. // 'mime_type' => 'audio/mod',
  604. // ),
  605. // MOD - audio - MODule (Impulse Tracker)
  606. 'it' => array(
  607. 'pattern' => '^IMPM',
  608. 'group' => 'audio',
  609. 'module' => 'mod',
  610. //'option' => 'it',
  611. 'mime_type' => 'audio/it',
  612. ),
  613. // MOD - audio - MODule (eXtended Module, various sub-formats)
  614. 'xm' => array(
  615. 'pattern' => '^Extended Module',
  616. 'group' => 'audio',
  617. 'module' => 'mod',
  618. //'option' => 'xm',
  619. 'mime_type' => 'audio/xm',
  620. ),
  621. // MOD - audio - MODule (ScreamTracker)
  622. 's3m' => array(
  623. 'pattern' => '^.{44}SCRM',
  624. 'group' => 'audio',
  625. 'module' => 'mod',
  626. //'option' => 's3m',
  627. 'mime_type' => 'audio/s3m',
  628. ),
  629. // MPC - audio - Musepack / MPEGplus
  630. 'mpc' => array(
  631. 'pattern' => '^(MPCK|MP\+|[\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])',
  632. 'group' => 'audio',
  633. 'module' => 'mpc',
  634. 'mime_type' => 'audio/x-musepack',
  635. ),
  636. // MP3 - audio - MPEG-audio Layer 3 (very similar to AAC-ADTS)
  637. 'mp3' => array(
  638. 'pattern' => '^\xFF[\xE2-\xE7\xF2-\xF7\xFA-\xFF][\x00-\x0B\x10-\x1B\x20-\x2B\x30-\x3B\x40-\x4B\x50-\x5B\x60-\x6B\x70-\x7B\x80-\x8B\x90-\x9B\xA0-\xAB\xB0-\xBB\xC0-\xCB\xD0-\xDB\xE0-\xEB\xF0-\xFB]',
  639. 'group' => 'audio',
  640. 'module' => 'mp3',
  641. 'mime_type' => 'audio/mpeg',
  642. ),
  643. // OFR - audio - OptimFROG
  644. 'ofr' => array(
  645. 'pattern' => '^(\*RIFF|OFR)',
  646. 'group' => 'audio',
  647. 'module' => 'optimfrog',
  648. 'mime_type' => 'application/octet-stream',
  649. ),
  650. // RKAU - audio - RKive AUdio compressor
  651. 'rkau' => array(
  652. 'pattern' => '^RKA',
  653. 'group' => 'audio',
  654. 'module' => 'rkau',
  655. 'mime_type' => 'application/octet-stream',
  656. ),
  657. // SHN - audio - Shorten
  658. 'shn' => array(
  659. 'pattern' => '^ajkg',
  660. 'group' => 'audio',
  661. 'module' => 'shorten',
  662. 'mime_type' => 'audio/xmms-shn',
  663. 'fail_id3' => 'ERROR',
  664. 'fail_ape' => 'ERROR',
  665. ),
  666. // TTA - audio - TTA Lossless Audio Compressor (http://tta.corecodec.org)
  667. 'tta' => array(
  668. 'pattern' => '^TTA', // could also be '^TTA(\x01|\x02|\x03|2|1)'
  669. 'group' => 'audio',
  670. 'module' => 'tta',
  671. 'mime_type' => 'application/octet-stream',
  672. ),
  673. // VOC - audio - Creative Voice (VOC)
  674. 'voc' => array(
  675. 'pattern' => '^Creative Voice File',
  676. 'group' => 'audio',
  677. 'module' => 'voc',
  678. 'mime_type' => 'audio/voc',
  679. ),
  680. // VQF - audio - transform-domain weighted interleave Vector Quantization Format (VQF)
  681. 'vqf' => array(
  682. 'pattern' => '^TWIN',
  683. 'group' => 'audio',
  684. 'module' => 'vqf',
  685. 'mime_type' => 'application/octet-stream',
  686. ),
  687. // WV - audio - WavPack (v4.0+)
  688. 'wv' => array(
  689. 'pattern' => '^wvpk',
  690. 'group' => 'audio',
  691. 'module' => 'wavpack',
  692. 'mime_type' => 'application/octet-stream',
  693. ),
  694. // Audio-Video formats
  695. // ASF - audio/video - Advanced Streaming Format, Windows Media Video, Windows Media Audio
  696. 'asf' => array(
  697. 'pattern' => '^\x30\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C',
  698. 'group' => 'audio-video',
  699. 'module' => 'asf',
  700. 'mime_type' => 'video/x-ms-asf',
  701. 'iconv_req' => false,
  702. ),
  703. // BINK - audio/video - Bink / Smacker
  704. 'bink' => array(
  705. 'pattern' => '^(BIK|SMK)',
  706. 'group' => 'audio-video',
  707. 'module' => 'bink',
  708. 'mime_type' => 'application/octet-stream',
  709. ),
  710. // FLV - audio/video - FLash Video
  711. 'flv' => array(
  712. 'pattern' => '^FLV\x01',
  713. 'group' => 'audio-video',
  714. 'module' => 'flv',
  715. 'mime_type' => 'video/x-flv',
  716. ),
  717. // MKAV - audio/video - Mastroka
  718. 'matroska' => array(
  719. 'pattern' => '^\x1A\x45\xDF\xA3',
  720. 'group' => 'audio-video',
  721. 'module' => 'matroska',
  722. 'mime_type' => 'video/x-matroska', // may also be audio/x-matroska
  723. ),
  724. // MPEG - audio/video - MPEG (Moving Pictures Experts Group)
  725. 'mpeg' => array(
  726. 'pattern' => '^\x00\x00\x01(\xBA|\xB3)',
  727. 'group' => 'audio-video',
  728. 'module' => 'mpeg',
  729. 'mime_type' => 'video/mpeg',
  730. ),
  731. // NSV - audio/video - Nullsoft Streaming Video (NSV)
  732. 'nsv' => array(
  733. 'pattern' => '^NSV[sf]',
  734. 'group' => 'audio-video',
  735. 'module' => 'nsv',
  736. 'mime_type' => 'application/octet-stream',
  737. ),
  738. // Ogg - audio/video - Ogg (Ogg-Vorbis, Ogg-FLAC, Speex, Ogg-Theora(*), Ogg-Tarkin(*))
  739. 'ogg' => array(
  740. 'pattern' => '^OggS',
  741. 'group' => 'audio',
  742. 'module' => 'ogg',
  743. 'mime_type' => 'application/ogg',
  744. 'fail_id3' => 'WARNING',
  745. 'fail_ape' => 'WARNING',
  746. ),
  747. // QT - audio/video - Quicktime
  748. 'quicktime' => array(
  749. 'pattern' => '^.{4}(cmov|free|ftyp|mdat|moov|pnot|skip|wide)',
  750. 'group' => 'audio-video',
  751. 'module' => 'quicktime',
  752. 'mime_type' => 'video/quicktime',
  753. ),
  754. // 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)
  755. 'riff' => array(
  756. 'pattern' => '^(RIFF|SDSS|FORM)',
  757. 'group' => 'audio-video',
  758. 'module' => 'riff',
  759. 'mime_type' => 'audio/x-wav',
  760. 'fail_ape' => 'WARNING',
  761. ),
  762. // Real - audio/video - RealAudio, RealVideo
  763. 'real' => array(
  764. 'pattern' => '^(\\.RMF|\\.ra)',
  765. 'group' => 'audio-video',
  766. 'module' => 'real',
  767. 'mime_type' => 'audio/x-realaudio',
  768. ),
  769. // SWF - audio/video - ShockWave Flash
  770. 'swf' => array(
  771. 'pattern' => '^(F|C)WS',
  772. 'group' => 'audio-video',
  773. 'module' => 'swf',
  774. 'mime_type' => 'application/x-shockwave-flash',
  775. ),
  776. // TS - audio/video - MPEG-2 Transport Stream
  777. 'ts' => array(
  778. 'pattern' => '^(\x47.{187}){10,}', // packets are 188 bytes long and start with 0x47 "G". Check for at least 10 packets matching this pattern
  779. 'group' => 'audio-video',
  780. 'module' => 'ts',
  781. 'mime_type' => 'video/MP2T',
  782. ),
  783. // Still-Image formats
  784. // BMP - still image - Bitmap (Windows, OS/2; uncompressed, RLE8, RLE4)
  785. 'bmp' => array(
  786. 'pattern' => '^BM',
  787. 'group' => 'graphic',
  788. 'module' => 'bmp',
  789. 'mime_type' => 'image/bmp',
  790. 'fail_id3' => 'ERROR',
  791. 'fail_ape' => 'ERROR',
  792. ),
  793. // GIF - still image - Graphics Interchange Format
  794. 'gif' => array(
  795. 'pattern' => '^GIF',
  796. 'group' => 'graphic',
  797. 'module' => 'gif',
  798. 'mime_type' => 'image/gif',
  799. 'fail_id3' => 'ERROR',
  800. 'fail_ape' => 'ERROR',
  801. ),
  802. // JPEG - still image - Joint Photographic Experts Group (JPEG)
  803. 'jpg' => array(
  804. 'pattern' => '^\xFF\xD8\xFF',
  805. 'group' => 'graphic',
  806. 'module' => 'jpg',
  807. 'mime_type' => 'image/jpeg',
  808. 'fail_id3' => 'ERROR',
  809. 'fail_ape' => 'ERROR',
  810. ),
  811. // PCD - still image - Kodak Photo CD
  812. 'pcd' => array(
  813. 'pattern' => '^.{2048}PCD_IPI\x00',
  814. 'group' => 'graphic',
  815. 'module' => 'pcd',
  816. 'mime_type' => 'image/x-photo-cd',
  817. 'fail_id3' => 'ERROR',
  818. 'fail_ape' => 'ERROR',
  819. ),
  820. // PNG - still image - Portable Network Graphics (PNG)
  821. 'png' => array(
  822. 'pattern' => '^\x89\x50\x4E\x47\x0D\x0A\x1A\x0A',
  823. 'group' => 'graphic',
  824. 'module' => 'png',
  825. 'mime_type' => 'image/png',
  826. 'fail_id3' => 'ERROR',
  827. 'fail_ape' => 'ERROR',
  828. ),
  829. // SVG - still image - Scalable Vector Graphics (SVG)
  830. 'svg' => array(
  831. 'pattern' => '(<!DOCTYPE svg PUBLIC |xmlns="http:\/\/www\.w3\.org\/2000\/svg")',
  832. 'group' => 'graphic',
  833. 'module' => 'svg',
  834. 'mime_type' => 'image/svg+xml',
  835. 'fail_id3' => 'ERROR',
  836. 'fail_ape' => 'ERROR',
  837. ),
  838. // TIFF - still image - Tagged Information File Format (TIFF)
  839. 'tiff' => array(
  840. 'pattern' => '^(II\x2A\x00|MM\x00\x2A)',
  841. 'group' => 'graphic',
  842. 'module' => 'tiff',
  843. 'mime_type' => 'image/tiff',
  844. 'fail_id3' => 'ERROR',
  845. 'fail_ape' => 'ERROR',
  846. ),
  847. // EFAX - still image - eFax (TIFF derivative)
  848. 'efax' => array(
  849. 'pattern' => '^\xDC\xFE',
  850. 'group' => 'graphic',
  851. 'module' => 'efax',
  852. 'mime_type' => 'image/efax',
  853. 'fail_id3' => 'ERROR',
  854. 'fail_ape' => 'ERROR',
  855. ),
  856. // Data formats
  857. // ISO - data - International Standards Organization (ISO) CD-ROM Image
  858. 'iso' => array(
  859. 'pattern' => '^.{32769}CD001',
  860. 'group' => 'misc',
  861. 'module' => 'iso',
  862. 'mime_type' => 'application/octet-stream',
  863. 'fail_id3' => 'ERROR',
  864. 'fail_ape' => 'ERROR',
  865. 'iconv_req' => false,
  866. ),
  867. // RAR - data - RAR compressed data
  868. 'rar' => array(
  869. 'pattern' => '^Rar\!',
  870. 'group' => 'archive',
  871. 'module' => 'rar',
  872. 'mime_type' => 'application/octet-stream',
  873. 'fail_id3' => 'ERROR',
  874. 'fail_ape' => 'ERROR',
  875. ),
  876. // SZIP - audio/data - SZIP compressed data
  877. 'szip' => array(
  878. 'pattern' => '^SZ\x0A\x04',
  879. 'group' => 'archive',
  880. 'module' => 'szip',
  881. 'mime_type' => 'application/octet-stream',
  882. 'fail_id3' => 'ERROR',
  883. 'fail_ape' => 'ERROR',
  884. ),
  885. // TAR - data - TAR compressed data
  886. 'tar' => array(
  887. '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}',
  888. 'group' => 'archive',
  889. 'module' => 'tar',
  890. 'mime_type' => 'application/x-tar',
  891. 'fail_id3' => 'ERROR',
  892. 'fail_ape' => 'ERROR',
  893. ),
  894. // GZIP - data - GZIP compressed data
  895. 'gz' => array(
  896. 'pattern' => '^\x1F\x8B\x08',
  897. 'group' => 'archive',
  898. 'module' => 'gzip',
  899. 'mime_type' => 'application/x-gzip',
  900. 'fail_id3' => 'ERROR',
  901. 'fail_ape' => 'ERROR',
  902. ),
  903. // ZIP - data - ZIP compressed data
  904. 'zip' => array(
  905. 'pattern' => '^PK\x03\x04',
  906. 'group' => 'archive',
  907. 'module' => 'zip',
  908. 'mime_type' => 'application/zip',
  909. 'fail_id3' => 'ERROR',
  910. 'fail_ape' => 'ERROR',
  911. ),
  912. // Misc other formats
  913. // PAR2 - data - Parity Volume Set Specification 2.0
  914. 'par2' => array (
  915. 'pattern' => '^PAR2\x00PKT',
  916. 'group' => 'misc',
  917. 'module' => 'par2',
  918. 'mime_type' => 'application/octet-stream',
  919. 'fail_id3' => 'ERROR',
  920. 'fail_ape' => 'ERROR',
  921. ),
  922. // PDF - data - Portable Document Format
  923. 'pdf' => array(
  924. 'pattern' => '^\x25PDF',
  925. 'group' => 'misc',
  926. 'module' => 'pdf',
  927. 'mime_type' => 'application/pdf',
  928. 'fail_id3' => 'ERROR',
  929. 'fail_ape' => 'ERROR',
  930. ),
  931. // MSOFFICE - data - ZIP compressed data
  932. 'msoffice' => array(
  933. 'pattern' => '^\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1', // D0CF11E == DOCFILE == Microsoft Office Document
  934. 'group' => 'misc',
  935. 'module' => 'msoffice',
  936. 'mime_type' => 'application/octet-stream',
  937. 'fail_id3' => 'ERROR',
  938. 'fail_ape' => 'ERROR',
  939. ),
  940. // CUE - data - CUEsheet (index to single-file disc images)
  941. 'cue' => array(
  942. 'pattern' => '', // empty pattern means cannot be automatically detected, will fall through all other formats and match based on filename and very basic file contents
  943. 'group' => 'misc',
  944. 'module' => 'cue',
  945. 'mime_type' => 'application/octet-stream',
  946. ),
  947. );
  948. }
  949. return $format_info;
  950. }
  951. public function GetFileFormat(&$filedata, $filename='') {
  952. // this function will determine the format of a file based on usually
  953. // the first 2-4 bytes of the file (8 bytes for PNG, 16 bytes for JPG,
  954. // and in the case of ISO CD image, 6 bytes offset 32kb from the start
  955. // of the file).
  956. // Identify file format - loop through $format_info and detect with reg expr
  957. foreach ($this->GetFileFormatArray() as $format_name => $info) {
  958. // The /s switch on preg_match() forces preg_match() NOT to treat
  959. // newline (0x0A) characters as special chars but do a binary match
  960. if (!empty($info['pattern']) && preg_match('#'.$info['pattern'].'#s', $filedata)) {
  961. $info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.php';
  962. return $info;
  963. }
  964. }
  965. if (preg_match('#\.mp[123a]$#i', $filename)) {
  966. // Too many mp3 encoders on the market put gabage in front of mpeg files
  967. // use assume format on these if format detection failed
  968. $GetFileFormatArray = $this->GetFileFormatArray();
  969. $info = $GetFileFormatArray['mp3'];
  970. $info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.php';
  971. return $info;
  972. } elseif (preg_match('/\.cue$/i', $filename) && preg_match('#FILE "[^"]+" (BINARY|MOTOROLA|AIFF|WAVE|MP3)#', $filedata)) {
  973. // there's not really a useful consistent "magic" at the beginning of .cue files to identify them
  974. // so until I think of something better, just go by filename if all other format checks fail
  975. // and verify there's at least one instance of "TRACK xx AUDIO" in the file
  976. $GetFileFormatArray = $this->GetFileFormatArray();
  977. $info = $GetFileFormatArray['cue'];
  978. $info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.php';
  979. return $info;
  980. }
  981. return false;
  982. }
  983. // converts array to $encoding charset from $this->encoding
  984. public function CharConvert(&$array, $encoding) {
  985. // identical encoding - end here
  986. if ($encoding == $this->encoding) {
  987. return;
  988. }
  989. // loop thru array
  990. foreach ($array as $key => $value) {
  991. // go recursive
  992. if (is_array($value)) {
  993. $this->CharConvert($array[$key], $encoding);
  994. }
  995. // convert string
  996. elseif (is_string($value)) {
  997. $array[$key] = trim(getid3_lib::iconv_fallback($encoding, $this->encoding, $value));
  998. }
  999. }
  1000. }
  1001. public function HandleAllTags() {
  1002. // key name => array (tag name, character encoding)
  1003. static $tags;
  1004. if (empty($tags)) {
  1005. $tags = array(
  1006. 'asf' => array('asf' , 'UTF-16LE'),
  1007. 'midi' => array('midi' , 'ISO-8859-1'),
  1008. 'nsv' => array('nsv' , 'ISO-8859-1'),
  1009. 'ogg' => array('vorbiscomment' , 'UTF-8'),
  1010. 'png' => array('png' , 'UTF-8'),
  1011. 'tiff' => array('tiff' , 'ISO-8859-1'),
  1012. 'quicktime' => array('quicktime' , 'UTF-8'),
  1013. 'real' => array('real' , 'ISO-8859-1'),
  1014. 'vqf' => array('vqf' , 'ISO-8859-1'),
  1015. 'zip' => array('zip' , 'ISO-8859-1'),
  1016. 'riff' => array('riff' , 'ISO-8859-1'),
  1017. 'lyrics3' => array('lyrics3' , 'ISO-8859-1'),
  1018. 'id3v1' => array('id3v1' , $this->encoding_id3v1),
  1019. 'id3v2' => array('id3v2' , 'UTF-8'), // not according to the specs (every frame can have a different encoding), but getID3() force-converts all encodings to UTF-8
  1020. 'ape' => array('ape' , 'UTF-8'),
  1021. 'cue' => array('cue' , 'ISO-8859-1'),
  1022. 'matroska' => array('matroska' , 'UTF-8'),
  1023. 'flac' => array('vorbiscomment' , 'UTF-8'),
  1024. 'divxtag' => array('divx' , 'ISO-8859-1'),
  1025. 'iptc' => array('iptc' , 'ISO-8859-1'),
  1026. );
  1027. }
  1028. // loop through comments array
  1029. foreach ($tags as $comment_name => $tagname_encoding_array) {
  1030. list($tag_name, $encoding) = $tagname_encoding_array;
  1031. // fill in default encoding type if not already present
  1032. if (isset($this->info[$comment_name]) && !isset($this->info[$comment_name]['encoding'])) {
  1033. $this->info[$comment_name]['encoding'] = $encoding;
  1034. }
  1035. // copy comments if key name set
  1036. if (!empty($this->info[$comment_name]['comments'])) {
  1037. foreach ($this->info[$comment_name]['comments'] as $tag_key => $valuearray) {
  1038. foreach ($valuearray as $key => $value) {
  1039. if (is_string($value)) {
  1040. $value = trim($value, " \r\n\t"); // do not trim nulls from $value!! Unicode characters will get mangled if trailing nulls are removed!
  1041. }
  1042. if ($value) {
  1043. if (!is_numeric($key)) {
  1044. $this->info['tags'][trim($tag_name)][trim($tag_key)][$key] = $value;
  1045. } else {
  1046. $this->info['tags'][trim($tag_name)][trim($tag_key)][] = $value;
  1047. }
  1048. }
  1049. }
  1050. if ($tag_key == 'picture') {
  1051. unset($this->info[$comment_name]['comments'][$tag_key]);
  1052. }
  1053. }
  1054. if (!isset($this->info['tags'][$tag_name])) {
  1055. // comments are set but contain nothing but empty strings, so skip
  1056. continue;
  1057. }
  1058. if ($this->option_tags_html) {
  1059. foreach ($this->info['tags'][$tag_name] as $tag_key => $valuearray) {
  1060. $this->info['tags_html'][$tag_name][$tag_key] = getid3_lib::recursiveMultiByteCharString2HTML($valuearray, $encoding);
  1061. }
  1062. }
  1063. // ID3v1 encoding detection hack start
  1064. // ID3v1 is defined as always using ISO-8859-1 encoding, but it is not uncommon to find files tagged with ID3v1 using Windows-1251 or other character sets
  1065. // Since ID3v1 has no concept of character sets there is no certain way to know we have the correct non-ISO-8859-1 character set, but we can guess
  1066. if ($comment_name == 'id3v1') {
  1067. if ($encoding == 'ISO-8859-1') {
  1068. if (function_exists('iconv')) {
  1069. foreach ($this->info['tags'][$tag_name] as $tag_key => $valuearray) {
  1070. foreach ($valuearray as $key => $value) {
  1071. if (preg_match('#^[\\x80-\\xFF]+$#', $value)) {
  1072. foreach (array('windows-1251', 'KOI8-R') as $id3v1_bad_encoding) {
  1073. if (@iconv($id3v1_bad_encoding, $id3v1_bad_encoding, $value) === $value) {
  1074. $encoding = $id3v1_bad_encoding;
  1075. break 3;
  1076. }
  1077. }
  1078. }
  1079. }
  1080. }
  1081. }
  1082. }
  1083. }
  1084. // ID3v1 encoding detection hack end
  1085. $this->CharConvert($this->info['tags'][$tag_name], $encoding); // only copy gets converted!
  1086. }
  1087. }
  1088. // pictures can take up a lot of space, and we don't need multiple copies of them
  1089. // let there be a single copy in [comments][picture], and not elsewhere
  1090. if (!empty($this->info['tags'])) {
  1091. $unset_keys = array('tags', 'tags_html');
  1092. foreach ($this->info['tags'] as $tagtype => $tagarray) {
  1093. foreach ($tagarray as $tagname => $tagdata) {
  1094. if ($tagname == 'picture') {
  1095. foreach ($tagdata as $key => $tagarray) {
  1096. $this->info['comments']['picture'][] = $tagarray;
  1097. if (isset($tagarray['data']) && isset($tagarray['image_mime'])) {
  1098. if (isset($this->info['tags'][$tagtype][$tagname][$key])) {
  1099. unset($this->info['tags'][$tagtype][$tagname][$key]);
  1100. }
  1101. if (isset($this->info['tags_html'][$tagtype][$tagname][$key])) {
  1102. unset($this->info['tags_html'][$tagtype][$tagname][$key]);
  1103. }
  1104. }
  1105. }
  1106. }
  1107. }
  1108. foreach ($unset_keys as $unset_key) {
  1109. // remove possible empty keys from (e.g. [tags][id3v2][picture])
  1110. if (empty($this->info[$unset_key][$tagtype]['picture'])) {
  1111. unset($this->info[$unset_key][$tagtype]['picture']);
  1112. }
  1113. if (empty($this->info[$unset_key][$tagtype])) {
  1114. unset($this->info[$unset_key][$tagtype]);
  1115. }
  1116. if (empty($this->info[$unset_key])) {
  1117. unset($this->info[$unset_key]);
  1118. }
  1119. }
  1120. // remove duplicate copy of picture data from (e.g. [id3v2][comments][picture])
  1121. if (isset($this->info[$tagtype]['comments']['picture'])) {
  1122. unset($this->info[$tagtype]['comments']['picture']);
  1123. }
  1124. if (empty($this->info[$tagtype]['comments'])) {
  1125. unset($this->info[$tagtype]['comments']);
  1126. }
  1127. if (empty($this->info[$tagtype])) {
  1128. unset($this->info[$tagtype]);
  1129. }
  1130. }
  1131. }
  1132. return true;
  1133. }
  1134. public function getHashdata($algorithm) {
  1135. switch ($algorithm) {
  1136. case 'md5':
  1137. case 'sha1':
  1138. break;
  1139. default:
  1140. return $this->error('bad algorithm "'.$algorithm.'" in getHashdata()');
  1141. break;
  1142. }
  1143. if (!empty($this->info['fileformat']) && !empty($this->info['dataformat']) && ($this->info['fileformat'] == 'ogg') && ($this->info['audio']['dataformat'] == 'vorbis')) {
  1144. // We cannot get an identical md5_data value for Ogg files where the comments
  1145. // span more than 1 Ogg page (compared to the same audio data with smaller
  1146. // comments) using the normal getID3() method of MD5'ing the data between the
  1147. // end of the comments and the end of the file (minus any trailing tags),
  1148. // because the page sequence numbers of the pages that the audio data is on
  1149. // do not match. Under normal circumstances, where comments are smaller than
  1150. // the nominal 4-8kB page size, then this is not a problem, but if there are
  1151. // very large comments, the only way around it is to strip off the comment
  1152. // tags with vorbiscomment and MD5 that file.
  1153. // This procedure must be applied to ALL Ogg files, not just the ones with
  1154. // comments larger than 1 page, because the below method simply MD5's the
  1155. // whole file with the comments stripped, not just the portion after the
  1156. // comments block (which is the standard getID3() method.
  1157. // The above-mentioned problem of comments spanning multiple pages and changing
  1158. // page sequence numbers likely happens for OggSpeex and OggFLAC as well, but
  1159. // currently vorbiscomment only works on OggVorbis files.
  1160. if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
  1161. $this->warning('Failed making system call to vorbiscomment.exe - '.$algorithm.'_data is incorrect - error returned: PHP running in Safe Mode (backtick operator not available)');
  1162. $this->info[$algorithm.'_data'] = false;
  1163. } else {
  1164. // Prevent user from aborting script
  1165. $old_abort = ignore_user_abort(true);
  1166. // Create empty file
  1167. $empty = tempnam(GETID3_TEMP_DIR, 'getID3');
  1168. touch($empty);
  1169. // Use vorbiscomment to make temp file without comments
  1170. $temp = tempnam(GETID3_TEMP_DIR, 'getID3');
  1171. $file = $this->info['filenamepath'];
  1172. if (GETID3_OS_ISWINDOWS) {
  1173. if (file_exists(GETID3_HELPERAPPSDIR.'vorbiscomment.exe')) {
  1174. $commandline = '"'.GETID3_HELPERAPPSDIR.'vorbiscomment.exe" -w -c "'.$empty.'" "'.$file.'" "'.$temp.'"';
  1175. $VorbisCommentError = `$commandline`;
  1176. } else {
  1177. $VorbisCommentError = 'vorbiscomment.exe not found in '.GETID3_HELPERAPPSDIR;
  1178. }
  1179. } else {
  1180. $commandline = 'vorbiscomment -w -c "'.$empty.'" "'.$file.'" "'.$temp.'" 2>&1';
  1181. $commandline = 'vorbiscomment -w -c '.escapeshellarg($empty).' '.escapeshellarg($file).' '.escapeshellarg($temp).' 2>&1';
  1182. $VorbisCommentError = `$commandline`;
  1183. }
  1184. if (!empty($VorbisCommentError)) {
  1185. $this->info['warning'][] = 'Failed making system call to vorbiscomment(.exe) - '.$algorithm.'_data will be incorrect. If vorbiscomment is unavailable, please download from http://www.vorbis.com/download.psp and put in the getID3() directory. Error returned: '.$VorbisCommentError;
  1186. $this->info[$algorithm.'_data'] = false;
  1187. } else {
  1188. // Get hash of newly created file
  1189. switch ($algorithm) {
  1190. case 'md5':
  1191. $this->info[$algorithm.'_data'] = md5_file($temp);
  1192. break;
  1193. case 'sha1':
  1194. $this->info[$algorithm.'_data'] = sha1_file($temp);
  1195. break;
  1196. }
  1197. }
  1198. // Clean up
  1199. unlink($empty);
  1200. unlink($temp);
  1201. // Reset abort setting
  1202. ignore_user_abort($old_abort);
  1203. }
  1204. } else {
  1205. if (!empty($this->info['avdataoffset']) || (isset($this->info['avdataend']) && ($this->info['avdataend'] < $this->info['filesize']))) {
  1206. // get hash from part of file
  1207. $this->info[$algorithm.'_data'] = getid3_lib::hash_data($this->info['filenamepath'], $this->info['avdataoffset'], $this->info['avdataend'], $algorithm);
  1208. } else {
  1209. // get hash from whole file
  1210. switch ($algorithm) {
  1211. case 'md5':
  1212. $this->info[$algorithm.'_data'] = md5_file($this->info['filenamepath']);
  1213. break;
  1214. case 'sha1':
  1215. $this->info[$algorithm.'_data'] = sha1_file($this->info['filenamepath']);
  1216. break;
  1217. }
  1218. }
  1219. }
  1220. return true;
  1221. }
  1222. public function ChannelsBitratePlaytimeCalculations() {
  1223. // set channelmode on audio
  1224. if (!empty($this->info['audio']['channelmode']) || !isset($this->info['audio']['channels'])) {
  1225. // ignore
  1226. } elseif ($this->info['audio']['channels'] == 1) {
  1227. $this->info['audio']['channelmode'] = 'mono';
  1228. } elseif ($this->info['audio']['channels'] == 2) {
  1229. $this->info['audio']['channelmode'] = 'stereo';
  1230. }
  1231. // Calculate combined bitrate - audio + video
  1232. $CombinedBitrate = 0;
  1233. $CombinedBitrate += (isset($this->info['audio']['bitrate']) ? $this->info['audio']['bitrate'] : 0);
  1234. $CombinedBitrate += (isset($this->info['video']['bitrate']) ? $this->info['video']['bitrate'] : 0);
  1235. if (($CombinedBitrate > 0) && empty($this->info['bitrate'])) {
  1236. $this->info['bitrate'] = $CombinedBitrate;
  1237. }
  1238. //if ((isset($this->info['video']) && !isset($this->info['video']['bitrate'])) || (isset($this->info['audio']) && !isset($this->info['audio']['bitrate']))) {
  1239. // // for example, VBR MPEG video files cannot determine video bitrate:
  1240. // // should not set overall bitrate and playtime from audio bitrate only
  1241. // unset($this->info['bitrate']);
  1242. //}
  1243. // video bitrate undetermined, but calculable
  1244. if (isset($this->info['video']['dataformat']) && $this->info['video']['dataformat'] && (!isset($this->info['video']['bitrate']) || ($this->info['video']['bitrate'] == 0))) {
  1245. // if video bitrate not set
  1246. if (isset($this->info['audio']['bitrate']) && ($this->info['audio']['bitrate'] > 0) && ($this->info['audio']['bitrate'] == $this->info['bitrate'])) {
  1247. // AND if audio bitrate is set to same as overall bitrate
  1248. if (isset($this->info['playtime_seconds']) && ($this->info['playtime_seconds'] > 0)) {
  1249. // AND if playtime is set
  1250. if (isset($this->info['avdataend']) && isset($this->info['avdataoffset'])) {
  1251. // AND if AV data offset start/end is known
  1252. // THEN we can calculate the video bitrate
  1253. $this->info['bitrate'] = round((($this->info['avdataend'] - $this->info['avdataoffset']) * 8) / $this->info['playtime_seconds']);
  1254. $this->info['video']['bitrate'] = $this->info['bitrate'] - $this->info['audio']['bitrate'];
  1255. }
  1256. }
  1257. }
  1258. }
  1259. if ((!isset($this->info['playtime_seconds']) || ($this->info['playtime_seconds'] <= 0)) && !empty($this->info['bitrate'])) {
  1260. $this->info['playtime_seconds'] = (($this->info['avdataend'] - $this->info['avdataoffset']) * 8) / $this->info['bitrate'];
  1261. }
  1262. if (!isset($this->info['bitrate']) && !empty($this->info['playtime_seconds'])) {
  1263. $this->info['bitrate'] = (($this->info['avdataend'] - $this->info['avdataoffset']) * 8) / $this->info['playtime_seconds'];
  1264. }
  1265. if (isset($this->info['bitrate']) && empty($this->info['audio']['bitrate']) && empty($this->info['video']['bitrate'])) {
  1266. if (isset($this->info['audio']['dataformat']) && empty($this->info['video']['resolution_x'])) {
  1267. // audio only
  1268. $this->info['audio']['bitrate'] = $this->info['bitrate'];
  1269. } elseif (isset($this->info['video']['resolution_x']) && empty($this->info['audio']['dataformat'])) {
  1270. // video only
  1271. $this->info['video']['bitrate'] = $this->info['bitrate'];
  1272. }
  1273. }
  1274. // Set playtime string
  1275. if (!empty($this->info['playtime_seconds']) && empty($this->info['playtime_string'])) {
  1276. $this->info['playtime_string'] = getid3_lib::PlaytimeString($this->info['playtime_seconds']);
  1277. }
  1278. }
  1279. public function CalculateCompressionRatioVideo() {
  1280. if (empty($this->info['video'])) {
  1281. return false;
  1282. }
  1283. if (empty($this->info['video']['resolution_x']) || empty($this->info['video']['resolution_y'])) {
  1284. return false;
  1285. }
  1286. if (empty($this->info['video']['bits_per_sample'])) {
  1287. return false;
  1288. }
  1289. switch ($this->info['video']['dataformat']) {
  1290. case 'bmp':
  1291. case 'gif':
  1292. case 'jpeg':
  1293. case 'jpg':
  1294. case 'png':
  1295. case 'tiff':
  1296. $FrameRate = 1;
  1297. $PlaytimeSeconds = 1;
  1298. $BitrateCompressed = $this->info['filesize'] * 8;
  1299. break;
  1300. default:
  1301. if (!empty($this->info['video']['frame_rate'])) {
  1302. $FrameRate = $this->info['video']['frame_rate'];
  1303. } else {
  1304. return false;
  1305. }
  1306. if (!empty($this->info['playtime_seconds'])) {
  1307. $PlaytimeSeconds = $this->info['playtime_seconds'];
  1308. } else {
  1309. return false;
  1310. }
  1311. if (!empty($this->info['video']['bitrate'])) {
  1312. $BitrateCompressed = $this->info['video']['bitrate'];
  1313. } else {
  1314. return false;
  1315. }
  1316. break;
  1317. }
  1318. $BitrateUncompressed = $this->info['video']['resolution_x'] * $this->info['video']['resolution_y'] * $this->info['video']['bits_per_sample'] * $FrameRate;
  1319. $this->info['video']['compression_ratio'] = $BitrateCompressed / $BitrateUncompressed;
  1320. return true;
  1321. }
  1322. public function CalculateCompressionRatioAudio() {
  1323. if (empty($this->info['audio']['bitrate']) || empty($this->info['audio']['channels']) || empty($this->info['audio']['sample_rate']) || !is_numeric($this->info['audio']['sample_rate'])) {
  1324. return false;
  1325. }
  1326. $this->info['audio']['compression_ratio'] = $this->info['audio']['bitrate'] / ($this->info['audio']['channels'] * $this->info['audio']['sample_rate'] * (!empty($this->info['audio']['bits_per_sample']) ? $this->info['audio']['bits_per_sample'] : 16));
  1327. if (!empty($this->info['audio']['streams'])) {
  1328. foreach ($this->info['audio']['streams'] as $streamnumber => $streamdata) {
  1329. if (!empty($streamdata['bitrate']) && !empty($streamdata['channels']) && !empty($streamdata['sample_rate'])) {
  1330. $this->info['audio']['streams'][$streamnumber]['compression_ratio'] = $streamdata['bitrate'] / ($streamdata['channels'] * $streamdata['sample_rate'] * (!empty($streamdata['bits_per_sample']) ? $streamdata['bits_per_sample'] : 16));
  1331. }
  1332. }
  1333. }
  1334. return true;
  1335. }
  1336. public function CalculateReplayGain() {
  1337. if (isset($this->info['replay_gain'])) {
  1338. if (!isset($this->info['replay_gain']['reference_volume'])) {
  1339. $this->info['replay_gain']['reference_volume'] = (double) 89.0;
  1340. }
  1341. if (isset($this->info['replay_gain']['track']['adjustment'])) {
  1342. $this->info['replay_gain']['track']['volume'] = $this->info['replay_gain']['reference_volume'] - $this->info['replay_gain']['track']['adjustment'];
  1343. }
  1344. if (isset($this->info['replay_gain']['album']['adjustment'])) {
  1345. $this->info['replay_gain']['album']['volume'] = $this->info['replay_gain']['reference_volume'] - $this->info['replay_gain']['album']['adjustment'];
  1346. }
  1347. if (isset($this->info['replay_gain']['track']['peak'])) {
  1348. $this->info['replay_gain']['track']['max_noclip_gain'] = 0 - getid3_lib::RGADamplitude2dB($this->info['replay_gain']['track']['peak']);
  1349. }
  1350. if (isset($this->info['replay_gain']['album']['peak'])) {
  1351. $this->info['replay_gain']['album']['max_noclip_gain'] = 0 - getid3_lib::RGADamplitude2dB($this->info['replay_gain']['album']['peak']);
  1352. }
  1353. }
  1354. return true;
  1355. }
  1356. public function ProcessAudioStreams() {
  1357. if (!empty($this->info['audio']['bitrate']) || !empty($this->info['audio']['channels']) || !empty($this->info['audio']['sample_rate'])) {
  1358. if (!isset($this->info['audio']['streams'])) {
  1359. foreach ($this->info['audio'] as $key => $value) {
  1360. if ($key != 'streams') {
  1361. $this->info['audio']['streams'][0][$key] = $value;
  1362. }
  1363. }
  1364. }
  1365. }
  1366. return true;
  1367. }
  1368. public function getid3_tempnam() {
  1369. return tempnam($this->tempdir, 'gI3');
  1370. }
  1371. public function include_module($name) {
  1372. //if (!file_exists($this->include_path.'module.'.$name.'.php')) {
  1373. if (!file_exists(GETID3_INCLUDEPATH.'module.'.$name.'.php')) {
  1374. throw new getid3_exception('Required module.'.$name.'.php is missing.');
  1375. }
  1376. include_once(GETID3_INCLUDEPATH.'module.'.$name.'.php');
  1377. return true;
  1378. }
  1379. }
  1380. abstract class getid3_handler {
  1381. /**
  1382. * @var getID3
  1383. */
  1384. protected $getid3; // pointer
  1385. protected $data_string_flag = false; // analyzing filepointer or string
  1386. protected $data_string = ''; // string to analyze
  1387. protected $data_string_position = 0; // seek position in string
  1388. protected $data_string_length = 0; // string length
  1389. private $dependency_to = null;
  1390. public function __construct(getID3 $getid3, $call_module=null) {
  1391. $this->getid3 = $getid3;
  1392. if ($call_module) {
  1393. $this->dependency_to = str_replace('getid3_', '', $call_module);
  1394. }
  1395. }
  1396. // Analyze from file pointer
  1397. abstract public function Analyze();
  1398. // Analyze from string instead
  1399. public function AnalyzeString($string) {
  1400. // Enter string mode
  1401. $this->setStringMode($string);
  1402. // Save info
  1403. $saved_avdataoffset = $this->getid3->info['avdataoffset'];
  1404. $saved_avdataend = $this->getid3->info['avdataend'];
  1405. $saved_filesize = (isset($this->getid3->info['filesize']) ? $this->getid3->info['filesize'] : null); // may be not set if called as dependency without openfile() call
  1406. // Reset some info
  1407. $this->getid3->info['avdataoffset'] = 0;
  1408. $this->getid3->info['avdataend'] = $this->getid3->info['filesize'] = $this->data_string_length;
  1409. // Analyze
  1410. $this->Analyze();
  1411. // Restore some info
  1412. $this->getid3->info['avdataoffset'] = $saved_avdataoffset;
  1413. $this->getid3->info['avdataend'] = $saved_avdataend;
  1414. $this->getid3->info['filesize'] = $saved_filesize;
  1415. // Exit string mode
  1416. $this->data_string_flag = false;
  1417. }
  1418. public function setStringMode($string) {
  1419. $this->data_string_flag = true;
  1420. $this->data_string = $string;
  1421. $this->data_string_length = strlen($string);
  1422. }
  1423. protected function ftell() {
  1424. if ($this->data_string_flag) {
  1425. return $this->data_string_position;
  1426. }
  1427. return ftell($this->getid3->fp);
  1428. }
  1429. protected function fread($bytes) {
  1430. if ($this->data_string_flag) {
  1431. $this->data_string_position += $bytes;
  1432. return substr($this->data_string, $this->data_string_position - $bytes, $bytes);
  1433. }
  1434. $pos = $this->ftell() + $bytes;
  1435. if (!getid3_lib::intValueSupported($pos)) {
  1436. throw new getid3_exception('cannot fread('.$bytes.' from '.$this->ftell().') because beyond PHP filesystem limit', 10);
  1437. }
  1438. return fread($this->getid3->fp, $bytes);
  1439. }
  1440. protected function fseek($bytes, $whence=SEEK_SET) {
  1441. if ($this->data_string_flag) {
  1442. switch ($whence) {
  1443. case SEEK_SET:
  1444. $this->data_string_position = $bytes;
  1445. break;
  1446. case SEEK_CUR:
  1447. $this->data_string_position += $bytes;
  1448. break;
  1449. case SEEK_END:
  1450. $this->data_string_position = $this->data_string_length + $bytes;
  1451. break;
  1452. }
  1453. return 0;
  1454. } else {
  1455. $pos = $bytes;
  1456. if ($whence == SEEK_CUR) {
  1457. $pos = $this->ftell() + $bytes;
  1458. } elseif ($whence == SEEK_END) {
  1459. $pos = $this->getid3->info['filesize'] + $bytes;
  1460. }
  1461. if (!getid3_lib::intValueSupported($pos)) {
  1462. throw new getid3_exception('cannot fseek('.$pos.') because beyond PHP filesystem limit', 10);
  1463. }
  1464. }
  1465. return fseek($this->getid3->fp, $bytes, $whence);
  1466. }
  1467. protected function feof() {
  1468. if ($this->data_string_flag) {
  1469. return $this->data_string_position >= $this->data_string_length;
  1470. }
  1471. return feof($this->getid3->fp);
  1472. }
  1473. final protected function isDependencyFor($module) {
  1474. return $this->dependency_to == $module;
  1475. }
  1476. protected function error($text) {
  1477. $this->getid3->info['error'][] = $text;
  1478. return false;
  1479. }
  1480. protected function warning($text) {
  1481. return $this->getid3->warning($text);
  1482. }
  1483. protected function notice($text) {
  1484. // does nothing for now
  1485. }
  1486. public function saveAttachment($name, $offset, $length, $image_mime=null) {
  1487. try {
  1488. // do not extract at all
  1489. if ($this->getid3->option_save_attachments === getID3::ATTACHMENTS_NONE) {
  1490. $attachment = null; // do not set any
  1491. // extract to return array
  1492. } elseif ($this->getid3->option_save_attachments === getID3::ATTACHMENTS_INLINE) {
  1493. $this->fseek($offset);
  1494. $attachment = $this->fread($length); // get whole data in one pass, till it is anyway stored in memory
  1495. if ($attachment === false || strlen($attachment) != $length) {
  1496. throw new Exception('failed to read attachment data');
  1497. }
  1498. // assume directory path is given
  1499. } else {
  1500. // set up destination path
  1501. $dir = rtrim(str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $this->getid3->option_save_attachments), DIRECTORY_SEPARATOR);
  1502. if (!is_dir($dir) || !is_writable($dir)) { // check supplied directory
  1503. throw new Exception('supplied path ('.$dir.') does not exist, or is not writable');
  1504. }
  1505. $dest = $dir.DIRECTORY_SEPARATOR.$name.($image_mime ? '.'.getid3_lib::ImageExtFromMime($image_mime) : '');
  1506. // create dest file
  1507. if (($fp_dest = fopen($dest, 'wb')) == false) {
  1508. throw new Exception('failed to create file '.$dest);
  1509. }
  1510. // copy data
  1511. $this->fseek($offset);
  1512. $buffersize = ($this->data_string_flag ? $length : $this->getid3->fread_buffer_size());
  1513. $bytesleft = $length;
  1514. while ($bytesleft > 0) {
  1515. if (($buffer = $this->fread(min($buffersize, $bytesleft))) === false || ($byteswritten = fwrite($fp_dest, $buffer)) === false || ($byteswritten === 0)) {
  1516. throw new Exception($buffer === false ? 'not enough data to read' : 'failed to write to destination file, may be not enough disk space');
  1517. }
  1518. $bytesleft -= $byteswritten;
  1519. }
  1520. fclose($fp_dest);
  1521. $attachment = $dest;
  1522. }
  1523. } catch (Exception $e) {
  1524. // close and remove dest file if created
  1525. if (isset($fp_dest) && is_resource($fp_dest)) {
  1526. fclose($fp_dest);
  1527. unlink($dest);
  1528. }
  1529. // do not set any is case of error
  1530. $attachment = null;
  1531. $this->warning('Failed to extract attachment '.$name.': '.$e->getMessage());
  1532. }
  1533. // seek to the end of attachment
  1534. $this->fseek($offset + $length);
  1535. return $attachment;
  1536. }
  1537. }
  1538. class getid3_exception extends Exception
  1539. {
  1540. public $message;
  1541. }