Location: PHPKode > projects > Simple Way to Usenet > swun/libs/swun/nzb/dnzb.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 dnzb {
	
	private static $_BE = Array('nbrid'	=> Array('conf' 	=> 'newzbinv2',							//config filename with user-pass for that backend
												 'url'		=> 'http://v3.newzbin.com/api/dnzb/')	//directID url 
							);
	
	public static function _init(){
		$host = client::host();
		$rpcs = hellarpcs::listall();
		foreach($rpcs as $k => &$rpc ){
			$rpc_host = hellarpcs::get_host($rpc);
			if ( $rpc_host === $host ){ return TRUE; }
		}
		header('X-DNZB-RCode: 400');
		header('X-DNZB-RText: SWUN Bad Request, wrong source host');
		exit;
	}
	
	
	private static function forwardto($ref, $id, $fileids=NULL){
		if ( !isset(self::$_BE[$ref]) ){
			errors::raise('Client : '.client::host().', msg: Unknow DNZB backend reference "'.$ref.'"', CORE_LOG_ERROR, 'DNZB');
			header('X-DNZB-RCode: 400');
			header('X-DNZB-RText: SWUN Bad Request, "'.$ref.'" unknow forward backend');
			return;
		}
		$post['username'] = configs::get(self::$_BE[$ref]['conf'], 'user');
		$post['password'] = configs::get(self::$_BE[$ref]['conf'], 'pass');
		if ( $fileids === NULL ){
			$post['reportid'] = $id;
		} else {
			$post['fileid'] = $fileids;
		}
		$o="";
		foreach ($post as $k=>$v){ $o.= "$k=".utf8_encode($v)."&"; }
    	$post=substr($o,0,-1);
		$curl = curl_init();
    	curl_setopt($curl, CURLOPT_HEADER, 1);
    	curl_setopt($curl, CURLOPT_POST, 1);
    	curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
    	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($curl, CURLOPT_URL, self::$_BE[$ref]['url']);   
		$result = curl_exec($curl);
    	if ( 0 != curl_errno($curl) ){
    		errors::raise('DNZB Forward failed, curl_error : '.curl_error($curl), CORE_LOG_ERROR, 'DNZB');
    		header('X-DNZB-RCode: 500');
			header('X-DNZB-RText: SWUN Internal curl error');
    	} else {
    		$hsize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
			$headers = substr($result, 0, $hsize - 4);
			//so cleanup and forward headers ;)
			$buff = split("\n", $headers);
			foreach($buff as $k => $header ){ if ( ereg('X-DNZB', $header) ){ header($header); } }
			//if rcode 200, we keep going with data.
			if ( ereg('X-DNZB-RCode: 200', $headers) ){
				errors::raise('Serving client : '.client::host().', NBRID:'.$id, CORE_LOG_INFO, 'DNZB');
				header('Content-Type: text/html; charset=ISO-8859-1');
				echo substr($result, $hsize); //we send body
			} else {
				errors::raise('DNZB Forward failed, X-DNZB-RCode: 200 not found in headers', CORE_LOG_ERROR, 'DNZB');
			}
    	}
	}
	
	public static function server(){
		if ( !self::_init() ){ return; }
		if ( ( !isset($_POST['username']) OR !isset($_POST['password']) ) OR ( !isset($_POST['reportid']) AND !isset($_POST['fileid']) ) ){
			errors::raise('client : '.client::host().', msg: Bad Request, please supply all parameters : username and password, and reportid or fileid', CORE_LOG_ERROR, 'DNZB');
			header('X-DNZB-RCode: 400');
			header('X-DNZB-RText: Bad Request, please supply all parameters : username and password, and reportid or fileid');
			
		//supposed to check auth here, but for now only check client::host() against RPCs ip
		//TODO maybe asign a directID pass into rpc conf, we use rpc name as user ;).
			
		} elseif ( isset($_POST['reportid']) ){
			$did = hellarpcs::splitdid($_POST['reportid']);
			if ( !is_array($did) ){
				errors::raise('client : '.client::host().', msg: Bad Request, please supply a valid reportID parameters :'.$_POST['reportid'], CORE_LOG_ERROR, 'DNZB');
				header('X-DNZB-RCode: 404');
				header('X-DNZB-RText: Bad Request, please supply a valid reportID parameters :'.$_POST['reportid']);
				return;
			}
			if ( $did['type'] == 'ftrid' ){
				header('X-DNZB-RCode: 404');
				header('X-DNZB-RText: Bad Request, ftrid engine is disabled');
				return;
				/*
				//internal DNZB service, build from report db, usenet header db, 
				//getting that amount of data, and keep it up2date humhum 
				$rp = new report();
				if ( FALSE === $rp->is_report($did['id']) ){
					header('X-DNZB-RCode: 404');
					header('X-DNZB-RText: Not Found, data doesn\'t exists');
					return;		
				}
				if ( FALSE !== ( $infos = $rp->build_nzbinfos($did['id'])) ){
					if ( count($infos['files']) == 0 ){
						header('X-DNZB-RCode: 404');
						header('X-DNZB-RText: No Files are linked to this report');
						return;		
					}
					$nzb = nzb::gen_frominfo($infos);
					if ( empty($nzb) ){
						header('X-DNZB-RCode: 500');
						header('X-DNZB-RText: Generated NZB is empty!');		
					} else {
						header('X-DNZB-Name: '.$infos['title']);
						header('X-DNZB-Category: '.$infos['category']);
						header('X-DNZB-RCode: 200');
						header('X-DNZB-RText: OK, NZB content follows');
						echo $nzb;
					}
					return;
				}
				*/
			} elseif ( $did['type'] == 'nzbid' ){
				//Uploaded NZB DNZB service.
				$ndb = new nzbdb();
				$infos = $ndb->get_infos($did['id']);
				if ( FALSE === $infos ){
					errors::raise('client : '.client::host().', msg: Unknow NZBid', CORE_LOG_ERROR, 'DNZB');
					header('X-DNZB-RCode: 400');
					header('X-DNZB-RText: Unknow NZBid');
					return;		
				}
				$file = $infos['file'];
				$cats = configs::get('newzbinv2', 'cats');
				if ( isset($cats[$infos['catid']]) ){
					$category = $cats[$infos['catid']]['name'];
				} else {
					$category = 'Unknow';
				}
				unset($infos, $cats);
				$repo = new file_abstract('NZB');
				if ( !$repo->connect() ){
					errors::raise('client : '.client::host().', msg: Could not connect to Uploaded NZB Repository', CORE_LOG_ERROR, 'DNZB');
					header('X-DNZB-RCode: 400');
					header('X-DNZB-RText: Could not connect to Uploaded NZB repository');
					return;		
				} elseif ( !$repo->isfile('/'.$file) ){
					errors::raise('client : '.client::host().', msg: NZB "'.$file.'" not found in Uploaded NZB Repository', CORE_LOG_ERROR, 'DNZB');
					header('X-DNZB-RCode: 400');
					header('X-DNZB-RText: NZB not found in NZB repo');
					return;		
				}
				$nzb = $repo->readfile('/'.$file);
				if ( empty($nzb) ){
					errors::raise('client : '.client::host().', msg: Uploaded NZB is empty !', CORE_LOG_ERROR, 'DNZB');
					header('X-DNZB-RCode: 500');
					header('X-DNZB-RText: Uploaded NZB is empty');		
				} else {
					errors::raise('Serving client : '.client::host().', NZBID:'.$did['id'], CORE_LOG_INFO, 'DNZB');
					header('X-DNZB-Name: '.str_replace('.nzb', '', $file));
					header('X-DNZB-Category: '.$category);
					header('X-DNZB-RCode: 200');
					header('X-DNZB-RText: OK, NZB content follows');
					echo $nzb;
				}
				return;
			}
			//external DNZB services, forward request
			return self::forwardto($did['type'], $did['id']);
			
		} elseif ( isset($_POST['fileid']) ){ //usenet fileid
			//ids come from newzbin.com FileFind3
			if ( empty($_POST['fileid']) ){
				header('X-DNZB-RCode: 400');
				header('X-DNZB-RText: Bad Request, please supply all parameters');
				return;
			} elseif ( !is_array($_POST['fileid']) ){
				$_POST['fileid'][] = $_POST['fileid'];
			}
			return self::forwardto('nbrid', NULL, $_POST['fileid']);
		}
	}

}
Return current item: Simple Way to Usenet