<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: function_comment.php 20630 2011-03-01 02:45:58Z congyushuai $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

function isblacklist($touid) {
	global $_G;

	return getcount('home_blacklist', array('uid'=>$touid, 'buid'=>$_G['uid']));
}
function hot_update($idtype, $id, $hotuser) {
	global $_G;

	$hotusers = empty($hotuser)?array():explode(',', $hotuser);
	if($hotusers && in_array($_G['uid'], $hotusers)) {
		return false;
	} else {
		$hotusers[] = $_G['uid'];
		$hotuser = implode(',', $hotusers);
	}
	$hotuser = daddslashes($hotuser);
	$newhot = count($hotusers)+1;
	if($newhot == $_G['setting']['feedhotmin']) {
		$tablename = gettablebyidtype($idtype);
		if($tablename) {
			$query = DB::query("SELECT uid FROM ".DB::table($tablename)." WHERE $idtype='$id'");
			$item = DB::fetch($query);
			updatecreditbyaction('hotinfo', $item['uid']);
		}
	}

	switch ($idtype) {
		case 'blogid':
			DB::query("UPDATE ".DB::table('home_blogfield')." SET hotuser='$hotuser' WHERE blogid='$id'");
			DB::query("UPDATE ".DB::table('home_blog')." SET hot=hot+1 WHERE blogid='$id'");
			break;
		case 'picid':
			DB::query("REPLACE INTO ".DB::table('home_picfield')." (picid, hotuser) VALUES ('$id', '$hotuser')");
			DB::query("UPDATE ".DB::table('home_pic')." SET hot=hot+1 WHERE picid='$id'");
			break;
		case 'sid':
			DB::query("UPDATE ".DB::table('home_share')." SET hot=hot+1, hotuser='$hotuser' WHERE sid='$id'");
			break;
		default:
			return false;
	}
	$query = DB::query("SELECT feedid, friend FROM ".DB::table('home_feed')." WHERE id='$id' AND idtype='$idtype'");
	if($feed = DB::fetch($query)) {
		if(empty($feed['friend'])) {
			DB::query("UPDATE ".DB::table('home_feed')." SET hot=hot+1 WHERE feedid='$feed[feedid]'");
		}
	} elseif($idtype == 'picid') {
		require_once libfile('function/feed');
		feed_publish($id, $idtype);
	}

	return true;
}
function interval_check($type) {
	global $_G;

	$waittime = 0;
	if(checkperm('disablepostctrl')) {
		return $waittime;
	}
	if($_G['setting']['floodctrl']) {
		space_merge($_G['member'], 'status');
		getuserprofile('lastpost');
		$waittime = $_G['setting']['floodctrl'] - ($_G['timestamp'] - $_G['member']['lastpost']);
	}
	return $waittime;
}

