<?php

namespace RSThemes\Template;

class License
{
    private $licenseKeyName = NULL;
    private $licenseSecretKey = NULL;
    private $licenseCacheConfigName = NULL;
    public $licenseKey = "";
    private $licenseInfo = [];
    public $url = "https://rsstudio.net/my-account/";
    public $licenseConfigName = "";
    public $keyFailExpDays = 30;
    public $keyFailWarningExpDays = 14;
    public $licenseCacheInfo = "";
    public $template = NULL;
    public $templateName = NULL;
    public $remote = false;
    public function __construct($licenseKeyName, $licenseSecretKey, $template)
    {
        $this->licenseKeyName = $licenseKeyName;
        $this->licenseSecretKey = $licenseSecretKey;
        $this->licenseCacheConfigName = sprintf("%s-data", $licenseKeyName);
        $this->licenseKey = (new \RSThemes\Models\Configuration())->getConfig($licenseKeyName);
        $this->templateName = $template->getMainName();
        $this->licenseInfoHash = (new \RSThemes\Models\Configuration())->getConfig($this->licenseCacheConfigName);
        $this->load();
    }
    private function load()
    {
        if (strlen($this->licenseInfoHash) <= 0) {
            $this->loadDefaultLicenseDetails();
        } else {
            $this->loadLicenseDetails();
        }
        if ($this->remoteRefreshNeeded()) {
            $this->reloadRemote();
        } else {
            $this->checkLicense();
        }
    }
    private function loadDefaultLicenseDetails()
    {
        $date = \Carbon\Carbon::now()->subYears(10)->format("Y-m-d");
        $licenseInfo = ["status" => "", "service_status" => "", "license_status" => "", "nextduedate" => "", "version" => "1.0.0", "lastRemoteChecked" => $date, "registeredname" => "", "companyname" => "", "email" => "", "serviceid" => "", "productid" => "", "productname" => "", "regdate" => "", "billingcycle" => "", "first_payment_amount" => "", "recuring_amount" => "", "payment_method" => "", "validdomain" => "", "validip" => "", "validdirectory" => "", "domainconnflict" => "", "ipconflict" => "", "dirconflict" => "", "message" => "", "messagecode" => "", "remoteChecked" => false, "licenseInvalidWarning" => false, "expiration" => false, "remoteProblem" => false];
        $data = $this->encodeLicense($licenseInfo);
        (new \RSThemes\Models\Configuration())->saveConfig($this->licenseCacheConfigName, $data);
        $this->licenseInfo = $licenseInfo;
        $this->licenseInfoHash = $data;
    }
    private function remoteRefreshNeeded()
    {
        if (strlen($this->licenseKey) <= 0) {
            return false;
        }
        if ($this->remote === true) {
            return true;
        }
        if ($this->licenseInfo["lastRemoteChecked"] != date("Y-m-d")) {
            return true;
        }
        return false;
    }
    public function expired()
    {
        if (0 < strlen($this->licenseInfo["nextduedate"])) {
            $dueDateDiff = \Carbon\Carbon::parse($this->licenseInfo["nextduedate"])->diffInDays(\Carbon\Carbon::today(), false);
            return in_array($dueDateDiff, [0, 7, 14, 30]);
        }
    }
    public function getExpiredText()
    {
        $dueDateDiff = \Carbon\Carbon::parse($this->licenseInfo["nextduedate"])->diffInDays(\Carbon\Carbon::today(), false);
        return \RSThemes\Helpers\Messages::get("expired." . $dueDateDiff);
    }
    public function warning()
    {
        if (isset($this->licenseInfo["warning"]) && $this->licenseInfo["warning"] == true) {
            return true;
        }
        return false;
    }
    private function remoteRefresh()
    {
        $data = $this->loadRemoteLicenseInfo($this->licenseKey);
        $this->loadDefaultLicenseDetails();
        $this->licenseInfo = array_merge($this->licenseInfo, $data);
        $this->checkLicense(true);
        $data = $this->encodeLicense($this->licenseInfo);
        (new \RSThemes\Models\Configuration())->saveConfig($this->licenseCacheConfigName, $data);
        $this->licenseInfoHash = $data;
    }
    private function reloadRemote()
    {
        $data = $this->loadRemoteLicenseInfo($this->licenseKey);
        if (in_array($data["license_status"], ["Active", "Suspended", "Expired"])) {
            $this->loadDefaultLicenseDetails();
            $this->licenseInfo = array_merge($this->licenseInfo, $data);
            $this->checkLicense();
            $data = $this->encodeLicense($this->licenseInfo);
            (new \RSThemes\Models\Configuration())->saveConfig($this->licenseCacheConfigName, $data);
            $this->licenseInfoHash = $data;
        } else {
            if (in_array($data["license_status"], ["Cancelled"])) {
                $this->deactivateTemplate();
            } else {
                if (in_array($data["license_status"], ["Invalid"]) && isset($this->licenseInfo["finalDate"])) {
                    if (0 < \Carbon\Carbon::parse($this->licenseInfo["finalDate"])->diffInDays(\Carbon\Carbon::today(), false)) {
                        $this->deactivateTemplate(true);
                    } else {
                        $this->licenseInfo["licenseInvalidWarning"] = true;
                        $data = $this->encodeLicense($this->licenseInfo);
                        (new \RSThemes\Models\Configuration())->saveConfig($this->licenseCacheConfigName, $data);
                        $this->licenseInfoHash = $data;
                    }
                } else {
                    if (in_array($data["license_status"], ["Unknown"]) && isset($this->licenseInfo["finalDate"])) {
                        if (0 < \Carbon\Carbon::parse($this->licenseInfo["finalDate"])->diffInDays(\Carbon\Carbon::today(), false)) {
                            $this->deactivateTemplate(true);
                            return NULL;
                        }
                        $this->licenseInfo["remoteProblem"] = true;
                        $data = $this->encodeLicense($this->licenseInfo);
                        (new \RSThemes\Models\Configuration())->saveConfig($this->licenseCacheConfigName, $data);
                        $this->licenseInfoHash = $data;
                    }
                }
            }
        }
    }
    private function checkLicense()
    {
        if (in_array($this->licenseInfo["license_status"], ["Active", "Suspended", "Expired"])) {
            $this->licenseInfo["finalDate"] = \Carbon\Carbon::now()->addDays($this->keyFailExpDays)->format("Y-m-d");
        }
    }
    private function loadLicenseDetails()
    {
        $this->licenseInfo = $this->decodeLicense($this->licenseInfoHash);
    }
    public function deactivateTemplate($warning = false)
    {
        if ((new \RSThemes\Models\Configuration())->getConfig("OrderFormTemplate") == $this->templateName) {
            (new \RSThemes\Models\Configuration())->saveConfig("OrderFormTemplate", "standard_cart");
        }
        if ((new \RSThemes\Models\Configuration())->getConfig("Template") == $this->templateName) {
            (new \RSThemes\Models\Configuration())->saveConfig("Template", "six");
        }
        $this->loadDefaultLicenseDetails();
        if ($warning) {
            $this->licenseInfo["expirationWarning"] = true;
            $data = $this->encodeLicense($this->licenseInfo);
            (new \RSThemes\Models\Configuration())->saveConfig($this->licenseCacheConfigName, $data);
            $this->licenseInfoHash = $data;
        }
    }
    public function isActive()
    {
        if (in_array($this->licenseInfo["license_status"], ["Active", "Suspended", "Expired"])) {
            return true;
        }
        return false;
    }
    public function getLicenseKey()
    {
        return $this->licenseKey;
    }
    public function details($key)
    {
        return isset($this->licenseInfo[$key]) ? $this->licenseInfo[$key] : "";
    }
    public function getDetails()
    {
        return $this->licenseInfo;
    }
    public function activateLicense($licenseKey)
    {
        $this->deactivateTemplate();
        (new \RSThemes\Models\Configuration())->saveConfig($this->licenseKeyName, $licenseKey);
        $this->licenseKey = $licenseKey;
        if (strlen($licenseKey) <= 0) {
            $this->deactivateTemplate();
            \RSThemes\Helpers\Flash::setFlashMessage("danger", \RSThemes\Helpers\Messages::get("errors.5"));
        } else {
            $this->remoteRefresh();
        }
        if (isset($this->licenseInfo["messagecode"]) && 0 < strlen($this->licenseInfo["messagecode"])) {
            return \RSThemes\Helpers\Flash::setFlashMessage("danger", $this->getMessagePart($this->licenseInfo["messagecode"], 0));
        }
    }
    public function getLastVersion()
    {
        return isset($this->licenseInfo["version"]) ? $this->licenseInfo["version"] : "";
    }
    public function getDashboardMessages()
    {
        $html = "";
        if (0 < strlen($this->licenseInfo["nextduedate"]) && $this->licenseInfo["nextduedate"] != "0000-00-00") {
            $dueDateDiff = \Carbon\Carbon::today()->diffInDays(\Carbon\Carbon::parse($this->licenseInfo["nextduedate"]), false);
            if (0 <= $dueDateDiff && $dueDateDiff <= 14) {
                $html .= "<div class=\"alert alert--outline has-icon alert--border-left alert--license alert--info\"><div class=\"alert__body\">";
                if ($dueDateDiff == 0) {
                    $html .= str_replace("%days%", $dueDateDiff, \RSThemes\Helpers\Messages::get("warnings.6"));
                } else {
                    $html .= str_replace("%days%", $dueDateDiff, \RSThemes\Helpers\Messages::get("warnings.5"));
                }
                $html .= "</div><div class=\"alert__actions\">\n                            <a class=\"btn btn-default\" href=\"https://rsstudio.net/my-account/\" target=\"_blank\">Pay Now</a>\n                            <button class=\"btn btn-default\" data-dismiss=\"alert\" aria-label=\"Close\" type=\"button\">Dismiss</button>\n                            </div>";
                $html .= "</div>";
                $html .= "<style>\n                    @font-face {\n                        font-family: \"Material-Design-Iconic-Font\";\n                        src: url(\"https://cdnjs.cloudflare.com/ajax/libs/material-design-iconic-font/2.2.0//fonts/Material-Design-Iconic-Font.woff2?v=2.2.0\") format(\"woff2\"), url(\"https://cdnjs.cloudflare.com/ajax/libs/material-design-iconic-font/2.2.0//fonts/Material-Design-Iconic-Font.woff?v=2.2.0\") format(\"woff\"), url(\"https://cdnjs.cloudflare.com/ajax/libs/material-design-iconic-font/2.2.0//fonts/Material-Design-Iconic-Font.ttf?v=2.2.0\") format(\"truetype\");\n                        font-weight: normal;\n                        font-style: normal;\n                    }\n                    .alert--license{\n                        position: relative;\n                        display: flex;\n                        justify-content: space-between;\n                        align-items: center;\n                        flex-flow: row wrap;\n                        padding: 13px 16px 13px 56px;\n                        margin-bottom: 32px;\n                        border: none;\n                        box-shadow: 0 2px 8px rgba(0,0,0,0.08);\n                        background: #fff;\n                    }\n                    .alert--license.alert--info{\n                        color: #50bfff;\n                        border-color: #50bfff;\n                    }\n                    .alert--license *{\n                        box-sizing: border-box;\n                    }\n                    .alert--license:before{\n                        position: absolute;\n                        top: 50%;\n                        left: 15px;\n                        width: 24px;\n                        height: 24px;                        \n                        margin-top: -12px;\n                        text-align: center;\n                        font-family: Material-Design-Iconic-Font;\n                        font-size: 24px;\n                        line-height: 22px;\n                    }\n                    .alert--license.alert--info:before{\n                        content: \"\\f1f7\";\n                    }\n                    .alert--license .alert__body{\n                        flex: 1;\n                        margin-right: auto;\n                    }\n                    .alert--license b{\n                        font-size: 14px;\n                        display: block;\n                        margin-bottom: 8px;\n                    }\n                    .alert--license b .hidden{\n                        display: inline-block!important;\n                    }\n                    .alert--license p{\n                        color: #505459;\n                        margin-bottom: 0;\n                    }\n                    .alert--license .alert__actions{\n                        display: flex;\n                        flex: 0 1 auto;\n                        white-space: nowrap;\n                    }\n                    .alert--license .alert__actions>*+* {\n                        margin-left: 16px;\n                    }\n                    .alert--license .alert__actions:last-child{\n                        margin-left: 16px;\n                    }\n                    .alert--license:after{\n                        position: absolute;\n                        top: -1px;\n                        bottom: -1px;\n                        left: -1px;\n                        content: \"\";\n                        border-radius: 3px 0 0 3px;\n                        border-left: 4px solid;\n                    }\n                    .widget-settings{\n                        margin-top: -28px;\n                    }\n                </style>";
            }
        }
        if (isset($this->licenseInfo["remoteProblem"]) && $this->licenseInfo["remoteProblem"] == true) {
            $days = \Carbon\Carbon::today()->diffInDays(\Carbon\Carbon::parse($this->licenseInfo["finalDate"]), false);
            $html .= "<div class=\"alert alert--outline has-icon alert--border-left alert--license alert--danger\"><div class=\"alert__body\">";
            $html .= str_replace("%days%", $days, \RSThemes\Helpers\Messages::get("warnings.7"));
            $html .= "</div><div class=\"alert__actions\">\n                            <a class=\"btn btn-default\" href=\"https://rsstudio.net/my-account/submitticket.php?step=2&deptid=7\" target=\"_blank\">Contact Us</a>\n                            </div>";
            $html .= "</div>";
            $html .= "<style>\n                    @font-face {\n                        font-family: \"Material-Design-Iconic-Font\";\n                        src: url(\"https://cdnjs.cloudflare.com/ajax/libs/material-design-iconic-font/2.2.0//fonts/Material-Design-Iconic-Font.woff2?v=2.2.0\") format(\"woff2\"), url(\"https://cdnjs.cloudflare.com/ajax/libs/material-design-iconic-font/2.2.0//fonts/Material-Design-Iconic-Font.woff?v=2.2.0\") format(\"woff\"), url(\"https://cdnjs.cloudflare.com/ajax/libs/material-design-iconic-font/2.2.0//fonts/Material-Design-Iconic-Font.ttf?v=2.2.0\") format(\"truetype\");\n                        font-weight: normal;\n                        font-style: normal;\n                    }\n                    .alert--license{\n                        position: relative;\n                        display: flex;\n                        justify-content: space-between;\n                        align-items: center;\n                        flex-flow: row wrap;\n                        padding: 13px 16px 13px 56px;\n                       \n                        margin-bottom: 32px;\n                        border: none;\n                        box-shadow: 0 2px 8px rgba(0,0,0,0.08);\n                        background: #fff;\n                        \n                    }\n                    .alert--license.alert--danger{\n                        color: #e53e3e;\n                        border-color: #e53e3e;\n                    }\n                    .alert--license *{\n                        box-sizing: border-box;\n                    }\n                    .alert--license:before{\n                        position: absolute;\n                        top: 50%;\n                        left: 15px;\n                        width: 24px;\n                        height: 24px;\n                        margin-top: -12px;\n                        text-align: center;\n                        font-family: Material-Design-Iconic-Font;\n                        font-size: 24px;\n                        line-height: 22px;\n                    }\n                    .alert--license.alert--danger:before{\n                        content: \"\\f1f4\";\n                    }\n                    .alert--license .alert__body{\n                        flex: 1;\n                        margin-right: auto;\n                    }\n                    .alert--license b{\n                        font-size: 14px;\n                        display: block;\n                        margin-bottom: 8px;\n                    }\n                    .alert--license b .hidden{\n                        display: inline-block!important;\n                    }\n                    .alert--license p{\n                        color: #505459;\n                        margin-bottom: 0;\n                    }\n                    .alert--license .alert__actions{\n                        display: flex;\n                        flex: 0 1 auto;\n                        white-space: nowrap;\n                    }\n                    .alert--license .alert__actions>*+* {\n                        margin-left: 16px;\n                    }\n                    .alert--license .alert__actions:last-child{\n                        margin-left: 16px;\n                    }\n                    .alert--license:after{\n                        position: absolute;\n                        top: -1px;\n                        bottom: -1px;\n                        left: -1px;\n                        content: \"\";\n                        border-radius: 3px 0 0 3px;\n                        border-left: 4px solid;\n                    }\n                    .widget-settings{\n                        margin-top: -28px;\n                    }\n                </style>";
        }
        if (isset($this->licenseInfo["licenseInvalidWarning"]) && $this->licenseInfo["licenseInvalidWarning"] == true) {
            $days = \Carbon\Carbon::today()->diffInDays(\Carbon\Carbon::parse($this->licenseInfo["finalDate"]), false);
            if ($days < $this->keyFailWarningExpDays) {
                $html .= "<div class=\"alert alert--outline has-icon alert--border-left alert--license alert--danger\"><div class=\"alert__body\">";
                $html .= str_replace("%days%", $days, \RSThemes\Helpers\Messages::get("warnings.9"));
                $html .= "</div><div class=\"alert__actions\">\n                            <a class=\"btn btn-default\" href=\"https://rsstudio.net/my-account/submitticket.php?step=2&deptid=7\" target=\"_blank\">Contact Us</a>\n                            </div>";
                $html .= "</div>";
                $html .= "<style>\n                    @font-face {\n                        font-family: \"Material-Design-Iconic-Font\";\n                        src: url(\"https://cdnjs.cloudflare.com/ajax/libs/material-design-iconic-font/2.2.0//fonts/Material-Design-Iconic-Font.woff2?v=2.2.0\") format(\"woff2\"), url(\"https://cdnjs.cloudflare.com/ajax/libs/material-design-iconic-font/2.2.0//fonts/Material-Design-Iconic-Font.woff?v=2.2.0\") format(\"woff\"), url(\"https://cdnjs.cloudflare.com/ajax/libs/material-design-iconic-font/2.2.0//fonts/Material-Design-Iconic-Font.ttf?v=2.2.0\") format(\"truetype\");\n                        font-weight: normal;\n                        font-style: normal;\n                    }\n                    .alert--license{\n                        position: relative;\n                        display: flex;\n                        justify-content: space-between;\n                        align-items: center;\n                        flex-flow: row wrap;\n                        padding: 13px 16px 13px 56px;\n                       \n                        margin-bottom: 32px;\n                        border: none;\n                        box-shadow: 0 2px 8px rgba(0,0,0,0.08);\n                        background: #fff;\n                        \n                    }\n                    .alert--license.alert--danger{\n                        color: #e53e3e;\n                        border-color: #e53e3e;\n                    }\n                    .alert--license *{\n                        box-sizing: border-box;\n                    }\n                    .alert--license:before{\n                        position: absolute;\n                        top: 50%;\n                        left: 15px;\n                        width: 24px;\n                        height: 24px;\n                        margin-top: -12px;\n                        text-align: center;\n                        font-family: Material-Design-Iconic-Font;\n                        font-size: 24px;\n                        line-height: 22px;\n                    }\n                    .alert--license.alert--danger:before{\n                        content: \"\\f1f4\";\n                    }\n                    .alert--license .alert__body{\n                        flex: 1;\n                        margin-right: auto;\n                    }\n                    .alert--license b{\n                        font-size: 14px;\n                        display: block;\n                        margin-bottom: 8px;\n                    }\n                    .alert--license b .hidden{\n                        display: inline-block!important;\n                    }\n                    .alert--license p{\n                        color: #505459;\n                        margin-bottom: 0;\n                    }\n                    .alert--license .alert__actions{\n                        display: flex;\n                        flex: 0 1 auto;\n                        white-space: nowrap;\n                    }\n                    .alert--license .alert__actions>*+* {\n                        margin-left: 16px;\n                    }\n                    .alert--license .alert__actions:last-child{\n                        margin-left: 16px;\n                    }\n                    .alert--license:after{\n                        position: absolute;\n                        top: -1px;\n                        bottom: -1px;\n                        left: -1px;\n                        content: \"\";\n                        border-radius: 3px 0 0 3px;\n                        border-left: 4px solid;\n                    }\n                    .widget-settings{\n                        margin-top: -28px;\n                    }\n                </style>";
            }
        }
        return $html;
    }
    public function hasProblem()
    {
        if (isset($this->licenseInfo["expiration"]) && $this->licenseInfo["expiration"] == true) {
            $days = \Carbon\Carbon::today()->diffInDays(\Carbon\Carbon::parse($this->licenseInfo["finalDate"]), false);
            if ($days < $this->keyFailWarningExpDays) {
                return true;
            }
        }
    }
    public function getProblem()
    {
        if (isset($this->licenseInfo["expiration"]) && $this->licenseInfo["expiration"] == true) {
            $days = \Carbon\Carbon::today()->diffInDays(\Carbon\Carbon::parse($this->licenseInfo["finalDate"]), false);
            if ($days < $this->keyFailWarningExpDays) {
                return $this->getMessagePart("warnings.8", 0);
            }
        }
    }
    public function hasInputError()
    {
        if (strlen($this->licenseKey) <= 0) {
            return true;
        }
        if (isset($this->licenseInfo["messagecode"]) && 0 < strlen($this->licenseInfo["messagecode"])) {
            return true;
        }
    }
    public function getInputError()
    {
        if (strlen($this->licenseKey) <= 0) {
            return \RSThemes\Helpers\Messages::get("messages.1");
        }
        if (isset($this->licenseInfo["messagecode"]) && 0 < strlen($this->licenseInfo["messagecode"])) {
            return $this->getMessagePart($this->licenseInfo["messagecode"]);
        }
    }
    public function getAddonMessages()
    {
        $html = "";
        if (0 < strlen($this->licenseInfo["nextduedate"]) && $this->licenseInfo["nextduedate"] != "0000-00-00") {
            $dueDateDiff = \Carbon\Carbon::today()->diffInDays(\Carbon\Carbon::parse($this->licenseInfo["nextduedate"]), false);
            if (0 <= $dueDateDiff && $dueDateDiff <= 30) {
                $html .= "<div class=\"alert alert--info alert--outline has-icon alert--border-left alert--license\" data-daysToExp=\"" . $days . "\"><div class=\"alert__body\">";
                if ($dueDateDiff == 0) {
                    $html .= str_replace("%days%", $dueDateDiff, \RSThemes\Helpers\Messages::get("warnings.6"));
                } else {
                    $html .= str_replace("%days%", $dueDateDiff, \RSThemes\Helpers\Messages::get("warnings.5"));
                }
                $html .= "</div><div class=\"alert__actions\">\n                            <a class=\"btn btn--default btn--outline btn--sm\" href=\"https://rsstudio.net/my-account/\" target=\"_blank\">Pay Now</a>\n                            <button class=\"btn btn--default btn--outline btn--sm\" data-dismiss=\"alert\" aria-label=\"Close\" type=\"button\">Dismiss</button>\n                        </div>";
                $html .= "</div>";
            }
        }
        if (isset($this->licenseInfo["remoteProblem"]) && $this->licenseInfo["remoteProblem"] == true && isset($this->licenseInfo["finalDate"])) {
            $days = \Carbon\Carbon::today()->diffInDays(\Carbon\Carbon::parse($this->licenseInfo["finalDate"]), false);
            $html .= "<div class=\"alert alert--danger alert--outline has-icon alert--border-left alert--license\" data-daysToExp=\"" . $days . "\"><div class=\"alert__body\">";
            $html .= str_replace("%x%", $days, \RSThemes\Helpers\Messages::get("warnings.7"));
            $html .= "</div><div class=\"alert__actions\">\n                <a class=\"btn btn--default btn--outline btn--sm\" href=\"https://rsstudio.net/my-account/submitticket.php?step=2&deptid=7\" target=\"_blank\">Contact Us</a>\n                </div>";
            $html .= "</div>";
        }
        if (isset($this->licenseInfo["licenseInvalidWarning"]) && $this->licenseInfo["licenseInvalidWarning"] == true) {
            $days = \Carbon\Carbon::today()->diffInDays(\Carbon\Carbon::parse($this->licenseInfo["finalDate"]), false);
            $html .= "<div class=\"alert alert--danger alert--outline has-icon alert--border-left alert--license\" data-daysToExp=\"" . $days . "\"><div class=\"alert__body\">";
            $html .= str_replace("%days%", $days, \RSThemes\Helpers\Messages::get("warnings.9"));
            $html .= "</div><div class=\"alert__actions\">\n                <a class=\"btn btn--default btn--outline btn--sm\" href=\"https://rsstudio.net/my-account/submitticket.php?step=2&deptid=7\" target=\"_blank\">Contact Us</a>\n                </div>";
            $html .= "</div>";
        }
        return $html;
    }
    private function getMessagePart($msg, $part = 1)
    {
        $message = explode("|", \RSThemes\Helpers\Messages::get($msg));
        if (count($message) == 0) {
            return $msg;
        }
        if (count($message) == 1) {
            return $message[0];
        }
        if (isset($message[$part])) {
            return $message[$part];
        }
        return $msg;
    }
    private function decodeLicense($localkey)
    {
        $localkey = str_replace("\n", "", $localkey);
        $localdata = substr($localkey, 0, strlen($localkey) - 32);
        $md5hash = substr($localkey, strlen($localkey) - 32);
        if ($md5hash == md5($localdata . $this->licenseSecretKey)) {
            $localdata = strrev($localdata);
            $md5hash = substr($localdata, 0, 32);
            $localdata = substr($localdata, 32);
            $localdata = base64_decode($localdata);
            $localdata = unserialize($localdata);
            return $localdata;
        }
        return $this->licenseInfo;
    }
    private function encodeLicense($details)
    {
        $data = serialize($details);
        $data = base64_encode($data);
        $checkdate = date("Ymd");
        $data = md5($checkdate . $this->licenseSecretKey) . $data;
        $data = strrev($data);
        $data = $data . md5($data . $this->licenseSecretKey);
        $data = wordwrap($data, 80, "\n", true);
        return $data;
    }
    private function loadRemoteLicenseInfo($licensekey)
    {
        $results["status"] = "Active";
        $results["service_status"] = "Active";
        $results["license_status"] = "Active";
        $results["lastRemoteChecked"] = date("Y-m-d");
        $results["remoteChecked"] = true;
        return $results;
    }
    private function getDomain()
    {
        if (defined("WHMCS_LICENSE_DOMAIN") && 0 < strlen(WHMCS_LICENSE_DOMAIN)) {
            return WHMCS_LICENSE_DOMAIN;
        }
        $domain = $_SERVER["SERVER_NAME"];
        $configName = sprintf("%s-%s-%s-%s", "RSTheme", $this->templateName, "license", "domain");
        if (0 < strlen($domain)) {
            (new \RSThemes\Models\Configuration())->saveConfig($configName, $domain);
            return $domain;
        }
        $domain = (new \RSThemes\Models\Configuration())->getConfig($configName);
        if (0 < strlen($domain)) {
            return $domain;
        }
        $domain = (new \RSThemes\Models\Configuration())->getConfig("Domain");
        if (0 < strlen($domain)) {
            $domain = parse_url($domain);
            if (isset($domain["host"]) && strlen($domain["host"])) {
                (new \RSThemes\Models\Configuration())->saveConfig($configName, $domain["host"]);
                return $domain["host"];
            }
        }
        $domain = (new \RSThemes\Models\Configuration())->getConfig("SystemURL");
        if (0 < strlen($domain)) {
            $domain = parse_url($domain);
            if (isset($domain["host"]) && strlen($domain["host"])) {
                (new \RSThemes\Models\Configuration())->saveConfig($configName, $domain["host"]);
                return $domain["host"];
            }
        }
        return "";
    }
    private function getIp()
    {
        if (defined("WHMCS_LICENSE_IP") && 0 < strlen(WHMCS_LICENSE_IP)) {
            return WHMCS_LICENSE_IP;
        }
        $installIp = "";
        if (isset($_SERVER["SERVER_ADDR"])) {
            $installIp = $_SERVER["SERVER_ADDR"];
        } else {
            if (isset($_SERVER["LOCAL_ADDR"])) {
                $installIp = $_SERVER["LOCAL_ADDR"];
            } else {
                if (function_exists("gethostname") && function_exists("gethostbyname")) {
                    $installIp = gethostbyname(gethostname());
                }
            }
        }
        if (0 < strlen($installIp)) {
            return $installIp;
        }
        $usersip = isset($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_ADDR"] : $_SERVER["LOCAL_ADDR"];
        if (0 < strlen($usersip)) {
            return $usersip;
        }
        return gethostbyname(gethostname());
    }
    private function getDirPath()
    {
        if (defined("WHMCS_LICENSE_DIR") && 0 < strlen(WHMCS_LICENSE_DIR)) {
            return WHMCS_LICENSE_DIR;
        }
        return str_replace("\\modules\\addons\\RSThemes\\src\\Template", "", str_replace("/modules/addons/RSThemes/src/Template", "", realpath(dirname(__FILE_2080e8a4_2e665c70__))));
    }
}

?>