File indexing completed on 2024-05-12 17:25:54

0001 <?php
0002 /////////////////////////////////////////////////////////////////
0003 /// getID3() by James Heinrich <info@getid3.org>               //
0004 //  available at http://getid3.sourceforge.net                 //
0005 //            or http://www.getid3.org                         //
0006 //          also https://github.com/JamesHeinrich/getID3       //
0007 /////////////////////////////////////////////////////////////////
0008 //                                                             //
0009 // extension.cache.mysql.php - part of getID3()                //
0010 // Please see readme.txt for more information                  //
0011 //                                                            ///
0012 /////////////////////////////////////////////////////////////////
0013 //                                                             //
0014 // This extension written by Allan Hansen <ahØartemis*dk>      //
0015 // Table name mod by Carlo Capocasa <calroØcarlocapocasa*com>  //
0016 //                                                            ///
0017 /////////////////////////////////////////////////////////////////
0018 
0019 
0020 /**
0021 * This is a caching extension for getID3(). It works the exact same
0022 * way as the getID3 class, but return cached information very fast
0023 *
0024 * Example:  (see also demo.cache.mysql.php in /demo/)
0025 *
0026 *    Normal getID3 usage (example):
0027 *
0028 *       require_once 'getid3/getid3.php';
0029 *       $getID3 = new getID3;
0030 *       $getID3->encoding = 'UTF-8';
0031 *       $info1 = $getID3->analyze('file1.flac');
0032 *       $info2 = $getID3->analyze('file2.wv');
0033 *
0034 *    getID3_cached usage:
0035 *
0036 *       require_once 'getid3/getid3.php';
0037 *       require_once 'getid3/getid3/extension.cache.mysql.php';
0038 *       // 5th parameter (tablename) is optional, default is 'getid3_cache'
0039 *       $getID3 = new getID3_cached_mysql('localhost', 'database', 'username', 'password', 'tablename');
0040 *       $getID3->encoding = 'UTF-8';
0041 *       $info1 = $getID3->analyze('file1.flac');
0042 *       $info2 = $getID3->analyze('file2.wv');
0043 *
0044 *
0045 * Supported Cache Types    (this extension)
0046 *
0047 *   SQL Databases:
0048 *
0049 *   cache_type          cache_options
0050 *   -------------------------------------------------------------------
0051 *   mysql               host, database, username, password
0052 *
0053 *
0054 *   DBM-Style Databases:    (use extension.cache.dbm)
0055 *
0056 *   cache_type          cache_options
0057 *   -------------------------------------------------------------------
0058 *   gdbm                dbm_filename, lock_filename
0059 *   ndbm                dbm_filename, lock_filename
0060 *   db2                 dbm_filename, lock_filename
0061 *   db3                 dbm_filename, lock_filename
0062 *   db4                 dbm_filename, lock_filename  (PHP5 required)
0063 *
0064 *   PHP must have write access to both dbm_filename and lock_filename.
0065 *
0066 *
0067 * Recommended Cache Types
0068 *
0069 *   Infrequent updates, many reads      any DBM
0070 *   Frequent updates                    mysql
0071 */
0072 
0073 
0074 class getID3_cached_mysql extends getID3
0075 {
0076 
0077   // private vars
0078   private $cursor;
0079   private $connection;
0080 
0081 
0082   // public: constructor - see top of this file for cache type and cache_options
0083   public function getID3_cached_mysql($host, $database, $username, $password, $table='getid3_cache') {
0084 
0085     // Check for mysql support
0086     if (!function_exists('mysql_pconnect')) {
0087       throw new Exception('PHP not compiled with mysql support.');
0088     }
0089 
0090     // Connect to database
0091     $this->connection = mysql_pconnect($host, $username, $password);
0092     if (!$this->connection) {
0093       throw new Exception('mysql_pconnect() failed - check permissions and spelling.');
0094     }
0095 
0096     // Select database
0097     if (!mysql_select_db($database, $this->connection)) {
0098       throw new Exception('Cannot use database '.$database);
0099     }
0100 
0101     // Set table
0102     $this->table = $table;
0103 
0104     // Create cache table if not exists
0105     $this->create_table();
0106 
0107     // Check version number and clear cache if changed
0108     $version = '';
0109     $SQLquery  = 'SELECT `value`';
0110     $SQLquery .= ' FROM `'.mysql_real_escape_string($this->table).'`';
0111     $SQLquery .= ' WHERE (`filename` = \''.mysql_real_escape_string(getID3::VERSION).'\')';
0112     $SQLquery .= ' AND (`filesize` = -1)';
0113     $SQLquery .= ' AND (`filetime` = -1)';
0114     $SQLquery .= ' AND (`analyzetime` = -1)';
0115     if ($this->cursor = mysql_query($SQLquery, $this->connection)) {
0116       list($version) = mysql_fetch_array($this->cursor);
0117     }
0118     if ($version != getID3::VERSION) {
0119       $this->clear_cache();
0120     }
0121 
0122     parent::__construct();
0123   }
0124 
0125 
0126 
0127   // public: clear cache
0128   public function clear_cache() {
0129 
0130     $this->cursor = mysql_query('DELETE FROM `'.mysql_real_escape_string($this->table).'`', $this->connection);
0131     $this->cursor = mysql_query('INSERT INTO `'.mysql_real_escape_string($this->table).'` VALUES (\''.getID3::VERSION.'\', -1, -1, -1, \''.getID3::VERSION.'\')', $this->connection);
0132   }
0133 
0134 
0135 
0136   // public: analyze file
0137   public function analyze($filename) {
0138 
0139     if (file_exists($filename)) {
0140 
0141       // Short-hands
0142       $filetime = filemtime($filename);
0143       $filesize =  filesize($filename);
0144 
0145       // Lookup file
0146       $SQLquery  = 'SELECT `value`';
0147       $SQLquery .= ' FROM `'.mysql_real_escape_string($this->table).'`';
0148       $SQLquery .= ' WHERE (`filename` = \''.mysql_real_escape_string($filename).'\')';
0149       $SQLquery .= '   AND (`filesize` = \''.mysql_real_escape_string($filesize).'\')';
0150       $SQLquery .= '   AND (`filetime` = \''.mysql_real_escape_string($filetime).'\')';
0151       $this->cursor = mysql_query($SQLquery, $this->connection);
0152       if (mysql_num_rows($this->cursor) > 0) {
0153         // Hit
0154         list($result) = mysql_fetch_array($this->cursor);
0155         return unserialize(base64_decode($result));
0156       }
0157     }
0158 
0159     // Miss
0160     $analysis = parent::analyze($filename);
0161 
0162     // Save result
0163     if (file_exists($filename)) {
0164       $SQLquery  = 'INSERT INTO `'.mysql_real_escape_string($this->table).'` (`filename`, `filesize`, `filetime`, `analyzetime`, `value`) VALUES (';
0165       $SQLquery .=   '\''.mysql_real_escape_string($filename).'\'';
0166       $SQLquery .= ', \''.mysql_real_escape_string($filesize).'\'';
0167       $SQLquery .= ', \''.mysql_real_escape_string($filetime).'\'';
0168       $SQLquery .= ', \''.mysql_real_escape_string(time()   ).'\'';
0169       $SQLquery .= ', \''.mysql_real_escape_string(base64_encode(serialize($analysis))).'\')';
0170       $this->cursor = mysql_query($SQLquery, $this->connection);
0171     }
0172     return $analysis;
0173   }
0174 
0175 
0176 
0177   // private: (re)create sql table
0178   private function create_table($drop=false) {
0179 
0180     $SQLquery  = 'CREATE TABLE IF NOT EXISTS `'.mysql_real_escape_string($this->table).'` (';
0181     $SQLquery .=   '`filename` VARCHAR(255) NOT NULL DEFAULT \'\'';
0182     $SQLquery .= ', `filesize` INT(11) NOT NULL DEFAULT \'0\'';
0183     $SQLquery .= ', `filetime` INT(11) NOT NULL DEFAULT \'0\'';
0184     $SQLquery .= ', `analyzetime` INT(11) NOT NULL DEFAULT \'0\'';
0185     $SQLquery .= ', `value` TEXT NOT NULL';
0186     $SQLquery .= ', PRIMARY KEY (`filename`, `filesize`, `filetime`)) ENGINE=MyISAM';
0187     $this->cursor = mysql_query($SQLquery, $this->connection);
0188     echo mysql_error($this->connection);
0189   }
0190 }