<?php
define('THIS_SCRIPT', 'bank_msg');
require_once 'global.php';
require_once INC_PATH.'dv_code.php';
require_once('./bank_inc.php');
error_reporting(7);

$lang = load_lang($lang, 'bank_msg' );
$lang['tpl.smsg.str6'] = str_replace('{$msg1money]}',$idv_trasvar['msg1money'],$lang['tpl.smsg.str6']);

$hometitle = str_replace('{$MemberName}',$membername,$lang['HeaderTitle2']);
$stats = $lang['template.strings4'];
PageLoad();
function PageLoad(){
	global $db,$lang,$userinfo,$dv,$userid,$ActInfo,$istoptable,$_POST;
	if( intval($userid) == 0 ){
		$istoptable = 1;
		head();
		showmsg($lang['MustLogin']);
		exit;
	}
	$istoptable = 0;

	switch($GLOBALS['action']){
		case 'new':
		case 'send':
			sendmsg();
			break;
	}
}//end function PageLoad

//发送信息
function sendmsg(){
	global $db,$lang,$userinfo,$dv,$userid,$ActInfo,$istoptable,$_POST,$idv_trasvar;
	$action = $GLOBALS['action'];


	$Friend_options =& OPTION_Friend();
	$mo_send = '';
	$title = preg_replace("/\<(.+?)\>/is", "", $title);
	$title = " value=\"{$title}\" ";
	$Sms_senduser = $groupsetting[33];
	$Sms_maxbody = $groupsetting[34];
	$reaction = trim($GLOBALS['reaction']);
	$chatloglist = EncodeJS($chatloglist);
	$myname = $membername;
	
	if(!isset($_POST['touser'])||!isset($_POST['message'])||!isset($_POST['title'])){
		$Sms_SendAct = 'bank_msg.php?action=send';
		head();
		require printout('bank_msg');
		footer();
		exit;
	}else{
		$dv_touser = Html2Ubb($_POST['touser']);
		$dv_title = Html2Ubb($_POST['title']);
		$dv_message = Html2Ubb($_POST['message']);
		$dv_touser = str_replace("'","",$dv_touser);
		$dv_title = str_replace("'","",$dv_title);
		$dv_message = str_replace("'","''",$dv_message);				
		$dv_touser = explode(',',trim($dv_touser));
		$dv_c = count($dv_touser);
		$i=0;
		$money = 0;
		$dv_money = 0;
		$nouser = '';
		$dv_sendc = 0;
		$stmt = "SELECT money FROM {$dv}bankstatus WHERE userid=".$userinfo['userid']." and bankstatus=1 limit 1";
		if( $query = $db->query($stmt) ){
			$row = $db->fetch_array($query);
			$money = floatval($row['money']);
			
		}		
		//当前金币足够发送短信条数
		$dv_ablesec = intval($money/$idv_trasvar['msg1money']);
		$dv_ablesec = ($money%$idv_trasvar['msg1money'])!=0?($dv_ablesec-1):$dv_ablesec;
		
		for(;$i<$dv_c;$i++){
			if($dv_ablesec == $i)
			{
				break;
			}
			$ishased = $db->scalar("SELECT count(userid) FROM {$dv}user where username='$dv_touser[$i]'");
			if( 0 != $ishased){
				$stmt = "INSERT INTO {$dv}message(sender, incept, title, content, flag, sendtime, delr, dels, issend) VALUES ('".$userinfo['username']."','$dv_touser[$i]','$dv_title','$dv_message',0,'".time()."',0,0,1)";
				$db->query($stmt);
				if(strlen($users)==0){
					$users = $dv_touser[$i];
				}else
					strlen($dv_touser[$i])>0 AND $users .= ', '.$dv_touser[$i];
				$dv_money += $idv_trasvar['msg1money'];
				$dv_sendc ++;
			}else{
				if(strlen($nouser)==0){
					$nouser = $dv_touser[$i];
				}else 
				{
					strlen($dv_touser[$i])>0 AND $nouser .= ','.$dv_touser[$i];
				}
			}
		}
		if( 0 != $dv_sendc ){
			$db->query("update {$dv}bankstatus set  money=money-$dv_money WHERE userid=".$userinfo['userid']."");
			$db->query("insert into {$dv}banklog set userid=".$userinfo['userid'].",username='".$userinfo['username']."',acttime=now(),action='".$lang['tpl.smsg.str13']."'");
		}
		$lang['tpl.smsg.str11'] = str_replace('$money',$dv_money,$lang['tpl.smsg.str11']);
		$lang['tpl.smsg.str11'] = str_replace('$count',$dv_sendc,$lang['tpl.smsg.str11']);
		$lang['tpl.smsg.str11'] = str_replace('$users',(strlen($users)>0?$users:'""'),$lang['tpl.smsg.str11']);
		if(strlen($nouser)>0){
			$lang['tpl.smsg.str12'] = str_replace('$nouser',$nouser,$lang['tpl.smsg.str12']);
			$lang['tpl.smsg.str11'] .= $lang['tpl.smsg.str12'];
		}
		head();
		showmsg($lang['tpl.smsg.str11']);
	}
}//end function sendmsg

//用户好友下拉名单
function &OPTION_Friend(){
	global $dv,$db,$userid;
	//$returnVal = '';
	$returnVal = array();
	if( $query = $db->query("SELECT f_friend FROM {$dv}friend WHERE f_userid={$userid} ORDER BY F_addtime DESC") ){
		while( $result =& $db->fetch_array($query) ){
			$returnVal[] =& $result;
		}
		$db->free_result($query);
	}
	return $returnVal;
}//end function OPTION_Friend

function EncodeJS($str){
	if( $str == '' ) return '';
	return str_replace("\r",'',str_replace("\r\n","\n",str_replace("'","''",str_replace('\\','\\',str_replace("\n",'',$str)))));
}//end function EncodeJS


//发帖时用，为了减少入库量
function &Html2Ubb($str){
	if( $str != '' && !is_null($str) ){
		$str = preg_replace('/(<br>)/i','[br]',$str);
		$str = str_replace('[br]','<br>',$str);

		$arrPatterns = array( '/(&nbsp;)/i' ,'/(>)('."\n".')(<)/i' ,'/(>)('."\n\n".')(<)/i' ,'/(<p>)/i' ,'/(<\\/p>)/i' ,'/(<STRONG>)/i' ,'/(<\\/STRONG>)/i' ,'/(<TBODY>)/i' ,'/(<\\/TBODY>)/i' );
		$arrReplacements = array( chr(9) ,'$1$3' ,'$1$3' ,'' ,chr(13).chr(10) ,'<b>' ,'</b>' ,'' ,'' );
		return htmlspecialchars(preg_replace($arrPatterns,$arrReplacements,$str));
	} else{
		return '';
	}
}//end function

?>