<?php
/**
 * SMTP管理界面
 * 提供WordPress后台的SMTP配置界面
 */

// 防止直接访问
if (!defined('ABSPATH')) {
    exit;
}

// 包含诊断工具
require_once __DIR__ . '/smtp-diagnostic.php';

/**
 * 添加SMTP设置菜单
 */
function nenghui_add_smtp_menu() {
    add_options_page(
        'SMTP邮件设置',
        'SMTP设置',
        'manage_options',
        'nenghui-smtp-settings',
        'nenghui_smtp_settings_page'
    );
}
add_action('admin_menu', 'nenghui_add_smtp_menu');

/**
 * 注册SMTP设置选项
 */
function nenghui_register_smtp_settings() {
    // 注册SMTP设置选项
    register_setting('nenghui_smtp_settings', 'nenghui_smtp_host');
    register_setting('nenghui_smtp_settings', 'nenghui_smtp_port');
    register_setting('nenghui_smtp_settings', 'nenghui_smtp_username');
    register_setting('nenghui_smtp_settings', 'nenghui_smtp_password');
    register_setting('nenghui_smtp_settings', 'nenghui_smtp_secure');
    register_setting('nenghui_smtp_settings', 'nenghui_smtp_from_email');
    register_setting('nenghui_smtp_settings', 'nenghui_smtp_from_name');
}
add_action('admin_init', 'nenghui_register_smtp_settings');

/**
 * SMTP设置页面
 */
