123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- class getID3_cached_sqlite3 extends getID3
- {
-
-
- private $db;
-
-
-
- private $table;
-
-
-
- public function __construct($table='getid3_cache', $hide=false) {
-
- if (!function_exists('sqlite_open')) {
- throw new Exception('PHP not compiled with SQLite3 support.');
- }
-
- $this->table = $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();
- $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) {
- $this->clear_cache();
- }
- parent::__construct();
- }
-
-
-
- public function __destruct() {
- $db=$this->db;
- $db->close();
- }
-
-
-
- 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();
- }
-
-
-
- public function analyze($filename, $filesize=null, $original_filename='', $fp=null) {
- if (!file_exists($filename)) {
- return false;
- }
-
- $filetime = filemtime($filename);
- $filesize_real = filesize($filename);
-
-
- $dirname = dirname($filename);
-
- $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));
- }
-
- $analysis = parent::analyze($filename, $filesize, $original_filename, $fp);
-
- $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;
- }
-
-
-
- private function create_table() {
- $db = $this->db;
- $sql = $this->getQuery('make_table');
- return $db->exec($sql);
- }
-
-
-
- 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;
- }
-
-
-
- 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;
- }
- }
-
-
-
- public function __get($name) {
- return $this->getQuery($name);
- }
-
- }
|