<?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_rpc extends pg_helper {
public static $PFEAT= Array('autorefresh' => 10, "tooltip"=> NULL);
public static $AJAX = Array('tt_imdb', 'rpcall');
public static $PARG = Array('rpc');
private $REALM, $RPCS;
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 hellarpc config file', CORE_LOG_ALERT, 'RPC');
return;
}
foreach($rpcs as $k => &$RRef ){
$tmp['rpc'] = hellarpcs::getname($RRef);
$tmp['canlist'] = $this->REALM->checks('rpc', 'list', $RRef);
$tmp['canread'] = $this->REALM->checks('rpc', 'read', $RRef);
$tmp['canuse'] = $this->REALM->checks('rpc', 'use', $RRef);
$tmp['cansetup'] = $this->REALM->checks('rpc', 'manage', $RRef);
if ( FALSE !== $tmp['canlist'] AND FALSE !== $tmp['canread'] 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') ){
errors::raise('Missing Argument : RPC reference, using last one.', CORE_LOG_DEBUG, 'PGEN');
$this->args['rpc'] = sessions::read('lastrpc', 'profile');
} else {
errors::raise('Missing Argument : RPC reference, using first available.', CORE_LOG_DEBUG, 'PGEN');
$this->args['rpc'] = $dftrpc;
}
if ( isset($_GET['do']) ){ unset($_GET['do']); }
} 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');
hellarpcs::load($this->args['rpc'], NULL);
}
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');
thm_mediaq::append_js_file('rpc_setrate');
$hdata['title'] = langs::translate('pg_title', Array('rpc'=>$this->RPCS[$this->args['rpc']]['rpc']), 'hellarpcs');
$hdata['rpc'] = $this->args['rpc'];
//$this->GET_action();
$this->check_action('list');
themes::send_header($hdata);
}
public function send_footer(){ return themes::send_footer(); }
public function send_content($pdata){
if ( FALSE === $this->check_action('list') ){ return; }
$tpl = new template();
$tpl->output_function('langs::output_for_client');
if ( FALSE === $tpl->load_file('pages/rpc/pg_rpc.tpl') ){ return FALSE; }
if ( FALSE === sessions::isdefined('form_rpc', 'profile') ){ sessions::set('form_rpc', CORE::hash(), 'profile'); }
$buf['managekey'] = sessions::read('form_rpc', 'profile');
$parts = Array('info', 'dload', 'queued', 'delayed', 'proc', 'log');
foreach($parts as $k => &$part){
thm_blockvis::append_block($part, 'rpc', '', TRUE);
$b = thm_blockvis::blockisopen('rpc', $part);
if ( $b ){ $buf[$part.'_blko'] = 'open'; } else { $buf[$part.'_blko'] = 'close'; }
}
unset($parts);
foreach($this->RPCS as $RRef => &$info ){
if ( $info['canlist'] ){ $buf['rpcs'][$RRef] = $info; }
}
$buf['multirpc'] = count($buf['rpcs']) > 1;
$buf['rid'] = $this->args['rpc'];
$buf['rpc'] = $this->RPCS[$this->args['rpc']]['rpc'];
$buf['isactif'] = hellarpcs::isactif($this->args['rpc']);
$buf['isup'] = hellarpcs::isrunning($this->args['rpc']);
$buf['can_manage'] = $this->RPCS[$this->args['rpc']]['cansetup'];
$buf['repository'] = hellarpcs::get_destrepo($this->args['rpc']);
if ( FALSE != $buf['repository'] ){
$repo_type = configs::get('file_repo', $buf['repository'], 'type');
if( $repo_type == 'local'){
$f = disk_free_space( configs::get('file_repo', $buf['repository'], 'conf') );
$s = CORE::Byteconvert($f);
$buf['spaceleft'] = langs::number($s[0], $s[2]).' '.$s[1];
$t = disk_total_space( configs::get('file_repo', $buf['repository'], 'conf') );
$buf['usedprct'] = CORE::ToPercent($t-$f, $t);
$s = CORE::Byteconvert($t);
$buf['spacetot'] = langs::number($s[0], $s[2]).' '.$s[1];
unset($s, $f, $t);
} else {
$buf['spaceleft'] = "?";
$buf['spacetot'] = "?";
$buf['usedprct'] = 0;
}
$buf['repository'] = $buf['repository']." ($repo_type)";
unset($repo_type);
} else {
$buf['repository'] = 'none';
$buf['spaceleft'] = "?";
$buf['spacetot'] = "?";
$buf['usedprct'] = 0;
}
if ( $buf['isup'] AND $buf['isactif']){
$r = hellarpcs::getstatus($this->args['rpc']);
$buf['uptime'] = $r['uptime'];
$buf['version'] = $r['version'];
$buf['maxrate'] = $r['maxrate'];
$buf['hostname'] = $r['hostname'];
$buf['is_paused'] = $r['is_paused'];
$buf['config_file'] = $r['config_file'];
$buf['tot_file_dload'] = langs::number($r['total_dl_files']);
$buf['tot_mb_dload'] = langs::number($r['total_dl_mb']);
$buf['tot_seg_dload'] = langs::number($r['total_dl_segments']);
$buf['showrpcdl'] = hellarpcs::isdloading($this->args['rpc']);
if ( $buf['showrpcdl'] ){
$buf['nzbName'] = htmlentities($r['currently_downloading'][0]['nzbName'], ENT_QUOTES, CORE::getintcharset());
if ( isset($r['currently_downloading'][0]['msgid']) ){
$did = hellarpcs::splitdid($r['currently_downloading'][0]['msgid']);
$buf['detail_type'] = $did['type'];
$buf['details'] = search::get_details($did['type'], $did['id']);
$buf['details']['detail'] = 'open';
if ( !$buf['details']['gotdetails'] ){
$buf['details']['title'] = $buf['nzbName'];
}
} else {
$buf['detail_type'] = 'nodid';
}
$buf['donepercent'] = $r['percent_complete'];
if ( ! $buf['is_paused'] ){
$buf['queue_mb']= langs::number($r['queued_mb'],0);
$buf['rate'] = langs::number($r['rate'],0);
$buf['eta'] = langs::number($r['eta'], 0);
}
}
foreach($r['queued'] as $n => $entry ){
$tmp['nzbName'] = htmlentities($entry['nzbName'], ENT_QUOTES, CORE::getintcharset());
$tmp['id'] = $entry['id'];
if ( isset($entry['msgid']) ){
$did = hellarpcs::splitdid($entry['msgid']);
$tmp['detail_type'] = $did['type'];
$tmp['details'] = search::get_details($did['type'], $did['id']);
$tmp['details']['detail'] = 'open';
if ( !$tmp['details']['gotdetails'] ){ //no details, so we use name from rpc
$tmp['details']['title'] = $tmp['nzbName'];
}
} else {
$tmp['detail_type'] = 'nodid';
}
$buf['queued'][$n] = $tmp;
unset($tmp);
}
$buf['showrpcproc'] = hellarpcs::isprocessing($this->args['rpc']);
if ( $buf['showrpcproc'] ){
foreach($r['currently_processing'] as $n => $entry ){
$buf['proc'][$n]['nzbName'] = htmlentities($entry['nzbName'], ENT_QUOTES, CORE::getintcharset());
$buf['proc'][$n]['id'] = $entry['id'];
if ( isset($entry['msgid']) ){
$did = hellarpcs::splitdid($entry['msgid']);
$buf['proc'][$n]['detail_type'] = $did['type'];
$buf['proc'][$n]['details'] = search::get_details($did['type'], $did['id']);
$buf['proc'][$n]['details']['detail'] = 'open';
if ( !$buf['proc'][$n]['details']['gotdetails'] ){
$buf['proc'][$n]['details']['title'] = $buf['proc'][$n]['nzbName'];
}
} else {
$buf['proc'][$n]['detail_type'] = 'nodid';
}
}
}
foreach($r['log_entries'] as $k => $log ){
if ( isset($log['INFO']) ){
//some cleaning, to prevent tidy warnings
$buf['log'][] = htmlentities($log['INFO'], ENT_QUOTES);
}
}
} else { //isdown
$buf['uptime'] = '0';
$buf['version'] = '-';
$buf['maxrate'] = '0';
$buf['hostname'] = '-';
$buf['is_paused'] = FALSE;
$buf['config_file'] = '-';
$buf['tot_file_dload'] = '0';
$buf['tot_mb_dload'] = '0';
$buf['tot_seg_dload'] = '0';
$buf['showrpcdl'] = FALSE;
$buf['showrpcproc'] = FALSE;
$buf['showrpcdl'] = FALSE;
}
/*
if ( nzbrate::q_count() != 0 ){ //delayed items
while( FALSE !== ( $rid = nzbrate::q_list() ) ){
$slot = unserialize(base64_decode($rid));
if ( $slot['type'] == 'enqueue' ){
$slot = $slot['data'];
if ( $slot[0] == $this->args['rpc'] ){
$tmp['id'] = $rid;
$tmp['detail_type'] = 'nbrid';
$tmp['details'] = search::get_details('nbrid', $slot[1]);
$buf['delayed'][] = $tmp;
unset($tmp);
}
}
}
}
*/
while( FALSE !== ( $delayed = hellarpcs::list_delayed($this->args['rpc']) ) ){
$tmp = $delayed;
$tmp['detail_type'] = 'nbrid';
$tmp['details'] = search::get_details('nbrid', $tmp['id']);
$buf['delayed'][] = $tmp;
unset($tmp);
}
$tpl->setdata($buf);
unset($buf);
$tpl->parse();
return TRUE;
}
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;
}
private function GET_action(){
if ( !isset($_GET['do']) ){ return; }
if ( ! isset($_GET['k']) OR ! sessions::isdefined('form_rpc', 'profile') ){
pagegen::add_event(langs::translate('err_nokey', NULL, 'hellarpcs'));
return;
} elseif ( $_GET['k'] != sessions::read('form_rpc', 'profile') ){
pagegen::add_event(langs::translate('err_badkey', NULL, 'hellarpcs'));
return;
}
sessions::del('form_rpc', 'profile'); //used only once to take an action, content will generate a newone
if ( $_GET['do'] == 'rpcdel' ){
if ( FALSE === $this->check_action('manage') ){ return; }
if ( isset($_GET['id']) ){
$r = hellarpcs::cancel($this->args['rpc'], $_GET['id']);
} else {
$r = hellarpcs::cancel($this->args['rpc']);
}
if ($r){
pagegen::add_event(langs::translate('msg_cancel', NULL, 'hellarpcs'));
} else {
pagegen::add_event(langs::translate('err_cancel', NULL, 'hellarpcs'));
}
} elseif ( $_GET['do'] == 'rpcup' ){
if ( FALSE === $this->check_action('manage') ){ return; }
if ( isset($_GET['id']) ){
if ( hellarpcs::moveup($this->args['rpc'], $_GET['id']) ){
pagegen::add_event(langs::translate('msg_mvup', NULL, 'hellarpcs'));
} else {
pagegen::add_event(langs::translate('err_mvup', NULL, 'hellarpcs'));
}
}
} elseif ( $_GET['do'] == 'rpcdwn' ){
if ( FALSE === $this->check_action('manage') ){ return; }
if ( isset($_GET['id']) ){
if ( hellarpcs::movedown($this->args['rpc'], $_GET['id']) ){
pagegen::add_event(langs::translate('hellarpcs', NULL, 'msg_mvdwn', NULL, 'hellarpcs'));
} else {
pagegen::add_event(langs::translate('hellarpcs', NULL, 'err_mvdwn', NULL, 'hellarpcs'));
}
}
} elseif ( $_GET['do'] == 'rpctop' ){
if ( FALSE === $this->check_action('manage') ){ return; }
if ( isset($_GET['id']) ){
if ( hellarpcs::movetop($this->args['rpc'], $_GET['id']) ){
pagegen::add_event(langs::translate('msg_mvtop', NULL, 'hellarpcs'));
} else {
pagegen::add_event(langs::translate('err_mvtop', NULL, 'hellarpcs'));
}
}
} elseif ( $_GET['do'] == 'rpcbot' ){
if ( FALSE === $this->check_action('manage') ){ return; }
if ( isset($_GET['id']) ){
if ( hellarpcs::movebot($this->args['rpc'], $_GET['id']) ){
pagegen::add_event(langs::translate('msg_mvbot', NULL, 'hellarpcs'));
} else {
pagegen::add_event(langs::translate('err_mvbot', NULL, 'hellarpcs'));
}
}
} elseif ( $_GET['do'] == 'rpcforce' ){
if ( FALSE === $this->check_action('manage') ){ return; }
if ( isset($_GET['id']) ){
if ( hellarpcs::force($this->args['rpc'], $_GET['id']) ){
pagegen::add_event(langs::translate('msg_force', NULL, 'hellarpcs'));
} else {
pagegen::add_event(langs::translate('err_force', NULL, 'hellarpcs'));
}
}
} elseif ( $_GET['do'] == 'pause' ){
if ( FALSE === $this->check_action('manage') ){ return; }
if ( hellarpcs::pause($this->args['rpc']) ){
pagegen::add_event(langs::translate('msg_pause', NULL, 'hellarpcs'));
} else {
pagegen::add_event(langs::translate('err_pause', NULL, 'hellarpcs'));
}
} elseif ( $_GET['do'] == 'resume' ){
if ( FALSE === $this->check_action('manage') ){ return; }
if ( hellarpcs::resume($this->args['rpc']) ){
pagegen::add_event(langs::translate('msg_resume', NULL, 'hellarpcs'));
} else {
pagegen::add_event(langs::translate('err_resume', NULL, 'hellarpcs'));
}
} elseif ( $_GET['do'] == 'setrate' ){
if ( FALSE === $this->check_action('manage') ){ return; }
if ( isset($_GET['rate']) ){
if ( hellarpcs::setrate($this->args['rpc'], $_GET['rate']) ){
pagegen::add_event(langs::translate('msg_setrate', NULL, 'hellarpcs'));
} else {
pagegen::add_event(langs::translate('err_setrate', NULL, 'hellarpcs'));
}
}
} elseif ( $_GET['do'] == 'remdelay' ){
if ( FALSE === $this->check_action('manage') ){ return; }
if ( isset($_GET['id']) ){
if ( nzbrate::q_del($_GET['id']) ){
pagegen::add_event(langs::translate('msg_delrem', NULL, 'hellarpcs'));
} else {
pagegen::add_event(langs::translate('err_delrem', NULL, 'hellarpcs'));
}
}
}
}
}