<?php
/**
 * PHP Settings Test File
 * Upload this file and visit it in your browser to check if PHP settings are correct
 */

header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html>
<head>
    <title>PHP Settings Test - YouTube MP3 Downloader</title>
    <style>
        /* Disable text selection */
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 50px auto;
            padding: 20px;
            background: #f5f5f5;
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
        }
        
        /* Allow text selection in specific elements */
        code, pre, .info {
            -webkit-user-select: text;
            -moz-user-select: text;
            -ms-user-select: text;
            user-select: text;
        }
        
        /* Disable image dragging */
        img {
            -webkit-user-drag: none;
            -khtml-user-drag: none;
            -moz-user-drag: none;
            -o-user-drag: none;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            border-bottom: 3px solid #667eea;
            padding-bottom: 10px;
        }
        .setting {
            padding: 15px;
            margin: 10px 0;
            border-radius: 5px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .required {
            background: #fff3cd;
            border-left: 4px solid #ffc107;
        }
        .recommended {
            background: #d1ecf1;
            border-left: 4px solid #17a2b8;
        }
        .ok {
            background: #d4edda;
            border-left: 4px solid #28a745;
        }
        .error {
            background: #f8d7da;
            border-left: 4px solid #dc3545;
        }
        .status {
            font-weight: bold;
            padding: 5px 15px;
            border-radius: 20px;
            font-size: 0.9em;
        }
        .status-ok {
            background: #28a745;
            color: white;
        }
        .status-error {
            background: #dc3545;
            color: white;
        }
        .status-warning {
            background: #ffc107;
            color: #333;
        }
        .info {
            margin-top: 20px;
            padding: 15px;
            background: #e7f3ff;
            border-radius: 5px;
            border-left: 4px solid #0066cc;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔧 PHP Settings Test</h1>
        <p>This page checks if your PHP settings are configured correctly for the YouTube MP3 Downloader.</p>
        
        <?php
        // Required settings
        $allow_url_fopen = ini_get('allow_url_fopen');
        // Check properly (can be "1", "On", "0", "Off", or false)
        $allow_url_fopen_ok = ($allow_url_fopen === '1' || $allow_url_fopen === 'On' || $allow_url_fopen === 'on' || $allow_url_fopen === true);
        
        // Also test if it actually works
        $testUrl = 'https://www.google.com/favicon.ico';
        $testResult = @file_get_contents($testUrl, false, stream_context_create([
            'http' => ['timeout' => 5]
        ]));
        $actualWorks = ($testResult !== false);
        
        // Use actual test result if ini_get is misleading
        if (!$allow_url_fopen_ok && $actualWorks) {
            $allow_url_fopen_ok = true;
            $allow_url_fopen = 'On (works despite ini_get)';
        }
        
        // Recommended settings
        $max_execution_time = ini_get('max_execution_time');
        $memory_limit = ini_get('memory_limit');
        $upload_max_filesize = ini_get('upload_max_filesize');
        $post_max_size = ini_get('post_max_size');
        
        // Check if values are adequate
        $max_execution_time_ok = (int)$max_execution_time >= 300;
        $memory_limit_bytes = return_bytes($memory_limit);
        $memory_limit_ok = $memory_limit_bytes >= (128 * 1024 * 1024); // At least 128MB
        
        // Helper function to convert memory limit to bytes
        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;
        }
        ?>
        
        <!-- Required Setting -->
        <div class="setting required">
            <div>
                <strong>allow_url_fopen</strong> (REQUIRED)<br>
                <small>Allows file_get_contents() to open URLs</small>
            </div>
            <span class="status <?php echo $allow_url_fopen_ok ? 'status-ok' : 'status-error'; ?>">
                <?php echo $allow_url_fopen_ok ? '✓ Enabled' : '✗ Disabled'; ?>
            </span>
        </div>
        
        <!-- Recommended Settings -->
        <div class="setting <?php echo $max_execution_time_ok ? 'ok' : 'recommended'; ?>">
            <div>
                <strong>max_execution_time</strong> (Recommended: 300)<br>
                <small>Maximum time a script can run</small>
            </div>
            <span class="status <?php echo $max_execution_time_ok ? 'status-ok' : 'status-warning'; ?>">
                <?php echo $max_execution_time; ?>s
            </span>
        </div>
        
        <div class="setting <?php echo $memory_limit_ok ? 'ok' : 'recommended'; ?>">
            <div>
                <strong>memory_limit</strong> (Recommended: 256M)<br>
                <small>Maximum memory a script can use</small>
            </div>
            <span class="status <?php echo $memory_limit_ok ? 'status-ok' : 'status-warning'; ?>">
                <?php echo $memory_limit; ?>
            </span>
        </div>
        
        <div class="setting recommended">
            <div>
                <strong>upload_max_filesize</strong><br>
                <small>Maximum upload file size</small>
            </div>
            <span class="status status-ok">
                <?php echo $upload_max_filesize; ?>
            </span>
        </div>
        
        <div class="setting recommended">
            <div>
                <strong>post_max_size</strong><br>
                <small>Maximum POST data size</small>
            </div>
            <span class="status status-ok">
                <?php echo $post_max_size; ?>
            </span>
        </div>
        
        <!-- Overall Status -->
        <div class="info">
            <h3>📋 Overall Status</h3>
            <?php if ($allow_url_fopen_ok): ?>
                <p><strong style="color: #28a745;">✓ Your PHP settings look good!</strong> The YouTube MP3 Downloader should work on your server.</p>
                <?php if ($actualWorks && ini_get('allow_url_fopen') !== '1' && ini_get('allow_url_fopen') !== 'On'): ?>
                    <p><small style="color: #666;">Note: <code>ini_get()</code> shows it as disabled, but it actually works. This is fine!</small></p>
                <?php endif; ?>
            <?php else: ?>
                <p><strong style="color: #dc3545;">✗ Critical Issue:</strong> <code>allow_url_fopen</code> appears to be disabled. This is required for the downloader to work.</p>
                <p><strong>How to fix:</strong></p>
                <ul>
                    <li>Upload <code>.htaccess</code> or <code>.user.ini</code> file to your website root</li>
                    <li>In cPanel, go to "MultiPHP INI Editor" and enable <code>allow_url_fopen</code></li>
                    <li>Contact your hosting provider to enable <code>allow_url_fopen</code></li>
                    <li>See <code>SETUP.md</code> for detailed instructions</li>
                </ul>
                <?php if (!$actualWorks): ?>
                    <p><strong style="color: #dc3545;">Test Result:</strong> Actual test to download from URL also failed. The setting is definitely disabled.</p>
                <?php endif; ?>
            <?php endif; ?>
            
            <?php if (!$max_execution_time_ok || !$memory_limit_ok): ?>
                <p><strong style="color: #ffc107;">⚠ Warning:</strong> Some settings are below recommended values. Large downloads might fail.</p>
            <?php endif; ?>
        </div>
        
        <!-- Additional Info -->
        <div class="info" style="margin-top: 20px;">
            <h3>ℹ️ Additional Information</h3>
            <p><strong>PHP Version:</strong> <?php echo phpversion(); ?></p>
            <p><strong>Server Software:</strong> <?php echo $_SERVER['SERVER_SOFTWARE'] ?? 'Unknown'; ?></p>
            <p><strong>Document Root:</strong> <?php echo $_SERVER['DOCUMENT_ROOT'] ?? 'Unknown'; ?></p>
        </div>
    </div>
    
    <script>
        // Disable right-click context menu
        document.addEventListener('contextmenu', function(e) {
            e.preventDefault();
            return false;
        });
        
        // Disable common keyboard shortcuts
        document.addEventListener('keydown', function(e) {
            // Disable F12 (Developer Tools)
            if (e.key === 'F12') {
                e.preventDefault();
                return false;
            }
            
            // Disable Ctrl+Shift+I (Developer Tools)
            if (e.ctrlKey && e.shiftKey && e.key === 'I') {
                e.preventDefault();
                return false;
            }
            
            // Disable Ctrl+Shift+J (Console)
            if (e.ctrlKey && e.shiftKey && e.key === 'J') {
                e.preventDefault();
                return false;
            }
            
            // Disable Ctrl+U (View Source)
            if (e.ctrlKey && e.key === 'u') {
                e.preventDefault();
                return false;
            }
            
            // Disable Ctrl+S (Save Page)
            if (e.ctrlKey && e.key === 's') {
                e.preventDefault();
                return false;
            }
            
            // Disable Ctrl+Shift+C (Inspect Element)
            if (e.ctrlKey && e.shiftKey && e.key === 'C') {
                e.preventDefault();
                return false;
            }
        });
        
        // Disable text selection (double-click, drag)
        document.addEventListener('selectstart', function(e) {
            // Allow selection in code blocks and info boxes
            if (e.target.tagName === 'CODE' || e.target.tagName === 'PRE' || e.target.closest('.info')) {
                return true;
            }
            e.preventDefault();
            return false;
        });
        
        // Disable drag and drop
        document.addEventListener('dragstart', function(e) {
            e.preventDefault();
            return false;
        });
        
        // Clear console on page load
        console.clear();
    </script>
</body>
</html>
