<?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 pg_rpcbrw extends pg_helper {
public static $PFEAT= Array('tooltip'=>NULL);
public static $AJAX = Array('nbnfo', 'imdbdata');
public static $PARG = Array('rpc');
public static $mimes = Array( '.nfo$' =>'nfo',
'.txt$' =>'nfo',
'.nzb$' =>'nzb',
'.avi$' =>'vid',
'.mkv$' =>'vid',
'.wmv$' =>'vid',
'.mp3$' =>'zik',
'.pls$' =>'zik',
'.wav$' =>'zik',
'.cue$' =>'zik',
'.m3u$' =>'zik',
'.flac$' =>'zik',
'.wma$' =>'zik',
'.ogg$' =>'zik',
'.par2$' =>'par',
'.zip$' =>'archive',
'.rar$' =>'archive',
'.r+[0-9]' =>'archive',
'.gz$' =>'archive',
'.tar$' =>'archive',
'.pk3$' =>'archive',
'.pk4$' =>'archive',
'.bin$' =>'bin',
'.exe$' =>'bin',
'.bat$' =>'bin',
'.sh$' =>'bin',
'.dmg$' =>'iso',
'.iso$' =>'iso',
'.img$' =>'iso',
'.000$' =>'iso',
'.jpg$' =>'img',
'.gif$' =>'img',
'.png$' =>'img',
'.bmp$' =>'img',
'.cbz$' =>'comics',
'.cbr$' =>'comics',
);
private $REALM, $RPCs, $REPO, $TPL;
public function __construct($pageid=NULL, $args=NULL){
if ( $pageid === NULL OR $args === NULL ){ return; }
parent::__construct($pageid, $args, self::$PFEAT, self::$AJAX);
$rpcs = hellarpcs::listall();
$this->REALM = new realm(pages::get_realm($pageid));
if ( !is_array($rpcs) ){
errors::raise('No RPCs could be found into configuration backend !, please check your hellaprc.opt.php file', CORE_LOG_ALERT, 'RPC');
}
foreach($rpcs as $k => &$RRef ){
$tmp['canlist'] = $this->REALM->checks('rpc', 'list', $RRef);
if ( FALSE !== $this->REALM->checks('rpc', 'list', $RRef) ){
$tmp['enabled'] = ( $this->REALM->checks('rpc', 'use', $RRef) AND 0 != hellarpcs::isactif($RRef) );
$tmp['canread'] = $this->REALM->checks('rpc', 'read', $RRef);
$tmp['cansetup'] = $this->REALM->checks('rpc', 'manage', $RRef);
$tmp['rpc'] = hellarpcs::getname($RRef);
if ( FALSE !== $tmp['enabled'] AND !isset($dftrpc) ){ $dftrpc = $RRef; }
$this->RPCs[$RRef] = $tmp;
unset($tmp);
}
}
if ( !isset($dftrpc) ){
errors::raise("Sorry, No rpc available for your account", CORE_LOG_ALERT, 'PGEN');
return;
}
sessions::start('profile');
if ( !isset($this->args['rpc']) ){
if ( sessions::isdefined('lastrpc', 'profile') ){
$this->args['rpc'] = sessions::read('lastrpc', 'profile');
} else {
$this->args['rpc'] = $dftrpc;
}
} elseif ( ! hellarpcs::isrpc($this->args['rpc']) ){
errors::raise("Invalid Argument : RPC reference, unknow hellarpc", CORE_LOG_ALERT, 'PGEN');
return;
} elseif ( FALSE === $this->RPCS[$this->args['rpc']]['canlist'] OR FALSE === $this->RPCS[$this->args['rpc']]['canread'] ){
errors::raise("Sorry, No enough rights to access that RPC", CORE_LOG_ALERT, 'PGEN');
return;
}
sessions::set('lastrpc', $this->args['rpc'], 'profile');
if ( FALSE == hellarpcs::isactif($this->args['rpc']) ){
pagegen::add_event('RPC is marked inactif, browsing is disabled');
}
hellarpcs::load($this->args['rpc'], 0);
}
public static function getpfeats(){ return self::$PFEAT; }
public function send_header(){
thm_mediaq::append_js_file('tooltip_imdb');
thm_mediaq::append_js_file('tooltip_nfo');
$rpc = hellarpcs::getname($this->args['rpc']);
$hdata['title'] = langs::translate('pg_brwtitle', Array('rpc'=>$rpc), 'hellarpcs');
$hdata['rpc'] = $this->args['rpc'];
$this->check_action('list');
themes::send_header($hdata);
}
public function send_footer(){ return themes::send_footer(); }
public function send_content($pdata){
$this->TPL = new template();
$this->TPL->output_function('langs::output_for_client');
if ( FALSE === $this->TPL->load_file('pages/browse/pg_browse.tpl') ){ return FALSE; }
$buf['rpc'] = $this->args['rpc'];
$buf['rpcs'] = $this->RPCs;
$buf['multirpc'] = ( 1 < count($buf['rpcs']) );
if ( !isset($_GET['order']) ){ $_GET['order'] = 'date'; }
if ( FALSE === array_search($_GET['order'], Array('title', 'size', 'date')) ){ $_GET['order'] = 'date'; }
if ( !isset($_GET['way']) ){ $_GET['way'] = 'DESC'; }
if ( FALSE === array_search($_GET['way'], Array('ASC', 'DESC')) ){ $_GET['way'] = 'DESC'; }
$buf['order'] = $_GET['order'];
$buf['way'] = $_GET['way'];
$buf['repository'] = hellarpcs::get_destrepo($this->args['rpc']);
if ( empty($buf['repository']) ){
$buf['repository'] = 'none';
$buf['repo_age'] = 0;
$buf['repo_type'] = '?';
} else {
$buf['repo_age'] = hellarpcs::get_lastbrw($this->args['rpc']);
$tmp = CORE::ElapsedTime($buf['repo_age']);
$buf['repo_age'] = langs::number($tmp[0]).' '.langs::translate($tmp[1], NULL, 'base');
$buf['repo_type'] = configs::get('file_repo', $buf['repository'], 'type');
unset($tmp);
}
if( $buf['repo_type'] == 'local'){
$s = disk_free_space( configs::get('file_repo', $buf['repository'], 'conf') );
$free = $s;
$s = CORE::Byteconvert($s);
$buf['repo_free'] = langs::number($s[0], $s[2]).' '.$s[1];
$s = disk_total_space( configs::get('file_repo', $buf['repository'], 'conf') );
$tot = $s;
$s = CORE::Byteconvert($s);
$buf['repo_tot'] = langs::number($s[0], $s[2]).' '.$s[1];
$buf['repo_usedprct'] = CORE::ToPercent(($tot-$free), $tot);
unset($s);
} else {
$buf['repo_free'] = "?";
$buf['repo_tot'] = "?";
$buf['repo_usedprct'] = 0;
}
$this->TPL->setdata($buf);
unset($buf);
$this->TPL->parse_before('categories');
if ( FALSE != hellarpcs::isactif($this->args['rpc']) ){
ini_set('max_execution_time', 60); //hmmmm
foreach(search_nzbid::$cats as $c => $cat){ $this->draw_cat($c); }
$this->draw_cat('');
} else {
$DATA['items'] = NULL;
$DATA['item_count'] = 0;
$DATA['blko'] = 'close';
foreach(search_nzbid::$cats as $c => $cat){
$DATA['cat'] = $cat;
$this->TPL->parse_once('categories', $DATA, $c);
}
$DATA['cat'] = '';
$this->TPL->parse_once('categories', $DATA, '');
}
$this->TPL->parse_after('categories');
unset($this->TPL);
return TRUE;
}
private function docontent($ct, $base){
if ( isset($ct['dir']) ){
foreach($ct['dir'] as $k => &$entry){
$entry['url'] = rawurlencode($entry['title']);
$entry['title'] = htmlentities($entry['title']);
$entry['content'] = $this->docontent($entry['content'], $base.'/'.$entry['url']);
$entry['content']['unic'] = CORE::hash();
$entry['unic'] = $entry['content']['unic'];
}
}
if ( isset($ct['file']) ){
foreach($ct['file'] as $k => &$entry){
$entry['url'] = rawurlencode($entry['title']);
$s = CORE::Byteconvert($entry['size']);
$entry['size'] = langs::number($s[0], $s[2]).' '.$s[1];
$entry['icon'] = '';
foreach(self::$mimes as $ext => &$type){
if ( eregi($ext, $entry['title']) ){ $entry['icon'] = $type;break; }
}
$entry['title'] = htmlentities($entry['title']);
}
}
$ct['baseurl'] = $base;
return $ct;
}
private function draw_cat($c){
if ( $c !== '' ){
$cat = search_nzbid::$cats[$c];
} else {
$cat ='';
}
while( FALSE !== ($item = hellarpcs::listbycat($this->args['rpc'], $c, $_GET['order'], $_GET['way']))){
$tmp['title'] = htmlentities($item['title']);
$tmp['unic'] = CORE::hash();
$tmp['blko'] = 'close'; //item detail blockvis status
$tmp['fileblko'] = 'close'; //item files list blockvis status
if ( $item['engine'] == '' OR $item['id'] == '' ){
$tmp['type'] = 'noid';
$tmp['id'] = '';
} else {
$tmp['type'] = $item['engine'];
$tmp['id'] = $item['id'];
$tmp['details'] = search::get_details($tmp['type'], $tmp['id']);
}
if ($c == '' ){
$tmp['baseurl'] = hellarpcs::get_repobaseurl($this->args['rpc'])."/".rawurlencode($item['title']);
} else {
$tmp['baseurl'] = hellarpcs::get_repobaseurl($this->args['rpc'])."$cat/".rawurlencode($item['title']);
}
$tmp['content'] = unserialize(base64_decode($item['data']));
$tmp['content'] = $this->docontent($tmp['content'], $tmp['baseurl']);
$tmp['content']['unic'] = $tmp['unic'];
$s = CORE::Byteconvert($item['size']);
$tmp['size'] = langs::number($s[0], $s[2]).' '.$s[1];
$tmp['numf'] = $tmp['content']['cf'];
$tmp['numd'] = $tmp['content']['cd'];
$buf = CORE::ElapsedTime($item['date']);
$tmp['age'] = langs::number($buf[0]).' '.langs::translate($buf[1], NULL, 'base');
unset($buf);
$ITEMS[] = $tmp;
}
$DATA['cat'] = $cat;
if ( !isset($ITEMS) ){ $ITEMS = NULL; }
$DATA['items'] = $ITEMS;
$DATA['item_count'] = count($ITEMS);
thm_blockvis::append_block($cat, 'browse', '', TRUE);
$b = thm_blockvis::blockisopen('browse', $cat);
if ( $b AND $DATA['item_count'] != 0 ){ $DATA['blko'] = 'open'; } else { $DATA['blko'] = 'close'; }
unset($ITEMS);
$this->TPL->parse_once('categories', $DATA, $c);
unset($DATA);
}
private function check_action($act){
$o = $this->REALM->checks('rpc', $act, $this->args['rpc']);
if ( FALSE === $o ){
pagegen::add_event("You are not allowed to '$act' RPC ".$this->args['rpc']);
}
return $o;
}
}