<?php
/**
 * Created by PhpStorm.
 * File: functions.php
 * Project: grilabs-woocommerce-parasut
 * Year: 2018
 */

if (!function_exists('grilabs_grilabswooparasut_checklicense')) {
    /**
     * @param null $hash
     * @param null $module_short_name
     */

    function grilabs_grilabswooparasut_checklicense($hash = null, $module_short_name = null)
    {
        $date = date('dmY');
        $domain = get_option('siteurl');
        $domain = str_replace(array('http://', 'https://', 'www.'), array('', '', ''), $domain);
        $domain = explode("/", $domain)[0];

        if (empty($domain)) {
            throw new Exception('Error #003 - Your domain could not read. Please check `siteurl` option in your Wordpress installation. Contact us: support@gri.net');
        }

        if ($hash == null or $module_short_name == null) {
            throw new Exception('Error #001 - Some parameters missing. Please contact us: support@gri.net');
        }

        $license_file = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'license_files' . DIRECTORY_SEPARATOR . $module_short_name;
        if (!file_exists($license_file)) {
            throw new Exception(sprintf('Error #002 - Please create %s file.', $license_file));
        }

        $read_license = file_get_contents($license_file);
        $read_license = explode("_", $read_license);

        // date check
        if (@$read_license[1] !== base64_encode(sha1($domain . $hash . $date)) || count($read_license) !== 2) {
            // first-check
            $curl_Url = "https://lcs.gri.net/check.php?" . http_build_query(array(
                    'domain' => $domain,
                    'product' => $module_short_name
                ));
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $curl_Url);
            curl_setopt($ch, CURLOPT_POST, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $get_json_str = curl_exec($ch);
            if (curl_errno($ch)) {
                throw new Exception(sprintf('Error #006 - cURL Request ended with %s error.', curl_errno($ch)));
            }
            curl_close($ch);

            if (!$get_json_str) {
                throw new Exception('Error #004 - Unable to connect to the license server. Please contact your server maanger or contact us: support@gri.net');
            }

            $jsonToArray = json_decode($get_json_str);

            if (!isset($jsonToArray->status)) {
                throw new Exception('Error #005 - License server is responding incorrect response. Contact us: support@gri.net');
            }

            if ($jsonToArray->status == "1") {
                // re-generate
                $newKey = base64_encode(sha1($domain . $hash . $date));
                file_put_contents($license_file, $jsonToArray->license_str . "_" . $newKey);
            } else {
                throw new Exception(sprintf('Error #007 - License could not found for %s module. Your domain is: %s', $module_short_name, $domain));
            }
        }
    }
}
try {
    grilabs_grilabswooparasut_checklicense("kE44breSwuba", "GRILABSWOOPARASUT");
} catch (Exception $e) {
    wp_die($e->getMessage());
}

include dirname(__FILE__) . '/functions/general.php';
include dirname(__FILE__) . '/functions/product.php';
include dirname(__FILE__) . '/functions_customization.php';
include dirname(__FILE__) . '/functions/product-after.php';
include dirname(__FILE__) . '/functions/template.php';
include dirname(__FILE__) . '/functions/customer.php';
include dirname(__FILE__) . '/functions/order.php';
include dirname(__FILE__) . '/functions/enums.php';
include dirname(__FILE__) . '/functions/email.php';
include dirname(__FILE__) . '/functions/payment.php';
include dirname(__FILE__) . '/functions/gateway.php';
include dirname(__FILE__) . '/functions/fees.php';