123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- class getID3_cached_dbm extends getID3
- {
-
- function getID3_cached_dbm($cache_type, $dbm_filename, $lock_filename) {
-
- if (!extension_loaded('dba')) {
- die('PHP is not compiled with dba support, required to use DBM style cache.');
- }
-
- if (function_exists('dba_handlers')) {
- if (!in_array('db3', dba_handlers())) {
- die('PHP is not compiled --with '.$cache_type.' support, required to use DBM style cache.');
- }
- }
- else {
- ob_start();
- phpinfo();
- $contents = ob_get_contents();
- ob_end_clean();
- if (!strstr($contents, $cache_type)) {
- die('PHP is not compiled --with '.$cache_type.' support, required to use DBM style cache.');
- }
- }
-
- if (!file_exists($lock_filename)) {
- if (!touch($lock_filename)) {
- die('failed to create lock file: ' . $lock_filename);
- }
- }
-
- if (!is_writeable($lock_filename)) {
- die('lock file: ' . $lock_filename . ' is not writable');
- }
- $this->lock = fopen($lock_filename, 'w');
-
- flock($this->lock, LOCK_EX);
-
- if (!file_exists($dbm_filename)) {
- if (!touch($dbm_filename)) {
- die('failed to create dbm file: ' . $dbm_filename);
- }
- }
-
- $this->dba = @dba_open($dbm_filename, 'w', $cache_type);
- if (!$this->dba) {
-
- $this->dba = dba_open($dbm_filename, 'n', $cache_type);
- if (!$this->dba) {
- die('failed to create dbm file: ' . $dbm_filename);
- }
-
- dba_insert(GETID3_VERSION, GETID3_VERSION, $this->dba);
- }
-
- $this->cache_type = $cache_type;
- $this->dbm_filename = $dbm_filename;
-
- register_shutdown_function(array($this, '__destruct'));
-
- if (dba_fetch(GETID3_VERSION, $this->dba) != GETID3_VERSION) {
- $this->clear_cache();
- }
- parent::getID3();
- }
-
- function __destruct() {
-
- @dba_close($this->dba);
-
- @flock($this->lock, LOCK_UN);
-
- @fclose($this->lock);
- }
-
- function clear_cache() {
-
- dba_close($this->dba);
-
- $this->dba = dba_open($this->dbm_filename, 'n', $this->cache_type);
- if (!$this->dba) {
- die('failed to clear cache/recreate dbm file: ' . $this->dbm_filename);
- }
-
- dba_insert(GETID3_VERSION, GETID3_VERSION, $this->dba);
-
- register_shutdown_function(array($this, '__destruct'));
- }
-
- function analyze($filename) {
- if (file_exists($filename)) {
-
- $key = $filename . '::' . filemtime($filename) . '::' . filesize($filename);
-
- $result = dba_fetch($key, $this->dba);
-
- if ($result !== false) {
- return unserialize($result);
- }
- }
-
- $result = parent::analyze($filename);
-
- if (file_exists($filename)) {
- dba_insert($key, serialize($result), $this->dba);
- }
- return $result;
- }
- }
- ?>
|