<?php
/**
 * 文档推荐
 *
 * @version        $id:recommend.php$
 * @package        DedeX.Site
 * @license        GNU GPL v2 (/license.txt)
 */
require_once(dirname(__FILE__)."/../system/common.inc.php");
require_once(DEDEINC."/channelunit.class.php");
if (!isset($action)) $action = '';
unset($_FILES);
if (isset($arcID)) $aid = $arcID;
$arcID = $aid = (isset($aid) && is_numeric($aid) ? $aid : 0);
$type = (!isset($type) ? "" : $type);
if (empty($aid)) {
    ShowMsg("文档ID不能为空", "-1");
    exit();
}
//读取文档信息
if ($action == '') {
    //读取文档信息
    $arcRow = GetOneArchive($aid);
    if ($arcRow['aid'] == '') {
        ShowMsg("无法把未知文档推荐给好友", "-1");
        exit();
    }
    extract($arcRow, EXTR_OVERWRITE);
}
//发送推荐信息
//发送推荐信息
else if ($action == 'send') {
    if (!CheckEmail($email)) {
        ShowMsg("邮箱格式不正确", -1);
        exit();
    }
    // 获取推荐文章信息
    $arcRow = GetOneArchive($aid);
    if (empty($arcRow['aid'])) {
        ShowMsg("无法推荐不存在的文章", -1);
        exit();
    }
    $title = htmlspecialchars($arcRow['title']);
    $arcurl = $arcRow['arcurl'];
    $description = htmlspecialchars(cn_substrR(strip_tags($arcRow['description'] ?: ''), 200));
    $pubdate = MyDate('Y-m-d', $arcRow['pubdate']);
    $writer = htmlspecialchars($arcRow['writer'] ?: '管理员');

    $msg = RemoveXSS(dede_htmlspecialchars($msg));
    $mailtitle = "{$cfg_webname} - 好友推荐文章：{$title}";

    // 构建 HTML 邮件模板（无 Emoji，现代风格）
    $mailbody = <<<EOF
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>好友推荐文章</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            background-color: #f0f2f5;
            font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
            font-size: 16px;
            line-height: 1.5;
            color: #1e293b;
        }
        .email-container {
            max-width: 600px;
            margin: 24px auto;
            background: #ffffff;
            border-radius: 20px;
            overflow: hidden;
            box-shadow: 0 8px 20px rgba(0,0,0,0.05);
        }
        .email-header {
            background: linear-gradient(135deg, #1e3c72, #2a5298);
            padding: 32px 28px;
            text-align: center;
            color: white;
        }
        .email-header h1 {
            margin: 0 0 8px;
            font-size: 28px;
            font-weight: 600;
        }
        .email-header p {
            margin: 0;
            opacity: 0.85;
            font-size: 15px;
        }
        .email-body {
            padding: 32px 28px;
        }
        .greeting {
            font-size: 18px;
            font-weight: 500;
            margin-bottom: 20px;
        }
        .info-card {
            background: #f8fafc;
            border-radius: 16px;
            padding: 20px;
            margin-bottom: 28px;
            border: 1px solid #e9ecef;
        }
        .info-row {
            display: flex;
            margin-bottom: 12px;
        }
        .info-row:last-child {
            margin-bottom: 0;
        }
        .info-label {
            width: 75px;
            font-weight: 600;
            color: #2a5298;
        }
        .info-value {
            flex: 1;
            color: #1e293b;
            word-break: break-word;
        }
        .message-box {
            background: #edf2f7;
            border-radius: 16px;
            padding: 18px 22px;
            border-left: 4px solid #2a5298;
            margin-bottom: 28px;
            font-size: 15px;
            line-height: 1.6;
            color: #1e293b;
        }
        .btn {
            display: inline-block;
            background: linear-gradient(135deg, #1e3c72, #2a5298);
            color: white !important;
            padding: 12px 28px;
            border-radius: 40px;
            text-decoration: none;
            font-weight: 600;
            font-size: 15px;
            text-align: center;
            margin: 12px 0 6px;
        }
        .text-muted {
            color: #6c757d;
            font-size: 13px;
        }
        .divider {
            height: 1px;
            background: #e9ecef;
            margin: 24px 0;
        }
        .email-footer {
            background: #f9fafb;
            padding: 20px 28px;
            text-align: center;
            font-size: 12px;
            color: #6c757d;
            border-top: 1px solid #e9ecef;
        }
        @media (max-width: 640px) {
            .email-body, .email-header, .email-footer { padding-left: 20px; padding-right: 20px; }
            .info-row { flex-direction: column; }
            .info-label { margin-bottom: 6px; }
            .btn { display: block; }
        }
    </style>
</head>
<body>
    <div class="email-container">
        <div class="email-header">
            <h1>好友推荐文章</h1>
            <p>您的朋友在 {$cfg_webname} 上发现了精彩内容</p>
        </div>
        <div class="email-body">
            <div class="greeting">您好，</div>
            <p class="text-muted" style="margin-top: -8px; margin-bottom: 20px;">您的朋友 <strong>{$writer}</strong> 给您推荐了一篇文章，以下是详情：</p>
            <div class="info-card">
                <div class="info-row"><span class="info-label">文章标题</span><span class="info-value"><strong>{$title}</strong></span></div>
                <div class="info-row"><span class="info-label">发布时间</span><span class="info-value">{$pubdate}</span></div>
                <div class="info-row"><span class="info-label">文章摘要</span><span class="info-value">{$description}</span></div>
            </div>
            <div class="message-box">
                <div style="font-weight: 600; margin-bottom: 10px;">好友留言：</div>
                {$msg}
            </div>
            <div style="text-align: center;">
                <a href="{$arcurl}" class="btn">阅读完整文章 →</a>
                <p class="text-muted" style="margin-top: 16px;">或复制链接到浏览器：<br><span style="color:#2a5298;">{$arcurl}</span></p>
            </div>
            <div class="divider"></div>
            <p class="text-muted" style="text-align: center;">如果您无法点击按钮，请将上方链接复制到浏览器中打开。</p>
        </div>
        <div class="email-footer">
            <p>此邮件由 <strong>{$cfg_webname}</strong> 系统自动发送，请勿直接回复。</p>
            <p>© 2026 {$cfg_webname} 保留所有权利。</p>
        </div>
    </div>
</body>
</html>
EOF;

    // 发送邮件
    $headers = "From: {$cfg_adminemail}\r\nReply-To: {$cfg_adminemail}";
    if ($cfg_sendmail_bysmtp == 'Y' && !empty($cfg_smtp_server)) {
        $mailtype = 'HTML';
        require_once(DEDEINC.'/libraries/mail.class.php');
        $smtp = new smtp($cfg_smtp_server, $cfg_smtp_port, true, $cfg_smtp_user, $cfg_smtp_password);
        $smtp->debug = false;
        $smtp->sendmail($email, $cfg_webname, $cfg_smtp_usermail, $mailtitle, $mailbody, $mailtype);
    } else {
        $headers = "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=utf-8\r\n";
        $headers .= "From: {$cfg_adminemail}\r\nReply-To: {$cfg_adminemail}";
        @mail($email, '=?UTF-8?B?' . base64_encode($mailtitle) . '?=', $mailbody, $headers);
    }
    ShowMsg("成功推荐文章给好友", $arcurl);
    exit();
}
$arcRow = GetOneArchive($aid);
if ($arcRow['aid'] == '') {
    ShowMsg("无法把未知文档推荐给好友", "-1");
    exit();
}
extract($arcRow, EXTR_OVERWRITE);
//显示模板简单PHP文件
include(DEDETEMPLATE.'/apps/recommend.htm');
?>