查看 1246
回复 0
Discuz 根据帖子ID还原帖子HTML
逆风天

16

主题

0

回帖

105

积分
发表于 2021-12-4 19:44:16
显示全部楼层 阅读模式
主要目的是还原帖子内的附件,图片等。
代码如下:
  1. <?php
  2. if ( !defined( 'IN_DISCUZ' ) ) {
  3.   exit( 'Access Denied' );
  4. }

  5. // $tid 为帖子ID
  6. // 在广播 测试过 只显示一条的信息
  7. $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" );
  8. require_once libfile( 'function/discuzcode' );
  9. $thread_info[ 'dateline' ] = date( "Y-m-d H:i:s", $thread_info[ 'dateline' ] );
  10. $thread_info[ 'message' ] = discuzcode( $thread_info[ 'message' ] );

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

  14. $attach_list = array();

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

  20. function get_lw_attach_path( $aid ) {
  21.   global $_G;
  22.   $return = $filename = '';
  23.   if ( $attach = C::t( 'forum_attachment_n' )->fetch( 'aid:' . $aid, $aid, array( 1, -1 ) ) ) {
  24.     return get_lw_attach_path_str( $attach );
  25.   }
  26.   return $filename;
  27. }

  28. function get_lw_attach_path_str( $attach ) {
  29.   global $_G;
  30.   if ( !$attach[ 'isimage' ] ) {
  31.     return '<a href="' . $_G[ 'siteurl' ] . 'forum.php?mod=attachment&aid=' . aidencode( $attach[ 'aid' ] ) . '">' . $attach[ 'filename' ] . '</a>';
  32.   }
  33.   if ( $attach[ 'remote' ] ) {
  34.     $imgurl = $_G[ 'setting' ][ 'ftp' ][ 'attachurl' ] . 'forum/' . $attach[ 'attachment' ];
  35.     return '<img onclick="viewimage(this);" src="' . $imgurl . '" />';
  36.   } else {
  37.     if ( preg_match( '/^(?!http:)/', $attach[ 'url' ] ) ) {
  38.       $attach[ 'url' ] = $_G[ 'siteurl' ] . 'data/attachment/forum/' . $attach[ 'url' ];
  39.     }
  40.     $imgurl = $attach[ 'url' ] . $attach[ 'attachment' ] . ( $_G[ 'gp_width' ] ? '&width=' . $_G[ 'gp_width' ] : '' ) . ( $_G[ 'gp_height' ] ? '&height=' . $_G[ 'gp_height' ] : '' );
  41.     return '<a href="' . $imgurl . '" target="_blank"><img height="320" width="320" src="' . $imgurl . '" /></a>';
  42.   }
  43. }

  44. echo $thread_info[ 'message' ];
复制代码


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


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


您需要登录后才可以回帖 登录 立即注册
QQ 快速回复 返回列表