<?php
define('THIS_SCRIPT', 'messanger');
require_once 'global.php';
require_once INC_PATH.'dv_code.php';
error_reporting(7);
$lang = load_lang($lang, 'messanger' );
$hometitle = str_replace('{$MemberName}',$membername,$lang['HeaderTitle2']);
$stats = $lang['template.strings4'];
PageLoad();

function PageLoad(){
	global $title,$UserToday,$ActInfo,$istoptable,$temptxt,$stmt,$groupsetting,$stats,$dv,$fsetting,$groupsetting,$membername,$userid,$lang,$db,$sms__write,$sms__reply,$sms__fw,$sms_delete,$empath,$fsetting,$URL,$master,$imgurl;
	if( intval($userid) == 0 ){
		$istoptable = 1;
		head();
		showmsg($lang['MustLogin']);
		exit;
	}
	if( $groupsetting[32] == 0 ){
		$istoptable = 1;
		head();
		showmsg($lang['template.strings33']);
		exit;
	}
	$istoptable = 0;
	$ActInfo = explode(',',$lang['template.strings62']);
	//这个变量需要重新计算
	$UserToday = array(0,0,0,0,0);//explode('|','0|0|0|0|0');

	//start common block
	require_once CACHE_PATH.'forum_pic.php';
	$empath = $_DCACHE['forum_emot'][0];
	$id = intval($GLOBALS['id']);
	if( $fsetting['msggetcode'] == 0 ){
		$getcode = '';
	} else{
		$getcode = 
		$codestr = GetCode();
	}
	$sms__write = $imgurl.$lang['template.pic5'];
	$sms__reply = $imgurl.$lang['template.pic6'];
	$sms__fw = $imgurl.$lang['template.pic7'];
	$sms_delete = $imgurl.$lang['template.pic8'];
	$sms_id = $id;
	//end common block

	switch($GLOBALS['action']){
		case 'new':
			sendmsg();
			break;
		case 'read':
			read();
			NewPassword();
			break;
		case 'outread':
			read();
			NewPassword();
			break;
		case 'newmsg':
			break;
		case 'send':
			savemsg();
			break;
		case 'fw':
			sendmsg();
			break;
		case 'edit':
			sendmsg();
			break;
		case 'savedit':
			savedit();
			break;
		case 'delet':
			DeleteMsg();
			break;
		case $ActInfo[5]:
			Delinbox();
			break;
		case $ActInfo[6]:
			AllDelinbox();
			break;
		case $ActInfo[7]:
			Deloutbox();
			break;
		case $ActInfo[8]:
			AllDeloutbox();
			break;
		case $ActInfo[9]:
			Delissend();
			break;
		case $ActInfo[10]:
			AllDelissend();
			break;
		case $ActInfo[11]:
			Delrecycle();
			break;
		case $ActInfo[12]:
			AllDelrecycle();
			break;
		default:
			head();
			showmsg($lang['template.strings51']);
			exit;
			break;
	}
}//end function PageLoad

