<?php

require_once(dirname(__FILE__).'/../../lib/base-lib.inc');
require_once(dirname(__FILE__).'/../../lib/class.XMLparser.inc.php');

class VPIclient
{
    var $url;
    var $sequence;
    var $transact_id = 0;
    var $data = array();
    var $parser;
    
    public function __construct($url, $username, $password, $msg='', $key='')
    {
	$res = false;
	
	$this->url = $url.'?q=';
	if (($this->parser = new XMLparser))
	{
	    $this->sequence = $this->calc_sequence( $key);
	    if ( $this->parse_response('session_start&USERNAME='.$username.'&PASSWORD='.md5($password).'&key='.$this->sequence.'&message='.$msg))
		$res = true;
	}
	if ( !$res)
	{
	    echo('Error in VPI initialization');
	    exit(1);
	}

	return $this;
    }
       
    function parse_response($url)
    {
	$res = false;
	if (($data = file_get_contents($this->url.$url)))
	{
		if ( ($this->data = $this->parser->parse( $data)))
		{
			if ( is_array($this->data['response']))
			{
			    $pattern = '';
			    if (isset($this->data['response']['session_id']))
				$pattern = $this->data['response']['session_id'];
			    if ( $this->calc_sequence( $pattern))
			    {
				if ( $this->data['response']['response_code'] == 'ok')
				    $res = true;
			    }
			}
		}
	}
	return $res;
    }

    function calc_sequence( $pattern)
    {
	switch($this->transact_id)
	{
	    case 0:
		if ( empty($pattern))
		    $this->sequence = uniq_gen(16);
		else
		    $this->sequence = $pattern;
		break;
	    case 1:
		if ( empty($pattern))
		    return false;
		$this->sequence = $this->sequence.$pattern;
		break;
	    default:
		$this->sequence = md5($this->sequence);
		break;
	}
	$this->transact_id ++;
	return true;
    }
    
    function end()
    {
	if ( $this->parse_response('session_end&sequence_id='.$this->sequence))
	    return true;
	return false;
    }
    
    function get_provider_list( $service, &$a)
    {
	if ( $this->parse_response('get_provider_list&sequence_id='.$this->sequence.'&service='.$service))
	{
	    $a = $this->data['response']['providers'];
	    return true;
	}
	return false;
    }
    
    function get_tariff_list( $service, $provider, &$a)
    {
	if ( $this->parse_response('get_tariff_list&sequence_id='.$this->sequence.'&service='.$service.'&provider_id='.$provider))
	{
	    $a = $this->data['response']['tariffs'];
	    return true;
	}
	return false;
    }

    function disable_user( $service, $user, $cause='')
    {
	if ( $this->parse_response('disable_user&sequence_id='.$this->sequence.'&service='.$service.'&user_id='.$user.'&cause='.$cause))
	    return true;
	return false;
    }

    function enable_user( $service, $user, $cause='')
    {
	if ( $this->parse_response('enable_user&sequence_id='.$this->sequence.'&service='.$service.'&user_id='.$user.'&cause='.$cause))
	    return true;
	return false;
    }
    
    function create_user( $service, $tariff, $cause, &$user)
    {
	if ( $this->parse_response('create_user&sequence_id='.$this->sequence.'&service='.$service.'&tariff_id='.$tariff.'&cause='.$cause))
	{
	    $user = $this->data['response'];
	    return true;
	}
	return false;
    }
    
    function proceed_payment( $service, $user, &$sum, $currency, $doc, $cause)
    {
	$hash = md5($this->sequence.$service.$user.$sum.$currency.$doc.$cause);
	if ( $this->parse_response('proceed_payment&sequence_id='.$this->sequence.'&service='.$service.'&user_id='.$user.'&cause='.$cause.'&sum='.$sum.'&currency='.$currency.'&doc_id='.$doc.'&hash='.$hash))
	{
	    $sum = $this->data['response']['amount'];
	    return true;
	}
	return false;
    }

    function get_user_id( $service, $user, $password, &$details)
    {
	if ( $this->parse_response('get_user_id&sequence_id='.$this->sequence.'&service='.$service.'&uname='.$user.'&passwd='.$password))
	{
	    $details = $this->data['response'];
	    return true;
	}
	return false;
    }

    function get_tariff_list_enabled( $service, &$details)
    {
	if ( $this->parse_response('get_tariff_list_enabled&sequence_id='.$this->sequence.'&service='.$service))
	{
	    $details = $this->data['response']['tariffs'];
	    return true;
	}
	return false;
    }

}
?>
