source: extensions/charlies_content/getid3/getid3/getid3.lib.php @ 9130

Last change on this file since 9130 was 3544, checked in by vdigital, 15 years ago

Change: getid3 upgraded to -> 1.7.9

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 40.0 KB
Line 
1<?php
2/////////////////////////////////////////////////////////////////
3/// getID3() by James Heinrich <info@getid3.org>               //
4//  available at http://getid3.sourceforge.net                 //
5//            or http://www.getid3.org                         //
6/////////////////////////////////////////////////////////////////
7//                                                             //
8// getid3.lib.php - part of getID3()                           //
9// See readme.txt for more details                             //
10//                                                            ///
11/////////////////////////////////////////////////////////////////
12
13
14class getid3_lib
15{
16
17        function PrintHexBytes($string, $hex=true, $spaces=true, $htmlsafe=true) {
18                $returnstring = '';
19                for ($i = 0; $i < strlen($string); $i++) {
20                        if ($hex) {
21                                $returnstring .= str_pad(dechex(ord($string{$i})), 2, '0', STR_PAD_LEFT);
22                        } else {
23                                $returnstring .= ' '.(ereg("[\x20-\x7E]", $string{$i}) ? $string{$i} : '¤');
24                        }
25                        if ($spaces) {
26                                $returnstring .= ' ';
27                        }
28                }
29                if ($htmlsafe) {
30                        $returnstring = htmlentities($returnstring);
31                }
32                return $returnstring;
33        }
34
35        function SafeStripSlashes($text) {
36                if (get_magic_quotes_gpc()) {
37                        return stripslashes($text);
38                }
39                return $text;
40        }
41
42
43        function trunc($floatnumber) {
44                // truncates a floating-point number at the decimal point
45                // returns int (if possible, otherwise float)
46                if ($floatnumber >= 1) {
47                        $truncatednumber = floor($floatnumber);
48                } elseif ($floatnumber <= -1) {
49                        $truncatednumber = ceil($floatnumber);
50                } else {
51                        $truncatednumber = 0;
52                }
53                if ($truncatednumber <= 1073741824) { // 2^30
54                        $truncatednumber = (int) $truncatednumber;
55                }
56                return $truncatednumber;
57        }
58
59
60        function CastAsInt($floatnum) {
61                // convert to float if not already
62                $floatnum = (float) $floatnum;
63
64                // convert a float to type int, only if possible
65                if (getid3_lib::trunc($floatnum) == $floatnum) {
66                        // it's not floating point
67                        if ($floatnum <= 2147483647) { // 2^31
68                                // it's within int range
69                                $floatnum = (int) $floatnum;
70                        }
71                }
72                return $floatnum;
73        }
74
75
76        function DecimalBinary2Float($binarynumerator) {
77                $numerator   = getid3_lib::Bin2Dec($binarynumerator);
78                $denominator = getid3_lib::Bin2Dec('1'.str_repeat('0', strlen($binarynumerator)));
79                return ($numerator / $denominator);
80        }
81
82
83        function NormalizeBinaryPoint($binarypointnumber, $maxbits=52) {
84                // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
85                if (strpos($binarypointnumber, '.') === false) {
86                        $binarypointnumber = '0.'.$binarypointnumber;
87                } elseif ($binarypointnumber{0} == '.') {
88                        $binarypointnumber = '0'.$binarypointnumber;
89                }
90                $exponent = 0;
91                while (($binarypointnumber{0} != '1') || (substr($binarypointnumber, 1, 1) != '.')) {
92                        if (substr($binarypointnumber, 1, 1) == '.') {
93                                $exponent--;
94                                $binarypointnumber = substr($binarypointnumber, 2, 1).'.'.substr($binarypointnumber, 3);
95                        } else {
96                                $pointpos = strpos($binarypointnumber, '.');
97                                $exponent += ($pointpos - 1);
98                                $binarypointnumber = str_replace('.', '', $binarypointnumber);
99                                $binarypointnumber = $binarypointnumber{0}.'.'.substr($binarypointnumber, 1);
100                        }
101                }
102                $binarypointnumber = str_pad(substr($binarypointnumber, 0, $maxbits + 2), $maxbits + 2, '0', STR_PAD_RIGHT);
103                return array('normalized'=>$binarypointnumber, 'exponent'=>(int) $exponent);
104        }
105
106
107        function Float2BinaryDecimal($floatvalue) {
108                // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
109                $maxbits = 128; // to how many bits of precision should the calculations be taken?
110                $intpart   = getid3_lib::trunc($floatvalue);
111                $floatpart = abs($floatvalue - $intpart);
112                $pointbitstring = '';
113                while (($floatpart != 0) && (strlen($pointbitstring) < $maxbits)) {
114                        $floatpart *= 2;
115                        $pointbitstring .= (string) getid3_lib::trunc($floatpart);
116                        $floatpart -= getid3_lib::trunc($floatpart);
117                }
118                $binarypointnumber = decbin($intpart).'.'.$pointbitstring;
119                return $binarypointnumber;
120        }
121
122
123        function Float2String($floatvalue, $bits) {
124                // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee-expl.html
125                switch ($bits) {
126                        case 32:
127                                $exponentbits = 8;
128                                $fractionbits = 23;
129                                break;
130
131                        case 64:
132                                $exponentbits = 11;
133                                $fractionbits = 52;
134                                break;
135
136                        default:
137                                return false;
138                                break;
139                }
140                if ($floatvalue >= 0) {
141                        $signbit = '0';
142                } else {
143                        $signbit = '1';
144                }
145                $normalizedbinary  = getid3_lib::NormalizeBinaryPoint(getid3_lib::Float2BinaryDecimal($floatvalue), $fractionbits);
146                $biasedexponent    = pow(2, $exponentbits - 1) - 1 + $normalizedbinary['exponent']; // (127 or 1023) +/- exponent
147                $exponentbitstring = str_pad(decbin($biasedexponent), $exponentbits, '0', STR_PAD_LEFT);
148                $fractionbitstring = str_pad(substr($normalizedbinary['normalized'], 2), $fractionbits, '0', STR_PAD_RIGHT);
149
150                return getid3_lib::BigEndian2String(getid3_lib::Bin2Dec($signbit.$exponentbitstring.$fractionbitstring), $bits % 8, false);
151        }
152
153
154        function LittleEndian2Float($byteword) {
155                return getid3_lib::BigEndian2Float(strrev($byteword));
156        }
157
158
159        function BigEndian2Float($byteword) {
160                // ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic
161                // http://www.psc.edu/general/software/packages/ieee/ieee.html
162                // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html
163
164                $bitword = getid3_lib::BigEndian2Bin($byteword);
165                if (!$bitword) {
166            return 0;
167        }
168                $signbit = $bitword{0};
169
170                switch (strlen($byteword) * 8) {
171                        case 32:
172                                $exponentbits = 8;
173                                $fractionbits = 23;
174                                break;
175
176                        case 64:
177                                $exponentbits = 11;
178                                $fractionbits = 52;
179                                break;
180
181                        case 80:
182                                // 80-bit Apple SANE format
183                                // http://www.mactech.com/articles/mactech/Vol.06/06.01/SANENormalized/
184                                $exponentstring = substr($bitword, 1, 15);
185                                $isnormalized = intval($bitword{16});
186                                $fractionstring = substr($bitword, 17, 63);
187                                $exponent = pow(2, getid3_lib::Bin2Dec($exponentstring) - 16383);
188                                $fraction = $isnormalized + getid3_lib::DecimalBinary2Float($fractionstring);
189                                $floatvalue = $exponent * $fraction;
190                                if ($signbit == '1') {
191                                        $floatvalue *= -1;
192                                }
193                                return $floatvalue;
194                                break;
195
196                        default:
197                                return false;
198                                break;
199                }
200                $exponentstring = substr($bitword, 1, $exponentbits);
201                $fractionstring = substr($bitword, $exponentbits + 1, $fractionbits);
202                $exponent = getid3_lib::Bin2Dec($exponentstring);
203                $fraction = getid3_lib::Bin2Dec($fractionstring);
204
205                if (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction != 0)) {
206                        // Not a Number
207                        $floatvalue = false;
208                } elseif (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction == 0)) {
209                        if ($signbit == '1') {
210                                $floatvalue = '-infinity';
211                        } else {
212                                $floatvalue = '+infinity';
213                        }
214                } elseif (($exponent == 0) && ($fraction == 0)) {
215                        if ($signbit == '1') {
216                                $floatvalue = -0;
217                        } else {
218                                $floatvalue = 0;
219                        }
220                        $floatvalue = ($signbit ? 0 : -0);
221                } elseif (($exponent == 0) && ($fraction != 0)) {
222                        // These are 'unnormalized' values
223                        $floatvalue = pow(2, (-1 * (pow(2, $exponentbits - 1) - 2))) * getid3_lib::DecimalBinary2Float($fractionstring);
224                        if ($signbit == '1') {
225                                $floatvalue *= -1;
226                        }
227                } elseif ($exponent != 0) {
228                        $floatvalue = pow(2, ($exponent - (pow(2, $exponentbits - 1) - 1))) * (1 + getid3_lib::DecimalBinary2Float($fractionstring));
229                        if ($signbit == '1') {
230                                $floatvalue *= -1;
231                        }
232                }
233                return (float) $floatvalue;
234        }
235
236
237        function BigEndian2Int($byteword, $synchsafe=false, $signed=false) {
238                $intvalue = 0;
239                $bytewordlen = strlen($byteword);
240                for ($i = 0; $i < $bytewordlen; $i++) {
241                        if ($synchsafe) { // disregard MSB, effectively 7-bit bytes
242                                $intvalue = $intvalue | (ord($byteword{$i}) & 0x7F) << (($bytewordlen - 1 - $i) * 7);
243                        } else {
244                                $intvalue += ord($byteword{$i}) * pow(256, ($bytewordlen - 1 - $i));
245                        }
246                }
247                if ($signed && !$synchsafe) {
248                        // synchsafe ints are not allowed to be signed
249                        switch ($bytewordlen) {
250                                case 1:
251                                case 2:
252                                case 3:
253                                case 4:
254                                        $signmaskbit = 0x80 << (8 * ($bytewordlen - 1));
255                                        if ($intvalue & $signmaskbit) {
256                                                $intvalue = 0 - ($intvalue & ($signmaskbit - 1));
257                                        }
258                                        break;
259
260                                default:
261                                        die('ERROR: Cannot have signed integers larger than 32-bits in getid3_lib::BigEndian2Int()');
262                                        break;
263                        }
264                }
265                return getid3_lib::CastAsInt($intvalue);
266        }
267
268
269        function LittleEndian2Int($byteword, $signed=false) {
270                return getid3_lib::BigEndian2Int(strrev($byteword), false, $signed);
271        }
272
273
274        function BigEndian2Bin($byteword) {
275                $binvalue = '';
276                $bytewordlen = strlen($byteword);
277                for ($i = 0; $i < $bytewordlen; $i++) {
278                        $binvalue .= str_pad(decbin(ord($byteword{$i})), 8, '0', STR_PAD_LEFT);
279                }
280                return $binvalue;
281        }
282
283
284        function BigEndian2String($number, $minbytes=1, $synchsafe=false, $signed=false) {
285                if ($number < 0) {
286                        return false;
287                }
288                $maskbyte = (($synchsafe || $signed) ? 0x7F : 0xFF);
289                $intstring = '';
290                if ($signed) {
291                        if ($minbytes > 4) {
292                                die('ERROR: Cannot have signed integers larger than 32-bits in getid3_lib::BigEndian2String()');
293                        }
294                        $number = $number & (0x80 << (8 * ($minbytes - 1)));
295                }
296                while ($number != 0) {
297                        $quotient = ($number / ($maskbyte + 1));
298                        $intstring = chr(ceil(($quotient - floor($quotient)) * $maskbyte)).$intstring;
299                        $number = floor($quotient);
300                }
301                return str_pad($intstring, $minbytes, "\x00", STR_PAD_LEFT);
302        }
303
304
305        function Dec2Bin($number) {
306                while ($number >= 256) {
307                        $bytes[] = (($number / 256) - (floor($number / 256))) * 256;
308                        $number = floor($number / 256);
309                }
310                $bytes[] = $number;
311                $binstring = '';
312                for ($i = 0; $i < count($bytes); $i++) {
313                        $binstring = (($i == count($bytes) - 1) ? decbin($bytes[$i]) : str_pad(decbin($bytes[$i]), 8, '0', STR_PAD_LEFT)).$binstring;
314                }
315                return $binstring;
316        }
317
318
319        function Bin2Dec($binstring, $signed=false) {
320                $signmult = 1;
321                if ($signed) {
322                        if ($binstring{0} == '1') {
323                                $signmult = -1;
324                        }
325                        $binstring = substr($binstring, 1);
326                }
327                $decvalue = 0;
328                for ($i = 0; $i < strlen($binstring); $i++) {
329                        $decvalue += ((int) substr($binstring, strlen($binstring) - $i - 1, 1)) * pow(2, $i);
330                }
331                return getid3_lib::CastAsInt($decvalue * $signmult);
332        }
333
334
335        function Bin2String($binstring) {
336                // return 'hi' for input of '0110100001101001'
337                $string = '';
338                $binstringreversed = strrev($binstring);
339                for ($i = 0; $i < strlen($binstringreversed); $i += 8) {
340                        $string = chr(getid3_lib::Bin2Dec(strrev(substr($binstringreversed, $i, 8)))).$string;
341                }
342                return $string;
343        }
344
345
346        function LittleEndian2String($number, $minbytes=1, $synchsafe=false) {
347                $intstring = '';
348                while ($number > 0) {
349                        if ($synchsafe) {
350                                $intstring = $intstring.chr($number & 127);
351                                $number >>= 7;
352                        } else {
353                                $intstring = $intstring.chr($number & 255);
354                                $number >>= 8;
355                        }
356                }
357                return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
358        }
359
360
361        function array_merge_clobber($array1, $array2) {
362                // written by kcØhireability*com
363                // taken from http://www.php.net/manual/en/function.array-merge-recursive.php
364                if (!is_array($array1) || !is_array($array2)) {
365                        return false;
366                }
367                $newarray = $array1;
368                foreach ($array2 as $key => $val) {
369                        if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) {
370                                $newarray[$key] = getid3_lib::array_merge_clobber($newarray[$key], $val);
371                        } else {
372                                $newarray[$key] = $val;
373                        }
374                }
375                return $newarray;
376        }
377
378
379        function array_merge_noclobber($array1, $array2) {
380                if (!is_array($array1) || !is_array($array2)) {
381                        return false;
382                }
383                $newarray = $array1;
384                foreach ($array2 as $key => $val) {
385                        if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) {
386                                $newarray[$key] = getid3_lib::array_merge_noclobber($newarray[$key], $val);
387                        } elseif (!isset($newarray[$key])) {
388                                $newarray[$key] = $val;
389                        }
390                }
391                return $newarray;
392        }
393
394
395        function fileextension($filename, $numextensions=1) {
396                if (strstr($filename, '.')) {
397                        $reversedfilename = strrev($filename);
398                        $offset = 0;
399                        for ($i = 0; $i < $numextensions; $i++) {
400                                $offset = strpos($reversedfilename, '.', $offset + 1);
401                                if ($offset === false) {
402                                        return '';
403                                }
404                        }
405                        return strrev(substr($reversedfilename, 0, $offset));
406                }
407                return '';
408        }
409
410
411        function PlaytimeString($playtimeseconds) {
412                $sign = (($playtimeseconds < 0) ? '-' : '');
413                $playtimeseconds = abs($playtimeseconds);
414                $contentseconds = round((($playtimeseconds / 60) - floor($playtimeseconds / 60)) * 60);
415                $contentminutes = floor($playtimeseconds / 60);
416                if ($contentseconds >= 60) {
417                        $contentseconds -= 60;
418                        $contentminutes++;
419                }
420                return $sign.intval($contentminutes).':'.str_pad($contentseconds, 2, 0, STR_PAD_LEFT);
421        }
422
423
424        function image_type_to_mime_type($imagetypeid) {
425                // only available in PHP v4.3.0+
426                static $image_type_to_mime_type = array();
427                if (empty($image_type_to_mime_type)) {
428                        $image_type_to_mime_type[1]  = 'image/gif';                     // GIF
429                        $image_type_to_mime_type[2]  = 'image/jpeg';                    // JPEG
430                        $image_type_to_mime_type[3]  = 'image/png';                     // PNG
431                        $image_type_to_mime_type[4]  = 'application/x-shockwave-flash'; // Flash
432                        $image_type_to_mime_type[5]  = 'image/psd';                     // PSD
433                        $image_type_to_mime_type[6]  = 'image/bmp';                     // BMP
434                        $image_type_to_mime_type[7]  = 'image/tiff';                    // TIFF: little-endian (Intel)
435                        $image_type_to_mime_type[8]  = 'image/tiff';                    // TIFF: big-endian (Motorola)
436                        //$image_type_to_mime_type[9]  = 'image/jpc';                   // JPC
437                        //$image_type_to_mime_type[10] = 'image/jp2';                   // JPC
438                        //$image_type_to_mime_type[11] = 'image/jpx';                   // JPC
439                        //$image_type_to_mime_type[12] = 'image/jb2';                   // JPC
440                        $image_type_to_mime_type[13] = 'application/x-shockwave-flash'; // Shockwave
441                        $image_type_to_mime_type[14] = 'image/iff';                     // IFF
442                }
443                return (isset($image_type_to_mime_type[$imagetypeid]) ? $image_type_to_mime_type[$imagetypeid] : 'application/octet-stream');
444        }
445
446
447        function DateMac2Unix($macdate) {
448                // Macintosh timestamp: seconds since 00:00h January 1, 1904
449                // UNIX timestamp:      seconds since 00:00h January 1, 1970
450                return getid3_lib::CastAsInt($macdate - 2082844800);
451        }
452
453
454        function FixedPoint8_8($rawdata) {
455                return getid3_lib::BigEndian2Int(substr($rawdata, 0, 1)) + (float) (getid3_lib::BigEndian2Int(substr($rawdata, 1, 1)) / pow(2, 8));
456        }
457
458
459        function FixedPoint16_16($rawdata) {
460                return getid3_lib::BigEndian2Int(substr($rawdata, 0, 2)) + (float) (getid3_lib::BigEndian2Int(substr($rawdata, 2, 2)) / pow(2, 16));
461        }
462
463
464        function FixedPoint2_30($rawdata) {
465                $binarystring = getid3_lib::BigEndian2Bin($rawdata);
466                return getid3_lib::Bin2Dec(substr($binarystring, 0, 2)) + (float) (getid3_lib::Bin2Dec(substr($binarystring, 2, 30)) / 1073741824);
467        }
468
469
470        function CreateDeepArray($ArrayPath, $Separator, $Value) {
471                // assigns $Value to a nested array path:
472                //   $foo = getid3_lib::CreateDeepArray('/path/to/my', '/', 'file.txt')
473                // is the same as:
474                //   $foo = array('path'=>array('to'=>'array('my'=>array('file.txt'))));
475                // or
476                //   $foo['path']['to']['my'] = 'file.txt';
477                while ($ArrayPath && ($ArrayPath{0} == $Separator)) {
478                        $ArrayPath = substr($ArrayPath, 1);
479                }
480                if (($pos = strpos($ArrayPath, $Separator)) !== false) {
481                        $ReturnedArray[substr($ArrayPath, 0, $pos)] = getid3_lib::CreateDeepArray(substr($ArrayPath, $pos + 1), $Separator, $Value);
482                } else {
483                        $ReturnedArray[$ArrayPath] = $Value;
484                }
485                return $ReturnedArray;
486        }
487
488        function array_max($arraydata, $returnkey=false) {
489                $maxvalue = false;
490                $maxkey = false;
491                foreach ($arraydata as $key => $value) {
492                        if (!is_array($value)) {
493                                if ($value > $maxvalue) {
494                                        $maxvalue = $value;
495                                        $maxkey = $key;
496                                }
497                        }
498                }
499                return ($returnkey ? $maxkey : $maxvalue);
500        }
501
502        function array_min($arraydata, $returnkey=false) {
503                $minvalue = false;
504                $minkey = false;
505                foreach ($arraydata as $key => $value) {
506                        if (!is_array($value)) {
507                                if ($value > $minvalue) {
508                                        $minvalue = $value;
509                                        $minkey = $key;
510                                }
511                        }
512                }
513                return ($returnkey ? $minkey : $minvalue);
514        }
515
516
517        function md5_file($file) {
518
519                // md5_file() exists in PHP 4.2.0+.
520                if (function_exists('md5_file')) {
521                        return md5_file($file);
522                }
523
524                if (GETID3_OS_ISWINDOWS) {
525
526                        $RequiredFiles = array('cygwin1.dll', 'md5sum.exe');
527                        foreach ($RequiredFiles as $required_file) {
528                                if (!is_readable(GETID3_HELPERAPPSDIR.$required_file)) {
529                                        die(implode(' and ', $RequiredFiles).' are required in '.GETID3_HELPERAPPSDIR.' for getid3_lib::md5_file() to function under Windows in PHP < v4.2.0');
530                                }
531                        }
532                        $commandline = GETID3_HELPERAPPSDIR.'md5sum.exe "'.str_replace('/', DIRECTORY_SEPARATOR, $file).'"';
533                        if (ereg("^[\\]?([0-9a-f]{32})", strtolower(`$commandline`), $r)) {
534                                return $r[1];
535                        }
536
537                } else {
538
539                        // The following works under UNIX only
540                        $file = str_replace('`', '\\`', $file);
541                        if (ereg("^([0-9a-f]{32})[ \t\n\r]", `md5sum "$file"`, $r)) {
542                                return $r[1];
543                        }
544
545                }
546                return false;
547        }
548
549
550        function sha1_file($file) {
551
552                // sha1_file() exists in PHP 4.3.0+.
553                if (function_exists('sha1_file')) {
554                        return sha1_file($file);
555                }
556
557                $file = str_replace('`', '\\`', $file);
558
559                if (GETID3_OS_ISWINDOWS) {
560
561                        $RequiredFiles = array('cygwin1.dll', 'sha1sum.exe');
562                        foreach ($RequiredFiles as $required_file) {
563                                if (!is_readable(GETID3_HELPERAPPSDIR.$required_file)) {
564                                        die(implode(' and ', $RequiredFiles).' are required in '.GETID3_HELPERAPPSDIR.' for getid3_lib::sha1_file() to function under Windows in PHP < v4.3.0');
565                                }
566                        }
567                        $commandline = GETID3_HELPERAPPSDIR.'sha1sum.exe "'.str_replace('/', DIRECTORY_SEPARATOR, $file).'"';
568                        if (ereg("^sha1=([0-9a-f]{40})", strtolower(`$commandline`), $r)) {
569                                return $r[1];
570                        }
571
572                } else {
573
574                        $commandline = 'sha1sum '.escapeshellarg($file).'';
575                        if (ereg("^([0-9a-f]{40})[ \t\n\r]", strtolower(`$commandline`), $r)) {
576                                return $r[1];
577                        }
578
579                }
580
581                return false;
582        }
583
584
585        // Allan Hansen <ahØartemis*dk>
586        // getid3_lib::md5_data() - returns md5sum for a file from startuing position to absolute end position
587        function hash_data($file, $offset, $end, $algorithm) {
588                if ($end >= pow(2, 31)) {
589                        return false;
590                }
591
592                switch ($algorithm) {
593                        case 'md5':
594                                $hash_function = 'md5_file';
595                                $unix_call     = 'md5sum';
596                                $windows_call  = 'md5sum.exe';
597                                $hash_length   = 32;
598                                break;
599
600                        case 'sha1':
601                                $hash_function = 'sha1_file';
602                                $unix_call     = 'sha1sum';
603                                $windows_call  = 'sha1sum.exe';
604                                $hash_length   = 40;
605                                break;
606
607                        default:
608                                die('Invalid algorithm ('.$algorithm.') in getid3_lib::hash_data()');
609                                break;
610                }
611                $size = $end - $offset;
612                while (true) {
613                        if (GETID3_OS_ISWINDOWS) {
614
615                                // It seems that sha1sum.exe for Windows only works on physical files, does not accept piped data
616                                // Fall back to create-temp-file method:
617                                if ($algorithm == 'sha1') {
618                                        break;
619                                }
620
621                                $RequiredFiles = array('cygwin1.dll', 'head.exe', 'tail.exe', $windows_call);
622                                foreach ($RequiredFiles as $required_file) {
623                                        if (!is_readable(GETID3_HELPERAPPSDIR.$required_file)) {
624                                                // helper apps not available - fall back to old method
625                                                break;
626                                        }
627                                }
628                                $commandline  = GETID3_HELPERAPPSDIR.'head.exe -c '.$end.' "'.escapeshellarg(str_replace('/', DIRECTORY_SEPARATOR, $file)).'" | ';
629                                $commandline .= GETID3_HELPERAPPSDIR.'tail.exe -c '.$size.' | ';
630                                $commandline .= GETID3_HELPERAPPSDIR.$windows_call;
631
632                        } else {
633
634                                $commandline  = 'head -c'.$end.' '.escapeshellarg($file).' | ';
635                                $commandline .= 'tail -c'.$size.' | ';
636                                $commandline .= $unix_call;
637
638                        }
639                        if ((bool) ini_get('safe_mode')) {
640                                $ThisFileInfo['warning'][] = 'PHP running in Safe Mode - backtick operator not available, using slower non-system-call '.$algorithm.' algorithm';
641                                break;
642                        }
643                        return substr(`$commandline`, 0, $hash_length);
644                }
645
646                // try to create a temporary file in the system temp directory - invalid dirname should force to system temp dir
647                if (($data_filename = tempnam('*', 'getID3')) === false) {
648                        // can't find anywhere to create a temp file, just die
649                        return false;
650                }
651
652                // Init
653                $result = false;
654
655                // copy parts of file
656                if ($fp = @fopen($file, 'rb')) {
657
658                        if ($fp_data = @fopen($data_filename, 'wb')) {
659
660                                fseek($fp, $offset, SEEK_SET);
661                                $byteslefttowrite = $end - $offset;
662                                while (($byteslefttowrite > 0) && ($buffer = fread($fp, GETID3_FREAD_BUFFER_SIZE))) {
663                                        $byteswritten = fwrite($fp_data, $buffer, $byteslefttowrite);
664                                        $byteslefttowrite -= $byteswritten;
665                                }
666                                fclose($fp_data);
667                                $result = getid3_lib::$hash_function($data_filename);
668
669                        }
670                        fclose($fp);
671                }
672                unlink($data_filename);
673                return $result;
674        }
675
676
677        function iconv_fallback_int_utf8($charval) {
678                if ($charval < 128) {
679                        // 0bbbbbbb
680                        $newcharstring = chr($charval);
681                } elseif ($charval < 2048) {
682                        // 110bbbbb 10bbbbbb
683                        $newcharstring  = chr(($charval >> 6) | 0xC0);
684                        $newcharstring .= chr(($charval & 0x3F) | 0x80);
685                } elseif ($charval < 65536) {
686                        // 1110bbbb 10bbbbbb 10bbbbbb
687                        $newcharstring  = chr(($charval >> 12) | 0xE0);
688                        $newcharstring .= chr(($charval >>  6) | 0xC0);
689                        $newcharstring .= chr(($charval & 0x3F) | 0x80);
690                } else {
691                        // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
692                        $newcharstring  = chr(($charval >> 18) | 0xF0);
693                        $newcharstring .= chr(($charval >> 12) | 0xC0);
694                        $newcharstring .= chr(($charval >>  6) | 0xC0);
695                        $newcharstring .= chr(($charval & 0x3F) | 0x80);
696                }
697                return $newcharstring;
698        }
699
700        // ISO-8859-1 => UTF-8
701        function iconv_fallback_iso88591_utf8($string, $bom=false) {
702                if (function_exists('utf8_encode')) {
703                        return utf8_encode($string);
704                }
705                // utf8_encode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support)
706                $newcharstring = '';
707                if ($bom) {
708                        $newcharstring .= "\xEF\xBB\xBF";
709                }
710                for ($i = 0; $i < strlen($string); $i++) {
711                        $charval = ord($string{$i});
712                        $newcharstring .= getid3_lib::iconv_fallback_int_utf8($charval);
713                }
714                return $newcharstring;
715        }
716
717        // ISO-8859-1 => UTF-16BE
718        function iconv_fallback_iso88591_utf16be($string, $bom=false) {
719                $newcharstring = '';
720                if ($bom) {
721                        $newcharstring .= "\xFE\xFF";
722                }
723                for ($i = 0; $i < strlen($string); $i++) {
724                        $newcharstring .= "\x00".$string{$i};
725                }
726                return $newcharstring;
727        }
728
729        // ISO-8859-1 => UTF-16LE
730        function iconv_fallback_iso88591_utf16le($string, $bom=false) {
731                $newcharstring = '';
732                if ($bom) {
733                        $newcharstring .= "\xFF\xFE";
734                }
735                for ($i = 0; $i < strlen($string); $i++) {
736                        $newcharstring .= $string{$i}."\x00";
737                }
738                return $newcharstring;
739        }
740
741        // ISO-8859-1 => UTF-16LE (BOM)
742        function iconv_fallback_iso88591_utf16($string) {
743                return getid3_lib::iconv_fallback_iso88591_utf16le($string, true);
744        }
745
746        // UTF-8 => ISO-8859-1
747        function iconv_fallback_utf8_iso88591($string) {
748                if (function_exists('utf8_decode')) {
749                        return utf8_decode($string);
750                }
751                // utf8_decode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support)
752                $newcharstring = '';
753                $offset = 0;
754                $stringlength = strlen($string);
755                while ($offset < $stringlength) {
756                        if ((ord($string{$offset}) | 0x07) == 0xF7) {
757                                // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
758                                $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
759                                           ((ord($string{($offset + 1)}) & 0x3F) << 12) &
760                                           ((ord($string{($offset + 2)}) & 0x3F) <<  6) &
761                                            (ord($string{($offset + 3)}) & 0x3F);
762                                $offset += 4;
763                        } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
764                                // 1110bbbb 10bbbbbb 10bbbbbb
765                                $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
766                                           ((ord($string{($offset + 1)}) & 0x3F) <<  6) &
767                                            (ord($string{($offset + 2)}) & 0x3F);
768                                $offset += 3;
769                        } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
770                                // 110bbbbb 10bbbbbb
771                                $charval = ((ord($string{($offset + 0)}) & 0x1F) <<  6) &
772                                            (ord($string{($offset + 1)}) & 0x3F);
773                                $offset += 2;
774                        } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
775                                // 0bbbbbbb
776                                $charval = ord($string{$offset});
777                                $offset += 1;
778                        } else {
779                                // error? throw some kind of warning here?
780                                $charval = false;
781                                $offset += 1;
782                        }
783                        if ($charval !== false) {
784                                $newcharstring .= (($charval < 256) ? chr($charval) : '?');
785                        }
786                }
787                return $newcharstring;
788        }
789
790        // UTF-8 => UTF-16BE
791        function iconv_fallback_utf8_utf16be($string, $bom=false) {
792                $newcharstring = '';
793                if ($bom) {
794                        $newcharstring .= "\xFE\xFF";
795                }
796                $offset = 0;
797                $stringlength = strlen($string);
798                while ($offset < $stringlength) {
799                        if ((ord($string{$offset}) | 0x07) == 0xF7) {
800                                // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
801                                $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
802                                           ((ord($string{($offset + 1)}) & 0x3F) << 12) &
803                                           ((ord($string{($offset + 2)}) & 0x3F) <<  6) &
804                                            (ord($string{($offset + 3)}) & 0x3F);
805                                $offset += 4;
806                        } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
807                                // 1110bbbb 10bbbbbb 10bbbbbb
808                                $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
809                                           ((ord($string{($offset + 1)}) & 0x3F) <<  6) &
810                                            (ord($string{($offset + 2)}) & 0x3F);
811                                $offset += 3;
812                        } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
813                                // 110bbbbb 10bbbbbb
814                                $charval = ((ord($string{($offset + 0)}) & 0x1F) <<  6) &
815                                            (ord($string{($offset + 1)}) & 0x3F);
816                                $offset += 2;
817                        } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
818                                // 0bbbbbbb
819                                $charval = ord($string{$offset});
820                                $offset += 1;
821                        } else {
822                                // error? throw some kind of warning here?
823                                $charval = false;
824                                $offset += 1;
825                        }
826                        if ($charval !== false) {
827                                $newcharstring .= (($charval < 65536) ? getid3_lib::BigEndian2String($charval, 2) : "\x00".'?');
828                        }
829                }
830                return $newcharstring;
831        }
832
833        // UTF-8 => UTF-16LE
834        function iconv_fallback_utf8_utf16le($string, $bom=false) {
835                $newcharstring = '';
836                if ($bom) {
837                        $newcharstring .= "\xFF\xFE";
838                }
839                $offset = 0;
840                $stringlength = strlen($string);
841                while ($offset < $stringlength) {
842                        if ((ord($string{$offset}) | 0x07) == 0xF7) {
843                                // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
844                                $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
845                                           ((ord($string{($offset + 1)}) & 0x3F) << 12) &
846                                           ((ord($string{($offset + 2)}) & 0x3F) <<  6) &
847                                            (ord($string{($offset + 3)}) & 0x3F);
848                                $offset += 4;
849                        } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
850                                // 1110bbbb 10bbbbbb 10bbbbbb
851                                $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
852                                           ((ord($string{($offset + 1)}) & 0x3F) <<  6) &
853                                            (ord($string{($offset + 2)}) & 0x3F);
854                                $offset += 3;
855                        } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
856                                // 110bbbbb 10bbbbbb
857                                $charval = ((ord($string{($offset + 0)}) & 0x1F) <<  6) &
858                                            (ord($string{($offset + 1)}) & 0x3F);
859                                $offset += 2;
860                        } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
861                                // 0bbbbbbb
862                                $charval = ord($string{$offset});
863                                $offset += 1;
864                        } else {
865                                // error? maybe throw some warning here?
866                                $charval = false;
867                                $offset += 1;
868                        }
869                        if ($charval !== false) {
870                                $newcharstring .= (($charval < 65536) ? getid3_lib::LittleEndian2String($charval, 2) : '?'."\x00");
871                        }
872                }
873                return $newcharstring;
874        }
875
876        // UTF-8 => UTF-16LE (BOM)
877        function iconv_fallback_utf8_utf16($string) {
878                return getid3_lib::iconv_fallback_utf8_utf16le($string, true);
879        }
880
881        // UTF-16BE => UTF-8
882        function iconv_fallback_utf16be_utf8($string) {
883                if (substr($string, 0, 2) == "\xFE\xFF") {
884                        // strip BOM
885                        $string = substr($string, 2);
886                }
887                $newcharstring = '';
888                for ($i = 0; $i < strlen($string); $i += 2) {
889                        $charval = getid3_lib::BigEndian2Int(substr($string, $i, 2));
890                        $newcharstring .= getid3_lib::iconv_fallback_int_utf8($charval);
891                }
892                return $newcharstring;
893        }
894
895        // UTF-16LE => UTF-8
896        function iconv_fallback_utf16le_utf8($string) {
897                if (substr($string, 0, 2) == "\xFF\xFE") {
898                        // strip BOM
899                        $string = substr($string, 2);
900                }
901                $newcharstring = '';
902                for ($i = 0; $i < strlen($string); $i += 2) {
903                        $charval = getid3_lib::LittleEndian2Int(substr($string, $i, 2));
904                        $newcharstring .= getid3_lib::iconv_fallback_int_utf8($charval);
905                }
906                return $newcharstring;
907        }
908
909        // UTF-16BE => ISO-8859-1
910        function iconv_fallback_utf16be_iso88591($string) {
911                if (substr($string, 0, 2) == "\xFE\xFF") {
912                        // strip BOM
913                        $string = substr($string, 2);
914                }
915                $newcharstring = '';
916                for ($i = 0; $i < strlen($string); $i += 2) {
917                        $charval = getid3_lib::BigEndian2Int(substr($string, $i, 2));
918                        $newcharstring .= (($charval < 256) ? chr($charval) : '?');
919                }
920                return $newcharstring;
921        }
922
923        // UTF-16LE => ISO-8859-1
924        function iconv_fallback_utf16le_iso88591($string) {
925                if (substr($string, 0, 2) == "\xFF\xFE") {
926                        // strip BOM
927                        $string = substr($string, 2);
928                }
929                $newcharstring = '';
930                for ($i = 0; $i < strlen($string); $i += 2) {
931                        $charval = getid3_lib::LittleEndian2Int(substr($string, $i, 2));
932                        $newcharstring .= (($charval < 256) ? chr($charval) : '?');
933                }
934                return $newcharstring;
935        }
936
937        // UTF-16 (BOM) => ISO-8859-1
938        function iconv_fallback_utf16_iso88591($string) {
939                $bom = substr($string, 0, 2);
940                if ($bom == "\xFE\xFF") {
941                        return getid3_lib::iconv_fallback_utf16be_iso88591(substr($string, 2));
942                } elseif ($bom == "\xFF\xFE") {
943                        return getid3_lib::iconv_fallback_utf16le_iso88591(substr($string, 2));
944                }
945                return $string;
946        }
947
948        // UTF-16 (BOM) => UTF-8
949        function iconv_fallback_utf16_utf8($string) {
950                $bom = substr($string, 0, 2);
951                if ($bom == "\xFE\xFF") {
952                        return getid3_lib::iconv_fallback_utf16be_utf8(substr($string, 2));
953                } elseif ($bom == "\xFF\xFE") {
954                        return getid3_lib::iconv_fallback_utf16le_utf8(substr($string, 2));
955                }
956                return $string;
957        }
958
959        function iconv_fallback($in_charset, $out_charset, $string) {
960
961                if ($in_charset == $out_charset) {
962                        return $string;
963                }
964
965                // iconv() availble
966                if (function_exists('iconv')) {
967
968                    if ($converted_string = @iconv($in_charset, $out_charset.'//TRANSLIT', $string)) {
969                        switch ($out_charset) {
970                                case 'ISO-8859-1':
971                                        $converted_string = rtrim($converted_string, "\x00");
972                                        break;
973                        }
974                        return $converted_string;
975                }
976
977                // iconv() may sometimes fail with "illegal character in input string" error message
978                // and return an empty string, but returning the unconverted string is more useful
979                return $string;
980        }
981
982
983        // iconv() not available
984                static $ConversionFunctionList = array();
985                if (empty($ConversionFunctionList)) {
986                        $ConversionFunctionList['ISO-8859-1']['UTF-8']    = 'iconv_fallback_iso88591_utf8';
987                        $ConversionFunctionList['ISO-8859-1']['UTF-16']   = 'iconv_fallback_iso88591_utf16';
988                        $ConversionFunctionList['ISO-8859-1']['UTF-16BE'] = 'iconv_fallback_iso88591_utf16be';
989                        $ConversionFunctionList['ISO-8859-1']['UTF-16LE'] = 'iconv_fallback_iso88591_utf16le';
990                        $ConversionFunctionList['UTF-8']['ISO-8859-1']    = 'iconv_fallback_utf8_iso88591';
991                        $ConversionFunctionList['UTF-8']['UTF-16']        = 'iconv_fallback_utf8_utf16';
992                        $ConversionFunctionList['UTF-8']['UTF-16BE']      = 'iconv_fallback_utf8_utf16be';
993                        $ConversionFunctionList['UTF-8']['UTF-16LE']      = 'iconv_fallback_utf8_utf16le';
994                        $ConversionFunctionList['UTF-16']['ISO-8859-1']   = 'iconv_fallback_utf16_iso88591';
995                        $ConversionFunctionList['UTF-16']['UTF-8']        = 'iconv_fallback_utf16_utf8';
996                        $ConversionFunctionList['UTF-16LE']['ISO-8859-1'] = 'iconv_fallback_utf16le_iso88591';
997                        $ConversionFunctionList['UTF-16LE']['UTF-8']      = 'iconv_fallback_utf16le_utf8';
998                        $ConversionFunctionList['UTF-16BE']['ISO-8859-1'] = 'iconv_fallback_utf16be_iso88591';
999                        $ConversionFunctionList['UTF-16BE']['UTF-8']      = 'iconv_fallback_utf16be_utf8';
1000                }
1001                if (isset($ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)])) {
1002                        $ConversionFunction = $ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)];
1003                        return getid3_lib::$ConversionFunction($string);
1004                }
1005                die('PHP does not have iconv() support - cannot convert from '.$in_charset.' to '.$out_charset);
1006        }
1007
1008
1009        function MultiByteCharString2HTML($string, $charset='ISO-8859-1') {
1010                $HTMLstring = '';
1011
1012                switch ($charset) {
1013                        case 'ISO-8859-1':
1014                        case 'ISO8859-1':
1015                        case 'ISO-8859-15':
1016                        case 'ISO8859-15':
1017                        case 'cp866':
1018                        case 'ibm866':
1019                        case '866':
1020                        case 'cp1251':
1021                        case 'Windows-1251':
1022                        case 'win-1251':
1023                        case '1251':
1024                        case 'cp1252':
1025                        case 'Windows-1252':
1026                        case '1252':
1027                        case 'KOI8-R':
1028                        case 'koi8-ru':
1029                        case 'koi8r':
1030                        case 'BIG5':
1031                        case '950':
1032                        case 'GB2312':
1033                        case '936':
1034                        case 'BIG5-HKSCS':
1035                        case 'Shift_JIS':
1036                        case 'SJIS':
1037                        case '932':
1038                        case 'EUC-JP':
1039                        case 'EUCJP':
1040                                $HTMLstring = htmlentities($string, ENT_COMPAT, $charset);
1041                                break;
1042
1043                        case 'UTF-8':
1044                                $strlen = strlen($string);
1045                                for ($i = 0; $i < $strlen; $i++) {
1046                                        $char_ord_val = ord($string{$i});
1047                                        $charval = 0;
1048                                        if ($char_ord_val < 0x80) {
1049                                                $charval = $char_ord_val;
1050                                        } elseif ((($char_ord_val & 0xF0) >> 4) == 0x0F  &&  $i+3 < $strlen) {
1051                                                $charval  = (($char_ord_val & 0x07) << 18);
1052                                                $charval += ((ord($string{++$i}) & 0x3F) << 12);
1053                                                $charval += ((ord($string{++$i}) & 0x3F) << 6);
1054                                                $charval +=  (ord($string{++$i}) & 0x3F);
1055                                        } elseif ((($char_ord_val & 0xE0) >> 5) == 0x07  &&  $i+2 < $strlen) {
1056                                                $charval  = (($char_ord_val & 0x0F) << 12);
1057                                                $charval += ((ord($string{++$i}) & 0x3F) << 6);
1058                                                $charval +=  (ord($string{++$i}) & 0x3F);
1059                                        } elseif ((($char_ord_val & 0xC0) >> 6) == 0x03  &&  $i+1 < $strlen) {
1060                                                $charval  = (($char_ord_val & 0x1F) << 6);
1061                                                $charval += (ord($string{++$i}) & 0x3F);
1062                                        }
1063                                        if (($charval >= 32) && ($charval <= 127)) {
1064                                                $HTMLstring .= htmlentities(chr($charval));
1065                                        } else {
1066                                                $HTMLstring .= '&#'.$charval.';';
1067                                        }
1068                                }
1069                                break;
1070
1071                        case 'UTF-16LE':
1072                                for ($i = 0; $i < strlen($string); $i += 2) {
1073                                        $charval = getid3_lib::LittleEndian2Int(substr($string, $i, 2));
1074                                        if (($charval >= 32) && ($charval <= 127)) {
1075                                                $HTMLstring .= chr($charval);
1076                                        } else {
1077                                                $HTMLstring .= '&#'.$charval.';';
1078                                        }
1079                                }
1080                                break;
1081
1082                        case 'UTF-16BE':
1083                                for ($i = 0; $i < strlen($string); $i += 2) {
1084                                        $charval = getid3_lib::BigEndian2Int(substr($string, $i, 2));
1085                                        if (($charval >= 32) && ($charval <= 127)) {
1086                                                $HTMLstring .= chr($charval);
1087                                        } else {
1088                                                $HTMLstring .= '&#'.$charval.';';
1089                                        }
1090                                }
1091                                break;
1092
1093                        default:
1094                                $HTMLstring = 'ERROR: Character set "'.$charset.'" not supported in MultiByteCharString2HTML()';
1095                                break;
1096                }
1097                return $HTMLstring;
1098        }
1099
1100
1101
1102        function RGADnameLookup($namecode) {
1103                static $RGADname = array();
1104                if (empty($RGADname)) {
1105                        $RGADname[0] = 'not set';
1106                        $RGADname[1] = 'Track Gain Adjustment';
1107                        $RGADname[2] = 'Album Gain Adjustment';
1108                }
1109
1110                return (isset($RGADname[$namecode]) ? $RGADname[$namecode] : '');
1111        }
1112
1113
1114        function RGADoriginatorLookup($originatorcode) {
1115                static $RGADoriginator = array();
1116                if (empty($RGADoriginator)) {
1117                        $RGADoriginator[0] = 'unspecified';
1118                        $RGADoriginator[1] = 'pre-set by artist/producer/mastering engineer';
1119                        $RGADoriginator[2] = 'set by user';
1120                        $RGADoriginator[3] = 'determined automatically';
1121                }
1122
1123                return (isset($RGADoriginator[$originatorcode]) ? $RGADoriginator[$originatorcode] : '');
1124        }
1125
1126
1127        function RGADadjustmentLookup($rawadjustment, $signbit) {
1128                $adjustment = $rawadjustment / 10;
1129                if ($signbit == 1) {
1130                        $adjustment *= -1;
1131                }
1132                return (float) $adjustment;
1133        }
1134
1135
1136        function RGADgainString($namecode, $originatorcode, $replaygain) {
1137                if ($replaygain < 0) {
1138                        $signbit = '1';
1139                } else {
1140                        $signbit = '0';
1141                }
1142                $storedreplaygain = intval(round($replaygain * 10));
1143                $gainstring  = str_pad(decbin($namecode), 3, '0', STR_PAD_LEFT);
1144                $gainstring .= str_pad(decbin($originatorcode), 3, '0', STR_PAD_LEFT);
1145                $gainstring .= $signbit;
1146                $gainstring .= str_pad(decbin($storedreplaygain), 9, '0', STR_PAD_LEFT);
1147
1148                return $gainstring;
1149        }
1150
1151        function RGADamplitude2dB($amplitude) {
1152                return 20 * log10($amplitude);
1153        }
1154
1155
1156        function GetDataImageSize($imgData, &$imageinfo) {
1157                $GetDataImageSize = false;
1158                if ($tempfilename = tempnam('*', 'getID3')) {
1159                        if ($tmp = @fopen($tempfilename, 'wb')) {
1160                                fwrite($tmp, $imgData);
1161                                fclose($tmp);
1162                                $GetDataImageSize = @GetImageSize($tempfilename, $imageinfo);
1163                        }
1164                        unlink($tempfilename);
1165                }
1166                return $GetDataImageSize;
1167        }
1168
1169        function ImageTypesLookup($imagetypeid) {
1170                static $ImageTypesLookup = array();
1171                if (empty($ImageTypesLookup)) {
1172                        $ImageTypesLookup[1]  = 'gif';
1173                        $ImageTypesLookup[2]  = 'jpeg';
1174                        $ImageTypesLookup[3]  = 'png';
1175                        $ImageTypesLookup[4]  = 'swf';
1176                        $ImageTypesLookup[5]  = 'psd';
1177                        $ImageTypesLookup[6]  = 'bmp';
1178                        $ImageTypesLookup[7]  = 'tiff (little-endian)';
1179                        $ImageTypesLookup[8]  = 'tiff (big-endian)';
1180                        $ImageTypesLookup[9]  = 'jpc';
1181                        $ImageTypesLookup[10] = 'jp2';
1182                        $ImageTypesLookup[11] = 'jpx';
1183                        $ImageTypesLookup[12] = 'jb2';
1184                        $ImageTypesLookup[13] = 'swc';
1185                        $ImageTypesLookup[14] = 'iff';
1186                }
1187                return (isset($ImageTypesLookup[$imagetypeid]) ? $ImageTypesLookup[$imagetypeid] : '');
1188        }
1189
1190        function CopyTagsToComments(&$ThisFileInfo) {
1191
1192                // Copy all entries from ['tags'] into common ['comments']
1193                if (!empty($ThisFileInfo['tags'])) {
1194                        foreach ($ThisFileInfo['tags'] as $tagtype => $tagarray) {
1195                                foreach ($tagarray as $tagname => $tagdata) {
1196                                        foreach ($tagdata as $key => $value) {
1197                                                if (!empty($value)) {
1198                                                        if (empty($ThisFileInfo['comments'][$tagname])) {
1199
1200                                                                // fall through and append value
1201
1202                                                        } elseif ($tagtype == 'id3v1') {
1203
1204                                                                $newvaluelength = strlen(trim($value));
1205                                                                foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) {
1206                                                                        $oldvaluelength = strlen(trim($existingvalue));
1207                                                                        if (($newvaluelength <= $oldvaluelength) && (substr($existingvalue, 0, $newvaluelength) == trim($value))) {
1208                                                                                // new value is identical but shorter-than (or equal-length to) one already in comments - skip
1209                                                                                break 2;
1210                                                                        }
1211                                                                }
1212
1213                                                        } else {
1214
1215                                                                $newvaluelength = strlen(trim($value));
1216                                                                foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) {
1217                                                                        $oldvaluelength = strlen(trim($existingvalue));
1218                                                                        if (($newvaluelength > $oldvaluelength) && (substr(trim($value), 0, strlen($existingvalue)) == $existingvalue)) {
1219                                                                                $ThisFileInfo['comments'][$tagname][$existingkey] = trim($value);
1220                                                                                break 2;
1221                                                                        }
1222                                                                }
1223
1224                                                        }
1225                                                        if (empty($ThisFileInfo['comments'][$tagname]) || !in_array(trim($value), $ThisFileInfo['comments'][$tagname])) {
1226                                                                $ThisFileInfo['comments'][$tagname][] = trim($value);
1227                                                        }
1228                                                }
1229                                        }
1230                                }
1231                        }
1232
1233                        // Copy to ['comments_html']
1234                foreach ($ThisFileInfo['comments'] as $field => $values) {
1235                    foreach ($values as $index => $value) {
1236                        $ThisFileInfo['comments_html'][$field][$index] = str_replace('&#0;', '', getid3_lib::MultiByteCharString2HTML($value, $ThisFileInfo['encoding']));
1237                    }
1238            }
1239                }
1240        }
1241
1242
1243        function EmbeddedLookup($key, $begin, $end, $file, $name) {
1244
1245                // Cached
1246                static $cache;
1247                if (isset($cache[$file][$name])) {
1248                        return @$cache[$file][$name][$key];
1249                }
1250
1251                // Init
1252                $keylength  = strlen($key);
1253                $line_count = $end - $begin - 7;
1254
1255                // Open php file
1256                $fp = fopen($file, 'r');
1257
1258                // Discard $begin lines
1259                for ($i = 0; $i < ($begin + 3); $i++) {
1260                        fgets($fp, 1024);
1261                }
1262
1263                // Loop thru line
1264                while (0 < $line_count--) {
1265
1266                        // Read line
1267                        $line = ltrim(fgets($fp, 1024), "\t ");
1268
1269                        // METHOD A: only cache the matching key - less memory but slower on next lookup of not-previously-looked-up key
1270                        //$keycheck = substr($line, 0, $keylength);
1271                        //if ($key == $keycheck)  {
1272                        //      $cache[$file][$name][$keycheck] = substr($line, $keylength + 1);
1273                        //      break;
1274                        //}
1275
1276                        // METHOD B: cache all keys in this lookup - more memory but faster on next lookup of not-previously-looked-up key
1277                        //$cache[$file][$name][substr($line, 0, $keylength)] = trim(substr($line, $keylength + 1));
1278                        @list($ThisKey, $ThisValue) = explode("\t", $line, 2);
1279                        $cache[$file][$name][$ThisKey] = trim($ThisValue);
1280                }
1281
1282                // Close and return
1283                fclose($fp);
1284                return @$cache[$file][$name][$key];
1285        }
1286
1287        function IncludeDependency($filename, $sourcefile, $DieOnFailure=false) {
1288                global $GETID3_ERRORARRAY;
1289
1290                if (file_exists($filename)) {
1291                        if (@include_once($filename)) {
1292                                return true;
1293                        } else {
1294                                $diemessage = basename($sourcefile).' depends on '.$filename.', which has errors';
1295                        }
1296                } else {
1297                        $diemessage = basename($sourcefile).' depends on '.$filename.', which is missing';
1298                }
1299                if ($DieOnFailure) {
1300                        die($diemessage);
1301                } else {
1302                        $GETID3_ERRORARRAY[] = $diemessage;
1303                }
1304                return false;
1305        }
1306
1307}
1308
1309?>
Note: See TracBrowser for help on using the repository browser.