PageRenderTime 63ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/includes/js/mootools-filemanager/Assets/Connector/Assets/getid3/getid3.php

https://github.com/KenBoyer/CompactCMS
PHP | 1392 lines | 949 code | 218 blank | 225 comment | 171 complexity | 15356b2d920335e391d9f5bccc15000b MD5 | raw file
  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. /////////////////////////////////////////////////////////////////
  7. // //
  8. // Please see readme.txt for more information //
  9. // ///
  10. /////////////////////////////////////////////////////////////////
  11. // Defines
  12. define('GETID3_VERSION', '1.8.5-20110218');
  13. define('GETID3_FREAD_BUFFER_SIZE', 16384); // read buffer size in bytes
  14. // attempt to define temp dir as something flexible but reliable
  15. $temp_dir = ini_get('upload_tmp_dir');
  16. if ($temp_dir && (!is_dir($temp_dir) || !is_readable($temp_dir))) {
  17. $temp_dir = '';
  18. }
  19. if (!$temp_dir && function_exists('sys_get_temp_dir')) {
  20. // PHP v5.2.1+
  21. // sys_get_temp_dir() may give inaccessible temp dir, e.g. with open_basedir on virtual hosts
  22. $temp_dir = sys_get_temp_dir();
  23. }
  24. $temp_dir = realpath($temp_dir);
  25. $open_basedir = ini_get('open_basedir');
  26. if ($open_basedir) {
  27. // e.g. "/var/www/vhosts/getid3.org/httpdocs/:/tmp/"
  28. $temp_dir = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $temp_dir);
  29. $open_basedir = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $open_basedir);
  30. if (substr($temp_dir, -1, 1) != DIRECTORY_SEPARATOR) {
  31. $temp_dir .= DIRECTORY_SEPARATOR;
  32. }
  33. $found_valid_tempdir = false;
  34. $open_basedirs = explode(':', $open_basedir);
  35. foreach ($open_basedirs as $basedir) {
  36. if (substr($basedir, -1, 1) != DIRECTORY_SEPARATOR) {
  37. $basedir .= DIRECTORY_SEPARATOR;
  38. }
  39. if (preg_match('#^'.preg_quote($basedir).'#', $temp_dir)) {
  40. $found_valid_tempdir = true;
  41. break;
  42. }
  43. }
  44. if (!$found_valid_tempdir) {
  45. $temp_dir = '';
  46. }
  47. unset($open_basedirs, $found_valid_tempdir, $basedir);
  48. }
  49. if (!$temp_dir) {
  50. $temp_dir = '*'; // invalid directory name should force tempnam() to use system default temp dir
  51. }
  52. // $temp_dir = '/something/else/'; // feel free to override temp dir here if it works better for your system
  53. define('GETID3_TEMP_DIR', $temp_dir);
  54. unset($open_basedir, $temp_dir);
  55. // define a constant rather than looking up every time it is needed
  56. if (!defined('GETID3_OS_ISWINDOWS')) {
  57. if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
  58. define('GETID3_OS_ISWINDOWS', true);
  59. } else {
  60. define('GETID3_OS_ISWINDOWS', false);
  61. }
  62. }
  63. // Get base path of getID3() - ONCE
  64. if (!defined('GETID3_INCLUDEPATH')) {
  65. foreach (get_included_files() as $key => $val) {
  66. if (basename($val) == 'getid3.php') {
  67. define('GETID3_INCLUDEPATH', dirname($val).DIRECTORY_SEPARATOR);
  68. break;
  69. }
  70. }
  71. }
  72. // End: Defines
  73. class getID3
  74. {
  75. // public: Settings
  76. var $encoding = 'ISO-8859-1'; // CASE SENSITIVE! - i.e. (must be supported by iconv())
  77. // Examples: ISO-8859-1 UTF-8 UTF-16 UTF-16BE
  78. var $encoding_id3v1 = 'ISO-8859-1'; // Should always be 'ISO-8859-1', but some tags may be written in other encodings such as 'EUC-CN'
  79. var $tempdir = GETID3_TEMP_DIR;
  80. // public: Optional tag checks - disable for speed.
  81. var $option_tag_id3v1 = true; // Read and process ID3v1 tags
  82. var $option_tag_id3v2 = true; // Read and process ID3v2 tags
  83. var $option_tag_lyrics3 = true; // Read and process Lyrics3 tags
  84. var $option_tag_apetag = true; // Read and process APE tags
  85. var $option_tags_process = true; // Copy tags to root key 'tags' and encode to $this->encoding
  86. var $option_tags_html = true; // Copy tags to root key 'tags_html' properly translated from various encodings to HTML entities
  87. // public: Optional tag/comment calucations
  88. var $option_extra_info = true; // Calculate additional info such as bitrate, channelmode etc
  89. // public: Optional calculations
  90. var $option_md5_data = false; // Get MD5 sum of data part - slow
  91. var $option_md5_data_source = false; // Use MD5 of source file if availble - only FLAC and OptimFROG
  92. var $option_sha1_data = false; // Get SHA1 sum of data part - slow
  93. var $option_max_2gb_check = null; // Check whether file is larger than 2GB and thus not supported by 32-bit PHP
  94. // private
  95. var $filename = '';
  96. var $startup_error = '';
  97. var $startup_warning = '';
  98. // public: constructor
  99. function getID3() {
  100. // Check for PHP version
  101. $required_php_version = '5.0.5';
  102. if (!function_exists('version_compare') || version_compare(PHP_VERSION, $required_php_version, '<')) {
  103. // version_compare not available before PHP v4.1.0, so don't use for first version checking
  104. $this->startup_error .= 'getID3() requires PHP v'.$required_php_version.' or higher - you are running v'.PHP_VERSION;
  105. return false;
  106. }
  107. // Check memory
  108. $memory_limit = ini_get('memory_limit');
  109. if (preg_match('#([0-9]+)M#i', $memory_limit, $matches)) {
  110. // could be stored as "16M" rather than 16777216 for example
  111. $memory_limit = $matches[1] * 1048576;
  112. } elseif (preg_match('#([0-9]+)G#i', $memory_limit, $matches)) { // The 'G' modifier is available since PHP 5.1.0
  113. // could be stored as "2G" rather than 2147483648 for example
  114. $memory_limit = $matches[1] * 1073741824;
  115. }
  116. if ($memory_limit <= 0) {
  117. // memory limits probably disabled
  118. } elseif ($memory_limit <= 4194304) {
  119. $this->startup_error .= 'PHP has less than 4MB available memory and will very likely run out. Increase memory_limit in php.ini';
  120. } elseif ($memory_limit <= 12582912) {
  121. $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';
  122. }
  123. // Check safe_mode off
  124. if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
  125. $this->warning('WARNING: Safe mode is on, shorten support disabled, md5data/sha1data for ogg vorbis disabled, ogg vorbos/flac tag writing disabled.');
  126. }
  127. if (intval(ini_get('mbstring.func_overload')) > 0) {
  128. $this->warning('WARNING: php.ini contains "mbstring.func_overload = '.ini_get('mbstring.func_overload').'", this may break things.');
  129. }
  130. /*
  131. // Check timezone config setting
  132. // this is needed to prevent E_STRICT warnings with any time/date functions
  133. if (!ini_get('date.timezone')) {
  134. if (function_exists('date_default_timezone_set')) { // exists since PHP v5.1.0
  135. $this->warning('php.ini should have "date.timezone" set, but it does not. Setting timezone to "'.date_default_timezone_get().'"');
  136. date_default_timezone_set(date_default_timezone_get());
  137. } else {
  138. $this->warning('php.ini should have "date.timezone" set, but it does not.');
  139. }
  140. }
  141. */
  142. // Check for magic_quotes_runtime
  143. if (function_exists('get_magic_quotes_runtime')) {
  144. if (get_magic_quotes_runtime()) {
  145. 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).');
  146. }
  147. }
  148. // Check for magic_quotes_gpc
  149. if (function_exists('magic_quotes_gpc')) {
  150. if (get_magic_quotes_gpc()) {
  151. 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).');
  152. }
  153. }
  154. // Load support library
  155. if (!include_once(GETID3_INCLUDEPATH.'getid3.lib.php')) {
  156. $this->startup_error .= 'getid3.lib.php is missing or corrupt';
  157. }
  158. if ($this->option_max_2gb_check === null) {
  159. $this->option_max_2gb_check = (PHP_INT_MAX <= 2147483647);
  160. }
  161. // Needed for Windows only:
  162. // Define locations of helper applications for Shorten, VorbisComment, MetaFLAC
  163. // as well as other helper functions such as head, tail, md5sum, etc
  164. // This path cannot contain spaces, but the below code will attempt to get the
  165. // 8.3-equivalent path automatically
  166. // IMPORTANT: This path must include the trailing slash
  167. if (GETID3_OS_ISWINDOWS && !defined('GETID3_HELPERAPPSDIR')) {
  168. $helperappsdir = GETID3_INCLUDEPATH.'..'.DIRECTORY_SEPARATOR.'helperapps'; // must not have any space in this path
  169. if (!is_dir($helperappsdir)) {
  170. $this->startup_error .= '"'.$helperappsdir.'" cannot be defined as GETID3_HELPERAPPSDIR because it does not exist';
  171. } elseif (strpos(realpath($helperappsdir), ' ') !== false) {
  172. $DirPieces = explode(DIRECTORY_SEPARATOR, realpath($helperappsdir));
  173. $path_so_far = array();
  174. foreach ($DirPieces as $key => $value) {
  175. if (strpos($value, ' ') !== false) {
  176. if (!empty($path_so_far)) {
  177. $commandline = 'dir /x '.escapeshellarg(implode(DIRECTORY_SEPARATOR, $path_so_far));
  178. $dir_listing = `$commandline`;
  179. $lines = explode("\n", $dir_listing);
  180. foreach ($lines as $line) {
  181. $line = trim($line);
  182. if (preg_match('#^([0-9/]{10}) +([0-9:]{4,5}( [AP]M)?) +(<DIR>|[0-9,]+) +([^ ]{0,11}) +(.+)$#', $line, $matches)) {
  183. list($dummy, $date, $time, $ampm, $filesize, $shortname, $filename) = $matches;
  184. if ((strtoupper($filesize) == '<DIR>') && (strtolower($filename) == strtolower($value))) {
  185. $value = $shortname;
  186. }
  187. }
  188. }
  189. } else {
  190. $this->startup_error .= '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.';
  191. }
  192. }
  193. $path_so_far[] = $value;
  194. }
  195. $helperappsdir = implode(DIRECTORY_SEPARATOR, $path_so_far);
  196. }
  197. define('GETID3_HELPERAPPSDIR', $helperappsdir.DIRECTORY_SEPARATOR);
  198. }
  199. }
  200. // public: setOption
  201. function setOption($optArray) {
  202. if (!is_array($optArray) || empty($optArray)) {
  203. return false;
  204. }
  205. foreach ($optArray as $opt => $val) {
  206. //if (isset($this, $opt) === false) {
  207. if (isset($this->$opt) === false) {
  208. continue;
  209. }
  210. $this->$opt = $val;
  211. }
  212. return true;
  213. }
  214. // public: analyze file
  215. function analyze($filename) {
  216. try {
  217. if (!empty($this->startup_error)) {
  218. return $this->error($this->startup_error);
  219. }
  220. if (!empty($this->startup_warning)) {
  221. $this->warning($this->startup_warning);
  222. }
  223. // init result array and set parameters
  224. $this->info = array();
  225. $this->info['GETID3_VERSION'] = GETID3_VERSION;
  226. // Check encoding/iconv support
  227. if (!function_exists('iconv') && !in_array($this->encoding, array('ISO-8859-1', 'UTF-8', 'UTF-16LE', 'UTF-16BE', 'UTF-16'))) {
  228. $errormessage = 'iconv() support is needed for encodings other than ISO-8859-1, UTF-8, UTF-16LE, UTF16-BE, UTF-16. ';
  229. if (GETID3_OS_ISWINDOWS) {
  230. $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';
  231. } else {
  232. $errormessage .= 'PHP is not compiled with iconv() support. Please recompile with the --with-iconv switch';
  233. }
  234. return $this->error($errormessage);
  235. }
  236. // remote files not supported
  237. if (preg_match('/^(ht|f)tp:\/\//', $filename)) {
  238. return $this->error('Remote files are not supported in this version of getID3() - please copy the file locally first');
  239. }
  240. $filename = str_replace('/', DIRECTORY_SEPARATOR, $filename);
  241. $filename = preg_replace('#(.+)'.preg_quote(DIRECTORY_SEPARATOR).'{2,}#U', '\1'.DIRECTORY_SEPARATOR, $filename);
  242. // open local file
  243. if (file_exists($filename) && is_file($filename)) {
  244. ob_start();
  245. if ($fp = fopen($filename, 'rb')) {
  246. // great
  247. ob_end_clean();
  248. } else {
  249. $fopen_error = ob_get_contents();
  250. ob_end_clean();
  251. return $this->error('Could not open file "'.$filename.'" (fopen says: '.$fopen_error.')');
  252. }
  253. } else {
  254. return $this->error('Could not open "'.$filename.'" (does not exist, or is not a file)');
  255. }
  256. // set parameters
  257. $this->info['filesize'] = filesize($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($fp, 0, SEEK_END);
  264. if (($fseek < 0) || (($this->info['filesize'] != 0) && (ftell($fp) == 0)) ||
  265. ($this->info['filesize'] < 0) ||
  266. (ftell($fp) < 0)) {
  267. $real_filesize = false;
  268. if (GETID3_OS_ISWINDOWS) {
  269. $commandline = 'dir /-C "'.str_replace('/', DIRECTORY_SEPARATOR, $filename).'"';
  270. $dir_output = `$commandline`;
  271. if (preg_match('#1 File\(s\)[ ]+([0-9]+) bytes#i', $dir_output, $matches)) {
  272. $real_filesize = (float) $matches[1];
  273. }
  274. } else {
  275. $commandline = 'ls -o -g -G --time-style=long-iso '.escapeshellarg($filename);
  276. $dir_output = `$commandline`;
  277. if (preg_match('#([0-9]+) ([0-9]{4}-[0-9]{2}\-[0-9]{2} [0-9]{2}:[0-9]{2}) '.str_replace('#', '\\#', preg_quote($filename)).'$#', $dir_output, $matches)) {
  278. $real_filesize = (float) $matches[1];
  279. }
  280. }
  281. if ($real_filesize === false) {
  282. unset($this->info['filesize']);
  283. fclose($fp);
  284. return $this->error('Unable to determine actual filesize. File is most likely larger than '.round(PHP_INT_MAX / 1073741824).'GB and is not supported by PHP.');
  285. } elseif (getid3_lib::intValueSupported($real_filesize)) {
  286. unset($this->info['filesize']);
  287. fclose($fp);
  288. return $this->error('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');
  289. }
  290. $this->info['filesize'] = $real_filesize;
  291. $this->error('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.');
  292. }
  293. }
  294. // set more parameters
  295. $this->info['avdataoffset'] = 0;
  296. $this->info['avdataend'] = $this->info['filesize'];
  297. $this->info['fileformat'] = ''; // filled in later
  298. $this->info['audio']['dataformat'] = ''; // filled in later, unset if not used
  299. $this->info['video']['dataformat'] = ''; // filled in later, unset if not used
  300. $this->info['tags'] = array(); // filled in later, unset if not used
  301. $this->info['error'] = array(); // filled in later, unset if not used
  302. $this->info['warning'] = array(); // filled in later, unset if not used
  303. $this->info['comments'] = array(); // filled in later, unset if not used
  304. $this->info['encoding'] = $this->encoding; // required by id3v2 and iso modules - can be unset at the end if desired
  305. // set redundant parameters - might be needed in some include file
  306. $this->info['filename'] = basename($filename);
  307. $this->info['filepath'] = str_replace('\\', '/', realpath(dirname($filename)));
  308. $this->info['filenamepath'] = $this->info['filepath'].'/'.$this->info['filename'];
  309. // handle ID3v2 tag - done first - already at beginning of file
  310. // ID3v2 detection (even if not parsing) is always done otherwise fileformat is much harder to detect
  311. if ($this->option_tag_id3v2) {
  312. $GETID3_ERRORARRAY = &$this->info['warning'];
  313. if (getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.id3v2.php', __FILE__, false)) {
  314. $tag = new getid3_id3v2($fp, $this->info);
  315. unset($tag);
  316. }
  317. } else {
  318. fseek($fp, 0, SEEK_SET);
  319. $header = fread($fp, 10);
  320. if ((substr($header, 0, 3) == 'ID3') && (strlen($header) == 10)) {
  321. $this->info['id3v2']['header'] = true;
  322. $this->info['id3v2']['majorversion'] = ord($header{3});
  323. $this->info['id3v2']['minorversion'] = ord($header{4});
  324. $this->info['id3v2']['headerlength'] = getid3_lib::BigEndian2Int(substr($header, 6, 4), 1) + 10; // length of ID3v2 tag in 10-byte header doesn't include 10-byte header length
  325. $this->info['id3v2']['tag_offset_start'] = 0;
  326. $this->info['id3v2']['tag_offset_end'] = $this->info['id3v2']['tag_offset_start'] + $this->info['id3v2']['headerlength'];
  327. $this->info['avdataoffset'] = $this->info['id3v2']['tag_offset_end'];
  328. }
  329. }
  330. // handle ID3v1 tag
  331. if ($this->option_tag_id3v1) {
  332. if (!file_exists(GETID3_INCLUDEPATH.'module.tag.id3v1.php') || !include_once(GETID3_INCLUDEPATH.'module.tag.id3v1.php')) {
  333. return $this->error('module.tag.id3v1.php is missing - you may disable option_tag_id3v1.');
  334. }
  335. $tag = new getid3_id3v1($fp, $this->info);
  336. unset($tag);
  337. }
  338. // handle APE tag
  339. if ($this->option_tag_apetag) {
  340. if (!file_exists(GETID3_INCLUDEPATH.'module.tag.apetag.php') || !include_once(GETID3_INCLUDEPATH.'module.tag.apetag.php')) {
  341. return $this->error('module.tag.apetag.php is missing - you may disable option_tag_apetag.');
  342. }
  343. $tag = new getid3_apetag($fp, $this->info);
  344. unset($tag);
  345. }
  346. // handle lyrics3 tag
  347. if ($this->option_tag_lyrics3) {
  348. if (!file_exists(GETID3_INCLUDEPATH.'module.tag.lyrics3.php') || !include_once(GETID3_INCLUDEPATH.'module.tag.lyrics3.php')) {
  349. return $this->error('module.tag.lyrics3.php is missing - you may disable option_tag_lyrics3.');
  350. }
  351. $tag = new getid3_lyrics3($fp, $this->info);
  352. unset($tag);
  353. }
  354. // read 32 kb file data
  355. fseek($fp, $this->info['avdataoffset'], SEEK_SET);
  356. $formattest = fread($fp, 32774);
  357. // determine format
  358. $determined_format = $this->GetFileFormat($formattest, $filename);
  359. // unable to determine file format
  360. if (!$determined_format) {
  361. fclose($fp);
  362. return $this->error('unable to determine file format');
  363. }
  364. // check for illegal ID3 tags
  365. if (isset($determined_format['fail_id3']) && (in_array('id3v1', $this->info['tags']) || in_array('id3v2', $this->info['tags']))) {
  366. if ($determined_format['fail_id3'] === 'ERROR') {
  367. fclose($fp);
  368. return $this->error('ID3 tags not allowed on this file type.');
  369. } elseif ($determined_format['fail_id3'] === 'WARNING') {
  370. $this->info['warning'][] = 'ID3 tags not allowed on this file type.';
  371. }
  372. }
  373. // check for illegal APE tags
  374. if (isset($determined_format['fail_ape']) && in_array('ape', $this->info['tags'])) {
  375. if ($determined_format['fail_ape'] === 'ERROR') {
  376. fclose($fp);
  377. return $this->error('APE tags not allowed on this file type.');
  378. } elseif ($determined_format['fail_ape'] === 'WARNING') {
  379. $this->info['warning'][] = 'APE tags not allowed on this file type.';
  380. }
  381. }
  382. // set mime type
  383. $this->info['mime_type'] = $determined_format['mime_type'];
  384. // supported format signature pattern detected, but module deleted
  385. if (!file_exists(GETID3_INCLUDEPATH.$determined_format['include'])) {
  386. fclose($fp);
  387. return $this->error('Format not supported, module "'.$determined_format['include'].'" was removed.');
  388. }
  389. // module requires iconv support
  390. if (!function_exists('iconv') && !empty($determined_format['iconv_req'])) {
  391. return $this->error('iconv support is required for this module ('.$determined_format['include'].').');
  392. }
  393. // include module
  394. include_once(GETID3_INCLUDEPATH.$determined_format['include']);
  395. // instantiate module class
  396. $class_name = 'getid3_'.$determined_format['module'];
  397. if (!class_exists($class_name)) {
  398. return $this->error('Format not supported, module "'.$determined_format['include'].'" is corrupt.');
  399. }
  400. if (isset($determined_format['option'])) {
  401. $class = new $class_name($fp, $this->info, $determined_format['option']);
  402. } else {
  403. $class = new $class_name($fp, $this->info);
  404. }
  405. unset($class);
  406. // close file
  407. fclose($fp);
  408. // process all tags - copy to 'tags' and convert charsets
  409. if ($this->option_tags_process) {
  410. $this->HandleAllTags();
  411. }
  412. // perform more calculations
  413. if ($this->option_extra_info) {
  414. $this->ChannelsBitratePlaytimeCalculations();
  415. $this->CalculateCompressionRatioVideo();
  416. $this->CalculateCompressionRatioAudio();
  417. $this->CalculateReplayGain();
  418. $this->ProcessAudioStreams();
  419. }
  420. // get the MD5 sum of the audio/video portion of the file - without ID3/APE/Lyrics3/etc header/footer tags
  421. if ($this->option_md5_data) {
  422. // do not cald md5_data if md5_data_source is present - set by flac only - future MPC/SV8 too
  423. if (!$this->option_md5_data_source || empty($this->info['md5_data_source'])) {
  424. $this->getHashdata('md5');
  425. }
  426. }
  427. // get the SHA1 sum of the audio/video portion of the file - without ID3/APE/Lyrics3/etc header/footer tags
  428. if ($this->option_sha1_data) {
  429. $this->getHashdata('sha1');
  430. }
  431. // remove undesired keys
  432. $this->CleanUp();
  433. } catch (Exception $e) {
  434. if (isset($this->info['error'])) {
  435. $this->info['error'][] = 'Caught exception: '.$e->getMessage();
  436. } else {
  437. $this->info['error'] = array('Caught exception: '.$e->getMessage());
  438. }
  439. }
  440. // return info array
  441. return $this->info;
  442. }
  443. // private: error handling
  444. function error($message) {
  445. $this->CleanUp();
  446. $this->info['error'][] = $message;
  447. return $this->info;
  448. }
  449. // private: warning handling
  450. function warning($message) {
  451. $this->info['warning'][] = $message;
  452. return true;
  453. }
  454. // private: CleanUp
  455. function CleanUp() {
  456. // remove possible empty keys
  457. $AVpossibleEmptyKeys = array('dataformat', 'bits_per_sample', 'encoder_options', 'streams', 'bitrate');
  458. foreach ($AVpossibleEmptyKeys as $dummy => $key) {
  459. if (empty($this->info['audio'][$key]) && isset($this->info['audio'][$key])) {
  460. unset($this->info['audio'][$key]);
  461. }
  462. if (empty($this->info['video'][$key]) && isset($this->info['video'][$key])) {
  463. unset($this->info['video'][$key]);
  464. }
  465. }
  466. // remove empty root keys
  467. if (!empty($this->info)) {
  468. foreach ($this->info as $key => $value) {
  469. if (empty($this->info[$key]) && ($this->info[$key] !== 0) && ($this->info[$key] !== '0')) {
  470. unset($this->info[$key]);
  471. }
  472. }
  473. }
  474. // remove meaningless entries from unknown-format files
  475. if (empty($this->info['fileformat'])) {
  476. if (isset($this->info['avdataoffset'])) {
  477. unset($this->info['avdataoffset']);
  478. }
  479. if (isset($this->info['avdataend'])) {
  480. unset($this->info['avdataend']);
  481. }
  482. }
  483. }
  484. // return array containing information about all supported formats
  485. function GetFileFormatArray() {
  486. static $format_info = array();
  487. if (empty($format_info)) {
  488. $format_info = array(
  489. // Audio formats
  490. // AC-3 - audio - Dolby AC-3 / Dolby Digital
  491. 'ac3' => array(
  492. 'pattern' => '^\x0B\x77',
  493. 'group' => 'audio',
  494. 'module' => 'ac3',
  495. 'mime_type' => 'audio/ac3',
  496. ),
  497. // AAC - audio - Advanced Audio Coding (AAC) - ADIF format
  498. 'adif' => array(
  499. 'pattern' => '^ADIF',
  500. 'group' => 'audio',
  501. 'module' => 'aac',
  502. 'option' => 'adif',
  503. 'mime_type' => 'application/octet-stream',
  504. 'fail_ape' => 'WARNING',
  505. ),
  506. // AA - audio - Audible Audiobook
  507. 'adts' => array(
  508. 'pattern' => '^.{4}\x57\x90\x75\x36',
  509. 'group' => 'audio',
  510. 'module' => 'aa',
  511. 'mime_type' => 'audio/audible ',
  512. ),
  513. // AAC - audio - Advanced Audio Coding (AAC) - ADTS format (very similar to MP3)
  514. 'adts' => array(
  515. 'pattern' => '^\xFF[\xF0-\xF1\xF8-\xF9]',
  516. 'group' => 'audio',
  517. 'module' => 'aac',
  518. 'option' => 'adts',
  519. 'mime_type' => 'application/octet-stream',
  520. 'fail_ape' => 'WARNING',
  521. ),
  522. // AU - audio - NeXT/Sun AUdio (AU)
  523. 'au' => array(
  524. 'pattern' => '^\.snd',
  525. 'group' => 'audio',
  526. 'module' => 'au',
  527. 'mime_type' => 'audio/basic',
  528. ),
  529. // AVR - audio - Audio Visual Research
  530. 'avr' => array(
  531. 'pattern' => '^2BIT',
  532. 'group' => 'audio',
  533. 'module' => 'avr',
  534. 'mime_type' => 'application/octet-stream',
  535. ),
  536. // BONK - audio - Bonk v0.9+
  537. 'bonk' => array(
  538. 'pattern' => '^\x00(BONK|INFO|META| ID3)',
  539. 'group' => 'audio',
  540. 'module' => 'bonk',
  541. 'mime_type' => 'audio/xmms-bonk',
  542. ),
  543. // DSS - audio - Digital Speech Standard
  544. 'dss' => array(
  545. 'pattern' => '^[\x02-\x03]dss',
  546. 'group' => 'audio',
  547. 'module' => 'dss',
  548. 'mime_type' => 'application/octet-stream',
  549. ),
  550. // DTS - audio - Dolby Theatre System
  551. 'dts' => array(
  552. 'pattern' => '^\x7F\xFE\x80\x01',
  553. 'group' => 'audio',
  554. 'module' => 'dts',
  555. 'mime_type' => 'audio/dts',
  556. ),
  557. // FLAC - audio - Free Lossless Audio Codec
  558. 'flac' => array(
  559. 'pattern' => '^fLaC',
  560. 'group' => 'audio',
  561. 'module' => 'flac',
  562. 'mime_type' => 'audio/x-flac',
  563. ),
  564. // LA - audio - Lossless Audio (LA)
  565. 'la' => array(
  566. 'pattern' => '^LA0[2-4]',
  567. 'group' => 'audio',
  568. 'module' => 'la',
  569. 'mime_type' => 'application/octet-stream',
  570. ),
  571. // LPAC - audio - Lossless Predictive Audio Compression (LPAC)
  572. 'lpac' => array(
  573. 'pattern' => '^LPAC',
  574. 'group' => 'audio',
  575. 'module' => 'lpac',
  576. 'mime_type' => 'application/octet-stream',
  577. ),
  578. // MIDI - audio - MIDI (Musical Instrument Digital Interface)
  579. 'midi' => array(
  580. 'pattern' => '^MThd',
  581. 'group' => 'audio',
  582. 'module' => 'midi',
  583. 'mime_type' => 'audio/midi',
  584. ),
  585. // MAC - audio - Monkey's Audio Compressor
  586. 'mac' => array(
  587. 'pattern' => '^MAC ',
  588. 'group' => 'audio',
  589. 'module' => 'monkey',
  590. 'mime_type' => 'application/octet-stream',
  591. ),
  592. // has been known to produce false matches in random files (e.g. JPEGs), leave out until more precise matching available
  593. // // MOD - audio - MODule (assorted sub-formats)
  594. // 'mod' => array(
  595. // 'pattern' => '^.{1080}(M\\.K\\.|M!K!|FLT4|FLT8|[5-9]CHN|[1-3][0-9]CH)',
  596. // 'group' => 'audio',
  597. // 'module' => 'mod',
  598. // 'option' => 'mod',
  599. // 'mime_type' => 'audio/mod',
  600. // ),
  601. // MOD - audio - MODule (Impulse Tracker)
  602. 'it' => array(
  603. 'pattern' => '^IMPM',
  604. 'group' => 'audio',
  605. 'module' => 'mod',
  606. 'option' => 'it',
  607. 'mime_type' => 'audio/it',
  608. ),
  609. // MOD - audio - MODule (eXtended Module, various sub-formats)
  610. 'xm' => array(
  611. 'pattern' => '^Extended Module',
  612. 'group' => 'audio',
  613. 'module' => 'mod',
  614. 'option' => 'xm',
  615. 'mime_type' => 'audio/xm',
  616. ),
  617. // MOD - audio - MODule (ScreamTracker)
  618. 's3m' => array(
  619. 'pattern' => '^.{44}SCRM',
  620. 'group' => 'audio',
  621. 'module' => 'mod',
  622. 'option' => 's3m',
  623. 'mime_type' => 'audio/s3m',
  624. ),
  625. // MPC - audio - Musepack / MPEGplus
  626. 'mpc' => array(
  627. '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])',
  628. 'group' => 'audio',
  629. 'module' => 'mpc',
  630. 'mime_type' => 'audio/x-musepack',
  631. ),
  632. // MP3 - audio - MPEG-audio Layer 3 (very similar to AAC-ADTS)
  633. 'mp3' => array(
  634. '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]',
  635. 'group' => 'audio',
  636. 'module' => 'mp3',
  637. 'mime_type' => 'audio/mpeg',
  638. ),
  639. // OFR - audio - OptimFROG
  640. 'ofr' => array(
  641. 'pattern' => '^(\*RIFF|OFR)',
  642. 'group' => 'audio',
  643. 'module' => 'optimfrog',
  644. 'mime_type' => 'application/octet-stream',
  645. ),
  646. // RKAU - audio - RKive AUdio compressor
  647. 'rkau' => array(
  648. 'pattern' => '^RKA',
  649. 'group' => 'audio',
  650. 'module' => 'rkau',
  651. 'mime_type' => 'application/octet-stream',
  652. ),
  653. // SHN - audio - Shorten
  654. 'shn' => array(
  655. 'pattern' => '^ajkg',
  656. 'group' => 'audio',
  657. 'module' => 'shorten',
  658. 'mime_type' => 'audio/xmms-shn',
  659. 'fail_id3' => 'ERROR',
  660. 'fail_ape' => 'ERROR',
  661. ),
  662. // TTA - audio - TTA Lossless Audio Compressor (http://tta.corecodec.org)
  663. 'tta' => array(
  664. 'pattern' => '^TTA', // could also be '^TTA(\x01|\x02|\x03|2|1)'
  665. 'group' => 'audio',
  666. 'module' => 'tta',
  667. 'mime_type' => 'application/octet-stream',
  668. ),
  669. // VOC - audio - Creative Voice (VOC)
  670. 'voc' => array(
  671. 'pattern' => '^Creative Voice File',
  672. 'group' => 'audio',
  673. 'module' => 'voc',
  674. 'mime_type' => 'audio/voc',
  675. ),
  676. // VQF - audio - transform-domain weighted interleave Vector Quantization Format (VQF)
  677. 'vqf' => array(
  678. 'pattern' => '^TWIN',
  679. 'group' => 'audio',
  680. 'module' => 'vqf',
  681. 'mime_type' => 'application/octet-stream',
  682. ),
  683. // WV - audio - WavPack (v4.0+)
  684. 'wv' => array(
  685. 'pattern' => '^wvpk',
  686. 'group' => 'audio',
  687. 'module' => 'wavpack',
  688. 'mime_type' => 'application/octet-stream',
  689. ),
  690. // Audio-Video formats
  691. // ASF - audio/video - Advanced Streaming Format, Windows Media Video, Windows Media Audio
  692. 'asf' => array(
  693. 'pattern' => '^\x30\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C',
  694. 'group' => 'audio-video',
  695. 'module' => 'asf',
  696. 'mime_type' => 'video/x-ms-asf',
  697. 'iconv_req' => false,
  698. ),
  699. // BINK - audio/video - Bink / Smacker
  700. 'bink' => array(
  701. 'pattern' => '^(BIK|SMK)',
  702. 'group' => 'audio-video',
  703. 'module' => 'bink',
  704. 'mime_type' => 'application/octet-stream',
  705. ),
  706. // FLV - audio/video - FLash Video
  707. 'flv' => array(
  708. 'pattern' => '^FLV\x01',
  709. 'group' => 'audio-video',
  710. 'module' => 'flv',
  711. 'mime_type' => 'video/x-flv',
  712. ),
  713. // MKAV - audio/video - Mastroka
  714. 'matroska' => array(
  715. 'pattern' => '^\x1A\x45\xDF\xA3',
  716. 'group' => 'audio-video',
  717. 'module' => 'matroska',
  718. 'mime_type' => 'video/x-matroska', // may also be audio/x-matroska
  719. ),
  720. // MPEG - audio/video - MPEG (Moving Pictures Experts Group)
  721. 'mpeg' => array(
  722. 'pattern' => '^\x00\x00\x01(\xBA|\xB3)',
  723. 'group' => 'audio-video',
  724. 'module' => 'mpeg',
  725. 'mime_type' => 'video/mpeg',
  726. ),
  727. // NSV - audio/video - Nullsoft Streaming Video (NSV)
  728. 'nsv' => array(
  729. 'pattern' => '^NSV[sf]',
  730. 'group' => 'audio-video',
  731. 'module' => 'nsv',
  732. 'mime_type' => 'application/octet-stream',
  733. ),
  734. // Ogg - audio/video - Ogg (Ogg-Vorbis, Ogg-FLAC, Speex, Ogg-Theora(*), Ogg-Tarkin(*))
  735. 'ogg' => array(
  736. 'pattern' => '^OggS',
  737. 'group' => 'audio',
  738. 'module' => 'ogg',
  739. 'mime_type' => 'application/ogg',
  740. 'fail_id3' => 'WARNING',
  741. 'fail_ape' => 'WARNING',
  742. ),
  743. // QT - audio/video - Quicktime
  744. 'quicktime' => array(
  745. 'pattern' => '^.{4}(cmov|free|ftyp|mdat|moov|pnot|skip|wide)',
  746. 'group' => 'audio-video',
  747. 'module' => 'quicktime',
  748. 'mime_type' => 'video/quicktime',
  749. ),
  750. // 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)
  751. 'riff' => array(
  752. 'pattern' => '^(RIFF|SDSS|FORM)',
  753. 'group' => 'audio-video',
  754. 'module' => 'riff',
  755. 'mime_type' => 'audio/x-wave',
  756. 'fail_ape' => 'WARNING',
  757. ),
  758. // Real - audio/video - RealAudio, RealVideo
  759. 'real' => array(
  760. 'pattern' => '^(\\.RMF|\\.ra)',
  761. 'group' => 'audio-video',
  762. 'module' => 'real',
  763. 'mime_type' => 'audio/x-realaudio',
  764. ),
  765. // SWF - audio/video - ShockWave Flash
  766. 'swf' => array(
  767. 'pattern' => '^(F|C)WS',
  768. 'group' => 'audio-video',
  769. 'module' => 'swf',
  770. 'mime_type' => 'application/x-shockwave-flash',
  771. ),
  772. // Still-Image formats
  773. // BMP - still image - Bitmap (Windows, OS/2; uncompressed, RLE8, RLE4)
  774. 'bmp' => array(
  775. 'pattern' => '^BM',
  776. 'group' => 'graphic',
  777. 'module' => 'bmp',
  778. 'mime_type' => 'image/bmp',
  779. 'fail_id3' => 'ERROR',
  780. 'fail_ape' => 'ERROR',
  781. ),
  782. // GIF - still image - Graphics Interchange Format
  783. 'gif' => array(
  784. 'pattern' => '^GIF',
  785. 'group' => 'graphic',
  786. 'module' => 'gif',
  787. 'mime_type' => 'image/gif',
  788. 'fail_id3' => 'ERROR',
  789. 'fail_ape' => 'ERROR',
  790. ),
  791. // JPEG - still image - Joint Photographic Experts Group (JPEG)
  792. 'jpg' => array(
  793. 'pattern' => '^\xFF\xD8\xFF',
  794. 'group' => 'graphic',
  795. 'module' => 'jpg',
  796. 'mime_type' => 'image/jpeg',
  797. 'fail_id3' => 'ERROR',
  798. 'fail_ape' => 'ERROR',
  799. ),
  800. // PCD - still image - Kodak Photo CD
  801. 'pcd' => array(
  802. 'pattern' => '^.{2048}PCD_IPI\x00',
  803. 'group' => 'graphic',
  804. 'module' => 'pcd',
  805. 'mime_type' => 'image/x-photo-cd',
  806. 'fail_id3' => 'ERROR',
  807. 'fail_ape' => 'ERROR',
  808. ),
  809. // PNG - still image - Portable Network Graphics (PNG)
  810. 'png' => array(
  811. 'pattern' => '^\x89\x50\x4E\x47\x0D\x0A\x1A\x0A',
  812. 'group' => 'graphic',
  813. 'module' => 'png',
  814. 'mime_type' => 'image/png',
  815. 'fail_id3' => 'ERROR',
  816. 'fail_ape' => 'ERROR',
  817. ),
  818. // SVG - still image - Scalable Vector Graphics (SVG)
  819. 'svg' => array(
  820. 'pattern' => '(<!DOCTYPE svg PUBLIC |xmlns="http:\/\/www\.w3\.org\/2000\/svg")',
  821. 'group' => 'graphic',
  822. 'module' => 'svg',
  823. 'mime_type' => 'image/svg+xml',
  824. 'fail_id3' => 'ERROR',
  825. 'fail_ape' => 'ERROR',
  826. ),
  827. // TIFF - still image - Tagged Information File Format (TIFF)
  828. 'tiff' => array(
  829. 'pattern' => '^(II\x2A\x00|MM\x00\x2A)',
  830. 'group' => 'graphic',
  831. 'module' => 'tiff',
  832. 'mime_type' => 'image/tiff',
  833. 'fail_id3' => 'ERROR',
  834. 'fail_ape' => 'ERROR',
  835. ),
  836. // Data formats
  837. // ISO - data - International Standards Organization (ISO) CD-ROM Image
  838. 'iso' => array(
  839. 'pattern' => '^.{32769}CD001',
  840. 'group' => 'misc',
  841. 'module' => 'iso',
  842. 'mime_type' => 'application/octet-stream',
  843. 'fail_id3' => 'ERROR',
  844. 'fail_ape' => 'ERROR',
  845. 'iconv_req' => false,
  846. ),
  847. // RAR - data - RAR compressed data
  848. 'rar' => array(
  849. 'pattern' => '^Rar\!',
  850. 'group' => 'archive',
  851. 'module' => 'rar',
  852. 'mime_type' => 'application/octet-stream',
  853. 'fail_id3' => 'ERROR',
  854. 'fail_ape' => 'ERROR',
  855. ),
  856. // SZIP - audio/data - SZIP compressed data
  857. 'szip' => array(
  858. 'pattern' => '^SZ\x0A\x04',
  859. 'group' => 'archive',
  860. 'module' => 'szip',
  861. 'mime_type' => 'application/octet-stream',
  862. 'fail_id3' => 'ERROR',
  863. 'fail_ape' => 'ERROR',
  864. ),
  865. // TAR - data - TAR compressed data
  866. 'tar' => array(
  867. '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}',
  868. 'group' => 'archive',
  869. 'module' => 'tar',
  870. 'mime_type' => 'application/x-tar',
  871. 'fail_id3' => 'ERROR',
  872. 'fail_ape' => 'ERROR',
  873. ),
  874. // GZIP - data - GZIP compressed data
  875. 'gz' => array(
  876. 'pattern' => '^\x1F\x8B\x08',
  877. 'group' => 'archive',
  878. 'module' => 'gzip',
  879. 'mime_type' => 'application/x-gzip',
  880. 'fail_id3' => 'ERROR',
  881. 'fail_ape' => 'ERROR',
  882. ),
  883. // ZIP - data - ZIP compressed data
  884. 'zip' => array(
  885. 'pattern' => '^PK\x03\x04',
  886. 'group' => 'archive',
  887. 'module' => 'zip',
  888. 'mime_type' => 'application/zip',
  889. 'fail_id3' => 'ERROR',
  890. 'fail_ape' => 'ERROR',
  891. ),
  892. // Misc other formats
  893. // PAR2 - data - Parity Volume Set Specification 2.0
  894. 'par2' => array (
  895. 'pattern' => '^PAR2\x00PKT',
  896. 'group' => 'misc',
  897. 'module' => 'par2',
  898. 'mime_type' => 'application/octet-stream',
  899. 'fail_id3' => 'ERROR',
  900. 'fail_ape' => 'ERROR',
  901. ),
  902. // PDF - data - Portable Document Format
  903. 'pdf' => array(
  904. 'pattern' => '^\x25PDF',
  905. 'group' => 'misc',
  906. 'module' => 'pdf',
  907. 'mime_type' => 'application/pdf',
  908. 'fail_id3' => 'ERROR',
  909. 'fail_ape' => 'ERROR',
  910. ),
  911. // MSOFFICE - data - ZIP compressed data
  912. 'msoffice' => array(
  913. 'pattern' => '^\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1', // D0CF11E == DOCFILE == Microsoft Office Document
  914. 'group' => 'misc',
  915. 'module' => 'msoffice',
  916. 'mime_type' => 'application/octet-stream',
  917. 'fail_id3' => 'ERROR',
  918. 'fail_ape' => 'ERROR',
  919. ),
  920. // CUE - data - CUEsheet (index to single-file disc images)
  921. 'cue' => array(
  922. 'pattern' => '', // empty pattern means cannot be automatically detected, will fall through all other formats and match based on filename and very basic file contents
  923. 'group' => 'misc',
  924. 'module' => 'cue',
  925. 'mime_type' => 'application/octet-stream',
  926. ),
  927. );
  928. }
  929. return $format_info;
  930. }
  931. function GetFileFormat(&$filedata, $filename='') {
  932. // this function will determine the format of a file based on usually
  933. // the first 2-4 bytes of the file (8 bytes for PNG, 16 bytes for JPG,
  934. // and in the case of ISO CD image, 6 bytes offset 32kb from the start
  935. // of the file).
  936. // Identify file format - loop through $format_info and detect with reg expr
  937. foreach ($this->GetFileFormatArray() as $format_name => $info) {
  938. // The /s switch on preg_match() forces preg_match() NOT to treat
  939. // newline (0x0A) characters as special chars but do a binary match
  940. if (!empty($info['pattern']) && preg_match('#'.$info['pattern'].'#s', $filedata)) {
  941. $info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.php';
  942. return $info;
  943. }
  944. }
  945. if (preg_match('#\.mp[123a]$#i', $filename)) {
  946. // Too many mp3 encoders on the market put gabage in front of mpeg files
  947. // use assume format on these if format detection failed
  948. $GetFileFormatArray = $this->GetFileFormatArray();
  949. $info = $GetFileFormatArray['mp3'];
  950. $info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.php';
  951. return $info;
  952. } elseif (preg_match('/\.cue$/i', $filename) && preg_match('#FILE "[^"]+" (BINARY|MOTOROLA|AIFF|WAVE|MP3)#', $filedata)) {
  953. // there's not really a useful consistent "magic" at the beginning of .cue files to identify them
  954. // so until I think of something better, just go by filename if all other format checks fail
  955. // and verify there's at least one instance of "TRACK xx AUDIO" in the file
  956. $GetFileFormatArray = $this->GetFileFormatArray();
  957. $info = $GetFileFormatArray['cue'];
  958. $info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.php';
  959. return $info;
  960. }
  961. return false;
  962. }
  963. // converts array to $encoding charset from $this->encoding
  964. function CharConvert(&$array, $encoding) {
  965. // identical encoding - end here
  966. if ($encoding == $this->encoding) {
  967. return;
  968. }
  969. // loop thru array
  970. foreach ($array as $key => $value) {
  971. // go recursive
  972. if (is_array($value)) {
  973. $this->CharConvert($array[$key], $encoding);
  974. }
  975. // convert string
  976. elseif (is_string($value)) {
  977. $array[$key] = trim(getid3_lib::iconv_fallback($encoding, $this->encoding, $value));
  978. }
  979. }
  980. }
  981. function HandleAllTags() {
  982. // key name => array (tag name, character encoding)
  983. static $tags;
  984. if (empty($tags)) {
  985. $tags = array(
  986. 'asf' => array('asf' , 'UTF-16LE'),
  987. 'midi' => array('midi' , 'ISO-8859-1'),
  988. 'nsv' => array('nsv' , 'ISO-8859-1'),
  989. 'ogg' => array('vorbiscomment' , 'UTF-8'),
  990. 'png' => array('png' , 'UTF-8'),
  991. 'tiff' => array('tiff' , 'ISO-8859-1'),
  992. 'quicktime' => array('quicktime' , 'UTF-8'),
  993. 'real' => array('real' , 'ISO-8859-1'),
  994. 'vqf' => array('vqf' , 'ISO-8859-1'),
  995. 'zip' => array('zip' , 'ISO-8859-1'),
  996. 'riff' => array('riff' , 'ISO-8859-1'),
  997. 'lyrics3' => array('lyrics3' , 'ISO-8859-1'),
  998. 'id3v1' => array('id3v1' , $this->encoding_id3v1),
  999. '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
  1000. 'ape' => array('ape' , 'UTF-8'),
  1001. 'cue' => array('cue' , 'ISO-8859-1'),
  1002. );
  1003. }
  1004. // loop thru comments array
  1005. foreach ($tags as $comment_name => $tagname_encoding_array) {
  1006. list($tag_name, $encoding) = $tagname_encoding_array;
  1007. // fill in default encoding type if not already present
  1008. if (isset($this->info[$comment_name]) && !isset($this->info[$comment_name]['encoding'])) {
  1009. $this->info[$comment_name]['encoding'] = $encoding;
  1010. }
  1011. // copy comments if key name set
  1012. if (!empty($this->info[$comment_name]['comments'])) {
  1013. foreach ($this->info[$comment_name]['comments'] as $tag_key => $valuearray) {
  1014. foreach ($valuearray as $key => $value) {
  1015. $value = (is_string($value) ? trim($value) : $value);
  1016. if (!empty($value) > 0) {
  1017. $this->info['tags'][trim($tag_name)][trim($tag_key)][] = $value; // do not trim!! Unicode characters will get mangled if trailing nulls are removed!
  1018. }
  1019. }
  1020. }
  1021. if (!isset($this->info['tags'][$tag_name])) {
  1022. // comments are set but contain nothing but empty strings, so skip
  1023. continue;
  1024. }
  1025. if ($this->option_tags_html) {
  1026. foreach ($this->info['tags'][$tag_name] as $tag_key => $valuearray) {
  1027. foreach ($valuearray as $key => $value) {
  1028. if (is_string($value)) {
  1029. //$this->info['tags_html'][$tag_name][$tag_key][$key] = getid3_lib::MultiByteCharString2HTML($value, $encoding);
  1030. $this->info['tags_html'][$tag_name][$tag_key][$key] = str_replace('&#0;', '', getid3_lib::MultiByteCharString2HTML($value, $encoding));
  1031. } else {
  1032. $this->info['tags_html'][$tag_name][$tag_key][$key] = $value;
  1033. }
  1034. }
  1035. }
  1036. }
  1037. $this->CharConvert($this->info['tags'][$tag_name], $encoding); // only copy gets converted!
  1038. }
  1039. }
  1040. return true;
  1041. }
  1042. function getHashdata($algorithm) {
  1043. switch ($algorithm) {
  1044. case 'md5':
  1045. case 'sha1':
  1046. break;
  1047. default:
  1048. return $this->error('bad algorithm "'.$algorithm.'" in getHashdata()');
  1049. break;
  1050. }
  1051. if (!empty($this->info['fileformat']) && !empty($this->info['dataformat']) && ($this->info['fileformat'] == 'ogg') && ($this->info['audio']['dataformat'] == 'vorbis')) {
  1052. // We cannot get an identical md5_data value for Ogg files where the comments
  1053. // span more than 1 Ogg page (compared to the same audio data with smaller
  1054. // comments) using the normal getID3() method of MD5'ing the data between the
  1055. // end of the comments and the end of the file (minus any trailing tags),
  1056. // because the page sequence numbers of the pages that the audio data is on
  1057. // do not match. Under normal circumstances, where comments are smaller than
  1058. // the nominal 4-8kB page size, then this is not a problem, but if there are
  1059. // very large comments, the only way around it is to strip off the comment
  1060. // tags with vorbiscomment and MD5 that file.
  1061. // This procedure must be applied to ALL Ogg files, not just the ones with
  1062. // comments larger than 1 page, because the below method simply MD5's the
  1063. // whole file with the comments stripped, not just the portion after the
  1064. // comments block (which is the standard getID3() method.
  1065. // The above-mentioned problem of comments spanning multiple pages and changing
  1066. // page sequence numbers likely happens for OggSpeex and OggFLAC as well, but
  1067. // currently vorbiscomment only works on OggVorbis files.
  1068. if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
  1069. $this->info['warning'][] = 'Failed making system call to vorbiscomment.exe - '.$algorithm.'_data is incorrect - error returned: PHP running in Safe Mode (backtick operator not available)';
  1070. $this->info[$algorithm.'_data'] = false;
  1071. } else {
  1072. // Prevent user from aborting script
  1073. $old_abort = ignore_user_abort(true);
  1074. // Create empty file
  1075. $empty = tempnam(GETID3_TEMP_DIR, 'getID3');
  1076. touch($empty);
  1077. // Use vorbiscomment to make temp file without comments
  1078. $temp = tempnam(GETID3_TEMP_DIR, 'getID3');
  1079. $file = $this->info['filenamepath'];
  1080. if (GETID3_OS_ISWINDOWS) {
  1081. if (file_exists(GETID3_HELPERAPPSDIR.'vorbiscomment.exe')) {
  1082. $commandline = '"'.GETID3_HELPERAPPSDIR.'vorbiscomment.exe" -w -c "'.$empty.'" "'.$file.'" "'.$temp.'"';
  1083. $VorbisCommentError = `$commandline`;
  1084. } else {
  1085. $VorbisCommentError = 'vorbiscomment.exe not found in '.GETID3_HELPERAPPSDIR;
  1086. }
  1087. } else {
  1088. $commandline = 'vorbiscomment -w -c "'.$empty.'" "'.$file.'" "'.$temp.'" 2>&1';
  1089. $commandline = 'vorbiscomment -w -c '.escapeshellarg($empty).' '.escapeshellarg($file).' '.escapeshellarg($temp).' 2>&1';
  1090. $VorbisCommentError = `$commandline`;
  1091. }
  1092. if (!empty($VorbisCommentError)) {
  1093. $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;
  1094. $this->info[$algorithm.'_data'] = false;
  1095. } else {
  1096. // Get hash of newly created file
  1097. switch ($algorithm) {
  1098. case 'md5':
  1099. $this->info[$algorithm.'_data'] = md5_file($temp);
  1100. break;
  1101. case 'sha1':
  1102. $this->info[$algorithm.'_data'] = sha1_file($temp);
  1103. break;
  1104. }
  1105. }
  1106. // Clean up
  1107. unlink($empty);
  1108. unlink($temp);
  1109. // Reset abort setting
  1110. ignore_user_abort($old_abort);
  1111. }
  1112. } else {
  1113. if (!empty($this->info['avdataoffset']) || (isset($this->info['avdataend']) && ($this->info['avdataend'] < $this->info['filesize']))) {
  1114. // get hash from part of file
  1115. $this->info[$algorithm.'_data'] = getid3_lib::hash_data($this->info['filenamepath'], $this->info['avdataoffset'], $this->info['avdataend'], $algorithm);
  1116. } else {
  1117. // get hash from whole file
  1118. switch ($algorithm) {
  1119. case 'md5':
  1120. $this->info[$algorithm.'_data'] = md5_file($this->info['filenamepath']);
  1121. break;
  1122. case 'sha1':
  1123. $this->info[$algorithm.'_data'] = sha1_file($this->info['filenamepath']);
  1124. break;
  1125. }
  1126. }
  1127. }
  1128. return true;
  1129. }
  1130. function ChannelsBitratePlaytimeCalculations() {
  1131. // set channelmode on audio
  1132. if (!isset($this->info['audio']['channels'])) {
  1133. // ignore
  1134. } elseif ($this->info['audio']['channels'] == 1) {
  1135. $this->info['audio']['channelmode'] = 'mono';
  1136. } elseif ($this->info['audio']['channels'] == 2) {
  1137. $this->info['audio']['channelmode'] = 'stereo';
  1138. }
  1139. // Calculate combined bitrate - audio + video
  1140. $CombinedBitrate = 0;
  1141. $CombinedBitrate += (isset($this->info['audio']['bitrate']) ? $this->info['audio']['bitrate'] : 0);
  1142. $CombinedBitrate += (isset($this->info['video']['bitrate']) ? $this->info['video']['bitrate'] : 0);
  1143. if (($CombinedBitrate > 0) && empty($this->info['bitrate'])) {
  1144. $this->info['bitrate'] = $CombinedBitrate;
  1145. }
  1146. //if ((isset($this->info['video']) && !isset($this->info['video']['bitrate'])) || (isset($this->info['audio']) && !isset($this->info['audio']['bitrate']))) {
  1147. // // for example, VBR MPEG video files cannot determine video bitrate:
  1148. // // should not set overall bitrate and playtime from audio bitrate only
  1149. // unset($this->info['bitrate']);
  1150. //}
  1151. // video bitrate undetermined, but calculable
  1152. if (isset($this->info['video']['dataformat']) && $this->info['video']['dataformat'] && (!isset($this->info['video']['bitrate']) || ($this->info['video']['bitrate'] == 0))) {
  1153. // if video bitrate not set
  1154. if (isset($this->info['audio']['bitrate']) && ($this->info['audio']['bitrate'] > 0) && ($this->info['audio']['bitrate'] == $this->info['bitrate'])) {
  1155. // AND if audio bitrate is set to same as overall bitrate
  1156. if (isset($this->info['playtime_seconds']) && ($this->info['playtime_seconds'] > 0)) {
  1157. // AND if playtime is set
  1158. if (isset($this->info['avdataend']) && isset($this->info['avdataoffset'])) {
  1159. // AND if AV data offset start/end is known
  1160. // THEN we can calculate the video bitrate
  1161. $this->info['bitrate'] = round((($this->info['avdataend'] - $this->info['avdataoffset']) * 8) / $this->info['playtime_seconds']);
  1162. $this->info['video']['bitrate'] = $this->info['bitrate'] - $this->info['audio']['bitrate'];
  1163. }
  1164. }
  1165. }
  1166. }
  1167. if ((!isset($this->info['playtime_seconds']) || ($this->info['playtime_seconds'] <= 0)) && !empty($this->info['bitrate'])) {
  1168. $this->info['playtime_seconds'] = (($this->info['avdataend'] - $this->info['avdataoffset']) * 8) / $this->info['bitrate'];
  1169. }