<?
//
// 接收引用通告的程序
//
define("_GNUBOARD_", TRUE);

include_once("./_common.php");
// 记录错误 write_log() 
include_once("$g4[path]/lib/etc.lib.php");

function tb_xml_msg($error, $msg="")
{
    global $g4;

    $s = "";
    $s .= "<?xml version=\"1.0\" encoding=\"$g4[charset]\"?>\n";
    $s .= "<response>\n";
    $s .= "<error>$error</error>\n";
    $s .= "<message>$msg</message>\n";
    $s .= "</response>\n";

    return $s;
}


$arr = explode("/", $_SERVER[PATH_INFO]);
            // 删除不是英文，数字及下划线的字节 (最多 20字节) 
$bo_table = preg_replace("/\W/", "", substr($arr[1],0,20));
            // 正数转换 
$wr_id = (int)$arr[2];
            // 删除不是英文小写，数字字节 (最多32字节) 
$to_token = preg_replace("/[^a-z0-9]/", "", substr($arr[3],0,32));

$write_table   = $g4[write_prefix] . $bo_table; // 论坛所有名称

$sql = " select wr_id, ca_name, wr_email from $write_table where wr_id = '$wr_id' ";
$wr = sql_fetch($sql, FALSE);

// wr_id错误时
if (!$wr[wr_id] || !($_POST[title] && $_POST[excerpt] && $_POST[url] && $_POST[blog_name])) 
{
    $tmp_dir = str_replace("/tb.php", "", $_SERVER[SCRIPT_NAME]);
    header("location:$tmp_dir/board.php?bo_table=$bo_table&wr_id=$wr_id");
    exit;
}


if (!$to_token)
{
    if (isset($_POST)) 
        write_log("$g4[path]/data/log/tb.log", $_POST);

    echo tb_xml_msg(1, "引用地址接收失败");
    exit;
}

$sql = " select to_token from $g4[token_table] where to_token = '$to_token' ";
$row = sql_fetch($sql);
if ($row[to_token] && $to_token) 
{
    // 禁止使用2次
    sql_query(" delete from $g4[token_table] where to_token = '$to_token' ");

    // 删除超过3个小时的引用
    if (isset($g4['token_time']) == false)
        $g4['token_time'] = 3; 

    $sql = " delete from $g4[token_table] where to_datetime < '".date("Y-m-d", $g4[server_time] - 3600 * $g4['token_time'])."' ";
    sql_query($sql);
}
else
{
    echo tb_xml_msg(1, "引用通告地址无效！(也有可能超过了有效时间)");
    exit;
}

$title   = $_POST[title];
$excerpt = $_POST[excerpt];

if (strlen($title) > 255)   $title   = cut_str($title, 255);
if (strlen($excerpt) > 255) $excerpt = cut_str($excerpt, 255);

$msg = "";
// 防止写入两次
if ($_POST[title]) 
{
    $board = sql_fetch(" select bo_subject, bo_use_trackback from $g4[board_table] where bo_table = '$bo_table' ");
    if (!$board[bo_use_trackback]) 
        $msg = "没有开启引用通告功能！";

    if (!$msg) 
    {
        $next_num = get_next_num($write_table);

        $sql = " select max(wr_comment) as max_comment from $write_table where wr_parent = '$wr_id' and wr_is_comment = 1 ";
        $row = sql_fetch($sql);
        $row[max_comment] += 1;

        $sql = " insert into $g4[write_prefix]$bo_table
                    set wr_num = '$next_num',
                        wr_parent = '$wr_id',
                        wr_is_comment = '1',
                        wr_comment = '$row[max_comment]',
                        wr_content = '$title\n$excerpt',
                        wr_trackback = '$_POST[url]',
                        wr_name = '$_POST[blog_name]',
                        wr_password = '".sql_password($g4[server_time])."',
                        wr_datetime = '$g4[time_ymdhis]', 
                        wr_ip = '$_SERVER[REMOTE_ADDR]' ";
        $result = sql_query($sql, FALSE);
        if ($result) 
        {
            $comment_id = mysql_insert_id();
            sql_query(" update $write_table set wr_comment = wr_comment + 1 where wr_id = '$wr_id' ", FALSE);
            sql_query(" insert into $g4[board_new_table] ( bo_table, wr_id, wr_parent, bn_datetime ) values ( '$bo_table', '$comment_id', '$wr_id', '$g4[time_ymdhis]' ) ");
            sql_query(" update $g4[board_table] set bo_count_comment = bo_count_comment + 1 where bo_table = '$bo_table' ", FALSE);
        } else 
            $msg = "$write_table TABLE INSERT 错误";
    }

    //write_log("$g4[path]/data/log/aaa", $msg);

    if ($msg) // 非正常
    { 
        echo tb_xml_msg(1, $msg);
        exit;
    } 
    else // 正常
    { 
        // 发送邮件
        if ($config[cf_email_use] && $board[bo_use_email])
        {
            include_once("$g4[path]/lib/mailer.lib.php");

            // 获取管理信息
            $super_admin = get_admin("super");
            $group_admin = get_admin("group");
            $board_admin = get_admin("board");

            $wr_name    = $blog_name = get_text(stripslashes($_POST[blog_name]));
            $wr_subject = $title = get_text(stripslashes($title));
            $wr_content = $excerpt = nl2br(get_text(stripslashes($excerpt)));

            $link_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

            $subject = "'{$board[bo_subject]}' 论坛新增了引用通告内容";

            define("_GNUBOARD_", TRUE);
            ob_start();
            include_once ("./write_update_mail.php");
            $content = ob_get_contents();
            ob_end_clean();

            // 邮件通知-版主
            if ($config[cf_email_wr_board_admin])
                mailer($blog_name, "", $board_admin[mb_email], $subject, $content, 1);

            // 邮件通知-版块管理员
            if ($group_admin[mb_email] != $board_admin[mb_email])
            {
                if ($config[cf_email_wr_group_admin])
                    mailer($blog_name, "", $group_admin[mb_email], $subject, $content, 1);
            }
            
            // 邮件通知-网站管理员
            if ($super_admin[mb_email] != $board_admin[mb_email])
            {
                if ($config[cf_email_wr_super_admin])
                    mailer($blog_name, "", $super_admin[mb_email], $subject, $content, 1);
            }

            // 回复邮件
            //if ($wr[wr_recv_email] && $wr[wr_email] && $wr[wr_email] != $admin[mb_email]) 
            if (strstr($wr[wr_option], 'mail') && $wr[wr_email] && $wr[wr_email] != $admin[mb_email]) 
            {
                if ($config[cf_email_wr_write])
                    mailer($blog_name, "", $wr[wr_email], $subject, $content, 1);

                // 通知所有回复作者
                if ($config[cf_email_wr_comment_all])
                {
                    $sql = " select wr_email from $write_table
                              where wr_email not in ( '$admin[mb_email]' , '$wr[wr_email]', '' )
                                and wr_parent = '$wr_id'
                              group by wr_email ";
                    $result = sql_query($sql);
                    while ($row=sql_fetch_array($result)) 
                        mailer($blog_name, "", $row[wr_email], $subject, $content, 1);
                }
            }
        }
    }
}

echo tb_xml_msg(0, "");
?>
