getid3.php 71 KB

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