<?php
/**
 * Encryption Functions
 * Handles encryption and decryption of sensitive data
 */

// Encryption key - CHANGE THIS TO A RANDOM STRING!
if (!defined('ENCRYPTION_KEY')) {
    define('ENCRYPTION_KEY', 'SpotifyPanel2025SecretKey!@#$%');
}

/**
 * Encrypt data
 * 
 * @param string $data Data to encrypt
 * @return string Encrypted data (base64 encoded)
 */
function encryptData($data) {
    if (empty($data)) {
        return '';
    }
    
    $key = hash('sha256', ENCRYPTION_KEY);
    $iv = substr(hash('sha256', ENCRYPTION_KEY), 0, 16);
    
    $encrypted = openssl_encrypt($data, 'AES-256-CBC', $key, 0, $iv);
    
    return base64_encode($encrypted);
}

/**
 * Decrypt data
 * 
 * @param string $data Encrypted data (base64 encoded)
 * @return string Decrypted data
 */
function decryptData($data) {
    if (empty($data)) {
        return '';
    }
    
    $key = hash('sha256', ENCRYPTION_KEY);
    $iv = substr(hash('sha256', ENCRYPTION_KEY), 0, 16);
    
    $decrypted = openssl_decrypt(base64_decode($data), 'AES-256-CBC', $key, 0, $iv);
    
    return $decrypted;
}

/**
 * Update config.php file with new values
 * 
 * @param array $settings Settings to update
 * @return bool Success status
 */
function updateConfigFile($settings) {
    $configFile = BASE_PATH . '/config/config.php';
    
    if (!file_exists($configFile)) {
        return false;
    }
    
    // Read current config
    $content = file_get_contents($configFile);
    
    // Update each setting
    foreach ($settings as $key => $value) {
        // Encrypt sensitive data
        if (in_array($key, ['TELEGRAM_BOT_TOKEN', 'TELEGRAM_CHAT_ID'])) {
            $value = encryptData($value);
        }
        
        // Escape special characters
        $value = addslashes($value);
        
        // Update the define statement
        $pattern = "/define\s*\(\s*['\"]" . preg_quote($key, '/') . "['\"]\s*,\s*['\"].*?['\"]\s*\)/";
        $replacement = "define('{$key}', '{$value}')";
        
        $content = preg_replace($pattern, $replacement, $content);
    }
    
    // Make file writable temporarily
    @chmod($configFile, 0666);
    
    // Write updated content
    $result = file_put_contents($configFile, $content, LOCK_EX);
    
    // Restore permissions
    @chmod($configFile, 0644);
    
    return $result !== false;
}

/**
 * Get decrypted config value
 * 
 * @param string $key Config key
 * @return string Decrypted value
 */
function getConfigValue($key) {
    if (!defined($key)) {
        return '';
    }
    
    $value = constant($key);
    
    // Decrypt if it's a sensitive value
    if (in_array($key, ['TELEGRAM_BOT_TOKEN', 'TELEGRAM_CHAT_ID'])) {
        // Check if it's already encrypted (base64)
        if (base64_encode(base64_decode($value, true)) === $value) {
            return decryptData($value);
        }
    }
    
    return $value;
}

/**
 * Check if value is encrypted
 * 
 * @param string $value Value to check
 * @return bool True if encrypted
 */
function isEncrypted($value) {
    // Check if it's valid base64
    if (base64_encode(base64_decode($value, true)) === $value) {
        // Try to decrypt
        $decrypted = decryptData($value);
        return !empty($decrypted);
    }
    
    return false;
}
?>
