<?php
/*********************/
/*                   */
/*  Dezend for PHP5  */
/*         NWS       */
/*      Nulled.WS    */
/*                   */
/*********************/

class Box_LicensingServer
{

    private $_log = NULL;
    private $_result = array( 'licensed_to' => NULL, 'created_at' => NULL, 'expires_at' => NULL, 'valid' => FALSE, 'error' => NULL, 'error_code' => NULL );

    public function __construct( $log )
    {
        $this->_log = $log;
    }

    public function handle( $request )
    {
        try
        {
            $this->process( $request );
        }
        catch ( LogicException $e )
        {
            $this->_log->info( $e->getMessage( )." ".$e->getCode( ) );
            $this->_result['error'] = $e->getMessage( );
            $this->_result['error_code'] = $e->getCode( );
    }
    catch ( Exception $e )
    {
        error_log( $e );
        $this->_log->info( $e->getMessage( )." ".$e->getCode( ) );
        $this->_result['error'] = "Licensing server is temporary unavailable";
        $this->_result['error_code'] = $e->getCode( );
}
return $this->_result;
}

    private function getServer( $key = null, $default = null )
    {
        if ( null === $key )
        {
            return $_SERVER;
        }
        return isset( $_SERVER[$key] ) ? $_SERVER[$key] : $default;
    }

    private function getIp( $checkProxy = true )
    {
        $ip = NULL;
        if ( $checkProxy && $this->getServer( "HTTP_CLIENT_IP" ) != null )
        {
            $ip = $this->getServer( "HTTP_CLIENT_IP" );
        }
        else if ( $checkProxy && $this->getServer( "HTTP_X_FORWARDED_FOR" ) != null )
        {
            $ip = $this->getServer( "HTTP_X_FORWARDED_FOR" );
        }
        else
        {
            $ip = $this->getServer( "REMOTE_ADDR" );
        }
        return $ip;
    }

    private function process( $json_str )
    {
        $data = ( $json_str );
        if ( empty( $data ) )
        {
            throw new LogicException( "Invalid JSON request", 1000 );
        }
        $data['ip'] = $this->getIp( );
        if ( !isset( $data['license'] ) || empty( $data['license'] ) )
        {
            throw new LogicException( "License key is not present in call", 1001 );
        }
        if ( !isset( $data['host'] ) || empty( $data['host'] ) )
        {
            throw new LogicException( "Host key is not present in call", 1002 );
        }
        if ( !isset( $data['version'] ) || empty( $data['version'] ) )
        {
            throw new LogicException( "Version key is not present in call", 1003 );
        }
        if ( !isset( $data['path'] ) || empty( $data['path'] ) )
        {
            throw new LogicException( "Path key is not present in call", 1004 );
        }
        $key = trim( $data['license'] );
        $ip = $data['ip'];
        $host = $data['host'];
        $version = $data['version'];
        $path = $data['path'];
        $table = ( "Model_ServiceLicense" );
        $model = $table->findOneBy( "license_key", $key );
        if ( !$model instanceof Model_ServiceLicense )
        {
            throw new LogicException( "Your license key is not valid.", 1005 );
        }
        if ( !$table->isLicenseActive( $model ) )
        {
            throw new LogicException( "License is not active", 1006 );
        }
        if ( !$table->isValidIp( $model, $ip ) )
        {
            throw new LogicException( sprintf( "Ip \"%s\" is not allowed for this license", $ip ), 1007 );
        }
        if ( !$table->isValidHost( $model, $host ) )
        {
            throw new LogicException( sprintf( "Host \"%s\" is not allowed for this license", $host ), 1008 );
        }
        if ( !$table->isValidVersion( $model, $version ) )
        {
            throw new LogicException( sprintf( "Version \"%s\" is not valid for this license", $version ), 1009 );
        }
        if ( !$table->isValidPath( $model, $path ) )
        {
            throw new LogicException( sprintf( "Software install path \"%s\" is not valid for this license", $path ), 1010 );
        }
        $this->_result['licensed_to'] = $table->getOwnerName( $model );
        $this->_result['created_at'] = $model->created_at;
        $this->_result['expires_at'] = $table->getExpirationDate( $model );
        $this->_result['valid'] = TRUE;
        $array = $table->getAdditionalParams( $model, $data );
        if ( !empty( $array ) )
        {
            foreach ( $array as $k => $v )
            {
                $this->_result[$k] = $v;
            }
        }
    }

}

?>
