<?php
/*
	copyright:ec1022（东海）@	
	update date: 2010-08-26
详细更新内容和使用说明请看：

*/

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

global $_G;
loadcache('plugin');







$x_config=$_G['cache']['plugin']['reply_robot'];
if($x_config['en_autor'])
{
$x_credittype=$x_config['credittype']?$x_config['credittype']:1;
$x_creditnum=$x_config['creditnum']?$x_config['creditnum']:1;
$x_t_num=($x_config['t_nums_min']&&$x_config['t_nums_max'])?rand($x_config['t_nums_min'],$x_config['t_nums_max']):1; //随机回复的主题数量10-50之间任意数字，可以任意修改
$x_t_time=$x_config['time_var']?$x_config['time_var']:1800; //设置回复时间随机变动范围,单位：秒,表示随机发回时间为当前时间至 秒前,设为0表示不启用随机回复时间
$x_r_sign=$x_config['r_sign']?$x_config['r_sign']:1;  //是否启用签名,1为启用,0为禁用.
$x_r_ubboff=$x_config['r_ubboff']?$x_config['r_ubboff']:0; //是否启用UBB代码,0为启用,1为禁用.
$x_r_num=($x_config['r_num_min']&&$x_config['r_num_max'])?rand($x_config['r_num_min'],$x_config['r_num_max']):1;  //每个主题回复条数
$x_u_num=$x_t_num*$x_r_num;
$t_views=$x_config['t_views']?rand(1,$x_config['t_views']):1;
$x_timestamp=TIMESTAMP;
if($x_config['users_set'])
{
$x_u_limited=" where uid IN (".$x_config['users_set'].")";
}
else
{
$x_limitusergroup=unserialize($x_config['u_groups']);
foreach ($x_limitusergroup as $x_value){
$x_ug_limited = $x_ug_limited ? $x_ug_limited.','.$x_value : $x_value;
}
$x_u_limited=$x_ug_limited?" where groupid IN (".$x_ug_limited.")":"";
}	

$x_members=array();
$x_query=DB::query("SELECT uid,username FROM ".DB::table('common_member')." {$x_u_limited} order by rand() limit ".$x_u_num);
while($x_mem=DB::fetch($x_query)){
	$x_members[$x_mem['uid']]=addslashes($x_mem['username']);
}
$t_date_limit="";
//设置回复主题时间限制
if($x_config['t_date_after'])
{
		$t_date_limit=$x_config['t_date_before']?" AND (dateline>".($x_timestamp-$x_config['t_date_after']*86400)." or dateline<".($x_timestamp-$x_config['t_date_before']*86400).")":" AND dateline>".($x_timestamp-$x_config['t_date_after']*86400);
}
else
{
	$t_date_limit=$x_config['t_date_before']?" AND dateline<".($x_timestamp-$x_config['t_date_before']*86400):"";
}

//设置回复数超过限制的主题不回复
$t_r_limit=$x_config['t_r_limit']?" AND replies<".$x_config['t_r_limit']:"";

$x_limitforum=unserialize($x_config['enforums']);
foreach ($x_limitforum as $x_value){
$x_forum_limited = $x_forum_limited ? $x_forum_limited.','.$x_value : $x_value;
}
$x_forum_limited = $x_forum_limited ? " AND fid IN (".$x_forum_limited.")":"";

//设置回帖优先情况
$db_orderby="";

switch ($x_config['orderby']) {
  case '1':
    $db_orderby=" order by rand()";
    break;
  case '2':
    $db_orderby=" order by tid desc";
    break;
  case '3':
    $db_orderby=" order by replies asc";
    break;
  case '4':
    $db_orderby=" order by views asc";
    break;
  default:
    $db_orderby=" order by rand()";
    break;
}

$x_query=DB::query("SELECT tid,fid FROM ".DB::table('forum_thread')." where closed=0 {$t_r_limit} {$t_date_limit} {$x_forum_limited} {$db_orderby} limit ".$x_t_num);

$x_threads=array();
while($x_thr=DB::fetch($x_query)){
	$x_threads[$x_thr['tid']]=$x_thr['fid'];
}

include DISCUZ_ROOT.'./source/plugin/reply_robot/postdata.inc.php'; 

foreach($x_threads as $x_tid=>$x_fid)
{
	$x_nums=array();
	if($x_r_num>1)
	{$x_nums=array_rand($x_postdatas,$x_r_num);}
	else
	{$x_nums[]=array_rand($x_postdatas);}
	foreach($x_nums as $x_num)
	{
		$x_postdata=NULL;
		$x_postdata=addslashes($x_postdatas[$x_num]);
if($x_config['en_timeorder'])
{	
		$x_query=DB::query("SELECT dateline FROM ".DB::table('forum_post')." order by dateline desc LIMIT 1");
		$x_post=DB::fetch($x_query);		
		if($x_timestamp >$x_post['dateline'])
		{
			do{$x_timestamp=TIMESTAMP-rand(0,$x_t_time);}
			while($x_timestamp<$x_post['dateline']);
		}
}
		else
		{
			$x_timestamp=TIMESTAMP-rand(0,$x_t_time);
		}
		$x_uid=array_rand($x_members);
		$x_user=$x_members[$x_uid];
		$x_subject=NULL;
		$x_useip="202.".rand(96,184).".".rand(124,127).".".rand(9,200);

		$pid = insertpost(array('fid' => $x_fid,'tid' => $x_tid,'first' => '0','author' => $x_user,'authorid' => $x_uid,'subject' => $x_subject,'dateline' => $x_timestamp,'message' => $x_postdata,'useip' => $x_useip,'invisible' => '0','anonymous' => '0','usesig' => $x_r_sign,'htmlon' => '0','bbcodeoff' => $x_r_ubboff,'smileyoff' => '0','parseurloff' => '0','attachment' => '0',));

		$x_query=DB::query("SELECT tid,subject FROM ".DB::table('forum_thread')." where tid='$x_tid'");
		$x_thread=DB::fetch($x_query);
		$x_lastpost = "$x_thread[tid]\t".addslashes($x_thread['subject'])."\t$x_timestamp\t$x_user";

	
		updatemembercount($x_uid, array($x_credittype => $x_creditnum));
		DB::query("UPDATE ".DB::table('common_member_count')." SET posts=posts+1 WHERE uid='$x_uid'", 'UNBUFFERED');
		DB::query("UPDATE ".DB::table('common_member_status')." SET lastip='$x_useip',lastvisit='$x_timestamp',lastactivity='$x_timestamp',lastpost='$x_timestamp' WHERE uid='$x_uid'", 'UNBUFFERED');
		DB::query("UPDATE ".DB::table('forum_forum')." SET posts=posts+1,todayposts=todayposts+1,lastpost='$x_lastpost' WHERE fid='$x_fid'", 'UNBUFFERED');
		DB::query("UPDATE ".DB::table('forum_thread')." SET replies=replies+1,views=views+'$t_views',lastposter='$x_user', lastpost='$x_timestamp' WHERE tid='$x_tid'", 'UNBUFFERED');
	}
}

}
?>