function add_comment($message, $id, $idtype, $cid = 0) {
	global $_G, $bbcode;

	$summay = getstr($message, 150, 1, 1, 0, -1);


	$comment = array();
	if($cid) {
		$query = DB::query("SELECT * FROM ".DB::table('dzz_comment')." WHERE cid='$cid' AND id='$id' AND idtype='$idtype'");
		$comment = DB::fetch($query);
		
		if($comment && $comment['authorid'] != $_G['uid']) {
			$comment['message'] = preg_replace("/\<div class=\"quote\"\>\<blockquote\>.*?\<\/blockquote\>\<\/div\>/is", '', $comment['message']);
			$comment['message'] = $bbcode->html2bbcode($comment['message']);
		} else {
			$comment = array();
		}
		
	}

	$hotarr = array();
	$stattype = '';
	$tospace = $pic = $blog = $album = $share = $poll = array();

	/*switch($idtype) {
		
		case 'appid':
			$query = DB::query("SELECT * FROM ".DB::table('dzz_apps')." WHERE appid='$id'");
			$app = DB::fetch($query);
			if(empty($app)) {
				showmessage('app_does_not_exist');
			}
			$tospace = getspace($app['uid']);
			$stattype = 'appcomment';
			break;
		default:
			showmessage('non_normal_operation');
			break;
	}*/
	
	if(isblacklist($tospace['uid'])) {
		showmessage('is_blacklist');
	}

	/*if($hotarr && $tospace['uid'] != $_G['uid']) {
		hot_update($hotarr[0], $hotarr[1], $hotarr[2]);
	}
*/
	/*$fs = array();
	$fs['icon'] = 'comment';
	$fs['target_ids'] = '';
	$fs['friend'] = '';
	$fs['body_template'] = '';
	$fs['body_data'] = array();
	$fs['body_general'] = '';
	$fs['images'] = array();
	$fs['image_links'] = array();*/

	switch ($idtype) {
	
		case 'appid':
			if(intval($_POST['pcid'])==0) DB::query("UPDATE ".DB::table('dzz_apps')." SET replynum=replynum+1 WHERE appid='$id'");
			//$fs['title_template'] = 'feed_comment_app';
			//$fs['title_data'] = array('app'=>"<a href=\"app.php?mod=app&op=view&appid=$id\">$app[appname]</a>");
			break;
		case 'lid':
			if(intval($_POST['pcid'])==0) DB::query("UPDATE ".DB::table('dzz_link')." SET replynum=replynum+1 WHERE lid='$id'");
			//$fs['title_template'] = 'feed_comment_app';
			//$fs['title_data'] = array('app'=>"<a href=\"app.php?mod=app&op=view&appid=$id\">$app[appname]</a>");
			break;
		case 'vid':
			if(intval($_POST['pcid'])==0) DB::query("UPDATE ".DB::table('dzz_video')." SET replynum=replynum+1 WHERE vid='$id'");
			//$fs['title_template'] = 'feed_comment_app';
			//$fs['title_data'] = array('app'=>"<a href=\"app.php?mod=app&op=view&appid=$id\">$app[appname]</a>");
			break;
		case 'mid':
			if(intval($_POST['pcid'])==0) DB::query("UPDATE ".DB::table('dzz_music')." SET replynum=replynum+1 WHERE mid='$id'");
			//$fs['title_template'] = 'feed_comment_app';
			//$fs['title_data'] = array('app'=>"<a href=\"app.php?mod=app&op=view&appid=$id\">$app[appname]</a>");
			break;
		case 'qid':
			if(intval($_POST['pcid'])==0) DB::query("UPDATE ".DB::table('dzz_attach')." SET replynum=replynum+1 WHERE qid='$id'");
			//$fs['title_template'] = 'feed_comment_app';
			//$fs['title_data'] = array('app'=>"<a href=\"app.php?mod=app&op=view&appid=$id\">$app[appname]</a>");
			break;
		case 'picid':
			if(intval($_POST['pcid'])==0) DB::query("UPDATE ".DB::table('dzz_image')." SET replynum=replynum+1 WHERE picid='$id'");
			//$fs['title_template'] = 'feed_comment_app';
			//$fs['title_data'] = array('app'=>"<a href=\"app.php?mod=app&op=view&appid=$id\">$app[appname]</a>");
			break;
	}

	$message = censor($message);
	if(censormod($message)) {
		$comment_status = 1;
	} else {
		$comment_status = 0;
	}

	$setarr = array(
		'uid' => $tospace['uid'],
		'id' => $id,
		'idtype' => $idtype,
		'authorid' => $_G['uid'],
		'author' => $_G['username'],
		'dateline' => $_G['timestamp'],
		'message' => $message,
		'ip' => $_G['clientip'],
		'status' => $comment_status,
		'pcid' => intval($_POST['pcid']),
	);
	$cid = DB::insert('dzz_comment', $setarr, 1);

	$action = 'comment';
	$becomment = 'getcomment';
	$note = $q_note = '';
	$note_values = $q_values = array();

	/*switch ($idtype) {
		
		case 'appid':
			$n_url = "app.php?mod=app&op=view&appid=$id&cid=$cid";
			$note_type = 'appcomment';
			$note = 'app_comment';
			$note_values = array('url'=>$n_url, 'subject'=>$movie['name']);
			$q_note = 'app_comment_reply';
			$q_values = array('url'=>$n_url);
			$becomment = '';
			$msg = 'do_success';
			$magvalues = array();

			break;
	}*/

/*	if(empty($comment)) {

		if($tospace['uid'] != $_G['uid']) {
			if(ckprivacy('comment', 'feed')) {
				require_once libfile('function/feed');
				$fs['title_data']['hash_data'] = "{$idtype}{$id}";
				feed_add($fs['icon'], $fs['title_template'], $fs['title_data'], $fs['body_template'], $fs['body_data'], $fs['body_general'],$fs['images'], $fs['image_links'], $fs['target_ids'], $fs['friend']);
			}

			$note_values['from_id'] = $id;
			$note_values['from_idtype'] = $idtype;
			$note_values['url'] .= "&goto=new#comment_{$cid}_li";

			if($_POST['idtype']!='appid') notification_add($tospace['uid'], $note_type, $note, $note_values);
		}

	} elseif($comment['authorid'] != $_G['uid'] && $_POST['idtype']!='appid') {
		notification_add($comment['authorid'], $note_type, $q_note, $q_values);
	}

	if($comment_status == 1) {
		updatemoderate($idtype.'_cid', $cid);
		manage_addnotify('verifycommontes');
	}*/
	
	/*if($tospace['uid'] != $_G['uid']) {
		$needle = $id;
		if($idtype != 'uid') {
			$needle = $idtype.$id;
		} else {
			$needle = $tospace['uid'];
		}
		updatecreditbyaction($action, 0, array(), $needle);
		if($becomment) {
			if($idtype == 'uid') {
				$needle = $_G['uid'];
			}
			updatecreditbyaction($becomment, $tospace['uid'], array(), $needle);
		}
	}*/
	$msg = 'do_success';
	DB::update('common_member_status', array('lastpost' => $_G['timestamp']), array('uid' => $_G['uid']));
	$magvalues['cid'] = $cid;
	$magvalues['pcid'] = intval($_POST['pcid']);
	$magvalues['id']=$id;
	$magvalues['idtype']=$idtype;
	return array('cid' => $cid, 'msg' => $msg, 'magvalues' => $magvalues);
}

