<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Mpc_env extends AdminController
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        show_404();
    }

    public function activate()
    {
        $res = $this->val_lic();
        if ($res['status']) {
            $res['original_url'] = $this->input->post('original_url');
        }
        echo json_encode($res);
    }

    public function upgrade_database()
    {
        $res = $this->val_lic();
        if ($res['status']) {
            $res['original_url'] = $this->input->post('original_url');
        }
        echo json_encode($res);
    }

    private function getUserIP()
    {
        $ipaddress = '';
        if (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } elseif (isset($_SERVER['HTTP_X_FORWARDED'])) {
            $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
        } elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
            $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
        } elseif (isset($_SERVER['HTTP_FORWARDED'])) {
            $ipaddress = $_SERVER['HTTP_FORWARDED'];
        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ipaddress = $_SERVER['REMOTE_ADDR'];
        } else {
            $ipaddress = 'UNKNOWN';
        }

        return $ipaddress;
    }

    private function val_lic()
    {

        $this->load->library('Envapi');
        if (empty($this->input->post('purchase_key'))) {
            return ['status' => false, 'message' => 'Purchase key is required'];
        }
        $module_name = $this->input->post('module_name');

        $lic_manager_validate = $this->envapi->validateLicense($this->input->post('purchase_key'));

        if (isset($lic_manager_validate->code) && !empty($lic_manager_validate->code)) {
            return ['status' => false, 'message' => $lic_manager_validate->message];
        }

        if (!empty($lic_manager_validate) && $lic_manager_validate->success == true && $lic_manager_validate->data->timesActivated <= 0) {

            $lic_manager = $this->envapi->getPurchaseData($this->input->post('purchase_key'));
            if (empty($lic_manager)) {
                return ['status' => false, 'message' => 'Something went wrong'];
            }

            if (isset($lic_manager->code) && !empty($lic_manager->code)) {
                return ['status' => false, 'message' => $lic_manager->message];
            }
            if (!empty($lic_manager->data->licenseKey)) {
                update_option($module_name . '_verification_id', $lic_manager->data->licenseKey);
                update_option($module_name . '_verified', true);
                update_option($module_name . '_last_verification', time());
                update_option($module_name . '_expire_verification', $lic_manager->data->expiresAt);
                return ['status' => true];
            } else {
                return ['status' => false, 'message' => 'Key not found!'];
            }
        } else {
            return ['status' => false, 'message' => 'This key is already in use!'];
        }
        return ['status' => false, 'message' => 'Something went wrong'];
    }
}
