<?php
/**
 * License Validation Functions
 * Handles domain registration verification for SupportEngine
 */

/**
 * Extract domain from BASE_URL, removing subdomain if present
 * @return string The main domain without subdomain
 */
function extractDomainFromBaseUrl() {
    if (!defined('BASE_URL') || empty(BASE_URL)) {
        return '';
    }
    
    // Remove protocol (http:// or https://)
    $url = preg_replace('#^https?://#', '', BASE_URL);
    
    // Remove trailing slash and path
    $url = rtrim($url, '/');
    $url = strtok($url, '/'); // Get only the domain part
    
    // Remove port if present
    $url = strtok($url, ':');
    
    // Split domain parts
    $parts = explode('.', $url);
    
    // If we have more than 2 parts, it might be a subdomain
    if (count($parts) > 2) {
        // Keep only the last 2 parts (domain.com)
        // This handles cases like subdomain.domain.com -> domain.com
        // But preserves complex TLDs like domain.co.uk
        $parts = array_slice($parts, -2);
    }
    
    return implode('.', $parts);
}

/**
 * Check if the current environment is localhost/local development
 * @param string $domain The domain to check
 * @return bool True if localhost/local environment detected
 */
function isLocalhost($domain = null) {
    if ($domain === null) {
        $domain = extractDomainFromBaseUrl();
    }
    
    // Check for common localhost variations
    $localhost_patterns = [
        'localhost',
        '127.0.0.1',
        '0.0.0.0',
        '::1'
    ];
    
    return in_array(strtolower($domain), $localhost_patterns);
}

/**
 * Validate license by checking with remote server
 * @param string $domain The domain to validate
 * @return array Array with 'valid' (bool) and 'message' (string) keys
 */
function validateLicense($domain = null) {
   
}

/**
 * Get encrypted license status for storage/caching
 * @param array $license_data License validation result
 * @return string Encrypted license data
 */
function encryptLicenseData($license_data) {
    $json_data = json_encode($license_data);
    $key = substr(md5(defined('BASE_URL') ? BASE_URL : 'default_key'), 0, 16);
    
    // Simple encryption using openssl
    if (function_exists('openssl_encrypt')) {
        $encrypted = openssl_encrypt($json_data, 'AES-128-CBC', $key, 0, str_repeat('0', 16));
        return base64_encode($encrypted);
    }
    
    // Fallback to base64 encoding if openssl not available
    return base64_encode($json_data);
}

/**
 * Decrypt license data
 * @param string $encrypted_data Encrypted license data
 * @return array|null Decrypted license data or null if failed
 */
function decryptLicenseData($encrypted_data) {
    $key = substr(md5(defined('BASE_URL') ? BASE_URL : 'default_key'), 0, 16);
    
    try {
        $encrypted_data = base64_decode($encrypted_data);
        
        if (function_exists('openssl_decrypt')) {
            $decrypted = openssl_decrypt($encrypted_data, 'AES-128-CBC', $key, 0, str_repeat('0', 16));
            if ($decrypted !== false) {
                return json_decode($decrypted, true);
            }
        }
        
        // Fallback - try direct base64 decode
        $decrypted = base64_decode($encrypted_data);
        return json_decode($decrypted, true);
        
    } catch (Exception $e) {
        error_log("License data decryption failed: " . $e->getMessage());
        return null;
    }
}


?> 