Location: PHPKode > projects > Simple Way to Usenet > libs/swun/search/search.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 {
	
	private static $engines = Array( //'ftrid' => 'FetchThat',
									'nbrid' => 'Newzbin Report',	//name are now taked from langs dicos 
									'nzbid' => 'Uploaded NZB',
									'unfid' => 'Usenet Raw'
								);				
	private static $args = Array('engine', 'type', 'cat', 'filter');
	private static $CC;
	private $engine;
	private $type;
	private $cat;
	private $filter;
	/* ORDER feature
	private $orderby;
	private $orderway;
	*/
	
	private $active, $_RESULT;
	
	private static $_SEARCH;
	
	public function __construct($toforce){
		if ( !isset( self::$CC) ){
			$run 		= Array(client_configs::SESSION=>'profile');
			$store		= Array(client_configs::PROFILE=>'site_profile');
			$runupdt	= Array(client_configs::POST=>'');
			self::$CC = new client_configs($run, $store, $runupdt, Array() );
		}
		foreach( self::$args as $k => &$arg ){
			if ( $arg == "engine" ){ //session key for args
				$sk = "search_engine";
			} elseif ( $arg == "type" ){ 
				$sk = "search_".$this->engine."_$arg";	
			} elseif ( $arg == 'cat' ){
				$sk = "search_".$this->engine."_".$this->type."_$arg";	
			} else {
				$sk = "search_$arg";					
			}
			if ( isset($toforce[$arg]) ) {
				if ( empty($toforce[$arg]) AND 0 != $toforce[$arg] ){ unset($toforce[$arg]); }
			}
			if ( isset($toforce[$arg]) ) {		
				if ( FALSE === $this->arg_isvalid($arg, $toforce[$arg]) ){ 
					$this->$arg = $this->get_defaultargval($arg);
				} else {
					$this->$arg = $toforce[$arg];
					//errors::raise("$arg Forced : ".$this->$arg, CORE_LOG_NOTICE, 'SEARCH');
					self::$CC->store($sk, $this->$arg, client_configs::FORSOME);
				}
			} elseif ( self::$CC->is_updated("search_$arg") ){
				$new = self::$CC->get_updated("search_$arg");
				if ( $arg === 'filter' ){ $new = stripslashes($new); }
				//errors::raise("$arg Updated : ".$new, CORE_LOG_NOTICE, 'SEARCH');
				if ( FALSE === $this->arg_isvalid($arg, $new) ){
					self::$CC->del_updated("search_$arg"); 
					$this->$arg = $this->get_defaultargval($arg);	
				} else {
					$this->$arg = $new;
					self::$CC->store($sk, $this->$arg, client_configs::FORSOME);
				}
				unset($new);
			} elseif ( self::$CC->is_stored($sk) ){
				$this->$arg = self::$CC->get_stored($sk);
				//errors::raise("$arg Stored : ".$this->$arg, CORE_LOG_NOTICE, 'SEARCH');
			} else {
				$this->$arg = $this->get_defaultargval($arg);
				//errors::raise("$arg Default : ".$this->$arg, CORE_LOG_NOTICE, 'SEARCH');
			}
			if ( $arg == 'engine' ){
				if ( FALSE === $this->loadengine($this->engine) ){
					errors::raise('Search engine '.$this->engine.' could not be loaded', CORE_LOG_ERROR, 'SEARCH');
					return;
				}
			}
		}
		$args = self::$_SEARCH[$this->engine]->arg_rewrite($this->type, $this->cat, $this->filter);
		$this->type = $args['type'];
		$this->cat = $args['cat'];
		if ( !isset($toforce['filter']) ){
			$this->filter = $args['filter'];
		}
	}
	
	private function restore_argument($arg){
		switch($arg){
			case 'engine' :break;
			case 'type'	  :break;
			case 'cat'	  :break;
			case 'filter' :break;
		}
		if ( !isset($o) ){ return FALSE; }
		return $o;
	}
	
	
	
	public static function isengine($eng){ return isset(self::$engines[$eng]); }

	public function get_engname($k){
		if ( self::isengine($k) ){ return langs::translate('seng_'.$k, NULL, 'search'); }
		return FALSE;
	}
	
	public function get_engine(){ return $this->engine; }
	
	private static function loadengine($eng){
		if ( FALSE === self::isengine($eng) ){ return FALSE; }
		if ( isset(self::$_SEARCH[$eng]) ){ return TRUE; }
		errors::raise("Loading Search engine : $eng", CORE_LOG_NOTICE, 'SEARCH');
		$class = 'search_'.$eng;
		self::$_SEARCH[$eng] = new $class();
		return TRUE;
	}
	
	public function get_arg($arg){
		if ( isset($this->$arg) ){ return $this->$arg; }
		return FALSE;
	}
	
	public function get_defaultargval($arg){
		if ( $arg == 'engine' ){
			return 'nbrid';
		} elseif ( $arg == 'type' ){
			return self::$_SEARCH[$this->engine]->get_default_type();
		} elseif ( $arg == 'cat' ){
			return self::$_SEARCH[$this->engine]->get_default_cat($this->type);			
		}
		return FALSE;
	}
	
	private function arg_isvalid($arg, $val){
		if ( $arg == 'engine' ){
			return self::isengine($val);
		} elseif ( $arg == 'type' ){
			return self::$_SEARCH[$this->engine]->type_isvalid($val);
		} elseif ( $arg == 'cat' ){
			return self::$_SEARCH[$this->engine]->cat_isvalid($this->type, $val);			
		} elseif ( $arg == 'filter' ){
			return TRUE;
		}
	}
	
	public function send(){
		$hash = CORE::hash();
		if ( !isset(self::$_SEARCH[$this->engine]) ){ return FALSE; }
		errors::raise('Sending Search: Engine:'.$this->engine.', Type:'.$this->type.', Cat:'.$this->cat.', Filter:'.$this->filter, CORE_LOG_DEBUG , 'SEARCH');
		$buf = self::$_SEARCH[$this->engine]->send($this->filter, $this->type, $this->cat);
		if ( FALSE === $buf ){ return FALSE; }
		$this->_RESULT[$hash] = $buf;
		return $hash;
	}
	
	public function count_result($hash){
		if ( !isset(self::$_SEARCH[$this->engine]) ){ return 0; }
		if ( !isset($this->_RESULT[$hash]) ){ return 0; }
		return self::$_SEARCH[$this->engine]->count($this->_RESULT[$hash]);
	}
	
	public function fetch($hash, $start=NULL, $end=NULL){
		if ( !isset(self::$_SEARCH[$this->engine]) ){ return FALSE; }
		if ( !isset($this->_RESULT[$hash]) ){ return FALSE; }
		$buf = self::$_SEARCH[$this->engine]->fetch($this->_RESULT[$hash], $start, $end);
		if ( FALSE === $buf ){ return FALSE; }
		$buf['type'] = $this->engine;
		if ( isset($buf['date']) ){	//from stamp days
			$tmp = CORE::ElapsedTime($buf['date']);
			$buf['age'] = langs::number($tmp[0]).'&nbsp;'.langs::translate($tmp[1], NULL, 'base');
		} else {
			$buf['age'] = 'n/a';
		}
		return $buf;
	}
	
	public function get_cats($type){ return self::$_SEARCH[$this->engine]->get_cats($type); }
	
	public function get_searchbar(){
		$o['filter'] = htmlentities($this->filter, ENT_QUOTES, CORE::getintcharset());
		$o['usefilter'] = self::$_SEARCH[$this->engine]->type_use_filter($this->type);
		if ( !isset(self::$_SEARCH[$this->engine]) ){ return FALSE; }
		foreach(self::$engines as $k => $v ){
			$buf['engine'] = $k;
			$buf['desc'] = $this->get_engname($k);
			$buf['isactive'] = ( $this->engine === $k );
			$o['engines'][] = $buf;			
		}
		unset($buf);
		$tmp = self::$_SEARCH[$this->engine]->get_types();
		foreach($tmp as $k => $v ){
			$buf['type'] = $k;
			$buf['desc'] = $v;
			$buf['isactive'] = ( $this->type == $k );
			$o['types']['types'][] = $buf;			
		}
		unset($tmp, $buf);
		$o['cats']['showcat'] = self::$_SEARCH[$this->engine]->type_got_cats($this->type);
		if ( $o['cats']['showcat'] ){
			if ( $o['usefilter'] ){
				$o['usefilter'] = self::$_SEARCH[$this->engine]->cat_use_filter($this->type, $this->cat);
			}
			$tmp = self::$_SEARCH[$this->engine]->get_cats($this->type);
			if ( is_array($tmp) ){
				$lang=FALSE;
				if ( $this->engine == 'nbrid' AND $this->type == 2 ){ $lang=TRUE;}
				if ( $this->engine == 'nzbid' AND $this->type == 1 ){ $lang=TRUE;}
				if ( $this->engine == 'unfid' AND $this->type == 1 ){ $lang=TRUE;}
				foreach($tmp as $k => &$v ){
					$buf['cat'] = $k;
					if ( $lang ){
						$buf['desc'] = langs::dico_translate("cat_$v", NULL, 'search');
					} else {
						$buf['desc'] = $v;
					}
					$buf['isactive'] = ( $this->cat == $k );
					$o['cats']['cats'][] = $buf;
				}
				unset($buf, $tmp);
			} 
		}
		return $o;
	}
	
	public function get_nfo($id){
		if ( method_exists(self::$_SEARCH[$this->engine], 'get_nfo') ){
			return self::$_SEARCH[$this->engine]->get_nfo($id);
		}
		return FALSE;
	}
	
	public static function get_details($eng, $id){
		if ( !self::loadengine($eng) ){ return FALSE; }
		$r = self::$_SEARCH[$eng]->get_details($id);
		if ( !isset($r['gotdetails']) ){ $r['gotdetails'] = ($r !== FALSE);	}
		$r['type'] = $eng;
		if ( isset($r['date']) ){	//from stamp days
			$buf = CORE::ElapsedTime($r['date']);
			$r['age'] = langs::number($buf[0]).' '.langs::translate($buf[1], NULL, 'base');
		} else {
			$r['age'] = 'n/a';
		}
		return $r;
	}
	
	
}
Return current item: Simple Way to Usenet