<?php

    /**

     *     个人中心好友动态

     *

     *     2016-4-25 wzf 

     */

    if(!defined('IN_DISCUZ')) {

            exit('Access Denied');

    }

    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();

    if(!$_G['uid']) {

        showmessage('user_not_login');

    }

    $space = getuserbyuid($_GET['uid']);

    $page = empty($_GET['page'])?1:intval($_GET['page']);

    if($page<1) $page=1;

    $allowviewuserthread = $_G['setting']['allowviewuserthread'];

    $perpage = 20;

    $start = ($page-1)*$perpage;

    $list = array();

    $authorid = 0;

    $replies = $closed = $displayorder = null;

    $dglue = '=';

//    if($_G['uid']) {

//        $allowview = true;

//        $viewtype=$_GET['type'];

//        if($space['uid'] != $_G['uid']) {

//            if($allowview) {

//                $viewfids = str_replace("'", '', $allowviewuserthread);

//                if(!empty($viewfids)) {

//                        $viewfids = explode(',', $viewfids);

//                }

//            }

//        }

//        if($viewtype == 'thread' && $allowview) {

//            $authorid = $space['uid'];

//            $ordersql = 't.tid DESC';

//        } 

//    }

//个人资料用户信息

//    unset($space);

    $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)) {

      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;

    }

    $forum = 'SELECT * FROM '.DB::table('forum_post').' WHERE authorid IN (' . implode(',', $user_list) . ')  ORDER BY dateline DESC ';

    $result = DB::query($forum);

    $forum_threadcount=count($result);

    $pagecount = @ceil($forum_threadcount / $perpage);

    $threads = &$list;

    $threadlist = array();

    foreach($threads as $thread) {

        $biu_fid = get_mocuz_setting('biu_data_source');

        if(isset($biu_fid['biu_data_source']) && $thread['fid'] == $biu_fid['biu_data_source']){

                continue;

        }

        $post['message'] = preg_replace('/\[attach\](.*)\[\/attach\]/s', '', $thread['message']);

        $post['message'] = preg_replace('/\[.*?\]/', '', $thread['message']);

        $thread['message'] = $post['message'];

        $thread['title']='';

        if($thread['first']){

            $thread['title']=odz_format_subject($thread['subject']);

        }else{

            $forum_post =DB::fetch_first('SELECT * FROM '.DB::table('forum_post').' WHERE tid = '.$thread['tid']);

            $thread['pid']=$forum_post['pid'];

            $thread['title']=$forum_post['subject'];

            $content=odz_format_subject($thread['message']);

        }

        if($thread['pid']){

            $thumb = '';

            $_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']));

//                        $sortlist[$i]['thumb_image']=$attach['url_ex'];

//                        $sortlist[$i]['weight']=$attach['width'];//获取图片的宽

//                        $sortlist[$i]['height']= $attach['height'] ? $attach['height'] : 100;//获取图片的宽

                }

                $i++;

            }

        }

        $threadlist[] = array(

            'tid' => $thread['tid'],

            'fid' => $thread['fid'],

            'pid' => $thread['pid'],

            'thumb' => $thumb,

            'avatar' => avatar($post['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($post['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));

?>