HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/ai.naniguide.com/app/Helpers/LicenseHelper.php
<?php

namespace App\Helpers;

use App\Model\Setting;
use Carbon\Carbon;
use Exception;
use GuzzleHttp\Client;
use App\Library\License;

class LicenseHelper
{
    // license type
    public const TYPE_REGULAR = 'regular';
    public const TYPE_EXTENDED = 'extended';

    public const STATUS_VALID = 'valid';
    public const STATUS_EXPIRED = 'expired';

    /**
     * Get license information
     *
     * [
     *    "id" => 20656,
     *    "item_number" => [EVT ITEM ID GOES HERE],
     *    "purchase_code" => "test-only",
     *    "purchase_date" => [some date],
     *    "buyer" => [buyer name],
     *    "licence" => "Regular License",
     *    "supported_until" => "Mon Oct 10 2033 02:39:47 GMT+0000",
     *    "created_at" => "Tue Oct 10 2023 02:17:44 GMT+0000",
     *    "status" => "active",
     *  ]
     *
     *
     */
    public static function getLicense($license)
    {
        $client = new Client(['verify' => false]);

        try {
            // Throw an exception if failed
            if (config('custom.japan')) {
                $endpoint = join_url(config('custom.license_verification_endpoint'), '/wp-json/automailjp/v1/license/check?license=' . $license);
                $response = $client->request('get', $endpoint);
            } else {
                // $endpoint = config('custom.license_verification_endpoint');
                $endpoint = 'https://verify.acellemail.com'; // Backward compatibility

                $response = $client->request(
                    'post',
                    $endpoint,
                    [
                        'headers' => [
                            'User-Agent' => md5($license),
                        ],
                        'form_params' => [
                            'purchase-code' => $license,
                            'item-id' => '17796082', // @todo hard-coded here
                            'secret' => session('secret'),
                            'version' => app_version(),
                        ],
                        'verify' => false
                    ]
                );
            }

            $responseBody = $response->getBody();
            $responseJson = json_decode($responseBody, true);
            return $responseJson;
        } catch (\GuzzleHttp\Exception\ClientException $ex) { // 400 error
            $response = $ex->getResponse();

            if (config('custom.japan')) {
                throw new Exception(trans('messages.license.error.invalid').': '.$license);
            } else {
                throw new Exception('Invalid license: '.$response->getBody()->getContents());
            }
        }
    }

    public static function updateLicense($licenseCode)
    {
        $license = self::getLicense($licenseCode);
        Setting::set('license', $license['purchase_code']);
        Setting::set('license_type', $license['licence']);
        Setting::set('license_status', $license['status']);
        Setting::set('license_supported_until', $license['supported_until']);
        Setting::set('license_buyer', $license['buyer']);
    }

    public static function removeLicense()
    {
        Setting::set('license', '');
        Setting::set('license_type', '');
        Setting::set('license_status', '');
        Setting::set('license_supported_until', '');
        Setting::set('license_buyer', '');
    }

    public static function getCurrentLicense()
    {
        if (empty(Setting::get('license'))) {
            return null;
        }

        return new license(
            Setting::get('license'),
            Setting::get('license_type'),
            Setting::get('license_status'),
            Setting::get('license_supported_until'),
            Setting::get('license_buyer'),
        );
    }

    public static function hasActiveLicense()
    {
        $license = self::getCurrentLicense();

        if (is_null($license)) {
            return false;
        }

        return $license->isActive();
    }

    public static function refreshLicense()
    {
        $license = self::getCurrentLicense();

        if ($license) {
            self::updateLicense($license->getLicenseNumber());
        }
    }
}