<?php
define('THIS_SCRIPT', 'forum_message');
include_once 'global.php';
$lang = load_lang($lang,'forum_message');
checklogin();
header("content-Type: text/html; charset=".$charset);
head();
if ( strpos($sysinfo['flag'],'6,') === false ){
	show_msg($lang['readme_tit'],$lang['error.str1'],0,'','','','');
	exit;
}
PageLoad();
footer();
function Pageload(){
	global $dv,$db,$lang,$action,$isshow,$pic_info1,$pic_width,$pic_info2,$pic_height,$num,$sysusername,$back_url;
		$back_url='javascript:void(window.history.back())';
		switch ($action){
		case "datdel":
			dat_del();
		break;
		case "all_del":
			all_del();
		break;
		case "delchk":
		    del_like();
		break;
		case "add":
			send_msg();
		break;
	}
include printout('forum_message');
}
function dat_del(){
	global $dv,$db,$lang,$action,$username,$back_url;
	if(trim($username)==""){
		show_msg($lang['error.info'],$lang['error.info1'],'0','','',$back_url,$lang['re.back']);
	exit;
	}
	else{
		$username=str_replace(' ','',$username);
		$c_sql="select count(1) from {$dv}message where incept='".$username."'";
		$c_result=$db->scalar($c_sql);//统计删除短信数；
		if($c_result<0){
			show_msg($lang['error.info'],$lang['error.info2'],'0','','',$back_url,$lang['re.back']);
		exit;
		
		}
		else{
			$sql="delete from {$dv}message where incept='".$username."'";
			$result=$db->query($sql);
			if($result){
				show_msg($lang['sys.info'],$lang['succeed.info1'].$c_result.$lang['succeed.info2'],'1','','',$back_url,$lang['re.back']);
			exit;		
			}
			else{
				show_msg($lang['error.info'],$lang['error.info3'],'0','','',$back_url,$lang['re.back']);
			exit;
			}
		}
	}
}
function all_del(){
	global $dv,$db,$lang,$action,$delDate,$isread,$back_url;
	if($isread=="yes")
		$str=" and flag=0 order by id";
	else 
		$str=" and flag=1 order by id";
	switch (intval($delDate)){
		case 7:
			$endsendtime=dv_gmtimestamp(gmdate('Y-m-d 00:00:00'))-(7*24*3600);
			$count="select count(1) from {$dv}message where sendtime<'".$endsendtime."'{$str}";
			$sql="delete from {$dv}message where sendtime<'".$endsendtime."'{$str}";
		break;
		case 30:
			$endsendtime=dv_gmtimestamp(gmdate('Y-m-d 00:00:00'))-(30*24*3600);
			$count="select count(1) from {$dv}message where sendtime<'".$endsendtime."'{$str}";
			$sql="delete from {$dv}message where sendtime<'".$endsendtime."'{$str}";
		break;
		case 60:
			$endsendtime=dv_gmtimestamp(gmdate('Y-m-d 00:00:00'))-(60*24*3600);
			$count="select count(1) from {$dv}message where sendtime<'".$endsendtime."'{$str}";
			$sql="delete from {$dv}message where sendtime<'".$endsendtime."'{$str}";
		break;
		case 180:
			$endsendtime=dv_gmtimestamp(gmdate('Y-m-d 00:00:00'))-(180*24*3600);
			$count="select count(1) from {$dv}message where sendtime<'".$endsendtime."'{$str}";
			$sql="delete from {$dv}message where sendtime<'".$endsendtime."'{$str}";
		break;
		default:
			$count="select count(1) from {$dv}message";
			$sql="delete from {$dv}message";
		break;
	}
	$c_count=$db->scalar($count);
	$result=$db->query($sql);
	if($result){
		show_msg($lang['sys.info'],$lang['succeed.info1'].$c_count.$lang['succeed.info2'],'1','','',$back_url,$lang['re.back']);
	exit;	
	}
	else{
		show_msg($lang['error.info'],$lang['error.info3'],'0','','',$back_url,$lang['re.back']);
	exit;
	}
}
function del_like(){
	global $dv,$db,$lang,$action,$keyword,$selaction,$back_url;
	$keyword=str_replace(' ','',$keyword);
	if($keyword==""){
		show_msg($lang['error.info'],$lang['error.info4'],'0','','',$back_url,$lang['re.back']);
	exit;
	
	}
	else{
		$s_str=" where $selaction like '%$keyword%'";
		$sql="delete from {$dv}message{$s_str}";
		$c_sql="select count(1) from {$dv}message{$s_str}";
		$c_count=$db->scalar($c_sql);
		$result=$db->query($sql);
		if($result){
			show_msg($lang['sys.info'],$lang['succeed.info1'].$c_count.$lang['succeed.info2'],'1','','',$back_url,$lang['re.back']);
			exit;	
		}
		else{
			show_msg($lang['error.info'],$lang['error.info3'],'0','','',$back_url,$lang['re.back']);
		exit;
		}
	}
}
function send_msg(){
	global $db,$dv,$lang,$action,$title,$stype,$message,$isshow,$pic_info1,$pic_width,$pic_info2,$pic_height,$num,$back_url,$sysusername;
	$lost_count=0;
	$title=str_replace(' ','',$title);
	if($title=="" or $message==""){
		show_msg($lang['error.info'],$lang['error.info5'],'0','','',$back_url,$lang['re.back']);
	exit;
	}
	else{
		if(strlen($message)>255){
			show_msg($lang['error.info'],$lang['error.info6'],'0','','',$back_url,$lang['re.back']);
		exit;
		}
		switch (intval($stype)){
			case 1:
				$count_sql="select count(1) from {$dv}online where userid>0";
				$s_sql="select username from {$dv}online where userid>0";
			break;
			case 2:
				$count_sql="select count(1) from {$dv}user where usergroupid=8";
				$s_sql="select username from {$dv}user where usergroupid=8 order by userid desc";
			break;
			case 3:
				$count_sql="select count(1) from {$dv}user where usergroupid=3";
				$s_sql="select username from {$dv}user where usergroupid=3 order by userid desc";
			break;
			case 4:
				$count_sql="select count(1) from {$dv}user where usergroupid=1";
				$s_sql="select username from {$dv}user where usergroupid=1 order by userid desc";
			break;
			case 5:
				$count_sql="select count(1) from {$dv}user where usergroupid<4";
				$s_sql="select username from {$dv}user where usergroupid<4 order by userid desc";
			break;
			case 6:
				$count_sql="select count(1) from {$dv}user";
				$s_sql="select username from {$dv}user order by userid desc";
			break;
			case 7:
				$count_sql="select count(1) from {$dv}user where usergroupid=2";
				$s_sql="select username from {$dv}user where usergroupid=2 order by userid desc";
			break;
			default://自定义用户组；
				$count_sql="select count(1) from {$dv}user where usergroupid={$stype}";
				$s_sql="select username from {$dv}user where '".$stype."' order by userid desc";
			break;
		}
		$record_count=$db->scalar($count_sql);
		$s_result=$db->query($s_sql);
		$userid=$sysusername;
		$now=time();//strtotime('now');
		while($s_val=$db->fetch_array($s_result)){
			$insert_resule=$db->query("insert into {$dv}message (incept,sender,title,content,sendtime,flag,issend) values ('".addslashes($s_val['username'])."','".$userid."','".$title."','".$message."','".$now."',0,1)");
			$num+=1;
			if(!$insert_result){
				continue;
				$lost_count+=1;//记录失效的次数;
			}
		}
		if(intval($isshow)==1){
			$pic_width="100%";
			$pic_info2="100%";
			$pic_height="1";
		//显示进度条信息；
		}
		else{
			if($insert_resule){
				$succeed_count=$record_count-$lost_count;//记录发送成功的次数;
				$succeed_effic=$succeed_count/$record_count*100;//成功率;
				$lost_effic=$lost_count/$record_count*100;	show_msg($lang['sys.info'],$lang['send.info1'].$record_count.$lang['send.info2'].$lang['send.info3'].$lang['send.info5'].$succeed_count.$lang['send.info6'].$succeed_effic.$lang['send.info8'].$lang['send.info7'].$lost_count.$lang['send.info6'].$lost_effic.$lang['send.info8'],'1','','','javascript:void(window.history.back())',$lang['re.back']);
			exit;	
			}
			else{
				show_msg($lang['error.info'],$lang['error.info3'],'0','','',$back_url,$lang['re.back']);
			exit;
			}
		}
	}
}
?>