//发送信息,回复，转发,编辑
function sendmsg(){
	global $_COOKIE,$title,$stats,$UserToday,$sms_id,$fsetting,$id,$temptxt,$gStmt,$groupsetting,$lang,$dv,$db,$userid,$membername,$sms__write,$sms__reply,$sms__fw,$sms_delete,$ActInfo,$userinfo,$master;

	if( false ){
		$istoptable = 1;
		head();
		showmsg($lang['Error.strings1']);
		exit;
	}
	$joindate = get_cookie('joindate');
	empty($joindate) AND ($joindate = gmdate('Y-m-d H:i:s', $userinfo['joindate']));
	if( $groupsetting[53] > 0 && dateDiff('s',$joindate,gmdate('Y-m-d H:i:s')) < $groupsetting[53]*60 ){
		head();
		showmsg(str_replace('{$Lim_Time}',$groupsetting[53],$lang['template.strings39']));
		exit;
	}
	if( $groupsetting[63] != 0 ){
		if( $groupsetting[63] <= $UserToday[1] ){
			head();
			showmsg(str_replace('{$smslimited}',$groupsetting[63],$lang['template.strings65']));
			exit;
		}
	}

	$action = $GLOBALS['action'];
	if( $action == 'fw' ){
		setcookie('Dvbbs','');
		$title = "FW: ";
		$stats = $ActInfo[3];//转发短信
		if( IsReadonly() && !$master ){
			head();
			showmsg($lang['Error.strings1']);
			exit;
		}
		$stmt = "SELECT id,title,content,incept,sender,sendtime FROM {$dv}message WHERE (incept='{$membername}' OR sender='{$membername}') AND  id={$id}";
		$temptxt = $lang['template.strings57'];
	} elseif( $action == 'edit' ){
		$_COOKIE['Dvbbs'] = '';
		$stats = $ActInfo[4];//修改短信
		if( IsReadonly() && !$master ){
			head();
			showmsg($lang['Error.strings1']);
			exit;
		}
		$stmt = "SELECT id,title,content,incept,sender,sendtime FROM{$dv}message WHERE sender='{$membername}' AND issend=0 AND id={$id}";
	}

	if( $id > 0 ){
		$title = 'RW:';
		$stmt = "SELECT id,title,content,incept,sender,sendtime FROM {$dv}message WHERE incept='{$membername}' AND id={$id}";
		$temptxt = $lang['template.strings56'];
	}
	$stats = $ActInfo[0];
	$action = $GLOBALS['action'];
	$touser = $db->escape_string(urldecode(trim($GLOBALS['touser'])));
	$dbResult = array();
	if( is_numeric($GLOBALS['id']) && $stmt != '' ){
		if( $query = $db->query($stmt) ){
			if( $dbResult =& $db->fetch_array($query) ){
				$db->free_result($query);
			}
		}
		if( empty($dbResult) ){
			head();
			showmsg($lang['template.strings35']);
			exit;
		}
	}
	else{
	}
	if( $GLOBALS['reaction'] == 'chatlog' ){
		$stmt = "SELECT sender,incept,title,content,sendtime FROM {$dv}message WHERE ((sender='{$membername}' AND incept='{$touser}') OR (sender='{$touser}' AND incept='{$membername}')) AND dels=0 ORDER BY sendtime DESC";
		if( $query = $db->query($stmt) ){
			while( $result =& $db->fetch_array($query) ){
				$dbResults[] =& $result;
			}
		} else{
		}
	}
	$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( $action == 'edit' ){
		$Sms_SendAct = "messanger.php?action=savedit&id={$id}";
	} else{
		$Sms_SendAct = 'messanger.php?action=send';
	}
	head();
	require printout('messanger.top');
	require printout('messanger.sendmsg');
	require printout('messanger.footer');
}//end function sendmsg

