PageRenderTime 83ms CodeModel.GetById 45ms RepoModel.GetById 1ms app.codeStats 0ms

/web/wordpress/wp-includes/ID3/getid3.php

https://github.com/jeremylightsmith/blog
PHP | 1776 lines | 1264 code | 268 blank | 244 comment | 234 complexity | 3c224328480a7a16ed0037af3c2232a8 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0

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

Large files files are truncated, but you can click here to view the full file