逆风天 发表于 2021-12-4 19:44:16

Discuz 根据帖子ID还原帖子HTML

主要目的是还原帖子内的附件,图片等。
代码如下:
<?php
if ( !defined( 'IN_DISCUZ' ) ) {
exit( 'Access Denied' );
}

// $tid 为帖子ID
// 在广播 测试过 只显示一条的信息
$thread_info = DB::fetch_first( "select a.`tid`, a.`authorid`, a.`author`, a.`dateline`, a.`subject`, b.`message` from " . DB::table( 'forum_thread' ) . " as a, " . DB::table( 'forum_post' ) . " as b where a.tid=$tid and a.tid=b.tid and b.first=1 order by pid desc limit 1" );
require_once libfile( 'function/discuzcode' );
$thread_info[ 'dateline' ] = date( "Y-m-d H:i:s", $thread_info[ 'dateline' ] );
$thread_info[ 'message' ] = discuzcode( $thread_info[ 'message' ] );

if ( preg_match_all( "/\(\d+)\[\/attach\]/i", $thread_info[ 'message' ], $matchaids ) ) {
$attach_ids = $matchaids[ 1 ];
}

$attach_list = array();

foreach ( $attach_ids as $aid ) {
$find = "/\$aid\[\/attach\]/i";
$thread_info[ 'message' ] = preg_replace( $find, get_lw_attach_path( $aid ), $thread_info[ 'message' ], 1 );
$thread_info[ 'message' ] = preg_replace( $find, '', $thread_info[ 'message' ] );
}

function get_lw_attach_path( $aid ) {
global $_G;
$return = $filename = '';
if ( $attach = C::t( 'forum_attachment_n' )->fetch( 'aid:' . $aid, $aid, array( 1, -1 ) ) ) {
    return get_lw_attach_path_str( $attach );
}
return $filename;
}

function get_lw_attach_path_str( $attach ) {
global $_G;
if ( !$attach[ 'isimage' ] ) {
    return '<a href="' . $_G[ 'siteurl' ] . 'forum.php?mod=attachment&aid=' . aidencode( $attach[ 'aid' ] ) . '">' . $attach[ 'filename' ] . '</a>';
}
if ( $attach[ 'remote' ] ) {
    $imgurl = $_G[ 'setting' ][ 'ftp' ][ 'attachurl' ] . 'forum/' . $attach[ 'attachment' ];
    return '<img onclick="viewimage(this);" src="' . $imgurl . '" />';
} else {
    if ( preg_match( '/^(?!http:)/', $attach[ 'url' ] ) ) {
      $attach[ 'url' ] = $_G[ 'siteurl' ] . 'data/attachment/forum/' . $attach[ 'url' ];
    }
    $imgurl = $attach[ 'url' ] . $attach[ 'attachment' ] . ( $_G[ 'gp_width' ] ? '&width=' . $_G[ 'gp_width' ] : '' ) . ( $_G[ 'gp_height' ] ? '&height=' . $_G[ 'gp_height' ] : '' );
    return '<a href="' . $imgurl . '" target="_blank"><img height="320" width="320" src="' . $imgurl . '" /></a>';
}
}

echo $thread_info[ 'message' ];

原文地址:discuz帖子内容还原HTML


进过测试,只能还原一个ID帖子的内容,可能是我哪里没有整对。


页: [1]
查看完整版本: Discuz 根据帖子ID还原帖子HTML