//检查失效时间
if ($options['trackback_life'] && $timestamp - intval($carr[1]) > 3600 * 24) {
    showxml('已经超过本文允许Trackback的时间');
}
$article = $DB->fetch_one_array("SELECT dateline,closetrackback FROM {$db_prefix}articles WHERE articleid='{$articleid}'");
if (!$article) {
    showxml('文章不存在');
} elseif ($article['closetrackback']) {
    showxml('本文此时不允许引用');
} elseif ($article['dateline'] != intval($carr[1])) {
    showxml('文章时间验证失败');
}
$url = sax_addslashes(trim($_POST['url']));
if ($url) {
    $title = sax_addslashes(html_excerpt($_POST['title']));
    $excerpt = sax_addslashes(trimmed_title(html_excerpt($_POST['excerpt'])), 200);
    $blog_name = sax_addslashes(html_excerpt($_POST['blog_name']));
}
if (!$title || !$excerpt || !$url || !$blog_name) {
    showxml('参数不正确');
} elseif (substr($url, 0, 7) != 'http://') {
    showxml('参数不正确');
}
// 检查Spam
// 定义发送来的此条Trackback初始分数
$point = 0;
$options['tb_spam_level'] = in_array($options['tb_spam_level'], array('strong', 'weak', 'never')) ? $options['tb_spam_level'] : 'weak';
if ($options['audit_trackback']) {
    //如果人工审核
    $visible = '0';
} elseif ($options['tb_spam_level'] != 'never') {
function newcomments_recache()
{
    global $DB, $db_prefix, $options;
    $query = $DB->query("SELECT * FROM {$db_prefix}settings WHERE title IN ('recentcomment_num','recentcomment_limit')");
    $set = array();
    while ($r = $DB->fetch_array($query)) {
        $set[$r['title']] = $r['value'];
    }
    unset($r);
    $article_comment_num = (int) $options['article_comment_num'];
    $commentdb = array();
    if ($set['recentcomment_num']) {
        $newcomments = $DB->query("SELECT c.commentid, c.articleid, c.author, c.email, c.url, c.dateline, c.content, a.title, a.alias FROM {$db_prefix}comments c LEFT JOIN {$db_prefix}articles a ON (a.articleid=c.articleid) WHERE a.readpassword = '' AND a.visible='1' AND c.visible='1' ORDER BY commentid DESC LIMIT " . intval($set['recentcomment_num']));
        $i = 0;
        while ($newcomment = $DB->fetch_array($newcomments)) {
            $newcomment['avatardb'] = get_avatar($newcomment['email'], 32);
            $newcomment['content'] = preg_replace("/\\[quote=(.*?)\\]\\s*(.+?)\\s*\\[\\/quote\\]/is", "", $newcomment['content']);
            if (empty($newcomment['content'])) {
                $newcomment['content'] = '......';
            }
            //处理链接
            if ($article_comment_num) {
                $cpost = $DB->result($DB->query("SELECT COUNT(commentid) FROM {$db_prefix}comments WHERE articleid='" . $newcomment['articleid'] . "' AND visible='1' AND commentid<='" . $newcomment['commentid'] . "'"), 0);
                if ($cpost / $article_comment_num <= 1) {
                    $page = 1;
                } else {
                    $page = @ceil($cpost / $article_comment_num);
                }
            } else {
                $page = 1;
            }
            $newcomment['dateline'] = sadate('m-d', $newcomment['dateline']);
            $newcomment['content'] = trimmed_title(htmlspecialchars(sax_addslashes(str_replace(array("\r\n", "\n", "\r"), '', $newcomment['content']))), $set['recentcomment_limit']);
            $cmnum = '#cm' . $newcomment['commentid'];
            $newcomment['article_url'] = getpermalink($newcomment['articleid'], $newcomment['alias'], $page > 1 ? $page : 0) . $cmnum;
            $commentdb[$newcomment['commentid']] = $newcomment;
        }
        unset($newcomment);
    }
    $contents = "\$newcommentcache = unserialize('" . addcslashes(serialize($commentdb), '\\\'') . "');";
    writetocache('newcomments', $contents);
}
function htmlSubString($content, $maxlen = 300, $offset = 0)
{
    //把字符按HTML标签变成数组。
    $content = preg_split("/(<[^>]+?>)/si", $content, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
    $wordrows = 0;
    //中英字数
    $outstr = "";
    //生成的字串
    $wordend = false;
    //是否符合最大的长度
    $beginTags = 0;
    //除<img><br><hr>这些短标签外,其它计算开始标签,如<div*>
    $endTags = 0;
    //计算结尾标签,如</div>,如果$beginTags==$endTags表示标签数目相对称,可以退出循环。
    //print_r($content);
    foreach ($content as $value) {
        if (trim($value) == "") {
            continue;
        }
        //如果该值为空,则继续下一个值
        if (strpos(";{$value}", "<") > 0) {
            //如果与要载取的标签相同,则到处结束截取。
            if (trim($value) == $maxlen) {
                $wordend = true;
                continue;
            }
            if ($wordend == false) {
                $outstr .= $value;
                if (!preg_match("/<img([^>]+?)>/is", $value) && !preg_match("/<param([^>]+?)>/is", $value) && !preg_match("/<!([^>]+?)>/is", $value) && !preg_match("/<br([^>]+?)>/is", $value) && !preg_match("/<hr([^>]+?)>/is", $value)) {
                    $beginTags++;
                    //除img,br,hr外的标签都加1
                }
            } else {
                if (preg_match("/<\\/([^>]+?)>/is", $value, $matches)) {
                    $endTags++;
                    $outstr .= $value;
                    if ($beginTags == $endTags && $wordend == true) {
                        break;
                    }
                    //字已载完了,并且标签数相称,就可以退出循环。
                } else {
                    if (!preg_match("/<img([^>]+?)>/is", $value) && !preg_match("/<param([^>]+?)>/is", $value) && !preg_match("/<!([^>]+?)>/is", $value) && !preg_match("/<br([^>]+?)>/is", $value) && !preg_match("/<hr([^>]+?)>/is", $value)) {
                        $beginTags++;
                        //除img,br,hr外的标签都加1
                        $outstr .= $value;
                    }
                }
            }
        } else {
            if (is_numeric($maxlen)) {
                //截取字数
                $curLength = getstrlen($value);
                $maxLength = $curLength + $wordrows;
                if ($wordend == false) {
                    if ($maxLength > $maxlen) {
                        //总字数大于要截取的字数,要在该行要截取
                        $outstr .= trimmed_title($value, $maxlen - $wordrows, $offset);
                        $wordend = true;
                    } else {
                        $wordrows = $maxLength;
                        $outstr .= $value;
                    }
                }
            } else {
                if ($wordend == false) {
                    $outstr .= $value;
                }
            }
        }
    }
    //循环替换掉多余的标签,如<p></p>这一类
    while (preg_match("/<([^\\/][^>]*?)><\\/([^>]+?)>/is", $outstr)) {
        $outstr = preg_replace_callback("/<([^\\/][^>]*?)><\\/([^>]+?)>/is", "strip_empty_html", $outstr);
    }
    //把误换的标签换回来
    if (strpos(";" . $outstr, "[html_") > 0) {
        $outstr = str_replace("[html_&lt;]", "<", $outstr);
        $outstr = str_replace("[html_&gt;]", ">", $outstr);
    }
    //echo htmlspecialchars($outstr);
    return $outstr;
}
 }
 //htmlSubString
 //echo 'aaa:'.getstrlen($article['content']);
 //截取内容
 if ($options['wap_article_limit'] && !$offset) {
     if (getstrlen($article['content']) < $options['wap_article_limit']) {
         $last = 0;
         $next = 0;
     } else {
         $article['content'] = trimmed_title($article['content'], $options['wap_article_limit']);
         $last = 0;
         $next = 1;
         $offset_next = $offset + $options['wap_article_limit'];
     }
 } elseif ($options['wap_article_limit'] && $offset > 0) {
     $article['content'] = trimmed_title($article['content'], $options['wap_article_limit'], $offset);
     $last = 1;
     $offset_last = $offset - $options['wap_article_limit'];
     if ($offset_last < 0) {
         $offset_last = 0;
     }
     if (getstrlen($article['content']) < $options['wap_article_limit']) {
         $next = 0;
     } else {
         $next = 1;
         $offset_next = $offset + $options['wap_article_limit'];
     }
 } else {
     $last = 0;
     $next = 0;
 }
         unset($meta);
         $relids = explode(',', $relaids);
         // 清除重复值的单元并删除当前ID
         $relids = array_unique($relids);
         $relids = array_flip($relids);
         unset($relids[$article['articleid']]);
         $relids = array_flip($relids);
         ////////
         $related_total = count($relids);
         $relids = implode(',', $relids);
         if ($related_total > 1 && $relids != $article['articleid']) {
             $query = $DB->query("SELECT articleid,title,alias,comments FROM {$db_prefix}articles WHERE visible='1' AND articleid IN ({$relids}) ORDER BY dateline DESC LIMIT " . intval($options['related_shownum']));
             $titledb = array();
             while ($title = $DB->fetch_array($query)) {
                 $title['url'] = getpermalink($title['articleid'], $title['alias']);
                 $title['title'] = trimmed_title($title['title'], $options['related_title_limit']);
                 $titledb[$title['articleid']] = $title;
             }
             unset($title);
             $DB->free_result($query);
         }
     }
 }
 // 评论
 if ($article['comments']) {
     $commentsql = '';
     /*
     改成评论嵌套就不用LIMIT了。直接查询全部,再用array_slice分割数组分页。
     if($article_comment_num) {
     	if($page) {
     		$cmtorderid = ($page - 1) * $article_comment_num;