<?php
/**
*
* @author Benjamin Gillissen <hide@address.com>
*
* **************************************************************
Copyright (C) 2009 Benjamin Gillissen
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details at:
http://www.gnu.org/copyleft/gpl.html
* **************************************************************
*/
class nb_cache_file {
private static $base;
private static $caches = Array('search', 'report', 'nfo', 'imdb');
private static $CACHE;
private static $HDL;
public function __construct(){
if (!isset(self::$base) ){
configs::set('file_repo', 'newzbin', Array('type'=>'local', 'conf'=>configs::get('newzbinv2', 'cacheconf')));
self::$base = TRUE;
}
foreach(self::$caches as &$cache){
if ( !isset(self::$CACHE[$cache]) ){
$expire = configs::get('newzbinv2', 'lifetime', $cache);
$size = configs::get('newzbinv2', 'size', $cache);
configs::set('file_cache', "nb_$cache", Array('match'=>"/$cache/%hash%", 'repo'=>'newzbin', 'size'=>$size, 'expire'=>$expire));
self::$CACHE[$cache] = new file_cache("nb_$cache");
if ( FALSE === self::$CACHE[$cache]->isdir("/$cache") ){
self::$CACHE[$cache]->createdir("/$cache");
}
}
}
if ( !chrono::isinit('nb_import') ){ chrono::init('nb_import'); }
if ( !chrono::isinit('nb_restore') ){ chrono::init('nb_restore'); }
}
private function getpath($type, $ref){ return '/'.$type.'/'.md5($ref); }
public function isexpired($type, $ref){
if ( !isset(self::$CACHE[$type]) ){ errors::raise('Unknow newzbin cache : '.$type, CORE_LOG_ERROR, 'NBCACHE');return FALSE; }
if ( FALSE === self::$CACHE[$type]->iscached($ref) ){ return TRUE; }
return self::$CACHE[$type]->isexpired($ref);
}
public function iscached($type, $ref){
if ( !isset(self::$CACHE[$type]) ){ errors::raise('Unknow newzbin cache : '.$type, CORE_LOG_ERROR, 'NBCACHE');return FALSE; }
return self::$CACHE[$type]->iscached($ref);
}
public function import_result($ref, $nbrid){
$chr = chrono::start('nb_import');
$k=md5($ref);
if ( !isset(self::$HDL[$k]) ){
self::$HDL[$k]['hdl'] = self::$CACHE['search']->file_open($this->getpath('search', $ref),'w+');
self::$HDL[$k]['c'] = 0;
if ( FALSE === self::$HDL[$k] ){ chrono::pause('nb_import', $chr);return FALSE; }
}
if ( FALSE !== self::$CACHE['search']->file_put(self::$HDL[$k]['hdl'], "$nbrid\n") ){ self::$HDL[$k]['c']++; }
chrono::pause('nb_import', $chr);
}
public function drop_results($ref){
if ( self::$CACHE['search']->iscached($ref) ){ return TRUE; }
return self::$CACHE['search']->remove($this->getpath('search', $ref));
}
public function import_report($ref, $data){
$chr = chrono::start('nb_import');
$hdl = self::$CACHE['report']->file_open($this->getpath('report', $ref),'w+');
if ( FALSE === $hdl ){
chrono::pause('nb_import', $chr);
return FALSE;
}
$data = nb_fetch::normalize_report($data);
$data = base64_encode(serialize($data));
self::$CACHE['report']->file_put($hdl, $data);
self::$CACHE['report']->file_close($hdl);
chrono::pause('nb_import', $chr);
}
public function update_report_date($ref){
$p=$this->getpath('report', $ref);
$buf = self::$CACHE['report']->readfile($p);
if ( FALSE === $buf ){ return FALSE; }
self::$CACHE['report']->remove($p);
$hdl = self::$CACHE['report']->file_open($p,'w+');
if ( FALSE === $hdl ){ return FALSE; }
self::$CACHE['report']->file_put($hdl, $buf);
self::$CACHE['report']->file_close($hdl);
}
public function drop_report($ref){
if ( !self::$CACHE['report']->iscached($ref) ){ return TRUE; }
return self::$CACHE['report']->remove($this->getpath('report', $ref));
}
public function count_results($ref){
$chr = chrono::start('nb_restore');
$k=md5($ref);
if ( isset(self::$HDL[$k]) ){
chrono::pause('nb_restore', $chr);
return self::$HDL[$k]['c'];
} elseif ( FALSE === self::$CACHE['search']->iscached($ref) ){
chrono::pause('nb_restore', $chr);
return 0;
}
self::$HDL[$k]['hdl'] = self::$CACHE['search']->file_open($this->getpath('search', $ref),'r');
self::$HDL[$k]['c'] = 0;
while ( FALSE !== ($line = self::$CACHE['search']->file_line(self::$HDL[$k]['hdl'])) ){ self::$HDL[$k]['c']++; }
chrono::pause('nb_restore', $chr);
return self::$HDL[$k]['c'];
}
public function fetch_results($ref, $start, $end){
$chr = chrono::start('nb_restore');
$k=md5($ref);
if ( !isset(self::$HDL[$k]['fetch']) ){
if ( isset(self::$HDL[$k]['hdl']) ){
self::$CACHE['search']->file_seek(self::$HDL[$k]['hdl'], 0);
} else {
if ( FALSE === self::$CACHE['search']->iscached($ref) ){ chrono::pause('nb_restore', $chr);return FALSE; }
self::$HDL[$k]['hdl'] = self::$CACHE['search']->file_open($this->getpath('search', $ref),'r');
self::$HDL[$k]['c'] = 0;
}
self::$HDL[$k]['fetch'] = 0;
while( self::$HDL[$k]['fetch'] != $start ){
self::$CACHE['search']->file_line(self::$HDL[$k]['hdl']);
self::$HDL[$k]['fetch']++;
}
}
if ( self::$HDL[$k]['fetch'] == $end ){ chrono::pause('nb_restore', $chr);return FALSE; }
$line = self::$CACHE['search']->file_line(self::$HDL[$k]['hdl']);
if ( empty($line) ){
self::$CACHE['search']->file_close(self::$HDL[$k]['hdl']);
unset(self::$HDL[$k]);
chrono::pause('nb_restore', $chr);
return FALSE;
}
self::$HDL[$k]['fetch']++;
chrono::pause('nb_restore', $chr);
return trim($line);
}
public function get_report($ref){
$chr = chrono::start('nb_restore');
$buf = self::$CACHE['report']->readfile($this->getpath('report', $ref));
if ( FALSE === $buf ){ chrono::pause('nb_restore', $chr);return FALSE; }
$o = unserialize( base64_decode($buf) );
chrono::pause('nb_restore', $chr);
return $o;
}
public function import_nfo($ref, $data){
$hdl = self::$CACHE['nfo']->file_open($this->getpath('nfo', $ref), 'w+');
if ( FALSE === $hdl ){ return FALSE; }
$o = self::$CACHE['nfo']->file_put($hdl, $data);
self::$CACHE['nfo']->file_close($hdl);
return $o;
}
public function read_nfo($ref){
return self::$CACHE['nfo']->readfile($this->getpath('nfo', $ref));
}
public function import_imdb($ref, $data){
$hdl = self::$CACHE['imdb']->file_open($this->getpath('imdb', $ref), 'w+');
if ( FALSE === $hdl ){ return FALSE; }
$o = self::$CACHE['imdb']->file_put($hdl, $data);
self::$CACHE['imdb']->file_close($hdl);
return $o;
}
public function read_imdb($ref){
return self::$CACHE['imdb']->readfile($this->getpath('imdb', $ref));
}
}
return TRUE;