<?php
require_once __DIR__ . '/../third_party/node.php';
if (!class_exists('\Requests')) {
    require_once __DIR__ . '/../third_party/Requests.php';
}
if (!class_exists('\Firebase\JWT\SignatureInvalidException')) {
    require_once __DIR__ . '/../third_party/php-jwt/SignatureInvalidException.php';
}
if (!class_exists('\Firebase\JWT\JWT')) {
    require_once __DIR__ . '/../third_party/php-jwt/JWT.php';
}

use \Firebase\JWT\JWT;
use Requests as Requests;

Requests::register_autoloader();

class Envapi
{

    private static $username = 'ck_3333a73f6e4f3ade0910beecb730623cbcf43f90'; // replace the API user here.
    private static $password = 'cs_c5ed36ad57c62b16d9d97e811af2b1985c2406ce'; // replace the API password here.

    public static function getPurchaseData($code)
    {
        //setting the header for the rest of the api

        $url = "https://myperfexcrm.com/wp-json/lmfwc/v2/licenses/activate/" . $code;

        $username = self::$username;
        $password = self::$password;

        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

        $resp = curl_exec($curl);
        curl_close($curl);

        if ('' != $resp) {
            return json_decode($resp);
        }

        return false;
    }


    public static function validateLicense($code)
    {

        $url = "https://myperfexcrm.com/wp-json/lmfwc/v2/licenses/validate/" . $code;

        $username = self::$username;
        $password = self::$password;

        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

        $resp = curl_exec($curl);
        curl_close($curl);

        if ('' != $resp) {
            return json_decode($resp);
        }

        return false;
    }

    public static function deactivateLicense($code)
    {
        $url = "https://myperfexcrm.com/wp-json/lmfwc/v2/licenses/deactivate/" . $code;

        $username = self::$username;
        $password = self::$password;

        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

        $resp = curl_exec($curl);
        curl_close($curl);

        if ('' != $resp) {
            return json_decode($resp);
        }

        return false;
    }


    public function validatePurchase($module_name)
    {

        $CI       = &get_instance();
        $verified = false;

        if (!option_exists($module_name . '_verification_id') || !option_exists($module_name . '_verified') || 1 != get_option($module_name . '_verified') || !option_exists($module_name . '_last_verification')) {
            $CI->app_modules->deactivate($module_name);
            return false;
        }

        $verification_id =  get_option($module_name . '_verification_id');
        $last_verification = get_option($module_name . '_last_verification');
        $expire_verification = get_option($module_name . '_expire_verification');



        if (($last_verification + 900) > time()) {
            $verified = true;
        } else {
            $lic_manager_validate = self::validateLicense($verification_id);

            if (isset($lic_manager_validate->code) && !empty($lic_manager_validate->code)) {
                $CI->app_modules->deactivate($module_name);
                return false;
            }

            if (!empty($lic_manager_validate) && $lic_manager_validate->success == true && $lic_manager_validate->data->timesActivated == 1) {
                $verified = true;
                update_option($module_name . '_last_verification', time());
            }
        }

        if (!$verified) {
            $CI->app_modules->deactivate($module_name);
        }

        return $verified;
    }
}
