<?php
if($_G['pa_count'] < 2) {
    odz_error('parameters_error', -112);
}
odz_loadforum();
require_once libfile('function/home');
if($isfirstpost && (($special == 1 && !$_G['group']['allowpostpoll']) || ($special == 2 && !$_G['group']['allowposttrade']) || ($special == 3 && !$_G['group']['allowpostreward']) || ($special == 4 && !$_G['group']['allowpostactivity']) || ($special == 5 && !$_G['group']['allowpostdebate']))) {
	$message=odz_lang('group_nopermission');
        if($server==1){
            showmessage('group_nopermission');
        }else{
            echo json_encode(odz_encode(array('status'=>3,'message'=>$message)));exit;
        }
}
if(!$_G['uid'] && !$_GET['uid'] && ($_G['setting']['need_avatar'] || $_G['setting']['need_email'] || $_G['setting']['need_friendnum'])) {
        $message=odz_lang('postperm_login_nopermission');
        if($server==1){
            showmessage('postperm_login_nopermission');
        }else{
            echo json_encode(odz_encode(array('status'=>3,'message'=>$message)));exit;
        }
}
$idtype = empty($_GET['idtype']) ? 'tid' : $_GET['idtype'];
$id = empty($_GET['id']) ? 0 : intval($_GET['id']);//贴子id
$server= empty($_GET['server']) ? 0 : intval($_GET['server']);//数据段
$uid= empty($_GET['uid']) ? 0 : intval($_GET['uid']);//数据段
if(!$idtype||!$id||$idtype=='') {
    if($server==1){
        showmessage('praise_cannot_praise');
    }else{
        $message=odz_lang('praise_cannot_praise');
        echo json_encode(odz_encode(array('status'=>3,'message'=>$message)));exit;
    }
}
if($uid){
    $_G['uid']= $uid;
    $space = getuserbyuid($_G['uid']);
    $_G['username']=$space['username'];
};
if(!$_G['uid']) {
    if($server==1){
        showmessage('user_not_login');
    }else{
        $message=odz_lang('user_not_login');
        echo json_encode(odz_encode(array('status'=>3,'message'=>$message)));exit;
    }
}
checklowerlimit('post', 0, 1, $_G['forum']['fid']);
//数据校对
$total_list= C::t('home_praise')->fetch_all_by_id_idtype($id,$idtype);
$total=count($total_list);

DB::query("UPDATE ".DB::table('home_praise_total')." SET num=$total  WHERE tid=%d and idtype=%s ", array($id,$idtype));
if($_GET['type'] == 'delete') {
        $thevalue  =  C::t('home_praise')->fetch_by_id_idtype($id,$idtype,$_G['uid']);
        if(empty($thevalue) || $thevalue['uid'] != $_G['uid']) {
            $message=odz_lang('praise_does_not_exist');
            if($server==1){
                showmessage('praise_does_not_exist');
            }else{
                echo json_encode(odz_encode(array('status'=>3,'message'=>$message)));exit;
            }
        }
        if(C::t('home_praise')->delete_by_id_idtype($id,$idtype,$_G['uid'])){
            if($idtype=='pid'){
              $thapost = C::t('forum_post')->fetch(0,$id);
            }else{
              $thapost = C::t('forum_post')->fetch_threadpost_by_tid_invisible($id, 0);
            }
            if($thapost['fid']==$_G['mocuz_config']['share_fid']){
                $rews= C::t('mocuz_push_message')->delete_by_id_idtype($id,$idtype,$_G['username']);
            }
            DB::query("UPDATE ".DB::table('home_praise_total')." SET num=(num - 1)  WHERE tid=%d and idtype=%s ", array($id,$idtype));
        }
        $praisenum=DB::result_first('SELECT num FROM '.DB::table('home_praise_total')." WHERE idtype = '$idtype' and tid= '$id'");
        $praisenum=$praisenum?$praisenum:0;
        if($server==1){
            odz_success('praise_delete_succeed');
        }else{
            echo json_encode(odz_encode(array('status'=>2,'praisenum'=>$praisenum)));exit;
        }
} else {
    // 新注册用户权限验证
	cknewuser();

    // 判断是否重复点赞
	$fav = C::t('home_praise')->fetch_by_id_idtype($id, $idtype, $_G['uid']);
	if($fav){
        if($server==1){
            showmessage('praise_repeat');
        }else{
            $message=odz_lang('praise_repeat');
            echo json_encode(odz_encode(array('status'=>3,'message'=>$message)));exit;
        }
	}

	$description = '';
	$description_show = nl2br($description);
	$fav_count = C::t('home_praise')->count_by_id_idtype($id, $idtype);
    $arr = array(
        'uid' => intval($_G['uid']),
        'idtype' => $idtype,
        'id' => $id,
        'spaceuid' => $spaceuid,
        'title' => getstr($title, 255),
        'dateline' => TIMESTAMP
    );
    $praid = C::t('home_praise')->insert($arr, true);
    if($praid){
        $praise_total_id=DB::query("UPDATE ".DB::table('home_praise_total')." SET num=(num + 1)  WHERE tid=%d AND idtype = '$idtype'", array($id));
        if(!$praise_total_id){
            $arr1 = array(
                'idtype' => $idtype,
                'tid' => $id,
                'num' => 1,
            );
            C::t('home_praise_total')->insert($arr1, true);
        }
    }
    //回复推送
    require 'includes/functions/function_push.php';
    push_reply($id,$idtype,$praid);
    if($_G['setting']['cloud_status']) {
        $praiseService = Cloud::loadClass('Service_Client_Favorite');
        $praiseService->add($arr['uid'], $praid, $arr['id'], $arr['idtype'], $arr['title'], $arr['description'], TIMESTAMP);
    }
    C::t('forum_thread')->increase($id, array('pratimes'=>1));
    require_once libfile('function/forum');
    update_threadpartake($id);
    $praisenum=DB::result_first('SELECT num FROM '.DB::table('home_praise_total')." WHERE idtype = '$idtype' and tid= '$id'");
    $praisenum=$praisenum?$praisenum:0;
    if($server==1){
        $result = array('praid'=>$praid);
        odz_result($result, 'praise_do_success');
    }else{
        echo json_encode(odz_encode(array('status'=>1,'praisenum'=>$praisenum)));exit;
    }
}
?>
