<?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_unfid {
const base_url = 'http://www.newzbin.com/api/filefind3/';
public $engine = 'unfid';
private static $_CACHE; //nb_cache_xxx object
private static $types = Array('all', 'bycats', 'bygrp'); //the search types
private $file, $hsize, $encoding;
public function __construct(){ }
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 get_default_type(){ return 0; }
public function type_isvalid($type){ return isset(self::$types[$type]); }
public function type_use_filter($type){ return TRUE; }
public function type_got_cats($type){ return ($type != 0); }
public function get_cats($type){
switch($type){
case 1 : $tmp = configs::get('newzbinv2', 'cats');
break;
case 2 : $prof = new profile('site_profile');
$tmp = $prof->read('savedgrp');
unset($prof);
break;
}
if ( !is_array($tmp) ){
if ( isset($o) ){ return $o; }
return FALSE;
}
foreach($tmp as $k => &$cat){ $o[$k] = $cat['name']; }
if ( isset($o) ){ return $o; }
return FALSE;
}
public function get_default_cat($type){ return 0; }
public function cat_isvalid($type, $cat){
switch($type){
case 1 : return (FALSE !== configs::get('newzbinv2', 'cats', $cat) );
break;
case 2 : $prof = new profile('site_profile');
$tmp = $prof->read('savedgrp');
return ( isset($tmp[$cat]) );
break;
}
return TRUE;
}
public function cat_use_filter($type, $cat){ return TRUE; }
private function build_post($search, $type, $cat){
$o['query'] = $search;
switch($type){
case 0 : break;
case 1 : $tmp = configs::get('newzbinv2', 'cats');
$o['category'] = $tmp[$cat]['name'];
break;
case 2 : $prof = new profile('site_profile');
$tmp = $prof->read('savedgrp');
if ( isset($tmp[$cat]) ){
$buf = split("\n", $tmp[$cat]['grp']);
foreach($buf as $k => &$v){ $v = trim($v); }
$o['group'] = implode(',', $buf);
}
unset($prof,$tmp);
break;
}
return $o;
}
public function send($search, $type, $cat){
$post = $this->build_post($search, $type, $cat);
$post['username'] = configs::get('newzbinv2', 'user');
$post['password'] = configs::get('newzbinv2', 'pass');
if ( !isset($_GET['page']) OR @$_GET['page'] == 0 ){ $_GET['page'] = 1; }
$bp=pfeat_resultbp::get_client_conf('unfid');
if ( isset($_GET['bp']) ){ $bp = $_GET['bp']; }
$post['offset'] = (($_GET['page']-1) * $bp);
$post['limit'] = $bp;
$this->file = CORE::tempfile();
$dst = fopen($this->file, 'w');
if ( FALSE === $dst ){
errors::raise('Newzbin Raw Search destination file could not be created, probably no valid temp folder detected by core::tempfile()', CORE_LOG_ERROR, 'UNFID');
return FALSE;
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_REFERER, '');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($curl, CURLOPT_URL, self::base_url);
curl_setopt($curl, CURLOPT_FILE, $dst);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
unset($post['username'], $post['password']);
errors::raise('Newzbin Raw Search : '.print_r($post, TRUE), CORE_LOG_NOTICE, 'UNFID');
$result = @curl_exec($curl);
if ( 0 != curl_errno($curl) ){
errors::raise('Curl error for Newzbin Raw Search : '.curl_error($curl), CORE_LOG_ERROR, 'UNFID');
curl_close($curl);
fclose($dst);
unlink($this->file);
unset($this->file);
return FALSE;
}
$this->hsize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
curl_close($curl);
fclose($dst);
$dst = fopen($this->file, 'r');
fseek($dst, 0);
$headers = fread($dst, $this->hsize);
$headers = split("\n", $headers);
$o = FALSE;
/* 200 = OK
* 204 = No Content (there were no results to return)
* 402 = Payment Required (the account is not a premium account)
* 403 = Forbidden (incorrect authentication details)
* 500 = Internal Server Error (Newzbin broke)
*/
foreach($headers as $k => &$header){
if ( ereg("Content-Type", $header) ){
$b = split('charset=', $header);
$this->encoding = trim($b[1]);
}
}
if ( !isset($this->encoding) ){ $this->encoding = "ISO-8859-1"; }
foreach($headers as $k => &$header){
if ( ereg('200', $header) ){ $o = TRUE;break; }
if ( ereg('204', $header) ){ errors::raise("Newzbin.com FileFind3 returned no result", CORE_LOG_NOTICE, 'NB_FETCH');break; }
if ( ereg('402', $header) ){ errors::raise("Newzbin.com payment required", CORE_LOG_ERROR, 'NB_FETCH');break; }
if ( ereg('403', $header) ){ errors::raise("Newzbin.com auth failure", CORE_LOG_ERROR, 'NB_FETCH');break; }
if ( ereg('500', $header) ){ errors::raise("Newzbin.com FileFind3 is down !", CORE_LOG_ERROR, 'NB_FETCH');break; }
}
fclose($dst);
return $o;
}
public function count($hash){
if ( !isset($this->file) ){ return 0; }
$dst = fopen($this->file, 'r');
fseek($dst, $this->hsize);
$tmp = fgets($dst);
fclose($dst);
return str_replace('TOTAL=', '', $tmp);
}
public function fetch($hash, $start, $end){
static $result;
if ( !isset($this->file) ){ return FALSE; }
if ( !isset($result) ){
$result = fopen($this->file, 'r');
fseek($result, $this->hsize);
$fl = fgets($result);
}
$line = fgets($result);
if ( FALSE === $line OR empty($line) ){
fclose($result);unset($result);unlink($this->file);unset($this->file);
return FALSE;
}
$line = langs::recode($line, $this->encoding, 'UTF-8//TRANSLITE');
$tmp = split(" ", trim( $line ));
$out['id'] = $tmp[0];
$out['title'] = $tmp[1];
$out['date'] = $tmp[2];
$s = CORE::Byteconvert($tmp[3]);
$out['size'] = langs::number($s[0], $s[2]).' '.$s[1];
unset($s);
$out['poster'] = htmlentities($tmp[4]);
$out['groups'] = split(',',$tmp[5]);
return $out;
}
public function get_details($unfid){ return FALSE; }
}
return TRUE;