rlm@3: | rlm@3: // | Allan Hansen | rlm@3: // +----------------------------------------------------------------------+ rlm@3: // | getid3.php | rlm@3: // | Main getID3() file. | rlm@3: // | dependencies: modules. | rlm@3: // +----------------------------------------------------------------------+ rlm@3: // rlm@3: // $Id: getid3.php,v 1.26 2006/12/25 23:44:23 ah Exp $ rlm@3: rlm@3: rlm@3: class getid3 rlm@3: { rlm@3: //// Settings Section - do NOT modify this file - change setting after newing getid3! rlm@3: rlm@3: // Encoding rlm@3: public $encoding = 'ISO-8859-1'; // CASE SENSITIVE! - i.e. (must be supported by iconv() - see http://www.gnu.org/software/libiconv/). Examples: ISO-8859-1 UTF-8 UTF-16 UTF-16BE. rlm@3: public $encoding_id3v1 = 'ISO-8859-1'; // Override SPECIFICATION encoding for broken ID3v1 tags caused by bad tag programs. Examples: 'EUC-CN' for "Chinese MP3s" and 'CP1251' for "Cyrillic". rlm@3: public $encoding_id3v2 = 'ISO-8859-1'; // Override ISO-8859-1 encoding for broken ID3v2 tags caused by BRAINDEAD tag programs that writes system codepage as 'ISO-8859-1' instead of UTF-8. rlm@3: rlm@3: // Tags - disable for speed rlm@3: public $option_tag_id3v1 = true; // Read and process ID3v1 tags. rlm@3: public $option_tag_id3v2 = true; // Read and process ID3v2 tags. rlm@3: public $option_tag_lyrics3 = true; // Read and process Lyrics3 tags. rlm@3: public $option_tag_apetag = true; // Read and process APE tags. rlm@3: rlm@3: // Misc calucations - disable for speed rlm@3: public $option_analyze = true; // Analyze file - disable if you only need to detect file format. rlm@3: public $option_accurate_results = true; // Disable to greatly speed up parsing of some file formats at the cost of accuracy. rlm@3: public $option_tags_process = true; // Copy tags to root key 'tags' and 'comments' and encode to $this->encoding. rlm@3: public $option_tags_images = false; // Scan tags for binary image data - ID3v2 and vorbiscomments only. rlm@3: public $option_extra_info = true; // Calculate/return additional info such as bitrate, channelmode etc. rlm@3: public $option_max_2gb_check = false; // Check whether file is larger than 2 Gb and thus not supported by PHP. rlm@3: rlm@3: // Misc data hashes - slow - require hash module rlm@3: public $option_md5_data = false; // Get MD5 sum of data part - slow. rlm@3: public $option_md5_data_source = false; // Use MD5 of source file if available - only FLAC, MAC, OptimFROG and Wavpack4. rlm@3: public $option_sha1_data = false; // Get SHA1 sum of data part - slow. rlm@3: rlm@3: // Public variables rlm@3: public $filename; // Filename of file being analysed. rlm@3: public $fp; // Filepointer to file being analysed. rlm@3: public $info; // Result array. rlm@3: rlm@3: // Protected variables rlm@3: protected $include_path; // getid3 include path. rlm@3: protected $warnings = array (); rlm@3: protected $iconv_present; rlm@3: rlm@3: // Class constants rlm@3: const VERSION = '2.0.0b4'; rlm@3: const FREAD_BUFFER_SIZE = 16384; // Read buffer size in bytes. rlm@3: const ICONV_TEST_STRING = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ '; rlm@3: rlm@3: rlm@3: rlm@3: // Constructor - check PHP enviroment and load library. rlm@3: public function __construct() { rlm@3: rlm@3: // Static varibles - no need to recalc every time we new getid3. rlm@3: static $include_path; rlm@3: static $iconv_present; rlm@3: rlm@3: rlm@3: static $initialized; rlm@3: if ($initialized) { rlm@3: rlm@3: // Import static variables rlm@3: $this->include_path = $include_path; rlm@3: $this->iconv_present = $iconv_present; rlm@3: rlm@3: // Run init checks only on first instance. rlm@3: return; rlm@3: } rlm@3: rlm@3: // Get include_path rlm@3: $this->include_path = $include_path = dirname(__FILE__) . '/'; rlm@3: rlm@3: // Check for presence of iconv() and make sure it works (simpel test only). rlm@3: if (function_exists('iconv') && @iconv('UTF-16LE', 'ISO-8859-1', @iconv('ISO-8859-1', 'UTF-16LE', getid3::ICONV_TEST_STRING)) == getid3::ICONV_TEST_STRING) { rlm@3: $this->iconv_present = $iconv_present = true; rlm@3: } rlm@3: rlm@3: // iconv() not present - load replacement module. rlm@3: else { rlm@3: $this->include_module('lib.iconv_replacement'); rlm@3: $this->iconv_present = $iconv_present = false; rlm@3: } rlm@3: rlm@3: rlm@3: // Require magic_quotes_runtime off rlm@3: if (get_magic_quotes_runtime()) { rlm@3: throw new getid3_exception('magic_quotes_runtime must be disabled before running getID3(). Surround getid3 block by set_magic_quotes_runtime(0) and set_magic_quotes_runtime(1).'); rlm@3: } rlm@3: rlm@3: rlm@3: // Check memory limit. rlm@3: $memory_limit = ini_get('memory_limit'); rlm@3: if (eregi('([0-9]+)M', $memory_limit, $matches)) { rlm@3: // could be stored as "16M" rather than 16777216 for example rlm@3: $memory_limit = $matches[1] * 1048576; rlm@3: } rlm@3: if ($memory_limit <= 0) { rlm@3: // Should not happen. rlm@3: } elseif ($memory_limit <= 4194304) { rlm@3: $this->warning('[SERIOUS] PHP has less than 4 Mb available memory and will very likely run out. Increase memory_limit in php.ini.'); rlm@3: } elseif ($memory_limit <= 12582912) { rlm@3: $this->warning('PHP has less than 12 Mb available memory and might run out if all modules are loaded. Increase memory_limit in php.ini if needed.'); rlm@3: } rlm@3: rlm@3: rlm@3: // Check safe_mode off rlm@3: if ((bool)ini_get('safe_mode')) { rlm@3: $this->warning('Safe mode is on, shorten support disabled, md5data/sha1data for ogg vorbis disabled, ogg vorbis/flac tag writing disabled.'); rlm@3: } rlm@3: rlm@3: $initialized = true; rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: // Analyze file by name rlm@3: public function Analyze($filename) { rlm@3: rlm@3: // Init and save values rlm@3: $this->filename = $filename; rlm@3: $this->warnings = array (); rlm@3: rlm@3: // Init result array and set parameters rlm@3: $this->info = array (); rlm@3: $this->info['GETID3_VERSION'] = getid3::VERSION; rlm@3: rlm@3: // Remote files not supported rlm@3: if (preg_match('/^(ht|f)tp:\/\//', $filename)) { rlm@3: throw new getid3_exception('Remote files are not supported - please copy the file locally first.'); rlm@3: } rlm@3: rlm@3: // Open local file rlm@3: if (!$this->fp = @fopen($filename, 'rb')) { rlm@3: throw new getid3_exception('Could not open file "'.$filename.'"'); rlm@3: } rlm@3: rlm@3: // Set filesize related parameters rlm@3: $this->info['filesize'] = filesize($filename); rlm@3: $this->info['avdataoffset'] = 0; rlm@3: $this->info['avdataend'] = $this->info['filesize']; rlm@3: rlm@3: // Option_max_2gb_check rlm@3: if ($this->option_max_2gb_check) { rlm@3: // PHP doesn't support integers larger than 31-bit (~2GB) rlm@3: // filesize() simply returns (filesize % (pow(2, 32)), no matter the actual filesize rlm@3: // ftell() returns 0 if seeking to the end is beyond the range of unsigned integer rlm@3: fseek($this->fp, 0, SEEK_END); rlm@3: if ((($this->info['filesize'] != 0) && (ftell($this->fp) == 0)) || rlm@3: ($this->info['filesize'] < 0) || rlm@3: (ftell($this->fp) < 0)) { rlm@3: unset($this->info['filesize']); rlm@3: fclose($this->fp); rlm@3: throw new getid3_exception('File is most likely larger than 2GB and is not supported by PHP.'); rlm@3: } rlm@3: } rlm@3: rlm@3: rlm@3: // ID3v2 detection (NOT parsing) done to make fileformat easier. rlm@3: if (!$this->option_tag_id3v2) { rlm@3: rlm@3: fseek($this->fp, 0, SEEK_SET); rlm@3: $header = fread($this->fp, 10); rlm@3: if (substr($header, 0, 3) == 'ID3' && strlen($header) == 10) { rlm@3: $this->info['id3v2']['header'] = true; rlm@3: $this->info['id3v2']['majorversion'] = ord($header{3}); rlm@3: $this->info['id3v2']['minorversion'] = ord($header{4}); rlm@3: $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 rlm@3: } rlm@3: } rlm@3: rlm@3: rlm@3: // Handle tags rlm@3: foreach (array ("id3v2", "id3v1", "apetag", "lyrics3") as $tag_name) { rlm@3: rlm@3: $option_tag = 'option_tag_' . $tag_name; rlm@3: if ($this->$option_tag) { rlm@3: $this->include_module('tag.'.$tag_name); rlm@3: try { rlm@3: $tag_class = 'getid3_' . $tag_name; rlm@3: $tag = new $tag_class($this); rlm@3: $tag->Analyze(); rlm@3: } rlm@3: catch (getid3_exception $e) { rlm@3: throw $e; rlm@3: } rlm@3: } rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: //// Determine file format by magic bytes in file header. rlm@3: rlm@3: // Read 32 kb file data rlm@3: fseek($this->fp, $this->info['avdataoffset'], SEEK_SET); rlm@3: $filedata = fread($this->fp, 32774); rlm@3: rlm@3: // Get huge FileFormatArray rlm@3: $file_format_array = getid3::GetFileFormatArray(); rlm@3: rlm@3: // Identify file format - loop through $format_info and detect with reg expr rlm@3: foreach ($file_format_array as $name => $info) { rlm@3: rlm@3: if (preg_match('/'.$info['pattern'].'/s', $filedata)) { // The /s switch on preg_match() forces preg_match() NOT to treat newline (0x0A) characters as special chars but do a binary match rlm@3: rlm@3: // Format detected but not supported rlm@3: if (!@$info['module'] || !@$info['group']) { rlm@3: fclose($this->fp); rlm@3: $this->info['fileformat'] = $name; rlm@3: $this->info['mime_type'] = $info['mime_type']; rlm@3: $this->warning('Format only detected. Parsing not available yet.'); rlm@3: $this->info['warning'] = $this->warnings; rlm@3: return $this->info; rlm@3: } rlm@3: rlm@3: $determined_format = $info; // copy $info deleted by foreach() rlm@3: continue; rlm@3: } rlm@3: } rlm@3: rlm@3: // Unable to determine file format rlm@3: if (!@$determined_format) { rlm@3: rlm@3: // Too many mp3 encoders on the market put gabage in front of mpeg files rlm@3: // use assume format on these if format detection failed rlm@3: if (preg_match('/\.mp[123a]$/i', $filename)) { rlm@3: $determined_format = $file_format_array['mp3']; rlm@3: } rlm@3: rlm@3: else { rlm@3: fclose($this->fp); rlm@3: throw new getid3_exception('Unable to determine file format'); rlm@3: } rlm@3: } rlm@3: rlm@3: // Free memory rlm@3: unset($file_format_array); rlm@3: rlm@3: // Check for illegal ID3 tags rlm@3: if (@$determined_format['fail_id3'] && (@$this->info['id3v1'] || @$this->info['id3v2'])) { rlm@3: if ($determined_format['fail_id3'] === 'ERROR') { rlm@3: fclose($this->fp); rlm@3: throw new getid3_exception('ID3 tags not allowed on this file type.'); rlm@3: } rlm@3: elseif ($determined_format['fail_id3'] === 'WARNING') { rlm@3: @$this->info['id3v1'] and $this->warning('ID3v1 tags not allowed on this file type.'); rlm@3: @$this->info['id3v2'] and $this->warning('ID3v2 tags not allowed on this file type.'); rlm@3: } rlm@3: } rlm@3: rlm@3: // Check for illegal APE tags rlm@3: if (@$determined_format['fail_ape'] && @$this->info['tags']['ape']) { rlm@3: if ($determined_format['fail_ape'] === 'ERROR') { rlm@3: fclose($this->fp); rlm@3: throw new getid3_exception('APE tags not allowed on this file type.'); rlm@3: } elseif ($determined_format['fail_ape'] === 'WARNING') { rlm@3: $this->warning('APE tags not allowed on this file type.'); rlm@3: } rlm@3: } rlm@3: rlm@3: rlm@3: // Set mime type rlm@3: $this->info['mime_type'] = $determined_format['mime_type']; rlm@3: rlm@3: // Calc module file name rlm@3: $determined_format['include'] = 'module.'.$determined_format['group'].'.'.$determined_format['module'].'.php'; rlm@3: rlm@3: // Supported format signature pattern detected, but module deleted. rlm@3: if (!file_exists($this->include_path.$determined_format['include'])) { rlm@3: fclose($this->fp); rlm@3: throw new getid3_exception('Format not supported, module, '.$determined_format['include'].', was removed.'); rlm@3: } rlm@3: rlm@3: // Include module rlm@3: $this->include_module($determined_format['group'].'.'.$determined_format['module']); rlm@3: rlm@3: // Instantiate module class and analyze rlm@3: $class_name = 'getid3_'.$determined_format['module']; rlm@3: if (!class_exists($class_name)) { rlm@3: throw new getid3_exception('Format not supported, module, '.$determined_format['include'].', is corrupt.'); rlm@3: } rlm@3: $class = new $class_name($this); rlm@3: rlm@3: try { rlm@3: $this->option_analyze and $class->Analyze(); rlm@3: } rlm@3: catch (getid3_exception $e) { rlm@3: throw $e; rlm@3: } rlm@3: catch (Exception $e) { rlm@3: throw new getid3_exception('Corrupt file.'); rlm@3: } rlm@3: rlm@3: // Close file rlm@3: fclose($this->fp); rlm@3: rlm@3: // Optional - Process all tags - copy to 'tags' and convert charsets rlm@3: if ($this->option_tags_process) { rlm@3: $this->HandleAllTags(); rlm@3: } rlm@3: rlm@3: rlm@3: //// Optional - perform more calculations rlm@3: if ($this->option_extra_info) { rlm@3: rlm@3: // Set channelmode on audio rlm@3: if (@$this->info['audio']['channels'] == '1') { rlm@3: $this->info['audio']['channelmode'] = 'mono'; rlm@3: } elseif (@$this->info['audio']['channels'] == '2') { rlm@3: $this->info['audio']['channelmode'] = 'stereo'; rlm@3: } rlm@3: rlm@3: // Calculate combined bitrate - audio + video rlm@3: $combined_bitrate = 0; rlm@3: $combined_bitrate += (isset($this->info['audio']['bitrate']) ? $this->info['audio']['bitrate'] : 0); rlm@3: $combined_bitrate += (isset($this->info['video']['bitrate']) ? $this->info['video']['bitrate'] : 0); rlm@3: if (($combined_bitrate > 0) && empty($this->info['bitrate'])) { rlm@3: $this->info['bitrate'] = $combined_bitrate; rlm@3: } rlm@3: if (!isset($this->info['playtime_seconds']) && !empty($this->info['bitrate'])) { rlm@3: $this->info['playtime_seconds'] = (($this->info['avdataend'] - $this->info['avdataoffset']) * 8) / $this->info['bitrate']; rlm@3: } rlm@3: rlm@3: // Set playtime string rlm@3: if (!empty($this->info['playtime_seconds']) && empty($this->info['playtime_string'])) { rlm@3: $this->info['playtime_string'] = floor(round($this->info['playtime_seconds']) / 60) . ':' . str_pad(floor(round($this->info['playtime_seconds']) % 60), 2, 0, STR_PAD_LEFT);; rlm@3: } rlm@3: rlm@3: rlm@3: // CalculateCompressionRatioVideo() { rlm@3: if (@$this->info['video'] && @$this->info['video']['resolution_x'] && @$this->info['video']['resolution_y'] && @$this->info['video']['bits_per_sample']) { rlm@3: rlm@3: // From static image formats rlm@3: if (in_array($this->info['video']['dataformat'], array ('bmp', 'gif', 'jpeg', 'jpg', 'png', 'tiff'))) { rlm@3: $frame_rate = 1; rlm@3: $bitrate_compressed = $this->info['filesize'] * 8; rlm@3: } rlm@3: rlm@3: // From video formats rlm@3: else { rlm@3: $frame_rate = @$this->info['video']['frame_rate']; rlm@3: $bitrate_compressed = @$this->info['video']['bitrate']; rlm@3: } rlm@3: rlm@3: if ($frame_rate && $bitrate_compressed) { rlm@3: $this->info['video']['compression_ratio'] = $bitrate_compressed / ($this->info['video']['resolution_x'] * $this->info['video']['resolution_y'] * $this->info['video']['bits_per_sample'] * $frame_rate); rlm@3: } rlm@3: } rlm@3: rlm@3: rlm@3: // CalculateCompressionRatioAudio() { rlm@3: if (@$this->info['audio']['bitrate'] && @$this->info['audio']['channels'] && @$this->info['audio']['sample_rate']) { rlm@3: $this->info['audio']['compression_ratio'] = $this->info['audio']['bitrate'] / ($this->info['audio']['channels'] * $this->info['audio']['sample_rate'] * (@$this->info['audio']['bits_per_sample'] ? $this->info['audio']['bits_per_sample'] : 16)); rlm@3: } rlm@3: rlm@3: if (@$this->info['audio']['streams']) { rlm@3: foreach ($this->info['audio']['streams'] as $stream_number => $stream_data) { rlm@3: if (@$stream_data['bitrate'] && @$stream_data['channels'] && @$stream_data['sample_rate']) { rlm@3: $this->info['audio']['streams'][$stream_number]['compression_ratio'] = $stream_data['bitrate'] / ($stream_data['channels'] * $stream_data['sample_rate'] * (@$stream_data['bits_per_sample'] ? $stream_data['bits_per_sample'] : 16)); rlm@3: } rlm@3: } rlm@3: } rlm@3: rlm@3: rlm@3: // CalculateReplayGain() { rlm@3: if (@$this->info['replay_gain']) { rlm@3: if (!@$this->info['replay_gain']['reference_volume']) { rlm@3: $this->info['replay_gain']['reference_volume'] = 89; rlm@3: } rlm@3: if (isset($this->info['replay_gain']['track']['adjustment'])) { rlm@3: $this->info['replay_gain']['track']['volume'] = $this->info['replay_gain']['reference_volume'] - $this->info['replay_gain']['track']['adjustment']; rlm@3: } rlm@3: if (isset($this->info['replay_gain']['album']['adjustment'])) { rlm@3: $this->info['replay_gain']['album']['volume'] = $this->info['replay_gain']['reference_volume'] - $this->info['replay_gain']['album']['adjustment']; rlm@3: } rlm@3: rlm@3: if (isset($this->info['replay_gain']['track']['peak'])) { rlm@3: $this->info['replay_gain']['track']['max_noclip_gain'] = 0 - 20 * log10($this->info['replay_gain']['track']['peak']); rlm@3: } rlm@3: if (isset($this->info['replay_gain']['album']['peak'])) { rlm@3: $this->info['replay_gain']['album']['max_noclip_gain'] = 0 - 20 * log10($this->info['replay_gain']['album']['peak']); rlm@3: } rlm@3: } rlm@3: rlm@3: rlm@3: // ProcessAudioStreams() { rlm@3: if (@!$this->info['audio']['streams'] && (@$this->info['audio']['bitrate'] || @$this->info['audio']['channels'] || @$this->info['audio']['sample_rate'])) { rlm@3: foreach ($this->info['audio'] as $key => $value) { rlm@3: if ($key != 'streams') { rlm@3: $this->info['audio']['streams'][0][$key] = $value; rlm@3: } rlm@3: } rlm@3: } rlm@3: } rlm@3: rlm@3: rlm@3: // Get the md5/sha1sum of the audio/video portion of the file - without ID3/APE/Lyrics3/etc header/footer tags. rlm@3: if ($this->option_md5_data || $this->option_sha1_data) { rlm@3: rlm@3: // Load data-hash library if needed rlm@3: $this->include_module('lib.data_hash'); rlm@3: rlm@3: if ($this->option_sha1_data) { rlm@3: new getid3_lib_data_hash($this, 'sha1'); rlm@3: } rlm@3: rlm@3: if ($this->option_md5_data) { rlm@3: rlm@3: // no md5_data_source or option disabled -- md5_data_source supported by FLAC, MAC, OptimFROG, Wavpack4 rlm@3: if (!$this->option_md5_data_source || !@$this->info['md5_data_source']) { rlm@3: new getid3_lib_data_hash($this, 'md5'); rlm@3: } rlm@3: rlm@3: // copy md5_data_source to md5_data if option set to true rlm@3: elseif ($this->option_md5_data_source && @$this->info['md5_data_source']) { rlm@3: $this->info['md5_data'] = $this->info['md5_data_source']; rlm@3: } rlm@3: } rlm@3: } rlm@3: rlm@3: // Set warnings rlm@3: if ($this->warnings) { rlm@3: $this->info['warning'] = $this->warnings; rlm@3: } rlm@3: rlm@3: // Return result rlm@3: return $this->info; rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: // Return array of warnings rlm@3: public function warnings() { rlm@3: rlm@3: return $this->warnings; rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: // Add warning(s) to $this->warnings[] rlm@3: public function warning($message) { rlm@3: rlm@3: if (is_array($message)) { rlm@3: $this->warnings = array_merge($this->warnings, $message); rlm@3: } rlm@3: else { rlm@3: $this->warnings[] = $message; rlm@3: } rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: // Clear all warnings when cloning rlm@3: public function __clone() { rlm@3: rlm@3: $this->warnings = array (); rlm@3: rlm@3: // Copy info array, otherwise it will be a reference. rlm@3: $temp = $this->info; rlm@3: unset($this->info); rlm@3: $this->info = $temp; rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: // Convert string between charsets -- iconv() wrapper rlm@3: public function iconv($in_charset, $out_charset, $string, $drop01 = false) { rlm@3: rlm@3: if ($drop01 && ($string === "\x00" || $string === "\x01")) { rlm@3: return ''; rlm@3: } rlm@3: rlm@3: rlm@3: if (!$this->iconv_present) { rlm@3: return getid3_iconv_replacement::iconv($in_charset, $out_charset, $string); rlm@3: } rlm@3: rlm@3: rlm@3: // iconv() present rlm@3: if ($result = @iconv($in_charset, $out_charset.'//TRANSLIT', $string)) { rlm@3: rlm@3: if ($out_charset == 'ISO-8859-1') { rlm@3: return rtrim($result, "\x00"); rlm@3: } rlm@3: return $result; rlm@3: } rlm@3: rlm@3: $this->warning('iconv() was unable to convert the string: "' . $string . '" from ' . $in_charset . ' to ' . $out_charset); rlm@3: return $string; rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: public function include_module($name) { rlm@3: rlm@3: if (!file_exists($this->include_path.'module.'.$name.'.php')) { rlm@3: throw new getid3_exception('Required module.'.$name.'.php is missing.'); rlm@3: } rlm@3: rlm@3: include_once($this->include_path.'module.'.$name.'.php'); rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: public function include_module_optional($name) { rlm@3: rlm@3: if (!file_exists($this->include_path.'module.'.$name.'.php')) { rlm@3: return; rlm@3: } rlm@3: rlm@3: include_once($this->include_path.'module.'.$name.'.php'); rlm@3: return true; rlm@3: } rlm@3: rlm@3: rlm@3: // Return array containing information about all supported formats rlm@3: public static function GetFileFormatArray() { rlm@3: rlm@3: static $format_info = array ( rlm@3: rlm@3: // Audio formats rlm@3: rlm@3: // AC-3 - audio - Dolby AC-3 / Dolby Digital rlm@3: 'ac3' => array ( rlm@3: 'pattern' => '^\x0B\x77', rlm@3: 'group' => 'audio', rlm@3: 'module' => 'ac3', rlm@3: 'mime_type' => 'audio/ac3', rlm@3: ), rlm@3: rlm@3: // AAC - audio - Advanced Audio Coding (AAC) - ADIF format rlm@3: 'adif' => array ( rlm@3: 'pattern' => '^ADIF', rlm@3: 'group' => 'audio', rlm@3: 'module' => 'aac_adif', rlm@3: 'mime_type' => 'application/octet-stream', rlm@3: 'fail_ape' => 'WARNING', rlm@3: ), rlm@3: rlm@3: rlm@3: // AAC - audio - Advanced Audio Coding (AAC) - ADTS format (very similar to MP3) rlm@3: 'adts' => array ( rlm@3: 'pattern' => '^\xFF[\xF0-\xF1\xF8-\xF9]', rlm@3: 'group' => 'audio', rlm@3: 'module' => 'aac_adts', rlm@3: 'mime_type' => 'application/octet-stream', rlm@3: 'fail_ape' => 'WARNING', rlm@3: ), rlm@3: rlm@3: rlm@3: // AU - audio - NeXT/Sun AUdio (AU) rlm@3: 'au' => array ( rlm@3: 'pattern' => '^\.snd', rlm@3: 'group' => 'audio', rlm@3: 'module' => 'au', rlm@3: 'mime_type' => 'audio/basic', rlm@3: ), rlm@3: rlm@3: // AVR - audio - Audio Visual Research rlm@3: 'avr' => array ( rlm@3: 'pattern' => '^2BIT', rlm@3: 'group' => 'audio', rlm@3: 'module' => 'avr', rlm@3: 'mime_type' => 'application/octet-stream', rlm@3: ), rlm@3: rlm@3: // BONK - audio - Bonk v0.9+ rlm@3: 'bonk' => array ( rlm@3: 'pattern' => '^\x00(BONK|INFO|META| ID3)', rlm@3: 'group' => 'audio', rlm@3: 'module' => 'bonk', rlm@3: 'mime_type' => 'audio/xmms-bonk', rlm@3: ), rlm@3: rlm@3: // DTS - audio - Dolby Theatre System rlm@3: 'dts' => array( rlm@3: 'pattern' => '^\x7F\xFE\x80\x01', rlm@3: 'group' => 'audio', rlm@3: 'module' => 'dts', rlm@3: 'mime_type' => 'audio/dts', rlm@3: ), rlm@3: rlm@3: // FLAC - audio - Free Lossless Audio Codec rlm@3: 'flac' => array ( rlm@3: 'pattern' => '^fLaC', rlm@3: 'group' => 'audio', rlm@3: 'module' => 'xiph', rlm@3: 'mime_type' => 'audio/x-flac', rlm@3: ), rlm@3: rlm@3: // LA - audio - Lossless Audio (LA) rlm@3: 'la' => array ( rlm@3: 'pattern' => '^LA0[2-4]', rlm@3: 'group' => 'audio', rlm@3: 'module' => 'la', rlm@3: 'mime_type' => 'application/octet-stream', rlm@3: ), rlm@3: rlm@3: // LPAC - audio - Lossless Predictive Audio Compression (LPAC) rlm@3: 'lpac' => array ( rlm@3: 'pattern' => '^LPAC', rlm@3: 'group' => 'audio', rlm@3: 'module' => 'lpac', rlm@3: 'mime_type' => 'application/octet-stream', rlm@3: ), rlm@3: rlm@3: // MIDI - audio - MIDI (Musical Instrument Digital Interface) rlm@3: 'midi' => array ( rlm@3: 'pattern' => '^MThd', rlm@3: 'group' => 'audio', rlm@3: 'module' => 'midi', rlm@3: 'mime_type' => 'audio/midi', rlm@3: ), rlm@3: rlm@3: // MAC - audio - Monkey's Audio Compressor rlm@3: 'mac' => array ( rlm@3: 'pattern' => '^MAC ', rlm@3: 'group' => 'audio', rlm@3: 'module' => 'monkey', rlm@3: 'mime_type' => 'application/octet-stream', rlm@3: ), rlm@3: rlm@3: // MOD - audio - MODule (assorted sub-formats) rlm@3: 'mod' => array ( rlm@3: 'pattern' => '^.{1080}(M.K.|[5-9]CHN|[1-3][0-9]CH)', rlm@3: 'mime_type' => 'audio/mod', rlm@3: ), rlm@3: rlm@3: // MOD - audio - MODule (Impulse Tracker) rlm@3: 'it' => array ( rlm@3: 'pattern' => '^IMPM', rlm@3: 'mime_type' => 'audio/it', rlm@3: ), rlm@3: rlm@3: // MOD - audio - MODule (eXtended Module, various sub-formats) rlm@3: 'xm' => array ( rlm@3: 'pattern' => '^Extended Module', rlm@3: 'mime_type' => 'audio/xm', rlm@3: ), rlm@3: rlm@3: // MOD - audio - MODule (ScreamTracker) rlm@3: 's3m' => array ( rlm@3: 'pattern' => '^.{44}SCRM', rlm@3: 'mime_type' => 'audio/s3m', rlm@3: ), rlm@3: rlm@3: // MPC - audio - Musepack / MPEGplus SV7+ rlm@3: 'mpc' => array ( rlm@3: 'pattern' => '^(MP\+)', rlm@3: 'group' => 'audio', rlm@3: 'module' => 'mpc', rlm@3: 'mime_type' => 'audio/x-musepack', rlm@3: ), rlm@3: rlm@3: // MPC - audio - Musepack / MPEGplus SV4-6 rlm@3: 'mpc_old' => array ( rlm@3: 'pattern' => '^([\x00\x01\x10\x11\x40\x41\x50\x51\x80\x81\x90\x91\xC0\xC1\xD0\xD1][\x20-37][\x00\x20\x40\x60\x80\xA0\xC0\xE0])', rlm@3: 'group' => 'audio', rlm@3: 'module' => 'mpc_old', rlm@3: 'mime_type' => 'application/octet-stream', rlm@3: ), rlm@3: rlm@3: rlm@3: // MP3 - audio - MPEG-audio Layer 3 (very similar to AAC-ADTS) rlm@3: 'mp3' => array ( rlm@3: 'pattern' => '^\xFF[\xE2-\xE7\xF2-\xF7\xFA-\xFF][\x00-\xEB]', rlm@3: 'group' => 'audio', rlm@3: 'module' => 'mp3', rlm@3: 'mime_type' => 'audio/mpeg', rlm@3: ), rlm@3: rlm@3: // OFR - audio - OptimFROG rlm@3: 'ofr' => array ( rlm@3: 'pattern' => '^(\*RIFF|OFR)', rlm@3: 'group' => 'audio', rlm@3: 'module' => 'optimfrog', rlm@3: 'mime_type' => 'application/octet-stream', rlm@3: ), rlm@3: rlm@3: // RKAU - audio - RKive AUdio compressor rlm@3: 'rkau' => array ( rlm@3: 'pattern' => '^RKA', rlm@3: 'group' => 'audio', rlm@3: 'module' => 'rkau', rlm@3: 'mime_type' => 'application/octet-stream', rlm@3: ), rlm@3: rlm@3: // SHN - audio - Shorten rlm@3: 'shn' => array ( rlm@3: 'pattern' => '^ajkg', rlm@3: 'group' => 'audio', rlm@3: 'module' => 'shorten', rlm@3: 'mime_type' => 'audio/xmms-shn', rlm@3: 'fail_id3' => 'ERROR', rlm@3: 'fail_ape' => 'ERROR', rlm@3: ), rlm@3: rlm@3: // TTA - audio - TTA Lossless Audio Compressor (http://tta.corecodec.org) rlm@3: 'tta' => array ( rlm@3: 'pattern' => '^TTA', // could also be '^TTA(\x01|\x02|\x03|2|1)' rlm@3: 'group' => 'audio', rlm@3: 'module' => 'tta', rlm@3: 'mime_type' => 'application/octet-stream', rlm@3: ), rlm@3: rlm@3: // VOC - audio - Creative Voice (VOC) rlm@3: 'voc' => array ( rlm@3: 'pattern' => '^Creative Voice File', rlm@3: 'group' => 'audio', rlm@3: 'module' => 'voc', rlm@3: 'mime_type' => 'audio/voc', rlm@3: ), rlm@3: rlm@3: // VQF - audio - transform-domain weighted interleave Vector Quantization Format (VQF) rlm@3: 'vqf' => array ( rlm@3: 'pattern' => '^TWIN', rlm@3: 'group' => 'audio', rlm@3: 'module' => 'vqf', rlm@3: 'mime_type' => 'application/octet-stream', rlm@3: ), rlm@3: rlm@3: // WV - audio - WavPack (v4.0+) rlm@3: 'vw' => array( rlm@3: 'pattern' => '^wvpk', rlm@3: 'group' => 'audio', rlm@3: 'module' => 'wavpack', rlm@3: 'mime_type' => 'application/octet-stream', rlm@3: ), rlm@3: rlm@3: rlm@3: // Audio-Video formats rlm@3: rlm@3: // ASF - audio/video - Advanced Streaming Format, Windows Media Video, Windows Media Audio rlm@3: 'asf' => array ( rlm@3: 'pattern' => '^\x30\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C', rlm@3: 'group' => 'audio-video', rlm@3: 'module' => 'asf', rlm@3: 'mime_type' => 'video/x-ms-asf', rlm@3: ), rlm@3: rlm@3: // BINK - audio/video - Bink / Smacker rlm@3: 'bink' => array( rlm@3: 'pattern' => '^(BIK|SMK)', rlm@3: 'mime_type' => 'application/octet-stream', rlm@3: ), rlm@3: rlm@3: // FLV - audio/video - FLash Video rlm@3: 'flv' => array( rlm@3: 'pattern' => '^FLV\x01', rlm@3: 'group' => 'audio-video', rlm@3: 'module' => 'flv', rlm@3: 'mime_type' => 'video/x-flv', rlm@3: ), rlm@3: rlm@3: // MKAV - audio/video - Mastroka rlm@3: 'matroska' => array ( rlm@3: 'pattern' => '^\x1A\x45\xDF\xA3', rlm@3: 'mime_type' => 'application/octet-stream', rlm@3: ), rlm@3: rlm@3: // MPEG - audio/video - MPEG (Moving Pictures Experts Group) rlm@3: 'mpeg' => array ( rlm@3: 'pattern' => '^\x00\x00\x01(\xBA|\xB3)', rlm@3: 'group' => 'audio-video', rlm@3: 'module' => 'mpeg', rlm@3: 'mime_type' => 'video/mpeg', rlm@3: ), rlm@3: rlm@3: // NSV - audio/video - Nullsoft Streaming Video (NSV) rlm@3: 'nsv' => array ( rlm@3: 'pattern' => '^NSV[sf]', rlm@3: 'group' => 'audio-video', rlm@3: 'module' => 'nsv', rlm@3: 'mime_type' => 'application/octet-stream', rlm@3: ), rlm@3: rlm@3: // Ogg - audio/video - Ogg (Ogg Vorbis, OggFLAC, Speex, Ogg Theora(*), Ogg Tarkin(*)) rlm@3: 'ogg' => array ( rlm@3: 'pattern' => '^OggS', rlm@3: 'group' => 'audio', rlm@3: 'module' => 'xiph', rlm@3: 'mime_type' => 'application/ogg', rlm@3: 'fail_id3' => 'WARNING', rlm@3: 'fail_ape' => 'WARNING', rlm@3: ), rlm@3: rlm@3: // QT - audio/video - Quicktime rlm@3: 'quicktime' => array ( rlm@3: 'pattern' => '^.{4}(cmov|free|ftyp|mdat|moov|pnot|skip|wide)', rlm@3: 'group' => 'audio-video', rlm@3: 'module' => 'quicktime', rlm@3: 'mime_type' => 'video/quicktime', rlm@3: ), rlm@3: rlm@3: // 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) rlm@3: 'riff' => array ( rlm@3: 'pattern' => '^(RIFF|SDSS|FORM)', rlm@3: 'group' => 'audio-video', rlm@3: 'module' => 'riff', rlm@3: 'mime_type' => 'audio/x-wave', rlm@3: 'fail_ape' => 'WARNING', rlm@3: ), rlm@3: rlm@3: // Real - audio/video - RealAudio, RealVideo rlm@3: 'real' => array ( rlm@3: 'pattern' => '^(\.RMF|.ra)', rlm@3: 'group' => 'audio-video', rlm@3: 'module' => 'real', rlm@3: 'mime_type' => 'audio/x-realaudio', rlm@3: ), rlm@3: rlm@3: // SWF - audio/video - ShockWave Flash rlm@3: 'swf' => array ( rlm@3: 'pattern' => '^(F|C)WS', rlm@3: 'group' => 'audio-video', rlm@3: 'module' => 'swf', rlm@3: 'mime_type' => 'application/x-shockwave-flash', rlm@3: ), rlm@3: rlm@3: rlm@3: // Still-Image formats rlm@3: rlm@3: // BMP - still image - Bitmap (Windows, OS/2; uncompressed, RLE8, RLE4) rlm@3: 'bmp' => array ( rlm@3: 'pattern' => '^BM', rlm@3: 'group' => 'graphic', rlm@3: 'module' => 'bmp', rlm@3: 'mime_type' => 'image/bmp', rlm@3: 'fail_id3' => 'ERROR', rlm@3: 'fail_ape' => 'ERROR', rlm@3: ), rlm@3: rlm@3: // GIF - still image - Graphics Interchange Format rlm@3: 'gif' => array ( rlm@3: 'pattern' => '^GIF', rlm@3: 'group' => 'graphic', rlm@3: 'module' => 'gif', rlm@3: 'mime_type' => 'image/gif', rlm@3: 'fail_id3' => 'ERROR', rlm@3: 'fail_ape' => 'ERROR', rlm@3: ), rlm@3: rlm@3: // JPEG - still image - Joint Photographic Experts Group (JPEG) rlm@3: 'jpeg' => array ( rlm@3: 'pattern' => '^\xFF\xD8\xFF', rlm@3: 'group' => 'graphic', rlm@3: 'module' => 'jpeg', rlm@3: 'mime_type' => 'image/jpeg', rlm@3: 'fail_id3' => 'ERROR', rlm@3: 'fail_ape' => 'ERROR', rlm@3: ), rlm@3: rlm@3: // PCD - still image - Kodak Photo CD rlm@3: 'pcd' => array ( rlm@3: 'pattern' => '^.{2048}PCD_IPI\x00', rlm@3: 'group' => 'graphic', rlm@3: 'module' => 'pcd', rlm@3: 'mime_type' => 'image/x-photo-cd', rlm@3: 'fail_id3' => 'ERROR', rlm@3: 'fail_ape' => 'ERROR', rlm@3: ), rlm@3: rlm@3: rlm@3: // PNG - still image - Portable Network Graphics (PNG) rlm@3: 'png' => array ( rlm@3: 'pattern' => '^\x89\x50\x4E\x47\x0D\x0A\x1A\x0A', rlm@3: 'group' => 'graphic', rlm@3: 'module' => 'png', rlm@3: 'mime_type' => 'image/png', rlm@3: 'fail_id3' => 'ERROR', rlm@3: 'fail_ape' => 'ERROR', rlm@3: ), rlm@3: rlm@3: rlm@3: // SVG - still image - Scalable Vector Graphics (SVG) rlm@3: 'svg' => array( rlm@3: 'pattern' => ' 'image/svg+xml', rlm@3: 'fail_id3' => 'ERROR', rlm@3: 'fail_ape' => 'ERROR', rlm@3: ), rlm@3: rlm@3: rlm@3: // TIFF - still image - Tagged Information File Format (TIFF) rlm@3: 'tiff' => array ( rlm@3: 'pattern' => '^(II\x2A\x00|MM\x00\x2A)', rlm@3: 'group' => 'graphic', rlm@3: 'module' => 'tiff', rlm@3: 'mime_type' => 'image/tiff', rlm@3: 'fail_id3' => 'ERROR', rlm@3: 'fail_ape' => 'ERROR', rlm@3: ), rlm@3: rlm@3: rlm@3: // Data formats rlm@3: rlm@3: 'exe' => array( rlm@3: 'pattern' => '^MZ', rlm@3: 'mime_type' => 'application/octet-stream', rlm@3: 'fail_id3' => 'ERROR', rlm@3: 'fail_ape' => 'ERROR', rlm@3: ), rlm@3: rlm@3: // ISO - data - International Standards Organization (ISO) CD-ROM Image rlm@3: 'iso' => array ( rlm@3: 'pattern' => '^.{32769}CD001', rlm@3: 'group' => 'misc', rlm@3: 'module' => 'iso', rlm@3: 'mime_type' => 'application/octet-stream', rlm@3: 'fail_id3' => 'ERROR', rlm@3: 'fail_ape' => 'ERROR', rlm@3: ), rlm@3: rlm@3: // RAR - data - RAR compressed data rlm@3: 'rar' => array( rlm@3: 'pattern' => '^Rar\!', rlm@3: 'mime_type' => 'application/octet-stream', rlm@3: 'fail_id3' => 'ERROR', rlm@3: 'fail_ape' => 'ERROR', rlm@3: ), rlm@3: rlm@3: // SZIP - audio - SZIP compressed data rlm@3: 'szip' => array ( rlm@3: 'pattern' => '^SZ\x0A\x04', rlm@3: 'group' => 'archive', rlm@3: 'module' => 'szip', rlm@3: 'mime_type' => 'application/octet-stream', rlm@3: 'fail_id3' => 'ERROR', rlm@3: 'fail_ape' => 'ERROR', rlm@3: ), rlm@3: rlm@3: // TAR - data - TAR compressed data rlm@3: 'tar' => array( rlm@3: '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}', rlm@3: 'group' => 'archive', rlm@3: 'module' => 'tar', rlm@3: 'mime_type' => 'application/x-tar', rlm@3: 'fail_id3' => 'ERROR', rlm@3: 'fail_ape' => 'ERROR', rlm@3: ), rlm@3: rlm@3: // GZIP - data - GZIP compressed data rlm@3: 'gz' => array( rlm@3: 'pattern' => '^\x1F\x8B\x08', rlm@3: 'group' => 'archive', rlm@3: 'module' => 'gzip', rlm@3: 'mime_type' => 'application/x-gzip', rlm@3: 'fail_id3' => 'ERROR', rlm@3: 'fail_ape' => 'ERROR', rlm@3: ), rlm@3: rlm@3: rlm@3: // ZIP - data - ZIP compressed data rlm@3: 'zip' => array ( rlm@3: 'pattern' => '^PK\x03\x04', rlm@3: 'group' => 'archive', rlm@3: 'module' => 'zip', rlm@3: 'mime_type' => 'application/zip', rlm@3: 'fail_id3' => 'ERROR', rlm@3: 'fail_ape' => 'ERROR', rlm@3: ), rlm@3: rlm@3: rlm@3: // PAR2 - data - Parity Volume Set Specification 2.0 rlm@3: 'par2' => array ( rlm@3: 'pattern' => '^PAR2\x00PKT', rlm@3: 'mime_type' => 'application/octet-stream', rlm@3: 'fail_id3' => 'ERROR', rlm@3: 'fail_ape' => 'ERROR', rlm@3: ), rlm@3: rlm@3: rlm@3: // PDF - data - Portable Document Format rlm@3: 'pdf' => array( rlm@3: 'pattern' => '^\x25PDF', rlm@3: 'mime_type' => 'application/pdf', rlm@3: 'fail_id3' => 'ERROR', rlm@3: 'fail_ape' => 'ERROR', rlm@3: ), rlm@3: rlm@3: // DOC - data - Microsoft Word rlm@3: 'msoffice' => array( rlm@3: 'pattern' => '^\xD0\xCF\x11\xE0', // D0CF11E == DOCFILE == Microsoft Office Document rlm@3: 'mime_type' => 'application/octet-stream', rlm@3: 'fail_id3' => 'ERROR', rlm@3: 'fail_ape' => 'ERROR', rlm@3: ), rlm@3: ); rlm@3: rlm@3: return $format_info; rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: // Recursive over array - converts array to $encoding charset from $this->encoding rlm@3: function CharConvert(&$array, $encoding) { rlm@3: rlm@3: // Identical encoding - end here rlm@3: if ($encoding == $this->encoding) { rlm@3: return; rlm@3: } rlm@3: rlm@3: // Loop thru array rlm@3: foreach ($array as $key => $value) { rlm@3: rlm@3: // Go recursive rlm@3: if (is_array($value)) { rlm@3: $this->CharConvert($array[$key], $encoding); rlm@3: } rlm@3: rlm@3: // Convert string rlm@3: elseif (is_string($value)) { rlm@3: $array[$key] = $this->iconv($encoding, $this->encoding, $value); rlm@3: } rlm@3: } rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: // Convert and copy tags rlm@3: protected function HandleAllTags() { rlm@3: rlm@3: // Key name => array (tag name, character encoding) rlm@3: static $tags = array ( rlm@3: 'asf' => array ('asf', 'UTF-16LE'), rlm@3: 'midi' => array ('midi', 'ISO-8859-1'), rlm@3: 'nsv' => array ('nsv', 'ISO-8859-1'), rlm@3: 'ogg' => array ('vorbiscomment', 'UTF-8'), rlm@3: 'png' => array ('png', 'UTF-8'), rlm@3: 'tiff' => array ('tiff', 'ISO-8859-1'), rlm@3: 'quicktime' => array ('quicktime', 'ISO-8859-1'), rlm@3: 'real' => array ('real', 'ISO-8859-1'), rlm@3: 'vqf' => array ('vqf', 'ISO-8859-1'), rlm@3: 'zip' => array ('zip', 'ISO-8859-1'), rlm@3: 'riff' => array ('riff', 'ISO-8859-1'), rlm@3: 'lyrics3' => array ('lyrics3', 'ISO-8859-1'), rlm@3: 'id3v1' => array ('id3v1', ''), // change below - cannot assign variable to static array rlm@3: 'id3v2' => array ('id3v2', 'UTF-8'), // module converts all frames to UTF-8 rlm@3: 'ape' => array ('ape', 'UTF-8') rlm@3: ); rlm@3: $tags['id3v1'][1] = $this->encoding_id3v1; rlm@3: rlm@3: // Loop thru tags array rlm@3: foreach ($tags as $comment_name => $tag_name_encoding_array) { rlm@3: list($tag_name, $encoding) = $tag_name_encoding_array; rlm@3: rlm@3: // Fill in default encoding type if not already present rlm@3: @$this->info[$comment_name] and $this->info[$comment_name]['encoding'] = $encoding; rlm@3: rlm@3: // Copy comments if key name set rlm@3: if (@$this->info[$comment_name]['comments']) { rlm@3: rlm@3: foreach ($this->info[$comment_name]['comments'] as $tag_key => $value_array) { rlm@3: foreach ($value_array as $key => $value) { rlm@3: if (strlen(trim($value)) > 0) { rlm@3: $this->info['tags'][$tag_name][trim($tag_key)][] = $value; // do not trim!! Unicode characters will get mangled if trailing nulls are removed! rlm@3: } rlm@3: } rlm@3: rlm@3: } rlm@3: rlm@3: if (!@$this->info['tags'][$tag_name]) { rlm@3: // comments are set but contain nothing but empty strings, so skip rlm@3: continue; rlm@3: } rlm@3: rlm@3: $this->CharConvert($this->info['tags'][$tag_name], $encoding); rlm@3: } rlm@3: } rlm@3: rlm@3: rlm@3: // Merge comments from ['tags'] into common ['comments'] rlm@3: if (@$this->info['tags']) { rlm@3: rlm@3: foreach ($this->info['tags'] as $tag_type => $tag_array) { rlm@3: rlm@3: foreach ($tag_array as $tag_name => $tagdata) { rlm@3: rlm@3: foreach ($tagdata as $key => $value) { rlm@3: rlm@3: if (!empty($value)) { rlm@3: rlm@3: if (empty($this->info['comments'][$tag_name])) { rlm@3: rlm@3: // fall through and append value rlm@3: } rlm@3: elseif ($tag_type == 'id3v1') { rlm@3: rlm@3: $new_value_length = strlen(trim($value)); rlm@3: foreach ($this->info['comments'][$tag_name] as $existing_key => $existing_value) { rlm@3: $old_value_length = strlen(trim($existing_value)); rlm@3: if (($new_value_length <= $old_value_length) && (substr($existing_value, 0, $new_value_length) == trim($value))) { rlm@3: // new value is identical but shorter-than (or equal-length to) one already in comments - skip rlm@3: break 2; rlm@3: } rlm@3: } rlm@3: } rlm@3: else { rlm@3: rlm@3: $new_value_length = strlen(trim($value)); rlm@3: foreach ($this->info['comments'][$tag_name] as $existing_key => $existing_value) { rlm@3: $old_value_length = strlen(trim($existing_value)); rlm@3: if (($new_value_length > $old_value_length) && (substr(trim($value), 0, strlen($existing_value)) == $existing_value)) { rlm@3: $this->info['comments'][$tag_name][$existing_key] = trim($value); rlm@3: break 2; rlm@3: } rlm@3: } rlm@3: } rlm@3: rlm@3: if (empty($this->info['comments'][$tag_name]) || !in_array(trim($value), $this->info['comments'][$tag_name])) { rlm@3: $this->info['comments'][$tag_name][] = trim($value); rlm@3: } rlm@3: } rlm@3: } rlm@3: } rlm@3: } rlm@3: } rlm@3: rlm@3: return true; rlm@3: } rlm@3: } rlm@3: rlm@3: rlm@3: abstract class getid3_handler rlm@3: { rlm@3: rlm@3: protected $getid3; // pointer rlm@3: rlm@3: protected $data_string_flag = false; // analyzing filepointer or string rlm@3: protected $data_string; // string to analyze rlm@3: protected $data_string_position = 0; // seek position in string rlm@3: rlm@3: rlm@3: public function __construct(getID3 $getid3) { rlm@3: rlm@3: $this->getid3 = $getid3; rlm@3: } rlm@3: rlm@3: rlm@3: // Analyze from file pointer rlm@3: abstract public function Analyze(); rlm@3: rlm@3: rlm@3: rlm@3: // Analyze from string instead rlm@3: public function AnalyzeString(&$string) { rlm@3: rlm@3: // Enter string mode rlm@3: $this->data_string_flag = true; rlm@3: $this->data_string = $string; rlm@3: rlm@3: // Save info rlm@3: $saved_avdataoffset = $this->getid3->info['avdataoffset']; rlm@3: $saved_avdataend = $this->getid3->info['avdataend']; rlm@3: $saved_filesize = $this->getid3->info['filesize']; rlm@3: rlm@3: // Reset some info rlm@3: $this->getid3->info['avdataoffset'] = 0; rlm@3: $this->getid3->info['avdataend'] = $this->getid3->info['filesize'] = strlen($string); rlm@3: rlm@3: // Analyze rlm@3: $this->Analyze(); rlm@3: rlm@3: // Restore some info rlm@3: $this->getid3->info['avdataoffset'] = $saved_avdataoffset; rlm@3: $this->getid3->info['avdataend'] = $saved_avdataend; rlm@3: $this->getid3->info['filesize'] = $saved_filesize; rlm@3: rlm@3: // Exit string mode rlm@3: $this->data_string_flag = false; rlm@3: } rlm@3: rlm@3: rlm@3: protected function ftell() { rlm@3: rlm@3: if ($this->data_string_flag) { rlm@3: return $this->data_string_position; rlm@3: } rlm@3: return ftell($this->getid3->fp); rlm@3: } rlm@3: rlm@3: rlm@3: protected function fread($bytes) { rlm@3: rlm@3: if ($this->data_string_flag) { rlm@3: $this->data_string_position += $bytes; rlm@3: return substr($this->data_string, $this->data_string_position - $bytes, $bytes); rlm@3: } rlm@3: return fread($this->getid3->fp, $bytes); rlm@3: } rlm@3: rlm@3: rlm@3: protected function fseek($bytes, $whence = SEEK_SET) { rlm@3: rlm@3: if ($this->data_string_flag) { rlm@3: switch ($whence) { rlm@3: case SEEK_SET: rlm@3: $this->data_string_position = $bytes; rlm@3: return; rlm@3: rlm@3: case SEEK_CUR: rlm@3: $this->data_string_position += $bytes; rlm@3: return; rlm@3: rlm@3: case SEEK_END: rlm@3: $this->data_string_position = strlen($this->data_string) + $bytes; rlm@3: return; rlm@3: } rlm@3: } rlm@3: return fseek($this->getid3->fp, $bytes, $whence); rlm@3: } rlm@3: rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: rlm@3: abstract class getid3_handler_write rlm@3: { rlm@3: protected $filename; rlm@3: protected $user_abort; rlm@3: rlm@3: private $fp_lock; rlm@3: private $owner; rlm@3: private $group; rlm@3: private $perms; rlm@3: rlm@3: rlm@3: public function __construct($filename) { rlm@3: rlm@3: if (!file_exists($filename)) { rlm@3: throw new getid3_exception('File does not exist: "' . $filename . '"'); rlm@3: } rlm@3: rlm@3: if (!is_writeable($filename)) { rlm@3: throw new getid3_exception('File is not writeable: "' . $filename . '"'); rlm@3: } rlm@3: rlm@3: if (!is_writeable(dirname($filename))) { rlm@3: throw new getid3_exception('Directory is not writeable: ' . dirname($filename) . ' (need to write lock file).'); rlm@3: } rlm@3: rlm@3: $this->user_abort = ignore_user_abort(true); rlm@3: rlm@3: $this->fp_lock = fopen($filename . '.getid3.lock', 'w'); rlm@3: flock($this->fp_lock, LOCK_EX); rlm@3: rlm@3: $this->filename = $filename; rlm@3: } rlm@3: rlm@3: rlm@3: public function __destruct() { rlm@3: rlm@3: flock($this->fp_lock, LOCK_UN); rlm@3: fclose($this->fp_lock); rlm@3: unlink($this->filename . '.getid3.lock'); rlm@3: rlm@3: ignore_user_abort($this->user_abort); rlm@3: } rlm@3: rlm@3: rlm@3: protected function save_permissions() { rlm@3: rlm@3: $this->owner = fileowner($this->filename); rlm@3: $this->group = filegroup($this->filename); rlm@3: $this->perms = fileperms($this->filename); rlm@3: } rlm@3: rlm@3: rlm@3: protected function restore_permissions() { rlm@3: rlm@3: @chown($this->filename, $this->owner); rlm@3: @chgrp($this->filename, $this->group); rlm@3: @chmod($this->filename, $this->perms); rlm@3: } rlm@3: rlm@3: rlm@3: abstract public function read(); rlm@3: rlm@3: abstract public function write(); rlm@3: rlm@3: abstract public function remove(); rlm@3: rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: rlm@3: class getid3_exception extends Exception rlm@3: { rlm@3: public $message; rlm@3: rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: rlm@3: class getid3_lib rlm@3: { rlm@3: rlm@3: // Convert Little Endian byte string to int - max 32 bits rlm@3: public static function LittleEndian2Int($byte_word, $signed = false) { rlm@3: rlm@3: return getid3_lib::BigEndian2Int(strrev($byte_word), $signed); rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: // Convert number to Little Endian byte string rlm@3: public static function LittleEndian2String($number, $minbytes=1, $synchsafe=false) { rlm@3: $intstring = ''; rlm@3: while ($number > 0) { rlm@3: if ($synchsafe) { rlm@3: $intstring = $intstring.chr($number & 127); rlm@3: $number >>= 7; rlm@3: } else { rlm@3: $intstring = $intstring.chr($number & 255); rlm@3: $number >>= 8; rlm@3: } rlm@3: } rlm@3: return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT); rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: // Convert Big Endian byte string to int - max 32 bits rlm@3: public static function BigEndian2Int($byte_word, $signed = false) { rlm@3: rlm@3: $int_value = 0; rlm@3: $byte_wordlen = strlen($byte_word); rlm@3: rlm@3: for ($i = 0; $i < $byte_wordlen; $i++) { rlm@3: $int_value += ord($byte_word{$i}) * pow(256, ($byte_wordlen - 1 - $i)); rlm@3: } rlm@3: rlm@3: if ($signed) { rlm@3: $sign_mask_bit = 0x80 << (8 * ($byte_wordlen - 1)); rlm@3: if ($int_value & $sign_mask_bit) { rlm@3: $int_value = 0 - ($int_value & ($sign_mask_bit - 1)); rlm@3: } rlm@3: } rlm@3: rlm@3: return $int_value; rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: // Convert Big Endian byte sybc safe string to int - max 32 bits rlm@3: public static function BigEndianSyncSafe2Int($byte_word) { rlm@3: rlm@3: $int_value = 0; rlm@3: $byte_wordlen = strlen($byte_word); rlm@3: rlm@3: // disregard MSB, effectively 7-bit bytes rlm@3: for ($i = 0; $i < $byte_wordlen; $i++) { rlm@3: $int_value = $int_value | (ord($byte_word{$i}) & 0x7F) << (($byte_wordlen - 1 - $i) * 7); rlm@3: } rlm@3: return $int_value; rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: // Convert Big Endian byte string to bit string rlm@3: public static function BigEndian2Bin($byte_word) { rlm@3: rlm@3: $bin_value = ''; rlm@3: $byte_wordlen = strlen($byte_word); rlm@3: for ($i = 0; $i < $byte_wordlen; $i++) { rlm@3: $bin_value .= str_pad(decbin(ord($byte_word{$i})), 8, '0', STR_PAD_LEFT); rlm@3: } rlm@3: return $bin_value; rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: public static function BigEndian2Float($byte_word) { rlm@3: rlm@3: // ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic rlm@3: // http://www.psc.edu/general/software/packages/ieee/ieee.html rlm@3: // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html rlm@3: rlm@3: $bit_word = getid3_lib::BigEndian2Bin($byte_word); rlm@3: if (!$bit_word) { rlm@3: return 0; rlm@3: } rlm@3: $sign_bit = $bit_word{0}; rlm@3: rlm@3: switch (strlen($byte_word) * 8) { rlm@3: case 32: rlm@3: $exponent_bits = 8; rlm@3: $fraction_bits = 23; rlm@3: break; rlm@3: rlm@3: case 64: rlm@3: $exponent_bits = 11; rlm@3: $fraction_bits = 52; rlm@3: break; rlm@3: rlm@3: case 80: rlm@3: // 80-bit Apple SANE format rlm@3: // http://www.mactech.com/articles/mactech/Vol.06/06.01/SANENormalized/ rlm@3: $exponent_string = substr($bit_word, 1, 15); rlm@3: $is_normalized = intval($bit_word{16}); rlm@3: $fraction_string = substr($bit_word, 17, 63); rlm@3: $exponent = pow(2, getid3_lib::Bin2Dec($exponent_string) - 16383); rlm@3: $fraction = $is_normalized + getid3_lib::DecimalBinary2Float($fraction_string); rlm@3: $float_value = $exponent * $fraction; rlm@3: if ($sign_bit == '1') { rlm@3: $float_value *= -1; rlm@3: } rlm@3: return $float_value; rlm@3: break; rlm@3: rlm@3: default: rlm@3: return false; rlm@3: break; rlm@3: } rlm@3: $exponent_string = substr($bit_word, 1, $exponent_bits); rlm@3: $fraction_string = substr($bit_word, $exponent_bits + 1, $fraction_bits); rlm@3: $exponent = bindec($exponent_string); rlm@3: $fraction = bindec($fraction_string); rlm@3: rlm@3: if (($exponent == (pow(2, $exponent_bits) - 1)) && ($fraction != 0)) { rlm@3: // Not a Number rlm@3: $float_value = false; rlm@3: } elseif (($exponent == (pow(2, $exponent_bits) - 1)) && ($fraction == 0)) { rlm@3: if ($sign_bit == '1') { rlm@3: $float_value = '-infinity'; rlm@3: } else { rlm@3: $float_value = '+infinity'; rlm@3: } rlm@3: } elseif (($exponent == 0) && ($fraction == 0)) { rlm@3: if ($sign_bit == '1') { rlm@3: $float_value = -0; rlm@3: } else { rlm@3: $float_value = 0; rlm@3: } rlm@3: $float_value = ($sign_bit ? 0 : -0); rlm@3: } elseif (($exponent == 0) && ($fraction != 0)) { rlm@3: // These are 'unnormalized' values rlm@3: $float_value = pow(2, (-1 * (pow(2, $exponent_bits - 1) - 2))) * getid3_lib::DecimalBinary2Float($fraction_string); rlm@3: if ($sign_bit == '1') { rlm@3: $float_value *= -1; rlm@3: } rlm@3: } elseif ($exponent != 0) { rlm@3: $float_value = pow(2, ($exponent - (pow(2, $exponent_bits - 1) - 1))) * (1 + getid3_lib::DecimalBinary2Float($fraction_string)); rlm@3: if ($sign_bit == '1') { rlm@3: $float_value *= -1; rlm@3: } rlm@3: } rlm@3: return (float) $float_value; rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: public static function LittleEndian2Float($byte_word) { rlm@3: rlm@3: return getid3_lib::BigEndian2Float(strrev($byte_word)); rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: public static function DecimalBinary2Float($binary_numerator) { rlm@3: $numerator = bindec($binary_numerator); rlm@3: $denominator = bindec('1'.str_repeat('0', strlen($binary_numerator))); rlm@3: return ($numerator / $denominator); rlm@3: } rlm@3: rlm@3: rlm@3: public static function PrintHexBytes($string, $hex=true, $spaces=true, $html_safe=true) { rlm@3: rlm@3: $return_string = ''; rlm@3: for ($i = 0; $i < strlen($string); $i++) { rlm@3: if ($hex) { rlm@3: $return_string .= str_pad(dechex(ord($string{$i})), 2, '0', STR_PAD_LEFT); rlm@3: } else { rlm@3: $return_string .= ' '.(ereg("[\x20-\x7E]", $string{$i}) ? $string{$i} : ''); rlm@3: } rlm@3: if ($spaces) { rlm@3: $return_string .= ' '; rlm@3: } rlm@3: } rlm@3: if ($html_safe) { rlm@3: $return_string = htmlentities($return_string); rlm@3: } rlm@3: return $return_string; rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: // Process header data string - read several values with algorithm and add to target rlm@3: // algorithm is one one the getid3_lib::Something2Something() function names rlm@3: // parts_array is index => length - $target[index] = algorithm(substring(data)) rlm@3: // - OR just substring(data) if length is negative! rlm@3: // indexes == 'IGNORE**' are ignored rlm@3: rlm@3: public static function ReadSequence($algorithm, &$target, &$data, $offset, $parts_array) { rlm@3: rlm@3: // Loop thru $parts_array rlm@3: foreach ($parts_array as $target_string => $length) { rlm@3: rlm@3: // Add to target rlm@3: if (!strstr($target_string, 'IGNORE')) { rlm@3: rlm@3: // substr(....length) rlm@3: if ($length < 0) { rlm@3: $target[$target_string] = substr($data, $offset, -$length); rlm@3: } rlm@3: rlm@3: // algorithm(substr(...length)) rlm@3: else { rlm@3: $target[$target_string] = getid3_lib::$algorithm(substr($data, $offset, $length)); rlm@3: } rlm@3: } rlm@3: rlm@3: // Move pointer rlm@3: $offset += abs($length); rlm@3: } rlm@3: } rlm@3: rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: class getid3_lib_replaygain rlm@3: { rlm@3: rlm@3: public static function NameLookup($name_code) { rlm@3: rlm@3: static $lookup = array ( rlm@3: 0 => 'not set', rlm@3: 1 => 'Track Gain Adjustment', rlm@3: 2 => 'Album Gain Adjustment' rlm@3: ); rlm@3: rlm@3: return @$lookup[$name_code]; rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: public static function OriginatorLookup($originator_code) { rlm@3: rlm@3: static $lookup = array ( rlm@3: 0 => 'unspecified', rlm@3: 1 => 'pre-set by artist/producer/mastering engineer', rlm@3: 2 => 'set by user', rlm@3: 3 => 'determined automatically' rlm@3: ); rlm@3: rlm@3: return @$lookup[$originator_code]; rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: public static function AdjustmentLookup($raw_adjustment, $sign_bit) { rlm@3: rlm@3: return (float)$raw_adjustment / 10 * ($sign_bit == 1 ? -1 : 1); rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: public static function GainString($name_code, $originator_code, $replaygain) { rlm@3: rlm@3: $sign_bit = $replaygain < 0 ? 1 : 0; rlm@3: rlm@3: $stored_replaygain = intval(round($replaygain * 10)); rlm@3: $gain_string = str_pad(decbin($name_code), 3, '0', STR_PAD_LEFT); rlm@3: $gain_string .= str_pad(decbin($originator_code), 3, '0', STR_PAD_LEFT); rlm@3: $gain_string .= $sign_bit; rlm@3: $gain_string .= str_pad(decbin($stored_replaygain), 9, '0', STR_PAD_LEFT); rlm@3: rlm@3: return $gain_string; rlm@3: } rlm@3: rlm@3: } rlm@3: rlm@3: rlm@3: rlm@3: rlm@3: ?>