<?php
/**
 * Script to help fix upload size issues in Bidda School Management System
 * This script provides diagnostics and recommendations for fixing the 413 Request Entity Too Large error
 */

// Check current PHP settings
$upload_max_filesize = ini_get('upload_max_filesize');
$post_max_size = ini_get('post_max_size');
$max_execution_time = ini_get('max_execution_time');
$max_input_time = ini_get('max_input_time');
$memory_limit = ini_get('memory_limit');

// Convert to bytes for comparison
function return_bytes($val) {
    $val = trim($val);
    $last = strtolower($val[strlen($val)-1]);
    $val = (int) $val;
    switch($last) {
        case 'g': $val *= 1024;
        case 'm': $val *= 1024;
        case 'k': $val *= 1024;
    }
    return $val;
}

$upload_max_bytes = return_bytes($upload_max_filesize);
$post_max_bytes = return_bytes($post_max_size);

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bidda Upload Size Fixer</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5">
        <div class="row">
            <div class="col-md-10 mx-auto">
                <div class="card shadow">
                    <div class="card-header bg-primary text-white">
                        <h3 class="mb-0"><i class="fas fa-cogs me-2"></i>Bidda Upload Size Fixer</h3>
                    </div>
                    <div class="card-body">
                        <h4><i class="fas fa-info-circle text-info me-2"></i>Current Server Configuration</h4>
                        
                        <div class="row mb-4">
                            <div class="col-md-6">
                                <div class="card bg-light">
                                    <div class="card-body">
                                        <h5 class="card-title"><i class="fas fa-server me-2 text-primary"></i>PHP Settings</h5>
                                        <ul class="list-group list-group-flush">
                                            <li class="list-group-item d-flex justify-content-between align-items-center">
                                                upload_max_filesize
                                                <span class="badge bg-primary"><?php echo $upload_max_filesize; ?></span>
                                            </li>
                                            <li class="list-group-item d-flex justify-content-between align-items-center">
                                                post_max_size
                                                <span class="badge bg-primary"><?php echo $post_max_size; ?></span>
                                            </li>
                                            <li class="list-group-item d-flex justify-content-between align-items-center">
                                                memory_limit
                                                <span class="badge bg-primary"><?php echo $memory_limit; ?></span>
                                            </li>
                                            <li class="list-group-item d-flex justify-content-between align-items-center">
                                                max_execution_time
                                                <span class="badge bg-primary"><?php echo $max_execution_time; ?>s</span>
                                            </li>
                                            <li class="list-group-item d-flex justify-content-between align-items-center">
                                                max_input_time
                                                <span class="badge bg-primary"><?php echo $max_input_time; ?>s</span>
                                            </li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                            
                            <div class="col-md-6">
                                <div class="card bg-light">
                                    <div class="card-body">
                                        <h5 class="card-title"><i class="fas fa-exclamation-triangle me-2 text-warning"></i>Issue Analysis</h5>
                                        <?php if ($upload_max_bytes < 5 * 1024 * 1024 || $post_max_bytes < 5 * 1024 * 1024): ?>
                                            <div class="alert alert-warning">
                                                <i class="fas fa-exclamation-circle"></i> 
                                                <strong>PHP Settings Low:</strong> Your upload limits are less than 5MB
                                            </div>
                                        <?php else: ?>
                                            <div class="alert alert-success">
                                                <i class="fas fa-check-circle"></i> 
                                                <strong>PHP Settings OK:</strong> Upload limits are sufficient
                                            </div>
                                        <?php endif; ?>
                                        
                                        <div class="alert alert-info">
                                            <i class="fas fa-info-circle"></i> 
                                            <strong>Note:</strong> The 413 error is usually from nginx, not PHP.
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <h4><i class="fas fa-wrench text-success me-2"></i>How to Fix the 413 Error</h4>
                        
                        <div class="alert alert-success">
                            <h5><i class="fas fa-server me-2"></i>Step 1: Configure Nginx</h5>
                            <p>To fix the "413 Request Entity Too Large" error, you need to modify your nginx configuration:</p>
                            <ol>
                                <li>Locate your nginx configuration file (usually at <code>C:\MAMP\conf\nginx\nginx.conf</code>)</li>
                                <li>Find the <code>http</code> block (typically around line 20-30)</li>
                                <li>Add this line inside the <code>http</code> block:<br>
                                    <code class="bg-dark text-white p-1 rounded">client_max_body_size 10M;</code></li>
                                <li>Save the file and restart your MAMP servers</li>
                            </ol>
                        </div>
                        
                        <div class="alert alert-info">
                            <h5><i class="fas fa-code me-2"></i>Step 2: Verify PHP Settings</h5>
                            <p>Make sure your PHP settings in <code>php.ini</code> are also sufficient:</p>
                            <pre class="bg-dark text-white p-2 rounded">upload_max_filesize = 10M
post_max_size = 10M
max_execution_time = 300
max_input_time = 300
memory_limit = 256M</pre>
                        </div>
                        
                        <div class="alert alert-warning">
                            <h5><i class="fas fa-camera me-2"></i>Step 3: Image Optimization</h5>
                            <p>Your admission application form now includes client-side image compression to reduce file sizes before upload:</p>
                            <ul>
                                <li>Images are automatically compressed to 80% quality</li>
                                <li>Large images are resized to max 1024x1024 pixels</li>
                                <li>Reduces the chance of hitting upload limits</li>
                            </ul>
                        </div>
                        
                        <h4><i class="fas fa-image text-info me-2"></i>Image Upload Best Practices</h4>
                        
                        <div class="row">
                            <div class="col-md-6">
                                <div class="card">
                                    <div class="card-body">
                                        <h5 class="card-title"><i class="fas fa-compress-arrows-alt me-2 text-success"></i>Optimization Tips</h5>
                                        <ul class="list-unstyled">
                                            <li><i class="fas fa-check text-success me-2"></i>Resize images before upload</li>
                                            <li><i class="fas fa-check text-success me-2"></i>Use JPG format for photos</li>
                                            <li><i class="fas fa-check text-success me-2"></i>Compress images to reduce size</li>
                                            <li><i class="fas fa-check text-success me-2"></i>Limit resolution to 1024x1024</li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="card">
                                    <div class="card-body">
                                        <h5 class="card-title"><i class="fas fa-file-upload me-2 text-primary"></i>Size Recommendations</h5>
                                        <ul class="list-unstyled">
                                            <li><i class="fas fa-ruler-combined me-2 text-info"></i>Profile photos: Under 200KB</li>
                                            <li><i class="fas fa-ruler-combined me-2 text-info"></i>Documents: Under 1MB</li>
                                            <li><i class="fas fa-ruler-combined me-2 text-info"></i>Photos: Under 500KB</li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="text-center mt-4">
                            <a href="admission_application.php" class="btn btn-primary me-2">
                                <i class="fas fa-user-plus me-1"></i>Admission Application
                            </a>
                            <a href="admission_receipt.php?application_no=TEST123456" class="btn btn-info me-2">
                                <i class="fas fa-receipt me-1"></i>Test Receipt
                            </a>
                            <a href="./" class="btn btn-secondary">
                                <i class="fas fa-home me-1"></i>Home
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>