//用户好友下拉名单
function &OPTION_Friend(){
	global $dv,$db,$userid;
	$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 savemsg(){
	global $stats,$istoptable,$subtype,$_COOKIE,$_SERVER,$UserToday,$sms_id,$fsetting,$id,$temptxt,$gStmt,$groupsetting,$lang,$dv,$db,$userid,$membername,$boardid,$_DCACHE,$fsetting,$userinfo;
	if( $fsetting['msggetcode'] == 1 ){
		if( CodeIsTrue() !== true ){
			head();
			showmsg($lang['Error.strings3']);
			exit;
		}
	}
	$joindate = get_cookie('joindate');
	empty($joindate) AND ($joindate = gmdate('Y-m-d H:i:s', $userinfo['joindate']));
	if( $groupsetting[53] > 0 && dateDiff('s',$joindate,gmdate('Y-m-d H:i:s')) < $groupsetting[53]*60 ){
		head();
		showmsg(str_replace('{$Lim_Time}',$groupsetting[53],$lang['template.strings39']),0,null,false);
		exit;
	}
	if( $groupsetting[63] != 0 ){
		if( $groupsetting[63] <= $UserToday[1] ){
			head();
			showmsg(str_replace('{$smslimited}',$groupsetting[63],$lang['template.strings65']),0,null,false);
			exit;
		}
	}
	if( $fsetting['msggetcode'] == 1 ){
		if( false ){//if( CodeIsTrue() )
			head();
			showmsg(str_replace('{$URL}',$_SERVER['HTTP_REFERER'],$lang['Error.strings2']),0,null,false);
			exit;
		}
	}
	if( false && strval($_COOKIE['Dvbbs']) == strval($boardid) ){
		head();
		showmsg($lang['template.strings26'],0,null,false);
		exit;
	}
	if( $GLOBALS['touser'] == '' ){
		head();
		showmsg($lang['template.strings35'],0,null,false);
		exit;
	} else{
		$incept = explode(',',$GLOBALS['touser']);
	}
	if( $GLOBALS['title'] == '' || Dv_StrLength($GLOBALS['title']) > 50 ){
		head();
		showmsg($lang['template.strings53'],0,null,false);
		exit;
	} else{
		$title = trim($GLOBALS['title']);
	}
	if( $GLOBALS['message'] == '' || ( Dv_StrLength($GLOBALS['message']) > $groupsetting[34] && $groupsetting[34] > 0 ) ){
		head();
		showmsg(str_replace('{$MaxLen}',$groupsetting[34],$lang['template.strings54']),0,null,false);
		exit;
	} else{
		$message =& $GLOBALS['message'];
	}

	$BoxName = explode(',',$lang['template.strings63']);
	$SendNum = 0;
	if( is_array($incept) ){
		$count = count($incept);
		for( $i = 0 ; $i < $count ; $i++ ){
			$InceptName = '';
			if( $query = $db->query("SELECT username FROM {$dv}user WHERE username='".trim($incept[$i])."'") ){
				if( $result =& $db->fetch_row($query) ){
					$db->free_result($query);
					$InceptName = $result[0];
				}
			}
			if( $InceptName == '' ){
				head();
				showmsg($lang['template.strings35'],0,null,false);
				exit;
			}
			if( CHKHateName($InceptName) ){
				head();
				showmsg(str_replace('{$color}',$_DCACHE['stylecss'][19],str_replace('{$incept}',$InceptName,$lang['template.strings64'])),0,null,false);
				exit;
			} else{
				$title = stripslashes($title);
				$message = stripslashes($message);
				$title = $db->escape_string(cross_site_html_proc($title, $errno, $errstr));
				$message = $db->escape_string(cross_site_html_proc($message, $errno, $errstr));
				if( $GLOBALS['sms_act'] == 'Sms_Issend' ){
					$stmt = "INSERT INTO {$dv}message (incept,sender,title,content,sendtime,flag,issend) VALUES ('{$InceptName}','{$membername}','{$title}','{$message}','".TIME_NOW."',0,1)";
					$subtype = $BoxName[2];//已发送的消息
					$SendNum++;
				} elseif( $GLOBALS['sms_act'] == 'Sms_Issave' ){
					$stmt = "INSERT INTO {$dv}message (incept,sender,title,content,sendtime,flag,issend) VALUES ( '".$InceptName."','".$membername."','".$title."','".$message."','".TIME_NOW."',0,0)";
					$subtype = $BoxName[4];//发件箱
				} else{
					$stmt = "INSERT INTO {$dv}message (incept,sender,title,content,sendtime,flag,issend) VALUES ('{$InceptName}','{$membername}','{$title}','{$message}','".TIME_NOW."',0,1)";
					$subtype = $BoxName[2];//已发送的消息
					$SendNum++;
				}
				$db->query($stmt);
				UPDATE_User_Msg($InceptName);
			}
			if( $i > $groupsetting[33] - 1 ){
				head();
				showmsg(str_replace('{$Sms_MaxSend}',$groupsetting[33],$lang['template.strings55']),0,null,false);
				return;
			}
		}
		//更新用户当日发短信数据以及缓存
		if( $SendNum > 0 ){
			$iUserInfo = $UserToday[0].'|'.($UserToday[1]+$SendNum).'|'.$UserToday[2].'|'.$UserToday[3].'|'.$UserToday[4];
			set_cookie('usertoday',$iUserInfo, $GLOBALS['user_selected_cookiedate']);
			set_cookie('postdata','', $GLOBALS['user_selected_cookiedate']);
			$db->query("UPDATE {$dv}user SET usertoday='{$iUserInfo}' WHERE userid={$userid}");
		}
		setcookie('Dvbbs',$boardid);
		head();
		showmsg(str_replace('{$SmsBOX}',$subtype,$lang['template.strings38']),1,'usermain.php',0,null,false);
	}
}//end function savemsg

//读取信息
function read(){
	global $ActInfo,$stats,$UserToday,$sms_id,$fsetting,$id,$temptxt,$gStmt,$groupsetting,$lang,$dv,$db,$userid,$membername,$sms__write,$sms__reply,$sms__fw,$sms_delete;
	$id = intval($GLOBALS['id']);
	if( $id <= 0 ){
		head();
		showmsg($lang['template.strings51']);
		exit;
	}


	$action = trim($GLOBALS['action']);
	if( $action == 'read' ){
		$db->query("UPDATE {$dv}message SET flag=1 WHERE id={$id}");
		UPDATE_User_Msg($membername);
	}
	if( $query = $db->query("SELECT id,sender FROM {$dv}message WHERE incept='{$membername}' AND flag=0 AND issend=1 AND id>{$id} ORDER BY sendtime",array('pageSize'=>1)) ){
		if( $result =& $db->fetch_row($query) ){
			$db->free_result($query);
			$nextid = $result[0];
			$nextsender = $result[1];
		}
	}
	if( $query = $db->query("SELECT id,sender,incept,title,content,flag,sendtime,delr,dels,issend FROM {$dv}message WHERE (incept='{$membername}' OR sender='$membername') AND id={$id}",array('pageSize'=>1)) ){
		if( $result =& $db->fetch_array($query) ){
			$db->free_result($query);
		}
	}
	$stats = $ActInfo[1];
	if( !empty($result) && is_array($result) ){
		$incept = $result['incept'];
		$sender = $result['sender'];
		$title = htmlspecialchars($result['title']);
		$sendtime = gmdate('Y-m-d H:i:s', $result['sendtime']+getUserTimeZoneOffset());
		$content =& $result['content'];
		$read_title = $title;
		$sms_id = $id;
		$tmpuserinfo = null;
		$textarea =& Dv_CodeProcess($content, $tmpuserinfo, Ubblist($content));
		global $istoptable;
		$istoptable = 0;
		head();
		require printout('messanger.top');
		require printout('messanger.readmsg');
		require printout('messanger.footer');
		unset($result);
	} else{
		head();
		showmsg($lang['template.strings34']);
		exit;
	}
}//end function read

//保存修改
function savedit(){
	global $_SERVER,$ActInfo,$subtype,$stats,$UserToday,$sms_id,$fsetting,$id,$temptxt,$gStmt,$groupsetting,$lang,$dv,$db,$userid,$membername,$sms__write,$sms__reply,$sms__fw,$sms_delete,$userinfo;
	$joindate = get_cookie('joindate');
	empty($joindate) AND ($joindate = gmdate('Y-m-d H:i:s', $userinfo['joindate']));
	if( $groupsetting[53] > 0 && dateDiff('s',$joindate,gmdate('Y-m-d H:i:s')) < $groupsetting[53]*60 ){
		head();
		showmsg(str_replace('{$Lim_Time}',$groupsetting[53],$lang['template.strings39']));
		exit;
	}
	if( !is_numeric($GLOBALS['id']) ){
		head();
		showmsg($lang['template.strings51']);
		exit;
	}
	if( $GLOBALS['touser'] == '' ){
		head();
		showmsg($lang['template.strings35']);
		exit;
	} else{
		$incept = $GLOBALS['touser'];
	}
	if( $GLOBALS['title'] == '' || Dv_Strlen($GLOBALS['title']) > 50 ){
		head();
		showmsg($lang['template.strings53']);
		exit;
	} else{
		$title = $GLOBALS['title'];
	}
	if( $GLOBALS['message'] == '' || ( $groupsetting[34] > 0 && Dv_Strlen($GLOBALS['message']) > $groupsetting[34] ) ){
		head();
		showmsg(str_replace('{{$MaxLen}',$groupsetting[34],$lang['template.strings54']));
		exit;
	} else{
		$message =& Html2Ubb(cross_site_html_proc(stripslashes($GLOBALS['message']), $errno, $errstr));
	}
	$SendNum = 0;
	if( $query = $db->query("SELECT username FROM {$dv}user WHERE username='{$incept}'") ){
		if( $result =& $db->fetch_row($query) ){
			$db->free_result($query);
		}
	}
	if( empty($result) ){
		head();
		showmsg($lang['template.strings35']);
		exit;
	}
	$BoxName = explode(',',$lang['template.strings63']);
	$title =& cross_site_html_proc(stripslashes($title), $errno, $errstr);
	if( $GLOBALS['Submit'] == 'Sms_Issend' ){
		$db->query("UPDATE {$dv}message SET incept='{$incept}',title='{$title}',content='{$message}',sendtime='".TIME_NOW."',flag=0,issend=1 WHERE id={$id}");
		$subtype = $BoxName[4];
		$SendNum = 1;
	} else{
		$db->query("UPDATE {$dv}message SET incept='{$incept}',title='{$title}',content='{$message}',sendtime='".TIME_NOW."',flag=0,issend=0 WHERE id={$id}");
		$subtype = $BoxName[4];
	}
	//更新用户当日发短信数据以及缓存
	if( $SendNum > 0 ){
		$iUserInfo = $UserToday[0].'|'.($UserToday[1]+$SendNum).'|'.$UserToday[2].'|'.$UserToday[3].'|'.$UserToday[4];
		set_cookie('usermsg',$iUserInfo, $GLOBALS['user_selected_cookiedate']);
		$db->query("Update {$dv}user SET usertoday='{$iUserInfo}' WHERE userid={$userid}");
	}
	UPDATE_User_Msg($incept);
	UPDATE_User_Msg($membername);
	head();
	showmsg(str_replace('{$SmsBOX}',$subtype,$lang['template.strings38']),1,$_SERVER['HTTP_REFERER']);
}//end function savedit

//删除的消息将置于您的回收站
function DeleteMsg(){
	global $_SERVER,$_COOKIE,$title,$stats,$UserToday,$sms_id,$fsetting,$id,$temptxt,$gStmt,$groupsetting,$lang,$dv,$db,$userid,$membername,$sms__write,$sms__reply,$sms__fw,$sms_delete;

	if( false ){//if( IsReadonly() && !$master ){
		$istoptable = 1;
		head();
		showmsg($lang['Error.strings1']);
		exit;
	}
	$id = intval($GLOBALS['id']);
	if( $id <= 0 ){
		head();
		showmsg($lang['template.strings51']);
		exit;
	} else{
		$db->query("UPDATE {$dv}message SET delr=1 WHERE incept='{$membername}' AND id={$id}");
		$db->query("UPDATE {$dv}message SET dels=1 WHERE sender='{$membername}' AND id={$id}");
		UPDATE_User_Msg($membername);
		head();
		showmsg($lang['template.strings36'],1,$_SERVER['HTTP_REFERER']);
	}
}//end function DeleteMsg

//删除收件箱
function Delinbox(){
	global $ActInfo,$_SERVER,$_COOKIE,$title,$stats,$UserToday,$sms_id,$fsetting,$id,$temptxt,$gStmt,$groupsetting,$lang,$dv,$db,$userid,$membername,$sms__write,$sms__reply,$sms__fw,$sms_delete;

	if( false ){
		$istoptable = 1;
		head();
		showmsg($lang['Error.strings1']);
		exit;
	}
	if($GLOBALS['id']==''){
		head();
		showmsg($lang['template.strings51']);
		exit;
	}
	else{
		$idList = @implode(',',$GLOBALS['id']);
	}
	$stats = $ActInfo[5];
	if( $id <= 0 ){
		head();
		showmsg($lang['template.strings51']);
		exit;
	} else{
		$db->query("UPDATE {$dv}message SET delr=1 WHERE incept='{$membername}' AND id IN ({$idList})");
		UPDATE_User_Msg($membername);
		head();
		showmsg($lang['template.strings36'],1,$_SERVER['HTTP_REFERER']);
	}
}//end function Delinbox

//清空收件箱
function AllDelinbox(){
	global $ActInfo,$_SERVER,$_COOKIE,$title,$stats,$UserToday,$sms_id,$fsetting,$id,$temptxt,$gStmt,$groupsetting,$lang,$dv,$db,$userid,$membername,$sms__write,$sms__reply,$sms__fw,$sms_delete;

	if( false ){
		$istoptable = 1;
		head();
		showmsg($lang['Error.strings1']);
		exit;
	}
	$stats = $ActInfo[6];
	$db->query("UPDATE {$dv}message SET delr=1 WHERE incept='{$membername}' AND delr=0");
	UPDATE_User_Msg($membername);
	head();
	showmsg($lang['template.strings36'],1,$_SERVER['HTTP_REFERER']);
}//end function AllDelinbox

//删除草稿箱
function Deloutbox(){
	global $ActInfo,$_SERVER,$_COOKIE,$title,$stats,$UserToday,$sms_id,$fsetting,$id,$temptxt,$gStmt,$groupsetting,$lang,$dv,$db,$userid,$membername,$sms__write,$sms__reply,$sms__fw,$sms_delete;
	if($GLOBALS['id']==''){
		head();
		showmsg($lang['template.strings51']);
		exit;
	}
	else{
		$idList = implode(',',$GLOBALS['id']);
	}
	$stats = $ActInfo[7];
	$db->query("UPDATE {$dv}message SET dels=1 WHERE sender='{$membername}' AND issend=0 AND id IN ({$idList})");
	UPDATE_User_Msg($membername);
	head();
	showmsg($lang['template.strings36'],1,$_SERVER['HTTP_REFERER']);
}//end function Deloutbox

//清空草稿箱
function AllDeloutbox(){
	global $ActInfo,$_SERVER,$_COOKIE,$title,$stats,$UserToday,$sms_id,$fsetting,$id,$temptxt,$gStmt,$groupsetting,$lang,$dv,$db,$userid,$membername,$sms__write,$sms__reply,$sms__fw,$sms_delete;

	$stats = $ActInfo[8];
	$db->query("UPDATE {$dv}message SET dels=1 WHERE sender='{$membername}' AND dels=0 AND issend=0");
	UPDATE_User_Msg($membername);
	head();
	showmsg($lang['template.strings36'],1,$_SERVER['HTTP_REFERER']);
}//end function AllDeloutbox

/**
 * 已发送逻辑删除，置于回收站，入口字段DelS，可用于批量及单个删除
 * DelS：0未操作，1发送者删除，2发送者从回收站删除
 */
function Delissend(){
	global $ActInfo,$_SERVER,$_COOKIE,$title,$stats,$UserToday,$sms_id,$fsetting,$id,$temptxt,$gStmt,$groupsetting,$lang,$dv,$db,$userid,$membername,$sms__write,$sms__reply,$sms__fw,$sms_delete;

	if($GLOBALS['id']==''){
		head();
		showmsg($lang['template.strings51']);
		exit;
	}
	else{
		$idList = implode(',',$GLOBALS['id']);
	}
	$stats = $ActInfo[9];
	$db->query("UPDATE {$dv}message SET dels=1 WHERE sender='{$membername}' AND issend=1 AND id IN ({$idList})");
	UPDATE_User_Msg($membername);
	head();
	showmsg($lang['template.strings36'],1,$_SERVER['HTTP_REFERER']);
}//end function Delissend

//将已发送的短信移到回收站。
function AllDelissend(){
	global $ActInfo,$_SERVER,$_COOKIE,$title,$stats,$UserToday,$sms_id,$fsetting,$id,$temptxt,$gStmt,$groupsetting,$lang,$dv,$db,$userid,$membername,$sms__write,$sms__reply,$sms__fw,$sms_delete;

	if( false ){
		$istoptable = 1;
		head();
		showmsg($lang['Error.strings1']);
		exit;
	}
	if($GLOBALS['id']==''){
		head();
		showmsg($lang['template.strings51']);
		exit;
	}
	else{
		$idList = implode(',',$GLOBALS['id']);
	}
	$stats = $ActInfo[10];
	$db->query("UPDATE {$dv}message SET dels=1 WHERE sender='{$membername}' AND dels=0 AND issend=1");
	UPDATE_User_Msg($membername);
	head();
	showmsg($lang['template.strings36'],1,$_SERVER['HTTP_REFERER']);
}//end function AllDelissend

//用户能完全删除收到信息和逻辑删除所发送信息，逻辑删除所发送信息设置入口字段DelS参数为2
function Delrecycle(){
	global $ActInfo,$_SERVER,$_COOKIE,$title,$stats,$UserToday,$sms_id,$fsetting,$id,$temptxt,$gStmt,$groupsetting,$lang,$dv,$db,$userid,$membername,$sms__write,$sms__reply,$sms__fw,$sms_delete;

	if( false ){
		$istoptable = 1;
		head();
		showmsg($lang['Error.strings1']);
		exit;
	}
	if($GLOBALS['id']==''){
		head();
		showmsg($lang['template.strings51']);
		exit;
	}
	else{
		if (!empty($GLOBALS['id']) AND is_array($GLOBALS['id'])) {
			$idList = implode(',', $GLOBALS['id']);
		}
	}
	$stats = $ActInfo[11];
	$db->query("DELETE FROM {$dv}message WHERE incept='{$membername}' AND delr=1 AND id IN({$idList})");
	$db->query("UPDATE {$dv}message SET dels=2 WHERE sender='{$membername}' AND dels=1 AND id IN({$idList})");
	UPDATE_User_Msg($membername);
	head();
	showmsg($lang['template.strings37'],1,$_SERVER['HTTP_REFERER']);
}//end function AllDelissend


/**
 * 收信人回收站： incept=收信人 DelR=1
 * 发信人回收站： sender=收信人 DelS=2
 * 清空及删除回收站记录，将不在回收站的记录放到回收站内
 */
function AllDelrecycle(){
	global $ActInfo,$_SERVER,$_COOKIE,$title,$stats,$UserToday,$sms_id,$fsetting,$id,$temptxt,$gStmt,$groupsetting,$lang,$dv,$db,$userid,$membername,$sms__write,$sms__reply,$sms__fw,$sms_delete;

	$stats = $ActInfo[12];
	$db->query("DELETE FROM {$dv}message WHERE incept='{$membername}' AND delr=1");
	$db->query("UPDATE {$dv}message SET dels=2 WHERE sender='{$membername}' AND dels=1");
	UPDATE_User_Msg($membername);
	head();
	showmsg($lang['template.strings37'],1,$_SERVER['HTTP_REFERER']);
}//end function AllDelrecycle

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 CHKHateName($name){
	if( $name == '' ) return false;
	global $membername,$userid,$dv,$db;
	$stmt = "SELECT f_friend FROM {$dv}friend WHERE (f_userid={$userid} OR f_username='{$name}') AND f_mod=2";
	if( $query = $db->query($stmt,array('absolutePage'=>1)) ){
		if( $result =& $db->fetch_row($query) ){
			$db->free_result($query);
		}
	}
	if( !empty($result) && is_array($result) ){
		if( strpos($result[0],$name) !== false || strpos($result[0],$membername) !== false ) return true;
		else return false;
	} else{
		return false;
	}
}//end function CHKHateName

//更新用户短信通知信息（新短信条数||新短讯ID||发信人名）
function UPDATE_User_Msg($username){
	global $dv,$db,$membername;
	$newmsg = newincept($username);
	if( $newmsg > 0 ){
		$msginfo = $newmsg.'||'.inceptid(1,$username).'||'.inceptid(2,$username);
	} else{
		$msginfo = "0||0||null";
	}
	$db->query("UPDATE {$dv}user SET usermsg='".addslashes($msginfo)."' WHERE username='{$username}'");
	if( $username == $membername ){
		set_cookie('usermsg',$msginfo, $GLOBALS['user_selected_cookiedate']);
	}
}//end function UPDATE_User_Msg

//统计留言
function newincept($iusername){
	global $dv,$db;
	$newincept = intval($db->scalar("SELECT COUNT(id) FROM {$dv}message WHERE flag=0 AND issend=1 AND delr=0 AND incept='{$iusername}'"));
	return $newincept;
}//end function

function inceptid($stype,$iusername){
	global $dv,$db;
	if( $query = $db->query("SELECT id,sender FROM {$dv}message WHERE flag=0 AND issend=1 AND delr=0 AND incept='{$iusername}'") ){
		$result =& $db->fetch_row($query);
		$db->free_result($query);
	}
	if( !empty($result) && is_array($result) ){
		if( $stype = 1 ){
			$inceptid = $result[0];
		} else{
			$inceptid = $result[1];
		}
	} else{
		if( $stype = 1 ){
			$inceptid = 0;
		} else{
			$inceptid = 'null';
		}
	}
	return $inceptid;
}//end function

//发帖时用，为了减少入库量
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

?>