<?php
/**
 * PHP Version Compatibility Check
 * 
 * This file enforces minimum PHP version requirements for security and compatibility.
 * Include this at the top of your main files to ensure proper PHP version.
 */

// Define minimum required PHP version for security
define('MIN_PHP_VERSION', '8.1.0');
define('RECOMMENDED_PHP_VERSION', '8.3.0');

/**
 * Check if current PHP version meets minimum requirements
 * 
 * @return bool True if PHP version is compatible
 */
function checkPhpVersion() {
    return version_compare(PHP_VERSION, MIN_PHP_VERSION, '>=');
}

/**
 * Get PHP version status information
 * 
 * @return array Status information about PHP version
 */
function getPhpVersionStatus() {
    $current = PHP_VERSION;
    $isCompatible = checkPhpVersion();
    $isRecommended = version_compare($current, RECOMMENDED_PHP_VERSION, '>=');
    $isSecure = version_compare($current, '8.1.0', '>='); // PHP 8.0 and below are EOL
    
    return [
        'current' => $current,
        'minimum' => MIN_PHP_VERSION,
        'recommended' => RECOMMENDED_PHP_VERSION,
        'is_compatible' => $isCompatible,
        'is_recommended' => $isRecommended,
        'is_secure' => $isSecure,
        'status' => $isCompatible ? ($isRecommended ? 'optimal' : ($isSecure ? 'good' : 'deprecated')) : 'incompatible'
    ];
}

/**
 * Display PHP version warning if needed
 */
function displayPhpVersionWarning() {
    $status = getPhpVersionStatus();
    
    if (!$status['is_compatible']) {
        // Critical error - incompatible version
        echo '<div class="alert alert-danger" role="alert">';
        echo '<h4><i class="bi bi-exclamation-triangle"></i> Critical: PHP Version Too Old</h4>';
        echo '<p><strong>Current PHP Version:</strong> ' . htmlspecialchars($status['current']) . '</p>';
        echo '<p><strong>Minimum Required:</strong> ' . htmlspecialchars($status['minimum']) . '</p>';
        echo '<p>Your PHP version is not supported and may have security vulnerabilities. Please upgrade immediately.</p>';
        echo '</div>';
    } elseif (!$status['is_secure']) {
        // Security warning - EOL version
        echo '<div class="alert alert-warning" role="alert">';
        echo '<h4><i class="bi bi-shield-alt"></i> Security Warning: PHP Version End-of-Life</h4>';
        echo '<p><strong>Current PHP Version:</strong> ' . htmlspecialchars($status['current']) . '</p>';
        echo '<p><strong>Recommended:</strong> ' . htmlspecialchars($status['recommended']) . '</p>';
        echo '<p>Your PHP version no longer receives security updates. Consider upgrading for better security.</p>';
        echo '</div>';
    } elseif (!$status['is_recommended']) {
        // Info - could be better
        echo '<div class="alert alert-info" role="alert">';
        echo '<h4><i class="bi bi-info-circle"></i> PHP Version Notice</h4>';
        echo '<p><strong>Current PHP Version:</strong> ' . htmlspecialchars($status['current']) . '</p>';
        echo '<p><strong>Recommended:</strong> ' . htmlspecialchars($status['recommended']) . '</p>';
        echo '<p>Consider upgrading to the latest PHP version for optimal performance and latest features.</p>';
        echo '</div>';
    }
}

// Auto-check on include (only if not in CLI mode)
if (php_sapi_name() !== 'cli' && !checkPhpVersion()) {
    // For critical incompatibility, we can choose to halt execution
    if (version_compare(PHP_VERSION, '8.0.0', '<')) {
        header('HTTP/1.1 503 Service Unavailable');
        echo '<!DOCTYPE html><html><head><title>PHP Version Error</title>';
        echo '<style>body{font-family:Arial,sans-serif;margin:40px;} .error{background:#f8d7da;color:#721c24;padding:20px;border:1px solid #f5c6cb;border-radius:5px;}</style>';
        echo '</head><body>';
        echo '<div class="error">';
        echo '<h1>PHP Version Too Old</h1>';
        echo '<p><strong>Current:</strong> ' . htmlspecialchars(PHP_VERSION) . '</p>';
        echo '<p><strong>Required:</strong> ' . htmlspecialchars(MIN_PHP_VERSION) . '</p>';
        echo '<p>This application requires PHP 8.1 or newer for security reasons. Please contact your hosting provider to upgrade PHP.</p>';
        echo '</div></body></html>';
        exit;
    }
}

/**
 * Check if specific PHP features are available
 * 
 * @return array Feature availability status
 */
function checkPhpFeatures() {
    return [
        'openssl' => extension_loaded('openssl'),
        'mysqli' => extension_loaded('mysqli'),
        'curl' => extension_loaded('curl'),
        'gd' => extension_loaded('gd'),
        'mbstring' => extension_loaded('mbstring'),
        'intl' => extension_loaded('intl'),
        'fileinfo' => extension_loaded('fileinfo'),
        'session' => extension_loaded('session'),
        'filter' => extension_loaded('filter'),
        'hash' => extension_loaded('hash'),
        'json' => extension_loaded('json'),
        'sodium' => extension_loaded('sodium'), // PHP 8.1+ recommended for enhanced security
    ];
}

/**
 * Get security recommendations based on PHP version
 * 
 * @return array Security recommendations
 */
function getSecurityRecommendations() {
    $recommendations = [];
    
    if (version_compare(PHP_VERSION, '8.1.0', '<')) {
        $recommendations[] = 'Upgrade to PHP 8.1+ for active security support';
    }
    
    if (version_compare(PHP_VERSION, '8.2.0', '<')) {
        $recommendations[] = 'Consider PHP 8.2+ for enhanced readonly properties and security features';
    }
    
    if (version_compare(PHP_VERSION, '8.3.0', '<')) {
        $recommendations[] = 'PHP 8.3+ offers the latest security patches and performance improvements';
    }
    
    $features = checkPhpFeatures();
    if (!$features['sodium']) {
        $recommendations[] = 'Enable Sodium extension for modern cryptographic functions';
    }
    
    if (!$features['openssl']) {
        $recommendations[] = 'Enable OpenSSL extension for secure communications';
    }
    
    return $recommendations;
} 