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