<?php
/*
	[Office 515158] (C) 2009-2014 天生创想 Inc.
	$Id: mod_sms mobile $
*/
(!defined('IN_TOA') || !defined('IN_ADMIN')) && exit('Access Denied!');
empty($do) && $do = 'list';
require('function/_sms.class.php');
if ($do == 'list') {
	$userview=mob_sms_receive($_USER->id);
	if(trim($userview)!=''){
		$num = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE."sms_receive where id in(".$userview.") ORDER BY date desc");
		$sql = "SELECT * FROM ".DB_TABLEPRE."sms_receive where id in(".$userview.") ORDER BY date desc";
		$result = $db->fetch_all($sql);
	}else{
		$num =0;
	}
	include_once('template/list.php');

}elseif ($do == 'user') {
	$sql = "SELECT a.id,b.name,b.pic FROM ".DB_TABLEPRE."user a,".DB_TABLEPRE."user_view b where a.id=b.uid ORDER BY a.numbers asc";
	$result = $db->fetch_all($sql);
	include_once('template/user.php');
}elseif ($do == 'views') {
	$receiveperson = getGP('receiveperson','G','int');
	$num = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE."sms_receive where (sendperson='".$_USER->id."' and receiveperson='".$receiveperson."') or (receiveperson='".$_USER->id."' and sendperson='".$receiveperson."') ORDER BY id desc");
	$sql = "SELECT * FROM ".DB_TABLEPRE."sms_receive where (sendperson='".$_USER->id."' and receiveperson='".$receiveperson."') or (receiveperson='".$_USER->id."' and sendperson='".$receiveperson."') ORDER BY id desc";
	$result = $db->fetch_all($sql);
	include_once('template/views.php');
}elseif ($do == 'add') {
	$receiveperson = getGP('receiveperson','G','int');
	//发送消息表
	$sms_send = array(
		'receiveperson' => get_realname($receiveperson),
		'content' => getGP('content','G'),
		'uid' => $_USER->id,
		'date' => get_date('Y-m-d H:i:s',PHP_TIME)
	);
	insert_db('sms_send',$sms_send);
	$id=$db->insert_id();
	$sms_receive = array(
		'sendperson' => $_USER->id,
		'date' => get_date('Y-m-d H:i:s',PHP_TIME),
		'content' => getGP('content','G'),
		'receiveperson' => $receiveperson,
		'type' => '2',
		'smskey' => '1',
		'sendid'=>$id
	);
	//接收消息表
	insert_db('sms_receive',$sms_receive);
	weixinsms(get_realname($receiveperson),trim(getGP('content','G')));
	//echo $receiveperson.'fdsa'.$_GET['content'];
	//exit;
}elseif ($do == 'ajax') {
	$receiveperson = getGP('receiveperson','G','int');
	$num = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE."sms_receive where (sendperson='".$_USER->id."' and receiveperson='".$receiveperson."') or (receiveperson='".$_USER->id."' and sendperson='".$receiveperson."') ORDER BY id desc");
	$sql = "SELECT * FROM ".DB_TABLEPRE."sms_receive where (sendperson='".$_USER->id."' and receiveperson='".$receiveperson."') or (receiveperson='".$_USER->id."' and sendperson='".$receiveperson."') ORDER BY id desc";
	$result = $db->fetch_all($sql);
	$i=0;
	foreach ($result as $row) {
		$content=explode('<a',$row['content']); 
		$content=preg_replace("/\s+/", " ",trim($content[0]));
		$content=str_replace('"', "",$content);
		$content=str_replace("'", "",$content);
		$content=str_replace(",", "，",$content);
		$content=str_replace("<br>", "",$content);
		$i++;
		if(trim($row['sendperson'])==trim($receiveperson)){
			$nums=1;
		}else{
			$nums=2;
		}
		echo '<div id="wraper'.$nums.'">';
		echo '<div id="wraper'.$nums.'font">';
		echo '<div class="bubble">';
		echo Sms_Date(strtotime($row['date']));
		echo '<div class="content">'.trim($content).'</div>';
		echo '</div>';
		echo '</div><span class="span">'.trim(get_realname($row['sendperson'])).'</span>';
		echo '</div>';
	}
}
?>