<?php
/**
 * Script to disable authentication in all admin files
 * تعطيل تسجيل الدخول من جميع ملفات الأدمن
 */

$adminDir = 'backend/admin';
$filesModified = 0;
$errors = [];

// Patterns to search and replace
$patterns = [
    // Pattern 1: Standard auth check
    [
        'search' => '///// Check admin access/s*/n/s*if /(!isset/(/$_SESSION/[/'user_id\'/]/) /|/| /$_SESSION/[/'role\'/] !== /'admin\'/) /{/s*/n/s*header/(/'Location: (\.\.\/)?login\.php\'/);/s*/n/s*exit;/s*/n/s*/}/s',
        'replace' => '// Check admin access - DISABLED
// if (!isset($_SESSION[\'user_id\']) || $_SESSION[\'role\'] !== \'admin\') {
//     header(\'Location: $1login.php\');
//     exit;
// }'
    ],
    // Pattern 2: Without comment
    [
        'search' => '/if /(!isset/(/$_SESSION/[/'user_id\'/]/) /|/| /$_SESSION/[/'role\'/] !== /'admin\'/) /{/s*/n/s*header/(/'Location: (\.\.\/)?login\.php\'/);/s*/n/s*exit;/s*/n/s*/}/s',
        'replace' => '// Authentication check - DISABLED
// if (!isset($_SESSION[\'user_id\']) || $_SESSION[\'role\'] !== \'admin\') {
//     header(\'Location: $1login.php\');
//     exit;
// }'
    ],
    // Pattern 3: With admin_id
    [
        'search' => '/if /(!isset/(/$_SESSION/[/'admin_id\'/]/)/) /{/s*/n/s*header/(/'Location: (\.\.\/)?login\.php\'/);/s*/n/s*exit;/s*/n/s*/}/s',
        'replace' => '// Authentication check - DISABLED
// if (!isset($_SESSION[\'admin_id\'])) {
//     header(\'Location: $1login.php\');
//     exit;
// }'
    ],
    // Pattern 4: With role check
    [
        'search' => '/if /(!isset/(/$_SESSION/[/'user_id\'/]/) /|/| !isset/(/$_SESSION/[/'role\'/]/) /|/| /$_SESSION/[/'role\'/] !== /'admin\'/) /{/s*/n/s*header/(/'Location: (\.\.\/)?login\.php\'/);/s*/n/s*exit;/s*/n/s*/}/s',
        'replace' => '// Authentication check - DISABLED
// if (!isset($_SESSION[\'user_id\']) || !isset($_SESSION[\'role\']) || $_SESSION[\'role\'] !== \'admin\') {
//     header(\'Location: $1login.php\');
//     exit;
// }'
    ]
];

function processDirectory($dir, &$filesModified, &$errors, $patterns) {
    $files = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($dir),
        RecursiveIteratorIterator::SELF_FIRST
    );
    
    foreach ($files as $file) {
        if ($file->isFile() && $file->getExtension() === 'php') {
            $filepath = $file->getPathname();
            $content = file_get_contents($filepath);
            $originalContent = $content;
            
            // Apply all patterns
            foreach ($patterns as $pattern) {
                $content = preg_replace($pattern['search'], $pattern['replace'], $content);
            }
            
            // If content changed, save it
            if ($content !== $originalContent) {
                if (file_put_contents($filepath, $content)) {
                    $filesModified++;
                    echo "✅ Modified: $filepath/n";
                } else {
                    $errors[] = "Failed to write: $filepath";
                    echo "❌ Failed: $filepath/n";
                }
            }
        }
    }
}

echo "🚀 Starting to disable authentication in admin files.../n/n";

try {
    processDirectory($adminDir, $filesModified, $errors, $patterns);
    
    echo "/n" . str_repeat('=', 50) . "/n";
    echo "✅ Process completed!/n";
    echo "📊 Files modified: $filesModified/n";
    
    if (!empty($errors)) {
        echo "❌ Errors: " . count($errors) . "/n";
        foreach ($errors as $error) {
            echo "   - $error/n";
        }
    }
    
} catch (Exception $e) {
    echo "❌ Error: " . $e->getMessage() . "/n";
}

echo "/n💡 Note: You may need to manually check some files for complex authentication patterns./n";
?>
