annotate e2gallerypro/e2upload/Backend/Assets/getid3/getid3.php @ 20:1038db2374ec judyates

change address
author Robert McIntyre <rlm@mit.edu>
date Sun, 08 Sep 2013 00:47:09 -0400
parents 3f6b44aa6b35
children
rev   line source
rlm@3 1 <?php
rlm@3 2 // +----------------------------------------------------------------------+
rlm@3 3 // | PHP version 5 |
rlm@3 4 // +----------------------------------------------------------------------+
rlm@3 5 // | Copyright (c) 2002-2006 James Heinrich, Allan Hansen |
rlm@3 6 // +----------------------------------------------------------------------+
rlm@3 7 // | This source file is subject to version 2 of the GPL license, |
rlm@3 8 // | that is bundled with this package in the file license.txt and is |
rlm@3 9 // | available through the world-wide-web at the following url: |
rlm@3 10 // | http://www.gnu.org/copyleft/gpl.html |
rlm@3 11 // +----------------------------------------------------------------------+
rlm@3 12 // | getID3() - http://getid3.sourceforge.net or http://www.getid3.org |
rlm@3 13 // +----------------------------------------------------------------------+
rlm@3 14 // | Authors: James Heinrich <infogetid3*org> |
rlm@3 15 // | Allan Hansen <ahartemis*dk> |
rlm@3 16 // +----------------------------------------------------------------------+
rlm@3 17 // | getid3.php |
rlm@3 18 // | Main getID3() file. |
rlm@3 19 // | dependencies: modules. |
rlm@3 20 // +----------------------------------------------------------------------+
rlm@3 21 //
rlm@3 22 // $Id: getid3.php,v 1.26 2006/12/25 23:44:23 ah Exp $
rlm@3 23
rlm@3 24
rlm@3 25 class getid3
rlm@3 26 {
rlm@3 27 //// Settings Section - do NOT modify this file - change setting after newing getid3!
rlm@3 28
rlm@3 29 // Encoding
rlm@3 30 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 31 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 32 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 33
rlm@3 34 // Tags - disable for speed
rlm@3 35 public $option_tag_id3v1 = true; // Read and process ID3v1 tags.
rlm@3 36 public $option_tag_id3v2 = true; // Read and process ID3v2 tags.
rlm@3 37 public $option_tag_lyrics3 = true; // Read and process Lyrics3 tags.
rlm@3 38 public $option_tag_apetag = true; // Read and process APE tags.
rlm@3 39
rlm@3 40 // Misc calucations - disable for speed
rlm@3 41 public $option_analyze = true; // Analyze file - disable if you only need to detect file format.
rlm@3 42 public $option_accurate_results = true; // Disable to greatly speed up parsing of some file formats at the cost of accuracy.
rlm@3 43 public $option_tags_process = true; // Copy tags to root key 'tags' and 'comments' and encode to $this->encoding.
rlm@3 44 public $option_tags_images = false; // Scan tags for binary image data - ID3v2 and vorbiscomments only.
rlm@3 45 public $option_extra_info = true; // Calculate/return additional info such as bitrate, channelmode etc.
rlm@3 46 public $option_max_2gb_check = false; // Check whether file is larger than 2 Gb and thus not supported by PHP.
rlm@3 47
rlm@3 48 // Misc data hashes - slow - require hash module
rlm@3 49 public $option_md5_data = false; // Get MD5 sum of data part - slow.
rlm@3 50 public $option_md5_data_source = false; // Use MD5 of source file if available - only FLAC, MAC, OptimFROG and Wavpack4.
rlm@3 51 public $option_sha1_data = false; // Get SHA1 sum of data part - slow.
rlm@3 52
rlm@3 53 // Public variables
rlm@3 54 public $filename; // Filename of file being analysed.
rlm@3 55 public $fp; // Filepointer to file being analysed.
rlm@3 56 public $info; // Result array.
rlm@3 57
rlm@3 58 // Protected variables
rlm@3 59 protected $include_path; // getid3 include path.
rlm@3 60 protected $warnings = array ();
rlm@3 61 protected $iconv_present;
rlm@3 62
rlm@3 63 // Class constants
rlm@3 64 const VERSION = '2.0.0b4';
rlm@3 65 const FREAD_BUFFER_SIZE = 16384; // Read buffer size in bytes.
rlm@3 66 const ICONV_TEST_STRING = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ ';
rlm@3 67
rlm@3 68
rlm@3 69
rlm@3 70 // Constructor - check PHP enviroment and load library.
rlm@3 71 public function __construct() {
rlm@3 72
rlm@3 73 // Static varibles - no need to recalc every time we new getid3.
rlm@3 74 static $include_path;
rlm@3 75 static $iconv_present;
rlm@3 76
rlm@3 77
rlm@3 78 static $initialized;
rlm@3 79 if ($initialized) {
rlm@3 80
rlm@3 81 // Import static variables
rlm@3 82 $this->include_path = $include_path;
rlm@3 83 $this->iconv_present = $iconv_present;
rlm@3 84
rlm@3 85 // Run init checks only on first instance.
rlm@3 86 return;
rlm@3 87 }
rlm@3 88
rlm@3 89 // Get include_path
rlm@3 90 $this->include_path = $include_path = dirname(__FILE__) . '/';
rlm@3 91
rlm@3 92 // Check for presence of iconv() and make sure it works (simpel test only).
rlm@3 93 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 94 $this->iconv_present = $iconv_present = true;
rlm@3 95 }
rlm@3 96
rlm@3 97 // iconv() not present - load replacement module.
rlm@3 98 else {
rlm@3 99 $this->include_module('lib.iconv_replacement');
rlm@3 100 $this->iconv_present = $iconv_present = false;
rlm@3 101 }
rlm@3 102
rlm@3 103
rlm@3 104 // Require magic_quotes_runtime off
rlm@3 105 if (get_magic_quotes_runtime()) {
rlm@3 106 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 107 }
rlm@3 108
rlm@3 109
rlm@3 110 // Check memory limit.
rlm@3 111 $memory_limit = ini_get('memory_limit');
rlm@3 112 if (eregi('([0-9]+)M', $memory_limit, $matches)) {
rlm@3 113 // could be stored as "16M" rather than 16777216 for example
rlm@3 114 $memory_limit = $matches[1] * 1048576;
rlm@3 115 }
rlm@3 116 if ($memory_limit <= 0) {
rlm@3 117 // Should not happen.
rlm@3 118 } elseif ($memory_limit <= 4194304) {
rlm@3 119 $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 120 } elseif ($memory_limit <= 12582912) {
rlm@3 121 $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 122 }
rlm@3 123
rlm@3 124
rlm@3 125 // Check safe_mode off
rlm@3 126 if ((bool)ini_get('safe_mode')) {
rlm@3 127 $this->warning('Safe mode is on, shorten support disabled, md5data/sha1data for ogg vorbis disabled, ogg vorbis/flac tag writing disabled.');
rlm@3 128 }
rlm@3 129
rlm@3 130 $initialized = true;
rlm@3 131 }
rlm@3 132
rlm@3 133
rlm@3 134
rlm@3 135 // Analyze file by name
rlm@3 136 public function Analyze($filename) {
rlm@3 137
rlm@3 138 // Init and save values
rlm@3 139 $this->filename = $filename;
rlm@3 140 $this->warnings = array ();
rlm@3 141
rlm@3 142 // Init result array and set parameters
rlm@3 143 $this->info = array ();
rlm@3 144 $this->info['GETID3_VERSION'] = getid3::VERSION;
rlm@3 145
rlm@3 146 // Remote files not supported
rlm@3 147 if (preg_match('/^(ht|f)tp:\/\//', $filename)) {
rlm@3 148 throw new getid3_exception('Remote files are not supported - please copy the file locally first.');
rlm@3 149 }
rlm@3 150
rlm@3 151 // Open local file
rlm@3 152 if (!$this->fp = @fopen($filename, 'rb')) {
rlm@3 153 throw new getid3_exception('Could not open file "'.$filename.'"');
rlm@3 154 }
rlm@3 155
rlm@3 156 // Set filesize related parameters
rlm@3 157 $this->info['filesize'] = filesize($filename);
rlm@3 158 $this->info['avdataoffset'] = 0;
rlm@3 159 $this->info['avdataend'] = $this->info['filesize'];
rlm@3 160
rlm@3 161 // Option_max_2gb_check
rlm@3 162 if ($this->option_max_2gb_check) {
rlm@3 163 // PHP doesn't support integers larger than 31-bit (~2GB)
rlm@3 164 // filesize() simply returns (filesize % (pow(2, 32)), no matter the actual filesize
rlm@3 165 // ftell() returns 0 if seeking to the end is beyond the range of unsigned integer
rlm@3 166 fseek($this->fp, 0, SEEK_END);
rlm@3 167 if ((($this->info['filesize'] != 0) && (ftell($this->fp) == 0)) ||
rlm@3 168 ($this->info['filesize'] < 0) ||
rlm@3 169 (ftell($this->fp) < 0)) {
rlm@3 170 unset($this->info['filesize']);
rlm@3 171 fclose($this->fp);
rlm@3 172 throw new getid3_exception('File is most likely larger than 2GB and is not supported by PHP.');
rlm@3 173 }
rlm@3 174 }
rlm@3 175
rlm@3 176
rlm@3 177 // ID3v2 detection (NOT parsing) done to make fileformat easier.
rlm@3 178 if (!$this->option_tag_id3v2) {
rlm@3 179
rlm@3 180 fseek($this->fp, 0, SEEK_SET);
rlm@3 181 $header = fread($this->fp, 10);
rlm@3 182 if (substr($header, 0, 3) == 'ID3' && strlen($header) == 10) {
rlm@3 183 $this->info['id3v2']['header'] = true;
rlm@3 184 $this->info['id3v2']['majorversion'] = ord($header{3});
rlm@3 185 $this->info['id3v2']['minorversion'] = ord($header{4});
rlm@3 186 $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 187 }
rlm@3 188 }
rlm@3 189
rlm@3 190
rlm@3 191 // Handle tags
rlm@3 192 foreach (array ("id3v2", "id3v1", "apetag", "lyrics3") as $tag_name) {
rlm@3 193
rlm@3 194 $option_tag = 'option_tag_' . $tag_name;
rlm@3 195 if ($this->$option_tag) {
rlm@3 196 $this->include_module('tag.'.$tag_name);
rlm@3 197 try {
rlm@3 198 $tag_class = 'getid3_' . $tag_name;
rlm@3 199 $tag = new $tag_class($this);
rlm@3 200 $tag->Analyze();
rlm@3 201 }
rlm@3 202 catch (getid3_exception $e) {
rlm@3 203 throw $e;
rlm@3 204 }
rlm@3 205 }
rlm@3 206 }
rlm@3 207
rlm@3 208
rlm@3 209
rlm@3 210 //// Determine file format by magic bytes in file header.
rlm@3 211
rlm@3 212 // Read 32 kb file data
rlm@3 213 fseek($this->fp, $this->info['avdataoffset'], SEEK_SET);
rlm@3 214 $filedata = fread($this->fp, 32774);
rlm@3 215
rlm@3 216 // Get huge FileFormatArray
rlm@3 217 $file_format_array = getid3::GetFileFormatArray();
rlm@3 218
rlm@3 219 // Identify file format - loop through $format_info and detect with reg expr
rlm@3 220 foreach ($file_format_array as $name => $info) {
rlm@3 221
rlm@3 222 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 223
rlm@3 224 // Format detected but not supported
rlm@3 225 if (!@$info['module'] || !@$info['group']) {
rlm@3 226 fclose($this->fp);
rlm@3 227 $this->info['fileformat'] = $name;
rlm@3 228 $this->info['mime_type'] = $info['mime_type'];
rlm@3 229 $this->warning('Format only detected. Parsing not available yet.');
rlm@3 230 $this->info['warning'] = $this->warnings;
rlm@3 231 return $this->info;
rlm@3 232 }
rlm@3 233
rlm@3 234 $determined_format = $info; // copy $info deleted by foreach()
rlm@3 235 continue;
rlm@3 236 }
rlm@3 237 }
rlm@3 238
rlm@3 239 // Unable to determine file format
rlm@3 240 if (!@$determined_format) {
rlm@3 241
rlm@3 242 // Too many mp3 encoders on the market put gabage in front of mpeg files
rlm@3 243 // use assume format on these if format detection failed
rlm@3 244 if (preg_match('/\.mp[123a]$/i', $filename)) {
rlm@3 245 $determined_format = $file_format_array['mp3'];
rlm@3 246 }
rlm@3 247
rlm@3 248 else {
rlm@3 249 fclose($this->fp);
rlm@3 250 throw new getid3_exception('Unable to determine file format');
rlm@3 251 }
rlm@3 252 }
rlm@3 253
rlm@3 254 // Free memory
rlm@3 255 unset($file_format_array);
rlm@3 256
rlm@3 257 // Check for illegal ID3 tags
rlm@3 258 if (@$determined_format['fail_id3'] && (@$this->info['id3v1'] || @$this->info['id3v2'])) {
rlm@3 259 if ($determined_format['fail_id3'] === 'ERROR') {
rlm@3 260 fclose($this->fp);
rlm@3 261 throw new getid3_exception('ID3 tags not allowed on this file type.');
rlm@3 262 }
rlm@3 263 elseif ($determined_format['fail_id3'] === 'WARNING') {
rlm@3 264 @$this->info['id3v1'] and $this->warning('ID3v1 tags not allowed on this file type.');
rlm@3 265 @$this->info['id3v2'] and $this->warning('ID3v2 tags not allowed on this file type.');
rlm@3 266 }
rlm@3 267 }
rlm@3 268
rlm@3 269 // Check for illegal APE tags
rlm@3 270 if (@$determined_format['fail_ape'] && @$this->info['tags']['ape']) {
rlm@3 271 if ($determined_format['fail_ape'] === 'ERROR') {
rlm@3 272 fclose($this->fp);
rlm@3 273 throw new getid3_exception('APE tags not allowed on this file type.');
rlm@3 274 } elseif ($determined_format['fail_ape'] === 'WARNING') {
rlm@3 275 $this->warning('APE tags not allowed on this file type.');
rlm@3 276 }
rlm@3 277 }
rlm@3 278
rlm@3 279
rlm@3 280 // Set mime type
rlm@3 281 $this->info['mime_type'] = $determined_format['mime_type'];
rlm@3 282
rlm@3 283 // Calc module file name
rlm@3 284 $determined_format['include'] = 'module.'.$determined_format['group'].'.'.$determined_format['module'].'.php';
rlm@3 285
rlm@3 286 // Supported format signature pattern detected, but module deleted.
rlm@3 287 if (!file_exists($this->include_path.$determined_format['include'])) {
rlm@3 288 fclose($this->fp);
rlm@3 289 throw new getid3_exception('Format not supported, module, '.$determined_format['include'].', was removed.');
rlm@3 290 }
rlm@3 291
rlm@3 292 // Include module
rlm@3 293 $this->include_module($determined_format['group'].'.'.$determined_format['module']);
rlm@3 294
rlm@3 295 // Instantiate module class and analyze
rlm@3 296 $class_name = 'getid3_'.$determined_format['module'];
rlm@3 297 if (!class_exists($class_name)) {
rlm@3 298 throw new getid3_exception('Format not supported, module, '.$determined_format['include'].', is corrupt.');
rlm@3 299 }
rlm@3 300 $class = new $class_name($this);
rlm@3 301
rlm@3 302 try {
rlm@3 303 $this->option_analyze and $class->Analyze();
rlm@3 304 }
rlm@3 305 catch (getid3_exception $e) {
rlm@3 306 throw $e;
rlm@3 307 }
rlm@3 308 catch (Exception $e) {
rlm@3 309 throw new getid3_exception('Corrupt file.');
rlm@3 310 }
rlm@3 311
rlm@3 312 // Close file
rlm@3 313 fclose($this->fp);
rlm@3 314
rlm@3 315 // Optional - Process all tags - copy to 'tags' and convert charsets
rlm@3 316 if ($this->option_tags_process) {
rlm@3 317 $this->HandleAllTags();
rlm@3 318 }
rlm@3 319
rlm@3 320
rlm@3 321 //// Optional - perform more calculations
rlm@3 322 if ($this->option_extra_info) {
rlm@3 323
rlm@3 324 // Set channelmode on audio
rlm@3 325 if (@$this->info['audio']['channels'] == '1') {
rlm@3 326 $this->info['audio']['channelmode'] = 'mono';
rlm@3 327 } elseif (@$this->info['audio']['channels'] == '2') {
rlm@3 328 $this->info['audio']['channelmode'] = 'stereo';
rlm@3 329 }
rlm@3 330
rlm@3 331 // Calculate combined bitrate - audio + video
rlm@3 332 $combined_bitrate = 0;
rlm@3 333 $combined_bitrate += (isset($this->info['audio']['bitrate']) ? $this->info['audio']['bitrate'] : 0);
rlm@3 334 $combined_bitrate += (isset($this->info['video']['bitrate']) ? $this->info['video']['bitrate'] : 0);
rlm@3 335 if (($combined_bitrate > 0) && empty($this->info['bitrate'])) {
rlm@3 336 $this->info['bitrate'] = $combined_bitrate;
rlm@3 337 }
rlm@3 338 if (!isset($this->info['playtime_seconds']) && !empty($this->info['bitrate'])) {
rlm@3 339 $this->info['playtime_seconds'] = (($this->info['avdataend'] - $this->info['avdataoffset']) * 8) / $this->info['bitrate'];
rlm@3 340 }
rlm@3 341
rlm@3 342 // Set playtime string
rlm@3 343 if (!empty($this->info['playtime_seconds']) && empty($this->info['playtime_string'])) {
rlm@3 344 $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 345 }
rlm@3 346
rlm@3 347
rlm@3 348 // CalculateCompressionRatioVideo() {
rlm@3 349 if (@$this->info['video'] && @$this->info['video']['resolution_x'] && @$this->info['video']['resolution_y'] && @$this->info['video']['bits_per_sample']) {
rlm@3 350
rlm@3 351 // From static image formats
rlm@3 352 if (in_array($this->info['video']['dataformat'], array ('bmp', 'gif', 'jpeg', 'jpg', 'png', 'tiff'))) {
rlm@3 353 $frame_rate = 1;
rlm@3 354 $bitrate_compressed = $this->info['filesize'] * 8;
rlm@3 355 }
rlm@3 356
rlm@3 357 // From video formats
rlm@3 358 else {
rlm@3 359 $frame_rate = @$this->info['video']['frame_rate'];
rlm@3 360 $bitrate_compressed = @$this->info['video']['bitrate'];
rlm@3 361 }
rlm@3 362
rlm@3 363 if ($frame_rate && $bitrate_compressed) {
rlm@3 364 $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 365 }
rlm@3 366 }
rlm@3 367
rlm@3 368
rlm@3 369 // CalculateCompressionRatioAudio() {
rlm@3 370 if (@$this->info['audio']['bitrate'] && @$this->info['audio']['channels'] && @$this->info['audio']['sample_rate']) {
rlm@3 371 $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 372 }
rlm@3 373
rlm@3 374 if (@$this->info['audio']['streams']) {
rlm@3 375 foreach ($this->info['audio']['streams'] as $stream_number => $stream_data) {
rlm@3 376 if (@$stream_data['bitrate'] && @$stream_data['channels'] && @$stream_data['sample_rate']) {
rlm@3 377 $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 378 }
rlm@3 379 }
rlm@3 380 }
rlm@3 381
rlm@3 382
rlm@3 383 // CalculateReplayGain() {
rlm@3 384 if (@$this->info['replay_gain']) {
rlm@3 385 if (!@$this->info['replay_gain']['reference_volume']) {
rlm@3 386 $this->info['replay_gain']['reference_volume'] = 89;
rlm@3 387 }
rlm@3 388 if (isset($this->info['replay_gain']['track']['adjustment'])) {
rlm@3 389 $this->info['replay_gain']['track']['volume'] = $this->info['replay_gain']['reference_volume'] - $this->info['replay_gain']['track']['adjustment'];
rlm@3 390 }
rlm@3 391 if (isset($this->info['replay_gain']['album']['adjustment'])) {
rlm@3 392 $this->info['replay_gain']['album']['volume'] = $this->info['replay_gain']['reference_volume'] - $this->info['replay_gain']['album']['adjustment'];
rlm@3 393 }
rlm@3 394
rlm@3 395 if (isset($this->info['replay_gain']['track']['peak'])) {
rlm@3 396 $this->info['replay_gain']['track']['max_noclip_gain'] = 0 - 20 * log10($this->info['replay_gain']['track']['peak']);
rlm@3 397 }
rlm@3 398 if (isset($this->info['replay_gain']['album']['peak'])) {
rlm@3 399 $this->info['replay_gain']['album']['max_noclip_gain'] = 0 - 20 * log10($this->info['replay_gain']['album']['peak']);
rlm@3 400 }
rlm@3 401 }
rlm@3 402
rlm@3 403
rlm@3 404 // ProcessAudioStreams() {
rlm@3 405 if (@!$this->info['audio']['streams'] && (@$this->info['audio']['bitrate'] || @$this->info['audio']['channels'] || @$this->info['audio']['sample_rate'])) {
rlm@3 406 foreach ($this->info['audio'] as $key => $value) {
rlm@3 407 if ($key != 'streams') {
rlm@3 408 $this->info['audio']['streams'][0][$key] = $value;
rlm@3 409 }
rlm@3 410 }
rlm@3 411 }
rlm@3 412 }
rlm@3 413
rlm@3 414
rlm@3 415 // Get the md5/sha1sum of the audio/video portion of the file - without ID3/APE/Lyrics3/etc header/footer tags.
rlm@3 416 if ($this->option_md5_data || $this->option_sha1_data) {
rlm@3 417
rlm@3 418 // Load data-hash library if needed
rlm@3 419 $this->include_module('lib.data_hash');
rlm@3 420
rlm@3 421 if ($this->option_sha1_data) {
rlm@3 422 new getid3_lib_data_hash($this, 'sha1');
rlm@3 423 }
rlm@3 424
rlm@3 425 if ($this->option_md5_data) {
rlm@3 426
rlm@3 427 // no md5_data_source or option disabled -- md5_data_source supported by FLAC, MAC, OptimFROG, Wavpack4
rlm@3 428 if (!$this->option_md5_data_source || !@$this->info['md5_data_source']) {
rlm@3 429 new getid3_lib_data_hash($this, 'md5');
rlm@3 430 }
rlm@3 431
rlm@3 432 // copy md5_data_source to md5_data if option set to true
rlm@3 433 elseif ($this->option_md5_data_source && @$this->info['md5_data_source']) {
rlm@3 434 $this->info['md5_data'] = $this->info['md5_data_source'];
rlm@3 435 }
rlm@3 436 }
rlm@3 437 }
rlm@3 438
rlm@3 439 // Set warnings
rlm@3 440 if ($this->warnings) {
rlm@3 441 $this->info['warning'] = $this->warnings;
rlm@3 442 }
rlm@3 443
rlm@3 444 // Return result
rlm@3 445 return $this->info;
rlm@3 446 }
rlm@3 447
rlm@3 448
rlm@3 449
rlm@3 450 // Return array of warnings
rlm@3 451 public function warnings() {
rlm@3 452
rlm@3 453 return $this->warnings;
rlm@3 454 }
rlm@3 455
rlm@3 456
rlm@3 457
rlm@3 458 // Add warning(s) to $this->warnings[]
rlm@3 459 public function warning($message) {
rlm@3 460
rlm@3 461 if (is_array($message)) {
rlm@3 462 $this->warnings = array_merge($this->warnings, $message);
rlm@3 463 }
rlm@3 464 else {
rlm@3 465 $this->warnings[] = $message;
rlm@3 466 }
rlm@3 467 }
rlm@3 468
rlm@3 469
rlm@3 470
rlm@3 471 // Clear all warnings when cloning
rlm@3 472 public function __clone() {
rlm@3 473
rlm@3 474 $this->warnings = array ();
rlm@3 475
rlm@3 476 // Copy info array, otherwise it will be a reference.
rlm@3 477 $temp = $this->info;
rlm@3 478 unset($this->info);
rlm@3 479 $this->info = $temp;
rlm@3 480 }
rlm@3 481
rlm@3 482
rlm@3 483
rlm@3 484 // Convert string between charsets -- iconv() wrapper
rlm@3 485 public function iconv($in_charset, $out_charset, $string, $drop01 = false) {
rlm@3 486
rlm@3 487 if ($drop01 && ($string === "\x00" || $string === "\x01")) {
rlm@3 488 return '';
rlm@3 489 }
rlm@3 490
rlm@3 491
rlm@3 492 if (!$this->iconv_present) {
rlm@3 493 return getid3_iconv_replacement::iconv($in_charset, $out_charset, $string);
rlm@3 494 }
rlm@3 495
rlm@3 496
rlm@3 497 // iconv() present
rlm@3 498 if ($result = @iconv($in_charset, $out_charset.'//TRANSLIT', $string)) {
rlm@3 499
rlm@3 500 if ($out_charset == 'ISO-8859-1') {
rlm@3 501 return rtrim($result, "\x00");
rlm@3 502 }
rlm@3 503 return $result;
rlm@3 504 }
rlm@3 505
rlm@3 506 $this->warning('iconv() was unable to convert the string: "' . $string . '" from ' . $in_charset . ' to ' . $out_charset);
rlm@3 507 return $string;
rlm@3 508 }
rlm@3 509
rlm@3 510
rlm@3 511
rlm@3 512 public function include_module($name) {
rlm@3 513
rlm@3 514 if (!file_exists($this->include_path.'module.'.$name.'.php')) {
rlm@3 515 throw new getid3_exception('Required module.'.$name.'.php is missing.');
rlm@3 516 }
rlm@3 517
rlm@3 518 include_once($this->include_path.'module.'.$name.'.php');
rlm@3 519 }
rlm@3 520
rlm@3 521
rlm@3 522
rlm@3 523 public function include_module_optional($name) {
rlm@3 524
rlm@3 525 if (!file_exists($this->include_path.'module.'.$name.'.php')) {
rlm@3 526 return;
rlm@3 527 }
rlm@3 528
rlm@3 529 include_once($this->include_path.'module.'.$name.'.php');
rlm@3 530 return true;
rlm@3 531 }
rlm@3 532
rlm@3 533
rlm@3 534 // Return array containing information about all supported formats
rlm@3 535 public static function GetFileFormatArray() {
rlm@3 536
rlm@3 537 static $format_info = array (
rlm@3 538
rlm@3 539 // Audio formats
rlm@3 540
rlm@3 541 // AC-3 - audio - Dolby AC-3 / Dolby Digital
rlm@3 542 'ac3' => array (
rlm@3 543 'pattern' => '^\x0B\x77',
rlm@3 544 'group' => 'audio',
rlm@3 545 'module' => 'ac3',
rlm@3 546 'mime_type' => 'audio/ac3',
rlm@3 547 ),
rlm@3 548
rlm@3 549 // AAC - audio - Advanced Audio Coding (AAC) - ADIF format
rlm@3 550 'adif' => array (
rlm@3 551 'pattern' => '^ADIF',
rlm@3 552 'group' => 'audio',
rlm@3 553 'module' => 'aac_adif',
rlm@3 554 'mime_type' => 'application/octet-stream',
rlm@3 555 'fail_ape' => 'WARNING',
rlm@3 556 ),
rlm@3 557
rlm@3 558
rlm@3 559 // AAC - audio - Advanced Audio Coding (AAC) - ADTS format (very similar to MP3)
rlm@3 560 'adts' => array (
rlm@3 561 'pattern' => '^\xFF[\xF0-\xF1\xF8-\xF9]',
rlm@3 562 'group' => 'audio',
rlm@3 563 'module' => 'aac_adts',
rlm@3 564 'mime_type' => 'application/octet-stream',
rlm@3 565 'fail_ape' => 'WARNING',
rlm@3 566 ),
rlm@3 567
rlm@3 568
rlm@3 569 // AU - audio - NeXT/Sun AUdio (AU)
rlm@3 570 'au' => array (
rlm@3 571 'pattern' => '^\.snd',
rlm@3 572 'group' => 'audio',
rlm@3 573 'module' => 'au',
rlm@3 574 'mime_type' => 'audio/basic',
rlm@3 575 ),
rlm@3 576
rlm@3 577 // AVR - audio - Audio Visual Research
rlm@3 578 'avr' => array (
rlm@3 579 'pattern' => '^2BIT',
rlm@3 580 'group' => 'audio',
rlm@3 581 'module' => 'avr',
rlm@3 582 'mime_type' => 'application/octet-stream',
rlm@3 583 ),
rlm@3 584
rlm@3 585 // BONK - audio - Bonk v0.9+
rlm@3 586 'bonk' => array (
rlm@3 587 'pattern' => '^\x00(BONK|INFO|META| ID3)',
rlm@3 588 'group' => 'audio',
rlm@3 589 'module' => 'bonk',
rlm@3 590 'mime_type' => 'audio/xmms-bonk',
rlm@3 591 ),
rlm@3 592
rlm@3 593 // DTS - audio - Dolby Theatre System
rlm@3 594 'dts' => array(
rlm@3 595 'pattern' => '^\x7F\xFE\x80\x01',
rlm@3 596 'group' => 'audio',
rlm@3 597 'module' => 'dts',
rlm@3 598 'mime_type' => 'audio/dts',
rlm@3 599 ),
rlm@3 600
rlm@3 601 // FLAC - audio - Free Lossless Audio Codec
rlm@3 602 'flac' => array (
rlm@3 603 'pattern' => '^fLaC',
rlm@3 604 'group' => 'audio',
rlm@3 605 'module' => 'xiph',
rlm@3 606 'mime_type' => 'audio/x-flac',
rlm@3 607 ),
rlm@3 608
rlm@3 609 // LA - audio - Lossless Audio (LA)
rlm@3 610 'la' => array (
rlm@3 611 'pattern' => '^LA0[2-4]',
rlm@3 612 'group' => 'audio',
rlm@3 613 'module' => 'la',
rlm@3 614 'mime_type' => 'application/octet-stream',
rlm@3 615 ),
rlm@3 616
rlm@3 617 // LPAC - audio - Lossless Predictive Audio Compression (LPAC)
rlm@3 618 'lpac' => array (
rlm@3 619 'pattern' => '^LPAC',
rlm@3 620 'group' => 'audio',
rlm@3 621 'module' => 'lpac',
rlm@3 622 'mime_type' => 'application/octet-stream',
rlm@3 623 ),
rlm@3 624
rlm@3 625 // MIDI - audio - MIDI (Musical Instrument Digital Interface)
rlm@3 626 'midi' => array (
rlm@3 627 'pattern' => '^MThd',
rlm@3 628 'group' => 'audio',
rlm@3 629 'module' => 'midi',
rlm@3 630 'mime_type' => 'audio/midi',
rlm@3 631 ),
rlm@3 632
rlm@3 633 // MAC - audio - Monkey's Audio Compressor
rlm@3 634 'mac' => array (
rlm@3 635 'pattern' => '^MAC ',
rlm@3 636 'group' => 'audio',
rlm@3 637 'module' => 'monkey',
rlm@3 638 'mime_type' => 'application/octet-stream',
rlm@3 639 ),
rlm@3 640
rlm@3 641 // MOD - audio - MODule (assorted sub-formats)
rlm@3 642 'mod' => array (
rlm@3 643 'pattern' => '^.{1080}(M.K.|[5-9]CHN|[1-3][0-9]CH)',
rlm@3 644 'mime_type' => 'audio/mod',
rlm@3 645 ),
rlm@3 646
rlm@3 647 // MOD - audio - MODule (Impulse Tracker)
rlm@3 648 'it' => array (
rlm@3 649 'pattern' => '^IMPM',
rlm@3 650 'mime_type' => 'audio/it',
rlm@3 651 ),
rlm@3 652
rlm@3 653 // MOD - audio - MODule (eXtended Module, various sub-formats)
rlm@3 654 'xm' => array (
rlm@3 655 'pattern' => '^Extended Module',
rlm@3 656 'mime_type' => 'audio/xm',
rlm@3 657 ),
rlm@3 658
rlm@3 659 // MOD - audio - MODule (ScreamTracker)
rlm@3 660 's3m' => array (
rlm@3 661 'pattern' => '^.{44}SCRM',
rlm@3 662 'mime_type' => 'audio/s3m',
rlm@3 663 ),
rlm@3 664
rlm@3 665 // MPC - audio - Musepack / MPEGplus SV7+
rlm@3 666 'mpc' => array (
rlm@3 667 'pattern' => '^(MP\+)',
rlm@3 668 'group' => 'audio',
rlm@3 669 'module' => 'mpc',
rlm@3 670 'mime_type' => 'audio/x-musepack',
rlm@3 671 ),
rlm@3 672
rlm@3 673 // MPC - audio - Musepack / MPEGplus SV4-6
rlm@3 674 'mpc_old' => array (
rlm@3 675 '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 676 'group' => 'audio',
rlm@3 677 'module' => 'mpc_old',
rlm@3 678 'mime_type' => 'application/octet-stream',
rlm@3 679 ),
rlm@3 680
rlm@3 681
rlm@3 682 // MP3 - audio - MPEG-audio Layer 3 (very similar to AAC-ADTS)
rlm@3 683 'mp3' => array (
rlm@3 684 'pattern' => '^\xFF[\xE2-\xE7\xF2-\xF7\xFA-\xFF][\x00-\xEB]',
rlm@3 685 'group' => 'audio',
rlm@3 686 'module' => 'mp3',
rlm@3 687 'mime_type' => 'audio/mpeg',
rlm@3 688 ),
rlm@3 689
rlm@3 690 // OFR - audio - OptimFROG
rlm@3 691 'ofr' => array (
rlm@3 692 'pattern' => '^(\*RIFF|OFR)',
rlm@3 693 'group' => 'audio',
rlm@3 694 'module' => 'optimfrog',
rlm@3 695 'mime_type' => 'application/octet-stream',
rlm@3 696 ),
rlm@3 697
rlm@3 698 // RKAU - audio - RKive AUdio compressor
rlm@3 699 'rkau' => array (
rlm@3 700 'pattern' => '^RKA',
rlm@3 701 'group' => 'audio',
rlm@3 702 'module' => 'rkau',
rlm@3 703 'mime_type' => 'application/octet-stream',
rlm@3 704 ),
rlm@3 705
rlm@3 706 // SHN - audio - Shorten
rlm@3 707 'shn' => array (
rlm@3 708 'pattern' => '^ajkg',
rlm@3 709 'group' => 'audio',
rlm@3 710 'module' => 'shorten',
rlm@3 711 'mime_type' => 'audio/xmms-shn',
rlm@3 712 'fail_id3' => 'ERROR',
rlm@3 713 'fail_ape' => 'ERROR',
rlm@3 714 ),
rlm@3 715
rlm@3 716 // TTA - audio - TTA Lossless Audio Compressor (http://tta.corecodec.org)
rlm@3 717 'tta' => array (
rlm@3 718 'pattern' => '^TTA', // could also be '^TTA(\x01|\x02|\x03|2|1)'
rlm@3 719 'group' => 'audio',
rlm@3 720 'module' => 'tta',
rlm@3 721 'mime_type' => 'application/octet-stream',
rlm@3 722 ),
rlm@3 723
rlm@3 724 // VOC - audio - Creative Voice (VOC)
rlm@3 725 'voc' => array (
rlm@3 726 'pattern' => '^Creative Voice File',
rlm@3 727 'group' => 'audio',
rlm@3 728 'module' => 'voc',
rlm@3 729 'mime_type' => 'audio/voc',
rlm@3 730 ),
rlm@3 731
rlm@3 732 // VQF - audio - transform-domain weighted interleave Vector Quantization Format (VQF)
rlm@3 733 'vqf' => array (
rlm@3 734 'pattern' => '^TWIN',
rlm@3 735 'group' => 'audio',
rlm@3 736 'module' => 'vqf',
rlm@3 737 'mime_type' => 'application/octet-stream',
rlm@3 738 ),
rlm@3 739
rlm@3 740 // WV - audio - WavPack (v4.0+)
rlm@3 741 'vw' => array(
rlm@3 742 'pattern' => '^wvpk',
rlm@3 743 'group' => 'audio',
rlm@3 744 'module' => 'wavpack',
rlm@3 745 'mime_type' => 'application/octet-stream',
rlm@3 746 ),
rlm@3 747
rlm@3 748
rlm@3 749 // Audio-Video formats
rlm@3 750
rlm@3 751 // ASF - audio/video - Advanced Streaming Format, Windows Media Video, Windows Media Audio
rlm@3 752 'asf' => array (
rlm@3 753 'pattern' => '^\x30\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C',
rlm@3 754 'group' => 'audio-video',
rlm@3 755 'module' => 'asf',
rlm@3 756 'mime_type' => 'video/x-ms-asf',
rlm@3 757 ),
rlm@3 758
rlm@3 759 // BINK - audio/video - Bink / Smacker
rlm@3 760 'bink' => array(
rlm@3 761 'pattern' => '^(BIK|SMK)',
rlm@3 762 'mime_type' => 'application/octet-stream',
rlm@3 763 ),
rlm@3 764
rlm@3 765 // FLV - audio/video - FLash Video
rlm@3 766 'flv' => array(
rlm@3 767 'pattern' => '^FLV\x01',
rlm@3 768 'group' => 'audio-video',
rlm@3 769 'module' => 'flv',
rlm@3 770 'mime_type' => 'video/x-flv',
rlm@3 771 ),
rlm@3 772
rlm@3 773 // MKAV - audio/video - Mastroka
rlm@3 774 'matroska' => array (
rlm@3 775 'pattern' => '^\x1A\x45\xDF\xA3',
rlm@3 776 'mime_type' => 'application/octet-stream',
rlm@3 777 ),
rlm@3 778
rlm@3 779 // MPEG - audio/video - MPEG (Moving Pictures Experts Group)
rlm@3 780 'mpeg' => array (
rlm@3 781 'pattern' => '^\x00\x00\x01(\xBA|\xB3)',
rlm@3 782 'group' => 'audio-video',
rlm@3 783 'module' => 'mpeg',
rlm@3 784 'mime_type' => 'video/mpeg',
rlm@3 785 ),
rlm@3 786
rlm@3 787 // NSV - audio/video - Nullsoft Streaming Video (NSV)
rlm@3 788 'nsv' => array (
rlm@3 789 'pattern' => '^NSV[sf]',
rlm@3 790 'group' => 'audio-video',
rlm@3 791 'module' => 'nsv',
rlm@3 792 'mime_type' => 'application/octet-stream',
rlm@3 793 ),
rlm@3 794
rlm@3 795 // Ogg - audio/video - Ogg (Ogg Vorbis, OggFLAC, Speex, Ogg Theora(*), Ogg Tarkin(*))
rlm@3 796 'ogg' => array (
rlm@3 797 'pattern' => '^OggS',
rlm@3 798 'group' => 'audio',
rlm@3 799 'module' => 'xiph',
rlm@3 800 'mime_type' => 'application/ogg',
rlm@3 801 'fail_id3' => 'WARNING',
rlm@3 802 'fail_ape' => 'WARNING',
rlm@3 803 ),
rlm@3 804
rlm@3 805 // QT - audio/video - Quicktime
rlm@3 806 'quicktime' => array (
rlm@3 807 'pattern' => '^.{4}(cmov|free|ftyp|mdat|moov|pnot|skip|wide)',
rlm@3 808 'group' => 'audio-video',
rlm@3 809 'module' => 'quicktime',
rlm@3 810 'mime_type' => 'video/quicktime',
rlm@3 811 ),
rlm@3 812
rlm@3 813 // 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 814 'riff' => array (
rlm@3 815 'pattern' => '^(RIFF|SDSS|FORM)',
rlm@3 816 'group' => 'audio-video',
rlm@3 817 'module' => 'riff',
rlm@3 818 'mime_type' => 'audio/x-wave',
rlm@3 819 'fail_ape' => 'WARNING',
rlm@3 820 ),
rlm@3 821
rlm@3 822 // Real - audio/video - RealAudio, RealVideo
rlm@3 823 'real' => array (
rlm@3 824 'pattern' => '^(\.RMF|.ra)',
rlm@3 825 'group' => 'audio-video',
rlm@3 826 'module' => 'real',
rlm@3 827 'mime_type' => 'audio/x-realaudio',
rlm@3 828 ),
rlm@3 829
rlm@3 830 // SWF - audio/video - ShockWave Flash
rlm@3 831 'swf' => array (
rlm@3 832 'pattern' => '^(F|C)WS',
rlm@3 833 'group' => 'audio-video',
rlm@3 834 'module' => 'swf',
rlm@3 835 'mime_type' => 'application/x-shockwave-flash',
rlm@3 836 ),
rlm@3 837
rlm@3 838
rlm@3 839 // Still-Image formats
rlm@3 840
rlm@3 841 // BMP - still image - Bitmap (Windows, OS/2; uncompressed, RLE8, RLE4)
rlm@3 842 'bmp' => array (
rlm@3 843 'pattern' => '^BM',
rlm@3 844 'group' => 'graphic',
rlm@3 845 'module' => 'bmp',
rlm@3 846 'mime_type' => 'image/bmp',
rlm@3 847 'fail_id3' => 'ERROR',
rlm@3 848 'fail_ape' => 'ERROR',
rlm@3 849 ),
rlm@3 850
rlm@3 851 // GIF - still image - Graphics Interchange Format
rlm@3 852 'gif' => array (
rlm@3 853 'pattern' => '^GIF',
rlm@3 854 'group' => 'graphic',
rlm@3 855 'module' => 'gif',
rlm@3 856 'mime_type' => 'image/gif',
rlm@3 857 'fail_id3' => 'ERROR',
rlm@3 858 'fail_ape' => 'ERROR',
rlm@3 859 ),
rlm@3 860
rlm@3 861 // JPEG - still image - Joint Photographic Experts Group (JPEG)
rlm@3 862 'jpeg' => array (
rlm@3 863 'pattern' => '^\xFF\xD8\xFF',
rlm@3 864 'group' => 'graphic',
rlm@3 865 'module' => 'jpeg',
rlm@3 866 'mime_type' => 'image/jpeg',
rlm@3 867 'fail_id3' => 'ERROR',
rlm@3 868 'fail_ape' => 'ERROR',
rlm@3 869 ),
rlm@3 870
rlm@3 871 // PCD - still image - Kodak Photo CD
rlm@3 872 'pcd' => array (
rlm@3 873 'pattern' => '^.{2048}PCD_IPI\x00',
rlm@3 874 'group' => 'graphic',
rlm@3 875 'module' => 'pcd',
rlm@3 876 'mime_type' => 'image/x-photo-cd',
rlm@3 877 'fail_id3' => 'ERROR',
rlm@3 878 'fail_ape' => 'ERROR',
rlm@3 879 ),
rlm@3 880
rlm@3 881
rlm@3 882 // PNG - still image - Portable Network Graphics (PNG)
rlm@3 883 'png' => array (
rlm@3 884 'pattern' => '^\x89\x50\x4E\x47\x0D\x0A\x1A\x0A',
rlm@3 885 'group' => 'graphic',
rlm@3 886 'module' => 'png',
rlm@3 887 'mime_type' => 'image/png',
rlm@3 888 'fail_id3' => 'ERROR',
rlm@3 889 'fail_ape' => 'ERROR',
rlm@3 890 ),
rlm@3 891
rlm@3 892
rlm@3 893 // SVG - still image - Scalable Vector Graphics (SVG)
rlm@3 894 'svg' => array(
rlm@3 895 'pattern' => '<!DOCTYPE svg PUBLIC ',
rlm@3 896 'mime_type' => 'image/svg+xml',
rlm@3 897 'fail_id3' => 'ERROR',
rlm@3 898 'fail_ape' => 'ERROR',
rlm@3 899 ),
rlm@3 900
rlm@3 901
rlm@3 902 // TIFF - still image - Tagged Information File Format (TIFF)
rlm@3 903 'tiff' => array (
rlm@3 904 'pattern' => '^(II\x2A\x00|MM\x00\x2A)',
rlm@3 905 'group' => 'graphic',
rlm@3 906 'module' => 'tiff',
rlm@3 907 'mime_type' => 'image/tiff',
rlm@3 908 'fail_id3' => 'ERROR',
rlm@3 909 'fail_ape' => 'ERROR',
rlm@3 910 ),
rlm@3 911
rlm@3 912
rlm@3 913 // Data formats
rlm@3 914
rlm@3 915 'exe' => array(
rlm@3 916 'pattern' => '^MZ',
rlm@3 917 'mime_type' => 'application/octet-stream',
rlm@3 918 'fail_id3' => 'ERROR',
rlm@3 919 'fail_ape' => 'ERROR',
rlm@3 920 ),
rlm@3 921
rlm@3 922 // ISO - data - International Standards Organization (ISO) CD-ROM Image
rlm@3 923 'iso' => array (
rlm@3 924 'pattern' => '^.{32769}CD001',
rlm@3 925 'group' => 'misc',
rlm@3 926 'module' => 'iso',
rlm@3 927 'mime_type' => 'application/octet-stream',
rlm@3 928 'fail_id3' => 'ERROR',
rlm@3 929 'fail_ape' => 'ERROR',
rlm@3 930 ),
rlm@3 931
rlm@3 932 // RAR - data - RAR compressed data
rlm@3 933 'rar' => array(
rlm@3 934 'pattern' => '^Rar\!',
rlm@3 935 'mime_type' => 'application/octet-stream',
rlm@3 936 'fail_id3' => 'ERROR',
rlm@3 937 'fail_ape' => 'ERROR',
rlm@3 938 ),
rlm@3 939
rlm@3 940 // SZIP - audio - SZIP compressed data
rlm@3 941 'szip' => array (
rlm@3 942 'pattern' => '^SZ\x0A\x04',
rlm@3 943 'group' => 'archive',
rlm@3 944 'module' => 'szip',
rlm@3 945 'mime_type' => 'application/octet-stream',
rlm@3 946 'fail_id3' => 'ERROR',
rlm@3 947 'fail_ape' => 'ERROR',
rlm@3 948 ),
rlm@3 949
rlm@3 950 // TAR - data - TAR compressed data
rlm@3 951 'tar' => array(
rlm@3 952 '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 953 'group' => 'archive',
rlm@3 954 'module' => 'tar',
rlm@3 955 'mime_type' => 'application/x-tar',
rlm@3 956 'fail_id3' => 'ERROR',
rlm@3 957 'fail_ape' => 'ERROR',
rlm@3 958 ),
rlm@3 959
rlm@3 960 // GZIP - data - GZIP compressed data
rlm@3 961 'gz' => array(
rlm@3 962 'pattern' => '^\x1F\x8B\x08',
rlm@3 963 'group' => 'archive',
rlm@3 964 'module' => 'gzip',
rlm@3 965 'mime_type' => 'application/x-gzip',
rlm@3 966 'fail_id3' => 'ERROR',
rlm@3 967 'fail_ape' => 'ERROR',
rlm@3 968 ),
rlm@3 969
rlm@3 970
rlm@3 971 // ZIP - data - ZIP compressed data
rlm@3 972 'zip' => array (
rlm@3 973 'pattern' => '^PK\x03\x04',
rlm@3 974 'group' => 'archive',
rlm@3 975 'module' => 'zip',
rlm@3 976 'mime_type' => 'application/zip',
rlm@3 977 'fail_id3' => 'ERROR',
rlm@3 978 'fail_ape' => 'ERROR',
rlm@3 979 ),
rlm@3 980
rlm@3 981
rlm@3 982 // PAR2 - data - Parity Volume Set Specification 2.0
rlm@3 983 'par2' => array (
rlm@3 984 'pattern' => '^PAR2\x00PKT',
rlm@3 985 'mime_type' => 'application/octet-stream',
rlm@3 986 'fail_id3' => 'ERROR',
rlm@3 987 'fail_ape' => 'ERROR',
rlm@3 988 ),
rlm@3 989
rlm@3 990
rlm@3 991 // PDF - data - Portable Document Format
rlm@3 992 'pdf' => array(
rlm@3 993 'pattern' => '^\x25PDF',
rlm@3 994 'mime_type' => 'application/pdf',
rlm@3 995 'fail_id3' => 'ERROR',
rlm@3 996 'fail_ape' => 'ERROR',
rlm@3 997 ),
rlm@3 998
rlm@3 999 // DOC - data - Microsoft Word
rlm@3 1000 'msoffice' => array(
rlm@3 1001 'pattern' => '^\xD0\xCF\x11\xE0', // D0CF11E == DOCFILE == Microsoft Office Document
rlm@3 1002 'mime_type' => 'application/octet-stream',
rlm@3 1003 'fail_id3' => 'ERROR',
rlm@3 1004 'fail_ape' => 'ERROR',
rlm@3 1005 ),
rlm@3 1006 );
rlm@3 1007
rlm@3 1008 return $format_info;
rlm@3 1009 }
rlm@3 1010
rlm@3 1011
rlm@3 1012
rlm@3 1013 // Recursive over array - converts array to $encoding charset from $this->encoding
rlm@3 1014 function CharConvert(&$array, $encoding) {
rlm@3 1015
rlm@3 1016 // Identical encoding - end here
rlm@3 1017 if ($encoding == $this->encoding) {
rlm@3 1018 return;
rlm@3 1019 }
rlm@3 1020
rlm@3 1021 // Loop thru array
rlm@3 1022 foreach ($array as $key => $value) {
rlm@3 1023
rlm@3 1024 // Go recursive
rlm@3 1025 if (is_array($value)) {
rlm@3 1026 $this->CharConvert($array[$key], $encoding);
rlm@3 1027 }
rlm@3 1028
rlm@3 1029 // Convert string
rlm@3 1030 elseif (is_string($value)) {
rlm@3 1031 $array[$key] = $this->iconv($encoding, $this->encoding, $value);
rlm@3 1032 }
rlm@3 1033 }
rlm@3 1034 }
rlm@3 1035
rlm@3 1036
rlm@3 1037
rlm@3 1038 // Convert and copy tags
rlm@3 1039 protected function HandleAllTags() {
rlm@3 1040
rlm@3 1041 // Key name => array (tag name, character encoding)
rlm@3 1042 static $tags = array (
rlm@3 1043 'asf' => array ('asf', 'UTF-16LE'),
rlm@3 1044 'midi' => array ('midi', 'ISO-8859-1'),
rlm@3 1045 'nsv' => array ('nsv', 'ISO-8859-1'),
rlm@3 1046 'ogg' => array ('vorbiscomment', 'UTF-8'),
rlm@3 1047 'png' => array ('png', 'UTF-8'),
rlm@3 1048 'tiff' => array ('tiff', 'ISO-8859-1'),
rlm@3 1049 'quicktime' => array ('quicktime', 'ISO-8859-1'),
rlm@3 1050 'real' => array ('real', 'ISO-8859-1'),
rlm@3 1051 'vqf' => array ('vqf', 'ISO-8859-1'),
rlm@3 1052 'zip' => array ('zip', 'ISO-8859-1'),
rlm@3 1053 'riff' => array ('riff', 'ISO-8859-1'),
rlm@3 1054 'lyrics3' => array ('lyrics3', 'ISO-8859-1'),
rlm@3 1055 'id3v1' => array ('id3v1', ''), // change below - cannot assign variable to static array
rlm@3 1056 'id3v2' => array ('id3v2', 'UTF-8'), // module converts all frames to UTF-8
rlm@3 1057 'ape' => array ('ape', 'UTF-8')
rlm@3 1058 );
rlm@3 1059 $tags['id3v1'][1] = $this->encoding_id3v1;
rlm@3 1060
rlm@3 1061 // Loop thru tags array
rlm@3 1062 foreach ($tags as $comment_name => $tag_name_encoding_array) {
rlm@3 1063 list($tag_name, $encoding) = $tag_name_encoding_array;
rlm@3 1064
rlm@3 1065 // Fill in default encoding type if not already present
rlm@3 1066 @$this->info[$comment_name] and $this->info[$comment_name]['encoding'] = $encoding;
rlm@3 1067
rlm@3 1068 // Copy comments if key name set
rlm@3 1069 if (@$this->info[$comment_name]['comments']) {
rlm@3 1070
rlm@3 1071 foreach ($this->info[$comment_name]['comments'] as $tag_key => $value_array) {
rlm@3 1072 foreach ($value_array as $key => $value) {
rlm@3 1073 if (strlen(trim($value)) > 0) {
rlm@3 1074 $this->info['tags'][$tag_name][trim($tag_key)][] = $value; // do not trim!! Unicode characters will get mangled if trailing nulls are removed!
rlm@3 1075 }
rlm@3 1076 }
rlm@3 1077
rlm@3 1078 }
rlm@3 1079
rlm@3 1080 if (!@$this->info['tags'][$tag_name]) {
rlm@3 1081 // comments are set but contain nothing but empty strings, so skip
rlm@3 1082 continue;
rlm@3 1083 }
rlm@3 1084
rlm@3 1085 $this->CharConvert($this->info['tags'][$tag_name], $encoding);
rlm@3 1086 }
rlm@3 1087 }
rlm@3 1088
rlm@3 1089
rlm@3 1090 // Merge comments from ['tags'] into common ['comments']
rlm@3 1091 if (@$this->info['tags']) {
rlm@3 1092
rlm@3 1093 foreach ($this->info['tags'] as $tag_type => $tag_array) {
rlm@3 1094
rlm@3 1095 foreach ($tag_array as $tag_name => $tagdata) {
rlm@3 1096
rlm@3 1097 foreach ($tagdata as $key => $value) {
rlm@3 1098
rlm@3 1099 if (!empty($value)) {
rlm@3 1100
rlm@3 1101 if (empty($this->info['comments'][$tag_name])) {
rlm@3 1102
rlm@3 1103 // fall through and append value
rlm@3 1104 }
rlm@3 1105 elseif ($tag_type == 'id3v1') {
rlm@3 1106
rlm@3 1107 $new_value_length = strlen(trim($value));
rlm@3 1108 foreach ($this->info['comments'][$tag_name] as $existing_key => $existing_value) {
rlm@3 1109 $old_value_length = strlen(trim($existing_value));
rlm@3 1110 if (($new_value_length <= $old_value_length) && (substr($existing_value, 0, $new_value_length) == trim($value))) {
rlm@3 1111 // new value is identical but shorter-than (or equal-length to) one already in comments - skip
rlm@3 1112 break 2;
rlm@3 1113 }
rlm@3 1114 }
rlm@3 1115 }
rlm@3 1116 else {
rlm@3 1117
rlm@3 1118 $new_value_length = strlen(trim($value));
rlm@3 1119 foreach ($this->info['comments'][$tag_name] as $existing_key => $existing_value) {
rlm@3 1120 $old_value_length = strlen(trim($existing_value));
rlm@3 1121 if (($new_value_length > $old_value_length) && (substr(trim($value), 0, strlen($existing_value)) == $existing_value)) {
rlm@3 1122 $this->info['comments'][$tag_name][$existing_key] = trim($value);
rlm@3 1123 break 2;
rlm@3 1124 }
rlm@3 1125 }
rlm@3 1126 }
rlm@3 1127
rlm@3 1128 if (empty($this->info['comments'][$tag_name]) || !in_array(trim($value), $this->info['comments'][$tag_name])) {
rlm@3 1129 $this->info['comments'][$tag_name][] = trim($value);
rlm@3 1130 }
rlm@3 1131 }
rlm@3 1132 }
rlm@3 1133 }
rlm@3 1134 }
rlm@3 1135 }
rlm@3 1136
rlm@3 1137 return true;
rlm@3 1138 }
rlm@3 1139 }
rlm@3 1140
rlm@3 1141
rlm@3 1142 abstract class getid3_handler
rlm@3 1143 {
rlm@3 1144
rlm@3 1145 protected $getid3; // pointer
rlm@3 1146
rlm@3 1147 protected $data_string_flag = false; // analyzing filepointer or string
rlm@3 1148 protected $data_string; // string to analyze
rlm@3 1149 protected $data_string_position = 0; // seek position in string
rlm@3 1150
rlm@3 1151
rlm@3 1152 public function __construct(getID3 $getid3) {
rlm@3 1153
rlm@3 1154 $this->getid3 = $getid3;
rlm@3 1155 }
rlm@3 1156
rlm@3 1157
rlm@3 1158 // Analyze from file pointer
rlm@3 1159 abstract public function Analyze();
rlm@3 1160
rlm@3 1161
rlm@3 1162
rlm@3 1163 // Analyze from string instead
rlm@3 1164 public function AnalyzeString(&$string) {
rlm@3 1165
rlm@3 1166 // Enter string mode
rlm@3 1167 $this->data_string_flag = true;
rlm@3 1168 $this->data_string = $string;
rlm@3 1169
rlm@3 1170 // Save info
rlm@3 1171 $saved_avdataoffset = $this->getid3->info['avdataoffset'];
rlm@3 1172 $saved_avdataend = $this->getid3->info['avdataend'];
rlm@3 1173 $saved_filesize = $this->getid3->info['filesize'];
rlm@3 1174
rlm@3 1175 // Reset some info
rlm@3 1176 $this->getid3->info['avdataoffset'] = 0;
rlm@3 1177 $this->getid3->info['avdataend'] = $this->getid3->info['filesize'] = strlen($string);
rlm@3 1178
rlm@3 1179 // Analyze
rlm@3 1180 $this->Analyze();
rlm@3 1181
rlm@3 1182 // Restore some info
rlm@3 1183 $this->getid3->info['avdataoffset'] = $saved_avdataoffset;
rlm@3 1184 $this->getid3->info['avdataend'] = $saved_avdataend;
rlm@3 1185 $this->getid3->info['filesize'] = $saved_filesize;
rlm@3 1186
rlm@3 1187 // Exit string mode
rlm@3 1188 $this->data_string_flag = false;
rlm@3 1189 }
rlm@3 1190
rlm@3 1191
rlm@3 1192 protected function ftell() {
rlm@3 1193
rlm@3 1194 if ($this->data_string_flag) {
rlm@3 1195 return $this->data_string_position;
rlm@3 1196 }
rlm@3 1197 return ftell($this->getid3->fp);
rlm@3 1198 }
rlm@3 1199
rlm@3 1200
rlm@3 1201 protected function fread($bytes) {
rlm@3 1202
rlm@3 1203 if ($this->data_string_flag) {
rlm@3 1204 $this->data_string_position += $bytes;
rlm@3 1205 return substr($this->data_string, $this->data_string_position - $bytes, $bytes);
rlm@3 1206 }
rlm@3 1207 return fread($this->getid3->fp, $bytes);
rlm@3 1208 }
rlm@3 1209
rlm@3 1210
rlm@3 1211 protected function fseek($bytes, $whence = SEEK_SET) {
rlm@3 1212
rlm@3 1213 if ($this->data_string_flag) {
rlm@3 1214 switch ($whence) {
rlm@3 1215 case SEEK_SET:
rlm@3 1216 $this->data_string_position = $bytes;
rlm@3 1217 return;
rlm@3 1218
rlm@3 1219 case SEEK_CUR:
rlm@3 1220 $this->data_string_position += $bytes;
rlm@3 1221 return;
rlm@3 1222
rlm@3 1223 case SEEK_END:
rlm@3 1224 $this->data_string_position = strlen($this->data_string) + $bytes;
rlm@3 1225 return;
rlm@3 1226 }
rlm@3 1227 }
rlm@3 1228 return fseek($this->getid3->fp, $bytes, $whence);
rlm@3 1229 }
rlm@3 1230
rlm@3 1231 }
rlm@3 1232
rlm@3 1233
rlm@3 1234
rlm@3 1235
rlm@3 1236 abstract class getid3_handler_write
rlm@3 1237 {
rlm@3 1238 protected $filename;
rlm@3 1239 protected $user_abort;
rlm@3 1240
rlm@3 1241 private $fp_lock;
rlm@3 1242 private $owner;
rlm@3 1243 private $group;
rlm@3 1244 private $perms;
rlm@3 1245
rlm@3 1246
rlm@3 1247 public function __construct($filename) {
rlm@3 1248
rlm@3 1249 if (!file_exists($filename)) {
rlm@3 1250 throw new getid3_exception('File does not exist: "' . $filename . '"');
rlm@3 1251 }
rlm@3 1252
rlm@3 1253 if (!is_writeable($filename)) {
rlm@3 1254 throw new getid3_exception('File is not writeable: "' . $filename . '"');
rlm@3 1255 }
rlm@3 1256
rlm@3 1257 if (!is_writeable(dirname($filename))) {
rlm@3 1258 throw new getid3_exception('Directory is not writeable: ' . dirname($filename) . ' (need to write lock file).');
rlm@3 1259 }
rlm@3 1260
rlm@3 1261 $this->user_abort = ignore_user_abort(true);
rlm@3 1262
rlm@3 1263 $this->fp_lock = fopen($filename . '.getid3.lock', 'w');
rlm@3 1264 flock($this->fp_lock, LOCK_EX);
rlm@3 1265
rlm@3 1266 $this->filename = $filename;
rlm@3 1267 }
rlm@3 1268
rlm@3 1269
rlm@3 1270 public function __destruct() {
rlm@3 1271
rlm@3 1272 flock($this->fp_lock, LOCK_UN);
rlm@3 1273 fclose($this->fp_lock);
rlm@3 1274 unlink($this->filename . '.getid3.lock');
rlm@3 1275
rlm@3 1276 ignore_user_abort($this->user_abort);
rlm@3 1277 }
rlm@3 1278
rlm@3 1279
rlm@3 1280 protected function save_permissions() {
rlm@3 1281
rlm@3 1282 $this->owner = fileowner($this->filename);
rlm@3 1283 $this->group = filegroup($this->filename);
rlm@3 1284 $this->perms = fileperms($this->filename);
rlm@3 1285 }
rlm@3 1286
rlm@3 1287
rlm@3 1288 protected function restore_permissions() {
rlm@3 1289
rlm@3 1290 @chown($this->filename, $this->owner);
rlm@3 1291 @chgrp($this->filename, $this->group);
rlm@3 1292 @chmod($this->filename, $this->perms);
rlm@3 1293 }
rlm@3 1294
rlm@3 1295
rlm@3 1296 abstract public function read();
rlm@3 1297
rlm@3 1298 abstract public function write();
rlm@3 1299
rlm@3 1300 abstract public function remove();
rlm@3 1301
rlm@3 1302 }
rlm@3 1303
rlm@3 1304
rlm@3 1305
rlm@3 1306
rlm@3 1307 class getid3_exception extends Exception
rlm@3 1308 {
rlm@3 1309 public $message;
rlm@3 1310
rlm@3 1311 }
rlm@3 1312
rlm@3 1313
rlm@3 1314
rlm@3 1315
rlm@3 1316 class getid3_lib
rlm@3 1317 {
rlm@3 1318
rlm@3 1319 // Convert Little Endian byte string to int - max 32 bits
rlm@3 1320 public static function LittleEndian2Int($byte_word, $signed = false) {
rlm@3 1321
rlm@3 1322 return getid3_lib::BigEndian2Int(strrev($byte_word), $signed);
rlm@3 1323 }
rlm@3 1324
rlm@3 1325
rlm@3 1326
rlm@3 1327 // Convert number to Little Endian byte string
rlm@3 1328 public static function LittleEndian2String($number, $minbytes=1, $synchsafe=false) {
rlm@3 1329 $intstring = '';
rlm@3 1330 while ($number > 0) {
rlm@3 1331 if ($synchsafe) {
rlm@3 1332 $intstring = $intstring.chr($number & 127);
rlm@3 1333 $number >>= 7;
rlm@3 1334 } else {
rlm@3 1335 $intstring = $intstring.chr($number & 255);
rlm@3 1336 $number >>= 8;
rlm@3 1337 }
rlm@3 1338 }
rlm@3 1339 return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
rlm@3 1340 }
rlm@3 1341
rlm@3 1342
rlm@3 1343
rlm@3 1344 // Convert Big Endian byte string to int - max 32 bits
rlm@3 1345 public static function BigEndian2Int($byte_word, $signed = false) {
rlm@3 1346
rlm@3 1347 $int_value = 0;
rlm@3 1348 $byte_wordlen = strlen($byte_word);
rlm@3 1349
rlm@3 1350 for ($i = 0; $i < $byte_wordlen; $i++) {
rlm@3 1351 $int_value += ord($byte_word{$i}) * pow(256, ($byte_wordlen - 1 - $i));
rlm@3 1352 }
rlm@3 1353
rlm@3 1354 if ($signed) {
rlm@3 1355 $sign_mask_bit = 0x80 << (8 * ($byte_wordlen - 1));
rlm@3 1356 if ($int_value & $sign_mask_bit) {
rlm@3 1357 $int_value = 0 - ($int_value & ($sign_mask_bit - 1));
rlm@3 1358 }
rlm@3 1359 }
rlm@3 1360
rlm@3 1361 return $int_value;
rlm@3 1362 }
rlm@3 1363
rlm@3 1364
rlm@3 1365
rlm@3 1366 // Convert Big Endian byte sybc safe string to int - max 32 bits
rlm@3 1367 public static function BigEndianSyncSafe2Int($byte_word) {
rlm@3 1368
rlm@3 1369 $int_value = 0;
rlm@3 1370 $byte_wordlen = strlen($byte_word);
rlm@3 1371
rlm@3 1372 // disregard MSB, effectively 7-bit bytes
rlm@3 1373 for ($i = 0; $i < $byte_wordlen; $i++) {
rlm@3 1374 $int_value = $int_value | (ord($byte_word{$i}) & 0x7F) << (($byte_wordlen - 1 - $i) * 7);
rlm@3 1375 }
rlm@3 1376 return $int_value;
rlm@3 1377 }
rlm@3 1378
rlm@3 1379
rlm@3 1380
rlm@3 1381 // Convert Big Endian byte string to bit string
rlm@3 1382 public static function BigEndian2Bin($byte_word) {
rlm@3 1383
rlm@3 1384 $bin_value = '';
rlm@3 1385 $byte_wordlen = strlen($byte_word);
rlm@3 1386 for ($i = 0; $i < $byte_wordlen; $i++) {
rlm@3 1387 $bin_value .= str_pad(decbin(ord($byte_word{$i})), 8, '0', STR_PAD_LEFT);
rlm@3 1388 }
rlm@3 1389 return $bin_value;
rlm@3 1390 }
rlm@3 1391
rlm@3 1392
rlm@3 1393
rlm@3 1394 public static function BigEndian2Float($byte_word) {
rlm@3 1395
rlm@3 1396 // ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic
rlm@3 1397 // http://www.psc.edu/general/software/packages/ieee/ieee.html
rlm@3 1398 // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html
rlm@3 1399
rlm@3 1400 $bit_word = getid3_lib::BigEndian2Bin($byte_word);
rlm@3 1401 if (!$bit_word) {
rlm@3 1402 return 0;
rlm@3 1403 }
rlm@3 1404 $sign_bit = $bit_word{0};
rlm@3 1405
rlm@3 1406 switch (strlen($byte_word) * 8) {
rlm@3 1407 case 32:
rlm@3 1408 $exponent_bits = 8;
rlm@3 1409 $fraction_bits = 23;
rlm@3 1410 break;
rlm@3 1411
rlm@3 1412 case 64:
rlm@3 1413 $exponent_bits = 11;
rlm@3 1414 $fraction_bits = 52;
rlm@3 1415 break;
rlm@3 1416
rlm@3 1417 case 80:
rlm@3 1418 // 80-bit Apple SANE format
rlm@3 1419 // http://www.mactech.com/articles/mactech/Vol.06/06.01/SANENormalized/
rlm@3 1420 $exponent_string = substr($bit_word, 1, 15);
rlm@3 1421 $is_normalized = intval($bit_word{16});
rlm@3 1422 $fraction_string = substr($bit_word, 17, 63);
rlm@3 1423 $exponent = pow(2, getid3_lib::Bin2Dec($exponent_string) - 16383);
rlm@3 1424 $fraction = $is_normalized + getid3_lib::DecimalBinary2Float($fraction_string);
rlm@3 1425 $float_value = $exponent * $fraction;
rlm@3 1426 if ($sign_bit == '1') {
rlm@3 1427 $float_value *= -1;
rlm@3 1428 }
rlm@3 1429 return $float_value;
rlm@3 1430 break;
rlm@3 1431
rlm@3 1432 default:
rlm@3 1433 return false;
rlm@3 1434 break;
rlm@3 1435 }
rlm@3 1436 $exponent_string = substr($bit_word, 1, $exponent_bits);
rlm@3 1437 $fraction_string = substr($bit_word, $exponent_bits + 1, $fraction_bits);
rlm@3 1438 $exponent = bindec($exponent_string);
rlm@3 1439 $fraction = bindec($fraction_string);
rlm@3 1440
rlm@3 1441 if (($exponent == (pow(2, $exponent_bits) - 1)) && ($fraction != 0)) {
rlm@3 1442 // Not a Number
rlm@3 1443 $float_value = false;
rlm@3 1444 } elseif (($exponent == (pow(2, $exponent_bits) - 1)) && ($fraction == 0)) {
rlm@3 1445 if ($sign_bit == '1') {
rlm@3 1446 $float_value = '-infinity';
rlm@3 1447 } else {
rlm@3 1448 $float_value = '+infinity';
rlm@3 1449 }
rlm@3 1450 } elseif (($exponent == 0) && ($fraction == 0)) {
rlm@3 1451 if ($sign_bit == '1') {
rlm@3 1452 $float_value = -0;
rlm@3 1453 } else {
rlm@3 1454 $float_value = 0;
rlm@3 1455 }
rlm@3 1456 $float_value = ($sign_bit ? 0 : -0);
rlm@3 1457 } elseif (($exponent == 0) && ($fraction != 0)) {
rlm@3 1458 // These are 'unnormalized' values
rlm@3 1459 $float_value = pow(2, (-1 * (pow(2, $exponent_bits - 1) - 2))) * getid3_lib::DecimalBinary2Float($fraction_string);
rlm@3 1460 if ($sign_bit == '1') {
rlm@3 1461 $float_value *= -1;
rlm@3 1462 }
rlm@3 1463 } elseif ($exponent != 0) {
rlm@3 1464 $float_value = pow(2, ($exponent - (pow(2, $exponent_bits - 1) - 1))) * (1 + getid3_lib::DecimalBinary2Float($fraction_string));
rlm@3 1465 if ($sign_bit == '1') {
rlm@3 1466 $float_value *= -1;
rlm@3 1467 }
rlm@3 1468 }
rlm@3 1469 return (float) $float_value;
rlm@3 1470 }
rlm@3 1471
rlm@3 1472
rlm@3 1473
rlm@3 1474 public static function LittleEndian2Float($byte_word) {
rlm@3 1475
rlm@3 1476 return getid3_lib::BigEndian2Float(strrev($byte_word));
rlm@3 1477 }
rlm@3 1478
rlm@3 1479
rlm@3 1480
rlm@3 1481 public static function DecimalBinary2Float($binary_numerator) {
rlm@3 1482 $numerator = bindec($binary_numerator);
rlm@3 1483 $denominator = bindec('1'.str_repeat('0', strlen($binary_numerator)));
rlm@3 1484 return ($numerator / $denominator);
rlm@3 1485 }
rlm@3 1486
rlm@3 1487
rlm@3 1488 public static function PrintHexBytes($string, $hex=true, $spaces=true, $html_safe=true) {
rlm@3 1489
rlm@3 1490 $return_string = '';
rlm@3 1491 for ($i = 0; $i < strlen($string); $i++) {
rlm@3 1492 if ($hex) {
rlm@3 1493 $return_string .= str_pad(dechex(ord($string{$i})), 2, '0', STR_PAD_LEFT);
rlm@3 1494 } else {
rlm@3 1495 $return_string .= ' '.(ereg("[\x20-\x7E]", $string{$i}) ? $string{$i} : '');
rlm@3 1496 }
rlm@3 1497 if ($spaces) {
rlm@3 1498 $return_string .= ' ';
rlm@3 1499 }
rlm@3 1500 }
rlm@3 1501 if ($html_safe) {
rlm@3 1502 $return_string = htmlentities($return_string);
rlm@3 1503 }
rlm@3 1504 return $return_string;
rlm@3 1505 }
rlm@3 1506
rlm@3 1507
rlm@3 1508
rlm@3 1509 // Process header data string - read several values with algorithm and add to target
rlm@3 1510 // algorithm is one one the getid3_lib::Something2Something() function names
rlm@3 1511 // parts_array is index => length - $target[index] = algorithm(substring(data))
rlm@3 1512 // - OR just substring(data) if length is negative!
rlm@3 1513 // indexes == 'IGNORE**' are ignored
rlm@3 1514
rlm@3 1515 public static function ReadSequence($algorithm, &$target, &$data, $offset, $parts_array) {
rlm@3 1516
rlm@3 1517 // Loop thru $parts_array
rlm@3 1518 foreach ($parts_array as $target_string => $length) {
rlm@3 1519
rlm@3 1520 // Add to target
rlm@3 1521 if (!strstr($target_string, 'IGNORE')) {
rlm@3 1522
rlm@3 1523 // substr(....length)
rlm@3 1524 if ($length < 0) {
rlm@3 1525 $target[$target_string] = substr($data, $offset, -$length);
rlm@3 1526 }
rlm@3 1527
rlm@3 1528 // algorithm(substr(...length))
rlm@3 1529 else {
rlm@3 1530 $target[$target_string] = getid3_lib::$algorithm(substr($data, $offset, $length));
rlm@3 1531 }
rlm@3 1532 }
rlm@3 1533
rlm@3 1534 // Move pointer
rlm@3 1535 $offset += abs($length);
rlm@3 1536 }
rlm@3 1537 }
rlm@3 1538
rlm@3 1539 }
rlm@3 1540
rlm@3 1541
rlm@3 1542
rlm@3 1543 class getid3_lib_replaygain
rlm@3 1544 {
rlm@3 1545
rlm@3 1546 public static function NameLookup($name_code) {
rlm@3 1547
rlm@3 1548 static $lookup = array (
rlm@3 1549 0 => 'not set',
rlm@3 1550 1 => 'Track Gain Adjustment',
rlm@3 1551 2 => 'Album Gain Adjustment'
rlm@3 1552 );
rlm@3 1553
rlm@3 1554 return @$lookup[$name_code];
rlm@3 1555 }
rlm@3 1556
rlm@3 1557
rlm@3 1558
rlm@3 1559 public static function OriginatorLookup($originator_code) {
rlm@3 1560
rlm@3 1561 static $lookup = array (
rlm@3 1562 0 => 'unspecified',
rlm@3 1563 1 => 'pre-set by artist/producer/mastering engineer',
rlm@3 1564 2 => 'set by user',
rlm@3 1565 3 => 'determined automatically'
rlm@3 1566 );
rlm@3 1567
rlm@3 1568 return @$lookup[$originator_code];
rlm@3 1569 }
rlm@3 1570
rlm@3 1571
rlm@3 1572
rlm@3 1573 public static function AdjustmentLookup($raw_adjustment, $sign_bit) {
rlm@3 1574
rlm@3 1575 return (float)$raw_adjustment / 10 * ($sign_bit == 1 ? -1 : 1);
rlm@3 1576 }
rlm@3 1577
rlm@3 1578
rlm@3 1579
rlm@3 1580 public static function GainString($name_code, $originator_code, $replaygain) {
rlm@3 1581
rlm@3 1582 $sign_bit = $replaygain < 0 ? 1 : 0;
rlm@3 1583
rlm@3 1584 $stored_replaygain = intval(round($replaygain * 10));
rlm@3 1585 $gain_string = str_pad(decbin($name_code), 3, '0', STR_PAD_LEFT);
rlm@3 1586 $gain_string .= str_pad(decbin($originator_code), 3, '0', STR_PAD_LEFT);
rlm@3 1587 $gain_string .= $sign_bit;
rlm@3 1588 $gain_string .= str_pad(decbin($stored_replaygain), 9, '0', STR_PAD_LEFT);
rlm@3 1589
rlm@3 1590 return $gain_string;
rlm@3 1591 }
rlm@3 1592
rlm@3 1593 }
rlm@3 1594
rlm@3 1595
rlm@3 1596
rlm@3 1597
rlm@3 1598 ?>