Skip to content
Extraits de code Groupes Projets

Comparer les révisions

Les modifications sont affichées comme si la révision source était fusionnée avec la révision cible. En savoir plus sur la comparaison des révisions.

Source

Sélectionner le projet cible
No results found

Cible

Sélectionner le projet cible
  • spip/medias
  • pierre.laszczak/medias
  • cy.altern/medias
  • MathieuAlphamosa/medias
  • taffit/medias
  • RealET/medias
  • johan/medias
  • Yohooo/medias
  • pierretux/medias
  • placido/medias
  • JLuc/medias
11 résultats
Afficher les modifications
Affichage de
avec 35 ajouts et 5949 suppressions
......@@ -3,13 +3,9 @@
// extrait automatiquement de https://trad.spip.net/tradlang_module/paquet-medias?lang_cible=lb
// ** ne pas modifier le fichier **
if (!defined('_ECRIRE_INC_VERSION')) {
return;
}
$GLOBALS[$GLOBALS['idx_lang']] = array(
return [
// M
'medias_description' => 'Gestioun vun de Medien vu SPIP',
'medias_slogan' => 'Gestioun vun de Medien am SPIP'
);
'medias_slogan' => 'Gestioun vun de Medien am SPIP',
];
<?php
// This is a SPIP language file -- Ceci est un fichier langue de SPIP
// extrait automatiquement de https://trad.spip.net/tradlang_module/paquet-medias?lang_cible=mg
// ** ne pas modifier le fichier **
return [
// M
'medias_description' => 'Gestion des médias de SPIP',
'medias_slogan' => 'Gestion des médias dans SPIP',
];
......@@ -3,13 +3,9 @@
// extrait automatiquement de https://trad.spip.net/tradlang_module/paquet-medias?lang_cible=nl
// ** ne pas modifier le fichier **
if (!defined('_ECRIRE_INC_VERSION')) {
return;
}
$GLOBALS[$GLOBALS['idx_lang']] = array(
return [
// M
'medias_description' => 'Beheer van de medias in SPIP',
'medias_slogan' => 'Beheer van de digitale documenten in SPIP'
);
'medias_slogan' => 'Beheer van de digitale documenten in SPIP',
];
......@@ -3,13 +3,9 @@
// extrait automatiquement de https://trad.spip.net/tradlang_module/paquet-medias?lang_cible=oc_ni_mis
// ** ne pas modifier le fichier **
if (!defined('_ECRIRE_INC_VERSION')) {
return;
}
$GLOBALS[$GLOBALS['idx_lang']] = array(
return [
// M
'medias_description' => 'Gestioun dei media de SPIP',
'medias_slogan' => 'Gestioun dei media en SPIP'
);
'medias_slogan' => 'Gestioun dei media en SPIP',
];
......@@ -3,13 +3,9 @@
// extrait automatiquement de https://trad.spip.net/tradlang_module/paquet-medias?lang_cible=pt
// ** ne pas modifier le fichier **
if (!defined('_ECRIRE_INC_VERSION')) {
return;
}
$GLOBALS[$GLOBALS['idx_lang']] = array(
return [
// M
'medias_description' => 'Gestão multimédia de SPIP',
'medias_slogan' => 'Gestão multimédia de SPIP'
);
'medias_slogan' => 'Gestão multimédia de SPIP',
];
......@@ -3,13 +3,9 @@
// extrait automatiquement de https://trad.spip.net/tradlang_module/paquet-medias?lang_cible=pt_br
// ** ne pas modifier le fichier **
if (!defined('_ECRIRE_INC_VERSION')) {
return;
}
$GLOBALS[$GLOBALS['idx_lang']] = array(
return [
// M
'medias_description' => 'Gerenciamento de mídias do SPIP',
'medias_slogan' => 'Gerenciamento de mídias do SPIP'
);
'medias_slogan' => 'Gerenciamento de mídias do SPIP',
];
......@@ -3,13 +3,9 @@
// extrait automatiquement de https://trad.spip.net/tradlang_module/paquet-medias?lang_cible=ru
// ** ne pas modifier le fichier **
if (!defined('_ECRIRE_INC_VERSION')) {
return;
}
$GLOBALS[$GLOBALS['idx_lang']] = array(
return [
// M
'medias_description' => 'Управление медиа файлами',
'medias_slogan' => 'Управление медиа файлами'
);
'medias_slogan' => 'Управление медиа файлами',
];
......@@ -3,13 +3,9 @@
// extrait automatiquement de https://trad.spip.net/tradlang_module/paquet-medias?lang_cible=sk
// ** ne pas modifier le fichier **
if (!defined('_ECRIRE_INC_VERSION')) {
return;
}
$GLOBALS[$GLOBALS['idx_lang']] = array(
return [
// M
'medias_description' => 'Správa multimédií v SPIPe',
'medias_slogan' => 'Správa multimédií v SPIPe'
);
'medias_slogan' => 'Správa multimédií v SPIPe',
];
......@@ -3,13 +3,9 @@
// extrait automatiquement de https://trad.spip.net/tradlang_module/paquet-medias?lang_cible=uk
// ** ne pas modifier le fichier **
if (!defined('_ECRIRE_INC_VERSION')) {
return;
}
$GLOBALS[$GLOBALS['idx_lang']] = array(
return [
// M
'medias_description' => 'Управління медіа-файлами',
'medias_slogan' => 'Управління медіа-файлами'
);
'medias_slogan' => 'Управління медіа-файлами',
];
<?php
/////////////////////////////////////////////////////////////////
/// getID3() by James Heinrich <info@getid3.org> //
// available at https://github.com/JamesHeinrich/getID3 //
// or https://www.getid3.org //
// or http://getid3.sourceforge.net //
// //
// extension.cache.dbm.php - part of getID3() //
// Please see readme.txt for more information //
// ///
/////////////////////////////////////////////////////////////////
// //
// This extension written by Allan Hansen <ahØartemis*dk> //
// ///
/////////////////////////////////////////////////////////////////
/**
* This is a caching extension for getID3(). It works the exact same
* way as the getID3 class, but return cached information very fast
*
* Example:
*
* Normal getID3 usage (example):
*
* require_once 'getid3/getid3.php';
* $getID3 = new getID3;
* $getID3->encoding = 'UTF-8';
* $info1 = $getID3->analyze('file1.flac');
* $info2 = $getID3->analyze('file2.wv');
*
* getID3_cached usage:
*
* require_once 'getid3/getid3.php';
* require_once 'getid3/getid3/extension.cache.dbm.php';
* $getID3 = new getID3_cached('db3', '/tmp/getid3_cache.dbm',
* '/tmp/getid3_cache.lock');
* $getID3->encoding = 'UTF-8';
* $info1 = $getID3->analyze('file1.flac');
* $info2 = $getID3->analyze('file2.wv');
*
*
* Supported Cache Types
*
* SQL Databases: (use extension.cache.mysql)
*
* cache_type cache_options
* -------------------------------------------------------------------
* mysql host, database, username, password
*
*
* DBM-Style Databases: (this extension)
*
* cache_type cache_options
* -------------------------------------------------------------------
* gdbm dbm_filename, lock_filename
* ndbm dbm_filename, lock_filename
* db2 dbm_filename, lock_filename
* db3 dbm_filename, lock_filename
* db4 dbm_filename, lock_filename (PHP5 required)
*
* PHP must have write access to both dbm_filename and lock_filename.
*
*
* Recommended Cache Types
*
* Infrequent updates, many reads any DBM
* Frequent updates mysql
*/
class getID3_cached_dbm extends getID3
{
/**
* @var resource
*/
private $dba;
/**
* @var resource|bool
*/
private $lock;
/**
* @var string
*/
private $cache_type;
/**
* @var string
*/
private $dbm_filename;
/**
* constructor - see top of this file for cache type and cache_options
*
* @param string $cache_type
* @param string $dbm_filename
* @param string $lock_filename
*
* @throws Exception
* @throws getid3_exception
*/
public function __construct($cache_type, $dbm_filename, $lock_filename) {
// Check for dba extension
if (!extension_loaded('dba')) {
throw new Exception('PHP is not compiled with dba support, required to use DBM style cache.');
}
// Check for specific dba driver
if (!function_exists('dba_handlers') || !in_array($cache_type, dba_handlers())) {
throw new Exception('PHP is not compiled --with '.$cache_type.' support, required to use DBM style cache.');
}
// Create lock file if needed
if (!file_exists($lock_filename)) {
if (!touch($lock_filename)) {
throw new Exception('failed to create lock file: '.$lock_filename);
}
}
// Open lock file for writing
if (!is_writeable($lock_filename)) {
throw new Exception('lock file: '.$lock_filename.' is not writable');
}
$this->lock = fopen($lock_filename, 'w');
// Acquire exclusive write lock to lock file
flock($this->lock, LOCK_EX);
// Create dbm-file if needed
if (!file_exists($dbm_filename)) {
if (!touch($dbm_filename)) {
throw new Exception('failed to create dbm file: '.$dbm_filename);
}
}
// Try to open dbm file for writing
$this->dba = dba_open($dbm_filename, 'w', $cache_type);
if (!$this->dba) {
// Failed - create new dbm file
$this->dba = dba_open($dbm_filename, 'n', $cache_type);
if (!$this->dba) {
throw new Exception('failed to create dbm file: '.$dbm_filename);
}
// Insert getID3 version number
dba_insert(getID3::VERSION, getID3::VERSION, $this->dba);
}
// Init misc values
$this->cache_type = $cache_type;
$this->dbm_filename = $dbm_filename;
// Register destructor
register_shutdown_function(array($this, '__destruct'));
// Check version number and clear cache if changed
if (dba_fetch(getID3::VERSION, $this->dba) != getID3::VERSION) {
$this->clear_cache();
}
parent::__construct();
}
/**
* destructor
*/
public function __destruct() {
// Close dbm file
dba_close($this->dba);
// Release exclusive lock
flock($this->lock, LOCK_UN);
// Close lock file
fclose($this->lock);
}
/**
* clear cache
*
* @throws Exception
*/
public function clear_cache() {
// Close dbm file
dba_close($this->dba);
// Create new dbm file
$this->dba = dba_open($this->dbm_filename, 'n', $this->cache_type);
if (!$this->dba) {
throw new Exception('failed to clear cache/recreate dbm file: '.$this->dbm_filename);
}
// Insert getID3 version number
dba_insert(getID3::VERSION, getID3::VERSION, $this->dba);
// Re-register shutdown function
register_shutdown_function(array($this, '__destruct'));
}
/**
* clear cache
*
* @param string $filename
* @param int $filesize
* @param string $original_filename
* @param resource $fp
*
* @return mixed
*/
public function analyze($filename, $filesize=null, $original_filename='', $fp=null) {
if (file_exists($filename)) {
// Calc key filename::mod_time::size - should be unique
$key = $filename.'::'.filemtime($filename).'::'.filesize($filename);
// Loopup key
$result = dba_fetch($key, $this->dba);
// Hit
if ($result !== false) {
return unserialize($result);
}
}
// Miss
$result = parent::analyze($filename, $filesize, $original_filename, $fp);
// Save result
if (isset($key) && file_exists($filename)) {
dba_insert($key, serialize($result), $this->dba);
}
return $result;
}
}
<?php
/////////////////////////////////////////////////////////////////
/// getID3() by James Heinrich <info@getid3.org> //
// available at https://github.com/JamesHeinrich/getID3 //
// or https://www.getid3.org //
// or http://getid3.sourceforge.net //
// //
// extension.cache.mysql.php - part of getID3() //
// Please see readme.txt for more information //
// ///
/////////////////////////////////////////////////////////////////
// //
// This extension written by Allan Hansen <ahØartemis*dk> //
// Table name mod by Carlo Capocasa <calroØcarlocapocasa*com> //
// ///
/////////////////////////////////////////////////////////////////
/**
* This is a caching extension for getID3(). It works the exact same
* way as the getID3 class, but return cached information very fast
*
* Example: (see also demo.cache.mysql.php in /demo/)
*
* Normal getID3 usage (example):
*
* require_once 'getid3/getid3.php';
* $getID3 = new getID3;
* $getID3->encoding = 'UTF-8';
* $info1 = $getID3->analyze('file1.flac');
* $info2 = $getID3->analyze('file2.wv');
*
* getID3_cached usage:
*
* require_once 'getid3/getid3.php';
* require_once 'getid3/getid3/extension.cache.mysql.php';
* // 5th parameter (tablename) is optional, default is 'getid3_cache'
* $getID3 = new getID3_cached_mysql('localhost', 'database', 'username', 'password', 'tablename');
* $getID3->encoding = 'UTF-8';
* $info1 = $getID3->analyze('file1.flac');
* $info2 = $getID3->analyze('file2.wv');
*
*
* Supported Cache Types (this extension)
*
* SQL Databases:
*
* cache_type cache_options
* -------------------------------------------------------------------
* mysql host, database, username, password
*
*
* DBM-Style Databases: (use extension.cache.dbm)
*
* cache_type cache_options
* -------------------------------------------------------------------
* gdbm dbm_filename, lock_filename
* ndbm dbm_filename, lock_filename
* db2 dbm_filename, lock_filename
* db3 dbm_filename, lock_filename
* db4 dbm_filename, lock_filename (PHP5 required)
*
* PHP must have write access to both dbm_filename and lock_filename.
*
*
* Recommended Cache Types
*
* Infrequent updates, many reads any DBM
* Frequent updates mysql
*/
class getID3_cached_mysql extends getID3
{
/**
* @var resource
*/
private $cursor;
/**
* @var resource
*/
private $connection;
/**
* @var string
*/
private $table;
/**
* constructor - see top of this file for cache type and cache_options
*
* @param string $host
* @param string $database
* @param string $username
* @param string $password
* @param string $table
*
* @throws Exception
* @throws getid3_exception
*/
public function __construct($host, $database, $username, $password, $table='getid3_cache') {
// Check for mysql support
if (!function_exists('mysql_pconnect')) {
throw new Exception('PHP not compiled with mysql support.');
}
// Connect to database
$this->connection = mysql_pconnect($host, $username, $password);
if (!$this->connection) {
throw new Exception('mysql_pconnect() failed - check permissions and spelling.');
}
// Select database
if (!mysql_select_db($database, $this->connection)) {
throw new Exception('Cannot use database '.$database);
}
// Set table
$this->table = $table;
// Create cache table if not exists
$this->create_table();
// Check version number and clear cache if changed
$version = '';
$SQLquery = 'SELECT `value`';
$SQLquery .= ' FROM `'.mysql_real_escape_string($this->table).'`';
$SQLquery .= ' WHERE (`filename` = \''.mysql_real_escape_string(getID3::VERSION).'\')';
$SQLquery .= ' AND (`filesize` = -1)';
$SQLquery .= ' AND (`filetime` = -1)';
$SQLquery .= ' AND (`analyzetime` = -1)';
if ($this->cursor = mysql_query($SQLquery, $this->connection)) {
list($version) = mysql_fetch_array($this->cursor);
}
if ($version != getID3::VERSION) {
$this->clear_cache();
}
parent::__construct();
}
/**
* clear cache
*/
public function clear_cache() {
$this->cursor = mysql_query('DELETE FROM `'.mysql_real_escape_string($this->table).'`', $this->connection);
$this->cursor = mysql_query('INSERT INTO `'.mysql_real_escape_string($this->table).'` VALUES (\''.getID3::VERSION.'\', -1, -1, -1, \''.getID3::VERSION.'\')', $this->connection);
}
/**
* analyze file
*
* @param string $filename
* @param int $filesize
* @param string $original_filename
* @param resource $fp
*
* @return mixed
*/
public function analyze($filename, $filesize=null, $original_filename='', $fp=null) {
$filetime = 0;
if (file_exists($filename)) {
// Short-hands
$filetime = filemtime($filename);
$filesize = filesize($filename);
// Lookup file
$SQLquery = 'SELECT `value`';
$SQLquery .= ' FROM `'.mysql_real_escape_string($this->table).'`';
$SQLquery .= ' WHERE (`filename` = \''.mysql_real_escape_string($filename).'\')';
$SQLquery .= ' AND (`filesize` = \''.mysql_real_escape_string($filesize).'\')';
$SQLquery .= ' AND (`filetime` = \''.mysql_real_escape_string($filetime).'\')';
$this->cursor = mysql_query($SQLquery, $this->connection);
if (mysql_num_rows($this->cursor) > 0) {
// Hit
list($result) = mysql_fetch_array($this->cursor);
return unserialize(base64_decode($result));
}
}
// Miss
$analysis = parent::analyze($filename, $filesize, $original_filename, $fp);
// Save result
if (file_exists($filename)) {
$SQLquery = 'INSERT INTO `'.mysql_real_escape_string($this->table).'` (`filename`, `filesize`, `filetime`, `analyzetime`, `value`) VALUES (';
$SQLquery .= '\''.mysql_real_escape_string($filename).'\'';
$SQLquery .= ', \''.mysql_real_escape_string($filesize).'\'';
$SQLquery .= ', \''.mysql_real_escape_string($filetime).'\'';
$SQLquery .= ', \''.mysql_real_escape_string(time() ).'\'';
$SQLquery .= ', \''.mysql_real_escape_string(base64_encode(serialize($analysis))).'\')';
$this->cursor = mysql_query($SQLquery, $this->connection);
}
return $analysis;
}
/**
* (re)create sql table
*
* @param bool $drop
*/
private function create_table($drop=false) {
$SQLquery = 'CREATE TABLE IF NOT EXISTS `'.mysql_real_escape_string($this->table).'` (';
$SQLquery .= '`filename` VARCHAR(990) NOT NULL DEFAULT \'\'';
$SQLquery .= ', `filesize` INT(11) NOT NULL DEFAULT \'0\'';
$SQLquery .= ', `filetime` INT(11) NOT NULL DEFAULT \'0\'';
$SQLquery .= ', `analyzetime` INT(11) NOT NULL DEFAULT \'0\'';
$SQLquery .= ', `value` LONGTEXT NOT NULL';
$SQLquery .= ', PRIMARY KEY (`filename`, `filesize`, `filetime`))';
$this->cursor = mysql_query($SQLquery, $this->connection);
echo mysql_error($this->connection);
}
}
<?php
/////////////////////////////////////////////////////////////////
/// getID3() by James Heinrich <info@getid3.org> //
// available at https://github.com/JamesHeinrich/getID3 //
// or https://www.getid3.org //
// or http://getid3.sourceforge.net //
// //
// extension.cache.mysqli.php - part of getID3() //
// Please see readme.txt for more information //
// //
/////////////////////////////////////////////////////////////////
// //
// This extension written by Allan Hansen <ahØartemis*dk> //
// Table name mod by Carlo Capocasa <calroØcarlocapocasa*com> //
// ///
/////////////////////////////////////////////////////////////////
/**
* This is a caching extension for getID3(). It works the exact same
* way as the getID3 class, but return cached information very fast
*
* Example: (see also demo.cache.mysql.php in /demo/)
*
* Normal getID3 usage (example):
*
* require_once 'getid3/getid3.php';
* $getID3 = new getID3;
* $getID3->encoding = 'UTF-8';
* $info1 = $getID3->analyze('file1.flac');
* $info2 = $getID3->analyze('file2.wv');
*
* getID3_cached usage:
*
* require_once 'getid3/getid3.php';
* require_once 'getid3/getid3/extension.cache.mysqli.php';
* // 5th parameter (tablename) is optional, default is 'getid3_cache'
* $getID3 = new getID3_cached_mysqli('localhost', 'database', 'username', 'password', 'tablename');
* $getID3->encoding = 'UTF-8';
* $info1 = $getID3->analyze('file1.flac');
* $info2 = $getID3->analyze('file2.wv');
*
*
* Supported Cache Types (this extension)
*
* SQL Databases:
*
* cache_type cache_options
* -------------------------------------------------------------------
* mysqli host, database, username, password
*
*
* DBM-Style Databases: (use extension.cache.dbm)
*
* cache_type cache_options
* -------------------------------------------------------------------
* gdbm dbm_filename, lock_filename
* ndbm dbm_filename, lock_filename
* db2 dbm_filename, lock_filename
* db3 dbm_filename, lock_filename
* db4 dbm_filename, lock_filename (PHP5 required)
*
* PHP must have write access to both dbm_filename and lock_filename.
*
*
* Recommended Cache Types
*
* Infrequent updates, many reads any DBM
* Frequent updates mysqli
*/
class getID3_cached_mysqli extends getID3
{
/**
* @var mysqli
*/
private $mysqli;
/**
* @var mysqli_result
*/
private $cursor;
/**
* @var string
*/
private $table;
/**
* @var bool
*/
private $db_structure_check;
/**
* constructor - see top of this file for cache type and cache_options
*
* @param string $host
* @param string $database
* @param string $username
* @param string $password
* @param string $table
*
* @throws Exception
* @throws getid3_exception
*/
public function __construct($host, $database, $username, $password, $table='getid3_cache') {
// Check for mysqli support
if (!function_exists('mysqli_connect')) {
throw new Exception('PHP not compiled with mysqli support.');
}
// Connect to database
$this->mysqli = new mysqli($host, $username, $password);
if ($this->mysqli->connect_error) {
throw new Exception('Connect Error (' . $this->mysqli->connect_errno . ') ' . $this->mysqli->connect_error);
}
// Select database
if (!$this->mysqli->select_db($database)) {
throw new Exception('Cannot use database '.$database);
}
// Set table
$this->table = $table;
// Create cache table if not exists
$this->create_table();
$this->db_structure_check = true; // set to false if you know your table structure has already been migrated to use `hash` as the primary key to avoid
$this->migrate_db_structure();
// Check version number and clear cache if changed
$version = '';
$SQLquery = 'SELECT `value`';
$SQLquery .= ' FROM `'.$this->mysqli->real_escape_string($this->table).'`';
$SQLquery .= ' WHERE (`filename` = \''.$this->mysqli->real_escape_string(getID3::VERSION).'\')';
$SQLquery .= ' AND (`hash` = \'getID3::VERSION\')';
if ($this->cursor = $this->mysqli->query($SQLquery)) {
list($version) = $this->cursor->fetch_array();
}
if ($version != getID3::VERSION) {
$this->clear_cache();
}
parent::__construct();
}
/**
* clear cache
*/
public function clear_cache() {
$this->mysqli->query('TRUNCATE TABLE `'.$this->mysqli->real_escape_string($this->table).'`');
$this->mysqli->query('INSERT INTO `'.$this->mysqli->real_escape_string($this->table).'` (`hash`, `filename`, `filesize`, `filetime`, `analyzetime`, `value`) VALUES (\'getID3::VERSION\', \''.getID3::VERSION.'\', -1, -1, -1, \''.getID3::VERSION.'\')');
}
/**
* migrate database structure if needed
*/
public function migrate_db_structure() {
// Check for table structure
if ($this->db_structure_check) {
$SQLquery = 'SHOW COLUMNS';
$SQLquery .= ' FROM `'.$this->mysqli->real_escape_string($this->table).'`';
$SQLquery .= ' LIKE \'hash\'';
$this->cursor = $this->mysqli->query($SQLquery);
if ($this->cursor->num_rows == 0) {
// table has not been migrated, add column, add hashes, change index
$SQLquery = 'ALTER TABLE `getid3_cache` DROP PRIMARY KEY, ADD `hash` CHAR(32) NOT NULL DEFAULT \'\' FIRST, ADD PRIMARY KEY(`hash`)';
$this->mysqli->query($SQLquery);
$SQLquery = 'UPDATE `getid3_cache` SET';
$SQLquery .= ' `hash` = MD5(`filename`, `filesize`, `filetime`)';
$SQLquery .= ' WHERE (`filesize` > -1)';
$this->mysqli->query($SQLquery);
$SQLquery = 'UPDATE `getid3_cache` SET';
$SQLquery .= ' `hash` = \'getID3::VERSION\'';
$SQLquery .= ' WHERE (`filesize` = -1)';
$SQLquery .= ' AND (`filetime` = -1)';
$SQLquery .= ' AND (`filetime` = -1)';
$this->mysqli->query($SQLquery);
}
}
}
/**
* analyze file
*
* @param string $filename
* @param int $filesize
* @param string $original_filename
* @param resource $fp
*
* @return mixed
*/
public function analyze($filename, $filesize=null, $original_filename='', $fp=null) {
$filetime = 0;
if (file_exists($filename)) {
// Short-hands
$filetime = filemtime($filename);
$filesize = filesize($filename);
// Lookup file
$SQLquery = 'SELECT `value`';
$SQLquery .= ' FROM `'.$this->mysqli->real_escape_string($this->table).'`';
$SQLquery .= ' WHERE (`hash` = \''.$this->mysqli->real_escape_string(md5($filename.$filesize.$filetime)).'\')';
$this->cursor = $this->mysqli->query($SQLquery);
if ($this->cursor->num_rows > 0) {
// Hit
list($result) = $this->cursor->fetch_array();
return unserialize(base64_decode($result));
}
}
// Miss
$analysis = parent::analyze($filename, $filesize, $original_filename, $fp);
// Save result
if (file_exists($filename)) {
$SQLquery = 'INSERT INTO `'.$this->mysqli->real_escape_string($this->table).'` (`hash`, `filename`, `filesize`, `filetime`, `analyzetime`, `value`) VALUES (';
$SQLquery .= '\''.$this->mysqli->real_escape_string(md5($filename.$filesize.$filetime)).'\'';
$SQLquery .= ', \''.$this->mysqli->real_escape_string($filename).'\'';
$SQLquery .= ', \''.$this->mysqli->real_escape_string($filesize).'\'';
$SQLquery .= ', \''.$this->mysqli->real_escape_string($filetime).'\'';
$SQLquery .= ', UNIX_TIMESTAMP()';
$SQLquery .= ', \''.$this->mysqli->real_escape_string(base64_encode(serialize($analysis))).'\'';
$SQLquery .= ')';
$this->cursor = $this->mysqli->query($SQLquery);
}
return $analysis;
}
/**
* (re)create mysqli table
*
* @param bool $drop
*/
private function create_table($drop=false) {
if ($drop) {
$SQLquery = 'DROP TABLE IF EXISTS `'.$this->mysqli->real_escape_string($this->table).'`';
$this->mysqli->query($SQLquery);
}
$SQLquery = 'CREATE TABLE IF NOT EXISTS `'.$this->mysqli->real_escape_string($this->table).'` (';
$SQLquery .= '`hash` CHAR(32) NOT NULL DEFAULT \'\'';
$SQLquery .= ', `filename` VARCHAR(1000) NOT NULL DEFAULT \'\'';
$SQLquery .= ', `filesize` INT(11) NOT NULL DEFAULT \'0\'';
$SQLquery .= ', `filetime` INT(11) NOT NULL DEFAULT \'0\'';
$SQLquery .= ', `analyzetime` INT(11) NOT NULL DEFAULT \'0\'';
$SQLquery .= ', `value` LONGTEXT NOT NULL';
$SQLquery .= ', PRIMARY KEY (`hash`))';
$this->cursor = $this->mysqli->query($SQLquery);
echo $this->mysqli->error;
}
}
<?php
/////////////////////////////////////////////////////////////////
/// getID3() by James Heinrich <info@getid3.org> //
// available at https://github.com/JamesHeinrich/getID3 //
// or https://www.getid3.org //
// or http://getid3.sourceforge.net //
// //
// extension.cache.mysqli.php - part of getID3() //
// Please see readme.txt for more information //
// //
/////////////////////////////////////////////////////////////////
// //
// extension.cache.sqlite3.php - part of getID3() //
// Please see readme.txt for more information //
// //
/////////////////////////////////////////////////////////////////
/// //
// MySQL extension written by Allan Hansen <ahØartemis*dk> //
// Table name mod by Carlo Capocasa <calroØcarlocapocasa*com> //
// MySQL extension was reworked for SQLite3 by //
// Karl G. Holz <newaeonØmac*com> //
// ///
/////////////////////////////////////////////////////////////////
/**
* This is a caching extension for getID3(). It works the exact same
* way as the getID3 class, but return cached information much faster
*
* Normal getID3 usage (example):
*
* require_once 'getid3/getid3.php';
* $getID3 = new getID3;
* $getID3->encoding = 'UTF-8';
* $info1 = $getID3->analyze('file1.flac');
* $info2 = $getID3->analyze('file2.wv');
*
* getID3_cached usage:
*
* require_once 'getid3/getid3.php';
* require_once 'getid3/extension.cache.sqlite3.php';
* // all parameters are optional, defaults are:
* $getID3 = new getID3_cached_sqlite3($table='getid3_cache', $hide=FALSE);
* $getID3->encoding = 'UTF-8';
* $info1 = $getID3->analyze('file1.flac');
* $info2 = $getID3->analyze('file2.wv');
*
*
* Supported Cache Types (this extension)
*
* SQL Databases:
*
* cache_type cache_options
* -------------------------------------------------------------------
* mysql host, database, username, password
*
* sqlite3 table='getid3_cache', hide=false (PHP5)
*
*
* *** database file will be stored in the same directory as this script,
* *** webserver must have write access to that directory!
* *** set $hide to TRUE to prefix db file with .ht to pervent access from web client
* *** this is a default setting in the Apache configuration:
*
* The following lines prevent .htaccess and .htpasswd files from being viewed by Web clients.
*
* <Files ~ "^\.ht">
* Order allow,deny
* Deny from all
* Satisfy all
* </Files>
*
********************************************************************************
*
* -------------------------------------------------------------------
* DBM-Style Databases: (use extension.cache.dbm)
*
* cache_type cache_options
* -------------------------------------------------------------------
* gdbm dbm_filename, lock_filename
* ndbm dbm_filename, lock_filename
* db2 dbm_filename, lock_filename
* db3 dbm_filename, lock_filename
* db4 dbm_filename, lock_filename (PHP5 required)
*
* PHP must have write access to both dbm_filename and lock_filename.
*
* Recommended Cache Types
*
* Infrequent updates, many reads any DBM
* Frequent updates mysql
********************************************************************************
*
* IMHO this is still a bit slow, I'm using this with MP4/MOV/ M4v files
* there is a plan to add directory scanning and analyzing to make things work much faster
*
*
*/
class getID3_cached_sqlite3 extends getID3
{
/**
* hold the sqlite db
*
* @var SQLite3 Resource
*/
private $db;
/**
* table to use for caching
*
* @var string $table
*/
private $table;
/**
* @param string $table holds name of sqlite table
* @param boolean $hide
*
* @throws getid3_exception
* @throws Exception
*/
public function __construct($table='getid3_cache', $hide=false) {
// Check for SQLite3 support
if (!function_exists('sqlite_open')) {
throw new Exception('PHP not compiled with SQLite3 support.');
}
$this->table = $table; // Set table
$file = dirname(__FILE__).'/'.basename(__FILE__, 'php').'sqlite';
if ($hide) {
$file = dirname(__FILE__).'/.ht.'.basename(__FILE__, 'php').'sqlite';
}
$this->db = new SQLite3($file);
$db = $this->db;
$this->create_table(); // Create cache table if not exists
$version = '';
$sql = $this->getQuery('version_check');
$stmt = $db->prepare($sql);
$stmt->bindValue(':filename', getID3::VERSION, SQLITE3_TEXT);
$result = $stmt->execute();
list($version) = $result->fetchArray();
if ($version != getID3::VERSION) { // Check version number and clear cache if changed
$this->clear_cache();
}
parent::__construct();
}
/**
* close the database connection
*/
public function __destruct() {
$db=$this->db;
$db->close();
}
/**
* clear the cache
*
* @return SQLite3Result
*/
private function clear_cache() {
$db = $this->db;
$sql = $this->getQuery('delete_cache');
$db->exec($sql);
$sql = $this->getQuery('set_version');
$stmt = $db->prepare($sql);
$stmt->bindValue(':filename', getID3::VERSION, SQLITE3_TEXT);
$stmt->bindValue(':dirname', getID3::VERSION, SQLITE3_TEXT);
$stmt->bindValue(':val', getID3::VERSION, SQLITE3_TEXT);
return $stmt->execute();
}
/**
* analyze file and cache them, if cached pull from the db
*
* @param string $filename
* @param integer $filesize
* @param string $original_filename
* @param resource $fp
*
* @return mixed|false
*/
public function analyze($filename, $filesize=null, $original_filename='', $fp=null) {
if (!file_exists($filename)) {
return false;
}
// items to track for caching
$filetime = filemtime($filename);
$filesize_real = filesize($filename);
// this will be saved for a quick directory lookup of analized files
// ... why do 50 seperate sql quries when you can do 1 for the same result
$dirname = dirname($filename);
// Lookup file
$db = $this->db;
$sql = $this->getQuery('get_id3_data');
$stmt = $db->prepare($sql);
$stmt->bindValue(':filename', $filename, SQLITE3_TEXT);
$stmt->bindValue(':filesize', $filesize_real, SQLITE3_INTEGER);
$stmt->bindValue(':filetime', $filetime, SQLITE3_INTEGER);
$res = $stmt->execute();
list($result) = $res->fetchArray();
if (count($result) > 0 ) {
return unserialize(base64_decode($result));
}
// if it hasn't been analyzed before, then do it now
$analysis = parent::analyze($filename, $filesize, $original_filename, $fp);
// Save result
$sql = $this->getQuery('cache_file');
$stmt = $db->prepare($sql);
$stmt->bindValue(':filename', $filename, SQLITE3_TEXT);
$stmt->bindValue(':dirname', $dirname, SQLITE3_TEXT);
$stmt->bindValue(':filesize', $filesize_real, SQLITE3_INTEGER);
$stmt->bindValue(':filetime', $filetime, SQLITE3_INTEGER);
$stmt->bindValue(':atime', time(), SQLITE3_INTEGER);
$stmt->bindValue(':val', base64_encode(serialize($analysis)), SQLITE3_TEXT);
$res = $stmt->execute();
return $analysis;
}
/**
* create data base table
* this is almost the same as MySQL, with the exception of the dirname being added
*
* @return bool
*/
private function create_table() {
$db = $this->db;
$sql = $this->getQuery('make_table');
return $db->exec($sql);
}
/**
* get cached directory
*
* This function is not in the MySQL extention, it's ment to speed up requesting multiple files
* which is ideal for podcasting, playlists, etc.
*
* @param string $dir directory to search the cache database for
*
* @return array return an array of matching id3 data
*/
public function get_cached_dir($dir) {
$db = $this->db;
$rows = array();
$sql = $this->getQuery('get_cached_dir');
$stmt = $db->prepare($sql);
$stmt->bindValue(':dirname', $dir, SQLITE3_TEXT);
$res = $stmt->execute();
while ($row=$res->fetchArray()) {
$rows[] = unserialize(base64_decode($row));
}
return $rows;
}
/**
* returns NULL if query is not found
*
* @param string $name
*
* @return null|string
*/
public function getQuery($name)
{
switch ($name) {
case 'version_check':
return "SELECT val FROM $this->table WHERE filename = :filename AND filesize = '-1' AND filetime = '-1' AND analyzetime = '-1'";
case 'delete_cache':
return "DELETE FROM $this->table";
case 'set_version':
return "INSERT INTO $this->table (filename, dirname, filesize, filetime, analyzetime, val) VALUES (:filename, :dirname, -1, -1, -1, :val)";
case 'get_id3_data':
return "SELECT val FROM $this->table WHERE filename = :filename AND filesize = :filesize AND filetime = :filetime";
case 'cache_file':
return "INSERT INTO $this->table (filename, dirname, filesize, filetime, analyzetime, val) VALUES (:filename, :dirname, :filesize, :filetime, :atime, :val)";
case 'make_table':
return "CREATE TABLE IF NOT EXISTS $this->table (filename VARCHAR(255) DEFAULT '', dirname VARCHAR(255) DEFAULT '', filesize INT(11) DEFAULT '0', filetime INT(11) DEFAULT '0', analyzetime INT(11) DEFAULT '0', val text, PRIMARY KEY (filename, filesize, filetime))";
case 'get_cached_dir':
return "SELECT val FROM $this->table WHERE dirname = :dirname";
default:
return null;
}
}
/**
* use the magical __get() for sql queries
*
* access as easy as $this->{case name}, returns NULL if query is not found
*
* @param string $name
*
* @return string
* @deprecated use getQuery() instead
*/
public function __get($name) {
return $this->getQuery($name);
}
}
<?php
/////////////////////////////////////////////////////////////////
/// getID3() by James Heinrich <info@getid3.org> //
// available at https://github.com/JamesHeinrich/getID3 //
// or https://www.getid3.org //
// or http://getid3.sourceforge.net //
// //
// getid3.lib.php - part of getID3() //
// see readme.txt for more details //
// ///
/////////////////////////////////////////////////////////////////
class getid3_lib
{
/**
* @param string $string
* @param bool $hex
* @param bool $spaces
* @param string|bool $htmlencoding
*
* @return string
*/
public static function PrintHexBytes($string, $hex=true, $spaces=true, $htmlencoding='UTF-8') {
$returnstring = '';
for ($i = 0; $i < strlen($string); $i++) {
if ($hex) {
$returnstring .= str_pad(dechex(ord($string[$i])), 2, '0', STR_PAD_LEFT);
} else {
$returnstring .= ' '.(preg_match("#[\x20-\x7E]#", $string[$i]) ? $string[$i] : '¤');
}
if ($spaces) {
$returnstring .= ' ';
}
}
if (!empty($htmlencoding)) {
if ($htmlencoding === true) {
$htmlencoding = 'UTF-8'; // prior to getID3 v1.9.0 the function's 4th parameter was boolean
}
$returnstring = htmlentities($returnstring, ENT_QUOTES, $htmlencoding);
}
return $returnstring;
}
/**
* Truncates a floating-point number at the decimal point.
*
* @param float $floatnumber
*
* @return float|int returns int (if possible, otherwise float)
*/
public static function trunc($floatnumber) {
if ($floatnumber >= 1) {
$truncatednumber = floor($floatnumber);
} elseif ($floatnumber <= -1) {
$truncatednumber = ceil($floatnumber);
} else {
$truncatednumber = 0;
}
if (self::intValueSupported($truncatednumber)) {
$truncatednumber = (int) $truncatednumber;
}
return $truncatednumber;
}
/**
* @param int|null $variable
* @param int $increment
*
* @return bool
*/
public static function safe_inc(&$variable, $increment=1) {
if (isset($variable)) {
$variable += $increment;
} else {
$variable = $increment;
}
return true;
}
/**
* @param int|float $floatnum
*
* @return int|float
*/
public static function CastAsInt($floatnum) {
// convert to float if not already
$floatnum = (float) $floatnum;
// convert a float to type int, only if possible
if (self::trunc($floatnum) == $floatnum) {
// it's not floating point
if (self::intValueSupported($floatnum)) {
// it's within int range
$floatnum = (int) $floatnum;
}
}
return $floatnum;
}
/**
* @param int $num
*
* @return bool
*/
public static function intValueSupported($num) {
// check if integers are 64-bit
static $hasINT64 = null;
if ($hasINT64 === null) { // 10x faster than is_null()
$hasINT64 = is_int(pow(2, 31)); // 32-bit int are limited to (2^31)-1
if (!$hasINT64 && !defined('PHP_INT_MIN')) {
define('PHP_INT_MIN', ~PHP_INT_MAX);
}
}
// if integers are 64-bit - no other check required
if ($hasINT64 || (($num <= PHP_INT_MAX) && ($num >= PHP_INT_MIN))) {
return true;
}
return false;
}
/**
* @param string $fraction
*
* @return float
*/
public static function DecimalizeFraction($fraction) {
list($numerator, $denominator) = explode('/', $fraction);
return $numerator / ($denominator ? $denominator : 1);
}
/**
* @param string $binarynumerator
*
* @return float
*/
public static function DecimalBinary2Float($binarynumerator) {
$numerator = self::Bin2Dec($binarynumerator);
$denominator = self::Bin2Dec('1'.str_repeat('0', strlen($binarynumerator)));
return ($numerator / $denominator);
}
/**
* @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
*
* @param string $binarypointnumber
* @param int $maxbits
*
* @return array
*/
public static function NormalizeBinaryPoint($binarypointnumber, $maxbits=52) {
if (strpos($binarypointnumber, '.') === false) {
$binarypointnumber = '0.'.$binarypointnumber;
} elseif ($binarypointnumber[0] == '.') {
$binarypointnumber = '0'.$binarypointnumber;
}
$exponent = 0;
while (($binarypointnumber[0] != '1') || (substr($binarypointnumber, 1, 1) != '.')) {
if (substr($binarypointnumber, 1, 1) == '.') {
$exponent--;
$binarypointnumber = substr($binarypointnumber, 2, 1).'.'.substr($binarypointnumber, 3);
} else {
$pointpos = strpos($binarypointnumber, '.');
$exponent += ($pointpos - 1);
$binarypointnumber = str_replace('.', '', $binarypointnumber);
$binarypointnumber = $binarypointnumber[0].'.'.substr($binarypointnumber, 1);
}
}
$binarypointnumber = str_pad(substr($binarypointnumber, 0, $maxbits + 2), $maxbits + 2, '0', STR_PAD_RIGHT);
return array('normalized'=>$binarypointnumber, 'exponent'=>(int) $exponent);
}
/**
* @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
*
* @param float $floatvalue
*
* @return string
*/
public static function Float2BinaryDecimal($floatvalue) {
$maxbits = 128; // to how many bits of precision should the calculations be taken?
$intpart = self::trunc($floatvalue);
$floatpart = abs($floatvalue - $intpart);
$pointbitstring = '';
while (($floatpart != 0) && (strlen($pointbitstring) < $maxbits)) {
$floatpart *= 2;
$pointbitstring .= (string) self::trunc($floatpart);
$floatpart -= self::trunc($floatpart);
}
$binarypointnumber = decbin($intpart).'.'.$pointbitstring;
return $binarypointnumber;
}
/**
* @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee-expl.html
*
* @param float $floatvalue
* @param int $bits
*
* @return string|false
*/
public static function Float2String($floatvalue, $bits) {
$exponentbits = 0;
$fractionbits = 0;
switch ($bits) {
case 32:
$exponentbits = 8;
$fractionbits = 23;
break;
case 64:
$exponentbits = 11;
$fractionbits = 52;
break;
default:
return false;
}
if ($floatvalue >= 0) {
$signbit = '0';
} else {
$signbit = '1';
}
$normalizedbinary = self::NormalizeBinaryPoint(self::Float2BinaryDecimal($floatvalue), $fractionbits);
$biasedexponent = pow(2, $exponentbits - 1) - 1 + $normalizedbinary['exponent']; // (127 or 1023) +/- exponent
$exponentbitstring = str_pad(decbin($biasedexponent), $exponentbits, '0', STR_PAD_LEFT);
$fractionbitstring = str_pad(substr($normalizedbinary['normalized'], 2), $fractionbits, '0', STR_PAD_RIGHT);
return self::BigEndian2String(self::Bin2Dec($signbit.$exponentbitstring.$fractionbitstring), $bits % 8, false);
}
/**
* @param string $byteword
*
* @return float|false
*/
public static function LittleEndian2Float($byteword) {
return self::BigEndian2Float(strrev($byteword));
}
/**
* ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic
*
* @link http://www.psc.edu/general/software/packages/ieee/ieee.html
* @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html
*
* @param string $byteword
*
* @return float|false
*/
public static function BigEndian2Float($byteword) {
$bitword = self::BigEndian2Bin($byteword);
if (!$bitword) {
return 0;
}
$signbit = $bitword[0];
$floatvalue = 0;
$exponentbits = 0;
$fractionbits = 0;
switch (strlen($byteword) * 8) {
case 32:
$exponentbits = 8;
$fractionbits = 23;
break;
case 64:
$exponentbits = 11;
$fractionbits = 52;
break;
case 80:
// 80-bit Apple SANE format
// http://www.mactech.com/articles/mactech/Vol.06/06.01/SANENormalized/
$exponentstring = substr($bitword, 1, 15);
$isnormalized = intval($bitword[16]);
$fractionstring = substr($bitword, 17, 63);
$exponent = pow(2, self::Bin2Dec($exponentstring) - 16383);
$fraction = $isnormalized + self::DecimalBinary2Float($fractionstring);
$floatvalue = $exponent * $fraction;
if ($signbit == '1') {
$floatvalue *= -1;
}
return $floatvalue;
default:
return false;
}
$exponentstring = substr($bitword, 1, $exponentbits);
$fractionstring = substr($bitword, $exponentbits + 1, $fractionbits);
$exponent = self::Bin2Dec($exponentstring);
$fraction = self::Bin2Dec($fractionstring);
if (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction != 0)) {
// Not a Number
$floatvalue = false;
} elseif (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction == 0)) {
if ($signbit == '1') {
$floatvalue = '-infinity';
} else {
$floatvalue = '+infinity';
}
} elseif (($exponent == 0) && ($fraction == 0)) {
if ($signbit == '1') {
$floatvalue = -0;
} else {
$floatvalue = 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);
if ($signbit == '1') {
$floatvalue *= -1;
}
} elseif ($exponent != 0) {
$floatvalue = pow(2, ($exponent - (pow(2, $exponentbits - 1) - 1))) * (1 + self::DecimalBinary2Float($fractionstring));
if ($signbit == '1') {
$floatvalue *= -1;
}
}
return (float) $floatvalue;
}
/**
* @param string $byteword
* @param bool $synchsafe
* @param bool $signed
*
* @return int|float|false
* @throws Exception
*/
public static function BigEndian2Int($byteword, $synchsafe=false, $signed=false) {
$intvalue = 0;
$bytewordlen = strlen($byteword);
if ($bytewordlen == 0) {
return false;
}
for ($i = 0; $i < $bytewordlen; $i++) {
if ($synchsafe) { // disregard MSB, effectively 7-bit bytes
//$intvalue = $intvalue | (ord($byteword{$i}) & 0x7F) << (($bytewordlen - 1 - $i) * 7); // faster, but runs into problems past 2^31 on 32-bit systems
$intvalue += (ord($byteword[$i]) & 0x7F) * pow(2, ($bytewordlen - 1 - $i) * 7);
} else {
$intvalue += ord($byteword[$i]) * pow(256, ($bytewordlen - 1 - $i));
}
}
if ($signed && !$synchsafe) {
// synchsafe ints are not allowed to be signed
if ($bytewordlen <= PHP_INT_SIZE) {
$signMaskBit = 0x80 << (8 * ($bytewordlen - 1));
if ($intvalue & $signMaskBit) {
$intvalue = 0 - ($intvalue & ($signMaskBit - 1));
}
} else {
throw new Exception('ERROR: Cannot have signed integers larger than '.(8 * PHP_INT_SIZE).'-bits ('.strlen($byteword).') in self::BigEndian2Int()');
}
}
return self::CastAsInt($intvalue);
}
/**
* @param string $byteword
* @param bool $signed
*
* @return int|float|false
*/
public static function LittleEndian2Int($byteword, $signed=false) {
return self::BigEndian2Int(strrev($byteword), false, $signed);
}
/**
* @param string $byteword
*
* @return string
*/
public static function LittleEndian2Bin($byteword) {
return self::BigEndian2Bin(strrev($byteword));
}
/**
* @param string $byteword
*
* @return string
*/
public static function BigEndian2Bin($byteword) {
$binvalue = '';
$bytewordlen = strlen($byteword);
for ($i = 0; $i < $bytewordlen; $i++) {
$binvalue .= str_pad(decbin(ord($byteword[$i])), 8, '0', STR_PAD_LEFT);
}
return $binvalue;
}
/**
* @param int $number
* @param int $minbytes
* @param bool $synchsafe
* @param bool $signed
*
* @return string
* @throws Exception
*/
public static function BigEndian2String($number, $minbytes=1, $synchsafe=false, $signed=false) {
if ($number < 0) {
throw new Exception('ERROR: self::BigEndian2String() does not support negative numbers');
}
$maskbyte = (($synchsafe || $signed) ? 0x7F : 0xFF);
$intstring = '';
if ($signed) {
if ($minbytes > PHP_INT_SIZE) {
throw new Exception('ERROR: Cannot have signed integers larger than '.(8 * PHP_INT_SIZE).'-bits in self::BigEndian2String()');
}
$number = $number & (0x80 << (8 * ($minbytes - 1)));
}
while ($number != 0) {
$quotient = ($number / ($maskbyte + 1));
$intstring = chr(ceil(($quotient - floor($quotient)) * $maskbyte)).$intstring;
$number = floor($quotient);
}
return str_pad($intstring, $minbytes, "\x00", STR_PAD_LEFT);
}
/**
* @param int $number
*
* @return string
*/
public static function Dec2Bin($number) {
while ($number >= 256) {
$bytes[] = (($number / 256) - (floor($number / 256))) * 256;
$number = floor($number / 256);
}
$bytes[] = $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;
}
return $binstring;
}
/**
* @param string $binstring
* @param bool $signed
*
* @return int|float
*/
public static function Bin2Dec($binstring, $signed=false) {
$signmult = 1;
if ($signed) {
if ($binstring[0] == '1') {
$signmult = -1;
}
$binstring = substr($binstring, 1);
}
$decvalue = 0;
for ($i = 0; $i < strlen($binstring); $i++) {
$decvalue += ((int) substr($binstring, strlen($binstring) - $i - 1, 1)) * pow(2, $i);
}
return self::CastAsInt($decvalue * $signmult);
}
/**
* @param string $binstring
*
* @return string
*/
public static function Bin2String($binstring) {
// return 'hi' for input of '0110100001101001'
$string = '';
$binstringreversed = strrev($binstring);
for ($i = 0; $i < strlen($binstringreversed); $i += 8) {
$string = chr(self::Bin2Dec(strrev(substr($binstringreversed, $i, 8)))).$string;
}
return $string;
}
/**
* @param int $number
* @param int $minbytes
* @param bool $synchsafe
*
* @return string
*/
public static function LittleEndian2String($number, $minbytes=1, $synchsafe=false) {
$intstring = '';
while ($number > 0) {
if ($synchsafe) {
$intstring = $intstring.chr($number & 127);
$number >>= 7;
} else {
$intstring = $intstring.chr($number & 255);
$number >>= 8;
}
}
return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
}
/**
* @param mixed $array1
* @param mixed $array2
*
* @return array|false
*/
public static function array_merge_clobber($array1, $array2) {
// written by kcØhireability*com
// taken from http://www.php.net/manual/en/function.array-merge-recursive.php
if (!is_array($array1) || !is_array($array2)) {
return false;
}
$newarray = $array1;
foreach ($array2 as $key => $val) {
if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) {
$newarray[$key] = self::array_merge_clobber($newarray[$key], $val);
} else {
$newarray[$key] = $val;
}
}
return $newarray;
}
/**
* @param mixed $array1
* @param mixed $array2
*
* @return array|false
*/
public static function array_merge_noclobber($array1, $array2) {
if (!is_array($array1) || !is_array($array2)) {
return false;
}
$newarray = $array1;
foreach ($array2 as $key => $val) {
if (is_array($val) && isset($newarray[$key]) && is_array($newarray[$key])) {
$newarray[$key] = self::array_merge_noclobber($newarray[$key], $val);
} elseif (!isset($newarray[$key])) {
$newarray[$key] = $val;
}
}
return $newarray;
}
/**
* @param mixed $array1
* @param mixed $array2
*
* @return array|false|null
*/
public static function flipped_array_merge_noclobber($array1, $array2) {
if (!is_array($array1) || !is_array($array2)) {
return false;
}
# naturally, this only works non-recursively
$newarray = array_flip($array1);
foreach (array_flip($array2) as $key => $val) {
if (!isset($newarray[$key])) {
$newarray[$key] = count($newarray);
}
}
return array_flip($newarray);
}
/**
* @param array $theArray
*
* @return bool
*/
public static function ksort_recursive(&$theArray) {
ksort($theArray);
foreach ($theArray as $key => $value) {
if (is_array($value)) {
self::ksort_recursive($theArray[$key]);
}
}
return true;
}
/**
* @param string $filename
* @param int $numextensions
*
* @return string
*/
public static function fileextension($filename, $numextensions=1) {
if (strstr($filename, '.')) {
$reversedfilename = strrev($filename);
$offset = 0;
for ($i = 0; $i < $numextensions; $i++) {
$offset = strpos($reversedfilename, '.', $offset + 1);
if ($offset === false) {
return '';
}
}
return strrev(substr($reversedfilename, 0, $offset));
}
return '';
}
/**
* @param int $seconds
*
* @return string
*/
public static function PlaytimeString($seconds) {
$sign = (($seconds < 0) ? '-' : '');
$seconds = round(abs($seconds));
$H = (int) floor( $seconds / 3600);
$M = (int) floor(($seconds - (3600 * $H) ) / 60);
$S = (int) round( $seconds - (3600 * $H) - (60 * $M) );
return $sign.($H ? $H.':' : '').($H ? str_pad($M, 2, '0', STR_PAD_LEFT) : intval($M)).':'.str_pad($S, 2, 0, STR_PAD_LEFT);
}
/**
* @param int $macdate
*
* @return int|float
*/
public static function DateMac2Unix($macdate) {
// Macintosh timestamp: seconds since 00:00h January 1, 1904
// UNIX timestamp: seconds since 00:00h January 1, 1970
return self::CastAsInt($macdate - 2082844800);
}
/**
* @param string $rawdata
*
* @return float
*/
public static function FixedPoint8_8($rawdata) {
return self::BigEndian2Int(substr($rawdata, 0, 1)) + (float) (self::BigEndian2Int(substr($rawdata, 1, 1)) / pow(2, 8));
}
/**
* @param string $rawdata
*
* @return float
*/
public static function FixedPoint16_16($rawdata) {
return self::BigEndian2Int(substr($rawdata, 0, 2)) + (float) (self::BigEndian2Int(substr($rawdata, 2, 2)) / pow(2, 16));
}
/**
* @param string $rawdata
*
* @return float
*/
public static function FixedPoint2_30($rawdata) {
$binarystring = self::BigEndian2Bin($rawdata);
return self::Bin2Dec(substr($binarystring, 0, 2)) + (float) (self::Bin2Dec(substr($binarystring, 2, 30)) / pow(2, 30));
}
/**
* @param string $ArrayPath
* @param string $Separator
* @param mixed $Value
*
* @return array
*/
public static function CreateDeepArray($ArrayPath, $Separator, $Value) {
// assigns $Value to a nested array path:
// $foo = self::CreateDeepArray('/path/to/my', '/', 'file.txt')
// is the same as:
// $foo = array('path'=>array('to'=>'array('my'=>array('file.txt'))));
// or
// $foo['path']['to']['my'] = 'file.txt';
$ArrayPath = ltrim($ArrayPath, $Separator);
if (($pos = strpos($ArrayPath, $Separator)) !== false) {
$ReturnedArray[substr($ArrayPath, 0, $pos)] = self::CreateDeepArray(substr($ArrayPath, $pos + 1), $Separator, $Value);
} else {
$ReturnedArray[$ArrayPath] = $Value;
}
return $ReturnedArray;
}
/**
* @param array $arraydata
* @param bool $returnkey
*
* @return int|false
*/
public static function array_max($arraydata, $returnkey=false) {
$maxvalue = false;
$maxkey = false;
foreach ($arraydata as $key => $value) {
if (!is_array($value)) {
if (($maxvalue === false) || ($value > $maxvalue)) {
$maxvalue = $value;
$maxkey = $key;
}
}
}
return ($returnkey ? $maxkey : $maxvalue);
}
/**
* @param array $arraydata
* @param bool $returnkey
*
* @return int|false
*/
public static function array_min($arraydata, $returnkey=false) {
$minvalue = false;
$minkey = false;
foreach ($arraydata as $key => $value) {
if (!is_array($value)) {
if (($minvalue === false) || ($value < $minvalue)) {
$minvalue = $value;
$minkey = $key;
}
}
}
return ($returnkey ? $minkey : $minvalue);
}
/**
* @param string $XMLstring
*
* @return array|false
*/
public static function XML2array($XMLstring) {
if (function_exists('simplexml_load_string') && function_exists('libxml_disable_entity_loader')) {
// http://websec.io/2012/08/27/Preventing-XEE-in-PHP.html
// https://core.trac.wordpress.org/changeset/29378
$loader = libxml_disable_entity_loader(true);
$XMLobject = simplexml_load_string($XMLstring, 'SimpleXMLElement', LIBXML_NOENT);
$return = self::SimpleXMLelement2array($XMLobject);
libxml_disable_entity_loader($loader);
return $return;
}
return false;
}
/**
* @param SimpleXMLElement|array|mixed $XMLobject
*
* @return mixed
*/
public static function SimpleXMLelement2array($XMLobject) {
if (!is_object($XMLobject) && !is_array($XMLobject)) {
return $XMLobject;
}
$XMLarray = $XMLobject instanceof SimpleXMLElement ? get_object_vars($XMLobject) : $XMLobject;
foreach ($XMLarray as $key => $value) {
$XMLarray[$key] = self::SimpleXMLelement2array($value);
}
return $XMLarray;
}
/**
* Returns checksum for a file from starting position to absolute end position.
*
* @param string $file
* @param int $offset
* @param int $end
* @param string $algorithm
*
* @return string|false
* @throws getid3_exception
*/
public static function hash_data($file, $offset, $end, $algorithm) {
if (!self::intValueSupported($end)) {
return false;
}
if (!in_array($algorithm, array('md5', 'sha1'))) {
throw new getid3_exception('Invalid algorithm ('.$algorithm.') in self::hash_data()');
}
$size = $end - $offset;
$fp = fopen($file, 'rb');
fseek($fp, $offset);
$ctx = hash_init($algorithm);
while ($size > 0) {
$buffer = fread($fp, min($size, getID3::FREAD_BUFFER_SIZE));
hash_update($ctx, $buffer);
$size -= getID3::FREAD_BUFFER_SIZE;
}
$hash = hash_final($ctx);
fclose($fp);
return $hash;
}
/**
* @param string $filename_source
* @param string $filename_dest
* @param int $offset
* @param int $length
*
* @return bool
* @throws Exception
*
* @deprecated Unused, may be removed in future versions of getID3
*/
public static function CopyFileParts($filename_source, $filename_dest, $offset, $length) {
if (!self::intValueSupported($offset + $length)) {
throw new Exception('cannot copy file portion, it extends beyond the '.round(PHP_INT_MAX / 1073741824).'GB limit');
}
if (is_readable($filename_source) && is_file($filename_source) && ($fp_src = fopen($filename_source, 'rb'))) {
if (($fp_dest = fopen($filename_dest, 'wb'))) {
if (fseek($fp_src, $offset) == 0) {
$byteslefttowrite = $length;
while (($byteslefttowrite > 0) && ($buffer = fread($fp_src, min($byteslefttowrite, getID3::FREAD_BUFFER_SIZE)))) {
$byteswritten = fwrite($fp_dest, $buffer, $byteslefttowrite);
$byteslefttowrite -= $byteswritten;
}
fclose($fp_dest);
return true;
} else {
fclose($fp_src);
throw new Exception('failed to seek to offset '.$offset.' in '.$filename_source);
}
} else {
throw new Exception('failed to create file for writing '.$filename_dest);
}
} else {
throw new Exception('failed to open file for reading '.$filename_source);
}
}
/**
* @param int $charval
*
* @return string
*/
public static function iconv_fallback_int_utf8($charval) {
if ($charval < 128) {
// 0bbbbbbb
$newcharstring = chr($charval);
} elseif ($charval < 2048) {
// 110bbbbb 10bbbbbb
$newcharstring = chr(($charval >> 6) | 0xC0);
$newcharstring .= chr(($charval & 0x3F) | 0x80);
} elseif ($charval < 65536) {
// 1110bbbb 10bbbbbb 10bbbbbb
$newcharstring = chr(($charval >> 12) | 0xE0);
$newcharstring .= chr(($charval >> 6) | 0xC0);
$newcharstring .= chr(($charval & 0x3F) | 0x80);
} else {
// 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
$newcharstring = chr(($charval >> 18) | 0xF0);
$newcharstring .= chr(($charval >> 12) | 0xC0);
$newcharstring .= chr(($charval >> 6) | 0xC0);
$newcharstring .= chr(($charval & 0x3F) | 0x80);
}
return $newcharstring;
}
/**
* ISO-8859-1 => UTF-8
*
* @param string $string
* @param bool $bom
*
* @return string
*/
public static function iconv_fallback_iso88591_utf8($string, $bom=false) {
if (function_exists('utf8_encode')) {
return utf8_encode($string);
}
// utf8_encode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support)
$newcharstring = '';
if ($bom) {
$newcharstring .= "\xEF\xBB\xBF";
}
for ($i = 0; $i < strlen($string); $i++) {
$charval = ord($string[$i]);
$newcharstring .= self::iconv_fallback_int_utf8($charval);
}
return $newcharstring;
}
/**
* ISO-8859-1 => UTF-16BE
*
* @param string $string
* @param bool $bom
*
* @return string
*/
public static function iconv_fallback_iso88591_utf16be($string, $bom=false) {
$newcharstring = '';
if ($bom) {
$newcharstring .= "\xFE\xFF";
}
for ($i = 0; $i < strlen($string); $i++) {
$newcharstring .= "\x00".$string[$i];
}
return $newcharstring;
}
/**
* ISO-8859-1 => UTF-16LE
*
* @param string $string
* @param bool $bom
*
* @return string
*/
public static function iconv_fallback_iso88591_utf16le($string, $bom=false) {
$newcharstring = '';
if ($bom) {
$newcharstring .= "\xFF\xFE";
}
for ($i = 0; $i < strlen($string); $i++) {
$newcharstring .= $string[$i]."\x00";
}
return $newcharstring;
}
/**
* ISO-8859-1 => UTF-16LE (BOM)
*
* @param string $string
*
* @return string
*/
public static function iconv_fallback_iso88591_utf16($string) {
return self::iconv_fallback_iso88591_utf16le($string, true);
}
/**
* UTF-8 => ISO-8859-1
*
* @param string $string
*
* @return string
*/
public static function iconv_fallback_utf8_iso88591($string) {
if (function_exists('utf8_decode')) {
return utf8_decode($string);
}
// utf8_decode() unavailable, use getID3()'s iconv_fallback() conversions (possibly PHP is compiled without XML support)
$newcharstring = '';
$offset = 0;
$stringlength = strlen($string);
while ($offset < $stringlength) {
if ((ord($string[$offset]) | 0x07) == 0xF7) {
// 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
$charval = ((ord($string[($offset + 0)]) & 0x07) << 18) &
((ord($string[($offset + 1)]) & 0x3F) << 12) &
((ord($string[($offset + 2)]) & 0x3F) << 6) &
(ord($string[($offset + 3)]) & 0x3F);
$offset += 4;
} elseif ((ord($string[$offset]) | 0x0F) == 0xEF) {
// 1110bbbb 10bbbbbb 10bbbbbb
$charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) &
((ord($string[($offset + 1)]) & 0x3F) << 6) &
(ord($string[($offset + 2)]) & 0x3F);
$offset += 3;
} elseif ((ord($string[$offset]) | 0x1F) == 0xDF) {
// 110bbbbb 10bbbbbb
$charval = ((ord($string[($offset + 0)]) & 0x1F) << 6) &
(ord($string[($offset + 1)]) & 0x3F);
$offset += 2;
} elseif ((ord($string[$offset]) | 0x7F) == 0x7F) {
// 0bbbbbbb
$charval = ord($string[$offset]);
$offset += 1;
} else {
// error? throw some kind of warning here?
$charval = false;
$offset += 1;
}
if ($charval !== false) {
$newcharstring .= (($charval < 256) ? chr($charval) : '?');
}
}
return $newcharstring;
}
/**
* UTF-8 => UTF-16BE
*
* @param string $string
* @param bool $bom
*
* @return string
*/
public static function iconv_fallback_utf8_utf16be($string, $bom=false) {
$newcharstring = '';
if ($bom) {
$newcharstring .= "\xFE\xFF";
}
$offset = 0;
$stringlength = strlen($string);
while ($offset < $stringlength) {
if ((ord($string[$offset]) | 0x07) == 0xF7) {
// 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
$charval = ((ord($string[($offset + 0)]) & 0x07) << 18) &
((ord($string[($offset + 1)]) & 0x3F) << 12) &
((ord($string[($offset + 2)]) & 0x3F) << 6) &
(ord($string[($offset + 3)]) & 0x3F);
$offset += 4;
} elseif ((ord($string[$offset]) | 0x0F) == 0xEF) {
// 1110bbbb 10bbbbbb 10bbbbbb
$charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) &
((ord($string[($offset + 1)]) & 0x3F) << 6) &
(ord($string[($offset + 2)]) & 0x3F);
$offset += 3;
} elseif ((ord($string[$offset]) | 0x1F) == 0xDF) {
// 110bbbbb 10bbbbbb
$charval = ((ord($string[($offset + 0)]) & 0x1F) << 6) &
(ord($string[($offset + 1)]) & 0x3F);
$offset += 2;
} elseif ((ord($string[$offset]) | 0x7F) == 0x7F) {
// 0bbbbbbb
$charval = ord($string[$offset]);
$offset += 1;
} else {
// error? throw some kind of warning here?
$charval = false;
$offset += 1;
}
if ($charval !== false) {
$newcharstring .= (($charval < 65536) ? self::BigEndian2String($charval, 2) : "\x00".'?');
}
}
return $newcharstring;
}
/**
* UTF-8 => UTF-16LE
*
* @param string $string
* @param bool $bom
*
* @return string
*/
public static function iconv_fallback_utf8_utf16le($string, $bom=false) {
$newcharstring = '';
if ($bom) {
$newcharstring .= "\xFF\xFE";
}
$offset = 0;
$stringlength = strlen($string);
while ($offset < $stringlength) {
if ((ord($string[$offset]) | 0x07) == 0xF7) {
// 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
$charval = ((ord($string[($offset + 0)]) & 0x07) << 18) &
((ord($string[($offset + 1)]) & 0x3F) << 12) &
((ord($string[($offset + 2)]) & 0x3F) << 6) &
(ord($string[($offset + 3)]) & 0x3F);
$offset += 4;
} elseif ((ord($string[$offset]) | 0x0F) == 0xEF) {
// 1110bbbb 10bbbbbb 10bbbbbb
$charval = ((ord($string[($offset + 0)]) & 0x0F) << 12) &
((ord($string[($offset + 1)]) & 0x3F) << 6) &
(ord($string[($offset + 2)]) & 0x3F);
$offset += 3;
} elseif ((ord($string[$offset]) | 0x1F) == 0xDF) {
// 110bbbbb 10bbbbbb
$charval = ((ord($string[($offset + 0)]) & 0x1F) << 6) &
(ord($string[($offset + 1)]) & 0x3F);
$offset += 2;
} elseif ((ord($string[$offset]) | 0x7F) == 0x7F) {
// 0bbbbbbb
$charval = ord($string[$offset]);
$offset += 1;
} else {
// error? maybe throw some warning here?
$charval = false;
$offset += 1;
}
if ($charval !== false) {
$newcharstring .= (($charval < 65536) ? self::LittleEndian2String($charval, 2) : '?'."\x00");
}
}
return $newcharstring;
}
/**
* UTF-8 => UTF-16LE (BOM)
*
* @param string $string
*
* @return string
*/
public static function iconv_fallback_utf8_utf16($string) {
return self::iconv_fallback_utf8_utf16le($string, true);
}
/**
* UTF-16BE => UTF-8
*
* @param string $string
*
* @return string
*/
public static function iconv_fallback_utf16be_utf8($string) {
if (substr($string, 0, 2) == "\xFE\xFF") {
// strip BOM
$string = substr($string, 2);
}
$newcharstring = '';
for ($i = 0; $i < strlen($string); $i += 2) {
$charval = self::BigEndian2Int(substr($string, $i, 2));
$newcharstring .= self::iconv_fallback_int_utf8($charval);
}
return $newcharstring;
}
/**
* UTF-16LE => UTF-8
*
* @param string $string
*
* @return string
*/
public static function iconv_fallback_utf16le_utf8($string) {
if (substr($string, 0, 2) == "\xFF\xFE") {
// strip BOM
$string = substr($string, 2);
}
$newcharstring = '';
for ($i = 0; $i < strlen($string); $i += 2) {
$charval = self::LittleEndian2Int(substr($string, $i, 2));
$newcharstring .= self::iconv_fallback_int_utf8($charval);
}
return $newcharstring;
}
/**
* UTF-16BE => ISO-8859-1
*
* @param string $string
*
* @return string
*/
public static function iconv_fallback_utf16be_iso88591($string) {
if (substr($string, 0, 2) == "\xFE\xFF") {
// strip BOM
$string = substr($string, 2);
}
$newcharstring = '';
for ($i = 0; $i < strlen($string); $i += 2) {
$charval = self::BigEndian2Int(substr($string, $i, 2));
$newcharstring .= (($charval < 256) ? chr($charval) : '?');
}
return $newcharstring;
}
/**
* UTF-16LE => ISO-8859-1
*
* @param string $string
*
* @return string
*/
public static function iconv_fallback_utf16le_iso88591($string) {
if (substr($string, 0, 2) == "\xFF\xFE") {
// strip BOM
$string = substr($string, 2);
}
$newcharstring = '';
for ($i = 0; $i < strlen($string); $i += 2) {
$charval = self::LittleEndian2Int(substr($string, $i, 2));
$newcharstring .= (($charval < 256) ? chr($charval) : '?');
}
return $newcharstring;
}
/**
* UTF-16 (BOM) => ISO-8859-1
*
* @param string $string
*
* @return string
*/
public static function iconv_fallback_utf16_iso88591($string) {
$bom = substr($string, 0, 2);
if ($bom == "\xFE\xFF") {
return self::iconv_fallback_utf16be_iso88591(substr($string, 2));
} elseif ($bom == "\xFF\xFE") {
return self::iconv_fallback_utf16le_iso88591(substr($string, 2));
}
return $string;
}
/**
* UTF-16 (BOM) => UTF-8
*
* @param string $string
*
* @return string
*/
public static function iconv_fallback_utf16_utf8($string) {
$bom = substr($string, 0, 2);
if ($bom == "\xFE\xFF") {
return self::iconv_fallback_utf16be_utf8(substr($string, 2));
} elseif ($bom == "\xFF\xFE") {
return self::iconv_fallback_utf16le_utf8(substr($string, 2));
}
return $string;
}
/**
* @param string $in_charset
* @param string $out_charset
* @param string $string
*
* @return string
* @throws Exception
*/
public static function iconv_fallback($in_charset, $out_charset, $string) {
if ($in_charset == $out_charset) {
return $string;
}
// mb_convert_encoding() available
if (function_exists('mb_convert_encoding')) {
if ((strtoupper($in_charset) == 'UTF-16') && (substr($string, 0, 2) != "\xFE\xFF") && (substr($string, 0, 2) != "\xFF\xFE")) {
// if BOM missing, mb_convert_encoding will mishandle the conversion, assume UTF-16BE and prepend appropriate BOM
$string = "\xFF\xFE".$string;
}
if ((strtoupper($in_charset) == 'UTF-16') && (strtoupper($out_charset) == 'UTF-8')) {
if (($string == "\xFF\xFE") || ($string == "\xFE\xFF")) {
// if string consists of only BOM, mb_convert_encoding will return the BOM unmodified
return '';
}
}
if ($converted_string = @mb_convert_encoding($string, $out_charset, $in_charset)) {
switch ($out_charset) {
case 'ISO-8859-1':
$converted_string = rtrim($converted_string, "\x00");
break;
}
return $converted_string;
}
return $string;
// iconv() available
} elseif (function_exists('iconv')) {
if ($converted_string = @iconv($in_charset, $out_charset.'//TRANSLIT', $string)) {
switch ($out_charset) {
case 'ISO-8859-1':
$converted_string = rtrim($converted_string, "\x00");
break;
}
return $converted_string;
}
// iconv() may sometimes fail with "illegal character in input string" error message
// and return an empty string, but returning the unconverted string is more useful
return $string;
}
// neither mb_convert_encoding or iconv() is available
static $ConversionFunctionList = array();
if (empty($ConversionFunctionList)) {
$ConversionFunctionList['ISO-8859-1']['UTF-8'] = 'iconv_fallback_iso88591_utf8';
$ConversionFunctionList['ISO-8859-1']['UTF-16'] = 'iconv_fallback_iso88591_utf16';
$ConversionFunctionList['ISO-8859-1']['UTF-16BE'] = 'iconv_fallback_iso88591_utf16be';
$ConversionFunctionList['ISO-8859-1']['UTF-16LE'] = 'iconv_fallback_iso88591_utf16le';
$ConversionFunctionList['UTF-8']['ISO-8859-1'] = 'iconv_fallback_utf8_iso88591';
$ConversionFunctionList['UTF-8']['UTF-16'] = 'iconv_fallback_utf8_utf16';
$ConversionFunctionList['UTF-8']['UTF-16BE'] = 'iconv_fallback_utf8_utf16be';
$ConversionFunctionList['UTF-8']['UTF-16LE'] = 'iconv_fallback_utf8_utf16le';
$ConversionFunctionList['UTF-16']['ISO-8859-1'] = 'iconv_fallback_utf16_iso88591';
$ConversionFunctionList['UTF-16']['UTF-8'] = 'iconv_fallback_utf16_utf8';
$ConversionFunctionList['UTF-16LE']['ISO-8859-1'] = 'iconv_fallback_utf16le_iso88591';
$ConversionFunctionList['UTF-16LE']['UTF-8'] = 'iconv_fallback_utf16le_utf8';
$ConversionFunctionList['UTF-16BE']['ISO-8859-1'] = 'iconv_fallback_utf16be_iso88591';
$ConversionFunctionList['UTF-16BE']['UTF-8'] = 'iconv_fallback_utf16be_utf8';
}
if (isset($ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)])) {
$ConversionFunction = $ConversionFunctionList[strtoupper($in_charset)][strtoupper($out_charset)];
return self::$ConversionFunction($string);
}
throw new Exception('PHP does not has mb_convert_encoding() or iconv() support - cannot convert from '.$in_charset.' to '.$out_charset);
}
/**
* @param mixed $data
* @param string $charset
*
* @return mixed
*/
public static function recursiveMultiByteCharString2HTML($data, $charset='ISO-8859-1') {
if (is_string($data)) {
return self::MultiByteCharString2HTML($data, $charset);
} elseif (is_array($data)) {
$return_data = array();
foreach ($data as $key => $value) {
$return_data[$key] = self::recursiveMultiByteCharString2HTML($value, $charset);
}
return $return_data;
}
// integer, float, objects, resources, etc
return $data;
}
/**
* @param string|int|float $string
* @param string $charset
*
* @return string
*/
public static function MultiByteCharString2HTML($string, $charset='ISO-8859-1') {
$string = (string) $string; // in case trying to pass a numeric (float, int) string, would otherwise return an empty string
$HTMLstring = '';
switch (strtolower($charset)) {
case '1251':
case '1252':
case '866':
case '932':
case '936':
case '950':
case 'big5':
case 'big5-hkscs':
case 'cp1251':
case 'cp1252':
case 'cp866':
case 'euc-jp':
case 'eucjp':
case 'gb2312':
case 'ibm866':
case 'iso-8859-1':
case 'iso-8859-15':
case 'iso8859-1':
case 'iso8859-15':
case 'koi8-r':
case 'koi8-ru':
case 'koi8r':
case 'shift_jis':
case 'sjis':
case 'win-1251':
case 'windows-1251':
case 'windows-1252':
$HTMLstring = htmlentities($string, ENT_COMPAT, $charset);
break;
case 'utf-8':
$strlen = strlen($string);
for ($i = 0; $i < $strlen; $i++) {
$char_ord_val = ord($string[$i]);
$charval = 0;
if ($char_ord_val < 0x80) {
$charval = $char_ord_val;
} elseif ((($char_ord_val & 0xF0) >> 4) == 0x0F && $i+3 < $strlen) {
$charval = (($char_ord_val & 0x07) << 18);
$charval += ((ord($string[++$i]) & 0x3F) << 12);
$charval += ((ord($string[++$i]) & 0x3F) << 6);
$charval += (ord($string[++$i]) & 0x3F);
} elseif ((($char_ord_val & 0xE0) >> 5) == 0x07 && $i+2 < $strlen) {
$charval = (($char_ord_val & 0x0F) << 12);
$charval += ((ord($string[++$i]) & 0x3F) << 6);
$charval += (ord($string[++$i]) & 0x3F);
} elseif ((($char_ord_val & 0xC0) >> 6) == 0x03 && $i+1 < $strlen) {
$charval = (($char_ord_val & 0x1F) << 6);
$charval += (ord($string[++$i]) & 0x3F);
}
if (($charval >= 32) && ($charval <= 127)) {
$HTMLstring .= htmlentities(chr($charval));
} else {
$HTMLstring .= '&#'.$charval.';';
}
}
break;
case 'utf-16le':
for ($i = 0; $i < strlen($string); $i += 2) {
$charval = self::LittleEndian2Int(substr($string, $i, 2));
if (($charval >= 32) && ($charval <= 127)) {
$HTMLstring .= chr($charval);
} else {
$HTMLstring .= '&#'.$charval.';';
}
}
break;
case 'utf-16be':
for ($i = 0; $i < strlen($string); $i += 2) {
$charval = self::BigEndian2Int(substr($string, $i, 2));
if (($charval >= 32) && ($charval <= 127)) {
$HTMLstring .= chr($charval);
} else {
$HTMLstring .= '&#'.$charval.';';
}
}
break;
default:
$HTMLstring = 'ERROR: Character set "'.$charset.'" not supported in MultiByteCharString2HTML()';
break;
}
return $HTMLstring;
}
/**
* @param int $namecode
*
* @return string
*/
public static function RGADnameLookup($namecode) {
static $RGADname = array();
if (empty($RGADname)) {
$RGADname[0] = 'not set';
$RGADname[1] = 'Track Gain Adjustment';
$RGADname[2] = 'Album Gain Adjustment';
}
return (isset($RGADname[$namecode]) ? $RGADname[$namecode] : '');
}
/**
* @param int $originatorcode
*
* @return string
*/
public static function RGADoriginatorLookup($originatorcode) {
static $RGADoriginator = array();
if (empty($RGADoriginator)) {
$RGADoriginator[0] = 'unspecified';
$RGADoriginator[1] = 'pre-set by artist/producer/mastering engineer';
$RGADoriginator[2] = 'set by user';
$RGADoriginator[3] = 'determined automatically';
}
return (isset($RGADoriginator[$originatorcode]) ? $RGADoriginator[$originatorcode] : '');
}
/**
* @param int $rawadjustment
* @param int $signbit
*
* @return float
*/
public static function RGADadjustmentLookup($rawadjustment, $signbit) {
$adjustment = (float) $rawadjustment / 10;
if ($signbit == 1) {
$adjustment *= -1;
}
return $adjustment;
}
/**
* @param int $namecode
* @param int $originatorcode
* @param int $replaygain
*
* @return string
*/
public static function RGADgainString($namecode, $originatorcode, $replaygain) {
if ($replaygain < 0) {
$signbit = '1';
} else {
$signbit = '0';
}
$storedreplaygain = intval(round($replaygain * 10));
$gainstring = str_pad(decbin($namecode), 3, '0', STR_PAD_LEFT);
$gainstring .= str_pad(decbin($originatorcode), 3, '0', STR_PAD_LEFT);
$gainstring .= $signbit;
$gainstring .= str_pad(decbin($storedreplaygain), 9, '0', STR_PAD_LEFT);
return $gainstring;
}
/**
* @param float $amplitude
*
* @return float
*/
public static function RGADamplitude2dB($amplitude) {
return 20 * log10($amplitude);
}
/**
* @param string $imgData
* @param array $imageinfo
*
* @return array|false
*/
public static function GetDataImageSize($imgData, &$imageinfo=array()) {
if (PHP_VERSION_ID >= 50400) {
$GetDataImageSize = @getimagesizefromstring($imgData, $imageinfo);
if ($GetDataImageSize === false || !isset($GetDataImageSize[0], $GetDataImageSize[1])) {
return false;
}
$GetDataImageSize['height'] = $GetDataImageSize[0];
$GetDataImageSize['width'] = $GetDataImageSize[1];
return $GetDataImageSize;
}
static $tempdir = '';
if (empty($tempdir)) {
if (function_exists('sys_get_temp_dir')) {
$tempdir = sys_get_temp_dir(); // https://github.com/JamesHeinrich/getID3/issues/52
}
// yes this is ugly, feel free to suggest a better way
if (include_once(dirname(__FILE__).'/getid3.php')) {
$getid3_temp = new getID3();
if ($getid3_temp_tempdir = $getid3_temp->tempdir) {
$tempdir = $getid3_temp_tempdir;
}
unset($getid3_temp, $getid3_temp_tempdir);
}
}
$GetDataImageSize = false;
if ($tempfilename = tempnam($tempdir, 'gI3')) {
if (is_writable($tempfilename) && is_file($tempfilename) && ($tmp = fopen($tempfilename, 'wb'))) {
fwrite($tmp, $imgData);
fclose($tmp);
$GetDataImageSize = @getimagesize($tempfilename, $imageinfo);
if (($GetDataImageSize === false) || !isset($GetDataImageSize[0]) || !isset($GetDataImageSize[1])) {
return false;
}
$GetDataImageSize['height'] = $GetDataImageSize[0];
$GetDataImageSize['width'] = $GetDataImageSize[1];
}
unlink($tempfilename);
}
return $GetDataImageSize;
}
/**
* @param string $mime_type
*
* @return string
*/
public static function ImageExtFromMime($mime_type) {
// temporary way, works OK for now, but should be reworked in the future
return str_replace(array('image/', 'x-', 'jpeg'), array('', '', 'jpg'), $mime_type);
}
/**
* @param array $ThisFileInfo
* @param bool $option_tags_html default true (just as in the main getID3 class)
*
* @return bool
*/
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);
}
foreach ($ThisFileInfo['tags'] as $tagtype => $tagarray) {
foreach ($tagarray as $tagname => $tagdata) {
foreach ($tagdata as $key => $value) {
if (!empty($value)) {
if (empty($ThisFileInfo['comments'][$tagname])) {
// fall through and append value
} elseif ($tagtype == 'id3v1') {
$newvaluelength = strlen(trim($value));
foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) {
$oldvaluelength = strlen(trim($existingvalue));
if (($newvaluelength <= $oldvaluelength) && (substr($existingvalue, 0, $newvaluelength) == trim($value))) {
// new value is identical but shorter-than (or equal-length to) one already in comments - skip
break 2;
}
}
if (function_exists('mb_convert_encoding')) {
if (trim($value) == trim(substr(mb_convert_encoding($existingvalue, $ThisFileInfo['id3v1']['encoding'], $ThisFileInfo['encoding']), 0, 30))) {
// value stored in ID3v1 appears to be probably the multibyte value transliterated (badly) into ISO-8859-1 in ID3v1.
// As an example, Foobar2000 will do this if you tag a file with Chinese or Arabic or Cyrillic or something that doesn't fit into ISO-8859-1 the ID3v1 will consist of mostly "?" characters, one per multibyte unrepresentable character
break 2;
}
}
} elseif (!is_array($value)) {
$newvaluelength = strlen(trim($value));
foreach ($ThisFileInfo['comments'][$tagname] as $existingkey => $existingvalue) {
$oldvaluelength = strlen(trim($existingvalue));
if ((strlen($existingvalue) > 10) && ($newvaluelength > $oldvaluelength) && (substr(trim($value), 0, strlen($existingvalue)) == $existingvalue)) {
$ThisFileInfo['comments'][$tagname][$existingkey] = trim($value);
break;
}
}
}
if (is_array($value) || empty($ThisFileInfo['comments'][$tagname]) || !in_array(trim($value), $ThisFileInfo['comments'][$tagname])) {
$value = (is_string($value) ? trim($value) : $value);
if (!is_int($key) && !ctype_digit($key)) {
$ThisFileInfo['comments'][$tagname][$key] = $value;
} else {
if (!isset($ThisFileInfo['comments'][$tagname])) {
$ThisFileInfo['comments'][$tagname] = array($value);
} else {
$ThisFileInfo['comments'][$tagname][] = $value;
}
}
}
}
}
}
}
// 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 ($option_tags_html) {
// Copy ['comments'] to ['comments_html']
if (!empty($ThisFileInfo['comments'])) {
foreach ($ThisFileInfo['comments'] as $field => $values) {
if ($field == 'picture') {
// pictures can take up a lot of space, and we don't need multiple copies of them
// let there be a single copy in [comments][picture], and not elsewhere
continue;
}
foreach ($values as $index => $value) {
if (is_array($value)) {
$ThisFileInfo['comments_html'][$field][$index] = $value;
} else {
$ThisFileInfo['comments_html'][$field][$index] = str_replace('&#0;', '', self::MultiByteCharString2HTML($value, $ThisFileInfo['encoding']));
}
}
}
}
}
}
return true;
}
/**
* @param string $key
* @param int $begin
* @param int $end
* @param string $file
* @param string $name
*
* @return string
*/
public static function EmbeddedLookup($key, $begin, $end, $file, $name) {
// Cached
static $cache;
if (isset($cache[$file][$name])) {
return (isset($cache[$file][$name][$key]) ? $cache[$file][$name][$key] : '');
}
// Init
$keylength = strlen($key);
$line_count = $end - $begin - 7;
// Open php file
$fp = fopen($file, 'r');
// Discard $begin lines
for ($i = 0; $i < ($begin + 3); $i++) {
fgets($fp, 1024);
}
// Loop thru line
while (0 < $line_count--) {
// Read line
$line = ltrim(fgets($fp, 1024), "\t ");
// METHOD A: only cache the matching key - less memory but slower on next lookup of not-previously-looked-up key
//$keycheck = substr($line, 0, $keylength);
//if ($key == $keycheck) {
// $cache[$file][$name][$keycheck] = substr($line, $keylength + 1);
// break;
//}
// METHOD B: cache all keys in this lookup - more memory but faster on next lookup of not-previously-looked-up key
//$cache[$file][$name][substr($line, 0, $keylength)] = trim(substr($line, $keylength + 1));
$explodedLine = explode("\t", $line, 2);
$ThisKey = (isset($explodedLine[0]) ? $explodedLine[0] : '');
$ThisValue = (isset($explodedLine[1]) ? $explodedLine[1] : '');
$cache[$file][$name][$ThisKey] = trim($ThisValue);
}
// Close and return
fclose($fp);
return (isset($cache[$file][$name][$key]) ? $cache[$file][$name][$key] : '');
}
/**
* @param string $filename
* @param string $sourcefile
* @param bool $DieOnFailure
*
* @return bool
* @throws Exception
*/
public static function IncludeDependency($filename, $sourcefile, $DieOnFailure=false) {
global $GETID3_ERRORARRAY;
if (file_exists($filename)) {
if (include_once($filename)) {
return true;
} else {
$diemessage = basename($sourcefile).' depends on '.$filename.', which has errors';
}
} else {
$diemessage = basename($sourcefile).' depends on '.$filename.', which is missing';
}
if ($DieOnFailure) {
throw new Exception($diemessage);
} else {
$GETID3_ERRORARRAY[] = $diemessage;
}
return false;
}
/**
* @param string $string
*
* @return string
*/
public static function trimNullByte($string) {
return trim($string, "\x00");
}
/**
* @param string $path
*
* @return float|bool
*/
public static function getFileSizeSyscall($path) {
$filesize = false;
if (GETID3_OS_ISWINDOWS) {
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:
$filesystem = new COM('Scripting.FileSystemObject');
$file = $filesystem->GetFile($path);
$filesize = $file->Size();
unset($filesystem, $file);
} else {
$commandline = 'for %I in ('.escapeshellarg($path).') do @echo %~zI';
}
} else {
$commandline = 'ls -l '.escapeshellarg($path).' | awk \'{print $5}\'';
}
if (isset($commandline)) {
$output = trim(`$commandline`);
if (ctype_digit($output)) {
$filesize = (float) $output;
}
}
return $filesize;
}
/**
* @param string $filename
*
* @return string|false
*/
public static function truepath($filename) {
// 2017-11-08: this could use some improvement, patches welcome
if (preg_match('#^(\\\\\\\\|//)[a-z0-9]#i', $filename, $matches)) {
// PHP's built-in realpath function does not work on UNC Windows shares
$goodpath = array();
foreach (explode('/', str_replace('\\', '/', $filename)) as $part) {
if ($part == '.') {
continue;
}
if ($part == '..') {
if (count($goodpath)) {
array_pop($goodpath);
} else {
// cannot step above this level, already at top level
return false;
}
} else {
$goodpath[] = $part;
}
}
return implode(DIRECTORY_SEPARATOR, $goodpath);
}
return realpath($filename);
}
/**
* Workaround for Bug #37268 (https://bugs.php.net/bug.php?id=37268)
*
* @param string $path A path.
* @param string $suffix If the name component ends in suffix this will also be cut off.
*
* @return string
*/
public static function mb_basename($path, $suffix = null) {
$splited = preg_split('#/#', rtrim($path, '/ '));
return substr(basename('X'.$splited[count($splited) - 1], $suffix), 1);
}
}
<?php
/////////////////////////////////////////////////////////////////
/// getID3() by James Heinrich <info@getid3.org> //
// available at https://github.com/JamesHeinrich/getID3 //
// or https://www.getid3.org //
// or http://getid3.sourceforge.net //
// //
// Please see readme.txt for more information //
// ///
/////////////////////////////////////////////////////////////////
// define a constant rather than looking up every time it is needed
if (!defined('GETID3_OS_ISWINDOWS')) {
define('GETID3_OS_ISWINDOWS', (stripos(PHP_OS, 'WIN') === 0));
}
// Get base path of getID3() - ONCE
if (!defined('GETID3_INCLUDEPATH')) {
define('GETID3_INCLUDEPATH', dirname(__FILE__).DIRECTORY_SEPARATOR);
}
// Workaround Bug #39923 (https://bugs.php.net/bug.php?id=39923)
if (!defined('IMG_JPG') && defined('IMAGETYPE_JPEG')) {
define('IMG_JPG', IMAGETYPE_JPEG);
}
if (!defined('ENT_SUBSTITUTE')) { // PHP5.3 adds ENT_IGNORE, PHP5.4 adds ENT_SUBSTITUTE
define('ENT_SUBSTITUTE', (defined('ENT_IGNORE') ? ENT_IGNORE : 8));
}
/*
https://www.getid3.org/phpBB3/viewtopic.php?t=2114
If you are running into a the problem where filenames with special characters are being handled
incorrectly by external helper programs (e.g. metaflac), notably with the special characters removed,
and you are passing in the filename in UTF8 (typically via a HTML form), try uncommenting this line:
*/
//setlocale(LC_CTYPE, 'en_US.UTF-8');
// attempt to define temp dir as something flexible but reliable
$temp_dir = ini_get('upload_tmp_dir');
if ($temp_dir && (!is_dir($temp_dir) || !is_readable($temp_dir))) {
$temp_dir = '';
}
if (!$temp_dir && function_exists('sys_get_temp_dir')) { // sys_get_temp_dir added in PHP v5.2.1
// sys_get_temp_dir() may give inaccessible temp dir, e.g. with open_basedir on virtual hosts
$temp_dir = sys_get_temp_dir();
}
$temp_dir = @realpath($temp_dir); // see https://github.com/JamesHeinrich/getID3/pull/10
$open_basedir = ini_get('open_basedir');
if ($open_basedir) {
// e.g. "/var/www/vhosts/getid3.org/httpdocs/:/tmp/"
$temp_dir = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $temp_dir);
$open_basedir = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $open_basedir);
if (substr($temp_dir, -1, 1) != DIRECTORY_SEPARATOR) {
$temp_dir .= DIRECTORY_SEPARATOR;
}
$found_valid_tempdir = false;
$open_basedirs = explode(PATH_SEPARATOR, $open_basedir);
foreach ($open_basedirs as $basedir) {
if (substr($basedir, -1, 1) != DIRECTORY_SEPARATOR) {
$basedir .= DIRECTORY_SEPARATOR;
}
if (preg_match('#^'.preg_quote($basedir).'#', $temp_dir)) {
$found_valid_tempdir = true;
break;
}
}
if (!$found_valid_tempdir) {
$temp_dir = '';
}
unset($open_basedirs, $found_valid_tempdir, $basedir);
}
if (!$temp_dir) {
$temp_dir = '*'; // invalid directory name should force tempnam() to use system default temp dir
}
// $temp_dir = '/something/else/'; // feel free to override temp dir here if it works better for your system
if (!defined('GETID3_TEMP_DIR')) {
define('GETID3_TEMP_DIR', $temp_dir);
}
unset($open_basedir, $temp_dir);
// End: Defines
class getID3
{
/*
* Settings
*/
/**
* CASE SENSITIVE! - i.e. (must be supported by iconv()). Examples: ISO-8859-1 UTF-8 UTF-16 UTF-16BE
*
* @var string
*/
public $encoding = 'UTF-8';
/**
* Should always be 'ISO-8859-1', but some tags may be written in other encodings such as 'EUC-CN' or 'CP1252'
*
* @var string
*/
public $encoding_id3v1 = 'ISO-8859-1';
/**
* ID3v1 should always be 'ISO-8859-1', but some tags may be written in other encodings such as 'Windows-1251' or 'KOI8-R'. If true attempt to detect these encodings, but may return incorrect values for some tags actually in ISO-8859-1 encoding
*
* @var bool
*/
public $encoding_id3v1_autodetect = false;
/*
* Optional tag checks - disable for speed.
*/
/**
* Read and process ID3v1 tags
*
* @var bool
*/
public $option_tag_id3v1 = true;
/**
* Read and process ID3v2 tags
*
* @var bool
*/
public $option_tag_id3v2 = true;
/**
* Read and process Lyrics3 tags
*
* @var bool
*/
public $option_tag_lyrics3 = true;
/**
* Read and process APE tags
*
* @var bool
*/
public $option_tag_apetag = true;
/**
* Copy tags to root key 'tags' and encode to $this->encoding
*
* @var bool
*/
public $option_tags_process = true;
/**
* Copy tags to root key 'tags_html' properly translated from various encodings to HTML entities
*
* @var bool
*/
public $option_tags_html = true;
/*
* Optional tag/comment calculations
*/
/**
* Calculate additional info such as bitrate, channelmode etc
*
* @var bool
*/
public $option_extra_info = true;
/*
* Optional handling of embedded attachments (e.g. images)
*/
/**
* Defaults to true (ATTACHMENTS_INLINE) for backward compatibility
*
* @var bool|string
*/
public $option_save_attachments = true;
/*
* Optional calculations
*/
/**
* Get MD5 sum of data part - slow
*
* @var bool
*/
public $option_md5_data = false;
/**
* Use MD5 of source file if availble - only FLAC and OptimFROG
*
* @var bool
*/
public $option_md5_data_source = false;
/**
* Get SHA1 sum of data part - slow
*
* @var bool
*/
public $option_sha1_data = false;
/**
* Check whether file is larger than 2GB and thus not supported by 32-bit PHP (null: auto-detect based on
* PHP_INT_MAX)
*
* @var bool|null
*/
public $option_max_2gb_check;
/**
* Read buffer size in bytes
*
* @var int
*/
public $option_fread_buffer_size = 32768;
// Public variables
/**
* Filename of file being analysed.
*
* @var string
*/
public $filename;
/**
* Filepointer to file being analysed.
*
* @var resource
*/
public $fp;
/**
* Result array.
*
* @var array
*/
public $info;
/**
* @var string
*/
public $tempdir = GETID3_TEMP_DIR;
/**
* @var int
*/
public $memory_limit = 0;
/**
* @var string
*/
protected $startup_error = '';
/**
* @var string
*/
protected $startup_warning = '';
const VERSION = '1.9.20-202006061653';
const FREAD_BUFFER_SIZE = 32768;
const ATTACHMENTS_NONE = false;
const ATTACHMENTS_INLINE = true;
public function __construct() {
// Check for PHP version
$required_php_version = '5.3.0';
if (version_compare(PHP_VERSION, $required_php_version, '<')) {
$this->startup_error .= 'getID3() requires PHP v'.$required_php_version.' or higher - you are running v'.PHP_VERSION."\n";
return;
}
// Check memory
$memoryLimit = ini_get('memory_limit');
if (preg_match('#([0-9]+) ?M#i', $memoryLimit, $matches)) {
// could be stored as "16M" rather than 16777216 for example
$memoryLimit = $matches[1] * 1048576;
} elseif (preg_match('#([0-9]+) ?G#i', $memoryLimit, $matches)) { // The 'G' modifier is available since PHP 5.1.0
// could be stored as "2G" rather than 2147483648 for example
$memoryLimit = $matches[1] * 1073741824;
}
$this->memory_limit = $memoryLimit;
if ($this->memory_limit <= 0) {
// memory limits probably disabled
} elseif ($this->memory_limit <= 4194304) {
$this->startup_error .= 'PHP has less than 4MB available memory and will very likely run out. Increase memory_limit in php.ini'."\n";
} elseif ($this->memory_limit <= 12582912) {
$this->startup_warning .= 'PHP has less than 12MB available memory and might run out if all modules are loaded. Increase memory_limit in php.ini'."\n";
}
// Check safe_mode off
if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
$this->warning('WARNING: Safe mode is on, shorten support disabled, md5data/sha1data for ogg vorbis disabled, ogg vorbos/flac tag writing disabled.');
}
if (($mbstring_func_overload = (int) ini_get('mbstring.func_overload')) && ($mbstring_func_overload & 0x02)) {
// http://php.net/manual/en/mbstring.overload.php
// "mbstring.func_overload in php.ini is a positive value that represents a combination of bitmasks specifying the categories of functions to be overloaded. It should be set to 1 to overload the mail() function. 2 for string functions, 4 for regular expression functions"
// getID3 cannot run when string functions are overloaded. It doesn't matter if mail() or ereg* functions are overloaded since getID3 does not use those.
$this->startup_error .= 'WARNING: php.ini contains "mbstring.func_overload = '.ini_get('mbstring.func_overload').'", getID3 cannot run with this setting (bitmask 2 (string functions) cannot be set). Recommended to disable entirely.'."\n";
}
// check for magic quotes in PHP < 7.4.0 (when these functions became deprecated)
if (version_compare(PHP_VERSION, '7.4.0', '<')) {
// Check for magic_quotes_runtime
if (function_exists('get_magic_quotes_runtime')) {
if (get_magic_quotes_runtime()) {
$this->startup_error .= 'magic_quotes_runtime must be disabled before running getID3(). Surround getid3 block by set_magic_quotes_runtime(0) and set_magic_quotes_runtime(1).'."\n";
}
}
// Check for magic_quotes_gpc
if (function_exists('get_magic_quotes_gpc')) {
if (get_magic_quotes_gpc()) {
$this->startup_error .= 'magic_quotes_gpc must be disabled before running getID3(). Surround getid3 block by set_magic_quotes_gpc(0) and set_magic_quotes_gpc(1).'."\n";
}
}
}
// Load support library
if (!include_once(GETID3_INCLUDEPATH.'getid3.lib.php')) {
$this->startup_error .= 'getid3.lib.php is missing or corrupt'."\n";
}
if ($this->option_max_2gb_check === null) {
$this->option_max_2gb_check = (PHP_INT_MAX <= 2147483647);
}
// Needed for Windows only:
// Define locations of helper applications for Shorten, VorbisComment, MetaFLAC
// as well as other helper functions such as head, etc
// This path cannot contain spaces, but the below code will attempt to get the
// 8.3-equivalent path automatically
// IMPORTANT: This path must include the trailing slash
if (GETID3_OS_ISWINDOWS && !defined('GETID3_HELPERAPPSDIR')) {
$helperappsdir = GETID3_INCLUDEPATH.'..'.DIRECTORY_SEPARATOR.'helperapps'; // must not have any space in this path
if (!is_dir($helperappsdir)) {
$this->startup_warning .= '"'.$helperappsdir.'" cannot be defined as GETID3_HELPERAPPSDIR because it does not exist'."\n";
} elseif (strpos(realpath($helperappsdir), ' ') !== false) {
$DirPieces = explode(DIRECTORY_SEPARATOR, realpath($helperappsdir));
$path_so_far = array();
foreach ($DirPieces as $key => $value) {
if (strpos($value, ' ') !== false) {
if (!empty($path_so_far)) {
$commandline = 'dir /x '.escapeshellarg(implode(DIRECTORY_SEPARATOR, $path_so_far));
$dir_listing = `$commandline`;
$lines = explode("\n", $dir_listing);
foreach ($lines as $line) {
$line = trim($line);
if (preg_match('#^([0-9/]{10}) +([0-9:]{4,5}( [AP]M)?) +(<DIR>|[0-9,]+) +([^ ]{0,11}) +(.+)$#', $line, $matches)) {
list($dummy, $date, $time, $ampm, $filesize, $shortname, $filename) = $matches;
if ((strtoupper($filesize) == '<DIR>') && (strtolower($filename) == strtolower($value))) {
$value = $shortname;
}
}
}
} else {
$this->startup_warning .= 'GETID3_HELPERAPPSDIR must not have any spaces in it - use 8dot3 naming convention if neccesary. You can run "dir /x" from the commandline to see the correct 8.3-style names.'."\n";
}
}
$path_so_far[] = $value;
}
$helperappsdir = implode(DIRECTORY_SEPARATOR, $path_so_far);
}
define('GETID3_HELPERAPPSDIR', $helperappsdir.DIRECTORY_SEPARATOR);
}
if (!empty($this->startup_error)) {
echo $this->startup_error;
throw new getid3_exception($this->startup_error);
}
}
/**
* @return string
*/
public function version() {
return self::VERSION;
}
/**
* @return int
*/
public function fread_buffer_size() {
return $this->option_fread_buffer_size;
}
/**
* @param array $optArray
*
* @return bool
*/
public function setOption($optArray) {
if (!is_array($optArray) || empty($optArray)) {
return false;
}
foreach ($optArray as $opt => $val) {
if (isset($this->$opt) === false) {
continue;
}
$this->$opt = $val;
}
return true;
}
/**
* @param string $filename
* @param int $filesize
* @param resource $fp
*
* @return bool
*
* @throws getid3_exception
*/
public function openfile($filename, $filesize=null, $fp=null) {
try {
if (!empty($this->startup_error)) {
throw new getid3_exception($this->startup_error);
}
if (!empty($this->startup_warning)) {
foreach (explode("\n", $this->startup_warning) as $startup_warning) {
$this->warning($startup_warning);
}
}
// init result array and set parameters
$this->filename = $filename;
$this->info = array();
$this->info['GETID3_VERSION'] = $this->version();
$this->info['php_memory_limit'] = (($this->memory_limit > 0) ? $this->memory_limit : false);
// remote files not supported
if (preg_match('#^(ht|f)tp://#', $filename)) {
throw new getid3_exception('Remote files are not supported - please copy the file locally first');
}
$filename = str_replace('/', DIRECTORY_SEPARATOR, $filename);
//$filename = preg_replace('#(?<!gs:)('.preg_quote(DIRECTORY_SEPARATOR).'{2,})#', DIRECTORY_SEPARATOR, $filename);
// open local file
//if (is_readable($filename) && is_file($filename) && ($this->fp = fopen($filename, 'rb'))) { // see https://www.getid3.org/phpBB3/viewtopic.php?t=1720
if (($fp != null) && ((get_resource_type($fp) == 'file') || (get_resource_type($fp) == 'stream'))) {
$this->fp = $fp;
} elseif ((is_readable($filename) || file_exists($filename)) && is_file($filename) && ($this->fp = fopen($filename, 'rb'))) {
// great
} else {
$errormessagelist = array();
if (!is_readable($filename)) {
$errormessagelist[] = '!is_readable';
}
if (!is_file($filename)) {
$errormessagelist[] = '!is_file';
}
if (!file_exists($filename)) {
$errormessagelist[] = '!file_exists';
}
if (empty($errormessagelist)) {
$errormessagelist[] = 'fopen failed';
}
throw new getid3_exception('Could not open "'.$filename.'" ('.implode('; ', $errormessagelist).')');
}
$this->info['filesize'] = (!is_null($filesize) ? $filesize : filesize($filename));
// set redundant parameters - might be needed in some include file
// filenames / filepaths in getID3 are always expressed with forward slashes (unix-style) for both Windows and other to try and minimize confusion
$filename = str_replace('\\', '/', $filename);
$this->info['filepath'] = str_replace('\\', '/', realpath(dirname($filename)));
$this->info['filename'] = getid3_lib::mb_basename($filename);
$this->info['filenamepath'] = $this->info['filepath'].'/'.$this->info['filename'];
// set more parameters
$this->info['avdataoffset'] = 0;
$this->info['avdataend'] = $this->info['filesize'];
$this->info['fileformat'] = ''; // filled in later
$this->info['audio']['dataformat'] = ''; // filled in later, unset if not used
$this->info['video']['dataformat'] = ''; // filled in later, unset if not used
$this->info['tags'] = array(); // filled in later, unset if not used
$this->info['error'] = array(); // filled in later, unset if not used
$this->info['warning'] = array(); // filled in later, unset if not used
$this->info['comments'] = array(); // filled in later, unset if not used
$this->info['encoding'] = $this->encoding; // required by id3v2 and iso modules - can be unset at the end if desired
// option_max_2gb_check
if ($this->option_max_2gb_check) {
// PHP (32-bit all, and 64-bit Windows) doesn't support integers larger than 2^31 (~2GB)
// filesize() simply returns (filesize % (pow(2, 32)), no matter the actual filesize
// ftell() returns 0 if seeking to the end is beyond the range of unsigned integer
$fseek = fseek($this->fp, 0, SEEK_END);
if (($fseek < 0) || (($this->info['filesize'] != 0) && (ftell($this->fp) == 0)) ||
($this->info['filesize'] < 0) ||
(ftell($this->fp) < 0)) {
$real_filesize = getid3_lib::getFileSizeSyscall($this->info['filenamepath']);
if ($real_filesize === false) {
unset($this->info['filesize']);
fclose($this->fp);
throw new getid3_exception('Unable to determine actual filesize. File is most likely larger than '.round(PHP_INT_MAX / 1073741824).'GB and is not supported by PHP.');
} elseif (getid3_lib::intValueSupported($real_filesize)) {
unset($this->info['filesize']);
fclose($this->fp);
throw new getid3_exception('PHP seems to think the file is larger than '.round(PHP_INT_MAX / 1073741824).'GB, but filesystem reports it as '.number_format($real_filesize / 1073741824, 3).'GB, please report to info@getid3.org');
}
$this->info['filesize'] = $real_filesize;
$this->warning('File is larger than '.round(PHP_INT_MAX / 1073741824).'GB (filesystem reports it as '.number_format($real_filesize / 1073741824, 3).'GB) and is not properly supported by PHP.');
}
}
return true;
} catch (Exception $e) {
$this->error($e->getMessage());
}
return false;
}
/**
* analyze file
*
* @param string $filename
* @param int $filesize
* @param string $original_filename
* @param resource $fp
*
* @return array
*/
public function analyze($filename, $filesize=null, $original_filename='', $fp=null) {
try {
if (!$this->openfile($filename, $filesize, $fp)) {
return $this->info;
}
// Handle tags
foreach (array('id3v2'=>'id3v2', 'id3v1'=>'id3v1', 'apetag'=>'ape', 'lyrics3'=>'lyrics3') as $tag_name => $tag_key) {
$option_tag = 'option_tag_'.$tag_name;
if ($this->$option_tag) {
$this->include_module('tag.'.$tag_name);
try {
$tag_class = 'getid3_'.$tag_name;
$tag = new $tag_class($this);
$tag->Analyze();
}
catch (getid3_exception $e) {
throw $e;
}
}
}
if (isset($this->info['id3v2']['tag_offset_start'])) {
$this->info['avdataoffset'] = max($this->info['avdataoffset'], $this->info['id3v2']['tag_offset_end']);
}
foreach (array('id3v1'=>'id3v1', 'apetag'=>'ape', 'lyrics3'=>'lyrics3') as $tag_name => $tag_key) {
if (isset($this->info[$tag_key]['tag_offset_start'])) {
$this->info['avdataend'] = min($this->info['avdataend'], $this->info[$tag_key]['tag_offset_start']);
}
}
// ID3v2 detection (NOT parsing), even if ($this->option_tag_id3v2 == false) done to make fileformat easier
if (!$this->option_tag_id3v2) {
fseek($this->fp, 0);
$header = fread($this->fp, 10);
if ((substr($header, 0, 3) == 'ID3') && (strlen($header) == 10)) {
$this->info['id3v2']['header'] = true;
$this->info['id3v2']['majorversion'] = ord($header[3]);
$this->info['id3v2']['minorversion'] = ord($header[4]);
$this->info['avdataoffset'] += getid3_lib::BigEndian2Int(substr($header, 6, 4), 1) + 10; // length of ID3v2 tag in 10-byte header doesn't include 10-byte header length
}
}
// read 32 kb file data
fseek($this->fp, $this->info['avdataoffset']);
$formattest = fread($this->fp, 32774);
// determine format
$determined_format = $this->GetFileFormat($formattest, ($original_filename ? $original_filename : $filename));
// unable to determine file format
if (!$determined_format) {
fclose($this->fp);
return $this->error('unable to determine file format');
}
// check for illegal ID3 tags
if (isset($determined_format['fail_id3']) && (in_array('id3v1', $this->info['tags']) || in_array('id3v2', $this->info['tags']))) {
if ($determined_format['fail_id3'] === 'ERROR') {
fclose($this->fp);
return $this->error('ID3 tags not allowed on this file type.');
} elseif ($determined_format['fail_id3'] === 'WARNING') {
$this->warning('ID3 tags not allowed on this file type.');
}
}
// check for illegal APE tags
if (isset($determined_format['fail_ape']) && in_array('ape', $this->info['tags'])) {
if ($determined_format['fail_ape'] === 'ERROR') {
fclose($this->fp);
return $this->error('APE tags not allowed on this file type.');
} elseif ($determined_format['fail_ape'] === 'WARNING') {
$this->warning('APE tags not allowed on this file type.');
}
}
// set mime type
$this->info['mime_type'] = $determined_format['mime_type'];
// supported format signature pattern detected, but module deleted
if (!file_exists(GETID3_INCLUDEPATH.$determined_format['include'])) {
fclose($this->fp);
return $this->error('Format not supported, module "'.$determined_format['include'].'" was removed.');
}
// module requires mb_convert_encoding/iconv support
// Check encoding/iconv support
if (!empty($determined_format['iconv_req']) && !function_exists('mb_convert_encoding') && !function_exists('iconv') && !in_array($this->encoding, array('ISO-8859-1', 'UTF-8', 'UTF-16LE', 'UTF-16BE', 'UTF-16'))) {
$errormessage = 'mb_convert_encoding() or iconv() support is required for this module ('.$determined_format['include'].') for encodings other than ISO-8859-1, UTF-8, UTF-16LE, UTF16-BE, UTF-16. ';
if (GETID3_OS_ISWINDOWS) {
$errormessage .= 'PHP does not have mb_convert_encoding() or iconv() support. Please enable php_mbstring.dll / php_iconv.dll in php.ini, and copy php_mbstring.dll / iconv.dll from c:/php/dlls to c:/windows/system32';
} else {
$errormessage .= 'PHP is not compiled with mb_convert_encoding() or iconv() support. Please recompile with the --enable-mbstring / --with-iconv switch';
}
return $this->error($errormessage);
}
// include module
include_once(GETID3_INCLUDEPATH.$determined_format['include']);
// instantiate module class
$class_name = 'getid3_'.$determined_format['module'];
if (!class_exists($class_name)) {
return $this->error('Format not supported, module "'.$determined_format['include'].'" is corrupt.');
}
$class = new $class_name($this);
$class->Analyze();
unset($class);
// close file
fclose($this->fp);
// process all tags - copy to 'tags' and convert charsets
if ($this->option_tags_process) {
$this->HandleAllTags();
}
// perform more calculations
if ($this->option_extra_info) {
$this->ChannelsBitratePlaytimeCalculations();
$this->CalculateCompressionRatioVideo();
$this->CalculateCompressionRatioAudio();
$this->CalculateReplayGain();
$this->ProcessAudioStreams();
}
// get the MD5 sum of the audio/video portion of the file - without ID3/APE/Lyrics3/etc header/footer tags
if ($this->option_md5_data) {
// do not calc md5_data if md5_data_source is present - set by flac only - future MPC/SV8 too
if (!$this->option_md5_data_source || empty($this->info['md5_data_source'])) {
$this->getHashdata('md5');
}
}
// get the SHA1 sum of the audio/video portion of the file - without ID3/APE/Lyrics3/etc header/footer tags
if ($this->option_sha1_data) {
$this->getHashdata('sha1');
}
// remove undesired keys
$this->CleanUp();
} catch (Exception $e) {
$this->error('Caught exception: '.$e->getMessage());
}
// return info array
return $this->info;
}
/**
* Error handling.
*
* @param string $message
*
* @return array
*/
public function error($message) {
$this->CleanUp();
if (!isset($this->info['error'])) {
$this->info['error'] = array();
}
$this->info['error'][] = $message;
return $this->info;
}
/**
* Warning handling.
*
* @param string $message
*
* @return bool
*/
public function warning($message) {
$this->info['warning'][] = $message;
return true;
}
/**
* @return bool
*/
private function CleanUp() {
// remove possible empty keys
$AVpossibleEmptyKeys = array('dataformat', 'bits_per_sample', 'encoder_options', 'streams', 'bitrate');
foreach ($AVpossibleEmptyKeys as $dummy => $key) {
if (empty($this->info['audio'][$key]) && isset($this->info['audio'][$key])) {
unset($this->info['audio'][$key]);
}
if (empty($this->info['video'][$key]) && isset($this->info['video'][$key])) {
unset($this->info['video'][$key]);
}
}
// remove empty root keys
if (!empty($this->info)) {
foreach ($this->info as $key => $value) {
if (empty($this->info[$key]) && ($this->info[$key] !== 0) && ($this->info[$key] !== '0')) {
unset($this->info[$key]);
}
}
}
// remove meaningless entries from unknown-format files
if (empty($this->info['fileformat'])) {
if (isset($this->info['avdataoffset'])) {
unset($this->info['avdataoffset']);
}
if (isset($this->info['avdataend'])) {
unset($this->info['avdataend']);
}
}
// remove possible duplicated identical entries
if (!empty($this->info['error'])) {
$this->info['error'] = array_values(array_unique($this->info['error']));
}
if (!empty($this->info['warning'])) {
$this->info['warning'] = array_values(array_unique($this->info['warning']));
}
// remove "global variable" type keys
unset($this->info['php_memory_limit']);
return true;
}
/**
* Return array containing information about all supported formats.
*
* @return array
*/
public function GetFileFormatArray() {
static $format_info = array();
if (empty($format_info)) {
$format_info = array(
// Audio formats
// AC-3 - audio - Dolby AC-3 / Dolby Digital
'ac3' => array(
'pattern' => '^\\x0B\\x77',
'group' => 'audio',
'module' => 'ac3',
'mime_type' => 'audio/ac3',
),
// AAC - audio - Advanced Audio Coding (AAC) - ADIF format
'adif' => array(
'pattern' => '^ADIF',
'group' => 'audio',
'module' => 'aac',
'mime_type' => 'audio/aac',
'fail_ape' => 'WARNING',
),
/*
// AA - audio - Audible Audiobook
'aa' => array(
'pattern' => '^.{4}\\x57\\x90\\x75\\x36',
'group' => 'audio',
'module' => 'aa',
'mime_type' => 'audio/audible',
),
*/
// AAC - audio - Advanced Audio Coding (AAC) - ADTS format (very similar to MP3)
'adts' => array(
'pattern' => '^\\xFF[\\xF0-\\xF1\\xF8-\\xF9]',
'group' => 'audio',
'module' => 'aac',
'mime_type' => 'audio/aac',
'fail_ape' => 'WARNING',
),
// AU - audio - NeXT/Sun AUdio (AU)
'au' => array(
'pattern' => '^\\.snd',
'group' => 'audio',
'module' => 'au',
'mime_type' => 'audio/basic',
),
// AMR - audio - Adaptive Multi Rate
'amr' => array(
'pattern' => '^\\x23\\x21AMR\\x0A', // #!AMR[0A]
'group' => 'audio',
'module' => 'amr',
'mime_type' => 'audio/amr',
),
// AVR - audio - Audio Visual Research
'avr' => array(
'pattern' => '^2BIT',
'group' => 'audio',
'module' => 'avr',
'mime_type' => 'application/octet-stream',
),
// BONK - audio - Bonk v0.9+
'bonk' => array(
'pattern' => '^\\x00(BONK|INFO|META| ID3)',
'group' => 'audio',
'module' => 'bonk',
'mime_type' => 'audio/xmms-bonk',
),
// DSF - audio - Direct Stream Digital (DSD) Storage Facility files (DSF) - https://en.wikipedia.org/wiki/Direct_Stream_Digital
'dsf' => array(
'pattern' => '^DSD ', // including trailing space: 44 53 44 20
'group' => 'audio',
'module' => 'dsf',
'mime_type' => 'audio/dsd',
),
// DSS - audio - Digital Speech Standard
'dss' => array(
'pattern' => '^[\\x02-\\x08]ds[s2]',
'group' => 'audio',
'module' => 'dss',
'mime_type' => 'application/octet-stream',
),
// DSDIFF - audio - Direct Stream Digital Interchange File Format
'dsdiff' => array(
'pattern' => '^FRM8',
'group' => 'audio',
'module' => 'dsdiff',
'mime_type' => 'audio/dsd',
),
// DTS - audio - Dolby Theatre System
'dts' => array(
'pattern' => '^\\x7F\\xFE\\x80\\x01',
'group' => 'audio',
'module' => 'dts',
'mime_type' => 'audio/dts',
),
// FLAC - audio - Free Lossless Audio Codec
'flac' => array(
'pattern' => '^fLaC',
'group' => 'audio',
'module' => 'flac',
'mime_type' => 'audio/flac',
),
// LA - audio - Lossless Audio (LA)
'la' => array(
'pattern' => '^LA0[2-4]',
'group' => 'audio',
'module' => 'la',
'mime_type' => 'application/octet-stream',
),
// LPAC - audio - Lossless Predictive Audio Compression (LPAC)
'lpac' => array(
'pattern' => '^LPAC',
'group' => 'audio',
'module' => 'lpac',
'mime_type' => 'application/octet-stream',
),
// MIDI - audio - MIDI (Musical Instrument Digital Interface)
'midi' => array(
'pattern' => '^MThd',
'group' => 'audio',
'module' => 'midi',
'mime_type' => 'audio/midi',
),
// MAC - audio - Monkey's Audio Compressor
'mac' => array(
'pattern' => '^MAC ',
'group' => 'audio',
'module' => 'monkey',
'mime_type' => 'audio/x-monkeys-audio',
),
// has been known to produce false matches in random files (e.g. JPEGs), leave out until more precise matching available
// // MOD - audio - MODule (assorted sub-formats)
// 'mod' => array(
// 'pattern' => '^.{1080}(M\\.K\\.|M!K!|FLT4|FLT8|[5-9]CHN|[1-3][0-9]CH)',
// 'group' => 'audio',
// 'module' => 'mod',
// 'option' => 'mod',
// 'mime_type' => 'audio/mod',
// ),
// MOD - audio - MODule (Impulse Tracker)
'it' => array(
'pattern' => '^IMPM',
'group' => 'audio',
'module' => 'mod',
//'option' => 'it',
'mime_type' => 'audio/it',
),
// MOD - audio - MODule (eXtended Module, various sub-formats)
'xm' => array(
'pattern' => '^Extended Module',
'group' => 'audio',
'module' => 'mod',
//'option' => 'xm',
'mime_type' => 'audio/xm',
),
// MOD - audio - MODule (ScreamTracker)
's3m' => array(
'pattern' => '^.{44}SCRM',
'group' => 'audio',
'module' => 'mod',
//'option' => 's3m',
'mime_type' => 'audio/s3m',
),
// MPC - audio - Musepack / MPEGplus
'mpc' => array(
'pattern' => '^(MPCK|MP\\+|[\\x00\\x01\\x10\\x11\\x40\\x41\\x50\\x51\\x80\\x81\\x90\\x91\\xC0\\xC1\\xD0\\xD1][\\x20-\\x37][\\x00\\x20\\x40\\x60\\x80\\xA0\\xC0\\xE0])',
'group' => 'audio',
'module' => 'mpc',
'mime_type' => 'audio/x-musepack',
),
// MP3 - audio - MPEG-audio Layer 3 (very similar to AAC-ADTS)
'mp3' => array(
'pattern' => '^\\xFF[\\xE2-\\xE7\\xF2-\\xF7\\xFA-\\xFF][\\x00-\\x0B\\x10-\\x1B\\x20-\\x2B\\x30-\\x3B\\x40-\\x4B\\x50-\\x5B\\x60-\\x6B\\x70-\\x7B\\x80-\\x8B\\x90-\\x9B\\xA0-\\xAB\\xB0-\\xBB\\xC0-\\xCB\\xD0-\\xDB\\xE0-\\xEB\\xF0-\\xFB]',
'group' => 'audio',
'module' => 'mp3',
'mime_type' => 'audio/mpeg',
),
// OFR - audio - OptimFROG
'ofr' => array(
'pattern' => '^(\\*RIFF|OFR)',
'group' => 'audio',
'module' => 'optimfrog',
'mime_type' => 'application/octet-stream',
),
// RKAU - audio - RKive AUdio compressor
'rkau' => array(
'pattern' => '^RKA',
'group' => 'audio',
'module' => 'rkau',
'mime_type' => 'application/octet-stream',
),
// SHN - audio - Shorten
'shn' => array(
'pattern' => '^ajkg',
'group' => 'audio',
'module' => 'shorten',
'mime_type' => 'audio/xmms-shn',
'fail_id3' => 'ERROR',
'fail_ape' => 'ERROR',
),
// TAK - audio - Tom's lossless Audio Kompressor
'tak' => array(
'pattern' => '^tBaK',
'group' => 'audio',
'module' => 'tak',
'mime_type' => 'application/octet-stream',
),
// TTA - audio - TTA Lossless Audio Compressor (http://tta.corecodec.org)
'tta' => array(
'pattern' => '^TTA', // could also be '^TTA(\\x01|\\x02|\\x03|2|1)'
'group' => 'audio',
'module' => 'tta',
'mime_type' => 'application/octet-stream',
),
// VOC - audio - Creative Voice (VOC)
'voc' => array(
'pattern' => '^Creative Voice File',
'group' => 'audio',
'module' => 'voc',
'mime_type' => 'audio/voc',
),
// VQF - audio - transform-domain weighted interleave Vector Quantization Format (VQF)
'vqf' => array(
'pattern' => '^TWIN',
'group' => 'audio',
'module' => 'vqf',
'mime_type' => 'application/octet-stream',
),
// WV - audio - WavPack (v4.0+)
'wv' => array(
'pattern' => '^wvpk',
'group' => 'audio',
'module' => 'wavpack',
'mime_type' => 'application/octet-stream',
),
// Audio-Video formats
// ASF - audio/video - Advanced Streaming Format, Windows Media Video, Windows Media Audio
'asf' => array(
'pattern' => '^\\x30\\x26\\xB2\\x75\\x8E\\x66\\xCF\\x11\\xA6\\xD9\\x00\\xAA\\x00\\x62\\xCE\\x6C',
'group' => 'audio-video',
'module' => 'asf',
'mime_type' => 'video/x-ms-asf',
'iconv_req' => false,
),
// BINK - audio/video - Bink / Smacker
'bink' => array(
'pattern' => '^(BIK|SMK)',
'group' => 'audio-video',
'module' => 'bink',
'mime_type' => 'application/octet-stream',
),
// FLV - audio/video - FLash Video
'flv' => array(
'pattern' => '^FLV[\\x01]',
'group' => 'audio-video',
'module' => 'flv',
'mime_type' => 'video/x-flv',
),
// IVF - audio/video - IVF
'ivf' => array(
'pattern' => '^DKIF',
'group' => 'audio-video',
'module' => 'ivf',
'mime_type' => 'video/x-ivf',
),
// MKAV - audio/video - Mastroka
'matroska' => array(
'pattern' => '^\\x1A\\x45\\xDF\\xA3',
'group' => 'audio-video',
'module' => 'matroska',
'mime_type' => 'video/x-matroska', // may also be audio/x-matroska
),
// MPEG - audio/video - MPEG (Moving Pictures Experts Group)
'mpeg' => array(
'pattern' => '^\\x00\\x00\\x01[\\xB3\\xBA]',
'group' => 'audio-video',
'module' => 'mpeg',
'mime_type' => 'video/mpeg',
),
// NSV - audio/video - Nullsoft Streaming Video (NSV)
'nsv' => array(
'pattern' => '^NSV[sf]',
'group' => 'audio-video',
'module' => 'nsv',
'mime_type' => 'application/octet-stream',
),
// Ogg - audio/video - Ogg (Ogg-Vorbis, Ogg-FLAC, Speex, Ogg-Theora(*), Ogg-Tarkin(*))
'ogg' => array(
'pattern' => '^OggS',
'group' => 'audio',
'module' => 'ogg',
'mime_type' => 'application/ogg',
'fail_id3' => 'WARNING',
'fail_ape' => 'WARNING',
),
// QT - audio/video - Quicktime
'quicktime' => array(
'pattern' => '^.{4}(cmov|free|ftyp|mdat|moov|pnot|skip|wide)',
'group' => 'audio-video',
'module' => 'quicktime',
'mime_type' => 'video/quicktime',
),
// RIFF - audio/video - Resource Interchange File Format (RIFF) / WAV / AVI / CD-audio / SDSS = renamed variant used by SmartSound QuickTracks (www.smartsound.com) / FORM = Audio Interchange File Format (AIFF)
'riff' => array(
'pattern' => '^(RIFF|SDSS|FORM)',
'group' => 'audio-video',
'module' => 'riff',
'mime_type' => 'audio/wav',
'fail_ape' => 'WARNING',
),
// Real - audio/video - RealAudio, RealVideo
'real' => array(
'pattern' => '^\\.(RMF|ra)',
'group' => 'audio-video',
'module' => 'real',
'mime_type' => 'audio/x-realaudio',
),
// SWF - audio/video - ShockWave Flash
'swf' => array(
'pattern' => '^(F|C)WS',
'group' => 'audio-video',
'module' => 'swf',
'mime_type' => 'application/x-shockwave-flash',
),
// TS - audio/video - MPEG-2 Transport Stream
'ts' => array(
'pattern' => '^(\\x47.{187}){10,}', // packets are 188 bytes long and start with 0x47 "G". Check for at least 10 packets matching this pattern
'group' => 'audio-video',
'module' => 'ts',
'mime_type' => 'video/MP2T',
),
// WTV - audio/video - Windows Recorded TV Show
'wtv' => array(
'pattern' => '^\\xB7\\xD8\\x00\\x20\\x37\\x49\\xDA\\x11\\xA6\\x4E\\x00\\x07\\xE9\\x5E\\xAD\\x8D',
'group' => 'audio-video',
'module' => 'wtv',
'mime_type' => 'video/x-ms-wtv',
),
// Still-Image formats
// BMP - still image - Bitmap (Windows, OS/2; uncompressed, RLE8, RLE4)
'bmp' => array(
'pattern' => '^BM',
'group' => 'graphic',
'module' => 'bmp',
'mime_type' => 'image/bmp',
'fail_id3' => 'ERROR',
'fail_ape' => 'ERROR',
),
// GIF - still image - Graphics Interchange Format
'gif' => array(
'pattern' => '^GIF',
'group' => 'graphic',
'module' => 'gif',
'mime_type' => 'image/gif',
'fail_id3' => 'ERROR',
'fail_ape' => 'ERROR',
),
// JPEG - still image - Joint Photographic Experts Group (JPEG)
'jpg' => array(
'pattern' => '^\\xFF\\xD8\\xFF',
'group' => 'graphic',
'module' => 'jpg',
'mime_type' => 'image/jpeg',
'fail_id3' => 'ERROR',
'fail_ape' => 'ERROR',
),
// PCD - still image - Kodak Photo CD
'pcd' => array(
'pattern' => '^.{2048}PCD_IPI\\x00',
'group' => 'graphic',
'module' => 'pcd',
'mime_type' => 'image/x-photo-cd',
'fail_id3' => 'ERROR',
'fail_ape' => 'ERROR',
),
// PNG - still image - Portable Network Graphics (PNG)
'png' => array(
'pattern' => '^\\x89\\x50\\x4E\\x47\\x0D\\x0A\\x1A\\x0A',
'group' => 'graphic',
'module' => 'png',
'mime_type' => 'image/png',
'fail_id3' => 'ERROR',
'fail_ape' => 'ERROR',
),
// SVG - still image - Scalable Vector Graphics (SVG)
'svg' => array(
'pattern' => '(<!DOCTYPE svg PUBLIC |xmlns="http://www\\.w3\\.org/2000/svg")',
'group' => 'graphic',
'module' => 'svg',
'mime_type' => 'image/svg+xml',
'fail_id3' => 'ERROR',
'fail_ape' => 'ERROR',
),
// TIFF - still image - Tagged Information File Format (TIFF)
'tiff' => array(
'pattern' => '^(II\\x2A\\x00|MM\\x00\\x2A)',
'group' => 'graphic',
'module' => 'tiff',
'mime_type' => 'image/tiff',
'fail_id3' => 'ERROR',
'fail_ape' => 'ERROR',
),
// EFAX - still image - eFax (TIFF derivative)
'efax' => array(
'pattern' => '^\\xDC\\xFE',
'group' => 'graphic',
'module' => 'efax',
'mime_type' => 'image/efax',
'fail_id3' => 'ERROR',
'fail_ape' => 'ERROR',
),
// Data formats
// ISO - data - International Standards Organization (ISO) CD-ROM Image
'iso' => array(
'pattern' => '^.{32769}CD001',
'group' => 'misc',
'module' => 'iso',
'mime_type' => 'application/octet-stream',
'fail_id3' => 'ERROR',
'fail_ape' => 'ERROR',
'iconv_req' => false,
),
// HPK - data - HPK compressed data
'hpk' => array(
'pattern' => '^BPUL',
'group' => 'archive',
'module' => 'hpk',
'mime_type' => 'application/octet-stream',
'fail_id3' => 'ERROR',
'fail_ape' => 'ERROR',
),
// RAR - data - RAR compressed data
'rar' => array(
'pattern' => '^Rar\\!',
'group' => 'archive',
'module' => 'rar',
'mime_type' => 'application/vnd.rar',
'fail_id3' => 'ERROR',
'fail_ape' => 'ERROR',
),
// SZIP - audio/data - SZIP compressed data
'szip' => array(
'pattern' => '^SZ\\x0A\\x04',
'group' => 'archive',
'module' => 'szip',
'mime_type' => 'application/octet-stream',
'fail_id3' => 'ERROR',
'fail_ape' => 'ERROR',
),
// TAR - data - TAR compressed data
'tar' => array(
'pattern' => '^.{100}[0-9\\x20]{7}\\x00[0-9\\x20]{7}\\x00[0-9\\x20]{7}\\x00[0-9\\x20\\x00]{12}[0-9\\x20\\x00]{12}',
'group' => 'archive',
'module' => 'tar',
'mime_type' => 'application/x-tar',
'fail_id3' => 'ERROR',
'fail_ape' => 'ERROR',
),
// GZIP - data - GZIP compressed data
'gz' => array(
'pattern' => '^\\x1F\\x8B\\x08',
'group' => 'archive',
'module' => 'gzip',
'mime_type' => 'application/gzip',
'fail_id3' => 'ERROR',
'fail_ape' => 'ERROR',
),
// ZIP - data - ZIP compressed data
'zip' => array(
'pattern' => '^PK\\x03\\x04',
'group' => 'archive',
'module' => 'zip',
'mime_type' => 'application/zip',
'fail_id3' => 'ERROR',
'fail_ape' => 'ERROR',
),
// XZ - data - XZ compressed data
'xz' => array(
'pattern' => '^\\xFD7zXZ\\x00',
'group' => 'archive',
'module' => 'xz',
'mime_type' => 'application/x-xz',
'fail_id3' => 'ERROR',
'fail_ape' => 'ERROR',
),
// Misc other formats
// PAR2 - data - Parity Volume Set Specification 2.0
'par2' => array (
'pattern' => '^PAR2\\x00PKT',
'group' => 'misc',
'module' => 'par2',
'mime_type' => 'application/octet-stream',
'fail_id3' => 'ERROR',
'fail_ape' => 'ERROR',
),
// PDF - data - Portable Document Format
'pdf' => array(
'pattern' => '^\\x25PDF',
'group' => 'misc',
'module' => 'pdf',
'mime_type' => 'application/pdf',
'fail_id3' => 'ERROR',
'fail_ape' => 'ERROR',
),
// MSOFFICE - data - ZIP compressed data
'msoffice' => array(
'pattern' => '^\\xD0\\xCF\\x11\\xE0\\xA1\\xB1\\x1A\\xE1', // D0CF11E == DOCFILE == Microsoft Office Document
'group' => 'misc',
'module' => 'msoffice',
'mime_type' => 'application/octet-stream',
'fail_id3' => 'ERROR',
'fail_ape' => 'ERROR',
),
// CUE - data - CUEsheet (index to single-file disc images)
'cue' => array(
'pattern' => '', // empty pattern means cannot be automatically detected, will fall through all other formats and match based on filename and very basic file contents
'group' => 'misc',
'module' => 'cue',
'mime_type' => 'application/octet-stream',
),
);
}
return $format_info;
}
/**
* @param string $filedata
* @param string $filename
*
* @return mixed|false
*/
public function GetFileFormat(&$filedata, $filename='') {
// this function will determine the format of a file based on usually
// the first 2-4 bytes of the file (8 bytes for PNG, 16 bytes for JPG,
// and in the case of ISO CD image, 6 bytes offset 32kb from the start
// of the file).
// Identify file format - loop through $format_info and detect with reg expr
foreach ($this->GetFileFormatArray() as $format_name => $info) {
// The /s switch on preg_match() forces preg_match() NOT to treat
// newline (0x0A) characters as special chars but do a binary match
if (!empty($info['pattern']) && preg_match('#'.$info['pattern'].'#s', $filedata)) {
$info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.php';
return $info;
}
}
if (preg_match('#\\.mp[123a]$#i', $filename)) {
// Too many mp3 encoders on the market put garbage in front of mpeg files
// use assume format on these if format detection failed
$GetFileFormatArray = $this->GetFileFormatArray();
$info = $GetFileFormatArray['mp3'];
$info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.php';
return $info;
} elseif (preg_match('#\\.cue$#i', $filename) && preg_match('#FILE "[^"]+" (BINARY|MOTOROLA|AIFF|WAVE|MP3)#', $filedata)) {
// there's not really a useful consistent "magic" at the beginning of .cue files to identify them
// so until I think of something better, just go by filename if all other format checks fail
// and verify there's at least one instance of "TRACK xx AUDIO" in the file
$GetFileFormatArray = $this->GetFileFormatArray();
$info = $GetFileFormatArray['cue'];
$info['include'] = 'module.'.$info['group'].'.'.$info['module'].'.php';
return $info;
}
return false;
}
/**
* Converts array to $encoding charset from $this->encoding.
*
* @param array $array
* @param string $encoding
*/
public function CharConvert(&$array, $encoding) {
// identical encoding - end here
if ($encoding == $this->encoding) {
return;
}
// loop thru array
foreach ($array as $key => $value) {
// go recursive
if (is_array($value)) {
$this->CharConvert($array[$key], $encoding);
}
// convert string
elseif (is_string($value)) {
$array[$key] = trim(getid3_lib::iconv_fallback($encoding, $this->encoding, $value));
}
}
}
/**
* @return bool
*/
public function HandleAllTags() {
// key name => array (tag name, character encoding)
static $tags;
if (empty($tags)) {
$tags = array(
'asf' => array('asf' , 'UTF-16LE'),
'midi' => array('midi' , 'ISO-8859-1'),
'nsv' => array('nsv' , 'ISO-8859-1'),
'ogg' => array('vorbiscomment' , 'UTF-8'),
'png' => array('png' , 'UTF-8'),
'tiff' => array('tiff' , 'ISO-8859-1'),
'quicktime' => array('quicktime' , 'UTF-8'),
'real' => array('real' , 'ISO-8859-1'),
'vqf' => array('vqf' , 'ISO-8859-1'),
'zip' => array('zip' , 'ISO-8859-1'),
'riff' => array('riff' , 'ISO-8859-1'),
'lyrics3' => array('lyrics3' , 'ISO-8859-1'),
'id3v1' => array('id3v1' , $this->encoding_id3v1),
'id3v2' => array('id3v2' , 'UTF-8'), // not according to the specs (every frame can have a different encoding), but getID3() force-converts all encodings to UTF-8
'ape' => array('ape' , 'UTF-8'),
'cue' => array('cue' , 'ISO-8859-1'),
'matroska' => array('matroska' , 'UTF-8'),
'flac' => array('vorbiscomment' , 'UTF-8'),
'divxtag' => array('divx' , 'ISO-8859-1'),
'iptc' => array('iptc' , 'ISO-8859-1'),
'dsdiff' => array('dsdiff' , 'ISO-8859-1'),
);
}
// loop through comments array
foreach ($tags as $comment_name => $tagname_encoding_array) {
list($tag_name, $encoding) = $tagname_encoding_array;
// fill in default encoding type if not already present
if (isset($this->info[$comment_name]) && !isset($this->info[$comment_name]['encoding'])) {
$this->info[$comment_name]['encoding'] = $encoding;
}
// copy comments if key name set
if (!empty($this->info[$comment_name]['comments'])) {
foreach ($this->info[$comment_name]['comments'] as $tag_key => $valuearray) {
foreach ($valuearray as $key => $value) {
if (is_string($value)) {
$value = trim($value, " \r\n\t"); // do not trim nulls from $value!! Unicode characters will get mangled if trailing nulls are removed!
}
if ($value) {
if (!is_numeric($key)) {
$this->info['tags'][trim($tag_name)][trim($tag_key)][$key] = $value;
} else {
$this->info['tags'][trim($tag_name)][trim($tag_key)][] = $value;
}
}
}
if ($tag_key == 'picture') {
// pictures can take up a lot of space, and we don't need multiple copies of them; let there be a single copy in [comments][picture], and not elsewhere
unset($this->info[$comment_name]['comments'][$tag_key]);
}
}
if (!isset($this->info['tags'][$tag_name])) {
// comments are set but contain nothing but empty strings, so skip
continue;
}
$this->CharConvert($this->info['tags'][$tag_name], $this->info[$comment_name]['encoding']); // only copy gets converted!
if ($this->option_tags_html) {
foreach ($this->info['tags'][$tag_name] as $tag_key => $valuearray) {
if ($tag_key == 'picture') {
// Do not to try to convert binary picture data to HTML
// https://github.com/JamesHeinrich/getID3/issues/178
continue;
}
$this->info['tags_html'][$tag_name][$tag_key] = getid3_lib::recursiveMultiByteCharString2HTML($valuearray, $this->info[$comment_name]['encoding']);
}
}
}
}
// pictures can take up a lot of space, and we don't need multiple copies of them; let there be a single copy in [comments][picture], and not elsewhere
if (!empty($this->info['tags'])) {
$unset_keys = array('tags', 'tags_html');
foreach ($this->info['tags'] as $tagtype => $tagarray) {
foreach ($tagarray as $tagname => $tagdata) {
if ($tagname == 'picture') {
foreach ($tagdata as $key => $tagarray) {
$this->info['comments']['picture'][] = $tagarray;
if (isset($tagarray['data']) && isset($tagarray['image_mime'])) {
if (isset($this->info['tags'][$tagtype][$tagname][$key])) {
unset($this->info['tags'][$tagtype][$tagname][$key]);
}
if (isset($this->info['tags_html'][$tagtype][$tagname][$key])) {
unset($this->info['tags_html'][$tagtype][$tagname][$key]);
}
}
}
}
}
foreach ($unset_keys as $unset_key) {
// remove possible empty keys from (e.g. [tags][id3v2][picture])
if (empty($this->info[$unset_key][$tagtype]['picture'])) {
unset($this->info[$unset_key][$tagtype]['picture']);
}
if (empty($this->info[$unset_key][$tagtype])) {
unset($this->info[$unset_key][$tagtype]);
}
if (empty($this->info[$unset_key])) {
unset($this->info[$unset_key]);
}
}
// remove duplicate copy of picture data from (e.g. [id3v2][comments][picture])
if (isset($this->info[$tagtype]['comments']['picture'])) {
unset($this->info[$tagtype]['comments']['picture']);
}
if (empty($this->info[$tagtype]['comments'])) {
unset($this->info[$tagtype]['comments']);
}
if (empty($this->info[$tagtype])) {
unset($this->info[$tagtype]);
}
}
}
return true;
}
/**
* Calls getid3_lib::CopyTagsToComments() but passes in the option_tags_html setting from this instance of getID3
*
* @param array $ThisFileInfo
*
* @return bool
*/
public function CopyTagsToComments(&$ThisFileInfo) {
return getid3_lib::CopyTagsToComments($ThisFileInfo, $this->option_tags_html);
}
/**
* @param string $algorithm
*
* @return array|bool
*/
public function getHashdata($algorithm) {
switch ($algorithm) {
case 'md5':
case 'sha1':
break;
default:
return $this->error('bad algorithm "'.$algorithm.'" in getHashdata()');
}
if (!empty($this->info['fileformat']) && !empty($this->info['dataformat']) && ($this->info['fileformat'] == 'ogg') && ($this->info['audio']['dataformat'] == 'vorbis')) {
// We cannot get an identical md5_data value for Ogg files where the comments
// span more than 1 Ogg page (compared to the same audio data with smaller
// comments) using the normal getID3() method of MD5'ing the data between the
// end of the comments and the end of the file (minus any trailing tags),
// because the page sequence numbers of the pages that the audio data is on
// do not match. Under normal circumstances, where comments are smaller than
// the nominal 4-8kB page size, then this is not a problem, but if there are
// very large comments, the only way around it is to strip off the comment
// tags with vorbiscomment and MD5 that file.
// This procedure must be applied to ALL Ogg files, not just the ones with
// comments larger than 1 page, because the below method simply MD5's the
// whole file with the comments stripped, not just the portion after the
// comments block (which is the standard getID3() method.
// The above-mentioned problem of comments spanning multiple pages and changing
// page sequence numbers likely happens for OggSpeex and OggFLAC as well, but
// currently vorbiscomment only works on OggVorbis files.
if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
$this->warning('Failed making system call to vorbiscomment.exe - '.$algorithm.'_data is incorrect - error returned: PHP running in Safe Mode (backtick operator not available)');
$this->info[$algorithm.'_data'] = false;
} else {
// Prevent user from aborting script
$old_abort = ignore_user_abort(true);
// Create empty file
$empty = tempnam(GETID3_TEMP_DIR, 'getID3');
touch($empty);
// Use vorbiscomment to make temp file without comments
$temp = tempnam(GETID3_TEMP_DIR, 'getID3');
$file = $this->info['filenamepath'];
if (GETID3_OS_ISWINDOWS) {
if (file_exists(GETID3_HELPERAPPSDIR.'vorbiscomment.exe')) {
$commandline = '"'.GETID3_HELPERAPPSDIR.'vorbiscomment.exe" -w -c "'.$empty.'" "'.$file.'" "'.$temp.'"';
$VorbisCommentError = `$commandline`;
} else {
$VorbisCommentError = 'vorbiscomment.exe not found in '.GETID3_HELPERAPPSDIR;
}
} else {
$commandline = 'vorbiscomment -w -c '.escapeshellarg($empty).' '.escapeshellarg($file).' '.escapeshellarg($temp).' 2>&1';
$VorbisCommentError = `$commandline`;
}
if (!empty($VorbisCommentError)) {
$this->warning('Failed making system call to vorbiscomment(.exe) - '.$algorithm.'_data will be incorrect. If vorbiscomment is unavailable, please download from http://www.vorbis.com/download.psp and put in the getID3() directory. Error returned: '.$VorbisCommentError);
$this->info[$algorithm.'_data'] = false;
} else {
// Get hash of newly created file
switch ($algorithm) {
case 'md5':
$this->info[$algorithm.'_data'] = md5_file($temp);
break;
case 'sha1':
$this->info[$algorithm.'_data'] = sha1_file($temp);
break;
}
}
// Clean up
unlink($empty);
unlink($temp);
// Reset abort setting
ignore_user_abort($old_abort);
}
} else {
if (!empty($this->info['avdataoffset']) || (isset($this->info['avdataend']) && ($this->info['avdataend'] < $this->info['filesize']))) {
// get hash from part of file
$this->info[$algorithm.'_data'] = getid3_lib::hash_data($this->info['filenamepath'], $this->info['avdataoffset'], $this->info['avdataend'], $algorithm);
} else {
// get hash from whole file
switch ($algorithm) {
case 'md5':
$this->info[$algorithm.'_data'] = md5_file($this->info['filenamepath']);
break;
case 'sha1':
$this->info[$algorithm.'_data'] = sha1_file($this->info['filenamepath']);
break;
}
}
}
return true;
}
public function ChannelsBitratePlaytimeCalculations() {
// set channelmode on audio
if (!empty($this->info['audio']['channelmode']) || !isset($this->info['audio']['channels'])) {
// ignore
} elseif ($this->info['audio']['channels'] == 1) {
$this->info['audio']['channelmode'] = 'mono';
} elseif ($this->info['audio']['channels'] == 2) {
$this->info['audio']['channelmode'] = 'stereo';
}
// Calculate combined bitrate - audio + video
$CombinedBitrate = 0;
$CombinedBitrate += (isset($this->info['audio']['bitrate']) ? $this->info['audio']['bitrate'] : 0);
$CombinedBitrate += (isset($this->info['video']['bitrate']) ? $this->info['video']['bitrate'] : 0);
if (($CombinedBitrate > 0) && empty($this->info['bitrate'])) {
$this->info['bitrate'] = $CombinedBitrate;
}
//if ((isset($this->info['video']) && !isset($this->info['video']['bitrate'])) || (isset($this->info['audio']) && !isset($this->info['audio']['bitrate']))) {
// // for example, VBR MPEG video files cannot determine video bitrate:
// // should not set overall bitrate and playtime from audio bitrate only
// unset($this->info['bitrate']);
//}
// video bitrate undetermined, but calculable
if (isset($this->info['video']['dataformat']) && $this->info['video']['dataformat'] && (!isset($this->info['video']['bitrate']) || ($this->info['video']['bitrate'] == 0))) {
// if video bitrate not set
if (isset($this->info['audio']['bitrate']) && ($this->info['audio']['bitrate'] > 0) && ($this->info['audio']['bitrate'] == $this->info['bitrate'])) {
// AND if audio bitrate is set to same as overall bitrate
if (isset($this->info['playtime_seconds']) && ($this->info['playtime_seconds'] > 0)) {
// AND if playtime is set
if (isset($this->info['avdataend']) && isset($this->info['avdataoffset'])) {
// AND if AV data offset start/end is known
// THEN we can calculate the video bitrate
$this->info['bitrate'] = round((($this->info['avdataend'] - $this->info['avdataoffset']) * 8) / $this->info['playtime_seconds']);
$this->info['video']['bitrate'] = $this->info['bitrate'] - $this->info['audio']['bitrate'];
}
}
}
}
if ((!isset($this->info['playtime_seconds']) || ($this->info['playtime_seconds'] <= 0)) && !empty($this->info['bitrate'])) {
$this->info['playtime_seconds'] = (($this->info['avdataend'] - $this->info['avdataoffset']) * 8) / $this->info['bitrate'];
}
if (!isset($this->info['bitrate']) && !empty($this->info['playtime_seconds'])) {
$this->info['bitrate'] = (($this->info['avdataend'] - $this->info['avdataoffset']) * 8) / $this->info['playtime_seconds'];
}
if (isset($this->info['bitrate']) && empty($this->info['audio']['bitrate']) && empty($this->info['video']['bitrate'])) {
if (isset($this->info['audio']['dataformat']) && empty($this->info['video']['resolution_x'])) {
// audio only
$this->info['audio']['bitrate'] = $this->info['bitrate'];
} elseif (isset($this->info['video']['resolution_x']) && empty($this->info['audio']['dataformat'])) {
// video only
$this->info['video']['bitrate'] = $this->info['bitrate'];
}
}
// Set playtime string
if (!empty($this->info['playtime_seconds']) && empty($this->info['playtime_string'])) {
$this->info['playtime_string'] = getid3_lib::PlaytimeString($this->info['playtime_seconds']);
}
}
/**
* @return bool
*/
public function CalculateCompressionRatioVideo() {
if (empty($this->info['video'])) {
return false;
}
if (empty($this->info['video']['resolution_x']) || empty($this->info['video']['resolution_y'])) {
return false;
}
if (empty($this->info['video']['bits_per_sample'])) {
return false;
}
switch ($this->info['video']['dataformat']) {
case 'bmp':
case 'gif':
case 'jpeg':
case 'jpg':
case 'png':
case 'tiff':
$FrameRate = 1;
$PlaytimeSeconds = 1;
$BitrateCompressed = $this->info['filesize'] * 8;
break;
default:
if (!empty($this->info['video']['frame_rate'])) {
$FrameRate = $this->info['video']['frame_rate'];
} else {
return false;
}
if (!empty($this->info['playtime_seconds'])) {
$PlaytimeSeconds = $this->info['playtime_seconds'];
} else {
return false;
}
if (!empty($this->info['video']['bitrate'])) {
$BitrateCompressed = $this->info['video']['bitrate'];
} else {
return false;
}
break;
}
$BitrateUncompressed = $this->info['video']['resolution_x'] * $this->info['video']['resolution_y'] * $this->info['video']['bits_per_sample'] * $FrameRate;
$this->info['video']['compression_ratio'] = $BitrateCompressed / $BitrateUncompressed;
return true;
}
/**
* @return bool
*/
public function CalculateCompressionRatioAudio() {
if (empty($this->info['audio']['bitrate']) || empty($this->info['audio']['channels']) || empty($this->info['audio']['sample_rate']) || !is_numeric($this->info['audio']['sample_rate'])) {
return false;
}
$this->info['audio']['compression_ratio'] = $this->info['audio']['bitrate'] / ($this->info['audio']['channels'] * $this->info['audio']['sample_rate'] * (!empty($this->info['audio']['bits_per_sample']) ? $this->info['audio']['bits_per_sample'] : 16));
if (!empty($this->info['audio']['streams'])) {
foreach ($this->info['audio']['streams'] as $streamnumber => $streamdata) {
if (!empty($streamdata['bitrate']) && !empty($streamdata['channels']) && !empty($streamdata['sample_rate'])) {
$this->info['audio']['streams'][$streamnumber]['compression_ratio'] = $streamdata['bitrate'] / ($streamdata['channels'] * $streamdata['sample_rate'] * (!empty($streamdata['bits_per_sample']) ? $streamdata['bits_per_sample'] : 16));
}
}
}
return true;
}
/**
* @return bool
*/
public function CalculateReplayGain() {
if (isset($this->info['replay_gain'])) {
if (!isset($this->info['replay_gain']['reference_volume'])) {
$this->info['replay_gain']['reference_volume'] = 89.0;
}
if (isset($this->info['replay_gain']['track']['adjustment'])) {
$this->info['replay_gain']['track']['volume'] = $this->info['replay_gain']['reference_volume'] - $this->info['replay_gain']['track']['adjustment'];
}
if (isset($this->info['replay_gain']['album']['adjustment'])) {
$this->info['replay_gain']['album']['volume'] = $this->info['replay_gain']['reference_volume'] - $this->info['replay_gain']['album']['adjustment'];
}
if (isset($this->info['replay_gain']['track']['peak'])) {
$this->info['replay_gain']['track']['max_noclip_gain'] = 0 - getid3_lib::RGADamplitude2dB($this->info['replay_gain']['track']['peak']);
}
if (isset($this->info['replay_gain']['album']['peak'])) {
$this->info['replay_gain']['album']['max_noclip_gain'] = 0 - getid3_lib::RGADamplitude2dB($this->info['replay_gain']['album']['peak']);
}
}
return true;
}
/**
* @return bool
*/
public function ProcessAudioStreams() {
if (!empty($this->info['audio']['bitrate']) || !empty($this->info['audio']['channels']) || !empty($this->info['audio']['sample_rate'])) {
if (!isset($this->info['audio']['streams'])) {
foreach ($this->info['audio'] as $key => $value) {
if ($key != 'streams') {
$this->info['audio']['streams'][0][$key] = $value;
}
}
}
}
return true;
}
/**
* @return string|bool
*/
public function getid3_tempnam() {
return tempnam($this->tempdir, 'gI3');
}
/**
* @param string $name
*
* @return bool
*
* @throws getid3_exception
*/
public function include_module($name) {
//if (!file_exists($this->include_path.'module.'.$name.'.php')) {
if (!file_exists(GETID3_INCLUDEPATH.'module.'.$name.'.php')) {
throw new getid3_exception('Required module.'.$name.'.php is missing.');
}
include_once(GETID3_INCLUDEPATH.'module.'.$name.'.php');
return true;
}
/**
* @param string $filename
*
* @return bool
*/
public static function is_writable ($filename) {
$ret = is_writable($filename);
if (!$ret) {
$perms = fileperms($filename);
$ret = ($perms & 0x0080) || ($perms & 0x0010) || ($perms & 0x0002);
}
return $ret;
}
}
abstract class getid3_handler
{
/**
* @var getID3
*/
protected $getid3; // pointer
/**
* Analyzing filepointer or string.
*
* @var bool
*/
protected $data_string_flag = false;
/**
* String to analyze.
*
* @var string
*/
protected $data_string = '';
/**
* Seek position in string.
*
* @var int
*/
protected $data_string_position = 0;
/**
* String length.
*
* @var int
*/
protected $data_string_length = 0;
/**
* @var string
*/
private $dependency_to;
/**
* getid3_handler constructor.
*
* @param getID3 $getid3
* @param string $call_module
*/
public function __construct(getID3 $getid3, $call_module=null) {
$this->getid3 = $getid3;
if ($call_module) {
$this->dependency_to = str_replace('getid3_', '', $call_module);
}
}
/**
* Analyze from file pointer.
*
* @return bool
*/
abstract public function Analyze();
/**
* Analyze from string instead.
*
* @param string $string
*/
public function AnalyzeString($string) {
// Enter string mode
$this->setStringMode($string);
// Save info
$saved_avdataoffset = $this->getid3->info['avdataoffset'];
$saved_avdataend = $this->getid3->info['avdataend'];
$saved_filesize = (isset($this->getid3->info['filesize']) ? $this->getid3->info['filesize'] : null); // may be not set if called as dependency without openfile() call
// Reset some info
$this->getid3->info['avdataoffset'] = 0;
$this->getid3->info['avdataend'] = $this->getid3->info['filesize'] = $this->data_string_length;
// Analyze
$this->Analyze();
// Restore some info
$this->getid3->info['avdataoffset'] = $saved_avdataoffset;
$this->getid3->info['avdataend'] = $saved_avdataend;
$this->getid3->info['filesize'] = $saved_filesize;
// Exit string mode
$this->data_string_flag = false;
}
/**
* @param string $string
*/
public function setStringMode($string) {
$this->data_string_flag = true;
$this->data_string = $string;
$this->data_string_length = strlen($string);
}
/**
* @return int|bool
*/
protected function ftell() {
if ($this->data_string_flag) {
return $this->data_string_position;
}
return ftell($this->getid3->fp);
}
/**
* @param int $bytes
*
* @return string|false
*
* @throws getid3_exception
*/
protected function fread($bytes) {
if ($this->data_string_flag) {
$this->data_string_position += $bytes;
return substr($this->data_string, $this->data_string_position - $bytes, $bytes);
}
$pos = $this->ftell() + $bytes;
if (!getid3_lib::intValueSupported($pos)) {
throw new getid3_exception('cannot fread('.$bytes.' from '.$this->ftell().') because beyond PHP filesystem limit', 10);
}
//return fread($this->getid3->fp, $bytes);
/*
* https://www.getid3.org/phpBB3/viewtopic.php?t=1930
* "I found out that the root cause for the problem was how getID3 uses the PHP system function fread().
* It seems to assume that fread() would always return as many bytes as were requested.
* However, according the PHP manual (http://php.net/manual/en/function.fread.php), this is the case only with regular local files, but not e.g. with Linux pipes.
* The call may return only part of the requested data and a new call is needed to get more."
*/
$contents = '';
do {
//if (($this->getid3->memory_limit > 0) && ($bytes > $this->getid3->memory_limit)) {
if (($this->getid3->memory_limit > 0) && (($bytes / $this->getid3->memory_limit) > 0.99)) { // enable a more-fuzzy match to prevent close misses generating errors like "PHP Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 33554464 bytes)"
throw new getid3_exception('cannot fread('.$bytes.' from '.$this->ftell().') that is more than available PHP memory ('.$this->getid3->memory_limit.')', 10);
}
$part = fread($this->getid3->fp, $bytes);
$partLength = strlen($part);
$bytes -= $partLength;
$contents .= $part;
} while (($bytes > 0) && ($partLength > 0));
return $contents;
}
/**
* @param int $bytes
* @param int $whence
*
* @return int
*
* @throws getid3_exception
*/
protected function fseek($bytes, $whence=SEEK_SET) {
if ($this->data_string_flag) {
switch ($whence) {
case SEEK_SET:
$this->data_string_position = $bytes;
break;
case SEEK_CUR:
$this->data_string_position += $bytes;
break;
case SEEK_END:
$this->data_string_position = $this->data_string_length + $bytes;
break;
}
return 0;
} else {
$pos = $bytes;
if ($whence == SEEK_CUR) {
$pos = $this->ftell() + $bytes;
} elseif ($whence == SEEK_END) {
$pos = $this->getid3->info['filesize'] + $bytes;
}
if (!getid3_lib::intValueSupported($pos)) {
throw new getid3_exception('cannot fseek('.$pos.') because beyond PHP filesystem limit', 10);
}
}
return fseek($this->getid3->fp, $bytes, $whence);
}
/**
* @return string|false
*
* @throws getid3_exception
*/
protected function fgets() {
// must be able to handle CR/LF/CRLF but not read more than one lineend
$buffer = ''; // final string we will return
$prevchar = ''; // save previously-read character for end-of-line checking
if ($this->data_string_flag) {
while (true) {
$thischar = substr($this->data_string, $this->data_string_position++, 1);
if (($prevchar == "\r") && ($thischar != "\n")) {
// read one byte too many, back up
$this->data_string_position--;
break;
}
$buffer .= $thischar;
if ($thischar == "\n") {
break;
}
if ($this->data_string_position >= $this->data_string_length) {
// EOF
break;
}
$prevchar = $thischar;
}
} else {
// Ideally we would just use PHP's fgets() function, however...
// it does not behave consistently with regards to mixed line endings, may be system-dependent
// and breaks entirely when given a file with mixed \r vs \n vs \r\n line endings (e.g. some PDFs)
//return fgets($this->getid3->fp);
while (true) {
$thischar = fgetc($this->getid3->fp);
if (($prevchar == "\r") && ($thischar != "\n")) {
// read one byte too many, back up
fseek($this->getid3->fp, -1, SEEK_CUR);
break;
}
$buffer .= $thischar;
if ($thischar == "\n") {
break;
}
if (feof($this->getid3->fp)) {
break;
}
$prevchar = $thischar;
}
}
return $buffer;
}
/**
* @return bool
*/
protected function feof() {
if ($this->data_string_flag) {
return $this->data_string_position >= $this->data_string_length;
}
return feof($this->getid3->fp);
}
/**
* @param string $module
*
* @return bool
*/
final protected function isDependencyFor($module) {
return $this->dependency_to == $module;
}
/**
* @param string $text
*
* @return bool
*/
protected function error($text) {
$this->getid3->info['error'][] = $text;
return false;
}
/**
* @param string $text
*
* @return bool
*/
protected function warning($text) {
return $this->getid3->warning($text);
}
/**
* @param string $text
*/
protected function notice($text) {
// does nothing for now
}
/**
* @param string $name
* @param int $offset
* @param int $length
* @param string $image_mime
*
* @return string|null
*
* @throws Exception
* @throws getid3_exception
*/
public function saveAttachment($name, $offset, $length, $image_mime=null) {
try {
// do not extract at all
if ($this->getid3->option_save_attachments === getID3::ATTACHMENTS_NONE) {
$attachment = null; // do not set any
// extract to return array
} elseif ($this->getid3->option_save_attachments === getID3::ATTACHMENTS_INLINE) {
$this->fseek($offset);
$attachment = $this->fread($length); // get whole data in one pass, till it is anyway stored in memory
if ($attachment === false || strlen($attachment) != $length) {
throw new Exception('failed to read attachment data');
}
// assume directory path is given
} else {
// set up destination path
$dir = rtrim(str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $this->getid3->option_save_attachments), DIRECTORY_SEPARATOR);
if (!is_dir($dir) || !getID3::is_writable($dir)) { // check supplied directory
throw new Exception('supplied path ('.$dir.') does not exist, or is not writable');
}
$dest = $dir.DIRECTORY_SEPARATOR.$name.($image_mime ? '.'.getid3_lib::ImageExtFromMime($image_mime) : '');
// create dest file
if (($fp_dest = fopen($dest, 'wb')) == false) {
throw new Exception('failed to create file '.$dest);
}
// copy data
$this->fseek($offset);
$buffersize = ($this->data_string_flag ? $length : $this->getid3->fread_buffer_size());
$bytesleft = $length;
while ($bytesleft > 0) {
if (($buffer = $this->fread(min($buffersize, $bytesleft))) === false || ($byteswritten = fwrite($fp_dest, $buffer)) === false || ($byteswritten === 0)) {
throw new Exception($buffer === false ? 'not enough data to read' : 'failed to write to destination file, may be not enough disk space');
}
$bytesleft -= $byteswritten;
}
fclose($fp_dest);
$attachment = $dest;
}
} catch (Exception $e) {
// close and remove dest file if created
if (isset($fp_dest) && is_resource($fp_dest)) {
fclose($fp_dest);
}
if (isset($dest) && file_exists($dest)) {
unlink($dest);
}
// do not set any is case of error
$attachment = null;
$this->warning('Failed to extract attachment '.$name.': '.$e->getMessage());
}
// seek to the end of attachment
$this->fseek($offset + $length);
return $attachment;
}
}
class getid3_exception extends Exception
{
public $message;
}
<?php
/////////////////////////////////////////////////////////////////
/// getID3() by James Heinrich <info@getid3.org> //
// available at https://github.com/JamesHeinrich/getID3 //
// or https://www.getid3.org //
// or http://getid3.sourceforge.net //
// see readme.txt for more details //
/////////////////////////////////////////////////////////////////
// //
// module.archive.gzip.php //
// module for analyzing GZIP files //
// dependencies: NONE //
// ///
/////////////////////////////////////////////////////////////////
// //
// Module originally written by //
// Mike Mozolin <teddybearØmail*ru> //
// //
/////////////////////////////////////////////////////////////////
if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
exit;
}
class getid3_gzip extends getid3_handler
{
/**
* Optional file list - disable for speed.
*
* Decode gzipped files, if possible, and parse recursively (.tar.gz for example).
*
* @var bool
*/
public $option_gzip_parse_contents = false;
/**
* @return bool
*/
public function Analyze() {
$info = &$this->getid3->info;
$info['fileformat'] = 'gzip';
$start_length = 10;
$unpack_header = 'a1id1/a1id2/a1cmethod/a1flags/a4mtime/a1xflags/a1os';
//+---+---+---+---+---+---+---+---+---+---+
//|ID1|ID2|CM |FLG| MTIME |XFL|OS |
//+---+---+---+---+---+---+---+---+---+---+
if ($info['php_memory_limit'] && ($info['filesize'] > $info['php_memory_limit'])) {
$this->error('File is too large ('.number_format($info['filesize']).' bytes) to read into memory (limit: '.number_format($info['php_memory_limit'] / 1048576).'MB)');
return false;
}
$this->fseek(0);
$buffer = $this->fread($info['filesize']);
$arr_members = explode("\x1F\x8B\x08", $buffer);
$num_members = 0;
while (true) {
$is_wrong_members = false;
$num_members = intval(count($arr_members));
for ($i = 0; $i < $num_members; $i++) {
if (strlen($arr_members[$i]) == 0) {
continue;
}
$buf = "\x1F\x8B\x08".$arr_members[$i];
$attr = unpack($unpack_header, substr($buf, 0, $start_length));
if (!$this->get_os_type(ord($attr['os']))) {
// Merge member with previous if wrong OS type
$arr_members[($i - 1)] .= $buf;
$arr_members[$i] = '';
$is_wrong_members = true;
continue;
}
}
if (!$is_wrong_members) {
break;
}
}
$info['gzip']['files'] = array();
$fpointer = 0;
$idx = 0;
for ($i = 0; $i < $num_members; $i++) {
if (strlen($arr_members[$i]) == 0) {
continue;
}
$thisInfo = &$info['gzip']['member_header'][++$idx];
$buff = "\x1F\x8B\x08".$arr_members[$i];
$attr = unpack($unpack_header, substr($buff, 0, $start_length));
$thisInfo['filemtime'] = getid3_lib::LittleEndian2Int($attr['mtime']);
$thisInfo['raw']['id1'] = ord($attr['cmethod']);
$thisInfo['raw']['id2'] = ord($attr['cmethod']);
$thisInfo['raw']['cmethod'] = ord($attr['cmethod']);
$thisInfo['raw']['os'] = ord($attr['os']);
$thisInfo['raw']['xflags'] = ord($attr['xflags']);
$thisInfo['raw']['flags'] = ord($attr['flags']);
$thisInfo['flags']['crc16'] = (bool) ($thisInfo['raw']['flags'] & 0x02);
$thisInfo['flags']['extra'] = (bool) ($thisInfo['raw']['flags'] & 0x04);
$thisInfo['flags']['filename'] = (bool) ($thisInfo['raw']['flags'] & 0x08);
$thisInfo['flags']['comment'] = (bool) ($thisInfo['raw']['flags'] & 0x10);
$thisInfo['compression'] = $this->get_xflag_type($thisInfo['raw']['xflags']);
$thisInfo['os'] = $this->get_os_type($thisInfo['raw']['os']);
if (!$thisInfo['os']) {
$this->error('Read error on gzip file');
return false;
}
$fpointer = 10;
$arr_xsubfield = array();
// bit 2 - FLG.FEXTRA
//+---+---+=================================+
//| XLEN |...XLEN bytes of "extra field"...|
//+---+---+=================================+
if ($thisInfo['flags']['extra']) {
$w_xlen = substr($buff, $fpointer, 2);
$xlen = getid3_lib::LittleEndian2Int($w_xlen);
$fpointer += 2;
$thisInfo['raw']['xfield'] = substr($buff, $fpointer, $xlen);
// Extra SubFields
//+---+---+---+---+==================================+
//|SI1|SI2| LEN |... LEN bytes of subfield data ...|
//+---+---+---+---+==================================+
$idx = 0;
while (true) {
if ($idx >= $xlen) {
break;
}
$si1 = ord(substr($buff, $fpointer + $idx++, 1));
$si2 = ord(substr($buff, $fpointer + $idx++, 1));
if (($si1 == 0x41) && ($si2 == 0x70)) {
$w_xsublen = substr($buff, $fpointer + $idx, 2);
$xsublen = getid3_lib::LittleEndian2Int($w_xsublen);
$idx += 2;
$arr_xsubfield[] = substr($buff, $fpointer + $idx, $xsublen);
$idx += $xsublen;
} else {
break;
}
}
$fpointer += $xlen;
}
// bit 3 - FLG.FNAME
//+=========================================+
//|...original file name, zero-terminated...|
//+=========================================+
// GZIP files may have only one file, with no filename, so assume original filename is current filename without .gz
$thisInfo['filename'] = preg_replace('#\\.gz$#i', '', $info['filename']);
if ($thisInfo['flags']['filename']) {
$thisInfo['filename'] = '';
while (true) {
if (ord($buff[$fpointer]) == 0) {
$fpointer++;
break;
}
$thisInfo['filename'] .= $buff[$fpointer];
$fpointer++;
}
}
// bit 4 - FLG.FCOMMENT
//+===================================+
//|...file comment, zero-terminated...|
//+===================================+
if ($thisInfo['flags']['comment']) {
while (true) {
if (ord($buff[$fpointer]) == 0) {
$fpointer++;
break;
}
$thisInfo['comment'] .= $buff[$fpointer];
$fpointer++;
}
}
// bit 1 - FLG.FHCRC
//+---+---+
//| CRC16 |
//+---+---+
if ($thisInfo['flags']['crc16']) {
$w_crc = substr($buff, $fpointer, 2);
$thisInfo['crc16'] = getid3_lib::LittleEndian2Int($w_crc);
$fpointer += 2;
}
// bit 0 - FLG.FTEXT
//if ($thisInfo['raw']['flags'] & 0x01) {
// Ignored...
//}
// bits 5, 6, 7 - reserved
$thisInfo['crc32'] = getid3_lib::LittleEndian2Int(substr($buff, strlen($buff) - 8, 4));
$thisInfo['filesize'] = getid3_lib::LittleEndian2Int(substr($buff, strlen($buff) - 4));
$info['gzip']['files'] = getid3_lib::array_merge_clobber($info['gzip']['files'], getid3_lib::CreateDeepArray($thisInfo['filename'], '/', $thisInfo['filesize']));
if ($this->option_gzip_parse_contents) {
// Try to inflate GZip
$csize = 0;
$inflated = '';
$chkcrc32 = '';
if (function_exists('gzinflate')) {
$cdata = substr($buff, $fpointer);
$cdata = substr($cdata, 0, strlen($cdata) - 8);
$csize = strlen($cdata);
$inflated = gzinflate($cdata);
// Calculate CRC32 for inflated content
$thisInfo['crc32_valid'] = sprintf('%u', crc32($inflated)) == $thisInfo['crc32'];
// determine format
$formattest = substr($inflated, 0, 32774);
$getid3_temp = new getID3();
$determined_format = $getid3_temp->GetFileFormat($formattest);
unset($getid3_temp);
// file format is determined
$determined_format['module'] = (isset($determined_format['module']) ? $determined_format['module'] : '');
switch ($determined_format['module']) {
case 'tar':
// view TAR-file info
if (file_exists(GETID3_INCLUDEPATH.$determined_format['include']) && include_once(GETID3_INCLUDEPATH.$determined_format['include'])) {
if (($temp_tar_filename = tempnam(GETID3_TEMP_DIR, 'getID3')) === false) {
// can't find anywhere to create a temp file, abort
$this->error('Unable to create temp file to parse TAR inside GZIP file');
break;
}
if ($fp_temp_tar = fopen($temp_tar_filename, 'w+b')) {
fwrite($fp_temp_tar, $inflated);
fclose($fp_temp_tar);
$getid3_temp = new getID3();
$getid3_temp->openfile($temp_tar_filename);
$getid3_tar = new getid3_tar($getid3_temp);
$getid3_tar->Analyze();
$info['gzip']['member_header'][$idx]['tar'] = $getid3_temp->info['tar'];
unset($getid3_temp, $getid3_tar);
unlink($temp_tar_filename);
} else {
$this->error('Unable to fopen() temp file to parse TAR inside GZIP file');
break;
}
}
break;
case '':
default:
// unknown or unhandled format
break;
}
} else {
$this->warning('PHP is not compiled with gzinflate() support. Please enable PHP Zlib extension or recompile with the --with-zlib switch');
}
}
}
return true;
}
/**
* Converts the OS type.
*
* @param string $key
*
* @return string
*/
public function get_os_type($key) {
static $os_type = array(
'0' => 'FAT filesystem (MS-DOS, OS/2, NT/Win32)',
'1' => 'Amiga',
'2' => 'VMS (or OpenVMS)',
'3' => 'Unix',
'4' => 'VM/CMS',
'5' => 'Atari TOS',
'6' => 'HPFS filesystem (OS/2, NT)',
'7' => 'Macintosh',
'8' => 'Z-System',
'9' => 'CP/M',
'10' => 'TOPS-20',
'11' => 'NTFS filesystem (NT)',
'12' => 'QDOS',
'13' => 'Acorn RISCOS',
'255' => 'unknown'
);
return (isset($os_type[$key]) ? $os_type[$key] : '');
}
/**
* Converts the eXtra FLags.
*
* @param string $key
*
* @return string
*/
public function get_xflag_type($key) {
static $xflag_type = array(
'0' => 'unknown',
'2' => 'maximum compression',
'4' => 'fastest algorithm'
);
return (isset($xflag_type[$key]) ? $xflag_type[$key] : '');
}
}
<?php
/////////////////////////////////////////////////////////////////
/// getID3() by James Heinrich <info@getid3.org> //
// available at https://github.com/JamesHeinrich/getID3 //
// or https://www.getid3.org //
// or http://getid3.sourceforge.net //
// see readme.txt for more details //
/////////////////////////////////////////////////////////////////
// //
// module.archive.hpk.php //
// module for analyzing HPK files //
// dependencies: NONE //
// ///
/////////////////////////////////////////////////////////////////
if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
exit;
}
class getid3_hpk extends getid3_handler
{
/**
* @return bool
*/
public function Analyze() {
$info = &$this->getid3->info;
$info['fileformat'] = 'hpk';
$this->fseek($info['avdataoffset']);
$HPKheader = $this->fread(36);
if (substr($HPKheader, 0, 4) == 'BPUL') {
$info['hpk']['header']['signature'] = substr($HPKheader, 0, 4);
$info['hpk']['header']['data_offset'] = getid3_lib::LittleEndian2Int(substr($HPKheader, 4, 4));
$info['hpk']['header']['fragments_per_file'] = getid3_lib::LittleEndian2Int(substr($HPKheader, 8, 4));
//$info['hpk']['header']['unknown1'] = getid3_lib::LittleEndian2Int(substr($HPKheader, 12, 4));
$info['hpk']['header']['fragments_residual_offset'] = getid3_lib::LittleEndian2Int(substr($HPKheader, 16, 4));
$info['hpk']['header']['fragments_residual_count'] = getid3_lib::LittleEndian2Int(substr($HPKheader, 20, 4));
//$info['hpk']['header']['unknown2'] = getid3_lib::LittleEndian2Int(substr($HPKheader, 24, 4));
$info['hpk']['header']['fragmented_filesystem_offset'] = getid3_lib::LittleEndian2Int(substr($HPKheader, 28, 4));
$info['hpk']['header']['fragmented_filesystem_length'] = getid3_lib::LittleEndian2Int(substr($HPKheader, 32, 4));
$info['hpk']['header']['filesystem_entries'] = $info['hpk']['header']['fragmented_filesystem_length'] / ($info['hpk']['header']['fragments_per_file'] * 8);
$this->fseek($info['hpk']['header']['fragmented_filesystem_offset']);
for ($i = 0; $i < $info['hpk']['header']['filesystem_entries']; $i++) {
$offset = getid3_lib::LittleEndian2Int($this->fread(4));
$length = getid3_lib::LittleEndian2Int($this->fread(4));
$info['hpk']['filesystem'][$i] = array('offset' => $offset, 'length' => $length);
}
$this->error('HPK parsing incomplete (and mostly broken) in this version of getID3() ['.$this->getid3->version().']');
/*
$filename = '';
$dirs = array();
foreach ($info['hpk']['filesystem'] as $key => $filesystemdata) {
$this->fseek($filesystemdata['offset']);
$first4 = $this->fread(4);
if (($first4 == 'LZ4 ') || ($first4 == 'ZLIB')) {
// actual data, ignore
$info['hpk']['toc'][$key] = array(
'filename' => ltrim(implode('/', $dirs).'/'.$filename, '/'),
'offset' => $filesystemdata['offset'],
'length' => $filesystemdata['length'],
);
$filename = '';
$dirs = array();
} else {
$fragment_index = getid3_lib::LittleEndian2Int($first4);
$fragment_type = getid3_lib::LittleEndian2Int($this->fread(4)); // file = 0, directory = 1
$name_length = getid3_lib::LittleEndian2Int($this->fread(2));
if ($fragment_type == 1) {
$dirs[] = $this->fread($name_length);
} else {
$filename = $this->fread($name_length);
}
}
}
*/
} else {
$this->error('Expecting "BPUL" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes(substr($HPKheader, 0, 4)).'"');
return false;
}
return true;
}
}
<?php
/////////////////////////////////////////////////////////////////
/// getID3() by James Heinrich <info@getid3.org> //
// available at https://github.com/JamesHeinrich/getID3 //
// or https://www.getid3.org //
// or http://getid3.sourceforge.net //
// see readme.txt for more details //
/////////////////////////////////////////////////////////////////
// //
// module.archive.rar.php //
// module for analyzing RAR files //
// dependencies: NONE //
// ///
/////////////////////////////////////////////////////////////////
if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
exit;
}
class getid3_rar extends getid3_handler
{
/**
* @var bool
*/
public $option_use_rar_extension = false;
/**
* @return bool
*/
public function Analyze() {
$info = &$this->getid3->info;
$info['fileformat'] = 'rar';
if ($this->option_use_rar_extension === true) {
if (function_exists('rar_open')) {
if ($rp = rar_open($info['filenamepath'])) {
$info['rar']['files'] = array();
$entries = rar_list($rp);
foreach ($entries as $entry) {
$info['rar']['files'] = getid3_lib::array_merge_clobber($info['rar']['files'], getid3_lib::CreateDeepArray($entry->getName(), '/', $entry->getUnpackedSize()));
}
rar_close($rp);
return true;
} else {
$this->error('failed to rar_open('.$info['filename'].')');
}
} else {
$this->error('RAR support does not appear to be available in this PHP installation');
}
} else {
$this->error('PHP-RAR processing has been disabled (set $getid3_rar->option_use_rar_extension=true to enable)');
}
return false;
}
}
<?php
/////////////////////////////////////////////////////////////////
/// getID3() by James Heinrich <info@getid3.org> //
// available at https://github.com/JamesHeinrich/getID3 //
// or https://www.getid3.org //
// or http://getid3.sourceforge.net //
// see readme.txt for more details //
/////////////////////////////////////////////////////////////////
// //
// module.archive.szip.php //
// module for analyzing SZIP compressed files //
// dependencies: NONE //
// ///
/////////////////////////////////////////////////////////////////
if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
exit;
}
class getid3_szip extends getid3_handler
{
/**
* @return bool
*/
public function Analyze() {
$info = &$this->getid3->info;
$this->fseek($info['avdataoffset']);
$SZIPHeader = $this->fread(6);
if (substr($SZIPHeader, 0, 4) != "SZ\x0A\x04") {
$this->error('Expecting "53 5A 0A 04" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes(substr($SZIPHeader, 0, 4)).'"');
return false;
}
$info['fileformat'] = 'szip';
$info['szip']['major_version'] = getid3_lib::BigEndian2Int(substr($SZIPHeader, 4, 1));
$info['szip']['minor_version'] = getid3_lib::BigEndian2Int(substr($SZIPHeader, 5, 1));
$this->error('SZIP parsing not enabled in this version of getID3() ['.$this->getid3->version().']');
return false;
while (!$this->feof()) {
$NextBlockID = $this->fread(2);
switch ($NextBlockID) {
case 'SZ':
// Note that szip files can be concatenated, this has the same effect as
// concatenating the files. this also means that global header blocks
// might be present between directory/data blocks.
$this->fseek(4, SEEK_CUR);
break;
case 'BH':
$BHheaderbytes = getid3_lib::BigEndian2Int($this->fread(3));
$BHheaderdata = $this->fread($BHheaderbytes);
$BHheaderoffset = 0;
while (strpos($BHheaderdata, "\x00", $BHheaderoffset) > 0) {
//filename as \0 terminated string (empty string indicates end)
//owner as \0 terminated string (empty is same as last file)
//group as \0 terminated string (empty is same as last file)
//3 byte filelength in this block
//2 byte access flags
//4 byte creation time (like in unix)
//4 byte modification time (like in unix)
//4 byte access time (like in unix)
$BHdataArray['filename'] = substr($BHheaderdata, $BHheaderoffset, strcspn($BHheaderdata, "\x00"));
$BHheaderoffset += (strlen($BHdataArray['filename']) + 1);
$BHdataArray['owner'] = substr($BHheaderdata, $BHheaderoffset, strcspn($BHheaderdata, "\x00"));
$BHheaderoffset += (strlen($BHdataArray['owner']) + 1);
$BHdataArray['group'] = substr($BHheaderdata, $BHheaderoffset, strcspn($BHheaderdata, "\x00"));
$BHheaderoffset += (strlen($BHdataArray['group']) + 1);
$BHdataArray['filelength'] = getid3_lib::BigEndian2Int(substr($BHheaderdata, $BHheaderoffset, 3));
$BHheaderoffset += 3;
$BHdataArray['access_flags'] = getid3_lib::BigEndian2Int(substr($BHheaderdata, $BHheaderoffset, 2));
$BHheaderoffset += 2;
$BHdataArray['creation_time'] = getid3_lib::BigEndian2Int(substr($BHheaderdata, $BHheaderoffset, 4));
$BHheaderoffset += 4;
$BHdataArray['modification_time'] = getid3_lib::BigEndian2Int(substr($BHheaderdata, $BHheaderoffset, 4));
$BHheaderoffset += 4;
$BHdataArray['access_time'] = getid3_lib::BigEndian2Int(substr($BHheaderdata, $BHheaderoffset, 4));
$BHheaderoffset += 4;
$info['szip']['BH'][] = $BHdataArray;
}
break;
default:
break 2;
}
}
return true;
}
}
<?php
/////////////////////////////////////////////////////////////////
/// getID3() by James Heinrich <info@getid3.org> //
// available at https://github.com/JamesHeinrich/getID3 //
// or https://www.getid3.org //
// or http://getid3.sourceforge.net //
// see readme.txt for more details //
/////////////////////////////////////////////////////////////////
// //
// module.archive.tar.php //
// module for analyzing TAR files //
// dependencies: NONE //
// ///
/////////////////////////////////////////////////////////////////
// //
// Module originally written by //
// Mike Mozolin <teddybearØmail*ru> //
// //
/////////////////////////////////////////////////////////////////
if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
exit;
}
class getid3_tar extends getid3_handler
{
/**
* @return bool
*/
public function Analyze() {
$info = &$this->getid3->info;
$info['fileformat'] = 'tar';
$info['tar']['files'] = array();
$unpack_header = 'a100fname/a8mode/a8uid/a8gid/a12size/a12mtime/a8chksum/a1typflag/a100lnkname/a6magic/a2ver/a32uname/a32gname/a8devmaj/a8devmin/a155prefix';
$null_512k = str_repeat("\x00", 512); // end-of-file marker
$this->fseek(0);
while (!feof($this->getid3->fp)) {
$buffer = $this->fread(512);
if (strlen($buffer) < 512) {
break;
}
// check the block
$checksum = 0;
for ($i = 0; $i < 148; $i++) {
$checksum += ord($buffer[$i]);
}
for ($i = 148; $i < 156; $i++) {
$checksum += ord(' ');
}
for ($i = 156; $i < 512; $i++) {
$checksum += ord($buffer[$i]);
}
$attr = unpack($unpack_header, $buffer);
$name = (isset($attr['fname'] ) ? trim($attr['fname'] ) : '');
$mode = octdec(isset($attr['mode'] ) ? trim($attr['mode'] ) : '');
$uid = octdec(isset($attr['uid'] ) ? trim($attr['uid'] ) : '');
$gid = octdec(isset($attr['gid'] ) ? trim($attr['gid'] ) : '');
$size = octdec(isset($attr['size'] ) ? trim($attr['size'] ) : '');
$mtime = octdec(isset($attr['mtime'] ) ? trim($attr['mtime'] ) : '');
$chksum = octdec(isset($attr['chksum'] ) ? trim($attr['chksum'] ) : '');
$typflag = (isset($attr['typflag']) ? trim($attr['typflag']) : '');
$lnkname = (isset($attr['lnkname']) ? trim($attr['lnkname']) : '');
$magic = (isset($attr['magic'] ) ? trim($attr['magic'] ) : '');
$ver = (isset($attr['ver'] ) ? trim($attr['ver'] ) : '');
$uname = (isset($attr['uname'] ) ? trim($attr['uname'] ) : '');
$gname = (isset($attr['gname'] ) ? trim($attr['gname'] ) : '');
$devmaj = octdec(isset($attr['devmaj'] ) ? trim($attr['devmaj'] ) : '');
$devmin = octdec(isset($attr['devmin'] ) ? trim($attr['devmin'] ) : '');
$prefix = (isset($attr['prefix'] ) ? trim($attr['prefix'] ) : '');
if (($checksum == 256) && ($chksum == 0)) {
// EOF Found
break;
}
if ($prefix) {
$name = $prefix.'/'.$name;
}
if ((preg_match('#/$#', $name)) && !$name) {
$typeflag = 5;
}
if ($buffer == $null_512k) {
// it's the end of the tar-file...
break;
}
// Read to the next chunk
$this->fseek($size, SEEK_CUR);
$diff = $size % 512;
if ($diff != 0) {
// Padding, throw away
$this->fseek((512 - $diff), SEEK_CUR);
}
// Protect against tar-files with garbage at the end
if ($name == '') {
break;
}
$info['tar']['file_details'][$name] = array (
'name' => $name,
'mode_raw' => $mode,
'mode' => self::display_perms($mode),
'uid' => $uid,
'gid' => $gid,
'size' => $size,
'mtime' => $mtime,
'chksum' => $chksum,
'typeflag' => self::get_flag_type($typflag),
'linkname' => $lnkname,
'magic' => $magic,
'version' => $ver,
'uname' => $uname,
'gname' => $gname,
'devmajor' => $devmaj,
'devminor' => $devmin
);
$info['tar']['files'] = getid3_lib::array_merge_clobber($info['tar']['files'], getid3_lib::CreateDeepArray($info['tar']['file_details'][$name]['name'], '/', $size));
}
return true;
}
/**
* Parses the file mode to file permissions.
*
* @param int $mode
*
* @return string
*/
public function display_perms($mode) {
// Determine Type
if ($mode & 0x1000) $type='p'; // FIFO pipe
elseif ($mode & 0x2000) $type='c'; // Character special
elseif ($mode & 0x4000) $type='d'; // Directory
elseif ($mode & 0x6000) $type='b'; // Block special
elseif ($mode & 0x8000) $type='-'; // Regular
elseif ($mode & 0xA000) $type='l'; // Symbolic Link
elseif ($mode & 0xC000) $type='s'; // Socket
else $type='u'; // UNKNOWN
// Determine permissions
$owner['read'] = (($mode & 00400) ? 'r' : '-');
$owner['write'] = (($mode & 00200) ? 'w' : '-');
$owner['execute'] = (($mode & 00100) ? 'x' : '-');
$group['read'] = (($mode & 00040) ? 'r' : '-');
$group['write'] = (($mode & 00020) ? 'w' : '-');
$group['execute'] = (($mode & 00010) ? 'x' : '-');
$world['read'] = (($mode & 00004) ? 'r' : '-');
$world['write'] = (($mode & 00002) ? 'w' : '-');
$world['execute'] = (($mode & 00001) ? 'x' : '-');
// Adjust for SUID, SGID and sticky bit
if ($mode & 0x800) $owner['execute'] = ($owner['execute'] == 'x') ? 's' : 'S';
if ($mode & 0x400) $group['execute'] = ($group['execute'] == 'x') ? 's' : 'S';
if ($mode & 0x200) $world['execute'] = ($world['execute'] == 'x') ? 't' : 'T';
$s = sprintf('%1s', $type);
$s .= sprintf('%1s%1s%1s', $owner['read'], $owner['write'], $owner['execute']);
$s .= sprintf('%1s%1s%1s', $group['read'], $group['write'], $group['execute']);
$s .= sprintf('%1s%1s%1s'."\n", $world['read'], $world['write'], $world['execute']);
return $s;
}
/**
* Converts the file type.
*
* @param string $typflag
*
* @return mixed|string
*/
public function get_flag_type($typflag) {
static $flag_types = array(
'0' => 'LF_NORMAL',
'1' => 'LF_LINK',
'2' => 'LF_SYNLINK',
'3' => 'LF_CHR',
'4' => 'LF_BLK',
'5' => 'LF_DIR',
'6' => 'LF_FIFO',
'7' => 'LF_CONFIG',
'D' => 'LF_DUMPDIR',
'K' => 'LF_LONGLINK',
'L' => 'LF_LONGNAME',
'M' => 'LF_MULTIVOL',
'N' => 'LF_NAMES',
'S' => 'LF_SPARSE',
'V' => 'LF_VOLHDR'
);
return (isset($flag_types[$typflag]) ? $flag_types[$typflag] : '');
}
}