Skip to content

Commit

Permalink
Merge branch 'feature/upgrade-getid3'
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-xo committed Apr 18, 2022
2 parents dcc7739 + 8387182 commit 90d95f0
Show file tree
Hide file tree
Showing 13 changed files with 647 additions and 523 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Changelog
=========

1.32 2022-04-18 * Upgrade getID3 (includes important security fix for PHP 8+)

1.31 2021-12-30 * Some fixes for future deprecation warnings as we approach PHP 8.2
* Debugging options and more tests
* Podcast will now refresh if you update any of the feed metadata
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![Testing dir2cast](https://github.com/ben-xo/dir2cast/actions/workflows/testing.yml/badge.svg)](https://github.com/ben-xo/dir2cast/actions/workflows/testing.yml)

dir2cast by Ben XO v1.31 (2021-12-30)
dir2cast by Ben XO v1.32 (2022-04-18)
================================================================================

https://github.com/ben-xo/dir2cast/
Expand Down
36 changes: 3 additions & 33 deletions dir2cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
/* DEFAULTS *********************************************/

// error handler needs these, so let's set them now.
define('VERSION', '1.31');
define('VERSION', '1.32');
define('DIR2CAST_HOMEPAGE', 'https://github.com/ben-xo/dir2cast/');
define('GENERATOR', 'dir2cast ' . VERSION . ' by Ben XO (' . DIR2CAST_HOMEPAGE . ')');

Expand Down Expand Up @@ -130,27 +130,6 @@ public function appendToItem(DOMElement $d, DOMDocument $doc, RSS_Item $item);
public function addNamespaceTo(DOMElement $d, DOMDocument $doc);
}

class QuicktimeImageHelper {
public function findCover($info) {

if(!is_array($info)) return false;

// search for covr atom
if($info['name'] == 'covr') return $info;

if(isset($info['subatoms']) && is_array($info['subatoms'])) {
foreach($info['subatoms'] as $subatom) {
$cover = $this->findCover($subatom);
if($cover !== false) {
return $cover;
}
}
}

return false;
}
}

