<?php
/**
 * @package JAdmin!
 * @version 1.5.3.0
 * @copyright (C) Copyright 2008-2010 CMS Fruit, CMSFruit.com. All rights reserved.
 * @license GNU/LGPL http://www.gnu.org/licenses/lgpl-3.0.txt

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as published by
 the Free Software Foundation; either version 3 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 Lesser General Public
 License for more details.

 You should have received a copy of the GNU Lesser General Public License
 along with this program.  If not, see http://www.gnu.org/licenses/.
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

set_time_limit(0);

jimport( 'joomla.application.component.model' );

require_once dirname(__FILE__).DS.'json.php';

class JAdminModelXMLRPCServer
{
    function addIPBlockerRule($xmlRpcMsg)
    {
	require_once dirname(__FILE__).DS.'user.php';
	require_once dirname(__FILE__).DS.'ipblocker.php';
	
        $_user =& JModel::getInstance('User', 'JAdminModel');
	$_ipBlocker =& JModel::getInstance('IPBlocker', 'JAdminModel');

        //////////////////////////////////////////////////////
        //    Check security
        //////////////////////////////////////////////////////
        $authKeyParam = $xmlRpcMsg->getParam(0);
        $userAuthkey = $authKeyParam->me['string'];

	$adminUser = $_user->isKeyValid($userAuthkey);

        if(!$adminUser) jexit('Access Denied');
        //////////////////////////////////////////////////////

	$descNameParam = $xmlRpcMsg->getParam(1);
	$descName = $descNameParam->me['string'];

	if(!empty($descName))
	{
	    $descName = base64_decode($descName);
	}
	else
	{
	    $descName = null;
	}

	$sourceIPAddressParam = $xmlRpcMsg->getParam(2);
	$sourceIPAddress = $sourceIPAddressParam->me['string'];

        return new xmlrpcresp(new xmlrpcval((bool)$_ipBlocker->addRule($adminUser, $descName, $sourceIPAddress), 'boolean'));
    }

    function deleteIPBlockerRules($xmlRpcMsg)
    {
	require_once dirname(__FILE__).DS.'user.php';
	require_once dirname(__FILE__).DS.'ipblocker.php';
	
        $_user =& JModel::getInstance('User', 'JAdminModel');
        $_ipBlocker =& JModel::getInstance('IPBlocker', 'JAdminModel');

        //////////////////////////////////////////////////////
        //    Check security
        //////////////////////////////////////////////////////
        $authKeyParam = $xmlRpcMsg->getParam(0);
        $userAuthkey = $authKeyParam->me['string'];

	$adminUser = $_user->isKeyValid($userAuthkey);

        if(!$adminUser) jexit('Access Denied');
        //////////////////////////////////////////////////////

	$ruleIdsParam = $xmlRpcMsg->getParam(1);
	$ruleIds = $ruleIdsParam->me['array'];

	$deleteRuleIds = array();

	if(!empty($ruleIds))
	{
	    foreach($ruleIds as $xmlRpcValObj)
	    {
		$deleteRuleIds[] = (int)php_xmlrpc_decode($xmlRpcValObj);
	    }
	}

        return new xmlrpcresp(new xmlrpcval((bool)$_ipBlocker->deleteRules($adminUser['user_id'], $deleteRuleIds), 'boolean'));
    }

    function updateOperatorSyncMethod($xmlRpcMsg)
    {
	require_once dirname(__FILE__).DS.'user.php';
	
        $_user =& JModel::getInstance('User', 'JAdminModel');

        //////////////////////////////////////////////////////
        //    Check security
        //////////////////////////////////////////////////////
        $authKeyParam = $xmlRpcMsg->getParam(0);
        $userAuthkey = $authKeyParam->me['string'];

	$adminUser = $_user->isKeyValid($userAuthkey);

        if(!$adminUser) jexit('Access Denied');
        //////////////////////////////////////////////////////

	require_once dirname(__FILE__).DS.'sync.php';

	$syncObj =& JModel::getInstance('Sync', 'JAdminModel');
	$syncObj->setUser($adminUser);
	
	$syncModeParam = $xmlRpcMsg->getParam(1);
	$syncMode = $syncModeParam->me['string'];

	$ipAddress = null;
	$listenPort = null;

	if($syncMode == 'push')
	{
	    $ipParam = $xmlRpcMsg->getParam(2);
	    $ipAddress = $ipParam->me['string'];

	    $listenPortParam = $xmlRpcMsg->getParam(3);
	    $listenPort = $listenPortParam->me['int'];
	}

        return new xmlrpcresp(new xmlrpcval((bool)$syncObj->updateOperatorSyncMethod($syncMode, $ipAddress, $listenPort), 'boolean'));
    }

    function getGatewayAuthkey($xmlRpcMsg)
    {
	require_once dirname(__FILE__).DS.'user.php';

        $_user =& JModel::getInstance('User', 'JAdminModel');

        //////////////////////////////////////////////////////
        //    Check security
        //////////////////////////////////////////////////////
        $authKeyParam = $xmlRpcMsg->getParam(0);
        $userAuthkey = $authKeyParam->me['string'];

	$adminUser = $_user->isKeyValid($userAuthkey);

        if(!$adminUser) jexit('Access Denied');
        //////////////////////////////////////////////////////

	return new xmlrpcresp(new xmlrpcval($_user->getGatewayAuthKey($adminUser['user_id']), 'string'));
    }
}

class JAdminModelServer
{
    function api_serve()
    {
	header('Content-type: text/plain; charset=utf-8'); // utf-8 encoding
	header('Cache-Control: no-cache, must-revalidate'); // HTTP/1.1
	header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
	
	$session =& JFactory::getSession();
	$session->destroy();
	$session->close();

	$mode = JRequest::getVar('mode', 'xmlrpc', 'method');

	require_once dirname(__FILE__).DS.'user.php';

	if($mode == 'regular')
	{
	    require_once dirname(__FILE__).DS.'sync.php';
	    
	    $key = JRequest::getVar('k', null, 'method');
	    $systemUUID = JRequest::getVar('uuid', null, 'method');

	    $userObj =& JModel::getInstance('User', 'JAdminModel');

	    $adminUser = $userObj->isKeyValid($key);

	    if(!$adminUser || empty($key)) jexit('Access Denied');

	    $json = new JAdminModelJSON();
	    
	    $syncObj =& JModel::getInstance('Sync', 'JAdminModel');
	    $syncObj->setUser($adminUser);
	    $syncObj->setUUID($systemUUID);
	    
	    $userObj->applyPendingChanges();
	   
	    echo $json->encode($syncObj->getUserData());
	}
	elseif($mode == 'xmlrpc')
	{
	    $useBuiltInXMLRPCLib = true;

	    if($useBuiltInXMLRPCLib)
	    {
		jimport('phpxmlrpc.xmlrpc');
		jimport('phpxmlrpc.xmlrpcs');
		jimport('phpxmlrpc.xmlrpc_wrappers');
	    }
	    else
	    {
		$xmlRPCPath = dirname(__FILE__).DS.'phpxmlrpc';

		require $xmlRPCPath.DS.'xmlrpc.php';
		require $xmlRPCPath.DS.'xmlrpcs.php';
		require $xmlRPCPath.DS.'xmlrpc_wrappers.php';
	    }
	    
	    $rpcServer = new xmlrpc_server(array(
		'add_ipblocker_rule' => array('function' => 'JAdminModelXMLRPCServer::addIPBlockerRule'),
		'delete_ipblocker_rules' => array('function' => 'JAdminModelXMLRPCServer::deleteIPBlockerRules'),
		'update_operator_sync_method' => array('function' => 'JAdminModelXMLRPCServer::updateOperatorSyncMethod'),
		'get_gateway_authkey' => array('function' => 'JAdminModelXMLRPCServer::getGatewayAuthkey')
	    ));
	}
	
	jexit();
    }
}