<?php
/**
 * Script to install PHPMailer for the StoreWeb application
 */

echo "<h2>PHPMailer Installation Script</h2>";

// Check if PHPMailer is already installed
if (file_exists('includes/PHPMailer/src/PHPMailer.php')) {
    echo "<p style='color: green;'>PHPMailer is already installed!</p>";
    echo "<p><a href='test_email.php'>Test Email Functionality</a></p>";
    exit;
}

// Check if allow_url_fopen is enabled
if (!ini_get('allow_url_fopen')) {
    echo "<p style='color: red; font-weight: bold;'>allow_url_fopen is disabled. Cannot download files automatically.</p>";
    echo "<h3>Manual Installation Instructions:</h3>";
    echo "<ol>";
    echo "<li>Download PHPMailer from <a href='https://github.com/PHPMailer/PHPMailer'>https://github.com/PHPMailer/PHPMailer</a></li>";
    echo "<li>Click on the 'Clone or download' button and select 'Download ZIP'</li>";
    echo "<li>Extract the ZIP file</li>";
    echo "<li>Copy the 'src' folder contents to <code>includes/PHPMailer/src/</code></li>";
    echo "<li>The required files are:";
    echo "<ul>";
    echo "<li><code>PHPMailer.php</code></li>";
    echo "<li><code>SMTP.php</code></li>";
    echo "<li><code>Exception.php</code></li>";
    echo "</ul>";
    echo "</li>";
    echo "<li>Then <a href='test_email.php'>test email functionality</a></li>";
    echo "</ol>";
    echo "<p><a href='index.php'>Back to Home</a></p>";
    exit;
}

// Try to download PHPMailer
echo "<p>Downloading PHPMailer...</p>";

// Create directory structure
if (!file_exists('includes/PHPMailer')) {
    mkdir('includes/PHPMailer', 0755, true);
}

if (!file_exists('includes/PHPMailer/src')) {
    mkdir('includes/PHPMailer/src', 0755, true);
}

// URLs for PHPMailer files
$files = [
    'https://raw.githubusercontent.com/PHPMailer/PHPMailer/master/src/PHPMailer.php' => 'includes/PHPMailer/src/PHPMailer.php',
    'https://raw.githubusercontent.com/PHPMailer/PHPMailer/master/src/SMTP.php' => 'includes/PHPMailer/src/SMTP.php',
    'https://raw.githubusercontent.com/PHPMailer/PHPMailer/master/src/Exception.php' => 'includes/PHPMailer/src/Exception.php'
];

$success = true;

foreach ($files as $url => $destination) {
    echo "<p>Downloading " . basename($destination) . "...</p>";
    
    // Download file
    $content = @file_get_contents($url);
    if ($content === false) {
        echo "<p style='color: red;'>Failed to download " . basename($destination) . "</p>";
        $success = false;
    } else {
        if (file_put_contents($destination, $content)) {
            echo "<p style='color: green;'>Successfully downloaded " . basename($destination) . "</p>";
        } else {
            echo "<p style='color: red;'>Failed to save " . basename($destination) . "</p>";
            $success = false;
        }
    }
}

if ($success) {
    echo "<p style='color: green; font-weight: bold;'>PHPMailer installation completed successfully!</p>";
    echo "<p>Now you can test email functionality with improved SMTP support.</p>";
    echo "<p><a href='test_email.php'>Test Email Functionality</a></p>";
} else {
    echo "<p style='color: red; font-weight: bold;'>PHPMailer installation failed.</p>";
    echo "<h3>Manual Installation Instructions:</h3>";
    echo "<ol>";
    echo "<li>Download PHPMailer from <a href='https://github.com/PHPMailer/PHPMailer'>https://github.com/PHPMailer/PHPMailer</a></li>";
    echo "<li>Click on the 'Clone or download' button and select 'Download ZIP'</li>";
    echo "<li>Extract the ZIP file</li>";
    echo "<li>Copy the 'src' folder contents to <code>includes/PHPMailer/src/</code></li>";
    echo "<li>The required files are:";
    echo "<ul>";
    echo "<li><code>PHPMailer.php</code></li>";
    echo "<li><code>SMTP.php</code></li>";
    echo "<li><code>Exception.php</code></li>";
    echo "</ul>";
    echo "</li>";
    echo "<li>Then <a href='test_email.php'>test email functionality</a></li>";
    echo "</ol>";
}

echo "<p><a href='index.php'>Back to Home</a></p>";
?>