<?php
define('THIS_SCRIPT', 'usersms');
require_once 'global.php';
$lang = load_lang($lang, 'usermanager' );
$stats = $lang['SmsHeaderTitle'];
$arrNavMenu = array(
	0=>array('linktitle'=>$lang['HeaderTitle1'],'linkurl'=>'usermain.php')
	,1=>array('linktitle'=>$stats)
);
LoadStyleJs();
PageLoad();
LoadFooter();

function PageLoad(){
	global $fsetting,$groupsetting,$membername,$userid,$db,$lang,$imgurl;
	if( intval($userid) <= 0 ){
		showmsg($lang['MustLogin']);
		exit;
	}
	if( $groupsetting[16] == 0 ){
		showmsg($lang['error.sms.strings1']);
	}
	require printout('usermanager.navmenu');
	MainFunc();
}//end function PageLoad

function MainFunc(){
	global $membername,$smstype,$fsetting,$groupsetting,$userid,$dv,$lang,$db,$_DCACHE,$userinfo,$imgurl;

	$iMagicFace =  explode('|',$userinfo['userface']);
	if(!empty($iMagicFace[1])){
		$UserFace = "<img src=\"".Dv_FilterJS($iMagicFace[1])."\" width=\"".$userinfo['userwidth']."\" height=\"".$userinfo['userheight']."\" alt=\"\">";
	}
	elseif(!empty($iMagicFace[0])){
		$UserFace = "<img src=\"".Dv_FilterJS($iMagicFace[0])."\" width=\"".$userinfo['userwidth']."\" height=\"".$userinfo['userheight']."\" alt=\"\">";
	}
	else{
		$UserFace = "<img src=\"".$userinfo['userface']."\" width=\"".$userinfo['userwidth']."\" height=\"".$userinfo['userheight']."\" alt=\"\">";			
	}
	$TableWidth =& $_DCACHE['stylecss'][1];
	$color =& $_DCACHE['stylecss'][19];
	$user_Article =& $userinfo['userpost'];
	$user_Group =& $userinfo['userclass'];
	$user_Face_left = $UserFace;
	$user_Title =& $userinfo['usertitle'];
	$user_Wealth =& $userinfo['userwealth'];
	$user_EP =& $userinfo['userep'];
	$user_CP =& $userinfo['usercp'];
	$user_Money =& $userinfo['usermoney'];
	$user_Ticket =& $userinfo['userticket'];
	$user_IsBest =& $userinfo['userisbest'];
	$user_AddDate =& $userinfo['joindate'];
	$user_Logins =& $userinfo['userlogins'];
	$username =& $userinfo['username'];
	$msg_newincept = intval($userinfo['uermsg']);

	//用户组短信限制，条数
	$Sms_max=intval($groupsetting[35]);
	if( $Sms_max == 0 ) $Sms_Max = 9999;
	$SmsCount = 0;
	$boxinfo = explode(',',$lang['tpl.sms.strings63']);
	$query = $db->query("SELECT COUNT(id) FROM {$dv}message WHERE incept='{$membername}'");
	$result = $db->fetch_row($query);
	$SmsCount = intval($result[0]);
	//以下判断为自动删除多出来的短消息
	if( $SmsCount > $Sms_max ){
		$i = $SmsCount - $Sms_max;
		if( $query = $db->query("SELECT id FROM {$dv}message WHERE incept='{$membername}' ORDER BY id,delr DESC",array('pageSize'=>$i)) ){
			while( $result = $db->fetch_row($query) ){
				$msgIDList[] = $result[0];
			}
			if( !empty($msgIDList) && is_array($msgIDList) ){
				$db->free_result($query);
				$db->query("DELETE FROM {$dv}message WHERE id IN(".implode(',',$msgIDList).")");
			}
			$SmsCount = $Sms_max;
		}
	}
	$stmt = "SELECT id,sender,incept,title,content,flag,sendtime,delr,dels,issend FROM {$dv}message ";
	switch($GLOBALS['action']){
		case 'outbox':
			$Sms_inbox = $imgurl.$lang['tpl.sms.pic0'];
			$Sms_outbox = $imgurl.$lang['tpl.sms.pic20'];
			$Sms_issend = $imgurl.$lang['tpl.sms.pic2'];
			$Sms_recycle = $imgurl.$lang['tpl.sms.pic3'];
			$boxname = $boxinfo[1];//草稿箱
			$smstype = "outbox";
			$readaction = "edit";
			$turl = "sms";
			$subStmt = " WHERE sender='{$membername}' AND issend=0 AND dels=0 ORDER BY id DESC ";
			break;
		case 'issend':
			$Sms_inbox = $imgurl.$lang['tpl.sms.pic0'];
			$Sms_outbox = $imgurl.$lang['tpl.sms.pic1'];
			$Sms_issend = $imgurl.$lang['tpl.sms.pic21'];
			$Sms_recycle = $imgurl.$lang['tpl.sms.pic3'];
			$boxname = $boxinfo[2];//已发送的消息
			$smstype = "issend";
			$readaction = "outread";
			$turl = "readsms";
			$subStmt = " WHERE sender='{$membername}' AND issend=1 AND dels=0 ORDER BY id DESC ";
			break;
		case 'recycle':
			$Sms_inbox = $imgurl.$lang['tpl.sms.pic0'];
			$Sms_outbox = $imgurl.$lang['tpl.sms.pic1'];
			$Sms_issend = $imgurl.$lang['tpl.sms.pic2'];
			$Sms_recycle = $imgurl.$lang['tpl.sms.pic22'];
			$boxname = $boxinfo[3];//垃圾箱
			$smstype = "recycle";
			$readaction = "read";
			$turl = "readsms";
			$subStmt = " WHERE ((sender='".trim($membername)."' AND dels=1) OR (incept='".trim($membername)."' AND delr=1)) AND dels<>2 ORDER BY id DESC ";
			break;
		case 'inbox':
		default:
			$Sms_inbox = $imgurl.$lang['tpl.sms.pic19'];
			$Sms_outbox = $imgurl.$lang['tpl.sms.pic1'];
			$Sms_issend = $imgurl.$lang['tpl.sms.pic2'];
			$Sms_recycle = $imgurl.$lang['tpl.sms.pic3'];
			$boxname = $boxinfo[0];//收件箱
			$smstype = "inbox";
			$readaction = "read";
			$turl = "readsms";
			$subStmt = " WHERE issend=1 AND delr=0 AND incept='{$membername}' ORDER BY flag,id DESC";
			break;
	}
	$Sms_address = $imgurl.$lang['tpl.sms.pic4'];
	$Sms_write = $imgurl.$lang['tpl.sms.pic5'];
	$Sms_issend_1 = $imgurl.$lang['tpl.sms.pic9'];
	$Sms_issend_2 = $imgurl.$lang['tpl.sms.pic10'];
	$Sms_news = $imgurl.$lang['tpl.sms.pic11'];
	$Sms_olds = $imgurl.$lang['tpl.sms.pic12'];

	$PageListNum = intval($fsetting['otherpagesize']);
	if ($PageListNum < 1) {
		$PageListNum = 30;
	}
	$CurrentPage = empty($GLOBALS['page']) ? 1 : intval($GLOBALS['page']);

	$scalar = $db->scalar("SELECT COUNT(1) FROM {$dv}message {$subStmt}");
	$totalrec = ( $scalar !== null ? intval($scalar) : 0 );
	$dbResults = array();
	if( $query = $db->query($stmt.$subStmt,array('absolutePage'=>$CurrentPage,'pageSize'=>$PageListNum)) ){
		$page_count = 0;
		while( $result = $db->fetch_array($query) ){
			$page_count++;
			$result['title'] = ChkBadWords($result['title']);
			$result['title'] = preg_replace("/\<(.+?)\>/is", "", $result['title']);
			$dbResults[] = $result;
		}
		$db->free_result($query);
	}
	$PageNum = $PageListNum;
	$Pcount = ceil((float)$totalrec / (float)$PageListNum);
	if( $page_count == 0 ) $currentpage = 0;
	$colSpan = 6;
	$SearchStr = "?action={$GLOBALS['action']}";
	$redcolor = $_DCACHE['stylecss'][19];
	require printout('usersms');
}//end function MainFunc

?>