<?php
    /**
     *     个人中心好友动态
     *
     *     2016-4-25 wzf
     */
    if(!defined('IN_DISCUZ')) {
            exit('Access Denied');
    }
    require_once 'includes/gotye.php';
    require_once libfile('function/misc');
    require_once libfile('function/forum');
    require_once libfile('function/attachment');
    require_once libfile('function/forumlist');
    require_once libfile('function/spacecp');
    require_once 'includes/functions/attachment.php';
    loadcache(array('forums'));
    odz_loadforum();
//    echo $_G['uid'];
//    echo $_G['username'];exit;
    if(!$_G['uid']) {
        showmessage('user_not_login');
    }

    $forum_threadcount='';
    $pagecount='';
    $threadlist=array();
    $page = empty($_GET['page'])?1:intval($_GET['page']);
    if($page<1) $page=1;
    $perpage = 20;
    $start = ($page-1)*$perpage;
    $list = array();
    //个人资料用户信息
    $sql='SELECT fuid,fusername FROM '.DB::table('home_follow_friend')." WHERE uid = '$_G[uid]'";
    $home_follow_friend=DB::fetch_all($sql);
    if(empty($home_follow_friend)) {
        odz_result(array('threadlist'=>$threadlist,'pagecount'=>$pagecount,'page'=>$page,'forum_threadcount'=>$forum_threadcount));
//        showmessage('user_none_friend');
//      echo  odz_lang('user_none_friend');exit;
    }

    foreach($home_follow_friend as $val){
        $user_list[]=$val['fuid'];
    }
    $sql_forum_thread = 'SELECT * FROM '.DB::table('forum_post').' WHERE authorid IN (' . implode(',', $user_list) . ')  ORDER BY dateline DESC '.DB::limit($start, $perpage);
    $query = DB::query($sql_forum_thread);
    while($post = DB::fetch($query)) {
            $list[] = $post;
    }
    $threads = &$list;
    $forum = 'SELECT count(*) as num FROM '.DB::table('forum_post').' WHERE authorid IN (' . implode(',', $user_list) . ')  ORDER BY dateline DESC ';
    $result = DB::fetch(DB::query($forum));
    $forum_threadcount=$result['num'];
    $pagecount = @ceil($forum_threadcount / $perpage);
    $threadlist = array();
    foreach($threads as $thread) {
//        $thread=viewthread_procpost($thread);
        $curpostnum = $thread['position'];
	$pidpage = ceil($curpostnum / $perpage);
        $post['message'] = preg_replace('/\[attach\](.*)\[\/attach\]/s', '', $thread['message']);
        $post['message'] = preg_replace('/\[.*?\]/', '', $thread['message']);
        $thread['message'] = $post['message'];
        $thread['title']='';
        $thumb = '';
        if($thread['first']==1){
            $thread['title']=odz_format_subject($thread['subject']);
            $content='';
        }else{
            $forum_post =DB::fetch_first('SELECT * FROM '.DB::table('forum_post').' WHERE tid = '.$thread['tid']." and first=1");
            $thread['pid']=$forum_post['pid'];
            $thread['title']=$forum_post['subject'];
            if($forum_post['first']){
                $reg = '/((http|https):\/\/)+(\w+\.)+(\w+)[\w\/\.\-]*(jpg|gif|png)/';
                $matches = array();
                preg_match_all($reg, $forum_post['message'], $matches);
                if(!empty($matches[0])){
                   $thumb =$matches[0][0];
                }
            }
            $content=odz_format_subject($thread['message']);
        }
//        if($thread['pid']){
//            $sortlist = array();
//            $_G['tid']=$thread['tid'];
//            $forum_thread_list=$thread;
//            odz_parseattach($thread['pid'], '', $forum_thread_list, '');
//            $i=0;
//            foreach($forum_thread_list[$thread['pid']]['attachments'] as  $attach) {
//                $attachurl = $attach['url'].$attach['attachment'].($attach['thumb'] ? '.thumb.jpg' : '');
//                if ($attach['isimage'] != 0&&$i<3) {
//                        $sortlist[$i]= mocuz_thumb($attach['aid'], intval($attach['width']), intval($attach['height']));
//                }
//                $i++;
//            }
//        }
        if($thread['pid']){
            $_G['tid']=$thread['tid'];
            $forum_thread_list=$thread;
            odz_parseattach($thread['pid'], '', $forum_thread_list, '');
             $i=0;
            foreach($forum_thread_list[$thread['pid']]['attachments'] as  $attach) {
                $attachurl = $attach['url'].$attach['attachment'].($attach['thumb'] ? '.thumb.jpg' : '');
                if ($attach['isimage'] != 0&&$i<1) {
                    $thumb= mocuz_thumb($attach['aid'], intval($attach['width']), intval($attach['height']));
//                    $thumb=$attach['url_ex'];
                }
                $i++;
            }
//            else{
//                $thumb='';
//            }
        }

        $threadlist[] = array(
            'tid' => $thread['tid'],
            'fid' => $thread['fid'],
            'pid' => $thread['pid'],
            'pidpage' => $pidpage,
            'thumb' => $thumb,
            'avatar' => avatar($thread['authorid'], 'small', true),
            'author' => $thread['author'],
            'authorid' => $thread['authorid'],
            'title' => $thread['title'],
            'idtype' =>'tid',
//            'praid' => $thread['praid'],
            'first' => $thread['first'],
//            'pratimes' => $total['num'],
            'content' => isset($content)?$content:'',
//            'subject' => odz_format_subject($thread['subject']),
            'dateline' => $thread['dateline'],
            'showtime' => dgmdate($thread['dateline'], 'u', '9999', getglobal('setting/dateformat').' H:i:s'),
            'views' => isset($thread['views'])?$thread['views']:0 ,
            'replies' =>isset($thread['replies'])?$thread['replies']:0 ,
//            'forumname' => strip_tags($thread['forumname']),
            'hasimageattachment' => ($thread['attachment'] == 2 ? 1 : 0),
            'haslock' => isset($thread['closed'])?$thread['closed']:0 ,
        );
    }

    odz_result(array('threadlist'=>$threadlist,'pagecount'=>$pagecount,'page'=>$page,'forum_threadcount'=>$forum_threadcount));
?>
