Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like getid3_lib often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use getid3_lib, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class getid3_lib |
||
16 | { |
||
17 | |||
18 | public static function PrintHexBytes($string, $hex=true, $spaces=true, $htmlencoding='UTF-8') { |
||
19 | $returnstring = ''; |
||
20 | for ($i = 0; $i < strlen($string); $i++) { |
||
21 | if ($hex) { |
||
22 | $returnstring .= str_pad(dechex(ord($string{$i})), 2, '0', STR_PAD_LEFT); |
||
23 | } else { |
||
24 | $returnstring .= ' '.(preg_match("#[\x20-\x7E]#", $string{$i}) ? $string{$i} : '¤'); |
||
25 | } |
||
26 | if ($spaces) { |
||
27 | $returnstring .= ' '; |
||
28 | } |
||
29 | } |
||
30 | if (!empty($htmlencoding)) { |
||
31 | if ($htmlencoding === true) { |
||
32 | $htmlencoding = 'UTF-8'; // prior to getID3 v1.9.0 the function's 4th parameter was boolean |
||
33 | } |
||
34 | $returnstring = htmlentities($returnstring, ENT_QUOTES, $htmlencoding); |
||
35 | } |
||
36 | return $returnstring; |
||
37 | } |
||
38 | |||
39 | public static function trunc($floatnumber) { |
||
40 | // truncates a floating-point number at the decimal point |
||
41 | // returns int (if possible, otherwise float) |
||
42 | if ($floatnumber >= 1) { |
||
43 | $truncatednumber = floor($floatnumber); |
||
44 | } elseif ($floatnumber <= -1) { |
||
45 | $truncatednumber = ceil($floatnumber); |
||
46 | } else { |
||
47 | $truncatednumber = 0; |
||
48 | } |
||
49 | if (self::intValueSupported($truncatednumber)) { |
||
50 | $truncatednumber = (int) $truncatednumber; |
||
51 | } |
||
52 | return $truncatednumber; |
||
53 | } |
||
54 | |||
55 | |||
56 | public static function safe_inc(&$variable, $increment=1) { |
||
57 | if (isset($variable)) { |
||
58 | $variable += $increment; |
||
59 | } else { |
||
60 | $variable = $increment; |
||
61 | } |
||
62 | return true; |
||
63 | } |
||
64 | |||
65 | public static function CastAsInt($floatnum) { |
||
66 | // convert to float if not already |
||
67 | $floatnum = (float) $floatnum; |
||
68 | |||
69 | // convert a float to type int, only if possible |
||
70 | if (self::trunc($floatnum) == $floatnum) { |
||
71 | // it's not floating point |
||
72 | if (self::intValueSupported($floatnum)) { |
||
73 | // it's within int range |
||
74 | $floatnum = (int) $floatnum; |
||
75 | } |
||
76 | } |
||
77 | return $floatnum; |
||
78 | } |
||
79 | |||
80 | public static function intValueSupported($num) { |
||
81 | // check if integers are 64-bit |
||
82 | static $hasINT64 = null; |
||
83 | if ($hasINT64 === null) { // 10x faster than is_null() |
||
84 | $hasINT64 = is_int(pow(2, 31)); // 32-bit int are limited to (2^31)-1 |
||
85 | if (!$hasINT64 && !defined('PHP_INT_MIN')) { |
||
86 | define('PHP_INT_MIN', ~PHP_INT_MAX); |
||
87 | } |
||
88 | } |
||
89 | // if integers are 64-bit - no other check required |
||
90 | if ($hasINT64 || (($num <= PHP_INT_MAX) && ($num >= PHP_INT_MIN))) { |
||
91 | return true; |
||
92 | } |
||
93 | return false; |
||
94 | } |
||
95 | |||
96 | public static function DecimalizeFraction($fraction) { |
||
97 | list($numerator, $denominator) = explode('/', $fraction); |
||
98 | return $numerator / ($denominator ? $denominator : 1); |
||
99 | } |
||
100 | |||
101 | |||
102 | public static function DecimalBinary2Float($binarynumerator) { |
||
103 | $numerator = self::Bin2Dec($binarynumerator); |
||
104 | $denominator = self::Bin2Dec('1'.str_repeat('0', strlen($binarynumerator))); |
||
105 | return ($numerator / $denominator); |
||
106 | } |
||
107 | |||
108 | |||
109 | public static function NormalizeBinaryPoint($binarypointnumber, $maxbits=52) { |
||
110 | // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html |
||
111 | if (strpos($binarypointnumber, '.') === false) { |
||
112 | $binarypointnumber = '0.'.$binarypointnumber; |
||
113 | } elseif ($binarypointnumber{0} == '.') { |
||
114 | $binarypointnumber = '0'.$binarypointnumber; |
||
115 | } |
||
116 | $exponent = 0; |
||
117 | while (($binarypointnumber{0} != '1') || (substr($binarypointnumber, 1, 1) != '.')) { |
||
118 | if (substr($binarypointnumber, 1, 1) == '.') { |
||
119 | $exponent--; |
||
120 | $binarypointnumber = substr($binarypointnumber, 2, 1).'.'.substr($binarypointnumber, 3); |
||
121 | } else { |
||
122 | $pointpos = strpos($binarypointnumber, '.'); |
||
123 | $exponent += ($pointpos - 1); |
||
124 | $binarypointnumber = str_replace('.', '', $binarypointnumber); |
||
125 | $binarypointnumber = $binarypointnumber{0}.'.'.substr($binarypointnumber, 1); |
||
126 | } |
||
127 | } |
||
128 | $binarypointnumber = str_pad(substr($binarypointnumber, 0, $maxbits + 2), $maxbits + 2, '0', STR_PAD_RIGHT); |
||
129 | return array('normalized'=>$binarypointnumber, 'exponent'=>(int) $exponent); |
||
130 | } |
||
131 | |||
132 | |||
133 | public static function Float2BinaryDecimal($floatvalue) { |
||
134 | // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html |
||
135 | $maxbits = 128; // to how many bits of precision should the calculations be taken? |
||
136 | $intpart = self::trunc($floatvalue); |
||
137 | $floatpart = abs($floatvalue - $intpart); |
||
138 | $pointbitstring = ''; |
||
139 | while (($floatpart != 0) && (strlen($pointbitstring) < $maxbits)) { |
||
140 | $floatpart *= 2; |
||
141 | $pointbitstring .= (string) self::trunc($floatpart); |
||
142 | $floatpart -= self::trunc($floatpart); |
||
143 | } |
||
144 | $binarypointnumber = decbin($intpart).'.'.$pointbitstring; |
||
145 | return $binarypointnumber; |
||
146 | } |
||
147 | |||
148 | |||
149 | public static function Float2String($floatvalue, $bits) { |
||
150 | // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee-expl.html |
||
151 | switch ($bits) { |
||
152 | case 32: |
||
153 | $exponentbits = 8; |
||
154 | $fractionbits = 23; |
||
155 | break; |
||
156 | |||
157 | case 64: |
||
158 | $exponentbits = 11; |
||
159 | $fractionbits = 52; |
||
160 | break; |
||
161 | |||
162 | default: |
||
163 | return false; |
||
164 | break; |
||
165 | } |
||
166 | if ($floatvalue >= 0) { |
||
167 | $signbit = '0'; |
||
168 | } else { |
||
169 | $signbit = '1'; |
||
170 | } |
||
171 | $normalizedbinary = self::NormalizeBinaryPoint(self::Float2BinaryDecimal($floatvalue), $fractionbits); |
||
172 | $biasedexponent = pow(2, $exponentbits - 1) - 1 + $normalizedbinary['exponent']; // (127 or 1023) +/- exponent |
||
173 | $exponentbitstring = str_pad(decbin($biasedexponent), $exponentbits, '0', STR_PAD_LEFT); |
||
174 | $fractionbitstring = str_pad(substr($normalizedbinary['normalized'], 2), $fractionbits, '0', STR_PAD_RIGHT); |
||
175 | |||
176 | return self::BigEndian2String(self::Bin2Dec($signbit.$exponentbitstring.$fractionbitstring), $bits % 8, false); |
||
177 | } |
||
178 | |||
179 | |||
180 | public static function LittleEndian2Float($byteword) { |
||
181 | return self::BigEndian2Float(strrev($byteword)); |
||
182 | } |
||
183 | |||
184 | |||
185 | public static function BigEndian2Float($byteword) { |
||
186 | // ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic |
||
187 | // http://www.psc.edu/general/software/packages/ieee/ieee.html |
||
188 | // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html |
||
189 | |||
190 | $bitword = self::BigEndian2Bin($byteword); |
||
191 | if (!$bitword) { |
||
192 | return 0; |
||
193 | } |
||
194 | $signbit = $bitword{0}; |
||
195 | |||
196 | switch (strlen($byteword) * 8) { |
||
197 | case 32: |
||
198 | $exponentbits = 8; |
||
199 | $fractionbits = 23; |
||
200 | break; |
||
201 | |||
202 | case 64: |
||
203 | $exponentbits = 11; |
||
204 | $fractionbits = 52; |
||
205 | break; |
||
206 | |||
207 | case 80: |
||
208 | // 80-bit Apple SANE format |
||
209 | // http://www.mactech.com/articles/mactech/Vol.06/06.01/SANENormalized/ |
||
210 | $exponentstring = substr($bitword, 1, 15); |
||
211 | $isnormalized = intval($bitword{16}); |
||
212 | $fractionstring = substr($bitword, 17, 63); |
||
213 | $exponent = pow(2, self::Bin2Dec($exponentstring) - 16383); |
||
214 | $fraction = $isnormalized + self::DecimalBinary2Float($fractionstring); |
||
215 | $floatvalue = $exponent * $fraction; |
||
216 | if ($signbit == '1') { |
||
217 | $floatvalue *= -1; |
||
218 | } |
||
219 | return $floatvalue; |
||
220 | break; |
||
221 | |||
222 | default: |
||
223 | return false; |
||
224 | break; |
||
225 | } |
||
226 | $exponentstring = substr($bitword, 1, $exponentbits); |
||
227 | $fractionstring = substr($bitword, $exponentbits + 1, $fractionbits); |
||
228 | $exponent = self::Bin2Dec($exponentstring); |
||
229 | $fraction = self::Bin2Dec($fractionstring); |
||
230 | |||
231 | if (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction != 0)) { |
||
232 | // Not a Number |
||
233 | $floatvalue = false; |
||
234 | } elseif (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction == 0)) { |
||
235 | if ($signbit == '1') { |
||
236 | $floatvalue = '-infinity'; |
||
237 | } else { |
||
238 | $floatvalue = '+infinity'; |
||
239 | } |
||
240 | } elseif (($exponent == 0) && ($fraction == 0)) { |
||
241 | if ($signbit == '1') { |
||
242 | $floatvalue = -0; |
||
243 | } else { |
||
244 | $floatvalue = 0; |
||
245 | } |
||
246 | $floatvalue = ($signbit ? 0 : -0); |
||
247 | } elseif (($exponent == 0) && ($fraction != 0)) { |
||
248 | // These are 'unnormalized' values |
||
249 | $floatvalue = pow(2, (-1 * (pow(2, $exponentbits - 1) - 2))) * self::DecimalBinary2Float($fractionstring); |
||
250 | if ($signbit == '1') { |
||
251 | $floatvalue *= -1; |
||
252 | } |
||
253 | } elseif ($exponent != 0) { |
||
254 | $floatvalue = pow(2, ($exponent - (pow(2, $exponentbits - 1) - 1))) * (1 + self::DecimalBinary2Float($fractionstring)); |
||
255 | if ($signbit == '1') { |
||
256 | $floatvalue *= -1; |
||
257 | } |
||
258 | } |
||
259 | return (float) $floatvalue; |
||
260 | } |
||
261 | |||
262 | |||
263 | public static function BigEndian2Int($byteword, $synchsafe=false, $signed=false) { |
||
264 | $intvalue = 0; |
||
265 | $bytewordlen = strlen($byteword); |
||
266 | if ($bytewordlen == 0) { |
||
267 | return false; |
||
268 | } |
||
269 | for ($i = 0; $i < $bytewordlen; $i++) { |
||
270 | if ($synchsafe) { // disregard MSB, effectively 7-bit bytes |
||
271 | //$intvalue = $intvalue | (ord($byteword{$i}) & 0x7F) << (($bytewordlen - 1 - $i) * 7); // faster, but runs into problems past 2^31 on 32-bit systems |
||
272 | $intvalue += (ord($byteword{$i}) & 0x7F) * pow(2, ($bytewordlen - 1 - $i) * 7); |
||
273 | } else { |
||
274 | $intvalue += ord($byteword{$i}) * pow(256, ($bytewordlen - 1 - $i)); |
||
275 | } |
||
276 | } |
||
277 | if ($signed && !$synchsafe) { |
||
278 | // synchsafe ints are not allowed to be signed |
||
279 | if ($bytewordlen <= PHP_INT_SIZE) { |
||
280 | $signMaskBit = 0x80 << (8 * ($bytewordlen - 1)); |
||
281 | if ($intvalue & $signMaskBit) { |
||
282 | $intvalue = 0 - ($intvalue & ($signMaskBit - 1)); |
||
283 | } |
||
284 | } else { |
||
285 | throw new Exception('ERROR: Cannot have signed integers larger than '.(8 * PHP_INT_SIZE).'-bits ('.strlen($byteword).') in self::BigEndian2Int()'); |
||
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') && function_exists('libxml_disable_entity_loader')) { |
||
523 | // http://websec.io/2012/08/27/Preventing-XEE-in-PHP.html |
||
524 | // https://core.trac.wordpress.org/changeset/29378 |
||
525 | $loader = libxml_disable_entity_loader(true); |
||
526 | $XMLobject = simplexml_load_string($XMLstring, 'SimpleXMLElement', LIBXML_NOENT); |
||
527 | $return = self::SimpleXMLelement2array($XMLobject); |
||
528 | libxml_disable_entity_loader($loader); |
||
529 | return $return; |
||
530 | } |
||
531 | return false; |
||
532 | } |
||
533 | |||
534 | public static function SimpleXMLelement2array($XMLobject) { |
||
535 | if (!is_object($XMLobject) && !is_array($XMLobject)) { |
||
536 | return $XMLobject; |
||
537 | } |
||
538 | $XMLarray = (is_object($XMLobject) ? get_object_vars($XMLobject) : $XMLobject); |
||
539 | foreach ($XMLarray as $key => $value) { |
||
540 | $XMLarray[$key] = self::SimpleXMLelement2array($value); |
||
541 | } |
||
542 | return $XMLarray; |
||
543 | } |
||
544 | |||
545 | |||
546 | // Allan Hansen <ahØartemis*dk> |
||
547 | // self::md5_data() - returns md5sum for a file from startuing position to absolute end position |
||
548 | public static function hash_data($file, $offset, $end, $algorithm) { |
||
549 | static $tempdir = ''; |
||
550 | if (!self::intValueSupported($end)) { |
||
551 | return false; |
||
552 | } |
||
553 | switch ($algorithm) { |
||
554 | case 'md5': |
||
555 | $hash_function = 'md5_file'; |
||
556 | $unix_call = 'md5sum'; |
||
557 | $windows_call = 'md5sum.exe'; |
||
558 | $hash_length = 32; |
||
559 | break; |
||
560 | |||
561 | case 'sha1': |
||
562 | $hash_function = 'sha1_file'; |
||
563 | $unix_call = 'sha1sum'; |
||
564 | $windows_call = 'sha1sum.exe'; |
||
565 | $hash_length = 40; |
||
566 | break; |
||
567 | |||
568 | default: |
||
569 | throw new Exception('Invalid algorithm ('.$algorithm.') in self::hash_data()'); |
||
570 | break; |
||
571 | } |
||
572 | $size = $end - $offset; |
||
573 | while (true) { |
||
574 | if (GETID3_OS_ISWINDOWS) { |
||
575 | |||
576 | // It seems that sha1sum.exe for Windows only works on physical files, does not accept piped data |
||
577 | // Fall back to create-temp-file method: |
||
578 | if ($algorithm == 'sha1') { |
||
579 | break; |
||
580 | } |
||
581 | |||
582 | $RequiredFiles = array('cygwin1.dll', 'head.exe', 'tail.exe', $windows_call); |
||
583 | foreach ($RequiredFiles as $required_file) { |
||
584 | if (!is_readable(GETID3_HELPERAPPSDIR.$required_file)) { |
||
585 | // helper apps not available - fall back to old method |
||
586 | break 2; |
||
587 | } |
||
588 | } |
||
589 | $commandline = GETID3_HELPERAPPSDIR.'head.exe -c '.$end.' '.escapeshellarg(str_replace('/', DIRECTORY_SEPARATOR, $file)).' | '; |
||
590 | $commandline .= GETID3_HELPERAPPSDIR.'tail.exe -c '.$size.' | '; |
||
591 | $commandline .= GETID3_HELPERAPPSDIR.$windows_call; |
||
592 | |||
593 | } else { |
||
594 | |||
595 | $commandline = 'head -c'.$end.' '.escapeshellarg($file).' | '; |
||
596 | $commandline .= 'tail -c'.$size.' | '; |
||
597 | $commandline .= $unix_call; |
||
598 | |||
599 | } |
||
600 | if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) { |
||
601 | //throw new Exception('PHP running in Safe Mode - backtick operator not available, using slower non-system-call '.$algorithm.' algorithm'); |
||
602 | break; |
||
603 | } |
||
604 | return substr(`$commandline`, 0, $hash_length); |
||
605 | } |
||
606 | |||
607 | if (empty($tempdir)) { |
||
608 | // yes this is ugly, feel free to suggest a better way |
||
609 | require_once(dirname(__FILE__).'/getid3.php'); |
||
610 | $getid3_temp = new getID3(); |
||
611 | $tempdir = $getid3_temp->tempdir; |
||
612 | unset($getid3_temp); |
||
613 | } |
||
614 | // try to create a temporary file in the system temp directory - invalid dirname should force to system temp dir |
||
615 | if (($data_filename = tempnam($tempdir, 'gI3')) === false) { |
||
616 | // can't find anywhere to create a temp file, just fail |
||
617 | return false; |
||
618 | } |
||
619 | |||
620 | // Init |
||
621 | $result = false; |
||
622 | |||
623 | // copy parts of file |
||
624 | try { |
||
625 | self::CopyFileParts($file, $data_filename, $offset, $end - $offset); |
||
626 | $result = $hash_function($data_filename); |
||
627 | } catch (Exception $e) { |
||
628 | throw new Exception('self::CopyFileParts() failed in getid_lib::hash_data(): '.$e->getMessage()); |
||
629 | } |
||
630 | unlink($data_filename); |
||
631 | return $result; |
||
632 | } |
||
633 | |||
634 | public static function CopyFileParts($filename_source, $filename_dest, $offset, $length) { |
||
635 | if (!self::intValueSupported($offset + $length)) { |
||
636 | throw new Exception('cannot copy file portion, it extends beyond the '.round(PHP_INT_MAX / 1073741824).'GB limit'); |
||
637 | } |
||
638 | if (is_readable($filename_source) && is_file($filename_source) && ($fp_src = fopen($filename_source, 'rb'))) { |
||
639 | if (($fp_dest = fopen($filename_dest, 'wb'))) { |
||
640 | if (fseek($fp_src, $offset) == 0) { |
||
641 | $byteslefttowrite = $length; |
||
642 | while (($byteslefttowrite > 0) && ($buffer = fread($fp_src, min($byteslefttowrite, getID3::FREAD_BUFFER_SIZE)))) { |
||
643 | $byteswritten = fwrite($fp_dest, $buffer, $byteslefttowrite); |
||
644 | $byteslefttowrite -= $byteswritten; |
||
645 | } |
||
646 | return true; |
||
647 | } else { |
||
648 | throw new Exception('failed to seek to offset '.$offset.' in '.$filename_source); |
||
649 | } |
||
650 | fclose($fp_dest); |
||
651 | } else { |
||
652 | throw new Exception('failed to create file for writing '.$filename_dest); |
||
653 | } |
||
654 | fclose($fp_src); |
||
655 | } else { |
||
656 | throw new Exception('failed to open file for reading '.$filename_source); |
||
657 | } |
||
658 | return false; |
||
659 | } |
||
660 | |||
661 | public static function iconv_fallback_int_utf8($charval) { |
||
662 | if ($charval < 128) { |
||
663 | // 0bbbbbbb |
||
664 | $newcharstring = chr($charval); |
||
665 | } elseif ($charval < 2048) { |
||
666 | // 110bbbbb 10bbbbbb |
||
667 | $newcharstring = chr(($charval >> 6) | 0xC0); |
||
668 | $newcharstring .= chr(($charval & 0x3F) | 0x80); |
||
669 | } elseif ($charval < 65536) { |
||
670 | // 1110bbbb 10bbbbbb 10bbbbbb |
||
671 | $newcharstring = chr(($charval >> 12) | 0xE0); |
||
672 | $newcharstring .= chr(($charval >> 6) | 0xC0); |
||
673 | $newcharstring .= chr(($charval & 0x3F) | 0x80); |
||
674 | } else { |
||
675 | // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb |
||
676 | $newcharstring = chr(($charval >> 18) | 0xF0); |
||
677 | $newcharstring .= chr(($charval >> 12) | 0xC0); |
||
678 | $newcharstring .= chr(($charval >> 6) | 0xC0); |
||
679 | $newcharstring .= chr(($charval & 0x3F) | 0x80); |
||
680 | } |
||
681 | return $newcharstring; |
||
682 | } |
||
683 | |||
684 | // ISO-8859-1 => UTF-8 |
||
685 | public static function iconv_fallback_iso88591_utf8($string, $bom=false) { |
||
686 | if (function_exists('utf8_encode')) { |
||
687 | return utf8_encode($string); |
||
688 | } |
||
689 | // utf8_encode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support) |
||
690 | $newcharstring = ''; |
||
691 | if ($bom) { |
||
692 | $newcharstring .= "\xEF\xBB\xBF"; |
||
693 | } |
||
694 | for ($i = 0; $i < strlen($string); $i++) { |
||
695 | $charval = ord($string{$i}); |
||
696 | $newcharstring .= self::iconv_fallback_int_utf8($charval); |
||
697 | } |
||
698 | return $newcharstring; |
||
699 | } |
||
700 | |||
701 | // ISO-8859-1 => UTF-16BE |
||
702 | public static function iconv_fallback_iso88591_utf16be($string, $bom=false) { |
||
703 | $newcharstring = ''; |
||
704 | if ($bom) { |
||
705 | $newcharstring .= "\xFE\xFF"; |
||
706 | } |
||
707 | for ($i = 0; $i < strlen($string); $i++) { |
||
708 | $newcharstring .= "\x00".$string{$i}; |
||
709 | } |
||
710 | return $newcharstring; |
||
711 | } |
||
712 | |||
713 | // ISO-8859-1 => UTF-16LE |
||
714 | public static function iconv_fallback_iso88591_utf16le($string, $bom=false) { |
||
715 | $newcharstring = ''; |
||
716 | if ($bom) { |
||
717 | $newcharstring .= "\xFF\xFE"; |
||
718 | } |
||
719 | for ($i = 0; $i < strlen($string); $i++) { |
||
720 | $newcharstring .= $string{$i}."\x00"; |
||
721 | } |
||
722 | return $newcharstring; |
||
723 | } |
||
724 | |||
725 | // ISO-8859-1 => UTF-16LE (BOM) |
||
726 | public static function iconv_fallback_iso88591_utf16($string) { |
||
727 | return self::iconv_fallback_iso88591_utf16le($string, true); |
||
728 | } |
||
729 | |||
730 | // UTF-8 => ISO-8859-1 |
||
731 | public static function iconv_fallback_utf8_iso88591($string) { |
||
732 | if (function_exists('utf8_decode')) { |
||
733 | return utf8_decode($string); |
||
734 | } |
||
735 | // utf8_decode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support) |
||
736 | $newcharstring = ''; |
||
737 | $offset = 0; |
||
738 | $stringlength = strlen($string); |
||
739 | while ($offset < $stringlength) { |
||
740 | if ((ord($string{$offset}) | 0x07) == 0xF7) { |
||
741 | // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb |
||
742 | $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) & |
||
743 | ((ord($string{($offset + 1)}) & 0x3F) << 12) & |
||
744 | ((ord($string{($offset + 2)}) & 0x3F) << 6) & |
||
745 | (ord($string{($offset + 3)}) & 0x3F); |
||
746 | $offset += 4; |
||
747 | } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) { |
||
748 | // 1110bbbb 10bbbbbb 10bbbbbb |
||
749 | $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) & |
||
750 | ((ord($string{($offset + 1)}) & 0x3F) << 6) & |
||
751 | (ord($string{($offset + 2)}) & 0x3F); |
||
752 | $offset += 3; |
||
753 | } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) { |
||
754 | // 110bbbbb 10bbbbbb |
||
755 | $charval = ((ord($string{($offset + 0)}) & 0x1F) << 6) & |
||
756 | (ord($string{($offset + 1)}) & 0x3F); |
||
757 | $offset += 2; |
||
758 | } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) { |
||
759 | // 0bbbbbbb |
||
760 | $charval = ord($string{$offset}); |
||
761 | $offset += 1; |
||
762 | } else { |
||
763 | // error? throw some kind of warning here? |
||
764 | $charval = false; |
||
765 | $offset += 1; |
||
766 | } |
||
767 | if ($charval !== false) { |
||
768 | $newcharstring .= (($charval < 256) ? chr($charval) : '?'); |
||
769 | } |
||
770 | } |
||
771 | return $newcharstring; |
||
772 | } |
||
773 | |||
774 | // UTF-8 => UTF-16BE |
||
775 | public static function iconv_fallback_utf8_utf16be($string, $bom=false) { |
||
776 | $newcharstring = ''; |
||
777 | if ($bom) { |
||
778 | $newcharstring .= "\xFE\xFF"; |
||
779 | } |
||
780 | $offset = 0; |
||
781 | $stringlength = strlen($string); |
||
782 | while ($offset < $stringlength) { |
||
783 | if ((ord($string{$offset}) | 0x07) == 0xF7) { |
||
784 | // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb |
||
785 | $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) & |
||
786 | ((ord($string{($offset + 1)}) & 0x3F) << 12) & |
||
787 | ((ord($string{($offset + 2)}) & 0x3F) << 6) & |
||
788 | (ord($string{($offset + 3)}) & 0x3F); |
||
789 | $offset += 4; |
||
790 | } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) { |
||
791 | // 1110bbbb 10bbbbbb 10bbbbbb |
||
792 | $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) & |
||
793 | ((ord($string{($offset + 1)}) & 0x3F) << 6) & |
||
794 | (ord($string{($offset + 2)}) & 0x3F); |
||
795 | $offset += 3; |
||
796 | } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) { |
||
797 | // 110bbbbb 10bbbbbb |
||
798 | $charval = ((ord($string{($offset + 0)}) & 0x1F) << 6) & |
||
799 | (ord($string{($offset + 1)}) & 0x3F); |
||
800 | $offset += 2; |
||
801 | } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) { |
||
802 | // 0bbbbbbb |
||
803 | $charval = ord($string{$offset}); |
||
804 | $offset += 1; |
||
805 | } else { |
||
806 | // error? throw some kind of warning here? |
||
807 | $charval = false; |
||
808 | $offset += 1; |
||
809 | } |
||
810 | if ($charval !== false) { |
||
811 | $newcharstring .= (($charval < 65536) ? self::BigEndian2String($charval, 2) : "\x00".'?'); |
||
812 | } |
||
813 | } |
||
814 | return $newcharstring; |
||
815 | } |
||
816 | |||
817 | // UTF-8 => UTF-16LE |
||
818 | public static function iconv_fallback_utf8_utf16le($string, $bom=false) { |
||
819 | $newcharstring = ''; |
||
820 | if ($bom) { |
||
821 | $newcharstring .= "\xFF\xFE"; |
||
822 | } |
||
823 | $offset = 0; |
||
824 | $stringlength = strlen($string); |
||
825 | while ($offset < $stringlength) { |
||
826 | if ((ord($string{$offset}) | 0x07) == 0xF7) { |
||
827 | // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb |
||
828 | $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) & |
||
829 | ((ord($string{($offset + 1)}) & 0x3F) << 12) & |
||
830 | ((ord($string{($offset + 2)}) & 0x3F) << 6) & |
||
831 | (ord($string{($offset + 3)}) & 0x3F); |
||
832 | $offset += 4; |
||
833 | } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) { |
||
834 | // 1110bbbb 10bbbbbb 10bbbbbb |
||
835 | $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) & |
||
836 | ((ord($string{($offset + 1)}) & 0x3F) << 6) & |
||
837 | (ord($string{($offset + 2)}) & 0x3F); |
||
838 | $offset += 3; |
||
839 | } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) { |
||
840 | // 110bbbbb 10bbbbbb |
||
841 | $charval = ((ord($string{($offset + 0)}) & 0x1F) << 6) & |
||
842 | (ord($string{($offset + 1)}) & 0x3F); |
||
843 | $offset += 2; |
||
844 | } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) { |
||
845 | // 0bbbbbbb |
||
846 | $charval = ord($string{$offset}); |
||
847 | $offset += 1; |
||
848 | } else { |
||
849 | // error? maybe throw some warning here? |
||
850 | $charval = false; |
||
851 | $offset += 1; |
||
852 | } |
||
853 | if ($charval !== false) { |
||
854 | $newcharstring .= (($charval < 65536) ? self::LittleEndian2String($charval, 2) : '?'."\x00"); |
||
855 | } |
||
856 | } |
||
857 | return $newcharstring; |
||
858 | } |
||
859 | |||
860 | // UTF-8 => UTF-16LE (BOM) |
||
861 | public static function iconv_fallback_utf8_utf16($string) { |
||
862 | return self::iconv_fallback_utf8_utf16le($string, true); |
||
863 | } |
||
864 | |||
865 | // UTF-16BE => UTF-8 |
||
866 | public static function iconv_fallback_utf16be_utf8($string) { |
||
867 | if (substr($string, 0, 2) == "\xFE\xFF") { |
||
868 | // strip BOM |
||
869 | $string = substr($string, 2); |
||
870 | } |
||
871 | $newcharstring = ''; |
||
872 | for ($i = 0; $i < strlen($string); $i += 2) { |
||
873 | $charval = self::BigEndian2Int(substr($string, $i, 2)); |
||
874 | $newcharstring .= self::iconv_fallback_int_utf8($charval); |
||
875 | } |
||
876 | return $newcharstring; |
||
877 | } |
||
878 | |||
879 | // UTF-16LE => UTF-8 |
||
880 | public static function iconv_fallback_utf16le_utf8($string) { |
||
881 | if (substr($string, 0, 2) == "\xFF\xFE") { |
||
882 | // strip BOM |
||
883 | $string = substr($string, 2); |
||
884 | } |
||
885 | $newcharstring = ''; |
||
886 | for ($i = 0; $i < strlen($string); $i += 2) { |
||
887 | $charval = self::LittleEndian2Int(substr($string, $i, 2)); |
||
888 | $newcharstring .= self::iconv_fallback_int_utf8($charval); |
||
889 | } |
||
890 | return $newcharstring; |
||
891 | } |
||
892 | |||
893 | // UTF-16BE => ISO-8859-1 |
||
894 | public static function iconv_fallback_utf16be_iso88591($string) { |
||
895 | if (substr($string, 0, 2) == "\xFE\xFF") { |
||
896 | // strip BOM |
||
897 | $string = substr($string, 2); |
||
898 | } |
||
899 | $newcharstring = ''; |
||
900 | for ($i = 0; $i < strlen($string); $i += 2) { |
||
901 | $charval = self::BigEndian2Int(substr($string, $i, 2)); |
||
902 | $newcharstring .= (($charval < 256) ? chr($charval) : '?'); |
||
903 | } |
||
904 | return $newcharstring; |
||
905 | } |
||
906 | |||
907 | // UTF-16LE => ISO-8859-1 |
||
908 | public static function iconv_fallback_utf16le_iso88591($string) { |
||
909 | if (substr($string, 0, 2) == "\xFF\xFE") { |
||
910 | // strip BOM |
||
911 | $string = substr($string, 2); |
||
912 | } |
||
913 | $newcharstring = ''; |
||
914 | for ($i = 0; $i < strlen($string); $i += 2) { |
||
915 | $charval = self::LittleEndian2Int(substr($string, $i, 2)); |
||
916 | $newcharstring .= (($charval < 256) ? chr($charval) : '?'); |
||
917 | } |
||
918 | return $newcharstring; |
||
919 | } |
||
920 | |||
921 | // UTF-16 (BOM) => ISO-8859-1 |
||
922 | public static function iconv_fallback_utf16_iso88591($string) { |
||
923 | $bom = substr($string, 0, 2); |
||
924 | if ($bom == "\xFE\xFF") { |
||
925 | return self::iconv_fallback_utf16be_iso88591(substr($string, 2)); |
||
926 | } elseif ($bom == "\xFF\xFE") { |
||
927 | return self::iconv_fallback_utf16le_iso88591(substr($string, 2)); |
||
928 | } |
||
929 | return $string; |
||
930 | } |
||
931 | |||
932 | // UTF-16 (BOM) => UTF-8 |
||
933 | public static function iconv_fallback_utf16_utf8($string) { |
||
934 | $bom = substr($string, 0, 2); |
||
935 | if ($bom == "\xFE\xFF") { |
||
936 | return self::iconv_fallback_utf16be_utf8(substr($string, 2)); |
||
937 | } elseif ($bom == "\xFF\xFE") { |
||
938 | return self::iconv_fallback_utf16le_utf8(substr($string, 2)); |
||
939 | } |
||
940 | return $string; |
||
941 | } |
||
942 | |||
943 | public static function iconv_fallback($in_charset, $out_charset, $string) { |
||
944 | |||
945 | if ($in_charset == $out_charset) { |
||
946 | return $string; |
||
947 | } |
||
948 | |||
949 | // iconv() availble |
||
950 | if (function_exists('iconv')) { |
||
951 | if ($converted_string = @iconv($in_charset, $out_charset.'//TRANSLIT', $string)) { |
||
952 | switch ($out_charset) { |
||
953 | case 'ISO-8859-1': |
||
954 | $converted_string = rtrim($converted_string, "\x00"); |
||
955 | break; |
||
956 | } |
||
957 | return $converted_string; |
||
958 | } |
||
959 | |||
960 | // iconv() may sometimes fail with "illegal character in input string" error message |
||
961 | // and return an empty string, but returning the unconverted string is more useful |
||
962 | return $string; |
||
963 | } |
||
964 | |||
965 | |||
966 | // iconv() not available |
||
967 | static $ConversionFunctionList = array(); |
||
968 | if (empty($ConversionFunctionList)) { |
||
969 | $ConversionFunctionList['ISO-8859-1']['UTF-8'] = 'iconv_fallback_iso88591_utf8'; |
||
970 | $ConversionFunctionList['ISO-8859-1']['UTF-16'] = 'iconv_fallback_iso88591_utf16'; |
||
971 | $ConversionFunctionList['ISO-8859-1']['UTF-16BE'] = 'iconv_fallback_iso88591_utf16be'; |
||
972 | $ConversionFunctionList['ISO-8859-1']['UTF-16LE'] = 'iconv_fallback_iso88591_utf16le'; |
||
973 | $ConversionFunctionList['UTF-8']['ISO-8859-1'] = 'iconv_fallback_utf8_iso88591'; |
||
974 | $ConversionFunctionList['UTF-8']['UTF-16'] = 'iconv_fallback_utf8_utf16'; |
||
975 | $ConversionFunctionList['UTF-8']['UTF-16BE'] = 'iconv_fallback_utf8_utf16be'; |
||
976 | $ConversionFunctionList['UTF-8']['UTF-16LE'] = 'iconv_fallback_utf8_utf16le'; |
||
977 | $ConversionFunctionList['UTF-16']['ISO-8859-1'] = 'iconv_fallback_utf16_iso88591'; |
||
978 | $ConversionFunctionList['UTF-16']['UTF-8'] = 'iconv_fallback_utf16_utf8'; |
||
979 | $ConversionFunctionList['UTF-16LE']['ISO-8859-1'] = 'iconv_fallback_utf16le_iso88591'; |
||
980 | $ConversionFunctionList['UTF-16LE']['UTF-8'] = 'iconv_fallback_utf16le_utf8'; |
||
981 | $ConversionFunctionList['UTF-16BE']['ISO-8859-1'] = 'iconv_fallback_utf16be_iso88591'; |
||
982 | $ConversionFunctionList['UTF-16BE']['UTF-8'] = 'iconv_fallback_utf16be_utf8'; |
||
983 | } |
||
984 | if (isset($ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)])) { |
||
985 | $ConversionFunction = $ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)]; |
||
986 | return self::$ConversionFunction($string); |
||
987 | } |
||
988 | throw new Exception('PHP does not have iconv() support - cannot convert from '.$in_charset.' to '.$out_charset); |
||
989 | } |
||
990 | |||
991 | public static function recursiveMultiByteCharString2HTML($data, $charset='ISO-8859-1') { |
||
992 | if (is_string($data)) { |
||
993 | return self::MultiByteCharString2HTML($data, $charset); |
||
994 | } elseif (is_array($data)) { |
||
995 | $return_data = array(); |
||
996 | foreach ($data as $key => $value) { |
||
997 | $return_data[$key] = self::recursiveMultiByteCharString2HTML($value, $charset); |
||
998 | } |
||
999 | return $return_data; |
||
1000 | } |
||
1001 | // integer, float, objects, resources, etc |
||
1002 | return $data; |
||
1003 | } |
||
1004 | |||
1005 | public static function MultiByteCharString2HTML($string, $charset='ISO-8859-1') { |
||
1006 | $string = (string) $string; // in case trying to pass a numeric (float, int) string, would otherwise return an empty string |
||
1007 | $HTMLstring = ''; |
||
1008 | |||
1009 | switch ($charset) { |
||
1010 | case '1251': |
||
1011 | case '1252': |
||
1012 | case '866': |
||
1013 | case '932': |
||
1014 | case '936': |
||
1015 | case '950': |
||
1016 | case 'BIG5': |
||
1017 | case 'BIG5-HKSCS': |
||
1018 | case 'cp1251': |
||
1019 | case 'cp1252': |
||
1020 | case 'cp866': |
||
1021 | case 'EUC-JP': |
||
1022 | case 'EUCJP': |
||
1023 | case 'GB2312': |
||
1024 | case 'ibm866': |
||
1025 | case 'ISO-8859-1': |
||
1026 | case 'ISO-8859-15': |
||
1027 | case 'ISO8859-1': |
||
1028 | case 'ISO8859-15': |
||
1029 | case 'KOI8-R': |
||
1030 | case 'koi8-ru': |
||
1031 | case 'koi8r': |
||
1032 | case 'Shift_JIS': |
||
1033 | case 'SJIS': |
||
1034 | case 'win-1251': |
||
1035 | case 'Windows-1251': |
||
1036 | case 'Windows-1252': |
||
1037 | $HTMLstring = htmlentities($string, ENT_COMPAT, $charset); |
||
1038 | break; |
||
1039 | |||
1040 | case 'UTF-8': |
||
1041 | $strlen = strlen($string); |
||
1042 | for ($i = 0; $i < $strlen; $i++) { |
||
1043 | $char_ord_val = ord($string{$i}); |
||
1044 | $charval = 0; |
||
1045 | if ($char_ord_val < 0x80) { |
||
1046 | $charval = $char_ord_val; |
||
1047 | } elseif ((($char_ord_val & 0xF0) >> 4) == 0x0F && $i+3 < $strlen) { |
||
1048 | $charval = (($char_ord_val & 0x07) << 18); |
||
1049 | $charval += ((ord($string{++$i}) & 0x3F) << 12); |
||
1050 | $charval += ((ord($string{++$i}) & 0x3F) << 6); |
||
1051 | $charval += (ord($string{++$i}) & 0x3F); |
||
1052 | } elseif ((($char_ord_val & 0xE0) >> 5) == 0x07 && $i+2 < $strlen) { |
||
1053 | $charval = (($char_ord_val & 0x0F) << 12); |
||
1054 | $charval += ((ord($string{++$i}) & 0x3F) << 6); |
||
1055 | $charval += (ord($string{++$i}) & 0x3F); |
||
1056 | } elseif ((($char_ord_val & 0xC0) >> 6) == 0x03 && $i+1 < $strlen) { |
||
1057 | $charval = (($char_ord_val & 0x1F) << 6); |
||
1058 | $charval += (ord($string{++$i}) & 0x3F); |
||
1059 | } |
||
1060 | if (($charval >= 32) && ($charval <= 127)) { |
||
1061 | $HTMLstring .= htmlentities(chr($charval)); |
||
1062 | } else { |
||
1063 | $HTMLstring .= '&#'.$charval.';'; |
||
1064 | } |
||
1065 | } |
||
1066 | break; |
||
1067 | |||
1068 | case 'UTF-16LE': |
||
1069 | for ($i = 0; $i < strlen($string); $i += 2) { |
||
1070 | $charval = self::LittleEndian2Int(substr($string, $i, 2)); |
||
1071 | if (($charval >= 32) && ($charval <= 127)) { |
||
1072 | $HTMLstring .= chr($charval); |
||
1073 | } else { |
||
1074 | $HTMLstring .= '&#'.$charval.';'; |
||
1075 | } |
||
1076 | } |
||
1077 | break; |
||
1078 | |||
1079 | case 'UTF-16BE': |
||
1080 | for ($i = 0; $i < strlen($string); $i += 2) { |
||
1081 | $charval = self::BigEndian2Int(substr($string, $i, 2)); |
||
1082 | if (($charval >= 32) && ($charval <= 127)) { |
||
1083 | $HTMLstring .= chr($charval); |
||
1084 | } else { |
||
1085 | $HTMLstring .= '&#'.$charval.';'; |
||
1086 | } |
||
1087 | } |
||
1088 | break; |
||
1089 | |||
1090 | default: |
||
1091 | $HTMLstring = 'ERROR: Character set "'.$charset.'" not supported in MultiByteCharString2HTML()'; |
||
1092 | break; |
||
1093 | } |
||
1094 | return $HTMLstring; |
||
1095 | } |
||
1096 | |||
1097 | |||
1098 | |||
1099 | public static function RGADnameLookup($namecode) { |
||
1100 | static $RGADname = array(); |
||
1101 | if (empty($RGADname)) { |
||
1102 | $RGADname[0] = 'not set'; |
||
1103 | $RGADname[1] = 'Track Gain Adjustment'; |
||
1104 | $RGADname[2] = 'Album Gain Adjustment'; |
||
1105 | } |
||
1106 | |||
1107 | return (isset($RGADname[$namecode]) ? $RGADname[$namecode] : ''); |
||
1108 | } |
||
1109 | |||
1110 | |||
1111 | public static function RGADoriginatorLookup($originatorcode) { |
||
1112 | static $RGADoriginator = array(); |
||
1113 | if (empty($RGADoriginator)) { |
||
1114 | $RGADoriginator[0] = 'unspecified'; |
||
1115 | $RGADoriginator[1] = 'pre-set by artist/producer/mastering engineer'; |
||
1116 | $RGADoriginator[2] = 'set by user'; |
||
1117 | $RGADoriginator[3] = 'determined automatically'; |
||
1118 | } |
||
1119 | |||
1120 | return (isset($RGADoriginator[$originatorcode]) ? $RGADoriginator[$originatorcode] : ''); |
||
1121 | } |
||
1122 | |||
1123 | |||
1124 | public static function RGADadjustmentLookup($rawadjustment, $signbit) { |
||
1125 | $adjustment = $rawadjustment / 10; |
||
1126 | if ($signbit == 1) { |
||
1127 | $adjustment *= -1; |
||
1128 | } |
||
1129 | return (float) $adjustment; |
||
1130 | } |
||
1131 | |||
1132 | |||
1133 | public static function RGADgainString($namecode, $originatorcode, $replaygain) { |
||
1134 | if ($replaygain < 0) { |
||
1135 | $signbit = '1'; |
||
1136 | } else { |
||
1137 | $signbit = '0'; |
||
1138 | } |
||
1139 | $storedreplaygain = intval(round($replaygain * 10)); |
||
1140 | $gainstring = str_pad(decbin($namecode), 3, '0', STR_PAD_LEFT); |
||
1141 | $gainstring .= str_pad(decbin($originatorcode), 3, '0', STR_PAD_LEFT); |
||
1142 | $gainstring .= $signbit; |
||
1143 | $gainstring .= str_pad(decbin($storedreplaygain), 9, '0', STR_PAD_LEFT); |
||
1144 | |||
1145 | return $gainstring; |
||
1146 | } |
||
1147 | |||
1148 | public static function RGADamplitude2dB($amplitude) { |
||
1149 | return 20 * log10($amplitude); |
||
1150 | } |
||
1151 | |||
1152 | |||
1153 | public static function GetDataImageSize($imgData, &$imageinfo=array()) { |
||
1154 | static $tempdir = ''; |
||
1155 | if (empty($tempdir)) { |
||
1156 | // yes this is ugly, feel free to suggest a better way |
||
1157 | require_once(dirname(__FILE__).'/getid3.php'); |
||
1158 | $getid3_temp = new getID3(); |
||
1159 | $tempdir = $getid3_temp->tempdir; |
||
1160 | unset($getid3_temp); |
||
1161 | } |
||
1162 | $GetDataImageSize = false; |
||
1163 | if ($tempfilename = tempnam($tempdir, 'gI3')) { |
||
1164 | if (is_writable($tempfilename) && is_file($tempfilename) && ($tmp = fopen($tempfilename, 'wb'))) { |
||
1165 | fwrite($tmp, $imgData); |
||
1166 | fclose($tmp); |
||
1167 | $GetDataImageSize = @getimagesize($tempfilename, $imageinfo); |
||
1168 | $GetDataImageSize['height'] = $GetDataImageSize[0]; |
||
1169 | $GetDataImageSize['width'] = $GetDataImageSize[1]; |
||
1170 | } |
||
1171 | unlink($tempfilename); |
||
1172 | } |
||
1173 | return $GetDataImageSize; |
||
1174 | } |
||
1175 | |||
1176 | public static function ImageExtFromMime($mime_type) { |
||
1177 | // temporary way, works OK for now, but should be reworked in the future |
||
1178 | return str_replace(array('image/', 'x-', 'jpeg'), array('', '', 'jpg'), $mime_type); |
||
1179 | } |
||
1180 | |||
1181 | public static function ImageTypesLookup($imagetypeid) { |
||
1182 | static $ImageTypesLookup = array(); |
||
1183 | if (empty($ImageTypesLookup)) { |
||
1184 | $ImageTypesLookup[1] = 'gif'; |
||
1185 | $ImageTypesLookup[2] = 'jpeg'; |
||
1186 | $ImageTypesLookup[3] = 'png'; |
||
1187 | $ImageTypesLookup[4] = 'swf'; |
||
1188 | $ImageTypesLookup[5] = 'psd'; |
||
1189 | $ImageTypesLookup[6] = 'bmp'; |
||
1190 | $ImageTypesLookup[7] = 'tiff (little-endian)'; |
||
1191 | $ImageTypesLookup[8] = 'tiff (big-endian)'; |
||
1192 | $ImageTypesLookup[9] = 'jpc'; |
||
1193 | $ImageTypesLookup[10] = 'jp2'; |
||
1194 | $ImageTypesLookup[11] = 'jpx'; |
||
1195 | $ImageTypesLookup[12] = 'jb2'; |
||
1196 | $ImageTypesLookup[13] = 'swc'; |
||
1197 | $ImageTypesLookup[14] = 'iff'; |
||
1198 | } |
||
1199 | return (isset($ImageTypesLookup[$imagetypeid]) ? $ImageTypesLookup[$imagetypeid] : ''); |
||
1200 | } |
||
1201 | |||
1202 | public static function CopyTagsToComments(&$ThisFileInfo) { |
||
1203 | |||
1204 | // Copy all entries from ['tags'] into common ['comments'] |
||
1205 | if (!empty($ThisFileInfo['tags'])) { |
||
1206 | foreach ($ThisFileInfo['tags'] as $tagtype => $tagarray) { |
||
1207 | foreach ($tagarray as $tagname => $tagdata) { |
||
1208 | foreach ($tagdata as $key => $value) { |
||
1209 | if (!empty($value)) { |
||
1210 | if (empty($ThisFileInfo['comments'][$tagname])) { |
||
1211 | |||
1212 | // fall through and append value |
||
1213 | |||
1214 | } elseif ($tagtype == 'id3v1') { |
||
1215 | |||
1216 | $newvaluelength = strlen(trim($value)); |
||
1217 | foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) { |
||
1218 | $oldvaluelength = strlen(trim($existingvalue)); |
||
1219 | if (($newvaluelength <= $oldvaluelength) && (substr($existingvalue, 0, $newvaluelength) == trim($value))) { |
||
1220 | // new value is identical but shorter-than (or equal-length to) one already in comments - skip |
||
1221 | break 2; |
||
1222 | } |
||
1223 | } |
||
1224 | |||
1225 | } elseif (!is_array($value)) { |
||
1226 | |||
1227 | $newvaluelength = strlen(trim($value)); |
||
1228 | foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) { |
||
1229 | $oldvaluelength = strlen(trim($existingvalue)); |
||
1230 | if ((strlen($existingvalue) > 10) && ($newvaluelength > $oldvaluelength) && (substr(trim($value), 0, strlen($existingvalue)) == $existingvalue)) { |
||
1231 | $ThisFileInfo['comments'][$tagname][$existingkey] = trim($value); |
||
1232 | //break 2; |
||
1233 | break; |
||
1234 | } |
||
1235 | } |
||
1236 | |||
1237 | } |
||
1238 | if (is_array($value) || empty($ThisFileInfo['comments'][$tagname]) || !in_array(trim($value), $ThisFileInfo['comments'][$tagname])) { |
||
1239 | $value = (is_string($value) ? trim($value) : $value); |
||
1240 | if (!is_numeric($key)) { |
||
1241 | $ThisFileInfo['comments'][$tagname][$key] = $value; |
||
1242 | } else { |
||
1243 | $ThisFileInfo['comments'][$tagname][] = $value; |
||
1244 | } |
||
1245 | } |
||
1246 | } |
||
1247 | } |
||
1248 | } |
||
1249 | } |
||
1250 | |||
1251 | // Copy to ['comments_html'] |
||
1252 | if (!empty($ThisFileInfo['comments'])) { |
||
1253 | foreach ($ThisFileInfo['comments'] as $field => $values) { |
||
1254 | if ($field == 'picture') { |
||
1255 | // pictures can take up a lot of space, and we don't need multiple copies of them |
||
1256 | // let there be a single copy in [comments][picture], and not elsewhere |
||
1257 | continue; |
||
1258 | } |
||
1259 | foreach ($values as $index => $value) { |
||
1260 | if (is_array($value)) { |
||
1261 | $ThisFileInfo['comments_html'][$field][$index] = $value; |
||
1262 | } else { |
||
1263 | $ThisFileInfo['comments_html'][$field][$index] = str_replace('�', '', self::MultiByteCharString2HTML($value, $ThisFileInfo['encoding'])); |
||
1264 | } |
||
1265 | } |
||
1266 | } |
||
1267 | } |
||
1268 | |||
1269 | } |
||
1270 | return true; |
||
1271 | } |
||
1272 | |||
1273 | |||
1274 | public static function EmbeddedLookup($key, $begin, $end, $file, $name) { |
||
1275 | |||
1276 | // Cached |
||
1277 | static $cache; |
||
1278 | if (isset($cache[$file][$name])) { |
||
1279 | return (isset($cache[$file][$name][$key]) ? $cache[$file][$name][$key] : ''); |
||
1280 | } |
||
1281 | |||
1282 | // Init |
||
1283 | $keylength = strlen($key); |
||
1284 | $line_count = $end - $begin - 7; |
||
1285 | |||
1286 | // Open php file |
||
1287 | $fp = fopen($file, 'r'); |
||
1288 | |||
1289 | // Discard $begin lines |
||
1290 | for ($i = 0; $i < ($begin + 3); $i++) { |
||
1291 | fgets($fp, 1024); |
||
1292 | } |
||
1293 | |||
1294 | // Loop thru line |
||
1295 | while (0 < $line_count--) { |
||
1296 | |||
1297 | // Read line |
||
1298 | $line = ltrim(fgets($fp, 1024), "\t "); |
||
1299 | |||
1300 | // METHOD A: only cache the matching key - less memory but slower on next lookup of not-previously-looked-up key |
||
1301 | //$keycheck = substr($line, 0, $keylength); |
||
1302 | //if ($key == $keycheck) { |
||
1303 | // $cache[$file][$name][$keycheck] = substr($line, $keylength + 1); |
||
1304 | // break; |
||
1305 | //} |
||
1306 | |||
1307 | // METHOD B: cache all keys in this lookup - more memory but faster on next lookup of not-previously-looked-up key |
||
1308 | //$cache[$file][$name][substr($line, 0, $keylength)] = trim(substr($line, $keylength + 1)); |
||
1309 | $explodedLine = explode("\t", $line, 2); |
||
1310 | $ThisKey = (isset($explodedLine[0]) ? $explodedLine[0] : ''); |
||
1311 | $ThisValue = (isset($explodedLine[1]) ? $explodedLine[1] : ''); |
||
1312 | $cache[$file][$name][$ThisKey] = trim($ThisValue); |
||
1313 | } |
||
1314 | |||
1315 | // Close and return |
||
1316 | fclose($fp); |
||
1317 | return (isset($cache[$file][$name][$key]) ? $cache[$file][$name][$key] : ''); |
||
1318 | } |
||
1319 | |||
1320 | public static function IncludeDependency($filename, $sourcefile, $DieOnFailure=false) { |
||
1321 | global $GETID3_ERRORARRAY; |
||
1322 | |||
1323 | if (file_exists($filename)) { |
||
1324 | if (include_once($filename)) { |
||
1325 | return true; |
||
1326 | } else { |
||
1327 | $diemessage = basename($sourcefile).' depends on '.$filename.', which has errors'; |
||
1328 | } |
||
1329 | } else { |
||
1330 | $diemessage = basename($sourcefile).' depends on '.$filename.', which is missing'; |
||
1331 | } |
||
1332 | if ($DieOnFailure) { |
||
1333 | throw new Exception($diemessage); |
||
1334 | } else { |
||
1335 | $GETID3_ERRORARRAY[] = $diemessage; |
||
1336 | } |
||
1337 | return false; |
||
1338 | } |
||
1339 | |||
1340 | public static function trimNullByte($string) { |
||
1341 | return trim($string, "\x00"); |
||
1342 | } |
||
1343 | |||
1344 | public static function getFileSizeSyscall($path) { |
||
1345 | $filesize = false; |
||
1346 | |||
1347 | if (GETID3_OS_ISWINDOWS) { |
||
1348 | 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: |
||
1349 | $filesystem = new COM('Scripting.FileSystemObject'); |
||
1350 | $file = $filesystem->GetFile($path); |
||
1351 | $filesize = $file->Size(); |
||
1352 | unset($filesystem, $file); |
||
1353 | } else { |
||
1354 | $commandline = 'for %I in ('.escapeshellarg($path).') do @echo %~zI'; |
||
1355 | } |
||
1356 | } else { |
||
1357 | $commandline = 'ls -l '.escapeshellarg($path).' | awk \'{print $5}\''; |
||
1358 | } |
||
1359 | if (isset($commandline)) { |
||
1360 | $output = trim(`$commandline`); |
||
1361 | if (ctype_digit($output)) { |
||
1362 | $filesize = (float) $output; |
||
1363 | } |
||
1364 | } |
||
1365 | return $filesize; |
||
1366 | } |
||
1367 | |||
1368 | |||
1369 | /** |
||
1370 | * Workaround for Bug #37268 (https://bugs.php.net/bug.php?id=37268) |
||
1371 | * @param string $path A path. |
||
1372 | * @param string $suffix If the name component ends in suffix this will also be cut off. |
||
1373 | * @return string |
||
1374 | */ |
||
1375 | public static function mb_basename($path, $suffix = null) { |
||
1376 | $splited = preg_split('#/#', rtrim($path, '/ ')); |
||
1377 | return substr(basename('X'.$splited[count($splited) - 1], $suffix), 1); |
||
1378 | } |
||
1379 | |||
1380 | } |
||
1381 |