function dzz_deletecomments($cids) {
	global $_G;

	$blognums = $newcids = $dels = $counts =$imagenums = array();
	$allowmanage = checkperm('managecomment');

	$query = DB::query("SELECT * FROM ".DB::table('dzz_comment')." WHERE cid IN (".dimplode($cids).")");
	while ($value = DB::fetch($query)) {
		if($allowmanage || $value['authorid'] == $_G['uid'] || $value['uid'] == $_G['uid']) {
			$dels[] = $value;
			$newcids[] = $value['cid'];
			if($value['authorid'] != $_G['uid'] && $value['uid'] != $_G['uid']) {
				$counts[$value['authorid']]['coef'] -= 1;
			}
			if($value['idtype'] == 'blogid') {
				$blognums[$value['id']]++;
			}
			if($value['idtype'] == 'appid' && $value['pcid']==0) {
				$appnums[$value['id']]++;
			}
			if($value['idtype'] == 'lid' && $value['pcid']==0) {
				$linknums[$value['id']]++;
			}
			if($value['idtype'] == 'mid' && $value['pcid']==0) {
				$musicnums[$value['id']]++;
			}
			if($value['idtype'] == 'vid' && $value['pcid']==0) {
				$videonums[$value['id']]++;
			}
			if($value['idtype'] == 'aid' && $value['pcid']==0) {
				$attachnums[$value['id']]++;
			}
			if($value['idtype'] == 'picid' && $value['pcid']==0) {
				$imagenums[$value['id']]++;
			}
		}
	}

	if(empty($dels)) return array();

	DB::query("DELETE FROM ".DB::table('dzz_comment')." WHERE cid IN (".dimplode($newcids).")");

	if($counts) {
		foreach ($counts as $uid => $setarr) {
			batchupdatecredit('comment', $uid, array(), $setarr['coef']);
		}
	}
	if($appnums) {
		$nums = renum($appnums);
		foreach ($nums[0] as $num) {
			DB::query("UPDATE ".DB::table('dzz_apps')." SET replynum=replynum-$num WHERE appid IN (".dimplode($nums[1][$num]).")");
		}
	}
	if($linknums) {
		$nums = renum($linknums);
		foreach ($nums[0] as $num) {
			DB::query("UPDATE ".DB::table('dzz_link')." SET replynum=replynum-$num WHERE lid IN (".dimplode($nums[1][$num]).")");
		}
	}
	if($musicnums) {
		$nums = renum($musicnums);
		foreach ($nums[0] as $num) {
			DB::query("UPDATE ".DB::table('dzz_music')." SET replynum=replynum-$num WHERE mid IN (".dimplode($nums[1][$num]).")");
		}
	}
	if($videonums) {
		$nums = renum($videonums);
		foreach ($nums[0] as $num) {
			DB::query("UPDATE ".DB::table('dzz_video')." SET replynum=replynum-$num WHERE vid IN (".dimplode($nums[1][$num]).")");
		}
	}
	if($attachnums) {
		$nums = renum($attachnums);
		foreach ($nums[0] as $num) {
			DB::query("UPDATE ".DB::table('dzz_attach')." SET replynum=replynum-$num WHERE qid IN (".dimplode($nums[1][$num]).")");
		}
	}
	if($imagenums) {
		$nums = renum($imagenums);
		foreach ($nums[0] as $num) {
			DB::query("UPDATE ".DB::table('dzz_image')." SET replynum=replynum-$num WHERE picid IN (".dimplode($nums[1][$num]).")");
		}
	}
	return $dels;
}
?>