Make WordPress Core


Ignore:
Timestamp:
09/14/2019 07:06:09 PM (7 years ago)
Author:
jorbin
Message:

Update getID3 library to fix issues with PHP7.4

Updates to trunk version that includes fixes for PHP7.4

Changelog:
​https://github.com/JamesHeinrich/getID3/compare/v1.9.14...00f3fbfd77e583099ca70a3cf0bc092e113d2b20

See: #47751,#47783.
Fixes: #48040.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/ID3/getid3.lib.php

    r41196 r46112  
    11<?php
     2
    23/////////////////////////////////////////////////////////////////
    34/// getID3() by James Heinrich <info@getid3.org>               //
    4 //  available at http://getid3.sourceforge.net                 //
    5 //            or http://www.getid3.org                         //
    6 //          also https://github.com/JamesHeinrich/getID3       //
    7 /////////////////////////////////////////////////////////////////
     5//  available at https://github.com/JamesHeinrich/getID3       //
     6//            or https://www.getid3.org                        //
     7//            or http://getid3.sourceforge.net                 //
    88//                                                             //
    99// getid3.lib.php - part of getID3()                           //
    10 // See readme.txt for more details                             //
     10//  see readme.txt for more details                            //
    1111//                                                            ///
    1212/////////////////////////////////////////////////////////////////
    … …  
    1515class getid3_lib
    1616{
    17 
     17        /**
     18         * @param string $string
     19         * @param bool   $hex
     20         * @param bool   $spaces
     21         * @param string $htmlencoding
     22         *
     23         * @return string
     24         */
    1825        public static function PrintHexBytes($string, $hex=true, $spaces=true, $htmlencoding='UTF-8') {
    1926                $returnstring = '';
    2027                for ($i = 0; $i < strlen($string); $i++) {
    2128                        if ($hex) {
    22                                 $returnstring .= str_pad(dechex(ord($string{$i})), 2, '0', STR_PAD_LEFT);
     29                                $returnstring .= str_pad(dechex(ord($string[$i])), 2, '0', STR_PAD_LEFT);
    2330                        } else {
    24                                 $returnstring .= ' '.(preg_match("#[\x20-\x7E]#", $string{$i}) ? $string{$i} : '¤');
     31                                $returnstring .= ' '.(preg_match("#[\x20-\x7E]#", $string[$i]) ? $string[$i] : '¤');
    2532                        }
    2633                        if ($spaces) {
    … …  
    3744        }
    3845
     46        /**
     47         * Truncates a floating-point number at the decimal point.
     48         *
     49         * @param float $floatnumber
     50         *
     51         * @return float|int returns int (if possible, otherwise float)
     52         */
    3953        public static function trunc($floatnumber) {
    40                 // truncates a floating-point number at the decimal point
    41                 // returns int (if possible, otherwise float)
    4254                if ($floatnumber >= 1) {
    4355                        $truncatednumber = floor($floatnumber);
    … …  
    5365        }
    5466
    55 
     67        /**
     68         * @param int|null $variable
     69         * @param int      $increment
     70         *
     71         * @return bool
     72         */
    5673        public static function safe_inc(&$variable, $increment=1) {
    5774                if (isset($variable)) {
    … …  
    6380        }
    6481
     82        /**
     83         * @param int|float $floatnum
     84         *
     85         * @return int|float
     86         */
    6587        public static function CastAsInt($floatnum) {
    6688                // convert to float if not already
    … …  
    78100        }
    79101
     102        /**
     103         * @param int $num
     104         *
     105         * @return bool
     106         */
    80107        public static function intValueSupported($num) {
    81108                // check if integers are 64-bit
    … …  
    94121        }
    95122
     123        /**
     124         * @param string $fraction
     125         *
     126         * @return float
     127         */
    96128        public static function DecimalizeFraction($fraction) {
    97129                list($numerator, $denominator) = explode('/', $fraction);
    … …  
    99131        }
    100132
    101 
     133        /**
     134         * @param string $binarynumerator
     135         *
     136         * @return float
     137         */
    102138        public static function DecimalBinary2Float($binarynumerator) {
    103139                $numerator   = self::Bin2Dec($binarynumerator);
    … …  
    106142        }
    107143
    108 
     144        /**
     145         * @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
     146         *
     147         * @param string $binarypointnumber
     148         * @param int    $maxbits
     149         *
     150         * @return array
     151         */
    109152        public static function NormalizeBinaryPoint($binarypointnumber, $maxbits=52) {
    110                 // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
    111153                if (strpos($binarypointnumber, '.') === false) {
    112154                        $binarypointnumber = '0.'.$binarypointnumber;
    113                 } elseif ($binarypointnumber{0} == '.') {
     155                } elseif ($binarypointnumber[0] == '.') {
    114156                        $binarypointnumber = '0'.$binarypointnumber;
    115157                }
    116158                $exponent = 0;
    117                 while (($binarypointnumber{0} != '1') || (substr($binarypointnumber, 1, 1) != '.')) {
     159                while (($binarypointnumber[0] != '1') || (substr($binarypointnumber, 1, 1) != '.')) {
    118160                        if (substr($binarypointnumber, 1, 1) == '.') {
    119161                                $exponent--;
    … …  
    123165                                $exponent += ($pointpos - 1);
    124166                                $binarypointnumber = str_replace('.', '', $binarypointnumber);
    125                                 $binarypointnumber = $binarypointnumber{0}.'.'.substr($binarypointnumber, 1);
     167                                $binarypointnumber = $binarypointnumber[0].'.'.substr($binarypointnumber, 1);
    126168                        }
    127169                }
    … …  
    130172        }
    131173
    132 
     174        /**
     175         * @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
     176         *
     177         * @param float $floatvalue
     178         *
     179         * @return string
     180         */
    133181        public static function Float2BinaryDecimal($floatvalue) {
    134                 // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
    135182                $maxbits = 128; // to how many bits of precision should the calculations be taken?
    136183                $intpart   = self::trunc($floatvalue);
    … …  
    146193        }
    147194
    148 
     195        /**
     196         * @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee-expl.html
     197         *
     198         * @param float $floatvalue
     199         * @param int $bits
     200         *
     201         * @return string|false
     202         */
    149203        public static function Float2String($floatvalue, $bits) {
    150                 // http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee-expl.html
     204                $exponentbits = 0;
     205                $fractionbits = 0;
    151206                switch ($bits) {
    152207                        case 32:
    … …  
    177232        }
    178233
    179 
     234        /**
     235         * @param string $byteword
     236         *
     237         * @return float|false
     238         */
    180239        public static function LittleEndian2Float($byteword) {
    181240                return self::BigEndian2Float(strrev($byteword));
    182241        }
    183242
    184 
     243        /**
     244         * ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic
     245         *
     246         * @link http://www.psc.edu/general/software/packages/ieee/ieee.html
     247         * @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html
     248         *
     249         * @param string $byteword
     250         *
     251         * @return float|false
     252         */
    185253        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 
    190254                $bitword = self::BigEndian2Bin($byteword);
    191255                if (!$bitword) {
    192256                        return 0;
    193257                }
    194                 $signbit = $bitword{0};
     258                $signbit = $bitword[0];
     259                $floatvalue = 0;
     260                $exponentbits = 0;
     261                $fractionbits = 0;
    195262
    196263                switch (strlen($byteword) * 8) {
    … …  
    209276                                // http://www.mactech.com/articles/mactech/Vol.06/06.01/SANENormalized/
    210277                                $exponentstring = substr($bitword, 1, 15);
    211                                 $isnormalized = intval($bitword{16});
     278                                $isnormalized = intval($bitword[16]);
    212279                                $fractionstring = substr($bitword, 17, 63);
    213280                                $exponent = pow(2, self::Bin2Dec($exponentstring) - 16383);
    … …  
    260327        }
    261328
    262 
     329        /**
     330         * @param string $byteword
     331         * @param bool   $synchsafe
     332         * @param bool   $signed
     333         *
     334         * @return int|float|false
     335         * @throws Exception
     336         */
    263337        public static function BigEndian2Int($byteword, $synchsafe=false, $signed=false) {
    264338                $intvalue = 0;
    … …  
    270344                        if ($synchsafe) { // disregard MSB, effectively 7-bit bytes
    271345                                //$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);
     346                                $intvalue += (ord($byteword[$i]) & 0x7F) * pow(2, ($bytewordlen - 1 - $i) * 7);
    273347                        } else {
    274                                 $intvalue += ord($byteword{$i}) * pow(256, ($bytewordlen - 1 - $i));
     348                                $intvalue += ord($byteword[$i]) * pow(256, ($bytewordlen - 1 - $i));
    275349                        }
    276350                }
    … …  
    289363        }
    290364
    291 
     365        /**
     366         * @param string $byteword
     367         * @param bool   $signed
     368         *
     369         * @return int|float|false
     370         */
    292371        public static function LittleEndian2Int($byteword, $signed=false) {
    293372                return self::BigEndian2Int(strrev($byteword), false, $signed);
    294373        }
    295374
     375        /**
     376         * @param string $byteword
     377         *
     378         * @return string
     379         */
    296380        public static function LittleEndian2Bin($byteword) {
    297381                return self::BigEndian2Bin(strrev($byteword));
    298382        }
    299383
     384        /**
     385         * @param string $byteword
     386         *
     387         * @return string
     388         */
    300389        public static function BigEndian2Bin($byteword) {
    301390                $binvalue = '';
    302391                $bytewordlen = strlen($byteword);
    303392                for ($i = 0; $i < $bytewordlen; $i++) {
    304                         $binvalue .= str_pad(decbin(ord($byteword{$i})), 8, '0', STR_PAD_LEFT);
     393                        $binvalue .= str_pad(decbin(ord($byteword[$i])), 8, '0', STR_PAD_LEFT);
    305394                }
    306395                return $binvalue;
    307396        }
    308397
    309 
     398        /**
     399         * @param int  $number
     400         * @param int  $minbytes
     401         * @param bool $synchsafe
     402         * @param bool $signed
     403         *
     404         * @return string
     405         * @throws Exception
     406         */
    310407        public static function BigEndian2String($number, $minbytes=1, $synchsafe=false, $signed=false) {
    311408                if ($number < 0) {
    … …  
    328425        }
    329426
    330 
     427        /**
     428         * @param int $number
     429         *
     430         * @return string
     431         */
    331432        public static function Dec2Bin($number) {
    332433                while ($number >= 256) {
    … …  
    342443        }
    343444
    344 
     445        /**
     446         * @param string $binstring
     447         * @param bool   $signed
     448         *
     449         * @return int|float
     450         */
    345451        public static function Bin2Dec($binstring, $signed=false) {
    346452                $signmult = 1;
    347453                if ($signed) {
    348                         if ($binstring{0} == '1') {
     454                        if ($binstring[0] == '1') {
    349455                                $signmult = -1;
    350456                        }
    … …  
    358464        }
    359465
    360 
     466        /**
     467         * @param string $binstring
     468         *
     469         * @return string
     470         */
    361471        public static function Bin2String($binstring) {
    362472                // return 'hi' for input of '0110100001101001'
    … …  
    369479        }
    370480
    371 
     481        /**
     482         * @param int  $number
     483         * @param int  $minbytes
     484         * @param bool $synchsafe
     485         *
     486         * @return string
     487         */
    372488        public static function LittleEndian2String($number, $minbytes=1, $synchsafe=false) {
    373489                $intstring = '';
    … …  
    384500        }
    385501
    386 
     502        /**
     503         * @param array $array1
     504         * @param array $array2
     505         *
     506         * @return array|false
     507         */
    387508        public static function array_merge_clobber($array1, $array2) {
    388509                // written by kcØhireability*com
    … …  
    402523        }
    403524
    404 
     525        /**
     526         * @param array $array1
     527         * @param array $array2
     528         *
     529         * @return array|false
     530         */
    405531        public static function array_merge_noclobber($array1, $array2) {
    406532                if (!is_array($array1) || !is_array($array2)) {
    … …  
    418544        }
    419545
     546        /**
     547         * @param array $array1
     548         * @param array $array2
     549         *
     550         * @return array|false|null
     551         */
    420552        public static function flipped_array_merge_noclobber($array1, $array2) {
    421553                if (!is_array($array1) || !is_array($array2)) {
    … …  
    432564        }
    433565
    434 
     566        /**
     567         * @param array $theArray
     568         *
     569         * @return bool
     570         */
    435571        public static function ksort_recursive(&$theArray) {
    436572                ksort($theArray);
    … …  
    443579        }
    444580
     581        /**
     582         * @param string $filename
     583         * @param int    $numextensions
     584         *
     585         * @return string
     586         */
    445587        public static function fileextension($filename, $numextensions=1) {
    446588                if (strstr($filename, '.')) {
    … …  
    458600        }
    459601
    460 
     602        /**
     603         * @param int $seconds
     604         *
     605         * @return string
     606         */
    461607        public static function PlaytimeString($seconds) {
    462608                $sign = (($seconds < 0) ? '-' : '');
    … …  
    468614        }
    469615
    470 
     616        /**
     617         * @param int $macdate
     618         *
     619         * @return int|float
     620         */
    471621        public static function DateMac2Unix($macdate) {
    472622                // Macintosh timestamp: seconds since 00:00h January 1, 1904
    … …  
    475625        }
    476626
    477 
     627        /**
     628         * @param string $rawdata
     629         *
     630         * @return float
     631         */
    478632        public static function FixedPoint8_8($rawdata) {
    479633                return self::BigEndian2Int(substr($rawdata, 0, 1)) + (float) (self::BigEndian2Int(substr($rawdata, 1, 1)) / pow(2, 8));
    480634        }
    481635
    482 
     636        /**
     637         * @param string $rawdata
     638         *
     639         * @return float
     640         */
    483641        public static function FixedPoint16_16($rawdata) {
    484642                return self::BigEndian2Int(substr($rawdata, 0, 2)) + (float) (self::BigEndian2Int(substr($rawdata, 2, 2)) / pow(2, 16));
    485643        }
    486644
    487 
     645        /**
     646         * @param string $rawdata
     647         *
     648         * @return float
     649         */
    488650        public static function FixedPoint2_30($rawdata) {
    489651                $binarystring = self::BigEndian2Bin($rawdata);
    … …  
    492654
    493655
     656        /**
     657         * @param string $ArrayPath
     658         * @param string $Separator
     659         * @param mixed $Value
     660         *
     661         * @return array
     662         */
    494663        public static function CreateDeepArray($ArrayPath, $Separator, $Value) {
    495664                // assigns $Value to a nested array path:
    … …  
    508677        }
    509678
     679        /**
     680         * @param array $arraydata
     681         * @param bool  $returnkey
     682         *
     683         * @return int|false
     684         */
    510685        public static function array_max($arraydata, $returnkey=false) {
    511686                $maxvalue = false;
    … …  
    522697        }
    523698
     699        /**
     700         * @param array $arraydata
     701         * @param bool  $returnkey
     702         *
     703         * @return int|false
     704         */
    524705        public static function array_min($arraydata, $returnkey=false) {
    525706                $minvalue = false;
    … …  
    536717        }
    537718
     719        /**
     720         * @param string $XMLstring
     721         *
     722         * @return array|false
     723         */
    538724        public static function XML2array($XMLstring) {
    539725                if (function_exists('simplexml_load_string') && function_exists('libxml_disable_entity_loader')) {
    … …  
    549735        }
    550736
     737        /**
     738        * @param SimpleXMLElement|array $XMLobject
     739        *
     740        * @return array
     741        */
    551742        public static function SimpleXMLelement2array($XMLobject) {
    552743                if (!is_object($XMLobject) && !is_array($XMLobject)) {
    553744                        return $XMLobject;
    554745                }
    555                 $XMLarray = (is_object($XMLobject) ? get_object_vars($XMLobject) : $XMLobject);
     746                $XMLarray = $XMLobject instanceof SimpleXMLElement ? get_object_vars($XMLobject) : $XMLobject;
    556747                foreach ($XMLarray as $key => $value) {
    557748                        $XMLarray[$key] = self::SimpleXMLelement2array($value);
    … …  
    560751        }
    561752
    562 
    563         // Allan Hansen <ahØartemis*dk>
    564         // self::md5_data() - returns md5sum for a file from startuing position to absolute end position
     753        /**
     754         * Returns checksum for a file from starting position to absolute end position.
     755         *
     756         * @param string $file
     757         * @param int    $offset
     758         * @param int    $end
     759         * @param string $algorithm
     760         *
     761         * @return string|false
     762         * @throws getid3_exception
     763         */
    565764        public static function hash_data($file, $offset, $end, $algorithm) {
    566                 static $tempdir = '';
    567765                if (!self::intValueSupported($end)) {
    568766                        return false;
    569767                }
    570                 switch ($algorithm) {
    571                         case 'md5':
    572                                 $hash_function = 'md5_file';
    573                                 $unix_call     = 'md5sum';
    574                                 $windows_call  = 'md5sum.exe';
    575                                 $hash_length   = 32;
    576                                 break;
    577 
    578                         case 'sha1':
    579                                 $hash_function = 'sha1_file';
    580                                 $unix_call     = 'sha1sum';
    581                                 $windows_call  = 'sha1sum.exe';
    582                                 $hash_length   = 40;
    583                                 break;
    584 
    585                         default:
    586                                 throw new Exception('Invalid algorithm ('.$algorithm.') in self::hash_data()');
    587                                 break;
    588                 }
     768                if (!in_array($algorithm, array('md5', 'sha1'))) {
     769                        throw new getid3_exception('Invalid algorithm ('.$algorithm.') in self::hash_data()');
     770                }
     771
    589772                $size = $end - $offset;
    590                 while (true) {
    591                         if (GETID3_OS_ISWINDOWS) {
    592 
    593                                 // It seems that sha1sum.exe for Windows only works on physical files, does not accept piped data
    594                                 // Fall back to create-temp-file method:
    595                                 if ($algorithm == 'sha1') {
    596                                         break;
    597                                 }
    598 
    599                                 $RequiredFiles = array('cygwin1.dll', 'head.exe', 'tail.exe', $windows_call);
    600                                 foreach ($RequiredFiles as $required_file) {
    601                                         if (!is_readable(GETID3_HELPERAPPSDIR.$required_file)) {
    602                                                 // helper apps not available - fall back to old method
    603                                                 break 2;
    604                                         }
    605                                 }
    606                                 $commandline  = GETID3_HELPERAPPSDIR.'head.exe -c '.$end.' '.escapeshellarg(str_replace('/', DIRECTORY_SEPARATOR, $file)).' | ';
    607                                 $commandline .= GETID3_HELPERAPPSDIR.'tail.exe -c '.$size.' | ';
    608                                 $commandline .= GETID3_HELPERAPPSDIR.$windows_call;
    609 
    610                         } else {
    611 
    612                                 $commandline  = 'head -c'.$end.' '.escapeshellarg($file).' | ';
    613                                 $commandline .= 'tail -c'.$size.' | ';
    614                                 $commandline .= $unix_call;
    615 
    616                         }
    617                         if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
    618                                 //throw new Exception('PHP running in Safe Mode - backtick operator not available, using slower non-system-call '.$algorithm.' algorithm');
    619                                 break;
    620                         }
    621                         return substr(`$commandline`, 0, $hash_length);
    622                 }
    623 
    624                 if (empty($tempdir)) {
    625                         // yes this is ugly, feel free to suggest a better way
    626                         require_once(dirname(__FILE__).'/getid3.php');
    627                         $getid3_temp = new getID3();
    628                         $tempdir = $getid3_temp->tempdir;
    629                         unset($getid3_temp);
    630                 }
    631                 // try to create a temporary file in the system temp directory - invalid dirname should force to system temp dir
    632                 if (($data_filename = tempnam($tempdir, 'gI3')) === false) {
    633                         // can't find anywhere to create a temp file, just fail
    634                         return false;
    635                 }
    636 
    637                 // Init
    638                 $result = false;
    639 
    640                 // copy parts of file
    641                 try {
    642                         self::CopyFileParts($file, $data_filename, $offset, $end - $offset);
    643                         $result = $hash_function($data_filename);
    644                 } catch (Exception $e) {
    645                         throw new Exception('self::CopyFileParts() failed in getid_lib::hash_data(): '.$e->getMessage());
    646                 }
    647                 unlink($data_filename);
    648                 return $result;
    649         }
    650 
     773
     774                $fp = fopen($file, 'rb');
     775                fseek($fp, $offset);
     776                $ctx = hash_init($algorithm);
     777                while ($size > 0) {
     778                        $buffer = fread($fp, min($size, getID3::FREAD_BUFFER_SIZE));
     779                        hash_update($ctx, $buffer);
     780                        $size -= getID3::FREAD_BUFFER_SIZE;
     781                }
     782                $hash = hash_final($ctx);
     783                fclose($fp);
     784
     785                return $hash;
     786        }
     787
     788        /**
     789         * @param string $filename_source
     790         * @param string $filename_dest
     791         * @param int    $offset
     792         * @param int    $length
     793         *
     794         * @return bool
     795         * @throws Exception
     796         *
     797         * @deprecated Unused, may be removed in future versions of getID3
     798         */
    651799        public static function CopyFileParts($filename_source, $filename_dest, $offset, $length) {
    652800                if (!self::intValueSupported($offset + $length)) {
    … …  
    661809                                                $byteslefttowrite -= $byteswritten;
    662810                                        }
     811                                        fclose($fp_dest);
    663812                                        return true;
    664813                                } else {
     814                                        fclose($fp_src);
    665815                                        throw new Exception('failed to seek to offset '.$offset.' in '.$filename_source);
    666816                                }
    667                                 fclose($fp_dest);
    668817                        } else {
    669818                                throw new Exception('failed to create file for writing '.$filename_dest);
    670819                        }
    671                         fclose($fp_src);
    672820                } else {
    673821                        throw new Exception('failed to open file for reading '.$filename_source);
    674822                }
    675                 return false;
    676         }
    677 
     823        }
     824
     825        /**
     826         * @param int $charval
     827         *
     828         * @return string
     829         */
    678830        public static function iconv_fallback_int_utf8($charval) {
    679831                if ($charval < 128) {
    … …  
    699851        }
    700852
    701         // ISO-8859-1 => UTF-8
     853        /**
     854         * ISO-8859-1 => UTF-8
     855         *
     856         * @param string $string
     857         * @param bool   $bom
     858         *
     859         * @return string
     860         */
    702861        public static function iconv_fallback_iso88591_utf8($string, $bom=false) {
    703862                if (function_exists('utf8_encode')) {
    … …  
    710869                }
    711870                for ($i = 0; $i < strlen($string); $i++) {
    712                         $charval = ord($string{$i});
     871                        $charval = ord($string[$i]);
    713872                        $newcharstring .= self::iconv_fallback_int_utf8($charval);
    714873                }
    … …  
    716875        }
    717876
    718         // ISO-8859-1 => UTF-16BE
     877        /**
     878         * ISO-8859-1 => UTF-16BE
     879         *
     880         * @param string $string
     881         * @param bool   $bom
     882         *
     883         * @return string
     884         */
    719885        public static function iconv_fallback_iso88591_utf16be($string, $bom=false) {
    720886                $newcharstring = '';
    … …  
    723889                }
    724890                for ($i = 0; $i < strlen($string); $i++) {
    725                         $newcharstring .= "\x00".$string{$i};
     891                        $newcharstring .= "\x00".$string[$i];
    726892                }
    727893                return $newcharstring;
    728894        }
    729895
    730         // ISO-8859-1 => UTF-16LE
     896        /**
     897         * ISO-8859-1 => UTF-16LE
     898         *
     899         * @param string $string
     900         * @param bool   $bom
     901         *
     902         * @return string
     903         */
    731904        public static function iconv_fallback_iso88591_utf16le($string, $bom=false) {
    732905                $newcharstring = '';
    … …  
    735908                }
    736909                for ($i = 0; $i < strlen($string); $i++) {
    737                         $newcharstring .= $string{$i}."\x00";
     910                        $newcharstring .= $string[$i]."\x00";
    738911                }
    739912                return $newcharstring;
    740913        }
    741914
    742         // ISO-8859-1 => UTF-16LE (BOM)
     915        /**
     916         * ISO-8859-1 => UTF-16LE (BOM)
     917         *
     918         * @param string $string
     919         *
     920         * @return string
     921         */
    743922        public static function iconv_fallback_iso88591_utf16($string) {
    744923                return self::iconv_fallback_iso88591_utf16le($string, true);
    745924        }
    746925
    747         // UTF-8 => ISO-8859-1
     926        /**
     927         * UTF-8 => ISO-8859-1
     928         *
     929         * @param string $string
     930         *
     931         * @return string
     932         */
    748933        public static function iconv_fallback_utf8_iso88591($string) {
    749934                if (function_exists('utf8_decode')) {
    … …  
    755940                $stringlength = strlen($string);
    756941                while ($offset < $stringlength) {
    757                         if ((ord($string{$offset}) | 0x07) == 0xF7) {
     942                        if ((ord($string[$offset]) | 0x07) == 0xF7) {
    758943                                // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
    759                                 $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
    760                                                    ((ord($string{($offset + 1)}) & 0x3F) << 12) &
    761                                                    ((ord($string{($offset + 2)}) & 0x3F) <<  6) &
    762                                                         (ord($string{($offset + 3)}) & 0x3F);
     944                                $charval = ((ord($string[($offset + 0)]) & 0x07) << 18) &
     945                                                   ((ord($string[($offset + 1)]) & 0x3F) << 12) &
     946                                                   ((ord($string[($offset + 2)]) & 0x3F) <<  6) &
     947                                                        (ord($string[($offset + 3)]) & 0x3F);
    763948                                $offset += 4;
    764                         } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
     949                        } elseif ((ord($string[$offset]) | 0x0F) == 0xEF) {
    765950                                // 1110bbbb 10bbbbbb 10bbbbbb
    766                                 $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
    767                                                    ((ord($string{($offset + 1)}) & 0x3F) <<  6) &
    768                                                         (ord($string{($offset + 2)}) & 0x3F);
     951                                $charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) &
     952                                                   ((ord($string[($offset + 1)]) & 0x3F) <<  6) &
     953                                                        (ord($string[($offset + 2)]) & 0x3F);
    769954                                $offset += 3;
    770                         } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
     955                        } elseif ((ord($string[$offset]) | 0x1F) == 0xDF) {
    771956                                // 110bbbbb 10bbbbbb
    772                                 $charval = ((ord($string{($offset + 0)}) & 0x1F) <<  6) &
    773                                                         (ord($string{($offset + 1)}) & 0x3F);
     957                                $charval = ((ord($string[($offset + 0)]) & 0x1F) <<  6) &
     958                                                        (ord($string[($offset + 1)]) & 0x3F);
    774959                                $offset += 2;
    775                         } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
     960                        } elseif ((ord($string[$offset]) | 0x7F) == 0x7F) {
    776961                                // 0bbbbbbb
    777                                 $charval = ord($string{$offset});
     962                                $charval = ord($string[$offset]);
    778963                                $offset += 1;
    779964                        } else {
    … …  
    789974        }
    790975
    791         // UTF-8 => UTF-16BE
     976        /**
     977         * UTF-8 => UTF-16BE
     978         *
     979         * @param string $string
     980         * @param bool   $bom
     981         *
     982         * @return string
     983         */
    792984        public static function iconv_fallback_utf8_utf16be($string, $bom=false) {
    793985                $newcharstring = '';
    … …  
    798990                $stringlength = strlen($string);
    799991                while ($offset < $stringlength) {
    800                         if ((ord($string{$offset}) | 0x07) == 0xF7) {
     992                        if ((ord($string[$offset]) | 0x07) == 0xF7) {
    801993                                // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
    802                                 $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
    803                                                    ((ord($string{($offset + 1)}) & 0x3F) << 12) &
    804                                                    ((ord($string{($offset + 2)}) & 0x3F) <<  6) &
    805                                                         (ord($string{($offset + 3)}) & 0x3F);
     994                                $charval = ((ord($string[($offset + 0)]) & 0x07) << 18) &
     995                                                   ((ord($string[($offset + 1)]) & 0x3F) << 12) &
     996                                                   ((ord($string[($offset + 2)]) & 0x3F) <<  6) &
     997                                                        (ord($string[($offset + 3)]) & 0x3F);
    806998                                $offset += 4;
    807                         } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
     999                        } elseif ((ord($string[$offset]) | 0x0F) == 0xEF) {
    8081000                                // 1110bbbb 10bbbbbb 10bbbbbb
    809                                 $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
    810                                                    ((ord($string{($offset + 1)}) & 0x3F) <<  6) &
    811                                                         (ord($string{($offset + 2)}) & 0x3F);
     1001                                $charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) &
     1002                                                   ((ord($string[($offset + 1)]) & 0x3F) <<  6) &
     1003                                                        (ord($string[($offset + 2)]) & 0x3F);
    8121004                                $offset += 3;
    813                         } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
     1005                        } elseif ((ord($string[$offset]) | 0x1F) == 0xDF) {
    8141006                                // 110bbbbb 10bbbbbb
    815                                 $charval = ((ord($string{($offset + 0)}) & 0x1F) <<  6) &
    816                                                         (ord($string{($offset + 1)}) & 0x3F);
     1007                                $charval = ((ord($string[($offset + 0)]) & 0x1F) <<  6) &
     1008                                                        (ord($string[($offset + 1)]) & 0x3F);
    8171009                                $offset += 2;
    818                         } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
     1010                        } elseif ((ord($string[$offset]) | 0x7F) == 0x7F) {
    8191011                                // 0bbbbbbb
    820                                 $charval = ord($string{$offset});
     1012                                $charval = ord($string[$offset]);
    8211013                                $offset += 1;
    8221014                        } else {
    … …  
    8321024        }
    8331025
    834         // UTF-8 => UTF-16LE
     1026        /**
     1027         * UTF-8 => UTF-16LE
     1028         *
     1029         * @param string $string
     1030         * @param bool   $bom
     1031         *
     1032         * @return string
     1033         */
    8351034        public static function iconv_fallback_utf8_utf16le($string, $bom=false) {
    8361035                $newcharstring = '';
    … …  
    8411040                $stringlength = strlen($string);
    8421041                while ($offset < $stringlength) {
    843                         if ((ord($string{$offset}) | 0x07) == 0xF7) {
     1042                        if ((ord($string[$offset]) | 0x07) == 0xF7) {
    8441043                                // 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
    845                                 $charval = ((ord($string{($offset + 0)}) & 0x07) << 18) &
    846                                                    ((ord($string{($offset + 1)}) & 0x3F) << 12) &
    847                                                    ((ord($string{($offset + 2)}) & 0x3F) <<  6) &
    848                                                         (ord($string{($offset + 3)}) & 0x3F);
     1044                                $charval = ((ord($string[($offset + 0)]) & 0x07) << 18) &
     1045                                                   ((ord($string[($offset + 1)]) & 0x3F) << 12) &
     1046                                                   ((ord($string[($offset + 2)]) & 0x3F) <<  6) &
     1047                                                        (ord($string[($offset + 3)]) & 0x3F);
    8491048                                $offset += 4;
    850                         } elseif ((ord($string{$offset}) | 0x0F) == 0xEF) {
     1049                        } elseif ((ord($string[$offset]) | 0x0F) == 0xEF) {
    8511050                                // 1110bbbb 10bbbbbb 10bbbbbb
    852                                 $charval = ((ord($string{($offset + 0)}) & 0x0F) << 12) &
    853                                                    ((ord($string{($offset + 1)}) & 0x3F) <<  6) &
    854                                                         (ord($string{($offset + 2)}) & 0x3F);
     1051                                $charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) &
     1052                                                   ((ord($string[($offset + 1)]) & 0x3F) <<  6) &
     1053                                                        (ord($string[($offset + 2)]) & 0x3F);
    8551054                                $offset += 3;
    856                         } elseif ((ord($string{$offset}) | 0x1F) == 0xDF) {
     1055                        } elseif ((ord($string[$offset]) | 0x1F) == 0xDF) {
    8571056                                // 110bbbbb 10bbbbbb
    858                                 $charval = ((ord($string{($offset + 0)}) & 0x1F) <<  6) &
    859                                                         (ord($string{($offset + 1)}) & 0x3F);
     1057                                $charval = ((ord($string[($offset + 0)]) & 0x1F) <<  6) &
     1058                                                        (ord($string[($offset + 1)]) & 0x3F);
    8601059                                $offset += 2;
    861                         } elseif ((ord($string{$offset}) | 0x7F) == 0x7F) {
     1060                        } elseif ((ord($string[$offset]) | 0x7F) == 0x7F) {
    8621061                                // 0bbbbbbb
    863                                 $charval = ord($string{$offset});
     1062                                $charval = ord($string[$offset]);
    8641063                                $offset += 1;
    8651064                        } else {
    … …  
    8751074        }
    8761075
    877         // UTF-8 => UTF-16LE (BOM)
     1076        /**
     1077         * UTF-8 => UTF-16LE (BOM)
     1078         *
     1079         * @param string $string
     1080         *
     1081         * @return string
     1082         */
    8781083        public static function iconv_fallback_utf8_utf16($string) {
    8791084                return self::iconv_fallback_utf8_utf16le($string, true);
    8801085        }
    8811086
    882         // UTF-16BE => UTF-8
     1087        /**
     1088         * UTF-16BE => UTF-8
     1089         *
     1090         * @param string $string
     1091         *
     1092         * @return string
     1093         */
    8831094        public static function iconv_fallback_utf16be_utf8($string) {
    8841095                if (substr($string, 0, 2) == "\xFE\xFF") {
    … …  
    8941105        }
    8951106
    896         // UTF-16LE => UTF-8
     1107        /**
     1108         * UTF-16LE => UTF-8
     1109         *
     1110         * @param string $string
     1111         *
     1112         * @return string
     1113         */
    8971114        public static function iconv_fallback_utf16le_utf8($string) {
    8981115                if (substr($string, 0, 2) == "\xFF\xFE") {
    … …  
    9081125        }
    9091126
    910         // UTF-16BE => ISO-8859-1
     1127        /**
     1128         * UTF-16BE => ISO-8859-1
     1129         *
     1130         * @param string $string
     1131         *
     1132         * @return string
     1133         */
    9111134        public static function iconv_fallback_utf16be_iso88591($string) {
    9121135                if (substr($string, 0, 2) == "\xFE\xFF") {
    … …  
    9221145        }
    9231146
    924         // UTF-16LE => ISO-8859-1
     1147        /**
     1148         * UTF-16LE => ISO-8859-1
     1149         *
     1150         * @param string $string
     1151         *
     1152         * @return string
     1153         */
    9251154        public static function iconv_fallback_utf16le_iso88591($string) {
    9261155                if (substr($string, 0, 2) == "\xFF\xFE") {
    … …  
    9361165        }
    9371166
    938         // UTF-16 (BOM) => ISO-8859-1
     1167        /**
     1168         * UTF-16 (BOM) => ISO-8859-1
     1169         *
     1170         * @param string $string
     1171         *
     1172         * @return string
     1173         */
    9391174        public static function iconv_fallback_utf16_iso88591($string) {
    9401175                $bom = substr($string, 0, 2);
    … …  
    9471182        }
    9481183
    949         // UTF-16 (BOM) => UTF-8
     1184        /**
     1185         * UTF-16 (BOM) => UTF-8
     1186         *
     1187         * @param string $string
     1188         *
     1189         * @return string
     1190         */
    9501191        public static function iconv_fallback_utf16_utf8($string) {
    9511192                $bom = substr($string, 0, 2);
    … …  
    9581199        }
    9591200
     1201        /**
     1202         * @param string $in_charset
     1203         * @param string $out_charset
     1204         * @param string $string
     1205         *
     1206         * @return string
     1207         * @throws Exception
     1208         */
    9601209        public static function iconv_fallback($in_charset, $out_charset, $string) {
    9611210
    … …  
    9641213                }
    9651214
    966                 // mb_convert_encoding() availble
     1215                // mb_convert_encoding() available
    9671216                if (function_exists('mb_convert_encoding')) {
     1217                        if ((strtoupper($in_charset) == 'UTF-16') && (substr($string, 0, 2) != "\xFE\xFF") && (substr($string, 0, 2) != "\xFF\xFE")) {
     1218                                // if BOM missing, mb_convert_encoding will mishandle the conversion, assume UTF-16BE and prepend appropriate BOM
     1219                                $string = "\xFF\xFE".$string;
     1220                        }
     1221                        if ((strtoupper($in_charset) == 'UTF-16') && (strtoupper($out_charset) == 'UTF-8')) {
     1222                                if (($string == "\xFF\xFE") || ($string == "\xFE\xFF")) {
     1223                                        // if string consists of only BOM, mb_convert_encoding will return the BOM unmodified
     1224                                        return '';
     1225                                }
     1226                        }
    9681227                        if ($converted_string = @mb_convert_encoding($string, $out_charset, $in_charset)) {
    9691228                                switch ($out_charset) {
    … …  
    9751234                        }
    9761235                        return $string;
    977                 }
    978                 // iconv() availble
    979                 else if (function_exists('iconv')) {
     1236
     1237                // iconv() available
     1238                } elseif (function_exists('iconv')) {
    9801239                        if ($converted_string = @iconv($in_charset, $out_charset.'//TRANSLIT', $string)) {
    9811240                                switch ($out_charset) {
    … …  
    10181277        }
    10191278
     1279        /**
     1280         * @param mixed  $data
     1281         * @param string $charset
     1282         *
     1283         * @return mixed
     1284         */
    10201285        public static function recursiveMultiByteCharString2HTML($data, $charset='ISO-8859-1') {
    10211286                if (is_string($data)) {
    … …  
    10321297        }
    10331298
     1299        /**
     1300         * @param string|int|float $string
     1301         * @param string           $charset
     1302         *
     1303         * @return string
     1304         */
    10341305        public static function MultiByteCharString2HTML($string, $charset='ISO-8859-1') {
    10351306                $string = (string) $string; // in case trying to pass a numeric (float, int) string, would otherwise return an empty string
    … …  
    10701341                                $strlen = strlen($string);
    10711342                                for ($i = 0; $i < $strlen; $i++) {
    1072                                         $char_ord_val = ord($string{$i});
     1343                                        $char_ord_val = ord($string[$i]);
    10731344                                        $charval = 0;
    10741345                                        if ($char_ord_val < 0x80) {
    … …  
    10761347                                        } elseif ((($char_ord_val & 0xF0) >> 4) == 0x0F  &&  $i+3 < $strlen) {
    10771348                                                $charval  = (($char_ord_val & 0x07) << 18);
    1078                                                 $charval += ((ord($string{++$i}) & 0x3F) << 12);
    1079                                                 $charval += ((ord($string{++$i}) & 0x3F) << 6);
    1080                                                 $charval +=  (ord($string{++$i}) & 0x3F);
     1349                                                $charval += ((ord($string[++$i]) & 0x3F) << 12);
     1350                                                $charval += ((ord($string[++$i]) & 0x3F) << 6);
     1351                                                $charval +=  (ord($string[++$i]) & 0x3F);
    10811352                                        } elseif ((($char_ord_val & 0xE0) >> 5) == 0x07  &&  $i+2 < $strlen) {
    10821353                                                $charval  = (($char_ord_val & 0x0F) << 12);
    1083                                                 $charval += ((ord($string{++$i}) & 0x3F) << 6);
    1084                                                 $charval +=  (ord($string{++$i}) & 0x3F);
     1354                                                $charval += ((ord($string[++$i]) & 0x3F) << 6);
     1355                                                $charval +=  (ord($string[++$i]) & 0x3F);
    10851356                                        } elseif ((($char_ord_val & 0xC0) >> 6) == 0x03  &&  $i+1 < $strlen) {
    10861357                                                $charval  = (($char_ord_val & 0x1F) << 6);
    1087                                                 $charval += (ord($string{++$i}) & 0x3F);
     1358                                                $charval += (ord($string[++$i]) & 0x3F);
    10881359                                        }
    10891360                                        if (($charval >= 32) && ($charval <= 127)) {
    … …  
    11241395        }
    11251396
    1126 
    1127 
     1397        /**
     1398         * @param int $namecode
     1399         *
     1400         * @return string
     1401         */
    11281402        public static function RGADnameLookup($namecode) {
    11291403                static $RGADname = array();
    … …  
    11371411        }
    11381412
    1139 
     1413        /**
     1414         * @param int $originatorcode
     1415         *
     1416         * @return string
     1417         */
    11401418        public static function RGADoriginatorLookup($originatorcode) {
    11411419                static $RGADoriginator = array();
    … …  
    11501428        }
    11511429
    1152 
     1430        /**
     1431         * @param int $rawadjustment
     1432         * @param int $signbit
     1433         *
     1434         * @return float
     1435         */
    11531436        public static function RGADadjustmentLookup($rawadjustment, $signbit) {
    1154                 $adjustment = $rawadjustment / 10;
     1437                $adjustment = (float) $rawadjustment / 10;
    11551438                if ($signbit == 1) {
    11561439                        $adjustment *= -1;
    11571440                }
    1158                 return (float) $adjustment;
    1159         }
    1160 
    1161 
     1441                return $adjustment;
     1442        }
     1443
     1444        /**
     1445         * @param int $namecode
     1446         * @param int $originatorcode
     1447         * @param int $replaygain
     1448         *
     1449         * @return string
     1450         */
    11621451        public static function RGADgainString($namecode, $originatorcode, $replaygain) {
    11631452                if ($replaygain < 0) {
    … …  
    11751464        }
    11761465
     1466        /**
     1467         * @param float $amplitude
     1468         *
     1469         * @return float
     1470         */
    11771471        public static function RGADamplitude2dB($amplitude) {
    11781472                return 20 * log10($amplitude);
    11791473        }
    11801474
    1181 
     1475        /**
     1476         * @param string $imgData
     1477         * @param array  $imageinfo
     1478         *
     1479         * @return array|false
     1480         */
    11821481        public static function GetDataImageSize($imgData, &$imageinfo=array()) {
    11831482                static $tempdir = '';
    … …  
    12141513        }
    12151514
     1515        /**
     1516         * @param string $mime_type
     1517         *
     1518         * @return string
     1519         */
    12161520        public static function ImageExtFromMime($mime_type) {
    12171521                // temporary way, works OK for now, but should be reworked in the future
    … …  
    12191523        }
    12201524
    1221         public static function ImageTypesLookup($imagetypeid) {
    1222                 static $ImageTypesLookup = array();
    1223                 if (empty($ImageTypesLookup)) {
    1224                         $ImageTypesLookup[1]  = 'gif';
    1225                         $ImageTypesLookup[2]  = 'jpeg';
    1226                         $ImageTypesLookup[3]  = 'png';
    1227                         $ImageTypesLookup[4]  = 'swf';
    1228                         $ImageTypesLookup[5]  = 'psd';
    1229                         $ImageTypesLookup[6]  = 'bmp';
    1230                         $ImageTypesLookup[7]  = 'tiff (little-endian)';
    1231                         $ImageTypesLookup[8]  = 'tiff (big-endian)';
    1232                         $ImageTypesLookup[9]  = 'jpc';
    1233                         $ImageTypesLookup[10] = 'jp2';
    1234                         $ImageTypesLookup[11] = 'jpx';
    1235                         $ImageTypesLookup[12] = 'jb2';
    1236                         $ImageTypesLookup[13] = 'swc';
    1237                         $ImageTypesLookup[14] = 'iff';
    1238                 }
    1239                 return (isset($ImageTypesLookup[$imagetypeid]) ? $ImageTypesLookup[$imagetypeid] : '');
    1240         }
    1241 
     1525        /**
     1526         * @param array $ThisFileInfo
     1527         *
     1528         * @return bool
     1529         */
    12421530        public static function CopyTagsToComments(&$ThisFileInfo) {
    12431531
    … …  
    13271615        }
    13281616
    1329 
     1617        /**
     1618         * @param string $key
     1619         * @param int    $begin
     1620         * @param int    $end
     1621         * @param string $file
     1622         * @param string $name
     1623         *
     1624         * @return string
     1625         */
    13301626        public static function EmbeddedLookup($key, $begin, $end, $file, $name) {
    13311627
    … …  
    13741670        }
    13751671
     1672        /**
     1673         * @param string $filename
     1674         * @param string $sourcefile
     1675         * @param bool   $DieOnFailure
     1676         *
     1677         * @return bool
     1678         * @throws Exception
     1679         */
    13761680        public static function IncludeDependency($filename, $sourcefile, $DieOnFailure=false) {
    13771681                global $GETID3_ERRORARRAY;
    … …  
    13941698        }
    13951699
     1700        /**
     1701         * @param string $string
     1702         *
     1703         * @return string
     1704         */
    13961705        public static function trimNullByte($string) {
    13971706                return trim($string, "\x00");
    13981707        }
    13991708
     1709        /**
     1710         * @param string $path
     1711         *
     1712         * @return float|bool
     1713         */
    14001714        public static function getFileSizeSyscall($path) {
    14011715                $filesize = false;
    … …  
    14221736        }
    14231737
    1424 
    1425         /**
    1426         * Workaround for Bug #37268 (https://bugs.php.net/bug.php?id=37268)
    1427         * @param string $path A path.
    1428         * @param string $suffix If the name component ends in suffix this will also be cut off.
    1429         * @return string
    1430         */
     1738        /**
     1739         * @param string $filename
     1740         *
     1741         * @return string|false
     1742         */
     1743        public static function truepath($filename) {
     1744                // 2017-11-08: this could use some improvement, patches welcome
     1745                if (preg_match('#^(\\\\\\\\|//)[a-z0-9]#i', $filename, $matches)) {
     1746                        // PHP's built-in realpath function does not work on UNC Windows shares
     1747                        $goodpath = array();
     1748                        foreach (explode('/', str_replace('\\', '/', $filename)) as $part) {
     1749                                if ($part == '.') {
     1750                                        continue;
     1751                                }
     1752                                if ($part == '..') {
     1753                                        if (count($goodpath)) {
     1754                                                array_pop($goodpath);
     1755                                        } else {
     1756                                                // cannot step above this level, already at top level
     1757                                                return false;
     1758                                        }
     1759                                } else {
     1760                                        $goodpath[] = $part;
     1761                                }
     1762                        }
     1763                        return implode(DIRECTORY_SEPARATOR, $goodpath);
     1764                }
     1765                return realpath($filename);
     1766        }
     1767
     1768        /**
     1769         * Workaround for Bug #37268 (https://bugs.php.net/bug.php?id=37268)
     1770         *
     1771         * @param string $path A path.
     1772         * @param string $suffix If the name component ends in suffix this will also be cut off.
     1773         *
     1774         * @return string
     1775         */
    14311776        public static function mb_basename($path, $suffix = null) {
    14321777                $splited = preg_split('#/#', rtrim($path, '/ '));
Note: See TracChangeset for help on using the changeset viewer.