Location: PHPKode > projects > Simple Way to Usenet > libs/swun/search/search_nzbid.class.php
<?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 search_nzbid extends nzbdb {
	
	public $engine = 'nzbid';
	private static $types = Array('all', 'bycats');	//the search types
	public static $cats = Array('Anime', 'Apps', 'Books', 'Consoles', 'Emulation', 'Games', 'Misc', 'Movies', 'Music', 'PDA', 'Resources', 'TV', 'Unknow','XXX');
	
	private static $_SEARCH;
	
	public function __construct(){
		parent::__construct();
		//self::$_CATDB = new rp_catdb();
	}

	public function arg_rewrite($type, $cat, $filter){
		$o['type'] = $type;
		$o['cat'] = $cat;
		$o['filter'] = $filter;
		return $o;
	}
	
	public function get_types(){ return self::$types; }
	
	public function type_use_filter($type){ return TRUE; }

	public function type_got_cats($type){  return ($type == 1); }

	public function get_default_type(){ return 0; }

	public function get_default_cat($type){ return 0; }
	
	public function type_isvalid($type){ return isset(self::$types[$type]); }
	
	public function cat_isvalid($type, $cat){ 
		if ( $type == 1 ){ return isset(self::$cats[$cat]); }
		return FALSE;
	}
	
	public function get_cats($type){
		if ( $type == 1 ){ return self::$cats; } 
		return FALSE; 
	}
	
	public function cat_use_filter($type, $cat){ return TRUE; }
	
	public function send($search, $type, $cat){
			$hash = CORE::hash();
			self::$_SEARCH[$hash]['type'] = $type;
			self::$_SEARCH[$hash]['cat'] = $cat;
			self::$_SEARCH[$hash]['filter'] = $search;
			return $hash;
	}
	
	public function count($hash){
		if ( !isset(self::$_SEARCH[$hash]) ){ return 0; }
		if ( self::$_SEARCH[$hash]['type'] == 1 ){
			if ( empty(self::$_SEARCH[$hash]['filter']) ){
				$c = $this->count_bycat(self::$_SEARCH[$hash]['cat']);
			} else {
				$c = $this->count_searchbycat(self::$_SEARCH[$hash]['cat'], self::$_SEARCH[$hash]['type']);
			}
		} elseif ( empty(self::$_SEARCH[$hash]['filter']) ){
			$c = $this->count_all();
		} else {
			$c = $this->count_searchall(self::$_SEARCH[$hash]['filter']);
		}
		return $c;
	}
	
	public function fetch($hash, $start, $end){ 
		if ( !isset(self::$_SEARCH[$hash]) ){ return FALSE; } //must send before fetch eh
		if (self::$_SEARCH[$hash]['type'] == 1 ){
			if ( empty(self::$_SEARCH[$hash]['filter']) ){
				errors::raise("list_bycat", CORE_LOG_NOTICE, 'NZBDB');
				$nzbid = $this->list_bycat(self::$_SEARCH[$hash]['cat'], $start, $end);
			} else {
				errors::raise("search_bycat", CORE_LOG_NOTICE, 'NZBDB');
				$nzbid = $this->search_bycat(self::$_SEARCH[$hash]['filter'], self::$_SEARCH[$hash]['cat'], $start, $end);
			}
		} elseif ( empty(self::$_SEARCH[$hash]['filter']) ){
			$nzbid = $this->list_all($start, $end);
		} else {
			$nzbid = $this->search_all(self::$_SEARCH[$hash]['filter'], $start, $end);
		}
		if ( $nzbid === FALSE ){ 
			unset(self::$_SEARCH[$hash]);
			return FALSE; 
		}
		$o	= $this->get_details($nzbid);	
		$o['id'] = $nzbid;
		return $o;
	}
	
	
	public function get_details($nzbid){
		if ( !$this->exists($nzbid) ){ return FALSE; }
		$db = $this->get_infos($nzbid);
		$item['nzbid'] = $nzbid;
		$item['category'] = self::$cats[$db['catid']];
		unset($t);
		$item['catid'] = $db['catid'];
		$item['title']	= str_replace('.nzb', '', $db['file']);
		//$db['file'] = addslashes($db['file']);
		$path = configs::get('file_repo', 'NZB', 'conf');
		$ar = nzb::parse($path.'/'.$db['file']);
		$item['gotdetails'] = is_array($ar);
		if ( !$item['gotdetails'] ){ return $item; }
		$item['numfile'] = count($ar);
		$item['numsegment'] = 0;
		$item['size'] = 0;
		foreach($ar as $n => &$file ){ 
			if ( isset($file['date']) AND !isset($item['date']) ){ $item['date'] = $file['date']; }
			if ( isset($file['date']) AND $file['date'] < $item['date'] ){ $item['date'] = $file['date']; }
			
			foreach($file['groups'] as $n => &$ngrp ){
				if ( !isset($item['groups']) ){
					$item['groups'][] = $ngrp;
				} elseif ( FALSE === array_search($ngrp,$item['groups'])){
					$item['groups'][] = $ngrp;
				} 
			}
			foreach($file['segments'] as $n => &$seg ){
				if ( isset($seg['bytes']) ){ $item['size'] += $seg['bytes']; }
				$item['numsegment']++;
			}
		}
		if ( !isset($item['date']) ){ $item['date'] = 0; }
		$item['poster'] = htmlentities($ar[0]['poster']);
		$s = CORE::Byteconvert($item['size']);
		$item['size'] = langs::number($s[0], $s[2]).' '.$s[1];
		unset($s);
		return $item; 
	}
	
	public function get_nfo($nzbid){ return FALSE; }
}
Return current item: Simple Way to Usenet