function nenghui_smtp_settings_page() {
    // 处理表单提交
    if (isset($_POST['submit'])) {
        // 验证nonce
        if (!wp_verify_nonce($_POST['_wpnonce'], 'nenghui_smtp_settings-options')) {
            wp_die('安全验证失败');
        }
        
        // 保存设置
        update_option('nenghui_smtp_host', sanitize_text_field($_POST['nenghui_smtp_host']));
        update_option('nenghui_smtp_port', intval($_POST['nenghui_smtp_port']));
        update_option('nenghui_smtp_username', sanitize_text_field($_POST['nenghui_smtp_username']));
        update_option('nenghui_smtp_password', sanitize_text_field($_POST['nenghui_smtp_password']));
        update_option('nenghui_smtp_secure', sanitize_text_field($_POST['nenghui_smtp_secure']));
        update_option('nenghui_smtp_from_email', sanitize_email($_POST['nenghui_smtp_from_email']));
        update_option('nenghui_smtp_from_name', sanitize_text_field($_POST['nenghui_smtp_from_name']));
        
        echo '<div class="notice notice-success is-dismissible"><p>设置已保存！</p></div>';
    }
    
    // 获取当前设置
    $smtp_host = get_option('nenghui_smtp_host', '');
    $smtp_port = get_option('nenghui_smtp_port', '587');
    $smtp_username = get_option('nenghui_smtp_username', '');
    $smtp_password = get_option('nenghui_smtp_password', '');
    $smtp_secure = get_option('nenghui_smtp_secure', 'tls');
    $smtp_from_email = get_option('nenghui_smtp_from_email', get_option('admin_email'));
    $smtp_from_name = get_option('nenghui_smtp_from_name', get_bloginfo('name'));
    
    ?>
    <div class="wrap">
        <h1>SMTP邮件设置</h1>
        
        <div class="nenghui-smtp-admin">
            <form method="post" action="">
                <?php wp_nonce_field('nenghui_smtp_settings-options'); ?>
                
                <h2>SMTP服务器设置</h2>
                <p class="description">配置SMTP服务器以确保邮件能够正常发送。如果不配置SMTP，将使用服务器默认的邮件发送方式。</p>
                
                <table class="form-table">
                    <tbody>
                        <tr>
                            <th scope="row">SMTP主机</th>
                            <td>
                                <input type="text" name="nenghui_smtp_host" value="<?php echo esc_attr($smtp_host); ?>" class="regular-text" placeholder="smtp.gmail.com">
                                <p class="description">SMTP服务器地址，例如：smtp.gmail.com, smtp.qq.com</p>
                            </td>
                        </tr>
                        
                        <tr>
                            <th scope="row">SMTP端口</th>
                            <td>
                                <input type="number" name="nenghui_smtp_port" value="<?php echo esc_attr($smtp_port); ?>" class="small-text" id="smtp_port_input">
                                <p class="description">通常为 587 (TLS) 或 465 (SSL)</p>
                                <div id="port_25_warning" style="display:none; background:#fff3cd; border:1px solid #ffeaa7; padding:10px; margin-top:5px; border-radius:4px;">
                                    <strong style="color:#856404;">⚠️ 端口25警告：</strong><br>
                                    大多数云服务商（阿里云、腾讯云等）默认封禁端口25，建议使用：<br>
                                    • 端口587 + TLS加密（推荐）<br>
                                    • 端口465 + SSL加密
                                </div>
                                <script>
                                document.getElementById('smtp_port_input').addEventListener('input', function() {
                                    var warning = document.getElementById('port_25_warning');
                                    if (this.value == '25') {
                                        warning.style.display = 'block';
                                    } else {
                                        warning.style.display = 'none';
                                    }
                                });
                                // 页面加载时检查
                                if (document.getElementById('smtp_port_input').value == '25') {
                                    document.getElementById('port_25_warning').style.display = 'block';
                                }
                                </script>
                            </td>
                        </tr>
                        
                        <tr>
                            <th scope="row">加密方式</th>
                            <td>
                                <select name="nenghui_smtp_secure">
                                    <option value="tls" <?php selected($smtp_secure, 'tls'); ?>>TLS</option>
                                    <option value="ssl" <?php selected($smtp_secure, 'ssl'); ?>>SSL</option>
                                    <option value="none" <?php selected($smtp_secure, 'none'); ?>>无加密</option>
                                </select>
                                <p class="description">推荐使用 TLS 或 SSL</p>
                            </td>
                        </tr>
                        
                        <tr>
                            <th scope="row">SMTP用户名</th>
                            <td>
                                <input type="text" name="nenghui_smtp_username" value="<?php echo esc_attr($smtp_username); ?>" class="regular-text" placeholder="your-email@gmail.com">
                                <p class="description">通常是您的邮箱地址</p>
                            </td>
                        </tr>
                        
                        <tr>
                            <th scope="row">SMTP密码</th>
                            <td>
                                <input type="password" name="nenghui_smtp_password" value="<?php echo esc_attr($smtp_password); ?>" class="regular-text">
                                <p class="description"><strong style="color:red;">重要提示：</strong>大多数邮箱服务需要使用<strong>授权码</strong>而不是登录密码！<br>
                                • 阿里云邮箱：必须使用授权码<br>
                                • QQ邮箱：需要开启SMTP并使用授权码<br>
                                • Gmail：需要使用应用专用密码</p>
                            </td>
                        </tr>
                        
                        <tr>
                            <th scope="row">发件人邮箱</th>
                            <td>
                                <input type="email" name="nenghui_smtp_from_email" value="<?php echo esc_attr($smtp_from_email); ?>" class="regular-text">
                                <p class="description">邮件的发件人邮箱地址</p>
                            </td>
                        </tr>
                        
                        <tr>
                            <th scope="row">发件人姓名</th>
                            <td>
                                <input type="text" name="nenghui_smtp_from_name" value="<?php echo esc_attr($smtp_from_name); ?>" class="regular-text">
                                <p class="description">邮件的发件人显示名称</p>
                            </td>
                        </tr>
                    </tbody>
                </table>
                
                <?php submit_button('保存设置'); ?>
            </form>
            
            <div class="nenghui-smtp-test">
                <h2>测试SMTP连接</h2>
                <p>点击下面的按钮发送测试邮件，验证SMTP配置是否正确。</p>
                <p><strong>测试邮件将发送到：</strong><?php echo esc_html(get_option('admin_email')); ?></p>
                <p>
                    <a href="<?php echo admin_url('options-general.php?page=nenghui-smtp-settings&nenghui_smtp_diagnostic=1'); ?>" class="button button-primary">系统诊断</a>
                    <a href="<?php echo admin_url('options-general.php?page=nenghui-smtp-settings&nenghui_test_smtp_connection=1'); ?>" class="button button-secondary" style="margin-left: 10px;">测试连接</a>
                    <a href="<?php echo admin_url('options-general.php?page=nenghui-smtp-settings&nenghui_test_smtp=1'); ?>" class="button button-secondary" style="margin-left: 10px;">发送测试邮件</a>
                </p>
                <p class="description">
                    <strong>系统诊断：</strong>检查服务器环境和PHP扩展支持<br>
                    <strong>测试连接：</strong>验证能否连接到SMTP服务器<br>
                    <strong>发送测试邮件：</strong>实际发送一封测试邮件
                </p>
                
                <div class="smtp-test-tips" style="margin-top: 15px; padding: 10px; background: #fff3cd; border: 1px solid #ffeaa7; border-radius: 4px;">
                    <h4 style="margin-top: 0;">测试前请确认：</h4>
                    <ul style="margin: 5px 0;">
                        <li>所有SMTP配置项都已正确填写</li>
                        <li>邮箱服务商已开启SMTP服务</li>
                        <li>使用的是正确的密码（Gmail等需要应用专用密码）</li>
                        <li>服务器网络可以访问SMTP服务器</li>
                    </ul>
                </div>
            </div>
            
            <div class="nenghui-smtp-help">
                <h2>常用SMTP配置</h2>
                <div class="smtp-providers">
                    <div class="provider">
                        <h3>Gmail</h3>
                        <ul>
                            <li>主机: smtp.gmail.com</li>
                            <li>端口: 587</li>
                            <li>加密: TLS</li>
                            <li>注意: 需要开启两步验证并使用应用专用密码</li>
                        </ul>
                    </div>
                    
                    <div class="provider">
                        <h3>QQ邮箱</h3>
                        <ul>
                            <li>主机: smtp.qq.com</li>
                            <li>端口: 587 或 465</li>
                            <li>加密: TLS 或 SSL</li>
                            <li>注意: 需要开启SMTP服务并获取授权码</li>
                        </ul>
                    </div>
                    
                    <div class="provider">
                        <h3>163邮箱</h3>
                        <ul>
                            <li>主机: smtp.163.com</li>
                            <li>端口: 587 或 465</li>
                            <li>加密: TLS 或 SSL</li>
                            <li>注意: 需要开启SMTP服务并使用授权码</li>
                        </ul>
                    </div>
                    
                    <div class="provider">
                        <h3>阿里云邮箱</h3>
                        <ul>
                            <li>主机: smtp.mxhichina.com</li>
                            <li>端口: 587 或 465</li>
                            <li>加密: TLS 或 SSL</li>
                            <li style="color:red;"><strong>重要: 必须使用授权码，不能使用登录密码！</strong></li>
                            <li>获取授权码: 登录阿里云邮箱 → 设置 → 账户安全 → 生成授权码</li>
                        </ul>
                    </div>
                    
                    <div class="provider">
                        <h3>hMailServer 自建邮箱</h3>
                        <ul>
                            <li>主机: 您的邮件服务器域名或IP</li>
                            <li>端口: 25 (无加密) / 587 (TLS) / 465 (SSL)</li>
                            <li>加密: 根据服务器配置选择 None/TLS/SSL</li>
                            <li>用户名: 完整邮箱地址或用户名</li>
                            <li>密码: 邮箱账户密码</li>
                            <li style="color:blue;"><strong>提示:</strong> 确保hMailServer已启用SMTP服务</li>
                            <li>常见端口: 25(标准), 587(提交), 465(SSL)</li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <style>
    .nenghui-smtp-admin .smtp-providers {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
        gap: 20px;
        margin-top: 15px;
    }
    
    .nenghui-smtp-admin .provider {
        background: #f9f9f9;
        padding: 15px;
        border-radius: 5px;
        border-left: 4px solid #0073aa;
    }
    
    .nenghui-smtp-admin .provider h3 {
        margin-top: 0;
        color: #0073aa;
    }
    
    .nenghui-smtp-admin .provider ul {
        margin: 10px 0;
        padding-left: 20px;
    }
    
    .nenghui-smtp-admin .provider li {
        margin: 5px 0;
        font-size: 13px;
    }
    
    .nenghui-smtp-test {
        background: #fff;
        border: 1px solid #ccd0d4;
        border-radius: 4px;
        padding: 20px;
        margin: 20px 0;
    }
    
    .nenghui-smtp-help {
        background: #fff;
        border: 1px solid #ccd0d4;
        border-radius: 4px;
        padding: 20px;
        margin: 20px 0;
    }
    </style>
    <?php
}

?>