<?php
/**
 * Script to help locate nginx configuration files
 */
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nginx Config Finder - Bidda School Management System</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-success text-white">
                        <h3 class="mb-0"><i class="fas fa-search me-2"></i>Find Nginx Configuration Files</h3>
                    </div>
                    <div class="card-body">
                        <h5><i class="fas fa-info-circle me-2 text-info"></i>Server Information:</h5>
                        <div class="row mb-4">
                            <div class="col-md-6">
                                <ul class="list-group">
                                    <li class="list-group-item d-flex justify-content-between align-items-center">
                                        <span><i class="fas fa-server me-2 text-primary"></i>Server Software</span>
                                        <span class="badge bg-primary"><?php echo $_SERVER['SERVER_SOFTWARE'] ?? 'Unknown'; ?></span>
                                    </li>
                                    <li class="list-group-item d-flex justify-content-between align-items-center">
                                        <span><i class="fas fa-code me-2 text-success"></i>PHP Version</span>
                                        <span class="badge bg-success"><?php echo PHP_VERSION; ?></span>
                                    </li>
                                </ul>
                            </div>
                            <div class="col-md-6">
                                <ul class="list-group">
                                    <li class="list-group-item d-flex justify-content-between align-items-center">
                                        <span><i class="fas fa-database me-2 text-warning"></i>Operating System</span>
                                        <span class="badge bg-warning"><?php echo PHP_OS; ?></span>
                                    </li>
                                    <li class="list-group-item d-flex justify-content-between align-items-center">
                                        <span><i class="fas fa-plug me-2 text-info"></i>SAPI</span>
                                        <span class="badge bg-info"><?php echo PHP_SAPI; ?></span>
                                    </li>
                                </ul>
                            </div>
                        </div>

                        <h5><i class="fas fa-folder-open me-2 text-info"></i>Potential Nginx Configuration Locations:</h5>
                        <div class="mb-4">
                            <p>The following locations are commonly used for nginx configuration in MAMP installations:</p>
                            
                            <?php
                            $potentialPaths = [
                                'C:/MAMP/conf/nginx/nginx.conf',
                                'C:/MAMP/conf/nginx/',
                                'C:/MAMP64/conf/nginx/nginx.conf',
                                'C:/MAMP64/conf/nginx/',
                                'C:/Program Files/MAMP/conf/nginx/nginx.conf',
                                'C:/Program Files (x86)/MAMP/conf/nginx/nginx.conf'
                            ];
                            
                            $foundConfigs = [];
                            foreach ($potentialPaths as $path) {
                                if (file_exists(dirname($path))) {
                                    if (basename($path) === 'nginx.conf') {
                                        if (file_exists($path)) {
                                            $foundConfigs[] = $path;
                                        }
                                    } else {
                                        // Directory exists, check for nginx.conf inside
                                        $configPath = $path . '/nginx.conf';
                                        if (file_exists($configPath)) {
                                            $foundConfigs[] = $configPath;
                                        }
                                    }
                                }
                            }
                            ?>
                            
                            <ul class="list-group">
                                <?php foreach ($potentialPaths as $path): ?>
                                    <?php 
                                    $exists = file_exists($path) || (dirname($path) !== '.' && file_exists(dirname($path)));
                                    $isConfig = basename($path) === 'nginx.conf' && file_exists($path);
                                    $icon = $exists ? 'fa-check-circle text-success' : 'fa-times-circle text-danger';
                                    $status = $exists ? 'Exists' : 'Not Found';
                                    $badgeClass = $exists ? 'bg-success' : 'bg-secondary';
                                    if ($isConfig) $badgeClass = 'bg-primary';
                                    ?>
                                    <li class="list-group-item d-flex justify-content-between align-items-center">
                                        <div>
                                            <i class="fas <?php echo $icon; ?> me-2"></i>
                                            <code><?php echo $path; ?></code>
                                            <?php if ($isConfig): ?>
                                                <span class="badge bg-primary ms-2">Main Config</span>
                                            <?php endif; ?>
                                        </div>
                                        <span class="badge <?php echo $badgeClass; ?>"><?php echo $status; ?></span>
                                    </li>
                                <?php endforeach; ?>
                            </ul>
                        </div>

                        <?php if (!empty($foundConfigs)): ?>
                            <div class="alert alert-success">
                                <h6><i class="fas fa-check-circle me-2"></i>Configuration Files Found:</h6>
                                <ul>
                                    <?php foreach ($foundConfigs as $config): ?>
                                        <li><code><?php echo $config; ?></code></li>
                                    <?php endforeach; ?>
                                </ul>
                            </div>
                        <?php else: ?>
                            <div class="alert alert-warning">
                                <h6><i class="fas fa-exclamation-triangle me-2"></i>No Configuration Files Found:</h6>
                                <p>None of the common paths contain nginx configuration files. You may need to locate them manually.</p>
                            </div>
                        <?php endif; ?>

                        <h5><i class="fas fa-wrench me-2 text-info"></i>How to Fix the 413 Error:</h5>
                        <div class="alert alert-light border">
                            <ol>
                                <li>
                                    <strong>Locate your nginx.conf file</strong> - Look in the paths listed above or search for "nginx.conf" in your MAMP installation directory.
                                </li>
                                <li>
                                    <strong>Edit the file</strong> - Open it with a text editor as Administrator.
                                </li>
                                <li>
                                    <strong>Add the configuration</strong> - Find the <code>http</code> block and add:
                                    <pre class="bg-dark text-white p-2 rounded"><code>client_max_body_size 50M;</code></pre>
                                </li>
                                <li>
                                    <strong>Save and restart</strong> - Save the file and restart your MAMP servers.
                                </li>
                            </ol>
                        </div>

                        <h5><i class="fas fa-question-circle me-2 text-info"></i>Troubleshooting Tips:</h5>
                        <div class="alert alert-info">
                            <ul>
                                <li>If you still encounter issues after changing the nginx config, check that the file permissions allow writing</li>
                                <li>Make sure to restart both Apache and Nginx services in MAMP after making changes</li>
                                <li>Look for include directives in nginx.conf that might point to other config files</li>
                                <li>The setting must be placed in the correct context (http, server, or location block)</li>
                            </ul>
                        </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>