<?php



require_once libfile('function/post');

loadforum();

$_G['tid'] = (int)$_GET['tid'];



if($_GET['action'] == 'recommend') {



	dsetcookie('discuz_recommend', '', -1, 0);

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

        if($_GET['ishtml']) {

            echo odz_lang('not_loggedin');

            exit;

        }

		odz_error('not_loggedin', ODZ_ERR_LOGIN);

	}

	if(!$_G['setting']['recommendthread']['status'] || !$_G['group']['allowrecommend']) {

        if($_GET['ishtml']) {

            echo odz_lang('no_privilege_recommend');

            exit;

        }

		odz_error('no_privilege_recommend');

	}



	if($thread['authorid'] == $_G['uid'] && !$_G['setting']['recommendthread']['ownthread']) {

        if($_GET['ishtml']) {

            echo odz_lang('recommend_self_disallow');

            exit;

        }

		odz_error('recommend_self_disallow');

	}

	if(C::t('forum_memberrecommend')->fetch_by_recommenduid_tid($_G['uid'], $_G['tid'])) {

        if($_GET['ishtml']) {

            echo odz_lang('recommend_duplicate');

            exit;

        }

        odz_error('recommend_duplicate');

	}



	$recommendcount = C::t('forum_memberrecommend')->count_by_recommenduid_dateline($_G['uid'], $_G['timestamp']-86400);

	if($_G['setting']['recommendthread']['daycount'] && $recommendcount >= $_G['setting']['recommendthread']['daycount']) {

        if($_GET['ishtml']) {

            echo odz_lang('recommend_outoftimes');

            exit;

        }

		odz_error('recommend_outoftimes');

	}



	$_G['group']['allowrecommend'] = intval($_GET['do'] == 'add' ? $_G['group']['allowrecommend'] : -$_G['group']['allowrecommend']);

	$fieldarr = array();

	if($_GET['do'] == 'add') {

		$heatadd = 'recommend_add=recommend_add+1';

		$fieldarr['recommend_add'] = 1;

	} else {

		$heatadd = 'recommend_sub=recommend_sub+1';

		$fieldarr['recommend_sub'] = 1;

	}



    update_threadpartake($_G['tid']);

    $fieldarr['heats'] = 0;

	$fieldarr['recommends'] = $_G['group']['allowrecommend'];

	C::t('forum_thread')->increase($_G['tid'], $fieldarr);

	C::t('forum_memberrecommend')->insert(array('tid'=>$_G['tid'], 'recommenduid'=>$_G['uid'], 'dateline'=>$_G['timestamp']));



	dsetcookie('recommend', 1, 43200);

	$recommendv = $_G['group']['allowrecommend'] > 0 ? '+'.$_G['group']['allowrecommend'] : $_G['group']['allowrecommend'];

	if($_G['setting']['recommendthread']['daycount']) {

		$daycount = $_G['setting']['recommendthread']['daycount'] - $recommendcount;

		odz_success('recommend_daycount_succeed', 0, array('daycount'=>$daycount));

	} else {

		odz_success('recommend_succeed');

	}



} elseif($_GET['action'] == 'commentmore') {



	if(!$_G['setting']['commentnumber']) {

        if($_GET['ishtml']) {

            echo odz_lang('postcomment_closed');

            exit;

        }

	    odz_error('postcomment_closed');

	}

	require_once libfile('function/discuzcode');

	$commentlimit = intval($_G['setting']['commentnumber']);

	$page = max(1, $_G['page']);

	$start_limit = ($page - 1) * $commentlimit;

	$comments = array();

	foreach(C::t('forum_postcomment')->fetch_all_by_search(null, $_GET['pid'], null, null, null, null, null, $start_limit, $commentlimit) as $comment) {

		$comment['avatar'] = avatar($comment['authorid'], 'small', true);

		$comment['dateline'] = dgmdate($comment['dateline']);

		$comment['comment'] = str_replace(array('[b]', '[/b]', '[/color]'), array('<b>', '</b>', '</font>'), preg_replace("/\[color=([#\w]+?)\]/i", "<font color=\"\\1\">", $comment['comment']));

		$comments[] = $comment;

	}

	$totalcomment = C::t('forum_postcomment')->fetch_standpoint_by_pid($_GET['pid']);

	$totalcomment = $totalcomment['comment'];

	$totalcomment = preg_replace('/<i>([\.\d]+)<\/i>/e', "'<i class=\"cmstarv\" style=\"background-position:20px -'.(intval(\\1) * 16).'px\">'.sprintf('%1.1f', \\1).'</i>'.(\$cic++ % 2 ? '<br />' : '');", $totalcomment);

	$count = C::t('forum_postcomment')->count_by_search(null, $_GET['pid']);

	$multi = multi($count, $commentlimit, $page, "forum.php?mod=misc&action=commentmore&tid=$_G[tid]&pid=$_GET[pid]");



	odz_result(array('total'=>$count, 'perpage'=>$commentlimit, 'page'=>$page, 'comments'=>$comments));



} elseif ($_GET['action'] == 'postreview') {



    if(!$_G['setting']['repliesrank'] || empty($_G['uid'])) {

        if($_GET['ishtml']) {

            echo odz_lang('to_login');

            exit;

        }

        odz_error('to_login');

    }



    $doArray = array('support', 'against');



    $post = C::t('forum_post')->fetch('tid:'.$_GET['tid'], $_GET['pid'], false);



    if(!in_array($_GET['do'], $doArray) || empty($post) || $post['first'] == 1 || ($_G['setting']['threadfilternum'] && $_G['setting']['filterednovote'] && getstatus($post['status'], 11))) {

        if($_GET['ishtml']) {

            echo odz_lang('undefined_action');

            exit;

        }

        odz_error('undefined_action');

    }



    $hotreply = C::t('forum_hotreply_number')->fetch_by_pid($post['pid']);

    if($_G['uid'] == $post['authorid']) {

        if($_GET['ishtml']) {

            echo odz_lang('noreply_yourself_error');

            exit;

        }

        odz_error('noreply_yourself_error');

    }



    if(empty($hotreply)) {

        $hotreply['pid'] = C::t('forum_hotreply_number')->insert(array(

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

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

            'support' => 0,

            'against' => 0,

            'total' => 0,

        ), true);

    } else {

        if(C::t('forum_hotreply_member')->fetch($post['pid'], $_G['uid'])) {

            if($_GET['ishtml']) {

                echo odz_lang('noreply_voted_error');

                exit;

            }

            odz_error('noreply_voted_error');

        }

    }



    $typeid = $_GET['do'] == 'support' ? 1 : 0;



    C::t('forum_hotreply_number')->update_num($post['pid'], $typeid);

    C::t('forum_hotreply_member')->insert(array(

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

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

        'uid' => $_G['uid'],

        'attitude' => $typeid,

    ));



    $hotreply[$_GET['do']]++;



    odz_success('operation_done');



} elseif($_GET['action'] == 'activityapplies') { // �

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

        if ($_GET['ishtml']) {

            // $res = array('errcode'=>-1, 'errmsg'=>odz_lang('not_loggedin'));

            // echo json_encode($res);

            echo odz_lang('not_loggedin');

            exit;

        }

        odz_error('not_loggedin', ODZ_ERR_LOGIN);

    }



    if($_GET['activitysubmit']) {

        $activity = C::t('forum_activity')->fetch($_G['tid']);

        if($activity['expiration'] && $activity['expiration'] < TIMESTAMP) {

            // showmessage('activity_stop', NULL, array(), array('login' => 1));

            if ($_GET['ishtml']) {

                // $res = array('errcode'=>-1, 'errmsg'=>odz_lang('activity_stop'));

                // echo json_encode($res);

                echo odz_lang('activity_stop');

                exit;

            }

            odz_error('activity_stop');

        }

        $applyinfo = array();

        $applyinfo = C::t('forum_activityapply')->fetch_info_for_user($_G['uid'], $_G['tid']);

        if($applyinfo && $applyinfo['verified'] < 2) {

            if ($_GET['ishtml']) {

                // $res = array('errcode'=>-1, 'errmsg'=>odz_lang('activity_repeat_apply'));

                // echo json_encode($res);

                echo odz_lang('activity_repeat_apply');

                exit;

            }

            odz_error('activity_repeat_apply');

        }

        $payvalue = intval($_GET['payvalue']);

        $payment = $_GET['payment'] ? $payvalue : -1;

        $message = cutstr(dhtmlspecialchars($_GET['message']), 200);

        $verified = $thread['authorid'] == $_G['uid'] ? 1 : 0;

         if($activity['ufield']) {

             $ufielddata = array();

             $activity['ufield'] = dunserialize($activity['ufield']);

             if(!empty($activity['ufield']['userfield'])) {

                 $censor = discuz_censor::instance();

                 loadcache('profilesetting');

                 foreach($activity['ufield']['userfield'] as $filedname) {

                     $value = $_GET[$filedname];

                     if(is_array($value)) {

                         $value = implode(',', $value);

                     }

//                     $value = cutstr(dhtmlspecialchars(trim($value)), 100, '.');

//                     if($_G['cache']['profilesetting'][$filedname]['formtype'] == 'file' && !preg_match("/^https?:\/\/(.*)?\.(jpg|png|gif|jpeg|bmp)$/i", $value)) {

//                         // showmessage('activity_imgurl_error');

//                         odz_error('activity_imgurl_error');

//                     }

//                     if(empty($value)/* && $filedname != 'residedist' && $filedname != 'residecommunity'*/) {

//                         // showmessage('activity_exile_field');

//                         odz_error('activity_exile_field');

//                     }

                     $ufielddata['userfield'][$filedname] = $value;

                 }

             }

             if(!empty($activity['ufield']['extfield'])) {

                 foreach($activity['ufield']['extfield'] as $fieldid) {

                     $value = cutstr(dhtmlspecialchars(trim($_GET[''.$fieldid])), 50, '.');

                     $ufielddata['extfield'][$fieldid] = $value;

                 }

             }

             $ufielddata = !empty($ufielddata) ? serialize($ufielddata) : '';

         }

        

        if($_G['setting']['activitycredit'] && $activity['credit'] && empty($applyinfo['verified'])) {

            // checklowerlimit(array('extcredits'.$_G['setting']['activitycredit'] => '-'.$activity['credit']));

            updatemembercount($_G['uid'], array($_G['setting']['activitycredit'] => '-'.$activity['credit']), true, 'ACC', $_G['tid']);

        }

        if($applyinfo && $applyinfo['verified'] == 2) {

            $newinfo = array(

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

                'username' => $_G['username'],

                'uid' => $_G['uid'],

                'message' => $message,

                'verified' => $verified,

                'dateline' => $_G['timestamp'],

                'payment' => $payment,

                'ufielddata' => $ufielddata

            );

            C::t('forum_activityapply')->update($applyinfo['applyid'], $newinfo);

        } else {

            $data = array('tid' => $_G['tid'], 'username' => $_G['username'], 'uid' => $_G['uid'], 'message' => $message, 'verified' => $verified, 'dateline' => $_G['timestamp'], 'payment' => $payment, 'ufielddata' => $ufielddata);

            C::t('forum_activityapply')->insert($data);

        }



        $applynumber = C::t('forum_activityapply')->fetch_count_for_thread($_G['tid']);

        C::t('forum_activity')->update($_G['tid'], array('applynumber' => $applynumber));



        if($thread['authorid'] != $_G['uid']) {

            notification_add($thread['authorid'], 'activity', 'activity_notice', array(

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

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

            ));

            $space = array();

            space_merge($space, 'field_home');



            if(!empty($space['privacy']['feed']['newreply'])) {

                $feed['icon'] = 'activity';

                $feed['title_template'] = 'feed_reply_activity_title';

                $feed['title_data'] = array(

                    'subject' => "<a href=\"forum.php?mod=viewthread&tid=$_G[tid]\">$thread[subject]</a>",

                    'hash_data' => "tid{$_G[tid]}"

                );

                $feed['id'] = $_G['tid'];

                $feed['idtype'] = 'tid';

                postfeed($feed);

            }

        }

        if ($_GET['ishtml']) {

            // $res = array('errcode'=>0, 'errmsg'=>odz_lang('activity_completion'));

            // echo json_encode($res);

            echo odz_lang('activity_completion');

            exit;

        }

        odz_success('activity_completion');



    } elseif($_GET['activitycancel']) {

        C::t('forum_activityapply')->delete_for_user($_G['uid'], $_G['tid']);

        $applynumber = C::t('forum_activityapply')->fetch_count_for_thread($_G['tid']);

        C::t('forum_activity')->update($_G['tid'], array('applynumber' => $applynumber));

        $message = cutstr(dhtmlspecialchars($_GET['message']), 200);

        if($thread['authorid'] != $_G['uid']) {

            notification_add($thread['authorid'], 'activity', 'activity_cancel', array(

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

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

                'reason' => $message

            ));

        }

        if ($_GET['ishtml']) {

            // $res = array('errcode'=>0, 'errmsg'=>odz_lang('activity_cancel_success'));

            // echo json_encode($res);

            echo odz_lang('activity_cancel_success');

            exit;

        }

        odz_success('activity_cancel_success');

    }



} elseif($_GET['action'] == 'votepoll') { // ͶƱ



    if(!$_G['group']['allowvote']) {

        if ($_GET['ishtml']) {

            // $res = array('errcode'=>-1, 'errmsg'=>odz_lang('group_nopermission', array('grouptitle' => $_G['group']['grouptitle'])));

            // echo json_encode($res, true);

            echo odz_lang('group_nopermission', array('grouptitle' => $_G['group']['grouptitle']));

            exit;

        }

        odz_error('group_nopermission', -2, array('grouptitle' => $_G['group']['grouptitle']));



    } elseif(!empty($thread['closed'])) {

        if ($_GET['ishtml']) {

            // $res = array('errcode'=>-3, 'errmsg'=>odz_lang('thread_poll_closed'));

            // echo json_encode($res, true);

            echo odz_lang('thread_poll_closed');

            exit;

        }

        odz_error('thread_poll_closed');

    } elseif(empty($_GET['pollanswers'])) {

        if ($_GET['ishtml']) {

            // $res = array('errcode'=>-4, 'errmsg'=>odz_lang('thread_poll_invalid'));

            // echo json_encode($res, true);

            echo odz_lang('thread_poll_invalid');

            exit;

        }

        odz_error('thread_poll_invalid');

    }



    $pollarray = C::t('forum_poll')->fetch($_G['tid']);

    $overt = $pollarray['overt'];

    if(!$pollarray) {

        if ($_GET['ishtml']) {

            // $res = array('errcode'=>-5, 'errmsg'=>odz_lang('poll_not_found'));

            // echo json_encode($res, true);

            echo odz_lang('poll_not_found');

            exit;

        }

        odz_error('poll_not_found');

    } elseif($pollarray['expiration'] && $pollarray['expiration'] < TIMESTAMP) {

        if ($_GET['ishtml']) {

            // $res = array('errcode'=>-6, 'errmsg'=>odz_lang('poll_overdue'));

            // echo json_encode($res, true);

            echo odz_lang('poll_overdue');

            exit;

        }

        odz_error('poll_overdue');

    } elseif($pollarray['maxchoices'] && $pollarray['maxchoices'] < count($_GET['pollanswers'])) {

        if ($_GET['ishtml']) {

            // $res = array('errcode'=>-6, 'errmsg'=>odz_lang('poll_overdue'));

            // echo json_encode($res, true);

            echo odz_lang('poll_choose_most', array('maxchoices' => $pollarray['maxchoices']));

            exit;

        }

        odz_error('poll_choose_most', -7, array('maxchoices' => $pollarray['maxchoices']));

    }



    $voterids = $_G['uid'] ? $_G['uid'] : $_G['clientip'];



    $polloptionid = array();

    $query = C::t('forum_polloption')->fetch_all_by_tid($_G['tid']);

    foreach($query as $pollarray) {

        if(strexists("\t".$pollarray['voterids']."\t", "\t".$voterids."\t")) {

            if ($_GET['ishtml']) {

                // $res = array('errcode'=>-8, 'errmsg'=>odz_lang('thread_poll_voted'));

                // echo json_encode($res, true);

                echo odz_lang('thread_poll_voted');

                exit;

            }

            odz_error('thread_poll_voted');

        }

        $polloptionid[] = $pollarray['polloptionid'];

    }



    $polloptionids = '';

    foreach($_GET['pollanswers'] as $key => $id) {

        if(!in_array($id, $polloptionid)) {

            if ($_GET['ishtml']) {

                // $res = array('errcode'=>-9, 'errmsg'=>odz_lang('parameters_error'));

                // echo json_encode($res, true);

                echo odz_lang('parameters_error');

                exit;

            }

            odz_error('parameters_error');



        }

        unset($polloptionid[$key]);

        $polloptionids[] = $id;

    }



    C::t('forum_polloption')->update_vote($polloptionids, $voterids."\t", 1);

    C::t('forum_thread')->update($_G['tid'], array('lastpost'=>$_G['timestamp']), true);

    C::t('forum_poll')->update_vote($_G['tid']);

    C::t('forum_pollvoter')->insert(array(

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

        'uid' => $_G['uid'],

        'username' => $_G['username'],

        'options' => implode("\t", $_GET['pollanswers']),

        'dateline' => $_G['timestamp'],

        ));

    updatecreditbyaction('joinpoll');



    $space = array();

    space_merge($space, 'field_home');



    if($overt && !empty($space['privacy']['feed']['newreply'])) {

        $feed['icon'] = 'poll';

        $feed['title_template'] = 'feed_thread_votepoll_title';

        $feed['title_data'] = array(

            'subject' => "<a href=\"forum.php?mod=viewthread&tid=$_G[tid]\">$thread[subject]</a>",

            'author' => "<a href=\"home.php?mod=space&uid=$thread[authorid]\">$thread[author]</a>",

            'hash_data' => "tid{$_G[tid]}"

        );

        $feed['id'] = $_G['tid'];

        $feed['idtype'] = 'tid';

        postfeed($feed);

    }



    if(!empty($_G['inajax'])) {

        if ($_GET['ishtml']) {

            // $res = array('errcode'=>0, 'errmsg'=>odz_lang('thread_poll_succeed'));

            // echo json_encode($res, true);

            echo odz_lang('thread_poll_succeed');

            exit;

        }

        odz_success('thread_poll_succeed');

    } else {

        if ($_GET['ishtml']) {

            // $res = array('errcode'=>0, 'errmsg'=>odz_lang('thread_poll_succeed'));

            // echo json_encode($res, true);

            echo odz_lang('thread_poll_succeed');

            exit;

        }

        odz_success('thread_poll_succeed');

    }



} elseif($_GET['action'] == 'viewvote') { // ��ʾͶƱ����



    if($_G[forum_thread][special] != 1) {

        echo odz_lang('thread_poll_none');

    }

    require_once libfile('function/post');

    $polloptionid = is_numeric($_GET['polloptionid']) ? $_GET['polloptionid'] : '';



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

    $perpage = 100;

    $pollinfo = C::t('forum_poll')->fetch($_G['tid']);

    $overt = $pollinfo['overt'];



    $polloptions = array();

    $query = C::t('forum_polloption')->fetch_all_by_tid($_G['tid']);

    foreach($query as $options) {

        if(empty($polloptionid)) {

            $polloptionid = $options['polloptionid'];

        }

        $options['polloption'] = preg_replace("/\[url=(https?){1}:\/\/([^\[\"']+?)\](.+?)\[\/url\]/i",

            "<a href=\"\\1://\\2\" target=\"_blank\">\\3</a>", $options['polloption']);

        $polloptions[] = $options;

    }

    $arrvoterids = array();

    if($overt || $_G['adminid'] == 1 || $thread['authorid'] == $_G['uid']) {

        foreach ($polloptions as $key => $value) {

            $polloptioninfo = C::t('forum_polloption')->fetch($value['polloptionid']);

            $voterids = $polloptioninfo['voterids'];

            $arrvoterid = explode("\t", trim($voterids));

            // $arrvoterids[$value['polloptionid']] = trim($voterids);

            $arrvoterids = array_merge($arrvoterids, $arrvoterid);



        }

    } else {

        // odz_error('thread_poll_nopermission');

        echo odz_lang('thread_poll_nopermission');

        exit;

    }

    if(!empty($arrvoterids)) {

        $count = count($arrvoterids);

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

        $multipage = multi($count, $perpage, $page, "forum.php?mod=misc&action=viewvote&tid=$_G[tid]&polloptionid=$polloptionid".( $_GET[handlekey] ? "&handlekey=".$_GET[handlekey] : '' ));

        $arrvoterids = array_slice($arrvoterids, $multi, $perpage);

    }

    $voterlist = $voter = array();

    if($arrvoterids) {

        $voterlist = C::t('common_member')->fetch_all($arrvoterids);

    }

    // print_r($arrvoterids);

    // print_r($polloptions);

    // print_r($voterlist);

    // print_r($arrvoterids);

    // print_r($multipage);

    // print_r($polloptioninfo);

    include './template/poll.php';;



}



?>