/**
* Uses external getID3 lib to analyze MP3 files.
*
Expand Down Expand Up @@ -202,29 +181,20 @@ public function appendToItem(DOMElement $d, DOMDocument $doc, RSS_Item $item)

if(self::$AUTO_SAVE_COVER_ART)
{
// this works for MP3s
if(!empty($info['comments']['picture'][0]))
{
$item->saveImage(
$info['comments']['picture'][0]['image_mime'],
$info['comments']['picture'][0]['data']
);
}

// this works for m4a and mp4
elseif(!empty($info['quicktime']))
{
$qt_helper = new QuicktimeImageHelper();
$image_atom = $qt_helper->findCover($info['quicktime']['moov']);
if($image_atom && isset($image_atom['image_mime']) && isset($image_atom['data'])) {
$item->saveImage($image_atom['image_mime'], $image_atom['data']);
}
}
}
}

if(!empty($info['playtime_string']))
$item->setDuration( $info['playtime_string'] );
else
$item->setDuration('0:00');

$item->setAnalyzed(true);
}
Expand Down
108 changes: 69 additions & 39 deletions getID3/getid3.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
// ///
/////////////////////////////////////////////////////////////////

if(!defined('GETID3_LIBXML_OPTIONS') && defined('LIBXML_VERSION')) {
if(LIBXML_VERSION >= 20621) {
define('GETID3_LIBXML_OPTIONS', LIBXML_NOENT | LIBXML_NONET | LIBXML_NOWARNING | LIBXML_COMPACT);
} else {
define('GETID3_LIBXML_OPTIONS', LIBXML_NOENT | LIBXML_NONET | LIBXML_NOWARNING);
}
}

class getid3_lib
{
Expand Down Expand Up @@ -242,7 +249,7 @@ public static function LittleEndian2Float($byteword) {
/**
* ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic
*
* @link http://www.psc.edu/general/software/packages/ieee/ieee.html
* @link https://web.archive.org/web/20120325162206/http://www.psc.edu/general/software/packages/ieee/ieee.php
* @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html
*
* @param string $byteword
Expand Down Expand Up @@ -294,20 +301,19 @@ public static function BigEndian2Float($byteword) {

if (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction != 0)) {
// Not a Number
$floatvalue = false;
$floatvalue = NAN;
} elseif (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction == 0)) {
if ($signbit == '1') {
$floatvalue = '-infinity';
$floatvalue = -INF;
} else {
$floatvalue = '+infinity';
$floatvalue = INF;
}
} elseif (($exponent == 0) && ($fraction == 0)) {
if ($signbit == '1') {
$floatvalue = -0;
$floatvalue = -0.0;
} else {
$floatvalue = 0;
$floatvalue = 0.0;
}
$floatvalue = ($signbit ? 0 : -0);
} elseif (($exponent == 0) && ($fraction != 0)) {
// These are 'unnormalized' values
$floatvalue = pow(2, (-1 * (pow(2, $exponentbits - 1) - 2))) * self::DecimalBinary2Float($fractionstring);
Expand Down Expand Up @@ -427,14 +433,20 @@ public static function BigEndian2String($number, $minbytes=1, $synchsafe=false,
* @return string
*/
public static function Dec2Bin($number) {
if (!is_numeric($number)) {
// https://github.com/JamesHeinrich/getID3/issues/299
trigger_error('TypeError: Dec2Bin(): Argument #1 ($number) must be numeric, '.gettype($number).' given', E_USER_WARNING);
return '';
}
$bytes = array();
while ($number >= 256) {
$bytes[] = (($number / 256) - (floor($number / 256))) * 256;
$bytes[] = (int) (($number / 256) - (floor($number / 256))) * 256;
$number = floor($number / 256);
}
$bytes[] = $number;
$bytes[] = (int) $number;
$binstring = '';
for ($i = 0; $i < count($bytes); $i++) {
$binstring = (($i == count($bytes) - 1) ? decbin($bytes[$i]) : str_pad(decbin($bytes[$i]), 8, '0', STR_PAD_LEFT)).$binstring;
foreach ($bytes as $i => $byte) {
$binstring = (($i == count($bytes) - 1) ? decbin($byte) : str_pad(decbin($byte), 8, '0', STR_PAD_LEFT)).$binstring;
}
return $binstring;
}
Expand Down Expand Up @@ -665,6 +677,7 @@ public static function CreateDeepArray($ArrayPath, $Separator, $Value) {
// or
// $foo['path']['to']['my'] = 'file.txt';
$ArrayPath = ltrim($ArrayPath, $Separator);
$ReturnedArray = array();
if (($pos = strpos($ArrayPath, $Separator)) !== false) {
$ReturnedArray[substr($ArrayPath, 0, $pos)] = self::CreateDeepArray(substr($ArrayPath, $pos + 1), $Separator, $Value);
} else {
Expand Down Expand Up @@ -720,18 +733,14 @@ public static function array_min($arraydata, $returnkey=false) {
*/
public static function XML2array($XMLstring) {
if (function_exists('simplexml_load_string') && function_exists('libxml_disable_entity_loader')) {
if (PHP_VERSION_ID < 80000) {
// http://websec.io/2012/08/27/Preventing-XEE-in-PHP.html
// https://core.trac.wordpress.org/changeset/29378
// This function has been deprecated in PHP 8.0 because in libxml 2.9.0, external entity loading is
// disabled by default, so this function is no longer needed to protect against XXE attacks.
$loader = libxml_disable_entity_loader(true);
}
$XMLobject = simplexml_load_string($XMLstring, 'SimpleXMLElement', LIBXML_NOENT);
// http://websec.io/2012/08/27/Preventing-XEE-in-PHP.html
// https://core.trac.wordpress.org/changeset/29378
// This function has been deprecated in PHP 8.0 because in libxml 2.9.0, external entity loading is
// disabled by default, but is still needed when LIBXML_NOENT is used.
$loader = @libxml_disable_entity_loader(true);
$XMLobject = simplexml_load_string($XMLstring, 'SimpleXMLElement', GETID3_LIBXML_OPTIONS);
$return = self::SimpleXMLelement2array($XMLobject);
if (PHP_VERSION_ID < 80000 && isset($loader)) {
libxml_disable_entity_loader($loader);
}
@libxml_disable_entity_loader($loader);
return $return;
}
return false;
Expand Down Expand Up @@ -1542,12 +1551,21 @@ public static function ImageExtFromMime($mime_type) {
public static function CopyTagsToComments(&$ThisFileInfo, $option_tags_html=true) {
// Copy all entries from ['tags'] into common ['comments']
if (!empty($ThisFileInfo['tags'])) {
if (isset($ThisFileInfo['tags']['id3v1'])) {
// bubble ID3v1 to the end, if present to aid in detecting bad ID3v1 encodings
$ID3v1 = $ThisFileInfo['tags']['id3v1'];
unset($ThisFileInfo['tags']['id3v1']);
$ThisFileInfo['tags']['id3v1'] = $ID3v1;
unset($ID3v1);

// Some tag types can only support limited character sets and may contain data in non-standard encoding (usually ID3v1)
// and/or poorly-transliterated tag values that are also in tag formats that do support full-range character sets
// To make the output more user-friendly, process the potentially-problematic tag formats last to enhance the chance that
// the first entries in [comments] are the most correct and the "bad" ones (if any) come later.
// https://github.com/JamesHeinrich/getID3/issues/338
$processLastTagTypes = array('id3v1','riff');
foreach ($processLastTagTypes as $processLastTagType) {
if (isset($ThisFileInfo['tags'][$processLastTagType])) {
// bubble ID3v1 to the end, if present to aid in detecting bad ID3v1 encodings
$temp = $ThisFileInfo['tags'][$processLastTagType];
unset($ThisFileInfo['tags'][$processLastTagType]);
$ThisFileInfo['tags'][$processLastTagType] = $temp;
unset($temp);
}
}
foreach ($ThisFileInfo['tags'] as $tagtype => $tagarray) {
foreach ($tagarray as $tagname => $tagdata) {
Expand Down Expand Up @@ -1578,9 +1596,18 @@ public static function CopyTagsToComments(&$ThisFileInfo, $option_tags_html=true

} elseif (!is_array($value)) {

$newvaluelength = strlen(trim($value));
$newvaluelength = strlen(trim($value));
$newvaluelengthMB = mb_strlen(trim($value));
foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) {
$oldvaluelength = strlen(trim($existingvalue));
$oldvaluelength = strlen(trim($existingvalue));
$oldvaluelengthMB = mb_strlen(trim($existingvalue));
if (($newvaluelengthMB == $oldvaluelengthMB) && ($existingvalue == getid3_lib::iconv_fallback('UTF-8', 'ASCII', $value))) {
// https://github.com/JamesHeinrich/getID3/issues/338
// check for tags containing extended characters that may have been forced into limited-character storage (e.g. UTF8 values into ASCII)
// which will usually display unrepresentable characters as "?"
$ThisFileInfo['comments'][$tagname][$existingkey] = trim($value);
break;
}
if ((strlen($existingvalue) > 10) && ($newvaluelength > $oldvaluelength) && (substr(trim($value), 0, strlen($existingvalue)) == $existingvalue)) {
$ThisFileInfo['comments'][$tagname][$existingkey] = trim($value);
break;
Expand All @@ -1606,14 +1633,16 @@ public static function CopyTagsToComments(&$ThisFileInfo, $option_tags_html=true
}

// attempt to standardize spelling of returned keys
$StandardizeFieldNames = array(
'tracknumber' => 'track_number',
'track' => 'track_number',
);
foreach ($StandardizeFieldNames as $badkey => $goodkey) {
if (array_key_exists($badkey, $ThisFileInfo['comments']) && !array_key_exists($goodkey, $ThisFileInfo['comments'])) {
$ThisFileInfo['comments'][$goodkey] = $ThisFileInfo['comments'][$badkey];
unset($ThisFileInfo['comments'][$badkey]);
if (!empty($ThisFileInfo['comments'])) {
$StandardizeFieldNames = array(
'tracknumber' => 'track_number',
'track' => 'track_number',
);
foreach ($StandardizeFieldNames as $badkey => $goodkey) {
if (array_key_exists($badkey, $ThisFileInfo['comments']) && !array_key_exists($goodkey, $ThisFileInfo['comments'])) {
$ThisFileInfo['comments'][$goodkey] = $ThisFileInfo['comments'][$badkey];
unset($ThisFileInfo['comments'][$badkey]);
}
}
}

Expand Down Expand Up @@ -1739,6 +1768,7 @@ public static function trimNullByte($string) {
* @return float|bool
*/
public static function getFileSizeSyscall($path) {
$commandline = null;
$filesize = false;

if (GETID3_OS_ISWINDOWS) {
Expand Down Expand Up @@ -1800,7 +1830,7 @@ public static function truepath($filename) {
*
* @return string
*/
public static function mb_basename($path, $suffix = null) {
public static function mb_basename($path, $suffix = '') {
$splited = preg_split('#/#', rtrim($path, '/ '));
return substr(basename('X'.$splited[count($splited) - 1], $suffix), 1);
}
Expand Down
Loading

0 comments on commit 90d95f0

Please sign in to comment.