<?php
require_once(dirname(__FILE__).'/../../lib/www-lib.inc');
require_once(dirname(__FILE__).'/../../lib/controller/core/class.PayOp.inc.php');

class VPI extends SSXApp {
    var	$time_interval = 300;
    var $dialin;

public function __construct() {
    $this->classname = 'SSXApp';
    parent::__construct(array('appname'=>'suite', 'sec_model'=>'RoboManager'));
    $this->dialin = $this->load_db_resource('WebRAD');
}
	
public function exec() {
    $att = Read();
    if (!isset( $GLOBALS['DOMAIN']))
	$GLOBALS['DOMAIN'] = $this->config['DOMAIN'];
    if (!isset( $GLOBALS['LANGUAGE']))
	$GLOBALS['LANGUAGE'] = $this->config['LANGUAGE'];
    $this->load_string_resource('cmmn');
    $method = $GLOBALS['q'];
    call_user_func(array($this,$method));
}

public function session_start() {
    if (!empty($GLOBALS['msg'])) {
	if ($this->security->validate_auth()) {
	    $session_id = md5($GLOBALS['key'].':'.uniq_gen(64).':'.$GLOBALS['nonce']);
	    if ($this->db->open_session($session_id, $_SERVER['REMOTE_ADDR'], $GLOBALS['login'], $GLOBALS['msg'], $GLOBALS['PROVIDER'], $GLOBALS['RESELLER'], $GLOBALS['SSXID']))
		return $this->ok(array('seq' => $session_id));
	}
    }
    return $this->fail();
}

public function session_end() {
    if (!empty($GLOBALS['seq'])) {
	if ( $this->db->close_session($GLOBALS['seq'], $_SERVER['REMOTE_ADDR']))
	    return $this->ok(array('seq' => ''));
    }
    return $this->fail();
}

public function get_tariff_list() {
    if (!empty($GLOBALS['seq']) && $this->db->validate_session($GLOBALS['seq'], $_SERVER['REMOTE_ADDR'])) {
	$operator_id = $this->db->get_operator_from_session($GLOBALS['seq'], $_SERVER['REMOTE_ADDR']);
	if ($a = $this->db->get_provider_from_operator($operator_id)) {
	    if (($a = $this->dialin->get_tariff_list_enabled($a[0][0], $operator_id))) {
		$r = array();
		for ( $i = 0; $i < count($a); $i++)
		    $r[] = array('id' => $a[$i][0], 'name' => utf8_encode($a[$i][1]), 'subject' => $a[$i][2], 'cost' => $a[$i][3], 'currency' => $a[$i][4]);
		$msg = array('tariffs' => $r);
		return $this->ok($msg);
	    }
	}
    }
    return $this->fail();
}

public function enable_user() {
    if (!empty($GLOBALS['seq']) && !empty($GLOBALS['uid']) && !empty($GLOBALS['cause'])) {
	if ($this->db->validate_session($GLOBALS['seq'], $_SERVER['REMOTE_ADDR'])) {
	    $operator_id = $this->db->get_operator_from_session($GLOBALS['seq'], $_SERVER['REMOTE_ADDR']);
	    if ($this->dialin->validate_access_to_uid($operator_id, $GLOBALS['uid'])) {
		if ($this->dialin->enable_user($GLOBALS['uid'])) {
		    $this->log(CMMN_INFO, $_SERVER['REMOTE_ADDR'], $_SERVER['PHP_SELF'], $GLOBALS['uid'], '', 'User enabled: '.$GLOBALS['cause']);	
		    return $this->ok(array('msg' => sprintf('User %s has been enabled: %s', $GLOBALS['uid'], $GLOBALS['cause'])));
		}
	    }
	}
    }
    return $this->fail();
}

public function disable_user() {
    if (!empty($GLOBALS['seq']) && !empty($GLOBALS['uid']) && !empty($GLOBALS['cause'])) {
	if ($this->db->validate_session($GLOBALS['seq'], $_SERVER['REMOTE_ADDR'])) {
	    $operator_id = $this->db->get_operator_from_session($GLOBALS['seq'], $_SERVER['REMOTE_ADDR']);
	    if ($this->dialin->validate_access_to_uid($operator_id, $GLOBALS['uid'])) {
		if ($this->dialin->disable_user($GLOBALS['uid'])) {
		    $this->log(CMMN_INFO, $_SERVER['REMOTE_ADDR'], $_SERVER['PHP_SELF'], $GLOBALS['uid'], '', 'User disabled: '.$GLOBALS['cause']);	
		    return $this->ok(array('msg' => sprintf('User %s has been disabled: %s', $GLOBALS['uid'], $GLOBALS['cause'])));
		}
	    }
	}
    }
    return $this->fail();
}

public function create_user() {
    if (!empty($GLOBALS['seq']) && !empty($GLOBALS['tariff']) && !empty($GLOBALS['cause'])) {
	if ($this->db->validate_session($GLOBALS['seq'], $_SERVER['REMOTE_ADDR'])) {
	    $operator_id = $this->db->get_operator_from_session($GLOBALS['seq'], $_SERVER['REMOTE_ADDR']);
	    if ($this->dialin->validate_access_to_tariff($operator_id, $GLOBALS['tariff'])) {
		$uname = uniq_gen($this->config['SECURITY']['password_len']);
		$password = uniq_gen($this->config['SECURITY']['password_len']);
		if ($this->dialin->insert_user($GLOBALS['tariff'], $uname, $password, true)) {
		    $uid = $this->dialin->get_uid($uname, $password);
		    if ($uid > 0) {
			$is_r = $this->dialin->is_account_recharged($uid);
			$this->log(CMMN_INFO, $_SERVER['REMOTE_ADDR'], $_SERVER['PHP_SELF'], $uid, '', 'User '.$uname.' disabled: '.$GLOBALS['cause']);
			return $this->ok(array('username' => $uname, 'password' => $password, 'uid' => $uid, 'can_top_up' => iif($is_r,1,0)));
		    } else
			$this->log(CMMN_ERROR, $_SERVER['REMOTE_ADDR'], $_SERVER['PHP_SELF'], "User :".$uname, '', 'Can not get ID!');
		} else
		    $this->log(CMMN_ERROR, $_SERVER['REMOTE_ADDR'], $_SERVER['PHP_SELF'], "TARIFF_ID :".$GLOBALS['tariff'], '', 'Can not create user!');
	    }
	}
    }
    return $this->fail();
}

public function get_uid() {
    if (!empty($GLOBALS['seq']) && !empty($GLOBALS['user']) && !empty($GLOBALS['pwd'])) {
	if ( $this->db->validate_session( $GLOBALS['seq'], $_SERVER['REMOTE_ADDR'])) {
	    $uid = $this->dialin->get_user_id( $GLOBALS['uname'], $GLOBALS['passwd']);
	    if ($uid > 0) {
		$operator_id = $this->db->get_operator_from_session($GLOBALS['seq'], $_SERVER['REMOTE_ADDR']);
		if ($this->dialin->validate_access_to_uid($operator_id, $uid)) {
		    $is_r = $this->dialin->is_account_recharged( $uid);
		    return $this->ok(array('uid' => $uid, 'can_top_up' => iif($is_r,1,0)));
		}
	    }
	}
    }
    return $this->fail();
}

public function proceed_payment() {
    if (!empty($GLOBALS['seq']) && !empty($GLOBALS['uid']) && !empty($GLOBALS['sum']) && !empty($GLOBALS['doc']) && !empty($GLOBALS['cause']) && !empty($GLOBALS['hash'])) {
	if ($this->db->validate_session($GLOBALS['seq'], $_SERVER['REMOTE_ADDR'])) {
	    $myhash = md5($GLOBALS['seq'].':'.$GLOBALS['uid'].':'.$GLOBALS['sum'].':'.$GLOBALS['doc'].':'.$GLOBALS['cause']);
	    if ($myhash == $GLOBALS['hash']) {
		$operator_id = $this->db->get_operator_from_session($GLOBALS['seq'], $_SERVER['REMOTE_ADDR']);
		if ($this->dialin->validate_access_to_uid($operator_id, $GLOBALS['uid'])) {
 		    $reseller_id = $this->db->get_reseller_asid_from_session($GLOBALS['seq'], $_SERVER['REMOTE_ADDR']);
		    $account_id = $this->dialin->get_account_from_uid($GLOBALS['uid']);
    		    $pay = new PayOp($this, $account_id, $GLOBALS['sum'], '', $reseller_id, $operator_id, $GLOBALS['doc'],$GLOBALS['cause']);
		    if (is_object($pay)) {
			if ($pay->process())
			    return $this->ok(array('uid' => $GLOBALS['uid'], 'sum' => $GLOBALS['sum'], 'action' => 'Top up'));
		    }
		}
	    }
	}
    }
    return $this->fail();
}

private function fail() {
    sleep(5);
    www_out_json(json_encode(array('response' => array('code' => "fail"))));
}

private function ok($embedded=array()) {
    www_out_json(json_encode(array('response' => array('code' => "ok", "data" => $embedded))));
}

}

?>
