<?php

// no direct access
defined('_JEXEC') or die;

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

class SPStgSrvControllerInstaller extends JControllerLegacy {

    public function install() {

        error_reporting(0);
        $app = JFactory::getApplication();
        $model = $this->getModel('installer');

        //$data = xmlrpc_decode(trim($_POST['data'])); 
        $data['package_name'] = trim($_POST['package_name']);
        $data['package_path'] = trim($_POST['package_path']);
        $data['created_by'] = trim($_POST['created_by']);

        $ip = $model->getIP();
        $data = $model->getItem($data);

        if (!$data['result']) {
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        if (!$model->authorizeIP($ip)) {
            $data['result'] = 0;
            $data['message'] = JText::_('COM_SPSTGSRV_UNAUTHORIZED_IP');
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        $message = $model->install($data);
        if ($message != 'success') {
            $data['result'] = 0;
            $data['message'] = $message;
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        $data['ip'] = $ip;
        $data['message'] = JText::_('COM_SPSTGSRV_INSTALL_SUCCESS');
        $model->save($data);
        echo json_encode($data);
        return true;
    }

    public function update() {
        error_reporting(0);
        $app = JFactory::getApplication();
        $model = $this->getModel('installer');
        //$data = xmlrpc_decode($_POST['data']);
        $data['extension_id'] = trim($_POST['extension_id']);
        $data['created_by'] = trim($_POST['created_by']);
        $ip = $model->getIP();

        $data = $model->getItem($data);
        if (!$data['result']) {
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        if (!$model->authorizeIP($ip)) {
            $data['result'] = 0;
            $data['message'] = JText::_('COM_SPSTGSRV_UNAUTHORIZED_IP');
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        $message = $model->update($data);
        if ($message != 'success') {
            $data['result'] = 0;
            $data['message'] = $message;
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        $data['ip'] = $ip;
        $data['message'] = JText::_('COM_SPSTGSRV_INSTALL_SUCCESS');
        $model->save($data);
        echo json_encode($data);
        return true;
    }

    public function read() {

        error_reporting(0);
        $app = JFactory::getApplication();
        $model = $this->getModel('installer');
        //$data = xmlrpc_decode($_POST['data']);
        $data['search'] = trim($_POST['search']);
        $data['enabled'] = trim($_POST['enabled']);
        $data['type'] = trim($_POST['type']);
        $data['client'] = trim($_POST['client']);
        $data['group'] = trim($_POST['group']);
        $data['hideprotected'] = trim($_POST['hideprotected']);
        $data['sp_limit'] = trim($_POST['sp_limit']); 
        $data['start'] = trim($_POST['start']); 

        $ip = $model->getIP();

        $data = $model->getItem($data);            
        if (!$data['result']) {
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        if (!$model->authorizeIP($ip)) {
            $data['result'] = 0;
            $data['message'] = JText::_('COM_SPSTGSRV_UNAUTHORIZED_IP');
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        $data = $model->getPackages($data);
        if ($data['message'] != 'success') {
            $data['result'] = 0;
            $data['ip'] = $ip;
            //$model->save($data);
            echo json_encode($data);
            return false;
        }

        $data['ip'] = $ip;
        $data['message'] = JText::_('COM_SPSTGSRV_READ_SUCCESS');
        //$model->save($data);
        echo json_encode($data);
        return true;
    }
    
    public function count() {
        error_reporting(0);
        $app = JFactory::getApplication();
        $model = $this->getModel('installer');
        //$data = xmlrpc_decode($_POST['data']);
        $data['search'] = trim($_POST['search']);
        $data['enabled'] = trim($_POST['enabled']);
        $data['type'] = trim($_POST['type']);
        $data['client'] = trim($_POST['client']);
        $data['group'] = trim($_POST['group']);
        $data['hideprotected'] = trim($_POST['hideprotected']); 
        
        $ip = $model->getIP();

        $data = $model->getItem($data); 

        if (!$data['result']) {
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        if (!$model->authorizeIP($ip)) {
            $data['result'] = 0;
            $data['message'] = JText::_('COM_SPSTGSRV_UNAUTHORIZED_IP');
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        $data = $model->count($data);       
        if ($data['message'] != 'success') {
            $data['result'] = 0;
            $data['ip'] = $ip;
            //$model->save($data);
            echo json_encode($data);
            return false;
        }

        $data['ip'] = $ip;
        $data['message'] = JText::_('COM_SPSTGSRV_READ_SUCCESS');
        //$model->save($data);
        echo json_encode($data);
        return true;
    }

    public function publish() {
        error_reporting(0);
        $app = JFactory::getApplication();
        $model = $this->getModel('installer');
        //$data = xmlrpc_decode($_POST['data']);
        $data['package_id'] = trim($_POST['package_id']);
        $data['enabled'] = trim($_POST['enabled']);
        $data['package_element'] = trim($_POST['package_element']);
        $data['created_by'] = trim($_POST['created_by']);
        $ip = $model->getIP();

        $data = $model->getItem($data);
        if (!$data['result']) {
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        if (!$model->authorizeIP($ip)) {
            $data['result'] = 0;
            $data['message'] = JText::_('COM_SPSTGSRV_UNAUTHORIZED_IP');
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        $message = $model->publish($data);
        if ($message != 'success') {
            $data['result'] = 0;
            $data['message'] = $message;
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        $data['ip'] = $ip;
        $data['message'] = JText::_('COM_SPSTGSRV_PUBLISH_SUCCESS');
        $model->save($data);
        echo json_encode($data);
        return true;
    }

    public function ping() {
        error_reporting(0);
        $app = JFactory::getApplication();
        $model = $this->getModel('installer');
        //$data = xmlrpc_decode($_POST['data']);
        $data['dummy'] = trim($_POST['dummy']);
        $ip = $model->getIP();

        $data = $model->getItem($data);
        if (!$data['result']) {
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        if (!$model->authorizeIP($ip)) {
            $data['result'] = 0;
            $data['message'] = JText::_('COM_SPSTGSRV_UNAUTHORIZED_IP');
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        $message = 'success';
        if ($message != 'success') {
            $data['result'] = 0;
            $data['message'] = $message;
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        $data['ip'] = $ip;
        $data['message'] = JText::_('COM_SPSTGSRV_PUBLISH_SUCCESS');
        echo json_encode($data);
        return true;
    }
    
    public function backup() {

        error_reporting(0);
        $app = JFactory::getApplication();
        $model = $this->getModel('installer');
        
        $ip = $model->getIP();

        $data = $model->getItem($data); 

        if (!$data['result']) {
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        if (!$model->authorizeIP($ip)) {
            $data['result'] = 0;
            $data['message'] = JText::_('COM_SPSTGSRV_UNAUTHORIZED_IP');
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        $data = $model->backup();       
        if ($data['message'] != 'success') {
            $data['result'] = 0;
            $data['ip'] = $ip;
            //$model->save($data);
            echo json_encode($data);
            return false;
        }

        $data['ip'] = $ip;
        $data['message'] = JText::_('COM_SPSTGSRV_BACKUP_SUCCESS');
        //$model->save($data);
        echo json_encode($data);
        return true;
    }

    public function remove() {
        error_reporting(0);
        $app = JFactory::getApplication();
        $model = $this->getModel('installer');
        //$data = xmlrpc_decode($_POST['data']);
        $data['package_name'] = trim($_POST['package_name']);
        $data['package_id'] = trim($_POST['package_id']);
        $data['package_element'] = trim($_POST['package_element']);
        $data['created_by'] = trim($_POST['created_by']);
        $ip = $model->getIP();

        $data = $model->getItem($data);
        if (!$data['result']) {
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        if (!$model->authorizeIP($ip)) {
            $data['result'] = 0;
            $data['message'] = JText::_('COM_SPSTGSRV_UNAUTHORIZED_IP');
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        $message = $model->remove($data);
        if ($message != 'success') {
            $data['result'] = 0;
            $data['message'] = $message;
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        $data['ip'] = $ip;
        $data['message'] = JText::_('COM_SPSTGSRV_UNINSTALL_SUCCESS');
        $model->save($data);
        echo json_encode($data);
        return true;
    }

    public function sync() {
        error_reporting(0);
        $app = JFactory::getApplication();
        $model = $this->getModel('installer');
        $data = xmlrpc_decode($_POST['data']);
        
        $ip = $model->getIP();

        $data = $model->getItem($data);
        if (!$data['result']) {
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        if (!$model->authorizeIP($ip)) {
            $data['result'] = 0;
            $data['message'] = JText::_('COM_SPSTGSRV_UNAUTHORIZED_IP');
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        $data = $model->sync($data);
        if (!$data['result']) {
            $data['result'] = 0;
            $data['message'] = $message;
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        $data['ip'] = $ip;
        $data['result'] = '1';
        $data['message'] = JText::_('COM_SPSTGSRV_SYNC_SUCCESS');
        $model->save($data);
        echo json_encode($data);
        return true;
    }

    public function find() {
        error_reporting(0);
        $app = JFactory::getApplication();
        $model = $this->getModel('installer');
        //$data = xmlrpc_decode($_POST['data']);
        $data['search'] = trim($_POST['search']);
        $data['enabled'] = trim($_POST['enabled']);
        $data['type'] = trim($_POST['type']);
        $data['client'] = trim($_POST['client']);
        $data['group'] = trim($_POST['group']);
        $data['hideprotected'] = trim($_POST['hideprotected']);
        $ip = $model->getIP();

        $data = $model->getItem($data);
        if (!$data['result']) {
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        if (!$model->authorizeIP($ip)) {
            $data['result'] = 0;
            $data['message'] = JText::_('COM_SPSTGSRV_UNAUTHORIZED_IP');
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        $data = $model->find($data);
        if (!$data['result']) {
            //$data['result'] = 0;
            //$data['message'] = $message;
            $data['ip'] = $ip;
            $model->save($data);
            echo json_encode($data);
            return false;
        }

        $data['ip'] = $ip;
        $data['message'] = JText::_('COM_SPSTGSRV_FIND_SUCCESS');
        $model->save($data);
        echo json_encode($data);
        return true;
    }

    public function getModel($name = '', $prefix = '', $config = array('ignore_request' => true)) {
        return parent::getModel($name, $prefix, array('ignore_request' => false));
    }

}

?>
